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