VFS messaging/interfacing work stage 9/99: VFS 'NEW' API WORK.
[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.17 2004/11/12 00:09:55 dillon 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, 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, 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, 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, &tdvp, 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 0
233         if (dvp != tdvp && (cnp->cn_flags & CNP_XXXISLASTCN) == 0)
234                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, td);
235 #endif
236
237         /*
238          * Lastly check if the current node is a mount point in
239          * which case walk up the mount hierarchy making sure not to
240          * bump into the root of the mount tree (ie. dvp != udvp).
241          *
242          * We use dvp as a temporary variable here, it is no longer related
243          * to the dvp above.  However, we have to ensure that both *pdvp and
244          * tdvp are locked on return.
245          */
246
247         dvp = tdvp;
248         while (
249             dvp != udvp && 
250             (dvp->v_type == VDIR) &&
251             (mp = dvp->v_mountedhere)
252         ) {
253                 int relock_pdvp = 0;
254
255                 if (vfs_busy(mp, 0, NULL, td))
256                         continue;
257
258                 if (dvp == *pdvp)
259                         relock_pdvp = 1;
260                 vput(dvp);
261                 dvp = NULL;
262                 error = VFS_ROOT(mp, &dvp);
263
264                 vfs_unbusy(mp, td);
265
266                 if (relock_pdvp)
267                         vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY, td);
268
269                 if (error) {
270                         *vpp = NULL;
271                         return (error);
272                 }
273         }
274         *vpp = dvp;
275         return (0);
276 }
277
278 /*
279  * union_lookup(struct vnodeop_desc *a_desc, struct vnode *a_dvp,
280  *              struct vnode **a_vpp, struct componentname *a_cnp)
281  */
282 static int
283 union_lookup(struct vop_lookup_args *ap)
284 {
285         int error;
286         int uerror, lerror;
287         struct vnode *uppervp, *lowervp;
288         struct vnode *upperdvp, *lowerdvp;
289         struct vnode *dvp = ap->a_dvp;          /* starting dir */
290         struct union_node *dun = VTOUNION(dvp); /* associated union node */
291         struct componentname *cnp = ap->a_cnp;
292         struct thread *td = cnp->cn_td;
293         int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
294         struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
295         struct ucred *saved_cred = NULL;
296         int iswhiteout;
297         struct vattr va;
298
299         *ap->a_vpp = NULLVP;
300
301         /*
302          * Disallow write attemps to the filesystem mounted read-only.
303          */
304         if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
305             (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) {
306                 return (EROFS);
307         }
308
309         /*
310          * For any lookup's we do, always return with the parent locked
311          */
312         cnp->cn_flags |= CNP_LOCKPARENT;
313
314         lowerdvp = dun->un_lowervp;
315         uppervp = NULLVP;
316         lowervp = NULLVP;
317         iswhiteout = 0;
318
319         uerror = ENOENT;
320         lerror = ENOENT;
321
322         /*
323          * Get a private lock on uppervp and a reference, effectively 
324          * taking it out of the union_node's control.
325          *
326          * We must lock upperdvp while holding our lock on dvp
327          * to avoid a deadlock.
328          */
329         upperdvp = union_lock_upper(dun, td);
330
331         /*
332          * do the lookup in the upper level.
333          * if that level comsumes additional pathnames,
334          * then assume that something special is going
335          * on and just return that vnode.
336          */
337         if (upperdvp != NULLVP) {
338                 /*
339                  * We do not have to worry about the DOTDOT case, we've
340                  * already unlocked dvp.
341                  */
342                 UDEBUG(("A %p\n", upperdvp));
343
344                 /*
345                  * Do the lookup.   We must supply a locked and referenced
346                  * upperdvp to the function and will get a new locked and
347                  * referenced upperdvp back with the old having been 
348                  * dereferenced.
349                  *
350                  * If an error is returned, uppervp will be NULLVP.  If no
351                  * error occurs, uppervp will be the locked and referenced
352                  * return vnode or possibly NULL, depending on what is being
353                  * requested.  It is possible that the returned uppervp
354                  * will be the same as upperdvp.
355                  */
356                 uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
357                 UDEBUG((
358                     "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
359                     uerror,
360                     upperdvp,
361                     upperdvp->v_usecount,
362                     VOP_ISLOCKED(upperdvp, NULL),
363                     uppervp,
364                     (uppervp ? uppervp->v_usecount : -99),
365                     (uppervp ? VOP_ISLOCKED(uppervp, NULL) : -99)
366                 ));
367
368                 /*
369                  * Disallow write attemps to the filesystem mounted read-only.
370                  */
371                 if (uerror == EJUSTRETURN && 
372                     (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
373                     (cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME)) {
374                         error = EROFS;
375                         goto out;
376                 }
377
378                 /*
379                  * Special case.  If cn_consume != 0 skip out.  The result
380                  * of the lookup is transfered to our return variable.  If
381                  * an error occured we have to throw away the results.
382                  */
383
384                 if (cnp->cn_consume != 0) {
385                         if ((error = uerror) == 0) {
386                                 *ap->a_vpp = uppervp;
387                                 uppervp = NULL;
388                         }
389                         goto out;
390                 }
391
392                 /*
393                  * Calculate whiteout, fall through
394                  */
395
396                 if (uerror == ENOENT || uerror == EJUSTRETURN) {
397                         if (cnp->cn_flags & CNP_ISWHITEOUT) {
398                                 iswhiteout = 1;
399                         } else if (lowerdvp != NULLVP) {
400                                 int terror;
401
402                                 terror = VOP_GETATTR(upperdvp, &va, cnp->cn_td);
403                                 if (terror == 0 && (va.va_flags & OPAQUE))
404                                         iswhiteout = 1;
405                         }
406                 }
407         }
408
409         /*
410          * in a similar way to the upper layer, do the lookup
411          * in the lower layer.   this time, if there is some
412          * component magic going on, then vput whatever we got
413          * back from the upper layer and return the lower vnode
414          * instead.
415          */
416
417         if (lowerdvp != NULLVP && !iswhiteout) {
418                 int nameiop;
419
420                 UDEBUG(("B %p\n", lowerdvp));
421
422                 /*
423                  * Force only LOOKUPs on the lower node, since
424                  * we won't be making changes to it anyway.
425                  */
426                 nameiop = cnp->cn_nameiop;
427                 cnp->cn_nameiop = NAMEI_LOOKUP;
428                 if (um->um_op == UNMNT_BELOW) {
429                         saved_cred = cnp->cn_cred;
430                         cnp->cn_cred = um->um_cred;
431                 }
432
433                 /*
434                  * We shouldn't have to worry about locking interactions
435                  * between the lower layer and our union layer (w.r.t.
436                  * `..' processing) because we don't futz with lowervp
437                  * locks in the union-node instantiation code path.
438                  *
439                  * union_lookup1() requires lowervp to be locked on entry,
440                  * and it will be unlocked on return.  The ref count will
441                  * not change.  On return lowervp doesn't represent anything
442                  * to us so we NULL it out.
443                  */
444                 vref(lowerdvp);
445                 vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY, td);
446                 lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
447                 if (lowerdvp == lowervp)
448                         vrele(lowerdvp);
449                 else
450                         vput(lowerdvp);
451                 lowerdvp = NULL;        /* lowerdvp invalid after vput */
452
453                 if (um->um_op == UNMNT_BELOW)
454                         cnp->cn_cred = saved_cred;
455                 cnp->cn_nameiop = nameiop;
456
457                 if (cnp->cn_consume != 0 || lerror == EACCES) {
458                         if ((error = lerror) == 0) {
459                                 *ap->a_vpp = lowervp;
460                                 lowervp = NULL;
461                         }
462                         goto out;
463                 }
464         } else {
465                 UDEBUG(("C %p\n", lowerdvp));
466                 if ((cnp->cn_flags & CNP_ISDOTDOT) && dun->un_pvp != NULLVP) {
467                         if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
468                                 vref(lowervp);
469                                 vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY, td);
470                                 lerror = 0;
471                         }
472                 }
473         }
474
475         /*
476          * Ok.  Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
477          *
478          * 1. If both layers returned an error, select the upper layer.
479          *
480          * 2. If the upper layer faile and the bottom layer succeeded,
481          *    two subcases occur:
482          *
483          *      a.  The bottom vnode is not a directory, in which case
484          *          just return a new union vnode referencing an
485          *          empty top layer and the existing bottom layer.
486          *
487          *      b.  The button vnode is a directory, in which case
488          *          create a new directory in the top layer and
489          *          and fall through to case 3.
490          *
491          * 3. If the top layer succeeded then return a new union
492          *    vnode referencing whatever the new top layer and
493          *    whatever the bottom layer returned.
494          */
495
496         /* case 1. */
497         if ((uerror != 0) && (lerror != 0)) {
498                 error = uerror;
499                 goto out;
500         }
501
502         /* case 2. */
503         if (uerror != 0 /* && (lerror == 0) */ ) {
504                 if (lowervp->v_type == VDIR) { /* case 2b. */
505                         KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
506                         /*
507                          * oops, uppervp has a problem, we may have to shadow.
508                          */
509                         uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
510                         if (uerror) {
511                                 error = uerror;
512                                 goto out;
513                         }
514                 }
515         }
516
517         /*
518          * Must call union_allocvp with both the upper and lower vnodes
519          * referenced and the upper vnode locked.   ap->a_vpp is returned 
520          * referenced and locked.  lowervp, uppervp, and upperdvp are 
521          * absorbed by union_allocvp() whether it succeeds or fails.
522          *
523          * upperdvp is the parent directory of uppervp which may be
524          * different, depending on the path, from dvp->un_uppervp.  That's
525          * why it is a separate argument.  Note that it must be unlocked.
526          *
527          * dvp must be locked on entry to the call and will be locked on
528          * return.
529          */
530
531         if (uppervp && uppervp != upperdvp)
532                 VOP_UNLOCK(uppervp, 0, td);
533         if (lowervp)
534                 VOP_UNLOCK(lowervp, 0, td);
535         if (upperdvp)
536                 VOP_UNLOCK(upperdvp, 0, td);
537
538         error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
539                               uppervp, lowervp, 1);
540
541         UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? ((*ap->a_vpp)->v_usecount) : -99));
542
543         uppervp = NULL;
544         upperdvp = NULL;
545         lowervp = NULL;
546
547         /* 
548          *      Termination Code
549          *
550          *      - put away any extra junk laying around.  Note that lowervp
551          *        (if not NULL) will never be the same as *ap->a_vp and 
552          *        neither will uppervp, because when we set that state we 
553          *        NULL-out lowervp or uppervp.  On the otherhand, upperdvp
554          *        may match uppervp or *ap->a_vpp.
555          *
556          *      - relock/unlock dvp if appropriate.
557          */
558
559 out:
560         if (upperdvp) {
561                 if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
562                         vrele(upperdvp);
563                 else
564                         vput(upperdvp);
565         }
566
567         if (uppervp)
568                 vput(uppervp);
569
570         if (lowervp)
571                 vput(lowervp);
572
573         /*
574          * Restore LOCKPARENT state
575          */
576
577         if (!lockparent)
578                 cnp->cn_flags &= ~CNP_LOCKPARENT;
579
580         UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
581                 ((*ap->a_vpp) ? (*ap->a_vpp)->v_usecount : -99),
582                 lowervp, uppervp));
583
584         /*
585          * dvp lock state, determine whether to relock dvp.  dvp is expected
586          * to be locked on return if:
587          *
588          *      - there was an error (except not EJUSTRETURN), or
589          *      - we hit the last component and lockparent is true
590          *
591          * dvp_is_locked is the current state of the dvp lock, not counting
592          * the possibility that *ap->a_vpp == dvp (in which case it is locked
593          * anyway).  Note that *ap->a_vpp == dvp only if no error occured.
594          */
595
596         if (*ap->a_vpp != dvp) {
597                 if ((error == 0 || error == EJUSTRETURN) && !lockparent) {
598                         VOP_UNLOCK(dvp, 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, &vp, cnp, ap->a_vap);
640                 if (error == 0) {
641                         mp = ap->a_dvp->v_mount;
642                         VOP_UNLOCK(vp, 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, 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, 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, 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, NULL, 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_head.a_ops = vp->v_ops;
795         ap->a_vp = vp;
796         return(vop_close_ap(ap));
797 }
798
799 /*
800  * Check access permission on the union vnode.
801  * The access check being enforced is to check
802  * against both the underlying vnode, and any
803  * copied vnode.  This ensures that no additional
804  * file permissions are given away simply because
805  * the user caused an implicit file copy.
806  *
807  * union_access(struct vnodeop_desc *a_desc, struct vnode *a_vp, int a_mode,
808  *              struct ucred *a_cred, struct thread *a_td)
809  */
810 static int
811 union_access(struct vop_access_args *ap)
812 {
813         struct union_node *un = VTOUNION(ap->a_vp);
814         struct thread *td = ap->a_td;
815         int error = EACCES;
816         struct vnode *vp;
817
818         /*
819          * Disallow write attempts on filesystems mounted read-only.
820          */
821         if ((ap->a_mode & VWRITE) && 
822             (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
823                 switch (ap->a_vp->v_type) {
824                 case VREG: 
825                 case VDIR:
826                 case VLNK:
827                         return (EROFS);
828                 default:
829                         break;
830                 }
831         }
832
833         if ((vp = union_lock_upper(un, td)) != NULLVP) {
834                 ap->a_head.a_ops = vp->v_ops;
835                 ap->a_vp = vp;
836                 error = vop_access_ap(ap);
837                 union_unlock_upper(vp, td);
838                 return(error);
839         }
840
841         if ((vp = un->un_lowervp) != NULLVP) {
842                 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
843                 ap->a_head.a_ops = vp->v_ops;
844                 ap->a_vp = vp;
845
846                 /*
847                  * Remove VWRITE from a_mode if our mount point is RW, because
848                  * we want to allow writes and lowervp may be read-only.
849                  */
850                 if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
851                         ap->a_mode &= ~VWRITE;
852
853                 error = vop_access_ap(ap);
854                 if (error == 0) {
855                         struct union_mount *um;
856
857                         um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
858
859                         if (um->um_op == UNMNT_BELOW) {
860                                 ap->a_cred = um->um_cred;
861                                 error = vop_access_ap(ap);
862                         }
863                 }
864                 VOP_UNLOCK(vp, 0, td);
865         }
866         return(error);
867 }
868
869 /*
870  * We handle getattr only to change the fsid and
871  * track object sizes
872  *
873  * It's not clear whether VOP_GETATTR is to be
874  * called with the vnode locked or not.  stat() calls
875  * it with (vp) locked, and fstat calls it with
876  * (vp) unlocked. 
877  *
878  * Because of this we cannot use our normal locking functions
879  * if we do not intend to lock the main a_vp node.  At the moment
880  * we are running without any specific locking at all, but beware
881  * to any programmer that care must be taken if locking is added
882  * to this function.
883  *
884  * union_getattr(struct vnode *a_vp, struct vattr *a_vap,
885  *               struct ucred *a_cred, struct thread *a_td)
886  */
887 static int
888 union_getattr(struct vop_getattr_args *ap)
889 {
890         int error;
891         struct union_node *un = VTOUNION(ap->a_vp);
892         struct vnode *vp;
893         struct vattr *vap;
894         struct vattr va;
895
896         /*
897          * Some programs walk the filesystem hierarchy by counting
898          * links to directories to avoid stat'ing all the time.
899          * This means the link count on directories needs to be "correct".
900          * The only way to do that is to call getattr on both layers
901          * and fix up the link count.  The link count will not necessarily
902          * be accurate but will be large enough to defeat the tree walkers.
903          */
904
905         vap = ap->a_vap;
906
907         if ((vp = un->un_uppervp) != NULLVP) {
908                 error = VOP_GETATTR(vp, vap, ap->a_td);
909                 if (error)
910                         return (error);
911                 /* XXX isn't this dangerouso without a lock? */
912                 union_newsize(ap->a_vp, vap->va_size, VNOVAL);
913         }
914
915         if (vp == NULLVP) {
916                 vp = un->un_lowervp;
917         } else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
918                 vp = un->un_lowervp;
919                 vap = &va;
920         } else {
921                 vp = NULLVP;
922         }
923
924         if (vp != NULLVP) {
925                 error = VOP_GETATTR(vp, vap, ap->a_td);
926                 if (error)
927                         return (error);
928                 /* XXX isn't this dangerous without a lock? */
929                 union_newsize(ap->a_vp, VNOVAL, vap->va_size);
930         }
931
932         if ((vap != ap->a_vap) && (vap->va_type == VDIR))
933                 ap->a_vap->va_nlink += vap->va_nlink;
934         return (0);
935 }
936
937 /*
938  * union_setattr(struct vnode *a_vp, struct vattr *a_vap,
939  *               struct ucred *a_cred, struct thread *a_td)
940  */
941 static int
942 union_setattr(struct vop_setattr_args *ap)
943 {
944         struct union_node *un = VTOUNION(ap->a_vp);
945         struct thread *td = ap->a_td;
946         struct vattr *vap = ap->a_vap;
947         struct vnode *uppervp;
948         int error;
949
950         /*
951          * Disallow write attempts on filesystems mounted read-only.
952          */
953         if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
954             (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
955              vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
956              vap->va_mtime.tv_sec != VNOVAL || 
957              vap->va_mode != (mode_t)VNOVAL)) {
958                 return (EROFS);
959         }
960
961         /*
962          * Handle case of truncating lower object to zero size,
963          * by creating a zero length upper object.  This is to
964          * handle the case of open with O_TRUNC and O_CREAT.
965          */
966         if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
967                 error = union_copyup(un, (ap->a_vap->va_size != 0),
968                             ap->a_cred, ap->a_td);
969                 if (error)
970                         return (error);
971         }
972
973         /*
974          * Try to set attributes in upper layer,
975          * otherwise return read-only filesystem error.
976          */
977         error = EROFS;
978         if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
979                 error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
980                                         ap->a_cred, ap->a_td);
981                 if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
982                         union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
983                 union_unlock_upper(uppervp, td);
984         }
985         return (error);
986 }
987
988 /*
989  *      union_getpages:
990  */
991
992 static int
993 union_getpages(struct vop_getpages_args *ap)
994 {
995         int r;
996
997         r = vnode_pager_generic_getpages(ap->a_vp, ap->a_m, 
998                 ap->a_count, ap->a_reqpage);
999         return(r);
1000 }
1001
1002 /*
1003  *      union_putpages:
1004  */
1005
1006 static int
1007 union_putpages(struct vop_putpages_args *ap)
1008 {
1009         int r;
1010
1011         r = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1012                 ap->a_sync, ap->a_rtvals);
1013         return(r);
1014 }
1015
1016 /*
1017  * union_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1018  *            struct ucred *a_cred)
1019  */
1020 static int
1021 union_read(struct vop_read_args *ap)
1022 {
1023         struct union_node *un = VTOUNION(ap->a_vp);
1024         struct thread *td = ap->a_uio->uio_td;
1025         struct vnode *uvp;
1026         int error;
1027
1028         uvp = union_lock_other(un, td);
1029         KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
1030
1031         if (ap->a_vp->v_flag & VOBJBUF)
1032                 union_vm_coherency(ap->a_vp, ap->a_uio, 0);
1033
1034         error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1035         union_unlock_other(uvp, td);
1036
1037         /*
1038          * XXX
1039          * perhaps the size of the underlying object has changed under
1040          * our feet.  take advantage of the offset information present
1041          * in the uio structure.
1042          */
1043         if (error == 0) {
1044                 struct union_node *un = VTOUNION(ap->a_vp);
1045                 off_t cur = ap->a_uio->uio_offset;
1046
1047                 if (uvp == un->un_uppervp) {
1048                         if (cur > un->un_uppersz)
1049                                 union_newsize(ap->a_vp, cur, VNOVAL);
1050                 } else {
1051                         if (cur > un->un_lowersz)
1052                                 union_newsize(ap->a_vp, VNOVAL, cur);
1053                 }
1054         }
1055         return (error);
1056 }
1057
1058 /*
1059  * union_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1060  *              struct ucred *a_cred)
1061  */
1062 static int
1063 union_write(struct vop_read_args *ap)
1064 {
1065         struct union_node *un = VTOUNION(ap->a_vp);
1066         struct thread *td = ap->a_uio->uio_td;
1067         struct vnode *uppervp;
1068         int error;
1069
1070         if ((uppervp = union_lock_upper(un, td)) == NULLVP)
1071                 panic("union: missing upper layer in write");
1072
1073         /*
1074          * Since our VM pages are associated with our vnode rather then
1075          * the real vnode, and since we do not run our reads and writes 
1076          * through our own VM cache, we have a VM/VFS coherency problem. 
1077          * We solve them by invalidating or flushing the associated VM
1078          * pages prior to allowing a normal read or write to occur.
1079          *
1080          * VM-backed writes (UIO_NOCOPY) have to be converted to normal
1081          * writes because we are not cache-coherent.  Normal writes need
1082          * to be made coherent with our VM-backing store, which we do by
1083          * first flushing any dirty VM pages associated with the write
1084          * range, and then destroying any clean VM pages associated with
1085          * the write range.
1086          */
1087
1088         if (ap->a_uio->uio_segflg == UIO_NOCOPY) {
1089                 ap->a_uio->uio_segflg = UIO_SYSSPACE;
1090         } else if (ap->a_vp->v_flag & VOBJBUF) {
1091                 union_vm_coherency(ap->a_vp, ap->a_uio, 1);
1092         }
1093
1094         error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1095
1096         /*
1097          * the size of the underlying object may be changed by the
1098          * write.
1099          */
1100         if (error == 0) {
1101                 off_t cur = ap->a_uio->uio_offset;
1102
1103                 if (cur > un->un_uppersz)
1104                         union_newsize(ap->a_vp, cur, VNOVAL);
1105         }
1106         union_unlock_upper(uppervp, td);
1107         return (error);
1108 }
1109
1110 /*
1111  * union_lease(struct vnode *a_vp, struct thread *a_td, struct ucred *a_cred,
1112  *              int a_flag)
1113  */
1114 static int
1115 union_lease(struct vop_lease_args *ap)
1116 {
1117         struct vnode *ovp = OTHERVP(ap->a_vp);
1118
1119         ap->a_head.a_ops = ovp->v_ops;
1120         ap->a_vp = ovp;
1121         return (vop_lease_ap(ap));
1122 }
1123
1124 /*
1125  * union_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
1126  *              struct ucred *a_cred, struct thread *a_td)
1127  */
1128 static int
1129 union_ioctl(struct vop_ioctl_args *ap)
1130 {
1131         struct vnode *ovp = OTHERVP(ap->a_vp);
1132
1133         ap->a_head.a_ops = ovp->v_ops;
1134         ap->a_vp = ovp;
1135         return(vop_ioctl_ap(ap));
1136 }
1137
1138 /*
1139  * union_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
1140  *            struct thread *a_td)
1141  */
1142 static int
1143 union_poll(struct vop_poll_args *ap)
1144 {
1145         struct vnode *ovp = OTHERVP(ap->a_vp);
1146
1147         ap->a_head.a_ops = ovp->v_ops;
1148         ap->a_vp = ovp;
1149         return(vop_poll_ap(ap));
1150 }
1151
1152 /*
1153  * union_revoke(struct vnode *a_vp, int a_flags, struct thread *a_td)
1154  */
1155 static int
1156 union_revoke(struct vop_revoke_args *ap)
1157 {
1158         struct vnode *vp = ap->a_vp;
1159         struct vnode *vx;
1160
1161         if ((vx = UPPERVP(vp)) != NULL) {
1162                 if (vx_get(vx) == 0) {
1163                         VOP_REVOKE(vx, ap->a_flags);
1164                         vx_put(vx);
1165                 }
1166         }
1167         if ((vx = LOWERVP(vp)) != NULL) {
1168                 if (vx_get(vx) == 0) {
1169                         VOP_REVOKE(vx, ap->a_flags);
1170                         vx_put(vx);
1171                 }
1172         }
1173         vgone(vp);
1174         return (0);
1175 }
1176
1177 /*
1178  * union_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
1179  *            struct thread *a_td)
1180  */
1181 static int
1182 union_mmap(struct vop_mmap_args *ap)
1183 {
1184         struct vnode *ovp = OTHERVP(ap->a_vp);
1185
1186         ap->a_head.a_ops = ovp->v_ops;
1187         ap->a_vp = ovp;
1188         return (vop_mmap_ap(ap));
1189 }
1190
1191 /*
1192  * union_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
1193  *              struct thread *a_td)
1194  */
1195 static int
1196 union_fsync(struct vop_fsync_args *ap)
1197 {
1198         int error = 0;
1199         struct thread *td = ap->a_td;
1200         struct vnode *targetvp;
1201         struct union_node *un = VTOUNION(ap->a_vp);
1202
1203         if ((targetvp = union_lock_other(un, td)) != NULLVP) {
1204                 error = VOP_FSYNC(targetvp, ap->a_waitfor, td);
1205                 union_unlock_other(targetvp, td);
1206         }
1207
1208         return (error);
1209 }
1210
1211 /*
1212  *      union_remove:
1213  *
1214  *      Remove the specified cnp.  The dvp and vp are passed to us locked
1215  *      and must remain locked on return.
1216  *
1217  * union_remove(struct vnode *a_dvp, struct vnode *a_vp,
1218  *              struct componentname *a_cnp)
1219  */
1220 static int
1221 union_remove(struct vop_remove_args *ap)
1222 {
1223         struct union_node *dun = VTOUNION(ap->a_dvp);
1224         struct union_node *un = VTOUNION(ap->a_vp);
1225         struct componentname *cnp = ap->a_cnp;
1226         struct thread *td = cnp->cn_td;
1227         struct vnode *uppervp;
1228         struct vnode *upperdvp;
1229         int error;
1230
1231         if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1232                 panic("union remove: null upper vnode");
1233
1234         if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1235                 if (union_dowhiteout(un, cnp->cn_cred, td))
1236                         cnp->cn_flags |= CNP_DOWHITEOUT;
1237                 error = VOP_REMOVE(upperdvp, uppervp, cnp);
1238 #if 0
1239                 /* XXX */
1240                 if (!error)
1241                         union_removed_upper(un);
1242 #endif
1243                 union_unlock_upper(uppervp, td);
1244         } else {
1245                 error = union_mkwhiteout(
1246                             MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1247                             upperdvp, ap->a_cnp, un->un_path);
1248         }
1249         union_unlock_upper(upperdvp, td);
1250         return (error);
1251 }
1252
1253 /*
1254  *      union_link:
1255  *
1256  *      tdvp will be locked on entry, vp will not be locked on entry.
1257  *      tdvp should remain locked on return and vp should remain unlocked
1258  *      on return.
1259  *
1260  * union_link(struct vnode *a_tdvp, struct vnode *a_vp,
1261  *            struct componentname *a_cnp)
1262  */
1263 static int
1264 union_link(struct vop_link_args *ap)
1265 {
1266         struct componentname *cnp = ap->a_cnp;
1267         struct thread *td = cnp->cn_td;
1268         struct union_node *dun = VTOUNION(ap->a_tdvp);
1269         struct vnode *vp;
1270         struct vnode *tdvp;
1271         int error = 0;
1272
1273         if (ap->a_tdvp->v_ops != ap->a_vp->v_ops) {
1274                 vp = ap->a_vp;
1275         } else {
1276                 struct union_node *tun = VTOUNION(ap->a_vp);
1277
1278                 if (tun->un_uppervp == NULLVP) {
1279                         vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY, td);
1280 #if 0
1281                         if (dun->un_uppervp == tun->un_dirvp) {
1282                                 if (dun->un_flags & UN_ULOCK) {
1283                                         dun->un_flags &= ~UN_ULOCK;
1284                                         VOP_UNLOCK(dun->un_uppervp, 0, td);
1285                                 }
1286                         }
1287 #endif
1288                         error = union_copyup(tun, 1, cnp->cn_cred, td);
1289 #if 0
1290                         if (dun->un_uppervp == tun->un_dirvp) {
1291                                 vn_lock(dun->un_uppervp, 
1292                                             LK_EXCLUSIVE | LK_RETRY, td);
1293                                 dun->un_flags |= UN_ULOCK;
1294                         }
1295 #endif
1296                         VOP_UNLOCK(ap->a_vp, 0, td);
1297                 }
1298                 vp = tun->un_uppervp;
1299         }
1300
1301         if (error)
1302                 return (error);
1303
1304         /*
1305          * Make sure upper is locked, then unlock the union directory we were 
1306          * called with to avoid a deadlock while we are calling VOP_LINK on 
1307          * the upper (with tdvp locked and vp not locked).  Our ap->a_tdvp
1308          * is expected to be locked on return.
1309          */
1310
1311         if ((tdvp = union_lock_upper(dun, td)) == NULLVP)
1312                 return (EROFS);
1313
1314         VOP_UNLOCK(ap->a_tdvp, 0, td);  /* unlock calling node */
1315         error = VOP_LINK(tdvp, vp, cnp); /* call link on upper */
1316
1317         /*
1318          * We have to unlock tdvp prior to relocking our calling node in
1319          * order to avoid a deadlock.
1320          */
1321         union_unlock_upper(tdvp, td);
1322         vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY, td);
1323         return (error);
1324 }
1325
1326 /*
1327  * union_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1328  *              struct componentname *a_fcnp, struct vnode *a_tdvp,
1329  *              struct vnode *a_tvp, struct componentname *a_tcnp)
1330  */
1331 static int
1332 union_rename(struct vop_rename_args *ap)
1333 {
1334         int error;
1335         struct vnode *fdvp = ap->a_fdvp;
1336         struct vnode *fvp = ap->a_fvp;
1337         struct vnode *tdvp = ap->a_tdvp;
1338         struct vnode *tvp = ap->a_tvp;
1339
1340         /*
1341          * Figure out what fdvp to pass to our upper or lower vnode.  If we
1342          * replace the fdvp, release the original one and ref the new one.
1343          */
1344
1345         if (fdvp->v_tag == VT_UNION) {  /* always true */
1346                 struct union_node *un = VTOUNION(fdvp);
1347                 if (un->un_uppervp == NULLVP) {
1348                         /*
1349                          * this should never happen in normal
1350                          * operation but might if there was
1351                          * a problem creating the top-level shadow
1352                          * directory.
1353                          */
1354                         error = EXDEV;
1355                         goto bad;
1356                 }
1357                 fdvp = un->un_uppervp;
1358                 vref(fdvp);
1359                 vrele(ap->a_fdvp);
1360         }
1361
1362         /*
1363          * Figure out what fvp to pass to our upper or lower vnode.  If we
1364          * replace the fvp, release the original one and ref the new one.
1365          */
1366
1367         if (fvp->v_tag == VT_UNION) {   /* always true */
1368                 struct union_node *un = VTOUNION(fvp);
1369 #if 0
1370                 struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
1371 #endif
1372
1373                 if (un->un_uppervp == NULLVP) {
1374                         switch(fvp->v_type) {
1375                         case VREG:
1376                                 vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_td);
1377                                 error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_td);
1378                                 VOP_UNLOCK(un->un_vnode, 0, ap->a_fcnp->cn_td);
1379                                 if (error)
1380                                         goto bad;
1381                                 break;
1382                         case VDIR:
1383                                 /*
1384                                  * XXX not yet.
1385                                  *
1386                                  * There is only one way to rename a directory
1387                                  * based in the lowervp, and that is to copy
1388                                  * the entire directory hierarchy.  Otherwise
1389                                  * it would not last across a reboot.
1390                                  */
1391 #if 0
1392                                 vrele(fvp);
1393                                 fvp = NULL;
1394                                 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY, ap->a_fcnp->cn_td);
1395                                 error = union_mkshadow(um, fdvp, 
1396                                             ap->a_fcnp, &un->un_uppervp);
1397                                 VOP_UNLOCK(fdvp, 0, ap->a_fcnp->cn_td);
1398                                 if (un->un_uppervp)
1399                                         VOP_UNLOCK(un->un_uppervp, 0, ap->a_fcnp->cn_td);
1400                                 if (error)
1401                                         goto bad;
1402                                 break;
1403 #endif
1404                         default:
1405                                 error = EXDEV;
1406                                 goto bad;
1407                         }
1408                 }
1409
1410                 if (un->un_lowervp != NULLVP)
1411                         ap->a_fcnp->cn_flags |= CNP_DOWHITEOUT;
1412                 fvp = un->un_uppervp;
1413                 vref(fvp);
1414                 vrele(ap->a_fvp);
1415         }
1416
1417         /*
1418          * Figure out what tdvp (destination directory) to pass to the
1419          * lower level.  If we replace it with uppervp, we need to vput the 
1420          * old one.  The exclusive lock is transfered to what we will pass
1421          * down in the VOP_RENAME and we replace uppervp with a simple
1422          * reference.
1423          */
1424
1425         if (tdvp->v_tag == VT_UNION) {
1426                 struct union_node *un = VTOUNION(tdvp);
1427
1428                 if (un->un_uppervp == NULLVP) {
1429                         /*
1430                          * this should never happen in normal
1431                          * operation but might if there was
1432                          * a problem creating the top-level shadow
1433                          * directory.
1434                          */
1435                         error = EXDEV;
1436                         goto bad;
1437                 }
1438
1439                 /*
1440                  * new tdvp is a lock and reference on uppervp, put away
1441                  * the old tdvp.
1442                  */
1443                 tdvp = union_lock_upper(un, ap->a_tcnp->cn_td);
1444                 vput(ap->a_tdvp);
1445         }
1446
1447         /*
1448          * Figure out what tvp (destination file) to pass to the
1449          * lower level.
1450          *
1451          * If the uppervp file does not exist put away the (wrong)
1452          * file and change tvp to NULL.
1453          */
1454
1455         if (tvp != NULLVP && tvp->v_tag == VT_UNION) {
1456                 struct union_node *un = VTOUNION(tvp);
1457
1458                 tvp = union_lock_upper(un, ap->a_tcnp->cn_td);
1459                 vput(ap->a_tvp);
1460                 /* note: tvp may be NULL */
1461         }
1462
1463         /*
1464          * VOP_RENAME releases/vputs prior to returning, so we have no
1465          * cleanup to do.
1466          */
1467
1468         return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1469
1470         /*
1471          * Error.  We still have to release / vput the various elements.
1472          */
1473
1474 bad:
1475         vrele(fdvp);
1476         if (fvp)
1477                 vrele(fvp);
1478         vput(tdvp);
1479         if (tvp != NULLVP) {
1480                 if (tvp != tdvp)
1481                         vput(tvp);
1482                 else
1483                         vrele(tvp);
1484         }
1485         return (error);
1486 }
1487
1488 /*
1489  * union_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1490  *              struct componentname *a_cnp, struct vattr *a_vap)
1491  */
1492 static int
1493 union_mkdir(struct vop_mkdir_args *ap)
1494 {
1495         struct union_node *dun = VTOUNION(ap->a_dvp);
1496         struct componentname *cnp = ap->a_cnp;
1497         struct thread *td = cnp->cn_td;
1498         struct vnode *upperdvp;
1499         int error = EROFS;
1500
1501         if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) {
1502                 struct vnode *vp;
1503
1504                 error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap);
1505                 union_unlock_upper(upperdvp, td);
1506
1507                 if (error == 0) {
1508                         VOP_UNLOCK(vp, 0, td);
1509                         UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vp->v_usecount));
1510                         error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
1511                                 ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
1512                         UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_usecount));
1513                 }
1514         }
1515         return (error);
1516 }
1517
1518 /*
1519  * union_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1520  *              struct componentname *a_cnp)
1521  */
1522 static int
1523 union_rmdir(struct vop_rmdir_args *ap)
1524 {
1525         struct union_node *dun = VTOUNION(ap->a_dvp);
1526         struct union_node *un = VTOUNION(ap->a_vp);
1527         struct componentname *cnp = ap->a_cnp;
1528         struct thread *td = cnp->cn_td;
1529         struct vnode *upperdvp;
1530         struct vnode *uppervp;
1531         int error;
1532
1533         if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1534                 panic("union rmdir: null upper vnode");
1535
1536         if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1537                 if (union_dowhiteout(un, cnp->cn_cred, td))
1538                         cnp->cn_flags |= CNP_DOWHITEOUT;
1539                 error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp);
1540                 union_unlock_upper(uppervp, td);
1541         } else {
1542                 error = union_mkwhiteout(
1543                             MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1544                             dun->un_uppervp, ap->a_cnp, un->un_path);
1545         }
1546         union_unlock_upper(upperdvp, td);
1547         return (error);
1548 }
1549
1550 /*
1551  *      union_symlink:
1552  *
1553  *      dvp is locked on entry and remains locked on return.  a_vpp is garbage
1554  *      (unused).
1555  *
1556  * union_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1557  *              struct componentname *a_cnp, struct vattr *a_vap,
1558  *              char *a_target)
1559  */
1560 static int
1561 union_symlink(struct vop_symlink_args *ap)
1562 {
1563         struct union_node *dun = VTOUNION(ap->a_dvp);
1564         struct componentname *cnp = ap->a_cnp;
1565         struct thread *td = cnp->cn_td;
1566         struct vnode *dvp;
1567         int error = EROFS;
1568
1569         if ((dvp = union_lock_upper(dun, td)) != NULLVP) {
1570                 error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap,
1571                             ap->a_target);
1572                 union_unlock_upper(dvp, td);
1573         }
1574         return (error);
1575 }
1576
1577 /*
1578  * union_readdir works in concert with getdirentries and
1579  * readdir(3) to provide a list of entries in the unioned
1580  * directories.  getdirentries is responsible for walking
1581  * down the union stack.  readdir(3) is responsible for
1582  * eliminating duplicate names from the returned data stream.
1583  *
1584  * union_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1585  *               int *a_eofflag, u_long *a_cookies, int a_ncookies)
1586  */
1587 static int
1588 union_readdir(struct vop_readdir_args *ap)
1589 {
1590         struct union_node *un = VTOUNION(ap->a_vp);
1591         struct thread *td = ap->a_uio->uio_td;
1592         struct vnode *uvp;
1593         int error = 0;
1594
1595         if ((uvp = union_lock_upper(un, td)) != NULLVP) {
1596                 ap->a_head.a_ops = uvp->v_ops;
1597                 ap->a_vp = uvp;
1598                 error = vop_readdir_ap(ap);
1599                 union_unlock_upper(uvp, td);
1600         }
1601         return(error);
1602 }
1603
1604 /*
1605  * union_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1606  */
1607 static int
1608 union_readlink(struct vop_readlink_args *ap)
1609 {
1610         int error;
1611         struct union_node *un = VTOUNION(ap->a_vp);
1612         struct uio *uio = ap->a_uio;
1613         struct thread *td = uio->uio_td;
1614         struct vnode *vp;
1615
1616         vp = union_lock_other(un, td);
1617         KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
1618
1619         ap->a_head.a_ops = vp->v_ops;
1620         ap->a_vp = vp;
1621         error = vop_readlink_ap(ap);
1622         union_unlock_other(vp, td);
1623
1624         return (error);
1625 }
1626
1627 /*
1628  *      union_inactive:
1629  *
1630  *      Called with the vnode locked.  We are expected to unlock the vnode.
1631  *
1632  * union_inactive(struct vnode *a_vp, struct thread *a_td)
1633  */
1634 static int
1635 union_inactive(struct vop_inactive_args *ap)
1636 {
1637         struct vnode *vp = ap->a_vp;
1638         /*struct thread *td = ap->a_td;*/
1639         struct union_node *un = VTOUNION(vp);
1640         struct vnode **vpp;
1641
1642         /*
1643          * Do nothing (and _don't_ bypass).
1644          * Wait to vrele lowervp until reclaim,
1645          * so that until then our union_node is in the
1646          * cache and reusable.
1647          *
1648          * NEEDSWORK: Someday, consider inactive'ing
1649          * the lowervp and then trying to reactivate it
1650          * with capabilities (v_id)
1651          * like they do in the name lookup cache code.
1652          * That's too much work for now.
1653          */
1654
1655         if (un->un_dircache != 0) {
1656                 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1657                         vrele(*vpp);
1658                 free (un->un_dircache, M_TEMP);
1659                 un->un_dircache = 0;
1660         }
1661
1662 #if 0
1663         if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
1664                 un->un_flags &= ~UN_ULOCK;
1665                 VOP_UNLOCK(un->un_uppervp, 0, td);
1666         }
1667 #endif
1668
1669         if ((un->un_flags & UN_CACHED) == 0)
1670                 vgone(vp);
1671
1672         return (0);
1673 }
1674
1675 /*
1676  * union_reclaim(struct vnode *a_vp)
1677  */
1678 static int
1679 union_reclaim(struct vop_reclaim_args *ap)
1680 {
1681         union_freevp(ap->a_vp);
1682
1683         return (0);
1684 }
1685
1686 static int
1687 union_lock(struct vop_lock_args *ap)
1688 {
1689 #if 0
1690         struct vnode *vp = ap->a_vp;
1691         struct thread *td = ap->a_td;
1692         int flags = ap->a_flags;
1693         struct union_node *un;
1694 #endif
1695         int error;
1696
1697         error = vop_stdlock(ap);
1698 #if 0
1699         un = VTOUNION(vp);
1700
1701         if (error == 0) {
1702                 /*
1703                  * Lock the upper if it exists and this is an exclusive lock
1704                  * request.
1705                  */
1706                 if (un->un_uppervp != NULLVP && 
1707                     (flags & LK_TYPE_MASK) == LK_EXCLUSIVE) {
1708                         if ((un->un_flags & UN_ULOCK) == 0 && vp->v_usecount) {
1709                                 error = vn_lock(un->un_uppervp, flags, td);
1710                                 if (error) {
1711                                         struct vop_unlock_args uap = { 0 };
1712                                         uap.a_vp = ap->a_vp;
1713                                         uap.a_flags = ap->a_flags;
1714                                         uap.a_td = ap->a_td;
1715                                         vop_stdunlock(&uap);
1716                                         return (error);
1717                                 }
1718                                 un->un_flags |= UN_ULOCK;
1719                         }
1720                 }
1721         }
1722 #endif
1723         return (error);
1724 }
1725
1726 /*
1727  *      union_unlock:
1728  *
1729  *      Unlock our union node.  This also unlocks uppervp.  
1730  *
1731  * union_unlock(struct vnode *a_vp, int a_flags, struct thread *a_td)
1732  */
1733 static int
1734 union_unlock(struct vop_unlock_args *ap)
1735 {
1736         int error;
1737 #if 0
1738         struct union_node *un = VTOUNION(ap->a_vp);
1739
1740         KASSERT((un->un_uppervp == NULL || un->un_uppervp->v_usecount > 0), ("uppervp usecount is 0"));
1741 #endif
1742
1743         error = vop_stdunlock(ap);
1744 #if 0
1745
1746         /*
1747          * If no exclusive locks remain and we are holding an uppervp lock,
1748          * remove the uppervp lock.
1749          */
1750
1751         if ((un->un_flags & UN_ULOCK) && 
1752             lockstatus(&un->un_lock, NULL) != LK_EXCLUSIVE) {
1753                 un->un_flags &= ~UN_ULOCK;
1754                 VOP_UNLOCK(un->un_uppervp, LK_EXCLUSIVE, td);
1755         }
1756 #endif
1757         return(error);
1758 }
1759
1760 /*
1761  *      union_bmap:
1762  *
1763  *      There isn't much we can do.  We cannot push through to the real vnode
1764  *      to get to the underlying device because this will bypass data
1765  *      cached by the real vnode.
1766  *
1767  *      For some reason we cannot return the 'real' vnode either, it seems
1768  *      to blow up memory maps.
1769  *
1770  * union_bmap(struct vnode *a_vp, daddr_t a_bn, struct vnode **a_vpp,
1771  *            daddr_t *a_bnp, int *a_runp, int *a_runb)
1772  */
1773 static int
1774 union_bmap(struct vop_bmap_args *ap)
1775 {
1776         return(EOPNOTSUPP);
1777 }
1778
1779 /*
1780  * union_print(struct vnode *a_vp)
1781  */
1782 static int
1783 union_print(struct vop_print_args *ap)
1784 {
1785         struct vnode *vp = ap->a_vp;
1786
1787         printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1788                         vp, UPPERVP(vp), LOWERVP(vp));
1789         if (UPPERVP(vp) != NULLVP)
1790                 vprint("union: upper", UPPERVP(vp));
1791         if (LOWERVP(vp) != NULLVP)
1792                 vprint("union: lower", LOWERVP(vp));
1793
1794         return (0);
1795 }
1796
1797 /*
1798  * union_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1799  */
1800 static int
1801 union_pathconf(struct vop_pathconf_args *ap)
1802 {
1803         int error;
1804         struct thread *td = curthread;          /* XXX */
1805         struct union_node *un = VTOUNION(ap->a_vp);
1806         struct vnode *vp;
1807
1808         vp = union_lock_other(un, td);
1809         KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
1810
1811         ap->a_head.a_ops = vp->v_ops;
1812         ap->a_vp = vp;
1813         error = vop_pathconf_ap(ap);
1814         union_unlock_other(vp, td);
1815
1816         return (error);
1817 }
1818
1819 /*
1820  * union_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1821  *               struct flock *a_fl, int a_flags)
1822  */
1823 static int
1824 union_advlock(struct vop_advlock_args *ap)
1825 {
1826         struct vnode *ovp = OTHERVP(ap->a_vp);
1827
1828         ap->a_head.a_ops = ovp->v_ops;
1829         ap->a_vp = ovp;
1830         return (vop_advlock_ap(ap));
1831 }
1832
1833
1834 /*
1835  * XXX - vop_strategy must be hand coded because it has no
1836  * YYY - and it is not coherent with anything
1837  *
1838  * vnode in its arguments.
1839  * This goes away with a merged VM/buffer cache.
1840  *
1841  * union_strategy(struct vnode *a_vp, struct buf *a_bp)
1842  */
1843 static int
1844 union_strategy(struct vop_strategy_args *ap)
1845 {
1846         struct buf *bp = ap->a_bp;
1847         struct vnode *othervp = OTHERVP(bp->b_vp);
1848
1849 #ifdef DIAGNOSTIC
1850         if (othervp == NULLVP)
1851                 panic("union_strategy: nil vp");
1852         if (((bp->b_flags & B_READ) == 0) &&
1853             (othervp == LOWERVP(bp->b_vp)))
1854                 panic("union_strategy: writing to lowervp");
1855 #endif
1856         return (VOP_STRATEGY(othervp, bp));
1857 }
1858
1859 /*
1860  * Global vfs data structures
1861  */
1862 struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1863         { &vop_default_desc,            vop_defaultop },
1864         { &vop_access_desc,             (void *) union_access },
1865         { &vop_advlock_desc,            (void *) union_advlock },
1866         { &vop_bmap_desc,               (void *) union_bmap },
1867         { &vop_close_desc,              (void *) union_close },
1868         { &vop_create_desc,             (void *) union_create },
1869         { &vop_fsync_desc,              (void *) union_fsync },
1870         { &vop_getpages_desc,           (void *) union_getpages },
1871         { &vop_putpages_desc,           (void *) union_putpages },
1872         { &vop_getattr_desc,            (void *) union_getattr },
1873         { &vop_inactive_desc,           (void *) union_inactive },
1874         { &vop_ioctl_desc,              (void *) union_ioctl },
1875         { &vop_islocked_desc,           (void *) vop_stdislocked },
1876         { &vop_lease_desc,              (void *) union_lease },
1877         { &vop_link_desc,               (void *) union_link },
1878         { &vop_lock_desc,               (void *) union_lock },
1879         { &vop_lookup_desc,             (void *) union_lookup },
1880         { &vop_mkdir_desc,              (void *) union_mkdir },
1881         { &vop_mknod_desc,              (void *) union_mknod },
1882         { &vop_mmap_desc,               (void *) union_mmap },
1883         { &vop_open_desc,               (void *) union_open },
1884         { &vop_pathconf_desc,           (void *) union_pathconf },
1885         { &vop_poll_desc,               (void *) union_poll },
1886         { &vop_print_desc,              (void *) union_print },
1887         { &vop_read_desc,               (void *) union_read },
1888         { &vop_readdir_desc,            (void *) union_readdir },
1889         { &vop_readlink_desc,           (void *) union_readlink },
1890         { &vop_reclaim_desc,            (void *) union_reclaim },
1891         { &vop_remove_desc,             (void *) union_remove },
1892         { &vop_rename_desc,             (void *) union_rename },
1893         { &vop_revoke_desc,             (void *) union_revoke },
1894         { &vop_rmdir_desc,              (void *) union_rmdir },
1895         { &vop_setattr_desc,            (void *) union_setattr },
1896         { &vop_strategy_desc,           (void *) union_strategy },
1897         { &vop_symlink_desc,            (void *) union_symlink },
1898         { &vop_unlock_desc,             (void *) union_unlock },
1899         { &vop_whiteout_desc,           (void *) union_whiteout },
1900         { &vop_write_desc,              (void *) union_write },
1901         { NULL, NULL }
1902 };
1903