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