xref: /dragonfly/sys/vfs/smbfs/smbfs_vfsops.c (revision 82730a9c)
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: src/sys/fs/smbfs/smbfs_vfsops.c,v 1.2.2.5 2003/01/17 08:20:26 tjr Exp $
33  */
34 
35 #ifndef KLD_MODULE
36 #include "opt_netsmb.h"
37 #ifndef NETSMB
38 #error "SMBFS requires option NETSMB"
39 #endif
40 #endif
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/stat.h>
50 #include <sys/malloc.h>
51 #include <sys/module.h>
52 
53 
54 #include <netproto/smb/smb.h>
55 #include <netproto/smb/smb_conn.h>
56 #include <netproto/smb/smb_subr.h>
57 #include <netproto/smb/smb_dev.h>
58 
59 #include "smbfs.h"
60 #include "smbfs_node.h"
61 #include "smbfs_subr.h"
62 
63 #include <sys/buf.h>
64 
65 extern struct vop_ops smbfs_vnode_vops;
66 
67 int smbfs_debuglevel = 0;
68 
69 static int smbfs_version = SMBFS_VERSION;
70 
71 #ifdef SMBFS_USEZONE
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_zone.h>
75 
76 vm_zone_t smbfsmount_zone;
77 #endif
78 
79 SYSCTL_NODE(_vfs, OID_AUTO, smbfs, CTLFLAG_RW, 0, "SMB/CIFS file system");
80 SYSCTL_INT(_vfs_smbfs, OID_AUTO, version, CTLFLAG_RD, &smbfs_version, 0, "");
81 SYSCTL_INT(_vfs_smbfs, OID_AUTO, debuglevel, CTLFLAG_RW, &smbfs_debuglevel, 0, "");
82 
83 static MALLOC_DEFINE(M_SMBFSHASH, "SMBFS hash", "SMBFS hash table");
84 
85 
86 static int smbfs_mount(struct mount *, char *, caddr_t, struct ucred *);
87 static int smbfs_root(struct mount *, struct vnode **);
88 static int smbfs_statfs(struct mount *, struct statfs *, struct ucred *);
89 static int smbfs_sync(struct mount *, int);
90 static int smbfs_unmount(struct mount *, int);
91 static int smbfs_init(struct vfsconf *vfsp);
92 static int smbfs_uninit(struct vfsconf *vfsp);
93 
94 static struct vfsops smbfs_vfsops = {
95 	.vfs_mount =    	smbfs_mount,
96 	.vfs_unmount =    	smbfs_unmount,
97 	.vfs_root =    		smbfs_root,
98 	.vfs_statfs =    	smbfs_statfs,
99 	.vfs_sync =    		smbfs_sync,
100 	.vfs_init =    		smbfs_init,
101 	.vfs_uninit =    	smbfs_uninit
102 };
103 
104 
105 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
106 MODULE_VERSION(smbfs, 1);
107 
108 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
109 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
110 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
111 
112 long smbfs_pbuf_freecnt = -1;	/* start out unlimited */
113 
114 static int
115 smbfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
116 {
117 	struct smbfs_args args; 	  /* will hold data from mount request */
118 	struct smbmount *smp = NULL;
119 	struct smb_vc *vcp;
120 	struct smb_share *ssp = NULL;
121 	struct vnode *vp;
122 	struct smb_cred scred;
123 	int error;
124 	char *pc, *pe;
125 
126 	if (data == NULL) {
127 		kprintf("missing data argument\n");
128 		return EINVAL;
129 	}
130 	if (mp->mnt_flag & MNT_UPDATE) {
131 		kprintf("MNT_UPDATE not implemented");
132 		return EOPNOTSUPP;
133 	}
134 	error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
135 	if (error)
136 		return error;
137 	if (args.version != SMBFS_VERSION) {
138 		kprintf("mount version mismatch: kernel=%d, mount=%d\n",
139 		    SMBFS_VERSION, args.version);
140 		return EINVAL;
141 	}
142 	smb_makescred(&scred, curthread, cred);
143 	error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
144 	if (error) {
145 		kprintf("invalid device handle %d (%d)\n", args.dev, error);
146 		return error;
147 	}
148 	vcp = SSTOVC(ssp);
149 	smb_share_unlock(ssp, 0);
150 	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
151 
152 #ifdef SMBFS_USEZONE
153 	smp = zalloc(smbfsmount_zone);
154 #else
155         smp = kmalloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_USE_RESERVE);
156 #endif
157         if (smp == NULL) {
158                 kprintf("could not alloc smbmount\n");
159                 error = ENOMEM;
160 		goto bad;
161         }
162 	bzero(smp, sizeof(*smp));
163         mp->mnt_data = (qaddr_t)smp;
164 	smp->sm_cred = crhold(cred);
165 	smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
166 	if (smp->sm_hash == NULL)
167 		goto bad;
168 	lockinit(&smp->sm_hashlock, "smbfsh", 0, 0);
169 	smp->sm_share = ssp;
170 	smp->sm_root = NULL;
171         smp->sm_args = args;
172 	smp->sm_caseopt = args.caseopt;
173 	smp->sm_args.file_mode = (smp->sm_args.file_mode &
174 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
175 	smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
176 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
177 
178 /*	simple_lock_init(&smp->sm_npslock);*/
179 	pc = mp->mnt_stat.f_mntfromname;
180 	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
181 	bzero(pc, MNAMELEN);
182 	*pc++ = '/';
183 	*pc++ = '/';
184 	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
185 	if (pc < pe-1) {
186 		*(pc++) = '@';
187 		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
188 		if (pc < pe - 1) {
189 			*(pc++) = '/';
190 			strncpy(pc, ssp->ss_name, pe - pc - 2);
191 		}
192 	}
193 	/* protect against invalid mount points */
194 	smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
195 	vfs_getnewfsid(mp);
196 
197 	vfs_add_vnodeops(mp, &smbfs_vnode_vops, &mp->mnt_vn_norm_ops);
198 
199 	error = smbfs_root(mp, &vp);
200 	if (error)
201 		goto bad;
202 	vn_unlock(vp);
203 	SMBVDEBUG("root.v_refcnt = %08x\n", vp->v_refcnt);
204 
205 #ifdef DIAGNOSTICS
206 	SMBERROR("mp=%p\n", mp);
207 #endif
208 	return error;
209 bad:
210         if (smp) {
211 		if (smp->sm_cred)
212 			crfree(smp->sm_cred);
213 		if (smp->sm_hash)
214 			hashdestroy(smp->sm_hash, M_SMBFSHASH,
215 			    smp->sm_hashlen);
216 		lockdestroy(&smp->sm_hashlock);
217 #ifdef SMBFS_USEZONE
218 		zfree(smbfsmount_zone, smp);
219 #else
220 		kfree(smp, M_SMBFSDATA);
221 #endif
222 	}
223 	if (ssp)
224 		smb_share_put(ssp, &scred);
225         return error;
226 }
227 
228 /* Unmount the filesystem described by mp. */
229 static int
230 smbfs_unmount(struct mount *mp, int mntflags)
231 {
232 	struct smbmount *smp = VFSTOSMBFS(mp);
233 	struct smb_cred scred;
234 	int error, flags;
235 
236 	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
237 	flags = 0;
238 	if (mntflags & MNT_FORCE)
239 		flags |= FORCECLOSE;
240 	/*
241 	 * Keep trying to flush the vnode list for the mount while
242 	 * some are still busy and we are making progress towards
243 	 * making them not busy. This is needed because smbfs vnodes
244 	 * reference their parent directory but may appear after their
245 	 * parent in the list; one pass over the vnode list is not
246 	 * sufficient in this case.
247 	 */
248 	do {
249 		smp->sm_didrele = 0;
250 		/* There is 1 extra root vnode reference from smbfs_mount(). */
251 		error = vflush(mp, 1, flags);
252 	} while (error == EBUSY && smp->sm_didrele != 0);
253 	if (error)
254 		return error;
255 	smb_makescred(&scred, curthread, smp->sm_cred);
256 	smb_share_put(smp->sm_share, &scred);
257 	mp->mnt_data = (qaddr_t)0;
258 
259 	if (smp->sm_cred)
260 		crfree(smp->sm_cred);
261 	if (smp->sm_hash)
262 		hashdestroy(smp->sm_hash, M_SMBFSHASH, smp->sm_hashlen);
263 	lockdestroy(&smp->sm_hashlock);
264 #ifdef SMBFS_USEZONE
265 	zfree(smbfsmount_zone, smp);
266 #else
267 	kfree(smp, M_SMBFSDATA);
268 #endif
269 	mp->mnt_flag &= ~MNT_LOCAL;
270 	return error;
271 }
272 
273 /*
274  * Return locked root vnode of a filesystem
275  */
276 static int
277 smbfs_root(struct mount *mp, struct vnode **vpp)
278 {
279 	struct thread *td = curthread;	/* XXX */
280 	struct smbmount *smp = VFSTOSMBFS(mp);
281 	struct vnode *vp;
282 	struct smbnode *np;
283 	struct smbfattr fattr;
284 	struct ucred *cred;
285 	struct smb_cred scred;
286 	int error;
287 
288 	if (smp == NULL) {
289 		SMBERROR("smp == NULL (bug in umount)\n");
290 		return EINVAL;
291 	}
292 	if (smp->sm_root) {
293 		*vpp = SMBTOV(smp->sm_root);
294 		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY);
295 	}
296 	if (td->td_proc)
297 		cred = td->td_proc->p_ucred;
298 	else
299 		cred = proc0.p_ucred;
300 
301 	smb_makescred(&scred, td, cred);
302 	error = smbfs_smb_lookup(NULL, NULL, 0, &fattr, &scred);
303 	if (error)
304 		return error;
305 	error = smbfs_nget(mp, NULL, "TheRooT", 7, &fattr, &vp);
306 	if (error)
307 		return error;
308 	vsetflags(vp, VROOT);
309 	np = VTOSMB(vp);
310 	smp->sm_root = np;
311 	*vpp = vp;
312 	return 0;
313 }
314 
315 /*ARGSUSED*/
316 int
317 smbfs_init(struct vfsconf *vfsp)
318 {
319 #ifdef SMBFS_USEZONE
320 	smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
321 #endif
322 	smbfs_pbuf_freecnt = nswbuf / 2 + 1;
323 	SMBVDEBUG("done.\n");
324 	return 0;
325 }
326 
327 /*ARGSUSED*/
328 int
329 smbfs_uninit(struct vfsconf *vfsp)
330 {
331 	SMBVDEBUG("done.\n");
332 	return 0;
333 }
334 
335 /*
336  * smbfs_statfs call
337  */
338 int
339 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
340 {
341 	struct smbmount *smp = VFSTOSMBFS(mp);
342 	struct smbnode *np = smp->sm_root;
343 	struct smb_share *ssp = smp->sm_share;
344 	struct smb_cred scred;
345 	int error = 0;
346 
347 	if (np == NULL)
348 		return EINVAL;
349 
350 	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
351 	sbp->f_spare2 = 0;			/* placeholder */
352 	smb_makescred(&scred, curthread, cred);
353 
354 	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
355 		error = smbfs_smb_statfs2(ssp, sbp, &scred);
356 	else
357 		error = smbfs_smb_statfs(ssp, sbp, &scred);
358 	if (error)
359 		return error;
360 	sbp->f_flags = 0;		/* copy of mount exported flags */
361 	if (sbp != &mp->mnt_stat) {
362 		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
363 		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
364 		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
365 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
366 	}
367 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
368 	return 0;
369 }
370 
371 /*
372  * Flush out the buffer cache
373  */
374 /* ARGSUSED */
375 static int
376 smbfs_sync(struct mount *mp, int waitfor)
377 {
378 	struct vnode *vp;
379 	int error, allerror = 0;
380 	/*
381 	 * Force stale buffer cache information to be flushed.
382 	 */
383 loop:
384 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
385 	     vp != NULL;
386 	     vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
387 		/*
388 		 * If the vnode that we are about to sync is no longer
389 		 * associated with this mount point, start over.
390 		 */
391 		if (vp->v_mount != mp)
392 			goto loop;
393 		if (vn_islocked(vp) || RB_EMPTY(&vp->v_rbdirty_tree) ||
394 		    (waitfor & MNT_LAZY))
395 			continue;
396 		if (vget(vp, LK_EXCLUSIVE))
397 			goto loop;
398 		error = VOP_FSYNC(vp, waitfor, 0);
399 		if (error)
400 			allerror = error;
401 		vput(vp);
402 	}
403 	return (allerror);
404 }
405