Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.6
      1 /*	$NetBSD: union_vfsops.c,v 1.6 2003/06/29 15:11:48 thorpej 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.6 2003/06/29 15:11:48 thorpej 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 proc *p = l->l_proc;
     94 	struct vnode *lowerrootvp = NULLVP;
     95 	struct vnode *upperrootvp = NULLVP;
     96 	struct union_mount *um = 0;
     97 	struct ucred *cred = 0;
     98 	char *cp;
     99 	int len;
    100 	size_t size;
    101 
    102 #ifdef UNION_DIAGNOSTIC
    103 	printf("union_mount(mp = %p)\n", mp);
    104 #endif
    105 
    106 	if (mp->mnt_flag & MNT_GETARGS) {
    107 		um = MOUNTTOUNIONMOUNT(mp);
    108 		if (um == NULL)
    109 			return EIO;
    110 		args.target = NULL;
    111 		args.mntflags = um->um_op;
    112 		return copyout(&args, data, sizeof(args));
    113 	}
    114 	/*
    115 	 * Update is a no-op
    116 	 */
    117 	if (mp->mnt_flag & MNT_UPDATE) {
    118 		/*
    119 		 * Need to provide.
    120 		 * 1. a way to convert between rdonly and rdwr mounts.
    121 		 * 2. support for nfs exports.
    122 		 */
    123 		error = EOPNOTSUPP;
    124 		goto bad;
    125 	}
    126 
    127 	/*
    128 	 * Get argument
    129 	 */
    130 	error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
    131 	if (error)
    132 		goto bad;
    133 
    134 	lowerrootvp = mp->mnt_vnodecovered;
    135 	VREF(lowerrootvp);
    136 
    137 	/*
    138 	 * Find upper node.
    139 	 */
    140 	NDINIT(ndp, LOOKUP, FOLLOW,
    141 	       UIO_USERSPACE, args.target, l);
    142 
    143 	if ((error = namei(ndp)) != 0)
    144 		goto bad;
    145 
    146 	upperrootvp = ndp->ni_vp;
    147 
    148 	if (upperrootvp->v_type != VDIR) {
    149 		error = EINVAL;
    150 		goto bad;
    151 	}
    152 
    153 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
    154 				M_UFSMNT, M_WAITOK);	/* XXX */
    155 
    156 	/*
    157 	 * Keep a held reference to the target vnodes.
    158 	 * They are vrele'd in union_unmount.
    159 	 *
    160 	 * Depending on the _BELOW flag, the filesystems are
    161 	 * viewed in a different order.  In effect, this is the
    162 	 * same as providing a mount under option to the mount syscall.
    163 	 */
    164 
    165 	um->um_op = args.mntflags & UNMNT_OPMASK;
    166 	switch (um->um_op) {
    167 	case UNMNT_ABOVE:
    168 		um->um_lowervp = lowerrootvp;
    169 		um->um_uppervp = upperrootvp;
    170 		break;
    171 
    172 	case UNMNT_BELOW:
    173 		um->um_lowervp = upperrootvp;
    174 		um->um_uppervp = lowerrootvp;
    175 		break;
    176 
    177 	case UNMNT_REPLACE:
    178 		vrele(lowerrootvp);
    179 		lowerrootvp = NULLVP;
    180 		um->um_uppervp = upperrootvp;
    181 		um->um_lowervp = lowerrootvp;
    182 		break;
    183 
    184 	default:
    185 		error = EINVAL;
    186 		goto bad;
    187 	}
    188 
    189 	/*
    190 	 * Unless the mount is readonly, ensure that the top layer
    191 	 * supports whiteout operations
    192 	 */
    193 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    194 		error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
    195 		if (error)
    196 			goto bad;
    197 	}
    198 
    199 	um->um_cred = p->p_ucred;
    200 	crhold(um->um_cred);
    201 	um->um_cmode = UN_DIRMODE &~ p->p_cwdi->cwdi_cmask;
    202 
    203 	/*
    204 	 * Depending on what you think the MNT_LOCAL flag might mean,
    205 	 * you may want the && to be || on the conditional below.
    206 	 * At the moment it has been defined that the filesystem is
    207 	 * only local if it is all local, ie the MNT_LOCAL flag implies
    208 	 * that the entire namespace is local.  If you think the MNT_LOCAL
    209 	 * flag implies that some of the files might be stored locally
    210 	 * then you will want to change the conditional.
    211 	 */
    212 	if (um->um_op == UNMNT_ABOVE) {
    213 		if (((um->um_lowervp == NULLVP) ||
    214 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
    215 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
    216 			mp->mnt_flag |= MNT_LOCAL;
    217 	}
    218 
    219 	/*
    220 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
    221 	 * of lookup() which explicitly checks the flag, rather than asking
    222 	 * the filesystem for it's own opinion.  This means, that an update
    223 	 * mount of the underlying filesystem to go from rdonly to rdwr
    224 	 * will leave the unioned view as read-only.
    225 	 */
    226 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
    227 
    228 	mp->mnt_data = um;
    229 	vfs_getnewfsid(mp);
    230 
    231 	error = set_statfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    232 	    mp, l);
    233 	if (error)
    234 		goto bad;
    235 
    236 	switch (um->um_op) {
    237 	case UNMNT_ABOVE:
    238 		cp = "<above>:";
    239 		break;
    240 	case UNMNT_BELOW:
    241 		cp = "<below>:";
    242 		break;
    243 	case UNMNT_REPLACE:
    244 		cp = "";
    245 		break;
    246 	default:
    247 		cp = "<invalid>:";
    248 #ifdef DIAGNOSTIC
    249 		panic("union_mount: bad um_op");
    250 #endif
    251 		break;
    252 	}
    253 	len = strlen(cp);
    254 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    255 
    256 	cp = mp->mnt_stat.f_mntfromname + len;
    257 	len = MNAMELEN - len;
    258 
    259 	(void) copyinstr(args.target, cp, len - 1, &size);
    260 	memset(cp + size, 0, len - size);
    261 
    262 #ifdef UNION_DIAGNOSTIC
    263 	printf("union_mount: from %s, on %s\n",
    264 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    265 #endif
    266 
    267 	/* Setup the readdir hook if it's not set already */
    268 	if (!vn_union_readdir_hook)
    269 		vn_union_readdir_hook = union_readdirhook;
    270 
    271 	return (0);
    272 
    273 bad:
    274 	if (um)
    275 		free(um, M_UFSMNT);
    276 	if (cred)
    277 		crfree(cred);
    278 	if (upperrootvp)
    279 		vrele(upperrootvp);
    280 	if (lowerrootvp)
    281 		vrele(lowerrootvp);
    282 	return (error);
    283 }
    284 
    285 /*
    286  * VFS start.  Nothing needed here - the start routine
    287  * on the underlying filesystem(s) will have been called
    288  * when that filesystem was mounted.
    289  */
    290  /*ARGSUSED*/
    291 int
    292 union_start(mp, flags, l)
    293 	struct mount *mp;
    294 	int flags;
    295 	struct lwp *l;
    296 {
    297 
    298 	return (0);
    299 }
    300 
    301 /*
    302  * Free reference to union layer
    303  */
    304 int
    305 union_unmount(mp, mntflags, l)
    306 	struct mount *mp;
    307 	int mntflags;
    308 	struct lwp *l;
    309 {
    310 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    311 	struct vnode *um_rootvp;
    312 	int error;
    313 	int freeing;
    314 
    315 #ifdef UNION_DIAGNOSTIC
    316 	printf("union_unmount(mp = %p)\n", mp);
    317 #endif
    318 
    319 	if ((error = union_root(mp, &um_rootvp, l)) != 0)
    320 		return (error);
    321 
    322 	/*
    323 	 * Keep flushing vnodes from the mount list.
    324 	 * This is needed because of the un_pvp held
    325 	 * reference to the parent vnode.
    326 	 * If more vnodes have been freed on a given pass,
    327 	 * the try again.  The loop will iterate at most
    328 	 * (d) times, where (d) is the maximum tree depth
    329 	 * in the filesystem.
    330 	 */
    331 	for (freeing = 0; vflush(mp, um_rootvp, 0) != 0;) {
    332 		struct vnode *vp;
    333 		int n;
    334 
    335 		/* count #vnodes held on mount list */
    336 		for (n = 0, vp = mp->mnt_vnodelist.lh_first;
    337 				vp != NULLVP;
    338 				vp = vp->v_mntvnodes.le_next)
    339 			n++;
    340 
    341 		/* if this is unchanged then stop */
    342 		if (n == freeing)
    343 			break;
    344 
    345 		/* otherwise try once more time */
    346 		freeing = n;
    347 	}
    348 
    349 	/*
    350 	 * Ok, now that we've tried doing it gently, get out the hammer.
    351 	 */
    352 
    353 	if (mntflags & MNT_FORCE)
    354 		vflush(mp, um_rootvp, FORCECLOSE);
    355 
    356 
    357 	/* At this point the root vnode should have a single reference */
    358 	if (um_rootvp->v_usecount > 1) {
    359 		vput(um_rootvp);
    360 		return (EBUSY);
    361 	}
    362 
    363 #ifdef UNION_DIAGNOSTIC
    364 	vprint("union root", um_rootvp);
    365 #endif
    366 	/*
    367 	 * Discard references to upper and lower target vnodes.
    368 	 */
    369 	if (um->um_lowervp)
    370 		vrele(um->um_lowervp);
    371 	vrele(um->um_uppervp);
    372 	crfree(um->um_cred);
    373 	/*
    374 	 * Release reference on underlying root vnode
    375 	 */
    376 	vput(um_rootvp);
    377 	/*
    378 	 * And blow it away for future re-use
    379 	 */
    380 	vgone(um_rootvp);
    381 	/*
    382 	 * Finally, throw away the union_mount structure
    383 	 */
    384 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    385 	mp->mnt_data = 0;
    386 	return (0);
    387 }
    388 
    389 int
    390 union_root(mp, vpp, l)
    391 	struct mount *mp;
    392 	struct vnode **vpp;
    393 	struct lwp *l;
    394 {
    395 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    396 	int error;
    397 	int loselock;
    398 
    399 	/*
    400 	 * Return locked reference to root.
    401 	 */
    402 	VREF(um->um_uppervp);
    403 	if ((um->um_op == UNMNT_BELOW) &&
    404 	     VOP_ISLOCKED(um->um_uppervp)) {
    405 		loselock = 1;
    406 	} else {
    407 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    408 		loselock = 0;
    409 	}
    410 	if (um->um_lowervp)
    411 		VREF(um->um_lowervp);
    412 	error = union_allocvp(vpp, mp,
    413 			      (struct vnode *) 0,
    414 			      (struct vnode *) 0,
    415 			      (struct componentname *) 0,
    416 			      um->um_uppervp,
    417 			      um->um_lowervp,
    418 			      1, 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