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