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.12 2004/05/20 05:09:18 cpressey 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));
106 union_list_lock(int ix)
108 if (unvplock[ix] & UNVP_LOCKED) {
109 unvplock[ix] |= UNVP_WANT;
110 (void) tsleep((caddr_t) &unvplock[ix], 0, "unllck", 0);
113 unvplock[ix] |= UNVP_LOCKED;
118 union_list_unlock(int ix)
120 unvplock[ix] &= ~UNVP_LOCKED;
122 if (unvplock[ix] & UNVP_WANT) {
123 unvplock[ix] &= ~UNVP_WANT;
124 wakeup((caddr_t) &unvplock[ix]);
131 * The uppervp, if not NULL, must be referenced and not locked by us
132 * The lowervp, if not NULL, must be referenced.
134 * if uppervp and lowervp match pointers already installed, nothing
135 * happens. The passed vp's (when matching) are not adjusted. This
136 * routine may only be called by union_newupper() and union_newlower().
140 union_updatevp(struct union_node *un, struct vnode *uppervp,
141 struct vnode *lowervp)
143 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
144 int nhash = UNION_HASH(uppervp, lowervp);
145 int docache = (lowervp != NULLVP || uppervp != NULLVP);
149 * Ensure locking is ordered from lower to higher
150 * to avoid deadlocks.
160 if (lhash != uhash) {
161 while (union_list_lock(lhash))
165 while (union_list_lock(uhash))
168 if (ohash != nhash || !docache) {
169 if (un->un_flags & UN_CACHED) {
170 un->un_flags &= ~UN_CACHED;
171 LIST_REMOVE(un, un_cache);
176 union_list_unlock(ohash);
178 if (un->un_lowervp != lowervp) {
179 if (un->un_lowervp) {
180 vrele(un->un_lowervp);
182 free(un->un_path, M_TEMP);
186 un->un_lowervp = lowervp;
187 un->un_lowersz = VNOVAL;
190 if (un->un_uppervp != uppervp) {
192 vrele(un->un_uppervp);
193 un->un_uppervp = uppervp;
194 un->un_uppersz = VNOVAL;
197 if (docache && (ohash != nhash)) {
198 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
199 un->un_flags |= UN_CACHED;
202 union_list_unlock(nhash);
206 * Set a new lowervp. The passed lowervp must be referenced and will be
207 * stored in the vp in a referenced state.
211 union_newlower(struct union_node *un, struct vnode *lowervp)
213 union_updatevp(un, un->un_uppervp, lowervp);
217 * Set a new uppervp. The passed uppervp must be locked and will be
218 * stored in the vp in a locked state. The caller should not unlock
223 union_newupper(struct union_node *un, struct vnode *uppervp)
225 union_updatevp(un, uppervp, un->un_lowervp);
229 * Keep track of size changes in the underlying vnodes.
230 * If the size changes, then callback to the vm layer
231 * giving priority to the upper layer size.
234 union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz)
236 struct union_node *un;
239 /* only interested in regular files */
240 if (vp->v_type != VREG)
246 if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
247 un->un_uppersz = uppersz;
252 if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
253 un->un_lowersz = lowersz;
259 UDEBUG(("union: %s size now %ld\n",
260 (uppersz != VNOVAL ? "upper" : "lower"), (long)sz));
261 vnode_pager_setsize(vp, sz);
266 * union_allocvp: allocate a union_node and associate it with a
267 * parent union_node and one or two vnodes.
269 * vpp Holds the returned vnode locked and referenced if no
272 * mp Holds the mount point. mp may or may not be busied.
273 * allocvp makes no changes to mp.
275 * dvp Holds the parent union_node to the one we wish to create.
276 * XXX may only be used to traverse an uncopied lowervp-based
279 * dvp may or may not be locked. allocvp makes no changes
282 * upperdvp Holds the parent vnode to uppervp, generally used along
283 * with path component information to create a shadow of
284 * lowervp when uppervp does not exist.
286 * upperdvp is referenced but unlocked on entry, and will be
287 * dereferenced on return.
289 * uppervp Holds the new uppervp vnode to be stored in the
290 * union_node we are allocating. uppervp is referenced but
291 * not locked, and will be dereferenced on return.
293 * lowervp Holds the new lowervp vnode to be stored in the
294 * union_node we are allocating. lowervp is referenced but
295 * not locked, and will be dereferenced on return.
297 * cnp Holds path component information to be coupled with
298 * lowervp and upperdvp to allow unionfs to create an uppervp
299 * later on. Only used if lowervp is valid. The conents
300 * of cnp is only valid for the duration of the call.
302 * docache Determine whether this node should be entered in the
303 * cache or whether it should be destroyed as soon as possible.
305 * all union_nodes are maintained on a singly-linked
306 * list. new nodes are only allocated when they cannot
307 * be found on this list. entries on the list are
308 * removed when the vfs reclaim entry is called.
310 * a single lock is kept for the entire list. this is
311 * needed because the getnewvnode() function can block
312 * waiting for a vnode to become free, in which case there
313 * may be more than one process trying to get the same
314 * vnode. this lock is only taken if we are going to
315 * call getnewvnode, since the kernel itself is single-threaded.
317 * if an entry is found on the list, then call vget() to
318 * take a reference. this is done because there may be
319 * zero references to it and so it needs to removed from
320 * the vnode free list.
324 union_allocvp(struct vnode **vpp,
326 struct vnode *dvp, /* parent union vnode */
327 struct vnode *upperdvp, /* parent vnode of uppervp */
328 struct componentname *cnp, /* may be null */
329 struct vnode *uppervp, /* may be null */
330 struct vnode *lowervp, /* may be null */
334 struct union_node *un = 0;
335 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
336 struct thread *td = (cnp) ? cnp->cn_td : curthread; /* XXX */
341 if (uppervp == NULLVP && lowervp == NULLVP)
342 panic("union: unidentifiable allocation");
344 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
349 /* detect the root vnode (and aliases) */
351 if ((uppervp == um->um_uppervp) &&
352 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
353 if (lowervp == NULLVP) {
354 lowervp = um->um_lowervp;
355 if (lowervp != NULLVP)
364 } else for (try = 0; try < 3; try++) {
367 if (lowervp == NULLVP)
369 hash = UNION_HASH(uppervp, lowervp);
373 if (uppervp == NULLVP)
375 hash = UNION_HASH(uppervp, NULLVP);
379 if (lowervp == NULLVP)
381 hash = UNION_HASH(NULLVP, lowervp);
385 while (union_list_lock(hash))
388 for (un = unhead[hash].lh_first; un != 0;
389 un = un->un_cache.le_next) {
390 if ((un->un_lowervp == lowervp ||
391 un->un_lowervp == NULLVP) &&
392 (un->un_uppervp == uppervp ||
393 un->un_uppervp == NULLVP) &&
394 (UNIONTOV(un)->v_mount == mp)) {
395 if (vget(UNIONTOV(un), NULL, 0,
396 cnp ? cnp->cn_td : NULL)) {
397 union_list_unlock(hash);
404 union_list_unlock(hash);
412 * Obtain a lock on the union_node. Everything is unlocked
413 * except for dvp, so check that case. If they match, our
414 * new un is already locked. Otherwise we have to lock our
417 * A potential deadlock situation occurs when we are holding
418 * one lock while trying to get another. We must follow
419 * strict ordering rules to avoid it. We try to locate dvp
420 * by scanning up from un_vnode, since the most likely
421 * scenario is un being under dvp.
424 if (dvp && un->un_vnode != dvp) {
425 struct vnode *scan = un->un_vnode;
428 scan = VTOUNION(scan)->un_pvp;
429 } while (scan && scan->v_tag == VT_UNION && scan != dvp);
432 * our new un is above dvp (we never saw dvp
433 * while moving up the tree).
436 VOP_UNLOCK(dvp, NULL, 0, td);
437 error = vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE, td);
438 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
442 * our new un is under dvp
444 error = vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE, td);
446 } else if (dvp == NULLVP) {
448 * dvp is NULL, we need to lock un.
450 error = vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE, td);
453 * dvp == un->un_vnode, we are already locked.
462 * At this point, the union_node is locked and referenced.
464 * uppervp is locked and referenced or NULL, lowervp is
465 * referenced or NULL.
467 UDEBUG(("Modify existing un %p vn %p upper %p(refs %d) -> %p(refs %d)\n",
468 un, un->un_vnode, un->un_uppervp,
469 (un->un_uppervp ? un->un_uppervp->v_usecount : -99),
471 (uppervp ? uppervp->v_usecount : -99)
474 if (uppervp != un->un_uppervp) {
475 KASSERT(uppervp == NULL || uppervp->v_usecount > 0, ("union_allocvp: too few refs %d (at least 1 required) on uppervp", uppervp->v_usecount));
476 union_newupper(un, uppervp);
477 } else if (uppervp) {
478 KASSERT(uppervp->v_usecount > 1, ("union_allocvp: too few refs %d (at least 2 required) on uppervp", uppervp->v_usecount));
483 * Save information about the lower layer.
484 * This needs to keep track of pathname
485 * and directory information which union_vn_create
488 if (lowervp != un->un_lowervp) {
489 union_newlower(un, lowervp);
490 if (cnp && (lowervp != NULLVP)) {
491 un->un_path = malloc(cnp->cn_namelen+1,
493 bcopy(cnp->cn_nameptr, un->un_path,
495 un->un_path[cnp->cn_namelen] = '\0';
497 } else if (lowervp) {
504 if (upperdvp != un->un_dirvp) {
507 un->un_dirvp = upperdvp;
508 } else if (upperdvp) {
518 * otherwise lock the vp list while we call getnewvnode
519 * since that can block.
521 hash = UNION_HASH(uppervp, lowervp);
523 if (union_list_lock(hash))
528 * Create new node rather then replace old node
531 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
534 * If an error occurs clear out vnodes.
546 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
549 (*vpp)->v_flag |= vflag;
551 (*vpp)->v_type = uppervp->v_type;
553 (*vpp)->v_type = lowervp->v_type;
556 bzero(un, sizeof(*un));
558 lockinit(&un->un_lock, 0, "unlock", VLKTIMEOUT, 0);
559 vn_lock(*vpp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
562 un->un_uppervp = uppervp;
563 un->un_uppersz = VNOVAL;
564 un->un_lowervp = lowervp;
565 un->un_lowersz = VNOVAL;
566 un->un_dirvp = upperdvp;
567 un->un_pvp = dvp; /* only parent dir in new allocation */
573 if (cnp && (lowervp != NULLVP)) {
574 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
575 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
576 un->un_path[cnp->cn_namelen] = '\0';
583 LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
584 un->un_flags |= UN_CACHED;
589 union_list_unlock(hash);
595 union_freevp(struct vnode *vp)
597 struct union_node *un = VTOUNION(vp);
599 if (un->un_flags & UN_CACHED) {
600 un->un_flags &= ~UN_CACHED;
601 LIST_REMOVE(un, un_cache);
604 if (un->un_pvp != NULLVP) {
608 if (un->un_uppervp != NULLVP) {
609 vrele(un->un_uppervp);
610 un->un_uppervp = NULL;
612 if (un->un_lowervp != NULLVP) {
613 vrele(un->un_lowervp);
614 un->un_lowervp = NULL;
616 if (un->un_dirvp != NULLVP) {
621 free(un->un_path, M_TEMP);
625 FREE(vp->v_data, M_TEMP);
632 * copyfile. copy the vnode (fvp) to the vnode (tvp)
633 * using a sequence of reads and writes. both (fvp)
634 * and (tvp) are locked on entry and exit.
636 * fvp and tvp are both exclusive locked on call, but their refcount's
637 * haven't been bumped at all.
640 union_copyfile(struct vnode *fvp, struct vnode *tvp, struct ucred *cred,
650 * allocate a buffer of size MAXBSIZE.
651 * loop doing reads and writes, keeping track
652 * of the current uio offset.
653 * give up at the first sign of trouble.
656 bzero(&uio, sizeof(uio));
659 uio.uio_segflg = UIO_SYSSPACE;
662 VOP_LEASE(fvp, td, cred, LEASE_READ);
663 VOP_LEASE(tvp, td, cred, LEASE_WRITE);
665 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
667 /* ugly loop follows... */
669 off_t offset = uio.uio_offset;
679 iov.iov_len = MAXBSIZE;
680 uio.uio_resid = iov.iov_len;
681 uio.uio_rw = UIO_READ;
683 if ((error = VOP_READ(fvp, &uio, 0, cred)) != 0)
687 * Get bytes read, handle read eof case and setup for
690 if ((count = MAXBSIZE - uio.uio_resid) == 0)
695 * Write until an error occurs or our buffer has been
696 * exhausted, then update the offset for the next read.
698 while (bufoffset < count) {
701 iov.iov_base = buf + bufoffset;
702 iov.iov_len = count - bufoffset;
703 uio.uio_offset = offset + bufoffset;
704 uio.uio_rw = UIO_WRITE;
705 uio.uio_resid = iov.iov_len;
707 if ((error = VOP_WRITE(tvp, &uio, 0, cred)) != 0)
709 bufoffset += (count - bufoffset) - uio.uio_resid;
711 uio.uio_offset = offset + bufoffset;
712 } while (error == 0);
720 * un's vnode is assumed to be locked on entry and remains locked on exit.
724 union_copyup(struct union_node *un, int docopy, struct ucred *cred,
728 struct vnode *lvp, *uvp;
731 * If the user does not have read permission, the vnode should not
732 * be copied to upper layer.
734 vn_lock(un->un_lowervp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
735 error = VOP_ACCESS(un->un_lowervp, VREAD, cred, td);
736 VOP_UNLOCK(un->un_lowervp, NULL, 0, td);
740 error = union_vn_create(&uvp, un, td);
744 lvp = un->un_lowervp;
746 KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
749 * XX - should not ignore errors
752 vn_lock(lvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
753 error = VOP_OPEN(lvp, FREAD, cred, td);
754 if (error == 0 && vn_canvmio(lvp) == TRUE)
755 error = vfs_object_create(lvp, td);
757 error = union_copyfile(lvp, uvp, cred, td);
758 VOP_UNLOCK(lvp, NULL, 0, td);
759 (void) VOP_CLOSE(lvp, FREAD, td);
762 UDEBUG(("union: copied up %s\n", un->un_path));
765 VOP_UNLOCK(uvp, NULL, 0, td);
766 union_newupper(un, uvp);
767 KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
768 union_vn_close(uvp, FWRITE, cred, td);
769 KASSERT(uvp->v_usecount > 0, ("copy: uvp refcount 0: %d", uvp->v_usecount));
771 * Subsequent IOs will go to the top layer, so
772 * call close on the lower vnode and open on the
773 * upper vnode to ensure that the filesystem keeps
774 * its references counts right. This doesn't do
775 * the right thing with (cred) and (FREAD) though.
776 * Ignoring error returns is not right, either.
781 for (i = 0; i < un->un_openl; i++) {
782 (void) VOP_CLOSE(lvp, FREAD, td);
783 (void) VOP_OPEN(uvp, FREAD, cred, td);
786 if (vn_canvmio(uvp) == TRUE)
787 error = vfs_object_create(uvp, td);
799 * dvp should be locked on entry and will be locked on return. No
800 * net change in the ref count will occur.
802 * If an error is returned, *vpp will be invalid, otherwise it
803 * will hold a locked, referenced vnode. If *vpp == dvp then
804 * remember that only one exclusive lock is held.
808 union_relookup(struct union_mount *um, struct vnode *dvp, struct vnode **vpp,
809 struct componentname *cnp, struct componentname *cn, char *path,
815 * A new componentname structure must be faked up because
816 * there is no way to know where the upper level cnp came
817 * from or what it is being used for. This must duplicate
818 * some of the work done by NDINIT, some of the work done
819 * by namei, some of the work done by lookup and some of
820 * the work done by VOP_LOOKUP when given a CREATE flag.
821 * Conclusion: Horrible.
823 cn->cn_namelen = pathlen;
824 cn->cn_pnbuf = zalloc(namei_zone);
825 bcopy(path, cn->cn_pnbuf, cn->cn_namelen);
826 cn->cn_pnbuf[cn->cn_namelen] = '\0';
828 cn->cn_nameiop = NAMEI_CREATE;
829 cn->cn_flags = (CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_HASBUF |
830 CNP_SAVENAME | CNP_ISLASTCN);
831 cn->cn_td = cnp->cn_td;
832 if (um->um_op == UNMNT_ABOVE)
833 cn->cn_cred = cnp->cn_cred;
835 cn->cn_cred = um->um_cred;
836 cn->cn_nameptr = cn->cn_pnbuf;
837 cn->cn_consume = cnp->cn_consume;
840 VOP_UNLOCK(dvp, NULL, 0, cnp->cn_td);
843 * Pass dvp unlocked and referenced on call to relookup().
845 * If an error occurs, dvp will be returned unlocked and dereferenced.
848 if ((error = relookup(dvp, vpp, cn)) != 0) {
849 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, cnp->cn_td);
854 * If no error occurs, dvp will be returned locked with the reference
855 * left as before, and vpp will be returned referenced and locked.
857 * We want to return with dvp as it was passed to us, so we get
858 * rid of our reference.
865 * Create a shadow directory in the upper layer.
866 * The new vnode is returned locked.
868 * (um) points to the union mount structure for access to the
869 * the mounting process's credentials.
870 * (dvp) is the directory in which to create the shadow directory,
871 * it is locked (but not ref'd) on entry and return.
872 * (cnp) is the componentname to be created.
873 * (vpp) is the returned newly created shadow directory, which
874 * is returned locked and ref'd
877 union_mkshadow(struct union_mount *um, struct vnode *dvp,
878 struct componentname *cnp, struct vnode **vpp)
882 struct thread *td = cnp->cn_td;
883 struct componentname cn;
885 error = union_relookup(um, dvp, vpp, cnp, &cn,
886 cnp->cn_nameptr, cnp->cn_namelen);
891 if (cn.cn_flags & CNP_HASBUF) {
892 zfree(namei_zone, cn.cn_pnbuf);
893 cn.cn_flags &= ~CNP_HASBUF;
904 * policy: when creating the shadow directory in the
905 * upper layer, create it owned by the user who did
906 * the mount, group from parent directory, and mode
907 * 777 modified by umask (ie mostly identical to the
908 * mkdir syscall). (jsp, kb)
913 va.va_mode = um->um_cmode;
915 /* VOP_LEASE: dvp is locked */
916 VOP_LEASE(dvp, td, cn.cn_cred, LEASE_WRITE);
918 error = VOP_MKDIR(dvp, NCPNULL, vpp, &cn, &va);
919 if (cn.cn_flags & CNP_HASBUF) {
920 zfree(namei_zone, cn.cn_pnbuf);
921 cn.cn_flags &= ~CNP_HASBUF;
928 * Create a whiteout entry in the upper layer.
930 * (um) points to the union mount structure for access to the
931 * the mounting process's credentials.
932 * (dvp) is the directory in which to create the whiteout.
933 * it is locked on entry and return.
934 * (cnp) is the componentname to be created.
937 union_mkwhiteout(struct union_mount *um, struct vnode *dvp,
938 struct componentname *cnp, char *path)
941 struct thread *td = cnp->cn_td;
943 struct componentname cn;
946 KKASSERT(td->td_proc);
947 cred = td->td_proc->p_ucred;
949 error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
954 if (cn.cn_flags & CNP_HASBUF) {
955 zfree(namei_zone, cn.cn_pnbuf);
956 cn.cn_flags &= ~CNP_HASBUF;
965 /* VOP_LEASE: dvp is locked */
966 VOP_LEASE(dvp, td, cred, LEASE_WRITE);
968 error = VOP_WHITEOUT(dvp, NCPNULL, &cn, NAMEI_CREATE);
969 if (cn.cn_flags & CNP_HASBUF) {
970 zfree(namei_zone, cn.cn_pnbuf);
971 cn.cn_flags &= ~CNP_HASBUF;
977 * union_vn_create: creates and opens a new shadow file
978 * on the upper union layer. this function is similar
979 * in spirit to calling vn_open but it avoids calling namei().
980 * the problem with calling namei is that a) it locks too many
981 * things, and b) it doesn't start at the "right" directory,
982 * whereas relookup is told where to start.
984 * On entry, the vnode associated with un is locked. It remains locked
987 * If no error occurs, *vpp contains a locked referenced vnode for your
988 * use. If an error occurs *vpp iis undefined.
991 union_vn_create(struct vnode **vpp, struct union_node *un, struct thread *td)
996 struct vattr *vap = &vat;
997 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
1000 struct componentname cn;
1002 KKASSERT(td->td_proc);
1003 cred = td->td_proc->p_ucred;
1004 cmode = UN_FILEMODE & ~td->td_proc->p_fd->fd_cmask;
1009 * Build a new componentname structure (for the same
1010 * reasons outlines in union_mkshadow).
1011 * The difference here is that the file is owned by
1012 * the current user, rather than by the person who
1013 * did the mount, since the current user needs to be
1014 * able to write the file (that's why it is being
1015 * copied in the first place).
1017 cn.cn_namelen = strlen(un->un_path);
1018 cn.cn_pnbuf = zalloc(namei_zone);
1019 bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
1020 cn.cn_nameiop = NAMEI_CREATE;
1021 cn.cn_flags = (CNP_LOCKPARENT | CNP_LOCKLEAF | CNP_HASBUF |
1022 CNP_SAVENAME | CNP_ISLASTCN);
1025 cn.cn_nameptr = cn.cn_pnbuf;
1029 * Pass dvp unlocked and referenced on call to relookup().
1031 * If an error occurs, dvp will be returned unlocked and dereferenced.
1034 error = relookup(un->un_dirvp, &vp, &cn);
1039 * If no error occurs, dvp will be returned locked with the reference
1040 * left as before, and vpp will be returned referenced and locked.
1044 if (cn.cn_flags & CNP_HASBUF) {
1045 zfree(namei_zone, cn.cn_pnbuf);
1046 cn.cn_flags &= ~CNP_HASBUF;
1048 if (vp == un->un_dirvp)
1056 * Good - there was no race to create the file
1057 * so go ahead and create it. The permissions
1058 * on the file will be 0666 modified by the
1059 * current user's umask. Access to the file, while
1060 * it is unioned, will require access to the top *and*
1061 * bottom files. Access when not unioned will simply
1062 * require access to the top-level file.
1063 * TODO: confirm choice of access permissions.
1066 vap->va_type = VREG;
1067 vap->va_mode = cmode;
1068 VOP_LEASE(un->un_dirvp, td, cred, LEASE_WRITE);
1069 error = VOP_CREATE(un->un_dirvp, NCPNULL, &vp, &cn, vap);
1070 if (cn.cn_flags & CNP_HASBUF) {
1071 zfree(namei_zone, cn.cn_pnbuf);
1072 cn.cn_flags &= ~CNP_HASBUF;
1078 error = VOP_OPEN(vp, fmode, cred, td);
1079 if (error == 0 && vn_canvmio(vp) == TRUE)
1080 error = vfs_object_create(vp, td);
1091 union_vn_close(struct vnode *vp, int fmode, struct ucred *cred,
1096 return (VOP_CLOSE(vp, fmode, td));
1102 * union_removed_upper:
1104 * called with union_node unlocked. XXX
1108 union_removed_upper(struct union_node *un)
1110 struct thread *td = curthread; /* XXX */
1114 * Do not set the uppervp to NULLVP. If lowervp is NULLVP,
1115 * union node will have neither uppervp nor lowervp. We remove
1116 * the union node from cache, so that it will not be referrenced.
1118 union_newupper(un, NULLVP);
1119 if (un->un_dircache != 0) {
1120 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1122 free(un->un_dircache, M_TEMP);
1123 un->un_dircache = 0;
1126 if (un->un_flags & UN_CACHED) {
1127 un->un_flags &= ~UN_CACHED;
1128 LIST_REMOVE(un, un_cache);
1135 * determine whether a whiteout is needed
1136 * during a remove/rmdir operation.
1139 union_dowhiteout(struct union_node *un, struct ucred *cred, struct thread *td)
1143 if (un->un_lowervp != NULLVP)
1146 if (VOP_GETATTR(un->un_uppervp, &va, td) == 0 &&
1147 (va.va_flags & OPAQUE))
1154 union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp)
1156 struct union_node *un;
1158 if (vp->v_op != union_vnodeop_p) {
1163 panic("union: dircache table too small");
1172 if (un->un_uppervp != NULLVP)
1173 union_dircache_r(un->un_uppervp, vppp, cntp);
1174 if (un->un_lowervp != NULLVP)
1175 union_dircache_r(un->un_lowervp, vppp, cntp);
1179 union_dircache(struct vnode *vp, struct thread *td)
1184 struct vnode **dircache;
1185 struct union_node *un;
1188 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1189 dircache = VTOUNION(vp)->un_dircache;
1193 if (dircache == NULL) {
1195 union_dircache_r(vp, 0, &cnt);
1197 dircache = malloc(cnt * sizeof(struct vnode *),
1200 union_dircache_r(vp, &vpp, &cnt);
1206 if (*vpp++ == VTOUNION(vp)->un_uppervp)
1208 } while (*vpp != NULLVP);
1214 /*vn_lock(*vpp, NULL, LK_EXCLUSIVE | LK_RETRY, td);*/
1215 UDEBUG(("ALLOCVP-3 %p ref %d\n", *vpp, (*vpp ? (*vpp)->v_usecount : -99)));
1217 error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, NULL, *vpp, NULLVP, 0);
1218 UDEBUG(("ALLOCVP-3B %p ref %d\n", nvp, (*vpp ? (*vpp)->v_usecount : -99)));
1222 VTOUNION(vp)->un_dircache = 0;
1224 un->un_dircache = dircache;
1227 VOP_UNLOCK(vp, NULL, 0, td);
1232 * Guarentee coherency with the VM cache by invalidating any clean VM pages
1233 * associated with this write and updating any dirty VM pages. Since our
1234 * vnode is locked, other processes will not be able to read the pages in
1235 * again until after our write completes.
1237 * We also have to be coherent with reads, by flushing any pending dirty
1238 * pages prior to issuing the read.
1240 * XXX this is somewhat of a hack at the moment. To support this properly
1241 * we would have to be able to run VOP_READ and VOP_WRITE through the VM
1242 * cache. Then we wouldn't need to worry about coherency.
1246 union_vm_coherency(struct vnode *vp, struct uio *uio, int cleanfls)
1253 if ((object = vp->v_object) == NULL)
1256 pgoff = uio->uio_offset & PAGE_MASK;
1257 pstart = uio->uio_offset / PAGE_SIZE;
1258 pend = pstart + (uio->uio_resid + pgoff + PAGE_MASK) / PAGE_SIZE;
1260 vm_object_page_clean(object, pstart, pend, OBJPC_SYNC);
1262 vm_object_page_remove(object, pstart, pend, TRUE);
1266 * Module glue to remove #ifdef UNION from vfs_syscalls.c
1269 union_dircheck(struct thread *td, struct vnode **vp, struct file *fp)
1273 if ((*vp)->v_op == union_vnodeop_p) {
1276 lvp = union_dircache(*vp, td);
1277 if (lvp != NULLVP) {
1281 * If the directory is opaque,
1282 * then don't show lower entries
1284 error = VOP_GETATTR(*vp, &va, td);
1285 if (va.va_flags & OPAQUE) {
1291 if (lvp != NULLVP) {
1292 error = VOP_OPEN(lvp, FREAD, fp->f_cred, td);
1293 if (error == 0 && vn_canvmio(lvp) == TRUE)
1294 error = vfs_object_create(lvp, td);
1299 VOP_UNLOCK(lvp, NULL, 0, td);
1300 fp->f_data = (caddr_t) lvp;
1302 error = vn_close(*vp, FREAD, td);
1306 return -1; /* goto unionread */
1313 union_modevent(module_t mod, int type, void *data)
1317 union_dircheckp = union_dircheck;
1320 union_dircheckp = NULL;
1328 static moduledata_t union_mod = {
1334 DECLARE_MODULE(union_dircheck, union_mod, SI_SUB_VFS, SI_ORDER_ANY);