2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
31 * Copyright (c) 1992, 1993, 1994, 1995
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kauth.h>
75 #include <sys/types.h>
76 #include <sys/vnode_internal.h>
77 #include <sys/mount_internal.h>
78 #include <sys/namei.h>
79 #include <sys/malloc.h>
80 #include <sys/buf_internal.h>
81 #include <sys/queue.h>
83 #include <miscfs/union/union.h>
84 #include <vfs/vfs_support.h>
86 #include <sys/uio_internal.h>
88 #define FIXUP(un, p) { \
89 if (((un)->un_flags & UN_ULOCK) == 0) { \
96 struct union_node
*un
;
100 un
->un_flags
|= UN_ULOCK
;
104 union_lookup1(struct vnode
*udvp
, struct vnode
**dvpp
, struct vnode
**vpp
,
105 struct componentname
*cnp
)
108 vfs_context_t ctx
= cnp
->cn_context
;
116 * If stepping up the directory tree, check for going
117 * back across the mount point, in which case do what
118 * lookup would do by stepping back down the mount
121 if (cnp
->cn_flags
& ISDOTDOT
) {
122 while ((dvp
!= udvp
) && (dvp
->v_flag
& VROOT
)) {
124 * Don't do the NOCROSSMOUNT check
125 * at this level. By definition,
126 * union fs deals with namespaces, not
130 *dvpp
= dvp
= dvp
->v_mount
->mnt_vnodecovered
;
136 error
= VNOP_LOOKUP(dvp
, &tdvp
, cnp
, ctx
);
142 * Lastly check if the current node is a mount point in
143 * which case walk up the mount hierarchy making sure not to
144 * bump into the root of the mount tree (ie. dvp != udvp).
146 while (dvp
!= udvp
&& (dvp
->v_type
== VDIR
) &&
147 (mp
= dvp
->v_mountedhere
)) {
148 if (vfs_busy(mp
, LK_NOWAIT
)) {
152 error
= VFS_ROOT(mp
, &tdvp
, ctx
);
169 struct vnop_lookup_args
/* {
170 struct vnodeop_desc *a_desc;
172 struct vnode **a_vpp;
173 struct componentname *a_cnp;
174 vfs_context_t a_context;
179 struct vnode
*uppervp
, *lowervp
;
180 struct vnode
*upperdvp
, *lowerdvp
;
181 struct vnode
*dvp
= ap
->a_dvp
;
182 struct union_node
*dun
= VTOUNION(dvp
);
183 struct componentname
*cnp
= ap
->a_cnp
;
184 vfs_context_t ctx
= cnp
->cn_context
;
185 struct proc
*p
= vfs_context_proc(ctx
);
186 int lockparent
= cnp
->cn_flags
& LOCKPARENT
;
187 struct union_mount
*um
= MOUNTTOUNIONMOUNT(dvp
->v_mount
);
188 kauth_cred_t saved_cred
;
190 struct vnode_attr va
;
193 if (cnp
->cn_namelen
== 3 &&
194 cnp
->cn_nameptr
[2] == '.' &&
195 cnp
->cn_nameptr
[1] == '.' &&
196 cnp
->cn_nameptr
[0] == '.') {
197 dvp
= *ap
->a_vpp
= LOWERVP(ap
->a_dvp
);
206 cnp
->cn_flags
|= LOCKPARENT
;
208 upperdvp
= dun
->un_uppervp
;
209 lowerdvp
= dun
->un_lowervp
;
215 * do the lookup in the upper level.
216 * if that level comsumes additional pathnames,
217 * then assume that something special is going
218 * on and just return that vnode.
220 if (upperdvp
!= NULLVP
) {
222 uerror
= union_lookup1(um
->um_uppervp
, &upperdvp
,
224 /*if (uppervp == upperdvp)
225 dun->un_flags |= UN_KLOCK;*/
227 if (cnp
->cn_consume
!= 0) {
228 *ap
->a_vpp
= uppervp
;
230 cnp
->cn_flags
&= ~LOCKPARENT
;
233 if (uerror
== ENOENT
|| uerror
== EJUSTRETURN
) {
234 if (cnp
->cn_flags
& ISWHITEOUT
) {
236 } else if (lowerdvp
!= NULLVP
) {
238 VATTR_WANTED(&va
, va_flags
);
239 lerror
= vnode_getattr(upperdvp
, &va
, ap
->a_context
);
240 if (lerror
== 0 && (va
.va_flags
& OPAQUE
))
249 * in a similar way to the upper layer, do the lookup
250 * in the lower layer. this time, if there is some
251 * component magic going on, then vnode_put whatever we got
252 * back from the upper layer and return the lower vnode
255 if (lowerdvp
!= NULLVP
&& !iswhiteout
) {
259 * Only do a LOOKUP on the bottom node, since
260 * we won't be making changes to it anyway.
262 nameiop
= cnp
->cn_nameiop
;
263 cnp
->cn_nameiop
= LOOKUP
;
264 if (um
->um_op
== UNMNT_BELOW
) {
266 saved_cred
= cnp
->cn_context
->vc_ucred
;
267 cnp
->cn_context
->vc_ucred
= um
->um_cred
;
268 lerror
= union_lookup1(um
->um_lowervp
, &lowerdvp
,
270 cnp
->cn_context
->vc_ucred
= saved_cred
;
272 lerror
= union_lookup1(um
->um_lowervp
, &lowerdvp
,
275 cnp
->cn_nameiop
= nameiop
;
277 if (cnp
->cn_consume
!= 0) {
278 if (uppervp
!= NULLVP
) {
282 *ap
->a_vpp
= lowervp
;
284 cnp
->cn_flags
&= ~LOCKPARENT
;
289 if ((cnp
->cn_flags
& ISDOTDOT
) && dun
->un_pvp
!= NULLVP
) {
290 lowervp
= LOWERVP(dun
->un_pvp
);
291 if (lowervp
!= NULLVP
) {
299 cnp
->cn_flags
&= ~LOCKPARENT
;
302 * at this point, we have uerror and lerror indicating
303 * possible errors with the lookups in the upper and lower
304 * layers. additionally, uppervp and lowervp are (locked)
305 * references to existing vnodes in the upper and lower layers.
307 * there are now three cases to consider.
308 * 1. if both layers returned an error, then return whatever
309 * error the upper layer generated.
311 * 2. if the top layer failed and the bottom layer succeeded
312 * then two subcases occur.
313 * a. the bottom vnode is not a directory, in which
314 * case just return a new union vnode referencing
315 * an empty top layer and the existing bottom layer.
316 * b. the bottom vnode is a directory, in which case
317 * create a new directory in the top-level and
318 * continue as in case 3.
320 * 3. if the top layer succeeded then return a new union
321 * vnode referencing whatever the new top layer and
322 * whatever the bottom layer returned.
328 if ((uerror
!= 0) && (lerror
!= 0)) {
333 if (uerror
!= 0 /* && (lerror == 0) */ ) {
334 if (lowervp
->v_type
== VDIR
) { /* case 2b. */
335 dun
->un_flags
&= ~UN_ULOCK
;
336 uerror
= union_mkshadow(um
, upperdvp
, cnp
, &uppervp
);
337 dun
->un_flags
|= UN_ULOCK
;
340 if (lowervp
!= NULLVP
) {
348 error
= union_allocvp(ap
->a_vpp
, dvp
->v_mount
, dvp
, upperdvp
, cnp
,
349 uppervp
, lowervp
, 1);
352 if (uppervp
!= NULLVP
)
354 if (lowervp
!= NULLVP
)
363 struct vnop_create_args
/* {
365 struct vnode **a_vpp;
366 struct componentname *a_cnp;
367 struct vnode_attr *a_vap;
368 vfs_context_t a_context;
371 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
372 struct vnode
*dvp
= un
->un_uppervp
;
373 struct componentname
*cnp
= ap
->a_cnp
;
374 vfs_context_t ctx
= cnp
->cn_context
;
375 struct proc
*p
= vfs_context_proc(ctx
);
384 un
->un_flags
|= UN_KLOCK
;
385 mp
= ap
->a_dvp
->v_mount
;
387 /* note that this is a direct passthrough to the filesystem */
388 error
= VNOP_CREATE(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_context
);
392 error
= union_allocvp(ap
->a_vpp
, mp
, NULLVP
, NULLVP
, cnp
, vp
,
403 struct vnop_whiteout_args
/* {
405 struct componentname *a_cnp;
407 vfs_context_t a_context;
410 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
411 struct componentname
*cnp
= ap
->a_cnp
;
412 vfs_context_t ctx
= cnp
->cn_context
;
413 struct proc
*p
= vfs_context_proc(ctx
);
415 if (un
->un_uppervp
== NULLVP
)
419 return (VNOP_WHITEOUT(un
->un_uppervp
, cnp
, ap
->a_flags
, ap
->a_context
));
424 struct vnop_mknod_args
/* {
426 struct vnode **a_vpp;
427 struct componentname *a_cnp;
428 struct vnode_attr *a_vap;
429 vfs_context_t a_context;
432 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
433 struct vnode
*dvp
= un
->un_uppervp
;
434 struct componentname
*cnp
= ap
->a_cnp
;
435 vfs_context_t ctx
= cnp
->cn_context
;
436 struct proc
*p
= vfs_context_proc(ctx
);
445 un
->un_flags
|= UN_KLOCK
;
446 mp
= ap
->a_dvp
->v_mount
;
448 /* note that this is a direct passthrough to the filesystem */
449 error
= VNOP_MKNOD(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_context
);
454 error
= union_allocvp(ap
->a_vpp
, mp
, NULLVP
, NULLVP
,
466 struct vnop_open_args
/* {
467 struct vnodeop_desc *a_desc;
470 vfs_context_t a_context;
473 struct union_node
*un
= VTOUNION(ap
->a_vp
);
475 int mode
= ap
->a_mode
;
476 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
477 struct proc
*p
= vfs_context_proc(ap
->a_context
);
481 * If there is an existing upper vp then simply open that.
483 tvp
= un
->un_uppervp
;
486 * If the lower vnode is being opened for writing, then
487 * copy the file contents to the upper vnode and open that,
488 * otherwise can simply open the lower vnode.
490 tvp
= un
->un_lowervp
;
491 if ((ap
->a_mode
& FWRITE
) && (tvp
->v_type
== VREG
)) {
492 error
= union_copyup(un
, (mode
&O_TRUNC
) == 0, cred
, p
);
494 error
= VNOP_OPEN(un
->un_uppervp
, mode
, ap
->a_context
);
499 * Just open the lower vnode
503 error
= VNOP_OPEN(tvp
, mode
, ap
->a_context
);
510 error
= VNOP_OPEN(tvp
, mode
, ap
->a_context
);
517 struct vnop_close_args
/* {
520 vfs_context_t a_context;
523 struct union_node
*un
= VTOUNION(ap
->a_vp
);
526 if ((vp
= un
->un_uppervp
) == NULLVP
) {
527 #ifdef UNION_DIAGNOSTIC
528 if (un
->un_openl
<= 0)
529 panic("union: un_openl cnt");
536 return (VCALL(vp
, VOFFSET(vnop_close
), ap
));
540 * Check access permission on the union vnode.
541 * The access check being enforced is to check
542 * against both the underlying vnode, and any
543 * copied vnode. This ensures that no additional
544 * file permissions are given away simply because
545 * the user caused an implicit file copy.
549 struct vnop_access_args
/* {
550 struct vnodeop_desc *a_desc;
553 vfs_context_t a_context;
556 struct union_node
*un
= VTOUNION(ap
->a_vp
);
557 struct proc
*p
= vfs_context_proc(ap
->a_context
);
561 if ((vp
= un
->un_uppervp
) != NULLVP
) {
564 return (VCALL(vp
, VOFFSET(vnop_access
), ap
));
567 if ((vp
= un
->un_lowervp
) != NULLVP
) {
569 error
= VCALL(vp
, VOFFSET(vnop_access
), ap
);
571 struct union_mount
*um
= MOUNTTOUNIONMOUNT(vp
->v_mount
);
573 if (um
->um_op
== UNMNT_BELOW
) {
575 // ap->a_cred = um->um_cred;
576 error
= VCALL(vp
, VOFFSET(vnop_access
), ap
);
587 * We handle getattr only to change the fsid and
592 struct vnop_getattr_args
/* {
594 struct vnode_attr *a_vap;
595 vfs_context_t a_context;
599 struct union_node
*un
= VTOUNION(ap
->a_vp
);
600 struct vnode
*vp
= un
->un_uppervp
;
601 struct proc
*p
= vfs_context_proc(ap
->a_context
);
602 struct vnode_attr
*vap
;
603 struct vnode_attr va
;
607 * Some programs walk the filesystem hierarchy by counting
608 * links to directories to avoid stat'ing all the time.
609 * This means the link count on directories needs to be "correct".
610 * The only way to do that is to call getattr on both layers
611 * and fix up the link count. The link count will not necessarily
612 * be accurate but will be large enough to defeat the tree walkers.
620 * It's not clear whether vnop_getattr is to be
621 * called with the vnode locked or not. stat() calls
622 * it with (vp) locked, and fstat calls it with
624 * In the mean time, compensate here by checking
625 * the union_node's lock flag.
627 if (un
->un_flags
& UN_LOCKED
)
630 error
= vnode_getattr(vp
, vap
, ap
->a_context
);
633 union_newsize(ap
->a_vp
, vap
->va_data_size
, VNOVAL
);
638 } else if (vp
->v_type
== VDIR
) {
641 /* all we want from the lower node is the link count */
642 VATTR_WANTED(&va
, va_nlink
);
649 error
= vnode_getattr(vp
, vap
, ap
->a_context
);
652 union_newsize(ap
->a_vp
, VNOVAL
, vap
->va_data_size
);
655 if ((vap
!= ap
->a_vap
) && (vap
->va_type
== VDIR
))
656 ap
->a_vap
->va_nlink
+= vap
->va_nlink
;
658 VATTR_RETURN(ap
->a_vap
, va_fsid
, ap
->a_vp
->v_mount
->mnt_vfsstat
.f_fsid
.val
[0]);
664 struct vnop_setattr_args
/* {
666 struct vnode_attr *a_vap;
667 vfs_context_t a_context;
670 struct union_node
*un
= VTOUNION(ap
->a_vp
);
671 struct proc
*p
= vfs_context_proc(ap
->a_context
);
672 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
676 * Handle case of truncating lower object to zero size,
677 * by creating a zero length upper object. This is to
678 * handle the case of open with O_TRUNC and O_CREAT.
680 if (VATTR_IS_ACTIVE(ap
->a_vap
, va_data_size
) &&
681 (un
->un_uppervp
== NULLVP
) &&
682 /* assert(un->un_lowervp != NULLVP) */
683 (un
->un_lowervp
->v_type
== VREG
)) {
684 error
= union_copyup(un
, (ap
->a_vap
->va_data_size
!= 0), cred
, p
);
690 * Try to set attributes in upper layer,
691 * otherwise return read-only filesystem error.
693 if (un
->un_uppervp
!= NULLVP
) {
695 error
= vnode_setattr(un
->un_uppervp
, ap
->a_vap
, ap
->a_context
);
696 if ((error
== 0) && VATTR_IS_ACTIVE(ap
->a_vap
, va_data_size
))
697 union_newsize(ap
->a_vp
, ap
->a_vap
->va_data_size
, VNOVAL
);
707 struct vnop_read_args
/* {
711 vfs_context_t a_context;
715 struct proc
*p
= vfs_context_proc(ap
->a_context
);
716 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
717 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
720 FIXUP(VTOUNION(ap
->a_vp
), p
);
721 error
= VNOP_READ(vp
, ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
);
725 * perhaps the size of the underlying object has changed under
726 * our feet. take advantage of the offset information present
727 * in the uio structure.
730 struct union_node
*un
= VTOUNION(ap
->a_vp
);
731 off_t cur
= ap
->a_uio
->uio_offset
;
733 if (vp
== un
->un_uppervp
) {
734 if (cur
> un
->un_uppersz
)
735 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
737 if (cur
> un
->un_lowersz
)
738 union_newsize(ap
->a_vp
, VNOVAL
, cur
);
747 struct vnop_read_args
/* {
751 vfs_context_t a_context;
756 struct union_node
*un
= VTOUNION(ap
->a_vp
);
757 struct proc
*p
= vfs_context_proc(ap
->a_context
);
759 vp
= UPPERVP(ap
->a_vp
);
761 panic("union: missing upper layer in write");
764 error
= VNOP_WRITE(vp
, ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
);
767 * the size of the underlying object may be changed by the
771 off_t cur
= ap
->a_uio
->uio_offset
;
773 if (cur
> un
->un_uppersz
)
774 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
783 struct vnop_ioctl_args
/* {
788 vfs_context_t a_context;
791 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
794 return (VCALL(ovp
, VOFFSET(vnop_ioctl
), ap
));
799 struct vnop_select_args
/* {
804 vfs_context_t a_context;
807 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
810 return (VCALL(ovp
, VOFFSET(vnop_select
), ap
));
815 struct vnop_revoke_args
/* {
818 vfs_context_t a_context;
821 struct vnode
*vp
= ap
->a_vp
;
824 VNOP_REVOKE(UPPERVP(vp
), ap
->a_flags
, ap
->a_context
);
826 VNOP_REVOKE(LOWERVP(vp
), ap
->a_flags
, ap
->a_context
);
832 struct vnop_mmap_args
/* {
839 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
842 return (VCALL(ovp
, VOFFSET(vnop_mmap
), ap
));
847 struct vnop_fsync_args
/* {
850 vfs_context_t a_context;
854 struct proc
*p
= vfs_context_proc(ap
->a_context
);
855 struct vnode
*targetvp
= OTHERVP(ap
->a_vp
);
857 if (targetvp
!= NULLVP
) {
858 int dolock
= (targetvp
== LOWERVP(ap
->a_vp
));
861 FIXUP(VTOUNION(ap
->a_vp
), p
);
862 error
= VNOP_FSYNC(targetvp
, ap
->a_waitfor
, ap
->a_context
);
870 struct vnop_remove_args
/* {
873 struct componentname *a_cnp;
874 vfs_context_t a_context;
878 struct union_node
*dun
= VTOUNION(ap
->a_dvp
);
879 struct union_node
*un
= VTOUNION(ap
->a_vp
);
880 struct componentname
*cnp
= ap
->a_cnp
;
881 vfs_context_t ctx
= cnp
->cn_context
;
882 struct proc
*p
= vfs_context_proc(ctx
);
884 if (dun
->un_uppervp
== NULLVP
)
885 panic("union remove: null upper vnode");
887 if (un
->un_uppervp
!= NULLVP
) {
888 struct vnode
*dvp
= dun
->un_uppervp
;
889 struct vnode
*vp
= un
->un_uppervp
;
892 dun
->un_flags
|= UN_KLOCK
;
894 un
->un_flags
|= UN_KLOCK
;
896 if (union_dowhiteout(un
, cnp
->cn_context
))
897 cnp
->cn_flags
|= DOWHITEOUT
;
898 error
= VNOP_REMOVE(dvp
, vp
, cnp
, 0, ap
->a_context
);
900 union_removed_upper(un
);
903 error
= union_mkwhiteout(
904 MOUNTTOUNIONMOUNT(UNIONTOV(dun
)->v_mount
),
905 dun
->un_uppervp
, ap
->a_cnp
, un
->un_path
);
913 struct vnop_link_args
/* {
915 struct vnode *a_tdvp;
916 struct componentname *a_cnp;
917 vfs_context_t a_context;
921 struct componentname
*cnp
= ap
->a_cnp
;
922 vfs_context_t ctx
= cnp
->cn_context
;
923 struct proc
*p
= vfs_context_proc(ctx
);
924 struct union_node
*un
;
928 un
= VTOUNION(ap
->a_tdvp
);
930 if (ap
->a_tdvp
->v_op
!= ap
->a_vp
->v_op
) {
933 struct union_node
*tun
= VTOUNION(ap
->a_vp
);
934 if (tun
->un_uppervp
== NULLVP
) {
935 if (un
->un_uppervp
== tun
->un_dirvp
) {
936 un
->un_flags
&= ~UN_ULOCK
;
938 error
= union_copyup(tun
, 1, vfs_context_ucred(ctx
), p
);
939 if (un
->un_uppervp
== tun
->un_dirvp
) {
940 un
->un_flags
|= UN_ULOCK
;
943 vp
= tun
->un_uppervp
;
945 tdvp
= un
->un_uppervp
;
955 un
->un_flags
|= UN_KLOCK
;
957 return (VNOP_LINK(vp
, tdvp
, cnp
, ap
->a_context
));
962 struct vnop_rename_args
/* {
963 struct vnode *a_fdvp;
965 struct componentname *a_fcnp;
966 struct vnode *a_tdvp;
968 struct componentname *a_tcnp;
969 vfs_context_t a_context;
974 struct vnode
*fdvp
= ap
->a_fdvp
;
975 struct vnode
*fvp
= ap
->a_fvp
;
976 struct vnode
*tdvp
= ap
->a_tdvp
;
977 struct vnode
*tvp
= ap
->a_tvp
;
979 if (fdvp
->v_op
== union_vnodeop_p
) { /* always true */
980 struct union_node
*un
= VTOUNION(fdvp
);
981 if (un
->un_uppervp
== NULLVP
) {
983 * this should never happen in normal
984 * operation but might if there was
985 * a problem creating the top-level shadow
992 fdvp
= un
->un_uppervp
;
996 if (fvp
->v_op
== union_vnodeop_p
) { /* always true */
997 struct union_node
*un
= VTOUNION(fvp
);
998 if (un
->un_uppervp
== NULLVP
) {
999 /* XXX: should do a copyup */
1004 if (un
->un_lowervp
!= NULLVP
)
1005 ap
->a_fcnp
->cn_flags
|= DOWHITEOUT
;
1007 fvp
= un
->un_uppervp
;
1011 if (tdvp
->v_op
== union_vnodeop_p
) {
1012 struct union_node
*un
= VTOUNION(tdvp
);
1013 if (un
->un_uppervp
== NULLVP
) {
1015 * this should never happen in normal
1016 * operation but might if there was
1017 * a problem creating the top-level shadow
1024 tdvp
= un
->un_uppervp
;
1026 un
->un_flags
|= UN_KLOCK
;
1029 if (tvp
!= NULLVP
&& tvp
->v_op
== union_vnodeop_p
) {
1030 struct union_node
*un
= VTOUNION(tvp
);
1032 tvp
= un
->un_uppervp
;
1033 if (tvp
!= NULLVP
) {
1035 un
->un_flags
|= UN_KLOCK
;
1039 return (VNOP_RENAME(fdvp
, fvp
, ap
->a_fcnp
, tdvp
, tvp
, ap
->a_tcnp
, ap
->a_context
));
1047 struct vnop_mkdir_args
/* {
1048 struct vnode *a_dvp;
1049 struct vnode **a_vpp;
1050 struct componentname *a_cnp;
1051 struct vnode_attr *a_vap;
1052 vfs_context_t a_context;
1055 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
1056 struct vnode
*dvp
= un
->un_uppervp
;
1057 struct componentname
*cnp
= ap
->a_cnp
;
1058 vfs_context_t ctx
= cnp
->cn_context
;
1059 struct proc
*p
= vfs_context_proc(ctx
);
1061 if (dvp
!= NULLVP
) {
1066 un
->un_flags
|= UN_KLOCK
;
1068 /* note that this is a direct fallthrough to the filesystem */
1069 error
= VNOP_MKDIR(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_context
);
1073 error
= union_allocvp(ap
->a_vpp
, ap
->a_dvp
->v_mount
, ap
->a_dvp
,
1074 NULLVP
, cnp
, vp
, NULLVP
, 1);
1084 struct vnop_rmdir_args
/* {
1085 struct vnode *a_dvp;
1087 struct componentname *a_cnp;
1088 vfs_context_t a_context;
1092 struct union_node
*dun
= VTOUNION(ap
->a_dvp
);
1093 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1094 struct componentname
*cnp
= ap
->a_cnp
;
1095 vfs_context_t ctx
= cnp
->cn_context
;
1096 struct proc
*p
= vfs_context_proc(ctx
);
1098 if (dun
->un_uppervp
== NULLVP
)
1099 panic("union rmdir: null upper vnode");
1101 if (un
->un_uppervp
!= NULLVP
) {
1102 struct vnode
*dvp
= dun
->un_uppervp
;
1103 struct vnode
*vp
= un
->un_uppervp
;
1107 dun
->un_flags
|= UN_KLOCK
;
1110 un
->un_flags
|= UN_KLOCK
;
1112 if (union_dowhiteout(un
, cnp
->cn_context
))
1113 cnp
->cn_flags
|= DOWHITEOUT
;
1114 error
= VNOP_RMDIR(dvp
, vp
, ap
->a_cnp
, ap
->a_context
);
1116 union_removed_upper(un
);
1119 error
= union_mkwhiteout(
1120 MOUNTTOUNIONMOUNT(UNIONTOV(dun
)->v_mount
),
1121 dun
->un_uppervp
, ap
->a_cnp
, un
->un_path
);
1128 struct vnop_symlink_args
/* {
1129 struct vnode *a_dvp;
1130 struct vnode **a_vpp;
1131 struct componentname *a_cnp;
1132 struct vnode_attr *a_vap;
1134 vfs_context_t a_context;
1137 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
1138 struct vnode
*dvp
= un
->un_uppervp
;
1139 struct componentname
*cnp
= ap
->a_cnp
;
1140 vfs_context_t ctx
= cnp
->cn_context
;
1141 struct proc
*p
= vfs_context_proc(ctx
);
1143 if (dvp
!= NULLVP
) {
1148 un
->un_flags
|= UN_KLOCK
;
1150 error
= VNOP_SYMLINK(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_target
, ap
->a_context
);
1151 *ap
->a_vpp
= NULLVP
;
1158 * union_readdir works in concert with getdirentries and
1159 * readdir(3) to provide a list of entries in the unioned
1160 * directories. getdirentries is responsible for walking
1161 * down the union stack. readdir(3) is responsible for
1162 * eliminating duplicate names from the returned data stream.
1166 struct vnop_readdir_args
/* {
1167 struct vnodeop_desc *a_desc;
1173 vfs_context_t a_context;
1176 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1177 struct vnode
*uvp
= un
->un_uppervp
;
1178 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1180 if (ap
->a_flags
& (VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
))
1188 return (VCALL(uvp
, VOFFSET(vnop_readdir
), ap
));
1193 struct vnop_readlink_args
/* {
1196 vfs_context_t a_context;
1200 struct uio
*uio
= ap
->a_uio
;
1201 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1202 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1203 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
1206 FIXUP(VTOUNION(ap
->a_vp
), p
);
1208 error
= VCALL(vp
, VOFFSET(vnop_readlink
), ap
);
1215 struct vnop_inactive_args
/* {
1217 vfs_context_t a_context;
1220 struct vnode
*vp
= ap
->a_vp
;
1221 struct union_node
*un
= VTOUNION(vp
);
1225 * Do nothing (and _don't_ bypass).
1226 * Wait to vnode_put lowervp until reclaim,
1227 * so that until then our union_node is in the
1228 * cache and reusable.
1230 * NEEDSWORK: Someday, consider inactive'ing
1231 * the lowervp and then trying to reactivate it
1232 * with capabilities (v_id)
1233 * like they do in the name lookup cache code.
1234 * That's too much work for now.
1237 if (un
->un_dircache
!= 0) {
1238 for (vpp
= un
->un_dircache
; *vpp
!= NULLVP
; vpp
++)
1240 _FREE(un
->un_dircache
, M_TEMP
);
1241 un
->un_dircache
= 0;
1244 if ((un
->un_flags
& UN_CACHED
) == 0)
1252 struct vnop_reclaim_args
/* {
1254 vfs_context_t a_context;
1258 union_freevp(ap
->a_vp
);
1265 struct vnop_blockmap_args
/* {
1276 struct proc
*p
= current_proc(); /* XXX */
1277 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1278 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
1281 FIXUP(VTOUNION(ap
->a_vp
), p
);
1283 error
= VCALL(vp
, VOFFSET(vnop_blockmap
), ap
);
1290 struct vnop_pathconf_args
/* {
1294 vfs_context_t a_context;
1298 struct proc
*p
= current_proc(); /* XXX */
1299 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1300 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
1303 FIXUP(VTOUNION(ap
->a_vp
), p
);
1305 error
= VCALL(vp
, VOFFSET(vnop_pathconf
), ap
);
1312 struct vnop_advlock_args
/* {
1318 vfs_context_t a_context;
1321 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
1324 return (VCALL(ovp
, VOFFSET(vnop_advlock
), ap
));
1329 * XXX - vnop_strategy must be hand coded because it has no
1330 * vnode in its arguments.
1331 * This goes away with a merged VM/buffer cache.
1335 struct vnop_strategy_args
/* {
1339 struct buf
*bp
= ap
->a_bp
;
1341 struct vnode
*savedvp
;
1343 savedvp
= buf_vnode(bp
);
1344 buf_setvnode(bp
, OTHERVP(savedvp
));
1347 if (buf_vnode(bp
) == NULLVP
)
1348 panic("union_strategy: nil vp");
1349 if (((buf_flags(bp
) & B_READ
) == 0) &&
1350 (buf_vnode(bp
) == LOWERVP(savedvp
)))
1351 panic("union_strategy: writing to lowervp");
1354 error
= VNOP_STRATEGY(bp
);
1355 buf_setvnode(bp
, savedvp
);
1363 struct vnop_pagein_args
/* {
1366 vm_offset_t a_pl_offset,
1370 vfs_context_t a_context;
1374 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1376 error
= VNOP_PAGEIN(vp
, ap
->a_pl
, ap
->a_pl_offset
, ap
->a_f_offset
,
1377 ap
->a_size
, ap
->a_flags
, ap
->a_context
);
1381 * perhaps the size of the underlying object has changed under
1382 * our feet. take advantage of the offset information present
1383 * in the uio structure.
1386 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1387 off_t cur
= ap
->a_f_offset
+ (off_t
)ap
->a_pl_offset
;
1389 if (vp
== un
->un_uppervp
) {
1390 if (cur
> un
->un_uppersz
)
1391 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
1393 if (cur
> un
->un_lowersz
)
1394 union_newsize(ap
->a_vp
, VNOVAL
, cur
);
1404 struct vnop_pageout_args
/* {
1407 vm_offset_t a_pl_offset,
1411 vfs_context_t a_context;
1416 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1418 vp
= UPPERVP(ap
->a_vp
);
1420 panic("union: missing upper layer in pageout");
1422 error
= VNOP_PAGEOUT(vp
, ap
->a_pl
, ap
->a_pl_offset
, ap
->a_f_offset
,
1423 ap
->a_size
, ap
->a_flags
, ap
->a_context
);
1426 * the size of the underlying object may be changed by the
1430 off_t cur
= ap
->a_f_offset
+ (off_t
)ap
->a_pl_offset
;
1432 if (cur
> un
->un_uppersz
)
1433 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
1439 /* Blktooff derives file offset for the given logical block number */
1442 struct vnop_blktooff_args
/* {
1449 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1451 error
= VNOP_BLKTOOFF(vp
, ap
->a_lblkno
, ap
->a_offset
);
1456 /* offtoblk derives file offset for the given logical block number */
1459 struct vnop_offtoblk_args
/* {
1462 daddr64_t *a_lblkno;
1466 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1468 error
= VNOP_OFFTOBLK(vp
, ap
->a_offset
, ap
->a_lblkno
);
1473 #define VOPFUNC int (*)(void *)
1476 * Global vfs data structures
1478 int (**union_vnodeop_p
)(void *);
1479 struct vnodeopv_entry_desc union_vnodeop_entries
[] = {
1480 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1481 { &vnop_lookup_desc
, (VOPFUNC
)union_lookup
}, /* lookup */
1482 { &vnop_create_desc
, (VOPFUNC
)union_create
}, /* create */
1483 { &vnop_whiteout_desc
, (VOPFUNC
)union_whiteout
}, /* whiteout */
1484 { &vnop_mknod_desc
, (VOPFUNC
)union_mknod
}, /* mknod */
1485 { &vnop_open_desc
, (VOPFUNC
)union_open
}, /* open */
1486 { &vnop_close_desc
, (VOPFUNC
)union_close
}, /* close */
1487 { &vnop_access_desc
, (VOPFUNC
)union_access
}, /* access */
1488 { &vnop_getattr_desc
, (VOPFUNC
)union_getattr
}, /* getattr */
1489 { &vnop_setattr_desc
, (VOPFUNC
)union_setattr
}, /* setattr */
1490 { &vnop_read_desc
, (VOPFUNC
)union_read
}, /* read */
1491 { &vnop_write_desc
, (VOPFUNC
)union_write
}, /* write */
1492 { &vnop_ioctl_desc
, (VOPFUNC
)union_ioctl
}, /* ioctl */
1493 { &vnop_select_desc
, (VOPFUNC
)union_select
}, /* select */
1494 { &vnop_revoke_desc
, (VOPFUNC
)union_revoke
}, /* revoke */
1495 { &vnop_mmap_desc
, (VOPFUNC
)union_mmap
}, /* mmap */
1496 { &vnop_fsync_desc
, (VOPFUNC
)union_fsync
}, /* fsync */
1497 { &vnop_remove_desc
, (VOPFUNC
)union_remove
}, /* remove */
1498 { &vnop_link_desc
, (VOPFUNC
)union_link
}, /* link */
1499 { &vnop_rename_desc
, (VOPFUNC
)union_rename
}, /* rename */
1500 { &vnop_mkdir_desc
, (VOPFUNC
)union_mkdir
}, /* mkdir */
1501 { &vnop_rmdir_desc
, (VOPFUNC
)union_rmdir
}, /* rmdir */
1502 { &vnop_symlink_desc
, (VOPFUNC
)union_symlink
}, /* symlink */
1503 { &vnop_readdir_desc
, (VOPFUNC
)union_readdir
}, /* readdir */
1504 { &vnop_readlink_desc
, (VOPFUNC
)union_readlink
}, /* readlink */
1505 { &vnop_inactive_desc
, (VOPFUNC
)union_inactive
}, /* inactive */
1506 { &vnop_reclaim_desc
, (VOPFUNC
)union_reclaim
}, /* reclaim */
1507 { &vnop_strategy_desc
, (VOPFUNC
)union_strategy
}, /* strategy */
1508 { &vnop_pathconf_desc
, (VOPFUNC
)union_pathconf
}, /* pathconf */
1509 { &vnop_advlock_desc
, (VOPFUNC
)union_advlock
}, /* advlock */
1511 { &vnop_bwrite_desc
, (VOPFUNC
)union_bwrite
}, /* bwrite */
1513 { &vnop_pagein_desc
, (VOPFUNC
)union_pagein
}, /* Pagein */
1514 { &vnop_pageout_desc
, (VOPFUNC
)union_pageout
}, /* Pageout */
1515 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1516 { &vnop_blktooff_desc
, (VOPFUNC
)union_blktooff
}, /* blktooff */
1517 { &vnop_offtoblk_desc
, (VOPFUNC
)union_offtoblk
}, /* offtoblk */
1518 { &vnop_blockmap_desc
, (VOPFUNC
)union_blockmap
}, /* blockmap */
1519 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1521 struct vnodeopv_desc union_vnodeop_opv_desc
=
1522 { &union_vnodeop_p
, union_vnodeop_entries
};