2 * Copyright (c) 1994 Jan-Simon Pendry
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 2005, 2006, 2012 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
6 * Copyright (c) 2006, 2012 Daichi Goto <daichi@freebsd.org>
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/namei.h>
48 #include <sys/vnode.h>
49 #include <sys/dirent.h>
50 #include <sys/fcntl.h>
51 #include <sys/filedesc.h>
53 #include <sys/resourcevar.h>
55 #include <security/mac/mac_framework.h>
59 #include <fs/unionfs/union.h>
61 #define NUNIONFSNODECACHE 16
63 static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table");
64 MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part");
65 MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
71 unionfs_init(struct vfsconf *vfsp)
73 UNIONFSDEBUG("unionfs_init\n"); /* printed during system boot */
81 unionfs_uninit(struct vfsconf *vfsp)
86 static struct unionfs_node_hashhead *
87 unionfs_get_hashhead(struct vnode *dvp, char *path)
91 struct unionfs_node *unp;
94 unp = VTOUNIONFS(dvp);
96 for (count = 0; path[count]; count++)
100 return (&(unp->un_hashtbl[hash & (unp->un_hashmask)]));
104 * Get the cached vnode.
106 static struct vnode *
107 unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp,
108 struct vnode *dvp, char *path)
110 struct unionfs_node_hashhead *hd;
111 struct unionfs_node *unp;
114 KASSERT((uvp == NULLVP || uvp->v_type == VDIR),
115 ("unionfs_get_cached_vnode: v_type != VDIR"));
116 KASSERT((lvp == NULLVP || lvp->v_type == VDIR),
117 ("unionfs_get_cached_vnode: v_type != VDIR"));
120 hd = unionfs_get_hashhead(dvp, path);
121 LIST_FOREACH(unp, hd, un_hash) {
122 if (!strcmp(unp->un_path, path)) {
123 vp = UNIONFSTOV(unp);
124 VI_LOCK_FLAGS(vp, MTX_DUPOK);
126 vp->v_iflag &= ~VI_OWEINACT;
127 if ((vp->v_iflag & (VI_DOOMED | VI_DOINGINACT)) != 0) {
141 * Add the new vnode into cache.
143 static struct vnode *
144 unionfs_ins_cached_vnode(struct unionfs_node *uncp,
145 struct vnode *dvp, char *path)
147 struct unionfs_node_hashhead *hd;
148 struct unionfs_node *unp;
151 KASSERT((uncp->un_uppervp==NULLVP || uncp->un_uppervp->v_type==VDIR),
152 ("unionfs_ins_cached_vnode: v_type != VDIR"));
153 KASSERT((uncp->un_lowervp==NULLVP || uncp->un_lowervp->v_type==VDIR),
154 ("unionfs_ins_cached_vnode: v_type != VDIR"));
157 hd = unionfs_get_hashhead(dvp, path);
158 LIST_FOREACH(unp, hd, un_hash) {
159 if (!strcmp(unp->un_path, path)) {
160 vp = UNIONFSTOV(unp);
161 VI_LOCK_FLAGS(vp, MTX_DUPOK);
162 vp->v_iflag &= ~VI_OWEINACT;
163 if ((vp->v_iflag & (VI_DOOMED | VI_DOINGINACT)) != 0) {
164 LIST_INSERT_HEAD(hd, uncp, un_hash);
174 LIST_INSERT_HEAD(hd, uncp, un_hash);
184 unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp)
186 KASSERT((unp != NULL), ("unionfs_rem_cached_vnode: null node"));
187 KASSERT((dvp != NULLVP),
188 ("unionfs_rem_cached_vnode: null parent vnode"));
189 KASSERT((unp->un_hash.le_prev != NULL),
190 ("unionfs_rem_cached_vnode: null hash"));
193 LIST_REMOVE(unp, un_hash);
194 unp->un_hash.le_next = NULL;
195 unp->un_hash.le_prev = NULL;
200 * Make a new or get existing unionfs node.
202 * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
203 * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
204 * you should not lock plurality simultaneously.
207 unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
208 struct vnode *lowervp, struct vnode *dvp,
209 struct vnode **vpp, struct componentname *cnp,
212 struct unionfs_mount *ump;
213 struct unionfs_node *unp;
220 ump = MOUNTTOUNIONFSMOUNT(mp);
221 lkflags = (cnp ? cnp->cn_lkflags : 0);
222 path = (cnp ? cnp->cn_nameptr : NULL);
225 if (uppervp == NULLVP && lowervp == NULLVP)
226 panic("unionfs_nodeget: upper and lower is null");
228 vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
230 /* If it has no ISLASTCN flag, path check is skipped. */
231 if (cnp && !(cnp->cn_flags & ISLASTCN))
234 /* check the cache */
235 if (path != NULL && dvp != NULLVP && vt == VDIR) {
236 vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp, path);
240 goto unionfs_nodeget_out;
244 if ((uppervp == NULLVP || ump->um_uppervp != uppervp) ||
245 (lowervp == NULLVP || ump->um_lowervp != lowervp)) {
246 /* dvp will be NULLVP only in case of root vnode. */
250 unp = malloc(sizeof(struct unionfs_node),
251 M_UNIONFSNODE, M_WAITOK | M_ZERO);
253 error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp);
255 free(unp, M_UNIONFSNODE);
258 error = insmntque(vp, mp); /* XXX: Too early for mpsafe fs */
260 free(unp, M_UNIONFSNODE);
265 if (uppervp != NULLVP)
267 if (lowervp != NULLVP)
271 unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH,
272 &(unp->un_hashmask));
275 unp->un_uppervp = uppervp;
276 unp->un_lowervp = lowervp;
278 if (uppervp != NULLVP)
279 vp->v_vnlock = uppervp->v_vnlock;
281 vp->v_vnlock = lowervp->v_vnlock;
284 unp->un_path = (char *)
285 malloc(cnp->cn_namelen +1, M_UNIONFSPATH, M_WAITOK|M_ZERO);
286 bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen);
287 unp->un_path[cnp->cn_namelen] = '\0';
292 if ((uppervp != NULLVP && ump->um_uppervp == uppervp) &&
293 (lowervp != NULLVP && ump->um_lowervp == lowervp))
294 vp->v_vflag |= VV_ROOT;
296 if (path != NULL && dvp != NULLVP && vt == VDIR)
297 *vpp = unionfs_ins_cached_vnode(unp, dvp, path);
298 if ((*vpp) != NULLVP) {
301 if (uppervp != NULLVP)
303 if (lowervp != NULLVP)
306 unp->un_uppervp = NULLVP;
307 unp->un_lowervp = NULLVP;
308 unp->un_dvp = NULLVP;
316 if (lkflags & LK_TYPE_MASK)
317 vn_lock(vp, lkflags | LK_RETRY);
323 * Clean up the unionfs node.
326 unionfs_noderem(struct vnode *vp, struct thread *td)
329 struct unionfs_node *unp, *unp_t1, *unp_t2;
330 struct unionfs_node_hashhead *hd;
331 struct unionfs_node_status *unsp, *unsp_tmp;
337 * Use the interlock to protect the clearing of v_data to
338 * prevent faults in unionfs_lock().
341 unp = VTOUNIONFS(vp);
342 lvp = unp->un_lowervp;
343 uvp = unp->un_uppervp;
345 unp->un_lowervp = unp->un_uppervp = NULLVP;
346 vp->v_vnlock = &(vp->v_lock);
352 VOP_UNLOCK(lvp, LK_RELEASE);
354 VOP_UNLOCK(uvp, LK_RELEASE);
356 if (dvp != NULLVP && unp->un_hash.le_prev != NULL)
357 unionfs_rem_cached_vnode(unp, dvp);
359 if (lockmgr(vp->v_vnlock, LK_EXCLUSIVE, VI_MTX(vp)) != 0)
360 panic("the lock for deletion is unacquirable.");
368 unp->un_dvp = NULLVP;
370 if (unp->un_path != NULL) {
371 free(unp->un_path, M_UNIONFSPATH);
375 if (unp->un_hashtbl != NULL) {
376 for (count = 0; count <= unp->un_hashmask; count++) {
377 hd = unp->un_hashtbl + count;
378 LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) {
379 LIST_REMOVE(unp_t1, un_hash);
380 unp_t1->un_hash.le_next = NULL;
381 unp_t1->un_hash.le_prev = NULL;
384 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, unp->un_hashmask);
387 LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) {
388 LIST_REMOVE(unsp, uns_list);
391 free(unp, M_UNIONFSNODE);
395 * Get the unionfs node status.
396 * You need exclusive lock this vnode.
399 unionfs_get_node_status(struct unionfs_node *unp, struct thread *td,
400 struct unionfs_node_status **unspp)
402 struct unionfs_node_status *unsp;
403 pid_t pid = td->td_proc->p_pid;
405 KASSERT(NULL != unspp, ("null pointer"));
406 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status");
408 LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
409 if (unsp->uns_pid == pid) {
415 /* create a new unionfs node status */
416 unsp = malloc(sizeof(struct unionfs_node_status),
417 M_TEMP, M_WAITOK | M_ZERO);
420 LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
426 * Remove the unionfs node status, if you can.
427 * You need exclusive lock this vnode.
430 unionfs_tryrem_node_status(struct unionfs_node *unp,
431 struct unionfs_node_status *unsp)
433 KASSERT(NULL != unsp, ("null pointer"));
434 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status");
436 if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
439 LIST_REMOVE(unsp, uns_list);
444 * Create upper node attr.
447 unionfs_create_uppervattr_core(struct unionfs_mount *ump,
453 uva->va_type = lva->va_type;
454 uva->va_atime = lva->va_atime;
455 uva->va_mtime = lva->va_mtime;
456 uva->va_ctime = lva->va_ctime;
458 switch (ump->um_copymode) {
459 case UNIONFS_TRANSPARENT:
460 uva->va_mode = lva->va_mode;
461 uva->va_uid = lva->va_uid;
462 uva->va_gid = lva->va_gid;
464 case UNIONFS_MASQUERADE:
465 if (ump->um_uid == lva->va_uid) {
466 uva->va_mode = lva->va_mode & 077077;
467 uva->va_mode |= (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile) & 0700;
468 uva->va_uid = lva->va_uid;
469 uva->va_gid = lva->va_gid;
471 uva->va_mode = (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile);
472 uva->va_uid = ump->um_uid;
473 uva->va_gid = ump->um_gid;
476 default: /* UNIONFS_TRADITIONAL */
477 uva->va_mode = 0777 & ~td->td_proc->p_fd->fd_cmask;
478 uva->va_uid = ump->um_uid;
479 uva->va_gid = ump->um_gid;
485 * Create upper node attr.
488 unionfs_create_uppervattr(struct unionfs_mount *ump,
497 if ((error = VOP_GETATTR(lvp, &lva, cred)))
500 unionfs_create_uppervattr_core(ump, &lva, uva, td);
508 * dvp should be locked on entry and will be locked on return.
510 * If an error is returned, *vpp will be invalid, otherwise it will hold a
511 * locked, referenced vnode. If *vpp == dvp then remember that only one
512 * LK_EXCLUSIVE lock is held.
515 unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
516 struct componentname *cnp, struct componentname *cn,
517 struct thread *td, char *path, int pathlen, u_long nameiop)
521 cn->cn_namelen = pathlen;
522 cn->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
523 bcopy(path, cn->cn_pnbuf, pathlen);
524 cn->cn_pnbuf[pathlen] = '\0';
526 cn->cn_nameiop = nameiop;
527 cn->cn_flags = (LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | ISLASTCN);
528 cn->cn_lkflags = LK_EXCLUSIVE;
530 cn->cn_cred = cnp->cn_cred;
532 cn->cn_nameptr = cn->cn_pnbuf;
533 cn->cn_consume = cnp->cn_consume;
535 if (nameiop == DELETE)
536 cn->cn_flags |= (cnp->cn_flags & (DOWHITEOUT | SAVESTART));
537 else if (RENAME == nameiop)
538 cn->cn_flags |= (cnp->cn_flags & SAVESTART);
539 else if (nameiop == CREATE)
540 cn->cn_flags |= NOCACHE;
543 VOP_UNLOCK(dvp, LK_RELEASE);
545 if ((error = relookup(dvp, vpp, cn))) {
546 uma_zfree(namei_zone, cn->cn_pnbuf);
547 cn->cn_flags &= ~HASBUF;
548 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
556 * relookup for CREATE namei operation.
558 * dvp is unionfs vnode. dvp should be locked.
560 * If it called 'unionfs_copyfile' function by unionfs_link etc,
561 * VOP_LOOKUP information is broken.
562 * So it need relookup in order to create link etc.
565 unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp,
571 struct componentname cn;
573 udvp = UNIONFSVPTOUPPERVP(dvp);
576 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
577 strlen(cnp->cn_nameptr), CREATE);
590 if (cn.cn_flags & HASBUF) {
591 uma_zfree(namei_zone, cn.cn_pnbuf);
592 cn.cn_flags &= ~HASBUF;
596 cn.cn_flags |= (cnp->cn_flags & HASBUF);
597 cnp->cn_flags = cn.cn_flags;
604 * relookup for DELETE namei operation.
606 * dvp is unionfs vnode. dvp should be locked.
609 unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp,
615 struct componentname cn;
617 udvp = UNIONFSVPTOUPPERVP(dvp);
620 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
621 strlen(cnp->cn_nameptr), DELETE);
634 if (cn.cn_flags & HASBUF) {
635 uma_zfree(namei_zone, cn.cn_pnbuf);
636 cn.cn_flags &= ~HASBUF;
640 cn.cn_flags |= (cnp->cn_flags & HASBUF);
641 cnp->cn_flags = cn.cn_flags;
648 * relookup for RENAME namei operation.
650 * dvp is unionfs vnode. dvp should be locked.
653 unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp,
659 struct componentname cn;
661 udvp = UNIONFSVPTOUPPERVP(dvp);
664 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
665 strlen(cnp->cn_nameptr), RENAME);
676 if (cn.cn_flags & HASBUF) {
677 uma_zfree(namei_zone, cn.cn_pnbuf);
678 cn.cn_flags &= ~HASBUF;
682 cn.cn_flags |= (cnp->cn_flags & HASBUF);
683 cnp->cn_flags = cn.cn_flags;
691 * Update the unionfs_node.
693 * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
694 * uvp's lock and lower's lock will be unlocked.
697 unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp,
700 unsigned count, lockrec;
705 vp = UNIONFSTOV(unp);
706 lvp = unp->un_lowervp;
707 ASSERT_VOP_ELOCKED(lvp, "unionfs_node_update");
714 unp->un_uppervp = uvp;
715 vp->v_vnlock = uvp->v_vnlock;
717 lockrec = lvp->v_vnlock->lk_recurse;
718 for (count = 0; count < lockrec; count++)
719 vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
724 if (unp->un_path != NULL && dvp != NULLVP && vp->v_type == VDIR) {
725 static struct unionfs_node_hashhead *hd;
728 hd = unionfs_get_hashhead(dvp, unp->un_path);
729 LIST_REMOVE(unp, un_hash);
730 LIST_INSERT_HEAD(hd, unp, un_hash);
736 * Create a new shadow dir.
738 * udvp should be locked on entry and will be locked on return.
740 * If no error returned, unp will be updated.
743 unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
744 struct unionfs_node *unp, struct componentname *cnp,
752 struct componentname cn;
755 struct ucred *credbk;
756 struct uidinfo *rootinfo;
758 if (unp->un_uppervp != NULLVP)
761 lvp = unp->un_lowervp;
763 credbk = cnp->cn_cred;
765 /* Authority change to root */
766 rootinfo = uifind((uid_t)0);
767 cred = crdup(cnp->cn_cred);
769 * The calls to chgproccnt() are needed to compensate for change_ruid()
770 * calling chgproccnt().
772 chgproccnt(cred->cr_ruidinfo, 1, 0);
773 change_euid(cred, rootinfo);
774 change_ruid(cred, rootinfo);
775 change_svuid(cred, (uid_t)0);
779 memset(&cn, 0, sizeof(cn));
781 if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred)))
782 goto unionfs_mkshadowdir_abort;
784 if ((error = unionfs_relookup(udvp, &uvp, cnp, &cn, td, cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
785 goto unionfs_mkshadowdir_abort;
793 goto unionfs_mkshadowdir_free_out;
796 if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)))
797 goto unionfs_mkshadowdir_free_out;
798 unionfs_create_uppervattr_core(ump, &lva, &va, td);
800 error = VOP_MKDIR(udvp, &uvp, &cn, &va);
803 unionfs_node_update(unp, uvp, td);
806 * XXX The bug which cannot set uid/gid was corrected.
810 VOP_SETATTR(uvp, &va, cn.cn_cred);
812 vn_finished_write(mp);
814 unionfs_mkshadowdir_free_out:
815 if (cn.cn_flags & HASBUF) {
816 uma_zfree(namei_zone, cn.cn_pnbuf);
817 cn.cn_flags &= ~HASBUF;
820 unionfs_mkshadowdir_abort:
821 cnp->cn_cred = credbk;
822 chgproccnt(cred->cr_ruidinfo, -1, 0);
829 * Create a new whiteout.
831 * dvp should be locked on entry and will be locked on return.
834 unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp,
835 struct thread *td, char *path)
839 struct componentname cn;
843 path = cnp->cn_nameptr;
846 if ((error = unionfs_relookup(dvp, &wvp, cnp, &cn, td, path, strlen(path), CREATE)))
849 if (cn.cn_flags & HASBUF) {
850 uma_zfree(namei_zone, cn.cn_pnbuf);
851 cn.cn_flags &= ~HASBUF;
861 if ((error = vn_start_write(dvp, &mp, V_WAIT | PCATCH)))
862 goto unionfs_mkwhiteout_free_out;
863 error = VOP_WHITEOUT(dvp, &cn, CREATE);
865 vn_finished_write(mp);
867 unionfs_mkwhiteout_free_out:
868 if (cn.cn_flags & HASBUF) {
869 uma_zfree(namei_zone, cn.cn_pnbuf);
870 cn.cn_flags &= ~HASBUF;
877 * Create a new vnode for create a new shadow file.
879 * If an error is returned, *vpp will be invalid, otherwise it will hold a
880 * locked, referenced and opened vnode.
882 * unp is never updated.
885 unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
886 struct unionfs_node *unp, struct vattr *uvap,
889 struct unionfs_mount *ump;
896 struct componentname cn;
898 ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
900 lvp = unp->un_lowervp;
902 fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
905 if ((error = VOP_GETATTR(lvp, &lva, cred)) != 0)
907 unionfs_create_uppervattr_core(ump, &lva, uvap, td);
909 if (unp->un_path == NULL)
910 panic("unionfs: un_path is null");
912 cn.cn_namelen = strlen(unp->un_path);
913 cn.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
914 bcopy(unp->un_path, cn.cn_pnbuf, cn.cn_namelen + 1);
915 cn.cn_nameiop = CREATE;
916 cn.cn_flags = (LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | ISLASTCN);
917 cn.cn_lkflags = LK_EXCLUSIVE;
920 cn.cn_nameptr = cn.cn_pnbuf;
924 if ((error = relookup(udvp, &vp, &cn)) != 0)
925 goto unionfs_vn_create_on_upper_free_out2;
934 goto unionfs_vn_create_on_upper_free_out1;
937 if ((error = VOP_CREATE(udvp, &vp, &cn, uvap)) != 0)
938 goto unionfs_vn_create_on_upper_free_out1;
940 if ((error = VOP_OPEN(vp, fmode, cred, td, NULL)) != 0) {
942 goto unionfs_vn_create_on_upper_free_out1;
944 VOP_ADD_WRITECOUNT(vp, 1);
945 CTR3(KTR_VFS, "%s: vp %p v_writecount increased to %d", __func__, vp,
949 unionfs_vn_create_on_upper_free_out1:
950 VOP_UNLOCK(udvp, LK_RELEASE);
952 unionfs_vn_create_on_upper_free_out2:
953 if (cn.cn_flags & HASBUF) {
954 uma_zfree(namei_zone, cn.cn_pnbuf);
955 cn.cn_flags &= ~HASBUF;
962 * Copy from lvp to uvp.
964 * lvp and uvp should be locked and opened on entry and will be locked and
968 unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
969 struct ucred *cred, struct thread *td)
980 memset(&uio, 0, sizeof(uio));
983 uio.uio_segflg = UIO_SYSSPACE;
986 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
989 offset = uio.uio_offset;
994 iov.iov_len = MAXBSIZE;
995 uio.uio_resid = iov.iov_len;
996 uio.uio_rw = UIO_READ;
998 if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
1000 if ((count = MAXBSIZE - uio.uio_resid) == 0)
1004 while (bufoffset < count) {
1007 iov.iov_base = buf + bufoffset;
1008 iov.iov_len = count - bufoffset;
1009 uio.uio_offset = offset + bufoffset;
1010 uio.uio_resid = iov.iov_len;
1011 uio.uio_rw = UIO_WRITE;
1013 if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
1016 bufoffset += (count - bufoffset) - uio.uio_resid;
1019 uio.uio_offset = offset + bufoffset;
1028 * Copy file from lower to upper.
1030 * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
1033 * If no error returned, unp will be updated.
1036 unionfs_copyfile(struct unionfs_node *unp, int docopy, struct ucred *cred,
1046 lvp = unp->un_lowervp;
1049 if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
1051 if (unp->un_dvp == NULLVP)
1053 if (unp->un_uppervp != NULLVP)
1055 udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
1058 if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
1061 error = VOP_ACCESS(lvp, VREAD, cred, td);
1065 if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)) != 0)
1067 error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva, td);
1069 vn_finished_write(mp);
1074 error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
1076 error = unionfs_copyfile_core(lvp, uvp, cred, td);
1077 VOP_CLOSE(lvp, FREAD, cred, td);
1080 VOP_CLOSE(uvp, FWRITE, cred, td);
1081 VOP_ADD_WRITECOUNT(uvp, -1);
1082 CTR3(KTR_VFS, "%s: vp %p v_writecount decreased to %d", __func__, uvp,
1085 vn_finished_write(mp);
1088 /* Reset the attributes. Ignore errors. */
1090 VOP_SETATTR(uvp, &uva, cred);
1093 unionfs_node_update(unp, uvp, td);
1099 * It checks whether vp can rmdir. (check empty)
1101 * vp is unionfs vnode.
1102 * vp should be locked.
1105 unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td)
1114 struct componentname cn;
1116 * The size of buf needs to be larger than DIRBLKSIZ.
1124 ASSERT_VOP_ELOCKED(vp, "unionfs_check_rmdir");
1127 uvp = UNIONFSVPTOUPPERVP(vp);
1128 lvp = UNIONFSVPTOLOWERVP(vp);
1131 if ((error = VOP_GETATTR(uvp, &va, cred)) != 0)
1133 if (va.va_flags & OPAQUE)
1138 if ((error = mac_vnode_check_open(cred, vp, VEXEC|VREAD)) != 0)
1141 if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred, td)) != 0)
1143 if ((error = VOP_OPEN(vp, FREAD, cred, td, NULL)) != 0)
1146 uio.uio_rw = UIO_READ;
1147 uio.uio_segflg = UIO_SYSSPACE;
1152 error = mac_vnode_check_readdir(td->td_ucred, lvp);
1154 while (!error && !eofflag) {
1156 iov.iov_len = sizeof(buf);
1159 uio.uio_resid = iov.iov_len;
1161 error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
1164 if (eofflag == 0 && uio.uio_resid == sizeof(buf)) {
1166 panic("bad readdir response from lower FS.");
1171 edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
1172 for (dp = (struct dirent*)buf; !error && dp < edp;
1173 dp = (struct dirent*)((caddr_t)dp + dp->d_reclen)) {
1174 if (dp->d_type == DT_WHT || dp->d_fileno == 0 ||
1175 (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1176 (dp->d_namlen == 2 && !bcmp(dp->d_name, "..", 2)))
1179 cn.cn_namelen = dp->d_namlen;
1181 cn.cn_nameptr = dp->d_name;
1182 cn.cn_nameiop = LOOKUP;
1183 cn.cn_flags = (LOCKPARENT | LOCKLEAF | SAVENAME | RDONLY | ISLASTCN);
1184 cn.cn_lkflags = LK_EXCLUSIVE;
1190 * check entry in lower.
1191 * Sometimes, readdir function returns
1194 lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
1199 continue; /* skip entry */
1203 * If it has no exist/whiteout entry in upper,
1204 * directory is not empty.
1206 cn.cn_flags = (LOCKPARENT | LOCKLEAF | SAVENAME | RDONLY | ISLASTCN);
1207 lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
1212 /* ignore exist or whiteout entry */
1214 (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
1222 VOP_CLOSE(vp, FREAD, cred, td);
1230 unionfs_checkuppervp(struct vnode *vp, char *fil, int lno)
1232 struct unionfs_node *unp;
1234 unp = VTOUNIONFS(vp);
1237 if (vp->v_op != unionfs_vnodeop_p) {
1238 printf("unionfs_checkuppervp: on non-unionfs-node.\n");
1240 kdb_enter(KDB_WHY_UNIONFS,
1241 "unionfs_checkuppervp: on non-unionfs-node.\n");
1243 panic("unionfs_checkuppervp");
1246 return (unp->un_uppervp);
1250 unionfs_checklowervp(struct vnode *vp, char *fil, int lno)
1252 struct unionfs_node *unp;
1254 unp = VTOUNIONFS(vp);
1257 if (vp->v_op != unionfs_vnodeop_p) {
1258 printf("unionfs_checklowervp: on non-unionfs-node.\n");
1260 kdb_enter(KDB_WHY_UNIONFS,
1261 "unionfs_checklowervp: on non-unionfs-node.\n");
1263 panic("unionfs_checklowervp");
1266 return (unp->un_lowervp);