2 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
3 * Copyright (c) 1992, 1993, 1994, 1995
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/fcntl.h>
41 #include <sys/kernel.h>
42 #include <sys/vnode.h>
43 #include <sys/mount.h>
44 #include <sys/namei.h>
45 #include <sys/malloc.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
52 #include <sys/event.h>
53 #include <sys/extattr.h>
54 #include <fs/unionfs/union.h>
57 #include <vm/vnode_pager.h>
59 #include <vm/vm_page.h>
60 #include <vm/vm_object.h>
65 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, "");
67 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, "");
70 static vop_access_t union_access;
71 static vop_aclcheck_t union_aclcheck;
72 static vop_advlock_t union_advlock;
73 static vop_close_t union_close;
74 static vop_closeextattr_t union_closeextattr;
75 static vop_create_t union_create;
76 static vop_deleteextattr_t union_deleteextattr;
77 static vop_fsync_t union_fsync;
78 static vop_getacl_t union_getacl;
79 static vop_getattr_t union_getattr;
80 static vop_getextattr_t union_getextattr;
81 static vop_inactive_t union_inactive;
82 static vop_ioctl_t union_ioctl;
83 static vop_lease_t union_lease;
84 static vop_link_t union_link;
85 static vop_listextattr_t union_listextattr;
86 static vop_lookup_t union_lookup;
87 static int union_lookup1(struct vnode *udvp, struct vnode **dvp,
89 struct componentname *cnp);
90 static vop_mkdir_t union_mkdir;
91 static vop_mknod_t union_mknod;
92 static vop_open_t union_open;
93 static vop_openextattr_t union_openextattr;
94 static vop_pathconf_t union_pathconf;
95 static vop_print_t union_print;
96 static vop_read_t union_read;
97 static vop_readdir_t union_readdir;
98 static vop_readlink_t union_readlink;
99 static vop_getwritemount_t union_getwritemount;
100 static vop_reclaim_t union_reclaim;
101 static vop_remove_t union_remove;
102 static vop_rename_t union_rename;
103 static vop_rmdir_t union_rmdir;
104 static vop_poll_t union_poll;
105 static vop_setacl_t union_setacl;
106 static vop_setattr_t union_setattr;
107 static vop_setlabel_t union_setlabel;
108 static vop_setextattr_t union_setextattr;
109 static vop_strategy_t union_strategy;
110 static vop_symlink_t union_symlink;
111 static vop_whiteout_t union_whiteout;
112 static vop_write_t union_write;
116 union_lock_upper(struct union_node *un, struct thread *td)
118 struct vnode *uppervp;
120 if ((uppervp = un->un_uppervp) != NULL) {
122 vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
124 KASSERT((uppervp == NULL || vrefcnt(uppervp) > 0), ("uppervp usecount is 0"));
130 union_unlock_upper(struct vnode *uppervp, struct thread *td)
137 union_lock_other(struct union_node *un, struct thread *td)
141 if (un->un_uppervp != NULL) {
142 vp = union_lock_upper(un, td);
143 } else if ((vp = un->un_lowervp) != NULL) {
145 vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
152 union_unlock_other(struct vnode *vp, struct thread *td)
160 * udvp must be exclusively locked on call and will remain
161 * exclusively locked on return. This is the mount point
162 * for our filesystem.
164 * dvp Our base directory, locked and referenced.
165 * The passed dvp will be dereferenced and unlocked on return
166 * and a new dvp will be returned which is locked and
167 * referenced in the same variable.
169 * vpp is filled in with the result if no error occured,
172 * If an error is returned, *vpp is set to NULLVP. If no
173 * error occurs, *vpp is returned with a reference and an
178 union_lookup1(udvp, pdvp, vpp, cnp)
182 struct componentname *cnp;
185 struct thread *td = cnp->cn_thread;
186 struct vnode *dvp = *pdvp;
191 * If stepping up the directory tree, check for going
192 * back across the mount point, in which case do what
193 * lookup would do by stepping back down the mount
196 if (cnp->cn_flags & ISDOTDOT) {
197 while ((dvp != udvp) && (dvp->v_vflag & VV_ROOT)) {
199 * Don't do the NOCROSSMOUNT check
200 * at this level. By definition,
201 * union fs deals with namespaces, not
205 dvp = dvp->v_mount->mnt_vnodecovered;
208 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
213 * Set return dvp to be the upperdvp 'parent directory.
218 * If the VOP_LOOKUP() call generates an error, tdvp is invalid and
219 * no changes will have been made to dvp, so we are set to return.
222 error = VOP_LOOKUP(dvp, &tdvp, cnp);
224 UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags));
228 UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags));
231 * Lastly check if the current node is a mount point in
232 * which case walk up the mount hierarchy making sure not to
233 * bump into the root of the mount tree (ie. dvp != udvp).
235 * We use dvp as a temporary variable here, it is no longer related
236 * to the dvp above. However, we have to ensure that both *pdvp and
237 * tdvp are locked on return.
243 (dvp->v_type == VDIR) &&
244 (mp = dvp->v_mountedhere)
248 if (vfs_busy(mp, 0, 0, td))
255 error = VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td);
260 vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, td);
273 struct vop_lookup_args /* {
274 struct vnodeop_desc *a_desc;
276 struct vnode **a_vpp;
277 struct componentname *a_cnp;
282 struct vnode *uppervp, *lowervp;
283 struct vnode *upperdvp, *lowerdvp;
284 struct vnode *dvp = ap->a_dvp; /* starting dir */
285 struct union_node *dun = VTOUNION(dvp); /* associated union node */
286 struct componentname *cnp = ap->a_cnp;
287 struct thread *td = cnp->cn_thread;
288 struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
289 struct ucred *saved_cred = NULL;
296 * Disallow write attempts to the filesystem mounted read-only.
298 if ((cnp->cn_flags & ISLASTCN) &&
299 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
300 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
305 * For any lookups we do, always return with the parent locked.
307 cnp->cn_flags |= LOCKPARENT;
309 lowerdvp = dun->un_lowervp;
318 * Get a private lock on uppervp and a reference, effectively
319 * taking it out of the union_node's control.
321 * We must lock upperdvp while holding our lock on dvp
322 * to avoid a deadlock.
324 upperdvp = union_lock_upper(dun, td);
327 * Do the lookup in the upper level.
328 * If that level consumes additional pathnames,
329 * then assume that something special is going
330 * on and just return that vnode.
332 if (upperdvp != NULLVP) {
334 * We do not have to worry about the DOTDOT case, we've
335 * already unlocked dvp.
337 UDEBUG(("A %p\n", upperdvp));
340 * Do the lookup. We must supply a locked and referenced
341 * upperdvp to the function and will get a new locked and
342 * referenced upperdvp back, with the old having been
345 * If an error is returned, uppervp will be NULLVP. If no
346 * error occurs, uppervp will be the locked and referenced.
347 * Return vnode, or possibly NULL, depending on what is being
348 * requested. It is possible that the returned uppervp
349 * will be the same as upperdvp.
351 uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
353 "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
357 VOP_ISLOCKED(upperdvp, NULL),
359 (uppervp ? vrefcnt(uppervp) : -99),
360 (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99)
364 * Disallow write attempts to the filesystem mounted read-only.
366 if (uerror == EJUSTRETURN && (cnp->cn_flags & ISLASTCN) &&
367 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
368 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {
374 * Special case: If cn_consume != 0 then skip out. The result
375 * of the lookup is transfered to our return variable. If
376 * an error occured we have to throw away the results.
379 if (cnp->cn_consume != 0) {
380 if ((error = uerror) == 0) {
381 *ap->a_vpp = uppervp;
388 * Calculate whiteout, fall through.
391 if (uerror == ENOENT || uerror == EJUSTRETURN) {
392 if (cnp->cn_flags & ISWHITEOUT) {
394 } else if (lowerdvp != NULLVP) {
397 terror = VOP_GETATTR(upperdvp, &va,
398 cnp->cn_cred, cnp->cn_thread);
399 if (terror == 0 && (va.va_flags & OPAQUE))
406 * In a similar way to the upper layer, do the lookup
407 * in the lower layer. This time, if there is some
408 * component magic going on, then vput whatever we got
409 * back from the upper layer and return the lower vnode
413 if (lowerdvp != NULLVP && !iswhiteout) {
416 UDEBUG(("B %p\n", lowerdvp));
419 * Force only LOOKUPs on the lower node, since
420 * we won't be making changes to it anyway.
422 nameiop = cnp->cn_nameiop;
423 cnp->cn_nameiop = LOOKUP;
424 if (um->um_op == UNMNT_BELOW) {
425 saved_cred = cnp->cn_cred;
426 cnp->cn_cred = um->um_cred;
430 * We shouldn't have to worry about locking interactions
431 * between the lower layer and our union layer (w.r.t.
432 * `..' processing) because we don't futz with lowervp
433 * locks in the union-node instantiation code path.
435 * union_lookup1() requires lowervp to be locked on entry,
436 * and it will be unlocked on return. The ref count will
437 * not change. On return lowervp doesn't represent anything
438 * to us so we NULL it out.
441 vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, td);
442 lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
443 if (lowerdvp == lowervp)
447 lowerdvp = NULL; /* lowerdvp invalid after vput */
449 if (um->um_op == UNMNT_BELOW)
450 cnp->cn_cred = saved_cred;
451 cnp->cn_nameiop = nameiop;
453 if (cnp->cn_consume != 0 || lerror == EACCES) {
454 if ((error = lerror) == 0) {
455 *ap->a_vpp = lowervp;
461 UDEBUG(("C %p\n", lowerdvp));
462 if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
463 if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
465 vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, td);
472 * Ok. Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
474 * 1. If both layers returned an error, select the upper layer.
476 * 2. If the upper layer failed and the bottom layer succeeded,
477 * two subcases occur:
479 * a. The bottom vnode is not a directory, in which case
480 * just return a new union vnode referencing an
481 * empty top layer and the existing bottom layer.
483 * b. The bottom vnode is a directory, in which case
484 * create a new directory in the top layer and
485 * and fall through to case 3.
487 * 3. If the top layer succeeded, then return a new union
488 * vnode referencing whatever the new top layer and
489 * whatever the bottom layer returned.
493 if ((uerror != 0) && (lerror != 0)) {
499 if (uerror != 0 /* && (lerror == 0) */ ) {
500 if (lowervp->v_type == VDIR) { /* case 2b. */
501 KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
503 * Oops, uppervp has a problem, we may have to shadow.
505 uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
514 * Must call union_allocvp() with both the upper and lower vnodes
515 * referenced and the upper vnode locked. ap->a_vpp is returned
516 * referenced and locked. lowervp, uppervp, and upperdvp are
517 * absorbed by union_allocvp() whether it succeeds or fails.
519 * upperdvp is the parent directory of uppervp which may be
520 * different, depending on the path, from dvp->un_uppervp. That's
521 * why it is a separate argument. Note that it must be unlocked.
523 * dvp must be locked on entry to the call and will be locked on
527 if (uppervp && uppervp != upperdvp)
528 VOP_UNLOCK(uppervp, 0, td);
530 VOP_UNLOCK(lowervp, 0, td);
532 VOP_UNLOCK(upperdvp, 0, td);
534 error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
535 uppervp, lowervp, 1);
537 UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99));
546 * - put away any extra junk laying around. Note that lowervp
547 * (if not NULL) will never be the same as *ap->a_vp and
548 * neither will uppervp, because when we set that state we
549 * NULL-out lowervp or uppervp. On the otherhand, upperdvp
550 * may match uppervp or *ap->a_vpp.
552 * - relock/unlock dvp if appropriate.
557 if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
569 UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
570 ((*ap->a_vpp) ? vrefcnt(*ap->a_vpp) : -99),
573 if (error == 0 || error == EJUSTRETURN) {
574 if (cnp->cn_namelen == 1 &&
575 cnp->cn_nameptr[0] == '.' &&
578 vprint("union_lookup: vp", *ap->a_vpp);
579 vprint("union_lookup: dvp", dvp);
581 panic("union_lookup returning . (%p) != startdir (%p)",
592 * a_dvp is locked on entry and remains locked on return. a_vpp is returned
593 * locked if no error occurs, otherwise it is garbage.
598 struct vop_create_args /* {
600 struct vnode **a_vpp;
601 struct componentname *a_cnp;
605 struct union_node *dun = VTOUNION(ap->a_dvp);
606 struct componentname *cnp = ap->a_cnp;
607 struct thread *td = cnp->cn_thread;
611 if ((dvp = union_lock_upper(dun, td)) != NULL) {
615 error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
617 mp = ap->a_dvp->v_mount;
618 VOP_UNLOCK(vp, 0, td);
619 UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vrefcnt(vp)));
620 error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
622 UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp)));
624 union_unlock_upper(dvp, td);
631 struct vop_whiteout_args /* {
633 struct componentname *a_cnp;
637 struct union_node *un = VTOUNION(ap->a_dvp);
638 struct componentname *cnp = ap->a_cnp;
639 struct vnode *uppervp;
642 switch (ap->a_flags) {
645 uppervp = union_lock_upper(un, cnp->cn_thread);
646 if (uppervp != NULLVP) {
647 error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags);
648 union_unlock_upper(uppervp, cnp->cn_thread);
656 panic("union_whiteout: unknown op");
664 * a_dvp is locked on entry and should remain locked on return.
665 * a_vpp is garbage whether an error occurs or not.
670 struct vop_mknod_args /* {
672 struct vnode **a_vpp;
673 struct componentname *a_cnp;
677 struct union_node *dun = VTOUNION(ap->a_dvp);
678 struct componentname *cnp = ap->a_cnp;
682 if ((dvp = union_lock_upper(dun, cnp->cn_thread)) != NULL) {
683 error = VOP_MKNOD(dvp, ap->a_vpp, cnp, ap->a_vap);
684 union_unlock_upper(dvp, cnp->cn_thread);
692 * run open VOP. When opening the underlying vnode we have to mimic
693 * vn_open(). What we *really* need to do to avoid screwups if the
694 * open semantics change is to call vn_open(). For example, ufs blows
695 * up if you open a file but do not vmio it prior to writing.
700 struct vop_open_args /* {
701 struct vnodeop_desc *a_desc;
704 struct ucred *a_cred;
708 struct union_node *un = VTOUNION(ap->a_vp);
710 int mode = ap->a_mode;
711 struct ucred *cred = ap->a_cred;
712 struct thread *td = ap->a_td;
717 * If there is an existing upper vp then simply open that.
718 * The upper vp takes precedence over the lower vp. When opening
719 * a lower vp for writing copy it to the uppervp and then open the
722 * At the end of this section tvp will be left locked.
724 if ((tvp = union_lock_upper(un, td)) == NULLVP) {
726 * If the lower vnode is being opened for writing, then
727 * copy the file contents to the upper vnode and open that,
728 * otherwise can simply open the lower vnode.
730 tvp = un->un_lowervp;
731 if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
732 int docopy = !(mode & O_TRUNC);
733 error = union_copyup(un, docopy, cred, td);
734 tvp = union_lock_upper(un, td);
738 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td);
744 * We are holding the correct vnode, open it.
748 error = VOP_OPEN(tvp, mode, cred, td, -1);
750 ap->a_vp->v_object = tvp->v_object;
752 * Release any locks held.
756 union_unlock_upper(tvp, td);
766 * It is unclear whether a_vp is passed locked or unlocked. Whatever
767 * the case we do not change it.
772 struct vop_close_args /* {
775 struct ucred *a_cred;
779 struct union_node *un = VTOUNION(ap->a_vp);
782 if ((vp = un->un_uppervp) == NULLVP) {
783 #ifdef UNION_DIAGNOSTIC
784 if (un->un_openl <= 0)
785 panic("union: un_openl cnt");
791 return (VOP_CLOSE_AP(ap));
795 * Check access permission on the union vnode.
796 * The access check being enforced is to check
797 * against both the underlying vnode, and any
798 * copied vnode. This ensures that no additional
799 * file permissions are given away simply because
800 * the user caused an implicit file copy.
804 struct vop_access_args /* {
805 struct vnodeop_desc *a_desc;
808 struct ucred *a_cred;
812 struct union_node *un = VTOUNION(ap->a_vp);
813 struct thread *td = ap->a_td;
818 * Disallow write attempts on filesystems mounted read-only.
820 if ((ap->a_mode & VWRITE) &&
821 (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
822 switch (ap->a_vp->v_type) {
832 if ((vp = union_lock_upper(un, td)) != NULLVP) {
834 error = VOP_ACCESS_AP(ap);
835 union_unlock_upper(vp, td);
839 if ((vp = un->un_lowervp) != NULLVP) {
840 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
844 * Remove VWRITE from a_mode if our mount point is RW, because
845 * we want to allow writes and lowervp may be read-only.
847 if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
848 ap->a_mode &= ~VWRITE;
850 error = VOP_ACCESS_AP(ap);
852 struct union_mount *um;
854 um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
856 if (um->um_op == UNMNT_BELOW) {
857 ap->a_cred = um->um_cred;
858 error = VOP_ACCESS_AP(ap);
861 VOP_UNLOCK(vp, 0, td);
867 * We handle getattr only to change the fsid and
870 * It's not clear whether VOP_GETATTR is to be
871 * called with the vnode locked or not. stat() calls
872 * it with (vp) locked, and fstat() calls it with
875 * Because of this we cannot use our normal locking functions
876 * if we do not intend to lock the main a_vp node. At the moment
877 * we are running without any specific locking at all, but beware
878 * to any programmer that care must be taken if locking is added
884 struct vop_getattr_args /* {
887 struct ucred *a_cred;
892 struct union_node *un = VTOUNION(ap->a_vp);
893 struct union_mount *um = MOUNTTOUNIONMOUNT(ap->a_vp->v_mount);
899 * Some programs walk the filesystem hierarchy by counting
900 * links to directories to avoid stat'ing all the time.
901 * This means the link count on directories needs to be "correct".
902 * The only way to do that is to call getattr on both layers
903 * and fix up the link count. The link count will not necessarily
904 * be accurate but will be large enough to defeat the tree walkers.
909 if ((vp = un->un_uppervp) != NULLVP) {
910 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
913 /* XXX isn't this dangerous without a lock? */
914 union_newsize(ap->a_vp, vap->va_size, VNOVAL);
919 } else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
927 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_td);
930 /* XXX isn't this dangerous without a lock? */
931 union_newsize(ap->a_vp, VNOVAL, vap->va_size);
934 if (ap->a_vap->va_fsid == um->um_upperdev)
935 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
937 if ((vap != ap->a_vap) && (vap->va_type == VDIR))
938 ap->a_vap->va_nlink += vap->va_nlink;
944 struct vop_setattr_args /* {
947 struct ucred *a_cred;
951 struct union_node *un = VTOUNION(ap->a_vp);
952 struct thread *td = ap->a_td;
953 struct vattr *vap = ap->a_vap;
954 struct vnode *uppervp;
958 * Disallow write attempts on filesystems mounted read-only.
960 if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
961 (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
962 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
963 vap->va_mtime.tv_sec != VNOVAL ||
964 vap->va_mode != (mode_t)VNOVAL)) {
969 * Handle case of truncating lower object to zero size
970 * by creating a zero length upper object. This is to
971 * handle the case of open with O_TRUNC and O_CREAT.
973 if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
974 error = union_copyup(un, (ap->a_vap->va_size != 0),
975 ap->a_cred, ap->a_td);
981 * Try to set attributes in upper layer,
982 * otherwise return read-only filesystem error.
985 if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
986 error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
987 ap->a_cred, ap->a_td);
988 if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
989 union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
990 union_unlock_upper(uppervp, td);
997 struct vop_read_args /* {
1001 struct ucred *a_cred;
1004 struct union_node *un = VTOUNION(ap->a_vp);
1005 struct thread *td = ap->a_uio->uio_td;
1009 uvp = union_lock_other(un, td);
1010 KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
1012 error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1013 union_unlock_other(uvp, td);
1017 * Perhaps the size of the underlying object has changed under
1018 * our feet. Take advantage of the offset information present
1019 * in the uio structure.
1022 struct union_node *un = VTOUNION(ap->a_vp);
1023 off_t cur = ap->a_uio->uio_offset;
1025 if (uvp == un->un_uppervp) {
1026 if (cur > un->un_uppersz)
1027 union_newsize(ap->a_vp, cur, VNOVAL);
1029 if (cur > un->un_lowersz)
1030 union_newsize(ap->a_vp, VNOVAL, cur);
1038 struct vop_write_args /* {
1042 struct ucred *a_cred;
1045 struct union_node *un = VTOUNION(ap->a_vp);
1046 struct thread *td = ap->a_uio->uio_td;
1047 struct vnode *uppervp;
1050 if ((uppervp = union_lock_upper(un, td)) == NULLVP)
1051 panic("union: missing upper layer in write");
1053 error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1056 * The size of the underlying object may be changed by the
1060 off_t cur = ap->a_uio->uio_offset;
1062 if (cur > un->un_uppersz)
1063 union_newsize(ap->a_vp, cur, VNOVAL);
1065 union_unlock_upper(uppervp, td);
1071 struct vop_lease_args /* {
1073 struct thread *a_td;
1074 struct ucred *a_cred;
1078 struct vnode *ovp = OTHERVP(ap->a_vp);
1081 return (VOP_LEASE_AP(ap));
1086 struct vop_ioctl_args /* {
1091 struct ucred *a_cred;
1092 struct thread *a_td;
1095 struct vnode *ovp = OTHERVP(ap->a_vp);
1098 return (VOP_IOCTL_AP(ap));
1103 struct vop_poll_args /* {
1106 struct ucred *a_cred;
1107 struct thread *a_td;
1110 struct vnode *ovp = OTHERVP(ap->a_vp);
1113 return (VOP_POLL_AP(ap));
1118 struct vop_fsync_args /* {
1120 struct ucred *a_cred;
1122 struct thread *a_td;
1126 struct thread *td = ap->a_td;
1127 struct vnode *targetvp;
1128 struct union_node *un = VTOUNION(ap->a_vp);
1130 if ((targetvp = union_lock_other(un, td)) != NULLVP) {
1131 error = VOP_FSYNC(targetvp, ap->a_waitfor, td);
1132 union_unlock_other(targetvp, td);
1141 * Remove the specified cnp. The dvp and vp are passed to us locked
1142 * and must remain locked on return.
1147 struct vop_remove_args /* {
1148 struct vnode *a_dvp;
1150 struct componentname *a_cnp;
1153 struct union_node *dun = VTOUNION(ap->a_dvp);
1154 struct union_node *un = VTOUNION(ap->a_vp);
1155 struct componentname *cnp = ap->a_cnp;
1156 struct thread *td = cnp->cn_thread;
1157 struct vnode *uppervp;
1158 struct vnode *upperdvp;
1161 if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1162 panic("union remove: null upper vnode");
1164 if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1165 if (union_dowhiteout(un, cnp->cn_cred, td))
1166 cnp->cn_flags |= DOWHITEOUT;
1167 if (cnp->cn_flags & DOWHITEOUT) /* XXX fs corruption */
1170 error = VOP_REMOVE(upperdvp, uppervp, cnp);
1172 union_removed_upper(un);
1173 union_unlock_upper(uppervp, td);
1175 error = union_mkwhiteout(
1176 MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1177 upperdvp, ap->a_cnp, un->un_path);
1179 union_unlock_upper(upperdvp, td);
1186 * tdvp and vp will be locked on entry.
1187 * tdvp and vp should remain locked on return.
1192 struct vop_link_args /* {
1193 struct vnode *a_tdvp;
1195 struct componentname *a_cnp;
1198 struct componentname *cnp = ap->a_cnp;
1199 struct thread *td = cnp->cn_thread;
1200 struct union_node *dun = VTOUNION(ap->a_tdvp);
1205 if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1208 struct union_node *tun = VTOUNION(ap->a_vp);
1210 if (tun->un_uppervp == NULLVP) {
1212 if (dun->un_uppervp == tun->un_dirvp) {
1213 if (dun->un_flags & UN_ULOCK) {
1214 dun->un_flags &= ~UN_ULOCK;
1215 VOP_UNLOCK(dun->un_uppervp, 0, td);
1219 error = union_copyup(tun, 1, cnp->cn_cred, td);
1221 if (dun->un_uppervp == tun->un_dirvp) {
1222 vn_lock(dun->un_uppervp,
1223 LK_EXCLUSIVE | LK_RETRY, td);
1224 dun->un_flags |= UN_ULOCK;
1230 vp = tun->un_uppervp;
1231 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1235 * Make sure upper is locked, then unlock the union directory we were
1236 * called with to avoid a deadlock while we are calling VOP_LINK() on
1237 * the upper (with tdvp locked and vp not locked). Our ap->a_tdvp
1238 * is expected to be locked on return.
1241 if ((tdvp = union_lock_upper(dun, td)) == NULLVP)
1244 VOP_UNLOCK(ap->a_tdvp, 0, td); /* unlock calling node */
1245 error = VOP_LINK(tdvp, vp, cnp); /* call link on upper */
1248 * Unlock tun->un_uppervp if we locked it above.
1250 if (ap->a_tdvp->v_op == ap->a_vp->v_op)
1251 VOP_UNLOCK(vp, 0, td);
1253 * We have to unlock tdvp prior to relocking our calling node in
1254 * order to avoid a deadlock. We also have to unlock ap->a_vp
1255 * before relocking the directory, but then we have to relock
1256 * ap->a_vp as our caller expects.
1258 VOP_UNLOCK(ap->a_vp, 0, td);
1259 union_unlock_upper(tdvp, td);
1260 vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, td);
1261 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
1267 struct vop_rename_args /* {
1268 struct vnode *a_fdvp;
1269 struct vnode *a_fvp;
1270 struct componentname *a_fcnp;
1271 struct vnode *a_tdvp;
1272 struct vnode *a_tvp;
1273 struct componentname *a_tcnp;
1277 struct vnode *fdvp = ap->a_fdvp;
1278 struct vnode *fvp = ap->a_fvp;
1279 struct vnode *tdvp = ap->a_tdvp;
1280 struct vnode *tvp = ap->a_tvp;
1283 * Figure out what fdvp to pass to our upper or lower vnode. If we
1284 * replace the fdvp, release the original one and ref the new one.
1287 if (fdvp->v_op == &union_vnodeops) { /* always true */
1288 struct union_node *un = VTOUNION(fdvp);
1289 if (un->un_uppervp == NULLVP) {
1291 * this should never happen in normal
1292 * operation but might if there was
1293 * a problem creating the top-level shadow
1299 fdvp = un->un_uppervp;
1305 * Figure out what fvp to pass to our upper or lower vnode. If we
1306 * replace the fvp, release the original one and ref the new one.
1309 if (fvp->v_op == &union_vnodeops) { /* always true */
1310 struct union_node *un = VTOUNION(fvp);
1312 struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
1315 if (un->un_uppervp == NULLVP) {
1316 switch(fvp->v_type) {
1318 vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread);
1319 error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_thread);
1320 VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_thread);
1328 * There is only one way to rename a directory
1329 * based in the lowervp, and that is to copy
1330 * the entire directory hierarchy. Otherwise
1331 * it would not last across a reboot.
1336 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_thread);
1337 error = union_mkshadow(um, fdvp,
1338 ap->a_fcnp, &un->un_uppervp);
1339 VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_thread);
1341 VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_thread);
1352 if (un->un_lowervp != NULLVP)
1353 ap->a_fcnp->cn_flags |= DOWHITEOUT;
1354 fvp = un->un_uppervp;
1360 * Figure out what tdvp (destination directory) to pass to the
1361 * lower level. If we replace it with uppervp, we need to vput the
1362 * old one. The exclusive lock is transfered to what we will pass
1363 * down in the VOP_RENAME() and we replace uppervp with a simple
1367 if (tdvp->v_op == &union_vnodeops) {
1368 struct union_node *un = VTOUNION(tdvp);
1370 if (un->un_uppervp == NULLVP) {
1372 * This should never happen in normal
1373 * operation but might if there was
1374 * a problem creating the top-level shadow
1382 * New tdvp is a lock and reference on uppervp.
1383 * Put away the old tdvp.
1385 tdvp = union_lock_upper(un, ap->a_tcnp->cn_thread);
1390 * Figure out what tvp (destination file) to pass to the
1393 * If the uppervp file does not exist, put away the (wrong)
1394 * file and change tvp to NULL.
1397 if (tvp != NULLVP && tvp->v_op == &union_vnodeops) {
1398 struct union_node *un = VTOUNION(tvp);
1400 tvp = union_lock_upper(un, ap->a_tcnp->cn_thread);
1402 /* note: tvp may be NULL */
1406 * VOP_RENAME() releases/vputs prior to returning, so we have no
1410 return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1413 * Error. We still have to release / vput the various elements.
1421 if (tvp != NULLVP) {
1432 struct vop_mkdir_args /* {
1433 struct vnode *a_dvp;
1434 struct vnode **a_vpp;
1435 struct componentname *a_cnp;
1436 struct vattr *a_vap;
1439 struct union_node *dun = VTOUNION(ap->a_dvp);
1440 struct componentname *cnp = ap->a_cnp;
1441 struct thread *td = cnp->cn_thread;
1442 struct vnode *upperdvp;
1445 if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) {
1448 error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap);
1449 union_unlock_upper(upperdvp, td);
1452 VOP_UNLOCK(vp, 0, td);
1453 UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vrefcnt(vp)));
1454 error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
1455 ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
1456 UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vrefcnt(vp)));
1464 struct vop_rmdir_args /* {
1465 struct vnode *a_dvp;
1467 struct componentname *a_cnp;
1470 struct union_node *dun = VTOUNION(ap->a_dvp);
1471 struct union_node *un = VTOUNION(ap->a_vp);
1472 struct componentname *cnp = ap->a_cnp;
1473 struct thread *td = cnp->cn_thread;
1474 struct vnode *upperdvp;
1475 struct vnode *uppervp;
1478 if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1479 panic("union rmdir: null upper vnode");
1481 if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1482 if (union_dowhiteout(un, cnp->cn_cred, td))
1483 cnp->cn_flags |= DOWHITEOUT;
1484 if (cnp->cn_flags & DOWHITEOUT) /* XXX fs corruption */
1487 error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp);
1489 union_removed_upper(un);
1490 union_unlock_upper(uppervp, td);
1492 error = union_mkwhiteout(
1493 MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1494 dun->un_uppervp, ap->a_cnp, un->un_path);
1496 union_unlock_upper(upperdvp, td);
1503 * dvp is locked on entry and remains locked on return. a_vpp is garbage
1509 struct vop_symlink_args /* {
1510 struct vnode *a_dvp;
1511 struct vnode **a_vpp;
1512 struct componentname *a_cnp;
1513 struct vattr *a_vap;
1517 struct union_node *dun = VTOUNION(ap->a_dvp);
1518 struct componentname *cnp = ap->a_cnp;
1519 struct thread *td = cnp->cn_thread;
1523 if ((dvp = union_lock_upper(dun, td)) != NULLVP) {
1524 error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap,
1526 union_unlock_upper(dvp, td);
1532 * union_readdir ()works in concert with getdirentries() and
1533 * readdir(3) to provide a list of entries in the unioned
1534 * directories. getdirentries() is responsible for walking
1535 * down the union stack. readdir(3) is responsible for
1536 * eliminating duplicate names from the returned data stream.
1540 struct vop_readdir_args /* {
1543 struct ucred *a_cred;
1549 struct union_node *un = VTOUNION(ap->a_vp);
1550 struct thread *td = ap->a_uio->uio_td;
1554 if ((uvp = union_lock_upper(un, td)) != NULLVP) {
1556 error = VOP_READDIR_AP(ap);
1557 union_unlock_upper(uvp, td);
1564 struct vop_readlink_args /* {
1567 struct ucred *a_cred;
1571 struct union_node *un = VTOUNION(ap->a_vp);
1572 struct uio *uio = ap->a_uio;
1573 struct thread *td = uio->uio_td;
1576 vp = union_lock_other(un, td);
1577 KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
1580 error = VOP_READLINK_AP(ap);
1581 union_unlock_other(vp, td);
1587 union_getwritemount(ap)
1588 struct vop_getwritemount_args /* {
1590 struct mount **a_mpp;
1593 struct vnode *vp = ap->a_vp;
1594 struct vnode *uvp = UPPERVP(vp);
1598 if (vp->v_iflag & VI_FREE) {
1600 return (EOPNOTSUPP);
1605 return(VOP_GETWRITEMOUNT(uvp, ap->a_mpp));
1611 * Called with the vnode locked. We are expected to unlock the vnode.
1616 struct vop_inactive_args /* {
1618 struct thread *a_td;
1621 struct vnode *vp = ap->a_vp;
1622 struct union_node *un = VTOUNION(vp);
1625 * Do nothing (and _don't_ bypass).
1626 * Wait to vrele lowervp until reclaim,
1627 * so that until then our union_node is in the
1628 * cache and reusable.
1632 if (un->un_dircache != NULL)
1633 union_dircache_free(un);
1636 if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
1637 un->un_flags &= ~UN_ULOCK;
1638 VOP_UNLOCK(un->un_uppervp, 0, td);
1642 if ((un->un_flags & UN_CACHED) == 0)
1650 struct vop_reclaim_args /* {
1654 union_freevp(ap->a_vp);
1661 struct vop_print_args /* {
1665 struct vnode *vp = ap->a_vp;
1667 printf("\tvp=%p, uppervp=%p, lowervp=%p\n",
1668 vp, UPPERVP(vp), LOWERVP(vp));
1669 if (UPPERVP(vp) != NULLVP)
1670 vprint("union: upper", UPPERVP(vp));
1671 if (LOWERVP(vp) != NULLVP)
1672 vprint("union: lower", LOWERVP(vp));
1679 struct vop_pathconf_args /* {
1686 struct thread *td = curthread; /* XXX */
1687 struct union_node *un = VTOUNION(ap->a_vp);
1690 vp = union_lock_other(un, td);
1691 KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
1694 error = VOP_PATHCONF_AP(ap);
1695 union_unlock_other(vp, td);
1702 struct vop_advlock_args /* {
1710 register struct vnode *ovp = OTHERVP(ap->a_vp);
1713 return (VOP_ADVLOCK_AP(ap));
1718 * XXX - vop_strategy must be hand coded because it has no
1719 * YYY - and it is not coherent with anything
1721 * vnode in its arguments.
1722 * This goes away with a merged VM/buffer cache.
1726 struct vop_strategy_args /* {
1731 struct buf *bp = ap->a_bp;
1732 struct vnode *othervp = OTHERVP(ap->a_vp);
1735 if (othervp == NULLVP)
1736 panic("union_strategy: nil vp");
1737 if ((bp->b_iocmd == BIO_WRITE) &&
1738 (othervp == LOWERVP(ap->a_vp)))
1739 panic("union_strategy: writing to lowervp");
1741 return (VOP_STRATEGY(othervp, bp));
1746 struct vop_getacl_args /* {
1750 struct ucred *a_cred;
1751 struct thread *a_td;
1755 struct union_node *un = VTOUNION(ap->a_vp);
1758 vp = union_lock_other(un, ap->a_td);
1760 error = VOP_GETACL_AP(ap);
1761 union_unlock_other(vp, ap->a_td);
1768 struct vop_setacl_args /* {
1772 struct ucred *a_cred;
1773 struct thread *a_td;
1777 struct union_node *un = VTOUNION(ap->a_vp);
1780 vp = union_lock_other(un, ap->a_td);
1782 error = VOP_SETACL_AP(ap);
1783 union_unlock_other(vp, ap->a_td);
1790 struct vop_aclcheck_args /* {
1794 struct ucred *a_cred;
1795 struct thread *a_td;
1798 struct vnode *ovp = OTHERVP(ap->a_vp);
1801 return (VOP_ACLCHECK_AP(ap));
1805 union_closeextattr(ap)
1806 struct vop_closeextattr_args /* {
1809 struct ucred *a_cred;
1810 struct thread *a_td;
1814 struct union_node *un = VTOUNION(ap->a_vp);
1817 vp = union_lock_other(un, ap->a_td);
1819 error = VOP_CLOSEEXTATTR_AP(ap);
1820 union_unlock_other(vp, ap->a_td);
1826 union_getextattr(ap)
1827 struct vop_getextattr_args /* {
1829 int a_attrnamespace;
1833 struct ucred *a_cred;
1834 struct thread *a_td;
1838 struct union_node *un = VTOUNION(ap->a_vp);
1841 vp = union_lock_other(un, ap->a_td);
1843 error = VOP_GETEXTATTR_AP(ap);
1844 union_unlock_other(vp, ap->a_td);
1850 union_listextattr(ap)
1851 struct vop_listextattr_args /* {
1853 int a_attrnamespace;
1856 struct ucred *a_cred;
1857 struct thread *a_td;
1861 struct union_node *un = VTOUNION(ap->a_vp);
1864 vp = union_lock_other(un, ap->a_td);
1866 error = VOP_LISTEXTATTR_AP(ap);
1867 union_unlock_other(vp, ap->a_td);
1873 union_openextattr(ap)
1874 struct vop_openextattr_args /* {
1876 struct ucred *a_cred;
1877 struct thread *a_td;
1881 struct union_node *un = VTOUNION(ap->a_vp);
1884 vp = union_lock_other(un, ap->a_td);
1886 error = VOP_OPENEXTATTR_AP(ap);
1887 union_unlock_other(vp, ap->a_td);
1893 union_deleteextattr(ap)
1894 struct vop_deleteextattr_args /* {
1896 int a_attrnamespace;
1898 struct ucred *a_cred;
1899 struct thread *a_td;
1903 struct union_node *un = VTOUNION(ap->a_vp);
1906 vp = union_lock_other(un, ap->a_td);
1908 error = VOP_DELETEEXTATTR_AP(ap);
1909 union_unlock_other(vp, ap->a_td);
1915 union_setextattr(ap)
1916 struct vop_setextattr_args /* {
1918 int a_attrnamespace;
1921 struct ucred *a_cred;
1922 struct thread *a_td;
1926 struct union_node *un = VTOUNION(ap->a_vp);
1929 vp = union_lock_other(un, ap->a_td);
1931 error = VOP_SETEXTATTR_AP(ap);
1932 union_unlock_other(vp, ap->a_td);
1939 struct vop_setlabel_args /* {
1941 struct label *a_label;
1942 struct ucred *a_cred;
1943 struct thread *a_td;
1947 struct union_node *un = VTOUNION(ap->a_vp);
1950 vp = union_lock_other(un, ap->a_td);
1952 error = VOP_SETLABEL_AP(ap);
1953 union_unlock_other(vp, ap->a_td);
1959 * Global vfs data structures
1961 struct vop_vector union_vnodeops = {
1962 .vop_default = &default_vnodeops,
1964 .vop_access = union_access,
1965 .vop_aclcheck = union_aclcheck,
1966 .vop_advlock = union_advlock,
1967 .vop_bmap = VOP_EOPNOTSUPP,
1968 .vop_close = union_close,
1969 .vop_closeextattr = union_closeextattr,
1970 .vop_create = union_create,
1971 .vop_deleteextattr = union_deleteextattr,
1972 .vop_fsync = union_fsync,
1973 .vop_getacl = union_getacl,
1974 .vop_getattr = union_getattr,
1975 .vop_getextattr = union_getextattr,
1976 .vop_getwritemount = union_getwritemount,
1977 .vop_inactive = union_inactive,
1978 .vop_ioctl = union_ioctl,
1979 .vop_lease = union_lease,
1980 .vop_link = union_link,
1981 .vop_listextattr = union_listextattr,
1982 .vop_lookup = union_lookup,
1983 .vop_mkdir = union_mkdir,
1984 .vop_mknod = union_mknod,
1985 .vop_open = union_open,
1986 .vop_openextattr = union_openextattr,
1987 .vop_pathconf = union_pathconf,
1988 .vop_poll = union_poll,
1989 .vop_print = union_print,
1990 .vop_read = union_read,
1991 .vop_readdir = union_readdir,
1992 .vop_readlink = union_readlink,
1993 .vop_reclaim = union_reclaim,
1994 .vop_remove = union_remove,
1995 .vop_rename = union_rename,
1996 .vop_rmdir = union_rmdir,
1997 .vop_setacl = union_setacl,
1998 .vop_setattr = union_setattr,
1999 .vop_setextattr = union_setextattr,
2000 .vop_setlabel = union_setlabel,
2001 .vop_strategy = union_strategy,
2002 .vop_symlink = union_symlink,
2003 .vop_whiteout = union_whiteout,
2004 .vop_write = union_write,