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@ongs.co.jp>, ONGS Inc.
5 * Copyright (c) 2006 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/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
48 #include <sys/vnode.h>
51 #include <fs/unionfs/union.h>
53 static MALLOC_DEFINE(M_UNIONFSMNT, "UNIONFS mount", "UNIONFS mount structure");
55 static vfs_fhtovp_t unionfs_fhtovp;
56 static vfs_checkexp_t unionfs_checkexp;
57 static vfs_mount_t unionfs_domount;
58 static vfs_quotactl_t unionfs_quotactl;
59 static vfs_root_t unionfs_root;
60 static vfs_sync_t unionfs_sync;
61 static vfs_statfs_t unionfs_statfs;
62 static vfs_unmount_t unionfs_unmount;
63 static vfs_vget_t unionfs_vget;
64 static vfs_extattrctl_t unionfs_extattrctl;
66 static struct vfsops unionfs_vfsops;
69 * Mount unionfs layer.
72 unionfs_domount(struct mount *mp, struct thread *td)
75 struct vnode *lowerrootvp;
76 struct vnode *upperrootvp;
77 struct unionfs_mount *ump;
88 unionfs_copymode copymode;
89 unionfs_whitemode whitemode;
90 struct componentname fakecn;
91 struct nameidata nd, *ndp;
94 UNIONFSDEBUG("unionfs_mount(mp = %p)\n", (void *)mp);
102 copymode = UNIONFS_TRANSPARENT; /* default */
103 whitemode = UNIONFS_WHITE_ALWAYS;
106 if (mp->mnt_flag & MNT_ROOTFS) {
107 vfs_mount_error(mp, "Cannot union mount root filesystem");
112 * Update is a no operation.
114 if (mp->mnt_flag & MNT_UPDATE) {
115 vfs_mount_error(mp, "unionfs does not support mount update");
122 error = vfs_getopt(mp->mnt_optnew, "target", (void **)&target, &len);
124 error = vfs_getopt(mp->mnt_optnew, "from", (void **)&target,
126 if (error || target[len - 1] != '\0') {
127 vfs_mount_error(mp, "Invalid target");
130 if (vfs_getopt(mp->mnt_optnew, "below", NULL, NULL) == 0)
132 if (vfs_getopt(mp->mnt_optnew, "udir", (void **)&tmp, NULL) == 0) {
134 udir = (mode_t)strtol(tmp, &ep, 8);
135 if (tmp == NULL || *ep) {
136 vfs_mount_error(mp, "Invalid udir");
139 udir &= S_IRWXU | S_IRWXG | S_IRWXO;
141 if (vfs_getopt(mp->mnt_optnew, "ufile", (void **)&tmp, NULL) == 0) {
143 ufile = (mode_t)strtol(tmp, &ep, 8);
144 if (tmp == NULL || *ep) {
145 vfs_mount_error(mp, "Invalid ufile");
148 ufile &= S_IRWXU | S_IRWXG | S_IRWXO;
150 /* check umask, uid and gid */
151 if (udir == 0 && ufile != 0)
153 if (ufile == 0 && udir != 0)
156 vn_lock(mp->mnt_vnodecovered, LK_SHARED | LK_RETRY, td);
157 error = VOP_GETATTR(mp->mnt_vnodecovered, &va, mp->mnt_cred, td);
166 VOP_UNLOCK(mp->mnt_vnodecovered, 0, td);
170 if (mp->mnt_cred->cr_ruid == 0) { /* root only */
171 if (vfs_getopt(mp->mnt_optnew, "uid", (void **)&tmp,
174 uid = (uid_t)strtol(tmp, &ep, 10);
175 if (tmp == NULL || *ep) {
176 vfs_mount_error(mp, "Invalid uid");
180 if (vfs_getopt(mp->mnt_optnew, "gid", (void **)&tmp,
183 gid = (gid_t)strtol(tmp, &ep, 10);
184 if (tmp == NULL || *ep) {
185 vfs_mount_error(mp, "Invalid gid");
189 if (vfs_getopt(mp->mnt_optnew, "copymode", (void **)&tmp,
192 vfs_mount_error(mp, "Invalid copymode");
194 } else if (strcasecmp(tmp, "traditional") == 0)
195 copymode = UNIONFS_TRADITIONAL;
196 else if (strcasecmp(tmp, "transparent") == 0)
197 copymode = UNIONFS_TRANSPARENT;
198 else if (strcasecmp(tmp, "masquerade") == 0)
199 copymode = UNIONFS_MASQUERADE;
201 vfs_mount_error(mp, "Invalid copymode");
205 if (vfs_getopt(mp->mnt_optnew, "whiteout", (void **)&tmp,
208 vfs_mount_error(mp, "Invalid whiteout mode");
210 } else if (strcasecmp(tmp, "always") == 0)
211 whitemode = UNIONFS_WHITE_ALWAYS;
212 else if (strcasecmp(tmp, "whenneeded") == 0)
213 whitemode = UNIONFS_WHITE_WHENNEEDED;
215 vfs_mount_error(mp, "Invalid whiteout mode");
220 /* If copymode is UNIONFS_TRADITIONAL, uid/gid is mounted user. */
221 if (copymode == UNIONFS_TRADITIONAL) {
222 uid = mp->mnt_cred->cr_ruid;
223 gid = mp->mnt_cred->cr_rgid;
226 UNIONFSDEBUG("unionfs_mount: uid=%d, gid=%d\n", uid, gid);
227 UNIONFSDEBUG("unionfs_mount: udir=0%03o, ufile=0%03o\n", udir, ufile);
228 UNIONFSDEBUG("unionfs_mount: copymode=%d\n", copymode);
233 NDINIT(ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, target, td);
234 if ((error = namei(ndp)))
237 NDFREE(ndp, NDF_ONLY_PNBUF);
239 /* get root vnodes */
240 lowerrootvp = mp->mnt_vnodecovered;
241 upperrootvp = ndp->ni_vp;
243 /* create unionfs_mount */
244 ump = (struct unionfs_mount *)malloc(sizeof(struct unionfs_mount),
245 M_UNIONFSMNT, M_WAITOK | M_ZERO);
251 VOP_UNLOCK(upperrootvp, 0, td);
252 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td);
253 ump->um_lowervp = upperrootvp;
254 ump->um_uppervp = lowerrootvp;
256 ump->um_lowervp = lowerrootvp;
257 ump->um_uppervp = upperrootvp;
259 ump->um_rootvp = NULLVP;
263 ump->um_ufile = ufile;
264 ump->um_copymode = copymode;
265 ump->um_whitemode = whitemode;
268 if ((lowerrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE) &&
269 (upperrootvp->v_mount->mnt_kern_flag & MNTK_MPSAFE))
270 mp->mnt_kern_flag |= MNTK_MPSAFE;
275 * Copy upper layer's RDONLY flag.
277 mp->mnt_flag |= ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY;
282 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
283 memset(&fakecn, 0, sizeof(fakecn));
284 fakecn.cn_nameiop = LOOKUP;
285 fakecn.cn_thread = td;
286 error = VOP_WHITEOUT(ump->um_uppervp, &fakecn, LOOKUP);
289 VOP_UNLOCK(ump->um_uppervp, 0, td);
292 vput(ump->um_uppervp);
293 free(ump, M_UNIONFSMNT);
302 VOP_UNLOCK(ump->um_uppervp, 0, td);
305 * Get the unionfs root vnode.
307 error = unionfs_nodeget(mp, ump->um_uppervp, ump->um_lowervp,
308 NULLVP, &(ump->um_rootvp), NULL, td);
311 free(ump, M_UNIONFSMNT);
319 if ((ump->um_lowervp->v_mount->mnt_flag & MNT_LOCAL) &&
320 (ump->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
321 mp->mnt_flag |= MNT_LOCAL;
329 tmp = mp->mnt_stat.f_mntfromname;
330 copystr((below ? "<below>:" : "<above>:"), tmp, len, &done);
333 copystr(target, tmp, len, NULL);
335 UNIONFSDEBUG("unionfs_mount: from %s, on %s\n",
336 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
342 * Free reference to unionfs layer
345 unionfs_unmount(struct mount *mp, int mntflags, struct thread *td)
347 struct unionfs_mount *ump;
353 UNIONFSDEBUG("unionfs_unmount: mp = %p\n", (void *)mp);
355 ump = MOUNTTOUNIONFSMOUNT(mp);
358 if (mntflags & MNT_FORCE)
361 /* vflush (no need to call vrele) */
362 for (freeing = 0; (error = vflush(mp, 1, flags, td)) != 0;) {
363 num = mp->mnt_nvnodelistsize;
372 free(ump, M_UNIONFSMNT);
379 unionfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
381 struct unionfs_mount *ump;
384 ump = MOUNTTOUNIONFSMOUNT(mp);
387 UNIONFSDEBUG("unionfs_root: rootvp=%p locked=%x\n",
388 vp, VOP_ISLOCKED(vp, td));
391 if (flags & LK_TYPE_MASK)
392 vn_lock(vp, flags, td);
400 unionfs_quotactl(struct mount *mp, int cmd, uid_t uid, void *arg,
403 struct unionfs_mount *ump;
405 ump = MOUNTTOUNIONFSMOUNT(mp);
408 * Writing is always performed to upper vnode.
410 return (VFS_QUOTACTL(ump->um_uppervp->v_mount, cmd, uid, arg, td));
414 unionfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
416 struct unionfs_mount *ump;
421 ump = MOUNTTOUNIONFSMOUNT(mp);
423 UNIONFSDEBUG("unionfs_statfs(mp = %p, lvp = %p, uvp = %p)\n",
424 (void *)mp, (void *)ump->um_lowervp, (void *)ump->um_uppervp);
426 bzero(&mstat, sizeof(mstat));
428 error = VFS_STATFS(ump->um_lowervp->v_mount, &mstat, td);
432 /* now copy across the "interesting" information and fake the rest */
433 sbp->f_blocks = mstat.f_blocks;
434 sbp->f_files = mstat.f_files;
436 lbsize = mstat.f_bsize;
438 error = VFS_STATFS(ump->um_uppervp->v_mount, &mstat, td);
443 * The FS type etc is copy from upper vfs.
444 * (write able vfs have priority)
446 sbp->f_type = mstat.f_type;
447 sbp->f_flags = mstat.f_flags;
448 sbp->f_bsize = mstat.f_bsize;
449 sbp->f_iosize = mstat.f_iosize;
451 if (mstat.f_bsize != lbsize)
452 sbp->f_blocks = ((off_t)sbp->f_blocks * lbsize) / mstat.f_bsize;
454 sbp->f_blocks += mstat.f_blocks;
455 sbp->f_bfree = mstat.f_bfree;
456 sbp->f_bavail = mstat.f_bavail;
457 sbp->f_files += mstat.f_files;
458 sbp->f_ffree = mstat.f_ffree;
463 unionfs_sync(struct mount *mp, int waitfor, struct thread *td)
470 unionfs_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
476 unionfs_fhtovp(struct mount *mp, struct fid *fidp, struct vnode **vpp)
482 unionfs_checkexp(struct mount *mp, struct sockaddr *nam, int *extflagsp,
483 struct ucred **credanonp)
489 unionfs_extattrctl(struct mount *mp, int cmd, struct vnode *filename_vp,
490 int namespace, const char *attrname, struct thread *td)
492 struct unionfs_mount *ump;
493 struct unionfs_node *unp;
495 ump = MOUNTTOUNIONFSMOUNT(mp);
496 unp = VTOUNIONFS(filename_vp);
498 if (unp->un_uppervp != NULLVP) {
499 return (VFS_EXTATTRCTL(ump->um_uppervp->v_mount, cmd,
500 unp->un_uppervp, namespace, attrname, td));
502 return (VFS_EXTATTRCTL(ump->um_lowervp->v_mount, cmd,
503 unp->un_lowervp, namespace, attrname, td));
507 static struct vfsops unionfs_vfsops = {
508 .vfs_checkexp = unionfs_checkexp,
509 .vfs_extattrctl = unionfs_extattrctl,
510 .vfs_fhtovp = unionfs_fhtovp,
511 .vfs_init = unionfs_init,
512 .vfs_mount = unionfs_domount,
513 .vfs_quotactl = unionfs_quotactl,
514 .vfs_root = unionfs_root,
515 .vfs_statfs = unionfs_statfs,
516 .vfs_sync = unionfs_sync,
517 .vfs_uninit = unionfs_uninit,
518 .vfs_unmount = unionfs_unmount,
519 .vfs_vget = unionfs_vget,
522 VFS_SET(unionfs_vfsops, unionfs, VFCF_LOOPBACK);