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*67416Spendry * @(#)union_vnops.c 8.17 (Berkeley) 06/17/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 */ 15467076Spendry 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 */ 17867076Spendry 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) { 20367076Spendry 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; 217*67416Spendry if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) { 218*67416Spendry lowervp = LOWERVP(dun->un_pvp); 219*67416Spendry if (lowervp != NULLVP) { 220*67416Spendry VREF(lowervp); 221*67416Spendry VOP_LOCK(lowervp); 222*67416Spendry lerror = 0; 223*67416Spendry } 224*67416Spendry } 22565935Spendry } 22665935Spendry 22765965Spendry if (!lockparent) 22865965Spendry cnp->cn_flags &= ~LOCKPARENT; 22965965Spendry 23065935Spendry /* 23165935Spendry * at this point, we have uerror and lerror indicating 23265935Spendry * possible errors with the lookups in the upper and lower 23365935Spendry * layers. additionally, uppervp and lowervp are (locked) 23465935Spendry * references to existing vnodes in the upper and lower layers. 23565935Spendry * 23665935Spendry * there are now three cases to consider. 23765935Spendry * 1. if both layers returned an error, then return whatever 23865935Spendry * error the upper layer generated. 23965935Spendry * 24065935Spendry * 2. if the top layer failed and the bottom layer succeeded 24165935Spendry * then two subcases occur. 24265935Spendry * a. the bottom vnode is not a directory, in which 24365935Spendry * case just return a new union vnode referencing 24465935Spendry * an empty top layer and the existing bottom layer. 24565935Spendry * b. the bottom vnode is a directory, in which case 24665935Spendry * create a new directory in the top-level and 24765935Spendry * continue as in case 3. 24865935Spendry * 24965935Spendry * 3. if the top layer succeeded then return a new union 25065935Spendry * vnode referencing whatever the new top layer and 25165935Spendry * whatever the bottom layer returned. 25265935Spendry */ 25365935Spendry 25466027Spendry *ap->a_vpp = NULLVP; 25566027Spendry 25665935Spendry /* case 1. */ 25765935Spendry if ((uerror != 0) && (lerror != 0)) { 25865935Spendry return (uerror); 25965935Spendry } 26065935Spendry 26165935Spendry /* case 2. */ 26265935Spendry if (uerror != 0 /* && (lerror == 0) */ ) { 26365935Spendry if (lowervp->v_type == VDIR) { /* case 2b. */ 26466051Spendry dun->un_flags &= ~UN_ULOCK; 26566051Spendry VOP_UNLOCK(upperdvp); 26665997Spendry uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); 26766051Spendry VOP_LOCK(upperdvp); 26866051Spendry dun->un_flags |= UN_ULOCK; 26966051Spendry 27065935Spendry if (uerror) { 27167076Spendry if (lowervp != NULLVP) { 27265935Spendry vput(lowervp); 27365997Spendry lowervp = NULLVP; 27465935Spendry } 27565935Spendry return (uerror); 27665935Spendry } 27765935Spendry } 27865935Spendry } 27965935Spendry 28067076Spendry if (lowervp != NULLVP) 28165965Spendry VOP_UNLOCK(lowervp); 28265965Spendry 28365997Spendry error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, 28465997Spendry uppervp, lowervp); 28565997Spendry 28665965Spendry if (error) { 28767076Spendry if (uppervp != NULLVP) 28866051Spendry vput(uppervp); 28967076Spendry if (lowervp != NULLVP) 29065965Spendry vrele(lowervp); 29165965Spendry } else { 29265994Spendry if (*ap->a_vpp != dvp) 29365994Spendry if (!lockparent || !(cnp->cn_flags & ISLASTCN)) 29465994Spendry VOP_UNLOCK(dvp); 29565965Spendry } 29665965Spendry 29765965Spendry return (error); 29865935Spendry } 29965935Spendry 30065963Spendry int 30165963Spendry union_create(ap) 30265963Spendry struct vop_create_args /* { 30365963Spendry struct vnode *a_dvp; 30465963Spendry struct vnode **a_vpp; 30565963Spendry struct componentname *a_cnp; 30665963Spendry struct vattr *a_vap; 30765963Spendry } */ *ap; 30865963Spendry { 30965963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 31065963Spendry struct vnode *dvp = un->un_uppervp; 31165963Spendry 31267076Spendry if (dvp != NULLVP) { 31365963Spendry int error; 31465963Spendry struct vnode *vp; 31565963Spendry 31666152Spendry FIXUP(un); 31766152Spendry 31865963Spendry VREF(dvp); 31966051Spendry un->un_flags |= UN_KLOCK; 32065963Spendry vput(ap->a_dvp); 32165963Spendry error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap); 32265963Spendry if (error) 32365963Spendry return (error); 32465963Spendry 32565963Spendry error = union_allocvp( 32665963Spendry ap->a_vpp, 32765989Spendry ap->a_dvp->v_mount, 32865989Spendry ap->a_dvp, 32965965Spendry NULLVP, 33065963Spendry ap->a_cnp, 33165963Spendry vp, 33265963Spendry NULLVP); 33365965Spendry if (error) 33466051Spendry vput(vp); 33565963Spendry return (error); 33665963Spendry } 33765963Spendry 33865963Spendry vput(ap->a_dvp); 33965963Spendry return (EROFS); 34065963Spendry } 34165963Spendry 34265963Spendry int 34365963Spendry union_mknod(ap) 34465963Spendry struct vop_mknod_args /* { 34565963Spendry struct vnode *a_dvp; 34665963Spendry struct vnode **a_vpp; 34765963Spendry struct componentname *a_cnp; 34865963Spendry struct vattr *a_vap; 34965963Spendry } */ *ap; 35065963Spendry { 35165963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 35265963Spendry struct vnode *dvp = un->un_uppervp; 35365963Spendry 35467076Spendry if (dvp != NULLVP) { 35565963Spendry int error; 35665963Spendry struct vnode *vp; 35765963Spendry 35866152Spendry FIXUP(un); 35966152Spendry 36065963Spendry VREF(dvp); 36166051Spendry un->un_flags |= UN_KLOCK; 36265963Spendry vput(ap->a_dvp); 36365963Spendry error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap); 36465963Spendry if (error) 36565963Spendry return (error); 36665963Spendry 36767076Spendry if (vp != NULLVP) { 36865965Spendry error = union_allocvp( 36965965Spendry ap->a_vpp, 37065989Spendry ap->a_dvp->v_mount, 37165989Spendry ap->a_dvp, 37265965Spendry NULLVP, 37365965Spendry ap->a_cnp, 37465965Spendry vp, 37565965Spendry NULLVP); 37665965Spendry if (error) 37766051Spendry vput(vp); 37865965Spendry } 37965963Spendry return (error); 38065963Spendry } 38165963Spendry 38265963Spendry vput(ap->a_dvp); 38365963Spendry return (EROFS); 38465963Spendry } 38565963Spendry 38665935Spendry int 38765935Spendry union_open(ap) 38865935Spendry struct vop_open_args /* { 38965935Spendry struct vnodeop_desc *a_desc; 39065935Spendry struct vnode *a_vp; 39165935Spendry int a_mode; 39265935Spendry struct ucred *a_cred; 39365935Spendry struct proc *a_p; 39465935Spendry } */ *ap; 39565935Spendry { 39665935Spendry struct union_node *un = VTOUNION(ap->a_vp); 39765965Spendry struct vnode *tvp; 39865935Spendry int mode = ap->a_mode; 39965935Spendry struct ucred *cred = ap->a_cred; 40065935Spendry struct proc *p = ap->a_p; 40165965Spendry int error; 40265935Spendry 40365935Spendry /* 40465935Spendry * If there is an existing upper vp then simply open that. 40565935Spendry */ 40665965Spendry tvp = un->un_uppervp; 40765965Spendry if (tvp == NULLVP) { 40865935Spendry /* 40965965Spendry * If the lower vnode is being opened for writing, then 41065965Spendry * copy the file contents to the upper vnode and open that, 41165965Spendry * otherwise can simply open the lower vnode. 41265935Spendry */ 41365965Spendry tvp = un->un_lowervp; 41465965Spendry if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { 41567169Spendry error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p); 41665965Spendry if (error == 0) 41765965Spendry error = VOP_OPEN(un->un_uppervp, mode, cred, p); 41865965Spendry return (error); 41965935Spendry } 42066051Spendry 42166051Spendry /* 42266051Spendry * Just open the lower vnode 42366051Spendry */ 42466027Spendry un->un_openl++; 42566051Spendry VOP_LOCK(tvp); 42666051Spendry error = VOP_OPEN(tvp, mode, cred, p); 42766051Spendry VOP_UNLOCK(tvp); 42866051Spendry 42966051Spendry return (error); 43065935Spendry } 43165935Spendry 43266152Spendry FIXUP(un); 43366152Spendry 43465965Spendry error = VOP_OPEN(tvp, mode, cred, p); 43565965Spendry 43665965Spendry return (error); 43765935Spendry } 43865935Spendry 43965963Spendry int 44065963Spendry union_close(ap) 44165963Spendry struct vop_close_args /* { 44265963Spendry struct vnode *a_vp; 44365963Spendry int a_fflag; 44465963Spendry struct ucred *a_cred; 44565963Spendry struct proc *a_p; 44665963Spendry } */ *ap; 44765963Spendry { 44865997Spendry struct union_node *un = VTOUNION(ap->a_vp); 44965997Spendry struct vnode *vp; 45065963Spendry 45167076Spendry if (un->un_uppervp != NULLVP) { 45265997Spendry vp = un->un_uppervp; 45365997Spendry } else { 45466027Spendry #ifdef UNION_DIAGNOSTIC 45566027Spendry if (un->un_openl <= 0) 45666027Spendry panic("union: un_openl cnt"); 45765997Spendry #endif 45866027Spendry --un->un_openl; 45965997Spendry vp = un->un_lowervp; 46065997Spendry } 46166027Spendry 46265997Spendry return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p)); 46365963Spendry } 46465963Spendry 46565935Spendry /* 46665963Spendry * Check access permission on the union vnode. 46765963Spendry * The access check being enforced is to check 46865963Spendry * against both the underlying vnode, and any 46965963Spendry * copied vnode. This ensures that no additional 47065963Spendry * file permissions are given away simply because 47165963Spendry * the user caused an implicit file copy. 47265963Spendry */ 47365963Spendry int 47465963Spendry union_access(ap) 47565963Spendry struct vop_access_args /* { 47665963Spendry struct vnodeop_desc *a_desc; 47765963Spendry struct vnode *a_vp; 47865963Spendry int a_mode; 47965963Spendry struct ucred *a_cred; 48065963Spendry struct proc *a_p; 48165963Spendry } */ *ap; 48265963Spendry { 48365963Spendry struct union_node *un = VTOUNION(ap->a_vp); 48466149Spendry int error = EACCES; 48565963Spendry struct vnode *vp; 48665963Spendry 48767076Spendry if ((vp = un->un_uppervp) != NULLVP) { 48866152Spendry FIXUP(un); 48966152Spendry return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); 49066152Spendry } 49166152Spendry 49267076Spendry if ((vp = un->un_lowervp) != NULLVP) { 49365965Spendry VOP_LOCK(vp); 49465963Spendry error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p); 49566152Spendry if (error == 0) { 49666152Spendry struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount); 49766152Spendry 49866152Spendry if (um->um_op == UNMNT_BELOW) 49966152Spendry error = VOP_ACCESS(vp, ap->a_mode, 50066152Spendry um->um_cred, ap->a_p); 50166152Spendry } 50265965Spendry VOP_UNLOCK(vp); 50365963Spendry if (error) 50465963Spendry return (error); 50565963Spendry } 50665963Spendry 50765965Spendry return (error); 50865963Spendry } 50965963Spendry 51065963Spendry /* 51167109Spendry * We handle getattr only to change the fsid and 51267109Spendry * track object sizes 51365935Spendry */ 51465935Spendry int 51565935Spendry union_getattr(ap) 51665935Spendry struct vop_getattr_args /* { 51765935Spendry struct vnode *a_vp; 51865935Spendry struct vattr *a_vap; 51965935Spendry struct ucred *a_cred; 52065935Spendry struct proc *a_p; 52165935Spendry } */ *ap; 52265935Spendry { 52365935Spendry int error; 52466062Spendry struct union_node *un = VTOUNION(ap->a_vp); 52566062Spendry struct vnode *vp = un->un_uppervp; 52666062Spendry struct vattr *vap; 52766062Spendry struct vattr va; 52865935Spendry 52966062Spendry 53066062Spendry /* 53166062Spendry * Some programs walk the filesystem hierarchy by counting 53266062Spendry * links to directories to avoid stat'ing all the time. 53366062Spendry * This means the link count on directories needs to be "correct". 53466062Spendry * The only way to do that is to call getattr on both layers 53566062Spendry * and fix up the link count. The link count will not necessarily 53666062Spendry * be accurate but will be large enough to defeat the tree walkers. 53766062Spendry */ 53866062Spendry 53966062Spendry vap = ap->a_vap; 54066062Spendry 54166062Spendry vp = un->un_uppervp; 54266062Spendry if (vp != NULLVP) { 54367073Spendry /* 54467073Spendry * It's not clear whether VOP_GETATTR is to be 54567073Spendry * called with the vnode locked or not. stat() calls 54667073Spendry * it with (vp) locked, and fstat calls it with 54767073Spendry * (vp) unlocked. 54867073Spendry * In the mean time, compensate here by checking 54967073Spendry * the union_node's lock flag. 55067073Spendry */ 55167073Spendry if (un->un_flags & UN_LOCKED) 55267073Spendry FIXUP(un); 55367073Spendry 55466062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 55566062Spendry if (error) 55666062Spendry return (error); 55767109Spendry union_newsize(ap->a_vp, vap->va_size, VNOVAL); 55866062Spendry } 55966062Spendry 56066062Spendry if (vp == NULLVP) { 56166062Spendry vp = un->un_lowervp; 56266062Spendry } else if (vp->v_type == VDIR) { 56366062Spendry vp = un->un_lowervp; 56466062Spendry vap = &va; 56566062Spendry } else { 56666062Spendry vp = NULLVP; 56766062Spendry } 56866062Spendry 56966062Spendry if (vp != NULLVP) { 57066051Spendry VOP_LOCK(vp); 57166062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 57266051Spendry VOP_UNLOCK(vp); 57366062Spendry if (error) 57466062Spendry return (error); 57567109Spendry union_newsize(ap->a_vp, VNOVAL, vap->va_size); 57666062Spendry } 57765965Spendry 57866062Spendry if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 57966062Spendry ap->a_vap->va_nlink += vap->va_nlink; 58066062Spendry 58167400Spendry ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 58265935Spendry return (0); 58365935Spendry } 58465935Spendry 58565963Spendry int 58665965Spendry union_setattr(ap) 58765963Spendry struct vop_setattr_args /* { 58865963Spendry struct vnode *a_vp; 58965963Spendry struct vattr *a_vap; 59065963Spendry struct ucred *a_cred; 59165963Spendry struct proc *a_p; 59265963Spendry } */ *ap; 59365963Spendry { 59465963Spendry struct union_node *un = VTOUNION(ap->a_vp); 59565963Spendry int error; 59665963Spendry 59766057Spendry /* 59866057Spendry * Handle case of truncating lower object to zero size, 59966057Spendry * by creating a zero length upper object. This is to 60066057Spendry * handle the case of open with O_TRUNC and O_CREAT. 60166057Spendry */ 60266057Spendry if ((un->un_uppervp == NULLVP) && 60366057Spendry /* assert(un->un_lowervp != NULLVP) */ 60466057Spendry (un->un_lowervp->v_type == VREG) && 60566057Spendry (ap->a_vap->va_size == 0)) { 60666057Spendry struct vnode *vp; 60766057Spendry 60866057Spendry error = union_vn_create(&vp, un, ap->a_p); 60966057Spendry if (error) 61066057Spendry return (error); 61166057Spendry 61266057Spendry /* at this point, uppervp is locked */ 61366057Spendry union_newupper(un, vp); 61466057Spendry 61566057Spendry VOP_UNLOCK(vp); 61666057Spendry union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p); 61766057Spendry VOP_LOCK(vp); 61866057Spendry un->un_flags |= UN_ULOCK; 61966057Spendry } 62066057Spendry 62166057Spendry /* 62266057Spendry * Try to set attributes in upper layer, 62366057Spendry * otherwise return read-only filesystem error. 62466057Spendry */ 62566057Spendry if (un->un_uppervp != NULLVP) { 62666152Spendry FIXUP(un); 62765963Spendry error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 62865963Spendry ap->a_cred, ap->a_p); 62967109Spendry if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 63067109Spendry union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 63165963Spendry } else { 63265963Spendry error = EROFS; 63365963Spendry } 63465963Spendry 63565963Spendry return (error); 63665963Spendry } 63765963Spendry 63865963Spendry int 63965963Spendry union_read(ap) 64065963Spendry struct vop_read_args /* { 64165963Spendry struct vnode *a_vp; 64265963Spendry struct uio *a_uio; 64365963Spendry int a_ioflag; 64465963Spendry struct ucred *a_cred; 64565963Spendry } */ *ap; 64665963Spendry { 64765963Spendry int error; 64865963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 64966051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 65065963Spendry 65166051Spendry if (dolock) 65266051Spendry VOP_LOCK(vp); 65366152Spendry else 65466152Spendry FIXUP(VTOUNION(ap->a_vp)); 65565963Spendry error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 65666051Spendry if (dolock) 65766051Spendry VOP_UNLOCK(vp); 65865963Spendry 65967109Spendry /* 66067109Spendry * XXX 66167109Spendry * perhaps the size of the underlying object has changed under 66267109Spendry * our feet. take advantage of the offset information present 66367109Spendry * in the uio structure. 66467109Spendry */ 66567109Spendry if (error == 0) { 66667109Spendry struct union_node *un = VTOUNION(ap->a_vp); 66767109Spendry off_t cur = ap->a_uio->uio_offset; 66867109Spendry 66967109Spendry if (vp == un->un_uppervp) { 67067109Spendry if (cur > un->un_uppersz) 67167109Spendry union_newsize(ap->a_vp, cur, VNOVAL); 67267109Spendry } else { 67367109Spendry if (cur > un->un_lowersz) 67467109Spendry union_newsize(ap->a_vp, VNOVAL, cur); 67567109Spendry } 67667109Spendry } 67767109Spendry 67865963Spendry return (error); 67965963Spendry } 68065963Spendry 68165963Spendry int 68265963Spendry union_write(ap) 68365963Spendry struct vop_read_args /* { 68465963Spendry struct vnode *a_vp; 68565963Spendry struct uio *a_uio; 68665963Spendry int a_ioflag; 68765963Spendry struct ucred *a_cred; 68865963Spendry } */ *ap; 68965963Spendry { 69065963Spendry int error; 69165963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 69266051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 69365963Spendry 69466051Spendry if (dolock) 69566051Spendry VOP_LOCK(vp); 69666152Spendry else 69766152Spendry FIXUP(VTOUNION(ap->a_vp)); 69865963Spendry error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 69966051Spendry if (dolock) 70066051Spendry VOP_UNLOCK(vp); 70165963Spendry 70267109Spendry /* 70367109Spendry * the size of the underlying object may be changed by the 70467109Spendry * write. 70567109Spendry */ 70667109Spendry if (error == 0) { 70767109Spendry struct union_node *un = VTOUNION(ap->a_vp); 70867109Spendry off_t cur = ap->a_uio->uio_offset; 70967109Spendry 71067109Spendry if (vp == un->un_uppervp) { 71167109Spendry if (cur > un->un_uppersz) 71267109Spendry union_newsize(ap->a_vp, cur, VNOVAL); 71367109Spendry } else { 71467109Spendry if (cur > un->un_lowersz) 71567109Spendry union_newsize(ap->a_vp, VNOVAL, cur); 71667109Spendry } 71767109Spendry } 71867109Spendry 71965963Spendry return (error); 72065963Spendry } 72165963Spendry 72265963Spendry int 72365963Spendry union_ioctl(ap) 72465963Spendry struct vop_ioctl_args /* { 72565963Spendry struct vnode *a_vp; 72665963Spendry int a_command; 72765963Spendry caddr_t a_data; 72865963Spendry int a_fflag; 72965963Spendry struct ucred *a_cred; 73065963Spendry struct proc *a_p; 73165963Spendry } */ *ap; 73265963Spendry { 73365963Spendry 73465963Spendry return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 73565963Spendry ap->a_fflag, ap->a_cred, ap->a_p)); 73665963Spendry } 73765963Spendry 73865963Spendry int 73965963Spendry union_select(ap) 74065963Spendry struct vop_select_args /* { 74165963Spendry struct vnode *a_vp; 74265963Spendry int a_which; 74365963Spendry int a_fflags; 74465963Spendry struct ucred *a_cred; 74565963Spendry struct proc *a_p; 74665963Spendry } */ *ap; 74765963Spendry { 74865963Spendry 74965963Spendry return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 75065963Spendry ap->a_cred, ap->a_p)); 75165963Spendry } 75265963Spendry 75365963Spendry int 75465963Spendry union_mmap(ap) 75565963Spendry struct vop_mmap_args /* { 75665963Spendry struct vnode *a_vp; 75765963Spendry int a_fflags; 75865963Spendry struct ucred *a_cred; 75965963Spendry struct proc *a_p; 76065963Spendry } */ *ap; 76165963Spendry { 76265963Spendry 76365963Spendry return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 76465963Spendry ap->a_cred, ap->a_p)); 76565963Spendry } 76665963Spendry 76765963Spendry int 76865963Spendry union_fsync(ap) 76965963Spendry struct vop_fsync_args /* { 77065963Spendry struct vnode *a_vp; 77165963Spendry struct ucred *a_cred; 77265963Spendry int a_waitfor; 77365963Spendry struct proc *a_p; 77465963Spendry } */ *ap; 77565963Spendry { 77665963Spendry int error = 0; 77765963Spendry struct vnode *targetvp = OTHERVP(ap->a_vp); 77865963Spendry 77967076Spendry if (targetvp != NULLVP) { 78066051Spendry int dolock = (targetvp == LOWERVP(ap->a_vp)); 78166051Spendry 78266051Spendry if (dolock) 78366051Spendry VOP_LOCK(targetvp); 78466152Spendry else 78566152Spendry FIXUP(VTOUNION(ap->a_vp)); 78665963Spendry error = VOP_FSYNC(targetvp, ap->a_cred, 78765963Spendry ap->a_waitfor, ap->a_p); 78866051Spendry if (dolock) 78966051Spendry VOP_UNLOCK(targetvp); 79065963Spendry } 79165963Spendry 79265963Spendry return (error); 79365963Spendry } 79465963Spendry 79565963Spendry int 79665963Spendry union_seek(ap) 79765963Spendry struct vop_seek_args /* { 79865963Spendry struct vnode *a_vp; 79965963Spendry off_t a_oldoff; 80065963Spendry off_t a_newoff; 80165963Spendry struct ucred *a_cred; 80265963Spendry } */ *ap; 80365963Spendry { 80465963Spendry 80565963Spendry return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 80665963Spendry } 80765963Spendry 80865963Spendry int 80965963Spendry union_remove(ap) 81065963Spendry struct vop_remove_args /* { 81165963Spendry struct vnode *a_dvp; 81265963Spendry struct vnode *a_vp; 81365963Spendry struct componentname *a_cnp; 81465963Spendry } */ *ap; 81565963Spendry { 81665963Spendry int error; 81765963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 81865963Spendry struct union_node *un = VTOUNION(ap->a_vp); 81965963Spendry 82067076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 82165963Spendry struct vnode *dvp = dun->un_uppervp; 82265963Spendry struct vnode *vp = un->un_uppervp; 82365963Spendry 82466152Spendry FIXUP(dun); 82565963Spendry VREF(dvp); 82666051Spendry dun->un_flags |= UN_KLOCK; 82765963Spendry vput(ap->a_dvp); 82866152Spendry FIXUP(un); 82965963Spendry VREF(vp); 83066051Spendry un->un_flags |= UN_KLOCK; 83165963Spendry vput(ap->a_vp); 83265963Spendry 83365963Spendry error = VOP_REMOVE(dvp, vp, ap->a_cnp); 83466027Spendry if (!error) 83566027Spendry union_removed_upper(un); 83666027Spendry 83766027Spendry /* 83866027Spendry * XXX: should create a whiteout here 83966027Spendry */ 84065963Spendry } else { 84165963Spendry /* 84265963Spendry * XXX: should create a whiteout here 84365963Spendry */ 84465963Spendry vput(ap->a_dvp); 84565963Spendry vput(ap->a_vp); 84665963Spendry error = EROFS; 84765963Spendry } 84865963Spendry 84965963Spendry return (error); 85065963Spendry } 85165963Spendry 85265963Spendry int 85365963Spendry union_link(ap) 85465963Spendry struct vop_link_args /* { 85565963Spendry struct vnode *a_vp; 85665963Spendry struct vnode *a_tdvp; 85765963Spendry struct componentname *a_cnp; 85865963Spendry } */ *ap; 85965963Spendry { 86067169Spendry int error = 0; 86167169Spendry struct union_node *un; 86267169Spendry struct vnode *vp; 86367169Spendry struct vnode *tdvp; 86465963Spendry 86567169Spendry un = VTOUNION(ap->a_vp); 86665963Spendry 86767169Spendry if (ap->a_vp->v_op != ap->a_tdvp->v_op) { 86867169Spendry tdvp = ap->a_tdvp; 86967169Spendry } else { 87067169Spendry struct union_node *tdun = VTOUNION(ap->a_tdvp); 87167169Spendry if (tdun->un_uppervp == NULLVP) { 87267169Spendry VOP_LOCK(ap->a_tdvp); 87367169Spendry if (un->un_uppervp == tdun->un_dirvp) { 87467169Spendry un->un_flags &= ~UN_ULOCK; 87567169Spendry VOP_UNLOCK(un->un_uppervp); 87667169Spendry } 87767169Spendry error = union_copyup(tdun, 1, ap->a_cnp->cn_cred, 87867169Spendry ap->a_cnp->cn_proc); 87967169Spendry if (un->un_uppervp == tdun->un_dirvp) { 88067169Spendry VOP_LOCK(un->un_uppervp); 88167169Spendry un->un_flags |= UN_ULOCK; 88267169Spendry } 88367169Spendry VOP_UNLOCK(ap->a_tdvp); 88467169Spendry } 88567169Spendry tdvp = tdun->un_uppervp; 88667169Spendry } 88765963Spendry 88867169Spendry vp = un->un_uppervp; 88967169Spendry if (vp == NULLVP) 89067169Spendry error = EROFS; 89167169Spendry 89267169Spendry if (error) { 89365963Spendry vput(ap->a_vp); 89467169Spendry return (error); 89565963Spendry } 89665963Spendry 89767169Spendry FIXUP(un); 89867169Spendry VREF(vp); 89967169Spendry un->un_flags |= UN_KLOCK; 90067169Spendry vput(ap->a_vp); 90167169Spendry 90267169Spendry return (VOP_LINK(vp, tdvp, ap->a_cnp)); 90365963Spendry } 90465963Spendry 90565963Spendry int 90665963Spendry union_rename(ap) 90765963Spendry struct vop_rename_args /* { 90865963Spendry struct vnode *a_fdvp; 90965963Spendry struct vnode *a_fvp; 91065963Spendry struct componentname *a_fcnp; 91165963Spendry struct vnode *a_tdvp; 91265963Spendry struct vnode *a_tvp; 91365963Spendry struct componentname *a_tcnp; 91465963Spendry } */ *ap; 91565963Spendry { 91665963Spendry int error; 91765963Spendry 91865963Spendry struct vnode *fdvp = ap->a_fdvp; 91965963Spendry struct vnode *fvp = ap->a_fvp; 92065963Spendry struct vnode *tdvp = ap->a_tdvp; 92165963Spendry struct vnode *tvp = ap->a_tvp; 92265963Spendry 92365963Spendry if (fdvp->v_op == union_vnodeop_p) { /* always true */ 92465963Spendry struct union_node *un = VTOUNION(fdvp); 92565997Spendry if (un->un_uppervp == NULLVP) { 92665963Spendry error = EROFS; 92765963Spendry goto bad; 92865963Spendry } 92965963Spendry 93065963Spendry fdvp = un->un_uppervp; 93165963Spendry VREF(fdvp); 93265963Spendry vrele(ap->a_fdvp); 93365963Spendry } 93465963Spendry 93565963Spendry if (fvp->v_op == union_vnodeop_p) { /* always true */ 93665963Spendry struct union_node *un = VTOUNION(fvp); 93765997Spendry if (un->un_uppervp == NULLVP) { 93865963Spendry error = EROFS; 93965963Spendry goto bad; 94065963Spendry } 94165963Spendry 94265963Spendry fvp = un->un_uppervp; 94365963Spendry VREF(fvp); 94465963Spendry vrele(ap->a_fvp); 94565963Spendry } 94665963Spendry 94765963Spendry if (tdvp->v_op == union_vnodeop_p) { 94865963Spendry struct union_node *un = VTOUNION(tdvp); 94965997Spendry if (un->un_uppervp == NULLVP) { 95067076Spendry /* 95167076Spendry * this should never happen in normal 95267076Spendry * operation but might if there was 95367076Spendry * a problem creating the top-level shadow 95467076Spendry * directory. 95567076Spendry */ 95665963Spendry error = EROFS; 95765963Spendry goto bad; 95865963Spendry } 95965963Spendry 96065963Spendry tdvp = un->un_uppervp; 96165963Spendry VREF(tdvp); 96266051Spendry un->un_flags |= UN_KLOCK; 96365997Spendry vput(ap->a_tdvp); 96465963Spendry } 96565963Spendry 96667076Spendry if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 96765963Spendry struct union_node *un = VTOUNION(tvp); 96865963Spendry 96965963Spendry tvp = un->un_uppervp; 97067076Spendry if (tvp != NULLVP) { 97167076Spendry VREF(tvp); 97267076Spendry un->un_flags |= UN_KLOCK; 97367076Spendry } 97465963Spendry vput(ap->a_tvp); 97565963Spendry } 97665963Spendry 97765963Spendry return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 97865963Spendry 97965963Spendry bad: 98065963Spendry vrele(fdvp); 98165963Spendry vrele(fvp); 98265963Spendry vput(tdvp); 98367076Spendry if (tvp != NULLVP) 98465963Spendry vput(tvp); 98565963Spendry 98665963Spendry return (error); 98765963Spendry } 98865963Spendry 98965963Spendry int 99065963Spendry union_mkdir(ap) 99165963Spendry struct vop_mkdir_args /* { 99265963Spendry struct vnode *a_dvp; 99365963Spendry struct vnode **a_vpp; 99465963Spendry struct componentname *a_cnp; 99565963Spendry struct vattr *a_vap; 99665963Spendry } */ *ap; 99765963Spendry { 99865963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 99965963Spendry struct vnode *dvp = un->un_uppervp; 100065963Spendry 100167076Spendry if (dvp != NULLVP) { 100265963Spendry int error; 100365963Spendry struct vnode *vp; 100465963Spendry 100566152Spendry FIXUP(un); 100665963Spendry VREF(dvp); 100766051Spendry un->un_flags |= UN_KLOCK; 100865963Spendry vput(ap->a_dvp); 100965963Spendry error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 101065963Spendry if (error) 101165963Spendry return (error); 101265963Spendry 101365963Spendry error = union_allocvp( 101465963Spendry ap->a_vpp, 101565989Spendry ap->a_dvp->v_mount, 101665989Spendry ap->a_dvp, 101765965Spendry NULLVP, 101865963Spendry ap->a_cnp, 101965963Spendry vp, 102065963Spendry NULLVP); 102165965Spendry if (error) 102266051Spendry vput(vp); 102365963Spendry return (error); 102465963Spendry } 102565963Spendry 102665963Spendry vput(ap->a_dvp); 102765963Spendry return (EROFS); 102865963Spendry } 102965963Spendry 103065963Spendry int 103165963Spendry union_rmdir(ap) 103265963Spendry struct vop_rmdir_args /* { 103365963Spendry struct vnode *a_dvp; 103465963Spendry struct vnode *a_vp; 103565963Spendry struct componentname *a_cnp; 103665963Spendry } */ *ap; 103765963Spendry { 103865963Spendry int error; 103965963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 104065963Spendry struct union_node *un = VTOUNION(ap->a_vp); 104165963Spendry 104267076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 104365963Spendry struct vnode *dvp = dun->un_uppervp; 104465963Spendry struct vnode *vp = un->un_uppervp; 104565963Spendry 104666152Spendry FIXUP(dun); 104765963Spendry VREF(dvp); 104866051Spendry dun->un_flags |= UN_KLOCK; 104965963Spendry vput(ap->a_dvp); 105066152Spendry FIXUP(un); 105165963Spendry VREF(vp); 105266051Spendry un->un_flags |= UN_KLOCK; 105365963Spendry vput(ap->a_vp); 105465963Spendry 105566051Spendry error = VOP_RMDIR(dvp, vp, ap->a_cnp); 105666027Spendry if (!error) 105766027Spendry union_removed_upper(un); 105866027Spendry 105966027Spendry /* 106066027Spendry * XXX: should create a whiteout here 106166027Spendry */ 106265963Spendry } else { 106365963Spendry /* 106465963Spendry * XXX: should create a whiteout here 106565963Spendry */ 106665963Spendry vput(ap->a_dvp); 106765963Spendry vput(ap->a_vp); 106865963Spendry error = EROFS; 106965963Spendry } 107065963Spendry 107165963Spendry return (error); 107265963Spendry } 107365963Spendry 107465963Spendry int 107565963Spendry union_symlink(ap) 107665963Spendry struct vop_symlink_args /* { 107765963Spendry struct vnode *a_dvp; 107865963Spendry struct vnode **a_vpp; 107965963Spendry struct componentname *a_cnp; 108065963Spendry struct vattr *a_vap; 108165963Spendry char *a_target; 108265963Spendry } */ *ap; 108365963Spendry { 108465963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 108565963Spendry struct vnode *dvp = un->un_uppervp; 108665963Spendry 108767076Spendry if (dvp != NULLVP) { 108865963Spendry int error; 108965963Spendry struct vnode *vp; 109065963Spendry struct mount *mp = ap->a_dvp->v_mount; 109165963Spendry 109266152Spendry FIXUP(un); 109365963Spendry VREF(dvp); 109466051Spendry un->un_flags |= UN_KLOCK; 109565963Spendry vput(ap->a_dvp); 109665963Spendry error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 109765963Spendry ap->a_vap, ap->a_target); 109865997Spendry *ap->a_vpp = NULLVP; 109965963Spendry return (error); 110065963Spendry } 110165963Spendry 110265963Spendry vput(ap->a_dvp); 110365963Spendry return (EROFS); 110465963Spendry } 110565963Spendry 110665935Spendry /* 110765935Spendry * union_readdir works in concert with getdirentries and 110865935Spendry * readdir(3) to provide a list of entries in the unioned 110965935Spendry * directories. getdirentries is responsible for walking 111065935Spendry * down the union stack. readdir(3) is responsible for 111165935Spendry * eliminating duplicate names from the returned data stream. 111265935Spendry */ 111365935Spendry int 111465935Spendry union_readdir(ap) 111565935Spendry struct vop_readdir_args /* { 111665935Spendry struct vnodeop_desc *a_desc; 111765935Spendry struct vnode *a_vp; 111865935Spendry struct uio *a_uio; 111965935Spendry struct ucred *a_cred; 112067369Smckusick int *a_eofflag; 112167369Smckusick u_long *a_cookies; 112267369Smckusick int a_ncookies; 112365935Spendry } */ *ap; 112465935Spendry { 112567369Smckusick register struct union_node *un = VTOUNION(ap->a_vp); 112667369Smckusick register struct vnode *uvp = un->un_uppervp; 112765935Spendry 112867369Smckusick if (uvp == NULLVP) 112967369Smckusick return (0); 113065935Spendry 113167369Smckusick FIXUP(un); 113267369Smckusick ap->a_vp = uvp; 113367369Smckusick return (VOCALL(uvp->v_op, VOFFSET(vop_readdir), ap)); 113465935Spendry } 113565935Spendry 113665935Spendry int 113765963Spendry union_readlink(ap) 113865963Spendry struct vop_readlink_args /* { 113965963Spendry struct vnode *a_vp; 114065963Spendry struct uio *a_uio; 114165963Spendry struct ucred *a_cred; 114265963Spendry } */ *ap; 114365963Spendry { 114465963Spendry int error; 114565963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 114666051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 114765963Spendry 114866051Spendry if (dolock) 114966051Spendry VOP_LOCK(vp); 115066152Spendry else 115166152Spendry FIXUP(VTOUNION(ap->a_vp)); 115265963Spendry error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 115366051Spendry if (dolock) 115466051Spendry VOP_UNLOCK(vp); 115565963Spendry 115665963Spendry return (error); 115765963Spendry } 115865963Spendry 115965963Spendry int 116065963Spendry union_abortop(ap) 116165963Spendry struct vop_abortop_args /* { 116265963Spendry struct vnode *a_dvp; 116365963Spendry struct componentname *a_cnp; 116465963Spendry } */ *ap; 116565963Spendry { 116665963Spendry int error; 116765965Spendry struct vnode *vp = OTHERVP(ap->a_dvp); 116865963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 116965963Spendry int islocked = un->un_flags & UN_LOCKED; 117066051Spendry int dolock = (vp == LOWERVP(ap->a_dvp)); 117165963Spendry 117266152Spendry if (islocked) { 117366152Spendry if (dolock) 117466152Spendry VOP_LOCK(vp); 117566152Spendry else 117666152Spendry FIXUP(VTOUNION(ap->a_dvp)); 117766152Spendry } 117865963Spendry error = VOP_ABORTOP(vp, ap->a_cnp); 117966051Spendry if (islocked && dolock) 118065963Spendry VOP_UNLOCK(vp); 118165963Spendry 118265963Spendry return (error); 118365963Spendry } 118465963Spendry 118565963Spendry int 118665935Spendry union_inactive(ap) 118765935Spendry struct vop_inactive_args /* { 118865935Spendry struct vnode *a_vp; 118965935Spendry } */ *ap; 119065935Spendry { 119167073Spendry struct union_node *un = VTOUNION(ap->a_vp); 119265935Spendry 119365935Spendry /* 119465935Spendry * Do nothing (and _don't_ bypass). 119565935Spendry * Wait to vrele lowervp until reclaim, 119665935Spendry * so that until then our union_node is in the 119765935Spendry * cache and reusable. 119865935Spendry * 119965935Spendry * NEEDSWORK: Someday, consider inactive'ing 120065935Spendry * the lowervp and then trying to reactivate it 120165935Spendry * with capabilities (v_id) 120265935Spendry * like they do in the name lookup cache code. 120365935Spendry * That's too much work for now. 120465935Spendry */ 120565989Spendry 120666027Spendry #ifdef UNION_DIAGNOSTIC 120765989Spendry if (un->un_flags & UN_LOCKED) 120865989Spendry panic("union: inactivating locked node"); 120967073Spendry if (un->un_flags & UN_ULOCK) 121067073Spendry panic("union: inactivating w/locked upper node"); 121165989Spendry #endif 121265989Spendry 121367073Spendry if ((un->un_flags & UN_CACHED) == 0) 121467073Spendry vgone(ap->a_vp); 121567073Spendry 121665935Spendry return (0); 121765935Spendry } 121865935Spendry 121965935Spendry int 122065935Spendry union_reclaim(ap) 122165935Spendry struct vop_reclaim_args /* { 122265935Spendry struct vnode *a_vp; 122365935Spendry } */ *ap; 122465935Spendry { 122565935Spendry 122666053Spendry union_freevp(ap->a_vp); 122766053Spendry 122865935Spendry return (0); 122965935Spendry } 123065935Spendry 123165963Spendry int 123265963Spendry union_lock(ap) 123365963Spendry struct vop_lock_args *ap; 123465963Spendry { 123566149Spendry struct vnode *vp = ap->a_vp; 123666149Spendry struct union_node *un; 123765935Spendry 123866149Spendry start: 123966149Spendry while (vp->v_flag & VXLOCK) { 124066149Spendry vp->v_flag |= VXWANT; 124166149Spendry sleep((caddr_t)vp, PINOD); 124266149Spendry } 124366149Spendry 124466149Spendry un = VTOUNION(vp); 124566149Spendry 124667076Spendry if (un->un_uppervp != NULLVP) { 124767230Spendry if (((un->un_flags & UN_ULOCK) == 0) && 124867230Spendry (vp->v_usecount != 0)) { 124966149Spendry un->un_flags |= UN_ULOCK; 125066051Spendry VOP_LOCK(un->un_uppervp); 125166051Spendry } 125266051Spendry #ifdef DIAGNOSTIC 125366051Spendry if (un->un_flags & UN_KLOCK) 125466051Spendry panic("union: dangling upper lock"); 125566051Spendry #endif 125666051Spendry } 125766051Spendry 125866149Spendry if (un->un_flags & UN_LOCKED) { 125965963Spendry #ifdef DIAGNOSTIC 126065989Spendry if (curproc && un->un_pid == curproc->p_pid && 126165989Spendry un->un_pid > -1 && curproc->p_pid > -1) 126265989Spendry panic("union: locking against myself"); 126365963Spendry #endif 126465963Spendry un->un_flags |= UN_WANT; 126565963Spendry sleep((caddr_t) &un->un_flags, PINOD); 126666149Spendry goto start; 126765963Spendry } 126865989Spendry 126965963Spendry #ifdef DIAGNOSTIC 127065989Spendry if (curproc) 127165989Spendry un->un_pid = curproc->p_pid; 127265989Spendry else 127365989Spendry un->un_pid = -1; 127465963Spendry #endif 127566028Spendry 127666149Spendry un->un_flags |= UN_LOCKED; 127766028Spendry return (0); 127865963Spendry } 127965963Spendry 128065935Spendry int 128165963Spendry union_unlock(ap) 128265963Spendry struct vop_lock_args *ap; 128365963Spendry { 128465963Spendry struct union_node *un = VTOUNION(ap->a_vp); 128565963Spendry 128665963Spendry #ifdef DIAGNOSTIC 128765965Spendry if ((un->un_flags & UN_LOCKED) == 0) 128865965Spendry panic("union: unlock unlocked node"); 128965989Spendry if (curproc && un->un_pid != curproc->p_pid && 129065989Spendry curproc->p_pid > -1 && un->un_pid > -1) 129165963Spendry panic("union: unlocking other process's union node"); 129265963Spendry #endif 129365963Spendry 129465963Spendry un->un_flags &= ~UN_LOCKED; 129566051Spendry 129666051Spendry if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 129766051Spendry VOP_UNLOCK(un->un_uppervp); 129866051Spendry 129966051Spendry un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 130066051Spendry 130165963Spendry if (un->un_flags & UN_WANT) { 130265963Spendry un->un_flags &= ~UN_WANT; 130365963Spendry wakeup((caddr_t) &un->un_flags); 130465963Spendry } 130565963Spendry 130665963Spendry #ifdef DIAGNOSTIC 130765963Spendry un->un_pid = 0; 130865963Spendry #endif 130966028Spendry 131066028Spendry return (0); 131165963Spendry } 131265963Spendry 131365963Spendry int 131465963Spendry union_bmap(ap) 131565963Spendry struct vop_bmap_args /* { 131665963Spendry struct vnode *a_vp; 131765963Spendry daddr_t a_bn; 131865963Spendry struct vnode **a_vpp; 131965963Spendry daddr_t *a_bnp; 132065963Spendry int *a_runp; 132165963Spendry } */ *ap; 132265963Spendry { 132365963Spendry int error; 132465963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 132566051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 132665963Spendry 132766051Spendry if (dolock) 132866051Spendry VOP_LOCK(vp); 132966152Spendry else 133066152Spendry FIXUP(VTOUNION(ap->a_vp)); 133165963Spendry error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 133266051Spendry if (dolock) 133366051Spendry VOP_UNLOCK(vp); 133465963Spendry 133565963Spendry return (error); 133665963Spendry } 133765963Spendry 133865963Spendry int 133965935Spendry union_print(ap) 134065935Spendry struct vop_print_args /* { 134165935Spendry struct vnode *a_vp; 134265935Spendry } */ *ap; 134365935Spendry { 134465935Spendry struct vnode *vp = ap->a_vp; 134565935Spendry 134665935Spendry printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 134765935Spendry vp, UPPERVP(vp), LOWERVP(vp)); 134865935Spendry return (0); 134965935Spendry } 135065935Spendry 135165963Spendry int 135265963Spendry union_islocked(ap) 135365963Spendry struct vop_islocked_args /* { 135465963Spendry struct vnode *a_vp; 135565963Spendry } */ *ap; 135665963Spendry { 135765935Spendry 135865963Spendry return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 135965963Spendry } 136065963Spendry 136165935Spendry int 136265963Spendry union_pathconf(ap) 136365963Spendry struct vop_pathconf_args /* { 136465963Spendry struct vnode *a_vp; 136565963Spendry int a_name; 136665963Spendry int *a_retval; 136765935Spendry } */ *ap; 136865935Spendry { 136965935Spendry int error; 137065963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 137166051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 137265935Spendry 137366051Spendry if (dolock) 137466051Spendry VOP_LOCK(vp); 137566152Spendry else 137666152Spendry FIXUP(VTOUNION(ap->a_vp)); 137765963Spendry error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 137866051Spendry if (dolock) 137966051Spendry VOP_UNLOCK(vp); 138065935Spendry 138165963Spendry return (error); 138265963Spendry } 138365935Spendry 138465963Spendry int 138565963Spendry union_advlock(ap) 138665963Spendry struct vop_advlock_args /* { 138765963Spendry struct vnode *a_vp; 138865963Spendry caddr_t a_id; 138965963Spendry int a_op; 139065963Spendry struct flock *a_fl; 139165963Spendry int a_flags; 139265963Spendry } */ *ap; 139365963Spendry { 139465935Spendry 139565963Spendry return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 139665963Spendry ap->a_fl, ap->a_flags)); 139765935Spendry } 139865935Spendry 139965935Spendry 140065935Spendry /* 140165963Spendry * XXX - vop_strategy must be hand coded because it has no 140265935Spendry * vnode in its arguments. 140365935Spendry * This goes away with a merged VM/buffer cache. 140465935Spendry */ 140565935Spendry int 140665963Spendry union_strategy(ap) 140765963Spendry struct vop_strategy_args /* { 140865935Spendry struct buf *a_bp; 140965935Spendry } */ *ap; 141065935Spendry { 141165935Spendry struct buf *bp = ap->a_bp; 141265935Spendry int error; 141365935Spendry struct vnode *savedvp; 141465935Spendry 141565935Spendry savedvp = bp->b_vp; 141665963Spendry bp->b_vp = OTHERVP(bp->b_vp); 141765935Spendry 141865935Spendry #ifdef DIAGNOSTIC 141965997Spendry if (bp->b_vp == NULLVP) 142065963Spendry panic("union_strategy: nil vp"); 142165963Spendry if (((bp->b_flags & B_READ) == 0) && 142265963Spendry (bp->b_vp == LOWERVP(savedvp))) 142365963Spendry panic("union_strategy: writing to lowervp"); 142465935Spendry #endif 142565935Spendry 142665963Spendry error = VOP_STRATEGY(bp); 142765935Spendry bp->b_vp = savedvp; 142865935Spendry 142965935Spendry return (error); 143065935Spendry } 143165935Spendry 143265935Spendry /* 143365935Spendry * Global vfs data structures 143465935Spendry */ 143565935Spendry int (**union_vnodeop_p)(); 143665965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = { 143765963Spendry { &vop_default_desc, vn_default_error }, 143865963Spendry { &vop_lookup_desc, union_lookup }, /* lookup */ 143965963Spendry { &vop_create_desc, union_create }, /* create */ 144065963Spendry { &vop_mknod_desc, union_mknod }, /* mknod */ 144165963Spendry { &vop_open_desc, union_open }, /* open */ 144265963Spendry { &vop_close_desc, union_close }, /* close */ 144365963Spendry { &vop_access_desc, union_access }, /* access */ 144465963Spendry { &vop_getattr_desc, union_getattr }, /* getattr */ 144565963Spendry { &vop_setattr_desc, union_setattr }, /* setattr */ 144665963Spendry { &vop_read_desc, union_read }, /* read */ 144765963Spendry { &vop_write_desc, union_write }, /* write */ 144865963Spendry { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 144965963Spendry { &vop_select_desc, union_select }, /* select */ 145065963Spendry { &vop_mmap_desc, union_mmap }, /* mmap */ 145165963Spendry { &vop_fsync_desc, union_fsync }, /* fsync */ 145265963Spendry { &vop_seek_desc, union_seek }, /* seek */ 145365963Spendry { &vop_remove_desc, union_remove }, /* remove */ 145465963Spendry { &vop_link_desc, union_link }, /* link */ 145565963Spendry { &vop_rename_desc, union_rename }, /* rename */ 145665963Spendry { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 145765963Spendry { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 145865963Spendry { &vop_symlink_desc, union_symlink }, /* symlink */ 145965963Spendry { &vop_readdir_desc, union_readdir }, /* readdir */ 146065963Spendry { &vop_readlink_desc, union_readlink }, /* readlink */ 146165963Spendry { &vop_abortop_desc, union_abortop }, /* abortop */ 146265963Spendry { &vop_inactive_desc, union_inactive }, /* inactive */ 146365963Spendry { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 146465963Spendry { &vop_lock_desc, union_lock }, /* lock */ 146565963Spendry { &vop_unlock_desc, union_unlock }, /* unlock */ 146665963Spendry { &vop_bmap_desc, union_bmap }, /* bmap */ 146765963Spendry { &vop_strategy_desc, union_strategy }, /* strategy */ 146865963Spendry { &vop_print_desc, union_print }, /* print */ 146965963Spendry { &vop_islocked_desc, union_islocked }, /* islocked */ 147065963Spendry { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 147165963Spendry { &vop_advlock_desc, union_advlock }, /* advlock */ 147265963Spendry #ifdef notdef 147365963Spendry { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 147465963Spendry { &vop_valloc_desc, union_valloc }, /* valloc */ 147565963Spendry { &vop_vfree_desc, union_vfree }, /* vfree */ 147665963Spendry { &vop_truncate_desc, union_truncate }, /* truncate */ 147765963Spendry { &vop_update_desc, union_update }, /* update */ 147865963Spendry { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 147965963Spendry #endif 148065935Spendry { (struct vnodeop_desc*)NULL, (int(*)())NULL } 148165935Spendry }; 148265935Spendry struct vnodeopv_desc union_vnodeop_opv_desc = 148365935Spendry { &union_vnodeop_p, union_vnodeop_entries }; 1484