2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994, 1995 The Regents of the University of California.
5 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
6 * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
7 * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
10 * This code is derived from software donated to Berkeley by
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/fcntl.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
51 #include <sys/vnode.h>
54 #include <fs/unionfs/union.h>
56 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
58 static vfs_fhtovp_t unionfs_fhtovp;
59 static vfs_checkexp_t unionfs_checkexp;
60 static vfs_mount_t unionfs_domount;
61 static vfs_quotactl_t unionfs_quotactl;
62 static vfs_root_t unionfs_root;
63 static vfs_sync_t unionfs_sync;
64 static vfs_statfs_t unionfs_statfs;
65 static vfs_unmount_t unionfs_unmount;
66 static vfs_vget_t unionfs_vget;
67 static vfs_extattrctl_t unionfs_extattrctl;
69 static struct vfsops unionfs_vfsops;
72 * Mount unionfs layer.
75 unionfs_domount(struct mount *mp)
78 struct vnode *lowerrootvp;
79 struct vnode *upperrootvp;
80 struct unionfs_mount *ump;
91 unionfs_copymode copymode;
92 unionfs_whitemode whitemode;
93 struct nameidata nd, *ndp;
96 UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
104 copymode = UNIONFS_TRANSPARENT; /* default */
105 whitemode = UNIONFS_WHITE_ALWAYS;
109 if (mp->mnt_flag & MNT_ROOTFS) {
110 vfs_mount_error(mp, "Cannot union mount root filesystem");
115 * Update is a no operation.
117 if (mp->mnt_flag & MNT_UPDATE) {
118 vfs_mount_error(mp, "unionfs does not support mount update");
125 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
127 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
129 if (error || target[len - 1] != '\0') {
130 vfs_mount_error(mp, "Invalid target");
133 if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
135 if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
137 udir = (mode_t)strtol(tmp, &ep, 8);
138 if (tmp == NULL || *ep) {
139 vfs_mount_error(mp, "Invalid udir");
142 udir &= S_IRWXU | S_IRWXG | S_IRWXO;
144 if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
146 ufile = (mode_t)strtol(tmp, &ep, 8);
147 if (tmp == NULL || *ep) {
148 vfs_mount_error(mp, "Invalid ufile");
151 ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
153 /* check umask, uid and gid */
154 if (udir == 0 && ufile != 0)
156 if (ufile == 0 && udir != 0)
159 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
160 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
169 VOP_UNLOCK(mp->mnt_vnodecovered);
173 if (mp->mnt_cred->cr_ruid == 0) { /* root only */
174 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
177 uid = (uid_t)strtol(tmp, &ep, 10);
178 if (tmp == NULL || *ep) {
179 vfs_mount_error(mp, "Invalid uid");
183 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
186 gid = (gid_t)strtol(tmp, &ep, 10);
187 if (tmp == NULL || *ep) {
188 vfs_mount_error(mp, "Invalid gid");
192 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
195 vfs_mount_error(mp, "Invalid copymode");
197 } else if (strcasecmp(tmp, "traditional") == 0)
198 copymode = UNIONFS_TRADITIONAL;
199 else if (strcasecmp(tmp, "transparent") == 0)
200 copymode = UNIONFS_TRANSPARENT;
201 else if (strcasecmp(tmp, "masquerade") == 0)
202 copymode = UNIONFS_MASQUERADE;
204 vfs_mount_error(mp, "Invalid copymode");
208 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
211 vfs_mount_error(mp, "Invalid whiteout mode");
213 } else if (strcasecmp(tmp, "always") == 0)
214 whitemode = UNIONFS_WHITE_ALWAYS;
215 else if (strcasecmp(tmp, "whenneeded") == 0)
216 whitemode = UNIONFS_WHITE_WHENNEEDED;
218 vfs_mount_error(mp, "Invalid whiteout mode");
223 /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
224 if (copymode == UNIONFS_TRADITIONAL) {
225 uid = mp->mnt_cred->cr_ruid;
226 gid = mp->mnt_cred->cr_rgid;
229 UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
230 UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
231 UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
236 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
237 if ((error = namei(ndp)))
240 NDFREE(ndp, NDF_ONLY_PNBUF);
242 /* get root vnodes */
243 lowerrootvp = mp->mnt_vnodecovered;
244 upperrootvp = ndp->ni_vp;
246 /* create unionfs_mount */
247 ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
248 M_UNIONFSMNT, M_WAITOK | M_ZERO);
254 VOP_UNLOCK(upperrootvp);
255 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
256 ump->um_lowervp = upperrootvp;
257 ump->um_uppervp = lowerrootvp;
259 ump->um_lowervp = lowerrootvp;
260 ump->um_uppervp = upperrootvp;
262 ump->um_rootvp = NULLVP;
266 ump->um_ufile = ufile;
267 ump->um_copymode = copymode;
268 ump->um_whitemode = whitemode;
273 * Copy upper layer's RDONLY flag.
275 mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
280 VOP_UNLOCK(ump->um_uppervp);
283 * Get the unionfs root vnode.
285 error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
286 NULLVP, &(ump->um_rootvp), NULL, td);
289 free(ump, M_UNIONFSMNT);
295 if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
296 (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
297 mp->mnt_flag |= MNT_LOCAL;
298 mp->mnt_kern_flag |= MNTK_NOMSYNC | MNTK_UNIONFS;
306 snprintf(mp->mnt_stat.f_mntfromname, MNAMELEN, "<%s>:%s",
307 below ? "below" : "above", target);
309 UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
310 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
316 * Free reference to unionfs layer
319 unionfs_unmount(struct mount *mp, int mntflags)
321 struct unionfs_mount *ump;
327 UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
329 ump = MOUNTTOUNIONFSMOUNT(mp);
332 if (mntflags & MNT_FORCE)
335 /* vflush (no need to call vrele) */
336 for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
337 num = mp->mnt_nvnodelistsize;
346 free(ump, M_UNIONFSMNT);
353 unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
355 struct unionfs_mount *ump;
358 ump = MOUNTTOUNIONFSMOUNT(mp);
361 UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
362 vp, VOP_ISLOCKED(vp));
365 if (flags & LK_TYPE_MASK)
374 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
376 struct unionfs_mount *ump;
378 ump = MOUNTTOUNIONFSMOUNT(mp);
381 * Writing is always performed to upper vnode.
383 return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg));
387 unionfs_statfs(struct mount *mp, struct statfs *sbp)
389 struct unionfs_mount *ump;
391 struct statfs *mstat;
394 ump = MOUNTTOUNIONFSMOUNT(mp);
396 UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
397 (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
399 mstat = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK | M_ZERO);
401 error = VFS_STATFS(ump->um_lowervp->v_mount, mstat);
403 free(mstat, M_STATFS);
407 /* now copy across the "interesting" information and fake the rest */
408 sbp->f_blocks = mstat->f_blocks;
409 sbp->f_files = mstat->f_files;
411 lbsize = mstat->f_bsize;
413 error = VFS_STATFS(ump->um_uppervp->v_mount, mstat);
415 free(mstat, M_STATFS);
420 * The FS type etc is copy from upper vfs.
421 * (write able vfs have priority)
423 sbp->f_type = mstat->f_type;
424 sbp->f_flags = mstat->f_flags;
425 sbp->f_bsize = mstat->f_bsize;
426 sbp->f_iosize = mstat->f_iosize;
428 if (mstat->f_bsize != lbsize)
429 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) /
432 sbp->f_blocks += mstat->f_blocks;
433 sbp->f_bfree = mstat->f_bfree;
434 sbp->f_bavail = mstat->f_bavail;
435 sbp->f_files += mstat->f_files;
436 sbp->f_ffree = mstat->f_ffree;
438 free(mstat, M_STATFS);
443 unionfs_sync(struct mount *mp, int waitfor)
450 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
456 unionfs_fhtovp(struct mount *mp, struct fid *fidp, int flags,
463 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, uint64_t *extflagsp,
464 struct ucred **credanonp, int *numsecflavors, int *secflavors)
470 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
471 int namespace, const char *attrname)
473 struct unionfs_mount *ump;
474 struct unionfs_node *unp;
476 ump = MOUNTTOUNIONFSMOUNT(mp);
477 unp = VTOUNIONFS(filename_vp);
479 if (unp->un_uppervp != NULLVP) {
480 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
481 unp->un_uppervp, namespace, attrname));
483 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
484 unp->un_lowervp, namespace, attrname));
488 static struct vfsops unionfs_vfsops = {
489 .vfs_checkexp = unionfs_checkexp,
490 .vfs_extattrctl = unionfs_extattrctl,
491 .vfs_fhtovp = unionfs_fhtovp,
492 .vfs_init = unionfs_init,
493 .vfs_mount = unionfs_domount,
494 .vfs_quotactl = unionfs_quotactl,
495 .vfs_root = unionfs_root,
496 .vfs_statfs = unionfs_statfs,
497 .vfs_sync = unionfs_sync,
498 .vfs_uninit = unionfs_uninit,
499 .vfs_unmount = unionfs_unmount,
500 .vfs_vget = unionfs_vget,
503 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK|VFCF_JAIL);