165935Spendry /* 268590Spendry * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry. 368590Spendry * Copyright (c) 1992, 1993, 1994, 1995 468590Spendry * The Regents of the University of California. 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*69389Spendry * @(#)union_vnops.c 8.26 (Berkeley) 05/11/95 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> 1967575Spendry #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; 14167575Spendry int iswhiteout; 14267575Spendry 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; 16667575Spendry 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 } 18767575Spendry if (uerror == ENOENT || uerror == EJUSTRETURN) { 18867575Spendry if (cnp->cn_flags & ISWHITEOUT) { 18967575Spendry iswhiteout = 1; 19067575Spendry } else if (lowerdvp != NULLVP) { 19167575Spendry lerror = VOP_GETATTR(upperdvp, &va, 19267575Spendry cnp->cn_cred, cnp->cn_proc); 19367575Spendry if (lerror == 0 && (va.va_flags & OPAQUE)) 19467575Spendry iswhiteout = 1; 19567575Spendry } 19667575Spendry } 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 */ 20867575Spendry 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, 31468078Spendry uppervp, lowervp, 1); 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; 34568078Spendry struct mount *mp; 34665963Spendry 34766152Spendry FIXUP(un); 34866152Spendry 34965963Spendry VREF(dvp); 35066051Spendry un->un_flags |= UN_KLOCK; 35168078Spendry mp = ap->a_dvp->v_mount; 35265963Spendry vput(ap->a_dvp); 35365963Spendry error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap); 35465963Spendry if (error) 35565963Spendry return (error); 35665963Spendry 35765963Spendry error = union_allocvp( 35865963Spendry ap->a_vpp, 35968078Spendry mp, 36065965Spendry NULLVP, 36168078Spendry NULLVP, 36265963Spendry ap->a_cnp, 36365963Spendry vp, 36468078Spendry NULLVP, 36568078Spendry 1); 36665965Spendry if (error) 36766051Spendry vput(vp); 36865963Spendry return (error); 36965963Spendry } 37065963Spendry 37165963Spendry vput(ap->a_dvp); 37265963Spendry return (EROFS); 37365963Spendry } 37465963Spendry 37565963Spendry int 37667575Spendry union_whiteout(ap) 37767575Spendry struct vop_whiteout_args /* { 37867575Spendry struct vnode *a_dvp; 37967575Spendry struct componentname *a_cnp; 38067575Spendry int a_flags; 38167575Spendry } */ *ap; 38267575Spendry { 38367575Spendry struct union_node *un = VTOUNION(ap->a_dvp); 38467575Spendry 38567575Spendry if (un->un_uppervp == NULLVP) 38667575Spendry return (EOPNOTSUPP); 38767575Spendry 38867575Spendry FIXUP(un); 38967575Spendry return (VOP_WHITEOUT(un->un_uppervp, ap->a_cnp, ap->a_flags)); 39067575Spendry } 39167575Spendry 39267575Spendry int 39365963Spendry union_mknod(ap) 39465963Spendry struct vop_mknod_args /* { 39565963Spendry struct vnode *a_dvp; 39665963Spendry struct vnode **a_vpp; 39765963Spendry struct componentname *a_cnp; 39865963Spendry struct vattr *a_vap; 39965963Spendry } */ *ap; 40065963Spendry { 40165963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 40265963Spendry struct vnode *dvp = un->un_uppervp; 40365963Spendry 40467076Spendry if (dvp != NULLVP) { 40565963Spendry int error; 40665963Spendry struct vnode *vp; 40768078Spendry struct mount *mp; 40865963Spendry 40966152Spendry FIXUP(un); 41066152Spendry 41165963Spendry VREF(dvp); 41266051Spendry un->un_flags |= UN_KLOCK; 41368078Spendry mp = ap->a_dvp->v_mount; 41465963Spendry vput(ap->a_dvp); 41565963Spendry error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap); 41665963Spendry if (error) 41765963Spendry return (error); 41865963Spendry 41967076Spendry if (vp != NULLVP) { 42065965Spendry error = union_allocvp( 42165965Spendry ap->a_vpp, 42268078Spendry mp, 42365965Spendry NULLVP, 42468078Spendry NULLVP, 42565965Spendry ap->a_cnp, 42665965Spendry vp, 42768078Spendry NULLVP, 42868078Spendry 1); 42965965Spendry if (error) 43066051Spendry vput(vp); 43165965Spendry } 43265963Spendry return (error); 43365963Spendry } 43465963Spendry 43565963Spendry vput(ap->a_dvp); 43665963Spendry return (EROFS); 43765963Spendry } 43865963Spendry 43965935Spendry int 44065935Spendry union_open(ap) 44165935Spendry struct vop_open_args /* { 44265935Spendry struct vnodeop_desc *a_desc; 44365935Spendry struct vnode *a_vp; 44465935Spendry int a_mode; 44565935Spendry struct ucred *a_cred; 44665935Spendry struct proc *a_p; 44765935Spendry } */ *ap; 44865935Spendry { 44965935Spendry struct union_node *un = VTOUNION(ap->a_vp); 45065965Spendry struct vnode *tvp; 45165935Spendry int mode = ap->a_mode; 45265935Spendry struct ucred *cred = ap->a_cred; 45365935Spendry struct proc *p = ap->a_p; 45465965Spendry int error; 45565935Spendry 45665935Spendry /* 45765935Spendry * If there is an existing upper vp then simply open that. 45865935Spendry */ 45965965Spendry tvp = un->un_uppervp; 46065965Spendry if (tvp == NULLVP) { 46165935Spendry /* 46265965Spendry * If the lower vnode is being opened for writing, then 46365965Spendry * copy the file contents to the upper vnode and open that, 46465965Spendry * otherwise can simply open the lower vnode. 46565935Spendry */ 46665965Spendry tvp = un->un_lowervp; 46765965Spendry if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 46867169Spendry error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p); 46965965Spendry if (error == 0) 47065965Spendry error = VOP_OPEN(un->un_uppervp, mode, cred, p); 47165965Spendry return (error); 47265935Spendry } 47366051Spendry 47466051Spendry /* 47566051Spendry * Just open the lower vnode 47666051Spendry */ 47766027Spendry un->un_openl++; 47866051Spendry VOP_LOCK(tvp); 47966051Spendry error = VOP_OPEN(tvp, mode, cred, p); 48066051Spendry VOP_UNLOCK(tvp); 48166051Spendry 48266051Spendry return (error); 48365935Spendry } 48465935Spendry 48566152Spendry FIXUP(un); 48666152Spendry 48765965Spendry error = VOP_OPEN(tvp, mode, cred, p); 48865965Spendry 48965965Spendry return (error); 49065935Spendry } 49165935Spendry 49265963Spendry int 49365963Spendry union_close(ap) 49465963Spendry struct vop_close_args /* { 49565963Spendry struct vnode *a_vp; 49665963Spendry int a_fflag; 49765963Spendry struct ucred *a_cred; 49865963Spendry struct proc *a_p; 49965963Spendry } */ *ap; 50065963Spendry { 50165997Spendry struct union_node *un = VTOUNION(ap->a_vp); 50265997Spendry struct vnode *vp; 50365963Spendry 50467076Spendry if (un->un_uppervp != NULLVP) { 50565997Spendry vp = un->un_uppervp; 50665997Spendry } else { 50766027Spendry #ifdef UNION_DIAGNOSTIC 50866027Spendry if (un->un_openl <= 0) 50966027Spendry panic("union: un_openl cnt"); 51065997Spendry #endif 51166027Spendry --un->un_openl; 51265997Spendry vp = un->un_lowervp; 51365997Spendry } 51466027Spendry 51565997Spendry return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p)); 51665963Spendry } 51765963Spendry 51865935Spendry /* 51965963Spendry * Check access permission on the union vnode. 52065963Spendry * The access check being enforced is to check 52165963Spendry * against both the underlying vnode, and any 52265963Spendry * copied vnode. This ensures that no additional 52365963Spendry * file permissions are given away simply because 52465963Spendry * the user caused an implicit file copy. 52565963Spendry */ 52665963Spendry int 52765963Spendry union_access(ap) 52865963Spendry struct vop_access_args /* { 52965963Spendry struct vnodeop_desc *a_desc; 53065963Spendry struct vnode *a_vp; 53165963Spendry int a_mode; 53265963Spendry struct ucred *a_cred; 53365963Spendry struct proc *a_p; 53465963Spendry } */ *ap; 53565963Spendry { 53665963Spendry struct union_node *un = VTOUNION(ap->a_vp); 53766149Spendry int error = EACCES; 53865963Spendry struct vnode *vp; 53965963Spendry 54067076Spendry if ((vp = un->un_uppervp) != NULLVP) { 54166152Spendry FIXUP(un); 54266152Spendry return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); 54366152Spendry } 54466152Spendry 54567076Spendry if ((vp = un->un_lowervp) != NULLVP) { 54665965Spendry VOP_LOCK(vp); 54765963Spendry error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p); 54866152Spendry if (error == 0) { 54966152Spendry struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount); 55066152Spendry 55166152Spendry if (um->um_op == UNMNT_BELOW) 55266152Spendry error = VOP_ACCESS(vp, ap->a_mode, 55366152Spendry um->um_cred, ap->a_p); 55466152Spendry } 55565965Spendry VOP_UNLOCK(vp); 55665963Spendry if (error) 55765963Spendry return (error); 55865963Spendry } 55965963Spendry 56065965Spendry return (error); 56165963Spendry } 56265963Spendry 56365963Spendry /* 56467109Spendry * We handle getattr only to change the fsid and 56567109Spendry * track object sizes 56665935Spendry */ 56765935Spendry int 56865935Spendry union_getattr(ap) 56965935Spendry struct vop_getattr_args /* { 57065935Spendry struct vnode *a_vp; 57165935Spendry struct vattr *a_vap; 57265935Spendry struct ucred *a_cred; 57365935Spendry struct proc *a_p; 57465935Spendry } */ *ap; 57565935Spendry { 57665935Spendry int error; 57766062Spendry struct union_node *un = VTOUNION(ap->a_vp); 57866062Spendry struct vnode *vp = un->un_uppervp; 57966062Spendry struct vattr *vap; 58066062Spendry struct vattr va; 58165935Spendry 58266062Spendry 58366062Spendry /* 58466062Spendry * Some programs walk the filesystem hierarchy by counting 58566062Spendry * links to directories to avoid stat'ing all the time. 58666062Spendry * This means the link count on directories needs to be "correct". 58766062Spendry * The only way to do that is to call getattr on both layers 58866062Spendry * and fix up the link count. The link count will not necessarily 58966062Spendry * be accurate but will be large enough to defeat the tree walkers. 59066062Spendry */ 59166062Spendry 59266062Spendry vap = ap->a_vap; 59366062Spendry 59466062Spendry vp = un->un_uppervp; 59566062Spendry if (vp != NULLVP) { 59667073Spendry /* 59767073Spendry * It's not clear whether VOP_GETATTR is to be 59867073Spendry * called with the vnode locked or not. stat() calls 59967073Spendry * it with (vp) locked, and fstat calls it with 60067073Spendry * (vp) unlocked. 60167073Spendry * In the mean time, compensate here by checking 60267073Spendry * the union_node's lock flag. 60367073Spendry */ 60467073Spendry if (un->un_flags & UN_LOCKED) 60567073Spendry FIXUP(un); 60667073Spendry 60766062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 60866062Spendry if (error) 60966062Spendry return (error); 61067109Spendry union_newsize(ap->a_vp, vap->va_size, VNOVAL); 61166062Spendry } 61266062Spendry 61366062Spendry if (vp == NULLVP) { 61466062Spendry vp = un->un_lowervp; 61566062Spendry } else if (vp->v_type == VDIR) { 61666062Spendry vp = un->un_lowervp; 61766062Spendry vap = &va; 61866062Spendry } else { 61966062Spendry vp = NULLVP; 62066062Spendry } 62166062Spendry 62266062Spendry if (vp != NULLVP) { 62366062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 62466062Spendry if (error) 62566062Spendry return (error); 62667109Spendry union_newsize(ap->a_vp, VNOVAL, vap->va_size); 62766062Spendry } 62865965Spendry 62966062Spendry if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 63066062Spendry ap->a_vap->va_nlink += vap->va_nlink; 63166062Spendry 63267400Spendry ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 63365935Spendry return (0); 63465935Spendry } 63565935Spendry 63665963Spendry int 63765965Spendry union_setattr(ap) 63865963Spendry struct vop_setattr_args /* { 63965963Spendry struct vnode *a_vp; 64065963Spendry struct vattr *a_vap; 64165963Spendry struct ucred *a_cred; 64265963Spendry struct proc *a_p; 64365963Spendry } */ *ap; 64465963Spendry { 64565963Spendry struct union_node *un = VTOUNION(ap->a_vp); 64665963Spendry int error; 64765963Spendry 64866057Spendry /* 64966057Spendry * Handle case of truncating lower object to zero size, 65066057Spendry * by creating a zero length upper object. This is to 65166057Spendry * handle the case of open with O_TRUNC and O_CREAT. 65266057Spendry */ 65366057Spendry if ((un->un_uppervp == NULLVP) && 65466057Spendry /* assert(un->un_lowervp != NULLVP) */ 65567575Spendry (un->un_lowervp->v_type == VREG)) { 65667575Spendry error = union_copyup(un, (ap->a_vap->va_size != 0), 65767575Spendry ap->a_cred, ap->a_p); 65866057Spendry if (error) 65966057Spendry return (error); 66066057Spendry } 66166057Spendry 66266057Spendry /* 66366057Spendry * Try to set attributes in upper layer, 66466057Spendry * otherwise return read-only filesystem error. 66566057Spendry */ 66666057Spendry if (un->un_uppervp != NULLVP) { 66766152Spendry FIXUP(un); 66865963Spendry error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 66965963Spendry ap->a_cred, ap->a_p); 67067109Spendry if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 67167109Spendry union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 67265963Spendry } else { 67365963Spendry error = EROFS; 67465963Spendry } 67565963Spendry 67665963Spendry return (error); 67765963Spendry } 67865963Spendry 67965963Spendry int 68065963Spendry union_read(ap) 68165963Spendry struct vop_read_args /* { 68265963Spendry struct vnode *a_vp; 68365963Spendry struct uio *a_uio; 68465963Spendry int a_ioflag; 68565963Spendry struct ucred *a_cred; 68665963Spendry } */ *ap; 68765963Spendry { 68865963Spendry int error; 68965963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 69066051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 69165963Spendry 69266051Spendry if (dolock) 69366051Spendry VOP_LOCK(vp); 69466152Spendry else 69566152Spendry FIXUP(VTOUNION(ap->a_vp)); 69665963Spendry error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 69766051Spendry if (dolock) 69866051Spendry VOP_UNLOCK(vp); 69965963Spendry 70067109Spendry /* 70167109Spendry * XXX 70267109Spendry * perhaps the size of the underlying object has changed under 70367109Spendry * our feet. take advantage of the offset information present 70467109Spendry * in the uio structure. 70567109Spendry */ 70667109Spendry if (error == 0) { 70767109Spendry struct union_node *un = VTOUNION(ap->a_vp); 70867109Spendry off_t cur = ap->a_uio->uio_offset; 70967109Spendry 71067109Spendry if (vp == un->un_uppervp) { 71167109Spendry if (cur > un->un_uppersz) 71267109Spendry union_newsize(ap->a_vp, cur, VNOVAL); 71367109Spendry } else { 71467109Spendry if (cur > un->un_lowersz) 71567109Spendry union_newsize(ap->a_vp, VNOVAL, cur); 71667109Spendry } 71767109Spendry } 71867109Spendry 71965963Spendry return (error); 72065963Spendry } 72165963Spendry 72265963Spendry int 72365963Spendry union_write(ap) 72465963Spendry struct vop_read_args /* { 72565963Spendry struct vnode *a_vp; 72665963Spendry struct uio *a_uio; 72765963Spendry int a_ioflag; 72865963Spendry struct ucred *a_cred; 72965963Spendry } */ *ap; 73065963Spendry { 73165963Spendry int error; 73267575Spendry struct vnode *vp; 73367575Spendry struct union_node *un = VTOUNION(ap->a_vp); 73465963Spendry 73567575Spendry vp = UPPERVP(ap->a_vp); 73667575Spendry if (vp == NULLVP) 73767575Spendry panic("union: missing upper layer in write"); 73867575Spendry 73967575Spendry FIXUP(un); 74065963Spendry error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 74165963Spendry 74267109Spendry /* 74367109Spendry * the size of the underlying object may be changed by the 74467109Spendry * write. 74567109Spendry */ 74667109Spendry if (error == 0) { 74767109Spendry off_t cur = ap->a_uio->uio_offset; 74867109Spendry 74967575Spendry if (cur > un->un_uppersz) 75067575Spendry union_newsize(ap->a_vp, cur, VNOVAL); 75167109Spendry } 75267109Spendry 75365963Spendry return (error); 75465963Spendry } 75565963Spendry 75667751Spendry union_lease(ap) 75767751Spendry struct vop_lease_args /* { 75867751Spendry struct vnode *a_vp; 75967751Spendry struct proc *a_p; 76067751Spendry struct ucred *a_cred; 76167751Spendry int a_flag; 76267751Spendry } */ *ap; 76367751Spendry { 76467751Spendry 76567751Spendry return (VOP_LEASE(OTHERVP(ap->a_vp), ap->a_p, ap->a_cred, ap->a_flag)); 76667751Spendry } 76767751Spendry 76865963Spendry int 76965963Spendry union_ioctl(ap) 77065963Spendry struct vop_ioctl_args /* { 77165963Spendry struct vnode *a_vp; 77265963Spendry int a_command; 77365963Spendry caddr_t a_data; 77465963Spendry int a_fflag; 77565963Spendry struct ucred *a_cred; 77665963Spendry struct proc *a_p; 77765963Spendry } */ *ap; 77865963Spendry { 77965963Spendry 78065963Spendry return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 78165963Spendry ap->a_fflag, ap->a_cred, ap->a_p)); 78265963Spendry } 78365963Spendry 78465963Spendry int 78565963Spendry union_select(ap) 78665963Spendry struct vop_select_args /* { 78765963Spendry struct vnode *a_vp; 78865963Spendry int a_which; 78965963Spendry int a_fflags; 79065963Spendry struct ucred *a_cred; 79165963Spendry struct proc *a_p; 79265963Spendry } */ *ap; 79365963Spendry { 79465963Spendry 79565963Spendry return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 79665963Spendry ap->a_cred, ap->a_p)); 79765963Spendry } 79865963Spendry 79965963Spendry int 800*69389Spendry union_revoke(ap) 801*69389Spendry struct vop_revoke_args /* { 802*69389Spendry struct vnode *a_vp; 803*69389Spendry int a_flags; 804*69389Spendry } */ *ap; 805*69389Spendry { 806*69389Spendry struct vnode *vp = ap->a_vp; 807*69389Spendry 808*69389Spendry if (UPPERVP(vp)) 809*69389Spendry VOP_REVOKE(UPPERVP(vp), ap->a_flags); 810*69389Spendry if (LOWERVP(vp)) 811*69389Spendry VOP_REVOKE(UPPERVP(vp), ap->a_flags); 812*69389Spendry vgone(vp); 813*69389Spendry } 814*69389Spendry 815*69389Spendry int 81665963Spendry union_mmap(ap) 81765963Spendry struct vop_mmap_args /* { 81865963Spendry struct vnode *a_vp; 81965963Spendry int a_fflags; 82065963Spendry struct ucred *a_cred; 82165963Spendry struct proc *a_p; 82265963Spendry } */ *ap; 82365963Spendry { 82465963Spendry 82565963Spendry return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 82665963Spendry ap->a_cred, ap->a_p)); 82765963Spendry } 82865963Spendry 82965963Spendry int 83065963Spendry union_fsync(ap) 83165963Spendry struct vop_fsync_args /* { 83265963Spendry struct vnode *a_vp; 83365963Spendry struct ucred *a_cred; 83465963Spendry int a_waitfor; 83565963Spendry struct proc *a_p; 83665963Spendry } */ *ap; 83765963Spendry { 83865963Spendry int error = 0; 83965963Spendry struct vnode *targetvp = OTHERVP(ap->a_vp); 84065963Spendry 84167076Spendry if (targetvp != NULLVP) { 84266051Spendry int dolock = (targetvp == LOWERVP(ap->a_vp)); 84366051Spendry 84466051Spendry if (dolock) 84566051Spendry VOP_LOCK(targetvp); 84666152Spendry else 84766152Spendry FIXUP(VTOUNION(ap->a_vp)); 84865963Spendry error = VOP_FSYNC(targetvp, ap->a_cred, 84965963Spendry ap->a_waitfor, ap->a_p); 85066051Spendry if (dolock) 85166051Spendry VOP_UNLOCK(targetvp); 85265963Spendry } 85365963Spendry 85465963Spendry return (error); 85565963Spendry } 85665963Spendry 85765963Spendry int 85865963Spendry union_seek(ap) 85965963Spendry struct vop_seek_args /* { 86065963Spendry struct vnode *a_vp; 86165963Spendry off_t a_oldoff; 86265963Spendry off_t a_newoff; 86365963Spendry struct ucred *a_cred; 86465963Spendry } */ *ap; 86565963Spendry { 86665963Spendry 86765963Spendry return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 86865963Spendry } 86965963Spendry 87065963Spendry int 87165963Spendry union_remove(ap) 87265963Spendry struct vop_remove_args /* { 87365963Spendry struct vnode *a_dvp; 87465963Spendry struct vnode *a_vp; 87565963Spendry struct componentname *a_cnp; 87665963Spendry } */ *ap; 87765963Spendry { 87865963Spendry int error; 87965963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 88065963Spendry struct union_node *un = VTOUNION(ap->a_vp); 88165963Spendry 88267575Spendry if (dun->un_uppervp == NULLVP) 88367575Spendry panic("union remove: null upper vnode"); 88467575Spendry 88567575Spendry if (un->un_uppervp != NULLVP) { 88665963Spendry struct vnode *dvp = dun->un_uppervp; 88765963Spendry struct vnode *vp = un->un_uppervp; 88867784Spendry struct componentname *cnp = ap->a_cnp; 88965963Spendry 89066152Spendry FIXUP(dun); 89165963Spendry VREF(dvp); 89266051Spendry dun->un_flags |= UN_KLOCK; 89365963Spendry vput(ap->a_dvp); 89466152Spendry FIXUP(un); 89565963Spendry VREF(vp); 89666051Spendry un->un_flags |= UN_KLOCK; 89765963Spendry vput(ap->a_vp); 89865963Spendry 89967784Spendry if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 90067784Spendry cnp->cn_flags |= DOWHITEOUT; 90167784Spendry error = VOP_REMOVE(dvp, vp, cnp); 90266027Spendry if (!error) 90366027Spendry union_removed_upper(un); 90465963Spendry } else { 90567575Spendry FIXUP(dun); 90667575Spendry error = union_mkwhiteout( 90767575Spendry MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 90867575Spendry dun->un_uppervp, ap->a_cnp, un->un_path); 90965963Spendry vput(ap->a_dvp); 91065963Spendry vput(ap->a_vp); 91165963Spendry } 91265963Spendry 91365963Spendry return (error); 91465963Spendry } 91565963Spendry 91665963Spendry int 91765963Spendry union_link(ap) 91865963Spendry struct vop_link_args /* { 91965963Spendry struct vnode *a_vp; 92065963Spendry struct vnode *a_tdvp; 92165963Spendry struct componentname *a_cnp; 92265963Spendry } */ *ap; 92365963Spendry { 92467169Spendry int error = 0; 92567169Spendry struct union_node *un; 92667169Spendry struct vnode *vp; 92767169Spendry struct vnode *tdvp; 92865963Spendry 92968535Smckusick un = VTOUNION(ap->a_tdvp); 93065963Spendry 93168535Smckusick if (ap->a_tdvp->v_op != ap->a_vp->v_op) { 93268535Smckusick vp = ap->a_vp; 93367169Spendry } else { 93468535Smckusick struct union_node *tun = VTOUNION(ap->a_vp); 93568535Smckusick if (tun->un_uppervp == NULLVP) { 93668535Smckusick VOP_LOCK(ap->a_vp); 93768535Smckusick if (un->un_uppervp == tun->un_dirvp) { 93867169Spendry un->un_flags &= ~UN_ULOCK; 93967169Spendry VOP_UNLOCK(un->un_uppervp); 94067169Spendry } 94168535Smckusick error = union_copyup(tun, 1, ap->a_cnp->cn_cred, 94267169Spendry ap->a_cnp->cn_proc); 94368535Smckusick if (un->un_uppervp == tun->un_dirvp) { 94467169Spendry VOP_LOCK(un->un_uppervp); 94567169Spendry un->un_flags |= UN_ULOCK; 94667169Spendry } 94768535Smckusick VOP_UNLOCK(ap->a_vp); 94867169Spendry } 94968535Smckusick vp = tun->un_uppervp; 95067169Spendry } 95165963Spendry 95268535Smckusick tdvp = un->un_uppervp; 95368535Smckusick if (tdvp == NULLVP) 95467169Spendry error = EROFS; 95567169Spendry 95667169Spendry if (error) { 95768535Smckusick vput(ap->a_tdvp); 95867169Spendry return (error); 95965963Spendry } 96065963Spendry 96167169Spendry FIXUP(un); 96268535Smckusick VREF(tdvp); 96367169Spendry un->un_flags |= UN_KLOCK; 96468535Smckusick vput(ap->a_tdvp); 96567169Spendry 96667169Spendry return (VOP_LINK(vp, tdvp, ap->a_cnp)); 96765963Spendry } 96865963Spendry 96965963Spendry int 97065963Spendry union_rename(ap) 97165963Spendry struct vop_rename_args /* { 97265963Spendry struct vnode *a_fdvp; 97365963Spendry struct vnode *a_fvp; 97465963Spendry struct componentname *a_fcnp; 97565963Spendry struct vnode *a_tdvp; 97665963Spendry struct vnode *a_tvp; 97765963Spendry struct componentname *a_tcnp; 97865963Spendry } */ *ap; 97965963Spendry { 98065963Spendry int error; 98165963Spendry 98265963Spendry struct vnode *fdvp = ap->a_fdvp; 98365963Spendry struct vnode *fvp = ap->a_fvp; 98465963Spendry struct vnode *tdvp = ap->a_tdvp; 98565963Spendry struct vnode *tvp = ap->a_tvp; 98665963Spendry 98765963Spendry if (fdvp->v_op == union_vnodeop_p) { /* always true */ 98865963Spendry struct union_node *un = VTOUNION(fdvp); 98965997Spendry if (un->un_uppervp == NULLVP) { 99067575Spendry /* 99167575Spendry * this should never happen in normal 99267575Spendry * operation but might if there was 99367575Spendry * a problem creating the top-level shadow 99467575Spendry * directory. 99567575Spendry */ 99667575Spendry error = EXDEV; 99765963Spendry goto bad; 99865963Spendry } 99965963Spendry 100065963Spendry fdvp = un->un_uppervp; 100165963Spendry VREF(fdvp); 100265963Spendry vrele(ap->a_fdvp); 100365963Spendry } 100465963Spendry 100565963Spendry if (fvp->v_op == union_vnodeop_p) { /* always true */ 100665963Spendry struct union_node *un = VTOUNION(fvp); 100765997Spendry if (un->un_uppervp == NULLVP) { 100867575Spendry /* XXX: should do a copyup */ 100967575Spendry error = EXDEV; 101065963Spendry goto bad; 101165963Spendry } 101265963Spendry 101367575Spendry if (un->un_lowervp != NULLVP) 101467575Spendry ap->a_fcnp->cn_flags |= DOWHITEOUT; 101567575Spendry 101665963Spendry fvp = un->un_uppervp; 101765963Spendry VREF(fvp); 101865963Spendry vrele(ap->a_fvp); 101965963Spendry } 102065963Spendry 102165963Spendry if (tdvp->v_op == union_vnodeop_p) { 102265963Spendry struct union_node *un = VTOUNION(tdvp); 102365997Spendry if (un->un_uppervp == NULLVP) { 102467076Spendry /* 102567076Spendry * this should never happen in normal 102667076Spendry * operation but might if there was 102767076Spendry * a problem creating the top-level shadow 102867076Spendry * directory. 102967076Spendry */ 103067575Spendry error = EXDEV; 103165963Spendry goto bad; 103265963Spendry } 103365963Spendry 103465963Spendry tdvp = un->un_uppervp; 103565963Spendry VREF(tdvp); 103666051Spendry un->un_flags |= UN_KLOCK; 103765997Spendry vput(ap->a_tdvp); 103865963Spendry } 103965963Spendry 104067076Spendry if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 104165963Spendry struct union_node *un = VTOUNION(tvp); 104265963Spendry 104365963Spendry tvp = un->un_uppervp; 104467076Spendry if (tvp != NULLVP) { 104567076Spendry VREF(tvp); 104667076Spendry un->un_flags |= UN_KLOCK; 104767076Spendry } 104865963Spendry vput(ap->a_tvp); 104965963Spendry } 105065963Spendry 105165963Spendry return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 105265963Spendry 105365963Spendry bad: 105465963Spendry vrele(fdvp); 105565963Spendry vrele(fvp); 105665963Spendry vput(tdvp); 105767076Spendry if (tvp != NULLVP) 105865963Spendry vput(tvp); 105965963Spendry 106065963Spendry return (error); 106165963Spendry } 106265963Spendry 106365963Spendry int 106465963Spendry union_mkdir(ap) 106565963Spendry struct vop_mkdir_args /* { 106665963Spendry struct vnode *a_dvp; 106765963Spendry struct vnode **a_vpp; 106865963Spendry struct componentname *a_cnp; 106965963Spendry struct vattr *a_vap; 107065963Spendry } */ *ap; 107165963Spendry { 107265963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 107365963Spendry struct vnode *dvp = un->un_uppervp; 107465963Spendry 107567076Spendry if (dvp != NULLVP) { 107665963Spendry int error; 107765963Spendry struct vnode *vp; 107865963Spendry 107966152Spendry FIXUP(un); 108065963Spendry VREF(dvp); 108166051Spendry un->un_flags |= UN_KLOCK; 108268078Spendry VOP_UNLOCK(ap->a_dvp); 108365963Spendry error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 108468078Spendry if (error) { 108568078Spendry vrele(ap->a_dvp); 108665963Spendry return (error); 108768078Spendry } 108865963Spendry 108965963Spendry error = union_allocvp( 109065963Spendry ap->a_vpp, 109165989Spendry ap->a_dvp->v_mount, 109265989Spendry ap->a_dvp, 109365965Spendry NULLVP, 109465963Spendry ap->a_cnp, 109565963Spendry vp, 109668078Spendry NULLVP, 109768078Spendry 1); 109868078Spendry vrele(ap->a_dvp); 109965965Spendry if (error) 110066051Spendry vput(vp); 110165963Spendry return (error); 110265963Spendry } 110365963Spendry 110465963Spendry vput(ap->a_dvp); 110565963Spendry return (EROFS); 110665963Spendry } 110765963Spendry 110865963Spendry int 110965963Spendry union_rmdir(ap) 111065963Spendry struct vop_rmdir_args /* { 111165963Spendry struct vnode *a_dvp; 111265963Spendry struct vnode *a_vp; 111365963Spendry struct componentname *a_cnp; 111465963Spendry } */ *ap; 111565963Spendry { 111665963Spendry int error; 111765963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 111865963Spendry struct union_node *un = VTOUNION(ap->a_vp); 111965963Spendry 112067575Spendry if (dun->un_uppervp == NULLVP) 112167575Spendry panic("union rmdir: null upper vnode"); 112267575Spendry 112367575Spendry if (un->un_uppervp != NULLVP) { 112465963Spendry struct vnode *dvp = dun->un_uppervp; 112565963Spendry struct vnode *vp = un->un_uppervp; 112667784Spendry struct componentname *cnp = ap->a_cnp; 112765963Spendry 112866152Spendry FIXUP(dun); 112965963Spendry VREF(dvp); 113066051Spendry dun->un_flags |= UN_KLOCK; 113165963Spendry vput(ap->a_dvp); 113266152Spendry FIXUP(un); 113365963Spendry VREF(vp); 113466051Spendry un->un_flags |= UN_KLOCK; 113565963Spendry vput(ap->a_vp); 113665963Spendry 113767784Spendry if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc)) 113867784Spendry cnp->cn_flags |= DOWHITEOUT; 113966051Spendry error = VOP_RMDIR(dvp, vp, ap->a_cnp); 114066027Spendry if (!error) 114166027Spendry union_removed_upper(un); 114265963Spendry } else { 114367575Spendry FIXUP(dun); 114467575Spendry error = union_mkwhiteout( 114567575Spendry MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount), 114667575Spendry dun->un_uppervp, ap->a_cnp, un->un_path); 114765963Spendry vput(ap->a_dvp); 114865963Spendry vput(ap->a_vp); 114965963Spendry } 115065963Spendry 115165963Spendry return (error); 115265963Spendry } 115365963Spendry 115465963Spendry int 115565963Spendry union_symlink(ap) 115665963Spendry struct vop_symlink_args /* { 115765963Spendry struct vnode *a_dvp; 115865963Spendry struct vnode **a_vpp; 115965963Spendry struct componentname *a_cnp; 116065963Spendry struct vattr *a_vap; 116165963Spendry char *a_target; 116265963Spendry } */ *ap; 116365963Spendry { 116465963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 116565963Spendry struct vnode *dvp = un->un_uppervp; 116665963Spendry 116767076Spendry if (dvp != NULLVP) { 116865963Spendry int error; 116965963Spendry struct vnode *vp; 117065963Spendry struct mount *mp = ap->a_dvp->v_mount; 117165963Spendry 117266152Spendry FIXUP(un); 117365963Spendry VREF(dvp); 117466051Spendry un->un_flags |= UN_KLOCK; 117565963Spendry vput(ap->a_dvp); 117665963Spendry error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 117765963Spendry ap->a_vap, ap->a_target); 117865997Spendry *ap->a_vpp = NULLVP; 117965963Spendry return (error); 118065963Spendry } 118165963Spendry 118265963Spendry vput(ap->a_dvp); 118365963Spendry return (EROFS); 118465963Spendry } 118565963Spendry 118665935Spendry /* 118765935Spendry * union_readdir works in concert with getdirentries and 118865935Spendry * readdir(3) to provide a list of entries in the unioned 118965935Spendry * directories. getdirentries is responsible for walking 119065935Spendry * down the union stack. readdir(3) is responsible for 119165935Spendry * eliminating duplicate names from the returned data stream. 119265935Spendry */ 119365935Spendry int 119465935Spendry union_readdir(ap) 119565935Spendry struct vop_readdir_args /* { 119665935Spendry struct vnodeop_desc *a_desc; 119765935Spendry struct vnode *a_vp; 119865935Spendry struct uio *a_uio; 119965935Spendry struct ucred *a_cred; 120067369Smckusick int *a_eofflag; 120167369Smckusick u_long *a_cookies; 120267369Smckusick int a_ncookies; 120365935Spendry } */ *ap; 120465935Spendry { 120567369Smckusick register struct union_node *un = VTOUNION(ap->a_vp); 120667369Smckusick register struct vnode *uvp = un->un_uppervp; 120765935Spendry 120867369Smckusick if (uvp == NULLVP) 120967369Smckusick return (0); 121065935Spendry 121167369Smckusick FIXUP(un); 121267369Smckusick ap->a_vp = uvp; 121367369Smckusick return (VOCALL(uvp->v_op, VOFFSET(vop_readdir), ap)); 121465935Spendry } 121565935Spendry 121665935Spendry int 121765963Spendry union_readlink(ap) 121865963Spendry struct vop_readlink_args /* { 121965963Spendry struct vnode *a_vp; 122065963Spendry struct uio *a_uio; 122165963Spendry struct ucred *a_cred; 122265963Spendry } */ *ap; 122365963Spendry { 122465963Spendry int error; 122565963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 122666051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 122765963Spendry 122866051Spendry if (dolock) 122966051Spendry VOP_LOCK(vp); 123066152Spendry else 123166152Spendry FIXUP(VTOUNION(ap->a_vp)); 123265963Spendry error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 123366051Spendry if (dolock) 123466051Spendry VOP_UNLOCK(vp); 123565963Spendry 123665963Spendry return (error); 123765963Spendry } 123865963Spendry 123965963Spendry int 124065963Spendry union_abortop(ap) 124165963Spendry struct vop_abortop_args /* { 124265963Spendry struct vnode *a_dvp; 124365963Spendry struct componentname *a_cnp; 124465963Spendry } */ *ap; 124565963Spendry { 124665963Spendry int error; 124765965Spendry struct vnode *vp = OTHERVP(ap->a_dvp); 124865963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 124965963Spendry int islocked = un->un_flags & UN_LOCKED; 125066051Spendry int dolock = (vp == LOWERVP(ap->a_dvp)); 125165963Spendry 125266152Spendry if (islocked) { 125366152Spendry if (dolock) 125466152Spendry VOP_LOCK(vp); 125566152Spendry else 125666152Spendry FIXUP(VTOUNION(ap->a_dvp)); 125766152Spendry } 125865963Spendry error = VOP_ABORTOP(vp, ap->a_cnp); 125966051Spendry if (islocked && dolock) 126065963Spendry VOP_UNLOCK(vp); 126165963Spendry 126265963Spendry return (error); 126365963Spendry } 126465963Spendry 126565963Spendry int 126665935Spendry union_inactive(ap) 126765935Spendry struct vop_inactive_args /* { 126865935Spendry struct vnode *a_vp; 126965935Spendry } */ *ap; 127065935Spendry { 127167073Spendry struct union_node *un = VTOUNION(ap->a_vp); 127268078Spendry struct vnode **vpp; 127365935Spendry 127465935Spendry /* 127565935Spendry * Do nothing (and _don't_ bypass). 127665935Spendry * Wait to vrele lowervp until reclaim, 127765935Spendry * so that until then our union_node is in the 127865935Spendry * cache and reusable. 127965935Spendry * 128065935Spendry * NEEDSWORK: Someday, consider inactive'ing 128165935Spendry * the lowervp and then trying to reactivate it 128265935Spendry * with capabilities (v_id) 128365935Spendry * like they do in the name lookup cache code. 128465935Spendry * That's too much work for now. 128565935Spendry */ 128665989Spendry 128766027Spendry #ifdef UNION_DIAGNOSTIC 128865989Spendry if (un->un_flags & UN_LOCKED) 128965989Spendry panic("union: inactivating locked node"); 129067073Spendry if (un->un_flags & UN_ULOCK) 129167073Spendry panic("union: inactivating w/locked upper node"); 129265989Spendry #endif 129365989Spendry 129468078Spendry if (un->un_dircache != 0) { 129568078Spendry for (vpp = un->un_dircache; *vpp != NULLVP; vpp++) 129668078Spendry vrele(*vpp); 129768078Spendry free(un->un_dircache, M_TEMP); 129868078Spendry un->un_dircache = 0; 129968078Spendry } 130068078Spendry 130167073Spendry if ((un->un_flags & UN_CACHED) == 0) 1302*69389Spendry vgone(ap->a_vp); 130367073Spendry 130465935Spendry return (0); 130565935Spendry } 130665935Spendry 130765935Spendry int 130865935Spendry union_reclaim(ap) 130965935Spendry struct vop_reclaim_args /* { 131065935Spendry struct vnode *a_vp; 131165935Spendry } */ *ap; 131265935Spendry { 131365935Spendry 131466053Spendry union_freevp(ap->a_vp); 131566053Spendry 131665935Spendry return (0); 131765935Spendry } 131865935Spendry 131965963Spendry int 132065963Spendry union_lock(ap) 132165963Spendry struct vop_lock_args *ap; 132265963Spendry { 132366149Spendry struct vnode *vp = ap->a_vp; 132466149Spendry struct union_node *un; 132565935Spendry 132666149Spendry start: 132766149Spendry while (vp->v_flag & VXLOCK) { 132866149Spendry vp->v_flag |= VXWANT; 132966149Spendry sleep((caddr_t)vp, PINOD); 133066149Spendry } 133166149Spendry 133266149Spendry un = VTOUNION(vp); 133366149Spendry 133467076Spendry if (un->un_uppervp != NULLVP) { 133567230Spendry if (((un->un_flags & UN_ULOCK) == 0) && 133667230Spendry (vp->v_usecount != 0)) { 133768590Spendry VOP_LOCK(un->un_uppervp); 133866149Spendry un->un_flags |= UN_ULOCK; 133966051Spendry } 134066051Spendry #ifdef DIAGNOSTIC 1341*69389Spendry if (un->un_flags & UN_KLOCK) { 134268590Spendry vprint("union: dangling klock", vp); 134368590Spendry panic("union: dangling upper lock (%lx)", vp); 1344*69389Spendry } 134566051Spendry #endif 134666051Spendry } 134766051Spendry 134866149Spendry if (un->un_flags & UN_LOCKED) { 134965963Spendry #ifdef DIAGNOSTIC 135065989Spendry if (curproc && un->un_pid == curproc->p_pid && 135165989Spendry un->un_pid > -1 && curproc->p_pid > -1) 135265989Spendry panic("union: locking against myself"); 135365963Spendry #endif 135465963Spendry un->un_flags |= UN_WANT; 135565963Spendry sleep((caddr_t) &un->un_flags, PINOD); 135666149Spendry goto start; 135765963Spendry } 135865989Spendry 135965963Spendry #ifdef DIAGNOSTIC 136065989Spendry if (curproc) 136165989Spendry un->un_pid = curproc->p_pid; 136265989Spendry else 136365989Spendry un->un_pid = -1; 136465963Spendry #endif 136566028Spendry 136666149Spendry un->un_flags |= UN_LOCKED; 136766028Spendry return (0); 136865963Spendry } 136965963Spendry 137068590Spendry /* 137168590Spendry * When operations want to vput() a union node yet retain a lock on 137268590Spendry * the upper vnode (say, to do some further operations like link(), 137368590Spendry * mkdir(), ...), they set UN_KLOCK on the union node, then call 137468590Spendry * vput() which calls VOP_UNLOCK() and comes here. union_unlock() 137568590Spendry * unlocks the union node (leaving the upper vnode alone), clears the 137668590Spendry * KLOCK flag, and then returns to vput(). The caller then does whatever 137768590Spendry * is left to do with the upper vnode, and ensures that it gets unlocked. 137868590Spendry * 137968590Spendry * If UN_KLOCK isn't set, then the upper vnode is unlocked here. 138068590Spendry */ 138165935Spendry int 138265963Spendry union_unlock(ap) 138365963Spendry struct vop_lock_args *ap; 138465963Spendry { 138565963Spendry struct union_node *un = VTOUNION(ap->a_vp); 138665963Spendry 138765963Spendry #ifdef DIAGNOSTIC 138865965Spendry if ((un->un_flags & UN_LOCKED) == 0) 138965965Spendry panic("union: unlock unlocked node"); 139065989Spendry if (curproc && un->un_pid != curproc->p_pid && 139165989Spendry curproc->p_pid > -1 && un->un_pid > -1) 139265963Spendry panic("union: unlocking other process's union node"); 139365963Spendry #endif 139465963Spendry 139565963Spendry un->un_flags &= ~UN_LOCKED; 139666051Spendry 139766051Spendry if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 139866051Spendry VOP_UNLOCK(un->un_uppervp); 139966051Spendry 140066051Spendry un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 140166051Spendry 140265963Spendry if (un->un_flags & UN_WANT) { 140365963Spendry un->un_flags &= ~UN_WANT; 140465963Spendry wakeup((caddr_t) &un->un_flags); 140565963Spendry } 140665963Spendry 140765963Spendry #ifdef DIAGNOSTIC 140865963Spendry un->un_pid = 0; 140965963Spendry #endif 141066028Spendry 141166028Spendry return (0); 141265963Spendry } 141365963Spendry 141465963Spendry int 141565963Spendry union_bmap(ap) 141665963Spendry struct vop_bmap_args /* { 141765963Spendry struct vnode *a_vp; 141865963Spendry daddr_t a_bn; 141965963Spendry struct vnode **a_vpp; 142065963Spendry daddr_t *a_bnp; 142165963Spendry int *a_runp; 142265963Spendry } */ *ap; 142365963Spendry { 142465963Spendry int error; 142565963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 142666051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 142765963Spendry 142866051Spendry if (dolock) 142966051Spendry VOP_LOCK(vp); 143066152Spendry else 143166152Spendry FIXUP(VTOUNION(ap->a_vp)); 143265963Spendry error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 143366051Spendry if (dolock) 143466051Spendry VOP_UNLOCK(vp); 143565963Spendry 143665963Spendry return (error); 143765963Spendry } 143865963Spendry 143965963Spendry int 144065935Spendry union_print(ap) 144165935Spendry struct vop_print_args /* { 144265935Spendry struct vnode *a_vp; 144365935Spendry } */ *ap; 144465935Spendry { 144565935Spendry struct vnode *vp = ap->a_vp; 144665935Spendry 144765935Spendry printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 144865935Spendry vp, UPPERVP(vp), LOWERVP(vp)); 144968590Spendry if (UPPERVP(vp) != NULLVP) 145068590Spendry vprint("union: upper", UPPERVP(vp)); 145168590Spendry if (LOWERVP(vp) != NULLVP) 145268590Spendry vprint("union: lower", LOWERVP(vp)); 145368590Spendry 145465935Spendry return (0); 145565935Spendry } 145665935Spendry 145765963Spendry int 145865963Spendry union_islocked(ap) 145965963Spendry struct vop_islocked_args /* { 146065963Spendry struct vnode *a_vp; 146165963Spendry } */ *ap; 146265963Spendry { 146365935Spendry 146465963Spendry return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 146565963Spendry } 146665963Spendry 146765935Spendry int 146865963Spendry union_pathconf(ap) 146965963Spendry struct vop_pathconf_args /* { 147065963Spendry struct vnode *a_vp; 147165963Spendry int a_name; 147265963Spendry int *a_retval; 147365935Spendry } */ *ap; 147465935Spendry { 147565935Spendry int error; 147665963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 147766051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 147865935Spendry 147966051Spendry if (dolock) 148066051Spendry VOP_LOCK(vp); 148166152Spendry else 148266152Spendry FIXUP(VTOUNION(ap->a_vp)); 148365963Spendry error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 148466051Spendry if (dolock) 148566051Spendry VOP_UNLOCK(vp); 148665935Spendry 148765963Spendry return (error); 148865963Spendry } 148965935Spendry 149065963Spendry int 149165963Spendry union_advlock(ap) 149265963Spendry struct vop_advlock_args /* { 149365963Spendry struct vnode *a_vp; 149465963Spendry caddr_t a_id; 149565963Spendry int a_op; 149665963Spendry struct flock *a_fl; 149765963Spendry int a_flags; 149865963Spendry } */ *ap; 149965963Spendry { 150065935Spendry 150165963Spendry return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 150265963Spendry ap->a_fl, ap->a_flags)); 150365935Spendry } 150465935Spendry 150565935Spendry 150665935Spendry /* 150765963Spendry * XXX - vop_strategy must be hand coded because it has no 150865935Spendry * vnode in its arguments. 150965935Spendry * This goes away with a merged VM/buffer cache. 151065935Spendry */ 151165935Spendry int 151265963Spendry union_strategy(ap) 151365963Spendry struct vop_strategy_args /* { 151465935Spendry struct buf *a_bp; 151565935Spendry } */ *ap; 151665935Spendry { 151765935Spendry struct buf *bp = ap->a_bp; 151865935Spendry int error; 151965935Spendry struct vnode *savedvp; 152065935Spendry 152165935Spendry savedvp = bp->b_vp; 152265963Spendry bp->b_vp = OTHERVP(bp->b_vp); 152365935Spendry 152465935Spendry #ifdef DIAGNOSTIC 152565997Spendry if (bp->b_vp == NULLVP) 152665963Spendry panic("union_strategy: nil vp"); 152765963Spendry if (((bp->b_flags & B_READ) == 0) && 152865963Spendry (bp->b_vp == LOWERVP(savedvp))) 152965963Spendry panic("union_strategy: writing to lowervp"); 153065935Spendry #endif 153165935Spendry 153265963Spendry error = VOP_STRATEGY(bp); 153365935Spendry bp->b_vp = savedvp; 153465935Spendry 153565935Spendry return (error); 153665935Spendry } 153765935Spendry 153865935Spendry /* 153965935Spendry * Global vfs data structures 154065935Spendry */ 154165935Spendry int (**union_vnodeop_p)(); 154265965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = { 154365963Spendry { &vop_default_desc, vn_default_error }, 154465963Spendry { &vop_lookup_desc, union_lookup }, /* lookup */ 154565963Spendry { &vop_create_desc, union_create }, /* create */ 154667575Spendry { &vop_whiteout_desc, union_whiteout }, /* whiteout */ 154765963Spendry { &vop_mknod_desc, union_mknod }, /* mknod */ 154865963Spendry { &vop_open_desc, union_open }, /* open */ 154965963Spendry { &vop_close_desc, union_close }, /* close */ 155065963Spendry { &vop_access_desc, union_access }, /* access */ 155165963Spendry { &vop_getattr_desc, union_getattr }, /* getattr */ 155265963Spendry { &vop_setattr_desc, union_setattr }, /* setattr */ 155365963Spendry { &vop_read_desc, union_read }, /* read */ 155465963Spendry { &vop_write_desc, union_write }, /* write */ 155567751Spendry { &vop_lease_desc, union_lease }, /* lease */ 155665963Spendry { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 155765963Spendry { &vop_select_desc, union_select }, /* select */ 1558*69389Spendry { &vop_revoke_desc, union_revoke }, /* revoke */ 155965963Spendry { &vop_mmap_desc, union_mmap }, /* mmap */ 156065963Spendry { &vop_fsync_desc, union_fsync }, /* fsync */ 156165963Spendry { &vop_seek_desc, union_seek }, /* seek */ 156265963Spendry { &vop_remove_desc, union_remove }, /* remove */ 156365963Spendry { &vop_link_desc, union_link }, /* link */ 156465963Spendry { &vop_rename_desc, union_rename }, /* rename */ 156565963Spendry { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 156665963Spendry { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 156765963Spendry { &vop_symlink_desc, union_symlink }, /* symlink */ 156865963Spendry { &vop_readdir_desc, union_readdir }, /* readdir */ 156965963Spendry { &vop_readlink_desc, union_readlink }, /* readlink */ 157065963Spendry { &vop_abortop_desc, union_abortop }, /* abortop */ 157165963Spendry { &vop_inactive_desc, union_inactive }, /* inactive */ 157265963Spendry { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 157365963Spendry { &vop_lock_desc, union_lock }, /* lock */ 157465963Spendry { &vop_unlock_desc, union_unlock }, /* unlock */ 157565963Spendry { &vop_bmap_desc, union_bmap }, /* bmap */ 157665963Spendry { &vop_strategy_desc, union_strategy }, /* strategy */ 157765963Spendry { &vop_print_desc, union_print }, /* print */ 157865963Spendry { &vop_islocked_desc, union_islocked }, /* islocked */ 157965963Spendry { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 158065963Spendry { &vop_advlock_desc, union_advlock }, /* advlock */ 158165963Spendry #ifdef notdef 158265963Spendry { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 158365963Spendry { &vop_valloc_desc, union_valloc }, /* valloc */ 158465963Spendry { &vop_vfree_desc, union_vfree }, /* vfree */ 158565963Spendry { &vop_truncate_desc, union_truncate }, /* truncate */ 158665963Spendry { &vop_update_desc, union_update }, /* update */ 158765963Spendry { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 158865963Spendry #endif 158965935Spendry { (struct vnodeop_desc*)NULL, (int(*)())NULL } 159065935Spendry }; 159165935Spendry struct vnodeopv_desc union_vnodeop_opv_desc = 159265935Spendry { &union_vnodeop_p, union_vnodeop_entries }; 1593