Home | History | Annotate | Line # | Download | only in union
union_vfsops.c revision 1.68.2.2
      1 /*	$NetBSD: union_vfsops.c,v 1.68.2.2 2017/12/03 11:38:44 jdolecek 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.68.2.2 2017/12/03 11:38:44 jdolecek 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 <miscfs/genfs/genfs.h>
     98 #include <fs/union/union.h>
     99 
    100 MODULE(MODULE_CLASS_VFS, union, NULL);
    101 
    102 static struct sysctllog *union_sysctl_log;
    103 
    104 /*
    105  * Mount union filesystem
    106  */
    107 int
    108 union_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    109 {
    110 	struct lwp *l = curlwp;
    111 	int error = 0;
    112 	struct union_args *args = data;
    113 	struct vnode *lowerrootvp = NULLVP;
    114 	struct vnode *upperrootvp = NULLVP;
    115 	struct union_mount *um = 0;
    116 	const char *cp;
    117 	char *xp;
    118 	int len;
    119 	size_t size;
    120 
    121 	if (args == NULL)
    122 		return EINVAL;
    123 	if (*data_len < sizeof *args)
    124 		return EINVAL;
    125 
    126 #ifdef UNION_DIAGNOSTIC
    127 	printf("union_mount(mp = %p)\n", mp);
    128 #endif
    129 
    130 	if (mp->mnt_flag & MNT_GETARGS) {
    131 		um = MOUNTTOUNIONMOUNT(mp);
    132 		if (um == NULL)
    133 			return EIO;
    134 		args->target = NULL;
    135 		args->mntflags = um->um_op;
    136 		*data_len = sizeof *args;
    137 		return 0;
    138 	}
    139 	/*
    140 	 * Update is a no-op
    141 	 */
    142 	if (mp->mnt_flag & MNT_UPDATE) {
    143 		/*
    144 		 * Need to provide.
    145 		 * 1. a way to convert between rdonly and rdwr mounts.
    146 		 * 2. support for nfs exports.
    147 		 */
    148 		error = EOPNOTSUPP;
    149 		goto bad;
    150 	}
    151 
    152 	lowerrootvp = mp->mnt_vnodecovered;
    153 	vref(lowerrootvp);
    154 
    155 	/*
    156 	 * Find upper node.
    157 	 */
    158 	error = namei_simple_user(args->target,
    159 				NSM_FOLLOW_NOEMULROOT, &upperrootvp);
    160 	if (error != 0)
    161 		goto bad;
    162 
    163 	if (upperrootvp->v_type != VDIR) {
    164 		error = EINVAL;
    165 		goto bad;
    166 	}
    167 
    168 	um = kmem_zalloc(sizeof(struct union_mount), KM_SLEEP);
    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 its 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 	mp->mnt_lower = um->um_uppervp->v_mount;
    256 
    257 	switch (um->um_op) {
    258 	case UNMNT_ABOVE:
    259 		cp = "<above>:";
    260 		break;
    261 	case UNMNT_BELOW:
    262 		cp = "<below>:";
    263 		break;
    264 	case UNMNT_REPLACE:
    265 		cp = "";
    266 		break;
    267 	default:
    268 		cp = "<invalid>:";
    269 #ifdef DIAGNOSTIC
    270 		panic("union_mount: bad um_op");
    271 #endif
    272 		break;
    273 	}
    274 	len = strlen(cp);
    275 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    276 
    277 	xp = mp->mnt_stat.f_mntfromname + len;
    278 	len = MNAMELEN - len;
    279 
    280 	(void) copyinstr(args->target, xp, len - 1, &size);
    281 	memset(xp + size, 0, len - size);
    282 
    283 #ifdef UNION_DIAGNOSTIC
    284 	printf("union_mount: from %s, on %s\n",
    285 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    286 #endif
    287 
    288 	/* Setup the readdir hook if it's not set already */
    289 	if (!vn_union_readdir_hook)
    290 		vn_union_readdir_hook = union_readdirhook;
    291 
    292 	return (0);
    293 
    294 bad:
    295 	if (um)
    296 		kmem_free(um, sizeof(struct union_mount));
    297 	if (upperrootvp)
    298 		vrele(upperrootvp);
    299 	if (lowerrootvp)
    300 		vrele(lowerrootvp);
    301 	return (error);
    302 }
    303 
    304 /*
    305  * VFS start.  Nothing needed here - the start routine
    306  * on the underlying filesystem(s) will have been called
    307  * when that filesystem was mounted.
    308  */
    309  /*ARGSUSED*/
    310 int
    311 union_start(struct mount *mp, int flags)
    312 {
    313 
    314 	return (0);
    315 }
    316 
    317 /*
    318  * Free reference to union layer
    319  */
    320 static bool
    321 union_unmount_selector(void *cl, struct vnode *vp)
    322 {
    323 	int *count = cl;
    324 
    325 	KASSERT(mutex_owned(vp->v_interlock));
    326 
    327 	*count += 1;
    328 	return false;
    329 }
    330 
    331 int
    332 union_unmount(struct mount *mp, int mntflags)
    333 {
    334 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    335 	int freeing;
    336 	int error;
    337 
    338 #ifdef UNION_DIAGNOSTIC
    339 	printf("union_unmount(mp = %p)\n", mp);
    340 #endif
    341 
    342 	/*
    343 	 * Keep flushing vnodes from the mount list.
    344 	 * This is needed because of the un_pvp held
    345 	 * reference to the parent vnode.
    346 	 * If more vnodes have been freed on a given pass,
    347 	 * the try again.  The loop will iterate at most
    348 	 * (d) times, where (d) is the maximum tree depth
    349 	 * in the filesystem.
    350 	 */
    351 	for (freeing = 0; (error = vflush(mp, NULL, 0)) != 0;) {
    352 		struct vnode_iterator *marker;
    353 		int n;
    354 
    355 		/* count #vnodes held on mount list */
    356 		n = 0;
    357 		vfs_vnode_iterator_init(mp, &marker);
    358 		vfs_vnode_iterator_next(marker, union_unmount_selector, &n);
    359 		vfs_vnode_iterator_destroy(marker);
    360 
    361 		/* if this is unchanged then stop */
    362 		if (n == freeing)
    363 			break;
    364 
    365 		/* otherwise try once more time */
    366 		freeing = n;
    367 	}
    368 
    369 	/*
    370 	 * Ok, now that we've tried doing it gently, get out the hammer.
    371 	 */
    372 
    373 	if (mntflags & MNT_FORCE)
    374 		error = vflush(mp, NULL, FORCECLOSE);
    375 
    376 	if (error)
    377 		return error;
    378 
    379 	/*
    380 	 * Discard references to upper and lower target vnodes.
    381 	 */
    382 	if (um->um_lowervp)
    383 		vrele(um->um_lowervp);
    384 	vrele(um->um_uppervp);
    385 	kauth_cred_free(um->um_cred);
    386 	/*
    387 	 * Finally, throw away the union_mount structure
    388 	 */
    389 	kmem_free(um, sizeof(struct union_mount));
    390 	mp->mnt_data = NULL;
    391 	return 0;
    392 }
    393 
    394 int
    395 union_root(struct mount *mp, struct vnode **vpp)
    396 {
    397 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    398 	int error;
    399 
    400 	/*
    401 	 * Return locked reference to root.
    402 	 */
    403 	vref(um->um_uppervp);
    404 	if (um->um_lowervp)
    405 		vref(um->um_lowervp);
    406 	error = union_allocvp(vpp, mp, NULL, NULL, NULL,
    407 			      um->um_uppervp, um->um_lowervp, 1);
    408 
    409 	if (error) {
    410 		vrele(um->um_uppervp);
    411 		if (um->um_lowervp)
    412 			vrele(um->um_lowervp);
    413 		return error;
    414 	}
    415 
    416 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
    417 
    418 	return 0;
    419 }
    420 
    421 int
    422 union_statvfs(struct mount *mp, struct statvfs *sbp)
    423 {
    424 	int error;
    425 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    426 	struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
    427 	unsigned long lbsize;
    428 
    429 #ifdef UNION_DIAGNOSTIC
    430 	printf("union_statvfs(mp = %p, lvp = %p, uvp = %p)\n", mp,
    431 	    um->um_lowervp, um->um_uppervp);
    432 #endif
    433 
    434 	if (um->um_lowervp) {
    435 		error = VFS_STATVFS(um->um_lowervp->v_mount, sbuf);
    436 		if (error)
    437 			goto done;
    438 	}
    439 
    440 	/* now copy across the "interesting" information and fake the rest */
    441 	lbsize = sbuf->f_bsize;
    442 	sbp->f_blocks = sbuf->f_blocks - sbuf->f_bfree;
    443 	sbp->f_files = sbuf->f_files - sbuf->f_ffree;
    444 
    445 	error = VFS_STATVFS(um->um_uppervp->v_mount, sbuf);
    446 	if (error)
    447 		goto done;
    448 
    449 	sbp->f_flag = sbuf->f_flag;
    450 	sbp->f_bsize = sbuf->f_bsize;
    451 	sbp->f_frsize = sbuf->f_frsize;
    452 	sbp->f_iosize = sbuf->f_iosize;
    453 
    454 	/*
    455 	 * The "total" fields count total resources in all layers,
    456 	 * the "free" fields count only those resources which are
    457 	 * free in the upper layer (since only the upper layer
    458 	 * is writable).
    459 	 */
    460 
    461 	if (sbuf->f_bsize != lbsize)
    462 		sbp->f_blocks = sbp->f_blocks * lbsize / sbuf->f_bsize;
    463 	sbp->f_blocks += sbuf->f_blocks;
    464 	sbp->f_bfree = sbuf->f_bfree;
    465 	sbp->f_bavail = sbuf->f_bavail;
    466 	sbp->f_bresvd = sbuf->f_bresvd;
    467 	sbp->f_files += sbuf->f_files;
    468 	sbp->f_ffree = sbuf->f_ffree;
    469 	sbp->f_favail = sbuf->f_favail;
    470 	sbp->f_fresvd = sbuf->f_fresvd;
    471 
    472 	copy_statvfs_info(sbp, mp);
    473 done:
    474 	free(sbuf, M_TEMP);
    475 	return error;
    476 }
    477 
    478 /*ARGSUSED*/
    479 int
    480 union_sync(struct mount *mp, int waitfor,
    481     kauth_cred_t cred)
    482 {
    483 
    484 	/*
    485 	 * XXX - Assumes no data cached at union layer.
    486 	 */
    487 	return (0);
    488 }
    489 
    490 /*ARGSUSED*/
    491 int
    492 union_vget(struct mount *mp, ino_t ino,
    493     struct vnode **vpp)
    494 {
    495 
    496 	return (EOPNOTSUPP);
    497 }
    498 
    499 static int
    500 union_renamelock_enter(struct mount *mp)
    501 {
    502 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    503 
    504 	/* Lock just the upper fs, where the action happens. */
    505 	return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
    506 }
    507 
    508 static void
    509 union_renamelock_exit(struct mount *mp)
    510 {
    511 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
    512 
    513 	VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
    514 }
    515 
    516 extern const struct vnodeopv_desc union_vnodeop_opv_desc;
    517 
    518 const struct vnodeopv_desc * const union_vnodeopv_descs[] = {
    519 	&union_vnodeop_opv_desc,
    520 	NULL,
    521 };
    522 
    523 struct vfsops union_vfsops = {
    524 	.vfs_name = MOUNT_UNION,
    525 	.vfs_min_mount_data = sizeof (struct union_args),
    526 	.vfs_mount = union_mount,
    527 	.vfs_start = union_start,
    528 	.vfs_unmount = union_unmount,
    529 	.vfs_root = union_root,
    530 	.vfs_quotactl = (void *)eopnotsupp,
    531 	.vfs_statvfs = union_statvfs,
    532 	.vfs_sync = union_sync,
    533 	.vfs_vget = union_vget,
    534 	.vfs_loadvnode = union_loadvnode,
    535 	.vfs_fhtovp = (void *)eopnotsupp,
    536 	.vfs_vptofh = (void *)eopnotsupp,
    537 	.vfs_init = union_init,
    538 	.vfs_reinit = union_reinit,
    539 	.vfs_done = union_done,
    540 	.vfs_snapshot = (void *)eopnotsupp,
    541 	.vfs_extattrctl = vfs_stdextattrctl,
    542 	.vfs_suspendctl = genfs_suspendctl,
    543 	.vfs_renamelock_enter = union_renamelock_enter,
    544 	.vfs_renamelock_exit = union_renamelock_exit,
    545 	.vfs_fsync = (void *)eopnotsupp,
    546 	.vfs_opv_descs = union_vnodeopv_descs
    547 };
    548 
    549 static int
    550 union_modcmd(modcmd_t cmd, void *arg)
    551 {
    552 	int error;
    553 
    554 	switch (cmd) {
    555 	case MODULE_CMD_INIT:
    556 		error = vfs_attach(&union_vfsops);
    557 		if (error != 0)
    558 			break;
    559 		sysctl_createv(&union_sysctl_log, 0, NULL, NULL,
    560 			       CTLFLAG_PERMANENT,
    561 			       CTLTYPE_NODE, "union",
    562 			       SYSCTL_DESCR("Union file system"),
    563 			       NULL, 0, NULL, 0,
    564 			       CTL_VFS, 15, CTL_EOL);
    565 		/*
    566 		 * XXX the "15" above could be dynamic, thereby eliminating
    567 		 * one more instance of the "number to vfs" mapping problem,
    568 		 * but "15" is the order as taken from sys/mount.h
    569 		 */
    570 		break;
    571 	case MODULE_CMD_FINI:
    572 		error = vfs_detach(&union_vfsops);
    573 		if (error != 0)
    574 			break;
    575 		sysctl_teardown(&union_sysctl_log);
    576 		break;
    577 	default:
    578 		error = ENOTTY;
    579 		break;
    580 	}
    581 
    582 	return (error);
    583 }
    584