xref: /netbsd-src/sys/fs/union/union_subr.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /*	$NetBSD: union_subr.c,v 1.28 2007/10/10 20:42:26 ad Exp $	*/
2 
3 /*
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
35  */
36 
37 /*
38  * Copyright (c) 1994 Jan-Simon Pendry
39  *
40  * This code is derived from software contributed to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)union_subr.c	8.20 (Berkeley) 5/20/95
72  */
73 
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.28 2007/10/10 20:42:26 ad Exp $");
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/time.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
83 #include <sys/namei.h>
84 #include <sys/malloc.h>
85 #include <sys/file.h>
86 #include <sys/filedesc.h>
87 #include <sys/queue.h>
88 #include <sys/mount.h>
89 #include <sys/stat.h>
90 #include <sys/kauth.h>
91 
92 #include <uvm/uvm_extern.h>
93 
94 #include <fs/union/union.h>
95 
96 /* must be power of two, otherwise change UNION_HASH() */
97 #define NHASH 32
98 
99 /* unsigned int ... */
100 #define UNION_HASH(u, l) \
101 	(((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
102 
103 static LIST_HEAD(unhead, union_node) unhead[NHASH];
104 static int unvplock[NHASH];
105 
106 static int union_list_lock(int);
107 static void union_list_unlock(int);
108 void union_updatevp(struct union_node *, struct vnode *, struct vnode *);
109 static int union_relookup(struct union_mount *, struct vnode *,
110 			       struct vnode **, struct componentname *,
111 			       struct componentname *, const char *, int);
112 int union_vn_close(struct vnode *, int, kauth_cred_t, struct lwp *);
113 static void union_dircache_r(struct vnode *, struct vnode ***, int *);
114 struct vnode *union_dircache(struct vnode *, struct lwp *);
115 
116 void
117 union_init()
118 {
119 	int i;
120 
121 	for (i = 0; i < NHASH; i++)
122 		LIST_INIT(&unhead[i]);
123 	memset(unvplock, 0, sizeof(unvplock));
124 }
125 
126 /*
127  * Free global unionfs resources.
128  */
129 void
130 union_done()
131 {
132 
133 	/* Make sure to unset the readdir hook. */
134 	vn_union_readdir_hook = NULL;
135 }
136 
137 static int
138 union_list_lock(ix)
139 	int ix;
140 {
141 
142 	if (unvplock[ix] & UN_LOCKED) {
143 		unvplock[ix] |= UN_WANTED;
144 		(void) tsleep(&unvplock[ix], PINOD, "unionlk", 0);
145 		return (1);
146 	}
147 
148 	unvplock[ix] |= UN_LOCKED;
149 
150 	return (0);
151 }
152 
153 static void
154 union_list_unlock(ix)
155 	int ix;
156 {
157 
158 	unvplock[ix] &= ~UN_LOCKED;
159 
160 	if (unvplock[ix] & UN_WANTED) {
161 		unvplock[ix] &= ~UN_WANTED;
162 		wakeup(&unvplock[ix]);
163 	}
164 }
165 
166 void
167 union_updatevp(un, uppervp, lowervp)
168 	struct union_node *un;
169 	struct vnode *uppervp;
170 	struct vnode *lowervp;
171 {
172 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
173 	int nhash = UNION_HASH(uppervp, lowervp);
174 	int docache = (lowervp != NULLVP || uppervp != NULLVP);
175 	int lhash, uhash;
176 
177 	/*
178 	 * Ensure locking is ordered from lower to higher
179 	 * to avoid deadlocks.
180 	 */
181 	if (nhash < ohash) {
182 		lhash = nhash;
183 		uhash = ohash;
184 	} else {
185 		lhash = ohash;
186 		uhash = nhash;
187 	}
188 
189 	if (lhash != uhash)
190 		while (union_list_lock(lhash))
191 			continue;
192 
193 	while (union_list_lock(uhash))
194 		continue;
195 
196 	if (ohash != nhash || !docache) {
197 		if (un->un_flags & UN_CACHED) {
198 			un->un_flags &= ~UN_CACHED;
199 			LIST_REMOVE(un, un_cache);
200 		}
201 	}
202 
203 	if (ohash != nhash)
204 		union_list_unlock(ohash);
205 
206 	if (un->un_lowervp != lowervp) {
207 		if (un->un_lowervp) {
208 			vrele(un->un_lowervp);
209 			if (un->un_path) {
210 				free(un->un_path, M_TEMP);
211 				un->un_path = 0;
212 			}
213 			if (un->un_dirvp) {
214 				vrele(un->un_dirvp);
215 				un->un_dirvp = NULLVP;
216 			}
217 		}
218 		un->un_lowervp = lowervp;
219 		un->un_lowersz = VNOVAL;
220 	}
221 
222 	if (un->un_uppervp != uppervp) {
223 		if (un->un_uppervp)
224 			vrele(un->un_uppervp);
225 
226 		un->un_uppervp = uppervp;
227 		un->un_uppersz = VNOVAL;
228 	}
229 
230 	if (docache && (ohash != nhash)) {
231 		LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
232 		un->un_flags |= UN_CACHED;
233 	}
234 
235 	union_list_unlock(nhash);
236 }
237 
238 void
239 union_newlower(un, lowervp)
240 	struct union_node *un;
241 	struct vnode *lowervp;
242 {
243 
244 	union_updatevp(un, un->un_uppervp, lowervp);
245 }
246 
247 void
248 union_newupper(un, uppervp)
249 	struct union_node *un;
250 	struct vnode *uppervp;
251 {
252 
253 	union_updatevp(un, uppervp, un->un_lowervp);
254 }
255 
256 /*
257  * Keep track of size changes in the underlying vnodes.
258  * If the size changes, then callback to the vm layer
259  * giving priority to the upper layer size.
260  */
261 void
262 union_newsize(vp, uppersz, lowersz)
263 	struct vnode *vp;
264 	off_t uppersz, lowersz;
265 {
266 	struct union_node *un;
267 	off_t sz;
268 
269 	/* only interested in regular files */
270 	if (vp->v_type != VREG) {
271 		uvm_vnp_setsize(vp, 0);
272 		return;
273 	}
274 
275 	un = VTOUNION(vp);
276 	sz = VNOVAL;
277 
278 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
279 		un->un_uppersz = uppersz;
280 		if (sz == VNOVAL)
281 			sz = un->un_uppersz;
282 	}
283 
284 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
285 		un->un_lowersz = lowersz;
286 		if (sz == VNOVAL)
287 			sz = un->un_lowersz;
288 	}
289 
290 	if (sz != VNOVAL) {
291 #ifdef UNION_DIAGNOSTIC
292 		printf("union: %s size now %qd\n",
293 		    uppersz != VNOVAL ? "upper" : "lower", sz);
294 #endif
295 		uvm_vnp_setsize(vp, sz);
296 	}
297 }
298 
299 /*
300  * allocate a union_node/vnode pair.  the vnode is
301  * referenced and locked.  the new vnode is returned
302  * via (vpp).  (mp) is the mountpoint of the union filesystem,
303  * (dvp) is the parent directory where the upper layer object
304  * should exist (but doesn't) and (cnp) is the componentname
305  * information which is partially copied to allow the upper
306  * layer object to be created at a later time.  (uppervp)
307  * and (lowervp) reference the upper and lower layer objects
308  * being mapped.  either, but not both, can be nil.
309  * if supplied, (uppervp) is locked.
310  * the reference is either maintained in the new union_node
311  * object which is allocated, or they are vrele'd.
312  *
313  * all union_nodes are maintained on a singly-linked
314  * list.  new nodes are only allocated when they cannot
315  * be found on this list.  entries on the list are
316  * removed when the vfs reclaim entry is called.
317  *
318  * a single lock is kept for the entire list.  this is
319  * needed because the getnewvnode() function can block
320  * waiting for a vnode to become free, in which case there
321  * may be more than one process trying to get the same
322  * vnode.  this lock is only taken if we are going to
323  * call getnewvnode, since the kernel itself is single-threaded.
324  *
325  * if an entry is found on the list, then call vget() to
326  * take a reference.  this is done because there may be
327  * zero references to it and so it needs to removed from
328  * the vnode free list.
329  */
330 int
331 union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp, docache)
332 	struct vnode **vpp;
333 	struct mount *mp;
334 	struct vnode *undvp;		/* parent union vnode */
335 	struct vnode *dvp;		/* may be null */
336 	struct componentname *cnp;	/* may be null */
337 	struct vnode *uppervp;		/* may be null */
338 	struct vnode *lowervp;		/* may be null */
339 	int docache;
340 {
341 	int error;
342 	struct vattr va;
343 	struct union_node *un = NULL;
344 	struct vnode *xlowervp = NULLVP;
345 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
346 	voff_t uppersz, lowersz;
347 	int hash = 0;
348 	int vflag, iflag;
349 	int try;
350 
351 	if (uppervp == NULLVP && lowervp == NULLVP)
352 		panic("union: unidentifiable allocation");
353 
354 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
355 		xlowervp = lowervp;
356 		lowervp = NULLVP;
357 	}
358 
359 	/* detect the root vnode (and aliases) */
360 	iflag = VI_LAYER;
361 	vflag = 0;
362 	if ((uppervp == um->um_uppervp) &&
363 	    ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
364 		if (lowervp == NULLVP) {
365 			lowervp = um->um_lowervp;
366 			if (lowervp != NULLVP)
367 				VREF(lowervp);
368 		}
369 		iflag = 0;
370 		vflag = VV_ROOT;
371 	}
372 
373 loop:
374 	if (!docache) {
375 		un = 0;
376 	} else for (try = 0; try < 3; try++) {
377 		switch (try) {
378 		case 0:
379 			if (lowervp == NULLVP)
380 				continue;
381 			hash = UNION_HASH(uppervp, lowervp);
382 			break;
383 
384 		case 1:
385 			if (uppervp == NULLVP)
386 				continue;
387 			hash = UNION_HASH(uppervp, NULLVP);
388 			break;
389 
390 		case 2:
391 			if (lowervp == NULLVP)
392 				continue;
393 			hash = UNION_HASH(NULLVP, lowervp);
394 			break;
395 		}
396 
397 		while (union_list_lock(hash))
398 			continue;
399 
400 		for (un = unhead[hash].lh_first; un != 0;
401 					un = un->un_cache.le_next) {
402 			if ((un->un_lowervp == lowervp ||
403 			     un->un_lowervp == NULLVP) &&
404 			    (un->un_uppervp == uppervp ||
405 			     un->un_uppervp == NULLVP) &&
406 			    (UNIONTOV(un)->v_mount == mp)) {
407 				if (vget(UNIONTOV(un), 0)) {
408 					union_list_unlock(hash);
409 					goto loop;
410 				}
411 				break;
412 			}
413 		}
414 
415 		union_list_unlock(hash);
416 
417 		if (un)
418 			break;
419 	}
420 
421 	if (un) {
422 		/*
423 		 * Obtain a lock on the union_node.
424 		 * uppervp is locked, though un->un_uppervp
425 		 * may not be.  this doesn't break the locking
426 		 * hierarchy since in the case that un->un_uppervp
427 		 * is not yet locked it will be vrele'd and replaced
428 		 * with uppervp.
429 		 */
430 
431 		if ((dvp != NULLVP) && (uppervp == dvp)) {
432 			/*
433 			 * Access ``.'', so (un) will already
434 			 * be locked.  Since this process has
435 			 * the lock on (uppervp) no other
436 			 * process can hold the lock on (un).
437 			 */
438 #ifdef DIAGNOSTIC
439 			if ((un->un_flags & UN_LOCKED) == 0)
440 				panic("union: . not locked");
441 			else if (curproc && un->un_pid != curproc->p_pid &&
442 				    un->un_pid > -1 && curproc->p_pid > -1)
443 				panic("union: allocvp not lock owner");
444 #endif
445 		} else {
446 			if (un->un_flags & UN_LOCKED) {
447 				vrele(UNIONTOV(un));
448 				un->un_flags |= UN_WANTED;
449 				(void) tsleep(&un->un_flags, PINOD,
450 				    "unionalloc", 0);
451 				goto loop;
452 			}
453 			un->un_flags |= UN_LOCKED;
454 
455 #ifdef DIAGNOSTIC
456 			if (curproc)
457 				un->un_pid = curproc->p_pid;
458 			else
459 				un->un_pid = -1;
460 #endif
461 		}
462 
463 		/*
464 		 * At this point, the union_node is locked,
465 		 * un->un_uppervp may not be locked, and uppervp
466 		 * is locked or nil.
467 		 */
468 
469 		/*
470 		 * Save information about the upper layer.
471 		 */
472 		if (uppervp != un->un_uppervp) {
473 			union_newupper(un, uppervp);
474 		} else if (uppervp) {
475 			vrele(uppervp);
476 		}
477 
478 		if (un->un_uppervp) {
479 			un->un_flags |= UN_ULOCK;
480 			un->un_flags &= ~UN_KLOCK;
481 		}
482 
483 		/*
484 		 * Save information about the lower layer.
485 		 * This needs to keep track of pathname
486 		 * and directory information which union_vn_create
487 		 * might need.
488 		 */
489 		if (lowervp != un->un_lowervp) {
490 			union_newlower(un, lowervp);
491 			if (cnp && (lowervp != NULLVP)) {
492 				un->un_hash = cnp->cn_hash;
493 				un->un_path = malloc(cnp->cn_namelen+1,
494 						M_TEMP, M_WAITOK);
495 				memcpy(un->un_path, cnp->cn_nameptr,
496 						cnp->cn_namelen);
497 				un->un_path[cnp->cn_namelen] = '\0';
498 				VREF(dvp);
499 				un->un_dirvp = dvp;
500 			}
501 		} else if (lowervp) {
502 			vrele(lowervp);
503 		}
504 		*vpp = UNIONTOV(un);
505 		return (0);
506 	}
507 
508 	uppersz = lowersz = VNOVAL;
509 	if (uppervp != NULLVP)
510 		if (VOP_GETATTR(uppervp, &va, FSCRED, NULL) == 0)
511 			uppersz = va.va_size;
512 	if (lowervp != NULLVP)
513 		if (VOP_GETATTR(lowervp, &va, FSCRED, NULL) == 0)
514 			lowersz = va.va_size;
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 	error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
528 	if (error) {
529 		if (uppervp) {
530 			if (dvp == uppervp)
531 				vrele(uppervp);
532 			else
533 				vput(uppervp);
534 		}
535 		if (lowervp)
536 			vrele(lowervp);
537 
538 		goto out;
539 	}
540 
541 	MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
542 		M_TEMP, M_WAITOK);
543 
544 	(*vpp)->v_vflag |= vflag;
545 	(*vpp)->v_iflag |= iflag;
546 	(*vpp)->v_vnlock = NULL;	/* Make upper layers call VOP_LOCK */
547 	if (uppervp)
548 		(*vpp)->v_type = uppervp->v_type;
549 	else
550 		(*vpp)->v_type = lowervp->v_type;
551 	un = VTOUNION(*vpp);
552 	un->un_vnode = *vpp;
553 	un->un_uppervp = uppervp;
554 	un->un_lowervp = lowervp;
555 	un->un_pvp = undvp;
556 	if (undvp != NULLVP)
557 		VREF(undvp);
558 	un->un_dircache = 0;
559 	un->un_openl = 0;
560 	un->un_flags = UN_LOCKED;
561 
562 	un->un_uppersz = VNOVAL;
563 	un->un_lowersz = VNOVAL;
564 	union_newsize(*vpp, uppersz, lowersz);
565 
566 	if (un->un_uppervp)
567 		un->un_flags |= UN_ULOCK;
568 #ifdef DIAGNOSTIC
569 	if (curproc)
570 		un->un_pid = curproc->p_pid;
571 	else
572 		un->un_pid = -1;
573 #endif
574 	if (dvp && cnp && (lowervp != NULLVP)) {
575 		un->un_hash = cnp->cn_hash;
576 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
577 		memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen);
578 		un->un_path[cnp->cn_namelen] = '\0';
579 		VREF(dvp);
580 		un->un_dirvp = dvp;
581 	} else {
582 		un->un_hash = 0;
583 		un->un_path = 0;
584 		un->un_dirvp = 0;
585 	}
586 
587 	if (docache) {
588 		LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
589 		un->un_flags |= UN_CACHED;
590 	}
591 
592 	if (xlowervp)
593 		vrele(xlowervp);
594 
595 out:
596 	if (docache)
597 		union_list_unlock(hash);
598 
599 	return (error);
600 }
601 
602 int
603 union_freevp(vp)
604 	struct vnode *vp;
605 {
606 	struct union_node *un = VTOUNION(vp);
607 
608 	if (un->un_flags & UN_CACHED) {
609 		un->un_flags &= ~UN_CACHED;
610 		LIST_REMOVE(un, un_cache);
611 	}
612 
613 	if (un->un_pvp != NULLVP)
614 		vrele(un->un_pvp);
615 	if (un->un_uppervp != NULLVP)
616 		vrele(un->un_uppervp);
617 	if (un->un_lowervp != NULLVP)
618 		vrele(un->un_lowervp);
619 	if (un->un_dirvp != NULLVP)
620 		vrele(un->un_dirvp);
621 	if (un->un_path)
622 		free(un->un_path, M_TEMP);
623 
624 	FREE(vp->v_data, M_TEMP);
625 	vp->v_data = 0;
626 
627 	return (0);
628 }
629 
630 /*
631  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
632  * using a sequence of reads and writes.  both (fvp)
633  * and (tvp) are locked on entry and exit.
634  */
635 int
636 union_copyfile(fvp, tvp, cred, l)
637 	struct vnode *fvp;
638 	struct vnode *tvp;
639 	kauth_cred_t cred;
640 	struct lwp *l;
641 {
642 	char *tbuf;
643 	struct uio uio;
644 	struct iovec iov;
645 	int error = 0;
646 
647 	/*
648 	 * strategy:
649 	 * allocate a buffer of size MAXBSIZE.
650 	 * loop doing reads and writes, keeping track
651 	 * of the current uio offset.
652 	 * give up at the first sign of trouble.
653 	 */
654 
655 	uio.uio_offset = 0;
656 	UIO_SETUP_SYSSPACE(&uio);
657 
658 	VOP_UNLOCK(fvp, 0);			/* XXX */
659 	VOP_LEASE(fvp, l, cred, LEASE_READ);
660 	vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
661 	VOP_UNLOCK(tvp, 0);			/* XXX */
662 	VOP_LEASE(tvp, l, cred, LEASE_WRITE);
663 	vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);	/* XXX */
664 
665 	tbuf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
666 
667 	/* ugly loop follows... */
668 	do {
669 		off_t offset = uio.uio_offset;
670 
671 		uio.uio_iov = &iov;
672 		uio.uio_iovcnt = 1;
673 		iov.iov_base = tbuf;
674 		iov.iov_len = MAXBSIZE;
675 		uio.uio_resid = iov.iov_len;
676 		uio.uio_rw = UIO_READ;
677 		error = VOP_READ(fvp, &uio, 0, cred);
678 
679 		if (error == 0) {
680 			uio.uio_iov = &iov;
681 			uio.uio_iovcnt = 1;
682 			iov.iov_base = tbuf;
683 			iov.iov_len = MAXBSIZE - uio.uio_resid;
684 			uio.uio_offset = offset;
685 			uio.uio_rw = UIO_WRITE;
686 			uio.uio_resid = iov.iov_len;
687 
688 			if (uio.uio_resid == 0)
689 				break;
690 
691 			do {
692 				error = VOP_WRITE(tvp, &uio, 0, cred);
693 			} while ((uio.uio_resid > 0) && (error == 0));
694 		}
695 
696 	} while (error == 0);
697 
698 	free(tbuf, M_TEMP);
699 	return (error);
700 }
701 
702 /*
703  * (un) is assumed to be locked on entry and remains
704  * locked on exit.
705  */
706 int
707 union_copyup(un, docopy, cred, l)
708 	struct union_node *un;
709 	int docopy;
710 	kauth_cred_t cred;
711 	struct lwp *l;
712 {
713 	int error;
714 	struct vnode *lvp, *uvp;
715 	struct vattr lvattr, uvattr;
716 
717 	error = union_vn_create(&uvp, un, l);
718 	if (error)
719 		return (error);
720 
721 	/* at this point, uppervp is locked */
722 	union_newupper(un, uvp);
723 	un->un_flags |= UN_ULOCK;
724 
725 	lvp = un->un_lowervp;
726 
727 	if (docopy) {
728 		/*
729 		 * XX - should not ignore errors
730 		 * from VOP_CLOSE
731 		 */
732 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
733 
734         	error = VOP_GETATTR(lvp, &lvattr, cred, l);
735 		if (error == 0)
736 			error = VOP_OPEN(lvp, FREAD, cred, l);
737 		if (error == 0) {
738 			error = union_copyfile(lvp, uvp, cred, l);
739 			(void) VOP_CLOSE(lvp, FREAD, cred, l);
740 		}
741 		if (error == 0) {
742 			/* Copy permissions up too */
743 			VATTR_NULL(&uvattr);
744 			uvattr.va_mode = lvattr.va_mode;
745 			uvattr.va_flags = lvattr.va_flags;
746         		error = VOP_SETATTR(uvp, &uvattr, cred, l);
747 		}
748 		VOP_UNLOCK(lvp, 0);
749 #ifdef UNION_DIAGNOSTIC
750 		if (error == 0)
751 			uprintf("union: copied up %s\n", un->un_path);
752 #endif
753 
754 	}
755 	union_vn_close(uvp, FWRITE, cred, l);
756 
757 	/*
758 	 * Subsequent IOs will go to the top layer, so
759 	 * call close on the lower vnode and open on the
760 	 * upper vnode to ensure that the filesystem keeps
761 	 * its references counts right.  This doesn't do
762 	 * the right thing with (cred) and (FREAD) though.
763 	 * Ignoring error returns is not right, either.
764 	 */
765 	if (error == 0) {
766 		int i;
767 
768 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
769 		for (i = 0; i < un->un_openl; i++) {
770 			(void) VOP_CLOSE(lvp, FREAD, cred, l);
771 			(void) VOP_OPEN(uvp, FREAD, cred, l);
772 		}
773 		un->un_openl = 0;
774 		VOP_UNLOCK(lvp, 0);
775 	}
776 
777 	return (error);
778 
779 }
780 
781 static int
782 union_relookup(um, dvp, vpp, cnp, cn, path, pathlen)
783 	struct union_mount *um;
784 	struct vnode *dvp;
785 	struct vnode **vpp;
786 	struct componentname *cnp;
787 	struct componentname *cn;
788 	const char *path;
789 	int pathlen;
790 {
791 	int error;
792 
793 	/*
794 	 * A new componentname structure must be faked up because
795 	 * there is no way to know where the upper level cnp came
796 	 * from or what it is being used for.  This must duplicate
797 	 * some of the work done by NDINIT, some of the work done
798 	 * by namei, some of the work done by lookup and some of
799 	 * the work done by VOP_LOOKUP when given a CREATE flag.
800 	 * Conclusion: Horrible.
801 	 *
802 	 * The pathname buffer will be PNBUF_PUT'd by VOP_MKDIR.
803 	 */
804 	cn->cn_namelen = pathlen;
805 	if ((cn->cn_namelen + 1) > MAXPATHLEN)
806 		return (ENAMETOOLONG);
807 	cn->cn_pnbuf = PNBUF_GET();
808 	memcpy(cn->cn_pnbuf, path, cn->cn_namelen);
809 	cn->cn_pnbuf[cn->cn_namelen] = '\0';
810 
811 	cn->cn_nameiop = CREATE;
812 	cn->cn_flags = (LOCKPARENT|HASBUF|SAVENAME|ISLASTCN);
813 	cn->cn_lwp = cnp->cn_lwp;
814 	if (um->um_op == UNMNT_ABOVE)
815 		cn->cn_cred = cnp->cn_cred;
816 	else
817 		cn->cn_cred = um->um_cred;
818 	cn->cn_nameptr = cn->cn_pnbuf;
819 	cn->cn_hash = cnp->cn_hash;
820 	cn->cn_consume = cnp->cn_consume;
821 
822 	error = relookup(dvp, vpp, cn);
823 	if (error) {
824 		PNBUF_PUT(cn->cn_pnbuf);
825 		cn->cn_pnbuf = 0;
826 	}
827 
828 	return (error);
829 }
830 
831 /*
832  * Create a shadow directory in the upper layer.
833  * The new vnode is returned locked.
834  *
835  * (um) points to the union mount structure for access to the
836  * the mounting process's credentials.
837  * (dvp) is the directory in which to create the shadow directory.
838  * it is unlocked on entry and exit.
839  * (cnp) is the componentname to be created.
840  * (vpp) is the returned newly created shadow directory, which
841  * is returned locked.
842  *
843  * N.B. We still attempt to create shadow directories even if the union
844  * is mounted read-only, which is a little nonintuitive.
845  */
846 int
847 union_mkshadow(um, dvp, cnp, vpp)
848 	struct union_mount *um;
849 	struct vnode *dvp;
850 	struct componentname *cnp;
851 	struct vnode **vpp;
852 {
853 	int error;
854 	struct vattr va;
855 	struct lwp *l = cnp->cn_lwp;
856 	struct componentname cn;
857 
858 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
859 	error = union_relookup(um, dvp, vpp, cnp, &cn,
860 			cnp->cn_nameptr, cnp->cn_namelen);
861 	if (error) {
862 		VOP_UNLOCK(dvp, 0);
863 		return (error);
864 	}
865 
866 	if (*vpp) {
867 		VOP_ABORTOP(dvp, &cn);
868 		if (dvp != *vpp)
869 			VOP_UNLOCK(dvp, 0);
870 		vput(*vpp);
871 		*vpp = NULLVP;
872 		return (EEXIST);
873 	}
874 
875 	/*
876 	 * policy: when creating the shadow directory in the
877 	 * upper layer, create it owned by the user who did
878 	 * the mount, group from parent directory, and mode
879 	 * 777 modified by umask (ie mostly identical to the
880 	 * mkdir syscall).  (jsp, kb)
881 	 */
882 
883 	VATTR_NULL(&va);
884 	va.va_type = VDIR;
885 	va.va_mode = um->um_cmode;
886 
887 	/* VOP_LEASE: dvp is locked */
888 	VOP_LEASE(dvp, l, cn.cn_cred, LEASE_WRITE);
889 
890 	vref(dvp);
891 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
892 	return (error);
893 }
894 
895 /*
896  * Create a whiteout entry in the upper layer.
897  *
898  * (um) points to the union mount structure for access to the
899  * the mounting process's credentials.
900  * (dvp) is the directory in which to create the whiteout.
901  * it is locked on entry and exit.
902  * (cnp) is the componentname to be created.
903  */
904 int
905 union_mkwhiteout(um, dvp, cnp, path)
906 	struct union_mount *um;
907 	struct vnode *dvp;
908 	struct componentname *cnp;
909 	char *path;
910 {
911 	int error;
912 	struct lwp *l = cnp->cn_lwp;
913 	struct vnode *wvp;
914 	struct componentname cn;
915 
916 	VOP_UNLOCK(dvp, 0);
917 	vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
918 	error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
919 	if (error)
920 		return (error);
921 
922 	if (wvp) {
923 		VOP_ABORTOP(dvp, &cn);
924 		if (dvp != wvp)
925 			VOP_UNLOCK(dvp, 0);
926 		vput(wvp);
927 		return (EEXIST);
928 	}
929 
930 	/* VOP_LEASE: dvp is locked */
931 	VOP_LEASE(dvp, l, l->l_cred, LEASE_WRITE);
932 
933 	error = VOP_WHITEOUT(dvp, &cn, CREATE);
934 	if (error)
935 		VOP_ABORTOP(dvp, &cn);
936 
937 	return (error);
938 }
939 
940 /*
941  * union_vn_create: creates and opens a new shadow file
942  * on the upper union layer.  this function is similar
943  * in spirit to calling vn_open but it avoids calling namei().
944  * the problem with calling namei is that a) it locks too many
945  * things, and b) it doesn't start at the "right" directory,
946  * whereas relookup is told where to start.
947  */
948 int
949 union_vn_create(vpp, un, l)
950 	struct vnode **vpp;
951 	struct union_node *un;
952 	struct lwp *l;
953 {
954 	struct vnode *vp;
955 	kauth_cred_t cred = l->l_cred;
956 	struct vattr vat;
957 	struct vattr *vap = &vat;
958 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
959 	int error;
960 	int cmode = UN_FILEMODE & ~l->l_proc->p_cwdi->cwdi_cmask;
961 	struct componentname cn;
962 
963 	*vpp = NULLVP;
964 
965 	/*
966 	 * Build a new componentname structure (for the same
967 	 * reasons outlines in union_mkshadow).
968 	 * The difference here is that the file is owned by
969 	 * the current user, rather than by the person who
970 	 * did the mount, since the current user needs to be
971 	 * able to write the file (that's why it is being
972 	 * copied in the first place).
973 	 */
974 	cn.cn_namelen = strlen(un->un_path);
975 	if ((cn.cn_namelen + 1) > MAXPATHLEN)
976 		return (ENAMETOOLONG);
977 	cn.cn_pnbuf = PNBUF_GET();
978 	memcpy(cn.cn_pnbuf, un->un_path, cn.cn_namelen+1);
979 	cn.cn_nameiop = CREATE;
980 	cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|ISLASTCN);
981 	cn.cn_lwp = l;
982 	cn.cn_cred = l->l_cred;
983 	cn.cn_nameptr = cn.cn_pnbuf;
984 	cn.cn_hash = un->un_hash;
985 	cn.cn_consume = 0;
986 
987 	vn_lock(un->un_dirvp, LK_EXCLUSIVE | LK_RETRY);
988 	error = relookup(un->un_dirvp, &vp, &cn);
989 	if (error) {
990 		VOP_UNLOCK(un->un_dirvp, 0);
991 		return (error);
992 	}
993 
994 	if (vp) {
995 		VOP_ABORTOP(un->un_dirvp, &cn);
996 		if (un->un_dirvp != vp)
997 			VOP_UNLOCK(un->un_dirvp, 0);
998 		vput(vp);
999 		return (EEXIST);
1000 	}
1001 
1002 	/*
1003 	 * Good - there was no race to create the file
1004 	 * so go ahead and create it.  The permissions
1005 	 * on the file will be 0666 modified by the
1006 	 * current user's umask.  Access to the file, while
1007 	 * it is unioned, will require access to the top *and*
1008 	 * bottom files.  Access when not unioned will simply
1009 	 * require access to the top-level file.
1010 	 * TODO: confirm choice of access permissions.
1011 	 */
1012 	VATTR_NULL(vap);
1013 	vap->va_type = VREG;
1014 	vap->va_mode = cmode;
1015 	VOP_LEASE(un->un_dirvp, l, cred, LEASE_WRITE);
1016 	vref(un->un_dirvp);
1017 	if ((error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) != 0)
1018 		return (error);
1019 
1020 	if ((error = VOP_OPEN(vp, fmode, cred, l)) != 0) {
1021 		vput(vp);
1022 		return (error);
1023 	}
1024 
1025 	vp->v_writecount++;
1026 	*vpp = vp;
1027 	return (0);
1028 }
1029 
1030 int
1031 union_vn_close(vp, fmode, cred, l)
1032 	struct vnode *vp;
1033 	int fmode;
1034 	kauth_cred_t cred;
1035 	struct lwp *l;
1036 {
1037 
1038 	if (fmode & FWRITE)
1039 		--vp->v_writecount;
1040 	return (VOP_CLOSE(vp, fmode, cred, l));
1041 }
1042 
1043 void
1044 union_removed_upper(un)
1045 	struct union_node *un;
1046 {
1047 #if 1
1048 	/*
1049 	 * We do not set the uppervp to NULLVP here, because lowervp
1050 	 * may also be NULLVP, so this routine would end up creating
1051 	 * a bogus union node with no upper or lower VP (that causes
1052 	 * pain in many places that assume at least one VP exists).
1053 	 * Since we've removed this node from the cache hash chains,
1054 	 * it won't be found again.  When all current holders
1055 	 * release it, union_inactive() will vgone() it.
1056 	 */
1057 	union_diruncache(un);
1058 #else
1059 	union_newupper(un, NULLVP);
1060 #endif
1061 
1062 	if (un->un_flags & UN_CACHED) {
1063 		un->un_flags &= ~UN_CACHED;
1064 		LIST_REMOVE(un, un_cache);
1065 	}
1066 
1067 	if (un->un_flags & UN_ULOCK) {
1068 		un->un_flags &= ~UN_ULOCK;
1069 		VOP_UNLOCK(un->un_uppervp, 0);
1070 	}
1071 }
1072 
1073 #if 0
1074 struct vnode *
1075 union_lowervp(vp)
1076 	struct vnode *vp;
1077 {
1078 	struct union_node *un = VTOUNION(vp);
1079 
1080 	if ((un->un_lowervp != NULLVP) &&
1081 	    (vp->v_type == un->un_lowervp->v_type)) {
1082 		if (vget(un->un_lowervp, 0) == 0)
1083 			return (un->un_lowervp);
1084 	}
1085 
1086 	return (NULLVP);
1087 }
1088 #endif
1089 
1090 /*
1091  * determine whether a whiteout is needed
1092  * during a remove/rmdir operation.
1093  */
1094 int
1095 union_dowhiteout(un, cred, l)
1096 	struct union_node *un;
1097 	kauth_cred_t cred;
1098 	struct lwp *l;
1099 {
1100 	struct vattr va;
1101 
1102 	if (un->un_lowervp != NULLVP)
1103 		return (1);
1104 
1105 	if (VOP_GETATTR(un->un_uppervp, &va, cred, l) == 0 &&
1106 	    (va.va_flags & OPAQUE))
1107 		return (1);
1108 
1109 	return (0);
1110 }
1111 
1112 static void
1113 union_dircache_r(vp, vppp, cntp)
1114 	struct vnode *vp;
1115 	struct vnode ***vppp;
1116 	int *cntp;
1117 {
1118 	struct union_node *un;
1119 
1120 	if (vp->v_op != union_vnodeop_p) {
1121 		if (vppp) {
1122 			VREF(vp);
1123 			*(*vppp)++ = vp;
1124 			if (--(*cntp) == 0)
1125 				panic("union: dircache table too small");
1126 		} else {
1127 			(*cntp)++;
1128 		}
1129 
1130 		return;
1131 	}
1132 
1133 	un = VTOUNION(vp);
1134 	if (un->un_uppervp != NULLVP)
1135 		union_dircache_r(un->un_uppervp, vppp, cntp);
1136 	if (un->un_lowervp != NULLVP)
1137 		union_dircache_r(un->un_lowervp, vppp, cntp);
1138 }
1139 
1140 struct vnode *
1141 union_dircache(struct vnode *vp, struct lwp *l)
1142 {
1143 	int cnt;
1144 	struct vnode *nvp = NULLVP;
1145 	struct vnode **vpp;
1146 	struct vnode **dircache;
1147 	int error;
1148 
1149 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1150 	dircache = VTOUNION(vp)->un_dircache;
1151 
1152 	nvp = NULLVP;
1153 
1154 	if (dircache == 0) {
1155 		cnt = 0;
1156 		union_dircache_r(vp, 0, &cnt);
1157 		cnt++;
1158 		dircache = (struct vnode **)
1159 				malloc(cnt * sizeof(struct vnode *),
1160 					M_TEMP, M_WAITOK);
1161 		vpp = dircache;
1162 		union_dircache_r(vp, &vpp, &cnt);
1163 		VTOUNION(vp)->un_dircache = dircache;
1164 		*vpp = NULLVP;
1165 		vpp = dircache + 1;
1166 	} else {
1167 		vpp = dircache;
1168 		do {
1169 			if (*vpp++ == VTOUNION(vp)->un_uppervp)
1170 				break;
1171 		} while (*vpp != NULLVP);
1172 	}
1173 
1174 	if (*vpp == NULLVP)
1175 		goto out;
1176 
1177 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1178 	VREF(*vpp);
1179 	error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0);
1180 	if (!error) {
1181 		VTOUNION(vp)->un_dircache = 0;
1182 		VTOUNION(nvp)->un_dircache = dircache;
1183 	}
1184 
1185 out:
1186 	VOP_UNLOCK(vp, 0);
1187 	return (nvp);
1188 }
1189 
1190 void
1191 union_diruncache(un)
1192 	struct union_node *un;
1193 {
1194 	struct vnode **vpp;
1195 
1196 	if (un->un_dircache != 0) {
1197 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1198 			vrele(*vpp);
1199 		free(un->un_dircache, M_TEMP);
1200 		un->un_dircache = 0;
1201 	}
1202 }
1203 
1204 /*
1205  * This hook is called from vn_readdir() to switch to lower directory
1206  * entry after the upper directory is read.
1207  */
1208 int
1209 union_readdirhook(struct vnode **vpp, struct file *fp, struct lwp *l)
1210 {
1211 	struct vnode *vp = *vpp, *lvp;
1212 	struct vattr va;
1213 	int error;
1214 
1215 	if (vp->v_op != union_vnodeop_p)
1216 		return (0);
1217 
1218 	if ((lvp = union_dircache(vp, l)) == NULLVP)
1219 		return (0);
1220 
1221 	/*
1222 	 * If the directory is opaque,
1223 	 * then don't show lower entries
1224 	 */
1225 	error = VOP_GETATTR(vp, &va, fp->f_cred, l);
1226 	if (error || (va.va_flags & OPAQUE)) {
1227 		vput(lvp);
1228 		return (error);
1229 	}
1230 
1231 	error = VOP_OPEN(lvp, FREAD, fp->f_cred, l);
1232 	if (error) {
1233 		vput(lvp);
1234 		return (error);
1235 	}
1236 	VOP_UNLOCK(lvp, 0);
1237 	fp->f_data = lvp;
1238 	fp->f_offset = 0;
1239 	error = vn_close(vp, FREAD, fp->f_cred, l);
1240 	if (error)
1241 		return (error);
1242 	*vpp = lvp;
1243 	return (0);
1244 }
1245