smbfs_vfsops.c revision 152466
1139776Simp/*- 275374Sbp * 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 * 3. All advertising materials mentioning features or use of this software 1475374Sbp * must display the following acknowledgement: 1575374Sbp * This product includes software developed by Boris Popov. 1675374Sbp * 4. Neither the name of the author nor the names of any co-contributors 1775374Sbp * may be used to endorse or promote products derived from this software 1875374Sbp * without specific prior written permission. 1975374Sbp * 2075374Sbp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 2175374Sbp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2275374Sbp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2375374Sbp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2475374Sbp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2575374Sbp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2675374Sbp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2775374Sbp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2875374Sbp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2975374Sbp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3075374Sbp * SUCH DAMAGE. 3175374Sbp * 3275374Sbp * $FreeBSD: head/sys/fs/smbfs/smbfs_vfsops.c 152466 2005-11-16 02:26:25Z rodrigc $ 3375374Sbp */ 3475374Sbp 3575374Sbp#include <sys/param.h> 3675374Sbp#include <sys/systm.h> 3775374Sbp#include <sys/proc.h> 3875374Sbp#include <sys/bio.h> 3975374Sbp#include <sys/buf.h> 4075374Sbp#include <sys/kernel.h> 4175374Sbp#include <sys/sysctl.h> 4275374Sbp#include <sys/vnode.h> 4375374Sbp#include <sys/mount.h> 4475374Sbp#include <sys/stat.h> 4575374Sbp#include <sys/malloc.h> 4687798Ssheldonh#include <sys/module.h> 4775374Sbp 4875374Sbp 4975374Sbp#include <netsmb/smb.h> 5075374Sbp#include <netsmb/smb_conn.h> 5175374Sbp#include <netsmb/smb_subr.h> 5275374Sbp#include <netsmb/smb_dev.h> 5375374Sbp 5475374Sbp#include <fs/smbfs/smbfs.h> 5575374Sbp#include <fs/smbfs/smbfs_node.h> 5675374Sbp#include <fs/smbfs/smbfs_subr.h> 5775374Sbp 58141620Sphkstatic int smbfs_debuglevel = 0; 5975374Sbp 6075374Sbpstatic int smbfs_version = SMBFS_VERSION; 6175374Sbp 6275374Sbp#ifdef SMBFS_USEZONE 6375374Sbp#include <vm/vm.h> 6475374Sbp#include <vm/vm_extern.h> 6575374Sbp 6675374Sbpvm_zone_t smbfsmount_zone; 6775374Sbp#endif 6875374Sbp 6996755StrhodesSYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem"); 7075374SbpSYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, ""); 7175374SbpSYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, ""); 7275374Sbp 73151897Srwatsonstatic MALLOC_DEFINE(M_SMBFSHASH, "smbfs_hash", "SMBFS hash table"); 7475374Sbp 75116271Sphkstatic vfs_init_t smbfs_init; 76116271Sphkstatic vfs_uninit_t smbfs_uninit; 77138490Sphkstatic vfs_cmount_t smbfs_cmount; 78138490Sphkstatic vfs_mount_t smbfs_mount; 79116271Sphkstatic vfs_root_t smbfs_root; 80116271Sphkstatic vfs_quotactl_t smbfs_quotactl; 81116271Sphkstatic vfs_statfs_t smbfs_statfs; 82116271Sphkstatic vfs_unmount_t smbfs_unmount; 8375374Sbp 8475374Sbpstatic struct vfsops smbfs_vfsops = { 85116271Sphk .vfs_init = smbfs_init, 86138490Sphk .vfs_cmount = smbfs_cmount, 87138490Sphk .vfs_mount = smbfs_mount, 88116271Sphk .vfs_quotactl = smbfs_quotactl, 89116271Sphk .vfs_root = smbfs_root, 90116271Sphk .vfs_statfs = smbfs_statfs, 91116271Sphk .vfs_sync = vfs_stdsync, 92116271Sphk .vfs_uninit = smbfs_uninit, 93116271Sphk .vfs_unmount = smbfs_unmount, 9475374Sbp}; 9575374Sbp 9675374Sbp 9775374SbpVFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK); 9875374Sbp 9975374SbpMODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION); 100120492SfjoeMODULE_DEPEND(smbfs, libiconv, 1, 1, 2); 10187798SsheldonhMODULE_DEPEND(smbfs, libmchain, 1, 1, 1); 10275374Sbp 10375374Sbpint smbfs_pbuf_freecnt = -1; /* start out unlimited */ 10475374Sbp 10575374Sbpstatic int 106138490Sphksmbfs_cmount(struct mntarg *ma, void * data, int flags, struct thread *td) 10775374Sbp{ 108138490Sphk struct smbfs_args args; 109138490Sphk int error; 110138490Sphk 111138490Sphk error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args)); 112138490Sphk if (error) 113138490Sphk return error; 114138490Sphk 115138490Sphk if (args.version != SMBFS_VERSION) { 116138490Sphk printf("mount version mismatch: kernel=%d, mount=%d\n", 117138490Sphk SMBFS_VERSION, args.version); 118138490Sphk return EINVAL; 119138490Sphk } 120138490Sphk ma = mount_argf(ma, "dev", "%d", args.dev); 121138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_SOFT, "nosoft"); 122138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_INTR, "nointr"); 123138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_STRONG, "nostrong"); 124138490Sphk ma = mount_argb(ma, args.flags & SMBFS_MOUNT_HAVE_NLS, "nohave_nls"); 125138490Sphk ma = mount_argb(ma, !(args.flags & SMBFS_MOUNT_NO_LONG), "nolong"); 126138490Sphk ma = mount_arg(ma, "rootpath", args.root_path, -1); 127138490Sphk ma = mount_argf(ma, "uid", "%d", args.uid); 128138490Sphk ma = mount_argf(ma, "gid", "%d", args.gid); 129138490Sphk ma = mount_argf(ma, "file_mode", "%d", args.file_mode); 130138490Sphk ma = mount_argf(ma, "dir_mode", "%d", args.dir_mode); 131138490Sphk ma = mount_argf(ma, "caseopt", "%d", args.caseopt); 132138490Sphk 133138490Sphk error = kernel_mount(ma, flags); 134138490Sphk 135138490Sphk return (error); 136138490Sphk} 137138490Sphk 138138490Sphkstatic const char *smbfs_opts[] = { 139138678Sphk "dev", "soft", "intr", "strong", "have_nls", "long", 140138490Sphk "mountpoint", "rootpath", "uid", "gid", "file_mode", "dir_mode", 141152466Srodrigc "caseopt", "errmsg", NULL 142138490Sphk}; 143138490Sphk 144138490Sphkstatic int 145138490Sphksmbfs_mount(struct mount *mp, struct thread *td) 146138490Sphk{ 14775374Sbp struct smbmount *smp = NULL; 14875374Sbp struct smb_vc *vcp; 14975374Sbp struct smb_share *ssp = NULL; 15075374Sbp struct vnode *vp; 15175374Sbp struct smb_cred scred; 152138490Sphk int error, v; 15375374Sbp char *pc, *pe; 15475374Sbp 155137479Sphk if (mp->mnt_flag & (MNT_UPDATE | MNT_ROOTFS)) 15675374Sbp return EOPNOTSUPP; 157137479Sphk 158152466Srodrigc if (vfs_filteropt(mp->mnt_optnew, smbfs_opts)) { 159152466Srodrigc vfs_mount_error(mp, "%s", "Invalid option"); 160138490Sphk return (EINVAL); 161152466Srodrigc } 162138490Sphk 16391406Sjhb smb_makescred(&scred, td, td->td_ucred); 164152466Srodrigc if (1 != vfs_scanopt(mp->mnt_optnew, "dev", "%d", &v)) { 165152466Srodrigc vfs_mount_error(mp, "No dev option"); 166138490Sphk return (EINVAL); 167152466Srodrigc } 168138490Sphk error = smb_dev2share(v, SMBM_EXEC, &scred, &ssp); 16975374Sbp if (error) { 170138490Sphk printf("invalid device handle %d (%d)\n", v, error); 171152466Srodrigc vfs_mount_error(mp, "invalid device handle %d (%d)\n", v, error); 17275374Sbp return error; 17375374Sbp } 17475374Sbp vcp = SSTOVC(ssp); 17587194Sbp smb_share_unlock(ssp, 0, td); 17675374Sbp mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax; 17775374Sbp 17875374Sbp#ifdef SMBFS_USEZONE 17975374Sbp smp = zalloc(smbfsmount_zone); 18075374Sbp#else 181112915Stjr MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, 182112915Stjr M_WAITOK|M_USE_RESERVE); 18375374Sbp#endif 18475374Sbp if (smp == NULL) { 185152466Srodrigc printf("could not alloc smbmount\n"); 186152466Srodrigc vfs_mount_error(mp, "could not alloc smbmount", v, error); 187152466Srodrigc error = ENOMEM; 18875374Sbp goto bad; 18975374Sbp } 19075374Sbp bzero(smp, sizeof(*smp)); 19175374Sbp mp->mnt_data = (qaddr_t)smp; 19275374Sbp smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen); 19375374Sbp if (smp->sm_hash == NULL) 19475374Sbp goto bad; 19575374Sbp lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0); 19675374Sbp smp->sm_share = ssp; 19775374Sbp smp->sm_root = NULL; 198138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, 199138490Sphk "caseopt", "%d", &smp->sm_caseopt)) { 200152466Srodrigc vfs_mount_error(mp, "Invalid caseopt"); 201138490Sphk error = EINVAL; 202138490Sphk goto bad; 203138490Sphk } 204138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v)) { 205152466Srodrigc vfs_mount_error(mp, "Invalid uid"); 206138490Sphk error = EINVAL; 207138490Sphk goto bad; 208138490Sphk } 209138490Sphk smp->sm_uid = v; 21075374Sbp 211138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v)) { 212152466Srodrigc vfs_mount_error(mp, "Invalid gid"); 213138490Sphk error = EINVAL; 214138490Sphk goto bad; 215138490Sphk } 216138490Sphk smp->sm_gid = v; 217138490Sphk 218138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "file_mode", "%d", &v)) { 219152466Srodrigc vfs_mount_error(mp, "Invalid file_mode"); 220138490Sphk error = EINVAL; 221138490Sphk goto bad; 222138490Sphk } 223138490Sphk smp->sm_file_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG; 224138490Sphk 225138490Sphk if (1 != vfs_scanopt(mp->mnt_optnew, "dir_mode", "%d", &v)) { 226152466Srodrigc vfs_mount_error(mp, "Invalid dir_mode"); 227138490Sphk error = EINVAL; 228138490Sphk goto bad; 229138490Sphk } 230138490Sphk smp->sm_dir_mode = (v & (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR; 231138490Sphk 232138490Sphk vfs_flagopt(mp->mnt_optnew, 233138490Sphk "long", &smp->sm_flags, SMBFS_MOUNT_NO_LONG); 234138490Sphk 235138490Sphk smp->sm_flags ^= SMBFS_MOUNT_NO_LONG; 236138490Sphk 23775374Sbp/* simple_lock_init(&smp->sm_npslock);*/ 23875374Sbp pc = mp->mnt_stat.f_mntfromname; 23975374Sbp pe = pc + sizeof(mp->mnt_stat.f_mntfromname); 24075374Sbp bzero(pc, MNAMELEN); 24175374Sbp *pc++ = '/'; 24275374Sbp *pc++ = '/'; 24375374Sbp pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0); 24475374Sbp if (pc < pe-1) { 24575374Sbp *(pc++) = '@'; 24675374Sbp pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0); 24775374Sbp if (pc < pe - 1) { 24875374Sbp *(pc++) = '/'; 24975374Sbp strncpy(pc, ssp->ss_name, pe - pc - 2); 25075374Sbp } 25175374Sbp } 25275374Sbp vfs_getnewfsid(mp); 253144058Sjeff error = smbfs_root(mp, LK_EXCLUSIVE, &vp, td); 254152466Srodrigc if (error) { 255152466Srodrigc vfs_mount_error(mp, "smbfs_root error: %d", error); 25675374Sbp goto bad; 257152466Srodrigc } 25887194Sbp VOP_UNLOCK(vp, 0, td); 259103936Sjeff SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp)); 26075374Sbp 26175374Sbp#ifdef DIAGNOSTICS 26275374Sbp SMBERROR("mp=%p\n", mp); 26375374Sbp#endif 26475374Sbp return error; 26575374Sbpbad: 26675374Sbp if (smp) { 26775374Sbp if (smp->sm_hash) 26875374Sbp free(smp->sm_hash, M_SMBFSHASH); 26975374Sbp lockdestroy(&smp->sm_hashlock); 27075374Sbp#ifdef SMBFS_USEZONE 27175374Sbp zfree(smbfsmount_zone, smp); 27275374Sbp#else 27375374Sbp free(smp, M_SMBFSDATA); 27475374Sbp#endif 27575374Sbp } 27675374Sbp if (ssp) 27775374Sbp smb_share_put(ssp, &scred); 27875374Sbp return error; 27975374Sbp} 28075374Sbp 28175374Sbp/* Unmount the filesystem described by mp. */ 28275374Sbpstatic int 28387194Sbpsmbfs_unmount(struct mount *mp, int mntflags, struct thread *td) 28475374Sbp{ 28575374Sbp struct smbmount *smp = VFSTOSMBFS(mp); 28675374Sbp struct smb_cred scred; 28775374Sbp int error, flags; 28875374Sbp 28975374Sbp SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags); 29075374Sbp flags = 0; 29175374Sbp if (mntflags & MNT_FORCE) 29275374Sbp flags |= FORCECLOSE; 293107842Stjr /* 294107842Stjr * Keep trying to flush the vnode list for the mount while 295107842Stjr * some are still busy and we are making progress towards 296107842Stjr * making them not busy. This is needed because smbfs vnodes 297107842Stjr * reference their parent directory but may appear after their 298107842Stjr * parent in the list; one pass over the vnode list is not 299107842Stjr * sufficient in this case. 300107842Stjr */ 301107842Stjr do { 302107842Stjr smp->sm_didrele = 0; 303107842Stjr /* There is 1 extra root vnode reference from smbfs_mount(). */ 304132023Salfred error = vflush(mp, 1, flags, td); 305107842Stjr } while (error == EBUSY && smp->sm_didrele != 0); 30675374Sbp if (error) 30775374Sbp return error; 30891406Sjhb smb_makescred(&scred, td, td->td_ucred); 30975374Sbp smb_share_put(smp->sm_share, &scred); 31075374Sbp mp->mnt_data = (qaddr_t)0; 31175374Sbp 31275374Sbp if (smp->sm_hash) 31375374Sbp free(smp->sm_hash, M_SMBFSHASH); 31475374Sbp lockdestroy(&smp->sm_hashlock); 31575374Sbp#ifdef SMBFS_USEZONE 31675374Sbp zfree(smbfsmount_zone, smp); 31775374Sbp#else 31875374Sbp free(smp, M_SMBFSDATA); 31975374Sbp#endif 32075374Sbp mp->mnt_flag &= ~MNT_LOCAL; 32175374Sbp return error; 32275374Sbp} 32375374Sbp 32475374Sbp/* 32575374Sbp * Return locked root vnode of a filesystem 32675374Sbp */ 32775374Sbpstatic int 328144058Sjeffsmbfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td) 32975374Sbp{ 33075374Sbp struct smbmount *smp = VFSTOSMBFS(mp); 33175374Sbp struct vnode *vp; 33275374Sbp struct smbnode *np; 33375374Sbp struct smbfattr fattr; 33491406Sjhb struct ucred *cred = td->td_ucred; 33575374Sbp struct smb_cred scred; 33675374Sbp int error; 33775374Sbp 33875374Sbp if (smp == NULL) { 33975374Sbp SMBERROR("smp == NULL (bug in umount)\n"); 340152466Srodrigc vfs_mount_error(mp, "smp == NULL (bug in umount)"); 34175374Sbp return EINVAL; 34275374Sbp } 34375374Sbp if (smp->sm_root) { 34475374Sbp *vpp = SMBTOV(smp->sm_root); 34587194Sbp return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td); 34675374Sbp } 34787194Sbp smb_makescred(&scred, td, cred); 34875374Sbp error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred); 34975374Sbp if (error) 35075374Sbp return error; 35175374Sbp error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp); 35275374Sbp if (error) 35375374Sbp return error; 354101308Sjeff ASSERT_VOP_LOCKED(vp, "smbfs_root"); 355101308Sjeff vp->v_vflag |= VV_ROOT; 35675374Sbp np = VTOSMB(vp); 35775374Sbp smp->sm_root = np; 35875374Sbp *vpp = vp; 35975374Sbp return 0; 36075374Sbp} 36175374Sbp 362110533Stjr/* 363110533Stjr * Do operations associated with quotas, not supported 364110533Stjr */ 365110533Stjr/* ARGSUSED */ 366110533Stjrstatic int 367110533Stjrsmbfs_quotactl(mp, cmd, uid, arg, td) 368110533Stjr struct mount *mp; 369110533Stjr int cmd; 370110533Stjr uid_t uid; 371110533Stjr caddr_t arg; 372110533Stjr struct thread *td; 373110533Stjr{ 374110533Stjr SMBVDEBUG("return EOPNOTSUPP\n"); 375110533Stjr return EOPNOTSUPP; 376110533Stjr} 377110533Stjr 37875374Sbp/*ARGSUSED*/ 37975374Sbpint 38075374Sbpsmbfs_init(struct vfsconf *vfsp) 38175374Sbp{ 38275374Sbp#ifdef SMBFS_USEZONE 38375374Sbp smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1); 38475374Sbp#endif 38575374Sbp smbfs_pbuf_freecnt = nswbuf / 2 + 1; 38675374Sbp SMBVDEBUG("done.\n"); 38775374Sbp return 0; 38875374Sbp} 38975374Sbp 390110533Stjr/*ARGSUSED*/ 391110533Stjrint 392110533Stjrsmbfs_uninit(struct vfsconf *vfsp) 393110533Stjr{ 394110533Stjr 395110533Stjr SMBVDEBUG("done.\n"); 396110533Stjr return 0; 397110533Stjr} 398110533Stjr 39975374Sbp/* 40075374Sbp * smbfs_statfs call 40175374Sbp */ 40275374Sbpint 40387194Sbpsmbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 40475374Sbp{ 40575374Sbp struct smbmount *smp = VFSTOSMBFS(mp); 40675374Sbp struct smbnode *np = smp->sm_root; 40775374Sbp struct smb_share *ssp = smp->sm_share; 40875374Sbp struct smb_cred scred; 40975374Sbp int error = 0; 41075374Sbp 411152466Srodrigc if (np == NULL) { 412152466Srodrigc vfs_mount_error(mp, "np == NULL"); 41375374Sbp return EINVAL; 414152466Srodrigc } 41575374Sbp 41675374Sbp sbp->f_iosize = SSTOVC(ssp)->vc_txmax; /* optimal transfer block size */ 41791406Sjhb smb_makescred(&scred, td, td->td_ucred); 41875374Sbp 41975374Sbp if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0) 42075374Sbp error = smbfs_smb_statfs2(ssp, sbp, &scred); 42175374Sbp else 42275374Sbp error = smbfs_smb_statfs(ssp, sbp, &scred); 42375374Sbp if (error) 42475374Sbp return error; 42575374Sbp sbp->f_flags = 0; /* copy of mount exported flags */ 42675374Sbp return 0; 42775374Sbp} 428