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