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