1d167cf6fSWarner Losh /*- 2996c772fSJohn Dyson * Copyright (c) 1994, 1995 The Regents of the University of California. 3996c772fSJohn Dyson * Copyright (c) 1994, 1995 Jan-Simon Pendry. 4df8bae1dSRodney W. Grimes * All rights reserved. 5df8bae1dSRodney W. Grimes * 6df8bae1dSRodney W. Grimes * This code is derived from software donated to Berkeley by 7df8bae1dSRodney W. Grimes * Jan-Simon Pendry. 8df8bae1dSRodney W. Grimes * 9df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 10df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 11df8bae1dSRodney W. Grimes * are met: 12df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 14df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 15df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 16df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 33996c772fSJohn Dyson * @(#)union_vfsops.c 8.20 (Berkeley) 5/20/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes /* 38df8bae1dSRodney W. Grimes * Union Layer 39df8bae1dSRodney W. Grimes */ 40df8bae1dSRodney W. Grimes 41df8bae1dSRodney W. Grimes #include <sys/param.h> 42df8bae1dSRodney W. Grimes #include <sys/systm.h> 43c9b1d604SGarrett Wollman #include <sys/kernel.h> 44805d90f7SJohn Baldwin #include <sys/lock.h> 45805d90f7SJohn Baldwin #include <sys/mutex.h> 46df8bae1dSRodney W. Grimes #include <sys/proc.h> 47df8bae1dSRodney W. Grimes #include <sys/vnode.h> 48df8bae1dSRodney W. Grimes #include <sys/mount.h> 49df8bae1dSRodney W. Grimes #include <sys/namei.h> 50df8bae1dSRodney W. Grimes #include <sys/malloc.h> 51df8bae1dSRodney W. Grimes #include <sys/filedesc.h> 5299d300a1SRuslan Ermilov #include <fs/unionfs/union.h> 53df8bae1dSRodney W. Grimes 54a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_UNIONFSMNT, "UNION mount", "UNION mount structure"); 55a1c995b6SPoul-Henning Kamp 567652131bSPoul-Henning Kamp extern vfs_init_t union_init; 577652131bSPoul-Henning Kamp static vfs_root_t union_root; 585e8c582aSPoul-Henning Kamp static vfs_mount_t union_mount; 597652131bSPoul-Henning Kamp static vfs_statfs_t union_statfs; 607652131bSPoul-Henning Kamp static vfs_unmount_t union_unmount; 61b5e8ce9fSBruce Evans 62df8bae1dSRodney W. Grimes /* 631c4ccf09SDon Lewis * Mount union filesystem. 64df8bae1dSRodney W. Grimes */ 6580b301c3SPoul-Henning Kamp static int 665e8c582aSPoul-Henning Kamp union_mount(mp, td) 67df8bae1dSRodney W. Grimes struct mount *mp; 68b40ce416SJulian Elischer struct thread *td; 69df8bae1dSRodney W. Grimes { 70df8bae1dSRodney W. Grimes int error = 0; 71a9f5c04aSMaxime Henrion struct vfsoptlist *opts; 72df8bae1dSRodney W. Grimes struct vnode *lowerrootvp = NULLVP; 73df8bae1dSRodney W. Grimes struct vnode *upperrootvp = NULLVP; 74996c772fSJohn Dyson struct union_mount *um = 0; 75919f5630STakanori Watanabe struct vattr va; 76df8bae1dSRodney W. Grimes struct ucred *cred = 0; 77a9f5c04aSMaxime Henrion char *cp = 0, *target; 784ce86ffdSMaxime Henrion int op; 79df8bae1dSRodney W. Grimes int len; 80a8f15ed7SMaxime Henrion size_t size; 81d1841903STim J. Robbins struct componentname fakecn; 825e8c582aSPoul-Henning Kamp struct nameidata nd, *ndp = &nd; 83df8bae1dSRodney W. Grimes 842a31267eSMatthew Dillon UDEBUG(("union_mount(mp = %p)\n", (void *)mp)); 85df8bae1dSRodney W. Grimes 86a9f5c04aSMaxime Henrion opts = mp->mnt_optnew; 87df8bae1dSRodney W. Grimes /* 8881bca6ddSKATO Takenori * Disable clustered write, otherwise system becomes unstable. 8981bca6ddSKATO Takenori */ 9081bca6ddSKATO Takenori mp->mnt_flag |= MNT_NOCLUSTERW; 9181bca6ddSKATO Takenori 9264042a76SPoul-Henning Kamp if (mp->mnt_flag & MNT_ROOTFS) 9364042a76SPoul-Henning Kamp return (EOPNOTSUPP); 9481bca6ddSKATO Takenori /* 95df8bae1dSRodney W. Grimes * Update is a no-op 96df8bae1dSRodney W. Grimes */ 97a9f5c04aSMaxime Henrion if (mp->mnt_flag & MNT_UPDATE) 98df8bae1dSRodney W. Grimes /* 991c4ccf09SDon Lewis * Need to provide: 100df8bae1dSRodney W. Grimes * 1. a way to convert between rdonly and rdwr mounts. 101df8bae1dSRodney W. Grimes * 2. support for nfs exports. 102df8bae1dSRodney W. Grimes */ 103a9f5c04aSMaxime Henrion return (EOPNOTSUPP); 104df8bae1dSRodney W. Grimes 105df8bae1dSRodney W. Grimes /* 106a9f5c04aSMaxime Henrion * Get arguments. 107df8bae1dSRodney W. Grimes */ 108a9f5c04aSMaxime Henrion error = vfs_getopt(opts, "target", (void **)&target, &len); 109a9f5c04aSMaxime Henrion if (error || target[len - 1] != '\0') 110a9f5c04aSMaxime Henrion return (EINVAL); 111a9f5c04aSMaxime Henrion 1124ce86ffdSMaxime Henrion op = 0; 1134ce86ffdSMaxime Henrion if (vfs_getopt(opts, "below", NULL, NULL) == 0) 1144ce86ffdSMaxime Henrion op = UNMNT_BELOW; 1154ce86ffdSMaxime Henrion if (vfs_getopt(opts, "replace", NULL, NULL) == 0) { 1164ce86ffdSMaxime Henrion /* These options are mutually exclusive. */ 1174ce86ffdSMaxime Henrion if (op) 118a9f5c04aSMaxime Henrion return (EINVAL); 1194ce86ffdSMaxime Henrion op = UNMNT_REPLACE; 1204ce86ffdSMaxime Henrion } 1214ce86ffdSMaxime Henrion /* 1224ce86ffdSMaxime Henrion * UNMNT_ABOVE is the default. 1234ce86ffdSMaxime Henrion */ 1244ce86ffdSMaxime Henrion if (op == 0) 1254ce86ffdSMaxime Henrion op = UNMNT_ABOVE; 126df8bae1dSRodney W. Grimes 1272a31267eSMatthew Dillon /* 1282a31267eSMatthew Dillon * Obtain lower vnode. Vnode is stored in mp->mnt_vnodecovered. 1292a31267eSMatthew Dillon * We need to reference it but not lock it. 1302a31267eSMatthew Dillon */ 1312a31267eSMatthew Dillon 132df8bae1dSRodney W. Grimes lowerrootvp = mp->mnt_vnodecovered; 133df8bae1dSRodney W. Grimes VREF(lowerrootvp); 134df8bae1dSRodney W. Grimes 1352a31267eSMatthew Dillon #if 0 136df8bae1dSRodney W. Grimes /* 1376db918e3SKATO Takenori * Unlock lower node to avoid deadlock. 1386db918e3SKATO Takenori */ 139afc2a558SKATO Takenori if (lowerrootvp->v_op == union_vnodeop_p) 140b40ce416SJulian Elischer VOP_UNLOCK(lowerrootvp, 0, td); 1412a31267eSMatthew Dillon #endif 1426db918e3SKATO Takenori 1436db918e3SKATO Takenori /* 1442a31267eSMatthew Dillon * Obtain upper vnode by calling namei() on the path. The 1452a31267eSMatthew Dillon * upperrootvp will be turned referenced but not locked. 146df8bae1dSRodney W. Grimes */ 147a9f5c04aSMaxime Henrion NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT, UIO_SYSSPACE, target, td); 148df8bae1dSRodney W. Grimes 1493a773ad0SPoul-Henning Kamp error = namei(ndp); 1502a31267eSMatthew Dillon 1512a31267eSMatthew Dillon #if 0 152afc2a558SKATO Takenori if (lowerrootvp->v_op == union_vnodeop_p) 153b40ce416SJulian Elischer vn_lock(lowerrootvp, LK_EXCLUSIVE | LK_RETRY, td); 1542a31267eSMatthew Dillon #endif 1553a773ad0SPoul-Henning Kamp if (error) 156df8bae1dSRodney W. Grimes goto bad; 157df8bae1dSRodney W. Grimes 158762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 159df8bae1dSRodney W. Grimes upperrootvp = ndp->ni_vp; 160df8bae1dSRodney W. Grimes vrele(ndp->ni_dvp); 161df8bae1dSRodney W. Grimes ndp->ni_dvp = NULL; 162df8bae1dSRodney W. Grimes 1636bdfe06aSEivind Eklund UDEBUG(("mount_root UPPERVP %p locked = %d\n", upperrootvp, 1646bdfe06aSEivind Eklund VOP_ISLOCKED(upperrootvp, NULL))); 1652a31267eSMatthew Dillon 1666db918e3SKATO Takenori /* 1676db918e3SKATO Takenori * Check multi union mount to avoid `lock myself again' panic. 1682a31267eSMatthew Dillon * Also require that it be a directory. 1696db918e3SKATO Takenori */ 1706db918e3SKATO Takenori if (upperrootvp == VTOUNION(lowerrootvp)->un_uppervp) { 171747e9157SKATO Takenori #ifdef DIAGNOSTIC 172747e9157SKATO Takenori printf("union_mount: multi union mount?\n"); 173747e9157SKATO Takenori #endif 1746db918e3SKATO Takenori error = EDEADLK; 1756db918e3SKATO Takenori goto bad; 1766db918e3SKATO Takenori } 1776db918e3SKATO Takenori 178df8bae1dSRodney W. Grimes if (upperrootvp->v_type != VDIR) { 179df8bae1dSRodney W. Grimes error = EINVAL; 180df8bae1dSRodney W. Grimes goto bad; 181df8bae1dSRodney W. Grimes } 182df8bae1dSRodney W. Grimes 183df8bae1dSRodney W. Grimes /* 1842a31267eSMatthew Dillon * Allocate our union_mount structure and populate the fields. 1852a31267eSMatthew Dillon * The vnode references are stored in the union_mount as held, 1862a31267eSMatthew Dillon * unlocked references. Depending on the _BELOW flag, the 1872a31267eSMatthew Dillon * filesystems are viewed in a different order. In effect this 1882a31267eSMatthew Dillon * is the same as providing a mount-under option to the mount 1892a31267eSMatthew Dillon * syscall. 190df8bae1dSRodney W. Grimes */ 191df8bae1dSRodney W. Grimes 1922a31267eSMatthew Dillon um = (struct union_mount *) malloc(sizeof(struct union_mount), 193a163d034SWarner Losh M_UNIONFSMNT, M_WAITOK | M_ZERO); 1942a31267eSMatthew Dillon 1954ce86ffdSMaxime Henrion um->um_op = op; 1962a31267eSMatthew Dillon 197919f5630STakanori Watanabe error = VOP_GETATTR(upperrootvp, &va, td->td_ucred, td); 198919f5630STakanori Watanabe if (error) 199919f5630STakanori Watanabe goto bad; 200919f5630STakanori Watanabe 201919f5630STakanori Watanabe um->um_upperdev = va.va_fsid; 202919f5630STakanori Watanabe 203df8bae1dSRodney W. Grimes switch (um->um_op) { 204df8bae1dSRodney W. Grimes case UNMNT_ABOVE: 205df8bae1dSRodney W. Grimes um->um_lowervp = lowerrootvp; 206df8bae1dSRodney W. Grimes um->um_uppervp = upperrootvp; 2072a31267eSMatthew Dillon upperrootvp = NULL; 2082a31267eSMatthew Dillon lowerrootvp = NULL; 209df8bae1dSRodney W. Grimes break; 210df8bae1dSRodney W. Grimes 211df8bae1dSRodney W. Grimes case UNMNT_BELOW: 212df8bae1dSRodney W. Grimes um->um_lowervp = upperrootvp; 213df8bae1dSRodney W. Grimes um->um_uppervp = lowerrootvp; 2142a31267eSMatthew Dillon upperrootvp = NULL; 2152a31267eSMatthew Dillon lowerrootvp = NULL; 216df8bae1dSRodney W. Grimes break; 217df8bae1dSRodney W. Grimes 218df8bae1dSRodney W. Grimes case UNMNT_REPLACE: 219df8bae1dSRodney W. Grimes vrele(lowerrootvp); 2202a31267eSMatthew Dillon lowerrootvp = NULL; 221df8bae1dSRodney W. Grimes um->um_uppervp = upperrootvp; 222df8bae1dSRodney W. Grimes um->um_lowervp = lowerrootvp; 2232a31267eSMatthew Dillon upperrootvp = NULL; 224df8bae1dSRodney W. Grimes break; 225df8bae1dSRodney W. Grimes 226df8bae1dSRodney W. Grimes default: 227df8bae1dSRodney W. Grimes error = EINVAL; 228df8bae1dSRodney W. Grimes goto bad; 229df8bae1dSRodney W. Grimes } 230df8bae1dSRodney W. Grimes 231996c772fSJohn Dyson /* 232996c772fSJohn Dyson * Unless the mount is readonly, ensure that the top layer 2331c4ccf09SDon Lewis * supports whiteout operations. 234996c772fSJohn Dyson */ 235996c772fSJohn Dyson if ((mp->mnt_flag & MNT_RDONLY) == 0) { 236d1841903STim J. Robbins /* 237d1841903STim J. Robbins * XXX Fake up a struct componentname with only cn_nameiop 238d1841903STim J. Robbins * and cn_thread valid; union_whiteout() needs to use the 239d1841903STim J. Robbins * thread pointer to lock the vnode. 240d1841903STim J. Robbins */ 241d1841903STim J. Robbins bzero(&fakecn, sizeof(fakecn)); 242d1841903STim J. Robbins fakecn.cn_nameiop = LOOKUP; 243d1841903STim J. Robbins fakecn.cn_thread = td; 244d1841903STim J. Robbins error = VOP_WHITEOUT(um->um_uppervp, &fakecn, LOOKUP); 245996c772fSJohn Dyson if (error) 246996c772fSJohn Dyson goto bad; 247996c772fSJohn Dyson } 248996c772fSJohn Dyson 249a854ed98SJohn Baldwin um->um_cred = crhold(td->td_ucred); 250124e4c3bSPoul-Henning Kamp FILEDESC_LOCK_FAST(td->td_proc->p_fd); 251b40ce416SJulian Elischer um->um_cmode = UN_DIRMODE &~ td->td_proc->p_fd->fd_cmask; 252124e4c3bSPoul-Henning Kamp FILEDESC_UNLOCK_FAST(td->td_proc->p_fd); 253df8bae1dSRodney W. Grimes 254df8bae1dSRodney W. Grimes /* 255df8bae1dSRodney W. Grimes * Depending on what you think the MNT_LOCAL flag might mean, 256df8bae1dSRodney W. Grimes * you may want the && to be || on the conditional below. 257df8bae1dSRodney W. Grimes * At the moment it has been defined that the filesystem is 258df8bae1dSRodney W. Grimes * only local if it is all local, ie the MNT_LOCAL flag implies 259df8bae1dSRodney W. Grimes * that the entire namespace is local. If you think the MNT_LOCAL 260df8bae1dSRodney W. Grimes * flag implies that some of the files might be stored locally 261df8bae1dSRodney W. Grimes * then you will want to change the conditional. 262df8bae1dSRodney W. Grimes */ 263df8bae1dSRodney W. Grimes if (um->um_op == UNMNT_ABOVE) { 264df8bae1dSRodney W. Grimes if (((um->um_lowervp == NULLVP) || 265df8bae1dSRodney W. Grimes (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && 266df8bae1dSRodney W. Grimes (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) 267df8bae1dSRodney W. Grimes mp->mnt_flag |= MNT_LOCAL; 268df8bae1dSRodney W. Grimes } 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes /* 271df8bae1dSRodney W. Grimes * Copy in the upper layer's RDONLY flag. This is for the benefit 272df8bae1dSRodney W. Grimes * of lookup() which explicitly checks the flag, rather than asking 273dc733423SDag-Erling Smørgrav * the filesystem for its own opinion. This means, that an update 274df8bae1dSRodney W. Grimes * mount of the underlying filesystem to go from rdonly to rdwr 275df8bae1dSRodney W. Grimes * will leave the unioned view as read-only. 276df8bae1dSRodney W. Grimes */ 277df8bae1dSRodney W. Grimes mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); 278df8bae1dSRodney W. Grimes 279df8bae1dSRodney W. Grimes mp->mnt_data = (qaddr_t) um; 280996c772fSJohn Dyson vfs_getnewfsid(mp); 281df8bae1dSRodney W. Grimes 282df8bae1dSRodney W. Grimes switch (um->um_op) { 283df8bae1dSRodney W. Grimes case UNMNT_ABOVE: 284996c772fSJohn Dyson cp = "<above>:"; 285df8bae1dSRodney W. Grimes break; 286df8bae1dSRodney W. Grimes case UNMNT_BELOW: 287996c772fSJohn Dyson cp = "<below>:"; 288df8bae1dSRodney W. Grimes break; 289df8bae1dSRodney W. Grimes case UNMNT_REPLACE: 290df8bae1dSRodney W. Grimes cp = ""; 291df8bae1dSRodney W. Grimes break; 292df8bae1dSRodney W. Grimes } 293df8bae1dSRodney W. Grimes len = strlen(cp); 294df8bae1dSRodney W. Grimes bcopy(cp, mp->mnt_stat.f_mntfromname, len); 295df8bae1dSRodney W. Grimes 296df8bae1dSRodney W. Grimes cp = mp->mnt_stat.f_mntfromname + len; 297df8bae1dSRodney W. Grimes len = MNAMELEN - len; 298df8bae1dSRodney W. Grimes 299a9f5c04aSMaxime Henrion (void) copystr(target, cp, len - 1, &size); 300df8bae1dSRodney W. Grimes bzero(cp + size, len - size); 301df8bae1dSRodney W. Grimes 3022a31267eSMatthew Dillon UDEBUG(("union_mount: from %s, on %s\n", 3032a31267eSMatthew Dillon mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname)); 304df8bae1dSRodney W. Grimes return (0); 305df8bae1dSRodney W. Grimes 306df8bae1dSRodney W. Grimes bad: 3072a31267eSMatthew Dillon if (um) { 3082a31267eSMatthew Dillon if (um->um_uppervp) 3092a31267eSMatthew Dillon vrele(um->um_uppervp); 3102a31267eSMatthew Dillon if (um->um_lowervp) 3112a31267eSMatthew Dillon vrele(um->um_lowervp); 3122a31267eSMatthew Dillon /* XXX other fields */ 313a1c995b6SPoul-Henning Kamp free(um, M_UNIONFSMNT); 3142a31267eSMatthew Dillon } 315df8bae1dSRodney W. Grimes if (cred) 316df8bae1dSRodney W. Grimes crfree(cred); 317df8bae1dSRodney W. Grimes if (upperrootvp) 318df8bae1dSRodney W. Grimes vrele(upperrootvp); 319df8bae1dSRodney W. Grimes if (lowerrootvp) 320df8bae1dSRodney W. Grimes vrele(lowerrootvp); 321df8bae1dSRodney W. Grimes return (error); 322df8bae1dSRodney W. Grimes } 323df8bae1dSRodney W. Grimes 324df8bae1dSRodney W. Grimes /* 3251c4ccf09SDon Lewis * Free reference to union layer. 326df8bae1dSRodney W. Grimes */ 32780b301c3SPoul-Henning Kamp static int 328b40ce416SJulian Elischer union_unmount(mp, mntflags, td) 329df8bae1dSRodney W. Grimes struct mount *mp; 330df8bae1dSRodney W. Grimes int mntflags; 331b40ce416SJulian Elischer struct thread *td; 332df8bae1dSRodney W. Grimes { 333df8bae1dSRodney W. Grimes struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 334df8bae1dSRodney W. Grimes int error; 335996c772fSJohn Dyson int freeing; 336df8bae1dSRodney W. Grimes int flags = 0; 337df8bae1dSRodney W. Grimes 3382a31267eSMatthew Dillon UDEBUG(("union_unmount(mp = %p)\n", (void *)mp)); 339df8bae1dSRodney W. Grimes 340996c772fSJohn Dyson if (mntflags & MNT_FORCE) 341996c772fSJohn Dyson flags |= FORCECLOSE; 342996c772fSJohn Dyson 343996c772fSJohn Dyson /* 344996c772fSJohn Dyson * Keep flushing vnodes from the mount list. 345996c772fSJohn Dyson * This is needed because of the un_pvp held 346996c772fSJohn Dyson * reference to the parent vnode. 347996c772fSJohn Dyson * If more vnodes have been freed on a given pass, 348996c772fSJohn Dyson * the try again. The loop will iterate at most 349996c772fSJohn Dyson * (d) times, where (d) is the maximum tree depth 350996c772fSJohn Dyson * in the filesystem. 351996c772fSJohn Dyson */ 352f257b7a5SAlfred Perlstein for (freeing = 0; (error = vflush(mp, 0, flags, td)) != 0;) { 353996c772fSJohn Dyson int n; 354996c772fSJohn Dyson 355996c772fSJohn Dyson /* count #vnodes held on mount list */ 356b792e030SAlexander Kabaev n = mp->mnt_nvnodelistsize; 357996c772fSJohn Dyson 358996c772fSJohn Dyson /* if this is unchanged then stop */ 359996c772fSJohn Dyson if (n == freeing) 360996c772fSJohn Dyson break; 361996c772fSJohn Dyson 362996c772fSJohn Dyson /* otherwise try once more time */ 363996c772fSJohn Dyson freeing = n; 364df8bae1dSRodney W. Grimes } 365df8bae1dSRodney W. Grimes 3661c4ccf09SDon Lewis /* 3671c4ccf09SDon Lewis * If the most recent vflush failed, the filesystem is still busy. 3681c4ccf09SDon Lewis */ 3690864ef1eSIan Dowse if (error) 3700864ef1eSIan Dowse return (error); 371df8bae1dSRodney W. Grimes 372df8bae1dSRodney W. Grimes /* 373df8bae1dSRodney W. Grimes * Discard references to upper and lower target vnodes. 374df8bae1dSRodney W. Grimes */ 375df8bae1dSRodney W. Grimes if (um->um_lowervp) 376df8bae1dSRodney W. Grimes vrele(um->um_lowervp); 377df8bae1dSRodney W. Grimes vrele(um->um_uppervp); 378df8bae1dSRodney W. Grimes crfree(um->um_cred); 379df8bae1dSRodney W. Grimes /* 3801c4ccf09SDon Lewis * Finally, throw away the union_mount structure. 381df8bae1dSRodney W. Grimes */ 382a1c995b6SPoul-Henning Kamp free(mp->mnt_data, M_UNIONFSMNT); /* XXX */ 383df8bae1dSRodney W. Grimes mp->mnt_data = 0; 384df8bae1dSRodney W. Grimes return (0); 385df8bae1dSRodney W. Grimes } 386df8bae1dSRodney W. Grimes 38780b301c3SPoul-Henning Kamp static int 388f257b7a5SAlfred Perlstein union_root(mp, vpp, td) 389df8bae1dSRodney W. Grimes struct mount *mp; 390df8bae1dSRodney W. Grimes struct vnode **vpp; 391f257b7a5SAlfred Perlstein struct thread *td; 392df8bae1dSRodney W. Grimes { 393df8bae1dSRodney W. Grimes struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 394df8bae1dSRodney W. Grimes int error; 395df8bae1dSRodney W. Grimes 396df8bae1dSRodney W. Grimes /* 3972a31267eSMatthew Dillon * Supply an unlocked reference to um_uppervp and to um_lowervp. It 3982a31267eSMatthew Dillon * is possible for um_uppervp to be locked without the associated 3992a31267eSMatthew Dillon * root union_node being locked. We let union_allocvp() deal with 4002a31267eSMatthew Dillon * it. 401df8bae1dSRodney W. Grimes */ 4026bdfe06aSEivind Eklund UDEBUG(("union_root UPPERVP %p locked = %d\n", um->um_uppervp, 4036bdfe06aSEivind Eklund VOP_ISLOCKED(um->um_uppervp, NULL))); 4042a31267eSMatthew Dillon 405df8bae1dSRodney W. Grimes VREF(um->um_uppervp); 406df8bae1dSRodney W. Grimes if (um->um_lowervp) 407df8bae1dSRodney W. Grimes VREF(um->um_lowervp); 408df8bae1dSRodney W. Grimes 4092a31267eSMatthew Dillon error = union_allocvp(vpp, mp, NULLVP, NULLVP, NULL, 4102a31267eSMatthew Dillon um->um_uppervp, um->um_lowervp, 1); 4112a31267eSMatthew Dillon UDEBUG(("error %d\n", error)); 4126bdfe06aSEivind Eklund UDEBUG(("union_root2 UPPERVP %p locked = %d\n", um->um_uppervp, 4136bdfe06aSEivind Eklund VOP_ISLOCKED(um->um_uppervp, NULL))); 414df8bae1dSRodney W. Grimes 415df8bae1dSRodney W. Grimes return (error); 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes 41880b301c3SPoul-Henning Kamp static int 419b40ce416SJulian Elischer union_statfs(mp, sbp, td) 420df8bae1dSRodney W. Grimes struct mount *mp; 421df8bae1dSRodney W. Grimes struct statfs *sbp; 422b40ce416SJulian Elischer struct thread *td; 423df8bae1dSRodney W. Grimes { 424df8bae1dSRodney W. Grimes int error; 425df8bae1dSRodney W. Grimes struct union_mount *um = MOUNTTOUNIONMOUNT(mp); 426df8bae1dSRodney W. Grimes struct statfs mstat; 427df8bae1dSRodney W. Grimes int lbsize; 428df8bae1dSRodney W. Grimes 4292a31267eSMatthew Dillon UDEBUG(("union_statfs(mp = %p, lvp = %p, uvp = %p)\n", 4302a31267eSMatthew Dillon (void *)mp, (void *)um->um_lowervp, (void *)um->um_uppervp)); 431df8bae1dSRodney W. Grimes 432df8bae1dSRodney W. Grimes bzero(&mstat, sizeof(mstat)); 433df8bae1dSRodney W. Grimes 434df8bae1dSRodney W. Grimes if (um->um_lowervp) { 435b40ce416SJulian Elischer error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, td); 436df8bae1dSRodney W. Grimes if (error) 437df8bae1dSRodney W. Grimes return (error); 438df8bae1dSRodney W. Grimes } 439df8bae1dSRodney W. Grimes 4401c4ccf09SDon Lewis /* 4411c4ccf09SDon Lewis * Now copy across the "interesting" information and fake the rest. 4421c4ccf09SDon Lewis */ 443df8bae1dSRodney W. Grimes #if 0 444df8bae1dSRodney W. Grimes sbp->f_type = mstat.f_type; 445df8bae1dSRodney W. Grimes sbp->f_flags = mstat.f_flags; 446df8bae1dSRodney W. Grimes sbp->f_bsize = mstat.f_bsize; 447df8bae1dSRodney W. Grimes sbp->f_iosize = mstat.f_iosize; 448df8bae1dSRodney W. Grimes #endif 449df8bae1dSRodney W. Grimes lbsize = mstat.f_bsize; 450df8bae1dSRodney W. Grimes sbp->f_blocks = mstat.f_blocks; 451df8bae1dSRodney W. Grimes sbp->f_bfree = mstat.f_bfree; 452df8bae1dSRodney W. Grimes sbp->f_bavail = mstat.f_bavail; 453df8bae1dSRodney W. Grimes sbp->f_files = mstat.f_files; 454df8bae1dSRodney W. Grimes sbp->f_ffree = mstat.f_ffree; 455df8bae1dSRodney W. Grimes 456b40ce416SJulian Elischer error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, td); 457df8bae1dSRodney W. Grimes if (error) 458df8bae1dSRodney W. Grimes return (error); 459df8bae1dSRodney W. Grimes 460df8bae1dSRodney W. Grimes sbp->f_flags = mstat.f_flags; 461df8bae1dSRodney W. Grimes sbp->f_bsize = mstat.f_bsize; 462df8bae1dSRodney W. Grimes sbp->f_iosize = mstat.f_iosize; 463df8bae1dSRodney W. Grimes 464df8bae1dSRodney W. Grimes /* 4651c4ccf09SDon Lewis * If the lower and upper blocksizes differ, then frig the 466df8bae1dSRodney W. Grimes * block counts so that the sizes reported by df make some 4671c4ccf09SDon Lewis * kind of sense. None of this makes sense though. 468df8bae1dSRodney W. Grimes */ 469df8bae1dSRodney W. Grimes 470996c772fSJohn Dyson if (mstat.f_bsize != lbsize) 471c9bf0111SKATO Takenori sbp->f_blocks = ((off_t) sbp->f_blocks * lbsize) / mstat.f_bsize; 472996c772fSJohn Dyson 473996c772fSJohn Dyson /* 474996c772fSJohn Dyson * The "total" fields count total resources in all layers, 475996c772fSJohn Dyson * the "free" fields count only those resources which are 476996c772fSJohn Dyson * free in the upper layer (since only the upper layer 477996c772fSJohn Dyson * is writeable). 478996c772fSJohn Dyson */ 479df8bae1dSRodney W. Grimes sbp->f_blocks += mstat.f_blocks; 480996c772fSJohn Dyson sbp->f_bfree = mstat.f_bfree; 481996c772fSJohn Dyson sbp->f_bavail = mstat.f_bavail; 482df8bae1dSRodney W. Grimes sbp->f_files += mstat.f_files; 483996c772fSJohn Dyson sbp->f_ffree = mstat.f_ffree; 484df8bae1dSRodney W. Grimes return (0); 485df8bae1dSRodney W. Grimes } 486df8bae1dSRodney W. Grimes 48780b301c3SPoul-Henning Kamp static struct vfsops union_vfsops = { 4887652131bSPoul-Henning Kamp .vfs_init = union_init, 4895e8c582aSPoul-Henning Kamp .vfs_mount = union_mount, 4907652131bSPoul-Henning Kamp .vfs_root = union_root, 4917652131bSPoul-Henning Kamp .vfs_statfs = union_statfs, 4927652131bSPoul-Henning Kamp .vfs_unmount = union_unmount, 493df8bae1dSRodney W. Grimes }; 494c901836cSGarrett Wollman 495c7b23e0fSRuslan Ermilov VFS_SET(union_vfsops, unionfs, VFCF_LOOPBACK); 496