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, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
5 * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
8 * This code is derived from software donated to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/fcntl.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/namei.h>
49 #include <sys/vnode.h>
52 #include <fs/unionfs/union.h>
54 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
56 static vfs_fhtovp_t unionfs_fhtovp;
57 static vfs_checkexp_t unionfs_checkexp;
58 static vfs_mount_t unionfs_domount;
59 static vfs_quotactl_t unionfs_quotactl;
60 static vfs_root_t unionfs_root;
61 static vfs_sync_t unionfs_sync;
62 static vfs_statfs_t unionfs_statfs;
63 static vfs_unmount_t unionfs_unmount;
64 static vfs_vget_t unionfs_vget;
65 static vfs_extattrctl_t unionfs_extattrctl;
67 static struct vfsops unionfs_vfsops;
70 * Mount unionfs layer.
73 unionfs_domount(struct mount *mp)
76 struct vnode *lowerrootvp;
77 struct vnode *upperrootvp;
78 struct unionfs_mount *ump;
90 unionfs_copymode copymode;
91 unionfs_whitemode whitemode;
92 struct nameidata nd, *ndp;
95 UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
103 copymode = UNIONFS_TRANSPARENT; /* default */
104 whitemode = UNIONFS_WHITE_ALWAYS;
108 if (mp->mnt_flag & MNT_ROOTFS) {
109 vfs_mount_error(mp, "Cannot union mount root filesystem");
114 * Update is a no operation.
116 if (mp->mnt_flag & MNT_UPDATE) {
117 vfs_mount_error(mp, "unionfs does not support mount update");
124 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
126 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
128 if (error || target[len - 1] != '\0') {
129 vfs_mount_error(mp, "Invalid target");
132 if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
134 if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
136 udir = (mode_t)strtol(tmp, &ep, 8);
137 if (tmp == NULL || *ep) {
138 vfs_mount_error(mp, "Invalid udir");
141 udir &= S_IRWXU | S_IRWXG | S_IRWXO;
143 if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
145 ufile = (mode_t)strtol(tmp, &ep, 8);
146 if (tmp == NULL || *ep) {
147 vfs_mount_error(mp, "Invalid ufile");
150 ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
152 /* check umask, uid and gid */
153 if (udir == 0 && ufile != 0)
155 if (ufile == 0 && udir != 0)
158 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY);
159 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred);
168 VOP_UNLOCK(mp->mnt_vnodecovered, LK_RELEASE);
172 if (mp->mnt_cred->cr_ruid == 0) { /* root only */
173 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
176 uid = (uid_t)strtol(tmp, &ep, 10);
177 if (tmp == NULL || *ep) {
178 vfs_mount_error(mp, "Invalid uid");
182 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
185 gid = (gid_t)strtol(tmp, &ep, 10);
186 if (tmp == NULL || *ep) {
187 vfs_mount_error(mp, "Invalid gid");
191 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
194 vfs_mount_error(mp, "Invalid copymode");
196 } else if (strcasecmp(tmp, "traditional") == 0)
197 copymode = UNIONFS_TRADITIONAL;
198 else if (strcasecmp(tmp, "transparent") == 0)
199 copymode = UNIONFS_TRANSPARENT;
200 else if (strcasecmp(tmp, "masquerade") == 0)
201 copymode = UNIONFS_MASQUERADE;
203 vfs_mount_error(mp, "Invalid copymode");
207 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
210 vfs_mount_error(mp, "Invalid whiteout mode");
212 } else if (strcasecmp(tmp, "always") == 0)
213 whitemode = UNIONFS_WHITE_ALWAYS;
214 else if (strcasecmp(tmp, "whenneeded") == 0)
215 whitemode = UNIONFS_WHITE_WHENNEEDED;
217 vfs_mount_error(mp, "Invalid whiteout mode");
222 /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
223 if (copymode == UNIONFS_TRADITIONAL) {
224 uid = mp->mnt_cred->cr_ruid;
225 gid = mp->mnt_cred->cr_rgid;
228 UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
229 UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
230 UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
235 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
236 if ((error = namei(ndp)))
239 NDFREE(ndp, NDF_ONLY_PNBUF);
241 /* get root vnodes */
242 lowerrootvp = mp->mnt_vnodecovered;
243 upperrootvp = ndp->ni_vp;
245 /* create unionfs_mount */
246 ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
247 M_UNIONFSMNT, M_WAITOK | M_ZERO);
253 VOP_UNLOCK(upperrootvp, LK_RELEASE);
254 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY);
255 ump->um_lowervp = upperrootvp;
256 ump->um_uppervp = lowerrootvp;
258 ump->um_lowervp = lowerrootvp;
259 ump->um_uppervp = upperrootvp;
261 ump->um_rootvp = NULLVP;
265 ump->um_ufile = ufile;
266 ump->um_copymode = copymode;
267 ump->um_whitemode = whitemode;
272 * Copy upper layer's RDONLY flag.
274 mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
279 VOP_UNLOCK(ump->um_uppervp, LK_RELEASE);
282 * Get the unionfs root vnode.
284 error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
285 NULLVP, &(ump->um_rootvp), NULL, td);
288 free(ump, M_UNIONFSMNT);
294 if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
295 (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
296 mp->mnt_flag |= MNT_LOCAL;
298 if ((ump->um_uppervp->v_mount->mnt_kern_flag & MNTK_SUSPENDABLE) != 0)
299 mp->mnt_kern_flag |= MNTK_SUSPENDABLE;
308 tmp = mp->mnt_stat.f_mntfromname;
309 copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
312 copystr(target, tmp, len, NULL);
314 UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
315 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
321 * Free reference to unionfs layer
324 unionfs_unmount(struct mount *mp, int mntflags)
326 struct unionfs_mount *ump;
332 UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
334 ump = MOUNTTOUNIONFSMOUNT(mp);
337 if (mntflags & MNT_FORCE)
340 /* vflush (no need to call vrele) */
341 for (freeing = 0; (error = vflush(mp, 1, flags, curthread)) != 0;) {
342 num = mp->mnt_nvnodelistsize;
351 free(ump, M_UNIONFSMNT);
358 unionfs_root(struct mount *mp, int flags, struct vnode **vpp)
360 struct unionfs_mount *ump;
363 ump = MOUNTTOUNIONFSMOUNT(mp);
366 UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
367 vp, VOP_ISLOCKED(vp));
370 if (flags & LK_TYPE_MASK)
379 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg)
381 struct unionfs_mount *ump;
383 ump = MOUNTTOUNIONFSMOUNT(mp);
386 * Writing is always performed to upper vnode.
388 return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg));
392 unionfs_statfs(struct mount *mp, struct statfs *sbp)
394 struct unionfs_mount *ump;
399 ump = MOUNTTOUNIONFSMOUNT(mp);
401 UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
402 (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
404 bzero(&mstat, sizeof(mstat));
406 error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat);
410 /* now copy across the "interesting" information and fake the rest */
411 sbp->f_blocks = mstat.f_blocks;
412 sbp->f_files = mstat.f_files;
414 lbsize = mstat.f_bsize;
416 error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat);
421 * The FS type etc is copy from upper vfs.
422 * (write able vfs have priority)
424 sbp->f_type = mstat.f_type;
425 sbp->f_flags = mstat.f_flags;
426 sbp->f_bsize = mstat.f_bsize;
427 sbp->f_iosize = mstat.f_iosize;
429 if (mstat.f_bsize != lbsize)
430 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
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;
441 unionfs_sync(struct mount *mp, int waitfor)
448 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
454 unionfs_fhtovp(struct mount *mp, struct fid *fidp, int flags,
461 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
462 struct ucred **credanonp, int *numsecflavors, int **secflavors)
468 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
469 int namespace, const char *attrname)
471 struct unionfs_mount *ump;
472 struct unionfs_node *unp;
474 ump = MOUNTTOUNIONFSMOUNT(mp);
475 unp = VTOUNIONFS(filename_vp);
477 if (unp->un_uppervp != NULLVP) {
478 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
479 unp->un_uppervp, namespace, attrname));
481 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
482 unp->un_lowervp, namespace, attrname));
486 static struct vfsops unionfs_vfsops = {
487 .vfs_checkexp = unionfs_checkexp,
488 .vfs_extattrctl = unionfs_extattrctl,
489 .vfs_fhtovp = unionfs_fhtovp,
490 .vfs_init = unionfs_init,
491 .vfs_mount = unionfs_domount,
492 .vfs_quotactl = unionfs_quotactl,
493 .vfs_root = unionfs_root,
494 .vfs_statfs = unionfs_statfs,
495 .vfs_sync = unionfs_sync,
496 .vfs_uninit = unionfs_uninit,
497 .vfs_unmount = unionfs_unmount,
498 .vfs_vget = unionfs_vget,
501 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);