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