155206514eb9fdf74cb48bfba1ff456ef63b4c51
[dragonfly.git] / sys / vfs / union / union_subr.c
1 /*
2  * Copyright (c) 1994 Jan-Simon Pendry
3  * Copyright (c) 1994
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_subr.c        8.20 (Berkeley) 5/20/95
38  * $FreeBSD: src/sys/miscfs/union/union_subr.c,v 1.43.2.2 2001/12/25 01:44:45 dillon Exp $
39  * $DragonFly: src/sys/vfs/union/union_subr.c,v 1.28 2007/05/06 19:23:35 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/proc.h>
47 #include <sys/namei.h>
48 #include <sys/malloc.h>
49 #include <sys/fcntl.h>
50 #include <sys/file.h>
51 #include <sys/filedesc.h>
52 #include <sys/module.h>
53 #include <sys/mount.h>
54 #include <sys/stat.h>
55 #include <vm/vm.h>
56 #include <vm/vm_extern.h>       /* for vnode_pager_setsize */
57 #include <vm/vm_zone.h>
58 #include <vm/vm_object.h>       /* for vm cache coherency */
59 #include "union.h"
60
61 extern int      union_init (void);
62
63 /* must be power of two, otherwise change UNION_HASH() */
64 #define NHASH 32
65
66 /* unsigned int ... */
67 #define UNION_HASH(u, l) \
68         (((((uintptr_t) (u)) + ((uintptr_t) l)) >> 8) & (NHASH-1))
69
70 static LIST_HEAD(unhead, union_node) unhead[NHASH];
71 static int unvplock[NHASH];
72
73 static void     union_dircache_r (struct vnode *vp, struct vnode ***vppp,
74                                       int *cntp);
75 static int      union_list_lock (int ix);
76 static void     union_list_unlock (int ix);
77 static int      union_relookup (struct union_mount *um, struct vnode *dvp,
78                                     struct vnode **vpp,
79                                     struct componentname *cnp,
80                                     struct componentname *cn, char *path,
81                                     int pathlen);
82 static void     union_updatevp (struct union_node *un,
83                                     struct vnode *uppervp,
84                                     struct vnode *lowervp);
85 static void union_newlower (struct union_node *, struct vnode *);
86 static void union_newupper (struct union_node *, struct vnode *);
87 static int union_copyfile (struct vnode *, struct vnode *,
88                                         struct ucred *, struct thread *);
89 static int union_vn_create (struct vnode **, struct union_node *,
90                                 struct thread *);
91 static int union_vn_close (struct vnode *, int, struct ucred *);
92
93 int
94 union_init(void)
95 {
96         int i;
97
98         for (i = 0; i < NHASH; i++)
99                 LIST_INIT(&unhead[i]);
100         bzero((caddr_t)unvplock, sizeof(unvplock));
101         return (0);
102 }
103
104 static int
105 union_list_lock(int ix)
106 {
107         if (unvplock[ix] & UNVP_LOCKED) {
108                 unvplock[ix] |= UNVP_WANT;
109                 (void) tsleep((caddr_t) &unvplock[ix], 0, "unllck", 0);
110                 return (1);
111         }
112         unvplock[ix] |= UNVP_LOCKED;
113         return (0);
114 }
115
116 static void
117 union_list_unlock(int ix)
118 {
119         unvplock[ix] &= ~UNVP_LOCKED;
120
121         if (unvplock[ix] & UNVP_WANT) {
122                 unvplock[ix] &= ~UNVP_WANT;
123                 wakeup((caddr_t) &unvplock[ix]);
124         }
125 }
126
127 /*
128  *      union_updatevp:
129  *
130  *      The uppervp, if not NULL, must be referenced and not locked by us
131  *      The lowervp, if not NULL, must be referenced.
132  *
133  *      if uppervp and lowervp match pointers already installed, nothing
134  *      happens. The passed vp's (when matching) are not adjusted.  This
135  *      routine may only be called by union_newupper() and union_newlower().
136  */
137
138 static void
139 union_updatevp(struct union_node *un, struct vnode *uppervp,
140                struct vnode *lowervp)
141 {
142         int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
143         int nhash = UNION_HASH(uppervp, lowervp);
144         int docache = (lowervp != NULLVP || uppervp != NULLVP);
145         int lhash, uhash;
146
147         /*
148          * Ensure locking is ordered from lower to higher
149          * to avoid deadlocks.
150          */
151         if (nhash < ohash) {
152                 lhash = nhash;
153                 uhash = ohash;
154         } else {
155                 lhash = ohash;
156                 uhash = nhash;
157         }
158
159         if (lhash != uhash) {
160                 while (union_list_lock(lhash))
161                         continue;
162         }
163
164         while (union_list_lock(uhash))
165                 continue;
166
167         if (ohash != nhash || !docache) {
168                 if (un->un_flags & UN_CACHED) {
169                         un->un_flags &= ~UN_CACHED;
170                         LIST_REMOVE(un, un_cache);
171                 }
172         }
173
174         if (ohash != nhash)
175                 union_list_unlock(ohash);
176
177         if (un->un_lowervp != lowervp) {
178                 if (un->un_lowervp) {
179                         vrele(un->un_lowervp);
180                         if (un->un_path) {
181                                 kfree(un->un_path, M_TEMP);
182                                 un->un_path = 0;
183                         }
184                 }
185                 un->un_lowervp = lowervp;
186                 un->un_lowersz = VNOVAL;
187         }
188
189         if (un->un_uppervp != uppervp) {
190                 if (un->un_uppervp)
191                         vrele(un->un_uppervp);
192                 un->un_uppervp = uppervp;
193                 un->un_uppersz = VNOVAL;
194         }
195
196         if (docache && (ohash != nhash)) {
197                 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
198                 un->un_flags |= UN_CACHED;
199         }
200
201         union_list_unlock(nhash);
202 }
203
204 /*
205  * Set a new lowervp.  The passed lowervp must be referenced and will be
206  * stored in the vp in a referenced state. 
207  */
208
209 static void
210 union_newlower(struct union_node *un, struct vnode *lowervp)
211 {
212         union_updatevp(un, un->un_uppervp, lowervp);
213 }
214
215 /*
216  * Set a new uppervp.  The passed uppervp must be locked and will be 
217  * stored in the vp in a locked state.  The caller should not unlock
218  * uppervp.
219  */
220
221 static void
222 union_newupper(struct union_node *un, struct vnode *uppervp)
223 {
224         union_updatevp(un, uppervp, un->un_lowervp);
225 }
226
227 /*
228  * Keep track of size changes in the underlying vnodes.
229  * If the size changes, then callback to the vm layer
230  * giving priority to the upper layer size.
231  */
232 void
233 union_newsize(struct vnode *vp, off_t uppersz, off_t lowersz)
234 {
235         struct union_node *un;
236         off_t sz;
237
238         /* only interested in regular files */
239         if (vp->v_type != VREG)
240                 return;
241
242         un = VTOUNION(vp);
243         sz = VNOVAL;
244
245         if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
246                 un->un_uppersz = uppersz;
247                 if (sz == VNOVAL)
248                         sz = un->un_uppersz;
249         }
250
251         if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
252                 un->un_lowersz = lowersz;
253                 if (sz == VNOVAL)
254                         sz = un->un_lowersz;
255         }
256
257         if (sz != VNOVAL) {
258                 UDEBUG(("union: %s size now %ld\n",
259                         (uppersz != VNOVAL ? "upper" : "lower"), (long)sz));
260                 vnode_pager_setsize(vp, sz);
261         }
262 }
263
264 /*
265  *      union_allocvp:  allocate a union_node and associate it with a
266  *                      parent union_node and one or two vnodes.
267  *
268  *      vpp     Holds the returned vnode locked and referenced if no 
269  *              error occurs.
270  *
271  *      mp      Holds the mount point.  mp may or may not be busied. 
272  *              allocvp makes no changes to mp.
273  *
274  *      dvp     Holds the parent union_node to the one we wish to create.
275  *              XXX may only be used to traverse an uncopied lowervp-based
276  *              tree?  XXX
277  *
278  *              dvp may or may not be locked.  allocvp makes no changes
279  *              to dvp.
280  *
281  *      upperdvp Holds the parent vnode to uppervp, generally used along
282  *              with path component information to create a shadow of
283  *              lowervp when uppervp does not exist.
284  *
285  *              upperdvp is referenced but unlocked on entry, and will be
286  *              dereferenced on return.
287  *
288  *      uppervp Holds the new uppervp vnode to be stored in the 
289  *              union_node we are allocating.  uppervp is referenced but
290  *              not locked, and will be dereferenced on return.
291  *
292  *      lowervp Holds the new lowervp vnode to be stored in the
293  *              union_node we are allocating.  lowervp is referenced but
294  *              not locked, and will be dereferenced on return.
295  * 
296  *      cnp     Holds path component information to be coupled with
297  *              lowervp and upperdvp to allow unionfs to create an uppervp
298  *              later on.  Only used if lowervp is valid.  The conents
299  *              of cnp is only valid for the duration of the call.
300  *
301  *      docache Determine whether this node should be entered in the
302  *              cache or whether it should be destroyed as soon as possible.
303  *
304  * all union_nodes are maintained on a singly-linked
305  * list.  new nodes are only allocated when they cannot
306  * be found on this list.  entries on the list are
307  * removed when the vfs reclaim entry is called.
308  *
309  * a single lock is kept for the entire list.  this is
310  * needed because the getnewvnode() function can block
311  * waiting for a vnode to become free, in which case there
312  * may be more than one process trying to get the same
313  * vnode.  this lock is only taken if we are going to
314  * call getnewvnode, since the kernel itself is single-threaded.
315  *
316  * if an entry is found on the list, then call vget() to
317  * take a reference.  this is done because there may be
318  * zero references to it and so it needs to removed from
319  * the vnode free list.
320  */
321
322 int
323 union_allocvp(struct vnode **vpp,
324               struct mount *mp,
325               struct vnode *dvp,                /* parent union vnode */
326               struct vnode *upperdvp,           /* parent vnode of uppervp */
327               struct componentname *cnp,        /* may be null */
328               struct vnode *uppervp,            /* may be null */
329               struct vnode *lowervp,            /* may be null */
330               int docache)
331 {
332         int error;
333         struct union_node *un = 0;
334         struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
335         struct thread *td = (cnp) ? cnp->cn_td : curthread; /* XXX */
336         int hash = 0;
337         int vflag;
338         int try;
339
340         if (uppervp == NULLVP && lowervp == NULLVP)
341                 panic("union: unidentifiable allocation");
342
343         if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
344                 vrele(lowervp);
345                 lowervp = NULLVP;
346         }
347
348         /* detect the root vnode (and aliases) */
349         vflag = 0;
350         if ((uppervp == um->um_uppervp) &&
351             ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
352                 if (lowervp == NULLVP) {
353                         lowervp = um->um_lowervp;
354                         if (lowervp != NULLVP)
355                                 vref(lowervp);
356                 }
357                 vflag = VROOT;
358         }
359
360 loop:
361         if (!docache) {
362                 un = 0;
363         } else for (try = 0; try < 3; try++) {
364                 switch (try) {
365                 case 0:
366                         if (lowervp == NULLVP)
367                                 continue;
368                         hash = UNION_HASH(uppervp, lowervp);
369                         break;
370
371                 case 1:
372                         if (uppervp == NULLVP)
373                                 continue;
374                         hash = UNION_HASH(uppervp, NULLVP);
375                         break;
376
377                 case 2:
378                         if (lowervp == NULLVP)
379                                 continue;
380                         hash = UNION_HASH(NULLVP, lowervp);
381                         break;
382                 }
383
384                 while (union_list_lock(hash))
385                         continue;
386
387                 for (un = unhead[hash].lh_first; un != 0;
388                                         un = un->un_cache.le_next) {
389                         if ((un->un_lowervp == lowervp ||
390                              un->un_lowervp == NULLVP) &&
391                             (un->un_uppervp == uppervp ||
392                              un->un_uppervp == NULLVP) &&
393                             (UNIONTOV(un)->v_mount == mp)) {
394                                 if (vget(UNIONTOV(un), LK_EXCLUSIVE|LK_SLEEPFAIL)) {
395                                         union_list_unlock(hash);
396                                         goto loop;
397                                 }
398                                 break;
399                         }
400                 }
401
402                 union_list_unlock(hash);
403
404                 if (un)
405                         break;
406         }
407
408         if (un) {
409                 /*
410                  * Obtain a lock on the union_node.  Everything is unlocked
411                  * except for dvp, so check that case.  If they match, our
412                  * new un is already locked.  Otherwise we have to lock our
413                  * new un.
414                  *
415                  * A potential deadlock situation occurs when we are holding
416                  * one lock while trying to get another.  We must follow 
417                  * strict ordering rules to avoid it.  We try to locate dvp
418                  * by scanning up from un_vnode, since the most likely 
419                  * scenario is un being under dvp.
420                  */
421
422                 if (dvp && un->un_vnode != dvp) {
423                         struct vnode *scan = un->un_vnode;
424
425                         do {
426                                 scan = VTOUNION(scan)->un_pvp;
427                         } while (scan && scan->v_tag == VT_UNION && scan != dvp);
428                         if (scan != dvp) {
429                                 /*
430                                  * our new un is above dvp (we never saw dvp
431                                  * while moving up the tree).
432                                  */
433                                 vref(dvp);
434                                 vn_unlock(dvp);
435                                 error = vn_lock(un->un_vnode, LK_EXCLUSIVE);
436                                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
437                                 vrele(dvp);
438                         } else {
439                                 /*
440                                  * our new un is under dvp
441                                  */
442                                 error = vn_lock(un->un_vnode, LK_EXCLUSIVE);
443                         }
444                 } else if (dvp == NULLVP) {
445                         /*
446                          * dvp is NULL, we need to lock un.
447                          */
448                         error = vn_lock(un->un_vnode, LK_EXCLUSIVE);
449                 } else {
450                         /*
451                          * dvp == un->un_vnode, we are already locked.
452                          */
453                         error = 0;
454                 }
455
456                 if (error)
457                         goto loop;
458
459                 /*
460                  * At this point, the union_node is locked and referenced.
461                  *
462                  * uppervp is locked and referenced or NULL, lowervp is
463                  * referenced or NULL.
464                  */
465                 UDEBUG(("Modify existing un %p vn %p upper %p(refs %d) -> %p(refs %d)\n",
466                         un, un->un_vnode, un->un_uppervp, 
467                         (un->un_uppervp ? un->un_uppervp->v_sysref.refcnt : -99),
468                         uppervp,
469                         (uppervp ? uppervp->v_sysref.refcnt : -99)
470                 ));
471
472                 if (uppervp != un->un_uppervp) {
473                         KASSERT(uppervp == NULL || uppervp->v_sysref.refcnt > 0, ("union_allocvp: too few refs %d (at least 1 required) on uppervp", uppervp->v_sysref.refcnt));
474                         union_newupper(un, uppervp);
475                 } else if (uppervp) {
476                         KASSERT(uppervp->v_sysref.refcnt > 1, ("union_allocvp: too few refs %d (at least 2 required) on uppervp", uppervp->v_sysref.refcnt));
477                         vrele(uppervp);
478                 }
479
480                 /*
481                  * Save information about the lower layer.
482                  * This needs to keep track of pathname
483                  * and directory information which union_vn_create
484                  * might need.
485                  */
486                 if (lowervp != un->un_lowervp) {
487                         union_newlower(un, lowervp);
488                         if (cnp && (lowervp != NULLVP)) {
489                                 un->un_path = malloc(cnp->cn_namelen+1,
490                                                 M_TEMP, M_WAITOK);
491                                 bcopy(cnp->cn_nameptr, un->un_path,
492                                                 cnp->cn_namelen);
493                                 un->un_path[cnp->cn_namelen] = '\0';
494                         }
495                 } else if (lowervp) {
496                         vrele(lowervp);
497                 }
498
499                 /*
500                  * and upperdvp
501                  */
502                 if (upperdvp != un->un_dirvp) {
503                         if (un->un_dirvp)
504                                 vrele(un->un_dirvp);
505                         un->un_dirvp = upperdvp;
506                 } else if (upperdvp) {
507                         vrele(upperdvp);
508                 }
509
510                 *vpp = UNIONTOV(un);
511                 return (0);
512         }
513
514         if (docache) {
515                 /*
516                  * otherwise lock the vp list while we call getnewvnode
517                  * since that can block.
518                  */ 
519                 hash = UNION_HASH(uppervp, lowervp);
520
521                 if (union_list_lock(hash))
522                         goto loop;
523         }
524
525         /*
526          * Create new node rather then replace old node
527          */
528
529         error = getnewvnode(VT_UNION, mp, vpp, 0, 0);
530         if (error) {
531                 /*
532                  * If an error occurs clear out vnodes.
533                  */
534                 if (lowervp)
535                         vrele(lowervp);
536                 if (uppervp) 
537                         vrele(uppervp);
538                 if (upperdvp)
539                         vrele(upperdvp);
540                 *vpp = NULL;
541                 goto out;
542         }
543
544         MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
545                 M_TEMP, M_WAITOK);
546
547         (*vpp)->v_flag |= vflag;
548         if (uppervp)
549                 (*vpp)->v_type = uppervp->v_type;
550         else
551                 (*vpp)->v_type = lowervp->v_type;
552
553         un = VTOUNION(*vpp);
554         bzero(un, sizeof(*un));
555
556         un->un_vnode = *vpp;
557         un->un_uppervp = uppervp;
558         un->un_uppersz = VNOVAL;
559         un->un_lowervp = lowervp;
560         un->un_lowersz = VNOVAL;
561         un->un_dirvp = upperdvp;
562         un->un_pvp = dvp;               /* only parent dir in new allocation */
563         if (dvp != NULLVP)
564                 vref(dvp);
565         un->un_dircache = 0;
566         un->un_openl = 0;
567
568         if (cnp && (lowervp != NULLVP)) {
569                 un->un_path = kmalloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
570                 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
571                 un->un_path[cnp->cn_namelen] = '\0';
572         } else {
573                 un->un_path = 0;
574                 un->un_dirvp = NULL;
575         }
576
577         if (docache) {
578                 LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
579                 un->un_flags |= UN_CACHED;
580         }
581
582         /* 
583          * locked refd vpp is returned
584          */
585
586 out:
587         if (docache)
588                 union_list_unlock(hash);
589
590         return (error);
591 }
592
593 int
594 union_freevp(struct vnode *vp)
595 {
596         struct union_node *un = VTOUNION(vp);
597
598         vp->v_data = NULL;
599         if (un->un_flags & UN_CACHED) {
600                 un->un_flags &= ~UN_CACHED;
601                 LIST_REMOVE(un, un_cache);
602         }
603         if (un->un_pvp != NULLVP) {
604                 vrele(un->un_pvp);
605                 un->un_pvp = NULL;
606         }
607         if (un->un_uppervp != NULLVP) {
608                 vrele(un->un_uppervp);
609                 un->un_uppervp = NULL;
610         }
611         if (un->un_lowervp != NULLVP) {
612                 vrele(un->un_lowervp);
613                 un->un_lowervp = NULL;
614         }
615         if (un->un_dirvp != NULLVP) {
616                 vrele(un->un_dirvp);
617                 un->un_dirvp = NULL;
618         }
619         if (un->un_path) {
620                 kfree(un->un_path, M_TEMP);
621                 un->un_path = NULL;
622         }
623         kfree(un, M_TEMP);
624         return (0);
625 }
626
627 /*
628  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
629  * using a sequence of reads and writes.  both (fvp)
630  * and (tvp) are locked on entry and exit.
631  *
632  * fvp and tvp are both exclusive locked on call, but their refcount's
633  * haven't been bumped at all.
634  */
635 static int
636 union_copyfile(struct vnode *fvp, struct vnode *tvp, struct ucred *cred,
637                struct thread *td)
638 {
639         char *buf;
640         struct uio uio;
641         struct iovec iov;
642         int error = 0;
643
644         /*
645          * strategy:
646          * allocate a buffer of size MAXBSIZE.
647          * loop doing reads and writes, keeping track
648          * of the current uio offset.
649          * give up at the first sign of trouble.
650          */
651
652         bzero(&uio, sizeof(uio));
653
654         uio.uio_td = td;
655         uio.uio_segflg = UIO_SYSSPACE;
656         uio.uio_offset = 0;
657
658         buf = kmalloc(MAXBSIZE, M_TEMP, M_WAITOK);
659
660         /* ugly loop follows... */
661         do {
662                 off_t offset = uio.uio_offset;
663                 int count;
664                 int bufoffset;
665
666                 /*
667                  * Setup for big read
668                  */
669                 uio.uio_iov = &iov;
670                 uio.uio_iovcnt = 1;
671                 iov.iov_base = buf;
672                 iov.iov_len = MAXBSIZE;
673                 uio.uio_resid = iov.iov_len;
674                 uio.uio_rw = UIO_READ;
675
676                 if ((error = VOP_READ(fvp, &uio, 0, cred)) != 0)
677                         break;
678
679                 /*
680                  * Get bytes read, handle read eof case and setup for
681                  * write loop
682                  */
683                 if ((count = MAXBSIZE - uio.uio_resid) == 0)
684                         break;
685                 bufoffset = 0;
686
687                 /*
688                  * Write until an error occurs or our buffer has been
689                  * exhausted, then update the offset for the next read.
690                  */
691                 while (bufoffset < count) {
692                         uio.uio_iov = &iov;
693                         uio.uio_iovcnt = 1;
694                         iov.iov_base = buf + bufoffset;
695                         iov.iov_len = count - bufoffset;
696                         uio.uio_offset = offset + bufoffset;
697                         uio.uio_rw = UIO_WRITE;
698                         uio.uio_resid = iov.iov_len;
699
700                         if ((error = VOP_WRITE(tvp, &uio, 0, cred)) != 0)
701                                 break;
702                         bufoffset += (count - bufoffset) - uio.uio_resid;
703                 }
704                 uio.uio_offset = offset + bufoffset;
705         } while (error == 0);
706
707         kfree(buf, M_TEMP);
708         return (error);
709 }
710
711 /*
712  *
713  * un's vnode is assumed to be locked on entry and remains locked on exit.
714  */
715
716 int
717 union_copyup(struct union_node *un, int docopy, struct ucred *cred,
718              struct thread *td)
719 {
720         int error;
721         struct vnode *lvp, *uvp;
722
723         /*
724          * If the user does not have read permission, the vnode should not
725          * be copied to upper layer.
726          */
727         vn_lock(un->un_lowervp, LK_EXCLUSIVE | LK_RETRY);
728         error = VOP_EACCESS(un->un_lowervp, VREAD, cred);
729         vn_unlock(un->un_lowervp);
730         if (error)
731                 return (error);
732
733         error = union_vn_create(&uvp, un, td);
734         if (error)
735                 return (error);
736
737         lvp = un->un_lowervp;
738
739         KASSERT(uvp->v_sysref.refcnt > 0, ("copy: uvp refcount 0: %d", uvp->v_sysref.refcnt));
740         if (docopy) {
741                 /*
742                  * XX - should not ignore errors
743                  * from VOP_CLOSE
744                  */
745                 vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
746                 error = VOP_OPEN(lvp, FREAD, cred, NULL);
747                 if (error == 0) {
748                         error = union_copyfile(lvp, uvp, cred, td);
749                         vn_unlock(lvp);
750                         (void) VOP_CLOSE(lvp, FREAD);
751                 }
752                 if (error == 0)
753                         UDEBUG(("union: copied up %s\n", un->un_path));
754
755         }
756         vn_unlock(uvp);
757         union_newupper(un, uvp);
758         KASSERT(uvp->v_sysref.refcnt > 0, ("copy: uvp refcount 0: %d", uvp->v_sysref.refcnt));
759         union_vn_close(uvp, FWRITE, cred);
760         KASSERT(uvp->v_sysref.refcnt > 0, ("copy: uvp refcount 0: %d", uvp->v_sysref.refcnt));
761         /*
762          * Subsequent IOs will go to the top layer, so
763          * call close on the lower vnode and open on the
764          * upper vnode to ensure that the filesystem keeps
765          * its references counts right.  This doesn't do
766          * the right thing with (cred) and (FREAD) though.
767          * Ignoring error returns is not right, either.
768          */
769         if (error == 0) {
770                 int i;
771
772                 for (i = 0; i < un->un_openl; i++) {
773                         VOP_CLOSE(lvp, FREAD);
774                         VOP_OPEN(uvp, FREAD, cred, NULL);
775                 }
776                 un->un_openl = 0;
777         }
778
779         return (error);
780
781 }
782
783 /*
784  *      union_relookup:
785  *
786  *      dvp should be locked on entry and will be locked on return.  No
787  *      net change in the ref count will occur.
788  *
789  *      If an error is returned, *vpp will be invalid, otherwise it
790  *      will hold a locked, referenced vnode.  If *vpp == dvp then
791  *      remember that only one exclusive lock is held.
792  */
793
794 static int
795 union_relookup(struct union_mount *um, struct vnode *dvp, struct vnode **vpp,
796                struct componentname *cnp, struct componentname *cn, char *path,
797                int pathlen)
798 {
799         int error;
800
801         /*
802          * A new componentname structure must be faked up because
803          * there is no way to know where the upper level cnp came
804          * from or what it is being used for.  This must duplicate
805          * some of the work done by NDINIT, some of the work done
806          * by namei, some of the work done by lookup and some of
807          * the work done by VOP_LOOKUP when given a CREATE flag.
808          * Conclusion: Horrible.
809          */
810         cn->cn_namelen = pathlen;
811         cn->cn_nameptr = objcache_get(namei_oc, M_WAITOK);
812         bcopy(path, cn->cn_nameptr, cn->cn_namelen);
813         cn->cn_nameptr[cn->cn_namelen] = '\0';
814
815         cn->cn_nameiop = NAMEI_CREATE;
816         cn->cn_flags = CNP_LOCKPARENT;
817         cn->cn_td = cnp->cn_td;
818         if (um->um_op == UNMNT_ABOVE)
819                 cn->cn_cred = cnp->cn_cred;
820         else
821                 cn->cn_cred = um->um_cred;
822         cn->cn_consume = cnp->cn_consume;
823
824         vref(dvp);
825         vn_unlock(dvp);
826
827         /*
828          * Pass dvp unlocked and referenced on call to relookup().
829          *
830          * If an error occurs, dvp will be returned unlocked and dereferenced.
831          */
832
833         if ((error = relookup(dvp, vpp, cn)) != 0) {
834                 objcache_put(namei_oc, cn->cn_nameptr);
835                 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
836                 return(error);
837         }
838         objcache_put(namei_oc, cn->cn_nameptr);
839
840         /*
841          * If no error occurs, dvp will be returned locked with the reference
842          * left as before, and vpp will be returned referenced and locked.
843          *
844          * We want to return with dvp as it was passed to us, so we get
845          * rid of our reference.
846          */
847         vrele(dvp);
848         return (0);
849 }
850
851 /*
852  * Create a shadow directory in the upper layer.
853  * The new vnode is returned locked.
854  *
855  * (um) points to the union mount structure for access to the
856  * the mounting process's credentials.
857  * (dvp) is the directory in which to create the shadow directory,
858  * it is locked (but not ref'd) on entry and return.
859  * (cnp) is the componentname to be created.
860  * (vpp) is the returned newly created shadow directory, which
861  * is returned locked and ref'd
862  */
863 int
864 union_mkshadow(struct union_mount *um, struct vnode *dvp,
865                struct componentname *cnp, struct vnode **vpp)
866 {
867         int error;
868         struct vattr va;
869         struct thread *td = cnp->cn_td;
870         struct componentname cn;
871
872         error = union_relookup(um, dvp, vpp, cnp, &cn,
873                         cnp->cn_nameptr, cnp->cn_namelen);
874         if (error)
875                 return (error);
876
877         if (*vpp) {
878                 if (dvp == *vpp)
879                         vrele(*vpp);
880                 else
881                         vput(*vpp);
882                 *vpp = NULLVP;
883                 return (EEXIST);
884         }
885
886         /*
887          * policy: when creating the shadow directory in the
888          * upper layer, create it owned by the user who did
889          * the mount, group from parent directory, and mode
890          * 777 modified by umask (ie mostly identical to the
891          * mkdir syscall).  (jsp, kb)
892          */
893
894         VATTR_NULL(&va);
895         va.va_type = VDIR;
896         va.va_mode = um->um_cmode;
897
898         error = VOP_MKDIR(dvp, vpp, &cn, &va);
899         /*vput(dvp);*/
900         return (error);
901 }
902
903 /*
904  * Create a whiteout entry in the upper layer.
905  *
906  * (um) points to the union mount structure for access to the
907  * the mounting process's credentials.
908  * (dvp) is the directory in which to create the whiteout.
909  * it is locked on entry and return.
910  * (cnp) is the componentname to be created.
911  */
912 int
913 union_mkwhiteout(struct union_mount *um, struct vnode *dvp,
914                  struct componentname *cnp, char *path)
915 {
916         int error;
917         struct thread *td = cnp->cn_td;
918         struct vnode *wvp;
919         struct componentname cn;
920         struct ucred *cred;
921
922         KKASSERT(td->td_proc);
923         cred = td->td_proc->p_ucred;
924
925         error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
926         if (error)
927                 return (error);
928
929         if (wvp) {
930                 if (wvp == dvp)
931                         vrele(wvp);
932                 else
933                         vput(wvp);
934                 return (EEXIST);
935         }
936
937         error = VOP_WHITEOUT(dvp, &cn, NAMEI_CREATE);
938         return (error);
939 }
940
941 /*
942  * union_vn_create: creates and opens a new shadow file
943  * on the upper union layer.  this function is similar
944  * in spirit to calling vn_open but it avoids calling namei().
945  * the problem with calling namei is that a) it locks too many
946  * things, and b) it doesn't start at the "right" directory,
947  * whereas relookup is told where to start.
948  *
949  * On entry, the vnode associated with un is locked.  It remains locked
950  * on return.
951  *
952  * If no error occurs, *vpp contains a locked referenced vnode for your
953  * use.  If an error occurs *vpp iis undefined.
954  */
955 static int
956 union_vn_create(struct vnode **vpp, struct union_node *un, struct thread *td)
957 {
958         struct vnode *vp;
959         struct ucred *cred;
960         struct vattr vat;
961         struct vattr *vap = &vat;
962         int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
963         int error;
964         int cmode;
965         struct componentname cn;
966
967         KKASSERT(td->td_proc);
968         cred = td->td_proc->p_ucred;
969         cmode = UN_FILEMODE & ~td->td_proc->p_fd->fd_cmask;
970
971         *vpp = NULLVP;
972
973         /*
974          * Build a new componentname structure (for the same
975          * reasons outlines in union_mkshadow).
976          * The difference here is that the file is owned by
977          * the current user, rather than by the person who
978          * did the mount, since the current user needs to be
979          * able to write the file (that's why it is being
980          * copied in the first place).
981          */
982         cn.cn_namelen = strlen(un->un_path);
983         cn.cn_nameptr = objcache_get(namei_oc, M_WAITOK);
984         bcopy(un->un_path, cn.cn_nameptr, cn.cn_namelen+1);
985         cn.cn_nameiop = NAMEI_CREATE;
986         cn.cn_flags = CNP_LOCKPARENT;
987         cn.cn_td = td;
988         cn.cn_cred = cred;
989         cn.cn_consume = 0;
990
991         /*
992          * Pass dvp unlocked and referenced on call to relookup().
993          *
994          * If an error occurs, dvp will be returned unlocked and dereferenced.
995          */
996         vref(un->un_dirvp);
997         error = relookup(un->un_dirvp, &vp, &cn);
998         objcache_put(namei_oc, cn.cn_nameptr);
999         if (error)
1000                 return (error);
1001
1002         /*
1003          * If no error occurs, dvp will be returned locked with the reference
1004          * left as before, and vpp will be returned referenced and locked.
1005          */
1006         if (vp) {
1007                 vput(un->un_dirvp);
1008                 if (vp == un->un_dirvp)
1009                         vrele(vp);
1010                 else
1011                         vput(vp);
1012                 return (EEXIST);
1013         }
1014
1015         /*
1016          * Good - there was no race to create the file
1017          * so go ahead and create it.  The permissions
1018          * on the file will be 0666 modified by the
1019          * current user's umask.  Access to the file, while
1020          * it is unioned, will require access to the top *and*
1021          * bottom files.  Access when not unioned will simply
1022          * require access to the top-level file.
1023          * TODO: confirm choice of access permissions.
1024          */
1025         VATTR_NULL(vap);
1026         vap->va_type = VREG;
1027         vap->va_mode = cmode;
1028         error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap);
1029         vput(un->un_dirvp);
1030         if (error)
1031                 return (error);
1032
1033         error = VOP_OPEN(vp, fmode, cred, NULL);
1034         if (error) {
1035                 vput(vp);
1036                 return (error);
1037         }
1038         *vpp = vp;
1039         return (0);
1040 }
1041
1042 static int
1043 union_vn_close(struct vnode *vp, int fmode, struct ucred *cred)
1044 {
1045         return (VOP_CLOSE(vp, fmode));
1046 }
1047
1048 #if 0
1049
1050 /*
1051  *      union_removed_upper:
1052  *
1053  *      called with union_node unlocked. XXX
1054  */
1055
1056 void
1057 union_removed_upper(struct union_node *un)
1058 {
1059         struct thread *td = curthread;  /* XXX */
1060         struct vnode **vpp;
1061
1062         /*
1063          * Do not set the uppervp to NULLVP.  If lowervp is NULLVP,
1064          * union node will have neither uppervp nor lowervp.  We remove
1065          * the union node from cache, so that it will not be referrenced.
1066          */
1067         union_newupper(un, NULLVP);
1068         if (un->un_dircache != 0) {
1069                 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1070                         vrele(*vpp);
1071                 kfree(un->un_dircache, M_TEMP);
1072                 un->un_dircache = 0;
1073         }
1074
1075         if (un->un_flags & UN_CACHED) {
1076                 un->un_flags &= ~UN_CACHED;
1077                 LIST_REMOVE(un, un_cache);
1078         }
1079 }
1080
1081 #endif
1082
1083 /*
1084  * determine whether a whiteout is needed
1085  * during a remove/rmdir operation.
1086  */
1087 int
1088 union_dowhiteout(struct union_node *un, struct ucred *cred, struct thread *td)
1089 {
1090         struct vattr va;
1091
1092         if (un->un_lowervp != NULLVP)
1093                 return (1);
1094
1095         if (VOP_GETATTR(un->un_uppervp, &va) == 0 &&
1096             (va.va_flags & OPAQUE))
1097                 return (1);
1098
1099         return (0);
1100 }
1101
1102 static void
1103 union_dircache_r(struct vnode *vp, struct vnode ***vppp, int *cntp)
1104 {
1105         struct union_node *un;
1106
1107         if (vp->v_tag != VT_UNION) {
1108                 if (vppp) {
1109                         vref(vp);
1110                         *(*vppp)++ = vp;
1111                         if (--(*cntp) == 0)
1112                                 panic("union: dircache table too small");
1113                 } else {
1114                         (*cntp)++;
1115                 }
1116
1117                 return;
1118         }
1119
1120         un = VTOUNION(vp);
1121         if (un->un_uppervp != NULLVP)
1122                 union_dircache_r(un->un_uppervp, vppp, cntp);
1123         if (un->un_lowervp != NULLVP)
1124                 union_dircache_r(un->un_lowervp, vppp, cntp);
1125 }
1126
1127 struct vnode *
1128 union_dircache(struct vnode *vp, struct thread *td)
1129 {
1130         int cnt;
1131         struct vnode *nvp;
1132         struct vnode **vpp;
1133         struct vnode **dircache;
1134         struct union_node *un;
1135         int error;
1136
1137         vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1138         dircache = VTOUNION(vp)->un_dircache;
1139
1140         nvp = NULLVP;
1141
1142         if (dircache == NULL) {
1143                 cnt = 0;
1144                 union_dircache_r(vp, 0, &cnt);
1145                 cnt++;
1146                 dircache = malloc(cnt * sizeof(struct vnode *),
1147                                 M_TEMP, M_WAITOK);
1148                 vpp = dircache;
1149                 union_dircache_r(vp, &vpp, &cnt);
1150                 *vpp = NULLVP;
1151                 vpp = dircache + 1;
1152         } else {
1153                 vpp = dircache;
1154                 do {
1155                         if (*vpp++ == VTOUNION(vp)->un_uppervp)
1156                                 break;
1157                 } while (*vpp != NULLVP);
1158         }
1159
1160         if (*vpp == NULLVP)
1161                 goto out;
1162
1163         /*vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);*/
1164         UDEBUG(("ALLOCVP-3 %p ref %d\n", *vpp, (*vpp ? (*vpp)->v_sysref.refcnt : -99)));
1165         vref(*vpp);
1166         error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, NULL, *vpp, NULLVP, 0);
1167         UDEBUG(("ALLOCVP-3B %p ref %d\n", nvp, (*vpp ? (*vpp)->v_sysref.refcnt : -99)));
1168         if (error)
1169                 goto out;
1170
1171         VTOUNION(vp)->un_dircache = 0;
1172         un = VTOUNION(nvp);
1173         un->un_dircache = dircache;
1174
1175 out:
1176         vn_unlock(vp);
1177         return (nvp);
1178 }
1179
1180 /*
1181  * Guarentee coherency with the VM cache by invalidating any clean VM pages
1182  * associated with this write and updating any dirty VM pages.  Since our
1183  * vnode is locked, other processes will not be able to read the pages in
1184  * again until after our write completes.
1185  *
1186  * We also have to be coherent with reads, by flushing any pending dirty
1187  * pages prior to issuing the read.
1188  *
1189  * XXX this is somewhat of a hack at the moment.  To support this properly
1190  * we would have to be able to run VOP_READ and VOP_WRITE through the VM
1191  * cache.  Then we wouldn't need to worry about coherency.
1192  */
1193
1194 void 
1195 union_vm_coherency(struct vnode *vp, struct uio *uio, int cleanfls)
1196 {
1197         vm_object_t object;
1198         vm_pindex_t pstart;
1199         vm_pindex_t pend;
1200         int pgoff;
1201
1202         if ((object = vp->v_object) == NULL)
1203             return;
1204
1205         pgoff = uio->uio_offset & PAGE_MASK;
1206         pstart = uio->uio_offset / PAGE_SIZE;
1207         pend = pstart + (uio->uio_resid + pgoff + PAGE_MASK) / PAGE_SIZE;
1208
1209         vm_object_page_clean(object, pstart, pend, OBJPC_SYNC);
1210         if (cleanfls)
1211                 vm_object_page_remove(object, pstart, pend, TRUE);
1212 }
1213
1214 /*
1215  * Module glue to remove #ifdef UNION from vfs_syscalls.c
1216  */
1217 static int
1218 union_dircheck(struct thread *td, struct vnode **vp, struct file *fp)
1219 {
1220         int error = 0;
1221
1222         if ((*vp)->v_tag == VT_UNION) {
1223                 struct vnode *lvp;
1224
1225                 lvp = union_dircache(*vp, td);
1226                 if (lvp != NULLVP) {
1227                         struct vattr va;
1228
1229                         /*
1230                          * If the directory is opaque,
1231                          * then don't show lower entries
1232                          */
1233                         error = VOP_GETATTR(*vp, &va);
1234                         if (va.va_flags & OPAQUE) {
1235                                 vput(lvp);
1236                                 lvp = NULL;
1237                         }
1238                 }
1239
1240                 if (lvp != NULLVP) {
1241                         error = VOP_OPEN(lvp, FREAD, fp->f_cred, NULL);
1242                         if (error) {
1243                                 vput(lvp);
1244                                 return (error);
1245                         }
1246                         vn_unlock(lvp);
1247                         fp->f_data = lvp;
1248                         fp->f_offset = 0;
1249                         error = vn_close(*vp, FREAD);
1250                         if (error)
1251                                 return (error);
1252                         *vp = lvp;
1253                         return -1;      /* goto unionread */
1254                 }
1255         }
1256         return error;
1257 }
1258
1259 static int
1260 union_modevent(module_t mod, int type, void *data)
1261 {
1262         switch (type) {
1263         case MOD_LOAD:
1264                 union_dircheckp = union_dircheck;
1265                 break;
1266         case MOD_UNLOAD:
1267                 union_dircheckp = NULL;
1268                 break;
1269         default:
1270                 break;
1271         }
1272         return 0;
1273 }
1274
1275 static moduledata_t union_mod = {
1276         "union_dircheck",
1277         union_modevent,
1278         NULL
1279 };
1280
1281 DECLARE_MODULE(union_dircheck, union_mod, SI_SUB_VFS, SI_ORDER_ANY);