Home | History | Annotate | Line # | Download | only in unionfs
unionfs_vfsops.c revision 1.5.12.1
      1 /*-
      2  * Copyright (c) 1994, 1995 The Regents of the University of California.
      3  * Copyright (c) 1994, 1995 Jan-Simon Pendry.
      4  * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa (at) ongs.co.jp>, ONGS Inc.
      5  * Copyright (c) 2006 Daichi Goto <daichi (at) freebsd.org>
      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  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)union_vfsops.c	8.20 (Berkeley) 5/20/95
     36  * $FreeBSD: src/sys/fs/unionfs/union_vfsops.c,v 1.89 2008/01/13 14:44:06 attilio Exp $
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/kernel.h>
     42 #include <sys/lock.h>
     43 #include <sys/malloc.h>
     44 #include <sys/mount.h>
     45 #include <sys/namei.h>
     46 #include <sys/proc.h>
     47 #include <sys/vnode.h>
     48 #include <sys/stat.h>
     49 #include <sys/sysctl.h>
     50 #include <sys/module.h>
     51 
     52 #include <fs/unionfs/unionfs.h>
     53 
     54 MODULE(MODULE_CLASS_VFS, unionfs, NULL);
     55 
     56 MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
     57 
     58 struct vfsops unionfs_vfsops;
     59 
     60 VFS_PROTOS(unionfs);
     61 
     62 static struct sysctllog *unionfs_sysctl_log;
     63 
     64 /*
     65  * Mount unionfs layer.
     66  */
     67 int
     68 unionfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     69 {
     70 	int		error;
     71 	struct vnode   *lowerrootvp;
     72 	struct vnode   *upperrootvp;
     73 	struct unionfs_mount *ump;
     74 	int		below;
     75 	uid_t		uid;
     76 	gid_t		gid;
     77 	u_short		udir;
     78 	u_short		ufile;
     79 	unionfs_copymode copymode;
     80 	unionfs_whitemode whitemode;
     81 	struct componentname fakecn;
     82 	struct nameidata nd, *ndp;
     83 	struct vattr	va;
     84 	struct union_args *args = data;
     85 	kauth_cred_t	cred;
     86 	size_t		size;
     87 	size_t		len;
     88 	const char	*cp;
     89 	char		*xp;
     90 
     91 	if (args == NULL)
     92 		return EINVAL;
     93 	if (*data_len < sizeof *args)
     94 		return EINVAL;
     95 
     96 	UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
     97 
     98 	error = 0;
     99 	below = 0;
    100 	uid = 0;
    101 	gid = 0;
    102 	udir = 0;
    103 	ufile = 0;
    104 	copymode = UNIONFS_TRANSPARENT;	/* default */
    105 	whitemode = UNIONFS_WHITE_ALWAYS;
    106 	ndp = &nd;
    107 	cred = kauth_cred_get();
    108 
    109 	if (mp->mnt_flag & MNT_ROOTFS) {
    110 		printf("union_mount: cannot union mount root filesystem\n");
    111 		return (EOPNOTSUPP);
    112 	}
    113 
    114 	if (mp->mnt_flag & MNT_GETARGS) {
    115 		ump = MOUNTTOUNIONFSMOUNT(mp);
    116 		if (ump == NULL)
    117 			return EIO;
    118 		args->target = NULL;
    119 		args->mntflags = ump->um_op;
    120 		*data_len = sizeof *args;
    121 		return 0;
    122 	}
    123 
    124 	/*
    125 	 * Update is a no operation.
    126 	 */
    127 	if (mp->mnt_flag & MNT_UPDATE) {
    128 		printf("union_mount: cannot update union mount\n");
    129 		return (EOPNOTSUPP);
    130 	}
    131 
    132 	vn_lock(mp->mnt_vnodecovered, LK_EXCLUSIVE | LK_RETRY);
    133 	error = VOP_GETATTR(mp->mnt_vnodecovered, &va, cred);
    134 	if (!error) {
    135 		if (udir == 0)
    136 			udir = va.va_mode;
    137 		if (ufile == 0)
    138 			ufile = va.va_mode;
    139 		uid = va.va_uid;
    140 		gid = va.va_gid;
    141 	}
    142 	VOP_UNLOCK(mp->mnt_vnodecovered, 0);
    143 	if (error)
    144 		return (error);
    145 
    146 	switch (args->mntflags & UNMNT_OPMASK) {
    147 	case UNMNT_ABOVE:
    148 		below = 0;
    149 		break;
    150 
    151 	case UNMNT_BELOW:
    152 		below = 1;
    153 		break;
    154 
    155 	case UNMNT_REPLACE:
    156 	default:
    157 		return EINVAL;
    158 	}
    159 
    160 	/* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
    161 	if (copymode == UNIONFS_TRADITIONAL) {
    162 		uid = kauth_cred_getuid(cred);
    163 		gid = kauth_cred_getgid(cred);
    164 	}
    165 
    166 	UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
    167 	UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
    168 	UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
    169 
    170 	/*
    171 	 * Find upper node
    172 	 */
    173 	NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, args->target);
    174 	if ((error = namei(ndp)))
    175 		return (error);
    176 
    177 	/* get root vnodes */
    178 	lowerrootvp = mp->mnt_vnodecovered;
    179 	upperrootvp = ndp->ni_vp;
    180 
    181 	vrele(ndp->ni_dvp);
    182 	ndp->ni_dvp = NULLVP;
    183 
    184 	/* create unionfs_mount */
    185 	ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
    186 	    M_UNIONFSMNT, M_WAITOK | M_ZERO);
    187 
    188 	/*
    189 	 * Save reference
    190 	 */
    191 	if (below) {
    192 		VOP_UNLOCK(upperrootvp, 0);
    193 		vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
    194 		ump->um_lowervp = upperrootvp;
    195 		ump->um_uppervp = lowerrootvp;
    196 	} else {
    197 		ump->um_lowervp = lowerrootvp;
    198 		ump->um_uppervp = upperrootvp;
    199 	}
    200 	ump->um_rootvp = NULLVP;
    201 	ump->um_uid = uid;
    202 	ump->um_gid = gid;
    203 	ump->um_udir = udir;
    204 	ump->um_ufile = ufile;
    205 	ump->um_copymode = copymode;
    206 	ump->um_whitemode = whitemode;
    207 
    208 	if ((lowerrootvp->v_mount->mnt_iflag & IMNT_MPSAFE) &&
    209 	    (upperrootvp->v_mount->mnt_flag & IMNT_MPSAFE))
    210 		mp->mnt_iflag |= IMNT_MPSAFE;
    211 	mp->mnt_data = ump;
    212 
    213 	/*
    214 	 * Copy upper layer's RDONLY flag.
    215 	 */
    216 	mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
    217 
    218 	/*
    219 	 * Check whiteout
    220 	 */
    221 	if ((mp->mnt_flag & MNT_RDONLY) == 0) {
    222 		memset(&fakecn, 0, sizeof(fakecn));
    223 		fakecn.cn_nameiop = LOOKUP;
    224 		error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP);
    225 		if (error) {
    226 			if (below) {
    227 				VOP_UNLOCK(ump->um_uppervp, 0);
    228 				vrele(upperrootvp);
    229 			} else
    230 				vput(ump->um_uppervp);
    231 			free(ump, M_UNIONFSMNT);
    232 			mp->mnt_data = NULL;
    233 			return (error);
    234 		}
    235 	}
    236 
    237 	/*
    238 	 * Unlock the node
    239 	 */
    240 	VOP_UNLOCK(ump->um_uppervp, 0);
    241 
    242 	ump->um_op = args->mntflags & UNMNT_OPMASK;
    243 
    244 	/*
    245 	 * Get the unionfs root vnode.
    246 	 */
    247 	error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
    248 	    NULLVP, &(ump->um_rootvp), NULL);
    249 	vrele(upperrootvp);
    250 	if (error) {
    251 		free(ump, M_UNIONFSMNT);
    252 		mp->mnt_data = NULL;
    253 		return (error);
    254 	}
    255 
    256 	/*
    257 	 * Check mnt_flag
    258 	 */
    259 	if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
    260 	    (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
    261 		mp->mnt_flag |= MNT_LOCAL;
    262 
    263 	/*
    264 	 * Get new fsid
    265 	 */
    266 	vfs_getnewfsid(mp);
    267 
    268 	error = set_statvfs_info(path, UIO_USERSPACE, NULL, UIO_USERSPACE,
    269 	    mp->mnt_op->vfs_name, mp, curlwp);
    270 	if (error) {
    271 		unionfs_noderem(ump->um_rootvp);
    272 		free(ump, M_UNIONFSMNT);
    273 		mp->mnt_data = NULL;
    274 		return (error);
    275 	}
    276 
    277 	switch (ump->um_op) {
    278 	case UNMNT_ABOVE:
    279 		cp = "<above>:";
    280 		break;
    281 	case UNMNT_BELOW:
    282 		cp = "<below>:";
    283 		break;
    284 	default:
    285 		panic("union_mount: bad um_op");
    286 		break;
    287 	}
    288 	len = strlen(cp);
    289 	memcpy(mp->mnt_stat.f_mntfromname, cp, len);
    290 	xp = mp->mnt_stat.f_mntfromname + len;
    291 	len = MNAMELEN - len;
    292 	(void) copyinstr(args->target, xp, len - 1, &size);
    293 	memset(xp + size, 0, len - size);
    294 
    295 	UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
    296 	    mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    297 
    298 	return (0);
    299 }
    300 
    301 /*
    302  * Free reference to unionfs layer
    303  */
    304 int
    305 unionfs_unmount(struct mount *mp, int mntflags)
    306 {
    307 	struct unionfs_mount *ump;
    308 	int		error;
    309 	int		freeing;
    310 	int		flags;
    311 
    312 	UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
    313 
    314 	ump = MOUNTTOUNIONFSMOUNT(mp);
    315 	flags = 0;
    316 
    317 	if (mntflags & MNT_FORCE)
    318 		flags |= FORCECLOSE;
    319 
    320 	/* vflush (no need to call vrele) */
    321 	for (freeing = 0; (error = vflush(mp, NULL, flags)) != 0;) {
    322 		struct vnode *vp;
    323 		int n;
    324 
    325 		/* count #vnodes held on mount list */
    326 		n = 0;
    327 		TAILQ_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes)
    328 			n++;
    329 
    330 		/* if this is unchanged then stop */
    331 		if (n == freeing)
    332 			break;
    333 
    334 		/* otherwise try once more time */
    335 		freeing = n;
    336 	}
    337 
    338 	if (error)
    339 		return (error);
    340 
    341 	free(ump, M_UNIONFSMNT);
    342 	mp->mnt_data = NULL;
    343 
    344 	return (0);
    345 }
    346 
    347 int
    348 unionfs_root(struct mount *mp, struct vnode **vpp)
    349 {
    350 	struct unionfs_mount *ump;
    351 	struct vnode   *vp;
    352 
    353 	ump = MOUNTTOUNIONFSMOUNT(mp);
    354 	vp = ump->um_rootvp;
    355 
    356 	UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
    357 	    vp, VOP_ISLOCKED(vp));
    358 
    359 	vref(vp);
    360 	vn_lock(vp, LK_EXCLUSIVE);
    361 
    362 	*vpp = vp;
    363 
    364 	return (0);
    365 }
    366 
    367 int
    368 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
    369 {
    370 	struct unionfs_mount *ump;
    371 
    372 	ump = MOUNTTOUNIONFSMOUNT(mp);
    373 
    374 	/*
    375 	 * Writing is always performed to upper vnode.
    376 	 */
    377 	return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg));
    378 }
    379 
    380 int
    381 unionfs_statvfs(struct mount *mp, struct statvfs *sbp)
    382 {
    383 	struct unionfs_mount *ump;
    384 	int		error;
    385 	uint64_t	lbsize;
    386 	struct statvfs *sbuf = malloc(sizeof(*sbuf), M_TEMP, M_WAITOK | M_ZERO);
    387 
    388 	ump = MOUNTTOUNIONFSMOUNT(mp);
    389 
    390 	UNIONFSDEBUG("unionfs_statvfs(mp = %p, lvp = %p, uvp = %p)\n",
    391 	    (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
    392 
    393 	error = VFS_STATVFS(ump->um_lowervp->v_mount, sbuf);
    394 	if (error)
    395 		goto done;
    396 
    397 	/* now copy across the "interesting" information and fake the rest */
    398 	sbp->f_blocks = sbuf->f_blocks;
    399 	sbp->f_files = sbuf->f_files;
    400 
    401 	lbsize = sbuf->f_bsize;
    402 
    403 	error = VFS_STATVFS(ump->um_uppervp->v_mount, sbuf);
    404 	if (error)
    405 		goto done;
    406 
    407 	/*
    408 	 * The FS type etc is copy from upper vfs.
    409 	 * (write able vfs have priority)
    410 	 */
    411 	sbp->f_flag = sbuf->f_flag;
    412 	sbp->f_bsize = sbuf->f_bsize;
    413 	sbp->f_iosize = sbuf->f_iosize;
    414 
    415 	if (sbuf->f_bsize != lbsize)
    416 		sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / sbuf->f_bsize;
    417 
    418 	sbp->f_blocks += sbuf->f_blocks;
    419 	sbp->f_bfree = sbuf->f_bfree;
    420 	sbp->f_bavail = sbuf->f_bavail;
    421 	sbp->f_files += sbuf->f_files;
    422 	sbp->f_ffree = sbuf->f_ffree;
    423 
    424  done:
    425 	free(sbuf, M_TEMP);
    426 	return (error);
    427 }
    428 
    429 int
    430 unionfs_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
    431 {
    432 	/* nothing to do */
    433 	return (0);
    434 }
    435 
    436 int
    437 unionfs_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
    438 {
    439 	return (EOPNOTSUPP);
    440 }
    441 
    442 int
    443 unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
    444 {
    445 	return (EOPNOTSUPP);
    446 }
    447 
    448 int
    449 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
    450     int namespace, const char *attrname)
    451 {
    452 	struct unionfs_mount *ump;
    453 	struct unionfs_node *unp;
    454 
    455 	ump = MOUNTTOUNIONFSMOUNT(mp);
    456 	unp = VTOUNIONFS(filename_vp);
    457 
    458 	if (unp->un_uppervp != NULLVP) {
    459 		return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
    460 		    unp->un_uppervp, namespace, attrname));
    461 	} else {
    462 		return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
    463 		    unp->un_lowervp, namespace, attrname));
    464 	}
    465 }
    466 
    467 /*
    468  * Initialize
    469  */
    470 void
    471 unionfs_init(void)
    472 {
    473 	UNIONFSDEBUG("unionfs_init\n");	/* printed during system boot */
    474 }
    475 
    476 static int
    477 unionfs_renamelock_enter(struct mount *mp)
    478 {
    479 	struct unionfs_mount *um = MOUNTTOUNIONFSMOUNT(mp);
    480 
    481 	/* Lock just the upper fs, where the action happens. */
    482 	return VFS_RENAMELOCK_ENTER(um->um_uppervp->v_mount);
    483 }
    484 
    485 static void
    486 unionfs_renamelock_exit(struct mount *mp)
    487 {
    488 	struct unionfs_mount *um = MOUNTTOUNIONFSMOUNT(mp);
    489 
    490 	VFS_RENAMELOCK_EXIT(um->um_uppervp->v_mount);
    491 }
    492 
    493 int
    494 unionfs_start(struct mount *mp, int flags)
    495 {
    496 
    497 	return (0);
    498 }
    499 
    500 void
    501 unionfs_done(void)
    502 {
    503 
    504 	/* Make sure to unset the readdir hook. */
    505 	vn_union_readdir_hook = NULL;
    506 }
    507 
    508 extern const struct vnodeopv_desc unionfs_vnodeop_opv_desc;
    509 
    510 const struct vnodeopv_desc * const unionfs_vnodeopv_descs[] = {
    511 	&unionfs_vnodeop_opv_desc,
    512 	NULL,
    513 };
    514 
    515 struct vfsops unionfs_vfsops = {
    516 	MOUNT_UNION,
    517 	sizeof (struct unionfs_args),
    518 	unionfs_mount,
    519 	unionfs_start,
    520 	unionfs_unmount,
    521 	unionfs_root,
    522 	(void *)eopnotsupp,		/* vfs_quotactl */
    523 	unionfs_statvfs,
    524 	unionfs_sync,
    525 	unionfs_vget,
    526 	(void *)eopnotsupp,		/* vfs_fhtovp */
    527 	(void *)eopnotsupp,		/* vfs_vptofh */
    528 	unionfs_init,
    529 	NULL,				/* vfs_reinit */
    530 	unionfs_done,
    531 	NULL,				/* vfs_mountroot */
    532 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    533 	vfs_stdextattrctl,
    534 	(void *)eopnotsupp,		/* vfs_suspendctl */
    535 	unionfs_renamelock_enter,
    536 	unionfs_renamelock_exit,
    537 	(void *)eopnotsupp,
    538 	unionfs_vnodeopv_descs,
    539 	0,				/* vfs_refcount */
    540 	{ NULL, NULL },
    541 };
    542 
    543 static int
    544 unionfs_modcmd(modcmd_t cmd, void *arg)
    545 {
    546 	int error;
    547 
    548 	switch (cmd) {
    549 	case MODULE_CMD_INIT:
    550 		error = vfs_attach(&unionfs_vfsops);
    551 		if (error != 0)
    552 			break;
    553 		sysctl_createv(&unionfs_sysctl_log, 0, NULL, NULL,
    554 			       CTLFLAG_PERMANENT,
    555 			       CTLTYPE_NODE, "vfs", NULL,
    556 			       NULL, 0, NULL, 0,
    557 			       CTL_VFS, CTL_EOL);
    558 		sysctl_createv(&unionfs_sysctl_log, 0, NULL, NULL,
    559 			       CTLFLAG_PERMANENT,
    560 			       CTLTYPE_NODE, "union",
    561 			       SYSCTL_DESCR("Union file system"),
    562 			       NULL, 0, NULL, 0,
    563 			       CTL_VFS, 15, CTL_EOL);
    564 		/*
    565 		 * XXX the "15" above could be dynamic, thereby eliminating
    566 		 * one more instance of the "number to vfs" mapping problem,
    567 		 * but "15" is the order as taken from sys/mount.h
    568 		 */
    569 		break;
    570 	case MODULE_CMD_FINI:
    571 		error = vfs_detach(&unionfs_vfsops);
    572 		if (error != 0)
    573 			break;
    574 		sysctl_teardown(&unionfs_sysctl_log);
    575 		break;
    576 	default:
    577 		error = ENOTTY;
    578 		break;
    579 	}
    580 
    581 	return (error);
    582 }
    583