]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/fs/unionfs/union_vnops.c
MFC:
[FreeBSD/FreeBSD.git] / sys / fs / unionfs / union_vnops.c
1 /*-
2  * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
3  * Copyright (c) 1992, 1993, 1994, 1995
4  *      The Regents of the University of California.
5  * Copyright (c) 2005, 2006 Masanori Ozawa <ozawa@ongs.co.jp>, ONGS Inc.
6  * Copyright (c) 2006 Daichi Goto <daichi@freebsd.org>
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Jan-Simon Pendry.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)union_vnops.c       8.32 (Berkeley) 6/23/95
37  * $FreeBSD$
38  *
39  */
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
49 #include <sys/namei.h>
50 #include <sys/sysctl.h>
51 #include <sys/vnode.h>
52 #include <sys/kdb.h>
53 #include <sys/fcntl.h>
54 #include <sys/stat.h>
55 #include <sys/dirent.h>
56 #include <sys/proc.h>
57 #include <sys/bio.h>
58 #include <sys/buf.h>
59
60 #include <fs/unionfs/union.h>
61
62 #include <vm/vm.h>
63 #include <vm/vm_extern.h>
64 #include <vm/vm_object.h>
65 #include <vm/vnode_pager.h>
66
67 #if 0
68 #define UNIONFS_INTERNAL_DEBUG(msg, args...)    printf(msg, ## args)
69 #define UNIONFS_IDBG_RENAME
70 #else
71 #define UNIONFS_INTERNAL_DEBUG(msg, args...)
72 #endif
73
74 /* lockmgr lock <-> reverse table */
75 struct lk_lr_table {
76         int     lock;
77         int     revlock;
78 };
79
80 static struct lk_lr_table un_llt[] = {
81         {LK_SHARED, LK_RELEASE},
82         {LK_EXCLUSIVE, LK_RELEASE},
83         {LK_UPGRADE, LK_DOWNGRADE},
84         {LK_EXCLUPGRADE, LK_DOWNGRADE},
85         {LK_DOWNGRADE, LK_UPGRADE},
86         {0, 0}
87 };
88
89
90 static int
91 unionfs_lookup(struct vop_cachedlookup_args *ap)
92 {
93         int             iswhiteout;
94         int             lockflag;
95         int             error , uerror, lerror;
96         u_long          nameiop;
97         u_long          cnflags, cnflagsbk;
98         struct unionfs_node *dunp;
99         struct vnode   *dvp, *udvp, *ldvp, *vp, *uvp, *lvp, *dtmpvp;
100         struct vattr    va;
101         struct componentname *cnp;
102         struct thread  *td;
103
104         iswhiteout = 0;
105         lockflag = 0;
106         error = uerror = lerror = ENOENT;
107         cnp = ap->a_cnp;
108         nameiop = cnp->cn_nameiop;
109         cnflags = cnp->cn_flags;
110         dvp = ap->a_dvp;
111         dunp = VTOUNIONFS(dvp);
112         udvp = dunp->un_uppervp;
113         ldvp = dunp->un_lowervp;
114         vp = uvp = lvp = NULLVP;
115         td = curthread;
116         *(ap->a_vpp) = NULLVP;
117
118         UNIONFS_INTERNAL_DEBUG("unionfs_lookup: enter: nameiop=%ld, flags=%lx, path=%s\n", nameiop, cnflags, cnp->cn_nameptr);
119
120         if (dvp->v_type != VDIR)
121                 return (ENOTDIR);
122
123         /*
124          * If read-only and op is not LOOKUP, will return EROFS.
125          */
126         if ((cnflags & ISLASTCN) &&
127             (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
128             LOOKUP != nameiop)
129                 return (EROFS);
130
131         /*
132          * lookup dotdot
133          */
134         if (cnflags & ISDOTDOT) {
135                 if (LOOKUP != nameiop && udvp == NULLVP)
136                         return (EROFS);
137
138                 if (udvp != NULLVP) {
139                         dtmpvp = udvp;
140                         if (ldvp != NULLVP)
141                                 VOP_UNLOCK(ldvp, 0, td);
142                 }
143                 else
144                         dtmpvp = ldvp;
145
146                 error = VOP_LOOKUP(dtmpvp, &vp, cnp);
147
148                 if (dtmpvp == udvp && ldvp != NULLVP) {
149                         VOP_UNLOCK(udvp, 0, td);
150                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
151                 }
152
153                 if (error == 0) {
154                         /*
155                          * Exchange lock and reference from vp to
156                          * dunp->un_dvp. vp is upper/lower vnode, but it
157                          * will need to return the unionfs vnode.
158                          */
159                         if (nameiop == DELETE  || nameiop == RENAME ||
160                             (cnp->cn_lkflags & LK_TYPE_MASK))
161                                 VOP_UNLOCK(vp, 0, td);
162                         vrele(vp);
163
164                         VOP_UNLOCK(dvp, 0, td);
165                         *(ap->a_vpp) = dunp->un_dvp;
166                         vref(dunp->un_dvp);
167
168                         if (nameiop == DELETE || nameiop == RENAME)
169                                 vn_lock(dunp->un_dvp, LK_EXCLUSIVE | LK_RETRY, td);
170                         else if (cnp->cn_lkflags & LK_TYPE_MASK)
171                                 vn_lock(dunp->un_dvp, cnp->cn_lkflags | LK_RETRY, td);
172
173                         vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
174                 } else if (error == ENOENT && (cnflags & MAKEENTRY) &&
175                     nameiop != CREATE)
176                         cache_enter(dvp, NULLVP, cnp);
177
178                 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error);
179
180                 return (error);
181         }
182
183         /*
184          * lookup upper layer
185          */
186         if (udvp != NULLVP) {
187                 uerror = VOP_LOOKUP(udvp, &uvp, cnp);
188
189                 if (uerror == 0) {
190                         if (udvp == uvp) {      /* is dot */
191                                 vrele(uvp);
192                                 *(ap->a_vpp) = dvp;
193                                 vref(dvp);
194
195                                 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", uerror);
196
197                                 return (uerror);
198                         }
199                         if (nameiop == DELETE || nameiop == RENAME ||
200                             (cnp->cn_lkflags & LK_TYPE_MASK))
201                                 VOP_UNLOCK(uvp, 0, td);
202                 }
203
204                 /* check whiteout */
205                 if (uerror == ENOENT || uerror == EJUSTRETURN)
206                         if (cnp->cn_flags & ISWHITEOUT)
207                                 iswhiteout = 1; /* don't lookup lower */
208                 if (iswhiteout == 0 && ldvp != NULLVP)
209                         if (VOP_GETATTR(udvp, &va, cnp->cn_cred, td) == 0 &&
210                             (va.va_flags & OPAQUE))
211                                 iswhiteout = 1; /* don't lookup lower */
212 #if 0
213                 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: debug: whiteout=%d, path=%s\n", iswhiteout, cnp->cn_nameptr);
214 #endif
215         }
216
217         /*
218          * lookup lower layer
219          */
220         if (ldvp != NULLVP && !(cnflags & DOWHITEOUT) && iswhiteout == 0) {
221                 /* always op is LOOKUP */
222                 cnp->cn_nameiop = LOOKUP;
223                 cnflagsbk = cnp->cn_flags;
224                 cnp->cn_flags = cnflags;
225
226                 lerror = VOP_LOOKUP(ldvp, &lvp, cnp);
227
228                 cnp->cn_nameiop = nameiop;
229                 if (udvp != NULLVP && (uerror == 0 || uerror == EJUSTRETURN))
230                         cnp->cn_flags = cnflagsbk;
231
232                 if (lerror == 0) {
233                         if (ldvp == lvp) {      /* is dot */
234                                 if (uvp != NULLVP)
235                                         vrele(uvp);     /* no need? */
236                                 vrele(lvp);
237                                 *(ap->a_vpp) = dvp;
238                                 vref(dvp);
239
240                                 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", lerror);
241
242                                 return (lerror);
243                         }
244                         if (cnp->cn_lkflags & LK_TYPE_MASK)
245                                 VOP_UNLOCK(lvp, 0, td);
246                 }
247         }
248
249         /*
250          * check lookup result
251          */
252         if (uvp == NULLVP && lvp == NULLVP) {
253                 UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n",
254                     (udvp != NULLVP ? uerror : lerror));
255                 return (udvp != NULLVP ? uerror : lerror);
256         }
257
258         /*
259          * check vnode type
260          */
261         if (uvp != NULLVP && lvp != NULLVP && uvp->v_type != lvp->v_type) {
262                 vrele(lvp);
263                 lvp = NULLVP;
264         }
265
266         /*
267          * check shadow dir
268          */
269         if (uerror != 0 && uerror != EJUSTRETURN && udvp != NULLVP &&
270             lerror == 0 && lvp != NULLVP && lvp->v_type == VDIR &&
271             !(dvp->v_mount->mnt_flag & MNT_RDONLY) &&
272             (1 < cnp->cn_namelen || '.' != *(cnp->cn_nameptr))) {
273                 /* get unionfs vnode in order to create a new shadow dir. */
274                 error = unionfs_nodeget(dvp->v_mount, NULLVP, lvp, dvp, &vp,
275                     cnp, td);
276                 if (error != 0)
277                         goto unionfs_lookup_out;
278
279                 if (LK_SHARED == (cnp->cn_lkflags & LK_TYPE_MASK))
280                         VOP_UNLOCK(vp, 0, td);
281                 if (LK_EXCLUSIVE != VOP_ISLOCKED(vp, td)) {
282                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
283                         lockflag = 1;
284                 }
285                 error = unionfs_mkshadowdir(MOUNTTOUNIONFSMOUNT(dvp->v_mount),
286                     udvp, VTOUNIONFS(vp), cnp, td);
287                 if (lockflag != 0)
288                         VOP_UNLOCK(vp, 0, td);
289                 if (error != 0) {
290                         UNIONFSDEBUG("unionfs_lookup: Unable to create shadow dir.");
291                         if ((cnp->cn_lkflags & LK_TYPE_MASK) == LK_EXCLUSIVE)
292                                 vput(vp);
293                         else
294                                 vrele(vp);
295                         goto unionfs_lookup_out;
296                 }
297                 if ((cnp->cn_lkflags & LK_TYPE_MASK) == LK_SHARED)
298                         vn_lock(vp, LK_SHARED | LK_RETRY, td);
299         }
300         /*
301          * get unionfs vnode.
302          */
303         else {
304                 if (uvp != NULLVP)
305                         error = uerror;
306                 else
307                         error = lerror;
308                 if (error != 0)
309                         goto unionfs_lookup_out;
310                 error = unionfs_nodeget(dvp->v_mount, uvp, lvp, dvp, &vp,
311                     cnp, td);
312                 if (error != 0) {
313                         UNIONFSDEBUG("unionfs_lookup: Unable to create unionfs vnode.");
314                         goto unionfs_lookup_out;
315                 }
316                 if ((nameiop == DELETE || nameiop == RENAME) &&
317                     (cnp->cn_lkflags & LK_TYPE_MASK) == 0)
318                         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
319         }
320
321         *(ap->a_vpp) = vp;
322
323         if (cnflags & MAKEENTRY)
324                 cache_enter(dvp, vp, cnp);
325
326 unionfs_lookup_out:
327         if (uvp != NULLVP)
328                 vrele(uvp);
329         if (lvp != NULLVP)
330                 vrele(lvp);
331
332         if (error == ENOENT && (cnflags & MAKEENTRY) && nameiop != CREATE)
333                 cache_enter(dvp, NULLVP, cnp);
334
335         UNIONFS_INTERNAL_DEBUG("unionfs_lookup: leave (%d)\n", error);
336
337         return (error);
338 }
339
340 static int
341 unionfs_create(struct vop_create_args *ap)
342 {
343         struct unionfs_node *dunp;
344         struct componentname *cnp;
345         struct thread  *td;
346         struct vnode   *udvp;
347         struct vnode   *vp;
348         int             error;
349
350         UNIONFS_INTERNAL_DEBUG("unionfs_create: enter\n");
351
352         dunp = VTOUNIONFS(ap->a_dvp);
353         cnp = ap->a_cnp;
354         td = curthread;
355         udvp = dunp->un_uppervp;
356         error = EROFS;
357
358         if (udvp != NULLVP) {
359                 if ((error = VOP_CREATE(udvp, &vp, cnp, ap->a_vap)) == 0) {
360                         VOP_UNLOCK(vp, 0, td);
361                         error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP,
362                             ap->a_dvp, ap->a_vpp, cnp, td);
363                         vrele(vp);
364                 }
365         }
366
367         UNIONFS_INTERNAL_DEBUG("unionfs_create: leave (%d)\n", error);
368
369         return (error);
370 }
371
372 static int
373 unionfs_whiteout(struct vop_whiteout_args *ap)
374 {
375         struct unionfs_node *dunp;
376         struct componentname *cnp;
377         struct vnode   *udvp;
378         int             error;
379
380         UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: enter\n");
381
382         dunp = VTOUNIONFS(ap->a_dvp);
383         cnp = ap->a_cnp;
384         udvp = dunp->un_uppervp;
385         error = EOPNOTSUPP;
386
387         if (udvp != NULLVP) {
388                 switch (ap->a_flags) {
389                 case CREATE:
390                 case DELETE:
391                 case LOOKUP:
392                         error = VOP_WHITEOUT(udvp, cnp, ap->a_flags);
393                         break;
394                 default:
395                         error = EINVAL;
396                         break;
397                 }
398         }
399
400         UNIONFS_INTERNAL_DEBUG("unionfs_whiteout: leave (%d)\n", error);
401
402         return (error);
403 }
404
405 static int
406 unionfs_mknod(struct vop_mknod_args *ap)
407 {
408         struct unionfs_node *dunp;
409         struct componentname *cnp;
410         struct thread  *td;
411         struct vnode   *udvp;
412         struct vnode   *vp;
413         int             error;
414
415         UNIONFS_INTERNAL_DEBUG("unionfs_mknod: enter\n");
416
417         dunp = VTOUNIONFS(ap->a_dvp);
418         cnp = ap->a_cnp;
419         td = curthread;
420         udvp = dunp->un_uppervp;
421         error = EROFS;
422
423         if (udvp != NULLVP) {
424                 if ((error = VOP_MKNOD(udvp, &vp, cnp, ap->a_vap)) == 0) {
425                         VOP_UNLOCK(vp, 0, td);
426                         error = unionfs_nodeget(ap->a_dvp->v_mount, vp, NULLVP,
427                             ap->a_dvp, ap->a_vpp, cnp, td);
428                         vrele(vp);
429                 }
430         }
431
432         UNIONFS_INTERNAL_DEBUG("unionfs_mknod: leave (%d)\n", error);
433
434         return (error);
435 }
436
437 static int
438 unionfs_open(struct vop_open_args *ap)
439 {
440         int             error;
441         struct unionfs_node *unp;
442         struct unionfs_node_status *unsp;
443         struct vnode   *uvp;
444         struct vnode   *lvp;
445         struct vnode   *targetvp;
446         struct ucred   *cred;
447         struct thread  *td;
448
449         UNIONFS_INTERNAL_DEBUG("unionfs_open: enter\n");
450
451         error = 0;
452         unp = VTOUNIONFS(ap->a_vp);
453         uvp = unp->un_uppervp;
454         lvp = unp->un_lowervp;
455         targetvp = NULLVP;
456         cred = ap->a_cred;
457         td = ap->a_td;
458
459         unionfs_get_node_status(unp, td, &unsp);
460
461         if (unsp->uns_lower_opencnt > 0 || unsp->uns_upper_opencnt > 0) {
462                 /* vnode is already opend. */
463                 if (unsp->uns_upper_opencnt > 0)
464                         targetvp = uvp;
465                 else
466                         targetvp = lvp;
467
468                 if (targetvp == lvp &&
469                     (ap->a_mode & FWRITE) && lvp->v_type == VREG)
470                         targetvp = NULLVP;
471         }
472         if (targetvp == NULLVP) {
473                 if (uvp == NULLVP) {
474                         if ((ap->a_mode & FWRITE) && lvp->v_type == VREG) {
475                                 error = unionfs_copyfile(unp,
476                                     !(ap->a_mode & O_TRUNC), cred, td);
477                                 if (error != 0)
478                                         goto unionfs_open_abort;
479                                 targetvp = uvp = unp->un_uppervp;
480                         } else
481                                 targetvp = lvp;
482                 } else
483                         targetvp = uvp;
484         }
485
486         error = VOP_OPEN(targetvp, ap->a_mode, cred, td, ap->a_fdidx);
487         if (error == 0) {
488                 if (targetvp == uvp) {
489                         if (uvp->v_type == VDIR && lvp != NULLVP &&
490                             unsp->uns_lower_opencnt <= 0) {
491                                 /* open lower for readdir */
492                                 error = VOP_OPEN(lvp, FREAD, cred, td, -1);
493                                 if (error != 0) {
494                                         VOP_CLOSE(uvp, ap->a_mode, cred, td);
495                                         goto unionfs_open_abort;
496                                 }
497                                 unsp->uns_node_flag |= UNS_OPENL_4_READDIR;
498                                 unsp->uns_lower_opencnt++;
499                         }
500                         unsp->uns_upper_opencnt++;
501                 } else {
502                         unsp->uns_lower_opencnt++;
503                         unsp->uns_lower_openmode = ap->a_mode;
504                         unsp->uns_lower_fdidx = ap->a_fdidx;
505                 }
506                 ap->a_vp->v_object = targetvp->v_object;
507         }
508
509 unionfs_open_abort:
510         if (error != 0)
511                 unionfs_tryrem_node_status(unp, td, unsp);
512
513         UNIONFS_INTERNAL_DEBUG("unionfs_open: leave (%d)\n", error);
514
515         return (error);
516 }
517
518 static int
519 unionfs_close(struct vop_close_args *ap)
520 {
521         int             error;
522         int             locked;
523         struct unionfs_node *unp;
524         struct unionfs_node_status *unsp;
525         struct ucred   *cred;
526         struct thread  *td;
527         struct vnode   *ovp;
528
529         UNIONFS_INTERNAL_DEBUG("unionfs_close: enter\n");
530
531         locked = 0;
532         unp = VTOUNIONFS(ap->a_vp);
533         cred = ap->a_cred;
534         td = ap->a_td;
535
536         if (VOP_ISLOCKED(ap->a_vp, td) != LK_EXCLUSIVE) {
537                 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
538                 locked = 1;
539         }
540         unionfs_get_node_status(unp, td, &unsp);
541
542         if (unsp->uns_lower_opencnt <= 0 && unsp->uns_upper_opencnt <= 0) {
543 #ifdef DIAGNOSTIC
544                 printf("unionfs_close: warning: open count is 0\n");
545 #endif
546                 if (unp->un_uppervp != NULLVP)
547                         ovp = unp->un_uppervp;
548                 else
549                         ovp = unp->un_lowervp;
550         } else if (unsp->uns_upper_opencnt > 0)
551                 ovp = unp->un_uppervp;
552         else
553                 ovp = unp->un_lowervp;
554
555         error = VOP_CLOSE(ovp, ap->a_fflag, cred, td);
556
557         if (error != 0)
558                 goto unionfs_close_abort;
559
560         ap->a_vp->v_object = ovp->v_object;
561
562         if (ovp == unp->un_uppervp) {
563                 unsp->uns_upper_opencnt--;
564                 if (unsp->uns_upper_opencnt == 0) {
565                         if (unsp->uns_node_flag & UNS_OPENL_4_READDIR) {
566                                 VOP_CLOSE(unp->un_lowervp, FREAD, cred, td);
567                                 unsp->uns_node_flag &= ~UNS_OPENL_4_READDIR;
568                                 unsp->uns_lower_opencnt--;
569                         }
570                         if (unsp->uns_lower_opencnt > 0)
571                                 ap->a_vp->v_object = unp->un_lowervp->v_object;
572                 }
573         } else
574                 unsp->uns_lower_opencnt--;
575
576 unionfs_close_abort:
577         unionfs_tryrem_node_status(unp, td, unsp);
578
579         if (locked != 0)
580                 VOP_UNLOCK(ap->a_vp, 0, td);
581
582         UNIONFS_INTERNAL_DEBUG("unionfs_close: leave (%d)\n", error);
583
584         return (error);
585 }
586
587 /*
588  * Check the access mode toward shadow file/dir.
589  */
590 static int
591 unionfs_check_corrected_access(u_short mode,
592                              struct vattr *va,
593                              struct ucred *cred)
594 {
595         int             count;
596         uid_t           uid;    /* upper side vnode's uid */
597         gid_t           gid;    /* upper side vnode's gid */
598         u_short         vmode;  /* upper side vnode's mode */
599         gid_t          *gp;
600         u_short         mask;
601
602         mask = 0;
603         uid = va->va_uid;
604         gid = va->va_gid;
605         vmode = va->va_mode;
606
607         /* check owner */
608         if (cred->cr_uid == uid) {
609                 if (mode & VEXEC)
610                         mask |= S_IXUSR;
611                 if (mode & VREAD)
612                         mask |= S_IRUSR;
613                 if (mode & VWRITE)
614                         mask |= S_IWUSR;
615                 return ((vmode & mask) == mask ? 0 : EACCES);
616         }
617
618         /* check group */
619         count = 0;
620         gp = cred->cr_groups;
621         for (; count < cred->cr_ngroups; count++, gp++) {
622                 if (gid == *gp) {
623                         if (mode & VEXEC)
624                                 mask |= S_IXGRP;
625                         if (mode & VREAD)
626                                 mask |= S_IRGRP;
627                         if (mode & VWRITE)
628                                 mask |= S_IWGRP;
629                         return ((vmode & mask) == mask ? 0 : EACCES);
630                 }
631         }
632
633         /* check other */
634         if (mode & VEXEC)
635                 mask |= S_IXOTH;
636         if (mode & VREAD)
637                 mask |= S_IROTH;
638         if (mode & VWRITE)
639                 mask |= S_IWOTH;
640
641         return ((vmode & mask) == mask ? 0 : EACCES);
642 }
643
644 static int
645 unionfs_access(struct vop_access_args *ap)
646 {
647         struct unionfs_mount *ump;
648         struct unionfs_node *unp;
649         struct vnode   *uvp;
650         struct vnode   *lvp;
651         struct thread  *td;
652         struct vattr    va;
653         int             mode;
654         int             error;
655
656         UNIONFS_INTERNAL_DEBUG("unionfs_access: enter\n");
657
658         ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
659         unp = VTOUNIONFS(ap->a_vp);
660         uvp = unp->un_uppervp;
661         lvp = unp->un_lowervp;
662         td = ap->a_td;
663         mode = ap->a_mode;
664         error = EACCES;
665
666         if ((mode & VWRITE) &&
667             (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
668                 switch (ap->a_vp->v_type) {
669                 case VREG:
670                 case VDIR:
671                 case VLNK:
672                         return (EROFS);
673                 default:
674                         break;
675                 }
676         }
677
678         if (uvp != NULLVP) {
679                 error = VOP_ACCESS(uvp, mode, ap->a_cred, td);
680
681                 UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error);
682
683                 return (error);
684         }
685
686         if (lvp != NULLVP) {
687                 if (mode & VWRITE) {
688                         if (ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY) {
689                                 switch (ap->a_vp->v_type) {
690                                 case VREG:
691                                 case VDIR:
692                                 case VLNK:
693                                         return (EROFS);
694                                 default:
695                                         break;
696                                 }
697                         } else if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) {
698                                 /* check shadow file/dir */
699                                 if (ump->um_copymode != UNIONFS_TRANSPARENT) {
700                                         error = unionfs_create_uppervattr(ump,
701                                             lvp, &va, ap->a_cred, td);
702                                         if (error != 0)
703                                                 return (error);
704
705                                         error = unionfs_check_corrected_access(
706                                             mode, &va, ap->a_cred);
707                                         if (error != 0)
708                                                 return (error);
709                                 }
710                         }
711                         mode &= ~VWRITE;
712                         mode |= VREAD; /* will copy to upper */
713                 }
714                 error = VOP_ACCESS(lvp, mode, ap->a_cred, td);
715         }
716
717         UNIONFS_INTERNAL_DEBUG("unionfs_access: leave (%d)\n", error);
718
719         return (error);
720 }
721
722 static int
723 unionfs_getattr(struct vop_getattr_args *ap)
724 {
725         int             error;
726         struct unionfs_node *unp;
727         struct unionfs_mount *ump;
728         struct vnode   *uvp;
729         struct vnode   *lvp;
730         struct thread  *td;
731         struct vattr    va;
732
733         UNIONFS_INTERNAL_DEBUG("unionfs_getattr: enter\n");
734
735         unp = VTOUNIONFS(ap->a_vp);
736         ump = MOUNTTOUNIONFSMOUNT(ap->a_vp->v_mount);
737         uvp = unp->un_uppervp;
738         lvp = unp->un_lowervp;
739         td = ap->a_td;
740
741         if (uvp != NULLVP) {
742                 if ((error = VOP_GETATTR(uvp, ap->a_vap, ap->a_cred, td)) == 0)
743                         ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
744
745                 UNIONFS_INTERNAL_DEBUG("unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n",
746                     ap->a_vap->va_mode, ap->a_vap->va_uid,
747                     ap->a_vap->va_gid, error);
748
749                 return (error);
750         }
751
752         error = VOP_GETATTR(lvp, ap->a_vap, ap->a_cred, td);
753
754         if (error == 0 && !(ump->um_uppervp->v_mount->mnt_flag & MNT_RDONLY)) {
755                 /* correct the attr toward shadow file/dir. */
756                 if (ap->a_vp->v_type == VREG || ap->a_vp->v_type == VDIR) {
757                         unionfs_create_uppervattr_core(ump, ap->a_vap, &va, td);
758                         ap->a_vap->va_mode = va.va_mode;
759                         ap->a_vap->va_uid = va.va_uid;
760                         ap->a_vap->va_gid = va.va_gid;
761                 }
762         }
763
764         if (error == 0)
765                 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
766
767         UNIONFS_INTERNAL_DEBUG("unionfs_getattr: leave mode=%o, uid=%d, gid=%d (%d)\n",
768             ap->a_vap->va_mode, ap->a_vap->va_uid, ap->a_vap->va_gid, error);
769
770         return (error);
771 }
772
773 static int
774 unionfs_setattr(struct vop_setattr_args *ap)
775 {
776         int             error;
777         struct unionfs_node *unp;
778         struct vnode   *uvp;
779         struct vnode   *lvp;
780         struct thread  *td;
781         struct vattr   *vap;
782
783         UNIONFS_INTERNAL_DEBUG("unionfs_setattr: enter\n");
784
785         error = EROFS;
786         unp = VTOUNIONFS(ap->a_vp);
787         uvp = unp->un_uppervp;
788         lvp = unp->un_lowervp;
789         td = ap->a_td;
790         vap = ap->a_vap;
791
792         if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
793             (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
794              vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
795              vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL))
796                 return (EROFS);
797
798         if (uvp == NULLVP && lvp->v_type == VREG) {
799                 error = unionfs_copyfile(unp, (vap->va_size != 0),
800                     ap->a_cred, td);
801                 if (error != 0)
802                         return (error);
803                 uvp = unp->un_uppervp;
804         }
805
806         if (uvp != NULLVP)
807                 error = VOP_SETATTR(uvp, vap, ap->a_cred, td);
808
809         UNIONFS_INTERNAL_DEBUG("unionfs_setattr: leave (%d)\n", error);
810
811         return (error);
812 }
813
814 static int
815 unionfs_read(struct vop_read_args *ap)
816 {
817         int             error;
818         struct unionfs_node *unp;
819         struct vnode   *tvp;
820
821         /* UNIONFS_INTERNAL_DEBUG("unionfs_read: enter\n"); */
822
823         unp = VTOUNIONFS(ap->a_vp);
824         tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
825
826         error = VOP_READ(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
827
828         /* UNIONFS_INTERNAL_DEBUG("unionfs_read: leave (%d)\n", error); */
829
830         return (error);
831 }
832
833 static int
834 unionfs_write(struct vop_write_args *ap)
835 {
836         int             error;
837         struct unionfs_node *unp;
838         struct vnode   *tvp;
839
840         /* UNIONFS_INTERNAL_DEBUG("unionfs_write: enter\n"); */
841
842         unp = VTOUNIONFS(ap->a_vp);
843         tvp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
844
845         error = VOP_WRITE(tvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
846
847         /* UNIONFS_INTERNAL_DEBUG("unionfs_write: leave (%d)\n", error); */
848
849         return (error);
850 }
851
852 static int
853 unionfs_lease(struct vop_lease_args *ap)
854 {
855         int error;
856         struct unionfs_node *unp;
857         struct vnode   *vp;
858
859         UNIONFS_INTERNAL_DEBUG("unionfs_lease: enter\n");
860
861         unp = VTOUNIONFS(ap->a_vp);
862         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
863
864         error = VOP_LEASE(vp, ap->a_td, ap->a_cred, ap->a_flag);
865
866         UNIONFS_INTERNAL_DEBUG("unionfs_lease: lease (%d)\n", error);
867
868         return (error);
869 }
870
871 static int
872 unionfs_ioctl(struct vop_ioctl_args *ap)
873 {
874         int error;
875         struct unionfs_node *unp;
876         struct unionfs_node_status *unsp;
877         struct vnode   *ovp;
878
879         UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: enter\n");
880
881         vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
882         unp = VTOUNIONFS(ap->a_vp);
883         unionfs_get_node_status(unp, ap->a_td, &unsp);
884         ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
885         unionfs_tryrem_node_status(unp, ap->a_td, unsp);
886         VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
887
888         if (ovp == NULLVP)
889                 return (EBADF);
890
891         error = VOP_IOCTL(ovp, ap->a_command, ap->a_data, ap->a_fflag,
892             ap->a_cred, ap->a_td);
893
894         UNIONFS_INTERNAL_DEBUG("unionfs_ioctl: lease (%d)\n", error);
895
896         return (error);
897 }
898
899 static int
900 unionfs_poll(struct vop_poll_args *ap)
901 {
902         struct unionfs_node *unp;
903         struct unionfs_node_status *unsp;
904         struct vnode   *ovp;
905
906         vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, ap->a_td);
907         unp = VTOUNIONFS(ap->a_vp);
908         unionfs_get_node_status(unp, ap->a_td, &unsp);
909         ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
910         unionfs_tryrem_node_status(unp, ap->a_td, unsp);
911         VOP_UNLOCK(ap->a_vp, 0, ap->a_td);
912
913         if (ovp == NULLVP)
914                 return (EBADF);
915
916         return (VOP_POLL(ovp, ap->a_events, ap->a_cred, ap->a_td));
917 }
918
919 static int
920 unionfs_fsync(struct vop_fsync_args *ap)
921 {
922         struct unionfs_node *unp;
923         struct unionfs_node_status *unsp;
924         struct vnode   *ovp;
925
926         unp = VTOUNIONFS(ap->a_vp);
927         unionfs_get_node_status(unp, ap->a_td, &unsp);
928         ovp = (unsp->uns_upper_opencnt ? unp->un_uppervp : unp->un_lowervp);
929         unionfs_tryrem_node_status(unp, ap->a_td, unsp);
930
931         if (ovp == NULLVP)
932                 return (EBADF);
933
934         return (VOP_FSYNC(ovp, ap->a_waitfor, ap->a_td));
935 }
936
937 static int
938 unionfs_remove(struct vop_remove_args *ap)
939 {
940         int             error;
941         struct unionfs_node *dunp;
942         struct unionfs_node *unp;
943         struct vnode   *udvp;
944         struct vnode   *uvp;
945         struct vnode   *lvp;
946         struct componentname *cnp;
947         struct thread  *td;
948
949         UNIONFS_INTERNAL_DEBUG("unionfs_remove: enter\n");
950
951         error = 0;
952         dunp = VTOUNIONFS(ap->a_dvp);
953         unp = VTOUNIONFS(ap->a_vp);
954         udvp = dunp->un_uppervp;
955         uvp = unp->un_uppervp;
956         lvp = unp->un_lowervp;
957         cnp = ap->a_cnp;
958         td = curthread;
959
960         if (udvp == NULLVP)
961                 return (EROFS);
962
963         if (uvp != NULLVP) {
964                 cnp->cn_flags |= DOWHITEOUT;
965                 error = VOP_REMOVE(udvp, uvp, cnp);
966         } else if (lvp != NULLVP)
967                 error = unionfs_mkwhiteout(udvp, cnp, td, unp->un_path);
968
969         UNIONFS_INTERNAL_DEBUG("unionfs_remove: leave (%d)\n", error);
970
971         return (error);
972 }
973
974 static int
975 unionfs_link(struct vop_link_args *ap)
976 {
977         int             error;
978         int             needrelookup;
979         struct unionfs_node *dunp;
980         struct unionfs_node *unp;
981         struct vnode   *udvp;
982         struct vnode   *uvp;
983         struct componentname *cnp;
984         struct thread  *td;
985
986         UNIONFS_INTERNAL_DEBUG("unionfs_link: enter\n");
987
988         error = 0;
989         needrelookup = 0;
990         dunp = VTOUNIONFS(ap->a_tdvp);
991         unp = NULL;
992         udvp = dunp->un_uppervp;
993         uvp = NULLVP;
994         cnp = ap->a_cnp;
995         td = curthread;
996
997         if (udvp == NULLVP)
998                 return (EROFS);
999
1000         if (ap->a_vp->v_op != &unionfs_vnodeops)
1001                 uvp = ap->a_vp;
1002         else {
1003                 unp = VTOUNIONFS(ap->a_vp);
1004
1005                 if (unp->un_uppervp == NULLVP) {
1006                         if (ap->a_vp->v_type != VREG)
1007                                 return (EOPNOTSUPP);
1008
1009                         error = unionfs_copyfile(unp, 1, cnp->cn_cred, td);
1010                         if (error != 0)
1011                                 return (error);
1012                         needrelookup = 1;
1013                 }
1014                 uvp = unp->un_uppervp;
1015         }
1016
1017         if (needrelookup != 0)
1018                 error = unionfs_relookup_for_create(ap->a_tdvp, cnp, td);
1019
1020         if (error == 0)
1021                 error = VOP_LINK(udvp, uvp, cnp);
1022
1023         UNIONFS_INTERNAL_DEBUG("unionfs_link: leave (%d)\n", error);
1024
1025         return (error);
1026 }
1027
1028 static int
1029 unionfs_rename(struct vop_rename_args *ap)
1030 {
1031         int             error;
1032         struct vnode   *fdvp;
1033         struct vnode   *fvp;
1034         struct componentname *fcnp;
1035         struct vnode   *tdvp;
1036         struct vnode   *tvp;
1037         struct componentname *tcnp;
1038         struct vnode   *ltdvp;
1039         struct vnode   *ltvp;
1040         struct thread  *td;
1041
1042         /* rename target vnodes */
1043         struct vnode   *rfdvp;
1044         struct vnode   *rfvp;
1045         struct vnode   *rtdvp;
1046         struct vnode   *rtvp;
1047
1048         int             needrelookup;
1049         struct unionfs_mount *ump;
1050         struct unionfs_node *unp;
1051
1052         UNIONFS_INTERNAL_DEBUG("unionfs_rename: enter\n");
1053
1054         error = 0;
1055         fdvp = ap->a_fdvp;
1056         fvp = ap->a_fvp;
1057         fcnp = ap->a_fcnp;
1058         tdvp = ap->a_tdvp;
1059         tvp = ap->a_tvp;
1060         tcnp = ap->a_tcnp;
1061         ltdvp = NULLVP;
1062         ltvp = NULLVP;
1063         td = curthread;
1064         rfdvp = fdvp;
1065         rfvp = fvp;
1066         rtdvp = tdvp;
1067         rtvp = tvp;
1068         needrelookup = 0;
1069
1070 #ifdef DIAGNOSTIC
1071         if (!(fcnp->cn_flags & HASBUF) || !(tcnp->cn_flags & HASBUF))
1072                 panic("unionfs_rename: no name");
1073 #endif
1074
1075         /* check for cross device rename */
1076         if (fvp->v_mount != tdvp->v_mount ||
1077             (tvp != NULLVP && fvp->v_mount != tvp->v_mount)) {
1078                 error = EXDEV;
1079                 goto unionfs_rename_abort;
1080         }
1081
1082         /* Renaming a file to itself has no effect. */
1083         if (fvp == tvp)
1084                 goto unionfs_rename_abort;
1085
1086         /*
1087          * from/to vnode is unionfs node.
1088          */
1089
1090         unp = VTOUNIONFS(fdvp);
1091 #ifdef UNIONFS_IDBG_RENAME
1092         UNIONFS_INTERNAL_DEBUG("fdvp=%p, ufdvp=%p, lfdvp=%p\n", fdvp, unp->un_uppervp, unp->un_lowervp);
1093 #endif
1094         if (unp->un_uppervp == NULLVP) {
1095                 error = ENODEV;
1096                 goto unionfs_rename_abort;
1097         }
1098         rfdvp = unp->un_uppervp;
1099         vref(rfdvp);
1100
1101         unp = VTOUNIONFS(fvp);
1102 #ifdef UNIONFS_IDBG_RENAME
1103         UNIONFS_INTERNAL_DEBUG("fvp=%p, ufvp=%p, lfvp=%p\n", fvp, unp->un_uppervp, unp->un_lowervp);
1104 #endif
1105         ump = MOUNTTOUNIONFSMOUNT(fvp->v_mount);
1106         if (unp->un_uppervp == NULLVP) {
1107                 switch (fvp->v_type) {
1108                 case VREG:
1109                         if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
1110                                 goto unionfs_rename_abort;
1111                         error = unionfs_copyfile(unp, 1, fcnp->cn_cred, td);
1112                         VOP_UNLOCK(fvp, 0, td);
1113                         if (error != 0)
1114                                 goto unionfs_rename_abort;
1115                         break;
1116                 case VDIR:
1117                         if ((error = vn_lock(fvp, LK_EXCLUSIVE, td)) != 0)
1118                                 goto unionfs_rename_abort;
1119                         error = unionfs_mkshadowdir(ump, rfdvp, unp, fcnp, td);
1120                         VOP_UNLOCK(fvp, 0, td);
1121                         if (error != 0)
1122                                 goto unionfs_rename_abort;
1123                         break;
1124                 default:
1125                         error = ENODEV;
1126                         goto unionfs_rename_abort;
1127                 }
1128
1129                 needrelookup = 1;
1130         }
1131
1132         if (unp->un_lowervp != NULLVP)
1133                 fcnp->cn_flags |= DOWHITEOUT;
1134         rfvp = unp->un_uppervp;
1135         vref(rfvp);
1136
1137         unp = VTOUNIONFS(tdvp);
1138 #ifdef UNIONFS_IDBG_RENAME
1139         UNIONFS_INTERNAL_DEBUG("tdvp=%p, utdvp=%p, ltdvp=%p\n", tdvp, unp->un_uppervp, unp->un_lowervp);
1140 #endif
1141         if (unp->un_uppervp == NULLVP) {
1142                 error = ENODEV;
1143                 goto unionfs_rename_abort;
1144         }
1145         rtdvp = unp->un_uppervp;
1146         ltdvp = unp->un_lowervp;
1147         vref(rtdvp);
1148
1149         if (tdvp == tvp) {
1150                 rtvp = rtdvp;
1151                 vref(rtvp);
1152         } else if (tvp != NULLVP) {
1153                 unp = VTOUNIONFS(tvp);
1154 #ifdef UNIONFS_IDBG_RENAME
1155                 UNIONFS_INTERNAL_DEBUG("tvp=%p, utvp=%p, ltvp=%p\n", tvp, unp->un_uppervp, unp->un_lowervp);
1156 #endif
1157                 if (unp->un_uppervp == NULLVP)
1158                         rtvp = NULLVP;
1159                 else {
1160                         if (tvp->v_type == VDIR) {
1161                                 error = EINVAL;
1162                                 goto unionfs_rename_abort;
1163                         }
1164                         rtvp = unp->un_uppervp;
1165                         ltvp = unp->un_lowervp;
1166                         vref(rtvp);
1167                 }
1168         }
1169
1170         if (needrelookup != 0) {
1171                 if ((error = vn_lock(fdvp, LK_EXCLUSIVE, td)) != 0)
1172                         goto unionfs_rename_abort;
1173                 error = unionfs_relookup_for_delete(fdvp, fcnp, td);
1174                 VOP_UNLOCK(fdvp, 0, td);
1175                 if (error != 0)
1176                         goto unionfs_rename_abort;
1177
1178                 /* Locke of tvp is canceled in order to avoid recursive lock. */
1179                 if (tvp != NULLVP && tvp != tdvp)
1180                         VOP_UNLOCK(tvp, 0, td);
1181                 error = unionfs_relookup_for_rename(tdvp, tcnp, td);
1182                 if (tvp != NULLVP && tvp != tdvp)
1183                         vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, td);
1184                 if (error != 0)
1185                         goto unionfs_rename_abort;
1186         }
1187
1188         error = VOP_RENAME(rfdvp, rfvp, fcnp, rtdvp, rtvp, tcnp);
1189
1190         if (error == 0) {
1191                 if (rtvp != NULLVP && rtvp->v_type == VDIR)
1192                         cache_purge(tdvp);
1193                 if (fvp->v_type == VDIR && fdvp != tdvp)
1194                         cache_purge(fdvp);
1195         }
1196
1197         if (fdvp != rfdvp)
1198                 vrele(fdvp);
1199         if (fvp != rfvp)
1200                 vrele(fvp);
1201         if (tdvp != rtdvp)
1202                 vrele(tdvp);
1203         if (tvp != rtvp && tvp != NULLVP) {
1204                 if (rtvp == NULLVP)
1205                         vput(tvp);
1206                 else
1207                         vrele(tvp);
1208         }
1209         if (ltdvp != NULLVP)
1210                 VOP_UNLOCK(ltdvp, 0, td);
1211         if (ltvp != NULLVP)
1212                 VOP_UNLOCK(ltvp, 0, td);
1213
1214         UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error);
1215
1216         return (error);
1217
1218 unionfs_rename_abort:
1219         if (fdvp != rfdvp)
1220                 vrele(rfdvp);
1221         if (fvp != rfvp)
1222                 vrele(rfvp);
1223         if (tdvp != rtdvp)
1224                 vrele(rtdvp);
1225         vput(tdvp);
1226         if (tvp != rtvp && rtvp != NULLVP)
1227                 vrele(rtvp);
1228         if (tvp != NULLVP) {
1229                 if (tdvp != tvp)
1230                         vput(tvp);
1231                 else
1232                         vrele(tvp);
1233         }
1234         vrele(fdvp);
1235         vrele(fvp);
1236
1237         UNIONFS_INTERNAL_DEBUG("unionfs_rename: leave (%d)\n", error);
1238
1239         return (error);
1240 }
1241
1242 static int
1243 unionfs_mkdir(struct vop_mkdir_args *ap)
1244 {
1245         int             error;
1246         int             lkflags;
1247         struct unionfs_node *dunp;
1248         struct componentname *cnp;
1249         struct thread  *td;
1250         struct vnode   *udvp;
1251         struct vnode   *uvp;
1252         struct vattr    va;
1253
1254         UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: enter\n");
1255
1256         error = EROFS;
1257         dunp = VTOUNIONFS(ap->a_dvp);
1258         cnp = ap->a_cnp;
1259         lkflags = cnp->cn_lkflags;
1260         td = curthread;
1261         udvp = dunp->un_uppervp;
1262
1263         if (udvp != NULLVP) {
1264                 /* check opaque */
1265                 if (!(cnp->cn_flags & ISWHITEOUT)) {
1266                         error = VOP_GETATTR(udvp, &va, cnp->cn_cred, td);
1267                         if (error != 0)
1268                                 return (error);
1269                         if (va.va_flags & OPAQUE) 
1270                                 cnp->cn_flags |= ISWHITEOUT;
1271                 }
1272
1273                 if ((error = VOP_MKDIR(udvp, &uvp, cnp, ap->a_vap)) == 0) {
1274                         VOP_UNLOCK(uvp, 0, td);
1275                         cnp->cn_lkflags = LK_EXCLUSIVE;
1276                         error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP,
1277                             ap->a_dvp, ap->a_vpp, cnp, td);
1278                         cnp->cn_lkflags = lkflags;
1279                         vrele(uvp);
1280                 }
1281         }
1282
1283         UNIONFS_INTERNAL_DEBUG("unionfs_mkdir: leave (%d)\n", error);
1284
1285         return (error);
1286 }
1287
1288 static int
1289 unionfs_rmdir(struct vop_rmdir_args *ap)
1290 {
1291         int             error;
1292         struct unionfs_node *dunp;
1293         struct unionfs_node *unp;
1294         struct componentname *cnp;
1295         struct thread  *td;
1296         struct vnode   *udvp;
1297         struct vnode   *uvp;
1298         struct vnode   *lvp;
1299
1300         UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: enter\n");
1301
1302         error = 0;
1303         dunp = VTOUNIONFS(ap->a_dvp);
1304         unp = VTOUNIONFS(ap->a_vp);
1305         cnp = ap->a_cnp;
1306         td = curthread;
1307         udvp = dunp->un_uppervp;
1308         uvp = unp->un_uppervp;
1309         lvp = unp->un_lowervp;
1310
1311         if (udvp == NULLVP)
1312                 return (EROFS);
1313
1314         if (udvp == uvp)
1315                 return (EOPNOTSUPP);
1316
1317         if (uvp != NULLVP) {
1318                 if (lvp != NULLVP) {
1319                         error = unionfs_check_rmdir(ap->a_vp, cnp->cn_cred, td);
1320                         if (error != 0)
1321                                 return (error);
1322                 }
1323                 cnp->cn_flags |= DOWHITEOUT;
1324                 error = VOP_RMDIR(udvp, uvp, cnp);
1325         }
1326         else if (lvp != NULLVP)
1327                 error = unionfs_mkwhiteout(udvp, cnp, td, unp->un_path);
1328
1329         if (error == 0) {
1330                 cache_purge(ap->a_dvp);
1331                 cache_purge(ap->a_vp);
1332         }
1333
1334         UNIONFS_INTERNAL_DEBUG("unionfs_rmdir: leave (%d)\n", error);
1335
1336         return (error);
1337 }
1338
1339 static int
1340 unionfs_symlink(struct vop_symlink_args *ap)
1341 {
1342         int             error;
1343         int             lkflags;
1344         struct unionfs_node *dunp;
1345         struct componentname *cnp;
1346         struct thread  *td;
1347         struct vnode   *udvp;
1348         struct vnode   *uvp;
1349
1350         UNIONFS_INTERNAL_DEBUG("unionfs_symlink: enter\n");
1351
1352         error = EROFS;
1353         dunp = VTOUNIONFS(ap->a_dvp);
1354         cnp = ap->a_cnp;
1355         lkflags = cnp->cn_lkflags;
1356         td = curthread;
1357         udvp = dunp->un_uppervp;
1358
1359         if (udvp != NULLVP) {
1360                 error = VOP_SYMLINK(udvp, &uvp, cnp, ap->a_vap, ap->a_target);
1361                 if (error == 0) {
1362                         VOP_UNLOCK(uvp, 0, td);
1363                         cnp->cn_lkflags = LK_EXCLUSIVE;
1364                         error = unionfs_nodeget(ap->a_dvp->v_mount, uvp, NULLVP,
1365                             ap->a_dvp, ap->a_vpp, cnp, td);
1366                         cnp->cn_lkflags = lkflags;
1367                         vrele(uvp);
1368                 }
1369         }
1370
1371         UNIONFS_INTERNAL_DEBUG("unionfs_symlink: leave (%d)\n", error);
1372
1373         return (error);
1374 }
1375
1376 static int
1377 unionfs_readdir(struct vop_readdir_args *ap)
1378 {
1379         int             error;
1380         int             eofflag;
1381         int             locked;
1382         struct unionfs_node *unp;
1383         struct unionfs_node_status *unsp;
1384         struct uio     *uio;
1385         struct vnode   *uvp;
1386         struct vnode   *lvp;
1387         struct thread  *td;
1388         struct vattr    va;
1389
1390         int             ncookies_bk;
1391         u_long         *cookies_bk;
1392
1393         UNIONFS_INTERNAL_DEBUG("unionfs_readdir: enter\n");
1394
1395         error = 0;
1396         eofflag = 0;
1397         locked = 0;
1398         unp = VTOUNIONFS(ap->a_vp);
1399         uio = ap->a_uio;
1400         uvp = unp->un_uppervp;
1401         lvp = unp->un_lowervp;
1402         td = uio->uio_td;
1403         ncookies_bk = 0;
1404         cookies_bk = NULL;
1405
1406         if (ap->a_vp->v_type != VDIR)
1407                 return (ENOTDIR);
1408
1409         /* check opaque */
1410         if (uvp != NULLVP && lvp != NULLVP) {
1411                 if ((error = VOP_GETATTR(uvp, &va, ap->a_cred, td)) != 0)
1412                         goto unionfs_readdir_exit;
1413                 if (va.va_flags & OPAQUE)
1414                         lvp = NULLVP;
1415         }
1416
1417         /* check the open count. unionfs needs to open before readdir. */
1418         if (VOP_ISLOCKED(ap->a_vp, td) != LK_EXCLUSIVE) {
1419                 vn_lock(ap->a_vp, LK_UPGRADE | LK_RETRY, td);
1420                 locked = 1;
1421         }
1422         unionfs_get_node_status(unp, td, &unsp);
1423         if ((uvp != NULLVP && unsp->uns_upper_opencnt <= 0) ||
1424             (lvp != NULLVP && unsp->uns_lower_opencnt <= 0)) {
1425                 unionfs_tryrem_node_status(unp, td, unsp);
1426                 error = EBADF;
1427         }
1428         if (locked == 1)
1429                 vn_lock(ap->a_vp, LK_DOWNGRADE | LK_RETRY, td);
1430         if (error != 0)
1431                 goto unionfs_readdir_exit;
1432
1433         /* upper only */
1434         if (uvp != NULLVP && lvp == NULLVP) {
1435                 error = VOP_READDIR(uvp, uio, ap->a_cred, ap->a_eofflag,
1436                     ap->a_ncookies, ap->a_cookies);
1437                 unsp->uns_readdir_status = 0;
1438
1439                 goto unionfs_readdir_exit;
1440         }
1441
1442         /* lower only */
1443         if (uvp == NULLVP && lvp != NULLVP) {
1444                 error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag,
1445                     ap->a_ncookies, ap->a_cookies);
1446                 unsp->uns_readdir_status = 2;
1447
1448                 goto unionfs_readdir_exit;
1449         }
1450
1451         /*
1452          * readdir upper and lower
1453          */
1454         if (uio->uio_offset == 0)
1455                 unsp->uns_readdir_status = 0;
1456
1457         if (unsp->uns_readdir_status == 0) {
1458                 /* read upper */
1459                 error = VOP_READDIR(uvp, uio, ap->a_cred, &eofflag,
1460                                     ap->a_ncookies, ap->a_cookies);
1461
1462                 if (error != 0 || eofflag == 0)
1463                         goto unionfs_readdir_exit;
1464                 unsp->uns_readdir_status = 1;
1465
1466                 /*
1467                  * ufs(and other fs) needs size of uio_resid larger than
1468                  * DIRBLKSIZ.
1469                  * size of DIRBLKSIZ equals DEV_BSIZE.
1470                  * (see: ufs/ufs/ufs_vnops.c ufs_readdir func , ufs/ufs/dir.h)
1471                  */
1472                 if (uio->uio_resid <= (uio->uio_resid & (DEV_BSIZE -1)))
1473                         goto unionfs_readdir_exit;
1474
1475                 /*
1476                  * backup cookies
1477                  * It prepares to readdir in lower.
1478                  */
1479                 if (ap->a_ncookies != NULL) {
1480                         ncookies_bk = *(ap->a_ncookies);
1481                         *(ap->a_ncookies) = 0;
1482                 }
1483                 if (ap->a_cookies != NULL) {
1484                         cookies_bk = *(ap->a_cookies);
1485                         *(ap->a_cookies) = NULL;
1486                 }
1487         }
1488
1489         /* initialize for readdir in lower */
1490         if (unsp->uns_readdir_status == 1) {
1491                 unsp->uns_readdir_status = 2;
1492                 uio->uio_offset = 0;
1493         }
1494
1495         if (lvp == NULLVP) {
1496                 error = EBADF;
1497                 goto unionfs_readdir_exit;
1498         }
1499         /* read lower */
1500         error = VOP_READDIR(lvp, uio, ap->a_cred, ap->a_eofflag,
1501                             ap->a_ncookies, ap->a_cookies);
1502
1503         if (cookies_bk != NULL) {
1504                 /* merge cookies */
1505                 int             size;
1506                 u_long         *newcookies, *pos;
1507
1508                 size = *(ap->a_ncookies) + ncookies_bk;
1509                 newcookies = (u_long *) malloc(size * sizeof(u_long),
1510                     M_TEMP, M_WAITOK);
1511                 pos = newcookies;
1512
1513                 memcpy(pos, cookies_bk, ncookies_bk * sizeof(u_long));
1514                 pos += ncookies_bk * sizeof(u_long);
1515                 memcpy(pos, *(ap->a_cookies), *(ap->a_ncookies) * sizeof(u_long));
1516                 free(cookies_bk, M_TEMP);
1517                 free(*(ap->a_cookies), M_TEMP);
1518                 *(ap->a_ncookies) = size;
1519                 *(ap->a_cookies) = newcookies;
1520         }
1521
1522 unionfs_readdir_exit:
1523         if (error != 0 && ap->a_eofflag != NULL)
1524                 *(ap->a_eofflag) = 1;
1525
1526         UNIONFS_INTERNAL_DEBUG("unionfs_readdir: leave (%d)\n", error);
1527
1528         return (error);
1529 }
1530
1531 static int
1532 unionfs_readlink(struct vop_readlink_args *ap)
1533 {
1534         int error;
1535         struct unionfs_node *unp;
1536         struct vnode   *vp;
1537
1538         UNIONFS_INTERNAL_DEBUG("unionfs_readlink: enter\n");
1539
1540         unp = VTOUNIONFS(ap->a_vp);
1541         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1542
1543         error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
1544
1545         UNIONFS_INTERNAL_DEBUG("unionfs_readlink: leave (%d)\n", error);
1546
1547         return (error);
1548 }
1549
1550 static int
1551 unionfs_getwritemount(struct vop_getwritemount_args *ap)
1552 {
1553         int             error;
1554         struct vnode   *uvp;
1555         struct vnode   *vp;
1556
1557         UNIONFS_INTERNAL_DEBUG("unionfs_getwritemount: enter\n");
1558
1559         error = 0;
1560         vp = ap->a_vp;
1561
1562         if (vp == NULLVP || (vp->v_mount->mnt_flag & MNT_RDONLY))
1563                 return (EACCES);
1564
1565         uvp = UNIONFSVPTOUPPERVP(vp);
1566         if (uvp == NULLVP && VREG == vp->v_type)
1567                 uvp = UNIONFSVPTOUPPERVP(VTOUNIONFS(vp)->un_dvp);
1568
1569         if (uvp != NULLVP)
1570                 error = VOP_GETWRITEMOUNT(uvp, ap->a_mpp);
1571         else {
1572                 VI_LOCK(vp);
1573                 if (vp->v_iflag & VI_FREE)
1574                         error = EOPNOTSUPP;
1575                 else
1576                         error = EACCES;
1577                 VI_UNLOCK(vp);
1578         }
1579
1580         UNIONFS_INTERNAL_DEBUG("unionfs_getwritemount: leave (%d)\n", error);
1581
1582         return (error);
1583 }
1584
1585 static int
1586 unionfs_inactive(struct vop_inactive_args *ap)
1587 {
1588         ap->a_vp->v_object = NULL;
1589         vrecycle(ap->a_vp, ap->a_td);
1590         return (0);
1591 }
1592
1593 static int
1594 unionfs_reclaim(struct vop_reclaim_args *ap)
1595 {
1596         /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: enter\n"); */
1597
1598         unionfs_noderem(ap->a_vp, ap->a_td);
1599
1600         /* UNIONFS_INTERNAL_DEBUG("unionfs_reclaim: leave\n"); */
1601
1602         return (0);
1603 }
1604
1605 static int
1606 unionfs_print(struct vop_print_args *ap)
1607 {
1608         struct unionfs_node *unp;
1609         /* struct unionfs_node_status *unsp; */
1610
1611         unp = VTOUNIONFS(ap->a_vp);
1612         /* unionfs_get_node_status(unp, curthread, &unsp); */
1613
1614         printf("unionfs_vp=%p, uppervp=%p, lowervp=%p\n",
1615             ap->a_vp, unp->un_uppervp, unp->un_lowervp);
1616         /*
1617         printf("unionfs opencnt: uppervp=%d, lowervp=%d\n",
1618             unsp->uns_upper_opencnt, unsp->uns_lower_opencnt);
1619         */
1620
1621         if (unp->un_uppervp != NULLVP)
1622                 vprint("unionfs: upper", unp->un_uppervp);
1623         if (unp->un_lowervp != NULLVP)
1624                 vprint("unionfs: lower", unp->un_lowervp);
1625
1626         return (0);
1627 }
1628
1629 static int
1630 unionfs_get_llt_revlock(int flags)
1631 {
1632         int count;
1633
1634         flags &= LK_TYPE_MASK;
1635         for (count = 0; un_llt[count].lock != 0; count++) {
1636                 if (flags == un_llt[count].lock) {
1637                         return un_llt[count].revlock;
1638                 }
1639         }
1640
1641         return 0;
1642 }
1643
1644 static int
1645 unionfs_lock(struct vop_lock_args *ap)
1646 {
1647         int             error;
1648         int             flags;
1649         int             revlock;
1650         int             uhold;
1651         struct unionfs_mount *ump;
1652         struct unionfs_node *unp;
1653         struct vnode   *vp;
1654         struct vnode   *uvp;
1655         struct vnode   *lvp;
1656         struct thread  *td;
1657
1658         error = 0;
1659         uhold = 0;
1660         flags = ap->a_flags;
1661         vp = ap->a_vp;
1662         td = ap->a_td;
1663
1664         if (LK_RELEASE == (flags & LK_TYPE_MASK) || !(flags & LK_TYPE_MASK))
1665                 return (VOP_UNLOCK(vp, flags, td));
1666
1667         if ((revlock = unionfs_get_llt_revlock(flags)) == 0)
1668                 panic("unknown lock type: 0x%x", flags & LK_TYPE_MASK);
1669
1670         if (!(flags & LK_INTERLOCK))
1671                 VI_LOCK(vp);
1672
1673         ump = MOUNTTOUNIONFSMOUNT(vp->v_mount);
1674         unp = VTOUNIONFS(vp);
1675         if (NULL == unp)
1676                 goto unionfs_lock_null_vnode;
1677         lvp = unp->un_lowervp;
1678         uvp = unp->un_uppervp;
1679
1680         /*
1681          * Sometimes, lower or upper is already exclusive locked.
1682          * (ex. vfs_domount: mounted vnode is already locked.)
1683          */
1684         if ((flags & LK_TYPE_MASK) == LK_EXCLUSIVE &&
1685             vp == ump->um_rootvp)
1686                 flags |= LK_CANRECURSE;
1687
1688         if (lvp != NULLVP) {
1689                 VI_LOCK_FLAGS(lvp, MTX_DUPOK);
1690                 flags |= LK_INTERLOCK;
1691                 vholdl(lvp);
1692
1693                 VI_UNLOCK(vp);
1694                 ap->a_flags &= ~LK_INTERLOCK;
1695
1696                 error = VOP_LOCK(lvp, flags, td);
1697
1698                 VI_LOCK(vp);
1699                 unp = VTOUNIONFS(vp);
1700                 if (unp == NULL) {
1701                         if (error == 0) 
1702                                 VOP_UNLOCK(lvp, 0, td);
1703                         VI_UNLOCK(vp);
1704                         vdrop(lvp);
1705                         return (vop_stdlock(ap));
1706                 }
1707         }
1708
1709         if (error == 0 && uvp != NULLVP) {
1710                 VI_LOCK_FLAGS(uvp, MTX_DUPOK);
1711                 flags |= LK_INTERLOCK;
1712                 vholdl(uvp);
1713                 uhold = 1;
1714
1715                 VI_UNLOCK(vp);
1716                 ap->a_flags &= ~LK_INTERLOCK;
1717
1718                 error = VOP_LOCK(uvp, flags, td);
1719
1720                 VI_LOCK(vp);
1721                 unp = VTOUNIONFS(vp);
1722                 if (unp == NULL) {
1723                         if (error == 0) {
1724                                 VOP_UNLOCK(uvp, 0, td);
1725                                 if (lvp != NULLVP)
1726                                         VOP_UNLOCK(lvp, 0, td);
1727                         }
1728                         VI_UNLOCK(vp);
1729                         if (lvp != NULLVP)
1730                                 vdrop(lvp);
1731                         vdrop(uvp);
1732                         return (vop_stdlock(ap));
1733                 }
1734
1735                 if (error != 0 && lvp != NULLVP)
1736                         vn_lock(lvp, revlock | LK_RETRY, td);
1737         }
1738
1739         VI_UNLOCK(vp);
1740         if (lvp != NULLVP)
1741                 vdrop(lvp);
1742         if (uhold != 0)
1743                 vdrop(uvp);
1744
1745         return (error);
1746
1747 unionfs_lock_null_vnode:
1748         ap->a_flags |= LK_INTERLOCK;
1749         return (vop_stdlock(ap));
1750 }
1751
1752 static int
1753 unionfs_unlock(struct vop_unlock_args *ap)
1754 {
1755         int             error;
1756         int             flags;
1757         int             mtxlkflag;
1758         int             uhold;
1759         struct vnode   *vp;
1760         struct vnode   *lvp;
1761         struct vnode   *uvp;
1762         struct unionfs_node *unp;
1763
1764         error = 0;
1765         mtxlkflag = 0;
1766         uhold = 0;
1767         flags = ap->a_flags | LK_RELEASE;
1768         vp = ap->a_vp;
1769
1770         if (flags & LK_INTERLOCK)
1771                 mtxlkflag = 1;
1772         else if (mtx_owned(VI_MTX(vp)) == 0) {
1773                 VI_LOCK(vp);
1774                 mtxlkflag = 2;
1775         }
1776
1777         unp = VTOUNIONFS(vp);
1778         if (unp == NULL)
1779                 goto unionfs_unlock_null_vnode;
1780         lvp = unp->un_lowervp;
1781         uvp = unp->un_uppervp;
1782
1783         if (lvp != NULLVP) {
1784                 VI_LOCK_FLAGS(lvp, MTX_DUPOK);
1785                 flags |= LK_INTERLOCK;
1786                 vholdl(lvp);
1787
1788                 VI_UNLOCK(vp);
1789                 ap->a_flags &= ~LK_INTERLOCK;
1790
1791                 error = VOP_UNLOCK(lvp, flags, ap->a_td);
1792
1793                 VI_LOCK(vp);
1794         }
1795
1796         if (error == 0 && uvp != NULLVP) {
1797                 VI_LOCK_FLAGS(uvp, MTX_DUPOK);
1798                 flags |= LK_INTERLOCK;
1799                 vholdl(uvp);
1800                 uhold = 1;
1801
1802                 VI_UNLOCK(vp);
1803                 ap->a_flags &= ~LK_INTERLOCK;
1804
1805                 error = VOP_UNLOCK(uvp, flags, ap->a_td);
1806
1807                 VI_LOCK(vp);
1808         }
1809
1810         VI_UNLOCK(vp);
1811         if (lvp != NULLVP)
1812                 vdrop(lvp);
1813         if (uhold != 0)
1814                 vdrop(uvp);
1815         if (mtxlkflag == 0)
1816                 VI_LOCK(vp);
1817
1818         return error;
1819
1820 unionfs_unlock_null_vnode:
1821         if (mtxlkflag == 2)
1822                 VI_UNLOCK(vp);
1823         return (vop_stdunlock(ap));
1824 }
1825
1826 static int
1827 unionfs_pathconf(struct vop_pathconf_args *ap)
1828 {
1829         struct unionfs_node *unp;
1830         struct vnode   *vp;
1831
1832         unp = VTOUNIONFS(ap->a_vp);
1833         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1834
1835         return (VOP_PATHCONF(vp, ap->a_name, ap->a_retval));
1836 }
1837
1838 static int
1839 unionfs_advlock(struct vop_advlock_args *ap)
1840 {
1841         int error;
1842         struct unionfs_node *unp;
1843         struct unionfs_node_status *unsp;
1844         struct vnode   *vp;
1845         struct vnode   *uvp;
1846         struct thread  *td;
1847
1848         UNIONFS_INTERNAL_DEBUG("unionfs_advlock: enter\n");
1849
1850         vp = ap->a_vp;
1851         td = curthread;
1852
1853         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1854
1855         unp = VTOUNIONFS(ap->a_vp);
1856         uvp = unp->un_uppervp;
1857
1858         if (uvp == NULLVP) {
1859                 error = unionfs_copyfile(unp, 1, td->td_ucred, td);
1860                 if (error != 0)
1861                         goto unionfs_advlock_abort;
1862                 uvp = unp->un_uppervp;
1863
1864                 unionfs_get_node_status(unp, td, &unsp);
1865                 if (unsp->uns_lower_opencnt > 0) {
1866                         /* try reopen the vnode */
1867                         error = VOP_OPEN(uvp, unsp->uns_lower_openmode, td->td_ucred, td, unsp->uns_lower_fdidx);
1868                         if (error)
1869                                 goto unionfs_advlock_abort;
1870                         unsp->uns_upper_opencnt++;
1871                         VOP_CLOSE(unp->un_lowervp, unsp->uns_lower_openmode, td->td_ucred, td);
1872                         unsp->uns_lower_opencnt--;
1873                 } else
1874                         unionfs_tryrem_node_status(unp, td, unsp);
1875         }
1876
1877         VOP_UNLOCK(vp, 0, td);
1878
1879         error = VOP_ADVLOCK(uvp, ap->a_id, ap->a_op, ap->a_fl, ap->a_flags);
1880
1881         UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error);
1882
1883         return error;
1884
1885 unionfs_advlock_abort:
1886         VOP_UNLOCK(vp, 0, td);
1887
1888         UNIONFS_INTERNAL_DEBUG("unionfs_advlock: leave (%d)\n", error);
1889
1890         return error;
1891 }
1892
1893 static int
1894 unionfs_strategy(struct vop_strategy_args *ap)
1895 {
1896         struct unionfs_node *unp;
1897         struct vnode   *vp;
1898
1899         unp = VTOUNIONFS(ap->a_vp);
1900         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1901
1902 #ifdef DIAGNOSTIC
1903         if (vp == NULLVP)
1904                 panic("unionfs_strategy: nullvp");
1905
1906         if (ap->a_bp->b_iocmd == BIO_WRITE && vp == unp->un_lowervp)
1907                 panic("unionfs_strategy: writing to lowervp");
1908 #endif
1909
1910         return (VOP_STRATEGY(vp, ap->a_bp));
1911 }
1912
1913 static int
1914 unionfs_getacl(struct vop_getacl_args *ap)
1915 {
1916         int             error;
1917         struct unionfs_node *unp;
1918         struct vnode   *vp;
1919
1920         unp = VTOUNIONFS(ap->a_vp);
1921         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1922
1923         UNIONFS_INTERNAL_DEBUG("unionfs_getacl: enter\n");
1924
1925         error = VOP_GETACL(vp, ap->a_type, ap->a_aclp, ap->a_cred, ap->a_td);
1926
1927         UNIONFS_INTERNAL_DEBUG("unionfs_getacl: leave (%d)\n", error);
1928
1929         return (error);
1930 }
1931
1932 static int
1933 unionfs_setacl(struct vop_setacl_args *ap)
1934 {
1935         int             error;
1936         struct unionfs_node *unp;
1937         struct vnode   *uvp;
1938         struct vnode   *lvp;
1939         struct thread  *td;
1940
1941         UNIONFS_INTERNAL_DEBUG("unionfs_setacl: enter\n");
1942
1943         error = EROFS;
1944         unp = VTOUNIONFS(ap->a_vp);
1945         uvp = unp->un_uppervp;
1946         lvp = unp->un_lowervp;
1947         td = ap->a_td;
1948
1949         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1950                 return (EROFS);
1951
1952         if (uvp == NULLVP && lvp->v_type == VREG) {
1953                 if ((error = unionfs_copyfile(unp, 1, ap->a_cred, td)) != 0)
1954                         return (error);
1955                 uvp = unp->un_uppervp;
1956         }
1957
1958         if (uvp != NULLVP)
1959                 error = VOP_SETACL(uvp, ap->a_type, ap->a_aclp, ap->a_cred, td);
1960
1961         UNIONFS_INTERNAL_DEBUG("unionfs_setacl: leave (%d)\n", error);
1962
1963         return (error);
1964 }
1965
1966 static int
1967 unionfs_aclcheck(struct vop_aclcheck_args *ap)
1968 {
1969         int             error;
1970         struct unionfs_node *unp;
1971         struct vnode   *vp;
1972
1973         UNIONFS_INTERNAL_DEBUG("unionfs_aclcheck: enter\n");
1974
1975         unp = VTOUNIONFS(ap->a_vp);
1976         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1977
1978         error = VOP_ACLCHECK(vp, ap->a_type, ap->a_aclp, ap->a_cred, ap->a_td);
1979
1980         UNIONFS_INTERNAL_DEBUG("unionfs_aclcheck: leave (%d)\n", error);
1981
1982         return (error);
1983 }
1984
1985 static int
1986 unionfs_openextattr(struct vop_openextattr_args *ap)
1987 {
1988         int             error;
1989         struct unionfs_node *unp;
1990         struct vnode   *vp;
1991
1992         unp = VTOUNIONFS(ap->a_vp);
1993         vp = (unp->un_uppervp != NULLVP ? unp->un_uppervp : unp->un_lowervp);
1994
1995         if ((vp == unp->un_uppervp && (unp->un_flag & UNIONFS_OPENEXTU)) ||
1996             (vp == unp->un_lowervp && (unp->un_flag & UNIONFS_OPENEXTL)))
1997                 return (EBUSY);
1998
1999         error = VOP_OPENEXTATTR(vp, ap->a_cred, ap->a_td);
2000
2001         if (error == 0) {
2002                 if (vp == unp->un_uppervp)
2003                         unp->un_flag |= UNIONFS_OPENEXTU;
2004                 else
2005                         unp->un_flag |= UNIONFS_OPENEXTL;
2006         }
2007
2008         return (error);
2009 }
2010
2011 static int
2012 unionfs_closeextattr(struct vop_closeextattr_args *ap)
2013 {
2014         int             error;
2015         struct unionfs_node *unp;
2016         struct vnode   *vp;
2017
2018         unp = VTOUNIONFS(ap->a_vp);
2019         vp = NULLVP;
2020
2021         if (unp->un_flag & UNIONFS_OPENEXTU)
2022                 vp = unp->un_uppervp;
2023         else if (unp->un_flag & UNIONFS_OPENEXTL)
2024                 vp = unp->un_lowervp;
2025
2026         if (vp == NULLVP)
2027                 return (EOPNOTSUPP);
2028
2029         error = VOP_CLOSEEXTATTR(vp, ap->a_commit, ap->a_cred, ap->a_td);
2030
2031         if (error == 0) {
2032                 if (vp == unp->un_uppervp)
2033                         unp->un_flag &= ~UNIONFS_OPENEXTU;
2034                 else
2035                         unp->un_flag &= ~UNIONFS_OPENEXTL;
2036         }
2037
2038         return (error);
2039 }
2040
2041 static int
2042 unionfs_getextattr(struct vop_getextattr_args *ap)
2043 {
2044         struct unionfs_node *unp;
2045         struct vnode   *vp;
2046
2047         unp = VTOUNIONFS(ap->a_vp);
2048         vp = NULLVP;
2049
2050         if (unp->un_flag & UNIONFS_OPENEXTU)
2051                 vp = unp->un_uppervp;
2052         else if (unp->un_flag & UNIONFS_OPENEXTL)
2053                 vp = unp->un_lowervp;
2054
2055         if (vp == NULLVP)
2056                 return (EOPNOTSUPP);
2057
2058         return (VOP_GETEXTATTR(vp, ap->a_attrnamespace, ap->a_name,
2059             ap->a_uio, ap->a_size, ap->a_cred, ap->a_td));
2060 }
2061
2062 static int
2063 unionfs_setextattr(struct vop_setextattr_args *ap)
2064 {
2065         int             error;
2066         struct unionfs_node *unp;
2067         struct vnode   *uvp;
2068         struct vnode   *lvp;
2069         struct vnode   *ovp;
2070         struct ucred   *cred;
2071         struct thread  *td;
2072
2073         error = EROFS;
2074         unp = VTOUNIONFS(ap->a_vp);
2075         uvp = unp->un_uppervp;
2076         lvp = unp->un_lowervp;
2077         ovp = NULLVP;
2078         cred = ap->a_cred;
2079         td = ap->a_td;
2080
2081         UNIONFS_INTERNAL_DEBUG("unionfs_setextattr: enter (un_flag=%x)\n", unp->un_flag);
2082
2083         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2084                 return (EROFS);
2085
2086         if (unp->un_flag & UNIONFS_OPENEXTU)
2087                 ovp = unp->un_uppervp;
2088         else if (unp->un_flag & UNIONFS_OPENEXTL)
2089                 ovp = unp->un_lowervp;
2090
2091         if (ovp == NULLVP)
2092                 return (EOPNOTSUPP);
2093
2094         if (ovp == lvp && lvp->v_type == VREG) {
2095                 VOP_CLOSEEXTATTR(lvp, 0, cred, td);
2096                 if (uvp == NULLVP &&
2097                     (error = unionfs_copyfile(unp, 1, cred, td)) != 0) {
2098 unionfs_setextattr_reopen:
2099                         if ((unp->un_flag & UNIONFS_OPENEXTL) &&
2100                             VOP_OPENEXTATTR(lvp, cred, td)) {
2101 #ifdef DIAGNOSTIC
2102                                 panic("unionfs: VOP_OPENEXTATTR failed");
2103 #endif
2104                                 unp->un_flag &= ~UNIONFS_OPENEXTL;
2105                         }
2106                         goto unionfs_setextattr_abort;
2107                 }
2108                 uvp = unp->un_uppervp;
2109                 if ((error = VOP_OPENEXTATTR(uvp, cred, td)) != 0)
2110                         goto unionfs_setextattr_reopen;
2111                 unp->un_flag &= ~UNIONFS_OPENEXTL;
2112                 unp->un_flag |= UNIONFS_OPENEXTU;
2113                 ovp = uvp;
2114         }
2115
2116         if (ovp == uvp)
2117                 error = VOP_SETEXTATTR(ovp, ap->a_attrnamespace, ap->a_name,
2118                     ap->a_uio, cred, td);
2119
2120 unionfs_setextattr_abort:
2121         UNIONFS_INTERNAL_DEBUG("unionfs_setextattr: leave (%d)\n", error);
2122
2123         return (error);
2124 }
2125
2126 static int
2127 unionfs_listextattr(struct vop_listextattr_args *ap)
2128 {
2129         struct unionfs_node *unp;
2130         struct vnode   *vp;
2131
2132         unp = VTOUNIONFS(ap->a_vp);
2133         vp = NULLVP;
2134
2135         if (unp->un_flag & UNIONFS_OPENEXTU)
2136                 vp = unp->un_uppervp;
2137         else if (unp->un_flag & UNIONFS_OPENEXTL)
2138                 vp = unp->un_lowervp;
2139
2140         if (vp == NULLVP)
2141                 return (EOPNOTSUPP);
2142
2143         return (VOP_LISTEXTATTR(vp, ap->a_attrnamespace, ap->a_uio,
2144             ap->a_size, ap->a_cred, ap->a_td));
2145 }
2146
2147 static int
2148 unionfs_deleteextattr(struct vop_deleteextattr_args *ap)
2149 {
2150         int             error;
2151         struct unionfs_node *unp;
2152         struct vnode   *uvp;
2153         struct vnode   *lvp;
2154         struct vnode   *ovp;
2155         struct ucred   *cred;
2156         struct thread  *td;
2157
2158         error = EROFS;
2159         unp = VTOUNIONFS(ap->a_vp);
2160         uvp = unp->un_uppervp;
2161         lvp = unp->un_lowervp;
2162         ovp = NULLVP;
2163         cred = ap->a_cred;
2164         td = ap->a_td;
2165
2166         UNIONFS_INTERNAL_DEBUG("unionfs_deleteextattr: enter (un_flag=%x)\n", unp->un_flag);
2167
2168         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2169                 return (EROFS);
2170
2171         if (unp->un_flag & UNIONFS_OPENEXTU)
2172                 ovp = unp->un_uppervp;
2173         else if (unp->un_flag & UNIONFS_OPENEXTL)
2174                 ovp = unp->un_lowervp;
2175
2176         if (ovp == NULLVP)
2177                 return (EOPNOTSUPP);
2178
2179         if (ovp == lvp && lvp->v_type == VREG) {
2180                 VOP_CLOSEEXTATTR(lvp, 0, cred, td);
2181                 if (uvp == NULLVP &&
2182                     (error = unionfs_copyfile(unp, 1, cred, td)) != 0) {
2183 unionfs_deleteextattr_reopen:
2184                         if ((unp->un_flag & UNIONFS_OPENEXTL) &&
2185                             VOP_OPENEXTATTR(lvp, cred, td)) {
2186 #ifdef DIAGNOSTIC
2187                                 panic("unionfs: VOP_OPENEXTATTR failed");
2188 #endif
2189                                 unp->un_flag &= ~UNIONFS_OPENEXTL;
2190                         }
2191                         goto unionfs_deleteextattr_abort;
2192                 }
2193                 uvp = unp->un_uppervp;
2194                 if ((error = VOP_OPENEXTATTR(uvp, cred, td)) != 0)
2195                         goto unionfs_deleteextattr_reopen;
2196                 unp->un_flag &= ~UNIONFS_OPENEXTL;
2197                 unp->un_flag |= UNIONFS_OPENEXTU;
2198                 ovp = uvp;
2199         }
2200
2201         if (ovp == uvp)
2202                 error = VOP_DELETEEXTATTR(ovp, ap->a_attrnamespace, ap->a_name,
2203                     ap->a_cred, ap->a_td);
2204
2205 unionfs_deleteextattr_abort:
2206         UNIONFS_INTERNAL_DEBUG("unionfs_deleteextattr: leave (%d)\n", error);
2207
2208         return (error);
2209 }
2210
2211 static int
2212 unionfs_setlabel(struct vop_setlabel_args *ap)
2213 {
2214         int             error;
2215         struct unionfs_node *unp;
2216         struct vnode   *uvp;
2217         struct vnode   *lvp;
2218         struct thread  *td;
2219
2220         UNIONFS_INTERNAL_DEBUG("unionfs_setlabel: enter\n");
2221
2222         error = EROFS;
2223         unp = VTOUNIONFS(ap->a_vp);
2224         uvp = unp->un_uppervp;
2225         lvp = unp->un_lowervp;
2226         td = ap->a_td;
2227
2228         if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2229                 return (EROFS);
2230
2231         if (uvp == NULLVP && lvp->v_type == VREG) {
2232                 if ((error = unionfs_copyfile(unp, 1, ap->a_cred, td)) != 0)
2233                         return (error);
2234                 uvp = unp->un_uppervp;
2235         }
2236
2237         if (uvp != NULLVP)
2238                 error = VOP_SETLABEL(uvp, ap->a_label, ap->a_cred, td);
2239
2240         UNIONFS_INTERNAL_DEBUG("unionfs_setlabel: leave (%d)\n", error);
2241
2242         return (error);
2243 }
2244
2245 struct vop_vector unionfs_vnodeops = {
2246         .vop_default =          &default_vnodeops,
2247
2248         .vop_access =           unionfs_access,
2249         .vop_aclcheck =         unionfs_aclcheck,
2250         .vop_advlock =          unionfs_advlock,
2251         .vop_bmap =             VOP_EOPNOTSUPP,
2252         .vop_cachedlookup =     unionfs_lookup,
2253         .vop_close =            unionfs_close,
2254         .vop_closeextattr =     unionfs_closeextattr,
2255         .vop_create =           unionfs_create,
2256         .vop_deleteextattr =    unionfs_deleteextattr,
2257         .vop_fsync =            unionfs_fsync,
2258         .vop_getacl =           unionfs_getacl,
2259         .vop_getattr =          unionfs_getattr,
2260         .vop_getextattr =       unionfs_getextattr,
2261         .vop_getwritemount =    unionfs_getwritemount,
2262         .vop_inactive =         unionfs_inactive,
2263         .vop_ioctl =            unionfs_ioctl,
2264         .vop_lease =            unionfs_lease,
2265         .vop_link =             unionfs_link,
2266         .vop_listextattr =      unionfs_listextattr,
2267         .vop_lock =             unionfs_lock,
2268         .vop_lookup =           vfs_cache_lookup,
2269         .vop_mkdir =            unionfs_mkdir,
2270         .vop_mknod =            unionfs_mknod,
2271         .vop_open =             unionfs_open,
2272         .vop_openextattr =      unionfs_openextattr,
2273         .vop_pathconf =         unionfs_pathconf,
2274         .vop_poll =             unionfs_poll,
2275         .vop_print =            unionfs_print,
2276         .vop_read =             unionfs_read,
2277         .vop_readdir =          unionfs_readdir,
2278         .vop_readlink =         unionfs_readlink,
2279         .vop_reclaim =          unionfs_reclaim,
2280         .vop_remove =           unionfs_remove,
2281         .vop_rename =           unionfs_rename,
2282         .vop_rmdir =            unionfs_rmdir,
2283         .vop_setacl =           unionfs_setacl,
2284         .vop_setattr =          unionfs_setattr,
2285         .vop_setextattr =       unionfs_setextattr,
2286         .vop_setlabel =         unionfs_setlabel,
2287         .vop_strategy =         unionfs_strategy,
2288         .vop_symlink =          unionfs_symlink,
2289         .vop_unlock =           unionfs_unlock,
2290         .vop_whiteout =         unionfs_whiteout,
2291         .vop_write =            unionfs_write,
2292 };