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