smbfs_vfsops.c revision 75374
1/* 2 * Copyright (c) 2000-2001, Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Boris Popov. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD: head/sys/fs/smbfs/smbfs_vfsops.c 75374 2001-04-10 07:59:06Z bp $ 33 */ 34#include "opt_netsmb.h" 35#ifndef NETSMB 36#error "SMBFS requires option NETSMB" 37#endif 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/proc.h> 42#include <sys/bio.h> 43#include <sys/buf.h> 44#include <sys/kernel.h> 45#include <sys/sysctl.h> 46#include <sys/vnode.h> 47#include <sys/mount.h> 48#include <sys/stat.h> 49#include <sys/malloc.h> 50 51 52#include <netsmb/smb.h> 53#include <netsmb/smb_conn.h> 54#include <netsmb/smb_subr.h> 55#include <netsmb/smb_dev.h> 56 57#include <fs/smbfs/smbfs.h> 58#include <fs/smbfs/smbfs_node.h> 59#include <fs/smbfs/smbfs_subr.h> 60 61int smbfs_debuglevel = 0; 62 63static int smbfs_version = SMBFS_VERSION; 64 65#ifdef SMBFS_USEZONE 66#include <vm/vm.h> 67#include <vm/vm_extern.h> 68#include <vm/vm_zone.h> 69 70vm_zone_t smbfsmount_zone; 71#endif 72 73SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system"); 74SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 75SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 76 77static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table"); 78 79 80static int smbfs_mount(struct mount *, char *, caddr_t, 81 struct nameidata *, struct proc *); 82static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *); 83static int smbfs_root(struct mount *, struct vnode **); 84static int smbfs_start(struct mount *, int, struct proc *); 85static int smbfs_statfs(struct mount *, struct statfs *, struct proc *); 86static int smbfs_sync(struct mount *, int, struct ucred *, struct proc *); 87static int smbfs_unmount(struct mount *, int, struct proc *); 88static int smbfs_init(struct vfsconf *vfsp); 89static int smbfs_uninit(struct vfsconf *vfsp); 90 91#if __FreeBSD_version < 400009 92static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp); 93static int smbfs_fhtovp(struct mount *, struct fid *, 94 struct sockaddr *, struct vnode **, int *, 95 struct ucred **); 96static int smbfs_vptofh(struct vnode *, struct fid *); 97#endif 98 99static struct vfsops smbfs_vfsops = { 100 smbfs_mount, 101 smbfs_start, 102 smbfs_unmount, 103 smbfs_root, 104 smbfs_quotactl, 105 smbfs_statfs, 106 smbfs_sync, 107#if __FreeBSD_version > 400008 108 vfs_stdvget, 109 vfs_stdfhtovp, /* shouldn't happen */ 110 vfs_stdcheckexp, 111 vfs_stdvptofh, /* shouldn't happen */ 112#else 113 smbfs_vget, 114 smbfs_fhtovp, 115 smbfs_vptofh, 116#endif 117 smbfs_init, 118 smbfs_uninit, 119#ifndef FB_RELENG3 120 vfs_stdextattrctl, 121#else 122#define M_USE_RESERVE M_KERNEL 123 &sysctl___vfs_smbfs 124#endif 125}; 126 127 128VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 129 130MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 131MODULE_DEPEND(smbfs, libiconv, 1, 1, 1); 132 133int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 134 135static int 136smbfs_mount(struct mount *mp, char *path, caddr_t data, 137 struct nameidata *ndp, struct proc *p) 138{ 139 struct smbfs_args args; /* will hold data from mount request */ 140 struct smbmount *smp = NULL; 141 struct smb_vc *vcp; 142 struct smb_share *ssp = NULL; 143 struct vnode *vp; 144 struct smb_cred scred; 145#ifndef FB_CURRENT 146 size_t size; 147#endif 148 int error; 149 char *pc, *pe; 150 151 if (data == NULL) { 152 printf("missing data argument\n"); 153 return EINVAL; 154 } 155 if (mp->mnt_flag & MNT_UPDATE) { 156 printf("MNT_UPDATE not implemented"); 157 return EOPNOTSUPP; 158 } 159 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args)); 160 if (error) 161 return error; 162 if (args.version != SMBFS_VERSION) { 163 printf("mount version mismatch: kernel=%d, mount=%d\n", 164 SMBFS_VERSION, args.version); 165 return EINVAL; 166 } 167 smb_makescred(&scred, p, p->p_ucred); 168 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp); 169 if (error) { 170 printf("invalid device handle %d (%d)\n", args.dev, error); 171 return error; 172 } 173 vcp = SSTOVC(ssp); 174 smb_share_unlock(ssp, 0, p); 175 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 176 177#ifdef SMBFS_USEZONE 178 smp = zalloc(smbfsmount_zone); 179#else 180 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_USE_RESERVE); 181#endif 182 if (smp == NULL) { 183 printf("could not alloc smbmount\n"); 184 error = ENOMEM; 185 goto bad; 186 } 187 bzero(smp, sizeof(*smp)); 188 mp->mnt_data = (qaddr_t)smp; 189 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 190 if (smp->sm_hash == NULL) 191 goto bad; 192 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 193 smp->sm_share = ssp; 194 smp->sm_root = NULL; 195 smp->sm_args = args; 196 smp->sm_caseopt = args.caseopt; 197 smp->sm_args.file_mode = (smp->sm_args.file_mode & 198 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 199 smp->sm_args.dir_mode = (smp->sm_args.dir_mode & 200 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 201 202/* simple_lock_init(&smp->sm_npslock);*/ 203#ifndef FB_CURRENT 204 error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 205 if (error) 206 goto bad; 207 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 208#endif 209 pc = mp->mnt_stat.f_mntfromname; 210 pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 211 bzero(pc, MNAMELEN); 212 *pc++ = '/'; 213 *pc++ = '/'; 214 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 215 if (pc < pe-1) { 216 *(pc++) = '@'; 217 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 218 if (pc < pe - 1) { 219 *(pc++) = '/'; 220 strncpy(pc, ssp->ss_name, pe - pc - 2); 221 } 222 } 223 /* protect against invalid mount points */ 224 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0'; 225 vfs_getnewfsid(mp); 226 error = smbfs_root(mp, &vp); 227 if (error) 228 goto bad; 229 VOP_UNLOCK(vp, 0, p); 230 SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount); 231 232#ifdef DIAGNOSTICS 233 SMBERROR("mp=%p\n", mp); 234#endif 235 return error; 236bad: 237 if (smp) { 238 if (smp->sm_hash) 239 free(smp->sm_hash, M_SMBFSHASH); 240 lockdestroy(&smp->sm_hashlock); 241#ifdef SMBFS_USEZONE 242 zfree(smbfsmount_zone, smp); 243#else 244 free(smp, M_SMBFSDATA); 245#endif 246 } 247 if (ssp) 248 smb_share_put(ssp, &scred); 249 return error; 250} 251 252/* Unmount the filesystem described by mp. */ 253static int 254smbfs_unmount(struct mount *mp, int mntflags, struct proc *p) 255{ 256 struct smbmount *smp = VFSTOSMBFS(mp); 257 struct vnode *vp; 258 struct smb_cred scred; 259 int error, flags; 260 261 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 262 flags = 0; 263 if (mntflags & MNT_FORCE) 264 flags |= FORCECLOSE; 265 error = VFS_ROOT(mp, &vp); 266 if (error) 267 return (error); 268 if (vp->v_usecount > 2) { 269 printf("smbfs_unmount: usecnt=%d\n", vp->v_usecount); 270 vput(vp); 271 return EBUSY; 272 } 273 error = vflush(mp, vp, flags); 274 if (error) { 275 vput(vp); 276 return error; 277 } 278 vput(vp); 279 vrele(vp); 280 vgone(vp); 281 smb_makescred(&scred, p, p->p_ucred); 282 smb_share_put(smp->sm_share, &scred); 283 mp->mnt_data = (qaddr_t)0; 284 285 if (smp->sm_hash) 286 free(smp->sm_hash, M_SMBFSHASH); 287 lockdestroy(&smp->sm_hashlock); 288#ifdef SMBFS_USEZONE 289 zfree(smbfsmount_zone, smp); 290#else 291 free(smp, M_SMBFSDATA); 292#endif 293 mp->mnt_flag &= ~MNT_LOCAL; 294 return error; 295} 296 297/* 298 * Return locked root vnode of a filesystem 299 */ 300static int 301smbfs_root(struct mount *mp, struct vnode **vpp) 302{ 303 struct smbmount *smp = VFSTOSMBFS(mp); 304 struct vnode *vp; 305 struct smbnode *np; 306 struct smbfattr fattr; 307 struct proc *p = curproc; 308 struct ucred *cred = p->p_ucred; 309 struct smb_cred scred; 310 int error; 311 312 if (smp == NULL) { 313 SMBERROR("smp == NULL (bug in umount)\n"); 314 return EINVAL; 315 } 316 if (smp->sm_root) { 317 *vpp = SMBTOV(smp->sm_root); 318 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, p); 319 } 320 smb_makescred(&scred, p, cred); 321 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 322 if (error) 323 return error; 324 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 325 if (error) 326 return error; 327 vp->v_flag |= VROOT; 328 np = VTOSMB(vp); 329 smp->sm_root = np; 330 *vpp = vp; 331 return 0; 332} 333 334/* 335 * Vfs start routine, a no-op. 336 */ 337/* ARGSUSED */ 338static int 339smbfs_start(mp, flags, p) 340 struct mount *mp; 341 int flags; 342 struct proc *p; 343{ 344 SMBVDEBUG("flags=%04x\n", flags); 345 return 0; 346} 347 348/* 349 * Do operations associated with quotas, not supported 350 */ 351/* ARGSUSED */ 352static int 353smbfs_quotactl(mp, cmd, uid, arg, p) 354 struct mount *mp; 355 int cmd; 356 uid_t uid; 357 caddr_t arg; 358 struct proc *p; 359{ 360 SMBVDEBUG("return EOPNOTSUPP\n"); 361 return EOPNOTSUPP; 362} 363 364/*ARGSUSED*/ 365int 366smbfs_init(struct vfsconf *vfsp) 367{ 368#ifndef SMP 369 int name[2]; 370 int olen, ncpu, plen, error; 371 372 name[0] = CTL_HW; 373 name[1] = HW_NCPU; 374 error = kernel_sysctl(curproc, name, 2, &ncpu, &olen, NULL, 0, &plen); 375 if (error == 0 && ncpu > 1) 376 printf("warning: smbfs module compiled without SMP support."); 377#endif 378 379#ifdef SMBFS_USEZONE 380 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 381#endif 382 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 383 SMBVDEBUG("done.\n"); 384 return 0; 385} 386 387/*ARGSUSED*/ 388int 389smbfs_uninit(struct vfsconf *vfsp) 390{ 391 392 SMBVDEBUG("done.\n"); 393 return 0; 394} 395 396/* 397 * smbfs_statfs call 398 */ 399int 400smbfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 401{ 402 struct smbmount *smp = VFSTOSMBFS(mp); 403 struct smbnode *np = smp->sm_root; 404 struct smb_share *ssp = smp->sm_share; 405 struct smb_cred scred; 406 int error = 0; 407 408 if (np == NULL) 409 return EINVAL; 410 411 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 412 sbp->f_spare2 = 0; /* placeholder */ 413 smb_makescred(&scred, p, p->p_ucred); 414 415 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 416 error = smbfs_smb_statfs2(ssp, sbp, &scred); 417 else 418 error = smbfs_smb_statfs(ssp, sbp, &scred); 419 if (error) 420 return error; 421 sbp->f_flags = 0; /* copy of mount exported flags */ 422 if (sbp != &mp->mnt_stat) { 423 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */ 424 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */ 425 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */ 426 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 427 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 428 } 429 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 430 return 0; 431} 432 433/* 434 * Flush out the buffer cache 435 */ 436/* ARGSUSED */ 437static int 438smbfs_sync(mp, waitfor, cred, p) 439 struct mount *mp; 440 int waitfor; 441 struct ucred *cred; 442 struct proc *p; 443{ 444 struct vnode *vp; 445 int error, allerror = 0; 446 /* 447 * Force stale buffer cache information to be flushed. 448 */ 449loop: 450 for (vp = mp->mnt_vnodelist.lh_first; 451 vp != NULL; 452 vp = vp->v_mntvnodes.le_next) { 453 /* 454 * If the vnode that we are about to sync is no longer 455 * associated with this mount point, start over. 456 */ 457 if (vp->v_mount != mp) 458 goto loop; 459#ifndef FB_RELENG3 460 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 461#else 462 if (VOP_ISLOCKED(vp) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 463#endif 464 waitfor == MNT_LAZY) 465 continue; 466 if (vget(vp, LK_EXCLUSIVE, p)) 467 goto loop; 468 error = VOP_FSYNC(vp, cred, waitfor, p); 469 if (error) 470 allerror = error; 471 vput(vp); 472 } 473 return (allerror); 474} 475 476#if __FreeBSD_version < 400009 477/* 478 * smbfs flat namespace lookup. Unsupported. 479 */ 480/* ARGSUSED */ 481static int smbfs_vget(mp, ino, vpp) 482 struct mount *mp; 483 ino_t ino; 484 struct vnode **vpp; 485{ 486 return (EOPNOTSUPP); 487} 488 489/* ARGSUSED */ 490static int smbfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 491 struct mount *mp; 492 struct fid *fhp; 493 struct sockaddr *nam; 494 struct vnode **vpp; 495 int *exflagsp; 496 struct ucred **credanonp; 497{ 498 return (EINVAL); 499} 500 501/* 502 * Vnode pointer to File handle, should never happen either 503 */ 504/* ARGSUSED */ 505static int 506smbfs_vptofh(vp, fhp) 507 struct vnode *vp; 508 struct fid *fhp; 509{ 510 return (EINVAL); 511} 512 513#endif /* __FreeBSD_version < 400009 */ 514