procfs_validfile does have a public prototyp, but doesn't seemed to be used.
[dragonfly.git] / sys / vfs / union / 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.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Jan-Simon Pendry.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by the University of
20  *      California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *      @(#)union_vnops.c       8.32 (Berkeley) 6/23/95
38  * $FreeBSD: src/sys/miscfs/union/union_vnops.c,v 1.72 1999/12/15 23:02:14 eivind Exp $
39  * $DragonFly: src/sys/vfs/union/union_vnops.c,v 1.12 2004/05/20 05:09:18 cpressey Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/fcntl.h>
46 #include <sys/stat.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/malloc.h>
52 #include <sys/buf.h>
53 #include <sys/lock.h>
54 #include <sys/sysctl.h>
55 #include "union.h"
56
57 #include <vm/vm.h>
58 #include <vm/vnode_pager.h>
59
60 #include <vm/vm_page.h>
61 #include <vm/vm_object.h>
62
63 int uniondebug = 0;
64
65 #if UDEBUG_ENABLED
66 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, "");
67 #else
68 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, "");
69 #endif
70
71 static int      union_access (struct vop_access_args *ap);
72 static int      union_advlock (struct vop_advlock_args *ap);
73 static int      union_bmap (struct vop_bmap_args *ap);
74 static int      union_close (struct vop_close_args *ap);
75 static int      union_create (struct vop_create_args *ap);
76 static int      union_fsync (struct vop_fsync_args *ap);
77 static int      union_getattr (struct vop_getattr_args *ap);
78 static int      union_inactive (struct vop_inactive_args *ap);
79 static int      union_ioctl (struct vop_ioctl_args *ap);
80 static int      union_lease (struct vop_lease_args *ap);
81 static int      union_link (struct vop_link_args *ap);
82 static int      union_lock (struct vop_lock_args *ap);
83 static int      union_lookup (struct vop_lookup_args *ap);
84 static int      union_lookup1 (struct vnode *udvp, struct vnode **dvp,
85                                    struct vnode **vpp,
86                                    struct componentname *cnp);
87 static int      union_mkdir (struct vop_mkdir_args *ap);
88 static int      union_mknod (struct vop_mknod_args *ap);
89 static int      union_mmap (struct vop_mmap_args *ap);
90 static int      union_open (struct vop_open_args *ap);
91 static int      union_pathconf (struct vop_pathconf_args *ap);
92 static int      union_print (struct vop_print_args *ap);
93 static int      union_read (struct vop_read_args *ap);
94 static int      union_readdir (struct vop_readdir_args *ap);
95 static int      union_readlink (struct vop_readlink_args *ap);
96 static int      union_reclaim (struct vop_reclaim_args *ap);
97 static int      union_remove (struct vop_remove_args *ap);
98 static int      union_rename (struct vop_rename_args *ap);
99 static int      union_revoke (struct vop_revoke_args *ap);
100 static int      union_rmdir (struct vop_rmdir_args *ap);
101 static int      union_poll (struct vop_poll_args *ap);
102 static int      union_setattr (struct vop_setattr_args *ap);
103 static int      union_strategy (struct vop_strategy_args *ap);
104 static int      union_getpages (struct vop_getpages_args *ap);
105 static int      union_putpages (struct vop_putpages_args *ap);
106 static int      union_symlink (struct vop_symlink_args *ap);
107 static int      union_unlock (struct vop_unlock_args *ap);
108 static int      union_whiteout (struct vop_whiteout_args *ap);
109 static int      union_write (struct vop_read_args *ap);
110
111 static __inline
112 struct vnode *
113 union_lock_upper(struct union_node *un, struct thread *td)
114 {
115         struct vnode *uppervp;
116
117         if ((uppervp = un->un_uppervp) != NULL) {
118                 vref(uppervp);
119                 vn_lock(uppervp, NULL, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
120         }
121         KASSERT((uppervp == NULL || uppervp->v_usecount > 0), ("uppervp usecount is 0"));
122         return(uppervp);
123 }
124
125 static __inline
126 void
127 union_unlock_upper(struct vnode *uppervp, struct thread *td)
128 {
129         vput(uppervp);
130 }
131
132 static __inline
133 struct vnode *
134 union_lock_other(struct union_node *un, struct thread *td)
135 {
136         struct vnode *vp;
137
138         if (un->un_uppervp != NULL) {
139                 vp = union_lock_upper(un, td);
140         } else if ((vp = un->un_lowervp) != NULL) {
141                 vref(vp);
142                 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY, td);
143         }
144         return(vp);
145 }
146
147 static __inline
148 void
149 union_unlock_other(struct vnode *vp, struct thread *td)
150 {
151         vput(vp);
152 }
153
154 /*
155  *      union_lookup:
156  *
157  *      udvp    must be exclusively locked on call and will remain 
158  *              exclusively locked on return.  This is the mount point 
159  *              for out filesystem.
160  *
161  *      dvp     Our base directory, locked and referenced.
162  *              The passed dvp will be dereferenced and unlocked on return
163  *              and a new dvp will be returned which is locked and 
164  *              referenced in the same variable.
165  *
166  *      vpp     is filled in with the result if no error occured,
167  *              locked and ref'd.
168  *
169  *              If an error is returned, *vpp is set to NULLVP.  If no
170  *              error occurs, *vpp is returned with a reference and an
171  *              exclusive lock.
172  */
173
174 static int
175 union_lookup1(struct vnode *udvp, struct vnode **pdvp, struct vnode **vpp,
176               struct componentname *cnp)
177 {
178         int error;
179         struct thread *td = cnp->cn_td;
180         struct vnode *dvp = *pdvp;
181         struct vnode *tdvp;
182         struct mount *mp;
183
184         /*
185          * If stepping up the directory tree, check for going
186          * back across the mount point, in which case do what
187          * lookup would do by stepping back down the mount
188          * hierarchy.
189          */
190         if (cnp->cn_flags & CNP_ISDOTDOT) {
191                 while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
192                         /*
193                          * Don't do the NOCROSSMOUNT check
194                          * at this level.  By definition,
195                          * union fs deals with namespaces, not
196                          * filesystems.
197                          */
198                         tdvp = dvp;
199                         dvp = dvp->v_mount->mnt_vnodecovered;
200                         vref(dvp);
201                         vput(tdvp);
202                         vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
203                 }
204         }
205
206         /*
207          * Set return dvp to be the upperdvp 'parent directory.
208          */
209         *pdvp = dvp;
210
211         /*
212          * If the VOP_LOOKUP call generates an error, tdvp is invalid and no
213          * changes will have been made to dvp, so we are set to return.
214          */
215
216         error = VOP_LOOKUP(dvp, NCPNULL, &tdvp, NCPPNULL, cnp);
217         if (error) {
218                 UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags));
219                 *vpp = NULL;
220                 return (error);
221         }
222
223         /*
224          * The parent directory will have been unlocked, unless lookup
225          * found the last component or if dvp == tdvp (tdvp must be locked).
226          *
227          * We want our dvp to remain locked and ref'd.  We also want tdvp
228          * to remain locked and ref'd.
229          */
230         UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags));
231
232         if (dvp != tdvp && (cnp->cn_flags & CNP_ISLASTCN) == 0)
233                 vn_lock(dvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
234
235         /*
236          * Lastly check if the current node is a mount point in
237          * which case walk up the mount hierarchy making sure not to
238          * bump into the root of the mount tree (ie. dvp != udvp).
239          *
240          * We use dvp as a temporary variable here, it is no longer related
241          * to the dvp above.  However, we have to ensure that both *pdvp and
242          * tdvp are locked on return.
243          */
244
245         dvp = tdvp;
246         while (
247             dvp != udvp && 
248             (dvp->v_type == VDIR) &&
249             (mp = dvp->v_mountedhere)
250         ) {
251                 int relock_pdvp = 0;
252
253                 if (vfs_busy(mp, 0, NULL, td))
254                         continue;
255
256                 if (dvp == *pdvp)
257                         relock_pdvp = 1;
258                 vput(dvp);
259                 dvp = NULL;
260                 error = VFS_ROOT(mp, &dvp);
261
262                 vfs_unbusy(mp, td);
263
264                 if (relock_pdvp)
265                         vn_lock(*pdvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
266
267                 if (error) {
268                         *vpp = NULL;
269                         return (error);
270                 }
271         }
272         *vpp = dvp;
273         return (0);
274 }
275
276 /*
277  * union_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
278  *              struct vnode **a_vpp, struct componentname *a_cnp)
279  */
280 static int
281 union_lookup(struct vop_lookup_args *ap)
282 {
283         int error;
284         int uerror, lerror;
285         struct vnode *uppervp, *lowervp;
286         struct vnode *upperdvp, *lowerdvp;
287         struct vnode *dvp = ap->a_dvp;          /* starting dir */
288         struct union_node *dun = VTOUNION(dvp); /* associated union node */
289         struct componentname *cnp = ap->a_cnp;
290         struct thread *td = cnp->cn_td;
291         int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
292         struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
293         struct ucred *saved_cred = NULL;
294         int iswhiteout;
295         struct vattr va;
296
297         *ap->a_vpp = NULLVP;
298
299         /*
300          * Disallow write attemps to the filesystem mounted read-only.
301          */
302         if ((cnp->cn_flags & CNP_ISLASTCN) && 
303             (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
304             (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) {
305                 return (EROFS);
306         }
307
308         /*
309          * For any lookup's we do, always return with the parent locked
310          */
311         cnp->cn_flags |= CNP_LOCKPARENT;
312
313         lowerdvp = dun->un_lowervp;
314         uppervp = NULLVP;
315         lowervp = NULLVP;
316         iswhiteout = 0;
317
318         uerror = ENOENT;
319         lerror = ENOENT;
320
321         /*
322          * Get a private lock on uppervp and a reference, effectively 
323          * taking it out of the union_node's control.
324          *
325          * We must lock upperdvp while holding our lock on dvp
326          * to avoid a deadlock.
327          */
328         upperdvp = union_lock_upper(dun, td);
329
330         /*
331          * do the lookup in the upper level.
332          * if that level comsumes additional pathnames,
333          * then assume that something special is going
334          * on and just return that vnode.
335          */
336         if (upperdvp != NULLVP) {
337                 /*
338                  * We do not have to worry about the DOTDOT case, we've
339                  * already unlocked dvp.
340                  */
341                 UDEBUG(("A %p\n", upperdvp));
342
343                 /*
344                  * Do the lookup.   We must supply a locked and referenced
345                  * upperdvp to the function and will get a new locked and
346                  * referenced upperdvp back with the old having been 
347                  * dereferenced.
348                  *
349                  * If an error is returned, uppervp will be NULLVP.  If no
350                  * error occurs, uppervp will be the locked and referenced
351                  * return vnode or possibly NULL, depending on what is being
352                  * requested.  It is possible that the returned uppervp
353                  * will be the same as upperdvp.
354                  */
355                 uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
356                 UDEBUG((
357                     "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
358                     uerror,
359                     upperdvp,
360                     upperdvp->v_usecount,
361                     VOP_ISLOCKED(upperdvp, NULL),
362                     uppervp,
363                     (uppervp ? uppervp->v_usecount : -99),
364                     (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99)
365                 ));
366
367                 /*
368                  * Disallow write attemps to the filesystem mounted read-only.
369                  */
370                 if (uerror == EJUSTRETURN && (cnp->cn_flags & CNP_ISLASTCN) &&
371                     (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
372                     (cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME)) {
373                         error = EROFS;
374                         goto out;
375                 }
376
377                 /*
378                  * Special case.  If cn_consume != 0 skip out.  The result
379                  * of the lookup is transfered to our return variable.  If
380                  * an error occured we have to throw away the results.
381                  */
382
383                 if (cnp->cn_consume != 0) {
384                         if ((error = uerror) == 0) {
385                                 *ap->a_vpp = uppervp;
386                                 uppervp = NULL;
387                         }
388                         goto out;
389                 }
390
391                 /*
392                  * Calculate whiteout, fall through
393                  */
394
395                 if (uerror == ENOENT || uerror == EJUSTRETURN) {
396                         if (cnp->cn_flags & CNP_ISWHITEOUT) {
397                                 iswhiteout = 1;
398                         } else if (lowerdvp != NULLVP) {
399                                 int terror;
400
401                                 terror = VOP_GETATTR(upperdvp, &va, cnp->cn_td);
402                                 if (terror == 0 && (va.va_flags & OPAQUE))
403                                         iswhiteout = 1;
404                         }
405                 }
406         }
407
408         /*
409          * in a similar way to the upper layer, do the lookup
410          * in the lower layer.   this time, if there is some
411          * component magic going on, then vput whatever we got
412          * back from the upper layer and return the lower vnode
413          * instead.
414          */
415
416         if (lowerdvp != NULLVP && !iswhiteout) {
417                 int nameiop;
418
419                 UDEBUG(("B %p\n", lowerdvp));
420
421                 /*
422                  * Force only LOOKUPs on the lower node, since
423                  * we won't be making changes to it anyway.
424                  */
425                 nameiop = cnp->cn_nameiop;
426                 cnp->cn_nameiop = NAMEI_LOOKUP;
427                 if (um->um_op == UNMNT_BELOW) {
428                         saved_cred = cnp->cn_cred;
429                         cnp->cn_cred = um->um_cred;
430                 }
431
432                 /*
433                  * We shouldn't have to worry about locking interactions
434                  * between the lower layer and our union layer (w.r.t.
435                  * `..' processing) because we don't futz with lowervp
436                  * locks in the union-node instantiation code path.
437                  *
438                  * union_lookup1() requires lowervp to be locked on entry,
439                  * and it will be unlocked on return.  The ref count will
440                  * not change.  On return lowervp doesn't represent anything
441                  * to us so we NULL it out.
442                  */
443                 vref(lowerdvp);
444                 vn_lock(lowerdvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
445                 lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
446                 if (lowerdvp == lowervp)
447                         vrele(lowerdvp);
448                 else
449                         vput(lowerdvp);
450                 lowerdvp = NULL;        /* lowerdvp invalid after vput */
451
452                 if (um->um_op == UNMNT_BELOW)
453                         cnp->cn_cred = saved_cred;
454                 cnp->cn_nameiop = nameiop;
455
456                 if (cnp->cn_consume != 0 || lerror == EACCES) {
457                         if ((error = lerror) == 0) {
458                                 *ap->a_vpp = lowervp;
459                                 lowervp = NULL;
460                         }
461                         goto out;
462                 }
463         } else {
464                 UDEBUG(("C %p\n", lowerdvp));
465                 if ((cnp->cn_flags & CNP_ISDOTDOT) && dun->un_pvp != NULLVP) {
466                         if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
467                                 vref(lowervp);
468                                 vn_lock(lowervp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
469                                 lerror = 0;
470                         }
471                 }
472         }
473
474         /*
475          * Ok.  Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
476          *
477          * 1. If both layers returned an error, select the upper layer.
478          *
479          * 2. If the upper layer faile and the bottom layer succeeded,
480          *    two subcases occur:
481          *
482          *      a.  The bottom vnode is not a directory, in which case
483          *          just return a new union vnode referencing an
484          *          empty top layer and the existing bottom layer.
485          *
486          *      b.  The button vnode is a directory, in which case
487          *          create a new directory in the top layer and
488          *          and fall through to case 3.
489          *
490          * 3. If the top layer succeeded then return a new union
491          *    vnode referencing whatever the new top layer and
492          *    whatever the bottom layer returned.
493          */
494
495         /* case 1. */
496         if ((uerror != 0) && (lerror != 0)) {
497                 error = uerror;
498                 goto out;
499         }
500
501         /* case 2. */
502         if (uerror != 0 /* && (lerror == 0) */ ) {
503                 if (lowervp->v_type == VDIR) { /* case 2b. */
504                         KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
505                         /*
506                          * oops, uppervp has a problem, we may have to shadow.
507                          */
508                         uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
509                         if (uerror) {
510                                 error = uerror;
511                                 goto out;
512                         }
513                 }
514         }
515
516         /*
517          * Must call union_allocvp with both the upper and lower vnodes
518          * referenced and the upper vnode locked.   ap->a_vpp is returned 
519          * referenced and locked.  lowervp, uppervp, and upperdvp are 
520          * absorbed by union_allocvp() whether it succeeds or fails.
521          *
522          * upperdvp is the parent directory of uppervp which may be
523          * different, depending on the path, from dvp->un_uppervp.  That's
524          * why it is a separate argument.  Note that it must be unlocked.
525          *
526          * dvp must be locked on entry to the call and will be locked on
527          * return.
528          */
529
530         if (uppervp && uppervp != upperdvp)
531                 VOP_UNLOCK(uppervp, NULL, 0, td);
532         if (lowervp)
533                 VOP_UNLOCK(lowervp, NULL, 0, td);
534         if (upperdvp)
535                 VOP_UNLOCK(upperdvp, NULL, 0, td);
536
537         error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
538                               uppervp, lowervp, 1);
539
540         UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? ((*ap->a_vpp)->v_usecount) : -99));
541
542         uppervp = NULL;
543         upperdvp = NULL;
544         lowervp = NULL;
545
546         /* 
547          *      Termination Code
548          *
549          *      - put away any extra junk laying around.  Note that lowervp
550          *        (if not NULL) will never be the same as *ap->a_vp and 
551          *        neither will uppervp, because when we set that state we 
552          *        NULL-out lowervp or uppervp.  On the otherhand, upperdvp
553          *        may match uppervp or *ap->a_vpp.
554          *
555          *      - relock/unlock dvp if appropriate.
556          */
557
558 out:
559         if (upperdvp) {
560                 if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
561                         vrele(upperdvp);
562                 else
563                         vput(upperdvp);
564         }
565
566         if (uppervp)
567                 vput(uppervp);
568
569         if (lowervp)
570                 vput(lowervp);
571
572         /*
573          * Restore LOCKPARENT state
574          */
575
576         if (!lockparent)
577                 cnp->cn_flags &= ~CNP_LOCKPARENT;
578
579         UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
580                 ((*ap->a_vpp) ? (*ap->a_vpp)->v_usecount : -99),
581                 lowervp, uppervp));
582
583         /*
584          * dvp lock state, determine whether to relock dvp.  dvp is expected
585          * to be locked on return if:
586          *
587          *      - there was an error (except not EJUSTRETURN), or
588          *      - we hit the last component and lockparent is true
589          *
590          * dvp_is_locked is the current state of the dvp lock, not counting
591          * the possibility that *ap->a_vpp == dvp (in which case it is locked
592          * anyway).  Note that *ap->a_vpp == dvp only if no error occured.
593          */
594
595         if (*ap->a_vpp != dvp) {
596                 if ((error == 0 || error == EJUSTRETURN) &&
597                     (!lockparent || (cnp->cn_flags & CNP_ISLASTCN) == 0)) {
598                         VOP_UNLOCK(dvp, NULL, 0, td);
599                 }
600         }
601
602         /*
603          * Diagnostics
604          */
605
606 #ifdef DIAGNOSTIC
607         if (cnp->cn_namelen == 1 &&
608             cnp->cn_nameptr[0] == '.' &&
609             *ap->a_vpp != dvp) {
610                 panic("union_lookup returning . (%p) not same as startdir (%p)", ap->a_vpp, dvp);
611         }
612 #endif
613
614         return (error);
615 }
616
617 /*
618  *      union_create:
619  *
620  * a_dvp is locked on entry and remains locked on return.  a_vpp is returned
621  * locked if no error occurs, otherwise it is garbage.
622  *
623  * union_create(struct vnode *a_dvp, struct vnode **a_vpp,
624  *              struct componentname *a_cnp, struct vattr *a_vap)
625  */
626 static int
627 union_create(struct vop_create_args *ap)
628 {
629         struct union_node *dun = VTOUNION(ap->a_dvp);
630         struct componentname *cnp = ap->a_cnp;
631         struct thread *td = cnp->cn_td;
632         struct vnode *dvp;
633         int error = EROFS;
634
635         if ((dvp = union_lock_upper(dun, td)) != NULL) {
636                 struct vnode *vp;
637                 struct mount *mp;
638
639                 error = VOP_CREATE(dvp, NCPNULL, &vp, cnp, ap->a_vap);
640                 if (error == 0) {
641                         mp = ap->a_dvp->v_mount;
642                         VOP_UNLOCK(vp, NULL, 0, td);
643                         UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vp->v_usecount));
644                         error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
645                                 cnp, vp, NULLVP, 1);
646                         UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
647                 }
648                 union_unlock_upper(dvp, td);
649         }
650         return (error);
651 }
652
653 /*
654  * union_whiteout(struct vnode *a_dvp, struct componentname *a_cnp,
655  *                int a_flags)
656  */
657 static int
658 union_whiteout(struct vop_whiteout_args *ap)
659 {
660         struct union_node *un = VTOUNION(ap->a_dvp);
661         struct componentname *cnp = ap->a_cnp;
662         struct vnode *uppervp;
663         int error = EOPNOTSUPP;
664
665         if ((uppervp = union_lock_upper(un, cnp->cn_td)) != NULLVP) {
666                 error = VOP_WHITEOUT(un->un_uppervp, NCPNULL, cnp, ap->a_flags);
667                 union_unlock_upper(uppervp, cnp->cn_td);
668         }
669         return(error);
670 }
671
672 /*
673  *      union_mknod:
674  *
675  *      a_dvp is locked on entry and should remain locked on return.
676  *      a_vpp is garbagre whether an error occurs or not.
677  *
678  * union_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
679  *              struct componentname *a_cnp, struct vattr *a_vap)
680  */
681 static int
682 union_mknod(struct vop_mknod_args *ap)
683 {
684         struct union_node *dun = VTOUNION(ap->a_dvp);
685         struct componentname *cnp = ap->a_cnp;
686         struct vnode *dvp;
687         int error = EROFS;
688
689         if ((dvp = union_lock_upper(dun, cnp->cn_td)) != NULL) {
690                 error = VOP_MKNOD(dvp, NCPNULL, ap->a_vpp, cnp, ap->a_vap);
691                 union_unlock_upper(dvp, cnp->cn_td);
692         }
693         return (error);
694 }
695
696 /*
697  *      union_open:
698  *
699  *      run open VOP.  When opening the underlying vnode we have to mimic
700  *      vn_open.  What we *really* need to do to avoid screwups if the
701  *      open semantics change is to call vn_open().  For example, ufs blows
702  *      up if you open a file but do not vmio it prior to writing.
703  *
704  * union_open(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_mode,
705  *            struct ucred *a_cred, struct thread *a_td)
706  */
707 static int
708 union_open(struct vop_open_args *ap)
709 {
710         struct union_node *un = VTOUNION(ap->a_vp);
711         struct vnode *tvp;
712         int mode = ap->a_mode;
713         struct ucred *cred = ap->a_cred;
714         struct thread *td = ap->a_td;
715         int error = 0;
716         int tvpisupper = 1;
717
718         /*
719          * If there is an existing upper vp then simply open that.
720          * The upper vp takes precedence over the lower vp.  When opening
721          * a lower vp for writing copy it to the uppervp and then open the
722          * uppervp.
723          *
724          * At the end of this section tvp will be left locked.
725          */
726         if ((tvp = union_lock_upper(un, td)) == NULLVP) {
727                 /*
728                  * If the lower vnode is being opened for writing, then
729                  * copy the file contents to the upper vnode and open that,
730                  * otherwise can simply open the lower vnode.
731                  */
732                 tvp = un->un_lowervp;
733                 if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
734                         int docopy = !(mode & O_TRUNC);
735                         error = union_copyup(un, docopy, cred, td);
736                         tvp = union_lock_upper(un, td);
737                 } else {
738                         un->un_openl++;
739                         vref(tvp);
740                         vn_lock(tvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
741                         tvpisupper = 0;
742                 }
743         }
744
745         /*
746          * We are holding the correct vnode, open it
747          */
748
749         if (error == 0)
750                 error = VOP_OPEN(tvp, mode, cred, td);
751
752         /*
753          * Absolutely necessary or UFS will blowup
754          */
755         if (error == 0 && vn_canvmio(tvp) == TRUE) {
756                 error = vfs_object_create(tvp, td);
757         }
758
759         /*
760          * Release any locks held
761          */
762         if (tvpisupper) {
763                 if (tvp)
764                         union_unlock_upper(tvp, td);
765         } else {
766                 vput(tvp);
767         }
768         return (error);
769 }
770
771 /*
772  *      union_close:
773  *
774  *      It is unclear whether a_vp is passed locked or unlocked.  Whatever
775  *      the case we do not change it.
776  *
777  * union_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
778  *              struct thread *a_td)
779  */
780 static int
781 union_close(struct vop_close_args *ap)
782 {
783         struct union_node *un = VTOUNION(ap->a_vp);
784         struct vnode *vp;
785
786         if ((vp = un->un_uppervp) == NULLVP) {
787 #ifdef UNION_DIAGNOSTIC
788                 if (un->un_openl <= 0)
789                         panic("union: un_openl cnt");
790 #endif
791                 --un->un_openl;
792                 vp = un->un_lowervp;
793         }
794         ap->a_vp = vp;
795         return (VCALL(vp, VOFFSET(vop_close), ap));
796 }
797
798 /*
799  * Check access permission on the union vnode.
800  * The access check being enforced is to check
801  * against both the underlying vnode, and any
802  * copied vnode.  This ensures that no additional
803  * file permissions are given away simply because
804  * the user caused an implicit file copy.
805  *
806  * union_access(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_mode,
807  *              struct ucred *a_cred, struct thread *a_td)
808  */
809 static int
810 union_access(struct vop_access_args *ap)
811 {
812         struct union_node *un = VTOUNION(ap->a_vp);
813         struct thread *td = ap->a_td;
814         int error = EACCES;
815         struct vnode *vp;
816
817         /*
818          * Disallow write attempts on filesystems mounted read-only.
819          */
820         if ((ap->a_mode & VWRITE) && 
821             (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
822                 switch (ap->a_vp->v_type) {
823                 case VREG: 
824                 case VDIR:
825                 case VLNK:
826                         return (EROFS);
827                 default:
828                         break;
829                 }
830         }
831
832         if ((vp = union_lock_upper(un, td)) != NULLVP) {
833                 ap->a_vp = vp;
834                 error = VCALL(vp, VOFFSET(vop_access), ap);
835                 union_unlock_upper(vp, td);
836                 return(error);
837         }
838
839         if ((vp = un->un_lowervp) != NULLVP) {
840                 vn_lock(vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
841                 ap->a_vp = vp;
842
843                 /*
844                  * Remove VWRITE from a_mode if our mount point is RW, because
845                  * we want to allow writes and lowervp may be read-only.
846                  */
847                 if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
848                         ap->a_mode &= ~VWRITE;
849
850                 error = VCALL(vp, VOFFSET(vop_access), ap);
851                 if (error == 0) {
852                         struct union_mount *um;
853
854                         um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
855
856                         if (um->um_op == UNMNT_BELOW) {
857                                 ap->a_cred = um->um_cred;
858                                 error = VCALL(vp, VOFFSET(vop_access), ap);
859                         }
860                 }
861                 VOP_UNLOCK(vp, NULL, 0, td);
862         }
863         return(error);
864 }
865
866 /*
867  * We handle getattr only to change the fsid and
868  * track object sizes
869  *
870  * It's not clear whether VOP_GETATTR is to be
871  * called with the vnode locked or not.  stat() calls
872  * it with (vp) locked, and fstat calls it with
873  * (vp) unlocked. 
874  *
875  * Because of this we cannot use our normal locking functions
876  * if we do not intend to lock the main a_vp node.  At the moment
877  * we are running without any specific locking at all, but beware
878  * to any programmer that care must be taken if locking is added
879  * to this function.
880  *
881  * union_getattr(struct vnode *a_vp, struct vattr *a_vap,
882  *               struct ucred *a_cred, struct thread *a_td)
883  */
884 static int
885 union_getattr(struct vop_getattr_args *ap)
886 {
887         int error;
888         struct union_node *un = VTOUNION(ap->a_vp);
889         struct vnode *vp;
890         struct vattr *vap;
891         struct vattr va;
892
893         /*
894          * Some programs walk the filesystem hierarchy by counting
895          * links to directories to avoid stat'ing all the time.
896          * This means the link count on directories needs to be "correct".
897          * The only way to do that is to call getattr on both layers
898          * and fix up the link count.  The link count will not necessarily
899          * be accurate but will be large enough to defeat the tree walkers.
900          */
901
902         vap = ap->a_vap;
903
904         if ((vp = un->un_uppervp) != NULLVP) {
905                 error = VOP_GETATTR(vp, vap, ap->a_td);
906                 if (error)
907                         return (error);
908                 /* XXX isn't this dangerouso without a lock? */
909                 union_newsize(ap->a_vp, vap->va_size, VNOVAL);
910         }
911
912         if (vp == NULLVP) {
913                 vp = un->un_lowervp;
914         } else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
915                 vp = un->un_lowervp;
916                 vap = &va;
917         } else {
918                 vp = NULLVP;
919         }
920
921         if (vp != NULLVP) {
922                 error = VOP_GETATTR(vp, vap, ap->a_td);
923                 if (error)
924                         return (error);
925                 /* XXX isn't this dangerous without a lock? */
926                 union_newsize(ap->a_vp, VNOVAL, vap->va_size);
927         }
928
929         if ((vap != ap->a_vap) && (vap->va_type == VDIR))
930                 ap->a_vap->va_nlink += vap->va_nlink;
931         return (0);
932 }
933
934 /*
935  * union_setattr(struct vnode *a_vp, struct vattr *a_vap,
936  *               struct ucred *a_cred, struct thread *a_td)
937  */
938 static int
939 union_setattr(struct vop_setattr_args *ap)
940 {
941         struct union_node *un = VTOUNION(ap->a_vp);
942         struct thread *td = ap->a_td;
943         struct vattr *vap = ap->a_vap;
944         struct vnode *uppervp;
945         int error;
946
947         /*
948          * Disallow write attempts on filesystems mounted read-only.
949          */
950         if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
951             (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
952              vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
953              vap->va_mtime.tv_sec != VNOVAL || 
954              vap->va_mode != (mode_t)VNOVAL)) {
955                 return (EROFS);
956         }
957
958         /*
959          * Handle case of truncating lower object to zero size,
960          * by creating a zero length upper object.  This is to
961          * handle the case of open with O_TRUNC and O_CREAT.
962          */
963         if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
964                 error = union_copyup(un, (ap->a_vap->va_size != 0),
965                             ap->a_cred, ap->a_td);
966                 if (error)
967                         return (error);
968         }
969
970         /*
971          * Try to set attributes in upper layer,
972          * otherwise return read-only filesystem error.
973          */
974         error = EROFS;
975         if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
976                 error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
977                                         ap->a_cred, ap->a_td);
978                 if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
979                         union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
980                 union_unlock_upper(uppervp, td);
981         }
982         return (error);
983 }
984
985 /*
986  *      union_getpages:
987  */
988
989 static int
990 union_getpages(struct vop_getpages_args *ap)
991 {
992         int r;
993
994         r = vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 
995                 ap->a_count, ap->a_reqpage);
996         return(r);
997 }
998
999 /*
1000  *      union_putpages:
1001  */
1002
1003 static int
1004 union_putpages(struct vop_putpages_args *ap)
1005 {
1006         int r;
1007
1008         r = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1009                 ap->a_sync, ap->a_rtvals);
1010         return(r);
1011 }
1012
1013 /*
1014  * union_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1015  *            struct ucred *a_cred)
1016  */
1017 static int
1018 union_read(struct vop_read_args *ap)
1019 {
1020         struct union_node *un = VTOUNION(ap->a_vp);
1021         struct thread *td = ap->a_uio->uio_td;
1022         struct vnode *uvp;
1023         int error;
1024
1025         uvp = union_lock_other(un, td);
1026         KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
1027
1028         if (ap->a_vp->v_flag & VOBJBUF)
1029                 union_vm_coherency(ap->a_vp, ap->a_uio, 0);
1030
1031         error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1032         union_unlock_other(uvp, td);
1033
1034         /*
1035          * XXX
1036          * perhaps the size of the underlying object has changed under
1037          * our feet.  take advantage of the offset information present
1038          * in the uio structure.
1039          */
1040         if (error == 0) {
1041                 struct union_node *un = VTOUNION(ap->a_vp);
1042                 off_t cur = ap->a_uio->uio_offset;
1043
1044                 if (uvp == un->un_uppervp) {
1045                         if (cur > un->un_uppersz)
1046                                 union_newsize(ap->a_vp, cur, VNOVAL);
1047                 } else {
1048                         if (cur > un->un_lowersz)
1049                                 union_newsize(ap->a_vp, VNOVAL, cur);
1050                 }
1051         }
1052         return (error);
1053 }
1054
1055 /*
1056  * union_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1057  *              struct ucred *a_cred)
1058  */
1059 static int
1060 union_write(struct vop_read_args *ap)
1061 {
1062         struct union_node *un = VTOUNION(ap->a_vp);
1063         struct thread *td = ap->a_uio->uio_td;
1064         struct vnode *uppervp;
1065         int error;
1066
1067         if ((uppervp = union_lock_upper(un, td)) == NULLVP)
1068                 panic("union: missing upper layer in write");
1069
1070         /*
1071          * Since our VM pages are associated with our vnode rather then
1072          * the real vnode, and since we do not run our reads and writes 
1073          * through our own VM cache, we have a VM/VFS coherency problem. 
1074          * We solve them by invalidating or flushing the associated VM
1075          * pages prior to allowing a normal read or write to occur.
1076          *
1077          * VM-backed writes (UIO_NOCOPY) have to be converted to normal
1078          * writes because we are not cache-coherent.  Normal writes need
1079          * to be made coherent with our VM-backing store, which we do by
1080          * first flushing any dirty VM pages associated with the write
1081          * range, and then destroying any clean VM pages associated with
1082          * the write range.
1083          */
1084
1085         if (ap->a_uio->uio_segflg == UIO_NOCOPY) {
1086                 ap->a_uio->uio_segflg = UIO_SYSSPACE;
1087         } else if (ap->a_vp->v_flag & VOBJBUF) {
1088                 union_vm_coherency(ap->a_vp, ap->a_uio, 1);
1089         }
1090
1091         error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1092
1093         /*
1094          * the size of the underlying object may be changed by the
1095          * write.
1096          */
1097         if (error == 0) {
1098                 off_t cur = ap->a_uio->uio_offset;
1099
1100                 if (cur > un->un_uppersz)
1101                         union_newsize(ap->a_vp, cur, VNOVAL);
1102         }
1103         union_unlock_upper(uppervp, td);
1104         return (error);
1105 }
1106
1107 /*
1108  * union_lease(struct vnode *a_vp, struct thread *a_td, struct ucred *a_cred,
1109  *              int a_flag)
1110  */
1111 static int
1112 union_lease(struct vop_lease_args *ap)
1113 {
1114         struct vnode *ovp = OTHERVP(ap->a_vp);
1115
1116         ap->a_vp = ovp;
1117         return (VCALL(ovp, VOFFSET(vop_lease), ap));
1118 }
1119
1120 /*
1121  * union_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
1122  *              struct ucred *a_cred, struct thread *a_td)
1123  */
1124 static int
1125 union_ioctl(struct vop_ioctl_args *ap)
1126 {
1127         struct vnode *ovp = OTHERVP(ap->a_vp);
1128
1129         ap->a_vp = ovp;
1130         return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
1131 }
1132
1133 /*
1134  * union_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
1135  *            struct thread *a_td)
1136  */
1137 static int
1138 union_poll(struct vop_poll_args *ap)
1139 {
1140         struct vnode *ovp = OTHERVP(ap->a_vp);
1141
1142         ap->a_vp = ovp;
1143         return (VCALL(ovp, VOFFSET(vop_poll), ap));
1144 }
1145
1146 /*
1147  * union_revoke(struct vnode *a_vp, int a_flags, struct thread *a_td)
1148  */
1149 static int
1150 union_revoke(struct vop_revoke_args *ap)
1151 {
1152         struct vnode *vp = ap->a_vp;
1153
1154         if (UPPERVP(vp))
1155                 VOP_REVOKE(UPPERVP(vp), ap->a_flags);
1156         if (LOWERVP(vp))
1157                 VOP_REVOKE(LOWERVP(vp), ap->a_flags);
1158         vgone(vp);
1159         return (0);
1160 }
1161
1162 /*
1163  * union_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
1164  *            struct thread *a_td)
1165  */
1166 static int
1167 union_mmap(struct vop_mmap_args *ap)
1168 {
1169         struct vnode *ovp = OTHERVP(ap->a_vp);
1170
1171         ap->a_vp = ovp;
1172         return (VCALL(ovp, VOFFSET(vop_mmap), ap));
1173 }
1174
1175 /*
1176  * union_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
1177  *              struct thread *a_td)
1178  */
1179 static int
1180 union_fsync(struct vop_fsync_args *ap)
1181 {
1182         int error = 0;
1183         struct thread *td = ap->a_td;
1184         struct vnode *targetvp;
1185         struct union_node *un = VTOUNION(ap->a_vp);
1186
1187         if ((targetvp = union_lock_other(un, td)) != NULLVP) {
1188                 error = VOP_FSYNC(targetvp, ap->a_waitfor, td);
1189                 union_unlock_other(targetvp, td);
1190         }
1191
1192         return (error);
1193 }
1194
1195 /*
1196  *      union_remove:
1197  *
1198  *      Remove the specified cnp.  The dvp and vp are passed to us locked
1199  *      and must remain locked on return.
1200  *
1201  * union_remove(struct vnode *a_dvp, struct vnode *a_vp,
1202  *              struct componentname *a_cnp)
1203  */
1204 static int
1205 union_remove(struct vop_remove_args *ap)
1206 {
1207         struct union_node *dun = VTOUNION(ap->a_dvp);
1208         struct union_node *un = VTOUNION(ap->a_vp);
1209         struct componentname *cnp = ap->a_cnp;
1210         struct thread *td = cnp->cn_td;
1211         struct vnode *uppervp;
1212         struct vnode *upperdvp;
1213         int error;
1214
1215         if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1216                 panic("union remove: null upper vnode");
1217
1218         if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1219                 if (union_dowhiteout(un, cnp->cn_cred, td))
1220                         cnp->cn_flags |= CNP_DOWHITEOUT;
1221                 error = VOP_REMOVE(upperdvp, NCPNULL, uppervp, cnp);
1222 #if 0
1223                 /* XXX */
1224                 if (!error)
1225                         union_removed_upper(un);
1226 #endif
1227                 union_unlock_upper(uppervp, td);
1228         } else {
1229                 error = union_mkwhiteout(
1230                             MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1231                             upperdvp, ap->a_cnp, un->un_path);
1232         }
1233         union_unlock_upper(upperdvp, td);
1234         return (error);
1235 }
1236
1237 /*
1238  *      union_link:
1239  *
1240  *      tdvp will be locked on entry, vp will not be locked on entry.
1241  *      tdvp should remain locked on return and vp should remain unlocked
1242  *      on return.
1243  *
1244  * union_link(struct vnode *a_tdvp, struct vnode *a_vp,
1245  *            struct componentname *a_cnp)
1246  */
1247 static int
1248 union_link(struct vop_link_args *ap)
1249 {
1250         struct componentname *cnp = ap->a_cnp;
1251         struct thread *td = cnp->cn_td;
1252         struct union_node *dun = VTOUNION(ap->a_tdvp);
1253         struct vnode *vp;
1254         struct vnode *tdvp;
1255         int error = 0;
1256
1257         if (ap->a_tdvp->v_op != ap->a_vp->v_op) {
1258                 vp = ap->a_vp;
1259         } else {
1260                 struct union_node *tun = VTOUNION(ap->a_vp);
1261
1262                 if (tun->un_uppervp == NULLVP) {
1263                         vn_lock(ap->a_vp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1264 #if 0
1265                         if (dun->un_uppervp == tun->un_dirvp) {
1266                                 if (dun->un_flags & UN_ULOCK) {
1267                                         dun->un_flags &= ~UN_ULOCK;
1268                                         VOP_UNLOCK(dun->un_uppervp, NULL, 0, td);
1269                                 }
1270                         }
1271 #endif
1272                         error = union_copyup(tun, 1, cnp->cn_cred, td);
1273 #if 0
1274                         if (dun->un_uppervp == tun->un_dirvp) {
1275                                 vn_lock(dun->un_uppervp, NULL,
1276                                             LK_EXCLUSIVE | LK_RETRY, td);
1277                                 dun->un_flags |= UN_ULOCK;
1278                         }
1279 #endif
1280                         VOP_UNLOCK(ap->a_vp, NULL, 0, td);
1281                 }
1282                 vp = tun->un_uppervp;
1283         }
1284
1285         if (error)
1286                 return (error);
1287
1288         /*
1289          * Make sure upper is locked, then unlock the union directory we were 
1290          * called with to avoid a deadlock while we are calling VOP_LINK on 
1291          * the upper (with tdvp locked and vp not locked).  Our ap->a_tdvp
1292          * is expected to be locked on return.
1293          */
1294
1295         if ((tdvp = union_lock_upper(dun, td)) == NULLVP)
1296                 return (EROFS);
1297
1298         VOP_UNLOCK(ap->a_tdvp, NULL, 0, td);    /* unlock calling node */
1299         error = VOP_LINK(tdvp, NCPNULL, vp, cnp); /* call link on upper */
1300
1301         /*
1302          * We have to unlock tdvp prior to relocking our calling node in
1303          * order to avoid a deadlock.
1304          */
1305         union_unlock_upper(tdvp, td);
1306         vn_lock(ap->a_tdvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
1307         return (error);
1308 }
1309
1310 /*
1311  * union_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1312  *              struct componentname *a_fcnp, struct vnode *a_tdvp,
1313  *              struct vnode *a_tvp, struct componentname *a_tcnp)
1314  */
1315 static int
1316 union_rename(struct vop_rename_args *ap)
1317 {
1318         int error;
1319         struct vnode *fdvp = ap->a_fdvp;
1320         struct vnode *fvp = ap->a_fvp;
1321         struct vnode *tdvp = ap->a_tdvp;
1322         struct vnode *tvp = ap->a_tvp;
1323
1324         /*
1325          * Figure out what fdvp to pass to our upper or lower vnode.  If we
1326          * replace the fdvp, release the original one and ref the new one.
1327          */
1328
1329         if (fdvp->v_op == union_vnodeop_p) {    /* always true */
1330                 struct union_node *un = VTOUNION(fdvp);
1331                 if (un->un_uppervp == NULLVP) {
1332                         /*
1333                          * this should never happen in normal
1334                          * operation but might if there was
1335                          * a problem creating the top-level shadow
1336                          * directory.
1337                          */
1338                         error = EXDEV;
1339                         goto bad;
1340                 }
1341                 fdvp = un->un_uppervp;
1342                 vref(fdvp);
1343                 vrele(ap->a_fdvp);
1344         }
1345
1346         /*
1347          * Figure out what fvp to pass to our upper or lower vnode.  If we
1348          * replace the fvp, release the original one and ref the new one.
1349          */
1350
1351         if (fvp->v_op == union_vnodeop_p) {     /* always true */
1352                 struct union_node *un = VTOUNION(fvp);
1353 #if 0
1354                 struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
1355 #endif
1356
1357                 if (un->un_uppervp == NULLVP) {
1358                         switch(fvp->v_type) {
1359                         case VREG:
1360                                 vn_lock(un->un_vnode, NULL, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_td);
1361                                 error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_td);
1362                                 VOP_UNLOCK(un->un_vnode, NULL, 0, ap->a_fcnp->cn_td);
1363                                 if (error)
1364                                         goto bad;
1365                                 break;
1366                         case VDIR:
1367                                 /*
1368                                  * XXX not yet.
1369                                  *
1370                                  * There is only one way to rename a directory
1371                                  * based in the lowervp, and that is to copy
1372                                  * the entire directory hierarchy.  Otherwise
1373                                  * it would not last across a reboot.
1374                                  */
1375 #if 0
1376                                 vrele(fvp);
1377                                 fvp = NULL;
1378                                 vn_lock(fdvp, NULL, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_td);
1379                                 error = union_mkshadow(um, fdvp, 
1380                                             ap->a_fcnp, &un->un_uppervp);
1381                                 VOP_UNLOCK(fdvp, NULL, 0, ap->a_fcnp->cn_td);
1382                                 if (un->un_uppervp)
1383                                         VOP_UNLOCK(un->un_uppervp, NULL, 0, ap->a_fcnp->cn_td);
1384                                 if (error)
1385                                         goto bad;
1386                                 break;
1387 #endif
1388                         default:
1389                                 error = EXDEV;
1390                                 goto bad;
1391                         }
1392                 }
1393
1394                 if (un->un_lowervp != NULLVP)
1395                         ap->a_fcnp->cn_flags |= CNP_DOWHITEOUT;
1396                 fvp = un->un_uppervp;
1397                 vref(fvp);
1398                 vrele(ap->a_fvp);
1399         }
1400
1401         /*
1402          * Figure out what tdvp (destination directory) to pass to the
1403          * lower level.  If we replace it with uppervp, we need to vput the 
1404          * old one.  The exclusive lock is transfered to what we will pass
1405          * down in the VOP_RENAME and we replace uppervp with a simple
1406          * reference.
1407          */
1408
1409         if (tdvp->v_op == union_vnodeop_p) {
1410                 struct union_node *un = VTOUNION(tdvp);
1411
1412                 if (un->un_uppervp == NULLVP) {
1413                         /*
1414                          * this should never happen in normal
1415                          * operation but might if there was
1416                          * a problem creating the top-level shadow
1417                          * directory.
1418                          */
1419                         error = EXDEV;
1420                         goto bad;
1421                 }
1422
1423                 /*
1424                  * new tdvp is a lock and reference on uppervp, put away
1425                  * the old tdvp.
1426                  */
1427                 tdvp = union_lock_upper(un, ap->a_tcnp->cn_td);
1428                 vput(ap->a_tdvp);
1429         }
1430
1431         /*
1432          * Figure out what tvp (destination file) to pass to the
1433          * lower level.
1434          *
1435          * If the uppervp file does not exist put away the (wrong)
1436          * file and change tvp to NULL.
1437          */
1438
1439         if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1440                 struct union_node *un = VTOUNION(tvp);
1441
1442                 tvp = union_lock_upper(un, ap->a_tcnp->cn_td);
1443                 vput(ap->a_tvp);
1444                 /* note: tvp may be NULL */
1445         }
1446
1447         /*
1448          * VOP_RENAME releases/vputs prior to returning, so we have no
1449          * cleanup to do.
1450          */
1451
1452         return (VOP_RENAME(fdvp, NCPNULL, fvp, ap->a_fcnp, tdvp, NCPNULL, tvp, ap->a_tcnp));
1453
1454         /*
1455          * Error.  We still have to release / vput the various elements.
1456          */
1457
1458 bad:
1459         vrele(fdvp);
1460         if (fvp)
1461                 vrele(fvp);
1462         vput(tdvp);
1463         if (tvp != NULLVP) {
1464                 if (tvp != tdvp)
1465                         vput(tvp);
1466                 else
1467                         vrele(tvp);
1468         }
1469         return (error);
1470 }
1471
1472 /*
1473  * union_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1474  *              struct componentname *a_cnp, struct vattr *a_vap)
1475  */
1476 static int
1477 union_mkdir(struct vop_mkdir_args *ap)
1478 {
1479         struct union_node *dun = VTOUNION(ap->a_dvp);
1480         struct componentname *cnp = ap->a_cnp;
1481         struct thread *td = cnp->cn_td;
1482         struct vnode *upperdvp;
1483         int error = EROFS;
1484
1485         if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) {
1486                 struct vnode *vp;
1487
1488                 error = VOP_MKDIR(upperdvp, NCPNULL, &vp, cnp, ap->a_vap);
1489                 union_unlock_upper(upperdvp, td);
1490
1491                 if (error == 0) {
1492                         VOP_UNLOCK(vp, NULL, 0, td);
1493                         UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vp->v_usecount));
1494                         error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
1495                                 ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
1496                         UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
1497                 }
1498         }
1499         return (error);
1500 }
1501
1502 /*
1503  * union_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1504  *              struct componentname *a_cnp)
1505  */
1506 static int
1507 union_rmdir(struct vop_rmdir_args *ap)
1508 {
1509         struct union_node *dun = VTOUNION(ap->a_dvp);
1510         struct union_node *un = VTOUNION(ap->a_vp);
1511         struct componentname *cnp = ap->a_cnp;
1512         struct thread *td = cnp->cn_td;
1513         struct vnode *upperdvp;
1514         struct vnode *uppervp;
1515         int error;
1516
1517         if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1518                 panic("union rmdir: null upper vnode");
1519
1520         if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1521                 if (union_dowhiteout(un, cnp->cn_cred, td))
1522                         cnp->cn_flags |= CNP_DOWHITEOUT;
1523                 error = VOP_RMDIR(upperdvp, NCPNULL, uppervp, ap->a_cnp);
1524                 union_unlock_upper(uppervp, td);
1525         } else {
1526                 error = union_mkwhiteout(
1527                             MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1528                             dun->un_uppervp, ap->a_cnp, un->un_path);
1529         }
1530         union_unlock_upper(upperdvp, td);
1531         return (error);
1532 }
1533
1534 /*
1535  *      union_symlink:
1536  *
1537  *      dvp is locked on entry and remains locked on return.  a_vpp is garbage
1538  *      (unused).
1539  *
1540  * union_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1541  *              struct componentname *a_cnp, struct vattr *a_vap,
1542  *              char *a_target)
1543  */
1544 static int
1545 union_symlink(struct vop_symlink_args *ap)
1546 {
1547         struct union_node *dun = VTOUNION(ap->a_dvp);
1548         struct componentname *cnp = ap->a_cnp;
1549         struct thread *td = cnp->cn_td;
1550         struct vnode *dvp;
1551         int error = EROFS;
1552
1553         if ((dvp = union_lock_upper(dun, td)) != NULLVP) {
1554                 error = VOP_SYMLINK(dvp, NCPNULL, ap->a_vpp, cnp, ap->a_vap,
1555                             ap->a_target);
1556                 union_unlock_upper(dvp, td);
1557         }
1558         return (error);
1559 }
1560
1561 /*
1562  * union_readdir works in concert with getdirentries and
1563  * readdir(3) to provide a list of entries in the unioned
1564  * directories.  getdirentries is responsible for walking
1565  * down the union stack.  readdir(3) is responsible for
1566  * eliminating duplicate names from the returned data stream.
1567  *
1568  * union_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1569  *               int *a_eofflag, u_long *a_cookies, int a_ncookies)
1570  */
1571 static int
1572 union_readdir(struct vop_readdir_args *ap)
1573 {
1574         struct union_node *un = VTOUNION(ap->a_vp);
1575         struct thread *td = ap->a_uio->uio_td;
1576         struct vnode *uvp;
1577         int error = 0;
1578
1579         if ((uvp = union_lock_upper(un, td)) != NULLVP) {
1580                 ap->a_vp = uvp;
1581                 error = VCALL(uvp, VOFFSET(vop_readdir), ap);
1582                 union_unlock_upper(uvp, td);
1583         }
1584         return(error);
1585 }
1586
1587 /*
1588  * union_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1589  */
1590 static int
1591 union_readlink(struct vop_readlink_args *ap)
1592 {
1593         int error;
1594         struct union_node *un = VTOUNION(ap->a_vp);
1595         struct uio *uio = ap->a_uio;
1596         struct thread *td = uio->uio_td;
1597         struct vnode *vp;
1598
1599         vp = union_lock_other(un, td);
1600         KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
1601
1602         ap->a_vp = vp;
1603         error = VCALL(vp, VOFFSET(vop_readlink), ap);
1604         union_unlock_other(vp, td);
1605
1606         return (error);
1607 }
1608
1609 /*
1610  *      union_inactive:
1611  *
1612  *      Called with the vnode locked.  We are expected to unlock the vnode.
1613  *
1614  * union_inactive(struct vnode *a_vp, struct thread *a_td)
1615  */
1616 static int
1617 union_inactive(struct vop_inactive_args *ap)
1618 {
1619         struct vnode *vp = ap->a_vp;
1620         struct thread *td = ap->a_td;
1621         struct union_node *un = VTOUNION(vp);
1622         struct vnode **vpp;
1623
1624         /*
1625          * Do nothing (and _don't_ bypass).
1626          * Wait to vrele lowervp until reclaim,
1627          * so that until then our union_node is in the
1628          * cache and reusable.
1629          *
1630          * NEEDSWORK: Someday, consider inactive'ing
1631          * the lowervp and then trying to reactivate it
1632          * with capabilities (v_id)
1633          * like they do in the name lookup cache code.
1634          * That's too much work for now.
1635          */
1636
1637         if (un->un_dircache != 0) {
1638                 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1639                         vrele(*vpp);
1640                 free (un->un_dircache, M_TEMP);
1641                 un->un_dircache = 0;
1642         }
1643
1644 #if 0
1645         if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
1646                 un->un_flags &= ~UN_ULOCK;
1647                 VOP_UNLOCK(un->un_uppervp, NULL, 0, td);
1648         }
1649 #endif
1650
1651         VOP_UNLOCK(vp, NULL, 0, td);
1652
1653         if ((un->un_flags & UN_CACHED) == 0)
1654                 vgone(vp);
1655
1656         return (0);
1657 }
1658
1659 /*
1660  * union_reclaim(struct vnode *a_vp)
1661  */
1662 static int
1663 union_reclaim(struct vop_reclaim_args *ap)
1664 {
1665         union_freevp(ap->a_vp);
1666
1667         return (0);
1668 }
1669
1670 static int
1671 union_lock(struct vop_lock_args *ap)
1672 {
1673 #if 0
1674         struct vnode *vp = ap->a_vp;
1675         struct thread *td = ap->a_td;
1676         int flags = ap->a_flags;
1677         struct union_node *un;
1678 #endif
1679         int error;
1680
1681         error = vop_stdlock(ap);
1682 #if 0
1683         un = VTOUNION(vp);
1684
1685         if (error == 0) {
1686                 /*
1687                  * Lock the upper if it exists and this is an exclusive lock
1688                  * request.
1689                  */
1690                 if (un->un_uppervp != NULLVP && 
1691                     (flags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1692                         if ((un->un_flags & UN_ULOCK) == 0 && vp->v_usecount) {
1693                                 error = vn_lock(un->un_uppervp, ap->a_vlock,
1694                                                 flags, td);
1695                                 if (error) {
1696                                         struct vop_unlock_args uap = { 0 };
1697                                         uap.a_vp = ap->a_vp;
1698                                         uap.a_vlock = ap->a_vlock;
1699                                         uap.a_flags = ap->a_flags;
1700                                         uap.a_td = ap->a_td;
1701                                         vop_stdunlock(&uap);
1702                                         return (error);
1703                                 }
1704                                 un->un_flags |= UN_ULOCK;
1705                         }
1706                 }
1707         }
1708 #endif
1709         return (error);
1710 }
1711
1712 /*
1713  *      union_unlock:
1714  *
1715  *      Unlock our union node.  This also unlocks uppervp.  
1716  *
1717  * union_unlock(struct vnode *a_vp, lwkt_tokref_t a_vlock, int a_flags,
1718  *              struct thread *a_td)
1719  */
1720 static int
1721 union_unlock(struct vop_unlock_args *ap)
1722 {
1723         int error;
1724 #if 0
1725         struct union_node *un = VTOUNION(ap->a_vp);
1726
1727         KASSERT((un->un_uppervp == NULL || un->un_uppervp->v_usecount > 0), ("uppervp usecount is 0"));
1728 #endif
1729
1730         error = vop_stdunlock(ap);
1731 #if 0
1732
1733         /*
1734          * If no exclusive locks remain and we are holding an uppervp lock,
1735          * remove the uppervp lock.
1736          */
1737
1738         if ((un->un_flags & UN_ULOCK) && 
1739             lockstatus(&un->un_lock, NULL) != LK_EXCLUSIVE) {
1740                 un->un_flags &= ~UN_ULOCK;
1741                 VOP_UNLOCK(un->un_uppervp, NULL, LK_EXCLUSIVE, td);
1742         }
1743 #endif
1744         return(error);
1745 }
1746
1747 /*
1748  *      union_bmap:
1749  *
1750  *      There isn't much we can do.  We cannot push through to the real vnode
1751  *      to get to the underlying device because this will bypass data
1752  *      cached by the real vnode.
1753  *
1754  *      For some reason we cannot return the 'real' vnode either, it seems
1755  *      to blow up memory maps.
1756  *
1757  * union_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
1758  *            daddr_t *a_bnp, int *a_runp, int *a_runb)
1759  */
1760 static int
1761 union_bmap(struct vop_bmap_args *ap)
1762 {
1763         return(EOPNOTSUPP);
1764 }
1765
1766 /*
1767  * union_print(struct vnode *a_vp)
1768  */
1769 static int
1770 union_print(struct vop_print_args *ap)
1771 {
1772         struct vnode *vp = ap->a_vp;
1773
1774         printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1775                         vp, UPPERVP(vp), LOWERVP(vp));
1776         if (UPPERVP(vp) != NULLVP)
1777                 vprint("union: upper", UPPERVP(vp));
1778         if (LOWERVP(vp) != NULLVP)
1779                 vprint("union: lower", LOWERVP(vp));
1780
1781         return (0);
1782 }
1783
1784 /*
1785  * union_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1786  */
1787 static int
1788 union_pathconf(struct vop_pathconf_args *ap)
1789 {
1790         int error;
1791         struct thread *td = curthread;          /* XXX */
1792         struct union_node *un = VTOUNION(ap->a_vp);
1793         struct vnode *vp;
1794
1795         vp = union_lock_other(un, td);
1796         KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
1797
1798         ap->a_vp = vp;
1799         error = VCALL(vp, VOFFSET(vop_pathconf), ap);
1800         union_unlock_other(vp, td);
1801
1802         return (error);
1803 }
1804
1805 /*
1806  * union_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1807  *               struct flock *a_fl, int a_flags)
1808  */
1809 static int
1810 union_advlock(struct vop_advlock_args *ap)
1811 {
1812         struct vnode *ovp = OTHERVP(ap->a_vp);
1813
1814         ap->a_vp = ovp;
1815         return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1816 }
1817
1818
1819 /*
1820  * XXX - vop_strategy must be hand coded because it has no
1821  * YYY - and it is not coherent with anything
1822  *
1823  * vnode in its arguments.
1824  * This goes away with a merged VM/buffer cache.
1825  *
1826  * union_strategy(struct vnode *a_vp, struct buf *a_bp)
1827  */
1828 static int
1829 union_strategy(struct vop_strategy_args *ap)
1830 {
1831         struct buf *bp = ap->a_bp;
1832         struct vnode *othervp = OTHERVP(bp->b_vp);
1833
1834 #ifdef DIAGNOSTIC
1835         if (othervp == NULLVP)
1836                 panic("union_strategy: nil vp");
1837         if (((bp->b_flags & B_READ) == 0) &&
1838             (othervp == LOWERVP(bp->b_vp)))
1839                 panic("union_strategy: writing to lowervp");
1840 #endif
1841         return (VOP_STRATEGY(othervp, bp));
1842 }
1843
1844 /*
1845  * Global vfs data structures
1846  */
1847 vop_t **union_vnodeop_p;
1848 static struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1849         { &vop_default_desc,            (vop_t *) vop_defaultop },
1850         { &vop_access_desc,             (vop_t *) union_access },
1851         { &vop_advlock_desc,            (vop_t *) union_advlock },
1852         { &vop_bmap_desc,               (vop_t *) union_bmap },
1853         { &vop_close_desc,              (vop_t *) union_close },
1854         { &vop_create_desc,             (vop_t *) union_create },
1855         { &vop_fsync_desc,              (vop_t *) union_fsync },
1856         { &vop_getpages_desc,           (vop_t *) union_getpages },
1857         { &vop_putpages_desc,           (vop_t *) union_putpages },
1858         { &vop_getattr_desc,            (vop_t *) union_getattr },
1859         { &vop_inactive_desc,           (vop_t *) union_inactive },
1860         { &vop_ioctl_desc,              (vop_t *) union_ioctl },
1861         { &vop_islocked_desc,           (vop_t *) vop_stdislocked },
1862         { &vop_lease_desc,              (vop_t *) union_lease },
1863         { &vop_link_desc,               (vop_t *) union_link },
1864         { &vop_lock_desc,               (vop_t *) union_lock },
1865         { &vop_lookup_desc,             (vop_t *) union_lookup },
1866         { &vop_mkdir_desc,              (vop_t *) union_mkdir },
1867         { &vop_mknod_desc,              (vop_t *) union_mknod },
1868         { &vop_mmap_desc,               (vop_t *) union_mmap },
1869         { &vop_open_desc,               (vop_t *) union_open },
1870         { &vop_pathconf_desc,           (vop_t *) union_pathconf },
1871         { &vop_poll_desc,               (vop_t *) union_poll },
1872         { &vop_print_desc,              (vop_t *) union_print },
1873         { &vop_read_desc,               (vop_t *) union_read },
1874         { &vop_readdir_desc,            (vop_t *) union_readdir },
1875         { &vop_readlink_desc,           (vop_t *) union_readlink },
1876         { &vop_reclaim_desc,            (vop_t *) union_reclaim },
1877         { &vop_remove_desc,             (vop_t *) union_remove },
1878         { &vop_rename_desc,             (vop_t *) union_rename },
1879         { &vop_revoke_desc,             (vop_t *) union_revoke },
1880         { &vop_rmdir_desc,              (vop_t *) union_rmdir },
1881         { &vop_setattr_desc,            (vop_t *) union_setattr },
1882         { &vop_strategy_desc,           (vop_t *) union_strategy },
1883         { &vop_symlink_desc,            (vop_t *) union_symlink },
1884         { &vop_unlock_desc,             (vop_t *) union_unlock },
1885         { &vop_whiteout_desc,           (vop_t *) union_whiteout },
1886         { &vop_write_desc,              (vop_t *) union_write },
1887         { NULL, NULL }
1888 };
1889 static struct vnodeopv_desc union_vnodeop_opv_desc =
1890         { &union_vnodeop_p, union_vnodeop_entries };
1891
1892 VNODEOP_SET(union_vnodeop_opv_desc);