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*67064Spendry * @(#)union_vnops.c 8.7 (Berkeley) 04/24/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> 1965935Spendry #include <sys/types.h> 2065935Spendry #include <sys/vnode.h> 2165935Spendry #include <sys/mount.h> 2265935Spendry #include <sys/namei.h> 2365935Spendry #include <sys/malloc.h> 2465935Spendry #include <sys/buf.h> 2566053Spendry #include <sys/queue.h> 2666055Spendry #include <miscfs/union/union.h> 2765935Spendry 2866152Spendry #define FIXUP(un) { \ 2966152Spendry if (((un)->un_flags & UN_ULOCK) == 0) { \ 3066152Spendry union_fixup(un); \ 3166152Spendry } \ 3266152Spendry } 3366152Spendry 3466152Spendry static void 3566152Spendry union_fixup(un) 3666152Spendry struct union_node *un; 3766152Spendry { 3866152Spendry 3966152Spendry VOP_LOCK(un->un_uppervp); 4066152Spendry un->un_flags |= UN_ULOCK; 4166152Spendry } 4266152Spendry 4365935Spendry static int 44*67064Spendry union_lookup1(udvp, dvpp, vpp, cnp) 4565989Spendry struct vnode *udvp; 46*67064Spendry struct vnode **dvpp; 4765935Spendry struct vnode **vpp; 4865935Spendry struct componentname *cnp; 4965935Spendry { 5065935Spendry int error; 5165935Spendry struct vnode *tdvp; 52*67064Spendry struct vnode *dvp; 5365935Spendry struct mount *mp; 5465935Spendry 55*67064Spendry dvp = *dvpp; 56*67064Spendry 5765994Spendry /* 5865994Spendry * If stepping up the directory tree, check for going 5965994Spendry * back across the mount point, in which case do what 6065994Spendry * lookup would do by stepping back down the mount 6165994Spendry * hierarchy. 6265994Spendry */ 6365935Spendry if (cnp->cn_flags & ISDOTDOT) { 64*67064Spendry while ((dvp != udvp) && (dvp->v_flag & VROOT)) { 6566034Spendry /* 6666034Spendry * Don't do the NOCROSSMOUNT check 6766034Spendry * at this level. By definition, 6866034Spendry * union fs deals with namespaces, not 6966034Spendry * filesystems. 7066034Spendry */ 7165935Spendry tdvp = dvp; 72*67064Spendry *dvpp = dvp = dvp->v_mount->mnt_vnodecovered; 7365935Spendry vput(tdvp); 7465935Spendry VREF(dvp); 7565935Spendry VOP_LOCK(dvp); 7665935Spendry } 7765935Spendry } 7866027Spendry 7965935Spendry error = VOP_LOOKUP(dvp, &tdvp, cnp); 8065935Spendry if (error) 8165935Spendry return (error); 8265935Spendry 8365994Spendry /* 8466027Spendry * The parent directory will have been unlocked, unless lookup 8566027Spendry * found the last component. In which case, re-lock the node 8666027Spendry * here to allow it to be unlocked again (phew) in union_lookup. 8765994Spendry */ 8866027Spendry if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN)) 8965994Spendry VOP_LOCK(dvp); 9065994Spendry 9165935Spendry dvp = tdvp; 9265994Spendry 9365994Spendry /* 9465994Spendry * Lastly check if the current node is a mount point in 9566034Spendry * which case walk up the mount hierarchy making sure not to 9665994Spendry * bump into the root of the mount tree (ie. dvp != udvp). 9765994Spendry */ 9865989Spendry while (dvp != udvp && (dvp->v_type == VDIR) && 9966034Spendry (mp = dvp->v_mountedhere)) { 10065935Spendry 10165935Spendry if (mp->mnt_flag & MNT_MLOCK) { 10265935Spendry mp->mnt_flag |= MNT_MWAIT; 10365935Spendry sleep((caddr_t) mp, PVFS); 10465935Spendry continue; 10565935Spendry } 10665935Spendry 10765935Spendry if (error = VFS_ROOT(mp, &tdvp)) { 10865935Spendry vput(dvp); 10965935Spendry return (error); 11065935Spendry } 11165935Spendry 11265965Spendry vput(dvp); 11365935Spendry dvp = tdvp; 11465935Spendry } 11565935Spendry 11665935Spendry *vpp = dvp; 11765935Spendry return (0); 11865935Spendry } 11965935Spendry 12065935Spendry int 12165935Spendry union_lookup(ap) 12265935Spendry struct vop_lookup_args /* { 12365935Spendry struct vnodeop_desc *a_desc; 12465935Spendry struct vnode *a_dvp; 12565935Spendry struct vnode **a_vpp; 12665935Spendry struct componentname *a_cnp; 12765935Spendry } */ *ap; 12865935Spendry { 12965965Spendry int error; 13065935Spendry int uerror, lerror; 13165935Spendry struct vnode *uppervp, *lowervp; 13265935Spendry struct vnode *upperdvp, *lowerdvp; 13365935Spendry struct vnode *dvp = ap->a_dvp; 13465989Spendry struct union_node *dun = VTOUNION(dvp); 13565935Spendry struct componentname *cnp = ap->a_cnp; 13665935Spendry int lockparent = cnp->cn_flags & LOCKPARENT; 13765994Spendry int rdonly = cnp->cn_flags & RDONLY; 13865997Spendry struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); 13966152Spendry struct ucred *saved_cred; 14065935Spendry 14165965Spendry cnp->cn_flags |= LOCKPARENT; 14265965Spendry 14365935Spendry upperdvp = dun->un_uppervp; 14465935Spendry lowerdvp = dun->un_lowervp; 14565997Spendry uppervp = NULLVP; 14665997Spendry lowervp = NULLVP; 14765935Spendry 14865935Spendry /* 14965935Spendry * do the lookup in the upper level. 15065935Spendry * if that level comsumes additional pathnames, 15165935Spendry * then assume that something special is going 15265935Spendry * on and just return that vnode. 15365935Spendry */ 15465935Spendry if (upperdvp) { 15566152Spendry FIXUP(dun); 156*67064Spendry uerror = union_lookup1(um->um_uppervp, &upperdvp, 15765997Spendry &uppervp, cnp); 15866051Spendry /*if (uppervp == upperdvp) 15966051Spendry dun->un_flags |= UN_KLOCK;*/ 16065965Spendry 16165935Spendry if (cnp->cn_consume != 0) { 16265935Spendry *ap->a_vpp = uppervp; 16365965Spendry if (!lockparent) 16465965Spendry cnp->cn_flags &= ~LOCKPARENT; 16565935Spendry return (uerror); 16665935Spendry } 16765935Spendry } else { 16865935Spendry uerror = ENOENT; 16965935Spendry } 17065935Spendry 17165935Spendry /* 17265935Spendry * in a similar way to the upper layer, do the lookup 17365935Spendry * in the lower layer. this time, if there is some 17465935Spendry * component magic going on, then vput whatever we got 17565935Spendry * back from the upper layer and return the lower vnode 17665935Spendry * instead. 17765935Spendry */ 17865935Spendry if (lowerdvp) { 17966051Spendry int nameiop; 18066051Spendry 18165965Spendry VOP_LOCK(lowerdvp); 18266051Spendry 18366051Spendry /* 18466051Spendry * Only do a LOOKUP on the bottom node, since 18566051Spendry * we won't be making changes to it anyway. 18666051Spendry */ 18766051Spendry nameiop = cnp->cn_nameiop; 18866051Spendry cnp->cn_nameiop = LOOKUP; 18966152Spendry if (um->um_op == UNMNT_BELOW) { 19066152Spendry saved_cred = cnp->cn_cred; 19166152Spendry cnp->cn_cred = um->um_cred; 19266152Spendry } 193*67064Spendry lerror = union_lookup1(um->um_lowervp, &lowerdvp, 19466051Spendry &lowervp, cnp); 19566152Spendry if (um->um_op == UNMNT_BELOW) 19666152Spendry cnp->cn_cred = saved_cred; 19766051Spendry cnp->cn_nameiop = nameiop; 19866051Spendry 19965989Spendry if (lowervp != lowerdvp) 20065989Spendry VOP_UNLOCK(lowerdvp); 20165965Spendry 20265935Spendry if (cnp->cn_consume != 0) { 20365935Spendry if (uppervp) { 20466051Spendry if (uppervp == upperdvp) 20566051Spendry vrele(uppervp); 20666051Spendry else 20766051Spendry vput(uppervp); 20865997Spendry uppervp = NULLVP; 20965935Spendry } 21065935Spendry *ap->a_vpp = lowervp; 21165965Spendry if (!lockparent) 21265965Spendry cnp->cn_flags &= ~LOCKPARENT; 21365935Spendry return (lerror); 21465935Spendry } 21565935Spendry } else { 21665935Spendry lerror = ENOENT; 21765935Spendry } 21865935Spendry 21965965Spendry if (!lockparent) 22065965Spendry cnp->cn_flags &= ~LOCKPARENT; 22165965Spendry 22265935Spendry /* 22365935Spendry * at this point, we have uerror and lerror indicating 22465935Spendry * possible errors with the lookups in the upper and lower 22565935Spendry * layers. additionally, uppervp and lowervp are (locked) 22665935Spendry * references to existing vnodes in the upper and lower layers. 22765935Spendry * 22865935Spendry * there are now three cases to consider. 22965935Spendry * 1. if both layers returned an error, then return whatever 23065935Spendry * error the upper layer generated. 23165935Spendry * 23265935Spendry * 2. if the top layer failed and the bottom layer succeeded 23365935Spendry * then two subcases occur. 23465935Spendry * a. the bottom vnode is not a directory, in which 23565935Spendry * case just return a new union vnode referencing 23665935Spendry * an empty top layer and the existing bottom layer. 23765935Spendry * b. the bottom vnode is a directory, in which case 23865935Spendry * create a new directory in the top-level and 23965935Spendry * continue as in case 3. 24065935Spendry * 24165935Spendry * 3. if the top layer succeeded then return a new union 24265935Spendry * vnode referencing whatever the new top layer and 24365935Spendry * whatever the bottom layer returned. 24465935Spendry */ 24565935Spendry 24666027Spendry *ap->a_vpp = NULLVP; 24766027Spendry 24865935Spendry /* case 1. */ 24965935Spendry if ((uerror != 0) && (lerror != 0)) { 25065935Spendry return (uerror); 25165935Spendry } 25265935Spendry 25365935Spendry /* case 2. */ 25465935Spendry if (uerror != 0 /* && (lerror == 0) */ ) { 25565935Spendry if (lowervp->v_type == VDIR) { /* case 2b. */ 25666051Spendry dun->un_flags &= ~UN_ULOCK; 25766051Spendry VOP_UNLOCK(upperdvp); 25865997Spendry uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 25966051Spendry VOP_LOCK(upperdvp); 26066051Spendry dun->un_flags |= UN_ULOCK; 26166051Spendry 26265935Spendry if (uerror) { 26365935Spendry if (lowervp) { 26465935Spendry vput(lowervp); 26565997Spendry lowervp = NULLVP; 26665935Spendry } 26765935Spendry return (uerror); 26865935Spendry } 26965935Spendry } 27065935Spendry } 27165935Spendry 27265965Spendry if (lowervp) 27365965Spendry VOP_UNLOCK(lowervp); 27465965Spendry 27565997Spendry error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 27665997Spendry uppervp, lowervp); 27765997Spendry 27865965Spendry if (error) { 27965965Spendry if (uppervp) 28066051Spendry vput(uppervp); 28165965Spendry if (lowervp) 28265965Spendry vrele(lowervp); 28365965Spendry } else { 28465994Spendry if (*ap->a_vpp != dvp) 28565994Spendry if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 28665994Spendry VOP_UNLOCK(dvp); 28765965Spendry } 28865965Spendry 28965965Spendry return (error); 29065935Spendry } 29165935Spendry 29265963Spendry int 29365963Spendry union_create(ap) 29465963Spendry struct vop_create_args /* { 29565963Spendry struct vnode *a_dvp; 29665963Spendry struct vnode **a_vpp; 29765963Spendry struct componentname *a_cnp; 29865963Spendry struct vattr *a_vap; 29965963Spendry } */ *ap; 30065963Spendry { 30165963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 30265963Spendry struct vnode *dvp = un->un_uppervp; 30365963Spendry 30465963Spendry if (dvp) { 30565963Spendry int error; 30665963Spendry struct vnode *vp; 30765963Spendry 30866152Spendry FIXUP(un); 30966152Spendry 31065963Spendry VREF(dvp); 31166051Spendry un->un_flags |= UN_KLOCK; 31265963Spendry vput(ap->a_dvp); 31365963Spendry error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap); 31465963Spendry if (error) 31565963Spendry return (error); 31665963Spendry 31765963Spendry error = union_allocvp( 31865963Spendry ap->a_vpp, 31965989Spendry ap->a_dvp->v_mount, 32065989Spendry ap->a_dvp, 32165965Spendry NULLVP, 32265963Spendry ap->a_cnp, 32365963Spendry vp, 32465963Spendry NULLVP); 32565965Spendry if (error) 32666051Spendry vput(vp); 32765963Spendry return (error); 32865963Spendry } 32965963Spendry 33065963Spendry vput(ap->a_dvp); 33165963Spendry return (EROFS); 33265963Spendry } 33365963Spendry 33465963Spendry int 33565963Spendry union_mknod(ap) 33665963Spendry struct vop_mknod_args /* { 33765963Spendry struct vnode *a_dvp; 33865963Spendry struct vnode **a_vpp; 33965963Spendry struct componentname *a_cnp; 34065963Spendry struct vattr *a_vap; 34165963Spendry } */ *ap; 34265963Spendry { 34365963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 34465963Spendry struct vnode *dvp = un->un_uppervp; 34565963Spendry 34665963Spendry if (dvp) { 34765963Spendry int error; 34865963Spendry struct vnode *vp; 34965963Spendry 35066152Spendry FIXUP(un); 35166152Spendry 35265963Spendry VREF(dvp); 35366051Spendry un->un_flags |= UN_KLOCK; 35465963Spendry vput(ap->a_dvp); 35565963Spendry error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap); 35665963Spendry if (error) 35765963Spendry return (error); 35865963Spendry 35965965Spendry if (vp) { 36065965Spendry error = union_allocvp( 36165965Spendry ap->a_vpp, 36265989Spendry ap->a_dvp->v_mount, 36365989Spendry ap->a_dvp, 36465965Spendry NULLVP, 36565965Spendry ap->a_cnp, 36665965Spendry vp, 36765965Spendry NULLVP); 36865965Spendry if (error) 36966051Spendry vput(vp); 37065965Spendry } 37165963Spendry return (error); 37265963Spendry } 37365963Spendry 37465963Spendry vput(ap->a_dvp); 37565963Spendry return (EROFS); 37665963Spendry } 37765963Spendry 37865935Spendry int 37965935Spendry union_open(ap) 38065935Spendry struct vop_open_args /* { 38165935Spendry struct vnodeop_desc *a_desc; 38265935Spendry struct vnode *a_vp; 38365935Spendry int a_mode; 38465935Spendry struct ucred *a_cred; 38565935Spendry struct proc *a_p; 38665935Spendry } */ *ap; 38765935Spendry { 38865935Spendry struct union_node *un = VTOUNION(ap->a_vp); 38965965Spendry struct vnode *tvp; 39065935Spendry int mode = ap->a_mode; 39165935Spendry struct ucred *cred = ap->a_cred; 39265935Spendry struct proc *p = ap->a_p; 39365965Spendry int error; 39465935Spendry 39565935Spendry /* 39665935Spendry * If there is an existing upper vp then simply open that. 39765935Spendry */ 39865965Spendry tvp = un->un_uppervp; 39965965Spendry if (tvp == NULLVP) { 40065935Spendry /* 40165965Spendry * If the lower vnode is being opened for writing, then 40265965Spendry * copy the file contents to the upper vnode and open that, 40365965Spendry * otherwise can simply open the lower vnode. 40465935Spendry */ 40565965Spendry tvp = un->un_lowervp; 40665965Spendry if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 40765994Spendry struct vnode *vp; 40865997Spendry int i; 40965963Spendry 41065965Spendry /* 41165965Spendry * Open the named file in the upper layer. Note that 41265965Spendry * the file may have come into existence *since* the 41365965Spendry * lookup was done, since the upper layer may really 41465965Spendry * be a loopback mount of some other filesystem... 41565965Spendry * so open the file with exclusive create and barf if 41665965Spendry * it already exists. 41766027Spendry * XXX - perhaps should re-lookup the node (once more 41865965Spendry * with feeling) and simply open that. Who knows. 41965965Spendry */ 42065997Spendry error = union_vn_create(&vp, un, p); 42165965Spendry if (error) 42265965Spendry return (error); 42366051Spendry 42466051Spendry /* at this point, uppervp is locked */ 42566053Spendry union_newupper(un, vp); 42666051Spendry un->un_flags |= UN_ULOCK; 42765965Spendry 42865965Spendry /* 42965965Spendry * Now, if the file is being opened with truncation, 43065965Spendry * then the (new) upper vnode is ready to fly, 43165965Spendry * otherwise the data from the lower vnode must be 43265965Spendry * copied to the upper layer first. This only works 43365965Spendry * for regular files (check is made above). 43465965Spendry */ 43565965Spendry if ((mode & O_TRUNC) == 0) { 43665965Spendry /* 43765965Spendry * XXX - should not ignore errors 43865965Spendry * from VOP_CLOSE 43965965Spendry */ 44065994Spendry VOP_LOCK(tvp); 44165965Spendry error = VOP_OPEN(tvp, FREAD, cred, p); 44265965Spendry if (error == 0) { 44365965Spendry error = union_copyfile(p, cred, 44465965Spendry tvp, un->un_uppervp); 44565965Spendry VOP_UNLOCK(tvp); 44665965Spendry (void) VOP_CLOSE(tvp, FREAD); 44765965Spendry } else { 44865965Spendry VOP_UNLOCK(tvp); 44965965Spendry } 45066051Spendry 45166152Spendry #ifdef UNION_DIAGNOSTIC 45265997Spendry if (!error) 45366027Spendry uprintf("union: copied up %s\n", 45465997Spendry un->un_path); 45566152Spendry #endif 45665935Spendry } 45765997Spendry 45866057Spendry un->un_flags &= ~UN_ULOCK; 45966057Spendry VOP_UNLOCK(un->un_uppervp); 46066057Spendry union_vn_close(un->un_uppervp, FWRITE, cred, p); 46166057Spendry VOP_LOCK(un->un_uppervp); 46266057Spendry un->un_flags |= UN_ULOCK; 46366057Spendry 46465997Spendry /* 46565997Spendry * Subsequent IOs will go to the top layer, so 46665997Spendry * call close on the lower vnode and open on the 46765997Spendry * upper vnode to ensure that the filesystem keeps 46865997Spendry * its references counts right. This doesn't do 46965997Spendry * the right thing with (cred) and (FREAD) though. 47065997Spendry * Ignoring error returns is not righ, either. 47165997Spendry */ 47266027Spendry for (i = 0; i < un->un_openl; i++) { 47365997Spendry (void) VOP_CLOSE(tvp, FREAD); 47465997Spendry (void) VOP_OPEN(un->un_uppervp, FREAD, cred, p); 47565997Spendry } 47666027Spendry un->un_openl = 0; 47765997Spendry 47865965Spendry if (error == 0) 47965965Spendry error = VOP_OPEN(un->un_uppervp, mode, cred, p); 48065965Spendry return (error); 48165935Spendry } 48266051Spendry 48366051Spendry /* 48466051Spendry * Just open the lower vnode 48566051Spendry */ 48666027Spendry un->un_openl++; 48766051Spendry VOP_LOCK(tvp); 48866051Spendry error = VOP_OPEN(tvp, mode, cred, p); 48966051Spendry VOP_UNLOCK(tvp); 49066051Spendry 49166051Spendry return (error); 49265935Spendry } 49365935Spendry 49466152Spendry FIXUP(un); 49566152Spendry 49665965Spendry error = VOP_OPEN(tvp, mode, cred, p); 49765965Spendry 49865965Spendry return (error); 49965935Spendry } 50065935Spendry 50165963Spendry int 50265963Spendry union_close(ap) 50365963Spendry struct vop_close_args /* { 50465963Spendry struct vnode *a_vp; 50565963Spendry int a_fflag; 50665963Spendry struct ucred *a_cred; 50765963Spendry struct proc *a_p; 50865963Spendry } */ *ap; 50965963Spendry { 51065997Spendry struct union_node *un = VTOUNION(ap->a_vp); 51165997Spendry struct vnode *vp; 51265963Spendry 51365997Spendry if (un->un_uppervp) { 51465997Spendry vp = un->un_uppervp; 51565997Spendry } else { 51666027Spendry #ifdef UNION_DIAGNOSTIC 51766027Spendry if (un->un_openl <= 0) 51866027Spendry panic("union: un_openl cnt"); 51965997Spendry #endif 52066027Spendry --un->un_openl; 52165997Spendry vp = un->un_lowervp; 52265997Spendry } 52366027Spendry 52465997Spendry return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p)); 52565963Spendry } 52665963Spendry 52765935Spendry /* 52865963Spendry * Check access permission on the union vnode. 52965963Spendry * The access check being enforced is to check 53065963Spendry * against both the underlying vnode, and any 53165963Spendry * copied vnode. This ensures that no additional 53265963Spendry * file permissions are given away simply because 53365963Spendry * the user caused an implicit file copy. 53465963Spendry */ 53565963Spendry int 53665963Spendry union_access(ap) 53765963Spendry struct vop_access_args /* { 53865963Spendry struct vnodeop_desc *a_desc; 53965963Spendry struct vnode *a_vp; 54065963Spendry int a_mode; 54165963Spendry struct ucred *a_cred; 54265963Spendry struct proc *a_p; 54365963Spendry } */ *ap; 54465963Spendry { 54565963Spendry struct union_node *un = VTOUNION(ap->a_vp); 54666149Spendry int error = EACCES; 54765963Spendry struct vnode *vp; 54865963Spendry 54966152Spendry if (vp = un->un_uppervp) { 55066152Spendry FIXUP(un); 55166152Spendry return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); 55266152Spendry } 55366152Spendry 55465963Spendry if (vp = un->un_lowervp) { 55565965Spendry VOP_LOCK(vp); 55665963Spendry error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p); 55766152Spendry if (error == 0) { 55866152Spendry struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount); 55966152Spendry 56066152Spendry if (um->um_op == UNMNT_BELOW) 56166152Spendry error = VOP_ACCESS(vp, ap->a_mode, 56266152Spendry um->um_cred, ap->a_p); 56366152Spendry } 56465965Spendry VOP_UNLOCK(vp); 56565963Spendry if (error) 56665963Spendry return (error); 56765963Spendry } 56865963Spendry 56965965Spendry return (error); 57065963Spendry } 57165963Spendry 57265963Spendry /* 57365935Spendry * We handle getattr only to change the fsid. 57465935Spendry */ 57565935Spendry int 57665935Spendry union_getattr(ap) 57765935Spendry struct vop_getattr_args /* { 57865935Spendry struct vnode *a_vp; 57965935Spendry struct vattr *a_vap; 58065935Spendry struct ucred *a_cred; 58165935Spendry struct proc *a_p; 58265935Spendry } */ *ap; 58365935Spendry { 58465935Spendry int error; 58566062Spendry struct union_node *un = VTOUNION(ap->a_vp); 58666062Spendry struct vnode *vp = un->un_uppervp; 58766062Spendry struct vattr *vap; 58866062Spendry struct vattr va; 58965935Spendry 59066062Spendry 59166062Spendry /* 59266062Spendry * Some programs walk the filesystem hierarchy by counting 59366062Spendry * links to directories to avoid stat'ing all the time. 59466062Spendry * This means the link count on directories needs to be "correct". 59566062Spendry * The only way to do that is to call getattr on both layers 59666062Spendry * and fix up the link count. The link count will not necessarily 59766062Spendry * be accurate but will be large enough to defeat the tree walkers. 59866062Spendry */ 59966062Spendry 60066062Spendry vap = ap->a_vap; 60166062Spendry 60266062Spendry vp = un->un_uppervp; 60366062Spendry if (vp != NULLVP) { 60466152Spendry FIXUP(un); 60566062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 60666062Spendry if (error) 60766062Spendry return (error); 60866062Spendry } 60966062Spendry 61066062Spendry if (vp == NULLVP) { 61166062Spendry vp = un->un_lowervp; 61266062Spendry } else if (vp->v_type == VDIR) { 61366062Spendry vp = un->un_lowervp; 61466062Spendry vap = &va; 61566062Spendry } else { 61666062Spendry vp = NULLVP; 61766062Spendry } 61866062Spendry 61966062Spendry if (vp != NULLVP) { 62066051Spendry VOP_LOCK(vp); 62166062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 62266051Spendry VOP_UNLOCK(vp); 62366062Spendry if (error) 62466062Spendry return (error); 62566062Spendry } 62665965Spendry 62766062Spendry if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 62866062Spendry ap->a_vap->va_nlink += vap->va_nlink; 62966062Spendry 63066062Spendry vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 63165935Spendry return (0); 63265935Spendry } 63365935Spendry 63465963Spendry int 63565965Spendry union_setattr(ap) 63665963Spendry struct vop_setattr_args /* { 63765963Spendry struct vnode *a_vp; 63865963Spendry struct vattr *a_vap; 63965963Spendry struct ucred *a_cred; 64065963Spendry struct proc *a_p; 64165963Spendry } */ *ap; 64265963Spendry { 64365963Spendry struct union_node *un = VTOUNION(ap->a_vp); 64465963Spendry int error; 64565963Spendry 64666057Spendry /* 64766057Spendry * Handle case of truncating lower object to zero size, 64866057Spendry * by creating a zero length upper object. This is to 64966057Spendry * handle the case of open with O_TRUNC and O_CREAT. 65066057Spendry */ 65166057Spendry if ((un->un_uppervp == NULLVP) && 65266057Spendry /* assert(un->un_lowervp != NULLVP) */ 65366057Spendry (un->un_lowervp->v_type == VREG) && 65466057Spendry (ap->a_vap->va_size == 0)) { 65566057Spendry struct vnode *vp; 65666057Spendry 65766057Spendry error = union_vn_create(&vp, un, ap->a_p); 65866057Spendry if (error) 65966057Spendry return (error); 66066057Spendry 66166057Spendry /* at this point, uppervp is locked */ 66266057Spendry union_newupper(un, vp); 66366057Spendry 66466057Spendry VOP_UNLOCK(vp); 66566057Spendry union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p); 66666057Spendry VOP_LOCK(vp); 66766057Spendry un->un_flags |= UN_ULOCK; 66866057Spendry } 66966057Spendry 67066057Spendry /* 67166057Spendry * Try to set attributes in upper layer, 67266057Spendry * otherwise return read-only filesystem error. 67366057Spendry */ 67466057Spendry if (un->un_uppervp != NULLVP) { 67566152Spendry FIXUP(un); 67665963Spendry error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 67765963Spendry ap->a_cred, ap->a_p); 67865963Spendry } else { 67965963Spendry error = EROFS; 68065963Spendry } 68165963Spendry 68265963Spendry return (error); 68365963Spendry } 68465963Spendry 68565963Spendry int 68665963Spendry union_read(ap) 68765963Spendry struct vop_read_args /* { 68865963Spendry struct vnode *a_vp; 68965963Spendry struct uio *a_uio; 69065963Spendry int a_ioflag; 69165963Spendry struct ucred *a_cred; 69265963Spendry } */ *ap; 69365963Spendry { 69465963Spendry int error; 69565963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 69666051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 69765963Spendry 69866051Spendry if (dolock) 69966051Spendry VOP_LOCK(vp); 70066152Spendry else 70166152Spendry FIXUP(VTOUNION(ap->a_vp)); 70265963Spendry error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 70366051Spendry if (dolock) 70466051Spendry VOP_UNLOCK(vp); 70565963Spendry 70665963Spendry return (error); 70765963Spendry } 70865963Spendry 70965963Spendry int 71065963Spendry union_write(ap) 71165963Spendry struct vop_read_args /* { 71265963Spendry struct vnode *a_vp; 71365963Spendry struct uio *a_uio; 71465963Spendry int a_ioflag; 71565963Spendry struct ucred *a_cred; 71665963Spendry } */ *ap; 71765963Spendry { 71865963Spendry int error; 71965963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 72066051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 72165963Spendry 72266051Spendry if (dolock) 72366051Spendry VOP_LOCK(vp); 72466152Spendry else 72566152Spendry FIXUP(VTOUNION(ap->a_vp)); 72665963Spendry error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 72766051Spendry if (dolock) 72866051Spendry VOP_UNLOCK(vp); 72965963Spendry 73065963Spendry return (error); 73165963Spendry } 73265963Spendry 73365963Spendry int 73465963Spendry union_ioctl(ap) 73565963Spendry struct vop_ioctl_args /* { 73665963Spendry struct vnode *a_vp; 73765963Spendry int a_command; 73865963Spendry caddr_t a_data; 73965963Spendry int a_fflag; 74065963Spendry struct ucred *a_cred; 74165963Spendry struct proc *a_p; 74265963Spendry } */ *ap; 74365963Spendry { 74465963Spendry 74565963Spendry return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 74665963Spendry ap->a_fflag, ap->a_cred, ap->a_p)); 74765963Spendry } 74865963Spendry 74965963Spendry int 75065963Spendry union_select(ap) 75165963Spendry struct vop_select_args /* { 75265963Spendry struct vnode *a_vp; 75365963Spendry int a_which; 75465963Spendry int a_fflags; 75565963Spendry struct ucred *a_cred; 75665963Spendry struct proc *a_p; 75765963Spendry } */ *ap; 75865963Spendry { 75965963Spendry 76065963Spendry return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 76165963Spendry ap->a_cred, ap->a_p)); 76265963Spendry } 76365963Spendry 76465963Spendry int 76565963Spendry union_mmap(ap) 76665963Spendry struct vop_mmap_args /* { 76765963Spendry struct vnode *a_vp; 76865963Spendry int a_fflags; 76965963Spendry struct ucred *a_cred; 77065963Spendry struct proc *a_p; 77165963Spendry } */ *ap; 77265963Spendry { 77365963Spendry 77465963Spendry return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 77565963Spendry ap->a_cred, ap->a_p)); 77665963Spendry } 77765963Spendry 77865963Spendry int 77965963Spendry union_fsync(ap) 78065963Spendry struct vop_fsync_args /* { 78165963Spendry struct vnode *a_vp; 78265963Spendry struct ucred *a_cred; 78365963Spendry int a_waitfor; 78465963Spendry struct proc *a_p; 78565963Spendry } */ *ap; 78665963Spendry { 78765963Spendry int error = 0; 78865963Spendry struct vnode *targetvp = OTHERVP(ap->a_vp); 78965963Spendry 79065963Spendry if (targetvp) { 79166051Spendry int dolock = (targetvp == LOWERVP(ap->a_vp)); 79266051Spendry 79366051Spendry if (dolock) 79466051Spendry VOP_LOCK(targetvp); 79566152Spendry else 79666152Spendry FIXUP(VTOUNION(ap->a_vp)); 79765963Spendry error = VOP_FSYNC(targetvp, ap->a_cred, 79865963Spendry ap->a_waitfor, ap->a_p); 79966051Spendry if (dolock) 80066051Spendry VOP_UNLOCK(targetvp); 80165963Spendry } 80265963Spendry 80365963Spendry return (error); 80465963Spendry } 80565963Spendry 80665963Spendry int 80765963Spendry union_seek(ap) 80865963Spendry struct vop_seek_args /* { 80965963Spendry struct vnode *a_vp; 81065963Spendry off_t a_oldoff; 81165963Spendry off_t a_newoff; 81265963Spendry struct ucred *a_cred; 81365963Spendry } */ *ap; 81465963Spendry { 81565963Spendry 81665963Spendry return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 81765963Spendry } 81865963Spendry 81965963Spendry int 82065963Spendry union_remove(ap) 82165963Spendry struct vop_remove_args /* { 82265963Spendry struct vnode *a_dvp; 82365963Spendry struct vnode *a_vp; 82465963Spendry struct componentname *a_cnp; 82565963Spendry } */ *ap; 82665963Spendry { 82765963Spendry int error; 82865963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 82965963Spendry struct union_node *un = VTOUNION(ap->a_vp); 83065963Spendry 83165963Spendry if (dun->un_uppervp && un->un_uppervp) { 83265963Spendry struct vnode *dvp = dun->un_uppervp; 83365963Spendry struct vnode *vp = un->un_uppervp; 83465963Spendry 83566152Spendry FIXUP(dun); 83665963Spendry VREF(dvp); 83766051Spendry dun->un_flags |= UN_KLOCK; 83865963Spendry vput(ap->a_dvp); 83966152Spendry FIXUP(un); 84065963Spendry VREF(vp); 84166051Spendry un->un_flags |= UN_KLOCK; 84265963Spendry vput(ap->a_vp); 84365963Spendry 84465963Spendry error = VOP_REMOVE(dvp, vp, ap->a_cnp); 84566027Spendry if (!error) 84666027Spendry union_removed_upper(un); 84766027Spendry 84866027Spendry /* 84966027Spendry * XXX: should create a whiteout here 85066027Spendry */ 85165963Spendry } else { 85265963Spendry /* 85365963Spendry * XXX: should create a whiteout here 85465963Spendry */ 85565963Spendry vput(ap->a_dvp); 85665963Spendry vput(ap->a_vp); 85765963Spendry error = EROFS; 85865963Spendry } 85965963Spendry 86065963Spendry return (error); 86165963Spendry } 86265963Spendry 86365963Spendry int 86465963Spendry union_link(ap) 86565963Spendry struct vop_link_args /* { 86665963Spendry struct vnode *a_vp; 86765963Spendry struct vnode *a_tdvp; 86865963Spendry struct componentname *a_cnp; 86965963Spendry } */ *ap; 87065963Spendry { 87165963Spendry int error; 87265963Spendry struct union_node *dun = VTOUNION(ap->a_vp); 87365963Spendry struct union_node *un = VTOUNION(ap->a_tdvp); 87465963Spendry 87565963Spendry if (dun->un_uppervp && un->un_uppervp) { 87665963Spendry struct vnode *dvp = dun->un_uppervp; 87765963Spendry struct vnode *vp = un->un_uppervp; 87865963Spendry 87966152Spendry FIXUP(dun); 88065963Spendry VREF(dvp); 88166051Spendry dun->un_flags |= UN_KLOCK; 88265963Spendry vput(ap->a_vp); 88366152Spendry FIXUP(un); 88465963Spendry VREF(vp); 88565963Spendry vrele(ap->a_tdvp); 88665963Spendry 88765963Spendry error = VOP_LINK(dvp, vp, ap->a_cnp); 88865963Spendry } else { 88965963Spendry /* 89065963Spendry * XXX: need to copy to upper layer 89165963Spendry * and do the link there. 89265963Spendry */ 89365963Spendry vput(ap->a_vp); 89465963Spendry vrele(ap->a_tdvp); 89565963Spendry error = EROFS; 89665963Spendry } 89765963Spendry 89865963Spendry return (error); 89965963Spendry } 90065963Spendry 90165963Spendry int 90265963Spendry union_rename(ap) 90365963Spendry struct vop_rename_args /* { 90465963Spendry struct vnode *a_fdvp; 90565963Spendry struct vnode *a_fvp; 90665963Spendry struct componentname *a_fcnp; 90765963Spendry struct vnode *a_tdvp; 90865963Spendry struct vnode *a_tvp; 90965963Spendry struct componentname *a_tcnp; 91065963Spendry } */ *ap; 91165963Spendry { 91265963Spendry int error; 91365963Spendry 91465963Spendry struct vnode *fdvp = ap->a_fdvp; 91565963Spendry struct vnode *fvp = ap->a_fvp; 91665963Spendry struct vnode *tdvp = ap->a_tdvp; 91765963Spendry struct vnode *tvp = ap->a_tvp; 91865963Spendry 91965963Spendry if (fdvp->v_op == union_vnodeop_p) { /* always true */ 92065963Spendry struct union_node *un = VTOUNION(fdvp); 92165997Spendry if (un->un_uppervp == NULLVP) { 92265963Spendry error = EROFS; 92365963Spendry goto bad; 92465963Spendry } 92565963Spendry 92666152Spendry FIXUP(un); 92765963Spendry fdvp = un->un_uppervp; 92865963Spendry VREF(fdvp); 92965963Spendry vrele(ap->a_fdvp); 93065963Spendry } 93165963Spendry 93265963Spendry if (fvp->v_op == union_vnodeop_p) { /* always true */ 93365963Spendry struct union_node *un = VTOUNION(fvp); 93465997Spendry if (un->un_uppervp == NULLVP) { 93565963Spendry error = EROFS; 93665963Spendry goto bad; 93765963Spendry } 93865963Spendry 93966152Spendry FIXUP(un); 94065963Spendry fvp = un->un_uppervp; 94165963Spendry VREF(fvp); 94265963Spendry vrele(ap->a_fvp); 94365963Spendry } 94465963Spendry 94565963Spendry if (tdvp->v_op == union_vnodeop_p) { 94665963Spendry struct union_node *un = VTOUNION(tdvp); 94765997Spendry if (un->un_uppervp == NULLVP) { 94865963Spendry error = EROFS; 94965963Spendry goto bad; 95065963Spendry } 95165963Spendry 95265963Spendry tdvp = un->un_uppervp; 95365963Spendry VREF(tdvp); 95466051Spendry un->un_flags |= UN_KLOCK; 95565997Spendry vput(ap->a_tdvp); 95665963Spendry } 95765963Spendry 95865963Spendry if (tvp && tvp->v_op == union_vnodeop_p) { 95965963Spendry struct union_node *un = VTOUNION(tvp); 96065997Spendry if (un->un_uppervp == NULLVP) { 96165963Spendry error = EROFS; 96265963Spendry goto bad; 96365963Spendry } 96465963Spendry 96565963Spendry tvp = un->un_uppervp; 96665963Spendry VREF(tvp); 96766051Spendry un->un_flags |= UN_KLOCK; 96865963Spendry vput(ap->a_tvp); 96965963Spendry } 97065963Spendry 97165963Spendry return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 97265963Spendry 97365963Spendry bad: 97465963Spendry vrele(fdvp); 97565963Spendry vrele(fvp); 97665963Spendry vput(tdvp); 97765963Spendry if (tvp) 97865963Spendry vput(tvp); 97965963Spendry 98065963Spendry return (error); 98165963Spendry } 98265963Spendry 98365963Spendry int 98465963Spendry union_mkdir(ap) 98565963Spendry struct vop_mkdir_args /* { 98665963Spendry struct vnode *a_dvp; 98765963Spendry struct vnode **a_vpp; 98865963Spendry struct componentname *a_cnp; 98965963Spendry struct vattr *a_vap; 99065963Spendry } */ *ap; 99165963Spendry { 99265963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 99365963Spendry struct vnode *dvp = un->un_uppervp; 99465963Spendry 99565963Spendry if (dvp) { 99665963Spendry int error; 99765963Spendry struct vnode *vp; 99865963Spendry 99966152Spendry FIXUP(un); 100065963Spendry VREF(dvp); 100166051Spendry un->un_flags |= UN_KLOCK; 100265963Spendry vput(ap->a_dvp); 100365963Spendry error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 100465963Spendry if (error) 100565963Spendry return (error); 100665963Spendry 100765963Spendry error = union_allocvp( 100865963Spendry ap->a_vpp, 100965989Spendry ap->a_dvp->v_mount, 101065989Spendry ap->a_dvp, 101165965Spendry NULLVP, 101265963Spendry ap->a_cnp, 101365963Spendry vp, 101465963Spendry NULLVP); 101565965Spendry if (error) 101666051Spendry vput(vp); 101765963Spendry return (error); 101865963Spendry } 101965963Spendry 102065963Spendry vput(ap->a_dvp); 102165963Spendry return (EROFS); 102265963Spendry } 102365963Spendry 102465963Spendry int 102565963Spendry union_rmdir(ap) 102665963Spendry struct vop_rmdir_args /* { 102765963Spendry struct vnode *a_dvp; 102865963Spendry struct vnode *a_vp; 102965963Spendry struct componentname *a_cnp; 103065963Spendry } */ *ap; 103165963Spendry { 103265963Spendry int error; 103365963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 103465963Spendry struct union_node *un = VTOUNION(ap->a_vp); 103565963Spendry 103665963Spendry if (dun->un_uppervp && un->un_uppervp) { 103765963Spendry struct vnode *dvp = dun->un_uppervp; 103865963Spendry struct vnode *vp = un->un_uppervp; 103965963Spendry 104066152Spendry FIXUP(dun); 104165963Spendry VREF(dvp); 104266051Spendry dun->un_flags |= UN_KLOCK; 104365963Spendry vput(ap->a_dvp); 104466152Spendry FIXUP(un); 104565963Spendry VREF(vp); 104666051Spendry un->un_flags |= UN_KLOCK; 104765963Spendry vput(ap->a_vp); 104865963Spendry 104966051Spendry error = VOP_RMDIR(dvp, vp, ap->a_cnp); 105066027Spendry if (!error) 105166027Spendry union_removed_upper(un); 105266027Spendry 105366027Spendry /* 105466027Spendry * XXX: should create a whiteout here 105566027Spendry */ 105665963Spendry } else { 105765963Spendry /* 105865963Spendry * XXX: should create a whiteout here 105965963Spendry */ 106065963Spendry vput(ap->a_dvp); 106165963Spendry vput(ap->a_vp); 106265963Spendry error = EROFS; 106365963Spendry } 106465963Spendry 106565963Spendry return (error); 106665963Spendry } 106765963Spendry 106865963Spendry int 106965963Spendry union_symlink(ap) 107065963Spendry struct vop_symlink_args /* { 107165963Spendry struct vnode *a_dvp; 107265963Spendry struct vnode **a_vpp; 107365963Spendry struct componentname *a_cnp; 107465963Spendry struct vattr *a_vap; 107565963Spendry char *a_target; 107665963Spendry } */ *ap; 107765963Spendry { 107865963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 107965963Spendry struct vnode *dvp = un->un_uppervp; 108065963Spendry 108165963Spendry if (dvp) { 108265963Spendry int error; 108365963Spendry struct vnode *vp; 108465963Spendry struct mount *mp = ap->a_dvp->v_mount; 108565963Spendry 108666152Spendry FIXUP(un); 108765963Spendry VREF(dvp); 108866051Spendry un->un_flags |= UN_KLOCK; 108965963Spendry vput(ap->a_dvp); 109065963Spendry error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 109165963Spendry ap->a_vap, ap->a_target); 109265997Spendry *ap->a_vpp = NULLVP; 109365963Spendry return (error); 109465963Spendry } 109565963Spendry 109665963Spendry vput(ap->a_dvp); 109765963Spendry return (EROFS); 109865963Spendry } 109965963Spendry 110065935Spendry /* 110165935Spendry * union_readdir works in concert with getdirentries and 110265935Spendry * readdir(3) to provide a list of entries in the unioned 110365935Spendry * directories. getdirentries is responsible for walking 110465935Spendry * down the union stack. readdir(3) is responsible for 110565935Spendry * eliminating duplicate names from the returned data stream. 110665935Spendry */ 110765935Spendry int 110865935Spendry union_readdir(ap) 110965935Spendry struct vop_readdir_args /* { 111065935Spendry struct vnodeop_desc *a_desc; 111165935Spendry struct vnode *a_vp; 111265935Spendry struct uio *a_uio; 111365935Spendry struct ucred *a_cred; 111465935Spendry } */ *ap; 111565935Spendry { 111665963Spendry int error = 0; 111765935Spendry struct union_node *un = VTOUNION(ap->a_vp); 111865935Spendry 111966152Spendry if (un->un_uppervp) { 112066152Spendry FIXUP(un); 112166051Spendry error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred); 112266152Spendry } 112365935Spendry 112465963Spendry return (error); 112565935Spendry } 112665935Spendry 112765935Spendry int 112865963Spendry union_readlink(ap) 112965963Spendry struct vop_readlink_args /* { 113065963Spendry struct vnode *a_vp; 113165963Spendry struct uio *a_uio; 113265963Spendry struct ucred *a_cred; 113365963Spendry } */ *ap; 113465963Spendry { 113565963Spendry int error; 113665963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 113766051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 113865963Spendry 113966051Spendry if (dolock) 114066051Spendry VOP_LOCK(vp); 114166152Spendry else 114266152Spendry FIXUP(VTOUNION(ap->a_vp)); 114365963Spendry error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 114466051Spendry if (dolock) 114566051Spendry VOP_UNLOCK(vp); 114665963Spendry 114765963Spendry return (error); 114865963Spendry } 114965963Spendry 115065963Spendry int 115165963Spendry union_abortop(ap) 115265963Spendry struct vop_abortop_args /* { 115365963Spendry struct vnode *a_dvp; 115465963Spendry struct componentname *a_cnp; 115565963Spendry } */ *ap; 115665963Spendry { 115765963Spendry int error; 115865965Spendry struct vnode *vp = OTHERVP(ap->a_dvp); 115965963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 116065963Spendry int islocked = un->un_flags & UN_LOCKED; 116166051Spendry int dolock = (vp == LOWERVP(ap->a_dvp)); 116265963Spendry 116366152Spendry if (islocked) { 116466152Spendry if (dolock) 116566152Spendry VOP_LOCK(vp); 116666152Spendry else 116766152Spendry FIXUP(VTOUNION(ap->a_dvp)); 116866152Spendry } 116965963Spendry error = VOP_ABORTOP(vp, ap->a_cnp); 117066051Spendry if (islocked && dolock) 117165963Spendry VOP_UNLOCK(vp); 117265963Spendry 117365963Spendry return (error); 117465963Spendry } 117565963Spendry 117665963Spendry int 117765935Spendry union_inactive(ap) 117865935Spendry struct vop_inactive_args /* { 117965935Spendry struct vnode *a_vp; 118065935Spendry } */ *ap; 118165935Spendry { 118265935Spendry 118365935Spendry /* 118465935Spendry * Do nothing (and _don't_ bypass). 118565935Spendry * Wait to vrele lowervp until reclaim, 118665935Spendry * so that until then our union_node is in the 118765935Spendry * cache and reusable. 118865935Spendry * 118965935Spendry * NEEDSWORK: Someday, consider inactive'ing 119065935Spendry * the lowervp and then trying to reactivate it 119165935Spendry * with capabilities (v_id) 119265935Spendry * like they do in the name lookup cache code. 119365935Spendry * That's too much work for now. 119465935Spendry */ 119565989Spendry 119666027Spendry #ifdef UNION_DIAGNOSTIC 119765989Spendry struct union_node *un = VTOUNION(ap->a_vp); 119865989Spendry 119965989Spendry if (un->un_flags & UN_LOCKED) 120065989Spendry panic("union: inactivating locked node"); 120165989Spendry #endif 120265989Spendry 120365935Spendry return (0); 120465935Spendry } 120565935Spendry 120665935Spendry int 120765935Spendry union_reclaim(ap) 120865935Spendry struct vop_reclaim_args /* { 120965935Spendry struct vnode *a_vp; 121065935Spendry } */ *ap; 121165935Spendry { 121265935Spendry 121366053Spendry union_freevp(ap->a_vp); 121466053Spendry 121565935Spendry return (0); 121665935Spendry } 121765935Spendry 121865963Spendry int 121965963Spendry union_lock(ap) 122065963Spendry struct vop_lock_args *ap; 122165963Spendry { 122266149Spendry struct vnode *vp = ap->a_vp; 122366149Spendry struct union_node *un; 122465935Spendry 122566149Spendry start: 122666149Spendry while (vp->v_flag & VXLOCK) { 122766149Spendry vp->v_flag |= VXWANT; 122866149Spendry sleep((caddr_t)vp, PINOD); 122966149Spendry } 123066149Spendry 123166149Spendry un = VTOUNION(vp); 123266149Spendry 123366051Spendry if (un->un_uppervp) { 123466051Spendry if ((un->un_flags & UN_ULOCK) == 0) { 123566149Spendry un->un_flags |= UN_ULOCK; 123666051Spendry VOP_LOCK(un->un_uppervp); 123766051Spendry } 123866051Spendry #ifdef DIAGNOSTIC 123966051Spendry if (un->un_flags & UN_KLOCK) 124066051Spendry panic("union: dangling upper lock"); 124166051Spendry #endif 124266051Spendry } 124366051Spendry 124466149Spendry if (un->un_flags & UN_LOCKED) { 124565963Spendry #ifdef DIAGNOSTIC 124665989Spendry if (curproc && un->un_pid == curproc->p_pid && 124765989Spendry un->un_pid > -1 && curproc->p_pid > -1) 124865989Spendry panic("union: locking against myself"); 124965963Spendry #endif 125065963Spendry un->un_flags |= UN_WANT; 125165963Spendry sleep((caddr_t) &un->un_flags, PINOD); 125266149Spendry goto start; 125365963Spendry } 125465989Spendry 125565963Spendry #ifdef DIAGNOSTIC 125665989Spendry if (curproc) 125765989Spendry un->un_pid = curproc->p_pid; 125865989Spendry else 125965989Spendry un->un_pid = -1; 126065963Spendry #endif 126166028Spendry 126266149Spendry un->un_flags |= UN_LOCKED; 126366028Spendry return (0); 126465963Spendry } 126565963Spendry 126665935Spendry int 126765963Spendry union_unlock(ap) 126865963Spendry struct vop_lock_args *ap; 126965963Spendry { 127065963Spendry struct union_node *un = VTOUNION(ap->a_vp); 127165963Spendry 127265963Spendry #ifdef DIAGNOSTIC 127365965Spendry if ((un->un_flags & UN_LOCKED) == 0) 127465965Spendry panic("union: unlock unlocked node"); 127565989Spendry if (curproc && un->un_pid != curproc->p_pid && 127665989Spendry curproc->p_pid > -1 && un->un_pid > -1) 127765963Spendry panic("union: unlocking other process's union node"); 127865963Spendry #endif 127965963Spendry 128065963Spendry un->un_flags &= ~UN_LOCKED; 128166051Spendry 128266051Spendry if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 128366051Spendry VOP_UNLOCK(un->un_uppervp); 128466051Spendry 128566051Spendry un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 128666051Spendry 128765963Spendry if (un->un_flags & UN_WANT) { 128865963Spendry un->un_flags &= ~UN_WANT; 128965963Spendry wakeup((caddr_t) &un->un_flags); 129065963Spendry } 129165963Spendry 129265963Spendry #ifdef DIAGNOSTIC 129365963Spendry un->un_pid = 0; 129465963Spendry #endif 129566028Spendry 129666028Spendry return (0); 129765963Spendry } 129865963Spendry 129965963Spendry int 130065963Spendry union_bmap(ap) 130165963Spendry struct vop_bmap_args /* { 130265963Spendry struct vnode *a_vp; 130365963Spendry daddr_t a_bn; 130465963Spendry struct vnode **a_vpp; 130565963Spendry daddr_t *a_bnp; 130665963Spendry int *a_runp; 130765963Spendry } */ *ap; 130865963Spendry { 130965963Spendry int error; 131065963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 131166051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 131265963Spendry 131366051Spendry if (dolock) 131466051Spendry VOP_LOCK(vp); 131566152Spendry else 131666152Spendry FIXUP(VTOUNION(ap->a_vp)); 131765963Spendry error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 131866051Spendry if (dolock) 131966051Spendry VOP_UNLOCK(vp); 132065963Spendry 132165963Spendry return (error); 132265963Spendry } 132365963Spendry 132465963Spendry int 132565935Spendry union_print(ap) 132665935Spendry struct vop_print_args /* { 132765935Spendry struct vnode *a_vp; 132865935Spendry } */ *ap; 132965935Spendry { 133065935Spendry struct vnode *vp = ap->a_vp; 133165935Spendry 133265935Spendry printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 133365935Spendry vp, UPPERVP(vp), LOWERVP(vp)); 133465935Spendry return (0); 133565935Spendry } 133665935Spendry 133765963Spendry int 133865963Spendry union_islocked(ap) 133965963Spendry struct vop_islocked_args /* { 134065963Spendry struct vnode *a_vp; 134165963Spendry } */ *ap; 134265963Spendry { 134365935Spendry 134465963Spendry return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 134565963Spendry } 134665963Spendry 134765935Spendry int 134865963Spendry union_pathconf(ap) 134965963Spendry struct vop_pathconf_args /* { 135065963Spendry struct vnode *a_vp; 135165963Spendry int a_name; 135265963Spendry int *a_retval; 135365935Spendry } */ *ap; 135465935Spendry { 135565935Spendry int error; 135665963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 135766051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 135865935Spendry 135966051Spendry if (dolock) 136066051Spendry VOP_LOCK(vp); 136166152Spendry else 136266152Spendry FIXUP(VTOUNION(ap->a_vp)); 136365963Spendry error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 136466051Spendry if (dolock) 136566051Spendry VOP_UNLOCK(vp); 136665935Spendry 136765963Spendry return (error); 136865963Spendry } 136965935Spendry 137065963Spendry int 137165963Spendry union_advlock(ap) 137265963Spendry struct vop_advlock_args /* { 137365963Spendry struct vnode *a_vp; 137465963Spendry caddr_t a_id; 137565963Spendry int a_op; 137665963Spendry struct flock *a_fl; 137765963Spendry int a_flags; 137865963Spendry } */ *ap; 137965963Spendry { 138065935Spendry 138165963Spendry return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 138265963Spendry ap->a_fl, ap->a_flags)); 138365935Spendry } 138465935Spendry 138565935Spendry 138665935Spendry /* 138765963Spendry * XXX - vop_strategy must be hand coded because it has no 138865935Spendry * vnode in its arguments. 138965935Spendry * This goes away with a merged VM/buffer cache. 139065935Spendry */ 139165935Spendry int 139265963Spendry union_strategy(ap) 139365963Spendry struct vop_strategy_args /* { 139465935Spendry struct buf *a_bp; 139565935Spendry } */ *ap; 139665935Spendry { 139765935Spendry struct buf *bp = ap->a_bp; 139865935Spendry int error; 139965935Spendry struct vnode *savedvp; 140065935Spendry 140165935Spendry savedvp = bp->b_vp; 140265963Spendry bp->b_vp = OTHERVP(bp->b_vp); 140365935Spendry 140465935Spendry #ifdef DIAGNOSTIC 140565997Spendry if (bp->b_vp == NULLVP) 140665963Spendry panic("union_strategy: nil vp"); 140765963Spendry if (((bp->b_flags & B_READ) == 0) && 140865963Spendry (bp->b_vp == LOWERVP(savedvp))) 140965963Spendry panic("union_strategy: writing to lowervp"); 141065935Spendry #endif 141165935Spendry 141265963Spendry error = VOP_STRATEGY(bp); 141365935Spendry bp->b_vp = savedvp; 141465935Spendry 141565935Spendry return (error); 141665935Spendry } 141765935Spendry 141865935Spendry /* 141965935Spendry * Global vfs data structures 142065935Spendry */ 142165935Spendry int (**union_vnodeop_p)(); 142265965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = { 142365963Spendry { &vop_default_desc, vn_default_error }, 142465963Spendry { &vop_lookup_desc, union_lookup }, /* lookup */ 142565963Spendry { &vop_create_desc, union_create }, /* create */ 142665963Spendry { &vop_mknod_desc, union_mknod }, /* mknod */ 142765963Spendry { &vop_open_desc, union_open }, /* open */ 142865963Spendry { &vop_close_desc, union_close }, /* close */ 142965963Spendry { &vop_access_desc, union_access }, /* access */ 143065963Spendry { &vop_getattr_desc, union_getattr }, /* getattr */ 143165963Spendry { &vop_setattr_desc, union_setattr }, /* setattr */ 143265963Spendry { &vop_read_desc, union_read }, /* read */ 143365963Spendry { &vop_write_desc, union_write }, /* write */ 143465963Spendry { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 143565963Spendry { &vop_select_desc, union_select }, /* select */ 143665963Spendry { &vop_mmap_desc, union_mmap }, /* mmap */ 143765963Spendry { &vop_fsync_desc, union_fsync }, /* fsync */ 143865963Spendry { &vop_seek_desc, union_seek }, /* seek */ 143965963Spendry { &vop_remove_desc, union_remove }, /* remove */ 144065963Spendry { &vop_link_desc, union_link }, /* link */ 144165963Spendry { &vop_rename_desc, union_rename }, /* rename */ 144265963Spendry { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 144365963Spendry { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 144465963Spendry { &vop_symlink_desc, union_symlink }, /* symlink */ 144565963Spendry { &vop_readdir_desc, union_readdir }, /* readdir */ 144665963Spendry { &vop_readlink_desc, union_readlink }, /* readlink */ 144765963Spendry { &vop_abortop_desc, union_abortop }, /* abortop */ 144865963Spendry { &vop_inactive_desc, union_inactive }, /* inactive */ 144965963Spendry { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 145065963Spendry { &vop_lock_desc, union_lock }, /* lock */ 145165963Spendry { &vop_unlock_desc, union_unlock }, /* unlock */ 145265963Spendry { &vop_bmap_desc, union_bmap }, /* bmap */ 145365963Spendry { &vop_strategy_desc, union_strategy }, /* strategy */ 145465963Spendry { &vop_print_desc, union_print }, /* print */ 145565963Spendry { &vop_islocked_desc, union_islocked }, /* islocked */ 145665963Spendry { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 145765963Spendry { &vop_advlock_desc, union_advlock }, /* advlock */ 145865963Spendry #ifdef notdef 145965963Spendry { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 146065963Spendry { &vop_valloc_desc, union_valloc }, /* valloc */ 146165963Spendry { &vop_vfree_desc, union_vfree }, /* vfree */ 146265963Spendry { &vop_truncate_desc, union_truncate }, /* truncate */ 146365963Spendry { &vop_update_desc, union_update }, /* update */ 146465963Spendry { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 146565963Spendry #endif 146665935Spendry { (struct vnodeop_desc*)NULL, (int(*)())NULL } 146765935Spendry }; 146865935Spendry struct vnodeopv_desc union_vnodeop_opv_desc = 146965935Spendry { &union_vnodeop_p, union_vnodeop_entries }; 1470