xref: /netbsd-src/sys/fs/union/union_vfsops.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: union_vfsops.c,v 1.4 2003/04/16 21:44:20 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1994 The Regents of the University of California.
5  * Copyright (c) 1994 Jan-Simon Pendry.
6  * All rights reserved.
7  *
8  * This code is derived from software donated to Berkeley by
9  * Jan-Simon Pendry.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
40  */
41 
42 /*
43  * Union Layer
44  */
45 
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: union_vfsops.c,v 1.4 2003/04/16 21:44:20 christos Exp $");
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/time.h>
52 #include <sys/proc.h>
53 #include <sys/vnode.h>
54 #include <sys/mount.h>
55 #include <sys/namei.h>
56 #include <sys/malloc.h>
57 #include <sys/filedesc.h>
58 #include <sys/queue.h>
59 #include <sys/stat.h>
60 
61 #include <fs/union/union.h>
62 
63 int union_mount __P((struct mount *, const char *, void *, struct nameidata *,
64 		     struct proc *));
65 int union_start __P((struct mount *, int, struct proc *));
66 int union_unmount __P((struct mount *, int, struct proc *));
67 int union_root __P((struct mount *, struct vnode **));
68 int union_quotactl __P((struct mount *, int, uid_t, caddr_t, struct proc *));
69 int union_statfs __P((struct mount *, struct statfs *, struct proc *));
70 int union_sync __P((struct mount *, int, struct ucred *, struct proc *));
71 int union_vget __P((struct mount *, ino_t, struct vnode **));
72 int union_fhtovp __P((struct mount *, struct fid *, struct vnode **));
73 int union_checkexp __P((struct mount *, struct mbuf *, int *,
74 		      struct ucred **));
75 int union_vptofh __P((struct vnode *, struct fid *));
76 int union_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
77 		      struct proc *));
78 
79 /*
80  * Mount union filesystem
81  */
82 int
83 union_mount(mp, path, data, ndp, p)
84 	struct mount *mp;
85 	const char *path;
86 	void *data;
87 	struct nameidata *ndp;
88 	struct proc *p;
89 {
90 	int error = 0;
91 	struct union_args args;
92 	struct vnode *lowerrootvp = NULLVP;
93 	struct vnode *upperrootvp = NULLVP;
94 	struct union_mount *um = 0;
95 	struct ucred *cred = 0;
96 	char *cp;
97 	int len;
98 	size_t size;
99 
100 #ifdef UNION_DIAGNOSTIC
101 	printf("union_mount(mp = %p)\n", mp);
102 #endif
103 
104 	if (mp->mnt_flag & MNT_GETARGS) {
105 		um = MOUNTTOUNIONMOUNT(mp);
106 		if (um == NULL)
107 			return EIO;
108 		args.target = NULL;
109 		args.mntflags = um->um_op;
110 		return copyout(&args, data, sizeof(args));
111 	}
112 	/*
113 	 * Update is a no-op
114 	 */
115 	if (mp->mnt_flag & MNT_UPDATE) {
116 		/*
117 		 * Need to provide.
118 		 * 1. a way to convert between rdonly and rdwr mounts.
119 		 * 2. support for nfs exports.
120 		 */
121 		error = EOPNOTSUPP;
122 		goto bad;
123 	}
124 
125 	/*
126 	 * Get argument
127 	 */
128 	error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
129 	if (error)
130 		goto bad;
131 
132 	lowerrootvp = mp->mnt_vnodecovered;
133 	VREF(lowerrootvp);
134 
135 	/*
136 	 * Find upper node.
137 	 */
138 	NDINIT(ndp, LOOKUP, FOLLOW,
139 	       UIO_USERSPACE, args.target, p);
140 
141 	if ((error = namei(ndp)) != 0)
142 		goto bad;
143 
144 	upperrootvp = ndp->ni_vp;
145 
146 	if (upperrootvp->v_type != VDIR) {
147 		error = EINVAL;
148 		goto bad;
149 	}
150 
151 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
152 				M_UFSMNT, M_WAITOK);	/* XXX */
153 
154 	/*
155 	 * Keep a held reference to the target vnodes.
156 	 * They are vrele'd in union_unmount.
157 	 *
158 	 * Depending on the _BELOW flag, the filesystems are
159 	 * viewed in a different order.  In effect, this is the
160 	 * same as providing a mount under option to the mount syscall.
161 	 */
162 
163 	um->um_op = args.mntflags & UNMNT_OPMASK;
164 	switch (um->um_op) {
165 	case UNMNT_ABOVE:
166 		um->um_lowervp = lowerrootvp;
167 		um->um_uppervp = upperrootvp;
168 		break;
169 
170 	case UNMNT_BELOW:
171 		um->um_lowervp = upperrootvp;
172 		um->um_uppervp = lowerrootvp;
173 		break;
174 
175 	case UNMNT_REPLACE:
176 		vrele(lowerrootvp);
177 		lowerrootvp = NULLVP;
178 		um->um_uppervp = upperrootvp;
179 		um->um_lowervp = lowerrootvp;
180 		break;
181 
182 	default:
183 		error = EINVAL;
184 		goto bad;
185 	}
186 
187 	/*
188 	 * Unless the mount is readonly, ensure that the top layer
189 	 * supports whiteout operations
190 	 */
191 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
192 		error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
193 		if (error)
194 			goto bad;
195 	}
196 
197 	um->um_cred = p->p_ucred;
198 	crhold(um->um_cred);
199 	um->um_cmode = UN_DIRMODE &~ p->p_cwdi->cwdi_cmask;
200 
201 	/*
202 	 * Depending on what you think the MNT_LOCAL flag might mean,
203 	 * you may want the && to be || on the conditional below.
204 	 * At the moment it has been defined that the filesystem is
205 	 * only local if it is all local, ie the MNT_LOCAL flag implies
206 	 * that the entire namespace is local.  If you think the MNT_LOCAL
207 	 * flag implies that some of the files might be stored locally
208 	 * then you will want to change the conditional.
209 	 */
210 	if (um->um_op == UNMNT_ABOVE) {
211 		if (((um->um_lowervp == NULLVP) ||
212 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
213 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
214 			mp->mnt_flag |= MNT_LOCAL;
215 	}
216 
217 	/*
218 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
219 	 * of lookup() which explicitly checks the flag, rather than asking
220 	 * the filesystem for it's own opinion.  This means, that an update
221 	 * mount of the underlying filesystem to go from rdonly to rdwr
222 	 * will leave the unioned view as read-only.
223 	 */
224 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
225 
226 	mp->mnt_data = um;
227 	vfs_getnewfsid(mp);
228 
229 	error = set_statfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
230 	    mp, p);
231 	if (error)
232 		goto bad;
233 
234 	switch (um->um_op) {
235 	case UNMNT_ABOVE:
236 		cp = "<above>:";
237 		break;
238 	case UNMNT_BELOW:
239 		cp = "<below>:";
240 		break;
241 	case UNMNT_REPLACE:
242 		cp = "";
243 		break;
244 	default:
245 		cp = "<invalid>:";
246 #ifdef DIAGNOSTIC
247 		panic("union_mount: bad um_op");
248 #endif
249 		break;
250 	}
251 	len = strlen(cp);
252 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
253 
254 	cp = mp->mnt_stat.f_mntfromname + len;
255 	len = MNAMELEN - len;
256 
257 	(void) copyinstr(args.target, cp, len - 1, &size);
258 	memset(cp + size, 0, len - size);
259 
260 #ifdef UNION_DIAGNOSTIC
261 	printf("union_mount: from %s, on %s\n",
262 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
263 #endif
264 
265 	/* Setup the readdir hook if it's not set already */
266 	if (!vn_union_readdir_hook)
267 		vn_union_readdir_hook = union_readdirhook;
268 
269 	return (0);
270 
271 bad:
272 	if (um)
273 		free(um, M_UFSMNT);
274 	if (cred)
275 		crfree(cred);
276 	if (upperrootvp)
277 		vrele(upperrootvp);
278 	if (lowerrootvp)
279 		vrele(lowerrootvp);
280 	return (error);
281 }
282 
283 /*
284  * VFS start.  Nothing needed here - the start routine
285  * on the underlying filesystem(s) will have been called
286  * when that filesystem was mounted.
287  */
288  /*ARGSUSED*/
289 int
290 union_start(mp, flags, p)
291 	struct mount *mp;
292 	int flags;
293 	struct proc *p;
294 {
295 
296 	return (0);
297 }
298 
299 /*
300  * Free reference to union layer
301  */
302 int
303 union_unmount(mp, mntflags, p)
304 	struct mount *mp;
305 	int mntflags;
306 	struct proc *p;
307 {
308 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
309 	struct vnode *um_rootvp;
310 	int error;
311 	int freeing;
312 
313 #ifdef UNION_DIAGNOSTIC
314 	printf("union_unmount(mp = %p)\n", mp);
315 #endif
316 
317 	if ((error = union_root(mp, &um_rootvp)) != 0)
318 		return (error);
319 
320 	/*
321 	 * Keep flushing vnodes from the mount list.
322 	 * This is needed because of the un_pvp held
323 	 * reference to the parent vnode.
324 	 * If more vnodes have been freed on a given pass,
325 	 * the try again.  The loop will iterate at most
326 	 * (d) times, where (d) is the maximum tree depth
327 	 * in the filesystem.
328 	 */
329 	for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
330 		struct vnode *vp;
331 		int n;
332 
333 		/* count #vnodes held on mount list */
334 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
335 				vp != NULLVP;
336 				vp = vp->v_mntvnodes.le_next)
337 			n++;
338 
339 		/* if this is unchanged then stop */
340 		if (n == freeing)
341 			break;
342 
343 		/* otherwise try once more time */
344 		freeing = n;
345 	}
346 
347 	/*
348 	 * Ok, now that we've tried doing it gently, get out the hammer.
349 	 */
350 
351 	if (mntflags & MNT_FORCE)
352 		vflush(mp, um_rootvp, FORCECLOSE);
353 
354 
355 	/* At this point the root vnode should have a single reference */
356 	if (um_rootvp->v_usecount > 1) {
357 		vput(um_rootvp);
358 		return (EBUSY);
359 	}
360 
361 #ifdef UNION_DIAGNOSTIC
362 	vprint("union root", um_rootvp);
363 #endif
364 	/*
365 	 * Discard references to upper and lower target vnodes.
366 	 */
367 	if (um->um_lowervp)
368 		vrele(um->um_lowervp);
369 	vrele(um->um_uppervp);
370 	crfree(um->um_cred);
371 	/*
372 	 * Release reference on underlying root vnode
373 	 */
374 	vput(um_rootvp);
375 	/*
376 	 * And blow it away for future re-use
377 	 */
378 	vgone(um_rootvp);
379 	/*
380 	 * Finally, throw away the union_mount structure
381 	 */
382 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
383 	mp->mnt_data = 0;
384 	return (0);
385 }
386 
387 int
388 union_root(mp, vpp)
389 	struct mount *mp;
390 	struct vnode **vpp;
391 {
392 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
393 	int error;
394 	int loselock;
395 
396 	/*
397 	 * Return locked reference to root.
398 	 */
399 	VREF(um->um_uppervp);
400 	if ((um->um_op == UNMNT_BELOW) &&
401 	     VOP_ISLOCKED(um->um_uppervp)) {
402 		loselock = 1;
403 	} else {
404 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
405 		loselock = 0;
406 	}
407 	if (um->um_lowervp)
408 		VREF(um->um_lowervp);
409 	error = union_allocvp(vpp, mp,
410 			      (struct vnode *) 0,
411 			      (struct vnode *) 0,
412 			      (struct componentname *) 0,
413 			      um->um_uppervp,
414 			      um->um_lowervp,
415 			      1);
416 
417 	if (error) {
418 		if (!loselock)
419 			VOP_UNLOCK(um->um_uppervp, 0);
420 		vrele(um->um_uppervp);
421 		if (um->um_lowervp)
422 			vrele(um->um_lowervp);
423 	} else {
424 		if (loselock)
425 			VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
426 	}
427 
428 	return (error);
429 }
430 
431 /*ARGSUSED*/
432 int
433 union_quotactl(mp, cmd, uid, arg, p)
434 	struct mount *mp;
435 	int cmd;
436 	uid_t uid;
437 	caddr_t arg;
438 	struct proc *p;
439 {
440 
441 	return (EOPNOTSUPP);
442 }
443 
444 int
445 union_statfs(mp, sbp, p)
446 	struct mount *mp;
447 	struct statfs *sbp;
448 	struct proc *p;
449 {
450 	int error;
451 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
452 	struct statfs mstat;
453 	int lbsize;
454 
455 #ifdef UNION_DIAGNOSTIC
456 	printf("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
457 	    um->um_lowervp, um->um_uppervp);
458 #endif
459 
460 	memset(&mstat, 0, sizeof(mstat));
461 
462 	if (um->um_lowervp) {
463 		error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
464 		if (error)
465 			return (error);
466 	}
467 
468 	/* now copy across the "interesting" information and fake the rest */
469 	lbsize = mstat.f_bsize;
470 	sbp->f_blocks = mstat.f_blocks - mstat.f_bfree;
471 	sbp->f_files = mstat.f_files - mstat.f_ffree;
472 
473 	error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
474 	if (error)
475 		return (error);
476 
477 	sbp->f_type = 0;
478 	sbp->f_flags = mstat.f_flags;
479 	sbp->f_bsize = mstat.f_bsize;
480 	sbp->f_iosize = mstat.f_iosize;
481 
482 	/*
483 	 * The "total" fields count total resources in all layers,
484 	 * the "free" fields count only those resources which are
485 	 * free in the upper layer (since only the upper layer
486 	 * is writable).
487 	 */
488 
489 	if (mstat.f_bsize != lbsize)
490 		sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize;
491 	sbp->f_blocks += mstat.f_blocks;
492 	sbp->f_bfree = mstat.f_bfree;
493 	sbp->f_bavail = mstat.f_bavail;
494 	sbp->f_files += mstat.f_files;
495 	sbp->f_ffree = mstat.f_ffree;
496 
497 	copy_statfs_info(sbp, mp);
498 	return (0);
499 }
500 
501 /*ARGSUSED*/
502 int
503 union_sync(mp, waitfor, cred, p)
504 	struct mount *mp;
505 	int waitfor;
506 	struct ucred *cred;
507 	struct proc *p;
508 {
509 
510 	/*
511 	 * XXX - Assumes no data cached at union layer.
512 	 */
513 	return (0);
514 }
515 
516 /*ARGSUSED*/
517 int
518 union_vget(mp, ino, vpp)
519 	struct mount *mp;
520 	ino_t ino;
521 	struct vnode **vpp;
522 {
523 
524 	return (EOPNOTSUPP);
525 }
526 
527 /*ARGSUSED*/
528 int
529 union_fhtovp(mp, fidp, vpp)
530 	struct mount *mp;
531 	struct fid *fidp;
532 	struct vnode **vpp;
533 {
534 
535 	return (EOPNOTSUPP);
536 }
537 
538 /*ARGSUSED*/
539 int
540 union_checkexp(mp, nam, exflagsp, credanonp)
541 	struct mount *mp;
542 	struct mbuf *nam;
543 	int *exflagsp;
544 	struct ucred **credanonp;
545 {
546 
547 	return (EOPNOTSUPP);
548 }
549 
550 /*ARGSUSED*/
551 int
552 union_vptofh(vp, fhp)
553 	struct vnode *vp;
554 	struct fid *fhp;
555 {
556 
557 	return (EOPNOTSUPP);
558 }
559 
560 int
561 union_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
562 	int *name;
563 	u_int namelen;
564 	void *oldp;
565 	size_t *oldlenp;
566 	void *newp;
567 	size_t newlen;
568 	struct proc *p;
569 {
570 	return (EOPNOTSUPP);
571 }
572 
573 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
574 
575 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
576 	&union_vnodeop_opv_desc,
577 	NULL,
578 };
579 
580 struct vfsops union_vfsops = {
581 	MOUNT_UNION,
582 	union_mount,
583 	union_start,
584 	union_unmount,
585 	union_root,
586 	union_quotactl,
587 	union_statfs,
588 	union_sync,
589 	union_vget,
590 	union_fhtovp,
591 	union_vptofh,
592 	union_init,
593 	NULL,				/* vfs_reinit */
594 	union_done,
595 	union_sysctl,
596 	NULL,				/* vfs_mountroot */
597 	union_checkexp,
598 	union_vnodeopv_descs,
599 };
600