2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1994 Jan-Simon Pendry
6 * The Regents of the University of California. All rights reserved.
7 * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
8 * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
10 * This code is derived from software contributed to Berkeley by
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
45 #include <sys/mutex.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/dirent.h>
52 #include <sys/fcntl.h>
53 #include <sys/filedesc.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 #include <sys/resourcevar.h>
59 #include <machine/atomic.h>
61 #include <security/mac/mac_framework.h>
65 #include <fs/unionfs/union.h>
67 #define NUNIONFSNODECACHE 16
68 #define UNIONFSHASHMASK (NUNIONFSNODECACHE - 1)
70 static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table");
71 MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part");
72 MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
74 static struct task unionfs_deferred_rele_task;
75 static struct mtx unionfs_deferred_rele_lock;
76 static STAILQ_HEAD(, unionfs_node) unionfs_deferred_rele_list =
77 STAILQ_HEAD_INITIALIZER(unionfs_deferred_rele_list);
78 static TASKQUEUE_DEFINE_THREAD(unionfs_rele);
80 unsigned int unionfs_ndeferred = 0;
81 SYSCTL_UINT(_vfs, OID_AUTO, unionfs_ndeferred, CTLFLAG_RD,
82 &unionfs_ndeferred, 0, "unionfs deferred vnode release");
84 static void unionfs_deferred_rele(void *, int);
90 unionfs_init(struct vfsconf *vfsp)
92 UNIONFSDEBUG("unionfs_init\n"); /* printed during system boot */
93 TASK_INIT(&unionfs_deferred_rele_task, 0, unionfs_deferred_rele, NULL);
94 mtx_init(&unionfs_deferred_rele_lock, "uniondefr", NULL, MTX_DEF);
102 unionfs_uninit(struct vfsconf *vfsp)
104 taskqueue_quiesce(taskqueue_unionfs_rele);
105 taskqueue_free(taskqueue_unionfs_rele);
106 mtx_destroy(&unionfs_deferred_rele_lock);
111 unionfs_deferred_rele(void *arg __unused, int pending __unused)
113 STAILQ_HEAD(, unionfs_node) local_rele_list;
114 struct unionfs_node *unp, *tunp;
115 unsigned int ndeferred;
118 STAILQ_INIT(&local_rele_list);
119 mtx_lock(&unionfs_deferred_rele_lock);
120 STAILQ_CONCAT(&local_rele_list, &unionfs_deferred_rele_list);
121 mtx_unlock(&unionfs_deferred_rele_lock);
122 STAILQ_FOREACH_SAFE(unp, &local_rele_list, un_rele, tunp) {
124 MPASS(unp->un_dvp != NULL);
126 free(unp, M_UNIONFSNODE);
129 /* We expect this function to be single-threaded, thus no atomic */
130 unionfs_ndeferred += ndeferred;
133 static struct unionfs_node_hashhead *
134 unionfs_get_hashhead(struct vnode *dvp, struct vnode *lookup)
136 struct unionfs_node *unp;
138 unp = VTOUNIONFS(dvp);
140 return (&(unp->un_hashtbl[vfs_hash_index(lookup) & UNIONFSHASHMASK]));
144 * Attempt to lookup a cached unionfs vnode by upper/lower vp
145 * from dvp, with dvp's interlock held.
147 static struct vnode *
148 unionfs_get_cached_vnode_locked(struct vnode *lookup, struct vnode *dvp)
150 struct unionfs_node *unp;
151 struct unionfs_node_hashhead *hd;
154 hd = unionfs_get_hashhead(dvp, lookup);
156 LIST_FOREACH(unp, hd, un_hash) {
157 if (unp->un_uppervp == lookup ||
158 unp->un_lowervp == lookup) {
159 vp = UNIONFSTOV(unp);
160 VI_LOCK_FLAGS(vp, MTX_DUPOK);
161 vp->v_iflag &= ~VI_OWEINACT;
162 if (VN_IS_DOOMED(vp) ||
163 ((vp->v_iflag & VI_DOINGINACT) != 0)) {
179 * Get the cached vnode.
181 static struct vnode *
182 unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp,
190 vp = unionfs_get_cached_vnode_locked(uvp, dvp);
191 else if (lvp != NULLVP)
192 vp = unionfs_get_cached_vnode_locked(lvp, dvp);
199 * Add the new vnode into cache.
201 static struct vnode *
202 unionfs_ins_cached_vnode(struct unionfs_node *uncp,
205 struct unionfs_node_hashhead *hd;
208 ASSERT_VOP_ELOCKED(uncp->un_uppervp, __func__);
209 ASSERT_VOP_ELOCKED(uncp->un_lowervp, __func__);
210 KASSERT(uncp->un_uppervp == NULLVP || uncp->un_uppervp->v_type == VDIR,
211 ("%s: v_type != VDIR", __func__));
212 KASSERT(uncp->un_lowervp == NULLVP || uncp->un_lowervp->v_type == VDIR,
213 ("%s: v_type != VDIR", __func__));
217 if (uncp->un_uppervp != NULL)
218 vp = unionfs_get_cached_vnode_locked(uncp->un_uppervp, dvp);
219 else if (uncp->un_lowervp != NULL)
220 vp = unionfs_get_cached_vnode_locked(uncp->un_lowervp, dvp);
222 hd = unionfs_get_hashhead(dvp, (uncp->un_uppervp != NULLVP ?
223 uncp->un_uppervp : uncp->un_lowervp));
224 LIST_INSERT_HEAD(hd, uncp, un_hash);
235 unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp)
237 KASSERT(unp != NULL, ("%s: null node", __func__));
238 KASSERT(dvp != NULLVP,
239 ("%s: null parent vnode", __func__));
242 if (unp->un_hash.le_prev != NULL) {
243 LIST_REMOVE(unp, un_hash);
244 unp->un_hash.le_next = NULL;
245 unp->un_hash.le_prev = NULL;
251 * Common cleanup handling for unionfs_nodeget
252 * Upper, lower, and parent directory vnodes are expected to be referenced by
253 * the caller. Upper and lower vnodes, if non-NULL, are also expected to be
254 * exclusively locked by the caller.
255 * This function will return with the caller's locks and references undone.
258 unionfs_nodeget_cleanup(struct vnode *vp, struct unionfs_node *unp)
262 * Lock and reset the default vnode lock; vgone() expects a locked
263 * vnode, and we're going to reset the vnode ops.
265 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
268 * Clear out private data and reset the vnode ops to avoid use of
269 * unionfs vnode ops on a partially constructed vnode.
273 vp->v_vnlock = &vp->v_lock;
274 vp->v_op = &dead_vnodeops;
279 if (unp->un_dvp != NULLVP)
281 if (unp->un_uppervp != NULLVP)
282 vput(unp->un_uppervp);
283 if (unp->un_lowervp != NULLVP)
284 vput(unp->un_lowervp);
285 if (unp->un_hashtbl != NULL)
286 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
287 free(unp->un_path, M_UNIONFSPATH);
288 free(unp, M_UNIONFSNODE);
292 * Make a new or get existing unionfs node.
294 * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
295 * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
296 * you should not lock plurality simultaneously.
299 unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
300 struct vnode *lowervp, struct vnode *dvp, struct vnode **vpp,
301 struct componentname *cnp)
304 struct unionfs_mount *ump;
305 struct unionfs_node *unp;
310 __enum_uint8(vtype) vt;
313 ump = MOUNTTOUNIONFSMOUNT(mp);
314 lkflags = (cnp ? cnp->cn_lkflags : 0);
315 path = (cnp ? cnp->cn_nameptr : NULL);
318 if (uppervp == NULLVP && lowervp == NULLVP)
319 panic("%s: upper and lower is null", __func__);
321 vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
323 /* If it has no ISLASTCN flag, path check is skipped. */
324 if (cnp && !(cnp->cn_flags & ISLASTCN))
327 /* check the cache */
328 if (dvp != NULLVP && vt == VDIR) {
329 vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp);
332 goto unionfs_nodeget_out;
336 unp = malloc(sizeof(struct unionfs_node),
337 M_UNIONFSNODE, M_WAITOK | M_ZERO);
339 error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp);
341 free(unp, M_UNIONFSNODE);
346 if (uppervp != NULLVP)
348 if (lowervp != NULLVP)
352 unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH,
354 KASSERT(hashmask == UNIONFSHASHMASK,
355 ("unexpected unionfs hash mask 0x%lx", hashmask));
359 unp->un_uppervp = uppervp;
360 unp->un_lowervp = lowervp;
362 if (uppervp != NULLVP)
363 vp->v_vnlock = uppervp->v_vnlock;
365 vp->v_vnlock = lowervp->v_vnlock;
368 unp->un_path = malloc(cnp->cn_namelen + 1,
369 M_UNIONFSPATH, M_WAITOK | M_ZERO);
370 bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen);
371 unp->un_path[cnp->cn_namelen] = '\0';
372 unp->un_pathlen = cnp->cn_namelen;
378 * TODO: This is an imperfect check, as there's no guarantee that
379 * the underlying filesystems will always return vnode pointers
380 * for the root inodes that match our cached values. To reduce
381 * the likelihood of failure, for example in the case where either
382 * vnode has been forcibly doomed, we check both pointers and set
383 * VV_ROOT if either matches.
385 if (ump->um_uppervp == uppervp || ump->um_lowervp == lowervp)
386 vp->v_vflag |= VV_ROOT;
387 KASSERT(dvp != NULL || (vp->v_vflag & VV_ROOT) != 0,
388 ("%s: NULL dvp for non-root vp %p", __func__, vp));
390 vn_lock_pair(lowervp, false, LK_EXCLUSIVE, uppervp, false,
392 error = insmntque1(vp, mp);
394 unionfs_nodeget_cleanup(vp, unp);
397 if (lowervp != NULL && VN_IS_DOOMED(lowervp)) {
399 unp->un_lowervp = lowervp = NULL;
401 if (uppervp != NULL && VN_IS_DOOMED(uppervp)) {
403 unp->un_uppervp = uppervp = NULL;
404 if (lowervp != NULLVP)
405 vp->v_vnlock = lowervp->v_vnlock;
407 if (lowervp == NULL && uppervp == NULL) {
408 unionfs_nodeget_cleanup(vp, unp);
412 vn_set_state(vp, VSTATE_CONSTRUCTED);
414 if (dvp != NULLVP && vt == VDIR)
415 *vpp = unionfs_ins_cached_vnode(unp, dvp);
416 if (*vpp != NULLVP) {
417 unionfs_nodeget_cleanup(vp, unp);
428 if (lkflags & LK_TYPE_MASK)
429 vn_lock(vp, lkflags | LK_RETRY);
435 * Clean up the unionfs node.
438 unionfs_noderem(struct vnode *vp)
440 struct unionfs_node *unp, *unp_t1, *unp_t2;
441 struct unionfs_node_hashhead *hd;
442 struct unionfs_node_status *unsp, *unsp_tmp;
450 * The root vnode lock may be recursed during unmount, because
451 * it may share the same lock as the unionfs mount's covered vnode,
452 * which is locked across VFS_UNMOUNT(). This lock will then be
453 * recursively taken during the vflush() issued by unionfs_unmount().
454 * But we still only need to lock the unionfs lock once, because only
455 * one of those lock operations was taken against a unionfs vnode and
456 * will be undone against a unionfs vnode.
458 KASSERT(vp->v_vnlock->lk_recurse == 0 || (vp->v_vflag & VV_ROOT) != 0,
459 ("%s: vnode %p locked recursively", __func__, vp));
460 if (lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
461 panic("%s: failed to acquire lock for vnode lock", __func__);
464 * Use the interlock to protect the clearing of v_data to
465 * prevent faults in unionfs_lock().
468 unp = VTOUNIONFS(vp);
469 lvp = unp->un_lowervp;
470 uvp = unp->un_uppervp;
472 unp->un_lowervp = unp->un_uppervp = NULLVP;
473 vp->v_vnlock = &(vp->v_lock);
476 if (unp->un_hashtbl != NULL) {
478 * Clear out any cached child vnodes. This should only
479 * be necessary during forced unmount, when the vnode may
480 * be reclaimed with a non-zero use count. Otherwise the
481 * reference held by each child should prevent reclamation.
483 for (count = 0; count <= UNIONFSHASHMASK; count++) {
484 hd = unp->un_hashtbl + count;
485 LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) {
486 LIST_REMOVE(unp_t1, un_hash);
487 unp_t1->un_hash.le_next = NULL;
488 unp_t1->un_hash.le_prev = NULL;
494 writerefs = atomic_load_int(&vp->v_writecount);
495 VNASSERT(writerefs >= 0, vp,
496 ("%s: write count %d, unexpected text ref", __func__, writerefs));
498 * If we were opened for write, we leased the write reference
499 * to the lower vnode. If this is a reclamation due to the
500 * forced unmount, undo the reference now.
503 VNASSERT(uvp != NULL, vp,
504 ("%s: write reference without upper vnode", __func__));
505 VOP_ADD_WRITECOUNT(uvp, -writerefs);
513 unionfs_rem_cached_vnode(unp, dvp);
519 if (unp->un_path != NULL) {
520 free(unp->un_path, M_UNIONFSPATH);
525 if (unp->un_hashtbl != NULL) {
526 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
529 LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) {
530 LIST_REMOVE(unsp, uns_list);
534 mtx_lock(&unionfs_deferred_rele_lock);
535 STAILQ_INSERT_TAIL(&unionfs_deferred_rele_list, unp, un_rele);
536 mtx_unlock(&unionfs_deferred_rele_lock);
537 taskqueue_enqueue(taskqueue_unionfs_rele,
538 &unionfs_deferred_rele_task);
540 free(unp, M_UNIONFSNODE);
544 * Get the unionfs node status object for the vnode corresponding to unp,
545 * for the process that owns td. Allocate a new status object if one
546 * does not already exist.
549 unionfs_get_node_status(struct unionfs_node *unp, struct thread *td,
550 struct unionfs_node_status **unspp)
552 struct unionfs_node_status *unsp;
555 pid = td->td_proc->p_pid;
557 KASSERT(NULL != unspp, ("%s: NULL status", __func__));
558 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), __func__);
560 LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
561 if (unsp->uns_pid == pid) {
567 /* create a new unionfs node status */
568 unsp = malloc(sizeof(struct unionfs_node_status),
569 M_TEMP, M_WAITOK | M_ZERO);
572 LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
578 * Remove the unionfs node status, if you can.
579 * You need exclusive lock this vnode.
582 unionfs_tryrem_node_status(struct unionfs_node *unp,
583 struct unionfs_node_status *unsp)
585 KASSERT(NULL != unsp, ("%s: NULL status", __func__));
586 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), __func__);
588 if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
591 LIST_REMOVE(unsp, uns_list);
596 * Create upper node attr.
599 unionfs_create_uppervattr_core(struct unionfs_mount *ump, struct vattr *lva,
600 struct vattr *uva, struct thread *td)
603 uva->va_type = lva->va_type;
604 uva->va_atime = lva->va_atime;
605 uva->va_mtime = lva->va_mtime;
606 uva->va_ctime = lva->va_ctime;
608 switch (ump->um_copymode) {
609 case UNIONFS_TRANSPARENT:
610 uva->va_mode = lva->va_mode;
611 uva->va_uid = lva->va_uid;
612 uva->va_gid = lva->va_gid;
614 case UNIONFS_MASQUERADE:
615 if (ump->um_uid == lva->va_uid) {
616 uva->va_mode = lva->va_mode & 077077;
617 uva->va_mode |= (lva->va_type == VDIR ?
618 ump->um_udir : ump->um_ufile) & 0700;
619 uva->va_uid = lva->va_uid;
620 uva->va_gid = lva->va_gid;
622 uva->va_mode = (lva->va_type == VDIR ?
623 ump->um_udir : ump->um_ufile);
624 uva->va_uid = ump->um_uid;
625 uva->va_gid = ump->um_gid;
628 default: /* UNIONFS_TRADITIONAL */
629 uva->va_mode = 0777 & ~td->td_proc->p_pd->pd_cmask;
630 uva->va_uid = ump->um_uid;
631 uva->va_gid = ump->um_gid;
637 * Create upper node attr.
640 unionfs_create_uppervattr(struct unionfs_mount *ump, struct vnode *lvp,
641 struct vattr *uva, struct ucred *cred, struct thread *td)
646 if ((error = VOP_GETATTR(lvp, &lva, cred)))
649 unionfs_create_uppervattr_core(ump, &lva, uva, td);
657 * dvp should be locked on entry and will be locked on return.
659 * If an error is returned, *vpp will be invalid, otherwise it will hold a
660 * locked, referenced vnode. If *vpp == dvp then remember that only one
661 * LK_EXCLUSIVE lock is held.
664 unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
665 struct componentname *cnp, struct componentname *cn, struct thread *td,
666 char *path, int pathlen, u_long nameiop)
671 cn->cn_namelen = pathlen;
673 cn->cn_nameiop = nameiop;
674 cn->cn_flags = (LOCKPARENT | LOCKLEAF | ISLASTCN);
675 cn->cn_lkflags = LK_EXCLUSIVE;
676 cn->cn_cred = cnp->cn_cred;
677 cn->cn_nameptr = cn->cn_pnbuf;
680 if (nameiop == DELETE) {
681 cn->cn_flags |= (cnp->cn_flags & DOWHITEOUT);
682 } else if (nameiop == RENAME) {
684 } else if (nameiop == CREATE) {
685 cn->cn_flags |= NOCACHE;
691 if ((error = vfs_relookup(dvp, vpp, cn, refstart))) {
692 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
696 KASSERT(cn->cn_pnbuf == path, ("%s: cn_pnbuf changed", __func__));
702 * relookup for CREATE namei operation.
704 * dvp is unionfs vnode. dvp should be locked.
706 * If it called 'unionfs_copyfile' function by unionfs_link etc,
707 * VOP_LOOKUP information is broken.
708 * So it need relookup in order to create link etc.
711 unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp,
716 struct componentname cn;
719 udvp = UNIONFSVPTOUPPERVP(dvp);
722 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
723 cnp->cn_namelen, CREATE);
740 * relookup for DELETE namei operation.
742 * dvp is unionfs vnode. dvp should be locked.
745 unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp,
750 struct componentname cn;
753 udvp = UNIONFSVPTOUPPERVP(dvp);
756 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
757 cnp->cn_namelen, DELETE);
774 * relookup for RENAME namei operation.
776 * dvp is unionfs vnode. dvp should be locked.
779 unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp,
784 struct componentname cn;
787 udvp = UNIONFSVPTOUPPERVP(dvp);
790 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
791 cnp->cn_namelen, RENAME);
806 * Update the unionfs_node.
808 * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
809 * uvp's lock and lower's lock will be unlocked.
812 unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp,
815 struct unionfs_node_hashhead *hd;
819 unsigned count, lockrec;
821 vp = UNIONFSTOV(unp);
822 lvp = unp->un_lowervp;
823 ASSERT_VOP_ELOCKED(lvp, __func__);
824 ASSERT_VOP_ELOCKED(uvp, __func__);
827 VNASSERT(vp->v_writecount == 0, vp,
828 ("%s: non-zero writecount", __func__));
830 * Update the upper vnode's lock state to match the lower vnode,
831 * and then switch the unionfs vnode's lock to the upper vnode.
833 lockrec = lvp->v_vnlock->lk_recurse;
834 for (count = 0; count < lockrec; count++)
835 vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
837 unp->un_uppervp = uvp;
838 vp->v_vnlock = uvp->v_vnlock;
842 * Re-cache the unionfs vnode against the upper vnode
844 if (dvp != NULLVP && vp->v_type == VDIR) {
846 if (unp->un_hash.le_prev != NULL) {
847 LIST_REMOVE(unp, un_hash);
848 hd = unionfs_get_hashhead(dvp, uvp);
849 LIST_INSERT_HEAD(hd, unp, un_hash);
851 VI_UNLOCK(unp->un_dvp);
856 * Create a new shadow dir.
858 * udvp should be locked on entry and will be locked on return.
860 * If no error returned, unp will be updated.
863 unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
864 struct unionfs_node *unp, struct componentname *cnp, struct thread *td)
873 struct ucred *credbk;
874 struct uidinfo *rootinfo;
877 if (unp->un_uppervp != NULLVP)
880 lvp = unp->un_lowervp;
882 credbk = cnp->cn_cred;
884 /* Authority change to root */
885 rootinfo = uifind((uid_t)0);
886 cred = crdup(cnp->cn_cred);
888 * The calls to chgproccnt() are needed to compensate for change_ruid()
889 * calling chgproccnt().
891 chgproccnt(cred->cr_ruidinfo, 1, 0);
892 change_euid(cred, rootinfo);
893 change_ruid(cred, rootinfo);
894 change_svuid(cred, (uid_t)0);
898 memset(&nd.ni_cnd, 0, sizeof(struct componentname));
901 if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
902 goto unionfs_mkshadowdir_abort;
904 if ((error = unionfs_relookup(udvp, &uvp, cnp, &nd.ni_cnd, td,
905 cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
906 goto unionfs_mkshadowdir_abort;
914 goto unionfs_mkshadowdir_abort;
917 if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)))
918 goto unionfs_mkshadowdir_abort;
919 unionfs_create_uppervattr_core(ump, &lva, &va, td);
921 error = VOP_MKDIR(udvp, &uvp, &nd.ni_cnd, &va);
924 unionfs_node_update(unp, uvp, td);
927 * XXX The bug which cannot set uid/gid was corrected.
931 VOP_SETATTR(uvp, &va, nd.ni_cnd.cn_cred);
933 vn_finished_write(mp);
935 unionfs_mkshadowdir_abort:
936 cnp->cn_cred = credbk;
937 chgproccnt(cred->cr_ruidinfo, -1, 0);
944 * Create a new whiteout.
946 * dvp should be locked on entry and will be locked on return.
949 unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp,
950 struct thread *td, char *path, int pathlen)
959 if ((error = unionfs_relookup(dvp, &wvp, cnp, &nd.ni_cnd, td, path,
972 if ((error = vn_start_write(dvp, &mp, V_WAIT | V_PCATCH)))
973 goto unionfs_mkwhiteout_free_out;
974 error = VOP_WHITEOUT(dvp, &nd.ni_cnd, CREATE);
976 vn_finished_write(mp);
978 unionfs_mkwhiteout_free_out:
983 * Create a new vnode for create a new shadow file.
985 * If an error is returned, *vpp will be invalid, otherwise it will hold a
986 * locked, referenced and opened vnode.
988 * unp is never updated.
991 unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
992 struct unionfs_node *unp, struct vattr *uvap, struct thread *td)
994 struct unionfs_mount *ump;
1003 ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
1005 lvp = unp->un_lowervp;
1006 cred = td->td_ucred;
1007 fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
1010 if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
1012 unionfs_create_uppervattr_core(ump, &lva, uvap, td);
1014 if (unp->un_path == NULL)
1015 panic("%s: NULL un_path", __func__);
1017 nd.ni_cnd.cn_namelen = unp->un_pathlen;
1018 nd.ni_cnd.cn_pnbuf = unp->un_path;
1019 nd.ni_cnd.cn_nameiop = CREATE;
1020 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | ISLASTCN;
1021 nd.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
1022 nd.ni_cnd.cn_cred = cred;
1023 nd.ni_cnd.cn_nameptr = nd.ni_cnd.cn_pnbuf;
1027 if ((error = vfs_relookup(udvp, &vp, &nd.ni_cnd, false)) != 0)
1028 goto unionfs_vn_create_on_upper_free_out2;
1037 goto unionfs_vn_create_on_upper_free_out1;
1040 if ((error = VOP_CREATE(udvp, &vp, &nd.ni_cnd, uvap)) != 0)
1041 goto unionfs_vn_create_on_upper_free_out1;
1043 if ((error = VOP_OPEN(vp, fmode, cred, td, NULL)) != 0) {
1045 goto unionfs_vn_create_on_upper_free_out1;
1047 error = VOP_ADD_WRITECOUNT(vp, 1);
1048 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1049 __func__, vp, vp->v_writecount);
1053 VOP_CLOSE(vp, fmode, cred, td);
1056 unionfs_vn_create_on_upper_free_out1:
1059 unionfs_vn_create_on_upper_free_out2:
1060 KASSERT(nd.ni_cnd.cn_pnbuf == unp->un_path,
1061 ("%s: cn_pnbuf changed", __func__));
1067 * Copy from lvp to uvp.
1069 * lvp and uvp should be locked and opened on entry and will be locked and
1073 unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
1074 struct ucred *cred, struct thread *td)
1085 memset(&uio, 0, sizeof(uio));
1088 uio.uio_segflg = UIO_SYSSPACE;
1091 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
1093 while (error == 0) {
1094 offset = uio.uio_offset;
1099 iov.iov_len = MAXBSIZE;
1100 uio.uio_resid = iov.iov_len;
1101 uio.uio_rw = UIO_READ;
1103 if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
1105 if ((count = MAXBSIZE - uio.uio_resid) == 0)
1109 while (bufoffset < count) {
1112 iov.iov_base = buf + bufoffset;
1113 iov.iov_len = count - bufoffset;
1114 uio.uio_offset = offset + bufoffset;
1115 uio.uio_resid = iov.iov_len;
1116 uio.uio_rw = UIO_WRITE;
1118 if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
1121 bufoffset += (count - bufoffset) - uio.uio_resid;
1124 uio.uio_offset = offset + bufoffset;
1133 * Copy file from lower to upper.
1135 * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
1138 * If no error returned, unp will be updated.
1141 unionfs_copyfile(struct unionfs_node *unp, int docopy, struct ucred *cred,
1151 lvp = unp->un_lowervp;
1154 if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
1156 if (unp->un_dvp == NULLVP)
1158 if (unp->un_uppervp != NULLVP)
1160 udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
1163 if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
1166 error = VOP_ACCESS(lvp, VREAD, cred, td);
1170 if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)) != 0)
1172 error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva, td);
1174 vn_finished_write(mp);
1179 error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
1181 error = unionfs_copyfile_core(lvp, uvp, cred, td);
1182 VOP_CLOSE(lvp, FREAD, cred, td);
1185 VOP_CLOSE(uvp, FWRITE, cred, td);
1186 VOP_ADD_WRITECOUNT_CHECKED(uvp, -1);
1187 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1188 __func__, uvp, uvp->v_writecount);
1190 vn_finished_write(mp);
1193 /* Reset the attributes. Ignore errors. */
1195 VOP_SETATTR(uvp, &uva, cred);
1198 unionfs_node_update(unp, uvp, td);
1204 * It checks whether vp can rmdir. (check empty)
1206 * vp is unionfs vnode.
1207 * vp should be locked.
1210 unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td)
1217 struct componentname cn;
1226 * The size of buf needs to be larger than DIRBLKSIZ.
1230 ASSERT_VOP_ELOCKED(vp, __func__);
1233 uvp = UNIONFSVPTOUPPERVP(vp);
1234 lvp = UNIONFSVPTOLOWERVP(vp);
1237 if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
1239 if (va.va_flags & OPAQUE)
1244 if ((error = mac_vnode_check_open(cred, vp, VEXEC|VREAD)) != 0)
1247 if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred, td)) != 0)
1249 if ((error = VOP_OPEN(vp, FREAD, cred, td, NULL)) != 0)
1252 uio.uio_rw = UIO_READ;
1253 uio.uio_segflg = UIO_SYSSPACE;
1258 error = mac_vnode_check_readdir(td->td_ucred, lvp);
1260 while (!error && !eofflag) {
1262 iov.iov_len = sizeof(buf);
1265 uio.uio_resid = iov.iov_len;
1267 error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
1270 KASSERT(eofflag != 0 || uio.uio_resid < sizeof(buf),
1271 ("%s: empty read from lower FS", __func__));
1273 edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
1274 for (dp = (struct dirent*)buf; !error && dp < edp;
1275 dp = (struct dirent*)((caddr_t)dp + dp->d_reclen)) {
1276 if (dp->d_type == DT_WHT || dp->d_fileno == 0 ||
1277 (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1278 (dp->d_namlen == 2 && !bcmp(dp->d_name, "..", 2)))
1281 cn.cn_namelen = dp->d_namlen;
1283 cn.cn_nameptr = dp->d_name;
1284 cn.cn_nameiop = LOOKUP;
1285 cn.cn_flags = LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN;
1286 cn.cn_lkflags = LK_EXCLUSIVE;
1290 * check entry in lower.
1291 * Sometimes, readdir function returns
1294 lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
1299 continue; /* skip entry */
1303 * If it has no exist/whiteout entry in upper,
1304 * directory is not empty.
1306 cn.cn_flags = LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN;
1307 lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
1312 /* ignore exist or whiteout entry */
1314 (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
1322 VOP_CLOSE(vp, FREAD, cred, td);