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