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