2 * Copyright (c) 1994 Jan-Simon Pendry
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
38 * $FreeBSD: src/sys/miscfs/union/union_subr.c,v 1.43.2.2 2001/12/25 01:44:45 dillon Exp $
39 * $DragonFly: src/sys/vfs/union/union_subr.c,v 1.11 2004/04/24 04:32:06 drhodus Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
47 #include <sys/namei.h>
48 #include <sys/malloc.h>
49 #include <sys/fcntl.h>
51 #include <sys/filedesc.h>
52 #include <sys/module.h>
53 #include <sys/mount.h>
56 #include <vm/vm_extern.h> /* for vnode_pager_setsize */
57 #include <vm/vm_zone.h>
58 #include <vm/vm_object.h> /* for vm cache coherency */
61 extern int union_init (void);
63 /* must be power of two, otherwise change UNION_HASH() */
66 /* unsigned int ... */
67 #define UNION_HASH(u, l) \
68 (((((uintptr_t) (u)) + ((uintptr_t) l)) >> 8) & (NHASH-1))
70 static LIST_HEAD(unhead, union_node) unhead[NHASH];
71 static int unvplock[NHASH];
73 static void union_dircache_r (struct vnode *vp, struct vnode ***vppp,
75 static int union_list_lock (int ix);
76 static void union_list_unlock (int ix);
77 static int union_relookup (struct union_mount *um, struct vnode *dvp,
79 struct componentname *cnp,
80 struct componentname *cn, char *path,
82 static void union_updatevp (struct union_node *un,
83 struct vnode *uppervp,
84 struct vnode *lowervp);
85 static void union_newlower (struct union_node *, struct vnode *);
86 static void union_newupper (struct union_node *, struct vnode *);
87 static int union_copyfile (struct vnode *, struct vnode *,
88 struct ucred *, struct thread *);
89 static int union_vn_create (struct vnode **, struct union_node *,
91 static int union_vn_close (struct vnode *, int, struct ucred *,
99 for (i = 0; i < NHASH; i++)
100 LIST_INIT(&unhead[i]);
101 bzero((caddr_t)unvplock, sizeof(unvplock));
109 if (unvplock[ix] & UNVP_LOCKED) {
110 unvplock[ix] |= UNVP_WANT;
111 (void) tsleep((caddr_t) &unvplock[ix], 0, "unllck", 0);
114 unvplock[ix] |= UNVP_LOCKED;
119 union_list_unlock(ix)
122 unvplock[ix] &= ~UNVP_LOCKED;
124 if (unvplock[ix] & UNVP_WANT) {
125 unvplock[ix] &= ~UNVP_WANT;
126 wakeup((caddr_t) &unvplock[ix]);
133 * The uppervp, if not NULL, must be referenced and not locked by us
134 * The lowervp, if not NULL, must be referenced.
136 * if uppervp and lowervp match pointers already installed, nothing
137 * happens. The passed vp's (when matching) are not adjusted. This
138 * routine may only be called by union_newupper() and union_newlower().
142 union_updatevp(un, uppervp, lowervp)
143 struct union_node *un;
144 struct vnode *uppervp;
145 struct vnode *lowervp;
147 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
148 int nhash = UNION_HASH(uppervp, lowervp);
149 int docache = (lowervp != NULLVP || uppervp != NULLVP);
153 * Ensure locking is ordered from lower to higher
154 * to avoid deadlocks.
164 if (lhash != uhash) {
165 while (union_list_lock(lhash))
169 while (union_list_lock(uhash))
172 if (ohash != nhash || !docache) {
173 if (un->un_flags & UN_CACHED) {
174 un->un_flags &= ~UN_CACHED;
175 LIST_REMOVE(un, un_cache);
180 union_list_unlock(ohash);
182 if (un->un_lowervp != lowervp) {
183 if (un->un_lowervp) {
184 vrele(un->un_lowervp);
186 free(un->un_path, M_TEMP);
190 un->un_lowervp = lowervp;
191 un->un_lowersz = VNOVAL;
194 if (un->un_uppervp != uppervp) {
196 vrele(un->un_uppervp);
197 un->un_uppervp = uppervp;
198 un->un_uppersz = VNOVAL;
201 if (docache && (ohash != nhash)) {
202 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
203 un->un_flags |= UN_CACHED;
206 union_list_unlock(nhash);
210 * Set a new lowervp. The passed lowervp must be referenced and will be
211 * stored in the vp in a referenced state.
215 union_newlower(un, lowervp)
216 struct union_node *un;
217 struct vnode *lowervp;
219 union_updatevp(un, un->un_uppervp, lowervp);
223 * Set a new uppervp. The passed uppervp must be locked and will be
224 * stored in the vp in a locked state. The caller should not unlock
229 union_newupper(un, uppervp)
230 struct union_node *un;
231 struct vnode *uppervp;
233 union_updatevp(un, uppervp, un->un_lowervp);
237 * Keep track of size changes in the underlying vnodes.
238 * If the size changes, then callback to the vm layer
239 * giving priority to the upper layer size.
242 union_newsize(vp, uppersz, lowersz)
244 off_t uppersz, lowersz;
246 struct union_node *un;
249 /* only interested in regular files */
250 if (vp->v_type != VREG)
256 if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
257 un->un_uppersz = uppersz;
262 if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
263 un->un_lowersz = lowersz;
269 UDEBUG(("union: %s size now %ld\n",
270 (uppersz != VNOVAL ? "upper" : "lower"), (long)sz));
271 vnode_pager_setsize(vp, sz);
276 * union_allocvp: allocate a union_node and associate it with a
277 * parent union_node and one or two vnodes.
279 * vpp Holds the returned vnode locked and referenced if no
282 * mp Holds the mount point. mp may or may not be busied.
283 * allocvp makes no changes to mp.
285 * dvp Holds the parent union_node to the one we wish to create.
286 * XXX may only be used to traverse an uncopied lowervp-based
289 * dvp may or may not be locked. allocvp makes no changes
292 * upperdvp Holds the parent vnode to uppervp, generally used along
293 * with path component information to create a shadow of
294 * lowervp when uppervp does not exist.
296 * upperdvp is referenced but unlocked on entry, and will be
297 * dereferenced on return.
299 * uppervp Holds the new uppervp vnode to be stored in the
300 * union_node we are allocating. uppervp is referenced but
301 * not locked, and will be dereferenced on return.
303 * lowervp Holds the new lowervp vnode to be stored in the
304 * union_node we are allocating. lowervp is referenced but
305 * not locked, and will be dereferenced on return.
307 * cnp Holds path component information to be coupled with
308 * lowervp and upperdvp to allow unionfs to create an uppervp
309 * later on. Only used if lowervp is valid. The conents
310 * of cnp is only valid for the duration of the call.
312 * docache Determine whether this node should be entered in the
313 * cache or whether it should be destroyed as soon as possible.
315 * all union_nodes are maintained on a singly-linked
316 * list. new nodes are only allocated when they cannot
317 * be found on this list. entries on the list are
318 * removed when the vfs reclaim entry is called.
320 * a single lock is kept for the entire list. this is
321 * needed because the getnewvnode() function can block
322 * waiting for a vnode to become free, in which case there
323 * may be more than one process trying to get the same
324 * vnode. this lock is only taken if we are going to
325 * call getnewvnode, since the kernel itself is single-threaded.
327 * if an entry is found on the list, then call vget() to
328 * take a reference. this is done because there may be
329 * zero references to it and so it needs to removed from
330 * the vnode free list.
334 union_allocvp(vpp, mp, dvp, upperdvp, cnp, uppervp, lowervp, docache)
337 struct vnode *dvp; /* parent union vnode */
338 struct vnode *upperdvp; /* parent vnode of uppervp */
339 struct componentname *cnp; /* may be null */
340 struct vnode *uppervp; /* may be null */
341 struct vnode *lowervp; /* may be null */
345 struct union_node *un = 0;
346 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
347 struct thread *td = (cnp) ? cnp->cn_td : curthread; /* XXX */
352 if (uppervp == NULLVP && lowervp == NULLVP)
353 panic("union: unidentifiable allocation");
355 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
360 /* detect the root vnode (and aliases) */
362 if ((uppervp == um->um_uppervp) &&
363 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
364 if (lowervp == NULLVP) {
365 lowervp = um->um_lowervp;
366 if (lowervp != NULLVP)
375 } else for (try = 0; try < 3; try++) {
378 if (lowervp == NULLVP)
380 hash = UNION_HASH(uppervp, lowervp);
384 if (uppervp == NULLVP)
386 hash = UNION_HASH(uppervp, NULLVP);
390 if (lowervp == NULLVP)
392 hash = UNION_HASH(NULLVP, lowervp);
396 while (union_list_lock(hash))
399 for (un = unhead[hash].lh_first; un != 0;
400 un = un->un_cache.le_next) {
401 if ((un->un_lowervp == lowervp ||
402 un->un_lowervp == NULLVP) &&
403 (un->un_uppervp == uppervp ||
404 un->un_uppervp == NULLVP) &&
405 (UNIONTOV(un)->v_mount == mp)) {
406 if (vget(UNIONTOV(un), NULL, 0,
407 cnp ? cnp->cn_td : NULL)) {
408 union_list_unlock(hash);
415 union_list_unlock(hash);
423 * Obtain a lock on the union_node. Everything is unlocked
424 * except for dvp, so check that case. If they match, our
425 * new un is already locked. Otherwise we have to lock our
428 * A potential deadlock situation occurs when we are holding
429 * one lock while trying to get another. We must follow
430 * strict ordering rules to avoid it. We try to locate dvp
431 * by scanning up from un_vnode, since the most likely
432 * scenario is un being under dvp.
435 if (dvp && un->un_vnode != dvp) {
436 struct vnode *scan = un->un_vnode;
439 scan = VTOUNION(scan)->un_pvp;
440 } while (scan && scan->v_tag == VT_UNION && scan != dvp);
443 * our new un is above dvp (we never saw dvp
444 * while moving up the tree).
447 VOP_UNLOCK(dvp, NULL, 0, td);
448 error = vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE, td);
449 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
453 * our new un is under dvp
455 error = vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE, td);
457 } else if (dvp == NULLVP) {
459 * dvp is NULL, we need to lock un.
461 error = vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE, td);
464 * dvp == un->un_vnode, we are already locked.
473 * At this point, the union_node is locked and referenced.
475 * uppervp is locked and referenced or NULL, lowervp is
476 * referenced or NULL.
478 UDEBUG(("Modify existing un %p vn %p upper %p(refs %d) -> %p(refs %d)\n",
479 un, un->un_vnode, un->un_uppervp,
480 (un->un_uppervp ? un->un_uppervp->v_usecount : -99),
482 (uppervp ? uppervp->v_usecount : -99)
485 if (uppervp != un->un_uppervp) {
486 KASSERT(uppervp == NULL || uppervp->v_usecount > 0, ("union_allocvp: too few refs %d (at least 1 required) on uppervp", uppervp->v_usecount));
487 union_newupper(un, uppervp);
488 } else if (uppervp) {
489 KASSERT(uppervp->v_usecount > 1, ("union_allocvp: too few refs %d (at least 2 required) on uppervp", uppervp->v_usecount));
494 * Save information about the lower layer.
495 * This needs to keep track of pathname
496 * and directory information which union_vn_create
499 if (lowervp != un->un_lowervp) {
500 union_newlower(un, lowervp);
501 if (cnp && (lowervp != NULLVP)) {
502 un->un_path = malloc(cnp->cn_namelen+1,
504 bcopy(cnp->cn_nameptr, un->un_path,
506 un->un_path[cnp->cn_namelen] = '\0';
508 } else if (lowervp) {
515 if (upperdvp != un->un_dirvp) {
518 un->un_dirvp = upperdvp;
519 } else if (upperdvp) {
529 * otherwise lock the vp list while we call getnewvnode
530 * since that can block.
532 hash = UNION_HASH(uppervp, lowervp);
534 if (union_list_lock(hash))
539 * Create new node rather then replace old node
542 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
545 * If an error occurs clear out vnodes.
557 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
560 (*vpp)->v_flag |= vflag;
562 (*vpp)->v_type = uppervp->v_type;
564 (*vpp)->v_type = lowervp->v_type;
567 bzero(un, sizeof(*un));
569 lockinit(&un->un_lock, 0, "unlock", VLKTIMEOUT, 0);
570 vn_lock(*vpp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
573 un->un_uppervp = uppervp;
574 un->un_uppersz = VNOVAL;
575 un->un_lowervp = lowervp;
576 un->un_lowersz = VNOVAL;
577 un->un_dirvp = upperdvp;
578 un->un_pvp = dvp; /* only parent dir in new allocation */
584 if (cnp && (lowervp != NULLVP)) {
585 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
586 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
587 un->un_path[cnp->cn_namelen] = '\0';
594 LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
595 un->un_flags |= UN_CACHED;
600 union_list_unlock(hash);
609 struct union_node *un = VTOUNION(vp);
611 if (un->un_flags & UN_CACHED) {
612 un->un_flags &= ~UN_CACHED;
613 LIST_REMOVE(un, un_cache);
616 if (un->un_pvp != NULLVP) {
620 if (un->un_uppervp != NULLVP) {
621 vrele(un->un_uppervp);
622 un->un_uppervp = NULL;
624 if (un->un_lowervp != NULLVP) {
625 vrele(un->un_lowervp);
626 un->un_lowervp = NULL;
628 if (un->un_dirvp != NULLVP) {
633 free(un->un_path, M_TEMP);
637 FREE(vp->v_data, M_TEMP);
644 * copyfile. copy the vnode (fvp) to the vnode (tvp)
645 * using a sequence of reads and writes. both (fvp)
646 * and (tvp) are locked on entry and exit.
648 * fvp and tvp are both exclusive locked on call, but their refcount's
649 * haven't been bumped at all.
652 union_copyfile(fvp, tvp, cred, td)
665 * allocate a buffer of size MAXBSIZE.
666 * loop doing reads and writes, keeping track
667 * of the current uio offset.
668 * give up at the first sign of trouble.
671 bzero(&uio, sizeof(uio));
674 uio.uio_segflg = UIO_SYSSPACE;
677 VOP_LEASE(fvp, td, cred, LEASE_READ);
678 VOP_LEASE(tvp, td, cred, LEASE_WRITE);
680 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
682 /* ugly loop follows... */
684 off_t offset = uio.uio_offset;
694 iov.iov_len = MAXBSIZE;
695 uio.uio_resid = iov.iov_len;
696 uio.uio_rw = UIO_READ;
698 if ((error = VOP_READ(fvp, &uio, 0, cred)) != 0)
702 * Get bytes read, handle read eof case and setup for
705 if ((count = MAXBSIZE - uio.uio_resid) == 0)
710 * Write until an error occurs or our buffer has been
711 * exhausted, then update the offset for the next read.
713 while (bufoffset < count) {
716 iov.iov_base = buf + bufoffset;
717 iov.iov_len = count - bufoffset;
718 uio.uio_offset = offset + bufoffset;
719 uio.uio_rw = UIO_WRITE;
720 uio.uio_resid = iov.iov_len;
722 if ((error = VOP_WRITE(tvp, &uio, 0, cred)) != 0)
724 bufoffset += (count - bufoffset) - uio.uio_resid;
726 uio.uio_offset = offset + bufoffset;
727 } while (error == 0);
735 * un's vnode is assumed to be locked on entry and remains locked on exit.
739 union_copyup(un, docopy, cred, td)
740 struct union_node *un;
746 struct vnode *lvp, *uvp;
749 * If the user does not have read permission, the vnode should not
750 * be copied to upper layer.
752 vn_lock(un->un_lowervp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
753 error = VOP_ACCESS(un->un_lowervp, VREAD, cred, td);
754 VOP_UNLOCK(un->un_lowervp, NULL, 0, td);
758 error = union_vn_create(&uvp, un, td);
762 lvp = un->un_lowervp;
764 KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
767 * XX - should not ignore errors
770 vn_lock(lvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
771 error = VOP_OPEN(lvp, FREAD, cred, td);
772 if (error == 0 && vn_canvmio(lvp) == TRUE)
773 error = vfs_object_create(lvp, td);
775 error = union_copyfile(lvp, uvp, cred, td);
776 VOP_UNLOCK(lvp, NULL, 0, td);
777 (void) VOP_CLOSE(lvp, FREAD, td);
780 UDEBUG(("union: copied up %s\n", un->un_path));
783 VOP_UNLOCK(uvp, NULL, 0, td);
784 union_newupper(un, uvp);
785 KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
786 union_vn_close(uvp, FWRITE, cred, td);
787 KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
789 * Subsequent IOs will go to the top layer, so
790 * call close on the lower vnode and open on the
791 * upper vnode to ensure that the filesystem keeps
792 * its references counts right. This doesn't do
793 * the right thing with (cred) and (FREAD) though.
794 * Ignoring error returns is not right, either.
799 for (i = 0; i < un->un_openl; i++) {
800 (void) VOP_CLOSE(lvp, FREAD, td);
801 (void) VOP_OPEN(uvp, FREAD, cred, td);
804 if (vn_canvmio(uvp) == TRUE)
805 error = vfs_object_create(uvp, td);
817 * dvp should be locked on entry and will be locked on return. No
818 * net change in the ref count will occur.
820 * If an error is returned, *vpp will be invalid, otherwise it
821 * will hold a locked, referenced vnode. If *vpp == dvp then
822 * remember that only one exclusive lock is held.
826 union_relookup(um, dvp, vpp, cnp, cn, path, pathlen)
827 struct union_mount *um;
830 struct componentname *cnp;
831 struct componentname *cn;
838 * A new componentname structure must be faked up because
839 * there is no way to know where the upper level cnp came
840 * from or what it is being used for. This must duplicate
841 * some of the work done by NDINIT, some of the work done
842 * by namei, some of the work done by lookup and some of
843 * the work done by VOP_LOOKUP when given a CREATE flag.
844 * Conclusion: Horrible.
846 cn->cn_namelen = pathlen;
847 cn->cn_pnbuf = zalloc(namei_zone);
848 bcopy(path, cn->cn_pnbuf, cn->cn_namelen);
849 cn->cn_pnbuf[cn->cn_namelen] = '\0';
851 cn->cn_nameiop = NAMEI_CREATE;
852 cn->cn_flags = (CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_HASBUF |
853 CNP_SAVENAME | CNP_ISLASTCN);
854 cn->cn_td = cnp->cn_td;
855 if (um->um_op == UNMNT_ABOVE)
856 cn->cn_cred = cnp->cn_cred;
858 cn->cn_cred = um->um_cred;
859 cn->cn_nameptr = cn->cn_pnbuf;
860 cn->cn_consume = cnp->cn_consume;
863 VOP_UNLOCK(dvp, NULL, 0, cnp->cn_td);
866 * Pass dvp unlocked and referenced on call to relookup().
868 * If an error occurs, dvp will be returned unlocked and dereferenced.
871 if ((error = relookup(dvp, vpp, cn)) != 0) {
872 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, cnp->cn_td);
877 * If no error occurs, dvp will be returned locked with the reference
878 * left as before, and vpp will be returned referenced and locked.
880 * We want to return with dvp as it was passed to us, so we get
881 * rid of our reference.
888 * Create a shadow directory in the upper layer.
889 * The new vnode is returned locked.
891 * (um) points to the union mount structure for access to the
892 * the mounting process's credentials.
893 * (dvp) is the directory in which to create the shadow directory,
894 * it is locked (but not ref'd) on entry and return.
895 * (cnp) is the componentname to be created.
896 * (vpp) is the returned newly created shadow directory, which
897 * is returned locked and ref'd
900 union_mkshadow(um, dvp, cnp, vpp)
901 struct union_mount *um;
903 struct componentname *cnp;
908 struct thread *td = cnp->cn_td;
909 struct componentname cn;
911 error = union_relookup(um, dvp, vpp, cnp, &cn,
912 cnp->cn_nameptr, cnp->cn_namelen);
917 if (cn.cn_flags & CNP_HASBUF) {
918 zfree(namei_zone, cn.cn_pnbuf);
919 cn.cn_flags &= ~CNP_HASBUF;
930 * policy: when creating the shadow directory in the
931 * upper layer, create it owned by the user who did
932 * the mount, group from parent directory, and mode
933 * 777 modified by umask (ie mostly identical to the
934 * mkdir syscall). (jsp, kb)
939 va.va_mode = um->um_cmode;
941 /* VOP_LEASE: dvp is locked */
942 VOP_LEASE(dvp, td, cn.cn_cred, LEASE_WRITE);
944 error = VOP_MKDIR(dvp, NCPNULL, vpp, &cn, &va);
945 if (cn.cn_flags & CNP_HASBUF) {
946 zfree(namei_zone, cn.cn_pnbuf);
947 cn.cn_flags &= ~CNP_HASBUF;
954 * Create a whiteout entry in the upper layer.
956 * (um) points to the union mount structure for access to the
957 * the mounting process's credentials.
958 * (dvp) is the directory in which to create the whiteout.
959 * it is locked on entry and return.
960 * (cnp) is the componentname to be created.
963 union_mkwhiteout(um, dvp, cnp, path)
964 struct union_mount *um;
966 struct componentname *cnp;
970 struct thread *td = cnp->cn_td;
972 struct componentname cn;
975 KKASSERT(td->td_proc);
976 cred = td->td_proc->p_ucred;
978 error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
983 if (cn.cn_flags & CNP_HASBUF) {
984 zfree(namei_zone, cn.cn_pnbuf);
985 cn.cn_flags &= ~CNP_HASBUF;
994 /* VOP_LEASE: dvp is locked */
995 VOP_LEASE(dvp, td, cred, LEASE_WRITE);
997 error = VOP_WHITEOUT(dvp, NCPNULL, &cn, NAMEI_CREATE);
998 if (cn.cn_flags & CNP_HASBUF) {
999 zfree(namei_zone, cn.cn_pnbuf);
1000 cn.cn_flags &= ~CNP_HASBUF;
1006 * union_vn_create: creates and opens a new shadow file
1007 * on the upper union layer. this function is similar
1008 * in spirit to calling vn_open but it avoids calling namei().
1009 * the problem with calling namei is that a) it locks too many
1010 * things, and b) it doesn't start at the "right" directory,
1011 * whereas relookup is told where to start.
1013 * On entry, the vnode associated with un is locked. It remains locked
1016 * If no error occurs, *vpp contains a locked referenced vnode for your
1017 * use. If an error occurs *vpp iis undefined.
1020 union_vn_create(vpp, un, td)
1022 struct union_node *un;
1028 struct vattr *vap = &vat;
1029 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
1032 struct componentname cn;
1034 KKASSERT(td->td_proc);
1035 cred = td->td_proc->p_ucred;
1036 cmode = UN_FILEMODE & ~td->td_proc->p_fd->fd_cmask;
1041 * Build a new componentname structure (for the same
1042 * reasons outlines in union_mkshadow).
1043 * The difference here is that the file is owned by
1044 * the current user, rather than by the person who
1045 * did the mount, since the current user needs to be
1046 * able to write the file (that's why it is being
1047 * copied in the first place).
1049 cn.cn_namelen = strlen(un->un_path);
1050 cn.cn_pnbuf = zalloc(namei_zone);
1051 bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
1052 cn.cn_nameiop = NAMEI_CREATE;
1053 cn.cn_flags = (CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_HASBUF |
1054 CNP_SAVENAME | CNP_ISLASTCN);
1057 cn.cn_nameptr = cn.cn_pnbuf;
1061 * Pass dvp unlocked and referenced on call to relookup().
1063 * If an error occurs, dvp will be returned unlocked and dereferenced.
1066 error = relookup(un->un_dirvp, &vp, &cn);
1071 * If no error occurs, dvp will be returned locked with the reference
1072 * left as before, and vpp will be returned referenced and locked.
1076 if (cn.cn_flags & CNP_HASBUF) {
1077 zfree(namei_zone, cn.cn_pnbuf);
1078 cn.cn_flags &= ~CNP_HASBUF;
1080 if (vp == un->un_dirvp)
1088 * Good - there was no race to create the file
1089 * so go ahead and create it. The permissions
1090 * on the file will be 0666 modified by the
1091 * current user's umask. Access to the file, while
1092 * it is unioned, will require access to the top *and*
1093 * bottom files. Access when not unioned will simply
1094 * require access to the top-level file.
1095 * TODO: confirm choice of access permissions.
1098 vap->va_type = VREG;
1099 vap->va_mode = cmode;
1100 VOP_LEASE(un->un_dirvp, td, cred, LEASE_WRITE);
1101 error = VOP_CREATE(un->un_dirvp, NCPNULL, &vp, &cn, vap);
1102 if (cn.cn_flags & CNP_HASBUF) {
1103 zfree(namei_zone, cn.cn_pnbuf);
1104 cn.cn_flags &= ~CNP_HASBUF;
1110 error = VOP_OPEN(vp, fmode, cred, td);
1111 if (error == 0 && vn_canvmio(vp) == TRUE)
1112 error = vfs_object_create(vp, td);
1123 union_vn_close(vp, fmode, cred, td)
1132 return (VOP_CLOSE(vp, fmode, td));
1138 * union_removed_upper:
1140 * called with union_node unlocked. XXX
1144 union_removed_upper(struct union_node *un)
1146 struct thread *td = curthread; /* XXX */
1150 * Do not set the uppervp to NULLVP. If lowervp is NULLVP,
1151 * union node will have neither uppervp nor lowervp. We remove
1152 * the union node from cache, so that it will not be referrenced.
1154 union_newupper(un, NULLVP);
1155 if (un->un_dircache != 0) {
1156 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1158 free(un->un_dircache, M_TEMP);
1159 un->un_dircache = 0;
1162 if (un->un_flags & UN_CACHED) {
1163 un->un_flags &= ~UN_CACHED;
1164 LIST_REMOVE(un, un_cache);
1171 * determine whether a whiteout is needed
1172 * during a remove/rmdir operation.
1175 union_dowhiteout(struct union_node *un, struct ucred *cred, struct thread *td)
1179 if (un->un_lowervp != NULLVP)
1182 if (VOP_GETATTR(un->un_uppervp, &va, td) == 0 &&
1183 (va.va_flags & OPAQUE))
1190 union_dircache_r(vp, vppp, cntp)
1192 struct vnode ***vppp;
1195 struct union_node *un;
1197 if (vp->v_op != union_vnodeop_p) {
1202 panic("union: dircache table too small");
1211 if (un->un_uppervp != NULLVP)
1212 union_dircache_r(un->un_uppervp, vppp, cntp);
1213 if (un->un_lowervp != NULLVP)
1214 union_dircache_r(un->un_lowervp, vppp, cntp);
1218 union_dircache(struct vnode *vp, struct thread *td)
1223 struct vnode **dircache;
1224 struct union_node *un;
1227 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1228 dircache = VTOUNION(vp)->un_dircache;
1232 if (dircache == NULL) {
1234 union_dircache_r(vp, 0, &cnt);
1236 dircache = malloc(cnt * sizeof(struct vnode *),
1239 union_dircache_r(vp, &vpp, &cnt);
1245 if (*vpp++ == VTOUNION(vp)->un_uppervp)
1247 } while (*vpp != NULLVP);
1253 /*vn_lock(*vpp, NULL, LK_EXCLUSIVE | LK_RETRY, td);*/
1254 UDEBUG(("ALLOCVP-3 %p ref %d\n", *vpp, (*vpp ? (*vpp)->v_usecount : -99)));
1256 error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, NULL, *vpp, NULLVP, 0);
1257 UDEBUG(("ALLOCVP-3B %p ref %d\n", nvp, (*vpp ? (*vpp)->v_usecount : -99)));
1261 VTOUNION(vp)->un_dircache = 0;
1263 un->un_dircache = dircache;
1266 VOP_UNLOCK(vp, NULL, 0, td);
1271 * Guarentee coherency with the VM cache by invalidating any clean VM pages
1272 * associated with this write and updating any dirty VM pages. Since our
1273 * vnode is locked, other processes will not be able to read the pages in
1274 * again until after our write completes.
1276 * We also have to be coherent with reads, by flushing any pending dirty
1277 * pages prior to issuing the read.
1279 * XXX this is somewhat of a hack at the moment. To support this properly
1280 * we would have to be able to run VOP_READ and VOP_WRITE through the VM
1281 * cache. Then we wouldn't need to worry about coherency.
1285 union_vm_coherency(struct vnode *vp, struct uio *uio, int cleanfls)
1292 if ((object = vp->v_object) == NULL)
1295 pgoff = uio->uio_offset & PAGE_MASK;
1296 pstart = uio->uio_offset / PAGE_SIZE;
1297 pend = pstart + (uio->uio_resid + pgoff + PAGE_MASK) / PAGE_SIZE;
1299 vm_object_page_clean(object, pstart, pend, OBJPC_SYNC);
1301 vm_object_page_remove(object, pstart, pend, TRUE);
1305 * Module glue to remove #ifdef UNION from vfs_syscalls.c
1308 union_dircheck(struct thread *td, struct vnode **vp, struct file *fp)
1312 if ((*vp)->v_op == union_vnodeop_p) {
1315 lvp = union_dircache(*vp, td);
1316 if (lvp != NULLVP) {
1320 * If the directory is opaque,
1321 * then don't show lower entries
1323 error = VOP_GETATTR(*vp, &va, td);
1324 if (va.va_flags & OPAQUE) {
1330 if (lvp != NULLVP) {
1331 error = VOP_OPEN(lvp, FREAD, fp->f_cred, td);
1332 if (error == 0 && vn_canvmio(lvp) == TRUE)
1333 error = vfs_object_create(lvp, td);
1338 VOP_UNLOCK(lvp, NULL, 0, td);
1339 fp->f_data = (caddr_t) lvp;
1341 error = vn_close(*vp, FREAD, td);
1345 return -1; /* goto unionread */
1352 union_modevent(module_t mod, int type, void *data)
1356 union_dircheckp = union_dircheck;
1359 union_dircheckp = NULL;
1367 static moduledata_t union_mod = {
1373 DECLARE_MODULE(union_dircheck, union_mod, SI_SUB_VFS, SI_ORDER_ANY);