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