2 * Copyright (c) 2000 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) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
26 * Copyright (c) 1992, 1993, 1994, 1995
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_vnops.c 8.32 (Berkeley) 6/23/95
63 #include <sys/param.h>
64 #include <sys/systm.h>
66 #include <sys/kauth.h>
70 #include <sys/types.h>
71 #include <sys/vnode_internal.h>
72 #include <sys/mount_internal.h>
73 #include <sys/namei.h>
74 #include <sys/malloc.h>
75 #include <sys/buf_internal.h>
76 #include <sys/queue.h>
78 #include <miscfs/union/union.h>
79 #include <vfs/vfs_support.h>
81 #include <sys/uio_internal.h>
83 #define FIXUP(un, p) { \
84 if (((un)->un_flags & UN_ULOCK) == 0) { \
91 struct union_node
*un
;
95 un
->un_flags
|= UN_ULOCK
;
99 union_lookup1(struct vnode
*udvp
, struct vnode
**dvpp
, struct vnode
**vpp
,
100 struct componentname
*cnp
)
103 vfs_context_t ctx
= cnp
->cn_context
;
111 * If stepping up the directory tree, check for going
112 * back across the mount point, in which case do what
113 * lookup would do by stepping back down the mount
116 if (cnp
->cn_flags
& ISDOTDOT
) {
117 while ((dvp
!= udvp
) && (dvp
->v_flag
& VROOT
)) {
119 * Don't do the NOCROSSMOUNT check
120 * at this level. By definition,
121 * union fs deals with namespaces, not
125 *dvpp
= dvp
= dvp
->v_mount
->mnt_vnodecovered
;
131 error
= VNOP_LOOKUP(dvp
, &tdvp
, cnp
, ctx
);
137 * Lastly check if the current node is a mount point in
138 * which case walk up the mount hierarchy making sure not to
139 * bump into the root of the mount tree (ie. dvp != udvp).
141 while (dvp
!= udvp
&& (dvp
->v_type
== VDIR
) &&
142 (mp
= dvp
->v_mountedhere
)) {
143 if (vfs_busy(mp
, LK_NOWAIT
)) {
147 error
= VFS_ROOT(mp
, &tdvp
, ctx
);
164 struct vnop_lookup_args
/* {
165 struct vnodeop_desc *a_desc;
167 struct vnode **a_vpp;
168 struct componentname *a_cnp;
169 vfs_context_t a_context;
174 struct vnode
*uppervp
, *lowervp
;
175 struct vnode
*upperdvp
, *lowerdvp
;
176 struct vnode
*dvp
= ap
->a_dvp
;
177 struct union_node
*dun
= VTOUNION(dvp
);
178 struct componentname
*cnp
= ap
->a_cnp
;
179 vfs_context_t ctx
= cnp
->cn_context
;
180 struct proc
*p
= vfs_context_proc(ctx
);
181 int lockparent
= cnp
->cn_flags
& LOCKPARENT
;
182 struct union_mount
*um
= MOUNTTOUNIONMOUNT(dvp
->v_mount
);
183 kauth_cred_t saved_cred
;
185 struct vnode_attr va
;
188 if (cnp
->cn_namelen
== 3 &&
189 cnp
->cn_nameptr
[2] == '.' &&
190 cnp
->cn_nameptr
[1] == '.' &&
191 cnp
->cn_nameptr
[0] == '.') {
192 dvp
= *ap
->a_vpp
= LOWERVP(ap
->a_dvp
);
201 cnp
->cn_flags
|= LOCKPARENT
;
203 upperdvp
= dun
->un_uppervp
;
204 lowerdvp
= dun
->un_lowervp
;
210 * do the lookup in the upper level.
211 * if that level comsumes additional pathnames,
212 * then assume that something special is going
213 * on and just return that vnode.
215 if (upperdvp
!= NULLVP
) {
217 uerror
= union_lookup1(um
->um_uppervp
, &upperdvp
,
219 /*if (uppervp == upperdvp)
220 dun->un_flags |= UN_KLOCK;*/
222 if (cnp
->cn_consume
!= 0) {
223 *ap
->a_vpp
= uppervp
;
225 cnp
->cn_flags
&= ~LOCKPARENT
;
228 if (uerror
== ENOENT
|| uerror
== EJUSTRETURN
) {
229 if (cnp
->cn_flags
& ISWHITEOUT
) {
231 } else if (lowerdvp
!= NULLVP
) {
233 VATTR_WANTED(&va
, va_flags
);
234 lerror
= vnode_getattr(upperdvp
, &va
, ap
->a_context
);
235 if (lerror
== 0 && (va
.va_flags
& OPAQUE
))
244 * in a similar way to the upper layer, do the lookup
245 * in the lower layer. this time, if there is some
246 * component magic going on, then vnode_put whatever we got
247 * back from the upper layer and return the lower vnode
250 if (lowerdvp
!= NULLVP
&& !iswhiteout
) {
254 * Only do a LOOKUP on the bottom node, since
255 * we won't be making changes to it anyway.
257 nameiop
= cnp
->cn_nameiop
;
258 cnp
->cn_nameiop
= LOOKUP
;
259 if (um
->um_op
== UNMNT_BELOW
) {
261 saved_cred
= cnp
->cn_context
->vc_ucred
;
262 cnp
->cn_context
->vc_ucred
= um
->um_cred
;
263 lerror
= union_lookup1(um
->um_lowervp
, &lowerdvp
,
265 cnp
->cn_context
->vc_ucred
= saved_cred
;
267 lerror
= union_lookup1(um
->um_lowervp
, &lowerdvp
,
270 cnp
->cn_nameiop
= nameiop
;
272 if (cnp
->cn_consume
!= 0) {
273 if (uppervp
!= NULLVP
) {
277 *ap
->a_vpp
= lowervp
;
279 cnp
->cn_flags
&= ~LOCKPARENT
;
284 if ((cnp
->cn_flags
& ISDOTDOT
) && dun
->un_pvp
!= NULLVP
) {
285 lowervp
= LOWERVP(dun
->un_pvp
);
286 if (lowervp
!= NULLVP
) {
294 cnp
->cn_flags
&= ~LOCKPARENT
;
297 * at this point, we have uerror and lerror indicating
298 * possible errors with the lookups in the upper and lower
299 * layers. additionally, uppervp and lowervp are (locked)
300 * references to existing vnodes in the upper and lower layers.
302 * there are now three cases to consider.
303 * 1. if both layers returned an error, then return whatever
304 * error the upper layer generated.
306 * 2. if the top layer failed and the bottom layer succeeded
307 * then two subcases occur.
308 * a. the bottom vnode is not a directory, in which
309 * case just return a new union vnode referencing
310 * an empty top layer and the existing bottom layer.
311 * b. the bottom vnode is a directory, in which case
312 * create a new directory in the top-level and
313 * continue as in case 3.
315 * 3. if the top layer succeeded then return a new union
316 * vnode referencing whatever the new top layer and
317 * whatever the bottom layer returned.
323 if ((uerror
!= 0) && (lerror
!= 0)) {
328 if (uerror
!= 0 /* && (lerror == 0) */ ) {
329 if (lowervp
->v_type
== VDIR
) { /* case 2b. */
330 dun
->un_flags
&= ~UN_ULOCK
;
331 uerror
= union_mkshadow(um
, upperdvp
, cnp
, &uppervp
);
332 dun
->un_flags
|= UN_ULOCK
;
335 if (lowervp
!= NULLVP
) {
343 error
= union_allocvp(ap
->a_vpp
, dvp
->v_mount
, dvp
, upperdvp
, cnp
,
344 uppervp
, lowervp
, 1);
347 if (uppervp
!= NULLVP
)
349 if (lowervp
!= NULLVP
)
358 struct vnop_create_args
/* {
360 struct vnode **a_vpp;
361 struct componentname *a_cnp;
362 struct vnode_attr *a_vap;
363 vfs_context_t a_context;
366 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
367 struct vnode
*dvp
= un
->un_uppervp
;
368 struct componentname
*cnp
= ap
->a_cnp
;
369 vfs_context_t ctx
= cnp
->cn_context
;
370 struct proc
*p
= vfs_context_proc(ctx
);
379 un
->un_flags
|= UN_KLOCK
;
380 mp
= ap
->a_dvp
->v_mount
;
382 /* note that this is a direct passthrough to the filesystem */
383 error
= VNOP_CREATE(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_context
);
387 error
= union_allocvp(ap
->a_vpp
, mp
, NULLVP
, NULLVP
, cnp
, vp
,
398 struct vnop_whiteout_args
/* {
400 struct componentname *a_cnp;
402 vfs_context_t a_context;
405 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
406 struct componentname
*cnp
= ap
->a_cnp
;
407 vfs_context_t ctx
= cnp
->cn_context
;
408 struct proc
*p
= vfs_context_proc(ctx
);
410 if (un
->un_uppervp
== NULLVP
)
414 return (VNOP_WHITEOUT(un
->un_uppervp
, cnp
, ap
->a_flags
, ap
->a_context
));
419 struct vnop_mknod_args
/* {
421 struct vnode **a_vpp;
422 struct componentname *a_cnp;
423 struct vnode_attr *a_vap;
424 vfs_context_t a_context;
427 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
428 struct vnode
*dvp
= un
->un_uppervp
;
429 struct componentname
*cnp
= ap
->a_cnp
;
430 vfs_context_t ctx
= cnp
->cn_context
;
431 struct proc
*p
= vfs_context_proc(ctx
);
440 un
->un_flags
|= UN_KLOCK
;
441 mp
= ap
->a_dvp
->v_mount
;
443 /* note that this is a direct passthrough to the filesystem */
444 error
= VNOP_MKNOD(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_context
);
449 error
= union_allocvp(ap
->a_vpp
, mp
, NULLVP
, NULLVP
,
461 struct vnop_open_args
/* {
462 struct vnodeop_desc *a_desc;
465 vfs_context_t a_context;
468 struct union_node
*un
= VTOUNION(ap
->a_vp
);
470 int mode
= ap
->a_mode
;
471 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
472 struct proc
*p
= vfs_context_proc(ap
->a_context
);
476 * If there is an existing upper vp then simply open that.
478 tvp
= un
->un_uppervp
;
481 * If the lower vnode is being opened for writing, then
482 * copy the file contents to the upper vnode and open that,
483 * otherwise can simply open the lower vnode.
485 tvp
= un
->un_lowervp
;
486 if ((ap
->a_mode
& FWRITE
) && (tvp
->v_type
== VREG
)) {
487 error
= union_copyup(un
, (mode
&O_TRUNC
) == 0, cred
, p
);
489 error
= VNOP_OPEN(un
->un_uppervp
, mode
, ap
->a_context
);
494 * Just open the lower vnode
498 error
= VNOP_OPEN(tvp
, mode
, ap
->a_context
);
505 error
= VNOP_OPEN(tvp
, mode
, ap
->a_context
);
512 struct vnop_close_args
/* {
515 vfs_context_t a_context;
518 struct union_node
*un
= VTOUNION(ap
->a_vp
);
521 if ((vp
= un
->un_uppervp
) == NULLVP
) {
522 #ifdef UNION_DIAGNOSTIC
523 if (un
->un_openl
<= 0)
524 panic("union: un_openl cnt");
531 return (VCALL(vp
, VOFFSET(vnop_close
), ap
));
535 * Check access permission on the union vnode.
536 * The access check being enforced is to check
537 * against both the underlying vnode, and any
538 * copied vnode. This ensures that no additional
539 * file permissions are given away simply because
540 * the user caused an implicit file copy.
544 struct vnop_access_args
/* {
545 struct vnodeop_desc *a_desc;
548 vfs_context_t a_context;
551 struct union_node
*un
= VTOUNION(ap
->a_vp
);
552 struct proc
*p
= vfs_context_proc(ap
->a_context
);
556 if ((vp
= un
->un_uppervp
) != NULLVP
) {
559 return (VCALL(vp
, VOFFSET(vnop_access
), ap
));
562 if ((vp
= un
->un_lowervp
) != NULLVP
) {
564 error
= VCALL(vp
, VOFFSET(vnop_access
), ap
);
566 struct union_mount
*um
= MOUNTTOUNIONMOUNT(vp
->v_mount
);
568 if (um
->um_op
== UNMNT_BELOW
) {
570 // ap->a_cred = um->um_cred;
571 error
= VCALL(vp
, VOFFSET(vnop_access
), ap
);
582 * We handle getattr only to change the fsid and
587 struct vnop_getattr_args
/* {
589 struct vnode_attr *a_vap;
590 vfs_context_t a_context;
594 struct union_node
*un
= VTOUNION(ap
->a_vp
);
595 struct vnode
*vp
= un
->un_uppervp
;
596 struct proc
*p
= vfs_context_proc(ap
->a_context
);
597 struct vnode_attr
*vap
;
598 struct vnode_attr va
;
602 * Some programs walk the filesystem hierarchy by counting
603 * links to directories to avoid stat'ing all the time.
604 * This means the link count on directories needs to be "correct".
605 * The only way to do that is to call getattr on both layers
606 * and fix up the link count. The link count will not necessarily
607 * be accurate but will be large enough to defeat the tree walkers.
615 * It's not clear whether vnop_getattr is to be
616 * called with the vnode locked or not. stat() calls
617 * it with (vp) locked, and fstat calls it with
619 * In the mean time, compensate here by checking
620 * the union_node's lock flag.
622 if (un
->un_flags
& UN_LOCKED
)
625 error
= vnode_getattr(vp
, vap
, ap
->a_context
);
628 union_newsize(ap
->a_vp
, vap
->va_data_size
, VNOVAL
);
633 } else if (vp
->v_type
== VDIR
) {
636 /* all we want from the lower node is the link count */
637 VATTR_WANTED(&va
, va_nlink
);
644 error
= vnode_getattr(vp
, vap
, ap
->a_context
);
647 union_newsize(ap
->a_vp
, VNOVAL
, vap
->va_data_size
);
650 if ((vap
!= ap
->a_vap
) && (vap
->va_type
== VDIR
))
651 ap
->a_vap
->va_nlink
+= vap
->va_nlink
;
653 VATTR_RETURN(ap
->a_vap
, va_fsid
, ap
->a_vp
->v_mount
->mnt_vfsstat
.f_fsid
.val
[0]);
659 struct vnop_setattr_args
/* {
661 struct vnode_attr *a_vap;
662 vfs_context_t a_context;
665 struct union_node
*un
= VTOUNION(ap
->a_vp
);
666 struct proc
*p
= vfs_context_proc(ap
->a_context
);
667 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
671 * Handle case of truncating lower object to zero size,
672 * by creating a zero length upper object. This is to
673 * handle the case of open with O_TRUNC and O_CREAT.
675 if (VATTR_IS_ACTIVE(ap
->a_vap
, va_data_size
) &&
676 (un
->un_uppervp
== NULLVP
) &&
677 /* assert(un->un_lowervp != NULLVP) */
678 (un
->un_lowervp
->v_type
== VREG
)) {
679 error
= union_copyup(un
, (ap
->a_vap
->va_data_size
!= 0), cred
, p
);
685 * Try to set attributes in upper layer,
686 * otherwise return read-only filesystem error.
688 if (un
->un_uppervp
!= NULLVP
) {
690 error
= vnode_setattr(un
->un_uppervp
, ap
->a_vap
, ap
->a_context
);
691 if ((error
== 0) && VATTR_IS_ACTIVE(ap
->a_vap
, va_data_size
))
692 union_newsize(ap
->a_vp
, ap
->a_vap
->va_data_size
, VNOVAL
);
702 struct vnop_read_args
/* {
706 vfs_context_t a_context;
710 struct proc
*p
= vfs_context_proc(ap
->a_context
);
711 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
712 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
715 FIXUP(VTOUNION(ap
->a_vp
), p
);
716 error
= VNOP_READ(vp
, ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
);
720 * perhaps the size of the underlying object has changed under
721 * our feet. take advantage of the offset information present
722 * in the uio structure.
725 struct union_node
*un
= VTOUNION(ap
->a_vp
);
726 off_t cur
= ap
->a_uio
->uio_offset
;
728 if (vp
== un
->un_uppervp
) {
729 if (cur
> un
->un_uppersz
)
730 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
732 if (cur
> un
->un_lowersz
)
733 union_newsize(ap
->a_vp
, VNOVAL
, cur
);
742 struct vnop_read_args
/* {
746 vfs_context_t a_context;
751 struct union_node
*un
= VTOUNION(ap
->a_vp
);
752 struct proc
*p
= vfs_context_proc(ap
->a_context
);
754 vp
= UPPERVP(ap
->a_vp
);
756 panic("union: missing upper layer in write");
759 error
= VNOP_WRITE(vp
, ap
->a_uio
, ap
->a_ioflag
, ap
->a_context
);
762 * the size of the underlying object may be changed by the
766 off_t cur
= ap
->a_uio
->uio_offset
;
768 if (cur
> un
->un_uppersz
)
769 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
778 struct vnop_ioctl_args
/* {
783 vfs_context_t a_context;
786 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
789 return (VCALL(ovp
, VOFFSET(vnop_ioctl
), ap
));
794 struct vnop_select_args
/* {
799 vfs_context_t a_context;
802 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
805 return (VCALL(ovp
, VOFFSET(vnop_select
), ap
));
810 struct vnop_revoke_args
/* {
813 vfs_context_t a_context;
816 struct vnode
*vp
= ap
->a_vp
;
819 VNOP_REVOKE(UPPERVP(vp
), ap
->a_flags
, ap
->a_context
);
821 VNOP_REVOKE(LOWERVP(vp
), ap
->a_flags
, ap
->a_context
);
827 struct vnop_mmap_args
/* {
834 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
837 return (VCALL(ovp
, VOFFSET(vnop_mmap
), ap
));
842 struct vnop_fsync_args
/* {
845 vfs_context_t a_context;
849 struct proc
*p
= vfs_context_proc(ap
->a_context
);
850 struct vnode
*targetvp
= OTHERVP(ap
->a_vp
);
852 if (targetvp
!= NULLVP
) {
853 int dolock
= (targetvp
== LOWERVP(ap
->a_vp
));
856 FIXUP(VTOUNION(ap
->a_vp
), p
);
857 error
= VNOP_FSYNC(targetvp
, ap
->a_waitfor
, ap
->a_context
);
865 struct vnop_remove_args
/* {
868 struct componentname *a_cnp;
869 vfs_context_t a_context;
873 struct union_node
*dun
= VTOUNION(ap
->a_dvp
);
874 struct union_node
*un
= VTOUNION(ap
->a_vp
);
875 struct componentname
*cnp
= ap
->a_cnp
;
876 vfs_context_t ctx
= cnp
->cn_context
;
877 struct proc
*p
= vfs_context_proc(ctx
);
879 if (dun
->un_uppervp
== NULLVP
)
880 panic("union remove: null upper vnode");
882 if (un
->un_uppervp
!= NULLVP
) {
883 struct vnode
*dvp
= dun
->un_uppervp
;
884 struct vnode
*vp
= un
->un_uppervp
;
887 dun
->un_flags
|= UN_KLOCK
;
889 un
->un_flags
|= UN_KLOCK
;
891 if (union_dowhiteout(un
, cnp
->cn_context
))
892 cnp
->cn_flags
|= DOWHITEOUT
;
893 error
= VNOP_REMOVE(dvp
, vp
, cnp
, 0, ap
->a_context
);
895 union_removed_upper(un
);
898 error
= union_mkwhiteout(
899 MOUNTTOUNIONMOUNT(UNIONTOV(dun
)->v_mount
),
900 dun
->un_uppervp
, ap
->a_cnp
, un
->un_path
);
908 struct vnop_link_args
/* {
910 struct vnode *a_tdvp;
911 struct componentname *a_cnp;
912 vfs_context_t a_context;
916 struct componentname
*cnp
= ap
->a_cnp
;
917 vfs_context_t ctx
= cnp
->cn_context
;
918 struct proc
*p
= vfs_context_proc(ctx
);
919 struct union_node
*un
;
923 un
= VTOUNION(ap
->a_tdvp
);
925 if (ap
->a_tdvp
->v_op
!= ap
->a_vp
->v_op
) {
928 struct union_node
*tun
= VTOUNION(ap
->a_vp
);
929 if (tun
->un_uppervp
== NULLVP
) {
930 if (un
->un_uppervp
== tun
->un_dirvp
) {
931 un
->un_flags
&= ~UN_ULOCK
;
933 error
= union_copyup(tun
, 1, vfs_context_ucred(ctx
), p
);
934 if (un
->un_uppervp
== tun
->un_dirvp
) {
935 un
->un_flags
|= UN_ULOCK
;
938 vp
= tun
->un_uppervp
;
940 tdvp
= un
->un_uppervp
;
950 un
->un_flags
|= UN_KLOCK
;
952 return (VNOP_LINK(vp
, tdvp
, cnp
, ap
->a_context
));
957 struct vnop_rename_args
/* {
958 struct vnode *a_fdvp;
960 struct componentname *a_fcnp;
961 struct vnode *a_tdvp;
963 struct componentname *a_tcnp;
964 vfs_context_t a_context;
969 struct vnode
*fdvp
= ap
->a_fdvp
;
970 struct vnode
*fvp
= ap
->a_fvp
;
971 struct vnode
*tdvp
= ap
->a_tdvp
;
972 struct vnode
*tvp
= ap
->a_tvp
;
974 if (fdvp
->v_op
== union_vnodeop_p
) { /* always true */
975 struct union_node
*un
= VTOUNION(fdvp
);
976 if (un
->un_uppervp
== NULLVP
) {
978 * this should never happen in normal
979 * operation but might if there was
980 * a problem creating the top-level shadow
987 fdvp
= un
->un_uppervp
;
991 if (fvp
->v_op
== union_vnodeop_p
) { /* always true */
992 struct union_node
*un
= VTOUNION(fvp
);
993 if (un
->un_uppervp
== NULLVP
) {
994 /* XXX: should do a copyup */
999 if (un
->un_lowervp
!= NULLVP
)
1000 ap
->a_fcnp
->cn_flags
|= DOWHITEOUT
;
1002 fvp
= un
->un_uppervp
;
1006 if (tdvp
->v_op
== union_vnodeop_p
) {
1007 struct union_node
*un
= VTOUNION(tdvp
);
1008 if (un
->un_uppervp
== NULLVP
) {
1010 * this should never happen in normal
1011 * operation but might if there was
1012 * a problem creating the top-level shadow
1019 tdvp
= un
->un_uppervp
;
1021 un
->un_flags
|= UN_KLOCK
;
1024 if (tvp
!= NULLVP
&& tvp
->v_op
== union_vnodeop_p
) {
1025 struct union_node
*un
= VTOUNION(tvp
);
1027 tvp
= un
->un_uppervp
;
1028 if (tvp
!= NULLVP
) {
1030 un
->un_flags
|= UN_KLOCK
;
1034 return (VNOP_RENAME(fdvp
, fvp
, ap
->a_fcnp
, tdvp
, tvp
, ap
->a_tcnp
, ap
->a_context
));
1042 struct vnop_mkdir_args
/* {
1043 struct vnode *a_dvp;
1044 struct vnode **a_vpp;
1045 struct componentname *a_cnp;
1046 struct vnode_attr *a_vap;
1047 vfs_context_t a_context;
1050 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
1051 struct vnode
*dvp
= un
->un_uppervp
;
1052 struct componentname
*cnp
= ap
->a_cnp
;
1053 vfs_context_t ctx
= cnp
->cn_context
;
1054 struct proc
*p
= vfs_context_proc(ctx
);
1056 if (dvp
!= NULLVP
) {
1061 un
->un_flags
|= UN_KLOCK
;
1063 /* note that this is a direct fallthrough to the filesystem */
1064 error
= VNOP_MKDIR(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_context
);
1068 error
= union_allocvp(ap
->a_vpp
, ap
->a_dvp
->v_mount
, ap
->a_dvp
,
1069 NULLVP
, cnp
, vp
, NULLVP
, 1);
1079 struct vnop_rmdir_args
/* {
1080 struct vnode *a_dvp;
1082 struct componentname *a_cnp;
1083 vfs_context_t a_context;
1087 struct union_node
*dun
= VTOUNION(ap
->a_dvp
);
1088 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1089 struct componentname
*cnp
= ap
->a_cnp
;
1090 vfs_context_t ctx
= cnp
->cn_context
;
1091 struct proc
*p
= vfs_context_proc(ctx
);
1093 if (dun
->un_uppervp
== NULLVP
)
1094 panic("union rmdir: null upper vnode");
1096 if (un
->un_uppervp
!= NULLVP
) {
1097 struct vnode
*dvp
= dun
->un_uppervp
;
1098 struct vnode
*vp
= un
->un_uppervp
;
1102 dun
->un_flags
|= UN_KLOCK
;
1105 un
->un_flags
|= UN_KLOCK
;
1107 if (union_dowhiteout(un
, cnp
->cn_context
))
1108 cnp
->cn_flags
|= DOWHITEOUT
;
1109 error
= VNOP_RMDIR(dvp
, vp
, ap
->a_cnp
, ap
->a_context
);
1111 union_removed_upper(un
);
1114 error
= union_mkwhiteout(
1115 MOUNTTOUNIONMOUNT(UNIONTOV(dun
)->v_mount
),
1116 dun
->un_uppervp
, ap
->a_cnp
, un
->un_path
);
1123 struct vnop_symlink_args
/* {
1124 struct vnode *a_dvp;
1125 struct vnode **a_vpp;
1126 struct componentname *a_cnp;
1127 struct vnode_attr *a_vap;
1129 vfs_context_t a_context;
1132 struct union_node
*un
= VTOUNION(ap
->a_dvp
);
1133 struct vnode
*dvp
= un
->un_uppervp
;
1134 struct componentname
*cnp
= ap
->a_cnp
;
1135 vfs_context_t ctx
= cnp
->cn_context
;
1136 struct proc
*p
= vfs_context_proc(ctx
);
1138 if (dvp
!= NULLVP
) {
1143 un
->un_flags
|= UN_KLOCK
;
1145 error
= VNOP_SYMLINK(dvp
, &vp
, cnp
, ap
->a_vap
, ap
->a_target
, ap
->a_context
);
1146 *ap
->a_vpp
= NULLVP
;
1153 * union_readdir works in concert with getdirentries and
1154 * readdir(3) to provide a list of entries in the unioned
1155 * directories. getdirentries is responsible for walking
1156 * down the union stack. readdir(3) is responsible for
1157 * eliminating duplicate names from the returned data stream.
1161 struct vnop_readdir_args
/* {
1162 struct vnodeop_desc *a_desc;
1168 vfs_context_t a_context;
1171 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1172 struct vnode
*uvp
= un
->un_uppervp
;
1173 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1175 if (ap
->a_flags
& (VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
))
1183 return (VCALL(uvp
, VOFFSET(vnop_readdir
), ap
));
1188 struct vnop_readlink_args
/* {
1191 vfs_context_t a_context;
1195 struct uio
*uio
= ap
->a_uio
;
1196 struct proc
*p
= vfs_context_proc(ap
->a_context
);
1197 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1198 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
1201 FIXUP(VTOUNION(ap
->a_vp
), p
);
1203 error
= VCALL(vp
, VOFFSET(vnop_readlink
), ap
);
1210 struct vnop_inactive_args
/* {
1212 vfs_context_t a_context;
1215 struct vnode
*vp
= ap
->a_vp
;
1216 struct union_node
*un
= VTOUNION(vp
);
1220 * Do nothing (and _don't_ bypass).
1221 * Wait to vnode_put lowervp until reclaim,
1222 * so that until then our union_node is in the
1223 * cache and reusable.
1225 * NEEDSWORK: Someday, consider inactive'ing
1226 * the lowervp and then trying to reactivate it
1227 * with capabilities (v_id)
1228 * like they do in the name lookup cache code.
1229 * That's too much work for now.
1232 if (un
->un_dircache
!= 0) {
1233 for (vpp
= un
->un_dircache
; *vpp
!= NULLVP
; vpp
++)
1235 _FREE(un
->un_dircache
, M_TEMP
);
1236 un
->un_dircache
= 0;
1239 if ((un
->un_flags
& UN_CACHED
) == 0)
1247 struct vnop_reclaim_args
/* {
1249 vfs_context_t a_context;
1253 union_freevp(ap
->a_vp
);
1260 struct vnop_blockmap_args
/* {
1271 struct proc
*p
= current_proc(); /* XXX */
1272 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1273 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
1276 FIXUP(VTOUNION(ap
->a_vp
), p
);
1278 error
= VCALL(vp
, VOFFSET(vnop_blockmap
), ap
);
1285 struct vnop_pathconf_args
/* {
1289 vfs_context_t a_context;
1293 struct proc
*p
= current_proc(); /* XXX */
1294 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1295 int dolock
= (vp
== LOWERVP(ap
->a_vp
));
1298 FIXUP(VTOUNION(ap
->a_vp
), p
);
1300 error
= VCALL(vp
, VOFFSET(vnop_pathconf
), ap
);
1307 struct vnop_advlock_args
/* {
1313 vfs_context_t a_context;
1316 register struct vnode
*ovp
= OTHERVP(ap
->a_vp
);
1319 return (VCALL(ovp
, VOFFSET(vnop_advlock
), ap
));
1324 * XXX - vnop_strategy must be hand coded because it has no
1325 * vnode in its arguments.
1326 * This goes away with a merged VM/buffer cache.
1330 struct vnop_strategy_args
/* {
1334 struct buf
*bp
= ap
->a_bp
;
1336 struct vnode
*savedvp
;
1338 savedvp
= buf_vnode(bp
);
1339 buf_setvnode(bp
, OTHERVP(savedvp
));
1342 if (buf_vnode(bp
) == NULLVP
)
1343 panic("union_strategy: nil vp");
1344 if (((buf_flags(bp
) & B_READ
) == 0) &&
1345 (buf_vnode(bp
) == LOWERVP(savedvp
)))
1346 panic("union_strategy: writing to lowervp");
1349 error
= VNOP_STRATEGY(bp
);
1350 buf_setvnode(bp
, savedvp
);
1358 struct vnop_pagein_args
/* {
1361 vm_offset_t a_pl_offset,
1365 vfs_context_t a_context;
1369 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1371 error
= VNOP_PAGEIN(vp
, ap
->a_pl
, ap
->a_pl_offset
, ap
->a_f_offset
,
1372 ap
->a_size
, ap
->a_flags
, ap
->a_context
);
1376 * perhaps the size of the underlying object has changed under
1377 * our feet. take advantage of the offset information present
1378 * in the uio structure.
1381 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1382 off_t cur
= ap
->a_f_offset
+ (off_t
)ap
->a_pl_offset
;
1384 if (vp
== un
->un_uppervp
) {
1385 if (cur
> un
->un_uppersz
)
1386 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
1388 if (cur
> un
->un_lowersz
)
1389 union_newsize(ap
->a_vp
, VNOVAL
, cur
);
1399 struct vnop_pageout_args
/* {
1402 vm_offset_t a_pl_offset,
1406 vfs_context_t a_context;
1411 struct union_node
*un
= VTOUNION(ap
->a_vp
);
1413 vp
= UPPERVP(ap
->a_vp
);
1415 panic("union: missing upper layer in pageout");
1417 error
= VNOP_PAGEOUT(vp
, ap
->a_pl
, ap
->a_pl_offset
, ap
->a_f_offset
,
1418 ap
->a_size
, ap
->a_flags
, ap
->a_context
);
1421 * the size of the underlying object may be changed by the
1425 off_t cur
= ap
->a_f_offset
+ (off_t
)ap
->a_pl_offset
;
1427 if (cur
> un
->un_uppersz
)
1428 union_newsize(ap
->a_vp
, cur
, VNOVAL
);
1434 /* Blktooff derives file offset for the given logical block number */
1437 struct vnop_blktooff_args
/* {
1444 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1446 error
= VNOP_BLKTOOFF(vp
, ap
->a_lblkno
, ap
->a_offset
);
1451 /* offtoblk derives file offset for the given logical block number */
1454 struct vnop_offtoblk_args
/* {
1457 daddr64_t *a_lblkno;
1461 struct vnode
*vp
= OTHERVP(ap
->a_vp
);
1463 error
= VNOP_OFFTOBLK(vp
, ap
->a_offset
, ap
->a_lblkno
);
1468 #define VOPFUNC int (*)(void *)
1471 * Global vfs data structures
1473 int (**union_vnodeop_p
)(void *);
1474 struct vnodeopv_entry_desc union_vnodeop_entries
[] = {
1475 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1476 { &vnop_lookup_desc
, (VOPFUNC
)union_lookup
}, /* lookup */
1477 { &vnop_create_desc
, (VOPFUNC
)union_create
}, /* create */
1478 { &vnop_whiteout_desc
, (VOPFUNC
)union_whiteout
}, /* whiteout */
1479 { &vnop_mknod_desc
, (VOPFUNC
)union_mknod
}, /* mknod */
1480 { &vnop_open_desc
, (VOPFUNC
)union_open
}, /* open */
1481 { &vnop_close_desc
, (VOPFUNC
)union_close
}, /* close */
1482 { &vnop_access_desc
, (VOPFUNC
)union_access
}, /* access */
1483 { &vnop_getattr_desc
, (VOPFUNC
)union_getattr
}, /* getattr */
1484 { &vnop_setattr_desc
, (VOPFUNC
)union_setattr
}, /* setattr */
1485 { &vnop_read_desc
, (VOPFUNC
)union_read
}, /* read */
1486 { &vnop_write_desc
, (VOPFUNC
)union_write
}, /* write */
1487 { &vnop_ioctl_desc
, (VOPFUNC
)union_ioctl
}, /* ioctl */
1488 { &vnop_select_desc
, (VOPFUNC
)union_select
}, /* select */
1489 { &vnop_revoke_desc
, (VOPFUNC
)union_revoke
}, /* revoke */
1490 { &vnop_mmap_desc
, (VOPFUNC
)union_mmap
}, /* mmap */
1491 { &vnop_fsync_desc
, (VOPFUNC
)union_fsync
}, /* fsync */
1492 { &vnop_remove_desc
, (VOPFUNC
)union_remove
}, /* remove */
1493 { &vnop_link_desc
, (VOPFUNC
)union_link
}, /* link */
1494 { &vnop_rename_desc
, (VOPFUNC
)union_rename
}, /* rename */
1495 { &vnop_mkdir_desc
, (VOPFUNC
)union_mkdir
}, /* mkdir */
1496 { &vnop_rmdir_desc
, (VOPFUNC
)union_rmdir
}, /* rmdir */
1497 { &vnop_symlink_desc
, (VOPFUNC
)union_symlink
}, /* symlink */
1498 { &vnop_readdir_desc
, (VOPFUNC
)union_readdir
}, /* readdir */
1499 { &vnop_readlink_desc
, (VOPFUNC
)union_readlink
}, /* readlink */
1500 { &vnop_inactive_desc
, (VOPFUNC
)union_inactive
}, /* inactive */
1501 { &vnop_reclaim_desc
, (VOPFUNC
)union_reclaim
}, /* reclaim */
1502 { &vnop_strategy_desc
, (VOPFUNC
)union_strategy
}, /* strategy */
1503 { &vnop_pathconf_desc
, (VOPFUNC
)union_pathconf
}, /* pathconf */
1504 { &vnop_advlock_desc
, (VOPFUNC
)union_advlock
}, /* advlock */
1506 { &vnop_bwrite_desc
, (VOPFUNC
)union_bwrite
}, /* bwrite */
1508 { &vnop_pagein_desc
, (VOPFUNC
)union_pagein
}, /* Pagein */
1509 { &vnop_pageout_desc
, (VOPFUNC
)union_pageout
}, /* Pageout */
1510 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1511 { &vnop_blktooff_desc
, (VOPFUNC
)union_blktooff
}, /* blktooff */
1512 { &vnop_offtoblk_desc
, (VOPFUNC
)union_offtoblk
}, /* offtoblk */
1513 { &vnop_blockmap_desc
, (VOPFUNC
)union_blockmap
}, /* blockmap */
1514 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1516 struct vnodeopv_desc union_vnodeop_opv_desc
=
1517 { &union_vnodeop_p
, union_vnodeop_entries
};