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: src/sys/fs/smbfs/smbfs_vfsops.c,v 1.2.2.5 2003/01/17 08:20:26 tjr Exp $ 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/kernel.h> 43 #include <sys/sysctl.h> 44 #include <sys/vnode.h> 45 #include <sys/mount.h> 46 #include <sys/stat.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 50 51 #include <netsmb/smb.h> 52 #include <netsmb/smb_conn.h> 53 #include <netsmb/smb_subr.h> 54 #include <netsmb/smb_dev.h> 55 56 #include <fs/smbfs/smbfs.h> 57 #include <fs/smbfs/smbfs_node.h> 58 #include <fs/smbfs/smbfs_subr.h> 59 60 #include <sys/buf.h> 61 62 int smbfs_debuglevel = 0; 63 64 static int smbfs_version = SMBFS_VERSION; 65 66 #ifdef SMBFS_USEZONE 67 #include <vm/vm.h> 68 #include <vm/vm_extern.h> 69 #include <vm/vm_zone.h> 70 71 vm_zone_t smbfsmount_zone; 72 #endif 73 74 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system"); 75 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 76 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 77 78 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table"); 79 80 81 static int smbfs_mount(struct mount *, char *, caddr_t, 82 struct nameidata *, struct proc *); 83 static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct proc *); 84 static int smbfs_root(struct mount *, struct vnode **); 85 static int smbfs_start(struct mount *, int, struct proc *); 86 static int smbfs_statfs(struct mount *, struct statfs *, struct proc *); 87 static int smbfs_sync(struct mount *, int, struct ucred *, struct proc *); 88 static int smbfs_unmount(struct mount *, int, struct proc *); 89 static int smbfs_init(struct vfsconf *vfsp); 90 static int smbfs_uninit(struct vfsconf *vfsp); 91 92 #if __FreeBSD_version < 400009 93 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp); 94 static int smbfs_fhtovp(struct mount *, struct fid *, 95 struct sockaddr *, struct vnode **, int *, 96 struct ucred **); 97 static int smbfs_vptofh(struct vnode *, struct fid *); 98 #endif 99 100 static struct vfsops smbfs_vfsops = { 101 smbfs_mount, 102 smbfs_start, 103 smbfs_unmount, 104 smbfs_root, 105 smbfs_quotactl, 106 smbfs_statfs, 107 smbfs_sync, 108 #if __FreeBSD_version > 400008 109 vfs_stdvget, 110 vfs_stdfhtovp, /* shouldn't happen */ 111 vfs_stdcheckexp, 112 vfs_stdvptofh, /* shouldn't happen */ 113 #else 114 smbfs_vget, 115 smbfs_fhtovp, 116 smbfs_vptofh, 117 #endif 118 smbfs_init, 119 smbfs_uninit, 120 vfs_stdextattrctl 121 }; 122 123 124 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 125 126 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 127 MODULE_DEPEND(smbfs, libiconv, 1, 1, 1); 128 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 129 130 int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 131 132 static int 133 smbfs_mount(struct mount *mp, char *path, caddr_t data, 134 struct nameidata *ndp, struct proc *p) 135 { 136 struct smbfs_args args; /* will hold data from mount request */ 137 struct smbmount *smp = NULL; 138 struct smb_vc *vcp; 139 struct smb_share *ssp = NULL; 140 struct vnode *vp; 141 struct smb_cred scred; 142 size_t size; 143 int error; 144 char *pc, *pe; 145 146 if (data == NULL) { 147 printf("missing data argument\n"); 148 return EINVAL; 149 } 150 if (mp->mnt_flag & MNT_UPDATE) { 151 printf("MNT_UPDATE not implemented"); 152 return EOPNOTSUPP; 153 } 154 error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args)); 155 if (error) 156 return error; 157 if (args.version != SMBFS_VERSION) { 158 printf("mount version mismatch: kernel=%d, mount=%d\n", 159 SMBFS_VERSION, args.version); 160 return EINVAL; 161 } 162 smb_makescred(&scred, p, p->p_ucred); 163 error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp); 164 if (error) { 165 printf("invalid device handle %d (%d)\n", args.dev, error); 166 return error; 167 } 168 vcp = SSTOVC(ssp); 169 smb_share_unlock(ssp, 0, p); 170 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 171 172 #ifdef SMBFS_USEZONE 173 smp = zalloc(smbfsmount_zone); 174 #else 175 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_USE_RESERVE); 176 #endif 177 if (smp == NULL) { 178 printf("could not alloc smbmount\n"); 179 error = ENOMEM; 180 goto bad; 181 } 182 bzero(smp, sizeof(*smp)); 183 mp->mnt_data = (qaddr_t)smp; 184 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 185 if (smp->sm_hash == NULL) 186 goto bad; 187 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 188 smp->sm_share = ssp; 189 smp->sm_root = NULL; 190 smp->sm_args = args; 191 smp->sm_caseopt = args.caseopt; 192 smp->sm_args.file_mode = (smp->sm_args.file_mode & 193 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 194 smp->sm_args.dir_mode = (smp->sm_args.dir_mode & 195 (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 196 197 /* simple_lock_init(&smp->sm_npslock);*/ 198 error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); 199 if (error) 200 goto bad; 201 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); 202 pc = mp->mnt_stat.f_mntfromname; 203 pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 204 bzero(pc, MNAMELEN); 205 *pc++ = '/'; 206 *pc++ = '/'; 207 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 208 if (pc < pe-1) { 209 *(pc++) = '@'; 210 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 211 if (pc < pe - 1) { 212 *(pc++) = '/'; 213 strncpy(pc, ssp->ss_name, pe - pc - 2); 214 } 215 } 216 /* protect against invalid mount points */ 217 smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0'; 218 vfs_getnewfsid(mp); 219 error = smbfs_root(mp, &vp); 220 if (error) 221 goto bad; 222 VOP_UNLOCK(vp, 0, p); 223 SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount); 224 225 #ifdef DIAGNOSTICS 226 SMBERROR("mp=%p\n", mp); 227 #endif 228 return error; 229 bad: 230 if (smp) { 231 if (smp->sm_hash) 232 free(smp->sm_hash, M_SMBFSHASH); 233 lockdestroy(&smp->sm_hashlock); 234 #ifdef SMBFS_USEZONE 235 zfree(smbfsmount_zone, smp); 236 #else 237 free(smp, M_SMBFSDATA); 238 #endif 239 } 240 if (ssp) 241 smb_share_put(ssp, &scred); 242 return error; 243 } 244 245 /* Unmount the filesystem described by mp. */ 246 static int 247 smbfs_unmount(struct mount *mp, int mntflags, struct proc *p) 248 { 249 struct smbmount *smp = VFSTOSMBFS(mp); 250 struct smb_cred scred; 251 int error, flags; 252 253 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 254 flags = 0; 255 if (mntflags & MNT_FORCE) 256 flags |= FORCECLOSE; 257 /* 258 * Keep trying to flush the vnode list for the mount while 259 * some are still busy and we are making progress towards 260 * making them not busy. This is needed because smbfs vnodes 261 * reference their parent directory but may appear after their 262 * parent in the list; one pass over the vnode list is not 263 * sufficient in this case. 264 */ 265 do { 266 smp->sm_didrele = 0; 267 /* There is 1 extra root vnode reference from smbfs_mount(). */ 268 error = vflush(mp, 1, flags); 269 } while (error == EBUSY && smp->sm_didrele != 0); 270 if (error) 271 return error; 272 smb_makescred(&scred, p, p->p_ucred); 273 smb_share_put(smp->sm_share, &scred); 274 mp->mnt_data = (qaddr_t)0; 275 276 if (smp->sm_hash) 277 free(smp->sm_hash, M_SMBFSHASH); 278 lockdestroy(&smp->sm_hashlock); 279 #ifdef SMBFS_USEZONE 280 zfree(smbfsmount_zone, smp); 281 #else 282 free(smp, M_SMBFSDATA); 283 #endif 284 mp->mnt_flag &= ~MNT_LOCAL; 285 return error; 286 } 287 288 /* 289 * Return locked root vnode of a filesystem 290 */ 291 static int 292 smbfs_root(struct mount *mp, struct vnode **vpp) 293 { 294 struct smbmount *smp = VFSTOSMBFS(mp); 295 struct vnode *vp; 296 struct smbnode *np; 297 struct smbfattr fattr; 298 struct proc *p = curproc; 299 struct ucred *cred = p->p_ucred; 300 struct smb_cred scred; 301 int error; 302 303 if (smp == NULL) { 304 SMBERROR("smp == NULL (bug in umount)\n"); 305 return EINVAL; 306 } 307 if (smp->sm_root) { 308 *vpp = SMBTOV(smp->sm_root); 309 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, p); 310 } 311 smb_makescred(&scred, p, cred); 312 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 313 if (error) 314 return error; 315 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 316 if (error) 317 return error; 318 vp->v_flag |= VROOT; 319 np = VTOSMB(vp); 320 smp->sm_root = np; 321 *vpp = vp; 322 return 0; 323 } 324 325 /* 326 * Vfs start routine, a no-op. 327 */ 328 /* ARGSUSED */ 329 static int 330 smbfs_start(mp, flags, p) 331 struct mount *mp; 332 int flags; 333 struct proc *p; 334 { 335 SMBVDEBUG("flags=%04x\n", flags); 336 return 0; 337 } 338 339 /* 340 * Do operations associated with quotas, not supported 341 */ 342 /* ARGSUSED */ 343 static int 344 smbfs_quotactl(mp, cmd, uid, arg, p) 345 struct mount *mp; 346 int cmd; 347 uid_t uid; 348 caddr_t arg; 349 struct proc *p; 350 { 351 SMBVDEBUG("return EOPNOTSUPP\n"); 352 return EOPNOTSUPP; 353 } 354 355 /*ARGSUSED*/ 356 int 357 smbfs_init(struct vfsconf *vfsp) 358 { 359 smb_checksmp(); 360 361 #ifdef SMBFS_USEZONE 362 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 363 #endif 364 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 365 SMBVDEBUG("done.\n"); 366 return 0; 367 } 368 369 /*ARGSUSED*/ 370 int 371 smbfs_uninit(struct vfsconf *vfsp) 372 { 373 374 SMBVDEBUG("done.\n"); 375 return 0; 376 } 377 378 /* 379 * smbfs_statfs call 380 */ 381 int 382 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p) 383 { 384 struct smbmount *smp = VFSTOSMBFS(mp); 385 struct smbnode *np = smp->sm_root; 386 struct smb_share *ssp = smp->sm_share; 387 struct smb_cred scred; 388 int error = 0; 389 390 if (np == NULL) 391 return EINVAL; 392 393 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 394 sbp->f_spare2 = 0; /* placeholder */ 395 smb_makescred(&scred, p, p->p_ucred); 396 397 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 398 error = smbfs_smb_statfs2(ssp, sbp, &scred); 399 else 400 error = smbfs_smb_statfs(ssp, sbp, &scred); 401 if (error) 402 return error; 403 sbp->f_flags = 0; /* copy of mount exported flags */ 404 if (sbp != &mp->mnt_stat) { 405 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */ 406 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */ 407 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */ 408 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 409 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 410 } 411 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 412 return 0; 413 } 414 415 /* 416 * Flush out the buffer cache 417 */ 418 /* ARGSUSED */ 419 static int 420 smbfs_sync(mp, waitfor, cred, p) 421 struct mount *mp; 422 int waitfor; 423 struct ucred *cred; 424 struct proc *p; 425 { 426 struct vnode *vp; 427 int error, allerror = 0; 428 /* 429 * Force stale buffer cache information to be flushed. 430 */ 431 loop: 432 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 433 vp != NULL; 434 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 435 /* 436 * If the vnode that we are about to sync is no longer 437 * associated with this mount point, start over. 438 */ 439 if (vp->v_mount != mp) 440 goto loop; 441 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 442 waitfor == MNT_LAZY) 443 continue; 444 if (vget(vp, LK_EXCLUSIVE, p)) 445 goto loop; 446 error = VOP_FSYNC(vp, cred, waitfor, p); 447 if (error) 448 allerror = error; 449 vput(vp); 450 } 451 return (allerror); 452 } 453 454 #if __FreeBSD_version < 400009 455 /* 456 * smbfs flat namespace lookup. Unsupported. 457 */ 458 /* ARGSUSED */ 459 static int smbfs_vget(mp, ino, vpp) 460 struct mount *mp; 461 ino_t ino; 462 struct vnode **vpp; 463 { 464 return (EOPNOTSUPP); 465 } 466 467 /* ARGSUSED */ 468 static int smbfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 469 struct mount *mp; 470 struct fid *fhp; 471 struct sockaddr *nam; 472 struct vnode **vpp; 473 int *exflagsp; 474 struct ucred **credanonp; 475 { 476 return (EINVAL); 477 } 478 479 /* 480 * Vnode pointer to File handle, should never happen either 481 */ 482 /* ARGSUSED */ 483 static int 484 smbfs_vptofh(vp, fhp) 485 struct vnode *vp; 486 struct fid *fhp; 487 { 488 return (EINVAL); 489 } 490 491 #endif /* __FreeBSD_version < 400009 */ 492