smbfs_vfsops.c revision 87798
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 87798 2001-12-13 13:08:34Z sheldonh $
33 */
34#include "opt_netsmb.h"
35#ifndef NETSMB
36#error "SMBFS requires option NETSMB"
37#endif
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/proc.h>
42#include <sys/bio.h>
43#include <sys/buf.h>
44#include <sys/kernel.h>
45#include <sys/sysctl.h>
46#include <sys/vnode.h>
47#include <sys/mount.h>
48#include <sys/stat.h>
49#include <sys/malloc.h>
50#include <sys/module.h>
51
52
53#include <netsmb/smb.h>
54#include <netsmb/smb_conn.h>
55#include <netsmb/smb_subr.h>
56#include <netsmb/smb_dev.h>
57
58#include <fs/smbfs/smbfs.h>
59#include <fs/smbfs/smbfs_node.h>
60#include <fs/smbfs/smbfs_subr.h>
61
62int smbfs_debuglevel = 0;
63
64static int smbfs_version = SMBFS_VERSION;
65
66#ifdef SMBFS_USEZONE
67#include <vm/vm.h>
68#include <vm/vm_extern.h>
69#include <vm/vm_zone.h>
70
71vm_zone_t smbfsmount_zone;
72#endif
73
74SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
75SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
76SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
77
78static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
79
80
81static int smbfs_mount(struct mount *, char *, caddr_t,
82			struct nameidata *, struct thread *);
83static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *);
84static int smbfs_root(struct mount *, struct vnode **);
85static int smbfs_start(struct mount *, int, struct thread *);
86static int smbfs_statfs(struct mount *, struct statfs *, struct thread *);
87static int smbfs_sync(struct mount *, int, struct ucred *, struct thread *);
88static int smbfs_unmount(struct mount *, int, struct thread *);
89static int smbfs_init(struct vfsconf *vfsp);
90static int smbfs_uninit(struct vfsconf *vfsp);
91
92#if __FreeBSD_version < 400009
93static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp);
94static int smbfs_fhtovp(struct mount *, struct fid *,
95			struct sockaddr *, struct vnode **, int *,
96			struct ucred **);
97static int smbfs_vptofh(struct vnode *, struct fid *);
98#endif
99
100static struct vfsops smbfs_vfsops = {
101	smbfs_mount,
102	smbfs_start,
103	smbfs_unmount,
104	smbfs_root,
105	smbfs_quotactl,
106	smbfs_statfs,
107	smbfs_sync,
108#if __FreeBSD_version > 400008
109	vfs_stdvget,
110	vfs_stdfhtovp,		/* shouldn't happen */
111	vfs_stdcheckexp,
112	vfs_stdvptofh,		/* shouldn't happen */
113#else
114	smbfs_vget,
115	smbfs_fhtovp,
116	smbfs_vptofh,
117#endif
118	smbfs_init,
119	smbfs_uninit,
120#ifndef FB_RELENG3
121	vfs_stdextattrctl,
122#else
123#define	M_USE_RESERVE	M_KERNEL
124	&sysctl___vfs_smbfs
125#endif
126};
127
128
129VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
130
131MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
132MODULE_DEPEND(smbfs, libiconv, 1, 1, 1);
133MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
134
135int smbfs_pbuf_freecnt = -1;	/* start out unlimited */
136
137static int
138smbfs_mount(struct mount *mp, char *path, caddr_t data,
139	struct nameidata *ndp, struct thread *td)
140{
141	struct smbfs_args args; 	  /* will hold data from mount request */
142	struct smbmount *smp = NULL;
143	struct smb_vc *vcp;
144	struct smb_share *ssp = NULL;
145	struct vnode *vp;
146	struct smb_cred scred;
147#ifndef	FB_CURRENT
148	size_t size;
149#endif
150	int error;
151	char *pc, *pe;
152
153	if (data == NULL) {
154		printf("missing data argument\n");
155		return EINVAL;
156	}
157	if (mp->mnt_flag & MNT_UPDATE) {
158		printf("MNT_UPDATE not implemented");
159		return EOPNOTSUPP;
160	}
161	error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
162	if (error)
163		return error;
164	if (args.version != SMBFS_VERSION) {
165		printf("mount version mismatch: kernel=%d, mount=%d\n",
166		    SMBFS_VERSION, args.version);
167		return EINVAL;
168	}
169	smb_makescred(&scred, td, td->td_proc->p_ucred);
170	error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
171	if (error) {
172		printf("invalid device handle %d (%d)\n", args.dev, error);
173		return error;
174	}
175	vcp = SSTOVC(ssp);
176	smb_share_unlock(ssp, 0, td);
177	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
178
179#ifdef SMBFS_USEZONE
180	smp = zalloc(smbfsmount_zone);
181#else
182        MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA, M_USE_RESERVE);
183#endif
184        if (smp == NULL) {
185                printf("could not alloc smbmount\n");
186                error = ENOMEM;
187		goto bad;
188        }
189	bzero(smp, sizeof(*smp));
190        mp->mnt_data = (qaddr_t)smp;
191	smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
192	if (smp->sm_hash == NULL)
193		goto bad;
194	lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0);
195	smp->sm_share = ssp;
196	smp->sm_root = NULL;
197        smp->sm_args = args;
198	smp->sm_caseopt = args.caseopt;
199	smp->sm_args.file_mode = (smp->sm_args.file_mode &
200			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
201	smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
202			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
203
204/*	simple_lock_init(&smp->sm_npslock);*/
205#ifndef	FB_CURRENT
206	error = copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
207	if (error)
208		goto bad;
209	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
210#endif
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=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
217	if (pc < pe-1) {
218		*(pc++) = '@';
219		pc = index(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	/* protect against invalid mount points */
226	smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
227	vfs_getnewfsid(mp);
228	error = smbfs_root(mp, &vp);
229	if (error)
230		goto bad;
231	VOP_UNLOCK(vp, 0, td);
232	SMBVDEBUG("root.v_usecount = %d\n", vp->v_usecount);
233
234#ifdef DIAGNOSTICS
235	SMBERROR("mp=%p\n", mp);
236#endif
237	return error;
238bad:
239        if (smp) {
240		if (smp->sm_hash)
241			free(smp->sm_hash, M_SMBFSHASH);
242		lockdestroy(&smp->sm_hashlock);
243#ifdef SMBFS_USEZONE
244		zfree(smbfsmount_zone, smp);
245#else
246		free(smp, M_SMBFSDATA);
247#endif
248	}
249	if (ssp)
250		smb_share_put(ssp, &scred);
251        return error;
252}
253
254/* Unmount the filesystem described by mp. */
255static int
256smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
257{
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	flags = 0;
264	if (mntflags & MNT_FORCE)
265		flags |= FORCECLOSE;
266	/* There is 1 extra root vnode reference from smbfs_mount(). */
267	error = vflush(mp, 1, flags);
268	if (error)
269		return error;
270	smb_makescred(&scred, td, td->td_proc->p_ucred);
271	smb_share_put(smp->sm_share, &scred);
272	mp->mnt_data = (qaddr_t)0;
273
274	if (smp->sm_hash)
275		free(smp->sm_hash, M_SMBFSHASH);
276	lockdestroy(&smp->sm_hashlock);
277#ifdef SMBFS_USEZONE
278	zfree(smbfsmount_zone, smp);
279#else
280	free(smp, M_SMBFSDATA);
281#endif
282	mp->mnt_flag &= ~MNT_LOCAL;
283	return error;
284}
285
286/*
287 * Return locked root vnode of a filesystem
288 */
289static int
290smbfs_root(struct mount *mp, struct vnode **vpp)
291{
292	struct smbmount *smp = VFSTOSMBFS(mp);
293	struct vnode *vp;
294	struct smbnode *np;
295	struct smbfattr fattr;
296	struct thread *td = curthread;
297	struct ucred *cred = td->td_proc->p_ucred;
298	struct smb_cred scred;
299	int error;
300
301	if (smp == NULL) {
302		SMBERROR("smp == NULL (bug in umount)\n");
303		return EINVAL;
304	}
305	if (smp->sm_root) {
306		*vpp = SMBTOV(smp->sm_root);
307		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
308	}
309	smb_makescred(&scred, td, cred);
310	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
311	if (error)
312		return error;
313	error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
314	if (error)
315		return error;
316	vp->v_flag |= VROOT;
317	np = VTOSMB(vp);
318	smp->sm_root = np;
319	*vpp = vp;
320	return 0;
321}
322
323/*
324 * Vfs start routine, a no-op.
325 */
326/* ARGSUSED */
327static int
328smbfs_start(mp, flags, td)
329	struct mount *mp;
330	int flags;
331	struct thread *td;
332{
333	SMBVDEBUG("flags=%04x\n", flags);
334	return 0;
335}
336
337/*
338 * Do operations associated with quotas, not supported
339 */
340/* ARGSUSED */
341static int
342smbfs_quotactl(mp, cmd, uid, arg, td)
343	struct mount *mp;
344	int cmd;
345	uid_t uid;
346	caddr_t arg;
347	struct thread *td;
348{
349	SMBVDEBUG("return EOPNOTSUPP\n");
350	return EOPNOTSUPP;
351}
352
353/*ARGSUSED*/
354int
355smbfs_init(struct vfsconf *vfsp)
356{
357#ifndef SMP
358	int name[2];
359	int olen, ncpu, plen, error;
360
361	name[0] = CTL_HW;
362	name[1] = HW_NCPU;
363	error = kernel_sysctl(curthread, name, 2, &ncpu, &olen, NULL, 0, &plen);
364	if (error == 0 && ncpu > 1)
365		printf("warning: smbfs module compiled without SMP support.");
366#endif
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	sbp->f_spare2 = 0;			/* placeholder */
402	smb_makescred(&scred, td, td->td_proc->p_ucred);
403
404	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
405		error = smbfs_smb_statfs2(ssp, sbp, &scred);
406	else
407		error = smbfs_smb_statfs(ssp, sbp, &scred);
408	if (error)
409		return error;
410	sbp->f_flags = 0;		/* copy of mount exported flags */
411	if (sbp != &mp->mnt_stat) {
412		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
413		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
414		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
415		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
416		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
417	}
418	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
419	return 0;
420}
421
422/*
423 * Flush out the buffer cache
424 */
425/* ARGSUSED */
426static int
427smbfs_sync(mp, waitfor, cred, td)
428	struct mount *mp;
429	int waitfor;
430	struct ucred *cred;
431	struct thread *td;
432{
433	struct vnode *vp;
434	int error, allerror = 0;
435	/*
436	 * Force stale buffer cache information to be flushed.
437	 */
438loop:
439	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
440	     vp != NULL;
441	     vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
442		/*
443		 * If the vnode that we are about to sync is no longer
444		 * associated with this mount point, start over.
445		 */
446		if (vp->v_mount != mp)
447			goto loop;
448#ifndef FB_RELENG3
449		if (VOP_ISLOCKED(vp, NULL) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
450#else
451		if (VOP_ISLOCKED(vp) || TAILQ_EMPTY(&vp->v_dirtyblkhd) ||
452#endif
453		    waitfor == MNT_LAZY)
454			continue;
455		if (vget(vp, LK_EXCLUSIVE, td))
456			goto loop;
457		error = VOP_FSYNC(vp, cred, waitfor, td);
458		if (error)
459			allerror = error;
460		vput(vp);
461	}
462	return (allerror);
463}
464
465#if __FreeBSD_version < 400009
466/*
467 * smbfs flat namespace lookup. Unsupported.
468 */
469/* ARGSUSED */
470static int smbfs_vget(mp, ino, vpp)
471	struct mount *mp;
472	ino_t ino;
473	struct vnode **vpp;
474{
475	return (EOPNOTSUPP);
476}
477
478/* ARGSUSED */
479static int smbfs_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
480	struct mount *mp;
481	struct fid *fhp;
482	struct sockaddr *nam;
483	struct vnode **vpp;
484	int *exflagsp;
485	struct ucred **credanonp;
486{
487	return (EINVAL);
488}
489
490/*
491 * Vnode pointer to File handle, should never happen either
492 */
493/* ARGSUSED */
494static int
495smbfs_vptofh(vp, fhp)
496	struct vnode *vp;
497	struct fid *fhp;
498{
499	return (EINVAL);
500}
501
502#endif /* __FreeBSD_version < 400009 */
503