xref: /dragonfly/sys/vfs/smbfs/smbfs_vfsops.c (revision b4f25088)
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 #if defined(__FreeBSD__) && __FreeBSD_version < 400009
95 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp);
96 static int smbfs_fhtovp(struct mount *, struct fid *,
97 			struct sockaddr *, struct vnode **, int *,
98 			struct ucred **);
99 static int smbfs_vptofh(struct vnode *, struct fid *);
100 #endif
101 
102 static struct vfsops smbfs_vfsops = {
103 	.vfs_mount =    	smbfs_mount,
104 	.vfs_unmount =    	smbfs_unmount,
105 	.vfs_root =    		smbfs_root,
106 	.vfs_statfs =    	smbfs_statfs,
107 	.vfs_sync =    		smbfs_sync,
108 	.vfs_init =    		smbfs_init,
109 	.vfs_uninit =    	smbfs_uninit
110 };
111 
112 
113 VFS_SET(smbfs_vfsops, smbfs, VFCF_NETWORK);
114 MODULE_VERSION(smbfs, 1);
115 
116 MODULE_DEPEND(smbfs, netsmb, NSMB_VERSION, NSMB_VERSION, NSMB_VERSION);
117 MODULE_DEPEND(smbfs, libiconv, 1, 1, 2);
118 MODULE_DEPEND(smbfs, libmchain, 1, 1, 1);
119 
120 long smbfs_pbuf_freecnt = -1;	/* start out unlimited */
121 
122 static int
123 smbfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
124 {
125 	struct smbfs_args args; 	  /* will hold data from mount request */
126 	struct smbmount *smp = NULL;
127 	struct smb_vc *vcp;
128 	struct smb_share *ssp = NULL;
129 	struct vnode *vp;
130 	struct smb_cred scred;
131 	int error;
132 	char *pc, *pe;
133 
134 	if (data == NULL) {
135 		kprintf("missing data argument\n");
136 		return EINVAL;
137 	}
138 	if (mp->mnt_flag & MNT_UPDATE) {
139 		kprintf("MNT_UPDATE not implemented");
140 		return EOPNOTSUPP;
141 	}
142 	error = copyin(data, (caddr_t)&args, sizeof(struct smbfs_args));
143 	if (error)
144 		return error;
145 	if (args.version != SMBFS_VERSION) {
146 		kprintf("mount version mismatch: kernel=%d, mount=%d\n",
147 		    SMBFS_VERSION, args.version);
148 		return EINVAL;
149 	}
150 	smb_makescred(&scred, curthread, cred);
151 	error = smb_dev2share(args.dev, SMBM_EXEC, &scred, &ssp);
152 	if (error) {
153 		kprintf("invalid device handle %d (%d)\n", args.dev, error);
154 		return error;
155 	}
156 	vcp = SSTOVC(ssp);
157 	smb_share_unlock(ssp, 0);
158 	mp->mnt_stat.f_iosize = SSTOVC(ssp)->vc_txmax;
159 
160 #ifdef SMBFS_USEZONE
161 	smp = zalloc(smbfsmount_zone);
162 #else
163         smp = kmalloc(sizeof(*smp), M_SMBFSDATA, M_WAITOK | M_USE_RESERVE);
164 #endif
165         if (smp == NULL) {
166                 kprintf("could not alloc smbmount\n");
167                 error = ENOMEM;
168 		goto bad;
169         }
170 	bzero(smp, sizeof(*smp));
171         mp->mnt_data = (qaddr_t)smp;
172 	smp->sm_cred = crhold(cred);
173 	smp->sm_hash = hashinit(desiredvnodes, M_SMBFSHASH, &smp->sm_hashlen);
174 	if (smp->sm_hash == NULL)
175 		goto bad;
176 	lockinit(&smp->sm_hashlock, "smbfsh", 0, 0);
177 	smp->sm_share = ssp;
178 	smp->sm_root = NULL;
179         smp->sm_args = args;
180 	smp->sm_caseopt = args.caseopt;
181 	smp->sm_args.file_mode = (smp->sm_args.file_mode &
182 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFREG;
183 	smp->sm_args.dir_mode  = (smp->sm_args.dir_mode &
184 			    (S_IRWXU|S_IRWXG|S_IRWXO)) | S_IFDIR;
185 
186 /*	simple_lock_init(&smp->sm_npslock);*/
187 	pc = mp->mnt_stat.f_mntfromname;
188 	pe = pc + sizeof(mp->mnt_stat.f_mntfromname);
189 	bzero(pc, MNAMELEN);
190 	*pc++ = '/';
191 	*pc++ = '/';
192 	pc=index(strncpy(pc, vcp->vc_username, pe - pc - 2), 0);
193 	if (pc < pe-1) {
194 		*(pc++) = '@';
195 		pc = index(strncpy(pc, vcp->vc_srvname, pe - pc - 2), 0);
196 		if (pc < pe - 1) {
197 			*(pc++) = '/';
198 			strncpy(pc, ssp->ss_name, pe - pc - 2);
199 		}
200 	}
201 	/* protect against invalid mount points */
202 	smp->sm_args.mount_point[sizeof(smp->sm_args.mount_point) - 1] = '\0';
203 	vfs_getnewfsid(mp);
204 
205 	vfs_add_vnodeops(mp, &smbfs_vnode_vops, &mp->mnt_vn_norm_ops);
206 
207 	error = smbfs_root(mp, &vp);
208 	if (error)
209 		goto bad;
210 	vn_unlock(vp);
211 	SMBVDEBUG("root.v_sysrefs = %d\n", vp->v_sysref.refcnt);
212 
213 #ifdef DIAGNOSTICS
214 	SMBERROR("mp=%p\n", mp);
215 #endif
216 	return error;
217 bad:
218         if (smp) {
219 		if (smp->sm_cred)
220 			crfree(smp->sm_cred);
221 		if (smp->sm_hash)
222 			hashdestroy(smp->sm_hash, M_SMBFSHASH,
223 			    smp->sm_hashlen);
224 		lockdestroy(&smp->sm_hashlock);
225 #ifdef SMBFS_USEZONE
226 		zfree(smbfsmount_zone, smp);
227 #else
228 		kfree(smp, M_SMBFSDATA);
229 #endif
230 	}
231 	if (ssp)
232 		smb_share_put(ssp, &scred);
233         return error;
234 }
235 
236 /* Unmount the filesystem described by mp. */
237 static int
238 smbfs_unmount(struct mount *mp, int mntflags)
239 {
240 	struct smbmount *smp = VFSTOSMBFS(mp);
241 	struct smb_cred scred;
242 	int error, flags;
243 
244 	SMBVDEBUG("smbfs_unmount: flags=%04x\n", mntflags);
245 	flags = 0;
246 	if (mntflags & MNT_FORCE)
247 		flags |= FORCECLOSE;
248 	/*
249 	 * Keep trying to flush the vnode list for the mount while
250 	 * some are still busy and we are making progress towards
251 	 * making them not busy. This is needed because smbfs vnodes
252 	 * reference their parent directory but may appear after their
253 	 * parent in the list; one pass over the vnode list is not
254 	 * sufficient in this case.
255 	 */
256 	do {
257 		smp->sm_didrele = 0;
258 		/* There is 1 extra root vnode reference from smbfs_mount(). */
259 		error = vflush(mp, 1, flags);
260 	} while (error == EBUSY && smp->sm_didrele != 0);
261 	if (error)
262 		return error;
263 	smb_makescred(&scred, curthread, smp->sm_cred);
264 	smb_share_put(smp->sm_share, &scred);
265 	mp->mnt_data = (qaddr_t)0;
266 
267 	if (smp->sm_cred)
268 		crfree(smp->sm_cred);
269 	if (smp->sm_hash)
270 		hashdestroy(smp->sm_hash, M_SMBFSHASH, smp->sm_hashlen);
271 	lockdestroy(&smp->sm_hashlock);
272 #ifdef SMBFS_USEZONE
273 	zfree(smbfsmount_zone, smp);
274 #else
275 	kfree(smp, M_SMBFSDATA);
276 #endif
277 	mp->mnt_flag &= ~MNT_LOCAL;
278 	return error;
279 }
280 
281 /*
282  * Return locked root vnode of a filesystem
283  */
284 static int
285 smbfs_root(struct mount *mp, struct vnode **vpp)
286 {
287 	struct thread *td = curthread;	/* XXX */
288 	struct smbmount *smp = VFSTOSMBFS(mp);
289 	struct vnode *vp;
290 	struct smbnode *np;
291 	struct smbfattr fattr;
292 	struct ucred *cred;
293 	struct smb_cred scred;
294 	int error;
295 
296 	if (smp == NULL) {
297 		SMBERROR("smp == NULL (bug in umount)\n");
298 		return EINVAL;
299 	}
300 	if (smp->sm_root) {
301 		*vpp = SMBTOV(smp->sm_root);
302 		return vget(*vpp, LK_EXCLUSIVE | LK_RETRY);
303 	}
304 	if (td->td_proc)
305 		cred = td->td_proc->p_ucred;
306 	else
307 		cred = proc0.p_ucred;
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 	vsetflags(vp, VROOT);
317 	np = VTOSMB(vp);
318 	smp->sm_root = np;
319 	*vpp = vp;
320 	return 0;
321 }
322 
323 /*ARGSUSED*/
324 int
325 smbfs_init(struct vfsconf *vfsp)
326 {
327 #ifdef SMBFS_USEZONE
328 	smbfsmount_zone = zinit("SMBFSMOUNT", sizeof(struct smbmount), 0, 0, 1);
329 #endif
330 	smbfs_pbuf_freecnt = nswbuf / 2 + 1;
331 	SMBVDEBUG("done.\n");
332 	return 0;
333 }
334 
335 /*ARGSUSED*/
336 int
337 smbfs_uninit(struct vfsconf *vfsp)
338 {
339 	SMBVDEBUG("done.\n");
340 	return 0;
341 }
342 
343 /*
344  * smbfs_statfs call
345  */
346 int
347 smbfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
348 {
349 	struct smbmount *smp = VFSTOSMBFS(mp);
350 	struct smbnode *np = smp->sm_root;
351 	struct smb_share *ssp = smp->sm_share;
352 	struct smb_cred scred;
353 	int error = 0;
354 
355 	if (np == NULL)
356 		return EINVAL;
357 
358 	sbp->f_iosize = SSTOVC(ssp)->vc_txmax;		/* optimal transfer block size */
359 	sbp->f_spare2 = 0;			/* placeholder */
360 	smb_makescred(&scred, curthread, cred);
361 
362 	if (SMB_DIALECT(SSTOVC(ssp)) >= SMB_DIALECT_LANMAN2_0)
363 		error = smbfs_smb_statfs2(ssp, sbp, &scred);
364 	else
365 		error = smbfs_smb_statfs(ssp, sbp, &scred);
366 	if (error)
367 		return error;
368 	sbp->f_flags = 0;		/* copy of mount exported flags */
369 	if (sbp != &mp->mnt_stat) {
370 		sbp->f_fsid = mp->mnt_stat.f_fsid;	/* file system id */
371 		sbp->f_owner = mp->mnt_stat.f_owner;	/* user that mounted the filesystem */
372 		sbp->f_type = mp->mnt_vfc->vfc_typenum;	/* type of filesystem */
373 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
374 	}
375 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
376 	return 0;
377 }
378 
379 /*
380  * Flush out the buffer cache
381  */
382 /* ARGSUSED */
383 static int
384 smbfs_sync(struct mount *mp, int waitfor)
385 {
386 	struct vnode *vp;
387 	int error, allerror = 0;
388 	/*
389 	 * Force stale buffer cache information to be flushed.
390 	 */
391 loop:
392 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
393 	     vp != NULL;
394 	     vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
395 		/*
396 		 * If the vnode that we are about to sync is no longer
397 		 * associated with this mount point, start over.
398 		 */
399 		if (vp->v_mount != mp)
400 			goto loop;
401 		if (vn_islocked(vp) || RB_EMPTY(&vp->v_rbdirty_tree) ||
402 		    (waitfor & MNT_LAZY))
403 			continue;
404 		if (vget(vp, LK_EXCLUSIVE))
405 			goto loop;
406 		error = VOP_FSYNC(vp, waitfor, 0);
407 		if (error)
408 			allerror = error;
409 		vput(vp);
410 	}
411 	return (allerror);
412 }
413 
414 #if defined(__FreeBSD__) && __FreeBSD_version < 400009
415 /*
416  * smbfs flat namespace lookup. Unsupported.
417  */
418 /* ARGSUSED */
419 static int smbfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
420 {
421 	return (EOPNOTSUPP);
422 }
423 
424 /* ARGSUSED */
425 static int smbfs_fhtovp(struct mount *mp, struct fid *fhp,
426 			struct sockaddr *nam, struct vnode **vpp,
427 			int *exflagsp, struct ucred **credanonp)
428 {
429 	return (EINVAL);
430 }
431 
432 /*
433  * Vnode pointer to File handle, should never happen either
434  */
435 /* ARGSUSED */
436 static int
437 smbfs_vptofh(struct vnode *vp, struct fid *fhp)
438 {
439 	return (EINVAL);
440 }
441 
442 #endif /* __FreeBSD_version < 400009 */
443