2 * Copyright (c) 1994 Jan-Simon Pendry
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
6 * Copyright (c) 2006 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>
61 #include <fs/unionfs/union.h>
63 #define NUNIONFSNODECACHE 16
65 static MALLOC_DEFINE(M_UNIONFSHASH, "UNIONFS hash", "UNIONFS hash table");
66 MALLOC_DEFINE(M_UNIONFSNODE, "UNIONFS node", "UNIONFS vnode private part");
67 MALLOC_DEFINE(M_UNIONFSPATH, "UNIONFS path", "UNIONFS path private part");
73 unionfs_init(struct vfsconf *vfsp)
75 UNIONFSDEBUG("unionfs_init\n"); /* printed during system boot */
83 unionfs_uninit(struct vfsconf *vfsp)
88 static struct unionfs_node_hashhead *
89 unionfs_get_hashhead(struct vnode *dvp, char *path)
93 struct unionfs_node *unp;
96 unp = VTOUNIONFS(dvp);
98 for (count = 0; path[count]; count++)
102 return (&(unp->un_hashtbl[hash & (unp->un_hashmask)]));
106 * Get the cached vnode.
108 static struct vnode *
109 unionfs_get_cached_vnode(struct vnode *uvp, struct vnode *lvp,
110 struct vnode *dvp, char *path)
112 struct unionfs_node_hashhead *hd;
113 struct unionfs_node *unp;
116 KASSERT((uvp == NULLVP || uvp->v_type == VDIR),
117 ("unionfs_get_cached_vnode: v_type != VDIR"));
118 KASSERT((lvp == NULLVP || lvp->v_type == VDIR),
119 ("unionfs_get_cached_vnode: v_type != VDIR"));
122 hd = unionfs_get_hashhead(dvp, path);
123 LIST_FOREACH(unp, hd, un_hash) {
124 if (!strcmp(unp->un_path, path)) {
125 vp = UNIONFSTOV(unp);
126 VI_LOCK_FLAGS(vp, MTX_DUPOK);
128 vp->v_iflag &= ~VI_OWEINACT;
129 if ((vp->v_iflag & (VI_DOOMED | VI_DOINGINACT)) != 0) {
143 * Add the new vnode into cache.
145 static struct vnode *
146 unionfs_ins_cached_vnode(struct unionfs_node *uncp,
147 struct vnode *dvp, char *path)
149 struct unionfs_node_hashhead *hd;
150 struct unionfs_node *unp;
153 KASSERT((uncp->un_uppervp==NULLVP || uncp->un_uppervp->v_type==VDIR),
154 ("unionfs_ins_cached_vnode: v_type != VDIR"));
155 KASSERT((uncp->un_lowervp==NULLVP || uncp->un_lowervp->v_type==VDIR),
156 ("unionfs_ins_cached_vnode: v_type != VDIR"));
159 hd = unionfs_get_hashhead(dvp, path);
160 LIST_FOREACH(unp, hd, un_hash) {
161 if (!strcmp(unp->un_path, path)) {
162 vp = UNIONFSTOV(unp);
163 VI_LOCK_FLAGS(vp, MTX_DUPOK);
164 vp->v_iflag &= ~VI_OWEINACT;
165 if ((vp->v_iflag & (VI_DOOMED | VI_DOINGINACT)) != 0) {
166 LIST_INSERT_HEAD(hd, uncp, un_hash);
176 LIST_INSERT_HEAD(hd, uncp, un_hash);
186 unionfs_rem_cached_vnode(struct unionfs_node *unp, struct vnode *dvp)
188 KASSERT((unp != NULL), ("unionfs_rem_cached_vnode: null node"));
189 KASSERT((dvp != NULLVP),
190 ("unionfs_rem_cached_vnode: null parent vnode"));
191 KASSERT((unp->un_hash.le_prev != NULL),
192 ("unionfs_rem_cached_vnode: null hash"));
195 LIST_REMOVE(unp, un_hash);
196 unp->un_hash.le_next = NULL;
197 unp->un_hash.le_prev = NULL;
202 * Make a new or get existing unionfs node.
204 * uppervp and lowervp should be unlocked. Because if new unionfs vnode is
205 * locked, uppervp or lowervp is locked too. In order to prevent dead lock,
206 * you should not lock plurality simultaneously.
209 unionfs_nodeget(struct mount *mp, struct vnode *uppervp,
210 struct vnode *lowervp, struct vnode *dvp,
211 struct vnode **vpp, struct componentname *cnp,
214 struct unionfs_mount *ump;
215 struct unionfs_node *unp;
222 ump = MOUNTTOUNIONFSMOUNT(mp);
223 lkflags = (cnp ? cnp->cn_lkflags : 0);
224 path = (cnp ? cnp->cn_nameptr : NULL);
227 if (uppervp == NULLVP && lowervp == NULLVP)
228 panic("unionfs_nodeget: upper and lower is null");
230 vt = (uppervp != NULLVP ? uppervp->v_type : lowervp->v_type);
232 /* If it has no ISLASTCN flag, path check is skipped. */
233 if (cnp && !(cnp->cn_flags & ISLASTCN))
236 /* check the cache */
237 if (path != NULL && dvp != NULLVP && vt == VDIR) {
238 vp = unionfs_get_cached_vnode(uppervp, lowervp, dvp, path);
242 goto unionfs_nodeget_out;
246 if ((uppervp == NULLVP || ump->um_uppervp != uppervp) ||
247 (lowervp == NULLVP || ump->um_lowervp != lowervp)) {
248 /* dvp will be NULLVP only in case of root vnode. */
254 * Do the MALLOC before the getnewvnode since doing so afterward
255 * might cause a bogus v_data pointer to get dereferenced elsewhere
256 * if MALLOC should block.
258 unp = malloc(sizeof(struct unionfs_node),
259 M_UNIONFSNODE, M_WAITOK | M_ZERO);
261 error = getnewvnode("unionfs", mp, &unionfs_vnodeops, &vp);
263 free(unp, M_UNIONFSNODE);
266 error = insmntque(vp, mp); /* XXX: Too early for mpsafe fs */
268 free(unp, M_UNIONFSNODE);
273 if (uppervp != NULLVP)
275 if (lowervp != NULLVP)
279 unp->un_hashtbl = hashinit(NUNIONFSNODECACHE, M_UNIONFSHASH,
280 &(unp->un_hashmask));
283 unp->un_uppervp = uppervp;
284 unp->un_lowervp = lowervp;
286 if (uppervp != NULLVP)
287 vp->v_vnlock = uppervp->v_vnlock;
289 vp->v_vnlock = lowervp->v_vnlock;
292 unp->un_path = (char *)
293 malloc(cnp->cn_namelen +1, M_UNIONFSPATH, M_WAITOK|M_ZERO);
294 bcopy(cnp->cn_nameptr, unp->un_path, cnp->cn_namelen);
295 unp->un_path[cnp->cn_namelen] = '\0';
300 if ((uppervp != NULLVP && ump->um_uppervp == uppervp) &&
301 (lowervp != NULLVP && ump->um_lowervp == lowervp))
302 vp->v_vflag |= VV_ROOT;
304 if (path != NULL && dvp != NULLVP && vt == VDIR)
305 *vpp = unionfs_ins_cached_vnode(unp, dvp, path);
306 if ((*vpp) != NULLVP) {
309 if (uppervp != NULLVP)
311 if (lowervp != NULLVP)
314 unp->un_uppervp = NULLVP;
315 unp->un_lowervp = NULLVP;
316 unp->un_dvp = NULLVP;
324 if (lkflags & LK_TYPE_MASK)
325 vn_lock(vp, lkflags | LK_RETRY, td);
331 * Clean up the unionfs node.
334 unionfs_noderem(struct vnode *vp, struct thread *td)
338 struct unionfs_node *unp, *unp_t1, *unp_t2;
339 struct unionfs_node_hashhead *hd;
340 struct unionfs_node_status *unsp, *unsp_tmp;
346 * Use the interlock to protect the clearing of v_data to
347 * prevent faults in unionfs_lock().
350 unp = VTOUNIONFS(vp);
351 lvp = unp->un_lowervp;
352 uvp = unp->un_uppervp;
354 unp->un_lowervp = unp->un_uppervp = NULLVP;
356 vp->v_vnlock = &(vp->v_lock);
358 lockmgr(vp->v_vnlock, LK_EXCLUSIVE | LK_INTERLOCK, VI_MTX(vp), td);
360 VOP_UNLOCK(lvp, 0, td);
362 VOP_UNLOCK(uvp, 0, td);
365 if (dvp != NULLVP && unp->un_hash.le_prev != NULL)
366 unionfs_rem_cached_vnode(unp, dvp);
369 vfslocked = VFS_LOCK_GIANT(lvp->v_mount);
371 VFS_UNLOCK_GIANT(vfslocked);
374 vfslocked = VFS_LOCK_GIANT(uvp->v_mount);
376 VFS_UNLOCK_GIANT(vfslocked);
379 vfslocked = VFS_LOCK_GIANT(dvp->v_mount);
381 VFS_UNLOCK_GIANT(vfslocked);
382 unp->un_dvp = NULLVP;
384 if (unp->un_path != NULL) {
385 free(unp->un_path, M_UNIONFSPATH);
389 if (unp->un_hashtbl != NULL) {
390 for (count = 0; count <= unp->un_hashmask; count++) {
391 hd = unp->un_hashtbl + count;
392 LIST_FOREACH_SAFE(unp_t1, hd, un_hash, unp_t2) {
393 LIST_REMOVE(unp_t1, un_hash);
394 unp_t1->un_hash.le_next = NULL;
395 unp_t1->un_hash.le_prev = NULL;
398 hashdestroy(unp->un_hashtbl, M_UNIONFSHASH, unp->un_hashmask);
401 LIST_FOREACH_SAFE(unsp, &(unp->un_unshead), uns_list, unsp_tmp) {
402 LIST_REMOVE(unsp, uns_list);
405 free(unp, M_UNIONFSNODE);
409 * Get the unionfs node status.
410 * You need exclusive lock this vnode.
413 unionfs_get_node_status(struct unionfs_node *unp, struct thread *td,
414 struct unionfs_node_status **unspp)
416 struct unionfs_node_status *unsp;
417 pid_t pid = td->td_proc->p_pid;
419 KASSERT(NULL != unspp, ("null pointer"));
420 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status");
422 LIST_FOREACH(unsp, &(unp->un_unshead), uns_list) {
423 if (unsp->uns_pid == pid) {
429 /* create a new unionfs node status */
430 unsp = malloc(sizeof(struct unionfs_node_status),
431 M_TEMP, M_WAITOK | M_ZERO);
434 LIST_INSERT_HEAD(&(unp->un_unshead), unsp, uns_list);
440 * Remove the unionfs node status, if you can.
441 * You need exclusive lock this vnode.
444 unionfs_tryrem_node_status(struct unionfs_node *unp,
445 struct unionfs_node_status *unsp)
447 KASSERT(NULL != unsp, ("null pointer"));
448 ASSERT_VOP_ELOCKED(UNIONFSTOV(unp), "unionfs_get_node_status");
450 if (0 < unsp->uns_lower_opencnt || 0 < unsp->uns_upper_opencnt)
453 LIST_REMOVE(unsp, uns_list);
458 * Create upper node attr.
461 unionfs_create_uppervattr_core(struct unionfs_mount *ump,
467 uva->va_type = lva->va_type;
468 uva->va_atime = lva->va_atime;
469 uva->va_mtime = lva->va_mtime;
470 uva->va_ctime = lva->va_ctime;
472 switch (ump->um_copymode) {
473 case UNIONFS_TRANSPARENT:
474 uva->va_mode = lva->va_mode;
475 uva->va_uid = lva->va_uid;
476 uva->va_gid = lva->va_gid;
478 case UNIONFS_MASQUERADE:
479 if (ump->um_uid == lva->va_uid) {
480 uva->va_mode = lva->va_mode & 077077;
481 uva->va_mode |= (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile) & 0700;
482 uva->va_uid = lva->va_uid;
483 uva->va_gid = lva->va_gid;
485 uva->va_mode = (lva->va_type == VDIR ? ump->um_udir : ump->um_ufile);
486 uva->va_uid = ump->um_uid;
487 uva->va_gid = ump->um_gid;
490 default: /* UNIONFS_TRADITIONAL */
491 FILEDESC_SLOCK(td->td_proc->p_fd);
492 uva->va_mode = 0777 & ~td->td_proc->p_fd->fd_cmask;
493 FILEDESC_SUNLOCK(td->td_proc->p_fd);
494 uva->va_uid = ump->um_uid;
495 uva->va_gid = ump->um_gid;
501 * Create upper node attr.
504 unionfs_create_uppervattr(struct unionfs_mount *ump,
513 if ((error = VOP_GETATTR(lvp, &lva, cred, td)))
516 unionfs_create_uppervattr_core(ump, &lva, uva, td);
524 * dvp should be locked on entry and will be locked on return.
526 * If an error is returned, *vpp will be invalid, otherwise it will hold a
527 * locked, referenced vnode. If *vpp == dvp then remember that only one
528 * LK_EXCLUSIVE lock is held.
531 unionfs_relookup(struct vnode *dvp, struct vnode **vpp,
532 struct componentname *cnp, struct componentname *cn,
533 struct thread *td, char *path, int pathlen, u_long nameiop)
537 cn->cn_namelen = pathlen;
538 cn->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
539 bcopy(path, cn->cn_pnbuf, pathlen);
540 cn->cn_pnbuf[pathlen] = '\0';
542 cn->cn_nameiop = nameiop;
543 cn->cn_flags = (LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | ISLASTCN);
544 cn->cn_lkflags = LK_EXCLUSIVE;
546 cn->cn_cred = cnp->cn_cred;
548 cn->cn_nameptr = cn->cn_pnbuf;
549 cn->cn_consume = cnp->cn_consume;
551 if (nameiop == DELETE)
552 cn->cn_flags |= (cnp->cn_flags & (DOWHITEOUT | SAVESTART));
553 else if (RENAME == nameiop)
554 cn->cn_flags |= (cnp->cn_flags & SAVESTART);
557 VOP_UNLOCK(dvp, 0, td);
559 if ((error = relookup(dvp, vpp, cn))) {
560 uma_zfree(namei_zone, cn->cn_pnbuf);
561 cn->cn_flags &= ~HASBUF;
562 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
570 * relookup for CREATE namei operation.
572 * dvp is unionfs vnode. dvp should be locked.
574 * If it called 'unionfs_copyfile' function by unionfs_link etc,
575 * VOP_LOOKUP information is broken.
576 * So it need relookup in order to create link etc.
579 unionfs_relookup_for_create(struct vnode *dvp, struct componentname *cnp,
585 struct componentname cn;
587 udvp = UNIONFSVPTOUPPERVP(dvp);
590 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
591 strlen(cnp->cn_nameptr), CREATE);
604 if (cn.cn_flags & HASBUF) {
605 uma_zfree(namei_zone, cn.cn_pnbuf);
606 cn.cn_flags &= ~HASBUF;
610 cn.cn_flags |= (cnp->cn_flags & HASBUF);
611 cnp->cn_flags = cn.cn_flags;
618 * relookup for DELETE namei operation.
620 * dvp is unionfs vnode. dvp should be locked.
623 unionfs_relookup_for_delete(struct vnode *dvp, struct componentname *cnp,
629 struct componentname cn;
631 udvp = UNIONFSVPTOUPPERVP(dvp);
634 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
635 strlen(cnp->cn_nameptr), DELETE);
648 if (cn.cn_flags & HASBUF) {
649 uma_zfree(namei_zone, cn.cn_pnbuf);
650 cn.cn_flags &= ~HASBUF;
654 cn.cn_flags |= (cnp->cn_flags & HASBUF);
655 cnp->cn_flags = cn.cn_flags;
662 * relookup for RENAME namei operation.
664 * dvp is unionfs vnode. dvp should be locked.
667 unionfs_relookup_for_rename(struct vnode *dvp, struct componentname *cnp,
673 struct componentname cn;
675 udvp = UNIONFSVPTOUPPERVP(dvp);
678 error = unionfs_relookup(udvp, &vp, cnp, &cn, td, cnp->cn_nameptr,
679 strlen(cnp->cn_nameptr), RENAME);
690 if (cn.cn_flags & HASBUF) {
691 uma_zfree(namei_zone, cn.cn_pnbuf);
692 cn.cn_flags &= ~HASBUF;
696 cn.cn_flags |= (cnp->cn_flags & HASBUF);
697 cnp->cn_flags = cn.cn_flags;
705 * Update the unionfs_node.
707 * uvp is new locked upper vnode. unionfs vnode's lock will be exchanged to the
708 * uvp's lock and lower's lock will be unlocked.
711 unionfs_node_update(struct unionfs_node *unp, struct vnode *uvp,
719 vp = UNIONFSTOV(unp);
720 lvp = unp->un_lowervp;
721 ASSERT_VOP_ELOCKED(lvp, "unionfs_node_update");
728 unp->un_uppervp = uvp;
729 vp->v_vnlock = uvp->v_vnlock;
730 lockcnt = lvp->v_vnlock->lk_exclusivecount;
732 panic("unionfs: no exclusive lock");
734 for (count = 1; count < lockcnt; count++)
735 vn_lock(uvp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
740 if (unp->un_path != NULL && dvp != NULLVP && vp->v_type == VDIR) {
741 static struct unionfs_node_hashhead *hd;
744 hd = unionfs_get_hashhead(dvp, unp->un_path);
745 LIST_REMOVE(unp, un_hash);
746 LIST_INSERT_HEAD(hd, unp, un_hash);
752 * Create a new shadow dir.
754 * udvp should be locked on entry and will be locked on return.
756 * If no error returned, unp will be updated.
759 unionfs_mkshadowdir(struct unionfs_mount *ump, struct vnode *udvp,
760 struct unionfs_node *unp, struct componentname *cnp,
768 struct componentname cn;
771 struct ucred *credbk;
772 struct uidinfo *rootinfo;
774 if (unp->un_uppervp != NULLVP)
777 lvp = unp->un_lowervp;
779 credbk = cnp->cn_cred;
781 /* Authority change to root */
782 rootinfo = uifind((uid_t)0);
783 cred = crdup(cnp->cn_cred);
784 chgproccnt(cred->cr_ruidinfo, 1, 0);
785 change_euid(cred, rootinfo);
786 change_ruid(cred, rootinfo);
787 change_svuid(cred, (uid_t)0);
791 memset(&cn, 0, sizeof(cn));
793 if ((error = VOP_GETATTR(lvp, &lva, cnp->cn_cred, td)))
794 goto unionfs_mkshadowdir_abort;
796 if ((error = unionfs_relookup(udvp, &uvp, cnp, &cn, td, cnp->cn_nameptr, cnp->cn_namelen, CREATE)))
797 goto unionfs_mkshadowdir_abort;
805 goto unionfs_mkshadowdir_free_out;
808 if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)))
809 goto unionfs_mkshadowdir_free_out;
810 if ((error = VOP_LEASE(udvp, td, cn.cn_cred, LEASE_WRITE))) {
811 vn_finished_write(mp);
812 goto unionfs_mkshadowdir_free_out;
814 unionfs_create_uppervattr_core(ump, &lva, &va, td);
816 error = VOP_MKDIR(udvp, &uvp, &cn, &va);
819 unionfs_node_update(unp, uvp, td);
822 * XXX The bug which cannot set uid/gid was corrected.
826 VOP_SETATTR(uvp, &va, cn.cn_cred, td);
828 vn_finished_write(mp);
830 unionfs_mkshadowdir_free_out:
831 if (cn.cn_flags & HASBUF) {
832 uma_zfree(namei_zone, cn.cn_pnbuf);
833 cn.cn_flags &= ~HASBUF;
836 unionfs_mkshadowdir_abort:
837 cnp->cn_cred = credbk;
838 chgproccnt(cred->cr_ruidinfo, -1, 0);
845 * Create a new whiteout.
847 * dvp should be locked on entry and will be locked on return.
850 unionfs_mkwhiteout(struct vnode *dvp, struct componentname *cnp,
851 struct thread *td, char *path)
855 struct componentname cn;
859 path = cnp->cn_nameptr;
862 if ((error = unionfs_relookup(dvp, &wvp, cnp, &cn, td, path, strlen(path), CREATE)))
865 if (cn.cn_flags & HASBUF) {
866 uma_zfree(namei_zone, cn.cn_pnbuf);
867 cn.cn_flags &= ~HASBUF;
877 if ((error = vn_start_write(dvp, &mp, V_WAIT | PCATCH)))
878 goto unionfs_mkwhiteout_free_out;
879 if (!(error = VOP_LEASE(dvp, td, td->td_ucred, LEASE_WRITE)))
880 error = VOP_WHITEOUT(dvp, &cn, CREATE);
882 vn_finished_write(mp);
884 unionfs_mkwhiteout_free_out:
885 if (cn.cn_flags & HASBUF) {
886 uma_zfree(namei_zone, cn.cn_pnbuf);
887 cn.cn_flags &= ~HASBUF;
894 * Create a new vnode for create a new shadow file.
896 * If an error is returned, *vpp will be invalid, otherwise it will hold a
897 * locked, referenced and opened vnode.
899 * unp is never updated.
902 unionfs_vn_create_on_upper(struct vnode **vpp, struct vnode *udvp,
903 struct unionfs_node *unp, struct vattr *uvap,
906 struct unionfs_mount *ump;
913 struct componentname cn;
915 ump = MOUNTTOUNIONFSMOUNT(UNIONFSTOV(unp)->v_mount);
917 lvp = unp->un_lowervp;
919 fmode = FFLAGS(O_WRONLY | O_CREAT | O_TRUNC | O_EXCL);
922 if ((error = VOP_GETATTR(lvp, &lva, cred, td)) != 0)
924 unionfs_create_uppervattr_core(ump, &lva, uvap, td);
926 if (unp->un_path == NULL)
927 panic("unionfs: un_path is null");
929 cn.cn_namelen = strlen(unp->un_path);
930 cn.cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
931 bcopy(unp->un_path, cn.cn_pnbuf, cn.cn_namelen + 1);
932 cn.cn_nameiop = CREATE;
933 cn.cn_flags = (LOCKPARENT | LOCKLEAF | HASBUF | SAVENAME | ISLASTCN);
934 cn.cn_lkflags = LK_EXCLUSIVE;
937 cn.cn_nameptr = cn.cn_pnbuf;
941 if ((error = relookup(udvp, &vp, &cn)) != 0)
942 goto unionfs_vn_create_on_upper_free_out2;
951 goto unionfs_vn_create_on_upper_free_out1;
954 if ((error = VOP_LEASE(udvp, td, cred, LEASE_WRITE)) != 0)
955 goto unionfs_vn_create_on_upper_free_out1;
957 if ((error = VOP_CREATE(udvp, &vp, &cn, uvap)) != 0)
958 goto unionfs_vn_create_on_upper_free_out1;
960 if ((error = VOP_OPEN(vp, fmode, cred, td, NULL)) != 0) {
962 goto unionfs_vn_create_on_upper_free_out1;
967 unionfs_vn_create_on_upper_free_out1:
968 VOP_UNLOCK(udvp, 0, td);
970 unionfs_vn_create_on_upper_free_out2:
971 if (cn.cn_flags & HASBUF) {
972 uma_zfree(namei_zone, cn.cn_pnbuf);
973 cn.cn_flags &= ~HASBUF;
980 * Copy from lvp to uvp.
982 * lvp and uvp should be locked and opened on entry and will be locked and
986 unionfs_copyfile_core(struct vnode *lvp, struct vnode *uvp,
987 struct ucred *cred, struct thread *td)
998 memset(&uio, 0, sizeof(uio));
1001 uio.uio_segflg = UIO_SYSSPACE;
1004 if ((error = VOP_LEASE(lvp, td, cred, LEASE_READ)) != 0)
1006 if ((error = VOP_LEASE(uvp, td, cred, LEASE_WRITE)) != 0)
1008 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
1010 while (error == 0) {
1011 offset = uio.uio_offset;
1016 iov.iov_len = MAXBSIZE;
1017 uio.uio_resid = iov.iov_len;
1018 uio.uio_rw = UIO_READ;
1020 if ((error = VOP_READ(lvp, &uio, 0, cred)) != 0)
1022 if ((count = MAXBSIZE - uio.uio_resid) == 0)
1026 while (bufoffset < count) {
1029 iov.iov_base = buf + bufoffset;
1030 iov.iov_len = count - bufoffset;
1031 uio.uio_offset = offset + bufoffset;
1032 uio.uio_resid = iov.iov_len;
1033 uio.uio_rw = UIO_WRITE;
1035 if ((error = VOP_WRITE(uvp, &uio, 0, cred)) != 0)
1038 bufoffset += (count - bufoffset) - uio.uio_resid;
1041 uio.uio_offset = offset + bufoffset;
1050 * Copy file from lower to upper.
1052 * If you need copy of the contents, set 1 to docopy. Otherwise, set 0 to
1055 * If no error returned, unp will be updated.
1058 unionfs_copyfile(struct unionfs_node *unp, int docopy, struct ucred *cred,
1068 lvp = unp->un_lowervp;
1071 if ((UNIONFSTOV(unp)->v_mount->mnt_flag & MNT_RDONLY))
1073 if (unp->un_dvp == NULLVP)
1075 if (unp->un_uppervp != NULLVP)
1077 udvp = VTOUNIONFS(unp->un_dvp)->un_uppervp;
1080 if ((udvp->v_mount->mnt_flag & MNT_RDONLY))
1083 error = VOP_ACCESS(lvp, VREAD, cred, td);
1087 if ((error = vn_start_write(udvp, &mp, V_WAIT | PCATCH)) != 0)
1089 error = unionfs_vn_create_on_upper(&uvp, udvp, unp, &uva, td);
1091 vn_finished_write(mp);
1096 error = VOP_OPEN(lvp, FREAD, cred, td, NULL);
1098 error = unionfs_copyfile_core(lvp, uvp, cred, td);
1099 VOP_CLOSE(lvp, FREAD, cred, td);
1102 VOP_CLOSE(uvp, FWRITE, cred, td);
1103 uvp->v_writecount--;
1105 vn_finished_write(mp);
1108 /* Reset the attributes. Ignore errors. */
1110 VOP_SETATTR(uvp, &uva, cred, td);
1113 unionfs_node_update(unp, uvp, td);
1119 * It checks whether vp can rmdir. (check empty)
1121 * vp is unionfs vnode.
1122 * vp should be locked.
1125 unionfs_check_rmdir(struct vnode *vp, struct ucred *cred, struct thread *td)
1134 struct componentname cn;
1136 * The size of buf needs to be larger than DIRBLKSIZ.
1144 ASSERT_VOP_ELOCKED(vp, "unionfs_check_rmdir");
1147 uvp = UNIONFSVPTOUPPERVP(vp);
1148 lvp = UNIONFSVPTOLOWERVP(vp);
1151 if ((error = VOP_GETATTR(uvp, &va, cred, td)) != 0)
1153 if (va.va_flags & OPAQUE)
1158 if ((error = mac_check_vnode_open(cred, vp, VEXEC|VREAD)) != 0)
1161 if ((error = VOP_ACCESS(vp, VEXEC|VREAD, cred, td)) != 0)
1163 if ((error = VOP_OPEN(vp, FREAD, cred, td, NULL)) != 0)
1166 uio.uio_rw = UIO_READ;
1167 uio.uio_segflg = UIO_SYSSPACE;
1172 error = mac_check_vnode_readdir(td->td_ucred, lvp);
1174 while (!error && !eofflag) {
1176 iov.iov_len = sizeof(buf);
1179 uio.uio_resid = iov.iov_len;
1181 error = VOP_READDIR(lvp, &uio, cred, &eofflag, NULL, NULL);
1184 if (eofflag == 0 && uio.uio_resid == sizeof(buf)) {
1186 panic("bad readdir response from lower FS.");
1191 edp = (struct dirent*)&buf[sizeof(buf) - uio.uio_resid];
1192 for (dp = (struct dirent*)buf; !error && dp < edp;
1193 dp = (struct dirent*)((caddr_t)dp + dp->d_reclen)) {
1194 if (dp->d_type == DT_WHT ||
1195 (dp->d_namlen == 1 && dp->d_name[0] == '.') ||
1196 (dp->d_namlen == 2 && !bcmp(dp->d_name, "..", 2)))
1199 cn.cn_namelen = dp->d_namlen;
1201 cn.cn_nameptr = dp->d_name;
1202 cn.cn_nameiop = LOOKUP;
1203 cn.cn_flags = (LOCKPARENT | LOCKLEAF | SAVENAME | RDONLY | ISLASTCN);
1204 cn.cn_lkflags = LK_EXCLUSIVE;
1210 * check entry in lower.
1211 * Sometimes, readdir function returns
1214 lookuperr = VOP_LOOKUP(lvp, &tvp, &cn);
1219 continue; /* skip entry */
1223 * If it has no exist/whiteout entry in upper,
1224 * directory is not empty.
1226 cn.cn_flags = (LOCKPARENT | LOCKLEAF | SAVENAME | RDONLY | ISLASTCN);
1227 lookuperr = VOP_LOOKUP(uvp, &tvp, &cn);
1232 /* ignore exist or whiteout entry */
1234 (lookuperr == ENOENT && (cn.cn_flags & ISWHITEOUT)))
1242 VOP_CLOSE(vp, FREAD, cred, td);
1250 unionfs_checkuppervp(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_checkuppervp: on non-unionfs-node.\n");
1260 kdb_enter_why(KDB_WHY_UNIONFS,
1261 "unionfs_checkuppervp: on non-unionfs-node.\n");
1263 panic("unionfs_checkuppervp");
1266 return (unp->un_uppervp);
1270 unionfs_checklowervp(struct vnode *vp, char *fil, int lno)
1272 struct unionfs_node *unp;
1274 unp = VTOUNIONFS(vp);
1277 if (vp->v_op != unionfs_vnodeop_p) {
1278 printf("unionfs_checklowervp: on non-unionfs-node.\n");
1280 kdb_enter_why(KDB_WHY_UNIONFS,
1281 "unionfs_checklowervp: on non-unionfs-node.\n");
1283 panic("unionfs_checklowervp");
1286 return (unp->un_lowervp);