]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/union/union_subr.c
1e3120b0ed47ce7b9ceefd2a4567e42fde1327af
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1994 Jan-Simon Pendry
27 * The Regents of the University of California. All rights reserved.
29 * This code is derived from software contributed to Berkeley by
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/proc_internal.h>
66 #include <sys/kauth.h>
68 #include <sys/kernel.h>
69 #include <sys/vnode_internal.h>
70 #include <sys/namei.h>
71 #include <sys/malloc.h>
73 #include <sys/filedesc.h>
74 #include <sys/queue.h>
75 #include <sys/mount_internal.h>
78 #include <sys/uio_internal.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
);
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 vnode_put(un
->un_lowervp
);
178 _FREE(un
->un_path
, M_TEMP
);
182 vnode_put(un
->un_dirvp
);
183 un
->un_dirvp
= NULLVP
;
186 un
->un_lowervp
= lowervp
;
187 un
->un_lowersz
= VNOVAL
;
190 if (un
->un_uppervp
!= uppervp
) {
192 vnode_put(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
);
266 * allocate a union_node/vnode pair. the vnode is
267 * referenced and locked. the new vnode is returned
268 * via (vpp). (mp) is the mountpoint of the union filesystem,
269 * (dvp) is the parent directory where the upper layer object
270 * should exist (but doesn't) and (cnp) is the componentname
271 * information which is partially copied to allow the upper
272 * layer object to be created at a later time. (uppervp)
273 * and (lowervp) reference the upper and lower layer objects
274 * being mapped. either, but not both, can be nil.
275 * if supplied, (uppervp) is locked.
276 * the reference is either maintained in the new union_node
277 * object which is allocated, or they are vnode_put'd.
279 * all union_nodes are maintained on a singly-linked
280 * list. new nodes are only allocated when they cannot
281 * be found on this list. entries on the list are
282 * removed when the vfs reclaim entry is called.
284 * a single lock is kept for the entire list. this is
285 * needed because the getnewvnode() function can block
286 * waiting for a vnode to become free, in which case there
287 * may be more than one process trying to get the same
288 * vnode. this lock is only taken if we are going to
289 * call getnewvnode, since the kernel itself is single-threaded.
291 * if an entry is found on the list, then call vnode_get() to
292 * take a reference. this is done because there may be
293 * zero references to it and so it needs to removed from
294 * the vnode free list.
297 union_allocvp(vpp
, mp
, undvp
, dvp
, cnp
, uppervp
, lowervp
, docache
)
300 struct vnode
*undvp
; /* parent union vnode */
301 struct vnode
*dvp
; /* may be null */
302 struct componentname
*cnp
; /* may be null */
303 struct vnode
*uppervp
; /* may be null */
304 struct vnode
*lowervp
; /* may be null */
308 struct union_node
*un
;
309 struct union_node
**pp
;
310 struct vnode
*xlowervp
= NULLVP
;
311 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
315 struct union_node
*unp
;
316 struct vnode_fsparam vfsp
;
319 if (uppervp
== NULLVP
&& lowervp
== NULLVP
)
320 panic("union: unidentifiable allocation");
322 if (uppervp
&& lowervp
&& (uppervp
->v_type
!= lowervp
->v_type
)) {
327 /* detect the root vnode (and aliases) */
329 if ((uppervp
== um
->um_uppervp
) &&
330 ((lowervp
== NULLVP
) || lowervp
== um
->um_lowervp
)) {
331 if (lowervp
== NULLVP
) {
332 lowervp
= um
->um_lowervp
;
333 if (lowervp
!= NULLVP
)
342 } else for (try = 0; try < 3; try++) {
345 if (lowervp
== NULLVP
)
347 hash
= UNION_HASH(uppervp
, lowervp
);
351 if (uppervp
== NULLVP
)
353 hash
= UNION_HASH(uppervp
, NULLVP
);
357 if (lowervp
== NULLVP
)
359 hash
= UNION_HASH(NULLVP
, lowervp
);
363 while (union_list_lock(hash
))
366 for (un
= unhead
[hash
].lh_first
; un
!= 0;
367 un
= un
->un_cache
.le_next
) {
368 if ((un
->un_lowervp
== lowervp
||
369 un
->un_lowervp
== NULLVP
) &&
370 (un
->un_uppervp
== uppervp
||
371 un
->un_uppervp
== NULLVP
) &&
372 (UNIONTOV(un
)->v_mount
== mp
)) {
373 if (vnode_get(UNIONTOV(un
))) {
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 vnode_put'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
) {
413 vnode_put(UNIONTOV(un
));
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
);
487 vtype
= uppervp
->v_type
;
489 vtype
= lowervp
->v_type
;
490 //bzero(&vfsp, sizeof(struct vnode_fsparam));
492 vfsp
.vnfs_vtype
= vtype
;
493 vfsp
.vnfs_str
= "unionfs";
495 vfsp
.vnfs_fsnode
= unp
;
497 vfsp
.vnfs_vops
= union_vnodeop_p
;
499 vfsp
.vnfs_filesize
= 0;
500 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
501 vfsp
.vnfs_marksystem
= 0;
502 vfsp
.vnfs_markroot
= markroot
;
504 error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, vpp
);
516 (*vpp
)->v_tag
= VT_UNION
;
519 un
->un_uppervp
= uppervp
;
520 un
->un_uppersz
= VNOVAL
;
521 un
->un_lowervp
= lowervp
;
522 un
->un_lowersz
= VNOVAL
;
528 un
->un_flags
= UN_LOCKED
;
530 un
->un_flags
|= UN_ULOCK
;
533 un
->un_pid
= current_proc()->p_pid
;
537 if (cnp
&& (lowervp
!= NULLVP
)) {
538 un
->un_hash
= cnp
->cn_hash
;
539 un
->un_path
= _MALLOC(cnp
->cn_namelen
+1, M_TEMP
, M_WAITOK
);
540 bcopy(cnp
->cn_nameptr
, un
->un_path
, cnp
->cn_namelen
);
541 un
->un_path
[cnp
->cn_namelen
] = '\0';
551 LIST_INSERT_HEAD(&unhead
[hash
], un
, un_cache
);
552 un
->un_flags
|= UN_CACHED
;
560 union_list_unlock(hash
);
569 struct union_node
*un
= VTOUNION(vp
);
571 if (un
->un_flags
& UN_CACHED
) {
572 un
->un_flags
&= ~UN_CACHED
;
573 LIST_REMOVE(un
, un_cache
);
576 if (un
->un_pvp
!= NULLVP
)
577 vnode_put(un
->un_pvp
);
578 if (un
->un_uppervp
!= NULLVP
)
579 vnode_put(un
->un_uppervp
);
580 if (un
->un_lowervp
!= NULLVP
)
581 vnode_put(un
->un_lowervp
);
582 if (un
->un_dirvp
!= NULLVP
)
583 vnode_put(un
->un_dirvp
);
585 _FREE(un
->un_path
, M_TEMP
);
587 FREE(vp
->v_data
, M_TEMP
);
594 * copyfile. copy the vnode (fvp) to the vnode (tvp)
595 * using a sequence of reads and writes. both (fvp)
596 * and (tvp) are locked on entry and exit.
599 union_copyfile(struct vnode
*fvp
, struct vnode
*tvp
, kauth_cred_t cred
,
605 struct vfs_context context
;
610 * allocate a buffer of size MAXPHYSIO.
611 * loop doing reads and writes, keeping track
612 * of the current uio offset.
613 * give up at the first sign of trouble.
617 context
.vc_ucred
= cred
;
619 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
620 uio
.uio_segflg
= UIO_SYSSPACE
;
622 uio
.uio_segflg
= UIO_SYSSPACE32
;
626 bufp
= _MALLOC(MAXPHYSIO
, M_TEMP
, M_WAITOK
);
628 /* ugly loop follows... */
630 off_t offset
= uio
.uio_offset
;
632 uio
.uio_iovs
.iov32p
= &iov
;
634 iov
.iov_base
= (uintptr_t)bufp
;
635 iov
.iov_len
= MAXPHYSIO
;
636 uio_setresid(&uio
, iov
.iov_len
);
637 uio
.uio_rw
= UIO_READ
;
638 error
= VNOP_READ(fvp
, &uio
, 0, &context
);
641 uio
.uio_iovs
.iov32p
= &iov
;
643 iov
.iov_base
= (uintptr_t)bufp
;
644 iov
.iov_len
= MAXPHYSIO
- uio_resid(&uio
);
645 uio
.uio_offset
= offset
;
646 uio
.uio_rw
= UIO_WRITE
;
647 uio_setresid(&uio
, iov
.iov_len
);
649 if (uio_resid(&uio
) == 0)
653 error
= VNOP_WRITE(tvp
, &uio
, 0, &context
);
654 } while ((uio_resid(&uio
) > 0) && (error
== 0));
657 } while (error
== 0);
664 * (un) is assumed to be locked on entry and remains
668 union_copyup(struct union_node
*un
, int docopy
, kauth_cred_t cred
,
672 struct vnode
*lvp
, *uvp
;
673 struct vfs_context context
;
675 error
= union_vn_create(&uvp
, un
, p
);
680 context
.vc_ucred
= cred
;
682 /* at this point, uppervp is locked */
683 union_newupper(un
, uvp
);
684 un
->un_flags
|= UN_ULOCK
;
686 lvp
= un
->un_lowervp
;
690 * XX - should not ignore errors
693 error
= VNOP_OPEN(lvp
, FREAD
, &context
);
695 error
= union_copyfile(lvp
, uvp
, cred
, p
);
696 (void) VNOP_CLOSE(lvp
, FREAD
, &context
);
698 #ifdef UNION_DIAGNOSTIC
700 uprintf("union: copied up %s\n", un
->un_path
);
704 un
->un_flags
&= ~UN_ULOCK
;
705 union_vn_close(uvp
, FWRITE
, cred
, p
);
706 un
->un_flags
|= UN_ULOCK
;
709 * Subsequent IOs will go to the top layer, so
710 * call close on the lower vnode and open on the
711 * upper vnode to ensure that the filesystem keeps
712 * its references counts right. This doesn't do
713 * the right thing with (cred) and (FREAD) though.
714 * Ignoring error returns is not right, either.
719 for (i
= 0; i
< un
->un_openl
; i
++) {
720 (void) VNOP_CLOSE(lvp
, FREAD
, &context
);
721 (void) VNOP_OPEN(uvp
, FREAD
, &context
);
731 union_relookup(um
, dvp
, vpp
, cnp
, cn
, path
, pathlen
)
732 struct union_mount
*um
;
735 struct componentname
*cnp
;
736 struct componentname
*cn
;
743 * A new componentname structure must be faked up because
744 * there is no way to know where the upper level cnp came
745 * from or what it is being used for. This must duplicate
746 * some of the work done by NDINIT, some of the work done
747 * by namei, some of the work done by lookup and some of
748 * the work done by vnop_lookup when given a CREATE flag.
749 * Conclusion: Horrible.
751 cn
->cn_namelen
= pathlen
;
752 cn
->cn_pnbuf
= _MALLOC_ZONE(cn
->cn_namelen
+1, M_NAMEI
, M_WAITOK
);
753 cn
->cn_pnlen
= cn
->cn_namelen
+1;
754 bcopy(path
, cn
->cn_pnbuf
, cn
->cn_namelen
);
755 cn
->cn_pnbuf
[cn
->cn_namelen
] = '\0';
757 cn
->cn_nameiop
= CREATE
;
758 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
;
766 cn
->cn_context
= cnp
->cn_context
; /* XXX !UNMNT_ABOVE case ??? */
767 cn
->cn_nameptr
= cn
->cn_pnbuf
;
768 cn
->cn_hash
= cnp
->cn_hash
;
769 cn
->cn_consume
= cnp
->cn_consume
;
772 error
= relookup(dvp
, vpp
, cn
);
780 * Create a shadow directory in the upper layer.
781 * The new vnode is returned locked.
783 * (um) points to the union mount structure for access to the
784 * the mounting process's credentials.
785 * (dvp) is the directory in which to create the shadow directory.
786 * it is unlocked on entry and exit.
787 * (cnp) is the componentname to be created.
788 * (vpp) is the returned newly created shadow directory, which
789 * is returned locked.
792 union_mkshadow(um
, dvp
, cnp
, vpp
)
793 struct union_mount
*um
;
795 struct componentname
*cnp
;
799 struct vnode_attr va
;
800 struct componentname cn
;
802 error
= union_relookup(um
, dvp
, vpp
, cnp
, &cn
,
803 cnp
->cn_nameptr
, cnp
->cn_namelen
);
814 * policy: when creating the shadow directory in the
815 * upper layer, create it owned by the user who did
816 * the mount, group from parent directory, and mode
817 * 777 modified by umask (ie mostly identical to the
818 * mkdir syscall). (jsp, kb)
821 VATTR_SET(&va
, va_type
, VDIR
);
822 VATTR_SET(&va
, va_mode
, um
->um_cmode
);
824 error
= vn_create(dvp
, vpp
, &cn
, &va
, 0, cnp
->cn_context
);
829 * Create a whiteout entry in the upper layer.
831 * (um) points to the union mount structure for access to the
832 * the mounting process's credentials.
833 * (dvp) is the directory in which to create the whiteout.
834 * it is locked on entry and exit.
835 * (cnp) is the componentname to be created.
838 union_mkwhiteout(um
, dvp
, cnp
, path
)
839 struct union_mount
*um
;
841 struct componentname
*cnp
;
846 struct componentname cn
;
848 error
= union_relookup(um
, dvp
, &wvp
, cnp
, &cn
, path
, strlen(path
));
858 error
= VNOP_WHITEOUT(dvp
, &cn
, CREATE
, cnp
->cn_context
);
866 * union_vn_create: creates and opens a new shadow file
867 * on the upper union layer. this function is similar
868 * in spirit to calling vn_open but it avoids calling namei().
869 * the problem with calling namei is that a) it locks too many
870 * things, and b) it doesn't start at the "right" directory,
871 * whereas relookup is told where to start.
874 union_vn_create(vpp
, un
, p
)
876 struct union_node
*un
;
880 kauth_cred_t cred
= p
->p_ucred
;
881 struct vnode_attr vat
;
882 struct vnode_attr
*vap
= &vat
;
883 struct vfs_context context
;
884 int fmode
= FFLAGS(O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
);
886 int cmode
= UN_FILEMODE
& ~p
->p_fd
->fd_cmask
;
888 struct componentname cn
;
893 context
.vc_ucred
= p
->p_ucred
;
896 * Build a new componentname structure (for the same
897 * reasons outlines in union_mkshadow).
898 * The difference here is that the file is owned by
899 * the current user, rather than by the person who
900 * did the mount, since the current user needs to be
901 * able to write the file (that's why it is being
902 * copied in the first place).
904 cn
.cn_namelen
= strlen(un
->un_path
);
905 cn
.cn_pnbuf
= (caddr_t
) _MALLOC_ZONE(cn
.cn_namelen
+1,
907 cn
.cn_pnlen
= cn
.cn_namelen
+1;
908 bcopy(un
->un_path
, cn
.cn_pnbuf
, cn
.cn_namelen
+1);
909 cn
.cn_nameiop
= CREATE
;
910 cn
.cn_flags
= (LOCKPARENT
|HASBUF
|SAVENAME
|SAVESTART
|ISLASTCN
);
911 cn
.cn_context
= &context
;
912 cn
.cn_nameptr
= cn
.cn_pnbuf
;
913 cn
.cn_hash
= un
->un_hash
;
916 vnode_get(un
->un_dirvp
);
917 if (error
= relookup(un
->un_dirvp
, &vp
, &cn
))
919 vnode_put(un
->un_dirvp
);
922 vnode_put(un
->un_dirvp
);
928 * Good - there was no race to create the file
929 * so go ahead and create it. The permissions
930 * on the file will be 0666 modified by the
931 * current user's umask. Access to the file, while
932 * it is unioned, will require access to the top *and*
933 * bottom files. Access when not unioned will simply
934 * require access to the top-level file.
936 * TODO: confirm choice of access permissions.
937 * decide on authorisation behaviour
941 VATTR_SET(vap
, va_type
, VREG
);
942 VATTR_SET(vap
, va_mode
, cmode
);
944 if (error
= vn_create(un
->un_dirvp
, &vp
, &cn
, vap
, 0, &context
))
947 if (error
= VNOP_OPEN(vp
, fmode
, &context
)) {
953 if (++vp
->v_writecount
<= 0)
954 panic("union: v_writecount");
961 union_vn_close(struct vnode
*vp
, int fmode
, kauth_cred_t cred
,
964 struct vfs_context context
;
967 context
.vc_ucred
= cred
;
969 if (fmode
& FWRITE
) {
974 return (VNOP_CLOSE(vp
, fmode
, &context
));
978 union_removed_upper(un
)
979 struct union_node
*un
;
981 struct proc
*p
= current_proc(); /* XXX */
983 union_newupper(un
, NULLVP
);
984 if (un
->un_flags
& UN_CACHED
) {
985 un
->un_flags
&= ~UN_CACHED
;
986 LIST_REMOVE(un
, un_cache
);
989 if (un
->un_flags
& UN_ULOCK
) {
990 un
->un_flags
&= ~UN_ULOCK
;
999 struct union_node
*un
= VTOUNION(vp
);
1001 if ((un
->un_lowervp
!= NULLVP
) &&
1002 (vp
->v_type
== un
->un_lowervp
->v_type
)) {
1003 if (vnode_get(un
->un_lowervp
) == 0)
1004 return (un
->un_lowervp
);
1012 * determine whether a whiteout is needed
1013 * during a remove/rmdir operation.
1016 union_dowhiteout(struct union_node
*un
, vfs_context_t ctx
)
1018 struct vnode_attr va
;
1020 if (un
->un_lowervp
!= NULLVP
)
1024 VATTR_WANTED(&va
, va_flags
);
1025 if (vnode_getattr(un
->un_uppervp
, &va
, ctx
) == 0 &&
1026 (va
.va_flags
& OPAQUE
))
1033 union_dircache_r(vp
, vppp
, cntp
)
1035 struct vnode
***vppp
;
1038 struct union_node
*un
;
1040 if (vp
->v_op
!= union_vnodeop_p
) {
1045 panic("union: dircache table too small");
1054 if (un
->un_uppervp
!= NULLVP
)
1055 union_dircache_r(un
->un_uppervp
, vppp
, cntp
);
1056 if (un
->un_lowervp
!= NULLVP
)
1057 union_dircache_r(un
->un_lowervp
, vppp
, cntp
);
1061 union_dircache(vp
, p
)
1068 struct vnode
**dircache
;
1069 struct union_node
*un
;
1072 dircache
= VTOUNION(vp
)->un_dircache
;
1076 if (dircache
== 0) {
1078 union_dircache_r(vp
, 0, &count
);
1080 dircache
= (struct vnode
**)
1081 _MALLOC(count
* sizeof(struct vnode
*),
1084 union_dircache_r(vp
, &vpp
, &count
);
1090 if (*vpp
++ == VTOUNION(vp
)->un_uppervp
)
1092 } while (*vpp
!= NULLVP
);
1099 error
= union_allocvp(&nvp
, vp
->v_mount
, NULLVP
, NULLVP
, 0, *vpp
, NULLVP
, 0);
1103 VTOUNION(vp
)->un_dircache
= 0;
1105 un
->un_dircache
= dircache
;