Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.64.2.1
      1 /*	$NetBSD: union_vfsops.c,v 1.64.2.1 2012/04/17 00:08:21 yamt 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.64.2.1 2012/04/17 00:08:21 yamt Exp $");
     81 
     82 #include <sys/param.h>
     83 #include <sys/systm.h>
     84 #include <sys/sysctl.h>
     85 #include <sys/time.h>
     86 #include <sys/proc.h>
     87 #include <sys/vnode.h>
     88 #include <sys/mount.h>
     89 #include <sys/namei.h>
     90 #include <sys/malloc.h>
     91 #include <sys/filedesc.h>
     92 #include <sys/queue.h>
     93 #include <sys/stat.h>
     94 #include <sys/kauth.h>
     95 #include <sys/module.h>
     96 
     97 #include <fs/union/union.h>
     98 
     99 MODULE(MODULE_CLASS_VFS, union, NULL);
    100 
    101 VFS_PROTOS(union);
    102 
    103 static struct sysctllog *union_sysctl_log;
    104 
    105 /*
    106  * Mount union filesystem
    107  */
    108 int
    109 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    110 {
    111 	struct lwp *l = curlwp;
    112 	int error = 0;
    113 	struct union_args *args = data;
    114 	struct vnode *lowerrootvp = NULLVP;
    115 	struct vnode *upperrootvp = NULLVP;
    116 	struct union_mount *um = 0;
    117 	const char *cp;
    118 	char *xp;
    119 	int len;
    120 	size_t size;
    121 
    122 	if (*data_len < sizeof *args)
    123 		return EINVAL;
    124 
    125 #ifdef UNION_DIAGNOSTIC
    126 	printf("union_mount(mp = %p)\n", mp);
    127 #endif
    128 
    129 	if (mp->mnt_flag & MNT_GETARGS) {
    130 		um = MOUNTTOUNIONMOUNT(mp);
    131 		if (um == NULL)
    132 			return EIO;
    133 		args->target = NULL;
    134 		args->mntflags = um->um_op;
    135 		*data_len = sizeof *args;
    136 		return 0;
    137 	}
    138 	/*
    139 	 * Update is a no-op
    140 	 */
    141 	if (mp->mnt_flag & MNT_UPDATE) {
    142 		/*
    143 		 * Need to provide.
    144 		 * 1. a way to convert between rdonly and rdwr mounts.
    145 		 * 2. support for nfs exports.
    146 		 */
    147 		error = EOPNOTSUPP;
    148 		goto bad;
    149 	}
    150 
    151 	lowerrootvp = mp->mnt_vnodecovered;
    152 	vref(lowerrootvp);
    153 
    154 	/*
    155 	 * Find upper node.
    156 	 */
    157 	error = namei_simple_user(args->target,
    158 				NSM_FOLLOW_NOEMULROOT, &upperrootvp);
    159 	if (error != 0)
    160 		goto bad;
    161 
    162 	if (upperrootvp->v_type != VDIR) {
    163 		error = EINVAL;
    164 		goto bad;
    165 	}
    166 
    167 	um = (struct union_mount *) malloc(sizeof(struct union_mount),
    168 				M_UFSMNT, M_WAITOK);	/* XXX */
    169 
    170 	/*
    171 	 * Keep a held reference to the target vnodes.
    172 	 * They are vrele'd in union_unmount.
    173 	 *
    174 	 * Depending on the _BELOW flag, the filesystems are
    175 	 * viewed in a different order.  In effect, this is the
    176 	 * same as providing a mount under option to the mount syscall.
    177 	 */
    178 
    179 	um->um_op = args->mntflags & UNMNT_OPMASK;
    180 	switch (um->um_op) {
    181 	case UNMNT_ABOVE:
    182 		um->um_lowervp = lowerrootvp;
    183 		um->um_uppervp = upperrootvp;
    184 		break;
    185 
    186 	case UNMNT_BELOW:
    187 		um->um_lowervp = upperrootvp;
    188 		um->um_uppervp = lowerrootvp;
    189 		break;
    190 
    191 	case UNMNT_REPLACE:
    192 		vrele(lowerrootvp);
    193 		lowerrootvp = NULLVP;
    194 		um->um_uppervp = upperrootvp;
    195 		um->um_lowervp = lowerrootvp;
    196 		break;
    197 
    198 	default:
    199 		error = EINVAL;
    200 		goto bad;
    201 	}
    202 
    203 	mp->mnt_iflag |= IMNT_MPSAFE;
    204 
    205 	/*
    206 	 * Unless the mount is readonly, ensure that the top layer
    207 	 * supports whiteout operations
    208 	 */
    209 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    210 		vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    211 		error = VOP_WHITEOUT(um->um_uppervp,
    212 		    (struct componentname *) 0, LOOKUP);
    213 		VOP_UNLOCK(um->um_uppervp);
    214 		if (error)
    215 			goto bad;
    216 	}
    217 
    218 	um->um_cred = l->l_cred;
    219 	kauth_cred_hold(um->um_cred);
    220 	um->um_cmode = UN_DIRMODE &~ l->l_proc->p_cwdi->cwdi_cmask;
    221 
    222 	/*
    223 	 * Depending on what you think the MNT_LOCAL flag might mean,
    224 	 * you may want the && to be || on the conditional below.
    225 	 * At the moment it has been defined that the filesystem is
    226 	 * only local if it is all local, ie the MNT_LOCAL flag implies
    227 	 * that the entire namespace is local.  If you think the MNT_LOCAL
    228 	 * flag implies that some of the files might be stored locally
    229 	 * then you will want to change the conditional.
    230 	 */
    231 	if (um->um_op == UNMNT_ABOVE) {
    232 		if (((um->um_lowervp == NULLVP) ||
    233 		     (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
    234 		    (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
    235 			mp->mnt_flag |= MNT_LOCAL;
    236 	}
    237 
    238 	/*
    239 	 * Copy in the upper layer's RDONLY flag.  This is for the benefit
    240 	 * of lookup() which explicitly checks the flag, rather than asking
    241 	 * the filesystem for it's own opinion.  This means, that an update
    242 	 * mount of the underlying filesystem to go from rdonly to rdwr
    243 	 * will leave the unioned view as read-only.
    244 	 */
    245 	mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
    246 
    247 	mp->mnt_data = um;
    248 	vfs_getnewfsid(mp);
    249 
    250 	error = set_statvfs_info( path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    251 	    mp->mnt_op->vfs_name, mp, l);
    252 	if (error)
    253 		goto bad;
    254 
    255 	switch (um->um_op) {
    256 	case UNMNT_ABOVE:
    257 		cp = "<above>:";
    258 		break;
    259 	case UNMNT_BELOW:
    260 		cp = "<below>:";
    261 		break;
    262 	case UNMNT_REPLACE:
    263 		cp = "";
    264 		break;
    265 	default:
    266 		cp = "<invalid>:";
    267 #ifdef DIAGNOSTIC
    268 		panic("union_mount: bad um_op");
    269 #endif
    270 		break;
    271 	}
    272 	len = strlen(cp);
    273 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    274 
    275 	xp = mp->mnt_stat.f_mntfromname + len;
    276 	len = MNAMELEN - len;
    277 
    278 	(void) copyinstr(args->target, xp, len - 1, &size);
    279 	memset(xp + size, 0, len - size);
    280 
    281 #ifdef UNION_DIAGNOSTIC
    282 	printf("union_mount: from %s, on %s\n",
    283 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    284 #endif
    285 
    286 	/* Setup the readdir hook if it's not set already */
    287 	if (!vn_union_readdir_hook)
    288 		vn_union_readdir_hook = union_readdirhook;
    289 
    290 	return (0);
    291 
    292 bad:
    293 	if (um)
    294 		free(um, M_UFSMNT);
    295 	if (upperrootvp)
    296 		vrele(upperrootvp);
    297 	if (lowerrootvp)
    298 		vrele(lowerrootvp);
    299 	return (error);
    300 }
    301 
    302 /*
    303  * VFS start.  Nothing needed here - the start routine
    304  * on the underlying filesystem(s) will have been called
    305  * when that filesystem was mounted.
    306  */
    307  /*ARGSUSED*/
    308 int
    309 union_start(struct mount *mp, int flags)
    310 {
    311 
    312 	return (0);
    313 }
    314 
    315 /*
    316  * Free reference to union layer
    317  */
    318 int
    319 union_unmount(struct mount *mp, int mntflags)
    320 {
    321 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    322 	int freeing;
    323 	int error;
    324 
    325 #ifdef UNION_DIAGNOSTIC
    326 	printf("union_unmount(mp = %p)\n", mp);
    327 #endif
    328 
    329 	/*
    330 	 * Keep flushing vnodes from the mount list.
    331 	 * This is needed because of the un_pvp held
    332 	 * reference to the parent vnode.
    333 	 * If more vnodes have been freed on a given pass,
    334 	 * the try again.  The loop will iterate at most
    335 	 * (d) times, where (d) is the maximum tree depth
    336 	 * in the filesystem.
    337 	 */
    338 	for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
    339 		struct vnode *vp;
    340 		int n;
    341 
    342 		/* count #vnodes held on mount list */
    343 		n = 0;
    344 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
    345 			n++;
    346 
    347 		/* if this is unchanged then stop */
    348 		if (n == freeing)
    349 			break;
    350 
    351 		/* otherwise try once more time */
    352 		freeing = n;
    353 	}
    354 
    355 	/*
    356 	 * Ok, now that we've tried doing it gently, get out the hammer.
    357 	 */
    358 
    359 	if (mntflags & MNT_FORCE)
    360 		error = vflush(mp, NULL, FORCECLOSE);
    361 
    362 	if (error)
    363 		return error;
    364 
    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 	kauth_cred_free(um->um_cred);
    372 	/*
    373 	 * Finally, throw away the union_mount structure
    374 	 */
    375 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    376 	mp->mnt_data = NULL;
    377 	return (0);
    378 }
    379 
    380 int
    381 union_root(struct mount *mp, struct vnode **vpp)
    382 {
    383 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    384 	int error;
    385 
    386 	/*
    387 	 * Return locked reference to root.
    388 	 */
    389 	vref(um->um_uppervp);
    390 	vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY);
    391 	if (um->um_lowervp)
    392 		vref(um->um_lowervp);
    393 	error = union_allocvp(vpp, mp, NULL, NULL, NULL,
    394 			      um->um_uppervp, um->um_lowervp, 1);
    395 
    396 	if (error) {
    397 		vput(um->um_uppervp);
    398 		if (um->um_lowervp)
    399 			vrele(um->um_lowervp);
    400 	}
    401 
    402 	return (error);
    403 }
    404 
    405 int
    406 union_statvfs(struct mount *mp, struct statvfs *sbp)
    407 {
    408 	int error;
    409 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    410 	struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
    411 	unsigned long lbsize;
    412 
    413 #ifdef UNION_DIAGNOSTIC
    414 	printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
    415 	    um->um_lowervp, um->um_uppervp);
    416 #endif
    417 
    418 	if (um->um_lowervp) {
    419 		error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
    420 		if (error)
    421 			goto done;
    422 	}
    423 
    424 	/* now copy across the "interesting" information and fake the rest */
    425 	lbsize = sbuf->f_bsize;
    426 	sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
    427 	sbp->f_files = sbuf->f_files - sbuf->f_ffree;
    428 
    429 	error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
    430 	if (error)
    431 		goto done;
    432 
    433 	sbp->f_flag = sbuf->f_flag;
    434 	sbp->f_bsize = sbuf->f_bsize;
    435 	sbp->f_frsize = sbuf->f_frsize;
    436 	sbp->f_iosize = sbuf->f_iosize;
    437 
    438 	/*
    439 	 * The "total" fields count total resources in all layers,
    440 	 * the "free" fields count only those resources which are
    441 	 * free in the upper layer (since only the upper layer
    442 	 * is writable).
    443 	 */
    444 
    445 	if (sbuf->f_bsize != lbsize)
    446 		sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
    447 	sbp->f_blocks += sbuf->f_blocks;
    448 	sbp->f_bfree = sbuf->f_bfree;
    449 	sbp->f_bavail = sbuf->f_bavail;
    450 	sbp->f_bresvd = sbuf->f_bresvd;
    451 	sbp->f_files += sbuf->f_files;
    452 	sbp->f_ffree = sbuf->f_ffree;
    453 	sbp->f_favail = sbuf->f_favail;
    454 	sbp->f_fresvd = sbuf->f_fresvd;
    455 
    456 	copy_statvfs_info(sbp, mp);
    457 done:
    458 	free(sbuf, M_TEMP);
    459 	return error;
    460 }
    461 
    462 /*ARGSUSED*/
    463 int
    464 union_sync(struct mount *mp, int waitfor,
    465     kauth_cred_t cred)
    466 {
    467 
    468 	/*
    469 	 * XXX - Assumes no data cached at union layer.
    470 	 */
    471 	return (0);
    472 }
    473 
    474 /*ARGSUSED*/
    475 int
    476 union_vget(struct mount *mp, ino_t ino,
    477     struct vnode **vpp)
    478 {
    479 
    480 	return (EOPNOTSUPP);
    481 }
    482 
    483 static int
    484 union_renamelock_enter(struct mount *mp)
    485 {
    486 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    487 
    488 	/* Lock just the upper fs, where the action happens. */
    489 	return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
    490 }
    491 
    492 static void
    493 union_renamelock_exit(struct mount *mp)
    494 {
    495 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    496 
    497 	VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
    498 }
    499 
    500 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
    501 
    502 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
    503 	&union_vnodeop_opv_desc,
    504 	NULL,
    505 };
    506 
    507 struct vfsops union_vfsops = {
    508 	MOUNT_UNION,
    509 	sizeof (struct union_args),
    510 	union_mount,
    511 	union_start,
    512 	union_unmount,
    513 	union_root,
    514 	(void *)eopnotsupp,		/* vfs_quotactl */
    515 	union_statvfs,
    516 	union_sync,
    517 	union_vget,
    518 	(void *)eopnotsupp,		/* vfs_fhtovp */
    519 	(void *)eopnotsupp,		/* vfs_vptofh */
    520 	union_init,
    521 	union_reinit,
    522 	union_done,
    523 	NULL,				/* vfs_mountroot */
    524 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    525 	vfs_stdextattrctl,
    526 	(void *)eopnotsupp,		/* vfs_suspendctl */
    527 	union_renamelock_enter,
    528 	union_renamelock_exit,
    529 	(void *)eopnotsupp,
    530 	union_vnodeopv_descs,
    531 	0,				/* vfs_refcount */
    532 	{ NULL, NULL },
    533 };
    534 
    535 static int
    536 union_modcmd(modcmd_t cmd, void *arg)
    537 {
    538 	int error;
    539 
    540 	switch (cmd) {
    541 	case MODULE_CMD_INIT:
    542 		error = vfs_attach(&union_vfsops);
    543 		if (error != 0)
    544 			break;
    545 		sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
    546 			       CTLFLAG_PERMANENT,
    547 			       CTLTYPE_NODE, "vfs", NULL,
    548 			       NULL, 0, NULL, 0,
    549 			       CTL_VFS, CTL_EOL);
    550 		sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
    551 			       CTLFLAG_PERMANENT,
    552 			       CTLTYPE_NODE, "union",
    553 			       SYSCTL_DESCR("Union file system"),
    554 			       NULL, 0, NULL, 0,
    555 			       CTL_VFS, 15, CTL_EOL);
    556 		/*
    557 		 * XXX the "15" above could be dynamic, thereby eliminating
    558 		 * one more instance of the "number to vfs" mapping problem,
    559 		 * but "15" is the order as taken from sys/mount.h
    560 		 */
    561 		break;
    562 	case MODULE_CMD_FINI:
    563 		error = vfs_detach(&union_vfsops);
    564 		if (error != 0)
    565 			break;
    566 		sysctl_teardown(&union_sysctl_log);
    567 		break;
    568 	default:
    569 		error = ENOTTY;
    570 		break;
    571 	}
    572 
    573 	return (error);
    574 }
    575