]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/union/union_subr.c
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1994 Jan-Simon Pendry
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/proc_internal.h>
65 #include <sys/kauth.h>
67 #include <sys/kernel.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/namei.h>
70 #include <sys/malloc.h>
72 #include <sys/filedesc.h>
73 #include <sys/queue.h>
74 #include <sys/mount_internal.h>
77 #include <sys/uio_internal.h>
78 #include <miscfs/union/union.h>
84 /* must be power of two, otherwise change UNION_HASH() */
87 /* unsigned int ... */
88 #define UNION_HASH(u, l) \
89 (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
91 static LIST_HEAD(unhead
, union_node
) unhead
[NHASH
];
92 static int unvplock
[NHASH
];
99 for (i
= 0; i
< NHASH
; i
++)
100 LIST_INIT(&unhead
[i
]);
101 bzero((caddr_t
) unvplock
, sizeof(unvplock
));
109 if (unvplock
[ix
] & UN_LOCKED
) {
110 unvplock
[ix
] |= UN_WANT
;
111 sleep((caddr_t
) &unvplock
[ix
], PINOD
);
115 unvplock
[ix
] |= UN_LOCKED
;
121 union_list_unlock(ix
)
125 unvplock
[ix
] &= ~UN_LOCKED
;
127 if (unvplock
[ix
] & UN_WANT
) {
128 unvplock
[ix
] &= ~UN_WANT
;
129 wakeup((caddr_t
) &unvplock
[ix
]);
134 union_updatevp(un
, uppervp
, lowervp
)
135 struct union_node
*un
;
136 struct vnode
*uppervp
;
137 struct vnode
*lowervp
;
139 int ohash
= UNION_HASH(un
->un_uppervp
, un
->un_lowervp
);
140 int nhash
= UNION_HASH(uppervp
, lowervp
);
141 int docache
= (lowervp
!= NULLVP
|| uppervp
!= NULLVP
);
145 * Ensure locking is ordered from lower to higher
146 * to avoid deadlocks.
157 while (union_list_lock(lhash
))
160 while (union_list_lock(uhash
))
163 if (ohash
!= nhash
|| !docache
) {
164 if (un
->un_flags
& UN_CACHED
) {
165 un
->un_flags
&= ~UN_CACHED
;
166 LIST_REMOVE(un
, un_cache
);
171 union_list_unlock(ohash
);
173 if (un
->un_lowervp
!= lowervp
) {
174 if (un
->un_lowervp
) {
175 vnode_put(un
->un_lowervp
);
177 _FREE(un
->un_path
, M_TEMP
);
181 vnode_put(un
->un_dirvp
);
182 un
->un_dirvp
= NULLVP
;
185 un
->un_lowervp
= lowervp
;
186 un
->un_lowersz
= VNOVAL
;
189 if (un
->un_uppervp
!= uppervp
) {
191 vnode_put(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 union_newlower(un
, lowervp
)
207 struct union_node
*un
;
208 struct vnode
*lowervp
;
211 union_updatevp(un
, un
->un_uppervp
, lowervp
);
215 union_newupper(un
, uppervp
)
216 struct union_node
*un
;
217 struct vnode
*uppervp
;
220 union_updatevp(un
, uppervp
, un
->un_lowervp
);
224 * Keep track of size changes in the underlying vnodes.
225 * If the size changes, then callback to the vm layer
226 * giving priority to the upper layer size.
229 union_newsize(vp
, uppersz
, lowersz
)
231 off_t uppersz
, lowersz
;
233 struct union_node
*un
;
236 /* only interested in regular files */
237 if (vp
->v_type
!= VREG
)
243 if ((uppersz
!= VNOVAL
) && (un
->un_uppersz
!= uppersz
)) {
244 un
->un_uppersz
= uppersz
;
249 if ((lowersz
!= VNOVAL
) && (un
->un_lowersz
!= lowersz
)) {
250 un
->un_lowersz
= lowersz
;
256 #ifdef UNION_DIAGNOSTIC
257 printf("union: %s size now %ld\n",
258 uppersz
!= VNOVAL
? "upper" : "lower", (long) sz
);
265 * allocate a union_node/vnode pair. the vnode is
266 * referenced and locked. the new vnode is returned
267 * via (vpp). (mp) is the mountpoint of the union filesystem,
268 * (dvp) is the parent directory where the upper layer object
269 * should exist (but doesn't) and (cnp) is the componentname
270 * information which is partially copied to allow the upper
271 * layer object to be created at a later time. (uppervp)
272 * and (lowervp) reference the upper and lower layer objects
273 * being mapped. either, but not both, can be nil.
274 * if supplied, (uppervp) is locked.
275 * the reference is either maintained in the new union_node
276 * object which is allocated, or they are vnode_put'd.
278 * all union_nodes are maintained on a singly-linked
279 * list. new nodes are only allocated when they cannot
280 * be found on this list. entries on the list are
281 * removed when the vfs reclaim entry is called.
283 * a single lock is kept for the entire list. this is
284 * needed because the getnewvnode() function can block
285 * waiting for a vnode to become free, in which case there
286 * may be more than one process trying to get the same
287 * vnode. this lock is only taken if we are going to
288 * call getnewvnode, since the kernel itself is single-threaded.
290 * if an entry is found on the list, then call vnode_get() to
291 * take a reference. this is done because there may be
292 * zero references to it and so it needs to removed from
293 * the vnode free list.
296 union_allocvp(vpp
, mp
, undvp
, dvp
, cnp
, uppervp
, lowervp
, docache
)
299 struct vnode
*undvp
; /* parent union vnode */
300 struct vnode
*dvp
; /* may be null */
301 struct componentname
*cnp
; /* may be null */
302 struct vnode
*uppervp
; /* may be null */
303 struct vnode
*lowervp
; /* may be null */
307 struct union_node
*un
;
308 struct union_node
**pp
;
309 struct vnode
*xlowervp
= NULLVP
;
310 struct union_mount
*um
= MOUNTTOUNIONMOUNT(mp
);
314 struct union_node
*unp
;
315 struct vnode_fsparam vfsp
;
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 (vnode_get(UNIONTOV(un
))) {
373 union_list_unlock(hash
);
380 union_list_unlock(hash
);
388 * Obtain a lock on the union_node.
389 * uppervp is locked, though un->un_uppervp
390 * may not be. this doesn't break the locking
391 * hierarchy since in the case that un->un_uppervp
392 * is not yet locked it will be vnode_put'd and replaced
396 if ((dvp
!= NULLVP
) && (uppervp
== dvp
)) {
398 * Access ``.'', so (un) will already
399 * be locked. Since this process has
400 * the lock on (uppervp) no other
401 * process can hold the lock on (un).
404 if ((un
->un_flags
& UN_LOCKED
) == 0)
405 panic("union: . not locked");
406 else if (current_proc() && un
->un_pid
!= current_proc()->p_pid
&&
407 un
->un_pid
> -1 && current_proc()->p_pid
> -1)
408 panic("union: allocvp not lock owner");
411 if (un
->un_flags
& UN_LOCKED
) {
412 vnode_put(UNIONTOV(un
));
413 un
->un_flags
|= UN_WANT
;
414 sleep((caddr_t
) &un
->un_flags
, PINOD
);
417 un
->un_flags
|= UN_LOCKED
;
421 un
->un_pid
= current_proc()->p_pid
;
428 * At this point, the union_node is locked,
429 * un->un_uppervp may not be locked, and uppervp
434 * Save information about the upper layer.
436 if (uppervp
!= un
->un_uppervp
) {
437 union_newupper(un
, uppervp
);
438 } else if (uppervp
) {
442 if (un
->un_uppervp
) {
443 un
->un_flags
|= UN_ULOCK
;
444 un
->un_flags
&= ~UN_KLOCK
;
448 * Save information about the lower layer.
449 * This needs to keep track of pathname
450 * and directory information which union_vn_create
453 if (lowervp
!= un
->un_lowervp
) {
454 union_newlower(un
, lowervp
);
455 if (cnp
&& (lowervp
!= NULLVP
)) {
456 un
->un_hash
= cnp
->cn_hash
;
457 MALLOC(un
->un_path
, caddr_t
, cnp
->cn_namelen
+1,
459 bcopy(cnp
->cn_nameptr
, un
->un_path
,
461 un
->un_path
[cnp
->cn_namelen
] = '\0';
465 } else if (lowervp
) {
474 * otherwise lock the vp list while we call getnewvnode
475 * since that can block.
477 hash
= UNION_HASH(uppervp
, lowervp
);
479 if (union_list_lock(hash
))
483 MALLOC(unp
, void *, sizeof(struct union_node
), M_TEMP
, M_WAITOK
);
486 vtype
= uppervp
->v_type
;
488 vtype
= lowervp
->v_type
;
489 //bzero(&vfsp, sizeof(struct vnode_fsparam));
491 vfsp
.vnfs_vtype
= vtype
;
492 vfsp
.vnfs_str
= "unionfs";
494 vfsp
.vnfs_fsnode
= unp
;
496 vfsp
.vnfs_vops
= union_vnodeop_p
;
498 vfsp
.vnfs_filesize
= 0;
499 vfsp
.vnfs_flags
= VNFS_NOCACHE
| VNFS_CANTCACHE
;
500 vfsp
.vnfs_marksystem
= 0;
501 vfsp
.vnfs_markroot
= markroot
;
503 error
= vnode_create(VNCREATE_FLAVOR
, VCREATESIZE
, &vfsp
, vpp
);
515 (*vpp
)->v_tag
= VT_UNION
;
518 un
->un_uppervp
= uppervp
;
519 un
->un_uppersz
= VNOVAL
;
520 un
->un_lowervp
= lowervp
;
521 un
->un_lowersz
= VNOVAL
;
527 un
->un_flags
= UN_LOCKED
;
529 un
->un_flags
|= UN_ULOCK
;
532 un
->un_pid
= current_proc()->p_pid
;
536 if (cnp
&& (lowervp
!= NULLVP
)) {
537 un
->un_hash
= cnp
->cn_hash
;
538 un
->un_path
= _MALLOC(cnp
->cn_namelen
+1, M_TEMP
, M_WAITOK
);
539 bcopy(cnp
->cn_nameptr
, un
->un_path
, cnp
->cn_namelen
);
540 un
->un_path
[cnp
->cn_namelen
] = '\0';
550 LIST_INSERT_HEAD(&unhead
[hash
], un
, un_cache
);
551 un
->un_flags
|= UN_CACHED
;
559 union_list_unlock(hash
);
568 struct union_node
*un
= VTOUNION(vp
);
570 if (un
->un_flags
& UN_CACHED
) {
571 un
->un_flags
&= ~UN_CACHED
;
572 LIST_REMOVE(un
, un_cache
);
575 if (un
->un_pvp
!= NULLVP
)
576 vnode_put(un
->un_pvp
);
577 if (un
->un_uppervp
!= NULLVP
)
578 vnode_put(un
->un_uppervp
);
579 if (un
->un_lowervp
!= NULLVP
)
580 vnode_put(un
->un_lowervp
);
581 if (un
->un_dirvp
!= NULLVP
)
582 vnode_put(un
->un_dirvp
);
584 _FREE(un
->un_path
, M_TEMP
);
586 FREE(vp
->v_data
, M_TEMP
);
593 * copyfile. copy the vnode (fvp) to the vnode (tvp)
594 * using a sequence of reads and writes. both (fvp)
595 * and (tvp) are locked on entry and exit.
598 union_copyfile(struct vnode
*fvp
, struct vnode
*tvp
, kauth_cred_t cred
,
604 struct vfs_context context
;
609 * allocate a buffer of size MAXPHYSIO.
610 * loop doing reads and writes, keeping track
611 * of the current uio offset.
612 * give up at the first sign of trouble.
616 context
.vc_ucred
= cred
;
618 #if 1 /* LP64todo - can't use new segment flags until the drivers are ready */
619 uio
.uio_segflg
= UIO_SYSSPACE
;
621 uio
.uio_segflg
= UIO_SYSSPACE32
;
625 bufp
= _MALLOC(MAXPHYSIO
, M_TEMP
, M_WAITOK
);
627 /* ugly loop follows... */
629 off_t offset
= uio
.uio_offset
;
631 uio
.uio_iovs
.iov32p
= &iov
;
633 iov
.iov_base
= (uintptr_t)bufp
;
634 iov
.iov_len
= MAXPHYSIO
;
635 uio_setresid(&uio
, iov
.iov_len
);
636 uio
.uio_rw
= UIO_READ
;
637 error
= VNOP_READ(fvp
, &uio
, 0, &context
);
640 uio
.uio_iovs
.iov32p
= &iov
;
642 iov
.iov_base
= (uintptr_t)bufp
;
643 iov
.iov_len
= MAXPHYSIO
- uio_resid(&uio
);
644 uio
.uio_offset
= offset
;
645 uio
.uio_rw
= UIO_WRITE
;
646 uio_setresid(&uio
, iov
.iov_len
);
648 if (uio_resid(&uio
) == 0)
652 error
= VNOP_WRITE(tvp
, &uio
, 0, &context
);
653 } while ((uio_resid(&uio
) > 0) && (error
== 0));
656 } while (error
== 0);
663 * (un) is assumed to be locked on entry and remains
667 union_copyup(struct union_node
*un
, int docopy
, kauth_cred_t cred
,
671 struct vnode
*lvp
, *uvp
;
672 struct vfs_context context
;
674 error
= union_vn_create(&uvp
, un
, p
);
679 context
.vc_ucred
= cred
;
681 /* at this point, uppervp is locked */
682 union_newupper(un
, uvp
);
683 un
->un_flags
|= UN_ULOCK
;
685 lvp
= un
->un_lowervp
;
689 * XX - should not ignore errors
692 error
= VNOP_OPEN(lvp
, FREAD
, &context
);
694 error
= union_copyfile(lvp
, uvp
, cred
, p
);
695 (void) VNOP_CLOSE(lvp
, FREAD
, &context
);
697 #ifdef UNION_DIAGNOSTIC
699 uprintf("union: copied up %s\n", un
->un_path
);
703 un
->un_flags
&= ~UN_ULOCK
;
704 union_vn_close(uvp
, FWRITE
, cred
, 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) VNOP_CLOSE(lvp
, FREAD
, &context
);
720 (void) VNOP_OPEN(uvp
, FREAD
, &context
);
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 vnop_lookup when given a CREATE flag.
748 * Conclusion: Horrible.
750 cn
->cn_namelen
= pathlen
;
751 cn
->cn_pnbuf
= _MALLOC_ZONE(cn
->cn_namelen
+1, M_NAMEI
, M_WAITOK
);
752 cn
->cn_pnlen
= cn
->cn_namelen
+1;
753 bcopy(path
, cn
->cn_pnbuf
, cn
->cn_namelen
);
754 cn
->cn_pnbuf
[cn
->cn_namelen
] = '\0';
756 cn
->cn_nameiop
= CREATE
;
757 cn
->cn_flags
= (LOCKPARENT
|HASBUF
|SAVENAME
|SAVESTART
|ISLASTCN
);
759 cn
->cn_proc
= cnp
->cn_proc
;
760 if (um
->um_op
== UNMNT_ABOVE
)
761 cn
->cn_cred
= cnp
->cn_cred
;
763 cn
->cn_cred
= um
->um_cred
;
765 cn
->cn_context
= cnp
->cn_context
; /* XXX !UNMNT_ABOVE case ??? */
766 cn
->cn_nameptr
= cn
->cn_pnbuf
;
767 cn
->cn_hash
= cnp
->cn_hash
;
768 cn
->cn_consume
= cnp
->cn_consume
;
771 error
= relookup(dvp
, vpp
, cn
);
779 * Create a shadow directory in the upper layer.
780 * The new vnode is returned locked.
782 * (um) points to the union mount structure for access to the
783 * the mounting process's credentials.
784 * (dvp) is the directory in which to create the shadow directory.
785 * it is unlocked on entry and exit.
786 * (cnp) is the componentname to be created.
787 * (vpp) is the returned newly created shadow directory, which
788 * is returned locked.
791 union_mkshadow(um
, dvp
, cnp
, vpp
)
792 struct union_mount
*um
;
794 struct componentname
*cnp
;
798 struct vnode_attr va
;
799 struct componentname cn
;
801 error
= union_relookup(um
, dvp
, vpp
, cnp
, &cn
,
802 cnp
->cn_nameptr
, cnp
->cn_namelen
);
813 * policy: when creating the shadow directory in the
814 * upper layer, create it owned by the user who did
815 * the mount, group from parent directory, and mode
816 * 777 modified by umask (ie mostly identical to the
817 * mkdir syscall). (jsp, kb)
820 VATTR_SET(&va
, va_type
, VDIR
);
821 VATTR_SET(&va
, va_mode
, um
->um_cmode
);
823 error
= vn_create(dvp
, vpp
, &cn
, &va
, 0, cnp
->cn_context
);
828 * Create a whiteout entry in the upper layer.
830 * (um) points to the union mount structure for access to the
831 * the mounting process's credentials.
832 * (dvp) is the directory in which to create the whiteout.
833 * it is locked on entry and exit.
834 * (cnp) is the componentname to be created.
837 union_mkwhiteout(um
, dvp
, cnp
, path
)
838 struct union_mount
*um
;
840 struct componentname
*cnp
;
845 struct componentname cn
;
847 error
= union_relookup(um
, dvp
, &wvp
, cnp
, &cn
, path
, strlen(path
));
857 error
= VNOP_WHITEOUT(dvp
, &cn
, CREATE
, cnp
->cn_context
);
865 * union_vn_create: creates and opens a new shadow file
866 * on the upper union layer. this function is similar
867 * in spirit to calling vn_open but it avoids calling namei().
868 * the problem with calling namei is that a) it locks too many
869 * things, and b) it doesn't start at the "right" directory,
870 * whereas relookup is told where to start.
873 union_vn_create(vpp
, un
, p
)
875 struct union_node
*un
;
879 kauth_cred_t cred
= p
->p_ucred
;
880 struct vnode_attr vat
;
881 struct vnode_attr
*vap
= &vat
;
882 struct vfs_context context
;
883 int fmode
= FFLAGS(O_WRONLY
|O_CREAT
|O_TRUNC
|O_EXCL
);
885 int cmode
= UN_FILEMODE
& ~p
->p_fd
->fd_cmask
;
887 struct componentname cn
;
892 context
.vc_ucred
= p
->p_ucred
;
895 * Build a new componentname structure (for the same
896 * reasons outlines in union_mkshadow).
897 * The difference here is that the file is owned by
898 * the current user, rather than by the person who
899 * did the mount, since the current user needs to be
900 * able to write the file (that's why it is being
901 * copied in the first place).
903 cn
.cn_namelen
= strlen(un
->un_path
);
904 cn
.cn_pnbuf
= (caddr_t
) _MALLOC_ZONE(cn
.cn_namelen
+1,
906 cn
.cn_pnlen
= cn
.cn_namelen
+1;
907 bcopy(un
->un_path
, cn
.cn_pnbuf
, cn
.cn_namelen
+1);
908 cn
.cn_nameiop
= CREATE
;
909 cn
.cn_flags
= (LOCKPARENT
|HASBUF
|SAVENAME
|SAVESTART
|ISLASTCN
);
910 cn
.cn_context
= &context
;
911 cn
.cn_nameptr
= cn
.cn_pnbuf
;
912 cn
.cn_hash
= un
->un_hash
;
915 vnode_get(un
->un_dirvp
);
916 if (error
= relookup(un
->un_dirvp
, &vp
, &cn
))
918 vnode_put(un
->un_dirvp
);
921 vnode_put(un
->un_dirvp
);
927 * Good - there was no race to create the file
928 * so go ahead and create it. The permissions
929 * on the file will be 0666 modified by the
930 * current user's umask. Access to the file, while
931 * it is unioned, will require access to the top *and*
932 * bottom files. Access when not unioned will simply
933 * require access to the top-level file.
935 * TODO: confirm choice of access permissions.
936 * decide on authorisation behaviour
940 VATTR_SET(vap
, va_type
, VREG
);
941 VATTR_SET(vap
, va_mode
, cmode
);
943 if (error
= vn_create(un
->un_dirvp
, &vp
, &cn
, vap
, 0, &context
))
946 if (error
= VNOP_OPEN(vp
, fmode
, &context
)) {
952 if (++vp
->v_writecount
<= 0)
953 panic("union: v_writecount");
960 union_vn_close(struct vnode
*vp
, int fmode
, kauth_cred_t cred
,
963 struct vfs_context context
;
966 context
.vc_ucred
= cred
;
968 if (fmode
& FWRITE
) {
973 return (VNOP_CLOSE(vp
, fmode
, &context
));
977 union_removed_upper(un
)
978 struct union_node
*un
;
980 struct proc
*p
= current_proc(); /* XXX */
982 union_newupper(un
, NULLVP
);
983 if (un
->un_flags
& UN_CACHED
) {
984 un
->un_flags
&= ~UN_CACHED
;
985 LIST_REMOVE(un
, un_cache
);
988 if (un
->un_flags
& UN_ULOCK
) {
989 un
->un_flags
&= ~UN_ULOCK
;
998 struct union_node
*un
= VTOUNION(vp
);
1000 if ((un
->un_lowervp
!= NULLVP
) &&
1001 (vp
->v_type
== un
->un_lowervp
->v_type
)) {
1002 if (vnode_get(un
->un_lowervp
) == 0)
1003 return (un
->un_lowervp
);
1011 * determine whether a whiteout is needed
1012 * during a remove/rmdir operation.
1015 union_dowhiteout(struct union_node
*un
, vfs_context_t ctx
)
1017 struct vnode_attr va
;
1019 if (un
->un_lowervp
!= NULLVP
)
1023 VATTR_WANTED(&va
, va_flags
);
1024 if (vnode_getattr(un
->un_uppervp
, &va
, ctx
) == 0 &&
1025 (va
.va_flags
& OPAQUE
))
1032 union_dircache_r(vp
, vppp
, cntp
)
1034 struct vnode
***vppp
;
1037 struct union_node
*un
;
1039 if (vp
->v_op
!= union_vnodeop_p
) {
1044 panic("union: dircache table too small");
1053 if (un
->un_uppervp
!= NULLVP
)
1054 union_dircache_r(un
->un_uppervp
, vppp
, cntp
);
1055 if (un
->un_lowervp
!= NULLVP
)
1056 union_dircache_r(un
->un_lowervp
, vppp
, cntp
);
1060 union_dircache(vp
, p
)
1067 struct vnode
**dircache
;
1068 struct union_node
*un
;
1071 dircache
= VTOUNION(vp
)->un_dircache
;
1075 if (dircache
== 0) {
1077 union_dircache_r(vp
, 0, &count
);
1079 dircache
= (struct vnode
**)
1080 _MALLOC(count
* sizeof(struct vnode
*),
1083 union_dircache_r(vp
, &vpp
, &count
);
1089 if (*vpp
++ == VTOUNION(vp
)->un_uppervp
)
1091 } while (*vpp
!= NULLVP
);
1098 error
= union_allocvp(&nvp
, vp
->v_mount
, NULLVP
, NULLVP
, 0, *vpp
, NULLVP
, 0);
1102 VTOUNION(vp
)->un_dircache
= 0;
1104 un
->un_dircache
= dircache
;