diff options
Diffstat (limited to 'sys/fs/unionfs')
-rw-r--r-- | sys/fs/unionfs/union.h | 117 | ||||
-rw-r--r-- | sys/fs/unionfs/union_subr.c | 744 | ||||
-rw-r--r-- | sys/fs/unionfs/union_vfsops.c | 550 | ||||
-rw-r--r-- | sys/fs/unionfs/union_vnops.c | 1495 |
4 files changed, 2906 insertions, 0 deletions
diff --git a/sys/fs/unionfs/union.h b/sys/fs/unionfs/union.h new file mode 100644 index 0000000..463218a --- /dev/null +++ b/sys/fs/unionfs/union.h @@ -0,0 +1,117 @@ +/* + * Copyright (c) 1994 The Regents of the University of California. + * Copyright (c) 1994 Jan-Simon Pendry. + * All rights reserved. + * + * This code is derived from software donated to Berkeley by + * Jan-Simon Pendry. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)union.h 8.2 (Berkeley) 2/17/94 + */ + +struct union_args { + char *target; /* Target of loopback */ + int mntflags; /* Options on the mount */ +}; + +#define UNMNT_ABOVE 0x0001 /* Target appears below mount point */ +#define UNMNT_BELOW 0x0002 /* Target appears below mount point */ +#define UNMNT_REPLACE 0x0003 /* Target replaces mount point */ +#define UNMNT_OPMASK 0x0003 + +struct union_mount { + struct vnode *um_uppervp; + struct vnode *um_lowervp; + struct ucred *um_cred; /* Credentials of user calling mount */ + int um_cmode; /* cmask from mount process */ + int um_op; /* Operation mode */ +}; + +#ifdef KERNEL + +/* + * DEFDIRMODE is the mode bits used to create a shadow directory. + */ +#define VRWXMODE (VREAD|VWRITE|VEXEC) +#define VRWMODE (VREAD|VWRITE) +#define UN_DIRMODE ((VRWXMODE)|(VRWXMODE>>3)|(VRWXMODE>>6)) +#define UN_FILEMODE ((VRWMODE)|(VRWMODE>>3)|(VRWMODE>>6)) + +/* + * A cache of vnode references + */ +struct union_node { + LIST_ENTRY(union_node) un_cache; /* Hash chain */ + struct vnode *un_vnode; /* Back pointer */ + struct vnode *un_uppervp; /* overlaying object */ + struct vnode *un_lowervp; /* underlying object */ + struct vnode *un_dirvp; /* Parent dir of uppervp */ + char *un_path; /* saved component name */ + int un_hash; /* saved un_path hash value */ + int un_openl; /* # of opens on lowervp */ + int un_flags; +#ifdef DIAGNOSTIC + pid_t un_pid; +#endif +}; + +#define UN_WANT 0x01 +#define UN_LOCKED 0x02 +#define UN_ULOCK 0x04 /* Upper node is locked */ +#define UN_KLOCK 0x08 /* Keep upper node locked on vput */ + +extern int union_allocvp __P((struct vnode **, struct mount *, + struct vnode *, struct vnode *, + struct componentname *, struct vnode *, + struct vnode *)); +extern int union_copyfile __P((struct proc *, struct ucred *, + struct vnode *, struct vnode *)); +extern int union_mkshadow __P((struct union_mount *, struct vnode *, + struct componentname *, struct vnode **)); +extern int union_vn_create __P((struct vnode **, struct union_node *, + struct proc *)); +extern int union_cn_close __P((struct vnode *, int, struct ucred *, + struct proc *)); +extern void union_removed_upper __P((struct union_node *un)); +extern struct vnode *union_lowervp __P((struct vnode *)); +extern void union_newlower __P((struct union_node *, struct vnode *)); +extern void union_newupper __P((struct union_node *, struct vnode *)); + +#define MOUNTTOUNIONMOUNT(mp) ((struct union_mount *)((mp)->mnt_data)) +#define VTOUNION(vp) ((struct union_node *)(vp)->v_data) +#define UNIONTOV(un) ((un)->un_vnode) +#define LOWERVP(vp) (VTOUNION(vp)->un_lowervp) +#define UPPERVP(vp) (VTOUNION(vp)->un_uppervp) +#define OTHERVP(vp) (UPPERVP(vp) ? UPPERVP(vp) : LOWERVP(vp)) + +extern int (**union_vnodeop_p)(); +extern struct vfsops union_vfsops; +#endif /* KERNEL */ diff --git a/sys/fs/unionfs/union_subr.c b/sys/fs/unionfs/union_subr.c new file mode 100644 index 0000000..77947d1 --- /dev/null +++ b/sys/fs/unionfs/union_subr.c @@ -0,0 +1,744 @@ +/* + * Copyright (c) 1994 Jan-Simon Pendry + * Copyright (c) 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Jan-Simon Pendry. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)union_subr.c 8.4 (Berkeley) 2/17/94 + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/time.h> +#include <sys/kernel.h> +#include <sys/vnode.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/file.h> +#include <sys/filedesc.h> +#include <sys/queue.h> +#include <miscfs/union/union.h> + +#ifdef DIAGNOSTIC +#include <sys/proc.h> +#endif + +/* must be power of two, otherwise change UNION_HASH() */ +#define NHASH 32 + +/* unsigned int ... */ +#define UNION_HASH(u, l) \ + (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1)) + +static LIST_HEAD(unhead, union_node) unhead[NHASH]; +static int unvplock[NHASH]; + +int +union_init() +{ + int i; + + for (i = 0; i < NHASH; i++) + LIST_INIT(&unhead[i]); + bzero((caddr_t) unvplock, sizeof(unvplock)); +} + +static int +union_list_lock(ix) + int ix; +{ + + if (unvplock[ix] & UN_LOCKED) { + unvplock[ix] |= UN_WANT; + sleep((caddr_t) &unvplock[ix], PINOD); + return (1); + } + + unvplock[ix] |= UN_LOCKED; + + return (0); +} + +static void +union_list_unlock(ix) + int ix; +{ + + unvplock[ix] &= ~UN_LOCKED; + + if (unvplock[ix] & UN_WANT) { + unvplock[ix] &= ~UN_WANT; + wakeup((caddr_t) &unvplock[ix]); + } +} + +void +union_updatevp(un, uppervp, lowervp) + struct union_node *un; + struct vnode *uppervp; + struct vnode *lowervp; +{ + int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp); + int nhash = UNION_HASH(uppervp, lowervp); + + if (ohash != nhash) { + /* + * Ensure locking is ordered from lower to higher + * to avoid deadlocks. + */ + if (nhash < ohash) { + int t = ohash; + ohash = nhash; + nhash = t; + } + + while (union_list_lock(ohash)) + continue; + + while (union_list_lock(nhash)) + continue; + + LIST_REMOVE(un, un_cache); + union_list_unlock(ohash); + } else { + while (union_list_lock(nhash)) + continue; + } + + if (un->un_lowervp != lowervp) { + if (un->un_lowervp) { + vrele(un->un_lowervp); + if (un->un_path) { + free(un->un_path, M_TEMP); + un->un_path = 0; + } + if (un->un_dirvp) { + vrele(un->un_dirvp); + un->un_dirvp = NULLVP; + } + } + un->un_lowervp = lowervp; + } + + if (un->un_uppervp != uppervp) { + if (un->un_uppervp) + vrele(un->un_uppervp); + + un->un_uppervp = uppervp; + } + + if (ohash != nhash) + LIST_INSERT_HEAD(&unhead[nhash], un, un_cache); + + union_list_unlock(nhash); +} + +void +union_newlower(un, lowervp) + struct union_node *un; + struct vnode *lowervp; +{ + + union_updatevp(un, un->un_uppervp, lowervp); +} + +void +union_newupper(un, uppervp) + struct union_node *un; + struct vnode *uppervp; +{ + + union_updatevp(un, uppervp, un->un_lowervp); +} + +/* + * allocate a union_node/vnode pair. the vnode is + * referenced and locked. the new vnode is returned + * via (vpp). (mp) is the mountpoint of the union filesystem, + * (dvp) is the parent directory where the upper layer object + * should exist (but doesn't) and (cnp) is the componentname + * information which is partially copied to allow the upper + * layer object to be created at a later time. (uppervp) + * and (lowervp) reference the upper and lower layer objects + * being mapped. either, but not both, can be nil. + * if supplied, (uppervp) is locked. + * the reference is either maintained in the new union_node + * object which is allocated, or they are vrele'd. + * + * all union_nodes are maintained on a singly-linked + * list. new nodes are only allocated when they cannot + * be found on this list. entries on the list are + * removed when the vfs reclaim entry is called. + * + * a single lock is kept for the entire list. this is + * needed because the getnewvnode() function can block + * waiting for a vnode to become free, in which case there + * may be more than one process trying to get the same + * vnode. this lock is only taken if we are going to + * call getnewvnode, since the kernel itself is single-threaded. + * + * if an entry is found on the list, then call vget() to + * take a reference. this is done because there may be + * zero references to it and so it needs to removed from + * the vnode free list. + */ +int +union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp) + struct vnode **vpp; + struct mount *mp; + struct vnode *undvp; + struct vnode *dvp; /* may be null */ + struct componentname *cnp; /* may be null */ + struct vnode *uppervp; /* may be null */ + struct vnode *lowervp; /* may be null */ +{ + int error; + struct union_node *un; + struct union_node **pp; + struct vnode *xlowervp = NULLVP; + int hash; + int try; + + if (uppervp == NULLVP && lowervp == NULLVP) + panic("union: unidentifiable allocation"); + + if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) { + xlowervp = lowervp; + lowervp = NULLVP; + } + +loop: + for (try = 0; try < 3; try++) { + switch (try) { + case 0: + if (lowervp == NULLVP) + continue; + hash = UNION_HASH(uppervp, lowervp); + break; + + case 1: + if (uppervp == NULLVP) + continue; + hash = UNION_HASH(uppervp, NULLVP); + break; + + case 2: + if (lowervp == NULLVP) + continue; + hash = UNION_HASH(NULLVP, lowervp); + break; + } + + while (union_list_lock(hash)) + continue; + + for (un = unhead[hash].lh_first; un != 0; + un = un->un_cache.le_next) { + if ((un->un_lowervp == lowervp || + un->un_lowervp == NULLVP) && + (un->un_uppervp == uppervp || + un->un_uppervp == NULLVP) && + (UNIONTOV(un)->v_mount == mp)) { + if (vget(UNIONTOV(un), 0)) { + union_list_unlock(hash); + goto loop; + } + break; + } + } + + union_list_unlock(hash); + + if (un) + break; + } + + if (un) { + /* + * Obtain a lock on the union_node. + * uppervp is locked, though un->un_uppervp + * may not be. this doesn't break the locking + * hierarchy since in the case that un->un_uppervp + * is not yet locked it will be vrele'd and replaced + * with uppervp. + */ + + if ((dvp != NULLVP) && (uppervp == dvp)) { + /* + * Access ``.'', so (un) will already + * be locked. Since this process has + * the lock on (uppervp) no other + * process can hold the lock on (un). + */ +#ifdef DIAGNOSTIC + if ((un->un_flags & UN_LOCKED) == 0) + panic("union: . not locked"); + else if (curproc && un->un_pid != curproc->p_pid && + un->un_pid > -1 && curproc->p_pid > -1) + panic("union: allocvp not lock owner"); +#endif + } else { + if (un->un_flags & UN_LOCKED) { + vrele(UNIONTOV(un)); + un->un_flags |= UN_WANT; + sleep((caddr_t) &un->un_flags, PINOD); + goto loop; + } + un->un_flags |= UN_LOCKED; + +#ifdef DIAGNOSTIC + if (curproc) + un->un_pid = curproc->p_pid; + else + un->un_pid = -1; +#endif + } + + /* + * At this point, the union_node is locked, + * un->un_uppervp may not be locked, and uppervp + * is locked or nil. + */ + + /* + * Save information about the upper layer. + */ + if (uppervp != un->un_uppervp) { + union_newupper(un, uppervp); + } else if (uppervp) { + vrele(uppervp); + } + + if (un->un_uppervp) { + un->un_flags |= UN_ULOCK; + un->un_flags &= ~UN_KLOCK; + } + + /* + * Save information about the lower layer. + * This needs to keep track of pathname + * and directory information which union_vn_create + * might need. + */ + if (lowervp != un->un_lowervp) { + union_newlower(un, lowervp); + if (cnp && (lowervp != NULLVP) && + (lowervp->v_type == VREG)) { + un->un_hash = cnp->cn_hash; + un->un_path = malloc(cnp->cn_namelen+1, + M_TEMP, M_WAITOK); + bcopy(cnp->cn_nameptr, un->un_path, + cnp->cn_namelen); + un->un_path[cnp->cn_namelen] = '\0'; + VREF(dvp); + un->un_dirvp = dvp; + } + } else if (lowervp) { + vrele(lowervp); + } + *vpp = UNIONTOV(un); + return (0); + } + + /* + * otherwise lock the vp list while we call getnewvnode + * since that can block. + */ + hash = UNION_HASH(uppervp, lowervp); + + if (union_list_lock(hash)) + goto loop; + + error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp); + if (error) { + if (uppervp) { + if (dvp == uppervp) + vrele(uppervp); + else + vput(uppervp); + } + if (lowervp) + vrele(lowervp); + + goto out; + } + + MALLOC((*vpp)->v_data, void *, sizeof(struct union_node), + M_TEMP, M_WAITOK); + + if (uppervp) + (*vpp)->v_type = uppervp->v_type; + else + (*vpp)->v_type = lowervp->v_type; + un = VTOUNION(*vpp); + un->un_vnode = *vpp; + un->un_uppervp = uppervp; + un->un_lowervp = lowervp; + un->un_openl = 0; + un->un_flags = UN_LOCKED; + if (un->un_uppervp) + un->un_flags |= UN_ULOCK; +#ifdef DIAGNOSTIC + if (curproc) + un->un_pid = curproc->p_pid; + else + un->un_pid = -1; +#endif + if (cnp && (lowervp != NULLVP) && (lowervp->v_type == VREG)) { + un->un_hash = cnp->cn_hash; + un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK); + bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen); + un->un_path[cnp->cn_namelen] = '\0'; + VREF(dvp); + un->un_dirvp = dvp; + } else { + un->un_hash = 0; + un->un_path = 0; + un->un_dirvp = 0; + } + + LIST_INSERT_HEAD(&unhead[hash], un, un_cache); + + if (xlowervp) + vrele(xlowervp); + +out: + union_list_unlock(hash); + + return (error); +} + +int +union_freevp(vp) + struct vnode *vp; +{ + struct union_node *un = VTOUNION(vp); + + LIST_REMOVE(un, un_cache); + + if (un->un_uppervp) + vrele(un->un_uppervp); + if (un->un_lowervp) + vrele(un->un_lowervp); + if (un->un_dirvp) + vrele(un->un_dirvp); + if (un->un_path) + free(un->un_path, M_TEMP); + + FREE(vp->v_data, M_TEMP); + vp->v_data = 0; + + return (0); +} + +/* + * copyfile. copy the vnode (fvp) to the vnode (tvp) + * using a sequence of reads and writes. both (fvp) + * and (tvp) are locked on entry and exit. + */ +int +union_copyfile(p, cred, fvp, tvp) + struct proc *p; + struct ucred *cred; + struct vnode *fvp; + struct vnode *tvp; +{ + char *buf; + struct uio uio; + struct iovec iov; + int error = 0; + + /* + * strategy: + * allocate a buffer of size MAXBSIZE. + * loop doing reads and writes, keeping track + * of the current uio offset. + * give up at the first sign of trouble. + */ + + uio.uio_procp = p; + uio.uio_segflg = UIO_SYSSPACE; + uio.uio_offset = 0; + + VOP_UNLOCK(fvp); /* XXX */ + LEASE_CHECK(fvp, p, cred, LEASE_READ); + VOP_LOCK(fvp); /* XXX */ + VOP_UNLOCK(tvp); /* XXX */ + LEASE_CHECK(tvp, p, cred, LEASE_WRITE); + VOP_LOCK(tvp); /* XXX */ + + buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK); + + /* ugly loop follows... */ + do { + off_t offset = uio.uio_offset; + + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + iov.iov_base = buf; + iov.iov_len = MAXBSIZE; + uio.uio_resid = iov.iov_len; + uio.uio_rw = UIO_READ; + error = VOP_READ(fvp, &uio, 0, cred); + + if (error == 0) { + uio.uio_iov = &iov; + uio.uio_iovcnt = 1; + iov.iov_base = buf; + iov.iov_len = MAXBSIZE - uio.uio_resid; + uio.uio_offset = offset; + uio.uio_rw = UIO_WRITE; + uio.uio_resid = iov.iov_len; + + if (uio.uio_resid == 0) + break; + + do { + error = VOP_WRITE(tvp, &uio, 0, cred); + } while ((uio.uio_resid > 0) && (error == 0)); + } + + } while (error == 0); + + free(buf, M_TEMP); + return (error); +} + +/* + * Create a shadow directory in the upper layer. + * The new vnode is returned locked. + * + * (um) points to the union mount structure for access to the + * the mounting process's credentials. + * (dvp) is the directory in which to create the shadow directory. + * it is unlocked on entry and exit. + * (cnp) is the componentname to be created. + * (vpp) is the returned newly created shadow directory, which + * is returned locked. + */ +int +union_mkshadow(um, dvp, cnp, vpp) + struct union_mount *um; + struct vnode *dvp; + struct componentname *cnp; + struct vnode **vpp; +{ + int error; + struct vattr va; + struct proc *p = cnp->cn_proc; + struct componentname cn; + + /* + * policy: when creating the shadow directory in the + * upper layer, create it owned by the user who did + * the mount, group from parent directory, and mode + * 777 modified by umask (ie mostly identical to the + * mkdir syscall). (jsp, kb) + */ + + /* + * A new componentname structure must be faked up because + * there is no way to know where the upper level cnp came + * from or what it is being used for. This must duplicate + * some of the work done by NDINIT, some of the work done + * by namei, some of the work done by lookup and some of + * the work done by VOP_LOOKUP when given a CREATE flag. + * Conclusion: Horrible. + * + * The pathname buffer will be FREEed by VOP_MKDIR. + */ + cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK); + bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen); + cn.cn_pnbuf[cnp->cn_namelen] = '\0'; + + cn.cn_nameiop = CREATE; + cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN); + cn.cn_proc = cnp->cn_proc; + if (um->um_op == UNMNT_ABOVE) + cn.cn_cred = cnp->cn_cred; + else + cn.cn_cred = um->um_cred; + cn.cn_nameptr = cn.cn_pnbuf; + cn.cn_namelen = cnp->cn_namelen; + cn.cn_hash = cnp->cn_hash; + cn.cn_consume = cnp->cn_consume; + + VREF(dvp); + if (error = relookup(dvp, vpp, &cn)) + return (error); + vrele(dvp); + + if (*vpp) { + VOP_ABORTOP(dvp, &cn); + VOP_UNLOCK(dvp); + vrele(*vpp); + *vpp = NULLVP; + return (EEXIST); + } + + VATTR_NULL(&va); + va.va_type = VDIR; + va.va_mode = um->um_cmode; + + /* LEASE_CHECK: dvp is locked */ + LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE); + + error = VOP_MKDIR(dvp, vpp, &cn, &va); + return (error); +} + +/* + * union_vn_create: creates and opens a new shadow file + * on the upper union layer. this function is similar + * in spirit to calling vn_open but it avoids calling namei(). + * the problem with calling namei is that a) it locks too many + * things, and b) it doesn't start at the "right" directory, + * whereas relookup is told where to start. + */ +int +union_vn_create(vpp, un, p) + struct vnode **vpp; + struct union_node *un; + struct proc *p; +{ + struct vnode *vp; + struct ucred *cred = p->p_ucred; + struct vattr vat; + struct vattr *vap = &vat; + int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL); + int error; + int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask; + char *cp; + struct componentname cn; + + *vpp = NULLVP; + + /* + * Build a new componentname structure (for the same + * reasons outlines in union_mkshadow). + * The difference here is that the file is owned by + * the current user, rather than by the person who + * did the mount, since the current user needs to be + * able to write the file (that's why it is being + * copied in the first place). + */ + cn.cn_namelen = strlen(un->un_path); + cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK); + bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1); + cn.cn_nameiop = CREATE; + cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN); + cn.cn_proc = p; + cn.cn_cred = p->p_ucred; + cn.cn_nameptr = cn.cn_pnbuf; + cn.cn_hash = un->un_hash; + cn.cn_consume = 0; + + VREF(un->un_dirvp); + if (error = relookup(un->un_dirvp, &vp, &cn)) + return (error); + vrele(un->un_dirvp); + + if (vp) { + VOP_ABORTOP(un->un_dirvp, &cn); + if (un->un_dirvp == vp) + vrele(un->un_dirvp); + else + vput(un->un_dirvp); + vrele(vp); + return (EEXIST); + } + + /* + * Good - there was no race to create the file + * so go ahead and create it. The permissions + * on the file will be 0666 modified by the + * current user's umask. Access to the file, while + * it is unioned, will require access to the top *and* + * bottom files. Access when not unioned will simply + * require access to the top-level file. + * TODO: confirm choice of access permissions. + */ + VATTR_NULL(vap); + vap->va_type = VREG; + vap->va_mode = cmode; + LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE); + if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) + return (error); + + if (error = VOP_OPEN(vp, fmode, cred, p)) { + vput(vp); + return (error); + } + + vp->v_writecount++; + *vpp = vp; + return (0); +} + +int +union_vn_close(vp, fmode, cred, p) + struct vnode *vp; + int fmode; + struct ucred *cred; + struct proc *p; +{ + if (fmode & FWRITE) + --vp->v_writecount; + return (VOP_CLOSE(vp, fmode)); +} + +void +union_removed_upper(un) + struct union_node *un; +{ + if (un->un_flags & UN_ULOCK) { + un->un_flags &= ~UN_ULOCK; + VOP_UNLOCK(un->un_uppervp); + } + + union_newupper(un, NULLVP); +} + +struct vnode * +union_lowervp(vp) + struct vnode *vp; +{ + struct union_node *un = VTOUNION(vp); + + if (un->un_lowervp && (vp->v_type == un->un_lowervp->v_type)) { + if (vget(un->un_lowervp, 0)) + return (NULLVP); + } + + return (un->un_lowervp); +} diff --git a/sys/fs/unionfs/union_vfsops.c b/sys/fs/unionfs/union_vfsops.c new file mode 100644 index 0000000..9fa2746 --- /dev/null +++ b/sys/fs/unionfs/union_vfsops.c @@ -0,0 +1,550 @@ +/* + * Copyright (c) 1994 The Regents of the University of California. + * Copyright (c) 1994 Jan-Simon Pendry. + * All rights reserved. + * + * This code is derived from software donated to Berkeley by + * Jan-Simon Pendry. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)union_vfsops.c 8.7 (Berkeley) 3/5/94 + */ + +/* + * Union Layer + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/proc.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/filedesc.h> +#include <sys/queue.h> +#include <miscfs/union/union.h> + +/* + * Mount union filesystem + */ +int +union_mount(mp, path, data, ndp, p) + struct mount *mp; + char *path; + caddr_t data; + struct nameidata *ndp; + struct proc *p; +{ + int error = 0; + struct union_args args; + struct vnode *lowerrootvp = NULLVP; + struct vnode *upperrootvp = NULLVP; + struct union_mount *um; + struct ucred *cred = 0; + struct ucred *scred; + struct vattr va; + char *cp; + int len; + u_int size; + +#ifdef UNION_DIAGNOSTIC + printf("union_mount(mp = %x)\n", mp); +#endif + + /* + * Update is a no-op + */ + if (mp->mnt_flag & MNT_UPDATE) { + /* + * Need to provide. + * 1. a way to convert between rdonly and rdwr mounts. + * 2. support for nfs exports. + */ + error = EOPNOTSUPP; + goto bad; + } + + /* + * Take a copy of the process's credentials. This isn't + * quite right since the euid will always be zero and we + * want to get the "real" users credentials. So fix up + * the uid field after taking the copy. + */ + cred = crdup(p->p_ucred); + cred->cr_uid = p->p_cred->p_ruid; + + /* + * Ensure the *real* user has write permission on the + * mounted-on directory. This allows the mount_union + * command to be made setuid root so allowing anyone + * to do union mounts onto any directory on which they + * have write permission and which they also own. + */ + error = VOP_GETATTR(mp->mnt_vnodecovered, &va, cred, p); + if (error) + goto bad; + if ((va.va_uid != cred->cr_uid) && + (cred->cr_uid != 0)) { + error = EACCES; + goto bad; + } + error = VOP_ACCESS(mp->mnt_vnodecovered, VWRITE, cred, p); + if (error) + goto bad; + + /* + * Get argument + */ + if (error = copyin(data, (caddr_t)&args, sizeof(struct union_args))) + goto bad; + + lowerrootvp = mp->mnt_vnodecovered; + VREF(lowerrootvp); + + /* + * Find upper node. Use the real process credentials, + * not the effective ones since this will have come + * through a setuid process (mount_union). All this + * messing around with permissions is entirely bogus + * and should be removed by allowing any user straight + * past the mount system call. + */ + scred = p->p_ucred; + p->p_ucred = cred; + NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT, + UIO_USERSPACE, args.target, p); + p->p_ucred = scred; + + if (error = namei(ndp)) + goto bad; + + upperrootvp = ndp->ni_vp; + vrele(ndp->ni_dvp); + ndp->ni_dvp = NULL; + + if (upperrootvp->v_type != VDIR) { + error = EINVAL; + goto bad; + } + + um = (struct union_mount *) malloc(sizeof(struct union_mount), + M_UFSMNT, M_WAITOK); /* XXX */ + + /* + * Keep a held reference to the target vnodes. + * They are vrele'd in union_unmount. + * + * Depending on the _BELOW flag, the filesystems are + * viewed in a different order. In effect, this is the + * same as providing a mount under option to the mount syscall. + */ + + um->um_op = args.mntflags & UNMNT_OPMASK; + switch (um->um_op) { + case UNMNT_ABOVE: + um->um_lowervp = lowerrootvp; + um->um_uppervp = upperrootvp; + break; + + case UNMNT_BELOW: + um->um_lowervp = upperrootvp; + um->um_uppervp = lowerrootvp; + break; + + case UNMNT_REPLACE: + vrele(lowerrootvp); + lowerrootvp = NULLVP; + um->um_uppervp = upperrootvp; + um->um_lowervp = lowerrootvp; + break; + + default: + error = EINVAL; + goto bad; + } + + um->um_cred = cred; + um->um_cmode = UN_DIRMODE &~ p->p_fd->fd_cmask; + + /* + * Depending on what you think the MNT_LOCAL flag might mean, + * you may want the && to be || on the conditional below. + * At the moment it has been defined that the filesystem is + * only local if it is all local, ie the MNT_LOCAL flag implies + * that the entire namespace is local. If you think the MNT_LOCAL + * flag implies that some of the files might be stored locally + * then you will want to change the conditional. + */ + if (um->um_op == UNMNT_ABOVE) { + if (((um->um_lowervp == NULLVP) || + (um->um_lowervp->v_mount->mnt_flag & MNT_LOCAL)) && + (um->um_uppervp->v_mount->mnt_flag & MNT_LOCAL)) + mp->mnt_flag |= MNT_LOCAL; + } + + /* + * Copy in the upper layer's RDONLY flag. This is for the benefit + * of lookup() which explicitly checks the flag, rather than asking + * the filesystem for it's own opinion. This means, that an update + * mount of the underlying filesystem to go from rdonly to rdwr + * will leave the unioned view as read-only. + */ + mp->mnt_flag |= (um->um_uppervp->v_mount->mnt_flag & MNT_RDONLY); + + /* + * This is a user mount. Privilege check for unmount + * will be done in union_unmount. + */ + mp->mnt_flag |= MNT_USER; + + mp->mnt_data = (qaddr_t) um; + getnewfsid(mp, MOUNT_UNION); + + (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size); + bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size); + + switch (um->um_op) { + case UNMNT_ABOVE: + cp = "<above>"; + break; + case UNMNT_BELOW: + cp = "<below>"; + break; + case UNMNT_REPLACE: + cp = ""; + break; + } + len = strlen(cp); + bcopy(cp, mp->mnt_stat.f_mntfromname, len); + + cp = mp->mnt_stat.f_mntfromname + len; + len = MNAMELEN - len; + + (void) copyinstr(args.target, cp, len - 1, &size); + bzero(cp + size, len - size); + +#ifdef UNION_DIAGNOSTIC + printf("union_mount: from %s, on %s\n", + mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname); +#endif + return (0); + +bad: + if (cred) + crfree(cred); + if (upperrootvp) + vrele(upperrootvp); + if (lowerrootvp) + vrele(lowerrootvp); + return (error); +} + +/* + * VFS start. Nothing needed here - the start routine + * on the underlying filesystem(s) will have been called + * when that filesystem was mounted. + */ +int +union_start(mp, flags, p) + struct mount *mp; + int flags; + struct proc *p; +{ + + return (0); +} + +/* + * Free reference to union layer + */ +int +union_unmount(mp, mntflags, p) + struct mount *mp; + int mntflags; + struct proc *p; +{ + struct union_mount *um = MOUNTTOUNIONMOUNT(mp); + struct vnode *um_rootvp; + int error; + int flags = 0; + extern int doforce; + +#ifdef UNION_DIAGNOSTIC + printf("union_unmount(mp = %x)\n", mp); +#endif + + /* only the mounter, or superuser can unmount */ + if ((p->p_cred->p_ruid != um->um_cred->cr_uid) && + (error = suser(p->p_ucred, &p->p_acflag))) + return (error); + + if (mntflags & MNT_FORCE) { + /* union can never be rootfs so don't check for it */ + if (!doforce) + return (EINVAL); + flags |= FORCECLOSE; + } + + if (error = union_root(mp, &um_rootvp)) + return (error); + if (um_rootvp->v_usecount > 1) { + vput(um_rootvp); + return (EBUSY); + } + if (error = vflush(mp, um_rootvp, flags)) { + vput(um_rootvp); + return (error); + } + +#ifdef UNION_DIAGNOSTIC + vprint("alias root of lower", um_rootvp); +#endif + /* + * Discard references to upper and lower target vnodes. + */ + if (um->um_lowervp) + vrele(um->um_lowervp); + vrele(um->um_uppervp); + crfree(um->um_cred); + /* + * Release reference on underlying root vnode + */ + vput(um_rootvp); + /* + * And blow it away for future re-use + */ + vgone(um_rootvp); + /* + * Finally, throw away the union_mount structure + */ + free(mp->mnt_data, M_UFSMNT); /* XXX */ + mp->mnt_data = 0; + return (0); +} + +int +union_root(mp, vpp) + struct mount *mp; + struct vnode **vpp; +{ + struct union_mount *um = MOUNTTOUNIONMOUNT(mp); + int error; + int loselock; + +#ifdef UNION_DIAGNOSTIC + printf("union_root(mp = %x, lvp = %x, uvp = %x)\n", mp, + um->um_lowervp, + um->um_uppervp); +#endif + + /* + * Return locked reference to root. + */ + VREF(um->um_uppervp); + if ((um->um_op == UNMNT_BELOW) && + VOP_ISLOCKED(um->um_uppervp)) { + loselock = 1; + } else { + VOP_LOCK(um->um_uppervp); + loselock = 0; + } + if (um->um_lowervp) + VREF(um->um_lowervp); + error = union_allocvp(vpp, mp, + (struct vnode *) 0, + (struct vnode *) 0, + (struct componentname *) 0, + um->um_uppervp, + um->um_lowervp); + + if (error) { + if (!loselock) + VOP_UNLOCK(um->um_uppervp); + vrele(um->um_uppervp); + if (um->um_lowervp) + vrele(um->um_lowervp); + } else { + (*vpp)->v_flag |= VROOT; + if (loselock) + VTOUNION(*vpp)->un_flags &= ~UN_ULOCK; + } + + return (error); +} + +int +union_quotactl(mp, cmd, uid, arg, p) + struct mount *mp; + int cmd; + uid_t uid; + caddr_t arg; + struct proc *p; +{ + + return (EOPNOTSUPP); +} + +int +union_statfs(mp, sbp, p) + struct mount *mp; + struct statfs *sbp; + struct proc *p; +{ + int error; + struct union_mount *um = MOUNTTOUNIONMOUNT(mp); + struct statfs mstat; + int lbsize; + +#ifdef UNION_DIAGNOSTIC + printf("union_statfs(mp = %x, lvp = %x, uvp = %x)\n", mp, + um->um_lowervp, + um->um_uppervp); +#endif + + bzero(&mstat, sizeof(mstat)); + + if (um->um_lowervp) { + error = VFS_STATFS(um->um_lowervp->v_mount, &mstat, p); + if (error) + return (error); + } + + /* now copy across the "interesting" information and fake the rest */ +#if 0 + sbp->f_type = mstat.f_type; + sbp->f_flags = mstat.f_flags; + sbp->f_bsize = mstat.f_bsize; + sbp->f_iosize = mstat.f_iosize; +#endif + lbsize = mstat.f_bsize; + sbp->f_blocks = mstat.f_blocks; + sbp->f_bfree = mstat.f_bfree; + sbp->f_bavail = mstat.f_bavail; + sbp->f_files = mstat.f_files; + sbp->f_ffree = mstat.f_ffree; + + error = VFS_STATFS(um->um_uppervp->v_mount, &mstat, p); + if (error) + return (error); + + sbp->f_type = MOUNT_UNION; + sbp->f_flags = mstat.f_flags; + sbp->f_bsize = mstat.f_bsize; + sbp->f_iosize = mstat.f_iosize; + + /* + * if the lower and upper blocksizes differ, then frig the + * block counts so that the sizes reported by df make some + * kind of sense. none of this makes sense though. + */ + + if (mstat.f_bsize != lbsize) { + sbp->f_blocks = sbp->f_blocks * lbsize / mstat.f_bsize; + sbp->f_bfree = sbp->f_bfree * lbsize / mstat.f_bsize; + sbp->f_bavail = sbp->f_bavail * lbsize / mstat.f_bsize; + } + sbp->f_blocks += mstat.f_blocks; + sbp->f_bfree += mstat.f_bfree; + sbp->f_bavail += mstat.f_bavail; + sbp->f_files += mstat.f_files; + sbp->f_ffree += mstat.f_ffree; + + if (sbp != &mp->mnt_stat) { + bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid)); + bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN); + bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN); + } + return (0); +} + +int +union_sync(mp, waitfor, cred, p) + struct mount *mp; + int waitfor; + struct ucred *cred; + struct proc *p; +{ + + /* + * XXX - Assumes no data cached at union layer. + */ + return (0); +} + +int +union_vget(mp, ino, vpp) + struct mount *mp; + ino_t ino; + struct vnode **vpp; +{ + + return (EOPNOTSUPP); +} + +int +union_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp) + struct mount *mp; + struct fid *fidp; + struct mbuf *nam; + struct vnode **vpp; + int *exflagsp; + struct ucred **credanonp; +{ + + return (EOPNOTSUPP); +} + +int +union_vptofh(vp, fhp) + struct vnode *vp; + struct fid *fhp; +{ + + return (EOPNOTSUPP); +} + +int union_init __P((void)); + +struct vfsops union_vfsops = { + union_mount, + union_start, + union_unmount, + union_root, + union_quotactl, + union_statfs, + union_sync, + union_vget, + union_fhtovp, + union_vptofh, + union_init, +}; diff --git a/sys/fs/unionfs/union_vnops.c b/sys/fs/unionfs/union_vnops.c new file mode 100644 index 0000000..96327b0 --- /dev/null +++ b/sys/fs/unionfs/union_vnops.c @@ -0,0 +1,1495 @@ +/* + * Copyright (c) 1992, 1993, 1994 The Regents of the University of California. + * Copyright (c) 1992, 1993, 1994 Jan-Simon Pendry. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Jan-Simon Pendry. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)union_vnops.c 8.6 (Berkeley) 2/17/94 + */ + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/proc.h> +#include <sys/file.h> +#include <sys/time.h> +#include <sys/types.h> +#include <sys/vnode.h> +#include <sys/mount.h> +#include <sys/namei.h> +#include <sys/malloc.h> +#include <sys/buf.h> +#include <sys/queue.h> +#include <miscfs/union/union.h> + +#define FIXUP(un) { \ + if (((un)->un_flags & UN_ULOCK) == 0) { \ + union_fixup(un); \ + } \ +} + +static void +union_fixup(un) + struct union_node *un; +{ + + VOP_LOCK(un->un_uppervp); + un->un_flags |= UN_ULOCK; +} + +static int +union_lookup1(udvp, dvp, vpp, cnp) + struct vnode *udvp; + struct vnode *dvp; + struct vnode **vpp; + struct componentname *cnp; +{ + int error; + struct vnode *tdvp; + struct mount *mp; + + /* + * If stepping up the directory tree, check for going + * back across the mount point, in which case do what + * lookup would do by stepping back down the mount + * hierarchy. + */ + if (cnp->cn_flags & ISDOTDOT) { + for (;;) { + /* + * Don't do the NOCROSSMOUNT check + * at this level. By definition, + * union fs deals with namespaces, not + * filesystems. + */ + if ((dvp->v_flag & VROOT) == 0) + break; + + tdvp = dvp; + dvp = dvp->v_mount->mnt_vnodecovered; + vput(tdvp); + VREF(dvp); + VOP_LOCK(dvp); + } + } + + error = VOP_LOOKUP(dvp, &tdvp, cnp); + if (error) + return (error); + + /* + * The parent directory will have been unlocked, unless lookup + * found the last component. In which case, re-lock the node + * here to allow it to be unlocked again (phew) in union_lookup. + */ + if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN)) + VOP_LOCK(dvp); + + dvp = tdvp; + + /* + * Lastly check if the current node is a mount point in + * which case walk up the mount hierarchy making sure not to + * bump into the root of the mount tree (ie. dvp != udvp). + */ + while (dvp != udvp && (dvp->v_type == VDIR) && + (mp = dvp->v_mountedhere)) { + + if (mp->mnt_flag & MNT_MLOCK) { + mp->mnt_flag |= MNT_MWAIT; + sleep((caddr_t) mp, PVFS); + continue; + } + + if (error = VFS_ROOT(mp, &tdvp)) { + vput(dvp); + return (error); + } + + vput(dvp); + dvp = tdvp; + } + + *vpp = dvp; + return (0); +} + +int +union_lookup(ap) + struct vop_lookup_args /* { + struct vnodeop_desc *a_desc; + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + } */ *ap; +{ + int error; + int uerror, lerror; + struct vnode *uppervp, *lowervp; + struct vnode *upperdvp, *lowerdvp; + struct vnode *dvp = ap->a_dvp; + struct union_node *dun = VTOUNION(dvp); + struct componentname *cnp = ap->a_cnp; + int lockparent = cnp->cn_flags & LOCKPARENT; + int rdonly = cnp->cn_flags & RDONLY; + struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount); + struct ucred *saved_cred; + + cnp->cn_flags |= LOCKPARENT; + + upperdvp = dun->un_uppervp; + lowerdvp = dun->un_lowervp; + uppervp = NULLVP; + lowervp = NULLVP; + + /* + * do the lookup in the upper level. + * if that level comsumes additional pathnames, + * then assume that something special is going + * on and just return that vnode. + */ + if (upperdvp) { + FIXUP(dun); + uerror = union_lookup1(um->um_uppervp, upperdvp, + &uppervp, cnp); + /*if (uppervp == upperdvp) + dun->un_flags |= UN_KLOCK;*/ + + if (cnp->cn_consume != 0) { + *ap->a_vpp = uppervp; + if (!lockparent) + cnp->cn_flags &= ~LOCKPARENT; + return (uerror); + } + } else { + uerror = ENOENT; + } + + /* + * in a similar way to the upper layer, do the lookup + * in the lower layer. this time, if there is some + * component magic going on, then vput whatever we got + * back from the upper layer and return the lower vnode + * instead. + */ + if (lowerdvp) { + int nameiop; + + VOP_LOCK(lowerdvp); + + /* + * Only do a LOOKUP on the bottom node, since + * we won't be making changes to it anyway. + */ + nameiop = cnp->cn_nameiop; + cnp->cn_nameiop = LOOKUP; + if (um->um_op == UNMNT_BELOW) { + saved_cred = cnp->cn_cred; + cnp->cn_cred = um->um_cred; + } + lerror = union_lookup1(um->um_lowervp, lowerdvp, + &lowervp, cnp); + if (um->um_op == UNMNT_BELOW) + cnp->cn_cred = saved_cred; + cnp->cn_nameiop = nameiop; + + if (lowervp != lowerdvp) + VOP_UNLOCK(lowerdvp); + + if (cnp->cn_consume != 0) { + if (uppervp) { + if (uppervp == upperdvp) + vrele(uppervp); + else + vput(uppervp); + uppervp = NULLVP; + } + *ap->a_vpp = lowervp; + if (!lockparent) + cnp->cn_flags &= ~LOCKPARENT; + return (lerror); + } + } else { + lerror = ENOENT; + } + + if (!lockparent) + cnp->cn_flags &= ~LOCKPARENT; + + /* + * at this point, we have uerror and lerror indicating + * possible errors with the lookups in the upper and lower + * layers. additionally, uppervp and lowervp are (locked) + * references to existing vnodes in the upper and lower layers. + * + * there are now three cases to consider. + * 1. if both layers returned an error, then return whatever + * error the upper layer generated. + * + * 2. if the top layer failed and the bottom layer succeeded + * then two subcases occur. + * a. the bottom vnode is not a directory, in which + * case just return a new union vnode referencing + * an empty top layer and the existing bottom layer. + * b. the bottom vnode is a directory, in which case + * create a new directory in the top-level and + * continue as in case 3. + * + * 3. if the top layer succeeded then return a new union + * vnode referencing whatever the new top layer and + * whatever the bottom layer returned. + */ + + *ap->a_vpp = NULLVP; + + /* case 1. */ + if ((uerror != 0) && (lerror != 0)) { + return (uerror); + } + + /* case 2. */ + if (uerror != 0 /* && (lerror == 0) */ ) { + if (lowervp->v_type == VDIR) { /* case 2b. */ + dun->un_flags &= ~UN_ULOCK; + VOP_UNLOCK(upperdvp); + uerror = union_mkshadow(um, upperdvp, cnp, &uppervp); + VOP_LOCK(upperdvp); + dun->un_flags |= UN_ULOCK; + + if (uerror) { + if (lowervp) { + vput(lowervp); + lowervp = NULLVP; + } + return (uerror); + } + } + } + + if (lowervp) + VOP_UNLOCK(lowervp); + + error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp, + uppervp, lowervp); + + if (error) { + if (uppervp) + vput(uppervp); + if (lowervp) + vrele(lowervp); + } else { + if (*ap->a_vpp != dvp) + if (!lockparent || !(cnp->cn_flags & ISLASTCN)) + VOP_UNLOCK(dvp); + } + + return (error); +} + +int +union_create(ap) + struct vop_create_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_dvp); + struct vnode *dvp = un->un_uppervp; + + if (dvp) { + int error; + struct vnode *vp; + + FIXUP(un); + + VREF(dvp); + un->un_flags |= UN_KLOCK; + vput(ap->a_dvp); + error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap); + if (error) + return (error); + + error = union_allocvp( + ap->a_vpp, + ap->a_dvp->v_mount, + ap->a_dvp, + NULLVP, + ap->a_cnp, + vp, + NULLVP); + if (error) + vput(vp); + return (error); + } + + vput(ap->a_dvp); + return (EROFS); +} + +int +union_mknod(ap) + struct vop_mknod_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_dvp); + struct vnode *dvp = un->un_uppervp; + + if (dvp) { + int error; + struct vnode *vp; + + FIXUP(un); + + VREF(dvp); + un->un_flags |= UN_KLOCK; + vput(ap->a_dvp); + error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap); + if (error) + return (error); + + if (vp) { + error = union_allocvp( + ap->a_vpp, + ap->a_dvp->v_mount, + ap->a_dvp, + NULLVP, + ap->a_cnp, + vp, + NULLVP); + if (error) + vput(vp); + } + return (error); + } + + vput(ap->a_dvp); + return (EROFS); +} + +int +union_open(ap) + struct vop_open_args /* { + struct vnodeop_desc *a_desc; + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_vp); + struct vnode *tvp; + int mode = ap->a_mode; + struct ucred *cred = ap->a_cred; + struct proc *p = ap->a_p; + int error; + + /* + * If there is an existing upper vp then simply open that. + */ + tvp = un->un_uppervp; + if (tvp == NULLVP) { + /* + * If the lower vnode is being opened for writing, then + * copy the file contents to the upper vnode and open that, + * otherwise can simply open the lower vnode. + */ + tvp = un->un_lowervp; + if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) { + struct vnode *vp; + int i; + + /* + * Open the named file in the upper layer. Note that + * the file may have come into existence *since* the + * lookup was done, since the upper layer may really + * be a loopback mount of some other filesystem... + * so open the file with exclusive create and barf if + * it already exists. + * XXX - perhaps should re-lookup the node (once more + * with feeling) and simply open that. Who knows. + */ + error = union_vn_create(&vp, un, p); + if (error) + return (error); + + /* at this point, uppervp is locked */ + union_newupper(un, vp); + un->un_flags |= UN_ULOCK; + + /* + * Now, if the file is being opened with truncation, + * then the (new) upper vnode is ready to fly, + * otherwise the data from the lower vnode must be + * copied to the upper layer first. This only works + * for regular files (check is made above). + */ + if ((mode & O_TRUNC) == 0) { + /* + * XXX - should not ignore errors + * from VOP_CLOSE + */ + VOP_LOCK(tvp); + error = VOP_OPEN(tvp, FREAD, cred, p); + if (error == 0) { + error = union_copyfile(p, cred, + tvp, un->un_uppervp); + VOP_UNLOCK(tvp); + (void) VOP_CLOSE(tvp, FREAD); + } else { + VOP_UNLOCK(tvp); + } + +#ifdef UNION_DIAGNOSTIC + if (!error) + uprintf("union: copied up %s\n", + un->un_path); +#endif + } + + un->un_flags &= ~UN_ULOCK; + VOP_UNLOCK(un->un_uppervp); + union_vn_close(un->un_uppervp, FWRITE, cred, p); + VOP_LOCK(un->un_uppervp); + un->un_flags |= UN_ULOCK; + + /* + * Subsequent IOs will go to the top layer, so + * call close on the lower vnode and open on the + * upper vnode to ensure that the filesystem keeps + * its references counts right. This doesn't do + * the right thing with (cred) and (FREAD) though. + * Ignoring error returns is not righ, either. + */ + for (i = 0; i < un->un_openl; i++) { + (void) VOP_CLOSE(tvp, FREAD); + (void) VOP_OPEN(un->un_uppervp, FREAD, cred, p); + } + un->un_openl = 0; + + if (error == 0) + error = VOP_OPEN(un->un_uppervp, mode, cred, p); + return (error); + } + + /* + * Just open the lower vnode + */ + un->un_openl++; + VOP_LOCK(tvp); + error = VOP_OPEN(tvp, mode, cred, p); + VOP_UNLOCK(tvp); + + return (error); + } + + FIXUP(un); + + error = VOP_OPEN(tvp, mode, cred, p); + + return (error); +} + +int +union_close(ap) + struct vop_close_args /* { + struct vnode *a_vp; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_vp); + struct vnode *vp; + + if (un->un_uppervp) { + vp = un->un_uppervp; + } else { +#ifdef UNION_DIAGNOSTIC + if (un->un_openl <= 0) + panic("union: un_openl cnt"); +#endif + --un->un_openl; + vp = un->un_lowervp; + } + + return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p)); +} + +/* + * Check access permission on the union vnode. + * The access check being enforced is to check + * against both the underlying vnode, and any + * copied vnode. This ensures that no additional + * file permissions are given away simply because + * the user caused an implicit file copy. + */ +int +union_access(ap) + struct vop_access_args /* { + struct vnodeop_desc *a_desc; + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_vp); + int error = EACCES; + struct vnode *vp; + + if (vp = un->un_uppervp) { + FIXUP(un); + return (VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p)); + } + + if (vp = un->un_lowervp) { + VOP_LOCK(vp); + error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p); + if (error == 0) { + struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount); + + if (um->um_op == UNMNT_BELOW) + error = VOP_ACCESS(vp, ap->a_mode, + um->um_cred, ap->a_p); + } + VOP_UNLOCK(vp); + if (error) + return (error); + } + + return (error); +} + +/* + * We handle getattr only to change the fsid. + */ +int +union_getattr(ap) + struct vop_getattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + int error; + struct union_node *un = VTOUNION(ap->a_vp); + struct vnode *vp = un->un_uppervp; + struct vattr *vap; + struct vattr va; + + + /* + * Some programs walk the filesystem hierarchy by counting + * links to directories to avoid stat'ing all the time. + * This means the link count on directories needs to be "correct". + * The only way to do that is to call getattr on both layers + * and fix up the link count. The link count will not necessarily + * be accurate but will be large enough to defeat the tree walkers. + */ + + vap = ap->a_vap; + + vp = un->un_uppervp; + if (vp != NULLVP) { + FIXUP(un); + error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); + if (error) + return (error); + } + + if (vp == NULLVP) { + vp = un->un_lowervp; + } else if (vp->v_type == VDIR) { + vp = un->un_lowervp; + vap = &va; + } else { + vp = NULLVP; + } + + if (vp != NULLVP) { + VOP_LOCK(vp); + error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p); + VOP_UNLOCK(vp); + if (error) + return (error); + } + + if ((vap != ap->a_vap) && (vap->va_type == VDIR)) + ap->a_vap->va_nlink += vap->va_nlink; + + vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; + return (0); +} + +int +union_setattr(ap) + struct vop_setattr_args /* { + struct vnode *a_vp; + struct vattr *a_vap; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_vp); + int error; + + /* + * Handle case of truncating lower object to zero size, + * by creating a zero length upper object. This is to + * handle the case of open with O_TRUNC and O_CREAT. + */ + if ((un->un_uppervp == NULLVP) && + /* assert(un->un_lowervp != NULLVP) */ + (un->un_lowervp->v_type == VREG) && + (ap->a_vap->va_size == 0)) { + struct vnode *vp; + + error = union_vn_create(&vp, un, ap->a_p); + if (error) + return (error); + + /* at this point, uppervp is locked */ + union_newupper(un, vp); + + VOP_UNLOCK(vp); + union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p); + VOP_LOCK(vp); + un->un_flags |= UN_ULOCK; + } + + /* + * Try to set attributes in upper layer, + * otherwise return read-only filesystem error. + */ + if (un->un_uppervp != NULLVP) { + FIXUP(un); + error = VOP_SETATTR(un->un_uppervp, ap->a_vap, + ap->a_cred, ap->a_p); + } else { + error = EROFS; + } + + return (error); +} + +int +union_read(ap) + struct vop_read_args /* { + struct vnode *a_vp; + struct uio *a_uio; + int a_ioflag; + struct ucred *a_cred; + } */ *ap; +{ + int error; + struct vnode *vp = OTHERVP(ap->a_vp); + int dolock = (vp == LOWERVP(ap->a_vp)); + + if (dolock) + VOP_LOCK(vp); + else + FIXUP(VTOUNION(ap->a_vp)); + error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); + if (dolock) + VOP_UNLOCK(vp); + + return (error); +} + +int +union_write(ap) + struct vop_read_args /* { + struct vnode *a_vp; + struct uio *a_uio; + int a_ioflag; + struct ucred *a_cred; + } */ *ap; +{ + int error; + struct vnode *vp = OTHERVP(ap->a_vp); + int dolock = (vp == LOWERVP(ap->a_vp)); + + if (dolock) + VOP_LOCK(vp); + else + FIXUP(VTOUNION(ap->a_vp)); + error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred); + if (dolock) + VOP_UNLOCK(vp); + + return (error); +} + +int +union_ioctl(ap) + struct vop_ioctl_args /* { + struct vnode *a_vp; + int a_command; + caddr_t a_data; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + + return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data, + ap->a_fflag, ap->a_cred, ap->a_p)); +} + +int +union_select(ap) + struct vop_select_args /* { + struct vnode *a_vp; + int a_which; + int a_fflags; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + + return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags, + ap->a_cred, ap->a_p)); +} + +int +union_mmap(ap) + struct vop_mmap_args /* { + struct vnode *a_vp; + int a_fflags; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap; +{ + + return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags, + ap->a_cred, ap->a_p)); +} + +int +union_fsync(ap) + struct vop_fsync_args /* { + struct vnode *a_vp; + struct ucred *a_cred; + int a_waitfor; + struct proc *a_p; + } */ *ap; +{ + int error = 0; + struct vnode *targetvp = OTHERVP(ap->a_vp); + + if (targetvp) { + int dolock = (targetvp == LOWERVP(ap->a_vp)); + + if (dolock) + VOP_LOCK(targetvp); + else + FIXUP(VTOUNION(ap->a_vp)); + error = VOP_FSYNC(targetvp, ap->a_cred, + ap->a_waitfor, ap->a_p); + if (dolock) + VOP_UNLOCK(targetvp); + } + + return (error); +} + +int +union_seek(ap) + struct vop_seek_args /* { + struct vnode *a_vp; + off_t a_oldoff; + off_t a_newoff; + struct ucred *a_cred; + } */ *ap; +{ + + return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred)); +} + +int +union_remove(ap) + struct vop_remove_args /* { + struct vnode *a_dvp; + struct vnode *a_vp; + struct componentname *a_cnp; + } */ *ap; +{ + int error; + struct union_node *dun = VTOUNION(ap->a_dvp); + struct union_node *un = VTOUNION(ap->a_vp); + + if (dun->un_uppervp && un->un_uppervp) { + struct vnode *dvp = dun->un_uppervp; + struct vnode *vp = un->un_uppervp; + + FIXUP(dun); + VREF(dvp); + dun->un_flags |= UN_KLOCK; + vput(ap->a_dvp); + FIXUP(un); + VREF(vp); + un->un_flags |= UN_KLOCK; + vput(ap->a_vp); + + error = VOP_REMOVE(dvp, vp, ap->a_cnp); + if (!error) + union_removed_upper(un); + + /* + * XXX: should create a whiteout here + */ + } else { + /* + * XXX: should create a whiteout here + */ + vput(ap->a_dvp); + vput(ap->a_vp); + error = EROFS; + } + + return (error); +} + +int +union_link(ap) + struct vop_link_args /* { + struct vnode *a_vp; + struct vnode *a_tdvp; + struct componentname *a_cnp; + } */ *ap; +{ + int error; + struct union_node *dun = VTOUNION(ap->a_vp); + struct union_node *un = VTOUNION(ap->a_tdvp); + + if (dun->un_uppervp && un->un_uppervp) { + struct vnode *dvp = dun->un_uppervp; + struct vnode *vp = un->un_uppervp; + + FIXUP(dun); + VREF(dvp); + dun->un_flags |= UN_KLOCK; + vput(ap->a_vp); + FIXUP(un); + VREF(vp); + vrele(ap->a_tdvp); + + error = VOP_LINK(dvp, vp, ap->a_cnp); + } else { + /* + * XXX: need to copy to upper layer + * and do the link there. + */ + vput(ap->a_vp); + vrele(ap->a_tdvp); + error = EROFS; + } + + return (error); +} + +int +union_rename(ap) + struct vop_rename_args /* { + struct vnode *a_fdvp; + struct vnode *a_fvp; + struct componentname *a_fcnp; + struct vnode *a_tdvp; + struct vnode *a_tvp; + struct componentname *a_tcnp; + } */ *ap; +{ + int error; + + struct vnode *fdvp = ap->a_fdvp; + struct vnode *fvp = ap->a_fvp; + struct vnode *tdvp = ap->a_tdvp; + struct vnode *tvp = ap->a_tvp; + + if (fdvp->v_op == union_vnodeop_p) { /* always true */ + struct union_node *un = VTOUNION(fdvp); + if (un->un_uppervp == NULLVP) { + error = EROFS; + goto bad; + } + + FIXUP(un); + fdvp = un->un_uppervp; + VREF(fdvp); + vrele(ap->a_fdvp); + } + + if (fvp->v_op == union_vnodeop_p) { /* always true */ + struct union_node *un = VTOUNION(fvp); + if (un->un_uppervp == NULLVP) { + error = EROFS; + goto bad; + } + + FIXUP(un); + fvp = un->un_uppervp; + VREF(fvp); + vrele(ap->a_fvp); + } + + if (tdvp->v_op == union_vnodeop_p) { + struct union_node *un = VTOUNION(tdvp); + if (un->un_uppervp == NULLVP) { + error = EROFS; + goto bad; + } + + tdvp = un->un_uppervp; + VREF(tdvp); + un->un_flags |= UN_KLOCK; + vput(ap->a_tdvp); + } + + if (tvp && tvp->v_op == union_vnodeop_p) { + struct union_node *un = VTOUNION(tvp); + if (un->un_uppervp == NULLVP) { + error = EROFS; + goto bad; + } + + tvp = un->un_uppervp; + VREF(tvp); + un->un_flags |= UN_KLOCK; + vput(ap->a_tvp); + } + + return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp)); + +bad: + vrele(fdvp); + vrele(fvp); + vput(tdvp); + if (tvp) + vput(tvp); + + return (error); +} + +int +union_mkdir(ap) + struct vop_mkdir_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_dvp); + struct vnode *dvp = un->un_uppervp; + + if (dvp) { + int error; + struct vnode *vp; + + FIXUP(un); + VREF(dvp); + un->un_flags |= UN_KLOCK; + vput(ap->a_dvp); + error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap); + if (error) + return (error); + + error = union_allocvp( + ap->a_vpp, + ap->a_dvp->v_mount, + ap->a_dvp, + NULLVP, + ap->a_cnp, + vp, + NULLVP); + if (error) + vput(vp); + return (error); + } + + vput(ap->a_dvp); + return (EROFS); +} + +int +union_rmdir(ap) + struct vop_rmdir_args /* { + struct vnode *a_dvp; + struct vnode *a_vp; + struct componentname *a_cnp; + } */ *ap; +{ + int error; + struct union_node *dun = VTOUNION(ap->a_dvp); + struct union_node *un = VTOUNION(ap->a_vp); + + if (dun->un_uppervp && un->un_uppervp) { + struct vnode *dvp = dun->un_uppervp; + struct vnode *vp = un->un_uppervp; + + FIXUP(dun); + VREF(dvp); + dun->un_flags |= UN_KLOCK; + vput(ap->a_dvp); + FIXUP(un); + VREF(vp); + un->un_flags |= UN_KLOCK; + vput(ap->a_vp); + + error = VOP_RMDIR(dvp, vp, ap->a_cnp); + if (!error) + union_removed_upper(un); + + /* + * XXX: should create a whiteout here + */ + } else { + /* + * XXX: should create a whiteout here + */ + vput(ap->a_dvp); + vput(ap->a_vp); + error = EROFS; + } + + return (error); +} + +int +union_symlink(ap) + struct vop_symlink_args /* { + struct vnode *a_dvp; + struct vnode **a_vpp; + struct componentname *a_cnp; + struct vattr *a_vap; + char *a_target; + } */ *ap; +{ + struct union_node *un = VTOUNION(ap->a_dvp); + struct vnode *dvp = un->un_uppervp; + + if (dvp) { + int error; + struct vnode *vp; + struct mount *mp = ap->a_dvp->v_mount; + + FIXUP(un); + VREF(dvp); + un->un_flags |= UN_KLOCK; + vput(ap->a_dvp); + error = VOP_SYMLINK(dvp, &vp, ap->a_cnp, + ap->a_vap, ap->a_target); + *ap->a_vpp = NULLVP; + return (error); + } + + vput(ap->a_dvp); + return (EROFS); +} + +/* + * union_readdir works in concert with getdirentries and + * readdir(3) to provide a list of entries in the unioned + * directories. getdirentries is responsible for walking + * down the union stack. readdir(3) is responsible for + * eliminating duplicate names from the returned data stream. + */ +int +union_readdir(ap) + struct vop_readdir_args /* { + struct vnodeop_desc *a_desc; + struct vnode *a_vp; + struct uio *a_uio; + struct ucred *a_cred; + } */ *ap; +{ + int error = 0; + struct union_node *un = VTOUNION(ap->a_vp); + + if (un->un_uppervp) { + FIXUP(un); + error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred); + } + + return (error); +} + +int +union_readlink(ap) + struct vop_readlink_args /* { + struct vnode *a_vp; + struct uio *a_uio; + struct ucred *a_cred; + } */ *ap; +{ + int error; + struct vnode *vp = OTHERVP(ap->a_vp); + int dolock = (vp == LOWERVP(ap->a_vp)); + + if (dolock) + VOP_LOCK(vp); + else + FIXUP(VTOUNION(ap->a_vp)); + error = VOP_READLINK(vp, ap->a_uio, ap->a_cred); + if (dolock) + VOP_UNLOCK(vp); + + return (error); +} + +int +union_abortop(ap) + struct vop_abortop_args /* { + struct vnode *a_dvp; + struct componentname *a_cnp; + } */ *ap; +{ + int error; + struct vnode *vp = OTHERVP(ap->a_dvp); + struct union_node *un = VTOUNION(ap->a_dvp); + int islocked = un->un_flags & UN_LOCKED; + int dolock = (vp == LOWERVP(ap->a_dvp)); + + if (islocked) { + if (dolock) + VOP_LOCK(vp); + else + FIXUP(VTOUNION(ap->a_dvp)); + } + error = VOP_ABORTOP(vp, ap->a_cnp); + if (islocked && dolock) + VOP_UNLOCK(vp); + + return (error); +} + +int +union_inactive(ap) + struct vop_inactive_args /* { + struct vnode *a_vp; + } */ *ap; +{ + + /* + * Do nothing (and _don't_ bypass). + * Wait to vrele lowervp until reclaim, + * so that until then our union_node is in the + * cache and reusable. + * + * NEEDSWORK: Someday, consider inactive'ing + * the lowervp and then trying to reactivate it + * with capabilities (v_id) + * like they do in the name lookup cache code. + * That's too much work for now. + */ + +#ifdef UNION_DIAGNOSTIC + struct union_node *un = VTOUNION(ap->a_vp); + + if (un->un_flags & UN_LOCKED) + panic("union: inactivating locked node"); +#endif + + return (0); +} + +int +union_reclaim(ap) + struct vop_reclaim_args /* { + struct vnode *a_vp; + } */ *ap; +{ + + union_freevp(ap->a_vp); + + return (0); +} + +int +union_lock(ap) + struct vop_lock_args *ap; +{ + struct vnode *vp = ap->a_vp; + struct union_node *un; + +start: + while (vp->v_flag & VXLOCK) { + vp->v_flag |= VXWANT; + sleep((caddr_t)vp, PINOD); + } + + un = VTOUNION(vp); + + if (un->un_uppervp) { + if ((un->un_flags & UN_ULOCK) == 0) { + un->un_flags |= UN_ULOCK; + VOP_LOCK(un->un_uppervp); + } +#ifdef DIAGNOSTIC + if (un->un_flags & UN_KLOCK) + panic("union: dangling upper lock"); +#endif + } + + if (un->un_flags & UN_LOCKED) { +#ifdef DIAGNOSTIC + if (curproc && un->un_pid == curproc->p_pid && + un->un_pid > -1 && curproc->p_pid > -1) + panic("union: locking against myself"); +#endif + un->un_flags |= UN_WANT; + sleep((caddr_t) &un->un_flags, PINOD); + goto start; + } + +#ifdef DIAGNOSTIC + if (curproc) + un->un_pid = curproc->p_pid; + else + un->un_pid = -1; +#endif + + un->un_flags |= UN_LOCKED; + return (0); +} + +int +union_unlock(ap) + struct vop_lock_args *ap; +{ + struct union_node *un = VTOUNION(ap->a_vp); + +#ifdef DIAGNOSTIC + if ((un->un_flags & UN_LOCKED) == 0) + panic("union: unlock unlocked node"); + if (curproc && un->un_pid != curproc->p_pid && + curproc->p_pid > -1 && un->un_pid > -1) + panic("union: unlocking other process's union node"); +#endif + + un->un_flags &= ~UN_LOCKED; + + if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK) + VOP_UNLOCK(un->un_uppervp); + + un->un_flags &= ~(UN_ULOCK|UN_KLOCK); + + if (un->un_flags & UN_WANT) { + un->un_flags &= ~UN_WANT; + wakeup((caddr_t) &un->un_flags); + } + +#ifdef DIAGNOSTIC + un->un_pid = 0; +#endif + + return (0); +} + +int +union_bmap(ap) + struct vop_bmap_args /* { + struct vnode *a_vp; + daddr_t a_bn; + struct vnode **a_vpp; + daddr_t *a_bnp; + int *a_runp; + } */ *ap; +{ + int error; + struct vnode *vp = OTHERVP(ap->a_vp); + int dolock = (vp == LOWERVP(ap->a_vp)); + + if (dolock) + VOP_LOCK(vp); + else + FIXUP(VTOUNION(ap->a_vp)); + error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp); + if (dolock) + VOP_UNLOCK(vp); + + return (error); +} + +int +union_print(ap) + struct vop_print_args /* { + struct vnode *a_vp; + } */ *ap; +{ + struct vnode *vp = ap->a_vp; + + printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n", + vp, UPPERVP(vp), LOWERVP(vp)); + return (0); +} + +int +union_islocked(ap) + struct vop_islocked_args /* { + struct vnode *a_vp; + } */ *ap; +{ + + return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0); +} + +int +union_pathconf(ap) + struct vop_pathconf_args /* { + struct vnode *a_vp; + int a_name; + int *a_retval; + } */ *ap; +{ + int error; + struct vnode *vp = OTHERVP(ap->a_vp); + int dolock = (vp == LOWERVP(ap->a_vp)); + + if (dolock) + VOP_LOCK(vp); + else + FIXUP(VTOUNION(ap->a_vp)); + error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval); + if (dolock) + VOP_UNLOCK(vp); + + return (error); +} + +int +union_advlock(ap) + struct vop_advlock_args /* { + struct vnode *a_vp; + caddr_t a_id; + int a_op; + struct flock *a_fl; + int a_flags; + } */ *ap; +{ + + return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op, + ap->a_fl, ap->a_flags)); +} + + +/* + * XXX - vop_strategy must be hand coded because it has no + * vnode in its arguments. + * This goes away with a merged VM/buffer cache. + */ +int +union_strategy(ap) + struct vop_strategy_args /* { + struct buf *a_bp; + } */ *ap; +{ + struct buf *bp = ap->a_bp; + int error; + struct vnode *savedvp; + + savedvp = bp->b_vp; + bp->b_vp = OTHERVP(bp->b_vp); + +#ifdef DIAGNOSTIC + if (bp->b_vp == NULLVP) + panic("union_strategy: nil vp"); + if (((bp->b_flags & B_READ) == 0) && + (bp->b_vp == LOWERVP(savedvp))) + panic("union_strategy: writing to lowervp"); +#endif + + error = VOP_STRATEGY(bp); + bp->b_vp = savedvp; + + return (error); +} + +/* + * Global vfs data structures + */ +int (**union_vnodeop_p)(); +struct vnodeopv_entry_desc union_vnodeop_entries[] = { + { &vop_default_desc, vn_default_error }, + { &vop_lookup_desc, union_lookup }, /* lookup */ + { &vop_create_desc, union_create }, /* create */ + { &vop_mknod_desc, union_mknod }, /* mknod */ + { &vop_open_desc, union_open }, /* open */ + { &vop_close_desc, union_close }, /* close */ + { &vop_access_desc, union_access }, /* access */ + { &vop_getattr_desc, union_getattr }, /* getattr */ + { &vop_setattr_desc, union_setattr }, /* setattr */ + { &vop_read_desc, union_read }, /* read */ + { &vop_write_desc, union_write }, /* write */ + { &vop_ioctl_desc, union_ioctl }, /* ioctl */ + { &vop_select_desc, union_select }, /* select */ + { &vop_mmap_desc, union_mmap }, /* mmap */ + { &vop_fsync_desc, union_fsync }, /* fsync */ + { &vop_seek_desc, union_seek }, /* seek */ + { &vop_remove_desc, union_remove }, /* remove */ + { &vop_link_desc, union_link }, /* link */ + { &vop_rename_desc, union_rename }, /* rename */ + { &vop_mkdir_desc, union_mkdir }, /* mkdir */ + { &vop_rmdir_desc, union_rmdir }, /* rmdir */ + { &vop_symlink_desc, union_symlink }, /* symlink */ + { &vop_readdir_desc, union_readdir }, /* readdir */ + { &vop_readlink_desc, union_readlink }, /* readlink */ + { &vop_abortop_desc, union_abortop }, /* abortop */ + { &vop_inactive_desc, union_inactive }, /* inactive */ + { &vop_reclaim_desc, union_reclaim }, /* reclaim */ + { &vop_lock_desc, union_lock }, /* lock */ + { &vop_unlock_desc, union_unlock }, /* unlock */ + { &vop_bmap_desc, union_bmap }, /* bmap */ + { &vop_strategy_desc, union_strategy }, /* strategy */ + { &vop_print_desc, union_print }, /* print */ + { &vop_islocked_desc, union_islocked }, /* islocked */ + { &vop_pathconf_desc, union_pathconf }, /* pathconf */ + { &vop_advlock_desc, union_advlock }, /* advlock */ +#ifdef notdef + { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */ + { &vop_valloc_desc, union_valloc }, /* valloc */ + { &vop_vfree_desc, union_vfree }, /* vfree */ + { &vop_truncate_desc, union_truncate }, /* truncate */ + { &vop_update_desc, union_update }, /* update */ + { &vop_bwrite_desc, union_bwrite }, /* bwrite */ +#endif + { (struct vnodeop_desc*)NULL, (int(*)())NULL } +}; +struct vnodeopv_desc union_vnodeop_opv_desc = + { &union_vnodeop_p, union_vnodeop_entries }; |