smbfs_vfsops.c revision 162647
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 162647 2006-09-26 04:12:49Z tegge $
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
111153400Sdes	error = copyin(data, &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,
233153121Savatar	    "nolong", &smp->sm_flags, SMBFS_MOUNT_NO_LONG);
234138490Sphk
23575374Sbp/*	simple_lock_init(&smp->sm_npslock);*/
23675374Sbp	pc = mp->mnt_stat.f_mntfromname;
23775374Sbp	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
23875374Sbp	bzero(pc, MNAMELEN);
23975374Sbp	*pc++ = '/';
24075374Sbp	*pc++ = '/';
24175374Sbp	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
24275374Sbp	if (pc < pe-1) {
24375374Sbp		*(pc++) = '@';
24475374Sbp		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
24575374Sbp		if (pc < pe - 1) {
24675374Sbp			*(pc++) = '/';
24775374Sbp			strncpy(pc, ssp->ss_name, pe - pc - 2);
24875374Sbp		}
24975374Sbp	}
25075374Sbp	vfs_getnewfsid(mp);
251144058Sjeff	error = smbfs_root(mp, LK_EXCLUSIVE, &vp, td);
252152466Srodrigc	if (error) {
253152466Srodrigc		vfs_mount_error(mp, "smbfs_root error: %d", error);
25475374Sbp		goto bad;
255152466Srodrigc	}
25687194Sbp	VOP_UNLOCK(vp, 0, td);
257103936Sjeff	SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
25875374Sbp
25975374Sbp#ifdef DIAGNOSTICS
26075374Sbp	SMBERROR("mp=%p\n", mp);
26175374Sbp#endif
26275374Sbp	return error;
26375374Sbpbad:
26475374Sbp        if (smp) {
26575374Sbp		if (smp->sm_hash)
26675374Sbp			free(smp->sm_hash, M_SMBFSHASH);
26775374Sbp		lockdestroy(&smp->sm_hashlock);
26875374Sbp#ifdef SMBFS_USEZONE
26975374Sbp		zfree(smbfsmount_zone, smp);
27075374Sbp#else
27175374Sbp		free(smp, M_SMBFSDATA);
27275374Sbp#endif
27375374Sbp	}
27475374Sbp	if (ssp)
27575374Sbp		smb_share_put(ssp, &scred);
27675374Sbp        return error;
27775374Sbp}
27875374Sbp
27975374Sbp/* Unmount the filesystem described by mp. */
28075374Sbpstatic int
28187194Sbpsmbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
28275374Sbp{
28375374Sbp	struct smbmount *smp = VFSTOSMBFS(mp);
28475374Sbp	struct smb_cred scred;
28575374Sbp	int error, flags;
28675374Sbp
28775374Sbp	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
28875374Sbp	flags = 0;
28975374Sbp	if (mntflags & MNT_FORCE)
29075374Sbp		flags |= FORCECLOSE;
291107842Stjr	/*
292107842Stjr	 * Keep trying to flush the vnode list for the mount while
293107842Stjr	 * some are still busy and we are making progress towards
294107842Stjr	 * making them not busy. This is needed because smbfs vnodes
295107842Stjr	 * reference their parent directory but may appear after their
296107842Stjr	 * parent in the list; one pass over the vnode list is not
297107842Stjr	 * sufficient in this case.
298107842Stjr	 */
299107842Stjr	do {
300107842Stjr		smp->sm_didrele = 0;
301107842Stjr		/* There is 1 extra root vnode reference from smbfs_mount(). */
302132023Salfred		error = vflush(mp, 1, flags, td);
303107842Stjr	} while (error == EBUSY && smp->sm_didrele != 0);
30475374Sbp	if (error)
30575374Sbp		return error;
30691406Sjhb	smb_makescred(&scred, td, td->td_ucred);
307160437Sjhb	error = smb_share_lock(smp->sm_share, LK_EXCLUSIVE, td);
308160437Sjhb	if (error)
309160437Sjhb		return error;
31075374Sbp	smb_share_put(smp->sm_share, &scred);
31175374Sbp	mp->mnt_data = (qaddr_t)0;
31275374Sbp
31375374Sbp	if (smp->sm_hash)
31475374Sbp		free(smp->sm_hash, M_SMBFSHASH);
31575374Sbp	lockdestroy(&smp->sm_hashlock);
31675374Sbp#ifdef SMBFS_USEZONE
31775374Sbp	zfree(smbfsmount_zone, smp);
31875374Sbp#else
31975374Sbp	free(smp, M_SMBFSDATA);
32075374Sbp#endif
321162647Stegge	MNT_ILOCK(mp);
32275374Sbp	mp->mnt_flag &= ~MNT_LOCAL;
323162647Stegge	MNT_IUNLOCK(mp);
32475374Sbp	return error;
32575374Sbp}
32675374Sbp
32775374Sbp/*
32875374Sbp * Return locked root vnode of a filesystem
32975374Sbp */
33075374Sbpstatic int
331144058Sjeffsmbfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
33275374Sbp{
33375374Sbp	struct smbmount *smp = VFSTOSMBFS(mp);
33475374Sbp	struct vnode *vp;
33575374Sbp	struct smbnode *np;
33675374Sbp	struct smbfattr fattr;
33791406Sjhb	struct ucred *cred = td->td_ucred;
33875374Sbp	struct smb_cred scred;
33975374Sbp	int error;
34075374Sbp
34175374Sbp	if (smp == NULL) {
34275374Sbp		SMBERROR("smp == NULL (bug in umount)\n");
343152466Srodrigc		vfs_mount_error(mp, "smp == NULL (bug in umount)");
34475374Sbp		return EINVAL;
34575374Sbp	}
34675374Sbp	if (smp->sm_root) {
34775374Sbp		*vpp = SMBTOV(smp->sm_root);
34887194Sbp		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
34975374Sbp	}
35087194Sbp	smb_makescred(&scred, td, cred);
35175374Sbp	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
35275374Sbp	if (error)
35375374Sbp		return error;
35475374Sbp	error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
35575374Sbp	if (error)
35675374Sbp		return error;
357101308Sjeff	ASSERT_VOP_LOCKED(vp, "smbfs_root");
358101308Sjeff	vp->v_vflag |= VV_ROOT;
35975374Sbp	np = VTOSMB(vp);
36075374Sbp	smp->sm_root = np;
36175374Sbp	*vpp = vp;
36275374Sbp	return 0;
36375374Sbp}
36475374Sbp
365110533Stjr/*
366110533Stjr * Do operations associated with quotas, not supported
367110533Stjr */
368110533Stjr/* ARGSUSED */
369110533Stjrstatic int
370110533Stjrsmbfs_quotactl(mp, cmd, uid, arg, td)
371110533Stjr	struct mount *mp;
372110533Stjr	int cmd;
373110533Stjr	uid_t uid;
374153400Sdes	void *arg;
375110533Stjr	struct thread *td;
376110533Stjr{
377110533Stjr	SMBVDEBUG("return EOPNOTSUPP\n");
378110533Stjr	return EOPNOTSUPP;
379110533Stjr}
380110533Stjr
38175374Sbp/*ARGSUSED*/
38275374Sbpint
38375374Sbpsmbfs_init(struct vfsconf *vfsp)
38475374Sbp{
38575374Sbp#ifdef SMBFS_USEZONE
38675374Sbp	smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
38775374Sbp#endif
38875374Sbp	smbfs_pbuf_freecnt = nswbuf / 2 + 1;
38975374Sbp	SMBVDEBUG("done.\n");
39075374Sbp	return 0;
39175374Sbp}
39275374Sbp
393110533Stjr/*ARGSUSED*/
394110533Stjrint
395110533Stjrsmbfs_uninit(struct vfsconf *vfsp)
396110533Stjr{
397110533Stjr
398110533Stjr	SMBVDEBUG("done.\n");
399110533Stjr	return 0;
400110533Stjr}
401110533Stjr
40275374Sbp/*
40375374Sbp * smbfs_statfs call
40475374Sbp */
40575374Sbpint
40687194Sbpsmbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
40775374Sbp{
40875374Sbp	struct smbmount *smp = VFSTOSMBFS(mp);
40975374Sbp	struct smbnode *np = smp->sm_root;
41075374Sbp	struct smb_share *ssp = smp->sm_share;
41175374Sbp	struct smb_cred scred;
41275374Sbp	int error = 0;
41375374Sbp
414152466Srodrigc	if (np == NULL) {
415152466Srodrigc		vfs_mount_error(mp, "np == NULL");
41675374Sbp		return EINVAL;
417152466Srodrigc	}
41875374Sbp
41975374Sbp	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
42091406Sjhb	smb_makescred(&scred, td, td->td_ucred);
42175374Sbp
42275374Sbp	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
42375374Sbp		error = smbfs_smb_statfs2(ssp, sbp, &scred);
42475374Sbp	else
42575374Sbp		error = smbfs_smb_statfs(ssp, sbp, &scred);
42675374Sbp	if (error)
42775374Sbp		return error;
42875374Sbp	sbp->f_flags = 0;		/* copy of mount exported flags */
42975374Sbp	return 0;
43075374Sbp}
431