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*67109Spendry * @(#)union_vnops.c 8.10 (Berkeley) 05/07/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; 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) { 26367076Spendry if (lowervp != NULLVP) { 26465935Spendry vput(lowervp); 26565997Spendry lowervp = NULLVP; 26665935Spendry } 26765935Spendry return (uerror); 26865935Spendry } 26965935Spendry } 27065935Spendry } 27165935Spendry 27267076Spendry 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) { 27967076Spendry if (uppervp != NULLVP) 28066051Spendry vput(uppervp); 28167076Spendry 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 30467076Spendry 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 34667076Spendry 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 35967076Spendry 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 51367076Spendry 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 54967076Spendry 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 55467076Spendry 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 /* 573*67109Spendry * We handle getattr only to change the fsid and 574*67109Spendry * track object sizes 57565935Spendry */ 57665935Spendry int 57765935Spendry union_getattr(ap) 57865935Spendry struct vop_getattr_args /* { 57965935Spendry struct vnode *a_vp; 58065935Spendry struct vattr *a_vap; 58165935Spendry struct ucred *a_cred; 58265935Spendry struct proc *a_p; 58365935Spendry } */ *ap; 58465935Spendry { 58565935Spendry int error; 58666062Spendry struct union_node *un = VTOUNION(ap->a_vp); 58766062Spendry struct vnode *vp = un->un_uppervp; 58866062Spendry struct vattr *vap; 58966062Spendry struct vattr va; 59065935Spendry 59166062Spendry 59266062Spendry /* 59366062Spendry * Some programs walk the filesystem hierarchy by counting 59466062Spendry * links to directories to avoid stat'ing all the time. 59566062Spendry * This means the link count on directories needs to be "correct". 59666062Spendry * The only way to do that is to call getattr on both layers 59766062Spendry * and fix up the link count. The link count will not necessarily 59866062Spendry * be accurate but will be large enough to defeat the tree walkers. 59966062Spendry */ 60066062Spendry 60166062Spendry vap = ap->a_vap; 60266062Spendry 60366062Spendry vp = un->un_uppervp; 60466062Spendry if (vp != NULLVP) { 60567073Spendry /* 60667073Spendry * It's not clear whether VOP_GETATTR is to be 60767073Spendry * called with the vnode locked or not. stat() calls 60867073Spendry * it with (vp) locked, and fstat calls it with 60967073Spendry * (vp) unlocked. 61067073Spendry * In the mean time, compensate here by checking 61167073Spendry * the union_node's lock flag. 61267073Spendry */ 61367073Spendry if (un->un_flags & UN_LOCKED) 61467073Spendry FIXUP(un); 61567073Spendry 61666062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 61766062Spendry if (error) 61866062Spendry return (error); 619*67109Spendry union_newsize(ap->a_vp, vap->va_size, VNOVAL); 62066062Spendry } 62166062Spendry 62266062Spendry if (vp == NULLVP) { 62366062Spendry vp = un->un_lowervp; 62466062Spendry } else if (vp->v_type == VDIR) { 62566062Spendry vp = un->un_lowervp; 62666062Spendry vap = &va; 62766062Spendry } else { 62866062Spendry vp = NULLVP; 62966062Spendry } 63066062Spendry 63166062Spendry if (vp != NULLVP) { 63266051Spendry VOP_LOCK(vp); 63366062Spendry error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); 63466051Spendry VOP_UNLOCK(vp); 63566062Spendry if (error) 63666062Spendry return (error); 637*67109Spendry union_newsize(ap->a_vp, VNOVAL, vap->va_size); 63866062Spendry } 63965965Spendry 64066062Spendry if ((vap != ap->a_vap) && (vap->va_type == VDIR)) 64166062Spendry ap->a_vap->va_nlink += vap->va_nlink; 64266062Spendry 64366062Spendry vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 64465935Spendry return (0); 64565935Spendry } 64665935Spendry 64765963Spendry int 64865965Spendry union_setattr(ap) 64965963Spendry struct vop_setattr_args /* { 65065963Spendry struct vnode *a_vp; 65165963Spendry struct vattr *a_vap; 65265963Spendry struct ucred *a_cred; 65365963Spendry struct proc *a_p; 65465963Spendry } */ *ap; 65565963Spendry { 65665963Spendry struct union_node *un = VTOUNION(ap->a_vp); 65765963Spendry int error; 65865963Spendry 65966057Spendry /* 66066057Spendry * Handle case of truncating lower object to zero size, 66166057Spendry * by creating a zero length upper object. This is to 66266057Spendry * handle the case of open with O_TRUNC and O_CREAT. 66366057Spendry */ 66466057Spendry if ((un->un_uppervp == NULLVP) && 66566057Spendry /* assert(un->un_lowervp != NULLVP) */ 66666057Spendry (un->un_lowervp->v_type == VREG) && 66766057Spendry (ap->a_vap->va_size == 0)) { 66866057Spendry struct vnode *vp; 66966057Spendry 67066057Spendry error = union_vn_create(&vp, un, ap->a_p); 67166057Spendry if (error) 67266057Spendry return (error); 67366057Spendry 67466057Spendry /* at this point, uppervp is locked */ 67566057Spendry union_newupper(un, vp); 67666057Spendry 67766057Spendry VOP_UNLOCK(vp); 67866057Spendry union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p); 67966057Spendry VOP_LOCK(vp); 68066057Spendry un->un_flags |= UN_ULOCK; 68166057Spendry } 68266057Spendry 68366057Spendry /* 68466057Spendry * Try to set attributes in upper layer, 68566057Spendry * otherwise return read-only filesystem error. 68666057Spendry */ 68766057Spendry if (un->un_uppervp != NULLVP) { 68866152Spendry FIXUP(un); 68965963Spendry error = VOP_SETATTR(un->un_uppervp, ap->a_vap, 69065963Spendry ap->a_cred, ap->a_p); 691*67109Spendry if ((error == 0) && (ap->a_vap->va_size != VNOVAL)) 692*67109Spendry union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL); 69365963Spendry } else { 69465963Spendry error = EROFS; 69565963Spendry } 69665963Spendry 69765963Spendry return (error); 69865963Spendry } 69965963Spendry 70065963Spendry int 70165963Spendry union_read(ap) 70265963Spendry struct vop_read_args /* { 70365963Spendry struct vnode *a_vp; 70465963Spendry struct uio *a_uio; 70565963Spendry int a_ioflag; 70665963Spendry struct ucred *a_cred; 70765963Spendry } */ *ap; 70865963Spendry { 70965963Spendry int error; 71065963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 71166051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 71265963Spendry 71366051Spendry if (dolock) 71466051Spendry VOP_LOCK(vp); 71566152Spendry else 71666152Spendry FIXUP(VTOUNION(ap->a_vp)); 71765963Spendry error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 71866051Spendry if (dolock) 71966051Spendry VOP_UNLOCK(vp); 72065963Spendry 721*67109Spendry /* 722*67109Spendry * XXX 723*67109Spendry * perhaps the size of the underlying object has changed under 724*67109Spendry * our feet. take advantage of the offset information present 725*67109Spendry * in the uio structure. 726*67109Spendry */ 727*67109Spendry if (error == 0) { 728*67109Spendry struct union_node *un = VTOUNION(ap->a_vp); 729*67109Spendry off_t cur = ap->a_uio->uio_offset; 730*67109Spendry 731*67109Spendry if (vp == un->un_uppervp) { 732*67109Spendry if (cur > un->un_uppersz) 733*67109Spendry union_newsize(ap->a_vp, cur, VNOVAL); 734*67109Spendry } else { 735*67109Spendry if (cur > un->un_lowersz) 736*67109Spendry union_newsize(ap->a_vp, VNOVAL, cur); 737*67109Spendry } 738*67109Spendry } 739*67109Spendry 74065963Spendry return (error); 74165963Spendry } 74265963Spendry 74365963Spendry int 74465963Spendry union_write(ap) 74565963Spendry struct vop_read_args /* { 74665963Spendry struct vnode *a_vp; 74765963Spendry struct uio *a_uio; 74865963Spendry int a_ioflag; 74965963Spendry struct ucred *a_cred; 75065963Spendry } */ *ap; 75165963Spendry { 75265963Spendry int error; 75365963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 75466051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 75565963Spendry 75666051Spendry if (dolock) 75766051Spendry VOP_LOCK(vp); 75866152Spendry else 75966152Spendry FIXUP(VTOUNION(ap->a_vp)); 76065963Spendry error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); 76166051Spendry if (dolock) 76266051Spendry VOP_UNLOCK(vp); 76365963Spendry 764*67109Spendry /* 765*67109Spendry * the size of the underlying object may be changed by the 766*67109Spendry * write. 767*67109Spendry */ 768*67109Spendry if (error == 0) { 769*67109Spendry struct union_node *un = VTOUNION(ap->a_vp); 770*67109Spendry off_t cur = ap->a_uio->uio_offset; 771*67109Spendry 772*67109Spendry if (vp == un->un_uppervp) { 773*67109Spendry if (cur > un->un_uppersz) 774*67109Spendry union_newsize(ap->a_vp, cur, VNOVAL); 775*67109Spendry } else { 776*67109Spendry if (cur > un->un_lowersz) 777*67109Spendry union_newsize(ap->a_vp, VNOVAL, cur); 778*67109Spendry } 779*67109Spendry } 780*67109Spendry 78165963Spendry return (error); 78265963Spendry } 78365963Spendry 78465963Spendry int 78565963Spendry union_ioctl(ap) 78665963Spendry struct vop_ioctl_args /* { 78765963Spendry struct vnode *a_vp; 78865963Spendry int a_command; 78965963Spendry caddr_t a_data; 79065963Spendry int a_fflag; 79165963Spendry struct ucred *a_cred; 79265963Spendry struct proc *a_p; 79365963Spendry } */ *ap; 79465963Spendry { 79565963Spendry 79665963Spendry return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, 79765963Spendry ap->a_fflag, ap->a_cred, ap->a_p)); 79865963Spendry } 79965963Spendry 80065963Spendry int 80165963Spendry union_select(ap) 80265963Spendry struct vop_select_args /* { 80365963Spendry struct vnode *a_vp; 80465963Spendry int a_which; 80565963Spendry int a_fflags; 80665963Spendry struct ucred *a_cred; 80765963Spendry struct proc *a_p; 80865963Spendry } */ *ap; 80965963Spendry { 81065963Spendry 81165963Spendry return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, 81265963Spendry ap->a_cred, ap->a_p)); 81365963Spendry } 81465963Spendry 81565963Spendry int 81665963Spendry union_mmap(ap) 81765963Spendry struct vop_mmap_args /* { 81865963Spendry struct vnode *a_vp; 81965963Spendry int a_fflags; 82065963Spendry struct ucred *a_cred; 82165963Spendry struct proc *a_p; 82265963Spendry } */ *ap; 82365963Spendry { 82465963Spendry 82565963Spendry return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, 82665963Spendry ap->a_cred, ap->a_p)); 82765963Spendry } 82865963Spendry 82965963Spendry int 83065963Spendry union_fsync(ap) 83165963Spendry struct vop_fsync_args /* { 83265963Spendry struct vnode *a_vp; 83365963Spendry struct ucred *a_cred; 83465963Spendry int a_waitfor; 83565963Spendry struct proc *a_p; 83665963Spendry } */ *ap; 83765963Spendry { 83865963Spendry int error = 0; 83965963Spendry struct vnode *targetvp = OTHERVP(ap->a_vp); 84065963Spendry 84167076Spendry if (targetvp != NULLVP) { 84266051Spendry int dolock = (targetvp == LOWERVP(ap->a_vp)); 84366051Spendry 84466051Spendry if (dolock) 84566051Spendry VOP_LOCK(targetvp); 84666152Spendry else 84766152Spendry FIXUP(VTOUNION(ap->a_vp)); 84865963Spendry error = VOP_FSYNC(targetvp, ap->a_cred, 84965963Spendry ap->a_waitfor, ap->a_p); 85066051Spendry if (dolock) 85166051Spendry VOP_UNLOCK(targetvp); 85265963Spendry } 85365963Spendry 85465963Spendry return (error); 85565963Spendry } 85665963Spendry 85765963Spendry int 85865963Spendry union_seek(ap) 85965963Spendry struct vop_seek_args /* { 86065963Spendry struct vnode *a_vp; 86165963Spendry off_t a_oldoff; 86265963Spendry off_t a_newoff; 86365963Spendry struct ucred *a_cred; 86465963Spendry } */ *ap; 86565963Spendry { 86665963Spendry 86765963Spendry return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); 86865963Spendry } 86965963Spendry 87065963Spendry int 87165963Spendry union_remove(ap) 87265963Spendry struct vop_remove_args /* { 87365963Spendry struct vnode *a_dvp; 87465963Spendry struct vnode *a_vp; 87565963Spendry struct componentname *a_cnp; 87665963Spendry } */ *ap; 87765963Spendry { 87865963Spendry int error; 87965963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 88065963Spendry struct union_node *un = VTOUNION(ap->a_vp); 88165963Spendry 88267076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 88365963Spendry struct vnode *dvp = dun->un_uppervp; 88465963Spendry struct vnode *vp = un->un_uppervp; 88565963Spendry 88666152Spendry FIXUP(dun); 88765963Spendry VREF(dvp); 88866051Spendry dun->un_flags |= UN_KLOCK; 88965963Spendry vput(ap->a_dvp); 89066152Spendry FIXUP(un); 89165963Spendry VREF(vp); 89266051Spendry un->un_flags |= UN_KLOCK; 89365963Spendry vput(ap->a_vp); 89465963Spendry 89565963Spendry error = VOP_REMOVE(dvp, vp, ap->a_cnp); 89666027Spendry if (!error) 89766027Spendry union_removed_upper(un); 89866027Spendry 89966027Spendry /* 90066027Spendry * XXX: should create a whiteout here 90166027Spendry */ 90265963Spendry } else { 90365963Spendry /* 90465963Spendry * XXX: should create a whiteout here 90565963Spendry */ 90665963Spendry vput(ap->a_dvp); 90765963Spendry vput(ap->a_vp); 90865963Spendry error = EROFS; 90965963Spendry } 91065963Spendry 91165963Spendry return (error); 91265963Spendry } 91365963Spendry 91465963Spendry int 91565963Spendry union_link(ap) 91665963Spendry struct vop_link_args /* { 91765963Spendry struct vnode *a_vp; 91865963Spendry struct vnode *a_tdvp; 91965963Spendry struct componentname *a_cnp; 92065963Spendry } */ *ap; 92165963Spendry { 92265963Spendry int error; 92365963Spendry struct union_node *dun = VTOUNION(ap->a_vp); 92465963Spendry struct union_node *un = VTOUNION(ap->a_tdvp); 92565963Spendry 92667076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 92765963Spendry struct vnode *dvp = dun->un_uppervp; 92865963Spendry struct vnode *vp = un->un_uppervp; 92965963Spendry 93066152Spendry FIXUP(dun); 93165963Spendry VREF(dvp); 93266051Spendry dun->un_flags |= UN_KLOCK; 93365963Spendry vput(ap->a_vp); 93466152Spendry FIXUP(un); 93565963Spendry VREF(vp); 93665963Spendry vrele(ap->a_tdvp); 93765963Spendry 93865963Spendry error = VOP_LINK(dvp, vp, ap->a_cnp); 93965963Spendry } else { 94065963Spendry /* 94167076Spendry * XXX: perhaps could copy to upper layer 94265963Spendry * and do the link there. 94365963Spendry */ 94465963Spendry vput(ap->a_vp); 94565963Spendry vrele(ap->a_tdvp); 94665963Spendry error = EROFS; 94765963Spendry } 94865963Spendry 94965963Spendry return (error); 95065963Spendry } 95165963Spendry 95265963Spendry int 95365963Spendry union_rename(ap) 95465963Spendry struct vop_rename_args /* { 95565963Spendry struct vnode *a_fdvp; 95665963Spendry struct vnode *a_fvp; 95765963Spendry struct componentname *a_fcnp; 95865963Spendry struct vnode *a_tdvp; 95965963Spendry struct vnode *a_tvp; 96065963Spendry struct componentname *a_tcnp; 96165963Spendry } */ *ap; 96265963Spendry { 96365963Spendry int error; 96465963Spendry 96565963Spendry struct vnode *fdvp = ap->a_fdvp; 96665963Spendry struct vnode *fvp = ap->a_fvp; 96765963Spendry struct vnode *tdvp = ap->a_tdvp; 96865963Spendry struct vnode *tvp = ap->a_tvp; 96965963Spendry 97065963Spendry if (fdvp->v_op == union_vnodeop_p) { /* always true */ 97165963Spendry struct union_node *un = VTOUNION(fdvp); 97265997Spendry if (un->un_uppervp == NULLVP) { 97365963Spendry error = EROFS; 97465963Spendry goto bad; 97565963Spendry } 97665963Spendry 97765963Spendry fdvp = un->un_uppervp; 97865963Spendry VREF(fdvp); 97965963Spendry vrele(ap->a_fdvp); 98065963Spendry } 98165963Spendry 98265963Spendry if (fvp->v_op == union_vnodeop_p) { /* always true */ 98365963Spendry struct union_node *un = VTOUNION(fvp); 98465997Spendry if (un->un_uppervp == NULLVP) { 98565963Spendry error = EROFS; 98665963Spendry goto bad; 98765963Spendry } 98865963Spendry 98965963Spendry fvp = un->un_uppervp; 99065963Spendry VREF(fvp); 99165963Spendry vrele(ap->a_fvp); 99265963Spendry } 99365963Spendry 99465963Spendry if (tdvp->v_op == union_vnodeop_p) { 99565963Spendry struct union_node *un = VTOUNION(tdvp); 99665997Spendry if (un->un_uppervp == NULLVP) { 99767076Spendry /* 99867076Spendry * this should never happen in normal 99967076Spendry * operation but might if there was 100067076Spendry * a problem creating the top-level shadow 100167076Spendry * directory. 100267076Spendry */ 100365963Spendry error = EROFS; 100465963Spendry goto bad; 100565963Spendry } 100665963Spendry 100765963Spendry tdvp = un->un_uppervp; 100865963Spendry VREF(tdvp); 100966051Spendry un->un_flags |= UN_KLOCK; 101065997Spendry vput(ap->a_tdvp); 101165963Spendry } 101265963Spendry 101367076Spendry if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) { 101465963Spendry struct union_node *un = VTOUNION(tvp); 101565963Spendry 101665963Spendry tvp = un->un_uppervp; 101767076Spendry if (tvp != NULLVP) { 101867076Spendry VREF(tvp); 101967076Spendry un->un_flags |= UN_KLOCK; 102067076Spendry } 102165963Spendry vput(ap->a_tvp); 102265963Spendry } 102365963Spendry 102465963Spendry return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); 102565963Spendry 102665963Spendry bad: 102765963Spendry vrele(fdvp); 102865963Spendry vrele(fvp); 102965963Spendry vput(tdvp); 103067076Spendry if (tvp != NULLVP) 103165963Spendry vput(tvp); 103265963Spendry 103365963Spendry return (error); 103465963Spendry } 103565963Spendry 103665963Spendry int 103765963Spendry union_mkdir(ap) 103865963Spendry struct vop_mkdir_args /* { 103965963Spendry struct vnode *a_dvp; 104065963Spendry struct vnode **a_vpp; 104165963Spendry struct componentname *a_cnp; 104265963Spendry struct vattr *a_vap; 104365963Spendry } */ *ap; 104465963Spendry { 104565963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 104665963Spendry struct vnode *dvp = un->un_uppervp; 104765963Spendry 104867076Spendry if (dvp != NULLVP) { 104965963Spendry int error; 105065963Spendry struct vnode *vp; 105165963Spendry 105266152Spendry FIXUP(un); 105365963Spendry VREF(dvp); 105466051Spendry un->un_flags |= UN_KLOCK; 105565963Spendry vput(ap->a_dvp); 105665963Spendry error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); 105765963Spendry if (error) 105865963Spendry return (error); 105965963Spendry 106065963Spendry error = union_allocvp( 106165963Spendry ap->a_vpp, 106265989Spendry ap->a_dvp->v_mount, 106365989Spendry ap->a_dvp, 106465965Spendry NULLVP, 106565963Spendry ap->a_cnp, 106665963Spendry vp, 106765963Spendry NULLVP); 106865965Spendry if (error) 106966051Spendry vput(vp); 107065963Spendry return (error); 107165963Spendry } 107265963Spendry 107365963Spendry vput(ap->a_dvp); 107465963Spendry return (EROFS); 107565963Spendry } 107665963Spendry 107765963Spendry int 107865963Spendry union_rmdir(ap) 107965963Spendry struct vop_rmdir_args /* { 108065963Spendry struct vnode *a_dvp; 108165963Spendry struct vnode *a_vp; 108265963Spendry struct componentname *a_cnp; 108365963Spendry } */ *ap; 108465963Spendry { 108565963Spendry int error; 108665963Spendry struct union_node *dun = VTOUNION(ap->a_dvp); 108765963Spendry struct union_node *un = VTOUNION(ap->a_vp); 108865963Spendry 108967076Spendry if (dun->un_uppervp != NULLVP && un->un_uppervp != NULLVP) { 109065963Spendry struct vnode *dvp = dun->un_uppervp; 109165963Spendry struct vnode *vp = un->un_uppervp; 109265963Spendry 109366152Spendry FIXUP(dun); 109465963Spendry VREF(dvp); 109566051Spendry dun->un_flags |= UN_KLOCK; 109665963Spendry vput(ap->a_dvp); 109766152Spendry FIXUP(un); 109865963Spendry VREF(vp); 109966051Spendry un->un_flags |= UN_KLOCK; 110065963Spendry vput(ap->a_vp); 110165963Spendry 110266051Spendry error = VOP_RMDIR(dvp, vp, ap->a_cnp); 110366027Spendry if (!error) 110466027Spendry union_removed_upper(un); 110566027Spendry 110666027Spendry /* 110766027Spendry * XXX: should create a whiteout here 110866027Spendry */ 110965963Spendry } else { 111065963Spendry /* 111165963Spendry * XXX: should create a whiteout here 111265963Spendry */ 111365963Spendry vput(ap->a_dvp); 111465963Spendry vput(ap->a_vp); 111565963Spendry error = EROFS; 111665963Spendry } 111765963Spendry 111865963Spendry return (error); 111965963Spendry } 112065963Spendry 112165963Spendry int 112265963Spendry union_symlink(ap) 112365963Spendry struct vop_symlink_args /* { 112465963Spendry struct vnode *a_dvp; 112565963Spendry struct vnode **a_vpp; 112665963Spendry struct componentname *a_cnp; 112765963Spendry struct vattr *a_vap; 112865963Spendry char *a_target; 112965963Spendry } */ *ap; 113065963Spendry { 113165963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 113265963Spendry struct vnode *dvp = un->un_uppervp; 113365963Spendry 113467076Spendry if (dvp != NULLVP) { 113565963Spendry int error; 113665963Spendry struct vnode *vp; 113765963Spendry struct mount *mp = ap->a_dvp->v_mount; 113865963Spendry 113966152Spendry FIXUP(un); 114065963Spendry VREF(dvp); 114166051Spendry un->un_flags |= UN_KLOCK; 114265963Spendry vput(ap->a_dvp); 114365963Spendry error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, 114465963Spendry ap->a_vap, ap->a_target); 114565997Spendry *ap->a_vpp = NULLVP; 114665963Spendry return (error); 114765963Spendry } 114865963Spendry 114965963Spendry vput(ap->a_dvp); 115065963Spendry return (EROFS); 115165963Spendry } 115265963Spendry 115365935Spendry /* 115465935Spendry * union_readdir works in concert with getdirentries and 115565935Spendry * readdir(3) to provide a list of entries in the unioned 115665935Spendry * directories. getdirentries is responsible for walking 115765935Spendry * down the union stack. readdir(3) is responsible for 115865935Spendry * eliminating duplicate names from the returned data stream. 115965935Spendry */ 116065935Spendry int 116165935Spendry union_readdir(ap) 116265935Spendry struct vop_readdir_args /* { 116365935Spendry struct vnodeop_desc *a_desc; 116465935Spendry struct vnode *a_vp; 116565935Spendry struct uio *a_uio; 116665935Spendry struct ucred *a_cred; 116765935Spendry } */ *ap; 116865935Spendry { 116965963Spendry int error = 0; 117065935Spendry struct union_node *un = VTOUNION(ap->a_vp); 117165935Spendry 117267076Spendry if (un->un_uppervp != NULLVP) { 117366152Spendry FIXUP(un); 117466051Spendry error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred); 117566152Spendry } 117665935Spendry 117765963Spendry return (error); 117865935Spendry } 117965935Spendry 118065935Spendry int 118165963Spendry union_readlink(ap) 118265963Spendry struct vop_readlink_args /* { 118365963Spendry struct vnode *a_vp; 118465963Spendry struct uio *a_uio; 118565963Spendry struct ucred *a_cred; 118665963Spendry } */ *ap; 118765963Spendry { 118865963Spendry int error; 118965963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 119066051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 119165963Spendry 119266051Spendry if (dolock) 119366051Spendry VOP_LOCK(vp); 119466152Spendry else 119566152Spendry FIXUP(VTOUNION(ap->a_vp)); 119665963Spendry error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); 119766051Spendry if (dolock) 119866051Spendry VOP_UNLOCK(vp); 119965963Spendry 120065963Spendry return (error); 120165963Spendry } 120265963Spendry 120365963Spendry int 120465963Spendry union_abortop(ap) 120565963Spendry struct vop_abortop_args /* { 120665963Spendry struct vnode *a_dvp; 120765963Spendry struct componentname *a_cnp; 120865963Spendry } */ *ap; 120965963Spendry { 121065963Spendry int error; 121165965Spendry struct vnode *vp = OTHERVP(ap->a_dvp); 121265963Spendry struct union_node *un = VTOUNION(ap->a_dvp); 121365963Spendry int islocked = un->un_flags & UN_LOCKED; 121466051Spendry int dolock = (vp == LOWERVP(ap->a_dvp)); 121565963Spendry 121666152Spendry if (islocked) { 121766152Spendry if (dolock) 121866152Spendry VOP_LOCK(vp); 121966152Spendry else 122066152Spendry FIXUP(VTOUNION(ap->a_dvp)); 122166152Spendry } 122265963Spendry error = VOP_ABORTOP(vp, ap->a_cnp); 122366051Spendry if (islocked && dolock) 122465963Spendry VOP_UNLOCK(vp); 122565963Spendry 122665963Spendry return (error); 122765963Spendry } 122865963Spendry 122965963Spendry int 123065935Spendry union_inactive(ap) 123165935Spendry struct vop_inactive_args /* { 123265935Spendry struct vnode *a_vp; 123365935Spendry } */ *ap; 123465935Spendry { 123567073Spendry struct union_node *un = VTOUNION(ap->a_vp); 123665935Spendry 123765935Spendry /* 123865935Spendry * Do nothing (and _don't_ bypass). 123965935Spendry * Wait to vrele lowervp until reclaim, 124065935Spendry * so that until then our union_node is in the 124165935Spendry * cache and reusable. 124265935Spendry * 124365935Spendry * NEEDSWORK: Someday, consider inactive'ing 124465935Spendry * the lowervp and then trying to reactivate it 124565935Spendry * with capabilities (v_id) 124665935Spendry * like they do in the name lookup cache code. 124765935Spendry * That's too much work for now. 124865935Spendry */ 124965989Spendry 125066027Spendry #ifdef UNION_DIAGNOSTIC 125165989Spendry if (un->un_flags & UN_LOCKED) 125265989Spendry panic("union: inactivating locked node"); 125367073Spendry if (un->un_flags & UN_ULOCK) 125467073Spendry panic("union: inactivating w/locked upper node"); 125565989Spendry #endif 125665989Spendry 125767073Spendry if ((un->un_flags & UN_CACHED) == 0) 125867073Spendry vgone(ap->a_vp); 125967073Spendry 126065935Spendry return (0); 126165935Spendry } 126265935Spendry 126365935Spendry int 126465935Spendry union_reclaim(ap) 126565935Spendry struct vop_reclaim_args /* { 126665935Spendry struct vnode *a_vp; 126765935Spendry } */ *ap; 126865935Spendry { 126965935Spendry 127066053Spendry union_freevp(ap->a_vp); 127166053Spendry 127265935Spendry return (0); 127365935Spendry } 127465935Spendry 127565963Spendry int 127665963Spendry union_lock(ap) 127765963Spendry struct vop_lock_args *ap; 127865963Spendry { 127966149Spendry struct vnode *vp = ap->a_vp; 128066149Spendry struct union_node *un; 128165935Spendry 128266149Spendry start: 128366149Spendry while (vp->v_flag & VXLOCK) { 128466149Spendry vp->v_flag |= VXWANT; 128566149Spendry sleep((caddr_t)vp, PINOD); 128666149Spendry } 128766149Spendry 128866149Spendry un = VTOUNION(vp); 128966149Spendry 129067076Spendry if (un->un_uppervp != NULLVP) { 129166051Spendry if ((un->un_flags & UN_ULOCK) == 0) { 129266149Spendry un->un_flags |= UN_ULOCK; 129366051Spendry VOP_LOCK(un->un_uppervp); 129466051Spendry } 129566051Spendry #ifdef DIAGNOSTIC 129666051Spendry if (un->un_flags & UN_KLOCK) 129766051Spendry panic("union: dangling upper lock"); 129866051Spendry #endif 129966051Spendry } 130066051Spendry 130166149Spendry if (un->un_flags & UN_LOCKED) { 130265963Spendry #ifdef DIAGNOSTIC 130365989Spendry if (curproc && un->un_pid == curproc->p_pid && 130465989Spendry un->un_pid > -1 && curproc->p_pid > -1) 130565989Spendry panic("union: locking against myself"); 130665963Spendry #endif 130765963Spendry un->un_flags |= UN_WANT; 130865963Spendry sleep((caddr_t) &un->un_flags, PINOD); 130966149Spendry goto start; 131065963Spendry } 131165989Spendry 131265963Spendry #ifdef DIAGNOSTIC 131365989Spendry if (curproc) 131465989Spendry un->un_pid = curproc->p_pid; 131565989Spendry else 131665989Spendry un->un_pid = -1; 131765963Spendry #endif 131866028Spendry 131966149Spendry un->un_flags |= UN_LOCKED; 132066028Spendry return (0); 132165963Spendry } 132265963Spendry 132365935Spendry int 132465963Spendry union_unlock(ap) 132565963Spendry struct vop_lock_args *ap; 132665963Spendry { 132765963Spendry struct union_node *un = VTOUNION(ap->a_vp); 132865963Spendry 132965963Spendry #ifdef DIAGNOSTIC 133065965Spendry if ((un->un_flags & UN_LOCKED) == 0) 133165965Spendry panic("union: unlock unlocked node"); 133265989Spendry if (curproc && un->un_pid != curproc->p_pid && 133365989Spendry curproc->p_pid > -1 && un->un_pid > -1) 133465963Spendry panic("union: unlocking other process's union node"); 133565963Spendry #endif 133665963Spendry 133765963Spendry un->un_flags &= ~UN_LOCKED; 133866051Spendry 133966051Spendry if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) 134066051Spendry VOP_UNLOCK(un->un_uppervp); 134166051Spendry 134266051Spendry un->un_flags &= ~(UN_ULOCK|UN_KLOCK); 134366051Spendry 134465963Spendry if (un->un_flags & UN_WANT) { 134565963Spendry un->un_flags &= ~UN_WANT; 134665963Spendry wakeup((caddr_t) &un->un_flags); 134765963Spendry } 134865963Spendry 134965963Spendry #ifdef DIAGNOSTIC 135065963Spendry un->un_pid = 0; 135165963Spendry #endif 135266028Spendry 135366028Spendry return (0); 135465963Spendry } 135565963Spendry 135665963Spendry int 135765963Spendry union_bmap(ap) 135865963Spendry struct vop_bmap_args /* { 135965963Spendry struct vnode *a_vp; 136065963Spendry daddr_t a_bn; 136165963Spendry struct vnode **a_vpp; 136265963Spendry daddr_t *a_bnp; 136365963Spendry int *a_runp; 136465963Spendry } */ *ap; 136565963Spendry { 136665963Spendry int error; 136765963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 136866051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 136965963Spendry 137066051Spendry if (dolock) 137166051Spendry VOP_LOCK(vp); 137266152Spendry else 137366152Spendry FIXUP(VTOUNION(ap->a_vp)); 137465963Spendry error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); 137566051Spendry if (dolock) 137666051Spendry VOP_UNLOCK(vp); 137765963Spendry 137865963Spendry return (error); 137965963Spendry } 138065963Spendry 138165963Spendry int 138265935Spendry union_print(ap) 138365935Spendry struct vop_print_args /* { 138465935Spendry struct vnode *a_vp; 138565935Spendry } */ *ap; 138665935Spendry { 138765935Spendry struct vnode *vp = ap->a_vp; 138865935Spendry 138965935Spendry printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", 139065935Spendry vp, UPPERVP(vp), LOWERVP(vp)); 139165935Spendry return (0); 139265935Spendry } 139365935Spendry 139465963Spendry int 139565963Spendry union_islocked(ap) 139665963Spendry struct vop_islocked_args /* { 139765963Spendry struct vnode *a_vp; 139865963Spendry } */ *ap; 139965963Spendry { 140065935Spendry 140165963Spendry return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); 140265963Spendry } 140365963Spendry 140465935Spendry int 140565963Spendry union_pathconf(ap) 140665963Spendry struct vop_pathconf_args /* { 140765963Spendry struct vnode *a_vp; 140865963Spendry int a_name; 140965963Spendry int *a_retval; 141065935Spendry } */ *ap; 141165935Spendry { 141265935Spendry int error; 141365963Spendry struct vnode *vp = OTHERVP(ap->a_vp); 141466051Spendry int dolock = (vp == LOWERVP(ap->a_vp)); 141565935Spendry 141666051Spendry if (dolock) 141766051Spendry VOP_LOCK(vp); 141866152Spendry else 141966152Spendry FIXUP(VTOUNION(ap->a_vp)); 142065963Spendry error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); 142166051Spendry if (dolock) 142266051Spendry VOP_UNLOCK(vp); 142365935Spendry 142465963Spendry return (error); 142565963Spendry } 142665935Spendry 142765963Spendry int 142865963Spendry union_advlock(ap) 142965963Spendry struct vop_advlock_args /* { 143065963Spendry struct vnode *a_vp; 143165963Spendry caddr_t a_id; 143265963Spendry int a_op; 143365963Spendry struct flock *a_fl; 143465963Spendry int a_flags; 143565963Spendry } */ *ap; 143665963Spendry { 143765935Spendry 143865963Spendry return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, 143965963Spendry ap->a_fl, ap->a_flags)); 144065935Spendry } 144165935Spendry 144265935Spendry 144365935Spendry /* 144465963Spendry * XXX - vop_strategy must be hand coded because it has no 144565935Spendry * vnode in its arguments. 144665935Spendry * This goes away with a merged VM/buffer cache. 144765935Spendry */ 144865935Spendry int 144965963Spendry union_strategy(ap) 145065963Spendry struct vop_strategy_args /* { 145165935Spendry struct buf *a_bp; 145265935Spendry } */ *ap; 145365935Spendry { 145465935Spendry struct buf *bp = ap->a_bp; 145565935Spendry int error; 145665935Spendry struct vnode *savedvp; 145765935Spendry 145865935Spendry savedvp = bp->b_vp; 145965963Spendry bp->b_vp = OTHERVP(bp->b_vp); 146065935Spendry 146165935Spendry #ifdef DIAGNOSTIC 146265997Spendry if (bp->b_vp == NULLVP) 146365963Spendry panic("union_strategy: nil vp"); 146465963Spendry if (((bp->b_flags & B_READ) == 0) && 146565963Spendry (bp->b_vp == LOWERVP(savedvp))) 146665963Spendry panic("union_strategy: writing to lowervp"); 146765935Spendry #endif 146865935Spendry 146965963Spendry error = VOP_STRATEGY(bp); 147065935Spendry bp->b_vp = savedvp; 147165935Spendry 147265935Spendry return (error); 147365935Spendry } 147465935Spendry 147565935Spendry /* 147665935Spendry * Global vfs data structures 147765935Spendry */ 147865935Spendry int (**union_vnodeop_p)(); 147965965Spendry struct vnodeopv_entry_desc union_vnodeop_entries[] = { 148065963Spendry { &vop_default_desc, vn_default_error }, 148165963Spendry { &vop_lookup_desc, union_lookup }, /* lookup */ 148265963Spendry { &vop_create_desc, union_create }, /* create */ 148365963Spendry { &vop_mknod_desc, union_mknod }, /* mknod */ 148465963Spendry { &vop_open_desc, union_open }, /* open */ 148565963Spendry { &vop_close_desc, union_close }, /* close */ 148665963Spendry { &vop_access_desc, union_access }, /* access */ 148765963Spendry { &vop_getattr_desc, union_getattr }, /* getattr */ 148865963Spendry { &vop_setattr_desc, union_setattr }, /* setattr */ 148965963Spendry { &vop_read_desc, union_read }, /* read */ 149065963Spendry { &vop_write_desc, union_write }, /* write */ 149165963Spendry { &vop_ioctl_desc, union_ioctl }, /* ioctl */ 149265963Spendry { &vop_select_desc, union_select }, /* select */ 149365963Spendry { &vop_mmap_desc, union_mmap }, /* mmap */ 149465963Spendry { &vop_fsync_desc, union_fsync }, /* fsync */ 149565963Spendry { &vop_seek_desc, union_seek }, /* seek */ 149665963Spendry { &vop_remove_desc, union_remove }, /* remove */ 149765963Spendry { &vop_link_desc, union_link }, /* link */ 149865963Spendry { &vop_rename_desc, union_rename }, /* rename */ 149965963Spendry { &vop_mkdir_desc, union_mkdir }, /* mkdir */ 150065963Spendry { &vop_rmdir_desc, union_rmdir }, /* rmdir */ 150165963Spendry { &vop_symlink_desc, union_symlink }, /* symlink */ 150265963Spendry { &vop_readdir_desc, union_readdir }, /* readdir */ 150365963Spendry { &vop_readlink_desc, union_readlink }, /* readlink */ 150465963Spendry { &vop_abortop_desc, union_abortop }, /* abortop */ 150565963Spendry { &vop_inactive_desc, union_inactive }, /* inactive */ 150665963Spendry { &vop_reclaim_desc, union_reclaim }, /* reclaim */ 150765963Spendry { &vop_lock_desc, union_lock }, /* lock */ 150865963Spendry { &vop_unlock_desc, union_unlock }, /* unlock */ 150965963Spendry { &vop_bmap_desc, union_bmap }, /* bmap */ 151065963Spendry { &vop_strategy_desc, union_strategy }, /* strategy */ 151165963Spendry { &vop_print_desc, union_print }, /* print */ 151265963Spendry { &vop_islocked_desc, union_islocked }, /* islocked */ 151365963Spendry { &vop_pathconf_desc, union_pathconf }, /* pathconf */ 151465963Spendry { &vop_advlock_desc, union_advlock }, /* advlock */ 151565963Spendry #ifdef notdef 151665963Spendry { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ 151765963Spendry { &vop_valloc_desc, union_valloc }, /* valloc */ 151865963Spendry { &vop_vfree_desc, union_vfree }, /* vfree */ 151965963Spendry { &vop_truncate_desc, union_truncate }, /* truncate */ 152065963Spendry { &vop_update_desc, union_update }, /* update */ 152165963Spendry { &vop_bwrite_desc, union_bwrite }, /* bwrite */ 152265963Spendry #endif 152365935Spendry { (struct vnodeop_desc*)NULL, (int(*)())NULL } 152465935Spendry }; 152565935Spendry struct vnodeopv_desc union_vnodeop_opv_desc = 152665935Spendry { &union_vnodeop_p, union_vnodeop_entries }; 1527