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