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 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/proc.h> 38 #include <sys/bio.h> 39 #include <sys/buf.h> 40 #include <sys/kernel.h> 41 #include <sys/sysctl.h> 42 #include <sys/vnode.h> 43 #include <sys/mount.h> 44 #include <sys/stat.h> 45 #include <sys/malloc.h> 46 #include <sys/module.h> 47 48 49 #include <netsmb/smb.h> 50 #include <netsmb/smb_conn.h> 51 #include <netsmb/smb_subr.h> 52 #include <netsmb/smb_dev.h> 53 54 #include <fs/smbfs/smbfs.h> 55 #include <fs/smbfs/smbfs_node.h> 56 #include <fs/smbfs/smbfs_subr.h> 57 58 static int smbfs_debuglevel = 0; 59 60 static int smbfs_version = SMBFS_VERSION; 61 62 #ifdef SMBFS_USEZONE 63 #include <vm/vm.h> 64 #include <vm/vm_extern.h> 65 66 vm_zone_t smbfsmount_zone; 67 #endif 68 69 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem"); 70 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 71 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 72 73 static MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table"); 74 75 static vfs_init_t smbfs_init; 76 static vfs_uninit_t smbfs_uninit; 77 static vfs_cmount_t smbfs_cmount; 78 static vfs_mount_t smbfs_mount; 79 static vfs_root_t smbfs_root; 80 static vfs_quotactl_t smbfs_quotactl; 81 static vfs_statfs_t smbfs_statfs; 82 static vfs_unmount_t smbfs_unmount; 83 84 static struct vfsops smbfs_vfsops = { 85 .vfs_init = smbfs_init, 86 .vfs_cmount = smbfs_cmount, 87 .vfs_mount = smbfs_mount, 88 .vfs_quotactl = smbfs_quotactl, 89 .vfs_root = smbfs_root, 90 .vfs_statfs = smbfs_statfs, 91 .vfs_sync = vfs_stdsync, 92 .vfs_uninit = smbfs_uninit, 93 .vfs_unmount = smbfs_unmount, 94 }; 95 96 97 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 98 99 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 100 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2); 101 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 102 103 int smbfs_pbuf_freecnt = -1; /* start out unlimited */ 104 105 static int 106 smbfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td) 107 { 108 struct smbfs_args args; 109 int error; 110 111 error = copyin(data, &args, sizeof(struct smbfs_args)); 112 if (error) 113 return error; 114 115 if (args.version != SMBFS_VERSION) { 116 printf("mount version mismatch: kernel=%d, mount=%d\n", 117 SMBFS_VERSION, args.version); 118 return EINVAL; 119 } 120 ma = mount_argf(ma, "dev", "%d", args.dev); 121 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft"); 122 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr"); 123 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong"); 124 ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls"); 125 ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong"); 126 ma = mount_arg(ma, "rootpath", args.root_path, -1); 127 ma = mount_argf(ma, "uid", "%d", args.uid); 128 ma = mount_argf(ma, "gid", "%d", args.gid); 129 ma = mount_argf(ma, "file_mode", "%d", args.file_mode); 130 ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode); 131 ma = mount_argf(ma, "caseopt", "%d", args.caseopt); 132 133 error = kernel_mount(ma, flags); 134 135 return (error); 136 } 137 138 static const char *smbfs_opts[] = { 139 "dev", "soft", "intr", "strong", "have_nls", "long", 140 "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode", 141 "caseopt", "errmsg", NULL 142 }; 143 144 static int 145 smbfs_mount(struct mount *mp, struct thread *td) 146 { 147 struct smbmount *smp = NULL; 148 struct smb_vc *vcp; 149 struct smb_share *ssp = NULL; 150 struct vnode *vp; 151 struct smb_cred scred; 152 int error, v; 153 char *pc, *pe; 154 155 if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) 156 return EOPNOTSUPP; 157 158 if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) { 159 vfs_mount_error(mp, "%s", "Invalid option"); 160 return (EINVAL); 161 } 162 163 smb_makescred(&scred, td, td->td_ucred); 164 if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) { 165 vfs_mount_error(mp, "No dev option"); 166 return (EINVAL); 167 } 168 error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp); 169 if (error) { 170 printf("invalid device handle %d (%d)\n", v, error); 171 vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error); 172 return error; 173 } 174 vcp = SSTOVC(ssp); 175 smb_share_unlock(ssp, 0, td); 176 mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 177 178 #ifdef SMBFS_USEZONE 179 smp = zalloc(smbfsmount_zone); 180 #else 181 MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, 182 M_WAITOK|M_USE_RESERVE); 183 #endif 184 if (smp == NULL) { 185 printf("could not alloc smbmount\n"); 186 vfs_mount_error(mp, "could not alloc smbmount", v, error); 187 error = ENOMEM; 188 goto bad; 189 } 190 bzero(smp, sizeof(*smp)); 191 mp->mnt_data = smp; 192 smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 193 if (smp->sm_hash == NULL) 194 goto bad; 195 lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 196 smp->sm_share = ssp; 197 smp->sm_root = NULL; 198 if (1 != vfs_scanopt(mp->mnt_optnew, 199 "caseopt", "%d", &smp->sm_caseopt)) { 200 vfs_mount_error(mp, "Invalid caseopt"); 201 error = EINVAL; 202 goto bad; 203 } 204 if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) { 205 vfs_mount_error(mp, "Invalid uid"); 206 error = EINVAL; 207 goto bad; 208 } 209 smp->sm_uid = v; 210 211 if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) { 212 vfs_mount_error(mp, "Invalid gid"); 213 error = EINVAL; 214 goto bad; 215 } 216 smp->sm_gid = v; 217 218 if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) { 219 vfs_mount_error(mp, "Invalid file_mode"); 220 error = EINVAL; 221 goto bad; 222 } 223 smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 224 225 if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) { 226 vfs_mount_error(mp, "Invalid dir_mode"); 227 error = EINVAL; 228 goto bad; 229 } 230 smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 231 232 vfs_flagopt(mp->mnt_optnew, 233 "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG); 234 235 /* simple_lock_init(&smp->sm_npslock);*/ 236 pc = mp->mnt_stat.f_mntfromname; 237 pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 238 bzero(pc, MNAMELEN); 239 *pc++ = '/'; 240 *pc++ = '/'; 241 pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 242 if (pc < pe-1) { 243 *(pc++) = '@'; 244 pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 245 if (pc < pe - 1) { 246 *(pc++) = '/'; 247 strncpy(pc, ssp->ss_name, pe - pc - 2); 248 } 249 } 250 vfs_getnewfsid(mp); 251 error = smbfs_root(mp, LK_EXCLUSIVE, &vp, td); 252 if (error) { 253 vfs_mount_error(mp, "smbfs_root error: %d", error); 254 goto bad; 255 } 256 VOP_UNLOCK(vp, 0); 257 SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp)); 258 259 #ifdef DIAGNOSTICS 260 SMBERROR("mp=%p\n", mp); 261 #endif 262 return error; 263 bad: 264 if (smp) { 265 if (smp->sm_hash) 266 free(smp->sm_hash, M_SMBFSHASH); 267 lockdestroy(&smp->sm_hashlock); 268 #ifdef SMBFS_USEZONE 269 zfree(smbfsmount_zone, smp); 270 #else 271 free(smp, M_SMBFSDATA); 272 #endif 273 } 274 if (ssp) 275 smb_share_put(ssp, &scred); 276 return error; 277 } 278 279 /* Unmount the filesystem described by mp. */ 280 static int 281 smbfs_unmount(struct mount *mp, int mntflags, struct thread *td) 282 { 283 struct smbmount *smp = VFSTOSMBFS(mp); 284 struct smb_cred scred; 285 int error, flags; 286 287 SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 288 flags = 0; 289 if (mntflags & MNT_FORCE) 290 flags |= FORCECLOSE; 291 /* 292 * Keep trying to flush the vnode list for the mount while 293 * some are still busy and we are making progress towards 294 * making them not busy. This is needed because smbfs vnodes 295 * reference their parent directory but may appear after their 296 * parent in the list; one pass over the vnode list is not 297 * sufficient in this case. 298 */ 299 do { 300 smp->sm_didrele = 0; 301 /* There is 1 extra root vnode reference from smbfs_mount(). */ 302 error = vflush(mp, 1, flags, td); 303 } while (error == EBUSY && smp->sm_didrele != 0); 304 if (error) 305 return error; 306 smb_makescred(&scred, td, td->td_ucred); 307 error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE, td); 308 if (error) 309 return error; 310 smb_share_put(smp->sm_share, &scred); 311 mp->mnt_data = NULL; 312 313 if (smp->sm_hash) 314 free(smp->sm_hash, M_SMBFSHASH); 315 lockdestroy(&smp->sm_hashlock); 316 #ifdef SMBFS_USEZONE 317 zfree(smbfsmount_zone, smp); 318 #else 319 free(smp, M_SMBFSDATA); 320 #endif 321 MNT_ILOCK(mp); 322 mp->mnt_flag &= ~MNT_LOCAL; 323 MNT_IUNLOCK(mp); 324 return error; 325 } 326 327 /* 328 * Return locked root vnode of a filesystem 329 */ 330 static int 331 smbfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 332 { 333 struct smbmount *smp = VFSTOSMBFS(mp); 334 struct vnode *vp; 335 struct smbnode *np; 336 struct smbfattr fattr; 337 struct ucred *cred = td->td_ucred; 338 struct smb_cred scred; 339 int error; 340 341 if (smp == NULL) { 342 SMBERROR("smp == NULL (bug in umount)\n"); 343 vfs_mount_error(mp, "smp == NULL (bug in umount)"); 344 return EINVAL; 345 } 346 if (smp->sm_root) { 347 *vpp = SMBTOV(smp->sm_root); 348 return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 349 } 350 smb_makescred(&scred, td, cred); 351 error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 352 if (error) 353 return error; 354 error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 355 if (error) 356 return error; 357 ASSERT_VOP_LOCKED(vp, "smbfs_root"); 358 vp->v_vflag |= VV_ROOT; 359 np = VTOSMB(vp); 360 smp->sm_root = np; 361 *vpp = vp; 362 return 0; 363 } 364 365 /* 366 * Do operations associated with quotas, not supported 367 */ 368 /* ARGSUSED */ 369 static int 370 smbfs_quotactl(mp, cmd, uid, arg, td) 371 struct mount *mp; 372 int cmd; 373 uid_t uid; 374 void *arg; 375 struct thread *td; 376 { 377 SMBVDEBUG("return EOPNOTSUPP\n"); 378 return EOPNOTSUPP; 379 } 380 381 /*ARGSUSED*/ 382 int 383 smbfs_init(struct vfsconf *vfsp) 384 { 385 #ifdef SMBFS_USEZONE 386 smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 387 #endif 388 smbfs_pbuf_freecnt = nswbuf / 2 + 1; 389 SMBVDEBUG("done.\n"); 390 return 0; 391 } 392 393 /*ARGSUSED*/ 394 int 395 smbfs_uninit(struct vfsconf *vfsp) 396 { 397 398 SMBVDEBUG("done.\n"); 399 return 0; 400 } 401 402 /* 403 * smbfs_statfs call 404 */ 405 int 406 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 407 { 408 struct smbmount *smp = VFSTOSMBFS(mp); 409 struct smbnode *np = smp->sm_root; 410 struct smb_share *ssp = smp->sm_share; 411 struct smb_cred scred; 412 int error = 0; 413 414 if (np == NULL) { 415 vfs_mount_error(mp, "np == NULL"); 416 return EINVAL; 417 } 418 419 sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 420 smb_makescred(&scred, td, td->td_ucred); 421 422 if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 423 error = smbfs_smb_statfs2(ssp, sbp, &scred); 424 else 425 error = smbfs_smb_statfs(ssp, sbp, &scred); 426 if (error) 427 return error; 428 sbp->f_flags = 0; /* copy of mount exported flags */ 429 return 0; 430 } 431