2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
5 * Copyright (c) 1992, 1993, 1994, 1995
6 * The Regents of the University of California.
7 * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
8 * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
11 * This code is derived from software contributed to Berkeley by
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95
42 #include <sys/param.h>
43 #include <sys/systm.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/sysctl.h>
52 #include <sys/vnode.h>
54 #include <sys/fcntl.h>
56 #include <sys/dirent.h>
61 #include <fs/unionfs/union.h>
63 #include <machine/atomic.h>
66 #include <vm/vm_extern.h>
67 #include <vm/vm_object.h>
68 #include <vm/vnode_pager.h>
71 #define UNIONFS_INTERNAL_DEBUG(msg, args...) printf(msg, ## args)
72 #define UNIONFS_IDBG_RENAME
74 #define UNIONFS_INTERNAL_DEBUG(msg, args...)
77 #define KASSERT_UNIONFS_VNODE(vp) \
78 VNASSERT(((vp)->v_op == &unionfs_vnodeops), vp, \
79 ("%s: non-unionfs vnode", __func__))
82 unionfs_lookup(struct vop_cachedlookup_args *ap)
84 struct unionfs_node *dunp;
85 struct vnode *dvp, *udvp, *ldvp, *vp, *uvp, *lvp, *dtmpvp;
87 struct componentname *cnp;
90 u_long cnflags, cnflagsbk;
93 int error , uerror, lerror;
97 error = uerror = lerror = ENOENT;
99 nameiop = cnp->cn_nameiop;
100 cnflags = cnp->cn_flags;
102 dunp = VTOUNIONFS(dvp);
103 udvp = dunp->un_uppervp;
104 ldvp = dunp->un_lowervp;
105 vp = uvp = lvp = NULLVP;
107 *(ap->a_vpp) = NULLVP;
109 UNIONFS_INTERNAL_DEBUG(
110 "unionfs_lookup: enter: nameiop=%ld, flags=%lx, path=%s\n",
111 nameiop, cnflags, cnp->cn_nameptr);
113 if (dvp->v_type != VDIR)
117 * If read-only and op is not LOOKUP, will return EROFS.
119 if ((cnflags & ISLASTCN) &&
120 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
127 if (cnflags & ISDOTDOT) {
128 if (LOOKUP != nameiop && udvp == NULLVP)
131 if (udvp != NULLVP) {
139 error = VOP_LOOKUP(dtmpvp, &vp, cnp);
141 if (dtmpvp == udvp && ldvp != NULLVP) {
143 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
148 * Exchange lock and reference from vp to
149 * dunp->un_dvp. vp is upper/lower vnode, but it
150 * will need to return the unionfs vnode.
152 if (nameiop == DELETE || nameiop == RENAME ||
153 (cnp->cn_lkflags & LK_TYPE_MASK))
158 *(ap->a_vpp) = dunp->un_dvp;
161 if (nameiop == DELETE || nameiop == RENAME)
162 vn_lock(dunp->un_dvp, LK_EXCLUSIVE | LK_RETRY);
163 else if (cnp->cn_lkflags & LK_TYPE_MASK)
164 vn_lock(dunp->un_dvp, cnp->cn_lkflags |
167 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
168 } else if (error == ENOENT && (cnflags & MAKEENTRY) != 0)
169 cache_enter(dvp, NULLVP, cnp);
171 goto unionfs_lookup_return;
177 if (udvp != NULLVP) {
178 uerror = VOP_LOOKUP(udvp, &uvp, cnp);
181 if (udvp == uvp) { /* is dot */
187 goto unionfs_lookup_return;
189 if (nameiop == DELETE || nameiop == RENAME ||
190 (cnp->cn_lkflags & LK_TYPE_MASK))
195 if (uerror == ENOENT || uerror == EJUSTRETURN)
196 if (cnp->cn_flags & ISWHITEOUT)
197 iswhiteout = 1; /* don't lookup lower */
198 if (iswhiteout == 0 && ldvp != NULLVP)
199 if (!VOP_GETATTR(udvp, &va, cnp->cn_cred) &&
200 (va.va_flags & OPAQUE))
201 iswhiteout = 1; /* don't lookup lower */
203 UNIONFS_INTERNAL_DEBUG(
204 "unionfs_lookup: debug: whiteout=%d, path=%s\n",
205 iswhiteout, cnp->cn_nameptr);
212 if (ldvp != NULLVP && !(cnflags & DOWHITEOUT) && iswhiteout == 0) {
213 /* always op is LOOKUP */
214 cnp->cn_nameiop = LOOKUP;
215 cnflagsbk = cnp->cn_flags;
216 cnp->cn_flags = cnflags;
218 lerror = VOP_LOOKUP(ldvp, &lvp, cnp);
220 cnp->cn_nameiop = nameiop;
221 if (udvp != NULLVP && (uerror == 0 || uerror == EJUSTRETURN))
222 cnp->cn_flags = cnflagsbk;
225 if (ldvp == lvp) { /* is dot */
227 vrele(uvp); /* no need? */
232 UNIONFS_INTERNAL_DEBUG(
233 "unionfs_lookup: leave (%d)\n", lerror);
237 if (cnp->cn_lkflags & LK_TYPE_MASK)
243 * check lookup result
245 if (uvp == NULLVP && lvp == NULLVP) {
246 error = (udvp != NULLVP ? uerror : lerror);
247 goto unionfs_lookup_return;
253 if (uvp != NULLVP && lvp != NULLVP && uvp->v_type != lvp->v_type) {
261 if (uerror != 0 && uerror != EJUSTRETURN && udvp != NULLVP &&
262 lerror == 0 && lvp != NULLVP && lvp->v_type == VDIR &&
263 !(dvp->v_mount->mnt_flag & MNT_RDONLY) &&
264 (1 < cnp->cn_namelen || '.' != *(cnp->cn_nameptr))) {
265 /* get unionfs vnode in order to create a new shadow dir. */
266 error = unionfs_nodeget(dvp->v_mount, NULLVP, lvp, dvp, &vp,
269 goto unionfs_lookup_cleanup;
271 if (LK_SHARED == (cnp->cn_lkflags & LK_TYPE_MASK))
273 if (LK_EXCLUSIVE != VOP_ISLOCKED(vp)) {
274 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
277 error = unionfs_mkshadowdir(MOUNTTOUNIONFSMOUNT(dvp->v_mount),
278 udvp, VTOUNIONFS(vp), cnp, td);
283 "unionfs_lookup: Unable to create shadow dir.");
284 if ((cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE)
288 goto unionfs_lookup_cleanup;
290 if ((cnp->cn_lkflags & LK_TYPE_MASK) == LK_SHARED)
291 vn_lock(vp, LK_SHARED | LK_RETRY);
302 goto unionfs_lookup_cleanup;
306 if (uvp != NULLVP && uvp->v_type == VSOCK) {
309 if (cnp->cn_lkflags & LK_TYPE_MASK)
310 vn_lock(vp, cnp->cn_lkflags | LK_RETRY);
312 else if (lvp != NULLVP && lvp->v_type == VSOCK) {
315 if (cnp->cn_lkflags & LK_TYPE_MASK)
316 vn_lock(vp, cnp->cn_lkflags | LK_RETRY);
322 error = unionfs_nodeget(dvp->v_mount, uvp, lvp,
326 "unionfs_lookup: Unable to create unionfs vnode.");
327 goto unionfs_lookup_cleanup;
329 if ((nameiop == DELETE || nameiop == RENAME) &&
330 (cnp->cn_lkflags & LK_TYPE_MASK) == 0)
331 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
336 if ((cnflags & MAKEENTRY) && vp->v_type != VSOCK)
337 cache_enter(dvp, vp, cnp);
339 unionfs_lookup_cleanup:
345 if (error == ENOENT && (cnflags & MAKEENTRY) != 0)
346 cache_enter(dvp, NULLVP, cnp);
348 unionfs_lookup_return:
350 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error);
356 unionfs_create(struct vop_create_args *ap)
358 struct unionfs_node *dunp;
359 struct componentname *cnp;
364 UNIONFS_INTERNAL_DEBUG("unionfs_create: enter\n");
366 KASSERT_UNIONFS_VNODE(ap->a_dvp);
368 dunp = VTOUNIONFS(ap->a_dvp);
370 udvp = dunp->un_uppervp;
373 if (udvp != NULLVP) {
374 error = VOP_CREATE(udvp, &vp, cnp, ap->a_vap);
376 goto unionfs_create_abort;
378 if (vp->v_type == VSOCK)
382 error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP,
383 ap->a_dvp, ap->a_vpp, cnp);
388 unionfs_create_abort:
389 UNIONFS_INTERNAL_DEBUG("unionfs_create: leave (%d)\n", error);
395 unionfs_whiteout(struct vop_whiteout_args *ap)
397 struct unionfs_node *dunp;
398 struct componentname *cnp;
402 UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: enter\n");
404 KASSERT_UNIONFS_VNODE(ap->a_dvp);
406 dunp = VTOUNIONFS(ap->a_dvp);
408 udvp = dunp->un_uppervp;
411 if (udvp != NULLVP) {
412 switch (ap->a_flags) {
416 error = VOP_WHITEOUT(udvp, cnp, ap->a_flags);
424 UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: leave (%d)\n", error);
430 unionfs_mknod(struct vop_mknod_args *ap)
432 struct unionfs_node *dunp;
433 struct componentname *cnp;
438 UNIONFS_INTERNAL_DEBUG("unionfs_mknod: enter\n");
440 KASSERT_UNIONFS_VNODE(ap->a_dvp);
442 dunp = VTOUNIONFS(ap->a_dvp);
444 udvp = dunp->un_uppervp;
447 if (udvp != NULLVP) {
448 error = VOP_MKNOD(udvp, &vp, cnp, ap->a_vap);
450 goto unionfs_mknod_abort;
452 if (vp->v_type == VSOCK)
456 error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP,
457 ap->a_dvp, ap->a_vpp, cnp);
463 UNIONFS_INTERNAL_DEBUG("unionfs_mknod: leave (%d)\n", error);
468 enum unionfs_lkupgrade {
469 UNIONFS_LKUPGRADE_SUCCESS, /* lock successfully upgraded */
470 UNIONFS_LKUPGRADE_ALREADY, /* lock already held exclusive */
471 UNIONFS_LKUPGRADE_DOOMED /* lock was upgraded, but vnode reclaimed */
474 static inline enum unionfs_lkupgrade
475 unionfs_upgrade_lock(struct vnode *vp)
477 ASSERT_VOP_LOCKED(vp, __func__);
479 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
480 return (UNIONFS_LKUPGRADE_ALREADY);
482 if (vn_lock(vp, LK_UPGRADE) != 0) {
483 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
484 if (VN_IS_DOOMED(vp))
485 return (UNIONFS_LKUPGRADE_DOOMED);
487 return (UNIONFS_LKUPGRADE_SUCCESS);
491 unionfs_downgrade_lock(struct vnode *vp, enum unionfs_lkupgrade status)
493 if (status != UNIONFS_LKUPGRADE_ALREADY)
494 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
498 unionfs_open(struct vop_open_args *ap)
500 struct unionfs_node *unp;
501 struct unionfs_node_status *unsp;
505 struct vnode *targetvp;
509 enum unionfs_lkupgrade lkstatus;
511 UNIONFS_INTERNAL_DEBUG("unionfs_open: enter\n");
513 KASSERT_UNIONFS_VNODE(ap->a_vp);
522 * The executable loader path may call this function with vp locked
523 * shared. If the vnode is reclaimed while upgrading, we can't safely
524 * use unp or do anything else unionfs- specific.
526 lkstatus = unionfs_upgrade_lock(vp);
527 if (lkstatus == UNIONFS_LKUPGRADE_DOOMED) {
529 goto unionfs_open_cleanup;
532 unp = VTOUNIONFS(vp);
533 uvp = unp->un_uppervp;
534 lvp = unp->un_lowervp;
535 unionfs_get_node_status(unp, td, &unsp);
537 if (unsp->uns_lower_opencnt > 0 || unsp->uns_upper_opencnt > 0) {
538 /* vnode is already opend. */
539 if (unsp->uns_upper_opencnt > 0)
544 if (targetvp == lvp &&
545 (ap->a_mode & FWRITE) && lvp->v_type == VREG)
548 if (targetvp == NULLVP) {
550 if ((ap->a_mode & FWRITE) && lvp->v_type == VREG) {
551 error = unionfs_copyfile(unp,
552 !(ap->a_mode & O_TRUNC), cred, td);
554 goto unionfs_open_abort;
555 targetvp = uvp = unp->un_uppervp;
562 error = VOP_OPEN(targetvp, ap->a_mode, cred, td, ap->a_fp);
564 if (targetvp == uvp) {
565 if (uvp->v_type == VDIR && lvp != NULLVP &&
566 unsp->uns_lower_opencnt <= 0) {
567 /* open lower for readdir */
568 error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
570 VOP_CLOSE(uvp, ap->a_mode, cred, td);
571 goto unionfs_open_abort;
573 unsp->uns_node_flag |= UNS_OPENL_4_READDIR;
574 unsp->uns_lower_opencnt++;
576 unsp->uns_upper_opencnt++;
578 unsp->uns_lower_opencnt++;
579 unsp->uns_lower_openmode = ap->a_mode;
581 vp->v_object = targetvp->v_object;
586 unionfs_tryrem_node_status(unp, unsp);
588 unionfs_open_cleanup:
589 unionfs_downgrade_lock(vp, lkstatus);
591 UNIONFS_INTERNAL_DEBUG("unionfs_open: leave (%d)\n", error);
597 unionfs_close(struct vop_close_args *ap)
599 struct unionfs_node *unp;
600 struct unionfs_node_status *unsp;
606 enum unionfs_lkupgrade lkstatus;;
608 UNIONFS_INTERNAL_DEBUG("unionfs_close: enter\n");
610 KASSERT_UNIONFS_VNODE(ap->a_vp);
618 * If the vnode is reclaimed while upgrading, we can't safely use unp
619 * or do anything else unionfs- specific.
621 lkstatus = unionfs_upgrade_lock(vp);
622 if (lkstatus == UNIONFS_LKUPGRADE_DOOMED)
623 goto unionfs_close_cleanup;
625 unp = VTOUNIONFS(vp);
626 unionfs_get_node_status(unp, td, &unsp);
628 if (unsp->uns_lower_opencnt <= 0 && unsp->uns_upper_opencnt <= 0) {
630 printf("unionfs_close: warning: open count is 0\n");
632 if (unp->un_uppervp != NULLVP)
633 ovp = unp->un_uppervp;
635 ovp = unp->un_lowervp;
636 } else if (unsp->uns_upper_opencnt > 0)
637 ovp = unp->un_uppervp;
639 ovp = unp->un_lowervp;
641 error = VOP_CLOSE(ovp, ap->a_fflag, cred, td);
644 goto unionfs_close_abort;
646 vp->v_object = ovp->v_object;
648 if (ovp == unp->un_uppervp) {
649 unsp->uns_upper_opencnt--;
650 if (unsp->uns_upper_opencnt == 0) {
651 if (unsp->uns_node_flag & UNS_OPENL_4_READDIR) {
652 VOP_CLOSE(unp->un_lowervp, FREAD, cred, td);
653 unsp->uns_node_flag &= ~UNS_OPENL_4_READDIR;
654 unsp->uns_lower_opencnt--;
656 if (unsp->uns_lower_opencnt > 0)
657 vp->v_object = unp->un_lowervp->v_object;
660 unsp->uns_lower_opencnt--;
663 unionfs_tryrem_node_status(unp, unsp);
665 unionfs_close_cleanup:
666 unionfs_downgrade_lock(vp, lkstatus);
668 UNIONFS_INTERNAL_DEBUG("unionfs_close: leave (%d)\n", error);
674 * Check the access mode toward shadow file/dir.
677 unionfs_check_corrected_access(accmode_t accmode, struct vattr *va,
680 uid_t uid; /* upper side vnode's uid */
681 gid_t gid; /* upper side vnode's gid */
682 u_short vmode; /* upper side vnode's mode */
691 if (cred->cr_uid == uid) {
696 if (accmode & VWRITE)
698 return ((vmode & mask) == mask ? 0 : EACCES);
702 if (groupmember(gid, cred)) {
707 if (accmode & VWRITE)
709 return ((vmode & mask) == mask ? 0 : EACCES);
717 if (accmode & VWRITE)
720 return ((vmode & mask) == mask ? 0 : EACCES);
724 unionfs_access(struct vop_access_args *ap)
726 struct unionfs_mount *ump;
727 struct unionfs_node *unp;
735 UNIONFS_INTERNAL_DEBUG("unionfs_access: enter\n");
737 KASSERT_UNIONFS_VNODE(ap->a_vp);
739 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
740 unp = VTOUNIONFS(ap->a_vp);
741 uvp = unp->un_uppervp;
742 lvp = unp->un_lowervp;
744 accmode = ap->a_accmode;
747 if ((accmode & VWRITE) &&
748 (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
749 switch (ap->a_vp->v_type) {
760 error = VOP_ACCESS(uvp, accmode, ap->a_cred, td);
762 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error);
768 if (accmode & VWRITE) {
769 if (ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY) {
770 switch (ap->a_vp->v_type) {
778 } else if (ap->a_vp->v_type == VREG ||
779 ap->a_vp->v_type == VDIR) {
780 /* check shadow file/dir */
781 if (ump->um_copymode != UNIONFS_TRANSPARENT) {
782 error = unionfs_create_uppervattr(ump,
783 lvp, &va, ap->a_cred, td);
787 error = unionfs_check_corrected_access(
788 accmode, &va, ap->a_cred);
793 accmode &= ~(VWRITE | VAPPEND);
794 accmode |= VREAD; /* will copy to upper */
796 error = VOP_ACCESS(lvp, accmode, ap->a_cred, td);
799 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error);
805 unionfs_getattr(struct vop_getattr_args *ap)
807 struct unionfs_node *unp;
808 struct unionfs_mount *ump;
815 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: enter\n");
817 KASSERT_UNIONFS_VNODE(ap->a_vp);
819 unp = VTOUNIONFS(ap->a_vp);
820 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
821 uvp = unp->un_uppervp;
822 lvp = unp->un_lowervp;
826 if ((error = VOP_GETATTR(uvp, ap->a_vap, ap->a_cred)) == 0)
828 ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
830 UNIONFS_INTERNAL_DEBUG(
831 "unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n",
832 ap->a_vap->va_mode, ap->a_vap->va_uid,
833 ap->a_vap->va_gid, error);
838 error = VOP_GETATTR(lvp, ap->a_vap, ap->a_cred);
840 if (error == 0 && !(ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY)) {
841 /* correct the attr toward shadow file/dir. */
842 if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) {
843 unionfs_create_uppervattr_core(ump, ap->a_vap, &va, td);
844 ap->a_vap->va_mode = va.va_mode;
845 ap->a_vap->va_uid = va.va_uid;
846 ap->a_vap->va_gid = va.va_gid;
851 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
853 UNIONFS_INTERNAL_DEBUG(
854 "unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n",
855 ap->a_vap->va_mode, ap->a_vap->va_uid, ap->a_vap->va_gid, error);
861 unionfs_setattr(struct vop_setattr_args *ap)
863 struct unionfs_node *unp;
870 UNIONFS_INTERNAL_DEBUG("unionfs_setattr: enter\n");
872 KASSERT_UNIONFS_VNODE(ap->a_vp);
875 unp = VTOUNIONFS(ap->a_vp);
876 uvp = unp->un_uppervp;
877 lvp = unp->un_lowervp;
881 if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
882 (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
883 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
884 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL))
887 if (uvp == NULLVP && lvp->v_type == VREG) {
888 error = unionfs_copyfile(unp, (vap->va_size != 0),
892 uvp = unp->un_uppervp;
896 error = VOP_SETATTR(uvp, vap, ap->a_cred);
898 UNIONFS_INTERNAL_DEBUG("unionfs_setattr: leave (%d)\n", error);
904 unionfs_read(struct vop_read_args *ap)
906 struct unionfs_node *unp;
910 /* UNIONFS_INTERNAL_DEBUG("unionfs_read: enter\n"); */
912 KASSERT_UNIONFS_VNODE(ap->a_vp);
914 unp = VTOUNIONFS(ap->a_vp);
915 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
917 error = VOP_READ(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
919 /* UNIONFS_INTERNAL_DEBUG("unionfs_read: leave (%d)\n", error); */
925 unionfs_write(struct vop_write_args *ap)
927 struct unionfs_node *unp;
931 /* UNIONFS_INTERNAL_DEBUG("unionfs_write: enter\n"); */
933 KASSERT_UNIONFS_VNODE(ap->a_vp);
935 unp = VTOUNIONFS(ap->a_vp);
936 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
938 error = VOP_WRITE(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
940 /* UNIONFS_INTERNAL_DEBUG("unionfs_write: leave (%d)\n", error); */
946 unionfs_ioctl(struct vop_ioctl_args *ap)
948 struct unionfs_node *unp;
949 struct unionfs_node_status *unsp;
953 UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: enter\n");
955 KASSERT_UNIONFS_VNODE(ap->a_vp);
957 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
958 unp = VTOUNIONFS(ap->a_vp);
959 unionfs_get_node_status(unp, ap->a_td, &unsp);
960 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
961 unionfs_tryrem_node_status(unp, unsp);
962 VOP_UNLOCK(ap->a_vp);
967 error = VOP_IOCTL(ovp, ap->a_command, ap->a_data, ap->a_fflag,
968 ap->a_cred, ap->a_td);
970 UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: leave (%d)\n", error);
976 unionfs_poll(struct vop_poll_args *ap)
978 struct unionfs_node *unp;
979 struct unionfs_node_status *unsp;
982 KASSERT_UNIONFS_VNODE(ap->a_vp);
984 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
985 unp = VTOUNIONFS(ap->a_vp);
986 unionfs_get_node_status(unp, ap->a_td, &unsp);
987 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
988 unionfs_tryrem_node_status(unp, unsp);
989 VOP_UNLOCK(ap->a_vp);
994 return (VOP_POLL(ovp, ap->a_events, ap->a_cred, ap->a_td));
998 unionfs_fsync(struct vop_fsync_args *ap)
1000 struct unionfs_node *unp;
1001 struct unionfs_node_status *unsp;
1004 KASSERT_UNIONFS_VNODE(ap->a_vp);
1006 unp = VTOUNIONFS(ap->a_vp);
1007 unionfs_get_node_status(unp, ap->a_td, &unsp);
1008 ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
1009 unionfs_tryrem_node_status(unp, unsp);
1014 return (VOP_FSYNC(ovp, ap->a_waitfor, ap->a_td));
1018 unionfs_remove(struct vop_remove_args *ap)
1021 struct unionfs_node *dunp;
1022 struct unionfs_node *unp;
1023 struct unionfs_mount *ump;
1028 struct componentname *cnp;
1029 struct componentname cn;
1034 UNIONFS_INTERNAL_DEBUG("unionfs_remove: enter\n");
1036 KASSERT_UNIONFS_VNODE(ap->a_dvp);
1039 dunp = VTOUNIONFS(ap->a_dvp);
1040 udvp = dunp->un_uppervp;
1044 if (ap->a_vp->v_op != &unionfs_vnodeops) {
1045 if (ap->a_vp->v_type != VSOCK)
1048 vp = uvp = lvp = NULLVP;
1050 VOP_UNLOCK(ap->a_vp);
1051 error = unionfs_relookup(udvp, &vp, cnp, &cn, td,
1052 cnp->cn_nameptr, cnp->cn_namelen, DELETE);
1053 if (error != 0 && error != ENOENT) {
1054 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1058 if (error == 0 && vp == ap->a_vp) {
1059 /* target vnode in upper */
1063 /* target vnode in lower */
1070 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1073 path = cnp->cn_nameptr;
1074 pathlen = cnp->cn_namelen;
1076 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
1077 unp = VTOUNIONFS(ap->a_vp);
1078 uvp = unp->un_uppervp;
1079 lvp = unp->un_lowervp;
1080 path = unp->un_path;
1081 pathlen = unp->un_pathlen;
1087 if (uvp != NULLVP) {
1089 * XXX: if the vnode type is VSOCK, it will create whiteout
1092 if (ump == NULL || ump->um_whitemode == UNIONFS_WHITE_ALWAYS ||
1094 cnp->cn_flags |= DOWHITEOUT;
1095 error = VOP_REMOVE(udvp, uvp, cnp);
1096 } else if (lvp != NULLVP)
1097 error = unionfs_mkwhiteout(udvp, cnp, td, path, pathlen);
1099 UNIONFS_INTERNAL_DEBUG("unionfs_remove: leave (%d)\n", error);
1105 unionfs_link(struct vop_link_args *ap)
1107 struct unionfs_node *dunp;
1108 struct unionfs_node *unp;
1111 struct componentname *cnp;
1116 UNIONFS_INTERNAL_DEBUG("unionfs_link: enter\n");
1118 KASSERT_UNIONFS_VNODE(ap->a_tdvp);
1119 KASSERT_UNIONFS_VNODE(ap->a_vp);
1123 dunp = VTOUNIONFS(ap->a_tdvp);
1125 udvp = dunp->un_uppervp;
1133 if (ap->a_vp->v_op != &unionfs_vnodeops)
1136 unp = VTOUNIONFS(ap->a_vp);
1138 if (unp->un_uppervp == NULLVP) {
1139 if (ap->a_vp->v_type != VREG)
1140 return (EOPNOTSUPP);
1142 error = unionfs_copyfile(unp, 1, cnp->cn_cred, td);
1147 uvp = unp->un_uppervp;
1150 if (needrelookup != 0)
1151 error = unionfs_relookup_for_create(ap->a_tdvp, cnp, td);
1154 error = VOP_LINK(udvp, uvp, cnp);
1156 UNIONFS_INTERNAL_DEBUG("unionfs_link: leave (%d)\n", error);
1162 unionfs_rename(struct vop_rename_args *ap)
1166 struct componentname *fcnp;
1169 struct componentname *tcnp;
1170 struct vnode *ltdvp;
1174 /* rename target vnodes */
1175 struct vnode *rfdvp;
1177 struct vnode *rtdvp;
1180 struct unionfs_mount *ump;
1181 struct unionfs_node *unp;
1185 UNIONFS_INTERNAL_DEBUG("unionfs_rename: enter\n");
1203 /* check for cross device rename */
1204 if (fvp->v_mount != tdvp->v_mount ||
1205 (tvp != NULLVP && fvp->v_mount != tvp->v_mount)) {
1206 if (fvp->v_op != &unionfs_vnodeops)
1210 goto unionfs_rename_abort;
1213 /* Renaming a file to itself has no effect. */
1215 goto unionfs_rename_abort;
1218 * from/to vnode is unionfs node.
1221 KASSERT_UNIONFS_VNODE(fdvp);
1222 KASSERT_UNIONFS_VNODE(fvp);
1223 KASSERT_UNIONFS_VNODE(tdvp);
1225 KASSERT_UNIONFS_VNODE(tvp);
1227 unp = VTOUNIONFS(fdvp);
1228 #ifdef UNIONFS_IDBG_RENAME
1229 UNIONFS_INTERNAL_DEBUG("fdvp=%p, ufdvp=%p, lfdvp=%p\n",
1230 fdvp, unp->un_uppervp, unp->un_lowervp);
1232 if (unp->un_uppervp == NULLVP) {
1234 goto unionfs_rename_abort;
1236 rfdvp = unp->un_uppervp;
1239 unp = VTOUNIONFS(fvp);
1240 #ifdef UNIONFS_IDBG_RENAME
1241 UNIONFS_INTERNAL_DEBUG("fvp=%p, ufvp=%p, lfvp=%p\n",
1242 fvp, unp->un_uppervp, unp->un_lowervp);
1244 ump = MOUNTTOUNIONFSMOUNT(fvp->v_mount);
1245 if (unp->un_uppervp == NULLVP) {
1246 switch (fvp->v_type) {
1248 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1249 goto unionfs_rename_abort;
1250 error = unionfs_copyfile(unp, 1, fcnp->cn_cred, td);
1253 goto unionfs_rename_abort;
1256 if ((error = vn_lock(fvp, LK_EXCLUSIVE)) != 0)
1257 goto unionfs_rename_abort;
1258 error = unionfs_mkshadowdir(ump, rfdvp, unp, fcnp, td);
1261 goto unionfs_rename_abort;
1265 goto unionfs_rename_abort;
1271 if (unp->un_lowervp != NULLVP)
1272 fcnp->cn_flags |= DOWHITEOUT;
1273 rfvp = unp->un_uppervp;
1276 unp = VTOUNIONFS(tdvp);
1277 #ifdef UNIONFS_IDBG_RENAME
1278 UNIONFS_INTERNAL_DEBUG("tdvp=%p, utdvp=%p, ltdvp=%p\n",
1279 tdvp, unp->un_uppervp, unp->un_lowervp);
1281 if (unp->un_uppervp == NULLVP) {
1283 goto unionfs_rename_abort;
1285 rtdvp = unp->un_uppervp;
1286 ltdvp = unp->un_lowervp;
1292 } else if (tvp != NULLVP) {
1293 unp = VTOUNIONFS(tvp);
1294 #ifdef UNIONFS_IDBG_RENAME
1295 UNIONFS_INTERNAL_DEBUG("tvp=%p, utvp=%p, ltvp=%p\n",
1296 tvp, unp->un_uppervp, unp->un_lowervp);
1298 if (unp->un_uppervp == NULLVP)
1301 if (tvp->v_type == VDIR) {
1303 goto unionfs_rename_abort;
1305 rtvp = unp->un_uppervp;
1306 ltvp = unp->un_lowervp;
1312 goto unionfs_rename_abort;
1314 if (needrelookup != 0) {
1315 if ((error = vn_lock(fdvp, LK_EXCLUSIVE)) != 0)
1316 goto unionfs_rename_abort;
1317 error = unionfs_relookup_for_delete(fdvp, fcnp, td);
1320 goto unionfs_rename_abort;
1322 /* Lock of tvp is canceled in order to avoid recursive lock. */
1323 if (tvp != NULLVP && tvp != tdvp)
1325 error = unionfs_relookup_for_rename(tdvp, tcnp, td);
1326 if (tvp != NULLVP && tvp != tdvp)
1327 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
1329 goto unionfs_rename_abort;
1332 error = VOP_RENAME(rfdvp, rfvp, fcnp, rtdvp, rtvp, tcnp);
1335 if (rtvp != NULLVP && rtvp->v_type == VDIR)
1337 if (fvp->v_type == VDIR && fdvp != tdvp)
1341 if (ltdvp != NULLVP)
1347 if (tvp != rtvp && tvp != NULLVP) {
1358 UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error);
1362 unionfs_rename_abort:
1366 if (tvp != NULLVP) {
1372 if (tvp != rtvp && rtvp != NULLVP)
1381 UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error);
1387 unionfs_mkdir(struct vop_mkdir_args *ap)
1389 struct unionfs_node *dunp;
1390 struct componentname *cnp;
1398 UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: enter\n");
1400 KASSERT_UNIONFS_VNODE(ap->a_dvp);
1404 dunp = VTOUNIONFS(dvp);
1406 lkflags = cnp->cn_lkflags;
1407 udvp = dunp->un_uppervp;
1409 if (udvp != NULLVP) {
1412 if (!(cnp->cn_flags & ISWHITEOUT)) {
1413 error = VOP_GETATTR(udvp, &va, cnp->cn_cred);
1415 goto unionfs_mkdir_cleanup;
1416 if ((va.va_flags & OPAQUE) != 0)
1417 cnp->cn_flags |= ISWHITEOUT;
1420 if ((error = VOP_MKDIR(udvp, &uvp, cnp, ap->a_vap)) == 0) {
1422 cnp->cn_lkflags = LK_EXCLUSIVE;
1424 * The underlying VOP_MKDIR() implementation may have
1425 * temporarily dropped the parent directory vnode lock.
1426 * Because the unionfs vnode ordinarily shares that
1427 * lock, this may allow the unionfs vnode to be reclaimed
1428 * and its lock field reset. In that case, the unionfs
1429 * vnode is effectively no longer locked, and we must
1430 * explicitly lock it before returning in order to meet
1431 * the locking requirements of VOP_MKDIR().
1433 if (__predict_false(VTOUNIONFS(dvp) == NULL)) {
1435 goto unionfs_mkdir_cleanup;
1437 error = unionfs_nodeget(dvp->v_mount, uvp, NULLVP,
1438 dvp, ap->a_vpp, cnp);
1439 cnp->cn_lkflags = lkflags;
1444 unionfs_mkdir_cleanup:
1446 if (__predict_false(VTOUNIONFS(dvp) == NULL)) {
1448 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
1449 } else if (udvp != NULLVP)
1452 UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: leave (%d)\n", error);
1458 unionfs_rmdir(struct vop_rmdir_args *ap)
1460 struct unionfs_node *dunp;
1461 struct unionfs_node *unp;
1462 struct unionfs_mount *ump;
1463 struct componentname *cnp;
1470 UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: enter\n");
1472 KASSERT_UNIONFS_VNODE(ap->a_dvp);
1473 KASSERT_UNIONFS_VNODE(ap->a_vp);
1476 dunp = VTOUNIONFS(ap->a_dvp);
1477 unp = VTOUNIONFS(ap->a_vp);
1480 udvp = dunp->un_uppervp;
1481 uvp = unp->un_uppervp;
1482 lvp = unp->un_lowervp;
1488 return (EOPNOTSUPP);
1490 if (uvp != NULLVP) {
1491 if (lvp != NULLVP) {
1492 error = unionfs_check_rmdir(ap->a_vp, cnp->cn_cred, td);
1496 ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
1497 if (ump->um_whitemode == UNIONFS_WHITE_ALWAYS || lvp != NULLVP)
1498 cnp->cn_flags |= DOWHITEOUT;
1500 * The relookup path will need to relock the parent dvp and
1501 * possibly the vp as well. Locking is expected to be done
1502 * in parent->child order; drop the lock on vp to avoid LOR
1503 * and potential recursion on vp's lock.
1504 * vp is expected to remain referenced during VOP_RMDIR(),
1505 * so vref/vrele should not be necessary here.
1507 VOP_UNLOCK(ap->a_vp);
1508 VNPASS(vrefcnt(ap->a_vp) > 0, ap->a_vp);
1509 error = unionfs_relookup_for_delete(ap->a_dvp, cnp, td);
1510 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1512 * VOP_RMDIR is dispatched against udvp, so if uvp became
1513 * doomed while the lock was dropped above the target
1514 * filesystem may not be able to cope.
1516 if (error == 0 && VN_IS_DOOMED(uvp))
1519 error = VOP_RMDIR(udvp, uvp, cnp);
1520 } else if (lvp != NULLVP)
1521 error = unionfs_mkwhiteout(udvp, cnp, td,
1522 unp->un_path, unp->un_pathlen);
1525 cache_purge(ap->a_dvp);
1526 cache_purge(ap->a_vp);
1529 UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: leave (%d)\n", error);
1535 unionfs_symlink(struct vop_symlink_args *ap)
1537 struct unionfs_node *dunp;
1538 struct componentname *cnp;
1544 UNIONFS_INTERNAL_DEBUG("unionfs_symlink: enter\n");
1546 KASSERT_UNIONFS_VNODE(ap->a_dvp);
1549 dunp = VTOUNIONFS(ap->a_dvp);
1551 lkflags = cnp->cn_lkflags;
1552 udvp = dunp->un_uppervp;
1554 if (udvp != NULLVP) {
1555 error = VOP_SYMLINK(udvp, &uvp, cnp, ap->a_vap, ap->a_target);
1558 cnp->cn_lkflags = LK_EXCLUSIVE;
1559 error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP,
1560 ap->a_dvp, ap->a_vpp, cnp);
1561 cnp->cn_lkflags = lkflags;
1566 UNIONFS_INTERNAL_DEBUG("unionfs_symlink: leave (%d)\n", error);
1572 unionfs_readdir(struct vop_readdir_args *ap)
1574 struct unionfs_node *unp;
1575 struct unionfs_node_status *unsp;
1583 uint64_t *cookies_bk;
1588 enum unionfs_lkupgrade lkstatus;
1590 UNIONFS_INTERNAL_DEBUG("unionfs_readdir: enter\n");
1592 KASSERT_UNIONFS_VNODE(ap->a_vp);
1605 if (vp->v_type != VDIR)
1609 * If the vnode is reclaimed while upgrading, we can't safely use unp
1610 * or do anything else unionfs- specific.
1612 lkstatus = unionfs_upgrade_lock(vp);
1613 if (lkstatus == UNIONFS_LKUPGRADE_DOOMED)
1616 unp = VTOUNIONFS(vp);
1617 uvp = unp->un_uppervp;
1618 lvp = unp->un_lowervp;
1619 /* check the open count. unionfs needs open before readdir. */
1620 unionfs_get_node_status(unp, td, &unsp);
1621 if ((uvp != NULLVP && unsp->uns_upper_opencnt <= 0) ||
1622 (lvp != NULLVP && unsp->uns_lower_opencnt <= 0)) {
1623 unionfs_tryrem_node_status(unp, unsp);
1627 unionfs_downgrade_lock(vp, lkstatus);
1629 goto unionfs_readdir_exit;
1632 if (uvp != NULLVP && lvp != NULLVP) {
1633 if ((error = VOP_GETATTR(uvp, &va, ap->a_cred)) != 0)
1634 goto unionfs_readdir_exit;
1635 if (va.va_flags & OPAQUE)
1640 if (uvp != NULLVP && lvp == NULLVP) {
1641 error = VOP_READDIR(uvp, uio, ap->a_cred, ap->a_eofflag,
1642 ap->a_ncookies, ap->a_cookies);
1643 unsp->uns_readdir_status = 0;
1645 goto unionfs_readdir_exit;
1649 if (uvp == NULLVP && lvp != NULLVP) {
1650 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag,
1651 ap->a_ncookies, ap->a_cookies);
1652 unsp->uns_readdir_status = 2;
1654 goto unionfs_readdir_exit;
1658 * readdir upper and lower
1660 KASSERT(uvp != NULLVP, ("unionfs_readdir: null upper vp"));
1661 KASSERT(lvp != NULLVP, ("unionfs_readdir: null lower vp"));
1662 if (uio->uio_offset == 0)
1663 unsp->uns_readdir_status = 0;
1665 if (unsp->uns_readdir_status == 0) {
1667 error = VOP_READDIR(uvp, uio, ap->a_cred, &eofflag,
1668 ap->a_ncookies, ap->a_cookies);
1670 if (error != 0 || eofflag == 0)
1671 goto unionfs_readdir_exit;
1672 unsp->uns_readdir_status = 1;
1675 * UFS(and other FS) needs size of uio_resid larger than
1677 * size of DIRBLKSIZ equals DEV_BSIZE.
1678 * (see: ufs/ufs/ufs_vnops.c ufs_readdir func , ufs/ufs/dir.h)
1680 if (uio->uio_resid <= (uio->uio_resid & (DEV_BSIZE -1)))
1681 goto unionfs_readdir_exit;
1685 * It prepares to readdir in lower.
1687 if (ap->a_ncookies != NULL) {
1688 ncookies_bk = *(ap->a_ncookies);
1689 *(ap->a_ncookies) = 0;
1691 if (ap->a_cookies != NULL) {
1692 cookies_bk = *(ap->a_cookies);
1693 *(ap->a_cookies) = NULL;
1697 /* initialize for readdir in lower */
1698 if (unsp->uns_readdir_status == 1) {
1699 unsp->uns_readdir_status = 2;
1701 * Backup uio_offset. See the comment after the
1702 * VOP_READDIR call on the lower layer.
1704 uio_offset_bk = uio->uio_offset;
1705 uio->uio_offset = 0;
1708 if (lvp == NULLVP) {
1710 goto unionfs_readdir_exit;
1713 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag,
1714 ap->a_ncookies, ap->a_cookies);
1717 * We can't return an uio_offset of 0: this would trigger an
1718 * infinite loop, because the next call to unionfs_readdir would
1719 * always restart with the upper layer (uio_offset == 0) and
1720 * always return some data.
1722 * This happens when the lower layer root directory is removed.
1723 * (A root directory deleting of unionfs should not be permitted.
1724 * But current VFS can not do it.)
1726 if (uio->uio_offset == 0)
1727 uio->uio_offset = uio_offset_bk;
1729 if (cookies_bk != NULL) {
1732 uint64_t *newcookies, *pos;
1734 size = *(ap->a_ncookies) + ncookies_bk;
1735 newcookies = (uint64_t *) malloc(size * sizeof(*newcookies),
1739 memcpy(pos, cookies_bk, ncookies_bk * sizeof(*newcookies));
1741 memcpy(pos, *(ap->a_cookies),
1742 *(ap->a_ncookies) * sizeof(*newcookies));
1743 free(cookies_bk, M_TEMP);
1744 free(*(ap->a_cookies), M_TEMP);
1745 *(ap->a_ncookies) = size;
1746 *(ap->a_cookies) = newcookies;
1749 unionfs_readdir_exit:
1750 if (error != 0 && ap->a_eofflag != NULL)
1751 *(ap->a_eofflag) = 1;
1753 UNIONFS_INTERNAL_DEBUG("unionfs_readdir: leave (%d)\n", error);
1759 unionfs_readlink(struct vop_readlink_args *ap)
1761 struct unionfs_node *unp;
1765 UNIONFS_INTERNAL_DEBUG("unionfs_readlink: enter\n");
1767 KASSERT_UNIONFS_VNODE(ap->a_vp);
1769 unp = VTOUNIONFS(ap->a_vp);
1770 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1772 error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
1774 UNIONFS_INTERNAL_DEBUG("unionfs_readlink: leave (%d)\n", error);
1780 unionfs_getwritemount(struct vop_getwritemount_args *ap)
1782 struct unionfs_node *unp;
1784 struct vnode *vp, *ovp;
1787 UNIONFS_INTERNAL_DEBUG("unionfs_getwritemount: enter\n");
1794 unp = VTOUNIONFS(vp);
1796 uvp = unp->un_uppervp;
1799 * If our node has no upper vnode, check the parent directory.
1800 * We may be initiating a write operation that will produce a
1801 * new upper vnode through CoW.
1803 if (uvp == NULLVP && unp != NULL) {
1807 * Only the root vnode should have an empty parent, but it
1808 * should not have an empty uppervp, so we shouldn't get here.
1810 VNASSERT(vp != NULL, ovp, ("%s: NULL parent vnode", __func__));
1813 unp = VTOUNIONFS(vp);
1815 uvp = unp->un_uppervp;
1820 if (uvp != NULLVP) {
1823 error = VOP_GETWRITEMOUNT(uvp, ap->a_mpp);
1827 *(ap->a_mpp) = NULL;
1830 UNIONFS_INTERNAL_DEBUG("unionfs_getwritemount: leave (%d)\n", error);
1836 unionfs_inactive(struct vop_inactive_args *ap)
1838 ap->a_vp->v_object = NULL;
1844 unionfs_reclaim(struct vop_reclaim_args *ap)
1846 /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: enter\n"); */
1848 unionfs_noderem(ap->a_vp);
1850 /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: leave\n"); */
1856 unionfs_print(struct vop_print_args *ap)
1858 struct unionfs_node *unp;
1859 /* struct unionfs_node_status *unsp; */
1861 unp = VTOUNIONFS(ap->a_vp);
1862 /* unionfs_get_node_status(unp, curthread, &unsp); */
1864 printf("unionfs_vp=%p, uppervp=%p, lowervp=%p\n",
1865 ap->a_vp, unp->un_uppervp, unp->un_lowervp);
1867 printf("unionfs opencnt: uppervp=%d, lowervp=%d\n",
1868 unsp->uns_upper_opencnt, unsp->uns_lower_opencnt);
1871 if (unp->un_uppervp != NULLVP)
1872 vn_printf(unp->un_uppervp, "unionfs: upper ");
1873 if (unp->un_lowervp != NULLVP)
1874 vn_printf(unp->un_lowervp, "unionfs: lower ");
1880 unionfs_get_llt_revlock(struct vnode *vp, int flags)
1886 switch (flags & LK_TYPE_MASK) {
1888 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
1889 revlock = LK_UPGRADE;
1891 revlock = LK_RELEASE;
1895 revlock = LK_RELEASE;
1898 revlock = LK_UPGRADE;
1908 * The state of an acquired lock is adjusted similarly to
1909 * the time of error generating.
1910 * flags: LK_RELEASE or LK_UPGRADE
1913 unionfs_revlock(struct vnode *vp, int flags)
1915 if (flags & LK_RELEASE)
1916 VOP_UNLOCK_FLAGS(vp, flags);
1919 if (vn_lock(vp, flags) != 0)
1920 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1925 unionfs_lock(struct vop_lock1_args *ap)
1927 struct unionfs_node *unp;
1938 * TODO: rework the unionfs locking scheme.
1939 * It's not guaranteed to be safe to blindly lock two vnodes on
1940 * different mounts as is done here. Further, the entanglement
1941 * of locking both vnodes with the various options that can be
1942 * passed to VOP_LOCK() makes this code hard to reason about.
1943 * Instead, consider locking only the upper vnode, or the lower
1944 * vnode is the upper is not present, and taking separate measures
1945 * to lock both vnodes in the few cases when that is needed.
1950 flags = ap->a_flags;
1953 if (LK_RELEASE == (flags & LK_TYPE_MASK) || !(flags & LK_TYPE_MASK))
1954 return (VOP_UNLOCK_FLAGS(vp, flags | LK_RELEASE));
1956 if ((flags & LK_INTERLOCK) == 0)
1959 unp = VTOUNIONFS(vp);
1961 goto unionfs_lock_null_vnode;
1963 KASSERT_UNIONFS_VNODE(ap->a_vp);
1965 lvp = unp->un_lowervp;
1966 uvp = unp->un_uppervp;
1968 if ((revlock = unionfs_get_llt_revlock(vp, flags)) == 0)
1969 panic("unknown lock type: 0x%x", flags & LK_TYPE_MASK);
1972 * During unmount, the root vnode lock may be taken recursively,
1973 * because it may share the same v_vnlock field as the vnode covered by
1974 * the unionfs mount. The covered vnode is locked across VFS_UNMOUNT(),
1975 * and the same lock may be taken recursively here during vflush()
1976 * issued by unionfs_unmount().
1978 if ((flags & LK_TYPE_MASK) == LK_EXCLUSIVE &&
1979 (vp->v_vflag & VV_ROOT) != 0)
1980 flags |= LK_CANRECURSE;
1982 if (lvp != NULLVP) {
1983 if (uvp != NULLVP && flags & LK_UPGRADE) {
1985 * Share Lock is once released and a deadlock is
1992 VI_LOCK_FLAGS(lvp, MTX_DUPOK);
1993 flags |= LK_INTERLOCK;
1997 ap->a_flags &= ~LK_INTERLOCK;
1999 error = VOP_LOCK(lvp, flags);
2002 unp = VTOUNIONFS(vp);
2004 /* vnode is released. */
2011 goto unionfs_lock_fallback;
2015 if (error == 0 && uvp != NULLVP) {
2016 if (uhold && flags & LK_UPGRADE) {
2017 flags &= ~LK_TYPE_MASK;
2018 flags |= LK_EXCLUSIVE;
2020 VI_LOCK_FLAGS(uvp, MTX_DUPOK);
2021 flags |= LK_INTERLOCK;
2028 ap->a_flags &= ~LK_INTERLOCK;
2030 error = VOP_LOCK(uvp, flags);
2033 unp = VTOUNIONFS(vp);
2035 /* vnode is released. */
2040 if (lvp != NULLVP) {
2044 goto unionfs_lock_fallback;
2046 if (error != 0 && lvp != NULLVP) {
2049 unionfs_revlock(lvp, revlock);
2063 unionfs_lock_null_vnode:
2064 ap->a_flags |= LK_INTERLOCK;
2065 return (vop_stdlock(ap));
2067 unionfs_lock_fallback:
2069 * If we reach this point, we've discovered the unionfs vnode
2070 * has been reclaimed while the upper/lower vnode locks were
2071 * temporarily dropped. Such temporary droppage may happen
2072 * during the course of an LK_UPGRADE operation itself, and in
2073 * that case LK_UPGRADE must be cleared as the unionfs vnode's
2074 * lock has been reset to point to the standard v_lock field,
2075 * which has not previously been held.
2077 if (flags & LK_UPGRADE) {
2078 ap->a_flags &= ~LK_TYPE_MASK;
2079 ap->a_flags |= LK_EXCLUSIVE;
2081 return (vop_stdlock(ap));
2085 unionfs_unlock(struct vop_unlock_args *ap)
2090 struct unionfs_node *unp;
2094 KASSERT_UNIONFS_VNODE(ap->a_vp);
2100 unp = VTOUNIONFS(vp);
2102 goto unionfs_unlock_null_vnode;
2103 lvp = unp->un_lowervp;
2104 uvp = unp->un_uppervp;
2106 if (lvp != NULLVP) {
2108 error = VOP_UNLOCK(lvp);
2111 if (error == 0 && uvp != NULLVP) {
2114 error = VOP_UNLOCK(uvp);
2124 unionfs_unlock_null_vnode:
2125 return (vop_stdunlock(ap));
2129 unionfs_pathconf(struct vop_pathconf_args *ap)
2131 struct unionfs_node *unp;
2134 KASSERT_UNIONFS_VNODE(ap->a_vp);
2136 unp = VTOUNIONFS(ap->a_vp);
2137 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
2139 return (VOP_PATHCONF(vp, ap->a_name, ap->a_retval));
2143 unionfs_advlock(struct vop_advlock_args *ap)
2145 struct unionfs_node *unp;
2146 struct unionfs_node_status *unsp;
2152 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: enter\n");
2154 KASSERT_UNIONFS_VNODE(ap->a_vp);
2159 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2161 unp = VTOUNIONFS(ap->a_vp);
2162 uvp = unp->un_uppervp;
2164 if (uvp == NULLVP) {
2165 error = unionfs_copyfile(unp, 1, td->td_ucred, td);
2167 goto unionfs_advlock_abort;
2168 uvp = unp->un_uppervp;
2170 unionfs_get_node_status(unp, td, &unsp);
2171 if (unsp->uns_lower_opencnt > 0) {
2172 /* try reopen the vnode */
2173 error = VOP_OPEN(uvp, unsp->uns_lower_openmode,
2174 td->td_ucred, td, NULL);
2176 goto unionfs_advlock_abort;
2177 unsp->uns_upper_opencnt++;
2178 VOP_CLOSE(unp->un_lowervp, unsp->uns_lower_openmode,
2180 unsp->uns_lower_opencnt--;
2182 unionfs_tryrem_node_status(unp, unsp);
2187 error = VOP_ADVLOCK(uvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);
2189 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error);
2193 unionfs_advlock_abort:
2196 UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error);
2202 unionfs_strategy(struct vop_strategy_args *ap)
2204 struct unionfs_node *unp;
2207 KASSERT_UNIONFS_VNODE(ap->a_vp);
2209 unp = VTOUNIONFS(ap->a_vp);
2210 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
2214 panic("unionfs_strategy: nullvp");
2216 if (ap->a_bp->b_iocmd == BIO_WRITE && vp == unp->un_lowervp)
2217 panic("unionfs_strategy: writing to lowervp");
2220 return (VOP_STRATEGY(vp, ap->a_bp));
2224 unionfs_getacl(struct vop_getacl_args *ap)
2226 struct unionfs_node *unp;
2230 KASSERT_UNIONFS_VNODE(ap->a_vp);
2232 unp = VTOUNIONFS(ap->a_vp);
2233 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
2235 UNIONFS_INTERNAL_DEBUG("unionfs_getacl: enter\n");
2237 error = VOP_GETACL(vp, ap->a_type, ap->a_aclp, ap->a_cred, ap->a_td);
2239 UNIONFS_INTERNAL_DEBUG("unionfs_getacl: leave (%d)\n", error);
2245 unionfs_setacl(struct vop_setacl_args *ap)
2247 struct unionfs_node *unp;
2253 UNIONFS_INTERNAL_DEBUG("unionfs_setacl: enter\n");
2255 KASSERT_UNIONFS_VNODE(ap->a_vp);
2258 unp = VTOUNIONFS(ap->a_vp);
2259 uvp = unp->un_uppervp;
2260 lvp = unp->un_lowervp;
2263 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2266 if (uvp == NULLVP && lvp->v_type == VREG) {
2267 if ((error = unionfs_copyfile(unp, 1, ap->a_cred, td)) != 0)
2269 uvp = unp->un_uppervp;
2273 error = VOP_SETACL(uvp, ap->a_type, ap->a_aclp, ap->a_cred, td);
2275 UNIONFS_INTERNAL_DEBUG("unionfs_setacl: leave (%d)\n", error);
2281 unionfs_aclcheck(struct vop_aclcheck_args *ap)
2283 struct unionfs_node *unp;
2287 UNIONFS_INTERNAL_DEBUG("unionfs_aclcheck: enter\n");
2289 KASSERT_UNIONFS_VNODE(ap->a_vp);
2291 unp = VTOUNIONFS(ap->a_vp);
2292 vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
2294 error = VOP_ACLCHECK(vp, ap->a_type, ap->a_aclp, ap->a_cred, ap->a_td);
2296 UNIONFS_INTERNAL_DEBUG("unionfs_aclcheck: leave (%d)\n", error);
2302 unionfs_openextattr(struct vop_openextattr_args *ap)
2304 struct unionfs_node *unp;
2309 KASSERT_UNIONFS_VNODE(ap->a_vp);
2312 unp = VTOUNIONFS(vp);
2313 tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
2315 if ((tvp == unp->un_uppervp && (unp->un_flag & UNIONFS_OPENEXTU)) ||
2316 (tvp == unp->un_lowervp && (unp->un_flag & UNIONFS_OPENEXTL)))
2319 error = VOP_OPENEXTATTR(tvp, ap->a_cred, ap->a_td);
2322 if (vn_lock(vp, LK_UPGRADE) != 0)
2323 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2324 if (!VN_IS_DOOMED(vp)) {
2325 if (tvp == unp->un_uppervp)
2326 unp->un_flag |= UNIONFS_OPENEXTU;
2328 unp->un_flag |= UNIONFS_OPENEXTL;
2330 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
2337 unionfs_closeextattr(struct vop_closeextattr_args *ap)
2339 struct unionfs_node *unp;
2344 KASSERT_UNIONFS_VNODE(ap->a_vp);
2347 unp = VTOUNIONFS(vp);
2350 if (unp->un_flag & UNIONFS_OPENEXTU)
2351 tvp = unp->un_uppervp;
2352 else if (unp->un_flag & UNIONFS_OPENEXTL)
2353 tvp = unp->un_lowervp;
2356 return (EOPNOTSUPP);
2358 error = VOP_CLOSEEXTATTR(tvp, ap->a_commit, ap->a_cred, ap->a_td);
2361 if (vn_lock(vp, LK_UPGRADE) != 0)
2362 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2363 if (!VN_IS_DOOMED(vp)) {
2364 if (tvp == unp->un_uppervp)
2365 unp->un_flag &= ~UNIONFS_OPENEXTU;
2367 unp->un_flag &= ~UNIONFS_OPENEXTL;
2369 vn_lock(vp, LK_DOWNGRADE | LK_RETRY);
2376 unionfs_getextattr(struct vop_getextattr_args *ap)
2378 struct unionfs_node *unp;
2381 KASSERT_UNIONFS_VNODE(ap->a_vp);
2383 unp = VTOUNIONFS(ap->a_vp);
2386 if (unp->un_flag & UNIONFS_OPENEXTU)
2387 vp = unp->un_uppervp;
2388 else if (unp->un_flag & UNIONFS_OPENEXTL)
2389 vp = unp->un_lowervp;
2392 return (EOPNOTSUPP);
2394 return (VOP_GETEXTATTR(vp, ap->a_attrnamespace, ap->a_name,
2395 ap->a_uio, ap->a_size, ap->a_cred, ap->a_td));
2399 unionfs_setextattr(struct vop_setextattr_args *ap)
2401 struct unionfs_node *unp;
2409 KASSERT_UNIONFS_VNODE(ap->a_vp);
2412 unp = VTOUNIONFS(ap->a_vp);
2413 uvp = unp->un_uppervp;
2414 lvp = unp->un_lowervp;
2419 UNIONFS_INTERNAL_DEBUG("unionfs_setextattr: enter (un_flag=%x)\n",
2422 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2425 if (unp->un_flag & UNIONFS_OPENEXTU)
2426 ovp = unp->un_uppervp;
2427 else if (unp->un_flag & UNIONFS_OPENEXTL)
2428 ovp = unp->un_lowervp;
2431 return (EOPNOTSUPP);
2433 if (ovp == lvp && lvp->v_type == VREG) {
2434 VOP_CLOSEEXTATTR(lvp, 0, cred, td);
2435 if (uvp == NULLVP &&
2436 (error = unionfs_copyfile(unp, 1, cred, td)) != 0) {
2437 unionfs_setextattr_reopen:
2438 if ((unp->un_flag & UNIONFS_OPENEXTL) &&
2439 VOP_OPENEXTATTR(lvp, cred, td)) {
2441 panic("unionfs: VOP_OPENEXTATTR failed");
2443 unp->un_flag &= ~UNIONFS_OPENEXTL;
2445 goto unionfs_setextattr_abort;
2447 uvp = unp->un_uppervp;
2448 if ((error = VOP_OPENEXTATTR(uvp, cred, td)) != 0)
2449 goto unionfs_setextattr_reopen;
2450 unp->un_flag &= ~UNIONFS_OPENEXTL;
2451 unp->un_flag |= UNIONFS_OPENEXTU;
2456 error = VOP_SETEXTATTR(ovp, ap->a_attrnamespace, ap->a_name,
2457 ap->a_uio, cred, td);
2459 unionfs_setextattr_abort:
2460 UNIONFS_INTERNAL_DEBUG("unionfs_setextattr: leave (%d)\n", error);
2466 unionfs_listextattr(struct vop_listextattr_args *ap)
2468 struct unionfs_node *unp;
2471 KASSERT_UNIONFS_VNODE(ap->a_vp);
2473 unp = VTOUNIONFS(ap->a_vp);
2476 if (unp->un_flag & UNIONFS_OPENEXTU)
2477 vp = unp->un_uppervp;
2478 else if (unp->un_flag & UNIONFS_OPENEXTL)
2479 vp = unp->un_lowervp;
2482 return (EOPNOTSUPP);
2484 return (VOP_LISTEXTATTR(vp, ap->a_attrnamespace, ap->a_uio,
2485 ap->a_size, ap->a_cred, ap->a_td));
2489 unionfs_deleteextattr(struct vop_deleteextattr_args *ap)
2491 struct unionfs_node *unp;
2499 KASSERT_UNIONFS_VNODE(ap->a_vp);
2502 unp = VTOUNIONFS(ap->a_vp);
2503 uvp = unp->un_uppervp;
2504 lvp = unp->un_lowervp;
2509 UNIONFS_INTERNAL_DEBUG("unionfs_deleteextattr: enter (un_flag=%x)\n",
2512 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2515 if (unp->un_flag & UNIONFS_OPENEXTU)
2516 ovp = unp->un_uppervp;
2517 else if (unp->un_flag & UNIONFS_OPENEXTL)
2518 ovp = unp->un_lowervp;
2521 return (EOPNOTSUPP);
2523 if (ovp == lvp && lvp->v_type == VREG) {
2524 VOP_CLOSEEXTATTR(lvp, 0, cred, td);
2525 if (uvp == NULLVP &&
2526 (error = unionfs_copyfile(unp, 1, cred, td)) != 0) {
2527 unionfs_deleteextattr_reopen:
2528 if ((unp->un_flag & UNIONFS_OPENEXTL) &&
2529 VOP_OPENEXTATTR(lvp, cred, td)) {
2531 panic("unionfs: VOP_OPENEXTATTR failed");
2533 unp->un_flag &= ~UNIONFS_OPENEXTL;
2535 goto unionfs_deleteextattr_abort;
2537 uvp = unp->un_uppervp;
2538 if ((error = VOP_OPENEXTATTR(uvp, cred, td)) != 0)
2539 goto unionfs_deleteextattr_reopen;
2540 unp->un_flag &= ~UNIONFS_OPENEXTL;
2541 unp->un_flag |= UNIONFS_OPENEXTU;
2546 error = VOP_DELETEEXTATTR(ovp, ap->a_attrnamespace, ap->a_name,
2547 ap->a_cred, ap->a_td);
2549 unionfs_deleteextattr_abort:
2550 UNIONFS_INTERNAL_DEBUG("unionfs_deleteextattr: leave (%d)\n", error);
2556 unionfs_setlabel(struct vop_setlabel_args *ap)
2558 struct unionfs_node *unp;
2564 UNIONFS_INTERNAL_DEBUG("unionfs_setlabel: enter\n");
2566 KASSERT_UNIONFS_VNODE(ap->a_vp);
2569 unp = VTOUNIONFS(ap->a_vp);
2570 uvp = unp->un_uppervp;
2571 lvp = unp->un_lowervp;
2574 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2577 if (uvp == NULLVP && lvp->v_type == VREG) {
2578 if ((error = unionfs_copyfile(unp, 1, ap->a_cred, td)) != 0)
2580 uvp = unp->un_uppervp;
2584 error = VOP_SETLABEL(uvp, ap->a_label, ap->a_cred, td);
2586 UNIONFS_INTERNAL_DEBUG("unionfs_setlabel: leave (%d)\n", error);
2592 unionfs_vptofh(struct vop_vptofh_args *ap)
2594 return (EOPNOTSUPP);
2598 unionfs_add_writecount(struct vop_add_writecount_args *ap)
2600 struct vnode *tvp, *vp;
2601 struct unionfs_node *unp;
2602 int error, writerefs __diagused;
2605 unp = VTOUNIONFS(vp);
2606 tvp = unp->un_uppervp;
2607 KASSERT(tvp != NULL,
2608 ("%s: adding write ref without upper vnode", __func__));
2609 error = VOP_ADD_WRITECOUNT(tvp, ap->a_inc);
2613 * We need to track the write refs we've passed to the underlying
2614 * vnodes so that we can undo them in case we are forcibly unmounted.
2616 writerefs = atomic_fetchadd_int(&vp->v_writecount, ap->a_inc);
2617 /* text refs are bypassed to lowervp */
2618 VNASSERT(writerefs >= 0, vp,
2619 ("%s: invalid write count %d", __func__, writerefs));
2620 VNASSERT(writerefs + ap->a_inc >= 0, vp,
2621 ("%s: invalid write count inc %d + %d", __func__,
2622 writerefs, ap->a_inc));
2627 unionfs_vput_pair(struct vop_vput_pair_args *ap)
2630 struct vnode *dvp, *vp, **vpp, *lvp, *ldvp, *uvp, *udvp, *tempvp;
2631 struct unionfs_node *dunp, *unp;
2641 dunp = VTOUNIONFS(dvp);
2642 udvp = dunp->un_uppervp;
2643 ldvp = dunp->un_lowervp;
2646 * Underlying vnodes should be locked because the encompassing unionfs
2647 * node is locked, but will not be referenced, as the reference will
2648 * only be on the unionfs node. Reference them now so that the vput()s
2649 * performed by VOP_VPUT_PAIR() will have a reference to drop.
2660 unp = VTOUNIONFS(vp);
2661 uvp = unp->un_uppervp;
2662 lvp = unp->un_lowervp;
2669 * If we're being asked to return a locked child vnode, then
2670 * we may need to create a replacement vnode in case the
2671 * original is reclaimed while the lock is dropped. In that
2672 * case we'll need to ensure the mount and the underlying
2673 * vnodes aren't also recycled during that window.
2675 if (!ap->a_unlock_vp) {
2687 * TODO: Because unionfs_lock() locks both the lower and upper vnodes
2688 * (if available), we must also call VOP_VPUT_PAIR() on both the lower
2689 * and upper parent/child pairs. If unionfs_lock() is reworked to lock
2690 * only a single vnode, this code will need to change to also only
2691 * operate on one vnode pair.
2693 ASSERT_VOP_LOCKED(ldvp, __func__);
2694 ASSERT_VOP_LOCKED(udvp, __func__);
2695 ASSERT_VOP_LOCKED(lvp, __func__);
2696 ASSERT_VOP_LOCKED(uvp, __func__);
2698 KASSERT(lvp == NULLVP || ldvp != NULLVP,
2699 ("%s: NULL ldvp with non-NULL lvp", __func__));
2701 res = VOP_VPUT_PAIR(ldvp, lvp != NULLVP ? &lvp : NULL, true);
2702 KASSERT(uvp == NULLVP || udvp != NULLVP,
2703 ("%s: NULL udvp with non-NULL uvp", __func__));
2705 res = VOP_VPUT_PAIR(udvp, uvp != NULLVP ? &uvp : NULL, true);
2707 ASSERT_VOP_UNLOCKED(ldvp, __func__);
2708 ASSERT_VOP_UNLOCKED(udvp, __func__);
2709 ASSERT_VOP_UNLOCKED(lvp, __func__);
2710 ASSERT_VOP_UNLOCKED(uvp, __func__);
2713 * VOP_VPUT_PAIR() dropped the references we added to the underlying
2714 * vnodes, now drop the caller's reference to the unionfs vnodes.
2716 if (vp != NULLVP && ap->a_unlock_vp)
2720 if (vp == NULLVP || ap->a_unlock_vp)
2724 * We're being asked to return a locked vnode. At this point, the
2725 * underlying vnodes have been unlocked, so vp may have been reclaimed.
2727 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2728 if (vp->v_data == NULL && vfs_busy(mp, MBF_NOWAIT) == 0) {
2730 error = unionfs_nodeget(mp, uvp, lvp, dvp, &tempvp, NULL);
2732 vn_lock(tempvp, LK_EXCLUSIVE | LK_RETRY);
2735 vget(vp, LK_EXCLUSIVE | LK_RETRY);
2749 unionfs_set_text(struct vop_set_text_args *ap)
2752 struct unionfs_node *unp;
2756 * We assume text refs are managed against lvp/uvp through the
2757 * executable mapping backed by its VM object. We therefore don't
2758 * need to track leased text refs in the case of a forcible unmount.
2760 unp = VTOUNIONFS(ap->a_vp);
2761 ASSERT_VOP_LOCKED(ap->a_vp, __func__);
2762 tvp = unp->un_uppervp != NULL ? unp->un_uppervp : unp->un_lowervp;
2763 error = VOP_SET_TEXT(tvp);
2768 unionfs_unset_text(struct vop_unset_text_args *ap)
2771 struct unionfs_node *unp;
2773 ASSERT_VOP_LOCKED(ap->a_vp, __func__);
2774 unp = VTOUNIONFS(ap->a_vp);
2775 tvp = unp->un_uppervp != NULL ? unp->un_uppervp : unp->un_lowervp;
2776 VOP_UNSET_TEXT_CHECKED(tvp);
2780 struct vop_vector unionfs_vnodeops = {
2781 .vop_default = &default_vnodeops,
2783 .vop_access = unionfs_access,
2784 .vop_aclcheck = unionfs_aclcheck,
2785 .vop_advlock = unionfs_advlock,
2786 .vop_bmap = VOP_EOPNOTSUPP,
2787 .vop_cachedlookup = unionfs_lookup,
2788 .vop_close = unionfs_close,
2789 .vop_closeextattr = unionfs_closeextattr,
2790 .vop_create = unionfs_create,
2791 .vop_deleteextattr = unionfs_deleteextattr,
2792 .vop_fsync = unionfs_fsync,
2793 .vop_getacl = unionfs_getacl,
2794 .vop_getattr = unionfs_getattr,
2795 .vop_getextattr = unionfs_getextattr,
2796 .vop_getwritemount = unionfs_getwritemount,
2797 .vop_inactive = unionfs_inactive,
2798 .vop_need_inactive = vop_stdneed_inactive,
2799 .vop_islocked = vop_stdislocked,
2800 .vop_ioctl = unionfs_ioctl,
2801 .vop_link = unionfs_link,
2802 .vop_listextattr = unionfs_listextattr,
2803 .vop_lock1 = unionfs_lock,
2804 .vop_lookup = vfs_cache_lookup,
2805 .vop_mkdir = unionfs_mkdir,
2806 .vop_mknod = unionfs_mknod,
2807 .vop_open = unionfs_open,
2808 .vop_openextattr = unionfs_openextattr,
2809 .vop_pathconf = unionfs_pathconf,
2810 .vop_poll = unionfs_poll,
2811 .vop_print = unionfs_print,
2812 .vop_read = unionfs_read,
2813 .vop_readdir = unionfs_readdir,
2814 .vop_readlink = unionfs_readlink,
2815 .vop_reclaim = unionfs_reclaim,
2816 .vop_remove = unionfs_remove,
2817 .vop_rename = unionfs_rename,
2818 .vop_rmdir = unionfs_rmdir,
2819 .vop_setacl = unionfs_setacl,
2820 .vop_setattr = unionfs_setattr,
2821 .vop_setextattr = unionfs_setextattr,
2822 .vop_setlabel = unionfs_setlabel,
2823 .vop_strategy = unionfs_strategy,
2824 .vop_symlink = unionfs_symlink,
2825 .vop_unlock = unionfs_unlock,
2826 .vop_whiteout = unionfs_whiteout,
2827 .vop_write = unionfs_write,
2828 .vop_vptofh = unionfs_vptofh,
2829 .vop_add_writecount = unionfs_add_writecount,
2830 .vop_vput_pair = unionfs_vput_pair,
2831 .vop_set_text = unionfs_set_text,
2832 .vop_unset_text = unionfs_unset_text,
2834 VFS_VOP_VECTOR_REGISTER(unionfs_vnodeops);