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
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
46 #include <sys/mutex.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/dirent.h>
53 #include <sys/fcntl.h>
54 #include <sys/filedesc.h>
56 #include <sys/sysctl.h>
57 #include <sys/taskqueue.h>
58 #include <sys/resourcevar.h>
60 #include <machine/atomic.h>
62 #include <security/mac/mac_framework.h>
66 #include <fs/unionfs/union.h>
68 #define NUNIONFSNODECACHE 16
69 #define UNIONFSHASHMASK (NUNIONFSNODECACHE - 1)
71 static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table");
72 MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part");
73 MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
75 static struct task unionfs_deferred_rele_task;
76 static struct mtx unionfs_deferred_rele_lock;
77 static STAILQ_HEAD(, unionfs_node) unionfs_deferred_rele_list =
78 STAILQ_HEAD_INITIALIZER(unionfs_deferred_rele_list);
79 static TASKQUEUE_DEFINE_THREAD(unionfs_rele);
81 unsigned int unionfs_ndeferred = 0;
82 SYSCTL_UINT(_vfs, OID_AUTO, unionfs_ndeferred, CTLFLAG_RD,
83 &unionfs_ndeferred, 0, "unionfs deferred vnode release");
85 static void unionfs_deferred_rele(void *, int);
91 unionfs_init(struct vfsconf *vfsp)
93 UNIONFSDEBUG("unionfs_init\n"); /* printed during system boot */
94 TASK_INIT(&unionfs_deferred_rele_task, 0, unionfs_deferred_rele, NULL);
95 mtx_init(&unionfs_deferred_rele_lock, "uniondefr", NULL, MTX_DEF);
103 unionfs_uninit(struct vfsconf *vfsp)
105 taskqueue_quiesce(taskqueue_unionfs_rele);
106 taskqueue_free(taskqueue_unionfs_rele);
107 mtx_destroy(&unionfs_deferred_rele_lock);
112 unionfs_deferred_rele(void *arg __unused, int pending __unused)
114 STAILQ_HEAD(, unionfs_node) local_rele_list;
115 struct unionfs_node *unp, *tunp;
116 unsigned int ndeferred;
119 STAILQ_INIT(&local_rele_list);
120 mtx_lock(&unionfs_deferred_rele_lock);
121 STAILQ_CONCAT(&local_rele_list, &unionfs_deferred_rele_list);
122 mtx_unlock(&unionfs_deferred_rele_lock);
123 STAILQ_FOREACH_SAFE(unp, &local_rele_list, un_rele, tunp) {
125 MPASS(unp->un_dvp != NULL);
127 free(unp, M_UNIONFSNODE);
130 /* We expect this function to be single-threaded, thus no atomic */
131 unionfs_ndeferred += ndeferred;
134 static struct unionfs_node_hashhead *
135 unionfs_get_hashhead(struct vnode *dvp, struct vnode *lookup)
137 struct unionfs_node *unp;
139 unp = VTOUNIONFS(dvp);
141 return (&(unp->un_hashtbl[vfs_hash_index(lookup) & UNIONFSHASHMASK]));
145 * Attempt to lookup a cached unionfs vnode by upper/lower vp
146 * from dvp, with dvp's interlock held.
148 static struct vnode *
149 unionfs_get_cached_vnode_locked(struct vnode *lookup, struct vnode *dvp)
151 struct unionfs_node *unp;
152 struct unionfs_node_hashhead *hd;
155 hd = unionfs_get_hashhead(dvp, lookup);
157 LIST_FOREACH(unp, hd, un_hash) {
158 if (unp->un_uppervp == lookup ||
159 unp->un_lowervp == lookup) {
160 vp = UNIONFSTOV(unp);
161 VI_LOCK_FLAGS(vp, MTX_DUPOK);
162 vp->v_iflag &= ~VI_OWEINACT;
163 if (VN_IS_DOOMED(vp) ||
164 ((vp->v_iflag & VI_DOINGINACT) != 0)) {
180 * Get the cached vnode.
182 static struct vnode *
183 unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp,
191 vp = unionfs_get_cached_vnode_locked(uvp, dvp);
192 else if (lvp != NULLVP)
193 vp = unionfs_get_cached_vnode_locked(lvp, dvp);
200 * Add the new vnode into cache.
202 static struct vnode *
203 unionfs_ins_cached_vnode(struct unionfs_node *uncp,
206 struct unionfs_node_hashhead *hd;
209 ASSERT_VOP_ELOCKED(uncp->un_uppervp, __func__);
210 ASSERT_VOP_ELOCKED(uncp->un_lowervp, __func__);
211 KASSERT(uncp->un_uppervp == NULLVP || uncp->un_uppervp->v_type == VDIR,
212 ("%s: v_type != VDIR", __func__));
213 KASSERT(uncp->un_lowervp == NULLVP || uncp->un_lowervp->v_type == VDIR,
214 ("%s: v_type != VDIR", __func__));
218 if (uncp->un_uppervp != NULL)
219 vp = unionfs_get_cached_vnode_locked(uncp->un_uppervp, dvp);
220 else if (uncp->un_lowervp != NULL)
221 vp = unionfs_get_cached_vnode_locked(uncp->un_lowervp, dvp);
223 hd = unionfs_get_hashhead(dvp, (uncp->un_uppervp != NULLVP ?
224 uncp->un_uppervp : uncp->un_lowervp));
225 LIST_INSERT_HEAD(hd, uncp, un_hash);
236 unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp)
238 KASSERT(unp != NULL, ("%s: null node", __func__));
239 KASSERT(dvp != NULLVP,
240 ("%s: null parent vnode", __func__));
243 if (unp->un_hash.le_prev != NULL) {
244 LIST_REMOVE(unp, un_hash);
245 unp->un_hash.le_next = NULL;
246 unp->un_hash.le_prev = NULL;
252 * Common cleanup handling for unionfs_nodeget
253 * Upper, lower, and parent directory vnodes are expected to be referenced by
254 * the caller. Upper and lower vnodes, if non-NULL, are also expected to be
255 * exclusively locked by the caller.
256 * This function will return with the caller's locks and references undone.
259 unionfs_nodeget_cleanup(struct vnode *vp, struct unionfs_node *unp)
263 * Lock and reset the default vnode lock; vgone() expects a locked
264 * vnode, and we're going to reset the vnode ops.
266 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
269 * Clear out private data and reset the vnode ops to avoid use of
270 * unionfs vnode ops on a partially constructed vnode.
274 vp->v_vnlock = &vp->v_lock;
275 vp->v_op = &dead_vnodeops;
280 if (unp->un_dvp != NULLVP)
282 if (unp->un_uppervp != NULLVP)
283 vput(unp->un_uppervp);
284 if (unp->un_lowervp != NULLVP)
285 vput(unp->un_lowervp);
286 if (unp->un_hashtbl != NULL)
287 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
288 free(unp->un_path, M_UNIONFSPATH);
289 free(unp, M_UNIONFSNODE);
293 * Make a new or get existing unionfs node.
295 * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
296 * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
297 * you should not lock plurality simultaneously.
300 unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
301 struct vnode *lowervp, struct vnode *dvp, struct vnode **vpp,
302 struct componentname *cnp)
305 struct unionfs_mount *ump;
306 struct unionfs_node *unp;
314 ump = MOUNTTOUNIONFSMOUNT(mp);
315 lkflags = (cnp ? cnp->cn_lkflags : 0);
316 path = (cnp ? cnp->cn_nameptr : NULL);
319 if (uppervp == NULLVP && lowervp == NULLVP)
320 panic("%s: upper and lower is null", __func__);
322 vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
324 /* If it has no ISLASTCN flag, path check is skipped. */
325 if (cnp && !(cnp->cn_flags & ISLASTCN))
328 /* check the cache */
329 if (dvp != NULLVP && vt == VDIR) {
330 vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp);
333 goto unionfs_nodeget_out;
337 unp = malloc(sizeof(struct unionfs_node),
338 M_UNIONFSNODE, M_WAITOK | M_ZERO);
340 error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp);
342 free(unp, M_UNIONFSNODE);
347 if (uppervp != NULLVP)
349 if (lowervp != NULLVP)
353 unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH,
355 KASSERT(hashmask == UNIONFSHASHMASK,
356 ("unexpected unionfs hash mask 0x%lx", hashmask));
360 unp->un_uppervp = uppervp;
361 unp->un_lowervp = lowervp;
363 if (uppervp != NULLVP)
364 vp->v_vnlock = uppervp->v_vnlock;
366 vp->v_vnlock = lowervp->v_vnlock;
369 unp->un_path = malloc(cnp->cn_namelen + 1,
370 M_UNIONFSPATH, M_WAITOK | M_ZERO);
371 bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen);
372 unp->un_path[cnp->cn_namelen] = '\0';
373 unp->un_pathlen = cnp->cn_namelen;
379 * TODO: This is an imperfect check, as there's no guarantee that
380 * the underlying filesystems will always return vnode pointers
381 * for the root inodes that match our cached values. To reduce
382 * the likelihood of failure, for example in the case where either
383 * vnode has been forcibly doomed, we check both pointers and set
384 * VV_ROOT if either matches.
386 if (ump->um_uppervp == uppervp || ump->um_lowervp == lowervp)
387 vp->v_vflag |= VV_ROOT;
388 KASSERT(dvp != NULL || (vp->v_vflag & VV_ROOT) != 0,
389 ("%s: NULL dvp for non-root vp %p", __func__, vp));
391 vn_lock_pair(lowervp, false, LK_EXCLUSIVE, uppervp, false,
393 error = insmntque1(vp, mp);
395 unionfs_nodeget_cleanup(vp, unp);
398 if (lowervp != NULL && VN_IS_DOOMED(lowervp)) {
400 unp->un_lowervp = lowervp = NULL;
402 if (uppervp != NULL && VN_IS_DOOMED(uppervp)) {
404 unp->un_uppervp = uppervp = NULL;
405 if (lowervp != NULLVP)
406 vp->v_vnlock = lowervp->v_vnlock;
408 if (lowervp == NULL && uppervp == NULL) {
409 unionfs_nodeget_cleanup(vp, unp);
413 vn_set_state(vp, VSTATE_CONSTRUCTED);
415 if (dvp != NULLVP && vt == VDIR)
416 *vpp = unionfs_ins_cached_vnode(unp, dvp);
417 if (*vpp != NULLVP) {
418 unionfs_nodeget_cleanup(vp, unp);
429 if (lkflags & LK_TYPE_MASK)
430 vn_lock(vp, lkflags | LK_RETRY);
436 * Clean up the unionfs node.
439 unionfs_noderem(struct vnode *vp)
441 struct unionfs_node *unp, *unp_t1, *unp_t2;
442 struct unionfs_node_hashhead *hd;
443 struct unionfs_node_status *unsp, *unsp_tmp;
451 * The root vnode lock may be recursed during unmount, because
452 * it may share the same lock as the unionfs mount's covered vnode,
453 * which is locked across VFS_UNMOUNT(). This lock will then be
454 * recursively taken during the vflush() issued by unionfs_unmount().
455 * But we still only need to lock the unionfs lock once, because only
456 * one of those lock operations was taken against a unionfs vnode and
457 * will be undone against a unionfs vnode.
459 KASSERT(vp->v_vnlock->lk_recurse == 0 || (vp->v_vflag & VV_ROOT) != 0,
460 ("%s: vnode %p locked recursively", __func__, vp));
461 if (lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
462 panic("%s: failed to acquire lock for vnode lock", __func__);
465 * Use the interlock to protect the clearing of v_data to
466 * prevent faults in unionfs_lock().
469 unp = VTOUNIONFS(vp);
470 lvp = unp->un_lowervp;
471 uvp = unp->un_uppervp;
473 unp->un_lowervp = unp->un_uppervp = NULLVP;
474 vp->v_vnlock = &(vp->v_lock);
477 if (unp->un_hashtbl != NULL) {
479 * Clear out any cached child vnodes. This should only
480 * be necessary during forced unmount, when the vnode may
481 * be reclaimed with a non-zero use count. Otherwise the
482 * reference held by each child should prevent reclamation.
484 for (count = 0; count <= UNIONFSHASHMASK; count++) {
485 hd = unp->un_hashtbl + count;
486 LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) {
487 LIST_REMOVE(unp_t1, un_hash);
488 unp_t1->un_hash.le_next = NULL;
489 unp_t1->un_hash.le_prev = NULL;
495 writerefs = atomic_load_int(&vp->v_writecount);
496 VNASSERT(writerefs >= 0, vp,
497 ("%s: write count %d, unexpected text ref", __func__, writerefs));
499 * If we were opened for write, we leased the write reference
500 * to the lower vnode. If this is a reclamation due to the
501 * forced unmount, undo the reference now.
504 VNASSERT(uvp != NULL, vp,
505 ("%s: write reference without upper vnode", __func__));
506 VOP_ADD_WRITECOUNT(uvp, -writerefs);
514 unionfs_rem_cached_vnode(unp, dvp);
520 if (unp->un_path != NULL) {
521 free(unp->un_path, M_UNIONFSPATH);
526 if (unp->un_hashtbl != NULL) {
527 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, UNIONFSHASHMASK);
530 LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) {
531 LIST_REMOVE(unsp, uns_list);
535 mtx_lock(&unionfs_deferred_rele_lock);
536 STAILQ_INSERT_TAIL(&unionfs_deferred_rele_list, unp, un_rele);
537 mtx_unlock(&unionfs_deferred_rele_lock);
538 taskqueue_enqueue(taskqueue_unionfs_rele,
539 &unionfs_deferred_rele_task);
541 free(unp, M_UNIONFSNODE);
545 * Get the unionfs node status object for the vnode corresponding to unp,
546 * for the process that owns td. Allocate a new status object if one
547 * does not already exist.
550 unionfs_get_node_status(struct unionfs_node *unp, struct thread *td,
551 struct unionfs_node_status **unspp)
553 struct unionfs_node_status *unsp;
556 pid = td->td_proc->p_pid;
558 KASSERT(NULL != unspp, ("%s: NULL status", __func__));
559 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), __func__);
561 LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
562 if (unsp->uns_pid == pid) {
568 /* create a new unionfs node status */
569 unsp = malloc(sizeof(struct unionfs_node_status),
570 M_TEMP, M_WAITOK | M_ZERO);
573 LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
579 * Remove the unionfs node status, if you can.
580 * You need exclusive lock this vnode.
583 unionfs_tryrem_node_status(struct unionfs_node *unp,
584 struct unionfs_node_status *unsp)
586 KASSERT(NULL != unsp, ("%s: NULL status", __func__));
587 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), __func__);
589 if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
592 LIST_REMOVE(unsp, uns_list);
597 * Create upper node attr.
600 unionfs_create_uppervattr_core(struct unionfs_mount *ump, struct vattr *lva,
601 struct vattr *uva, struct thread *td)
604 uva->va_type = lva->va_type;
605 uva->va_atime = lva->va_atime;
606 uva->va_mtime = lva->va_mtime;
607 uva->va_ctime = lva->va_ctime;
609 switch (ump->um_copymode) {
610 case UNIONFS_TRANSPARENT:
611 uva->va_mode = lva->va_mode;
612 uva->va_uid = lva->va_uid;
613 uva->va_gid = lva->va_gid;
615 case UNIONFS_MASQUERADE:
616 if (ump->um_uid == lva->va_uid) {
617 uva->va_mode = lva->va_mode & 077077;
618 uva->va_mode |= (lva->va_type == VDIR ?
619 ump->um_udir : ump->um_ufile) & 0700;
620 uva->va_uid = lva->va_uid;
621 uva->va_gid = lva->va_gid;
623 uva->va_mode = (lva->va_type == VDIR ?
624 ump->um_udir : ump->um_ufile);
625 uva->va_uid = ump->um_uid;
626 uva->va_gid = ump->um_gid;
629 default: /* UNIONFS_TRADITIONAL */
630 uva->va_mode = 0777 & ~td->td_proc->p_pd->pd_cmask;
631 uva->va_uid = ump->um_uid;
632 uva->va_gid = ump->um_gid;
638 * Create upper node attr.
641 unionfs_create_uppervattr(struct unionfs_mount *ump, struct vnode *lvp,
642 struct vattr *uva, struct ucred *cred, struct thread *td)
647 if ((error = VOP_GETATTR(lvp, &lva, cred)))
650 unionfs_create_uppervattr_core(ump, &lva, uva, td);
658 * dvp should be locked on entry and will be locked on return.
660 * If an error is returned, *vpp will be invalid, otherwise it will hold a
661 * locked, referenced vnode. If *vpp == dvp then remember that only one
662 * LK_EXCLUSIVE lock is held.
665 unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
666 struct componentname *cnp, struct componentname *cn, struct thread *td,
667 char *path, int pathlen, u_long nameiop)
672 cn->cn_namelen = pathlen;
674 cn->cn_nameiop = nameiop;
675 cn->cn_flags = (LOCKPARENT | LOCKLEAF | ISLASTCN);
676 cn->cn_lkflags = LK_EXCLUSIVE;
677 cn->cn_cred = cnp->cn_cred;
678 cn->cn_nameptr = cn->cn_pnbuf;
681 if (nameiop == DELETE) {
682 cn->cn_flags |= (cnp->cn_flags & DOWHITEOUT);
683 } else if (nameiop == RENAME) {
685 } else if (nameiop == CREATE) {
686 cn->cn_flags |= NOCACHE;
692 if ((error = vfs_relookup(dvp, vpp, cn, refstart))) {
693 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
697 KASSERT(cn->cn_pnbuf == path, ("%s: cn_pnbuf changed", __func__));
703 * relookup for CREATE namei operation.
705 * dvp is unionfs vnode. dvp should be locked.
707 * If it called 'unionfs_copyfile' function by unionfs_link etc,
708 * VOP_LOOKUP information is broken.
709 * So it need relookup in order to create link etc.
712 unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp,
717 struct componentname cn;
720 udvp = UNIONFSVPTOUPPERVP(dvp);
723 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
724 cnp->cn_namelen, CREATE);
741 * relookup for DELETE namei operation.
743 * dvp is unionfs vnode. dvp should be locked.
746 unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp,
751 struct componentname cn;
754 udvp = UNIONFSVPTOUPPERVP(dvp);
757 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
758 cnp->cn_namelen, DELETE);
775 * relookup for RENAME namei operation.
777 * dvp is unionfs vnode. dvp should be locked.
780 unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp,
785 struct componentname cn;
788 udvp = UNIONFSVPTOUPPERVP(dvp);
791 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
792 cnp->cn_namelen, RENAME);
807 * Update the unionfs_node.
809 * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
810 * uvp's lock and lower's lock will be unlocked.
813 unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp,
816 struct unionfs_node_hashhead *hd;
820 unsigned count, lockrec;
822 vp = UNIONFSTOV(unp);
823 lvp = unp->un_lowervp;
824 ASSERT_VOP_ELOCKED(lvp, __func__);
825 ASSERT_VOP_ELOCKED(uvp, __func__);
828 VNASSERT(vp->v_writecount == 0, vp,
829 ("%s: non-zero writecount", __func__));
831 * Update the upper vnode's lock state to match the lower vnode,
832 * and then switch the unionfs vnode's lock to the upper vnode.
834 lockrec = lvp->v_vnlock->lk_recurse;
835 for (count = 0; count < lockrec; count++)
836 vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
838 unp->un_uppervp = uvp;
839 vp->v_vnlock = uvp->v_vnlock;
843 * Re-cache the unionfs vnode against the upper vnode
845 if (dvp != NULLVP && vp->v_type == VDIR) {
847 if (unp->un_hash.le_prev != NULL) {
848 LIST_REMOVE(unp, un_hash);
849 hd = unionfs_get_hashhead(dvp, uvp);
850 LIST_INSERT_HEAD(hd, unp, un_hash);
852 VI_UNLOCK(unp->un_dvp);
857 * Create a new shadow dir.
859 * udvp should be locked on entry and will be locked on return.
861 * If no error returned, unp will be updated.
864 unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
865 struct unionfs_node *unp, struct componentname *cnp, struct thread *td)
874 struct ucred *credbk;
875 struct uidinfo *rootinfo;
878 if (unp->un_uppervp != NULLVP)
881 lvp = unp->un_lowervp;
883 credbk = cnp->cn_cred;
885 /* Authority change to root */
886 rootinfo = uifind((uid_t)0);
887 cred = crdup(cnp->cn_cred);
889 * The calls to chgproccnt() are needed to compensate for change_ruid()
890 * calling chgproccnt().
892 chgproccnt(cred->cr_ruidinfo, 1, 0);
893 change_euid(cred, rootinfo);
894 change_ruid(cred, rootinfo);
895 change_svuid(cred, (uid_t)0);
899 memset(&nd.ni_cnd, 0, sizeof(struct componentname));
902 if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
903 goto unionfs_mkshadowdir_abort;
905 if ((error = unionfs_relookup(udvp, &uvp, cnp, &nd.ni_cnd, td,
906 cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
907 goto unionfs_mkshadowdir_abort;
915 goto unionfs_mkshadowdir_abort;
918 if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)))
919 goto unionfs_mkshadowdir_abort;
920 unionfs_create_uppervattr_core(ump, &lva, &va, td);
922 error = VOP_MKDIR(udvp, &uvp, &nd.ni_cnd, &va);
925 unionfs_node_update(unp, uvp, td);
928 * XXX The bug which cannot set uid/gid was corrected.
932 VOP_SETATTR(uvp, &va, nd.ni_cnd.cn_cred);
934 vn_finished_write(mp);
936 unionfs_mkshadowdir_abort:
937 cnp->cn_cred = credbk;
938 chgproccnt(cred->cr_ruidinfo, -1, 0);
945 * Create a new whiteout.
947 * dvp should be locked on entry and will be locked on return.
950 unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp,
951 struct thread *td, char *path, int pathlen)
960 if ((error = unionfs_relookup(dvp, &wvp, cnp, &nd.ni_cnd, td, path,
973 if ((error = vn_start_write(dvp, &mp, V_WAIT | V_PCATCH)))
974 goto unionfs_mkwhiteout_free_out;
975 error = VOP_WHITEOUT(dvp, &nd.ni_cnd, CREATE);
977 vn_finished_write(mp);
979 unionfs_mkwhiteout_free_out:
984 * Create a new vnode for create a new shadow file.
986 * If an error is returned, *vpp will be invalid, otherwise it will hold a
987 * locked, referenced and opened vnode.
989 * unp is never updated.
992 unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
993 struct unionfs_node *unp, struct vattr *uvap, struct thread *td)
995 struct unionfs_mount *ump;
1000 struct nameidata nd;
1004 ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
1006 lvp = unp->un_lowervp;
1007 cred = td->td_ucred;
1008 fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
1011 if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
1013 unionfs_create_uppervattr_core(ump, &lva, uvap, td);
1015 if (unp->un_path == NULL)
1016 panic("%s: NULL un_path", __func__);
1018 nd.ni_cnd.cn_namelen = unp->un_pathlen;
1019 nd.ni_cnd.cn_pnbuf = unp->un_path;
1020 nd.ni_cnd.cn_nameiop = CREATE;
1021 nd.ni_cnd.cn_flags = LOCKPARENT | LOCKLEAF | ISLASTCN;
1022 nd.ni_cnd.cn_lkflags = LK_EXCLUSIVE;
1023 nd.ni_cnd.cn_cred = cred;
1024 nd.ni_cnd.cn_nameptr = nd.ni_cnd.cn_pnbuf;
1028 if ((error = vfs_relookup(udvp, &vp, &nd.ni_cnd, false)) != 0)
1029 goto unionfs_vn_create_on_upper_free_out2;
1038 goto unionfs_vn_create_on_upper_free_out1;
1041 if ((error = VOP_CREATE(udvp, &vp, &nd.ni_cnd, uvap)) != 0)
1042 goto unionfs_vn_create_on_upper_free_out1;
1044 if ((error = VOP_OPEN(vp, fmode, cred, td, NULL)) != 0) {
1046 goto unionfs_vn_create_on_upper_free_out1;
1048 error = VOP_ADD_WRITECOUNT(vp, 1);
1049 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d",
1050 __func__, vp, vp->v_writecount);
1054 VOP_CLOSE(vp, fmode, cred, td);
1057 unionfs_vn_create_on_upper_free_out1:
1060 unionfs_vn_create_on_upper_free_out2:
1061 KASSERT(nd.ni_cnd.cn_pnbuf == unp->un_path,
1062 ("%s: cn_pnbuf changed", __func__));
1068 * Copy from lvp to uvp.
1070 * lvp and uvp should be locked and opened on entry and will be locked and
1074 unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
1075 struct ucred *cred, struct thread *td)
1086 memset(&uio, 0, sizeof(uio));
1089 uio.uio_segflg = UIO_SYSSPACE;
1092 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
1094 while (error == 0) {
1095 offset = uio.uio_offset;
1100 iov.iov_len = MAXBSIZE;
1101 uio.uio_resid = iov.iov_len;
1102 uio.uio_rw = UIO_READ;
1104 if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
1106 if ((count = MAXBSIZE - uio.uio_resid) == 0)
1110 while (bufoffset < count) {
1113 iov.iov_base = buf + bufoffset;
1114 iov.iov_len = count - bufoffset;
1115 uio.uio_offset = offset + bufoffset;
1116 uio.uio_resid = iov.iov_len;
1117 uio.uio_rw = UIO_WRITE;
1119 if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
1122 bufoffset += (count - bufoffset) - uio.uio_resid;
1125 uio.uio_offset = offset + bufoffset;
1134 * Copy file from lower to upper.
1136 * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
1139 * If no error returned, unp will be updated.
1142 unionfs_copyfile(struct unionfs_node *unp, int docopy, struct ucred *cred,
1152 lvp = unp->un_lowervp;
1155 if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
1157 if (unp->un_dvp == NULLVP)
1159 if (unp->un_uppervp != NULLVP)
1161 udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
1164 if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
1167 error = VOP_ACCESS(lvp, VREAD, cred, td);
1171 if ((error = vn_start_write(udvp, &mp, V_WAIT | V_PCATCH)) != 0)
1173 error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva, td);
1175 vn_finished_write(mp);
1180 error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
1182 error = unionfs_copyfile_core(lvp, uvp, cred, td);
1183 VOP_CLOSE(lvp, FREAD, cred, td);
1186 VOP_CLOSE(uvp, FWRITE, cred, td);
1187 VOP_ADD_WRITECOUNT_CHECKED(uvp, -1);
1188 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d",
1189 __func__, uvp, uvp->v_writecount);
1191 vn_finished_write(mp);
1194 /* Reset the attributes. Ignore errors. */
1196 VOP_SETATTR(uvp, &uva, cred);
1199 unionfs_node_update(unp, uvp, td);
1205 * It checks whether vp can rmdir. (check empty)
1207 * vp is unionfs vnode.
1208 * vp should be locked.
1211 unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td)
1218 struct componentname cn;
1227 * The size of buf needs to be larger than DIRBLKSIZ.
1231 ASSERT_VOP_ELOCKED(vp, __func__);
1234 uvp = UNIONFSVPTOUPPERVP(vp);
1235 lvp = UNIONFSVPTOLOWERVP(vp);
1238 if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
1240 if (va.va_flags & OPAQUE)
1245 if ((error = mac_vnode_check_open(cred, vp, VEXEC|VREAD)) != 0)
1248 if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred, td)) != 0)
1250 if ((error = VOP_OPEN(vp, FREAD, cred, td, NULL)) != 0)
1253 uio.uio_rw = UIO_READ;
1254 uio.uio_segflg = UIO_SYSSPACE;
1259 error = mac_vnode_check_readdir(td->td_ucred, lvp);
1261 while (!error && !eofflag) {
1263 iov.iov_len = sizeof(buf);
1266 uio.uio_resid = iov.iov_len;
1268 error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
1271 KASSERT(eofflag != 0 || uio.uio_resid < sizeof(buf),
1272 ("%s: empty read from lower FS", __func__));
1274 edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
1275 for (dp = (struct dirent*)buf; !error && dp < edp;
1276 dp = (struct dirent*)((caddr_t)dp + dp->d_reclen)) {
1277 if (dp->d_type == DT_WHT || dp->d_fileno == 0 ||
1278 (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1279 (dp->d_namlen == 2 && !bcmp(dp->d_name, "..", 2)))
1282 cn.cn_namelen = dp->d_namlen;
1284 cn.cn_nameptr = dp->d_name;
1285 cn.cn_nameiop = LOOKUP;
1286 cn.cn_flags = LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN;
1287 cn.cn_lkflags = LK_EXCLUSIVE;
1291 * check entry in lower.
1292 * Sometimes, readdir function returns
1295 lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
1300 continue; /* skip entry */
1304 * If it has no exist/whiteout entry in upper,
1305 * directory is not empty.
1307 cn.cn_flags = LOCKPARENT | LOCKLEAF | RDONLY | ISLASTCN;
1308 lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
1313 /* ignore exist or whiteout entry */
1315 (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
1323 VOP_CLOSE(vp, FREAD, cred, td);