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