1139776Simp/*- 2206361Sjoel * Copyright (c) 2000-2001 Boris Popov 375374Sbp * All rights reserved. 475374Sbp * 575374Sbp * Redistribution and use in source and binary forms, with or without 675374Sbp * modification, are permitted provided that the following conditions 775374Sbp * are met: 875374Sbp * 1. Redistributions of source code must retain the above copyright 975374Sbp * notice, this list of conditions and the following disclaimer. 1075374Sbp * 2. Redistributions in binary form must reproduce the above copyright 1175374Sbp * notice, this list of conditions and the following disclaimer in the 1275374Sbp * documentation and/or other materials provided with the distribution. 1375374Sbp * 1475374Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1575374Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1675374Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1775374Sbp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1875374Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1975374Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2075374Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2175374Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2275374Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2375374Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2475374Sbp * SUCH DAMAGE. 2575374Sbp * 2675374Sbp * $FreeBSD$ 2775374Sbp */ 2875374Sbp 2975374Sbp#include <sys/param.h> 3075374Sbp#include <sys/systm.h> 3175374Sbp#include <sys/proc.h> 3275374Sbp#include <sys/bio.h> 3375374Sbp#include <sys/buf.h> 3475374Sbp#include <sys/kernel.h> 3575374Sbp#include <sys/sysctl.h> 3675374Sbp#include <sys/vnode.h> 3775374Sbp#include <sys/mount.h> 3875374Sbp#include <sys/stat.h> 3975374Sbp#include <sys/malloc.h> 4087798Ssheldonh#include <sys/module.h> 41176744Srwatson#include <sys/sx.h> 4275374Sbp 4375374Sbp 4475374Sbp#include <netsmb/smb.h> 4575374Sbp#include <netsmb/smb_conn.h> 4675374Sbp#include <netsmb/smb_subr.h> 4775374Sbp#include <netsmb/smb_dev.h> 4875374Sbp 4975374Sbp#include <fs/smbfs/smbfs.h> 5075374Sbp#include <fs/smbfs/smbfs_node.h> 5175374Sbp#include <fs/smbfs/smbfs_subr.h> 5275374Sbp 53141620Sphkstatic int smbfs_debuglevel = 0; 5475374Sbp 5575374Sbpstatic int smbfs_version = SMBFS_VERSION; 5675374Sbp 5775374Sbp#ifdef SMBFS_USEZONE 5875374Sbp#include <vm/vm.h> 5975374Sbp#include <vm/vm_extern.h> 6075374Sbp 6175374Sbpvm_zone_t smbfsmount_zone; 6275374Sbp#endif 6375374Sbp 6496755StrhodesSYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem"); 6575374SbpSYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 6675374SbpSYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 6775374Sbp 68151897Srwatsonstatic MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table"); 6975374Sbp 70116271Sphkstatic vfs_init_t smbfs_init; 71116271Sphkstatic vfs_uninit_t smbfs_uninit; 72138490Sphkstatic vfs_cmount_t smbfs_cmount; 73138490Sphkstatic vfs_mount_t smbfs_mount; 74116271Sphkstatic vfs_root_t smbfs_root; 75116271Sphkstatic vfs_quotactl_t smbfs_quotactl; 76116271Sphkstatic vfs_statfs_t smbfs_statfs; 77116271Sphkstatic vfs_unmount_t smbfs_unmount; 7875374Sbp 7975374Sbpstatic struct vfsops smbfs_vfsops = { 80116271Sphk .vfs_init = smbfs_init, 81138490Sphk .vfs_cmount = smbfs_cmount, 82138490Sphk .vfs_mount = smbfs_mount, 83116271Sphk .vfs_quotactl = smbfs_quotactl, 84116271Sphk .vfs_root = smbfs_root, 85116271Sphk .vfs_statfs = smbfs_statfs, 86116271Sphk .vfs_sync = vfs_stdsync, 87116271Sphk .vfs_uninit = smbfs_uninit, 88116271Sphk .vfs_unmount = smbfs_unmount, 8975374Sbp}; 9075374Sbp 9175374Sbp 9275374SbpVFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 9375374Sbp 9475374SbpMODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 95120492SfjoeMODULE_DEPEND(smbfs, libiconv, 1, 1, 2); 9687798SsheldonhMODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 9775374Sbp 9875374Sbpint smbfs_pbuf_freecnt = -1; /* start out unlimited */ 9975374Sbp 10075374Sbpstatic int 101230725Smckusicksmbfs_cmount(struct mntarg *ma, void * data, uint64_t flags) 10275374Sbp{ 103138490Sphk struct smbfs_args args; 104138490Sphk int error; 105138490Sphk 106153400Sdes error = copyin(data, &args, sizeof(struct smbfs_args)); 107138490Sphk if (error) 108138490Sphk return error; 109138490Sphk 110138490Sphk if (args.version != SMBFS_VERSION) { 111138490Sphk printf("mount version mismatch: kernel=%d, mount=%d\n", 112138490Sphk SMBFS_VERSION, args.version); 113138490Sphk return EINVAL; 114138490Sphk } 115138490Sphk ma = mount_argf(ma, "dev", "%d", args.dev); 116138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft"); 117138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr"); 118138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong"); 119138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls"); 120138490Sphk ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong"); 121138490Sphk ma = mount_arg(ma, "rootpath", args.root_path, -1); 122138490Sphk ma = mount_argf(ma, "uid", "%d", args.uid); 123138490Sphk ma = mount_argf(ma, "gid", "%d", args.gid); 124138490Sphk ma = mount_argf(ma, "file_mode", "%d", args.file_mode); 125138490Sphk ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode); 126138490Sphk ma = mount_argf(ma, "caseopt", "%d", args.caseopt); 127138490Sphk 128138490Sphk error = kernel_mount(ma, flags); 129138490Sphk 130138490Sphk return (error); 131138490Sphk} 132138490Sphk 133138490Sphkstatic const char *smbfs_opts[] = { 134138678Sphk "dev", "soft", "intr", "strong", "have_nls", "long", 135138490Sphk "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode", 136152466Srodrigc "caseopt", "errmsg", NULL 137138490Sphk}; 138138490Sphk 139138490Sphkstatic int 140191990Sattiliosmbfs_mount(struct mount *mp) 141138490Sphk{ 14275374Sbp struct smbmount *smp = NULL; 14375374Sbp struct smb_vc *vcp; 14475374Sbp struct smb_share *ssp = NULL; 14575374Sbp struct vnode *vp; 146191990Sattilio struct thread *td; 14775374Sbp struct smb_cred scred; 148138490Sphk int error, v; 14975374Sbp char *pc, *pe; 15075374Sbp 151191990Sattilio td = curthread; 152137479Sphk if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) 15375374Sbp return EOPNOTSUPP; 154137479Sphk 155152466Srodrigc if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) { 156152466Srodrigc vfs_mount_error(mp, "%s", "Invalid option"); 157138490Sphk return (EINVAL); 158152466Srodrigc } 159138490Sphk 16091406Sjhb smb_makescred(&scred, td, td->td_ucred); 161152466Srodrigc if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) { 162152466Srodrigc vfs_mount_error(mp, "No dev option"); 163138490Sphk return (EINVAL); 164152466Srodrigc } 165138490Sphk error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp); 16675374Sbp if (error) { 167138490Sphk printf("invalid device handle %d (%d)\n", v, error); 168152466Srodrigc vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error); 16975374Sbp return error; 17075374Sbp } 17175374Sbp vcp = SSTOVC(ssp); 172184572Srwatson smb_share_unlock(ssp, 0); 17375374Sbp mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 17475374Sbp 17575374Sbp#ifdef SMBFS_USEZONE 17675374Sbp smp = zalloc(smbfsmount_zone); 17775374Sbp#else 178213363Salc smp = malloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK); 17975374Sbp#endif 18075374Sbp if (smp == NULL) { 181152466Srodrigc printf("could not alloc smbmount\n"); 182152466Srodrigc vfs_mount_error(mp, "could not alloc smbmount", v, error); 183152466Srodrigc error = ENOMEM; 18475374Sbp goto bad; 18575374Sbp } 18675374Sbp bzero(smp, sizeof(*smp)); 187172697Salfred mp->mnt_data = smp; 18875374Sbp smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 18975374Sbp if (smp->sm_hash == NULL) 19075374Sbp goto bad; 191176744Srwatson sx_init(&smp->sm_hashlock, "smbfsh"); 19275374Sbp smp->sm_share = ssp; 19375374Sbp smp->sm_root = NULL; 194138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, 195138490Sphk "caseopt", "%d", &smp->sm_caseopt)) { 196152466Srodrigc vfs_mount_error(mp, "Invalid caseopt"); 197138490Sphk error = EINVAL; 198138490Sphk goto bad; 199138490Sphk } 200138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) { 201152466Srodrigc vfs_mount_error(mp, "Invalid uid"); 202138490Sphk error = EINVAL; 203138490Sphk goto bad; 204138490Sphk } 205138490Sphk smp->sm_uid = v; 20675374Sbp 207138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) { 208152466Srodrigc vfs_mount_error(mp, "Invalid gid"); 209138490Sphk error = EINVAL; 210138490Sphk goto bad; 211138490Sphk } 212138490Sphk smp->sm_gid = v; 213138490Sphk 214138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) { 215152466Srodrigc vfs_mount_error(mp, "Invalid file_mode"); 216138490Sphk error = EINVAL; 217138490Sphk goto bad; 218138490Sphk } 219138490Sphk smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 220138490Sphk 221138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) { 222152466Srodrigc vfs_mount_error(mp, "Invalid dir_mode"); 223138490Sphk error = EINVAL; 224138490Sphk goto bad; 225138490Sphk } 226138490Sphk smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 227138490Sphk 228138490Sphk vfs_flagopt(mp->mnt_optnew, 229153121Savatar "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG); 230138490Sphk 23175374Sbp/* simple_lock_init(&smp->sm_npslock);*/ 23275374Sbp pc = mp->mnt_stat.f_mntfromname; 23375374Sbp pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 23475374Sbp bzero(pc, MNAMELEN); 23575374Sbp *pc++ = '/'; 23675374Sbp *pc++ = '/'; 23775374Sbp pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 23875374Sbp if (pc < pe-1) { 23975374Sbp *(pc++) = '@'; 24075374Sbp pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 24175374Sbp if (pc < pe - 1) { 24275374Sbp *(pc++) = '/'; 24375374Sbp strncpy(pc, ssp->ss_name, pe - pc - 2); 24475374Sbp } 24575374Sbp } 24675374Sbp vfs_getnewfsid(mp); 247191990Sattilio error = smbfs_root(mp, LK_EXCLUSIVE, &vp); 248152466Srodrigc if (error) { 249152466Srodrigc vfs_mount_error(mp, "smbfs_root error: %d", error); 25075374Sbp goto bad; 251152466Srodrigc } 252175294Sattilio VOP_UNLOCK(vp, 0); 253103936Sjeff SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp)); 25475374Sbp 255198448Sru#ifdef DIAGNOSTIC 25675374Sbp SMBERROR("mp=%p\n", mp); 25775374Sbp#endif 25875374Sbp return error; 25975374Sbpbad: 26075374Sbp if (smp) { 26175374Sbp if (smp->sm_hash) 26275374Sbp free(smp->sm_hash, M_SMBFSHASH); 263176744Srwatson sx_destroy(&smp->sm_hashlock); 26475374Sbp#ifdef SMBFS_USEZONE 26575374Sbp zfree(smbfsmount_zone, smp); 26675374Sbp#else 26775374Sbp free(smp, M_SMBFSDATA); 26875374Sbp#endif 26975374Sbp } 27075374Sbp if (ssp) 27175374Sbp smb_share_put(ssp, &scred); 27275374Sbp return error; 27375374Sbp} 27475374Sbp 27575374Sbp/* Unmount the filesystem described by mp. */ 27675374Sbpstatic int 277191990Sattiliosmbfs_unmount(struct mount *mp, int mntflags) 27875374Sbp{ 279191990Sattilio struct thread *td; 28075374Sbp struct smbmount *smp = VFSTOSMBFS(mp); 28175374Sbp struct smb_cred scred; 28275374Sbp int error, flags; 28375374Sbp 28475374Sbp SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 285191990Sattilio td = curthread; 28675374Sbp flags = 0; 28775374Sbp if (mntflags & MNT_FORCE) 28875374Sbp flags |= FORCECLOSE; 289107842Stjr /* 290107842Stjr * Keep trying to flush the vnode list for the mount while 291107842Stjr * some are still busy and we are making progress towards 292107842Stjr * making them not busy. This is needed because smbfs vnodes 293107842Stjr * reference their parent directory but may appear after their 294107842Stjr * parent in the list; one pass over the vnode list is not 295107842Stjr * sufficient in this case. 296107842Stjr */ 297107842Stjr do { 298107842Stjr smp->sm_didrele = 0; 299107842Stjr /* There is 1 extra root vnode reference from smbfs_mount(). */ 300132023Salfred error = vflush(mp, 1, flags, td); 301107842Stjr } while (error == EBUSY && smp->sm_didrele != 0); 30275374Sbp if (error) 30375374Sbp return error; 30491406Sjhb smb_makescred(&scred, td, td->td_ucred); 305184572Srwatson error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE); 306160437Sjhb if (error) 307160437Sjhb return error; 30875374Sbp smb_share_put(smp->sm_share, &scred); 309172697Salfred mp->mnt_data = NULL; 31075374Sbp 31175374Sbp if (smp->sm_hash) 31275374Sbp free(smp->sm_hash, M_SMBFSHASH); 313176744Srwatson sx_destroy(&smp->sm_hashlock); 31475374Sbp#ifdef SMBFS_USEZONE 31575374Sbp zfree(smbfsmount_zone, smp); 31675374Sbp#else 31775374Sbp free(smp, M_SMBFSDATA); 31875374Sbp#endif 319162647Stegge MNT_ILOCK(mp); 32075374Sbp mp->mnt_flag &= ~MNT_LOCAL; 321162647Stegge MNT_IUNLOCK(mp); 32275374Sbp return error; 32375374Sbp} 32475374Sbp 32575374Sbp/* 32675374Sbp * Return locked root vnode of a filesystem 32775374Sbp */ 32875374Sbpstatic int 329191990Sattiliosmbfs_root(struct mount *mp, int flags, struct vnode **vpp) 33075374Sbp{ 33175374Sbp struct smbmount *smp = VFSTOSMBFS(mp); 33275374Sbp struct vnode *vp; 33375374Sbp struct smbnode *np; 33475374Sbp struct smbfattr fattr; 335191990Sattilio struct thread *td; 336191990Sattilio struct ucred *cred; 33775374Sbp struct smb_cred scred; 33875374Sbp int error; 33975374Sbp 340191990Sattilio td = curthread; 341191990Sattilio cred = td->td_ucred; 342191990Sattilio 34375374Sbp if (smp == NULL) { 34475374Sbp SMBERROR("smp == NULL (bug in umount)\n"); 345152466Srodrigc vfs_mount_error(mp, "smp == NULL (bug in umount)"); 34675374Sbp return EINVAL; 34775374Sbp } 34875374Sbp if (smp->sm_root) { 34975374Sbp *vpp = SMBTOV(smp->sm_root); 35087194Sbp return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 35175374Sbp } 35287194Sbp smb_makescred(&scred, td, cred); 35375374Sbp error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 35475374Sbp if (error) 35575374Sbp return error; 35675374Sbp error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 35775374Sbp if (error) 35875374Sbp return error; 359101308Sjeff ASSERT_VOP_LOCKED(vp, "smbfs_root"); 360101308Sjeff vp->v_vflag |= VV_ROOT; 36175374Sbp np = VTOSMB(vp); 36275374Sbp smp->sm_root = np; 36375374Sbp *vpp = vp; 36475374Sbp return 0; 36575374Sbp} 36675374Sbp 367110533Stjr/* 368110533Stjr * Do operations associated with quotas, not supported 369110533Stjr */ 370110533Stjr/* ARGSUSED */ 371110533Stjrstatic int 372191990Sattiliosmbfs_quotactl(mp, cmd, uid, arg) 373110533Stjr struct mount *mp; 374110533Stjr int cmd; 375110533Stjr uid_t uid; 376153400Sdes void *arg; 377110533Stjr{ 378110533Stjr SMBVDEBUG("return EOPNOTSUPP\n"); 379110533Stjr return EOPNOTSUPP; 380110533Stjr} 381110533Stjr 38275374Sbp/*ARGSUSED*/ 38375374Sbpint 38475374Sbpsmbfs_init(struct vfsconf *vfsp) 38575374Sbp{ 38675374Sbp#ifdef SMBFS_USEZONE 38775374Sbp smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 38875374Sbp#endif 38975374Sbp smbfs_pbuf_freecnt = nswbuf / 2 + 1; 39075374Sbp SMBVDEBUG("done.\n"); 39175374Sbp return 0; 39275374Sbp} 39375374Sbp 394110533Stjr/*ARGSUSED*/ 395110533Stjrint 396110533Stjrsmbfs_uninit(struct vfsconf *vfsp) 397110533Stjr{ 398110533Stjr 399110533Stjr SMBVDEBUG("done.\n"); 400110533Stjr return 0; 401110533Stjr} 402110533Stjr 40375374Sbp/* 40475374Sbp * smbfs_statfs call 40575374Sbp */ 40675374Sbpint 407191990Sattiliosmbfs_statfs(struct mount *mp, struct statfs *sbp) 40875374Sbp{ 409191990Sattilio struct thread *td = curthread; 41075374Sbp struct smbmount *smp = VFSTOSMBFS(mp); 41175374Sbp struct smbnode *np = smp->sm_root; 41275374Sbp struct smb_share *ssp = smp->sm_share; 41375374Sbp struct smb_cred scred; 414265246Sae int error; 41575374Sbp 416152466Srodrigc if (np == NULL) { 417152466Srodrigc vfs_mount_error(mp, "np == NULL"); 41875374Sbp return EINVAL; 419152466Srodrigc } 42075374Sbp 42175374Sbp sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 42291406Sjhb smb_makescred(&scred, td, td->td_ucred); 423265246Sae error = smbfs_smb_statfs(ssp, sbp, &scred); 424265246Sae if (error == 0) 425265246Sae sbp->f_flags = 0; /* copy of mount exported flags */ 426265246Sae return (error); 42775374Sbp} 428