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$ 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 61 int smbfs_debuglevel = 0; 62 63 static 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 70 vm_zone_t smbfsmount_zone; 71 #endif 72 73 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system"); 74 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 75 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 76 77 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table"); 78 79 80 static int smbfs_mount(struct mount *, char *, caddr_t, 81 struct nameidata *, struct thread *); 82 static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *); 83 static int smbfs_root(struct mount *, struct vnode **); 84 static int smbfs_start(struct mount *, int, struct thread *); 85 static int smbfs_statfs(struct mount *, struct statfs *, struct thread *); 86 static int smbfs_sync(struct mount *, int, struct ucred *, struct thread *); 87 static int smbfs_unmount(struct mount *, int, struct thread *); 88 static int smbfs_init(struct vfsconf *vfsp); 89 static int smbfs_uninit(struct vfsconf *vfsp); 90 91 #if __FreeBSD_version < 400009 92 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp); 93 static int smbfs_fhtovp(struct mount *, struct fid *, 94 struct sockaddr *, struct vnode **, int *, 95 struct ucred **); 96 static int smbfs_vptofh(struct vnode *, struct fid *); 97 #endif 98 99 static 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 128 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 129 130 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 131 MODULE_DEPEND(smbfs, libiconv, 1, 1, 1); 132 133 int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 134 135 static int 136 smbfs_mount(struct mount *mp, char *path, caddr_t data, 137 struct nameidata *ndp, struct thread *td) 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, td, td->td_proc->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, td); 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, td); 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; 236 bad: 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. */ 253 static int 254 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td) 255 { 256 struct smbmount *smp = VFSTOSMBFS(mp); 257 struct smb_cred scred; 258 int error, flags; 259 260 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 261 flags = 0; 262 if (mntflags & MNT_FORCE) 263 flags |= FORCECLOSE; 264 /* There is 1 extra root vnode reference from smbfs_mount(). */ 265 error = vflush(mp, 1, flags); 266 if (error) 267 return error; 268 smb_makescred(&scred, td, td->td_proc->p_ucred); 269 smb_share_put(smp->sm_share, &scred); 270 mp->mnt_data = (qaddr_t)0; 271 272 if (smp->sm_hash) 273 free(smp->sm_hash, M_SMBFSHASH); 274 lockdestroy(&smp->sm_hashlock); 275 #ifdef SMBFS_USEZONE 276 zfree(smbfsmount_zone, smp); 277 #else 278 free(smp, M_SMBFSDATA); 279 #endif 280 mp->mnt_flag &= ~MNT_LOCAL; 281 return error; 282 } 283 284 /* 285 * Return locked root vnode of a filesystem 286 */ 287 static int 288 smbfs_root(struct mount *mp, struct vnode **vpp) 289 { 290 struct smbmount *smp = VFSTOSMBFS(mp); 291 struct vnode *vp; 292 struct smbnode *np; 293 struct smbfattr fattr; 294 struct thread *td = curthread; 295 struct ucred *cred = td->td_proc->p_ucred; 296 struct smb_cred scred; 297 int error; 298 299 if (smp == NULL) { 300 SMBERROR("smp == NULL (bug in umount)\n"); 301 return EINVAL; 302 } 303 if (smp->sm_root) { 304 *vpp = SMBTOV(smp->sm_root); 305 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 306 } 307 smb_makescred(&scred, td, cred); 308 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 309 if (error) 310 return error; 311 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 312 if (error) 313 return error; 314 vp->v_flag |= VROOT; 315 np = VTOSMB(vp); 316 smp->sm_root = np; 317 *vpp = vp; 318 return 0; 319 } 320 321 /* 322 * Vfs start routine, a no-op. 323 */ 324 /* ARGSUSED */ 325 static int 326 smbfs_start(mp, flags, td) 327 struct mount *mp; 328 int flags; 329 struct thread *td; 330 { 331 SMBVDEBUG("flags=%04x\n", flags); 332 return 0; 333 } 334 335 /* 336 * Do operations associated with quotas, not supported 337 */ 338 /* ARGSUSED */ 339 static int 340 smbfs_quotactl(mp, cmd, uid, arg, td) 341 struct mount *mp; 342 int cmd; 343 uid_t uid; 344 caddr_t arg; 345 struct thread *td; 346 { 347 SMBVDEBUG("return EOPNOTSUPP\n"); 348 return EOPNOTSUPP; 349 } 350 351 /*ARGSUSED*/ 352 int 353 smbfs_init(struct vfsconf *vfsp) 354 { 355 #ifndef SMP 356 int name[2]; 357 int olen, ncpu, plen, error; 358 359 name[0] = CTL_HW; 360 name[1] = HW_NCPU; 361 error = kernel_sysctl(curthread, name, 2, &ncpu, &olen, NULL, 0, &plen); 362 if (error == 0 && ncpu > 1) 363 printf("warning: smbfs module compiled without SMP support."); 364 #endif 365 366 #ifdef SMBFS_USEZONE 367 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 368 #endif 369 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 370 SMBVDEBUG("done.\n"); 371 return 0; 372 } 373 374 /*ARGSUSED*/ 375 int 376 smbfs_uninit(struct vfsconf *vfsp) 377 { 378 379 SMBVDEBUG("done.\n"); 380 return 0; 381 } 382 383 /* 384 * smbfs_statfs call 385 */ 386 int 387 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 388 { 389 struct smbmount *smp = VFSTOSMBFS(mp); 390 struct smbnode *np = smp->sm_root; 391 struct smb_share *ssp = smp->sm_share; 392 struct smb_cred scred; 393 int error = 0; 394 395 if (np == NULL) 396 return EINVAL; 397 398 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 399 sbp->f_spare2 = 0; /* placeholder */ 400 smb_makescred(&scred, td, td->td_proc->p_ucred); 401 402 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 403 error = smbfs_smb_statfs2(ssp, sbp, &scred); 404 else 405 error = smbfs_smb_statfs(ssp, sbp, &scred); 406 if (error) 407 return error; 408 sbp->f_flags = 0; /* copy of mount exported flags */ 409 if (sbp != &mp->mnt_stat) { 410 sbp->f_fsid = mp->mnt_stat.f_fsid; /* file system id */ 411 sbp->f_owner = mp->mnt_stat.f_owner; /* user that mounted the filesystem */ 412 sbp->f_type = mp->mnt_vfc->vfc_typenum; /* type of filesystem */ 413 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); 414 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); 415 } 416 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN); 417 return 0; 418 } 419 420 /* 421 * Flush out the buffer cache 422 */ 423 /* ARGSUSED */ 424 static int 425 smbfs_sync(mp, waitfor, cred, td) 426 struct mount *mp; 427 int waitfor; 428 struct ucred *cred; 429 struct thread *td; 430 { 431 struct vnode *vp; 432 int error, allerror = 0; 433 /* 434 * Force stale buffer cache information to be flushed. 435 */ 436 loop: 437 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 438 vp != NULL; 439 vp = TAILQ_NEXT(vp, v_nmntvnodes)) { 440 /* 441 * If the vnode that we are about to sync is no longer 442 * associated with this mount point, start over. 443 */ 444 if (vp->v_mount != mp) 445 goto loop; 446 #ifndef FB_RELENG3 447 if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 448 #else 449 if (VOP_ISLOCKED(vp) || TAILQ_EMPTY(&vp->v_dirtyblkhd) || 450 #endif 451 waitfor == MNT_LAZY) 452 continue; 453 if (vget(vp, LK_EXCLUSIVE, td)) 454 goto loop; 455 error = VOP_FSYNC(vp, cred, waitfor, td); 456 if (error) 457 allerror = error; 458 vput(vp); 459 } 460 return (allerror); 461 } 462 463 #if __FreeBSD_version < 400009 464 /* 465 * smbfs flat namespace lookup. Unsupported. 466 */ 467 /* ARGSUSED */ 468 static int smbfs_vget(mp, ino, vpp) 469 struct mount *mp; 470 ino_t ino; 471 struct vnode **vpp; 472 { 473 return (EOPNOTSUPP); 474 } 475 476 /* ARGSUSED */ 477 static int smbfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 478 struct mount *mp; 479 struct fid *fhp; 480 struct sockaddr *nam; 481 struct vnode **vpp; 482 int *exflagsp; 483 struct ucred **credanonp; 484 { 485 return (EINVAL); 486 } 487 488 /* 489 * Vnode pointer to File handle, should never happen either 490 */ 491 /* ARGSUSED */ 492 static int 493 smbfs_vptofh(vp, fhp) 494 struct vnode *vp; 495 struct fid *fhp; 496 { 497 return (EINVAL); 498 } 499 500 #endif /* __FreeBSD_version < 400009 */ 501