165935Spendry /* 265935Spendry * Copyright (c) 1992, 1993, 1994 The Regents of the University of California. 365935Spendry * Copyright (c) 1992, 1993, 1994 Jan-Simon Pendry. 465935Spendry * All rights reserved. 565935Spendry * 665935Spendry * This code is derived from software contributed to Berkeley by 765963Spendry * Jan-Simon Pendry. 865935Spendry * 965935Spendry * %sccs.include.redist.c% 1065935Spendry * 11*67575Spendry * @(#)union_vnops.c 8.19 (Berkeley) 07/28/94 1265935Spendry */ 1365935Spendry 1465935Spendry #include <sys/param.h> 1565935Spendry #include <sys/systm.h> 1665935Spendry #include <sys/proc.h> 1765935Spendry #include <sys/file.h> 1865935Spendry #include <sys/time.h> 19*67575Spendry #include <sys/stat.h> 2065935Spendry #include <sys/types.h> 2165935Spendry #include <sys/vnode.h> 2265935Spendry #include <sys/mount.h> 2365935Spendry #include <sys/namei.h> 2465935Spendry #include <sys/malloc.h> 2565935Spendry #include <sys/buf.h> 2666053Spendry #include <sys/queue.h> 2766055Spendry #include <miscfs/union/union.h> 2865935Spendry 2966152Spendry #define FIXUP(un) { \ 3066152Spendry if (((un)->un_flags & UN_ULOCK) == 0) { \ 3166152Spendry union_fixup(un); \ 3266152Spendry } \ 3366152Spendry } 3466152Spendry 3566152Spendry static void 3666152Spendry union_fixup(un) 3766152Spendry struct union_node *un; 3866152Spendry { 3966152Spendry 4066152Spendry VOP_LOCK(un->un_uppervp); 4166152Spendry un->un_flags |= UN_ULOCK; 4266152Spendry } 4366152Spendry 4465935Spendry static int 4567064Spendry union_lookup1(udvp, dvpp, vpp, cnp) 4665989Spendry struct vnode *udvp; 4767064Spendry struct vnode **dvpp; 4865935Spendry struct vnode **vpp; 4965935Spendry struct componentname *cnp; 5065935Spendry { 5165935Spendry int error; 5265935Spendry struct vnode *tdvp; 5367064Spendry struct vnode *dvp; 5465935Spendry struct mount *mp; 5565935Spendry 5667064Spendry dvp = *dvpp; 5767064Spendry 5865994Spendry /* 5965994Spendry * If stepping up the directory tree, check for going 6065994Spendry * back across the mount point, in which case do what 6165994Spendry * lookup would do by stepping back down the mount 6265994Spendry * hierarchy. 6365994Spendry */ 6465935Spendry if (cnp->cn_flags & ISDOTDOT) { 6567064Spendry while ((dvp != udvp) && (dvp->v_flag & VROOT)) { 6666034Spendry /* 6766034Spendry * Don't do the NOCROSSMOUNT check 6866034Spendry * at this level. By definition, 6966034Spendry * union fs deals with namespaces, not 7066034Spendry * filesystems. 7166034Spendry */ 7265935Spendry tdvp = dvp; 7367064Spendry *dvpp = dvp = dvp->v_mount->mnt_vnodecovered; 7465935Spendry vput(tdvp); 7565935Spendry VREF(dvp); 7665935Spendry VOP_LOCK(dvp); 7765935Spendry } 7865935Spendry } 7966027Spendry 8065935Spendry error = VOP_LOOKUP(dvp, &tdvp, cnp); 8165935Spendry if (error) 8265935Spendry return (error); 8365935Spendry 8465994Spendry /* 8566027Spendry * The parent directory will have been unlocked, unless lookup 8666027Spendry * found the last component. In which case, re-lock the node 8766027Spendry * here to allow it to be unlocked again (phew) in union_lookup. 8865994Spendry */ 8966027Spendry if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN)) 9065994Spendry VOP_LOCK(dvp); 9165994Spendry 9265935Spendry dvp = tdvp; 9365994Spendry 9465994Spendry /* 9565994Spendry * Lastly check if the current node is a mount point in 9666034Spendry * which case walk up the mount hierarchy making sure not to 9765994Spendry * bump into the root of the mount tree (ie. dvp != udvp). 9865994Spendry */ 9965989Spendry while (dvp != udvp && (dvp->v_type == VDIR) && 10066034Spendry (mp = dvp->v_mountedhere)) { 10165935Spendry 10265935Spendry if (mp->mnt_flag & MNT_MLOCK) { 10365935Spendry mp->mnt_flag |= MNT_MWAIT; 10465935Spendry sleep((caddr_t) mp, PVFS); 10565935Spendry continue; 10665935Spendry } 10765935Spendry 10865935Spendry if (error = VFS_ROOT(mp, &tdvp)) { 10965935Spendry vput(dvp); 11065935Spendry return (error); 11165935Spendry } 11265935Spendry 11365965Spendry vput(dvp); 11465935Spendry dvp = tdvp; 11565935Spendry } 11665935Spendry 11765935Spendry *vpp = dvp; 11865935Spendry return (0); 11965935Spendry } 12065935Spendry 12165935Spendry int 12265935Spendry union_lookup(ap) 12365935Spendry struct vop_lookup_args /* { 12465935Spendry struct vnodeop_desc *a_desc; 12565935Spendry struct vnode *a_dvp; 12665935Spendry struct vnode **a_vpp; 12765935Spendry struct componentname *a_cnp; 12865935Spendry } */ *ap; 12965935Spendry { 13065965Spendry int error; 13165935Spendry int uerror, lerror; 13265935Spendry struct vnode *uppervp, *lowervp; 13365935Spendry struct vnode *upperdvp, *lowerdvp; 13465935Spendry struct vnode *dvp = ap->a_dvp; 13565989Spendry struct union_node *dun = VTOUNION(dvp); 13665935Spendry struct componentname *cnp = ap->a_cnp; 13765935Spendry int lockparent = cnp->cn_flags & LOCKPARENT; 13865994Spendry int rdonly = cnp->cn_flags & RDONLY; 13965997Spendry struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); 14066152Spendry struct ucred *saved_cred; 141*67575Spendry int iswhiteout; 142*67575Spendry struct vattr va; 14365935Spendry 14467446Spendry #ifdef notyet 14567446Spendry if (cnp->cn_namelen == 3 && 14667446Spendry cnp->cn_nameptr[2] == '.' && 14767446Spendry cnp->cn_nameptr[1] == '.' && 14867446Spendry cnp->cn_nameptr[0] == '.') { 14967446Spendry dvp = *ap->a_vpp = LOWERVP(ap->a_dvp); 15067446Spendry if (dvp == NULLVP) 15167446Spendry return (ENOENT); 15267446Spendry VREF(dvp); 15367446Spendry VOP_LOCK(dvp); 15467446Spendry if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 15567446Spendry VOP_UNLOCK(ap->a_dvp); 15667446Spendry return (0); 15767446Spendry } 15867446Spendry #endif 15967446Spendry 16065965Spendry cnp->cn_flags |= LOCKPARENT; 16165965Spendry 16265935Spendry upperdvp = dun->un_uppervp; 16365935Spendry lowerdvp = dun->un_lowervp; 16465997Spendry uppervp = NULLVP; 16565997Spendry lowervp = NULLVP; 166*67575Spendry iswhiteout = 0; 16765935Spendry 16865935Spendry /* 16965935Spendry * do the lookup in the upper level. 17065935Spendry * if that level comsumes additional pathnames, 17165935Spendry * then assume that something special is going 17265935Spendry * on and just return that vnode. 17365935Spendry */ 17467076Spendry if (upperdvp != NULLVP) { 17566152Spendry FIXUP(dun); 17667064Spendry uerror = union_lookup1(um->um_uppervp, &upperdvp, 17765997Spendry &uppervp, cnp); 17866051Spendry /*if (uppervp == upperdvp) 17966051Spendry dun->un_flags |= UN_KLOCK;*/ 18065965Spendry 18165935Spendry if (cnp->cn_consume != 0) { 18265935Spendry *ap->a_vpp = uppervp; 18365965Spendry if (!lockparent) 18465965Spendry cnp->cn_flags &= ~LOCKPARENT; 18565935Spendry return (uerror); 18665935Spendry } 187*67575Spendry if (uerror == ENOENT || uerror == EJUSTRETURN) { 188*67575Spendry if (cnp->cn_flags & ISWHITEOUT) { 189*67575Spendry iswhiteout = 1; 190*67575Spendry } else if (lowerdvp != NULLVP) { 191*67575Spendry lerror = VOP_GETATTR(upperdvp, &va, 192*67575Spendry cnp->cn_cred, cnp->cn_proc); 193*67575Spendry if (lerror == 0 && (va.va_flags & OPAQUE)) 194*67575Spendry iswhiteout = 1; 195*67575Spendry } 196*67575Spendry } 19765935Spendry } else { 19865935Spendry uerror = ENOENT; 19965935Spendry } 20065935Spendry 20165935Spendry /* 20265935Spendry * in a similar way to the upper layer, do the lookup 20365935Spendry * in the lower layer. this time, if there is some 20465935Spendry * component magic going on, then vput whatever we got 20565935Spendry * back from the upper layer and return the lower vnode 20665935Spendry * instead. 20765935Spendry */ 208*67575Spendry if (lowerdvp != NULLVP && !iswhiteout) { 20966051Spendry int nameiop; 21066051Spendry 21165965Spendry VOP_LOCK(lowerdvp); 21266051Spendry 21366051Spendry /* 21466051Spendry * Only do a LOOKUP on the bottom node, since 21566051Spendry * we won't be making changes to it anyway. 21666051Spendry */ 21766051Spendry nameiop = cnp->cn_nameiop; 21866051Spendry cnp->cn_nameiop = LOOKUP; 21966152Spendry if (um->um_op == UNMNT_BELOW) { 22066152Spendry saved_cred = cnp->cn_cred; 22166152Spendry cnp->cn_cred = um->um_cred; 22266152Spendry } 22367064Spendry lerror = union_lookup1(um->um_lowervp, &lowerdvp, 22466051Spendry &lowervp, cnp); 22566152Spendry if (um->um_op == UNMNT_BELOW) 22666152Spendry cnp->cn_cred = saved_cred; 22766051Spendry cnp->cn_nameiop = nameiop; 22866051Spendry 22965989Spendry if (lowervp != lowerdvp) 23065989Spendry VOP_UNLOCK(lowerdvp); 23165965Spendry 23265935Spendry if (cnp->cn_consume != 0) { 23367076Spendry if (uppervp != NULLVP) { 23466051Spendry if (uppervp == upperdvp) 23566051Spendry vrele(uppervp); 23666051Spendry else 23766051Spendry vput(uppervp); 23865997Spendry uppervp = NULLVP; 23965935Spendry } 24065935Spendry *ap->a_vpp = lowervp; 24165965Spendry if (!lockparent) 24265965Spendry cnp->cn_flags &= ~LOCKPARENT; 24365935Spendry return (lerror); 24465935Spendry } 24565935Spendry } else { 24665935Spendry lerror = ENOENT; 24767416Spendry if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) { 24867416Spendry lowervp = LOWERVP(dun->un_pvp); 24967416Spendry if (lowervp != NULLVP) { 25067416Spendry VREF(lowervp); 25167416Spendry VOP_LOCK(lowervp); 25267416Spendry lerror = 0; 25367416Spendry } 25467416Spendry } 25565935Spendry } 25665935Spendry 25765965Spendry if (!lockparent) 25865965Spendry cnp->cn_flags &= ~LOCKPARENT; 25965965Spendry 26065935Spendry /* 26165935Spendry * at this point, we have uerror and lerror indicating 26265935Spendry * possible errors with the lookups in the upper and lower 26365935Spendry * layers. additionally, uppervp and lowervp are (locked) 26465935Spendry * references to existing vnodes in the upper and lower layers. 26565935Spendry * 26665935Spendry * there are now three cases to consider. 26765935Spendry * 1. if both layers returned an error, then return whatever 26865935Spendry * error the upper layer generated. 26965935Spendry * 27065935Spendry * 2. if the top layer failed and the bottom layer succeeded 27165935Spendry * then two subcases occur. 27265935Spendry * a. the bottom vnode is not a directory, in which 27365935Spendry * case just return a new union vnode referencing 27465935Spendry * an empty top layer and the existing bottom layer. 27565935Spendry * b. the bottom vnode is a directory, in which case 27665935Spendry * create a new directory in the top-level and 27765935Spendry * continue as in case 3. 27865935Spendry * 27965935Spendry * 3. if the top layer succeeded then return a new union 28065935Spendry * vnode referencing whatever the new top layer and 28165935Spendry * whatever the bottom layer returned. 28265935Spendry */ 28365935Spendry 28466027Spendry *ap->a_vpp = NULLVP; 28566027Spendry 28665935Spendry /* case 1. */ 28765935Spendry if ((uerror != 0) && (lerror != 0)) { 28865935Spendry return (uerror); 28965935Spendry } 29065935Spendry 29165935Spendry /* case 2. */ 29265935Spendry if (uerror != 0 /* && (lerror == 0) */ ) { 29365935Spendry if (lowervp->v_type == VDIR) { /* case 2b. */ 29466051Spendry dun->un_flags &= ~UN_ULOCK; 29566051Spendry VOP_UNLOCK(upperdvp); 29665997Spendry uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 29766051Spendry VOP_LOCK(upperdvp); 29866051Spendry dun->un_flags |= UN_ULOCK; 29966051Spendry 30065935Spendry if (uerror) { 30167076Spendry if (lowervp != NULLVP) { 30265935Spendry vput(lowervp); 30365997Spendry lowervp = NULLVP; 30465935Spendry } 30565935Spendry return (uerror); 30665935Spendry } 30765935Spendry } 30865935Spendry } 30965935Spendry 31067076Spendry if (lowervp != NULLVP) 31165965Spendry VOP_UNLOCK(lowervp); 31265965Spendry 31365997Spendry error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 31465997Spendry uppervp, lowervp); 31565997Spendry 31665965Spendry if (error) { 31767076Spendry if (uppervp != NULLVP) 31866051Spendry vput(uppervp); 31967076Spendry if (lowervp != NULLVP) 32065965Spendry vrele(lowervp); 32165965Spendry } else { 32265994Spendry if (*ap->a_vpp != dvp) 32365994Spendry if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 32465994Spendry VOP_UNLOCK(dvp); 32565965Spendry } 32665965Spendry 32765965Spendry return (error); 32865935Spendry } 32965935Spendry 33065963Spendry int 33165963Spendry union_create(ap) 33265963Spendry struct vop_create_args /* { 33365963Spendry struct vnode *a_dvp; 33465963Spendry struct vnode **a_vpp; 33565963Spendry struct componentname *a_cnp; 33665963Spendry struct vattr *a_vap; 33765963Spendry } */ *ap; 33865963Spendry { 33965963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 34065963Spendry struct vnode *dvp = un->un_uppervp; 34165963Spendry 34267076Spendry if (dvp != NULLVP) { 34365963Spendry int error; 34465963Spendry struct vnode *vp; 34565963Spendry 34666152Spendry FIXUP(un); 34766152Spendry 34865963Spendry VREF(dvp); 34966051Spendry un->un_flags |= UN_KLOCK; 35065963Spendry vput(ap->a_dvp); 35165963Spendry error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap); 35265963Spendry if (error) 35365963Spendry return (error); 35465963Spendry 35565963Spendry error = union_allocvp( 35665963Spendry ap->a_vpp, 35765989Spendry ap->a_dvp->v_mount, 35865989Spendry ap->a_dvp, 35965965Spendry NULLVP, 36065963Spendry ap->a_cnp, 36165963Spendry vp, 36265963Spendry NULLVP); 36365965Spendry if (error) 36466051Spendry vput(vp); 36565963Spendry return (error); 36665963Spendry } 36765963Spendry 36865963Spendry vput(ap->a_dvp); 36965963Spendry return (EROFS); 37065963Spendry } 37165963Spendry 37265963Spendry int 373*67575Spendry union_whiteout(ap) 374*67575Spendry struct vop_whiteout_args /* { 375*67575Spendry struct vnode *a_dvp; 376*67575Spendry struct componentname *a_cnp; 377*67575Spendry int a_flags; 378*67575Spendry } */ *ap; 379*67575Spendry { 380*67575Spendry struct union_node *un = VTOUNION(ap->a_dvp); 381*67575Spendry 382*67575Spendry if (un->un_uppervp == NULLVP) 383*67575Spendry return (EOPNOTSUPP); 384*67575Spendry 385*67575Spendry FIXUP(un); 386*67575Spendry return (VOP_WHITEOUT(un->un_uppervp, ap->a_cnp, ap->a_flags)); 387*67575Spendry } 388*67575Spendry 389*67575Spendry int 39065963Spendry union_mknod(ap) 39165963Spendry struct vop_mknod_args /* { 39265963Spendry struct vnode *a_dvp; 39365963Spendry struct vnode **a_vpp; 39465963Spendry struct componentname *a_cnp; 39565963Spendry struct vattr *a_vap; 39665963Spendry } */ *ap; 39765963Spendry { 39865963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 39965963Spendry struct vnode *dvp = un->un_uppervp; 40065963Spendry 40167076Spendry if (dvp != NULLVP) { 40265963Spendry int error; 40365963Spendry struct vnode *vp; 40465963Spendry 40566152Spendry FIXUP(un); 40666152Spendry 40765963Spendry VREF(dvp); 40866051Spendry un->un_flags |= UN_KLOCK; 40965963Spendry vput(ap->a_dvp); 41065963Spendry error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap); 41165963Spendry if (error) 41265963Spendry return (error); 41365963Spendry 41467076Spendry if (vp != NULLVP) { 41565965Spendry error = union_allocvp( 41665965Spendry ap->a_vpp, 41765989Spendry ap->a_dvp->v_mount, 41865989Spendry ap->a_dvp, 41965965Spendry NULLVP, 42065965Spendry ap->a_cnp, 42165965Spendry vp, 42265965Spendry NULLVP); 42365965Spendry if (error) 42466051Spendry vput(vp); 42565965Spendry } 42665963Spendry return (error); 42765963Spendry } 42865963Spendry 42965963Spendry vput(ap->a_dvp); 43065963Spendry return (EROFS); 43165963Spendry } 43265963Spendry 43365935Spendry int 43465935Spendry union_open(ap) 43565935Spendry struct vop_open_args /* { 43665935Spendry struct vnodeop_desc *a_desc; 43765935Spendry struct vnode *a_vp; 43865935Spendry int a_mode; 43965935Spendry struct ucred *a_cred; 44065935Spendry struct proc *a_p; 44165935Spendry } */ *ap; 44265935Spendry { 44365935Spendry struct union_node *un = VTOUNION(ap->a_vp); 44465965Spendry struct vnode *tvp; 44565935Spendry int mode = ap->a_mode; 44665935Spendry struct ucred *cred = ap->a_cred; 44765935Spendry struct proc *p = ap->a_p; 44865965Spendry int error; 44965935Spendry 45065935Spendry /* 45165935Spendry * If there is an existing upper vp then simply open that. 45265935Spendry */ 45365965Spendry tvp = un->un_uppervp; 45465965Spendry if (tvp == NULLVP) { 45565935Spendry /* 45665965Spendry * If the lower vnode is being opened for writing, then 45765965Spendry * copy the file contents to the upper vnode and open that, 45865965Spendry * otherwise can simply open the lower vnode. 45965935Spendry */ 46065965Spendry tvp = un->un_lowervp; 46165965Spendry if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 46267169Spendry error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p); 46365965Spendry if (error == 0) 46465965Spendry error = VOP_OPEN(un->un_uppervp, mode, cred, p); 46565965Spendry return (error); 46665935Spendry } 46766051Spendry 46866051Spendry /* 46966051Spendry * Just open the lower vnode 47066051Spendry */ 47166027Spendry un->un_openl++; 47266051Spendry VOP_LOCK(tvp); 47366051Spendry error = VOP_OPEN(tvp, mode, cred, p); 47466051Spendry VOP_UNLOCK(tvp); 47566051Spendry 47666051Spendry return (error); 47765935Spendry } 47865935Spendry 47966152Spendry FIXUP(un); 48066152Spendry 48165965Spendry error = VOP_OPEN(tvp, mode, cred, p); 48265965Spendry 48365965Spendry return (error); 48465935Spendry } 48565935Spendry 48665963Spendry int 48765963Spendry union_close(ap) 48865963Spendry struct vop_close_args /* { 48965963Spendry struct vnode *a_vp; 49065963Spendry int a_fflag; 49165963Spendry struct ucred *a_cred; 49265963Spendry struct proc *a_p; 49365963Spendry } */ *ap; 49465963Spendry { 49565997Spendry struct union_node *un = VTOUNION(ap->a_vp); 49665997Spendry struct vnode *vp; 49765963Spendry 49867076Spendry if (un->un_uppervp != NULLVP) { 49965997Spendry vp = un->un_uppervp; 50065997Spendry } else { 50166027Spendry #ifdef UNION_DIAGNOSTIC 50266027Spendry if (un->un_openl <= 0) 50366027Spendry panic("union: un_openl cnt"); 50465997Spendry #endif 50566027Spendry --un->un_openl; 50665997Spendry vp = un->un_lowervp; 50765997Spendry } 50866027Spendry 50965997Spendry return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p)); 51065963Spendry } 51165963Spendry 51265935Spendry /* 51365963Spendry * Check access permission on the union vnode. 51465963Spendry * The access check being enforced is to check 51565963Spendry * against both the underlying vnode, and any 51665963Spendry * copied vnode. This ensures that no additional 51765963Spendry * file permissions are given away simply because 51865963Spendry * the user caused an implicit file copy. 51965963Spendry */ 52065963Spendry int 52165963Spendry union_access(ap) 52265963Spendry struct vop_access_args /* { 52365963Spendry struct vnodeop_desc *a_desc; 52465963Spendry struct vnode *a_vp; 52565963Spendry int a_mode; 52665963Spendry struct ucred *a_cred; 52765963Spendry struct proc *a_p; 52865963Spendry } */ *ap; 52965963Spendry { 53065963Spendry struct union_node *un = VTOUNION(ap->a_vp); 53166149Spendry int error = EACCES; 53265963Spendry struct vnode *vp; 53365963Spendry 53467076Spendry if ((vp = un->un_uppervp) != NULLVP) { 53566152Spendry FIXUP(un); 53666152Spendry return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); 53766152Spendry } 53866152Spendry 53967076Spendry if ((vp = un->un_lowervp) != NULLVP) { 54065965Spendry VOP_LOCK(vp); 54165963Spendry error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p); 54266152Spendry if (error == 0) { 54366152Spendry struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount); 54466152Spendry 54566152Spendry if (um->um_op == UNMNT_BELOW) 54666152Spendry error = VOP_ACCESS(vp, ap->a_mode, 54766152Spendry um->um_cred, ap->a_p); 54866152Spendry } 54965965Spendry VOP_UNLOCK(vp); 55065963Spendry if (error) 55165963Spendry return (error); 55265963Spendry } 55365963Spendry 55465965Spendry return (error); 55565963Spendry } 55665963Spendry 55765963Spendry /* 55867109Spendry * We handle getattr only to change the fsid and 55967109Spendry * track object sizes 56065935Spendry */ 56165935Spendry int 56265935Spendry union_getattr(ap) 56365935Spendry struct vop_getattr_args /* { 56465935Spendry struct vnode *a_vp; 56565935Spendry struct vattr *a_vap; 56665935Spendry struct ucred *a_cred; 56765935Spendry struct proc *a_p; 56865935Spendry } */ *ap; 56965935Spendry { 57065935Spendry int error; 57166062Spendry struct union_node *un = VTOUNION(ap->a_vp); 57266062Spendry struct vnode *vp = un->un_uppervp; 57366062Spendry struct vattr *vap; 57466062Spendry struct vattr va; 57565935Spendry 57666062Spendry 57766062Spendry /* 57866062Spendry * Some programs walk the filesystem hierarchy by counting 57966062Spendry * links to directories to avoid stat'ing all the time. 58066062Spendry * This means the link count on directories needs to be "correct". 58166062Spendry * The only way to do that is to call getattr on both layers 58266062Spendry * and fix up the link count. The link count will not necessarily 58366062Spendry * be accurate but will be large enough to defeat the tree walkers. 58466062Spendry */ 58566062Spendry 58666062Spendry vap = ap->a_vap; 58766062Spendry 58866062Spendry vp = un->un_uppervp; 58966062Spendry if (vp != NULLVP) { 59067073Spendry /* 59167073Spendry * It's not clear whether VOP_GETATTR is to be 59267073Spendry * called with the vnode locked or not. stat() calls 59367073Spendry * it with (vp) locked, and fstat calls it with 59467073Spendry * (vp) unlocked. 59567073Spendry * In the mean time, compensate here by checking 59667073Spendry * the union_node's lock flag. 59767073Spendry */ 59867073Spendry if (un->un_flags & UN_LOCKED) 59967073Spendry FIXUP(un); 60067073Spendry 60166062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 60266062Spendry if (error) 60366062Spendry return (error); 60467109Spendry union_newsize(ap->a_vp, vap->va_size, VNOVAL); 60566062Spendry } 60666062Spendry 60766062Spendry if (vp == NULLVP) { 60866062Spendry vp = un->un_lowervp; 60966062Spendry } else if (vp->v_type == VDIR) { 61066062Spendry vp = un->un_lowervp; 61166062Spendry vap = &va; 61266062Spendry } else { 61366062Spendry vp = NULLVP; 61466062Spendry } 61566062Spendry 61666062Spendry if (vp != NULLVP) { 61766051Spendry VOP_LOCK(vp); 61866062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 61966051Spendry VOP_UNLOCK(vp); 62066062Spendry if (error) 62166062Spendry return (error); 62267109Spendry union_newsize(ap->a_vp, VNOVAL, vap->va_size); 62366062Spendry } 62465965Spendry 62566062Spendry if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 62666062Spendry ap->a_vap->va_nlink += vap->va_nlink; 62766062Spendry 62867400Spendry ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 62965935Spendry return (0); 63065935Spendry } 63165935Spendry 63265963Spendry int 63365965Spendry union_setattr(ap) 63465963Spendry struct vop_setattr_args /* { 63565963Spendry struct vnode *a_vp; 63665963Spendry struct vattr *a_vap; 63765963Spendry struct ucred *a_cred; 63865963Spendry struct proc *a_p; 63965963Spendry } */ *ap; 64065963Spendry { 64165963Spendry struct union_node *un = VTOUNION(ap->a_vp); 64265963Spendry int error; 64365963Spendry 64466057Spendry /* 64566057Spendry * Handle case of truncating lower object to zero size, 64666057Spendry * by creating a zero length upper object. This is to 64766057Spendry * handle the case of open with O_TRUNC and O_CREAT. 64866057Spendry */ 64966057Spendry if ((un->un_uppervp == NULLVP) && 65066057Spendry /* assert(un->un_lowervp != NULLVP) */ 651*67575Spendry (un->un_lowervp->v_type == VREG)) { 652*67575Spendry error = union_copyup(un, (ap->a_vap->va_size != 0), 653*67575Spendry ap->a_cred, ap->a_p); 65466057Spendry if (error) 65566057Spendry return (error); 65666057Spendry } 65766057Spendry 65866057Spendry /* 65966057Spendry * Try to set attributes in upper layer, 66066057Spendry * otherwise return read-only filesystem error. 66166057Spendry */ 66266057Spendry if (un->un_uppervp != NULLVP) { 66366152Spendry FIXUP(un); 66465963Spendry error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 66565963Spendry ap->a_cred, ap->a_p); 66667109Spendry if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 66767109Spendry union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 66865963Spendry } else { 66965963Spendry error = EROFS; 67065963Spendry } 67165963Spendry 67265963Spendry return (error); 67365963Spendry } 67465963Spendry 67565963Spendry int 67665963Spendry union_read(ap) 67765963Spendry struct vop_read_args /* { 67865963Spendry struct vnode *a_vp; 67965963Spendry struct uio *a_uio; 68065963Spendry int a_ioflag; 68165963Spendry struct ucred *a_cred; 68265963Spendry } */ *ap; 68365963Spendry { 68465963Spendry int error; 68565963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 68666051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 68765963Spendry 68866051Spendry if (dolock) 68966051Spendry VOP_LOCK(vp); 69066152Spendry else 69166152Spendry FIXUP(VTOUNION(ap->a_vp)); 69265963Spendry error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 69366051Spendry if (dolock) 69466051Spendry VOP_UNLOCK(vp); 69565963Spendry 69667109Spendry /* 69767109Spendry * XXX 69867109Spendry * perhaps the size of the underlying object has changed under 69967109Spendry * our feet. take advantage of the offset information present 70067109Spendry * in the uio structure. 70167109Spendry */ 70267109Spendry if (error == 0) { 70367109Spendry struct union_node *un = VTOUNION(ap->a_vp); 70467109Spendry off_t cur = ap->a_uio->uio_offset; 70567109Spendry 70667109Spendry if (vp == un->un_uppervp) { 70767109Spendry if (cur > un->un_uppersz) 70867109Spendry union_newsize(ap->a_vp, cur, VNOVAL); 70967109Spendry } else { 71067109Spendry if (cur > un->un_lowersz) 71167109Spendry union_newsize(ap->a_vp, VNOVAL, cur); 71267109Spendry } 71367109Spendry } 71467109Spendry 71565963Spendry return (error); 71665963Spendry } 71765963Spendry 71865963Spendry int 71965963Spendry union_write(ap) 72065963Spendry struct vop_read_args /* { 72165963Spendry struct vnode *a_vp; 72265963Spendry struct uio *a_uio; 72365963Spendry int a_ioflag; 72465963Spendry struct ucred *a_cred; 72565963Spendry } */ *ap; 72665963Spendry { 72765963Spendry int error; 728*67575Spendry struct vnode *vp; 729*67575Spendry struct union_node *un = VTOUNION(ap->a_vp); 73065963Spendry 731*67575Spendry vp = UPPERVP(ap->a_vp); 732*67575Spendry if (vp == NULLVP) 733*67575Spendry panic("union: missing upper layer in write"); 734*67575Spendry 735*67575Spendry FIXUP(un); 73665963Spendry error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 73765963Spendry 73867109Spendry /* 73967109Spendry * the size of the underlying object may be changed by the 74067109Spendry * write. 74167109Spendry */ 74267109Spendry if (error == 0) { 74367109Spendry off_t cur = ap->a_uio->uio_offset; 74467109Spendry 745*67575Spendry if (cur > un->un_uppersz) 746*67575Spendry union_newsize(ap->a_vp, cur, VNOVAL); 74767109Spendry } 74867109Spendry 74965963Spendry return (error); 75065963Spendry } 75165963Spendry 75265963Spendry int 75365963Spendry union_ioctl(ap) 75465963Spendry struct vop_ioctl_args /* { 75565963Spendry struct vnode *a_vp; 75665963Spendry int a_command; 75765963Spendry caddr_t a_data; 75865963Spendry int a_fflag; 75965963Spendry struct ucred *a_cred; 76065963Spendry struct proc *a_p; 76165963Spendry } */ *ap; 76265963Spendry { 76365963Spendry 76465963Spendry return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 76565963Spendry ap->a_fflag, ap->a_cred, ap->a_p)); 76665963Spendry } 76765963Spendry 76865963Spendry int 76965963Spendry union_select(ap) 77065963Spendry struct vop_select_args /* { 77165963Spendry struct vnode *a_vp; 77265963Spendry int a_which; 77365963Spendry int a_fflags; 77465963Spendry struct ucred *a_cred; 77565963Spendry struct proc *a_p; 77665963Spendry } */ *ap; 77765963Spendry { 77865963Spendry 77965963Spendry return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 78065963Spendry ap->a_cred, ap->a_p)); 78165963Spendry } 78265963Spendry 78365963Spendry int 78465963Spendry union_mmap(ap) 78565963Spendry struct vop_mmap_args /* { 78665963Spendry struct vnode *a_vp; 78765963Spendry int a_fflags; 78865963Spendry struct ucred *a_cred; 78965963Spendry struct proc *a_p; 79065963Spendry } */ *ap; 79165963Spendry { 79265963Spendry 79365963Spendry return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 79465963Spendry ap->a_cred, ap->a_p)); 79565963Spendry } 79665963Spendry 79765963Spendry int 79865963Spendry union_fsync(ap) 79965963Spendry struct vop_fsync_args /* { 80065963Spendry struct vnode *a_vp; 80165963Spendry struct ucred *a_cred; 80265963Spendry int a_waitfor; 80365963Spendry struct proc *a_p; 80465963Spendry } */ *ap; 80565963Spendry { 80665963Spendry int error = 0; 80765963Spendry struct vnode *targetvp = OTHERVP(ap->a_vp); 80865963Spendry 80967076Spendry if (targetvp != NULLVP) { 81066051Spendry int dolock = (targetvp == LOWERVP(ap->a_vp)); 81166051Spendry 81266051Spendry if (dolock) 81366051Spendry VOP_LOCK(targetvp); 81466152Spendry else 81566152Spendry FIXUP(VTOUNION(ap->a_vp)); 81665963Spendry error = VOP_FSYNC(targetvp, ap->a_cred, 81765963Spendry ap->a_waitfor, ap->a_p); 81866051Spendry if (dolock) 81966051Spendry VOP_UNLOCK(targetvp); 82065963Spendry } 82165963Spendry 82265963Spendry return (error); 82365963Spendry } 82465963Spendry 82565963Spendry int 82665963Spendry union_seek(ap) 82765963Spendry struct vop_seek_args /* { 82865963Spendry struct vnode *a_vp; 82965963Spendry off_t a_oldoff; 83065963Spendry off_t a_newoff; 83165963Spendry struct ucred *a_cred; 83265963Spendry } */ *ap; 83365963Spendry { 83465963Spendry 83565963Spendry return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 83665963Spendry } 83765963Spendry 83865963Spendry int 83965963Spendry union_remove(ap) 84065963Spendry struct vop_remove_args /* { 84165963Spendry struct vnode *a_dvp; 84265963Spendry struct vnode *a_vp; 84365963Spendry struct componentname *a_cnp; 84465963Spendry } */ *ap; 84565963Spendry { 84665963Spendry int error; 84765963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 84865963Spendry struct union_node *un = VTOUNION(ap->a_vp); 84965963Spendry 850*67575Spendry if (dun->un_uppervp == NULLVP) 851*67575Spendry panic("union remove: null upper vnode"); 852*67575Spendry 853*67575Spendry if (un->un_uppervp != NULLVP) { 85465963Spendry struct vnode *dvp = dun->un_uppervp; 85565963Spendry struct vnode *vp = un->un_uppervp; 85665963Spendry 85766152Spendry FIXUP(dun); 85865963Spendry VREF(dvp); 85966051Spendry dun->un_flags |= UN_KLOCK; 86065963Spendry vput(ap->a_dvp); 86166152Spendry FIXUP(un); 86265963Spendry VREF(vp); 86366051Spendry un->un_flags |= UN_KLOCK; 86465963Spendry vput(ap->a_vp); 86565963Spendry 866*67575Spendry if (un->un_lowervp != NULLVP) 867*67575Spendry ap->a_cnp->cn_flags |= DOWHITEOUT; 86865963Spendry error = VOP_REMOVE(dvp, vp, ap->a_cnp); 86966027Spendry if (!error) 87066027Spendry union_removed_upper(un); 87165963Spendry } else { 872*67575Spendry FIXUP(dun); 873*67575Spendry error = union_mkwhiteout( 874*67575Spendry MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 875*67575Spendry dun->un_uppervp, ap->a_cnp, un->un_path); 87665963Spendry vput(ap->a_dvp); 87765963Spendry vput(ap->a_vp); 87865963Spendry } 87965963Spendry 88065963Spendry return (error); 88165963Spendry } 88265963Spendry 88365963Spendry int 88465963Spendry union_link(ap) 88565963Spendry struct vop_link_args /* { 88665963Spendry struct vnode *a_vp; 88765963Spendry struct vnode *a_tdvp; 88865963Spendry struct componentname *a_cnp; 88965963Spendry } */ *ap; 89065963Spendry { 89167169Spendry int error = 0; 89267169Spendry struct union_node *un; 89367169Spendry struct vnode *vp; 89467169Spendry struct vnode *tdvp; 89565963Spendry 89667169Spendry un = VTOUNION(ap->a_vp); 89765963Spendry 89867169Spendry if (ap->a_vp->v_op != ap->a_tdvp->v_op) { 89967169Spendry tdvp = ap->a_tdvp; 90067169Spendry } else { 90167169Spendry struct union_node *tdun = VTOUNION(ap->a_tdvp); 90267169Spendry if (tdun->un_uppervp == NULLVP) { 90367169Spendry VOP_LOCK(ap->a_tdvp); 90467169Spendry if (un->un_uppervp == tdun->un_dirvp) { 90567169Spendry un->un_flags &= ~UN_ULOCK; 90667169Spendry VOP_UNLOCK(un->un_uppervp); 90767169Spendry } 90867169Spendry error = union_copyup(tdun, 1, ap->a_cnp->cn_cred, 90967169Spendry ap->a_cnp->cn_proc); 91067169Spendry if (un->un_uppervp == tdun->un_dirvp) { 91167169Spendry VOP_LOCK(un->un_uppervp); 91267169Spendry un->un_flags |= UN_ULOCK; 91367169Spendry } 91467169Spendry VOP_UNLOCK(ap->a_tdvp); 91567169Spendry } 91667169Spendry tdvp = tdun->un_uppervp; 91767169Spendry } 91865963Spendry 91967169Spendry vp = un->un_uppervp; 92067169Spendry if (vp == NULLVP) 92167169Spendry error = EROFS; 92267169Spendry 92367169Spendry if (error) { 92465963Spendry vput(ap->a_vp); 92567169Spendry return (error); 92665963Spendry } 92765963Spendry 92867169Spendry FIXUP(un); 92967169Spendry VREF(vp); 93067169Spendry un->un_flags |= UN_KLOCK; 93167169Spendry vput(ap->a_vp); 93267169Spendry 93367169Spendry return (VOP_LINK(vp, tdvp, ap->a_cnp)); 93465963Spendry } 93565963Spendry 93665963Spendry int 93765963Spendry union_rename(ap) 93865963Spendry struct vop_rename_args /* { 93965963Spendry struct vnode *a_fdvp; 94065963Spendry struct vnode *a_fvp; 94165963Spendry struct componentname *a_fcnp; 94265963Spendry struct vnode *a_tdvp; 94365963Spendry struct vnode *a_tvp; 94465963Spendry struct componentname *a_tcnp; 94565963Spendry } */ *ap; 94665963Spendry { 94765963Spendry int error; 94865963Spendry 94965963Spendry struct vnode *fdvp = ap->a_fdvp; 95065963Spendry struct vnode *fvp = ap->a_fvp; 95165963Spendry struct vnode *tdvp = ap->a_tdvp; 95265963Spendry struct vnode *tvp = ap->a_tvp; 95365963Spendry 95465963Spendry if (fdvp->v_op == union_vnodeop_p) { /* always true */ 95565963Spendry struct union_node *un = VTOUNION(fdvp); 95665997Spendry if (un->un_uppervp == NULLVP) { 957*67575Spendry /* 958*67575Spendry * this should never happen in normal 959*67575Spendry * operation but might if there was 960*67575Spendry * a problem creating the top-level shadow 961*67575Spendry * directory. 962*67575Spendry */ 963*67575Spendry error = EXDEV; 96465963Spendry goto bad; 96565963Spendry } 96665963Spendry 96765963Spendry fdvp = un->un_uppervp; 96865963Spendry VREF(fdvp); 96965963Spendry vrele(ap->a_fdvp); 97065963Spendry } 97165963Spendry 97265963Spendry if (fvp->v_op == union_vnodeop_p) { /* always true */ 97365963Spendry struct union_node *un = VTOUNION(fvp); 97465997Spendry if (un->un_uppervp == NULLVP) { 975*67575Spendry /* XXX: should do a copyup */ 976*67575Spendry error = EXDEV; 97765963Spendry goto bad; 97865963Spendry } 97965963Spendry 980*67575Spendry if (un->un_lowervp != NULLVP) 981*67575Spendry ap->a_fcnp->cn_flags |= DOWHITEOUT; 982*67575Spendry 98365963Spendry fvp = un->un_uppervp; 98465963Spendry VREF(fvp); 98565963Spendry vrele(ap->a_fvp); 98665963Spendry } 98765963Spendry 98865963Spendry if (tdvp->v_op == union_vnodeop_p) { 98965963Spendry struct union_node *un = VTOUNION(tdvp); 99065997Spendry if (un->un_uppervp == NULLVP) { 99167076Spendry /* 99267076Spendry * this should never happen in normal 99367076Spendry * operation but might if there was 99467076Spendry * a problem creating the top-level shadow 99567076Spendry * directory. 99667076Spendry */ 997*67575Spendry error = EXDEV; 99865963Spendry goto bad; 99965963Spendry } 100065963Spendry 100165963Spendry tdvp = un->un_uppervp; 100265963Spendry VREF(tdvp); 100366051Spendry un->un_flags |= UN_KLOCK; 100465997Spendry vput(ap->a_tdvp); 100565963Spendry } 100665963Spendry 100767076Spendry if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 100865963Spendry struct union_node *un = VTOUNION(tvp); 100965963Spendry 101065963Spendry tvp = un->un_uppervp; 101167076Spendry if (tvp != NULLVP) { 101267076Spendry VREF(tvp); 101367076Spendry un->un_flags |= UN_KLOCK; 101467076Spendry } 101565963Spendry vput(ap->a_tvp); 101665963Spendry } 101765963Spendry 101865963Spendry return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 101965963Spendry 102065963Spendry bad: 102165963Spendry vrele(fdvp); 102265963Spendry vrele(fvp); 102365963Spendry vput(tdvp); 102467076Spendry if (tvp != NULLVP) 102565963Spendry vput(tvp); 102665963Spendry 102765963Spendry return (error); 102865963Spendry } 102965963Spendry 103065963Spendry int 103165963Spendry union_mkdir(ap) 103265963Spendry struct vop_mkdir_args /* { 103365963Spendry struct vnode *a_dvp; 103465963Spendry struct vnode **a_vpp; 103565963Spendry struct componentname *a_cnp; 103665963Spendry struct vattr *a_vap; 103765963Spendry } */ *ap; 103865963Spendry { 103965963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 104065963Spendry struct vnode *dvp = un->un_uppervp; 104165963Spendry 104267076Spendry if (dvp != NULLVP) { 104365963Spendry int error; 104465963Spendry struct vnode *vp; 104565963Spendry 104666152Spendry FIXUP(un); 104765963Spendry VREF(dvp); 104866051Spendry un->un_flags |= UN_KLOCK; 104965963Spendry vput(ap->a_dvp); 105065963Spendry error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 105165963Spendry if (error) 105265963Spendry return (error); 105365963Spendry 105465963Spendry error = union_allocvp( 105565963Spendry ap->a_vpp, 105665989Spendry ap->a_dvp->v_mount, 105765989Spendry ap->a_dvp, 105865965Spendry NULLVP, 105965963Spendry ap->a_cnp, 106065963Spendry vp, 106165963Spendry NULLVP); 106265965Spendry if (error) 106366051Spendry vput(vp); 106465963Spendry return (error); 106565963Spendry } 106665963Spendry 106765963Spendry vput(ap->a_dvp); 106865963Spendry return (EROFS); 106965963Spendry } 107065963Spendry 107165963Spendry int 107265963Spendry union_rmdir(ap) 107365963Spendry struct vop_rmdir_args /* { 107465963Spendry struct vnode *a_dvp; 107565963Spendry struct vnode *a_vp; 107665963Spendry struct componentname *a_cnp; 107765963Spendry } */ *ap; 107865963Spendry { 107965963Spendry int error; 108065963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 108165963Spendry struct union_node *un = VTOUNION(ap->a_vp); 108265963Spendry 1083*67575Spendry if (dun->un_uppervp == NULLVP) 1084*67575Spendry panic("union rmdir: null upper vnode"); 1085*67575Spendry 1086*67575Spendry if (un->un_uppervp != NULLVP) { 108765963Spendry struct vnode *dvp = dun->un_uppervp; 108865963Spendry struct vnode *vp = un->un_uppervp; 108965963Spendry 109066152Spendry FIXUP(dun); 109165963Spendry VREF(dvp); 109266051Spendry dun->un_flags |= UN_KLOCK; 109365963Spendry vput(ap->a_dvp); 109466152Spendry FIXUP(un); 109565963Spendry VREF(vp); 109666051Spendry un->un_flags |= UN_KLOCK; 109765963Spendry vput(ap->a_vp); 109865963Spendry 1099*67575Spendry if (un->un_lowervp != NULLVP) 1100*67575Spendry ap->a_cnp->cn_flags |= DOWHITEOUT; 110166051Spendry error = VOP_RMDIR(dvp, vp, ap->a_cnp); 110266027Spendry if (!error) 110366027Spendry union_removed_upper(un); 110465963Spendry } else { 1105*67575Spendry FIXUP(dun); 1106*67575Spendry error = union_mkwhiteout( 1107*67575Spendry MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 1108*67575Spendry dun->un_uppervp, ap->a_cnp, un->un_path); 110965963Spendry vput(ap->a_dvp); 111065963Spendry vput(ap->a_vp); 111165963Spendry } 111265963Spendry 111365963Spendry return (error); 111465963Spendry } 111565963Spendry 111665963Spendry int 111765963Spendry union_symlink(ap) 111865963Spendry struct vop_symlink_args /* { 111965963Spendry struct vnode *a_dvp; 112065963Spendry struct vnode **a_vpp; 112165963Spendry struct componentname *a_cnp; 112265963Spendry struct vattr *a_vap; 112365963Spendry char *a_target; 112465963Spendry } */ *ap; 112565963Spendry { 112665963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 112765963Spendry struct vnode *dvp = un->un_uppervp; 112865963Spendry 112967076Spendry if (dvp != NULLVP) { 113065963Spendry int error; 113165963Spendry struct vnode *vp; 113265963Spendry struct mount *mp = ap->a_dvp->v_mount; 113365963Spendry 113466152Spendry FIXUP(un); 113565963Spendry VREF(dvp); 113666051Spendry un->un_flags |= UN_KLOCK; 113765963Spendry vput(ap->a_dvp); 113865963Spendry error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 113965963Spendry ap->a_vap, ap->a_target); 114065997Spendry *ap->a_vpp = NULLVP; 114165963Spendry return (error); 114265963Spendry } 114365963Spendry 114465963Spendry vput(ap->a_dvp); 114565963Spendry return (EROFS); 114665963Spendry } 114765963Spendry 114865935Spendry /* 114965935Spendry * union_readdir works in concert with getdirentries and 115065935Spendry * readdir(3) to provide a list of entries in the unioned 115165935Spendry * directories. getdirentries is responsible for walking 115265935Spendry * down the union stack. readdir(3) is responsible for 115365935Spendry * eliminating duplicate names from the returned data stream. 115465935Spendry */ 115565935Spendry int 115665935Spendry union_readdir(ap) 115765935Spendry struct vop_readdir_args /* { 115865935Spendry struct vnodeop_desc *a_desc; 115965935Spendry struct vnode *a_vp; 116065935Spendry struct uio *a_uio; 116165935Spendry struct ucred *a_cred; 116267369Smckusick int *a_eofflag; 116367369Smckusick u_long *a_cookies; 116467369Smckusick int a_ncookies; 116565935Spendry } */ *ap; 116665935Spendry { 116767369Smckusick register struct union_node *un = VTOUNION(ap->a_vp); 116867369Smckusick register struct vnode *uvp = un->un_uppervp; 116965935Spendry 117067369Smckusick if (uvp == NULLVP) 117167369Smckusick return (0); 117265935Spendry 117367369Smckusick FIXUP(un); 117467369Smckusick ap->a_vp = uvp; 117567369Smckusick return (VOCALL(uvp->v_op, VOFFSET(vop_readdir), ap)); 117665935Spendry } 117765935Spendry 117865935Spendry int 117965963Spendry union_readlink(ap) 118065963Spendry struct vop_readlink_args /* { 118165963Spendry struct vnode *a_vp; 118265963Spendry struct uio *a_uio; 118365963Spendry struct ucred *a_cred; 118465963Spendry } */ *ap; 118565963Spendry { 118665963Spendry int error; 118765963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 118866051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 118965963Spendry 119066051Spendry if (dolock) 119166051Spendry VOP_LOCK(vp); 119266152Spendry else 119366152Spendry FIXUP(VTOUNION(ap->a_vp)); 119465963Spendry error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 119566051Spendry if (dolock) 119666051Spendry VOP_UNLOCK(vp); 119765963Spendry 119865963Spendry return (error); 119965963Spendry } 120065963Spendry 120165963Spendry int 120265963Spendry union_abortop(ap) 120365963Spendry struct vop_abortop_args /* { 120465963Spendry struct vnode *a_dvp; 120565963Spendry struct componentname *a_cnp; 120665963Spendry } */ *ap; 120765963Spendry { 120865963Spendry int error; 120965965Spendry struct vnode *vp = OTHERVP(ap->a_dvp); 121065963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 121165963Spendry int islocked = un->un_flags & UN_LOCKED; 121266051Spendry int dolock = (vp == LOWERVP(ap->a_dvp)); 121365963Spendry 121466152Spendry if (islocked) { 121566152Spendry if (dolock) 121666152Spendry VOP_LOCK(vp); 121766152Spendry else 121866152Spendry FIXUP(VTOUNION(ap->a_dvp)); 121966152Spendry } 122065963Spendry error = VOP_ABORTOP(vp, ap->a_cnp); 122166051Spendry if (islocked && dolock) 122265963Spendry VOP_UNLOCK(vp); 122365963Spendry 122465963Spendry return (error); 122565963Spendry } 122665963Spendry 122765963Spendry int 122865935Spendry union_inactive(ap) 122965935Spendry struct vop_inactive_args /* { 123065935Spendry struct vnode *a_vp; 123165935Spendry } */ *ap; 123265935Spendry { 123367073Spendry struct union_node *un = VTOUNION(ap->a_vp); 123465935Spendry 123565935Spendry /* 123665935Spendry * Do nothing (and _don't_ bypass). 123765935Spendry * Wait to vrele lowervp until reclaim, 123865935Spendry * so that until then our union_node is in the 123965935Spendry * cache and reusable. 124065935Spendry * 124165935Spendry * NEEDSWORK: Someday, consider inactive'ing 124265935Spendry * the lowervp and then trying to reactivate it 124365935Spendry * with capabilities (v_id) 124465935Spendry * like they do in the name lookup cache code. 124565935Spendry * That's too much work for now. 124665935Spendry */ 124765989Spendry 124866027Spendry #ifdef UNION_DIAGNOSTIC 124965989Spendry if (un->un_flags & UN_LOCKED) 125065989Spendry panic("union: inactivating locked node"); 125167073Spendry if (un->un_flags & UN_ULOCK) 125267073Spendry panic("union: inactivating w/locked upper node"); 125365989Spendry #endif 125465989Spendry 125567073Spendry if ((un->un_flags & UN_CACHED) == 0) 125667073Spendry vgone(ap->a_vp); 125767073Spendry 125865935Spendry return (0); 125965935Spendry } 126065935Spendry 126165935Spendry int 126265935Spendry union_reclaim(ap) 126365935Spendry struct vop_reclaim_args /* { 126465935Spendry struct vnode *a_vp; 126565935Spendry } */ *ap; 126665935Spendry { 126765935Spendry 126866053Spendry union_freevp(ap->a_vp); 126966053Spendry 127065935Spendry return (0); 127165935Spendry } 127265935Spendry 127365963Spendry int 127465963Spendry union_lock(ap) 127565963Spendry struct vop_lock_args *ap; 127665963Spendry { 127766149Spendry struct vnode *vp = ap->a_vp; 127866149Spendry struct union_node *un; 127965935Spendry 128066149Spendry start: 128166149Spendry while (vp->v_flag & VXLOCK) { 128266149Spendry vp->v_flag |= VXWANT; 128366149Spendry sleep((caddr_t)vp, PINOD); 128466149Spendry } 128566149Spendry 128666149Spendry un = VTOUNION(vp); 128766149Spendry 128867076Spendry if (un->un_uppervp != NULLVP) { 128967230Spendry if (((un->un_flags & UN_ULOCK) == 0) && 129067230Spendry (vp->v_usecount != 0)) { 129166149Spendry un->un_flags |= UN_ULOCK; 129266051Spendry VOP_LOCK(un->un_uppervp); 129366051Spendry } 129466051Spendry #ifdef DIAGNOSTIC 129566051Spendry if (un->un_flags & UN_KLOCK) 129666051Spendry panic("union: dangling upper lock"); 129766051Spendry #endif 129866051Spendry } 129966051Spendry 130066149Spendry if (un->un_flags & UN_LOCKED) { 130165963Spendry #ifdef DIAGNOSTIC 130265989Spendry if (curproc && un->un_pid == curproc->p_pid && 130365989Spendry un->un_pid > -1 && curproc->p_pid > -1) 130465989Spendry panic("union: locking against myself"); 130565963Spendry #endif 130665963Spendry un->un_flags |= UN_WANT; 130765963Spendry sleep((caddr_t) &un->un_flags, PINOD); 130866149Spendry goto start; 130965963Spendry } 131065989Spendry 131165963Spendry #ifdef DIAGNOSTIC 131265989Spendry if (curproc) 131365989Spendry un->un_pid = curproc->p_pid; 131465989Spendry else 131565989Spendry un->un_pid = -1; 131665963Spendry #endif 131766028Spendry 131866149Spendry un->un_flags |= UN_LOCKED; 131966028Spendry return (0); 132065963Spendry } 132165963Spendry 132265935Spendry int 132365963Spendry union_unlock(ap) 132465963Spendry struct vop_lock_args *ap; 132565963Spendry { 132665963Spendry struct union_node *un = VTOUNION(ap->a_vp); 132765963Spendry 132865963Spendry #ifdef DIAGNOSTIC 132965965Spendry if ((un->un_flags & UN_LOCKED) == 0) 133065965Spendry panic("union: unlock unlocked node"); 133165989Spendry if (curproc && un->un_pid != curproc->p_pid && 133265989Spendry curproc->p_pid > -1 && un->un_pid > -1) 133365963Spendry panic("union: unlocking other process's union node"); 133465963Spendry #endif 133565963Spendry 133665963Spendry un->un_flags &= ~UN_LOCKED; 133766051Spendry 133866051Spendry if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 133966051Spendry VOP_UNLOCK(un->un_uppervp); 134066051Spendry 134166051Spendry un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 134266051Spendry 134365963Spendry if (un->un_flags & UN_WANT) { 134465963Spendry un->un_flags &= ~UN_WANT; 134565963Spendry wakeup((caddr_t) &un->un_flags); 134665963Spendry } 134765963Spendry 134865963Spendry #ifdef DIAGNOSTIC 134965963Spendry un->un_pid = 0; 135065963Spendry #endif 135166028Spendry 135266028Spendry return (0); 135365963Spendry } 135465963Spendry 135565963Spendry int 135665963Spendry union_bmap(ap) 135765963Spendry struct vop_bmap_args /* { 135865963Spendry struct vnode *a_vp; 135965963Spendry daddr_t a_bn; 136065963Spendry struct vnode **a_vpp; 136165963Spendry daddr_t *a_bnp; 136265963Spendry int *a_runp; 136365963Spendry } */ *ap; 136465963Spendry { 136565963Spendry int error; 136665963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 136766051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 136865963Spendry 136966051Spendry if (dolock) 137066051Spendry VOP_LOCK(vp); 137166152Spendry else 137266152Spendry FIXUP(VTOUNION(ap->a_vp)); 137365963Spendry error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 137466051Spendry if (dolock) 137566051Spendry VOP_UNLOCK(vp); 137665963Spendry 137765963Spendry return (error); 137865963Spendry } 137965963Spendry 138065963Spendry int 138165935Spendry union_print(ap) 138265935Spendry struct vop_print_args /* { 138365935Spendry struct vnode *a_vp; 138465935Spendry } */ *ap; 138565935Spendry { 138665935Spendry struct vnode *vp = ap->a_vp; 138765935Spendry 138865935Spendry printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 138965935Spendry vp, UPPERVP(vp), LOWERVP(vp)); 139065935Spendry return (0); 139165935Spendry } 139265935Spendry 139365963Spendry int 139465963Spendry union_islocked(ap) 139565963Spendry struct vop_islocked_args /* { 139665963Spendry struct vnode *a_vp; 139765963Spendry } */ *ap; 139865963Spendry { 139965935Spendry 140065963Spendry return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 140165963Spendry } 140265963Spendry 140365935Spendry int 140465963Spendry union_pathconf(ap) 140565963Spendry struct vop_pathconf_args /* { 140665963Spendry struct vnode *a_vp; 140765963Spendry int a_name; 140865963Spendry int *a_retval; 140965935Spendry } */ *ap; 141065935Spendry { 141165935Spendry int error; 141265963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 141366051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 141465935Spendry 141566051Spendry if (dolock) 141666051Spendry VOP_LOCK(vp); 141766152Spendry else 141866152Spendry FIXUP(VTOUNION(ap->a_vp)); 141965963Spendry error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 142066051Spendry if (dolock) 142166051Spendry VOP_UNLOCK(vp); 142265935Spendry 142365963Spendry return (error); 142465963Spendry } 142565935Spendry 142665963Spendry int 142765963Spendry union_advlock(ap) 142865963Spendry struct vop_advlock_args /* { 142965963Spendry struct vnode *a_vp; 143065963Spendry caddr_t a_id; 143165963Spendry int a_op; 143265963Spendry struct flock *a_fl; 143365963Spendry int a_flags; 143465963Spendry } */ *ap; 143565963Spendry { 143665935Spendry 143765963Spendry return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 143865963Spendry ap->a_fl, ap->a_flags)); 143965935Spendry } 144065935Spendry 144165935Spendry 144265935Spendry /* 144365963Spendry * XXX - vop_strategy must be hand coded because it has no 144465935Spendry * vnode in its arguments. 144565935Spendry * This goes away with a merged VM/buffer cache. 144665935Spendry */ 144765935Spendry int 144865963Spendry union_strategy(ap) 144965963Spendry struct vop_strategy_args /* { 145065935Spendry struct buf *a_bp; 145165935Spendry } */ *ap; 145265935Spendry { 145365935Spendry struct buf *bp = ap->a_bp; 145465935Spendry int error; 145565935Spendry struct vnode *savedvp; 145665935Spendry 145765935Spendry savedvp = bp->b_vp; 145865963Spendry bp->b_vp = OTHERVP(bp->b_vp); 145965935Spendry 146065935Spendry #ifdef DIAGNOSTIC 146165997Spendry if (bp->b_vp == NULLVP) 146265963Spendry panic("union_strategy: nil vp"); 146365963Spendry if (((bp->b_flags & B_READ) == 0) && 146465963Spendry (bp->b_vp == LOWERVP(savedvp))) 146565963Spendry panic("union_strategy: writing to lowervp"); 146665935Spendry #endif 146765935Spendry 146865963Spendry error = VOP_STRATEGY(bp); 146965935Spendry bp->b_vp = savedvp; 147065935Spendry 147165935Spendry return (error); 147265935Spendry } 147365935Spendry 147465935Spendry /* 147565935Spendry * Global vfs data structures 147665935Spendry */ 147765935Spendry int (**union_vnodeop_p)(); 147865965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = { 147965963Spendry { &vop_default_desc, vn_default_error }, 148065963Spendry { &vop_lookup_desc, union_lookup }, /* lookup */ 148165963Spendry { &vop_create_desc, union_create }, /* create */ 1482*67575Spendry { &vop_whiteout_desc, union_whiteout }, /* whiteout */ 148365963Spendry { &vop_mknod_desc, union_mknod }, /* mknod */ 148465963Spendry { &vop_open_desc, union_open }, /* open */ 148565963Spendry { &vop_close_desc, union_close }, /* close */ 148665963Spendry { &vop_access_desc, union_access }, /* access */ 148765963Spendry { &vop_getattr_desc, union_getattr }, /* getattr */ 148865963Spendry { &vop_setattr_desc, union_setattr }, /* setattr */ 148965963Spendry { &vop_read_desc, union_read }, /* read */ 149065963Spendry { &vop_write_desc, union_write }, /* write */ 149165963Spendry { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 149265963Spendry { &vop_select_desc, union_select }, /* select */ 149365963Spendry { &vop_mmap_desc, union_mmap }, /* mmap */ 149465963Spendry { &vop_fsync_desc, union_fsync }, /* fsync */ 149565963Spendry { &vop_seek_desc, union_seek }, /* seek */ 149665963Spendry { &vop_remove_desc, union_remove }, /* remove */ 149765963Spendry { &vop_link_desc, union_link }, /* link */ 149865963Spendry { &vop_rename_desc, union_rename }, /* rename */ 149965963Spendry { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 150065963Spendry { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 150165963Spendry { &vop_symlink_desc, union_symlink }, /* symlink */ 150265963Spendry { &vop_readdir_desc, union_readdir }, /* readdir */ 150365963Spendry { &vop_readlink_desc, union_readlink }, /* readlink */ 150465963Spendry { &vop_abortop_desc, union_abortop }, /* abortop */ 150565963Spendry { &vop_inactive_desc, union_inactive }, /* inactive */ 150665963Spendry { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 150765963Spendry { &vop_lock_desc, union_lock }, /* lock */ 150865963Spendry { &vop_unlock_desc, union_unlock }, /* unlock */ 150965963Spendry { &vop_bmap_desc, union_bmap }, /* bmap */ 151065963Spendry { &vop_strategy_desc, union_strategy }, /* strategy */ 151165963Spendry { &vop_print_desc, union_print }, /* print */ 151265963Spendry { &vop_islocked_desc, union_islocked }, /* islocked */ 151365963Spendry { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 151465963Spendry { &vop_advlock_desc, union_advlock }, /* advlock */ 151565963Spendry #ifdef notdef 151665963Spendry { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 151765963Spendry { &vop_valloc_desc, union_valloc }, /* valloc */ 151865963Spendry { &vop_vfree_desc, union_vfree }, /* vfree */ 151965963Spendry { &vop_truncate_desc, union_truncate }, /* truncate */ 152065963Spendry { &vop_update_desc, union_update }, /* update */ 152165963Spendry { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 152265963Spendry #endif 152365935Spendry { (struct vnodeop_desc*)NULL, (int(*)())NULL } 152465935Spendry }; 152565935Spendry struct vnodeopv_desc union_vnodeop_opv_desc = 152665935Spendry { &union_vnodeop_p, union_vnodeop_entries }; 1527