2 * Copyright (c) 1994, 1995 The Regents of the University of California.
3 * Copyright (c) 1994, 1995 Jan-Simon Pendry.
6 * This code is derived from software donated to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. 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
38 * $Id: union_vfsops.c,v 1.26 1998/02/10 08:04:31 kato Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
53 #include <sys/filedesc.h>
54 #include <miscfs/union/union.h>
56 static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
58 extern int union_init __P((struct vfsconf *));
60 extern int union_fhtovp __P((struct mount *mp, struct fid *fidp,
61 struct mbuf *nam, struct vnode **vpp,
62 int *exflagsp, struct ucred **credanonp));
63 static int union_mount __P((struct mount *mp, char *path, caddr_t data,
64 struct nameidata *ndp, struct proc *p));
65 extern int union_quotactl __P((struct mount *mp, int cmd, uid_t uid,
66 caddr_t arg, struct proc *p));
67 static int union_root __P((struct mount *mp, struct vnode **vpp));
68 static int union_start __P((struct mount *mp, int flags, struct proc *p));
69 static int union_statfs __P((struct mount *mp, struct statfs *sbp,
71 extern int union_sync __P((struct mount *mp, int waitfor,
72 struct ucred *cred, struct proc *p));
73 static int union_unmount __P((struct mount *mp, int mntflags,
75 extern int union_vget __P((struct mount *mp, ino_t ino,
77 static int union_vrele __P((struct mount *mp, struct vnode *vp));
78 extern int union_vptofh __P((struct vnode *vp, struct fid *fhp));
81 * Mount union filesystem
84 union_mount(mp, path, data, ndp, p)
88 struct nameidata *ndp;
92 struct union_args args;
93 struct vnode *lowerrootvp = NULLVP;
94 struct vnode *upperrootvp = NULLVP;
95 struct union_mount *um = 0;
96 struct ucred *cred = 0;
101 #ifdef UNION_DIAGNOSTIC
102 printf("union_mount(mp = %x)\n", mp);
106 * Disable clustered write, otherwise system becomes unstable.
108 mp->mnt_flag |= MNT_NOCLUSTERW;
113 if (mp->mnt_flag & MNT_UPDATE) {
116 * 1. a way to convert between rdonly and rdwr mounts.
117 * 2. support for nfs exports.
126 error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
130 lowerrootvp = mp->mnt_vnodecovered;
134 * Unlock lower node to avoid deadlock.
136 if (lowerrootvp->v_op == union_vnodeop_p)
137 VOP_UNLOCK(lowerrootvp, 0, p);
142 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT,
143 UIO_USERSPACE, args.target, p);
146 if (lowerrootvp->v_op == union_vnodeop_p)
147 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, p);
151 upperrootvp = ndp->ni_vp;
156 * Check multi union mount to avoid `lock myself again' panic.
158 if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
160 printf("union_mount: multi union mount?\n");
166 if (upperrootvp->v_type != VDIR) {
171 um = (struct union_mount *) malloc(sizeof(struct union_mount),
172 M_UNIONFSMNT, M_WAITOK); /* XXX */
175 * Keep a held reference to the target vnodes.
176 * They are vrele'd in union_unmount.
178 * Depending on the _BELOW flag, the filesystems are
179 * viewed in a different order. In effect, this is the
180 * same as providing a mount under option to the mount syscall.
183 um->um_op = args.mntflags & UNMNT_OPMASK;
186 um->um_lowervp = lowerrootvp;
187 um->um_uppervp = upperrootvp;
191 um->um_lowervp = upperrootvp;
192 um->um_uppervp = lowerrootvp;
197 lowerrootvp = NULLVP;
198 um->um_uppervp = upperrootvp;
199 um->um_lowervp = lowerrootvp;
208 * Unless the mount is readonly, ensure that the top layer
209 * supports whiteout operations
211 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
212 error = VOP_WHITEOUT(um->um_uppervp, (struct componentname *) 0, LOOKUP);
217 um->um_cred = p->p_ucred;
219 um->um_cmode = UN_DIRMODE &~ p->p_fd->fd_cmask;
222 * Depending on what you think the MNT_LOCAL flag might mean,
223 * you may want the && to be || on the conditional below.
224 * At the moment it has been defined that the filesystem is
225 * only local if it is all local, ie the MNT_LOCAL flag implies
226 * that the entire namespace is local. If you think the MNT_LOCAL
227 * flag implies that some of the files might be stored locally
228 * then you will want to change the conditional.
230 if (um->um_op == UNMNT_ABOVE) {
231 if (((um->um_lowervp == NULLVP) ||
232 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
233 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
234 mp->mnt_flag |= MNT_LOCAL;
238 * Copy in the upper layer's RDONLY flag. This is for the benefit
239 * of lookup() which explicitly checks the flag, rather than asking
240 * the filesystem for it's own opinion. This means, that an update
241 * mount of the underlying filesystem to go from rdonly to rdwr
242 * will leave the unioned view as read-only.
244 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
246 mp->mnt_data = (qaddr_t) um;
249 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
250 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
264 bcopy(cp, mp->mnt_stat.f_mntfromname, len);
266 cp = mp->mnt_stat.f_mntfromname + len;
267 len = MNAMELEN - len;
269 (void) copyinstr(args.target, cp, len - 1, &size);
270 bzero(cp + size, len - size);
272 (void)union_statfs(mp, &mp->mnt_stat, p);
274 #ifdef UNION_DIAGNOSTIC
275 printf("union_mount: from %s, on %s\n",
276 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
282 free(um, M_UNIONFSMNT);
293 * VFS start. Nothing needed here - the start routine
294 * on the underlying filesystem(s) will have been called
295 * when that filesystem was mounted.
298 union_start(mp, flags, p)
308 * Free reference to union layer
311 union_unmount(mp, mntflags, p)
316 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
317 struct vnode *um_rootvp;
322 #ifdef UNION_DIAGNOSTIC
323 printf("union_unmount(mp = %x)\n", mp);
326 if (mntflags & MNT_FORCE)
329 if (error = union_root(mp, &um_rootvp))
333 * Keep flushing vnodes from the mount list.
334 * This is needed because of the un_pvp held
335 * reference to the parent vnode.
336 * If more vnodes have been freed on a given pass,
337 * the try again. The loop will iterate at most
338 * (d) times, where (d) is the maximum tree depth
341 for (freeing = 0; vflush(mp, um_rootvp, flags) != 0;) {
345 /* count #vnodes held on mount list */
346 for (n = 0, vp = mp->mnt_vnodelist.lh_first;
348 vp = vp->v_mntvnodes.le_next)
351 /* if this is unchanged then stop */
355 /* otherwise try once more time */
359 /* At this point the root vnode should have a single reference */
360 if (um_rootvp->v_usecount > 1) {
365 #ifdef UNION_DIAGNOSTIC
366 vprint("union root", um_rootvp);
369 * Discard references to upper and lower target vnodes.
372 vrele(um->um_lowervp);
373 vrele(um->um_uppervp);
376 * Release reference on underlying root vnode
380 * And blow it away for future re-use
384 * Finally, throw away the union_mount structure
386 free(mp->mnt_data, M_UNIONFSMNT); /* XXX */
396 struct proc *p = curproc; /* XXX */
397 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
402 if (um->um_lowervp && um->um_op != UNMNT_BELOW &&
403 VOP_ISLOCKED(um->um_lowervp)) {
404 VREF(um->um_lowervp);
405 VOP_UNLOCK(um->um_lowervp, 0, p);
410 * Return locked reference to root.
412 VREF(um->um_uppervp);
413 if ((um->um_op == UNMNT_BELOW) &&
414 VOP_ISLOCKED(um->um_uppervp)) {
417 vn_lock(um->um_uppervp, LK_EXCLUSIVE | LK_RETRY, p);
421 VREF(um->um_lowervp);
422 error = union_allocvp(vpp, mp,
425 (struct componentname *) 0,
432 vrele(um->um_uppervp);
434 vput(um->um_uppervp);
436 vrele(um->um_lowervp);
439 VTOUNION(*vpp)->un_flags &= ~UN_ULOCK;
442 vn_lock(um->um_lowervp, LK_EXCLUSIVE | LK_RETRY, p);
443 vrele(um->um_lowervp);
450 union_statfs(mp, sbp, p)
456 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
460 #ifdef UNION_DIAGNOSTIC
461 printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp,
466 bzero(&mstat, sizeof(mstat));
468 if (um->um_lowervp) {
469 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p);
474 /* now copy across the "interesting" information and fake the rest */
476 sbp->f_type = mstat.f_type;
477 sbp->f_flags = mstat.f_flags;
478 sbp->f_bsize = mstat.f_bsize;
479 sbp->f_iosize = mstat.f_iosize;
481 lbsize = mstat.f_bsize;
482 sbp->f_blocks = mstat.f_blocks;
483 sbp->f_bfree = mstat.f_bfree;
484 sbp->f_bavail = mstat.f_bavail;
485 sbp->f_files = mstat.f_files;
486 sbp->f_ffree = mstat.f_ffree;
488 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p);
492 sbp->f_flags = mstat.f_flags;
493 sbp->f_bsize = mstat.f_bsize;
494 sbp->f_iosize = mstat.f_iosize;
497 * if the lower and upper blocksizes differ, then frig the
498 * block counts so that the sizes reported by df make some
499 * kind of sense. none of this makes sense though.
502 if (mstat.f_bsize != lbsize)
503 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
506 * The "total" fields count total resources in all layers,
507 * the "free" fields count only those resources which are
508 * free in the upper layer (since only the upper layer
511 sbp->f_blocks += mstat.f_blocks;
512 sbp->f_bfree = mstat.f_bfree;
513 sbp->f_bavail = mstat.f_bavail;
514 sbp->f_files += mstat.f_files;
515 sbp->f_ffree = mstat.f_ffree;
517 if (sbp != &mp->mnt_stat) {
518 sbp->f_type = mp->mnt_vfc->vfc_typenum;
519 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
520 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
521 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
527 * Complement to all vpp returning ops.
528 * XXX - initially only to get rid of WILLRELE.
529 * XXX - may change when modification of vops start.
542 * XXX - Assumes no data cached at union layer.
544 #define union_sync ((int (*) __P((struct mount *, int, struct ucred *, \
545 struct proc *)))nullop)
547 #define union_fhtovp ((int (*) __P((struct mount *, struct fid *, \
548 struct sockaddr *, struct vnode **, int *, struct ucred **)))eopnotsupp)
549 #define union_quotactl ((int (*) __P((struct mount *, int, uid_t, caddr_t, \
550 struct proc *)))eopnotsupp)
551 #define union_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
552 size_t, struct proc *)))eopnotsupp)
553 #define union_vget ((int (*) __P((struct mount *, ino_t, struct vnode **))) \
555 #define union_vptofh ((int (*) __P((struct vnode *, struct fid *)))eopnotsupp)
557 static struct vfsops union_vfsops = {
572 VFS_SET(union_vfsops, union, MOUNT_UNION, VFCF_LOOPBACK);