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*67076Spendry * @(#)union_vnops.c 8.9 (Berkeley) 04/29/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 4467064Spendry union_lookup1(udvp, dvpp, vpp, cnp) 4565989Spendry struct vnode *udvp; 4667064Spendry struct vnode **dvpp; 4765935Spendry struct vnode **vpp; 4865935Spendry struct componentname *cnp; 4965935Spendry { 5065935Spendry int error; 5165935Spendry struct vnode *tdvp; 5267064Spendry struct vnode *dvp; 5365935Spendry struct mount *mp; 5465935Spendry 5567064Spendry dvp = *dvpp; 5667064Spendry 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) { 6467064Spendry 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; 7267064Spendry *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 */ 154*67076Spendry if (upperdvp != NULLVP) { 15566152Spendry FIXUP(dun); 15667064Spendry 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 */ 178*67076Spendry if (lowerdvp != NULLVP) { 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 } 19367064Spendry 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) { 203*67076Spendry if (uppervp != NULLVP) { 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) { 263*67076Spendry if (lowervp != NULLVP) { 26465935Spendry vput(lowervp); 26565997Spendry lowervp = NULLVP; 26665935Spendry } 26765935Spendry return (uerror); 26865935Spendry } 26965935Spendry } 27065935Spendry } 27165935Spendry 272*67076Spendry if (lowervp != NULLVP) 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) { 279*67076Spendry if (uppervp != NULLVP) 28066051Spendry vput(uppervp); 281*67076Spendry if (lowervp != NULLVP) 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 304*67076Spendry if (dvp != NULLVP) { 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 346*67076Spendry if (dvp != NULLVP) { 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 359*67076Spendry if (vp != NULLVP) { 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 513*67076Spendry if (un->un_uppervp != NULLVP) { 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 549*67076Spendry if ((vp = un->un_uppervp) != NULLVP) { 55066152Spendry FIXUP(un); 55166152Spendry return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); 55266152Spendry } 55366152Spendry 554*67076Spendry if ((vp = un->un_lowervp) != NULLVP) { 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) { 60467073Spendry /* 60567073Spendry * It's not clear whether VOP_GETATTR is to be 60667073Spendry * called with the vnode locked or not. stat() calls 60767073Spendry * it with (vp) locked, and fstat calls it with 60867073Spendry * (vp) unlocked. 60967073Spendry * In the mean time, compensate here by checking 61067073Spendry * the union_node's lock flag. 61167073Spendry */ 61267073Spendry if (un->un_flags & UN_LOCKED) 61367073Spendry FIXUP(un); 61467073Spendry 61566062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 61666062Spendry if (error) 61766062Spendry return (error); 61866062Spendry } 61966062Spendry 62066062Spendry if (vp == NULLVP) { 62166062Spendry vp = un->un_lowervp; 62266062Spendry } else if (vp->v_type == VDIR) { 62366062Spendry vp = un->un_lowervp; 62466062Spendry vap = &va; 62566062Spendry } else { 62666062Spendry vp = NULLVP; 62766062Spendry } 62866062Spendry 62966062Spendry if (vp != NULLVP) { 63066051Spendry VOP_LOCK(vp); 63166062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 63266051Spendry VOP_UNLOCK(vp); 63366062Spendry if (error) 63466062Spendry return (error); 63566062Spendry } 63665965Spendry 63766062Spendry if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 63866062Spendry ap->a_vap->va_nlink += vap->va_nlink; 63966062Spendry 64066062Spendry vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 64165935Spendry return (0); 64265935Spendry } 64365935Spendry 64465963Spendry int 64565965Spendry union_setattr(ap) 64665963Spendry struct vop_setattr_args /* { 64765963Spendry struct vnode *a_vp; 64865963Spendry struct vattr *a_vap; 64965963Spendry struct ucred *a_cred; 65065963Spendry struct proc *a_p; 65165963Spendry } */ *ap; 65265963Spendry { 65365963Spendry struct union_node *un = VTOUNION(ap->a_vp); 65465963Spendry int error; 65565963Spendry 65666057Spendry /* 65766057Spendry * Handle case of truncating lower object to zero size, 65866057Spendry * by creating a zero length upper object. This is to 65966057Spendry * handle the case of open with O_TRUNC and O_CREAT. 66066057Spendry */ 66166057Spendry if ((un->un_uppervp == NULLVP) && 66266057Spendry /* assert(un->un_lowervp != NULLVP) */ 66366057Spendry (un->un_lowervp->v_type == VREG) && 66466057Spendry (ap->a_vap->va_size == 0)) { 66566057Spendry struct vnode *vp; 66666057Spendry 66766057Spendry error = union_vn_create(&vp, un, ap->a_p); 66866057Spendry if (error) 66966057Spendry return (error); 67066057Spendry 67166057Spendry /* at this point, uppervp is locked */ 67266057Spendry union_newupper(un, vp); 67366057Spendry 67466057Spendry VOP_UNLOCK(vp); 67566057Spendry union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p); 67666057Spendry VOP_LOCK(vp); 67766057Spendry un->un_flags |= UN_ULOCK; 67866057Spendry } 67966057Spendry 68066057Spendry /* 68166057Spendry * Try to set attributes in upper layer, 68266057Spendry * otherwise return read-only filesystem error. 68366057Spendry */ 68466057Spendry if (un->un_uppervp != NULLVP) { 68566152Spendry FIXUP(un); 68665963Spendry error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 68765963Spendry ap->a_cred, ap->a_p); 68865963Spendry } else { 68965963Spendry error = EROFS; 69065963Spendry } 69165963Spendry 69265963Spendry return (error); 69365963Spendry } 69465963Spendry 69565963Spendry int 69665963Spendry union_read(ap) 69765963Spendry struct vop_read_args /* { 69865963Spendry struct vnode *a_vp; 69965963Spendry struct uio *a_uio; 70065963Spendry int a_ioflag; 70165963Spendry struct ucred *a_cred; 70265963Spendry } */ *ap; 70365963Spendry { 70465963Spendry int error; 70565963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 70666051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 70765963Spendry 70866051Spendry if (dolock) 70966051Spendry VOP_LOCK(vp); 71066152Spendry else 71166152Spendry FIXUP(VTOUNION(ap->a_vp)); 71265963Spendry error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 71366051Spendry if (dolock) 71466051Spendry VOP_UNLOCK(vp); 71565963Spendry 71665963Spendry return (error); 71765963Spendry } 71865963Spendry 71965963Spendry int 72065963Spendry union_write(ap) 72165963Spendry struct vop_read_args /* { 72265963Spendry struct vnode *a_vp; 72365963Spendry struct uio *a_uio; 72465963Spendry int a_ioflag; 72565963Spendry struct ucred *a_cred; 72665963Spendry } */ *ap; 72765963Spendry { 72865963Spendry int error; 72965963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 73066051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 73165963Spendry 73266051Spendry if (dolock) 73366051Spendry VOP_LOCK(vp); 73466152Spendry else 73566152Spendry FIXUP(VTOUNION(ap->a_vp)); 73665963Spendry error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 73766051Spendry if (dolock) 73866051Spendry VOP_UNLOCK(vp); 73965963Spendry 74065963Spendry return (error); 74165963Spendry } 74265963Spendry 74365963Spendry int 74465963Spendry union_ioctl(ap) 74565963Spendry struct vop_ioctl_args /* { 74665963Spendry struct vnode *a_vp; 74765963Spendry int a_command; 74865963Spendry caddr_t a_data; 74965963Spendry int a_fflag; 75065963Spendry struct ucred *a_cred; 75165963Spendry struct proc *a_p; 75265963Spendry } */ *ap; 75365963Spendry { 75465963Spendry 75565963Spendry return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 75665963Spendry ap->a_fflag, ap->a_cred, ap->a_p)); 75765963Spendry } 75865963Spendry 75965963Spendry int 76065963Spendry union_select(ap) 76165963Spendry struct vop_select_args /* { 76265963Spendry struct vnode *a_vp; 76365963Spendry int a_which; 76465963Spendry int a_fflags; 76565963Spendry struct ucred *a_cred; 76665963Spendry struct proc *a_p; 76765963Spendry } */ *ap; 76865963Spendry { 76965963Spendry 77065963Spendry return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 77165963Spendry ap->a_cred, ap->a_p)); 77265963Spendry } 77365963Spendry 77465963Spendry int 77565963Spendry union_mmap(ap) 77665963Spendry struct vop_mmap_args /* { 77765963Spendry struct vnode *a_vp; 77865963Spendry int a_fflags; 77965963Spendry struct ucred *a_cred; 78065963Spendry struct proc *a_p; 78165963Spendry } */ *ap; 78265963Spendry { 78365963Spendry 78465963Spendry return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 78565963Spendry ap->a_cred, ap->a_p)); 78665963Spendry } 78765963Spendry 78865963Spendry int 78965963Spendry union_fsync(ap) 79065963Spendry struct vop_fsync_args /* { 79165963Spendry struct vnode *a_vp; 79265963Spendry struct ucred *a_cred; 79365963Spendry int a_waitfor; 79465963Spendry struct proc *a_p; 79565963Spendry } */ *ap; 79665963Spendry { 79765963Spendry int error = 0; 79865963Spendry struct vnode *targetvp = OTHERVP(ap->a_vp); 79965963Spendry 800*67076Spendry if (targetvp != NULLVP) { 80166051Spendry int dolock = (targetvp == LOWERVP(ap->a_vp)); 80266051Spendry 80366051Spendry if (dolock) 80466051Spendry VOP_LOCK(targetvp); 80566152Spendry else 80666152Spendry FIXUP(VTOUNION(ap->a_vp)); 80765963Spendry error = VOP_FSYNC(targetvp, ap->a_cred, 80865963Spendry ap->a_waitfor, ap->a_p); 80966051Spendry if (dolock) 81066051Spendry VOP_UNLOCK(targetvp); 81165963Spendry } 81265963Spendry 81365963Spendry return (error); 81465963Spendry } 81565963Spendry 81665963Spendry int 81765963Spendry union_seek(ap) 81865963Spendry struct vop_seek_args /* { 81965963Spendry struct vnode *a_vp; 82065963Spendry off_t a_oldoff; 82165963Spendry off_t a_newoff; 82265963Spendry struct ucred *a_cred; 82365963Spendry } */ *ap; 82465963Spendry { 82565963Spendry 82665963Spendry return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 82765963Spendry } 82865963Spendry 82965963Spendry int 83065963Spendry union_remove(ap) 83165963Spendry struct vop_remove_args /* { 83265963Spendry struct vnode *a_dvp; 83365963Spendry struct vnode *a_vp; 83465963Spendry struct componentname *a_cnp; 83565963Spendry } */ *ap; 83665963Spendry { 83765963Spendry int error; 83865963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 83965963Spendry struct union_node *un = VTOUNION(ap->a_vp); 84065963Spendry 841*67076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 84265963Spendry struct vnode *dvp = dun->un_uppervp; 84365963Spendry struct vnode *vp = un->un_uppervp; 84465963Spendry 84566152Spendry FIXUP(dun); 84665963Spendry VREF(dvp); 84766051Spendry dun->un_flags |= UN_KLOCK; 84865963Spendry vput(ap->a_dvp); 84966152Spendry FIXUP(un); 85065963Spendry VREF(vp); 85166051Spendry un->un_flags |= UN_KLOCK; 85265963Spendry vput(ap->a_vp); 85365963Spendry 85465963Spendry error = VOP_REMOVE(dvp, vp, ap->a_cnp); 85566027Spendry if (!error) 85666027Spendry union_removed_upper(un); 85766027Spendry 85866027Spendry /* 85966027Spendry * XXX: should create a whiteout here 86066027Spendry */ 86165963Spendry } else { 86265963Spendry /* 86365963Spendry * XXX: should create a whiteout here 86465963Spendry */ 86565963Spendry vput(ap->a_dvp); 86665963Spendry vput(ap->a_vp); 86765963Spendry error = EROFS; 86865963Spendry } 86965963Spendry 87065963Spendry return (error); 87165963Spendry } 87265963Spendry 87365963Spendry int 87465963Spendry union_link(ap) 87565963Spendry struct vop_link_args /* { 87665963Spendry struct vnode *a_vp; 87765963Spendry struct vnode *a_tdvp; 87865963Spendry struct componentname *a_cnp; 87965963Spendry } */ *ap; 88065963Spendry { 88165963Spendry int error; 88265963Spendry struct union_node *dun = VTOUNION(ap->a_vp); 88365963Spendry struct union_node *un = VTOUNION(ap->a_tdvp); 88465963Spendry 885*67076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 88665963Spendry struct vnode *dvp = dun->un_uppervp; 88765963Spendry struct vnode *vp = un->un_uppervp; 88865963Spendry 88966152Spendry FIXUP(dun); 89065963Spendry VREF(dvp); 89166051Spendry dun->un_flags |= UN_KLOCK; 89265963Spendry vput(ap->a_vp); 89366152Spendry FIXUP(un); 89465963Spendry VREF(vp); 89565963Spendry vrele(ap->a_tdvp); 89665963Spendry 89765963Spendry error = VOP_LINK(dvp, vp, ap->a_cnp); 89865963Spendry } else { 89965963Spendry /* 900*67076Spendry * XXX: perhaps could copy to upper layer 90165963Spendry * and do the link there. 90265963Spendry */ 90365963Spendry vput(ap->a_vp); 90465963Spendry vrele(ap->a_tdvp); 90565963Spendry error = EROFS; 90665963Spendry } 90765963Spendry 90865963Spendry return (error); 90965963Spendry } 91065963Spendry 91165963Spendry int 91265963Spendry union_rename(ap) 91365963Spendry struct vop_rename_args /* { 91465963Spendry struct vnode *a_fdvp; 91565963Spendry struct vnode *a_fvp; 91665963Spendry struct componentname *a_fcnp; 91765963Spendry struct vnode *a_tdvp; 91865963Spendry struct vnode *a_tvp; 91965963Spendry struct componentname *a_tcnp; 92065963Spendry } */ *ap; 92165963Spendry { 92265963Spendry int error; 92365963Spendry 92465963Spendry struct vnode *fdvp = ap->a_fdvp; 92565963Spendry struct vnode *fvp = ap->a_fvp; 92665963Spendry struct vnode *tdvp = ap->a_tdvp; 92765963Spendry struct vnode *tvp = ap->a_tvp; 92865963Spendry 92965963Spendry if (fdvp->v_op == union_vnodeop_p) { /* always true */ 93065963Spendry struct union_node *un = VTOUNION(fdvp); 93165997Spendry if (un->un_uppervp == NULLVP) { 93265963Spendry error = EROFS; 93365963Spendry goto bad; 93465963Spendry } 93565963Spendry 93665963Spendry fdvp = un->un_uppervp; 93765963Spendry VREF(fdvp); 93865963Spendry vrele(ap->a_fdvp); 93965963Spendry } 94065963Spendry 94165963Spendry if (fvp->v_op == union_vnodeop_p) { /* always true */ 94265963Spendry struct union_node *un = VTOUNION(fvp); 94365997Spendry if (un->un_uppervp == NULLVP) { 94465963Spendry error = EROFS; 94565963Spendry goto bad; 94665963Spendry } 94765963Spendry 94865963Spendry fvp = un->un_uppervp; 94965963Spendry VREF(fvp); 95065963Spendry vrele(ap->a_fvp); 95165963Spendry } 95265963Spendry 95365963Spendry if (tdvp->v_op == union_vnodeop_p) { 95465963Spendry struct union_node *un = VTOUNION(tdvp); 95565997Spendry if (un->un_uppervp == NULLVP) { 956*67076Spendry /* 957*67076Spendry * this should never happen in normal 958*67076Spendry * operation but might if there was 959*67076Spendry * a problem creating the top-level shadow 960*67076Spendry * directory. 961*67076Spendry */ 96265963Spendry error = EROFS; 96365963Spendry goto bad; 96465963Spendry } 96565963Spendry 96665963Spendry tdvp = un->un_uppervp; 96765963Spendry VREF(tdvp); 96866051Spendry un->un_flags |= UN_KLOCK; 96965997Spendry vput(ap->a_tdvp); 97065963Spendry } 97165963Spendry 972*67076Spendry if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 97365963Spendry struct union_node *un = VTOUNION(tvp); 97465963Spendry 97565963Spendry tvp = un->un_uppervp; 976*67076Spendry if (tvp != NULLVP) { 977*67076Spendry VREF(tvp); 978*67076Spendry un->un_flags |= UN_KLOCK; 979*67076Spendry } 98065963Spendry vput(ap->a_tvp); 98165963Spendry } 98265963Spendry 98365963Spendry return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 98465963Spendry 98565963Spendry bad: 98665963Spendry vrele(fdvp); 98765963Spendry vrele(fvp); 98865963Spendry vput(tdvp); 989*67076Spendry if (tvp != NULLVP) 99065963Spendry vput(tvp); 99165963Spendry 99265963Spendry return (error); 99365963Spendry } 99465963Spendry 99565963Spendry int 99665963Spendry union_mkdir(ap) 99765963Spendry struct vop_mkdir_args /* { 99865963Spendry struct vnode *a_dvp; 99965963Spendry struct vnode **a_vpp; 100065963Spendry struct componentname *a_cnp; 100165963Spendry struct vattr *a_vap; 100265963Spendry } */ *ap; 100365963Spendry { 100465963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 100565963Spendry struct vnode *dvp = un->un_uppervp; 100665963Spendry 1007*67076Spendry if (dvp != NULLVP) { 100865963Spendry int error; 100965963Spendry struct vnode *vp; 101065963Spendry 101166152Spendry FIXUP(un); 101265963Spendry VREF(dvp); 101366051Spendry un->un_flags |= UN_KLOCK; 101465963Spendry vput(ap->a_dvp); 101565963Spendry error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 101665963Spendry if (error) 101765963Spendry return (error); 101865963Spendry 101965963Spendry error = union_allocvp( 102065963Spendry ap->a_vpp, 102165989Spendry ap->a_dvp->v_mount, 102265989Spendry ap->a_dvp, 102365965Spendry NULLVP, 102465963Spendry ap->a_cnp, 102565963Spendry vp, 102665963Spendry NULLVP); 102765965Spendry if (error) 102866051Spendry vput(vp); 102965963Spendry return (error); 103065963Spendry } 103165963Spendry 103265963Spendry vput(ap->a_dvp); 103365963Spendry return (EROFS); 103465963Spendry } 103565963Spendry 103665963Spendry int 103765963Spendry union_rmdir(ap) 103865963Spendry struct vop_rmdir_args /* { 103965963Spendry struct vnode *a_dvp; 104065963Spendry struct vnode *a_vp; 104165963Spendry struct componentname *a_cnp; 104265963Spendry } */ *ap; 104365963Spendry { 104465963Spendry int error; 104565963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 104665963Spendry struct union_node *un = VTOUNION(ap->a_vp); 104765963Spendry 1048*67076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 104965963Spendry struct vnode *dvp = dun->un_uppervp; 105065963Spendry struct vnode *vp = un->un_uppervp; 105165963Spendry 105266152Spendry FIXUP(dun); 105365963Spendry VREF(dvp); 105466051Spendry dun->un_flags |= UN_KLOCK; 105565963Spendry vput(ap->a_dvp); 105666152Spendry FIXUP(un); 105765963Spendry VREF(vp); 105866051Spendry un->un_flags |= UN_KLOCK; 105965963Spendry vput(ap->a_vp); 106065963Spendry 106166051Spendry error = VOP_RMDIR(dvp, vp, ap->a_cnp); 106266027Spendry if (!error) 106366027Spendry union_removed_upper(un); 106466027Spendry 106566027Spendry /* 106666027Spendry * XXX: should create a whiteout here 106766027Spendry */ 106865963Spendry } else { 106965963Spendry /* 107065963Spendry * XXX: should create a whiteout here 107165963Spendry */ 107265963Spendry vput(ap->a_dvp); 107365963Spendry vput(ap->a_vp); 107465963Spendry error = EROFS; 107565963Spendry } 107665963Spendry 107765963Spendry return (error); 107865963Spendry } 107965963Spendry 108065963Spendry int 108165963Spendry union_symlink(ap) 108265963Spendry struct vop_symlink_args /* { 108365963Spendry struct vnode *a_dvp; 108465963Spendry struct vnode **a_vpp; 108565963Spendry struct componentname *a_cnp; 108665963Spendry struct vattr *a_vap; 108765963Spendry char *a_target; 108865963Spendry } */ *ap; 108965963Spendry { 109065963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 109165963Spendry struct vnode *dvp = un->un_uppervp; 109265963Spendry 1093*67076Spendry if (dvp != NULLVP) { 109465963Spendry int error; 109565963Spendry struct vnode *vp; 109665963Spendry struct mount *mp = ap->a_dvp->v_mount; 109765963Spendry 109866152Spendry FIXUP(un); 109965963Spendry VREF(dvp); 110066051Spendry un->un_flags |= UN_KLOCK; 110165963Spendry vput(ap->a_dvp); 110265963Spendry error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 110365963Spendry ap->a_vap, ap->a_target); 110465997Spendry *ap->a_vpp = NULLVP; 110565963Spendry return (error); 110665963Spendry } 110765963Spendry 110865963Spendry vput(ap->a_dvp); 110965963Spendry return (EROFS); 111065963Spendry } 111165963Spendry 111265935Spendry /* 111365935Spendry * union_readdir works in concert with getdirentries and 111465935Spendry * readdir(3) to provide a list of entries in the unioned 111565935Spendry * directories. getdirentries is responsible for walking 111665935Spendry * down the union stack. readdir(3) is responsible for 111765935Spendry * eliminating duplicate names from the returned data stream. 111865935Spendry */ 111965935Spendry int 112065935Spendry union_readdir(ap) 112165935Spendry struct vop_readdir_args /* { 112265935Spendry struct vnodeop_desc *a_desc; 112365935Spendry struct vnode *a_vp; 112465935Spendry struct uio *a_uio; 112565935Spendry struct ucred *a_cred; 112665935Spendry } */ *ap; 112765935Spendry { 112865963Spendry int error = 0; 112965935Spendry struct union_node *un = VTOUNION(ap->a_vp); 113065935Spendry 1131*67076Spendry if (un->un_uppervp != NULLVP) { 113266152Spendry FIXUP(un); 113366051Spendry error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred); 113466152Spendry } 113565935Spendry 113665963Spendry return (error); 113765935Spendry } 113865935Spendry 113965935Spendry int 114065963Spendry union_readlink(ap) 114165963Spendry struct vop_readlink_args /* { 114265963Spendry struct vnode *a_vp; 114365963Spendry struct uio *a_uio; 114465963Spendry struct ucred *a_cred; 114565963Spendry } */ *ap; 114665963Spendry { 114765963Spendry int error; 114865963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 114966051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 115065963Spendry 115166051Spendry if (dolock) 115266051Spendry VOP_LOCK(vp); 115366152Spendry else 115466152Spendry FIXUP(VTOUNION(ap->a_vp)); 115565963Spendry error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 115666051Spendry if (dolock) 115766051Spendry VOP_UNLOCK(vp); 115865963Spendry 115965963Spendry return (error); 116065963Spendry } 116165963Spendry 116265963Spendry int 116365963Spendry union_abortop(ap) 116465963Spendry struct vop_abortop_args /* { 116565963Spendry struct vnode *a_dvp; 116665963Spendry struct componentname *a_cnp; 116765963Spendry } */ *ap; 116865963Spendry { 116965963Spendry int error; 117065965Spendry struct vnode *vp = OTHERVP(ap->a_dvp); 117165963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 117265963Spendry int islocked = un->un_flags & UN_LOCKED; 117366051Spendry int dolock = (vp == LOWERVP(ap->a_dvp)); 117465963Spendry 117566152Spendry if (islocked) { 117666152Spendry if (dolock) 117766152Spendry VOP_LOCK(vp); 117866152Spendry else 117966152Spendry FIXUP(VTOUNION(ap->a_dvp)); 118066152Spendry } 118165963Spendry error = VOP_ABORTOP(vp, ap->a_cnp); 118266051Spendry if (islocked && dolock) 118365963Spendry VOP_UNLOCK(vp); 118465963Spendry 118565963Spendry return (error); 118665963Spendry } 118765963Spendry 118865963Spendry int 118965935Spendry union_inactive(ap) 119065935Spendry struct vop_inactive_args /* { 119165935Spendry struct vnode *a_vp; 119265935Spendry } */ *ap; 119365935Spendry { 119467073Spendry struct union_node *un = VTOUNION(ap->a_vp); 119565935Spendry 119665935Spendry /* 119765935Spendry * Do nothing (and _don't_ bypass). 119865935Spendry * Wait to vrele lowervp until reclaim, 119965935Spendry * so that until then our union_node is in the 120065935Spendry * cache and reusable. 120165935Spendry * 120265935Spendry * NEEDSWORK: Someday, consider inactive'ing 120365935Spendry * the lowervp and then trying to reactivate it 120465935Spendry * with capabilities (v_id) 120565935Spendry * like they do in the name lookup cache code. 120665935Spendry * That's too much work for now. 120765935Spendry */ 120865989Spendry 120966027Spendry #ifdef UNION_DIAGNOSTIC 121065989Spendry if (un->un_flags & UN_LOCKED) 121165989Spendry panic("union: inactivating locked node"); 121267073Spendry if (un->un_flags & UN_ULOCK) 121367073Spendry panic("union: inactivating w/locked upper node"); 121465989Spendry #endif 121565989Spendry 121667073Spendry if ((un->un_flags & UN_CACHED) == 0) 121767073Spendry vgone(ap->a_vp); 121867073Spendry 121965935Spendry return (0); 122065935Spendry } 122165935Spendry 122265935Spendry int 122365935Spendry union_reclaim(ap) 122465935Spendry struct vop_reclaim_args /* { 122565935Spendry struct vnode *a_vp; 122665935Spendry } */ *ap; 122765935Spendry { 122865935Spendry 122966053Spendry union_freevp(ap->a_vp); 123066053Spendry 123165935Spendry return (0); 123265935Spendry } 123365935Spendry 123465963Spendry int 123565963Spendry union_lock(ap) 123665963Spendry struct vop_lock_args *ap; 123765963Spendry { 123866149Spendry struct vnode *vp = ap->a_vp; 123966149Spendry struct union_node *un; 124065935Spendry 124166149Spendry start: 124266149Spendry while (vp->v_flag & VXLOCK) { 124366149Spendry vp->v_flag |= VXWANT; 124466149Spendry sleep((caddr_t)vp, PINOD); 124566149Spendry } 124666149Spendry 124766149Spendry un = VTOUNION(vp); 124866149Spendry 1249*67076Spendry if (un->un_uppervp != NULLVP) { 125066051Spendry if ((un->un_flags & UN_ULOCK) == 0) { 125166149Spendry un->un_flags |= UN_ULOCK; 125266051Spendry VOP_LOCK(un->un_uppervp); 125366051Spendry } 125466051Spendry #ifdef DIAGNOSTIC 125566051Spendry if (un->un_flags & UN_KLOCK) 125666051Spendry panic("union: dangling upper lock"); 125766051Spendry #endif 125866051Spendry } 125966051Spendry 126066149Spendry if (un->un_flags & UN_LOCKED) { 126165963Spendry #ifdef DIAGNOSTIC 126265989Spendry if (curproc && un->un_pid == curproc->p_pid && 126365989Spendry un->un_pid > -1 && curproc->p_pid > -1) 126465989Spendry panic("union: locking against myself"); 126565963Spendry #endif 126665963Spendry un->un_flags |= UN_WANT; 126765963Spendry sleep((caddr_t) &un->un_flags, PINOD); 126866149Spendry goto start; 126965963Spendry } 127065989Spendry 127165963Spendry #ifdef DIAGNOSTIC 127265989Spendry if (curproc) 127365989Spendry un->un_pid = curproc->p_pid; 127465989Spendry else 127565989Spendry un->un_pid = -1; 127665963Spendry #endif 127766028Spendry 127866149Spendry un->un_flags |= UN_LOCKED; 127966028Spendry return (0); 128065963Spendry } 128165963Spendry 128265935Spendry int 128365963Spendry union_unlock(ap) 128465963Spendry struct vop_lock_args *ap; 128565963Spendry { 128665963Spendry struct union_node *un = VTOUNION(ap->a_vp); 128765963Spendry 128865963Spendry #ifdef DIAGNOSTIC 128965965Spendry if ((un->un_flags & UN_LOCKED) == 0) 129065965Spendry panic("union: unlock unlocked node"); 129165989Spendry if (curproc && un->un_pid != curproc->p_pid && 129265989Spendry curproc->p_pid > -1 && un->un_pid > -1) 129365963Spendry panic("union: unlocking other process's union node"); 129465963Spendry #endif 129565963Spendry 129665963Spendry un->un_flags &= ~UN_LOCKED; 129766051Spendry 129866051Spendry if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 129966051Spendry VOP_UNLOCK(un->un_uppervp); 130066051Spendry 130166051Spendry un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 130266051Spendry 130365963Spendry if (un->un_flags & UN_WANT) { 130465963Spendry un->un_flags &= ~UN_WANT; 130565963Spendry wakeup((caddr_t) &un->un_flags); 130665963Spendry } 130765963Spendry 130865963Spendry #ifdef DIAGNOSTIC 130965963Spendry un->un_pid = 0; 131065963Spendry #endif 131166028Spendry 131266028Spendry return (0); 131365963Spendry } 131465963Spendry 131565963Spendry int 131665963Spendry union_bmap(ap) 131765963Spendry struct vop_bmap_args /* { 131865963Spendry struct vnode *a_vp; 131965963Spendry daddr_t a_bn; 132065963Spendry struct vnode **a_vpp; 132165963Spendry daddr_t *a_bnp; 132265963Spendry int *a_runp; 132365963Spendry } */ *ap; 132465963Spendry { 132565963Spendry int error; 132665963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 132766051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 132865963Spendry 132966051Spendry if (dolock) 133066051Spendry VOP_LOCK(vp); 133166152Spendry else 133266152Spendry FIXUP(VTOUNION(ap->a_vp)); 133365963Spendry error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 133466051Spendry if (dolock) 133566051Spendry VOP_UNLOCK(vp); 133665963Spendry 133765963Spendry return (error); 133865963Spendry } 133965963Spendry 134065963Spendry int 134165935Spendry union_print(ap) 134265935Spendry struct vop_print_args /* { 134365935Spendry struct vnode *a_vp; 134465935Spendry } */ *ap; 134565935Spendry { 134665935Spendry struct vnode *vp = ap->a_vp; 134765935Spendry 134865935Spendry printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 134965935Spendry vp, UPPERVP(vp), LOWERVP(vp)); 135065935Spendry return (0); 135165935Spendry } 135265935Spendry 135365963Spendry int 135465963Spendry union_islocked(ap) 135565963Spendry struct vop_islocked_args /* { 135665963Spendry struct vnode *a_vp; 135765963Spendry } */ *ap; 135865963Spendry { 135965935Spendry 136065963Spendry return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 136165963Spendry } 136265963Spendry 136365935Spendry int 136465963Spendry union_pathconf(ap) 136565963Spendry struct vop_pathconf_args /* { 136665963Spendry struct vnode *a_vp; 136765963Spendry int a_name; 136865963Spendry int *a_retval; 136965935Spendry } */ *ap; 137065935Spendry { 137165935Spendry int error; 137265963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 137366051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 137465935Spendry 137566051Spendry if (dolock) 137666051Spendry VOP_LOCK(vp); 137766152Spendry else 137866152Spendry FIXUP(VTOUNION(ap->a_vp)); 137965963Spendry error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 138066051Spendry if (dolock) 138166051Spendry VOP_UNLOCK(vp); 138265935Spendry 138365963Spendry return (error); 138465963Spendry } 138565935Spendry 138665963Spendry int 138765963Spendry union_advlock(ap) 138865963Spendry struct vop_advlock_args /* { 138965963Spendry struct vnode *a_vp; 139065963Spendry caddr_t a_id; 139165963Spendry int a_op; 139265963Spendry struct flock *a_fl; 139365963Spendry int a_flags; 139465963Spendry } */ *ap; 139565963Spendry { 139665935Spendry 139765963Spendry return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 139865963Spendry ap->a_fl, ap->a_flags)); 139965935Spendry } 140065935Spendry 140165935Spendry 140265935Spendry /* 140365963Spendry * XXX - vop_strategy must be hand coded because it has no 140465935Spendry * vnode in its arguments. 140565935Spendry * This goes away with a merged VM/buffer cache. 140665935Spendry */ 140765935Spendry int 140865963Spendry union_strategy(ap) 140965963Spendry struct vop_strategy_args /* { 141065935Spendry struct buf *a_bp; 141165935Spendry } */ *ap; 141265935Spendry { 141365935Spendry struct buf *bp = ap->a_bp; 141465935Spendry int error; 141565935Spendry struct vnode *savedvp; 141665935Spendry 141765935Spendry savedvp = bp->b_vp; 141865963Spendry bp->b_vp = OTHERVP(bp->b_vp); 141965935Spendry 142065935Spendry #ifdef DIAGNOSTIC 142165997Spendry if (bp->b_vp == NULLVP) 142265963Spendry panic("union_strategy: nil vp"); 142365963Spendry if (((bp->b_flags & B_READ) == 0) && 142465963Spendry (bp->b_vp == LOWERVP(savedvp))) 142565963Spendry panic("union_strategy: writing to lowervp"); 142665935Spendry #endif 142765935Spendry 142865963Spendry error = VOP_STRATEGY(bp); 142965935Spendry bp->b_vp = savedvp; 143065935Spendry 143165935Spendry return (error); 143265935Spendry } 143365935Spendry 143465935Spendry /* 143565935Spendry * Global vfs data structures 143665935Spendry */ 143765935Spendry int (**union_vnodeop_p)(); 143865965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = { 143965963Spendry { &vop_default_desc, vn_default_error }, 144065963Spendry { &vop_lookup_desc, union_lookup }, /* lookup */ 144165963Spendry { &vop_create_desc, union_create }, /* create */ 144265963Spendry { &vop_mknod_desc, union_mknod }, /* mknod */ 144365963Spendry { &vop_open_desc, union_open }, /* open */ 144465963Spendry { &vop_close_desc, union_close }, /* close */ 144565963Spendry { &vop_access_desc, union_access }, /* access */ 144665963Spendry { &vop_getattr_desc, union_getattr }, /* getattr */ 144765963Spendry { &vop_setattr_desc, union_setattr }, /* setattr */ 144865963Spendry { &vop_read_desc, union_read }, /* read */ 144965963Spendry { &vop_write_desc, union_write }, /* write */ 145065963Spendry { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 145165963Spendry { &vop_select_desc, union_select }, /* select */ 145265963Spendry { &vop_mmap_desc, union_mmap }, /* mmap */ 145365963Spendry { &vop_fsync_desc, union_fsync }, /* fsync */ 145465963Spendry { &vop_seek_desc, union_seek }, /* seek */ 145565963Spendry { &vop_remove_desc, union_remove }, /* remove */ 145665963Spendry { &vop_link_desc, union_link }, /* link */ 145765963Spendry { &vop_rename_desc, union_rename }, /* rename */ 145865963Spendry { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 145965963Spendry { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 146065963Spendry { &vop_symlink_desc, union_symlink }, /* symlink */ 146165963Spendry { &vop_readdir_desc, union_readdir }, /* readdir */ 146265963Spendry { &vop_readlink_desc, union_readlink }, /* readlink */ 146365963Spendry { &vop_abortop_desc, union_abortop }, /* abortop */ 146465963Spendry { &vop_inactive_desc, union_inactive }, /* inactive */ 146565963Spendry { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 146665963Spendry { &vop_lock_desc, union_lock }, /* lock */ 146765963Spendry { &vop_unlock_desc, union_unlock }, /* unlock */ 146865963Spendry { &vop_bmap_desc, union_bmap }, /* bmap */ 146965963Spendry { &vop_strategy_desc, union_strategy }, /* strategy */ 147065963Spendry { &vop_print_desc, union_print }, /* print */ 147165963Spendry { &vop_islocked_desc, union_islocked }, /* islocked */ 147265963Spendry { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 147365963Spendry { &vop_advlock_desc, union_advlock }, /* advlock */ 147465963Spendry #ifdef notdef 147565963Spendry { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 147665963Spendry { &vop_valloc_desc, union_valloc }, /* valloc */ 147765963Spendry { &vop_vfree_desc, union_vfree }, /* vfree */ 147865963Spendry { &vop_truncate_desc, union_truncate }, /* truncate */ 147965963Spendry { &vop_update_desc, union_update }, /* update */ 148065963Spendry { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 148165963Spendry #endif 148265935Spendry { (struct vnodeop_desc*)NULL, (int(*)())NULL } 148365935Spendry }; 148465935Spendry struct vnodeopv_desc union_vnodeop_opv_desc = 148565935Spendry { &union_vnodeop_p, union_vnodeop_entries }; 1486