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 * $FreeBSD: src/sys/miscfs/union/union_vfsops.c,v 1.39.2.2 2001/10/25 19:18:53 dillon Exp $
39 * $DragonFly: src/sys/vfs/union/union_vfsops.c,v 1.5 2003/08/20 09:56:34 rob Exp $
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
54 #include <sys/filedesc.h>
56 #include <vm/vm_zone.h>
58 static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure");
60 extern int union_init (struct vfsconf *);
61 static int union_mount (struct mount *mp, char *path, caddr_t data,
62 struct nameidata *ndp, struct thread *td);
63 static int union_root (struct mount *mp, struct vnode **vpp);
64 static int union_statfs (struct mount *mp, struct statfs *sbp,
66 static int union_unmount (struct mount *mp, int mntflags,
70 * Mount union filesystem
73 union_mount(mp, path, data, ndp, td)
77 struct nameidata *ndp;
81 struct union_args args;
82 struct vnode *lowerrootvp = NULLVP;
83 struct vnode *upperrootvp = NULLVP;
84 struct union_mount *um = 0;
85 struct ucred *cred = 0;
90 UDEBUG(("union_mount(mp = %p)\n", (void *)mp));
92 KKASSERT(td->td_proc);
95 * Disable clustered write, otherwise system becomes unstable.
97 mp->mnt_flag |= MNT_NOCLUSTERW;
102 if (mp->mnt_flag & MNT_UPDATE) {
105 * 1. a way to convert between rdonly and rdwr mounts.
106 * 2. support for nfs exports.
115 error = copyin(data, (caddr_t)&args, sizeof(struct union_args));
120 * Obtain lower vnode. Vnode is stored in mp->mnt_vnodecovered.
121 * We need to reference it but not lock it.
124 lowerrootvp = mp->mnt_vnodecovered;
129 * Unlock lower node to avoid deadlock.
131 if (lowerrootvp->v_op == union_vnodeop_p)
132 VOP_UNLOCK(lowerrootvp, 0, td);
136 * Obtain upper vnode by calling namei() on the path. The
137 * upperrootvp will be turned referenced but not locked.
139 NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT, UIO_USERSPACE, args.target, td);
144 if (lowerrootvp->v_op == union_vnodeop_p)
145 vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td);
150 NDFREE(ndp, NDF_ONLY_PNBUF);
151 upperrootvp = ndp->ni_vp;
155 UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp,
156 VOP_ISLOCKED(upperrootvp, NULL)));
159 * Check multi union mount to avoid `lock myself again' panic.
160 * Also require that it be a directory.
162 if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) {
164 printf("union_mount: multi union mount?\n");
170 if (upperrootvp->v_type != VDIR) {
176 * Allocate our union_mount structure and populate the fields.
177 * The vnode references are stored in the union_mount as held,
178 * unlocked references. Depending on the _BELOW flag, the
179 * filesystems are viewed in a different order. In effect this
180 * is the same as providing a mount-under option to the mount
184 um = (struct union_mount *) malloc(sizeof(struct union_mount),
185 M_UNIONFSMNT, M_WAITOK);
187 bzero(um, sizeof(struct union_mount));
189 um->um_op = args.mntflags & UNMNT_OPMASK;
193 um->um_lowervp = lowerrootvp;
194 um->um_uppervp = upperrootvp;
200 um->um_lowervp = upperrootvp;
201 um->um_uppervp = lowerrootvp;
209 um->um_uppervp = upperrootvp;
210 um->um_lowervp = lowerrootvp;
220 * Unless the mount is readonly, ensure that the top layer
221 * supports whiteout operations
223 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
224 error = VOP_WHITEOUT(um->um_uppervp, NULL, LOOKUP);
229 um->um_cred = crhold(td->td_proc->p_ucred);
230 um->um_cmode = UN_DIRMODE & ~td->td_proc->p_fd->fd_cmask;
233 * Depending on what you think the MNT_LOCAL flag might mean,
234 * you may want the && to be || on the conditional below.
235 * At the moment it has been defined that the filesystem is
236 * only local if it is all local, ie the MNT_LOCAL flag implies
237 * that the entire namespace is local. If you think the MNT_LOCAL
238 * flag implies that some of the files might be stored locally
239 * then you will want to change the conditional.
241 if (um->um_op == UNMNT_ABOVE) {
242 if (((um->um_lowervp == NULLVP) ||
243 (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) &&
244 (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL))
245 mp->mnt_flag |= MNT_LOCAL;
249 * Copy in the upper layer's RDONLY flag. This is for the benefit
250 * of lookup() which explicitly checks the flag, rather than asking
251 * the filesystem for its own opinion. This means, that an update
252 * mount of the underlying filesystem to go from rdonly to rdwr
253 * will leave the unioned view as read-only.
255 mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY);
257 mp->mnt_data = (qaddr_t) um;
260 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
261 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
275 bcopy(cp, mp->mnt_stat.f_mntfromname, len);
277 cp = mp->mnt_stat.f_mntfromname + len;
278 len = MNAMELEN - len;
280 (void) copyinstr(args.target, cp, len - 1, &size);
281 bzero(cp + size, len - size);
283 (void)union_statfs(mp, &mp->mnt_stat, td);
285 UDEBUG(("union_mount: from %s, on %s\n",
286 mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname));
292 vrele(um->um_uppervp);
294 vrele(um->um_lowervp);
295 /* XXX other fields */
296 free(um, M_UNIONFSMNT);
308 * Free reference to union layer
311 union_unmount(mp, mntflags, td)
316 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
321 UDEBUG(("union_unmount(mp = %p)\n", (void *)mp));
323 if (mntflags & MNT_FORCE)
327 * Keep flushing vnodes from the mount list.
328 * This is needed because of the un_pvp held
329 * reference to the parent vnode.
330 * If more vnodes have been freed on a given pass,
331 * the try again. The loop will iterate at most
332 * (d) times, where (d) is the maximum tree depth
335 for (freeing = 0; (error = vflush(mp, 0, flags)) != 0;) {
339 /* count #vnodes held on mount list */
340 for (n = 0, vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
342 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
346 /* if this is unchanged then stop */
350 /* otherwise try once more time */
354 /* If the most recent vflush failed, the filesystem is still busy. */
359 * Discard references to upper and lower target vnodes.
362 vrele(um->um_lowervp);
363 vrele(um->um_uppervp);
366 * Finally, throw away the union_mount structure
368 free(mp->mnt_data, M_UNIONFSMNT); /* XXX */
378 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
382 * Supply an unlocked reference to um_uppervp and to um_lowervp. It
383 * is possible for um_uppervp to be locked without the associated
384 * root union_node being locked. We let union_allocvp() deal with
387 UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp,
388 VOP_ISLOCKED(um->um_uppervp, NULL)));
390 VREF(um->um_uppervp);
392 VREF(um->um_lowervp);
394 error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL,
395 um->um_uppervp, um->um_lowervp, 1);
396 UDEBUG(("error %d\n", error));
397 UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp,
398 VOP_ISLOCKED(um->um_uppervp, NULL)));
404 union_statfs(mp, sbp, td)
410 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
414 UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n",
415 (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp));
417 bzero(&mstat, sizeof(mstat));
419 if (um->um_lowervp) {
420 error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, td);
425 /* now copy across the "interesting" information and fake the rest */
427 sbp->f_type = mstat.f_type;
428 sbp->f_flags = mstat.f_flags;
429 sbp->f_bsize = mstat.f_bsize;
430 sbp->f_iosize = mstat.f_iosize;
432 lbsize = mstat.f_bsize;
433 sbp->f_blocks = mstat.f_blocks;
434 sbp->f_bfree = mstat.f_bfree;
435 sbp->f_bavail = mstat.f_bavail;
436 sbp->f_files = mstat.f_files;
437 sbp->f_ffree = mstat.f_ffree;
439 error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, td);
443 sbp->f_flags = mstat.f_flags;
444 sbp->f_bsize = mstat.f_bsize;
445 sbp->f_iosize = mstat.f_iosize;
448 * if the lower and upper blocksizes differ, then frig the
449 * block counts so that the sizes reported by df make some
450 * kind of sense. none of this makes sense though.
453 if (mstat.f_bsize != lbsize)
454 sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize;
457 * The "total" fields count total resources in all layers,
458 * the "free" fields count only those resources which are
459 * free in the upper layer (since only the upper layer
462 sbp->f_blocks += mstat.f_blocks;
463 sbp->f_bfree = mstat.f_bfree;
464 sbp->f_bavail = mstat.f_bavail;
465 sbp->f_files += mstat.f_files;
466 sbp->f_ffree = mstat.f_ffree;
468 if (sbp != &mp->mnt_stat) {
469 sbp->f_type = mp->mnt_vfc->vfc_typenum;
470 bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
471 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
472 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
477 static struct vfsops union_vfsops = {
479 vfs_stdstart, /* underlying start already done */
484 vfs_stdsync, /* XXX assumes no cached data on union level */
494 VFS_SET(union_vfsops, union, VFCF_LOOPBACK);