]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/union/union_subr.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1994 Jan-Simon Pendry
29 * The Regents of the University of California. All rights reserved.
31 * This code is derived from software contributed to Berkeley by
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
65 #include <sys/param.h>
66 #include <sys/systm.h>
69 #include <sys/kernel.h>
70 #include <sys/vnode.h>
71 #include <sys/namei.h>
72 #include <sys/malloc.h>
74 #include <sys/filedesc.h>
75 #include <sys/queue.h>
76 #include <sys/mount.h>
79 #include <miscfs/union/union.h>
85 /* must be power of two, otherwise change UNION_HASH() */
88 /* unsigned int ... */
89 #define UNION_HASH(u, l) \
90 (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
92 static LIST_HEAD(unhead
, union_node
) unhead
[NHASH
];
93 static int unvplock
[NHASH
];
100 for (i
= 0; i
< NHASH
; i
++)
101 LIST_INIT(&unhead
[i
]);
102 bzero((caddr_t
) unvplock
, sizeof(unvplock
));
110 if (unvplock
[ix
] & UN_LOCKED
) {
111 unvplock
[ix
] |= UN_WANT
;
112 sleep((caddr_t
) &unvplock
[ix
], PINOD
);
116 unvplock
[ix
] |= UN_LOCKED
;
122 union_list_unlock(ix
)
126 unvplock
[ix
] &= ~UN_LOCKED
;
128 if (unvplock
[ix
] & UN_WANT
) {
129 unvplock
[ix
] &= ~UN_WANT
;
130 wakeup((caddr_t
) &unvplock
[ix
]);
135 union_updatevp(un
, uppervp
, lowervp
)
136 struct union_node
*un
;
137 struct vnode
*uppervp
;
138 struct vnode
*lowervp
;
140 int ohash
= UNION_HASH(un
->un_uppervp
, un
->un_lowervp
);
141 int nhash
= UNION_HASH(uppervp
, lowervp
);
142 int docache
= (lowervp
!= NULLVP
|| uppervp
!= NULLVP
);
143 int lhash
, hhash
, uhash
;
146 * Ensure locking is ordered from lower to higher
147 * to avoid deadlocks.
158 while (union_list_lock(lhash
))
161 while (union_list_lock(uhash
))
164 if (ohash
!= nhash
|| !docache
) {
165 if (un
->un_flags
& UN_CACHED
) {
166 un
->un_flags
&= ~UN_CACHED
;
167 LIST_REMOVE(un
, un_cache
);
172 union_list_unlock(ohash
);
174 if (un
->un_lowervp
!= lowervp
) {
175 if (un
->un_lowervp
) {
176 vrele(un
->un_lowervp
);
178 _FREE(un
->un_path
, M_TEMP
);
183 un
->un_dirvp
= NULLVP
;
186 un
->un_lowervp
= lowervp
;
187 un
->un_lowersz
= VNOVAL
;
190 if (un
->un_uppervp
!= uppervp
) {
192 vrele(un
->un_uppervp
);
194 un
->un_uppervp
= uppervp
;
195 un
->un_uppersz
= VNOVAL
;
198 if (docache
&& (ohash
!= nhash
)) {
199 LIST_INSERT_HEAD(&unhead
[nhash
], un
, un_cache
);
200 un
->un_flags
|= UN_CACHED
;
203 union_list_unlock(nhash
);
207 union_newlower(un
, lowervp
)
208 struct union_node
*un
;
209 struct vnode
*lowervp
;
212 union_updatevp(un
, un
->un_uppervp
, lowervp
);
216 union_newupper(un
, uppervp
)
217 struct union_node
*un
;
218 struct vnode
*uppervp
;
221 union_updatevp(un
, uppervp
, un
->un_lowervp
);
225 * Keep track of size changes in the underlying vnodes.
226 * If the size changes, then callback to the vm layer
227 * giving priority to the upper layer size.
230 union_newsize(vp
, uppersz
, lowersz
)
232 off_t uppersz
, lowersz
;
234 struct union_node
*un
;
237 /* only interested in regular files */
238 if (vp
->v_type
!= VREG
)
244 if ((uppersz
!= VNOVAL
) && (un
->un_uppersz
!= uppersz
)) {
245 un
->un_uppersz
= uppersz
;
250 if ((lowersz
!= VNOVAL
) && (un
->un_lowersz
!= lowersz
)) {
251 un
->un_lowersz
= lowersz
;
257 #ifdef UNION_DIAGNOSTIC
258 printf("union: %s size now %ld\n",
259 uppersz
!= VNOVAL
? "upper" : "lower", (long) sz
);
262 ubc_setsize(vp
, sz
); /* XXX check error */
267 * allocate a union_node/vnode pair. the vnode is
268 * referenced and locked. the new vnode is returned
269 * via (vpp). (mp) is the mountpoint of the union filesystem,
270 * (dvp) is the parent directory where the upper layer object
271 * should exist (but doesn't) and (cnp) is the componentname
272 * information which is partially copied to allow the upper
273 * layer object to be created at a later time. (uppervp)
274 * and (lowervp) reference the upper and lower layer objects
275 * being mapped. either, but not both, can be nil.
276 * if supplied, (uppervp) is locked.
277 * the reference is either maintained in the new union_node
278 * object which is allocated, or they are vrele'd.
280 * all union_nodes are maintained on a singly-linked
281 * list. new nodes are only allocated when they cannot
282 * be found on this list. entries on the list are
283 * removed when the vfs reclaim entry is called.
285 * a single lock is kept for the entire list. this is
286 * needed because the getnewvnode() function can block
287 * waiting for a vnode to become free, in which case there
288 * may be more than one process trying to get the same
289 * vnode. this lock is only taken if we are going to
290 * call getnewvnode, since the kernel itself is single-threaded.
292 * if an entry is found on the list, then call vget() to
293 * take a reference. this is done because there may be
294 * zero references to it and so it needs to removed from
295 * the vnode free list.
298 union_allocvp(vpp
, mp
, undvp
, dvp
, cnp
, uppervp
, lowervp
, docache
)
301 struct vnode
*undvp
; /* parent union vnode */
302 struct vnode
*dvp
; /* may be null */
303 struct componentname
*cnp
; /* may be null */
304 struct vnode
*uppervp
; /* may be null */
305 struct vnode
*lowervp
; /* may be null */
309 struct union_node
*un
;
310 struct union_node
**pp
;
311 struct vnode
*xlowervp
= NULLVP
;
312 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
316 struct union_node
*unp
;
318 if (uppervp
== NULLVP
&& lowervp
== NULLVP
)
319 panic("union: unidentifiable allocation");
321 if (uppervp
&& lowervp
&& (uppervp
->v_type
!= lowervp
->v_type
)) {
326 /* detect the root vnode (and aliases) */
328 if ((uppervp
== um
->um_uppervp
) &&
329 ((lowervp
== NULLVP
) || lowervp
== um
->um_lowervp
)) {
330 if (lowervp
== NULLVP
) {
331 lowervp
= um
->um_lowervp
;
332 if (lowervp
!= NULLVP
)
341 } else for (try = 0; try < 3; try++) {
344 if (lowervp
== NULLVP
)
346 hash
= UNION_HASH(uppervp
, lowervp
);
350 if (uppervp
== NULLVP
)
352 hash
= UNION_HASH(uppervp
, NULLVP
);
356 if (lowervp
== NULLVP
)
358 hash
= UNION_HASH(NULLVP
, lowervp
);
362 while (union_list_lock(hash
))
365 for (un
= unhead
[hash
].lh_first
; un
!= 0;
366 un
= un
->un_cache
.le_next
) {
367 if ((un
->un_lowervp
== lowervp
||
368 un
->un_lowervp
== NULLVP
) &&
369 (un
->un_uppervp
== uppervp
||
370 un
->un_uppervp
== NULLVP
) &&
371 (UNIONTOV(un
)->v_mount
== mp
)) {
372 if (vget(UNIONTOV(un
), 0,
373 cnp
? cnp
->cn_proc
: NULL
)) {
374 union_list_unlock(hash
);
381 union_list_unlock(hash
);
389 * Obtain a lock on the union_node.
390 * uppervp is locked, though un->un_uppervp
391 * may not be. this doesn't break the locking
392 * hierarchy since in the case that un->un_uppervp
393 * is not yet locked it will be vrele'd and replaced
397 if ((dvp
!= NULLVP
) && (uppervp
== dvp
)) {
399 * Access ``.'', so (un) will already
400 * be locked. Since this process has
401 * the lock on (uppervp) no other
402 * process can hold the lock on (un).
405 if ((un
->un_flags
& UN_LOCKED
) == 0)
406 panic("union: . not locked");
407 else if (current_proc() && un
->un_pid
!= current_proc()->p_pid
&&
408 un
->un_pid
> -1 && current_proc()->p_pid
> -1)
409 panic("union: allocvp not lock owner");
412 if (un
->un_flags
& UN_LOCKED
) {
414 un
->un_flags
|= UN_WANT
;
415 sleep((caddr_t
) &un
->un_flags
, PINOD
);
418 un
->un_flags
|= UN_LOCKED
;
422 un
->un_pid
= current_proc()->p_pid
;
429 * At this point, the union_node is locked,
430 * un->un_uppervp may not be locked, and uppervp
435 * Save information about the upper layer.
437 if (uppervp
!= un
->un_uppervp
) {
438 union_newupper(un
, uppervp
);
439 } else if (uppervp
) {
443 if (un
->un_uppervp
) {
444 un
->un_flags
|= UN_ULOCK
;
445 un
->un_flags
&= ~UN_KLOCK
;
449 * Save information about the lower layer.
450 * This needs to keep track of pathname
451 * and directory information which union_vn_create
454 if (lowervp
!= un
->un_lowervp
) {
455 union_newlower(un
, lowervp
);
456 if (cnp
&& (lowervp
!= NULLVP
)) {
457 un
->un_hash
= cnp
->cn_hash
;
458 MALLOC(un
->un_path
, caddr_t
, cnp
->cn_namelen
+1,
460 bcopy(cnp
->cn_nameptr
, un
->un_path
,
462 un
->un_path
[cnp
->cn_namelen
] = '\0';
466 } else if (lowervp
) {
475 * otherwise lock the vp list while we call getnewvnode
476 * since that can block.
478 hash
= UNION_HASH(uppervp
, lowervp
);
480 if (union_list_lock(hash
))
484 MALLOC(unp
, void *, sizeof(struct union_node
), M_TEMP
, M_WAITOK
);
485 error
= getnewvnode(VT_UNION
, mp
, union_vnodeop_p
, vpp
);
500 (*vpp
)->v_data
= unp
;
501 (*vpp
)->v_flag
|= vflag
;
503 (*vpp
)->v_type
= uppervp
->v_type
;
505 (*vpp
)->v_type
= lowervp
->v_type
;
507 if ((*vpp
)->v_type
== VREG
)
512 un
->un_uppervp
= uppervp
;
513 un
->un_uppersz
= VNOVAL
;
514 un
->un_lowervp
= lowervp
;
515 un
->un_lowersz
= VNOVAL
;
521 un
->un_flags
= UN_LOCKED
;
523 un
->un_flags
|= UN_ULOCK
;
526 un
->un_pid
= current_proc()->p_pid
;
530 if (cnp
&& (lowervp
!= NULLVP
)) {
531 un
->un_hash
= cnp
->cn_hash
;
532 un
->un_path
= _MALLOC(cnp
->cn_namelen
+1, M_TEMP
, M_WAITOK
);
533 bcopy(cnp
->cn_nameptr
, un
->un_path
, cnp
->cn_namelen
);
534 un
->un_path
[cnp
->cn_namelen
] = '\0';
544 LIST_INSERT_HEAD(&unhead
[hash
], un
, un_cache
);
545 un
->un_flags
|= UN_CACHED
;
553 union_list_unlock(hash
);
562 struct union_node
*un
= VTOUNION(vp
);
564 if (un
->un_flags
& UN_CACHED
) {
565 un
->un_flags
&= ~UN_CACHED
;
566 LIST_REMOVE(un
, un_cache
);
569 if (un
->un_pvp
!= NULLVP
)
571 if (un
->un_uppervp
!= NULLVP
)
572 vrele(un
->un_uppervp
);
573 if (un
->un_lowervp
!= NULLVP
)
574 vrele(un
->un_lowervp
);
575 if (un
->un_dirvp
!= NULLVP
)
578 _FREE(un
->un_path
, M_TEMP
);
580 FREE(vp
->v_data
, M_TEMP
);
587 * copyfile. copy the vnode (fvp) to the vnode (tvp)
588 * using a sequence of reads and writes. both (fvp)
589 * and (tvp) are locked on entry and exit.
592 union_copyfile(fvp
, tvp
, cred
, p
)
605 * allocate a buffer of size MAXPHYSIO.
606 * loop doing reads and writes, keeping track
607 * of the current uio offset.
608 * give up at the first sign of trouble.
612 uio
.uio_segflg
= UIO_SYSSPACE
;
615 VOP_UNLOCK(fvp
, 0, p
); /* XXX */
616 VOP_LEASE(fvp
, p
, cred
, LEASE_READ
);
617 vn_lock(fvp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
618 VOP_UNLOCK(tvp
, 0, p
); /* XXX */
619 VOP_LEASE(tvp
, p
, cred
, LEASE_WRITE
);
620 vn_lock(tvp
, LK_EXCLUSIVE
| LK_RETRY
, p
); /* XXX */
622 buf
= _MALLOC(MAXPHYSIO
, M_TEMP
, M_WAITOK
);
624 /* ugly loop follows... */
626 off_t offset
= uio
.uio_offset
;
631 iov
.iov_len
= MAXPHYSIO
;
632 uio
.uio_resid
= iov
.iov_len
;
633 uio
.uio_rw
= UIO_READ
;
634 error
= VOP_READ(fvp
, &uio
, 0, cred
);
640 iov
.iov_len
= MAXPHYSIO
- uio
.uio_resid
;
641 uio
.uio_offset
= offset
;
642 uio
.uio_rw
= UIO_WRITE
;
643 uio
.uio_resid
= iov
.iov_len
;
645 if (uio
.uio_resid
== 0)
649 error
= VOP_WRITE(tvp
, &uio
, 0, cred
);
650 } while ((uio
.uio_resid
> 0) && (error
== 0));
653 } while (error
== 0);
660 * (un) is assumed to be locked on entry and remains
664 union_copyup(un
, docopy
, cred
, p
)
665 struct union_node
*un
;
671 struct vnode
*lvp
, *uvp
;
673 error
= union_vn_create(&uvp
, un
, p
);
677 /* at this point, uppervp is locked */
678 union_newupper(un
, uvp
);
679 un
->un_flags
|= UN_ULOCK
;
681 lvp
= un
->un_lowervp
;
685 * XX - should not ignore errors
688 vn_lock(lvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
689 error
= VOP_OPEN(lvp
, FREAD
, cred
, p
);
691 error
= union_copyfile(lvp
, uvp
, cred
, p
);
692 VOP_UNLOCK(lvp
, 0, p
);
693 (void) VOP_CLOSE(lvp
, FREAD
, cred
, p
);
695 #ifdef UNION_DIAGNOSTIC
697 uprintf("union: copied up %s\n", un
->un_path
);
701 un
->un_flags
&= ~UN_ULOCK
;
702 VOP_UNLOCK(uvp
, 0, p
);
703 union_vn_close(uvp
, FWRITE
, cred
, p
);
704 vn_lock(uvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
705 un
->un_flags
|= UN_ULOCK
;
708 * Subsequent IOs will go to the top layer, so
709 * call close on the lower vnode and open on the
710 * upper vnode to ensure that the filesystem keeps
711 * its references counts right. This doesn't do
712 * the right thing with (cred) and (FREAD) though.
713 * Ignoring error returns is not right, either.
718 for (i
= 0; i
< un
->un_openl
; i
++) {
719 (void) VOP_CLOSE(lvp
, FREAD
, cred
, p
);
720 (void) VOP_OPEN(uvp
, FREAD
, cred
, p
);
730 union_relookup(um
, dvp
, vpp
, cnp
, cn
, path
, pathlen
)
731 struct union_mount
*um
;
734 struct componentname
*cnp
;
735 struct componentname
*cn
;
742 * A new componentname structure must be faked up because
743 * there is no way to know where the upper level cnp came
744 * from or what it is being used for. This must duplicate
745 * some of the work done by NDINIT, some of the work done
746 * by namei, some of the work done by lookup and some of
747 * the work done by VOP_LOOKUP when given a CREATE flag.
748 * Conclusion: Horrible.
750 * The pathname buffer will be FREEed by VOP_MKDIR.
752 cn
->cn_namelen
= pathlen
;
753 cn
->cn_pnbuf
= _MALLOC_ZONE(cn
->cn_namelen
+1, M_NAMEI
, M_WAITOK
);
754 cn
->cn_pnlen
= cn
->cn_namelen
+1;
755 bcopy(path
, cn
->cn_pnbuf
, cn
->cn_namelen
);
756 cn
->cn_pnbuf
[cn
->cn_namelen
] = '\0';
758 cn
->cn_nameiop
= CREATE
;
759 cn
->cn_flags
= (LOCKPARENT
|HASBUF
|SAVENAME
|SAVESTART
|ISLASTCN
);
760 cn
->cn_proc
= cnp
->cn_proc
;
761 if (um
->um_op
== UNMNT_ABOVE
)
762 cn
->cn_cred
= cnp
->cn_cred
;
764 cn
->cn_cred
= um
->um_cred
;
765 cn
->cn_nameptr
= cn
->cn_pnbuf
;
766 cn
->cn_hash
= cnp
->cn_hash
;
767 cn
->cn_consume
= cnp
->cn_consume
;
770 error
= relookup(dvp
, vpp
, cn
);
778 * Create a shadow directory in the upper layer.
779 * The new vnode is returned locked.
781 * (um) points to the union mount structure for access to the
782 * the mounting process's credentials.
783 * (dvp) is the directory in which to create the shadow directory.
784 * it is unlocked on entry and exit.
785 * (cnp) is the componentname to be created.
786 * (vpp) is the returned newly created shadow directory, which
787 * is returned locked.
790 union_mkshadow(um
, dvp
, cnp
, vpp
)
791 struct union_mount
*um
;
793 struct componentname
*cnp
;
798 struct proc
*p
= cnp
->cn_proc
;
799 struct componentname cn
;
801 error
= union_relookup(um
, dvp
, vpp
, cnp
, &cn
,
802 cnp
->cn_nameptr
, cnp
->cn_namelen
);
807 VOP_ABORTOP(dvp
, &cn
);
808 VOP_UNLOCK(dvp
, 0, p
);
815 * policy: when creating the shadow directory in the
816 * upper layer, create it owned by the user who did
817 * the mount, group from parent directory, and mode
818 * 777 modified by umask (ie mostly identical to the
819 * mkdir syscall). (jsp, kb)
824 va
.va_mode
= um
->um_cmode
;
826 /* VOP_LEASE: dvp is locked */
827 VOP_LEASE(dvp
, p
, cn
.cn_cred
, LEASE_WRITE
);
829 error
= VOP_MKDIR(dvp
, vpp
, &cn
, &va
);
834 * Create a whiteout entry in the upper layer.
836 * (um) points to the union mount structure for access to the
837 * the mounting process's credentials.
838 * (dvp) is the directory in which to create the whiteout.
839 * it is locked on entry and exit.
840 * (cnp) is the componentname to be created.
843 union_mkwhiteout(um
, dvp
, cnp
, path
)
844 struct union_mount
*um
;
846 struct componentname
*cnp
;
851 struct proc
*p
= cnp
->cn_proc
;
853 struct componentname cn
;
855 VOP_UNLOCK(dvp
, 0, p
);
856 error
= union_relookup(um
, dvp
, &wvp
, cnp
, &cn
, path
, strlen(path
));
858 vn_lock(dvp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
863 VOP_ABORTOP(dvp
, &cn
);
869 /* VOP_LEASE: dvp is locked */
870 VOP_LEASE(dvp
, p
, p
->p_ucred
, LEASE_WRITE
);
872 error
= VOP_WHITEOUT(dvp
, &cn
, CREATE
);
874 VOP_ABORTOP(dvp
, &cn
);
882 * union_vn_create: creates and opens a new shadow file
883 * on the upper union layer. this function is similar
884 * in spirit to calling vn_open but it avoids calling namei().
885 * the problem with calling namei is that a) it locks too many
886 * things, and b) it doesn't start at the "right" directory,
887 * whereas relookup is told where to start.
890 union_vn_create(vpp
, un
, p
)
892 struct union_node
*un
;
896 struct ucred
*cred
= p
->p_ucred
;
898 struct vattr
*vap
= &vat
;
899 int fmode
= FFLAGS(O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
);
901 int cmode
= UN_FILEMODE
& ~p
->p_fd
->fd_cmask
;
903 struct componentname cn
;
908 * Build a new componentname structure (for the same
909 * reasons outlines in union_mkshadow).
910 * The difference here is that the file is owned by
911 * the current user, rather than by the person who
912 * did the mount, since the current user needs to be
913 * able to write the file (that's why it is being
914 * copied in the first place).
916 cn
.cn_namelen
= strlen(un
->un_path
);
917 cn
.cn_pnbuf
= (caddr_t
) _MALLOC_ZONE(cn
.cn_namelen
+1,
919 cn
.cn_pnlen
= cn
.cn_namelen
+1;
920 bcopy(un
->un_path
, cn
.cn_pnbuf
, cn
.cn_namelen
+1);
921 cn
.cn_nameiop
= CREATE
;
922 cn
.cn_flags
= (LOCKPARENT
|HASBUF
|SAVENAME
|SAVESTART
|ISLASTCN
);
924 cn
.cn_cred
= p
->p_ucred
;
925 cn
.cn_nameptr
= cn
.cn_pnbuf
;
926 cn
.cn_hash
= un
->un_hash
;
930 if (error
= relookup(un
->un_dirvp
, &vp
, &cn
))
935 VOP_ABORTOP(un
->un_dirvp
, &cn
);
936 if (un
->un_dirvp
== vp
)
945 * Good - there was no race to create the file
946 * so go ahead and create it. The permissions
947 * on the file will be 0666 modified by the
948 * current user's umask. Access to the file, while
949 * it is unioned, will require access to the top *and*
950 * bottom files. Access when not unioned will simply
951 * require access to the top-level file.
952 * TODO: confirm choice of access permissions.
956 vap
->va_mode
= cmode
;
957 VOP_LEASE(un
->un_dirvp
, p
, cred
, LEASE_WRITE
);
958 if (error
= VOP_CREATE(un
->un_dirvp
, &vp
, &cn
, vap
))
961 if (error
= VOP_OPEN(vp
, fmode
, cred
, p
)) {
966 if (++vp
->v_writecount
<= 0)
967 panic("union: v_writecount");
973 union_vn_close(vp
, fmode
, cred
, p
)
982 return (VOP_CLOSE(vp
, fmode
, cred
, p
));
986 union_removed_upper(un
)
987 struct union_node
*un
;
989 struct proc
*p
= current_proc(); /* XXX */
991 union_newupper(un
, NULLVP
);
992 if (un
->un_flags
& UN_CACHED
) {
993 un
->un_flags
&= ~UN_CACHED
;
994 LIST_REMOVE(un
, un_cache
);
997 if (un
->un_flags
& UN_ULOCK
) {
998 un
->un_flags
&= ~UN_ULOCK
;
999 VOP_UNLOCK(un
->un_uppervp
, 0, p
);
1008 struct union_node
*un
= VTOUNION(vp
);
1010 if ((un
->un_lowervp
!= NULLVP
) &&
1011 (vp
->v_type
== un
->un_lowervp
->v_type
)) {
1012 if (vget(un
->un_lowervp
, 0, current_proc()) == 0)
1013 return (un
->un_lowervp
);
1021 * determine whether a whiteout is needed
1022 * during a remove/rmdir operation.
1025 union_dowhiteout(un
, cred
, p
)
1026 struct union_node
*un
;
1032 if (un
->un_lowervp
!= NULLVP
)
1035 if (VOP_GETATTR(un
->un_uppervp
, &va
, cred
, p
) == 0 &&
1036 (va
.va_flags
& OPAQUE
))
1043 union_dircache_r(vp
, vppp
, cntp
)
1045 struct vnode
***vppp
;
1048 struct union_node
*un
;
1050 if (vp
->v_op
!= union_vnodeop_p
) {
1055 panic("union: dircache table too small");
1064 if (un
->un_uppervp
!= NULLVP
)
1065 union_dircache_r(un
->un_uppervp
, vppp
, cntp
);
1066 if (un
->un_lowervp
!= NULLVP
)
1067 union_dircache_r(un
->un_lowervp
, vppp
, cntp
);
1071 union_dircache(vp
, p
)
1078 struct vnode
**dircache
;
1079 struct union_node
*un
;
1082 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
1083 dircache
= VTOUNION(vp
)->un_dircache
;
1087 if (dircache
== 0) {
1089 union_dircache_r(vp
, 0, &cnt
);
1091 dircache
= (struct vnode
**)
1092 _MALLOC(cnt
* sizeof(struct vnode
*),
1095 union_dircache_r(vp
, &vpp
, &cnt
);
1101 if (*vpp
++ == VTOUNION(vp
)->un_uppervp
)
1103 } while (*vpp
!= NULLVP
);
1109 vn_lock(*vpp
, LK_EXCLUSIVE
| LK_RETRY
, p
);
1111 error
= union_allocvp(&nvp
, vp
->v_mount
, NULLVP
, NULLVP
, 0, *vpp
, NULLVP
, 0);
1115 VTOUNION(vp
)->un_dircache
= 0;
1117 un
->un_dircache
= dircache
;
1120 VOP_UNLOCK(vp
, 0, p
);