smbfs_vfsops.c revision 112915
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 112915 2003-04-01 02:42:02Z tjr $
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
70vm_zone_t smbfsmount_zone;
71#endif
72
73SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS filesystem");
74SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
75SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
76
77static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
78
79
80static int smbfs_mount(struct mount *, char *, caddr_t,
81			struct nameidata *, struct thread *);
82static int smbfs_quotactl(struct mount *, int, uid_t, caddr_t, struct thread *);
83static int smbfs_root(struct mount *, struct vnode **);
84static int smbfs_start(struct mount *, int, struct thread *);
85static int smbfs_statfs(struct mount *, struct statfs *, struct thread *);
86static int smbfs_unmount(struct mount *, int, struct thread *);
87static int smbfs_init(struct vfsconf *vfsp);
88static int smbfs_uninit(struct vfsconf *vfsp);
89
90static struct vfsops smbfs_vfsops = {
91	smbfs_mount,
92	smbfs_start,
93	smbfs_unmount,
94	smbfs_root,
95	smbfs_quotactl,
96	smbfs_statfs,
97	vfs_stdsync,
98	vfs_stdvget,
99	vfs_stdfhtovp,		/* shouldn't happen */
100	vfs_stdcheckexp,
101	vfs_stdvptofh,		/* shouldn't happen */
102	smbfs_init,
103	smbfs_uninit,
104	vfs_stdextattrctl
105};
106
107
108VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
109
110MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
111MODULE_DEPEND(smbfs, libiconv, 1, 1, 1);
112MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
113
114int smbfs_pbuf_freecnt = -1;	/* start out unlimited */
115
116static int
117smbfs_mount(struct mount *mp, char *path, caddr_t data,
118	struct nameidata *ndp, struct thread *td)
119{
120	struct smbfs_args args; 	  /* will hold data from mount request */
121	struct smbmount *smp = NULL;
122	struct smb_vc *vcp;
123	struct smb_share *ssp = NULL;
124	struct vnode *vp;
125	struct smb_cred scred;
126	int error;
127	char *pc, *pe;
128
129	if (data == NULL) {
130		printf("missing data argument\n");
131		return EINVAL;
132	}
133	if (mp->mnt_flag & MNT_UPDATE) {
134		printf("MNT_UPDATE not implemented");
135		return EOPNOTSUPP;
136	}
137	error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
138	if (error)
139		return error;
140	if (args.version != SMBFS_VERSION) {
141		printf("mount version mismatch: kernel=%d, mount=%d\n",
142		    SMBFS_VERSION, args.version);
143		return EINVAL;
144	}
145	smb_makescred(&scred, td, td->td_ucred);
146	error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
147	if (error) {
148		printf("invalid device handle %d (%d)\n", args.dev, error);
149		return error;
150	}
151	vcp = SSTOVC(ssp);
152	smb_share_unlock(ssp, 0, td);
153	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
154
155#ifdef SMBFS_USEZONE
156	smp = zalloc(smbfsmount_zone);
157#else
158	MALLOC(smp, struct smbmount*, sizeof(*smp), M_SMBFSDATA,
159	    M_WAITOK|M_USE_RESERVE);
160#endif
161        if (smp == NULL) {
162                printf("could not alloc smbmount\n");
163                error = ENOMEM;
164		goto bad;
165        }
166	bzero(smp, sizeof(*smp));
167        mp->mnt_data = (qaddr_t)smp;
168	smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
169	if (smp->sm_hash == NULL)
170		goto bad;
171	lockinit(&smp->sm_hashlock, PVFS, "smbfsh", 0, 0);
172	smp->sm_share = ssp;
173	smp->sm_root = NULL;
174        smp->sm_args = args;
175	smp->sm_caseopt = args.caseopt;
176	smp->sm_args.file_mode = (smp->sm_args.file_mode &
177			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
178	smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
179			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
180
181/*	simple_lock_init(&smp->sm_npslock);*/
182	pc = mp->mnt_stat.f_mntfromname;
183	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
184	bzero(pc, MNAMELEN);
185	*pc++ = '/';
186	*pc++ = '/';
187	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
188	if (pc < pe-1) {
189		*(pc++) = '@';
190		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
191		if (pc < pe - 1) {
192			*(pc++) = '/';
193			strncpy(pc, ssp->ss_name, pe - pc - 2);
194		}
195	}
196	/* protect against invalid mount points */
197	smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
198	vfs_getnewfsid(mp);
199	error = smbfs_root(mp, &vp);
200	if (error)
201		goto bad;
202	VOP_UNLOCK(vp, 0, td);
203	SMBVDEBUG("root.v_usecount = %d\n", vrefcnt(vp));
204
205#ifdef DIAGNOSTICS
206	SMBERROR("mp=%p\n", mp);
207#endif
208	return error;
209bad:
210        if (smp) {
211		if (smp->sm_hash)
212			free(smp->sm_hash, M_SMBFSHASH);
213		lockdestroy(&smp->sm_hashlock);
214#ifdef SMBFS_USEZONE
215		zfree(smbfsmount_zone, smp);
216#else
217		free(smp, M_SMBFSDATA);
218#endif
219	}
220	if (ssp)
221		smb_share_put(ssp, &scred);
222        return error;
223}
224
225/* Unmount the filesystem described by mp. */
226static int
227smbfs_unmount(struct mount *mp, int mntflags, struct thread *td)
228{
229	struct smbmount *smp = VFSTOSMBFS(mp);
230	struct smb_cred scred;
231	int error, flags;
232
233	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
234	flags = 0;
235	if (mntflags & MNT_FORCE)
236		flags |= FORCECLOSE;
237	/*
238	 * Keep trying to flush the vnode list for the mount while
239	 * some are still busy and we are making progress towards
240	 * making them not busy. This is needed because smbfs vnodes
241	 * reference their parent directory but may appear after their
242	 * parent in the list; one pass over the vnode list is not
243	 * sufficient in this case.
244	 */
245	do {
246		smp->sm_didrele = 0;
247		/* There is 1 extra root vnode reference from smbfs_mount(). */
248		error = vflush(mp, 1, flags);
249	} while (error == EBUSY && smp->sm_didrele != 0);
250	if (error)
251		return error;
252	smb_makescred(&scred, td, td->td_ucred);
253	smb_share_put(smp->sm_share, &scred);
254	mp->mnt_data = (qaddr_t)0;
255
256	if (smp->sm_hash)
257		free(smp->sm_hash, M_SMBFSHASH);
258	lockdestroy(&smp->sm_hashlock);
259#ifdef SMBFS_USEZONE
260	zfree(smbfsmount_zone, smp);
261#else
262	free(smp, M_SMBFSDATA);
263#endif
264	mp->mnt_flag &= ~MNT_LOCAL;
265	return error;
266}
267
268/*
269 * Return locked root vnode of a filesystem
270 */
271static int
272smbfs_root(struct mount *mp, struct vnode **vpp)
273{
274	struct smbmount *smp = VFSTOSMBFS(mp);
275	struct vnode *vp;
276	struct smbnode *np;
277	struct smbfattr fattr;
278	struct thread *td = curthread;
279	struct ucred *cred = td->td_ucred;
280	struct smb_cred scred;
281	int error;
282
283	if (smp == NULL) {
284		SMBERROR("smp == NULL (bug in umount)\n");
285		return EINVAL;
286	}
287	if (smp->sm_root) {
288		*vpp = SMBTOV(smp->sm_root);
289		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY, td);
290	}
291	smb_makescred(&scred, td, cred);
292	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
293	if (error)
294		return error;
295	error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
296	if (error)
297		return error;
298	ASSERT_VOP_LOCKED(vp, "smbfs_root");
299	vp->v_vflag |= VV_ROOT;
300	np = VTOSMB(vp);
301	smp->sm_root = np;
302	*vpp = vp;
303	return 0;
304}
305
306/*
307 * Vfs start routine, a no-op.
308 */
309/* ARGSUSED */
310static int
311smbfs_start(mp, flags, td)
312	struct mount *mp;
313	int flags;
314	struct thread *td;
315{
316	SMBVDEBUG("flags=%04x\n", flags);
317	return 0;
318}
319
320/*
321 * Do operations associated with quotas, not supported
322 */
323/* ARGSUSED */
324static int
325smbfs_quotactl(mp, cmd, uid, arg, td)
326	struct mount *mp;
327	int cmd;
328	uid_t uid;
329	caddr_t arg;
330	struct thread *td;
331{
332	SMBVDEBUG("return EOPNOTSUPP\n");
333	return EOPNOTSUPP;
334}
335
336/*ARGSUSED*/
337int
338smbfs_init(struct vfsconf *vfsp)
339{
340#ifndef SMP
341	int name[2];
342	int olen, ncpu, plen, error;
343
344	name[0] = CTL_HW;
345	name[1] = HW_NCPU;
346	error = kernel_sysctl(curthread, name, 2, &ncpu, &olen, NULL, 0, &plen);
347	if (error == 0 && ncpu > 1)
348		printf("warning: smbfs module compiled without SMP support.");
349#endif
350
351#ifdef SMBFS_USEZONE
352	smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
353#endif
354	smbfs_pbuf_freecnt = nswbuf / 2 + 1;
355	SMBVDEBUG("done.\n");
356	return 0;
357}
358
359/*ARGSUSED*/
360int
361smbfs_uninit(struct vfsconf *vfsp)
362{
363
364	SMBVDEBUG("done.\n");
365	return 0;
366}
367
368/*
369 * smbfs_statfs call
370 */
371int
372smbfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
373{
374	struct smbmount *smp = VFSTOSMBFS(mp);
375	struct smbnode *np = smp->sm_root;
376	struct smb_share *ssp = smp->sm_share;
377	struct smb_cred scred;
378	int error = 0;
379
380	if (np == NULL)
381		return EINVAL;
382
383	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
384	sbp->f_spare2 = 0;			/* placeholder */
385	smb_makescred(&scred, td, td->td_ucred);
386
387	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
388		error = smbfs_smb_statfs2(ssp, sbp, &scred);
389	else
390		error = smbfs_smb_statfs(ssp, sbp, &scred);
391	if (error)
392		return error;
393	sbp->f_flags = 0;		/* copy of mount exported flags */
394	if (sbp != &mp->mnt_stat) {
395		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* filesystem id */
396		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
397		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
398		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
399		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
400	}
401	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
402	return 0;
403}
404