xref: /freebsd/sys/fs/unionfs/union_subr.c (revision ce834215a70ff69e7e222827437116eee2f9ac6f)
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  * $Id: union_subr.c,v 1.17 1997/04/13 06:27:09 phk Exp $
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/namei.h>
47 #include <sys/malloc.h>
48 #include <sys/fcntl.h>
49 #include <sys/filedesc.h>
50 #include <sys/queue.h>
51 #include <sys/mount.h>
52 #include <sys/stat.h>
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>	/* for vnode_pager_setsize */
55 #include <miscfs/union/union.h>
56 
57 #include <sys/proc.h>
58 
59 extern int	union_init __P((void));
60 
61 /* must be power of two, otherwise change UNION_HASH() */
62 #define NHASH 32
63 
64 /* unsigned int ... */
65 #define UNION_HASH(u, l) \
66 	(((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
67 
68 static LIST_HEAD(unhead, union_node) unhead[NHASH];
69 static int unvplock[NHASH];
70 
71 static void	union_dircache_r __P((struct vnode *vp, struct vnode ***vppp,
72 				      int *cntp));
73 static int	union_list_lock __P((int ix));
74 static void	union_list_unlock __P((int ix));
75 static int	union_relookup __P((struct union_mount *um, struct vnode *dvp,
76 				    struct vnode **vpp,
77 				    struct componentname *cnp,
78 				    struct componentname *cn, char *path,
79 				    int pathlen));
80 extern void	union_updatevp __P((struct union_node *un,
81 				    struct vnode *uppervp,
82 				    struct vnode *lowervp));
83 
84 int
85 union_init()
86 {
87 	int i;
88 
89 	for (i = 0; i < NHASH; i++)
90 		LIST_INIT(&unhead[i]);
91 	bzero((caddr_t) unvplock, sizeof(unvplock));
92 	return (0);
93 }
94 
95 static int
96 union_list_lock(ix)
97 	int ix;
98 {
99 
100 	if (unvplock[ix] & UN_LOCKED) {
101 		unvplock[ix] |= UN_WANT;
102 		(void) tsleep((caddr_t) &unvplock[ix], PINOD, "unllck", 0);
103 		return (1);
104 	}
105 
106 	unvplock[ix] |= UN_LOCKED;
107 
108 	return (0);
109 }
110 
111 static void
112 union_list_unlock(ix)
113 	int ix;
114 {
115 
116 	unvplock[ix] &= ~UN_LOCKED;
117 
118 	if (unvplock[ix] & UN_WANT) {
119 		unvplock[ix] &= ~UN_WANT;
120 		wakeup((caddr_t) &unvplock[ix]);
121 	}
122 }
123 
124 void
125 union_updatevp(un, uppervp, lowervp)
126 	struct union_node *un;
127 	struct vnode *uppervp;
128 	struct vnode *lowervp;
129 {
130 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
131 	int nhash = UNION_HASH(uppervp, lowervp);
132 	int docache = (lowervp != NULLVP || uppervp != NULLVP);
133 	int lhash, hhash, uhash;
134 
135 	/*
136 	 * Ensure locking is ordered from lower to higher
137 	 * to avoid deadlocks.
138 	 */
139 	if (nhash < ohash) {
140 		lhash = nhash;
141 		uhash = ohash;
142 	} else {
143 		lhash = ohash;
144 		uhash = nhash;
145 	}
146 
147 	if (lhash != uhash)
148 		while (union_list_lock(lhash))
149 			continue;
150 
151 	while (union_list_lock(uhash))
152 		continue;
153 
154 	if (ohash != nhash || !docache) {
155 		if (un->un_flags & UN_CACHED) {
156 			un->un_flags &= ~UN_CACHED;
157 			LIST_REMOVE(un, un_cache);
158 		}
159 	}
160 
161 	if (ohash != nhash)
162 		union_list_unlock(ohash);
163 
164 	if (un->un_lowervp != lowervp) {
165 		if (un->un_lowervp) {
166 			vrele(un->un_lowervp);
167 			if (un->un_path) {
168 				free(un->un_path, M_TEMP);
169 				un->un_path = 0;
170 			}
171 			if (un->un_dirvp) {
172 				vrele(un->un_dirvp);
173 				un->un_dirvp = NULLVP;
174 			}
175 		}
176 		un->un_lowervp = lowervp;
177 		un->un_lowersz = VNOVAL;
178 	}
179 
180 	if (un->un_uppervp != uppervp) {
181 		if (un->un_uppervp)
182 			vrele(un->un_uppervp);
183 
184 		un->un_uppervp = uppervp;
185 		un->un_uppersz = VNOVAL;
186 	}
187 
188 	if (docache && (ohash != nhash)) {
189 		LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
190 		un->un_flags |= UN_CACHED;
191 	}
192 
193 	union_list_unlock(nhash);
194 }
195 
196 void
197 union_newlower(un, lowervp)
198 	struct union_node *un;
199 	struct vnode *lowervp;
200 {
201 
202 	union_updatevp(un, un->un_uppervp, lowervp);
203 }
204 
205 void
206 union_newupper(un, uppervp)
207 	struct union_node *un;
208 	struct vnode *uppervp;
209 {
210 
211 	union_updatevp(un, uppervp, un->un_lowervp);
212 }
213 
214 /*
215  * Keep track of size changes in the underlying vnodes.
216  * If the size changes, then callback to the vm layer
217  * giving priority to the upper layer size.
218  */
219 void
220 union_newsize(vp, uppersz, lowersz)
221 	struct vnode *vp;
222 	off_t uppersz, lowersz;
223 {
224 	struct union_node *un;
225 	off_t sz;
226 
227 	/* only interested in regular files */
228 	if (vp->v_type != VREG)
229 		return;
230 
231 	un = VTOUNION(vp);
232 	sz = VNOVAL;
233 
234 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
235 		un->un_uppersz = uppersz;
236 		if (sz == VNOVAL)
237 			sz = un->un_uppersz;
238 	}
239 
240 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
241 		un->un_lowersz = lowersz;
242 		if (sz == VNOVAL)
243 			sz = un->un_lowersz;
244 	}
245 
246 	if (sz != VNOVAL) {
247 #ifdef UNION_DIAGNOSTIC
248 		printf("union: %s size now %ld\n",
249 			uppersz != VNOVAL ? "upper" : "lower", (long) sz);
250 #endif
251 		vnode_pager_setsize(vp, sz);
252 	}
253 }
254 
255 /*
256  * allocate a union_node/vnode pair.  the vnode is
257  * referenced and locked.  the new vnode is returned
258  * via (vpp).  (mp) is the mountpoint of the union filesystem,
259  * (dvp) is the parent directory where the upper layer object
260  * should exist (but doesn't) and (cnp) is the componentname
261  * information which is partially copied to allow the upper
262  * layer object to be created at a later time.  (uppervp)
263  * and (lowervp) reference the upper and lower layer objects
264  * being mapped.  either, but not both, can be nil.
265  * if supplied, (uppervp) is locked.
266  * the reference is either maintained in the new union_node
267  * object which is allocated, or they are vrele'd.
268  *
269  * all union_nodes are maintained on a singly-linked
270  * list.  new nodes are only allocated when they cannot
271  * be found on this list.  entries on the list are
272  * removed when the vfs reclaim entry is called.
273  *
274  * a single lock is kept for the entire list.  this is
275  * needed because the getnewvnode() function can block
276  * waiting for a vnode to become free, in which case there
277  * may be more than one process trying to get the same
278  * vnode.  this lock is only taken if we are going to
279  * call getnewvnode, since the kernel itself is single-threaded.
280  *
281  * if an entry is found on the list, then call vget() to
282  * take a reference.  this is done because there may be
283  * zero references to it and so it needs to removed from
284  * the vnode free list.
285  */
286 int
287 union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp, docache)
288 	struct vnode **vpp;
289 	struct mount *mp;
290 	struct vnode *undvp;		/* parent union vnode */
291 	struct vnode *dvp;		/* may be null */
292 	struct componentname *cnp;	/* may be null */
293 	struct vnode *uppervp;		/* may be null */
294 	struct vnode *lowervp;		/* may be null */
295 	int docache;
296 {
297 	int error;
298 	struct union_node *un = 0;
299 	struct vnode *xlowervp = NULLVP;
300 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
301 	int hash;
302 	int vflag;
303 	int try;
304 
305 	if (uppervp == NULLVP && lowervp == NULLVP)
306 		panic("union: unidentifiable allocation");
307 
308 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
309 		xlowervp = lowervp;
310 		lowervp = NULLVP;
311 	}
312 
313 	/* detect the root vnode (and aliases) */
314 	vflag = 0;
315 	if ((uppervp == um->um_uppervp) &&
316 	    ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
317 		if (lowervp == NULLVP) {
318 			lowervp = um->um_lowervp;
319 			if (lowervp != NULLVP)
320 				VREF(lowervp);
321 		}
322 		vflag = VROOT;
323 	}
324 
325 loop:
326 	if (!docache) {
327 		un = 0;
328 	} else for (try = 0; try < 3; try++) {
329 		switch (try) {
330 		case 0:
331 			if (lowervp == NULLVP)
332 				continue;
333 			hash = UNION_HASH(uppervp, lowervp);
334 			break;
335 
336 		case 1:
337 			if (uppervp == NULLVP)
338 				continue;
339 			hash = UNION_HASH(uppervp, NULLVP);
340 			break;
341 
342 		case 2:
343 			if (lowervp == NULLVP)
344 				continue;
345 			hash = UNION_HASH(NULLVP, lowervp);
346 			break;
347 		}
348 
349 		while (union_list_lock(hash))
350 			continue;
351 
352 		for (un = unhead[hash].lh_first; un != 0;
353 					un = un->un_cache.le_next) {
354 			if ((un->un_lowervp == lowervp ||
355 			     un->un_lowervp == NULLVP) &&
356 			    (un->un_uppervp == uppervp ||
357 			     un->un_uppervp == NULLVP) &&
358 			    (UNIONTOV(un)->v_mount == mp)) {
359 				if (vget(UNIONTOV(un), 0,
360 				    cnp ? cnp->cn_proc : NULL)) {
361 					union_list_unlock(hash);
362 					goto loop;
363 				}
364 				break;
365 			}
366 		}
367 
368 		union_list_unlock(hash);
369 
370 		if (un)
371 			break;
372 	}
373 
374 	if (un) {
375 		/*
376 		 * Obtain a lock on the union_node.
377 		 * uppervp is locked, though un->un_uppervp
378 		 * may not be.  this doesn't break the locking
379 		 * hierarchy since in the case that un->un_uppervp
380 		 * is not yet locked it will be vrele'd and replaced
381 		 * with uppervp.
382 		 */
383 
384 		if ((dvp != NULLVP) && (uppervp == dvp)) {
385 			/*
386 			 * Access ``.'', so (un) will already
387 			 * be locked.  Since this process has
388 			 * the lock on (uppervp) no other
389 			 * process can hold the lock on (un).
390 			 */
391 #ifdef DIAGNOSTIC
392 			if ((un->un_flags & UN_LOCKED) == 0)
393 				panic("union: . not locked");
394 			else if (curproc && un->un_pid != curproc->p_pid &&
395 				    un->un_pid > -1 && curproc->p_pid > -1)
396 				panic("union: allocvp not lock owner");
397 #endif
398 		} else {
399 			if (un->un_flags & UN_LOCKED) {
400 				vrele(UNIONTOV(un));
401 				un->un_flags |= UN_WANT;
402 				(void) tsleep((caddr_t) &un->un_flags, PINOD, "unalvp", 0);
403 				goto loop;
404 			}
405 			un->un_flags |= UN_LOCKED;
406 
407 #ifdef DIAGNOSTIC
408 			if (curproc)
409 				un->un_pid = curproc->p_pid;
410 			else
411 				un->un_pid = -1;
412 #endif
413 		}
414 
415 		/*
416 		 * At this point, the union_node is locked,
417 		 * un->un_uppervp may not be locked, and uppervp
418 		 * is locked or nil.
419 		 */
420 
421 		/*
422 		 * Save information about the upper layer.
423 		 */
424 		if (uppervp != un->un_uppervp) {
425 			union_newupper(un, uppervp);
426 		} else if (uppervp) {
427 			vrele(uppervp);
428 		}
429 
430 		if (un->un_uppervp) {
431 			un->un_flags |= UN_ULOCK;
432 			un->un_flags &= ~UN_KLOCK;
433 		}
434 
435 		/*
436 		 * Save information about the lower layer.
437 		 * This needs to keep track of pathname
438 		 * and directory information which union_vn_create
439 		 * might need.
440 		 */
441 		if (lowervp != un->un_lowervp) {
442 			union_newlower(un, lowervp);
443 			if (cnp && (lowervp != NULLVP)) {
444 				un->un_hash = cnp->cn_hash;
445 				un->un_path = malloc(cnp->cn_namelen+1,
446 						M_TEMP, M_WAITOK);
447 				bcopy(cnp->cn_nameptr, un->un_path,
448 						cnp->cn_namelen);
449 				un->un_path[cnp->cn_namelen] = '\0';
450 				VREF(dvp);
451 				un->un_dirvp = dvp;
452 			}
453 		} else if (lowervp) {
454 			vrele(lowervp);
455 		}
456 		*vpp = UNIONTOV(un);
457 		return (0);
458 	}
459 
460 	if (docache) {
461 		/*
462 		 * otherwise lock the vp list while we call getnewvnode
463 		 * since that can block.
464 		 */
465 		hash = UNION_HASH(uppervp, lowervp);
466 
467 		if (union_list_lock(hash))
468 			goto loop;
469 	}
470 
471 	error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
472 	if (error) {
473 		if (uppervp) {
474 			if (dvp == uppervp)
475 				vrele(uppervp);
476 			else
477 				vput(uppervp);
478 		}
479 		if (lowervp)
480 			vrele(lowervp);
481 
482 		goto out;
483 	}
484 
485 	MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
486 		M_TEMP, M_WAITOK);
487 
488 	(*vpp)->v_flag |= vflag;
489 	if (uppervp)
490 		(*vpp)->v_type = uppervp->v_type;
491 	else
492 		(*vpp)->v_type = lowervp->v_type;
493 	un = VTOUNION(*vpp);
494 	un->un_vnode = *vpp;
495 	un->un_uppervp = uppervp;
496 	un->un_uppersz = VNOVAL;
497 	un->un_lowervp = lowervp;
498 	un->un_lowersz = VNOVAL;
499 	un->un_pvp = undvp;
500 	if (undvp != NULLVP)
501 		VREF(undvp);
502 	un->un_dircache = 0;
503 	un->un_openl = 0;
504 	un->un_flags = UN_LOCKED;
505 	if (un->un_uppervp)
506 		un->un_flags |= UN_ULOCK;
507 #ifdef DIAGNOSTIC
508 	if (curproc)
509 		un->un_pid = curproc->p_pid;
510 	else
511 		un->un_pid = -1;
512 #endif
513 	if (cnp && (lowervp != NULLVP)) {
514 		un->un_hash = cnp->cn_hash;
515 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
516 		bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
517 		un->un_path[cnp->cn_namelen] = '\0';
518 		VREF(dvp);
519 		un->un_dirvp = dvp;
520 	} else {
521 		un->un_hash = 0;
522 		un->un_path = 0;
523 		un->un_dirvp = 0;
524 	}
525 
526 	if (docache) {
527 		LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
528 		un->un_flags |= UN_CACHED;
529 	}
530 
531 	if (xlowervp)
532 		vrele(xlowervp);
533 
534 out:
535 	if (docache)
536 		union_list_unlock(hash);
537 
538 	return (error);
539 }
540 
541 int
542 union_freevp(vp)
543 	struct vnode *vp;
544 {
545 	struct union_node *un = VTOUNION(vp);
546 
547 	if (un->un_flags & UN_CACHED) {
548 		un->un_flags &= ~UN_CACHED;
549 		LIST_REMOVE(un, un_cache);
550 	}
551 
552 	if (un->un_pvp != NULLVP)
553 		vrele(un->un_pvp);
554 	if (un->un_uppervp != NULLVP)
555 		vrele(un->un_uppervp);
556 	if (un->un_lowervp != NULLVP)
557 		vrele(un->un_lowervp);
558 	if (un->un_dirvp != NULLVP)
559 		vrele(un->un_dirvp);
560 	if (un->un_path)
561 		free(un->un_path, M_TEMP);
562 
563 	FREE(vp->v_data, M_TEMP);
564 	vp->v_data = 0;
565 
566 	return (0);
567 }
568 
569 /*
570  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
571  * using a sequence of reads and writes.  both (fvp)
572  * and (tvp) are locked on entry and exit.
573  */
574 int
575 union_copyfile(fvp, tvp, cred, p)
576 	struct vnode *fvp;
577 	struct vnode *tvp;
578 	struct ucred *cred;
579 	struct proc *p;
580 {
581 	char *buf;
582 	struct uio uio;
583 	struct iovec iov;
584 	int error = 0;
585 
586 	/*
587 	 * strategy:
588 	 * allocate a buffer of size MAXBSIZE.
589 	 * loop doing reads and writes, keeping track
590 	 * of the current uio offset.
591 	 * give up at the first sign of trouble.
592 	 */
593 
594 	uio.uio_procp = p;
595 	uio.uio_segflg = UIO_SYSSPACE;
596 	uio.uio_offset = 0;
597 
598 	VOP_UNLOCK(fvp, 0, p);				/* XXX */
599 	VOP_LEASE(fvp, p, cred, LEASE_READ);
600 	vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY, p);	/* XXX */
601 	VOP_UNLOCK(tvp, 0, p);				/* XXX */
602 	VOP_LEASE(tvp, p, cred, LEASE_WRITE);
603 	vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY, p);	/* XXX */
604 
605 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
606 
607 	/* ugly loop follows... */
608 	do {
609 		off_t offset = uio.uio_offset;
610 
611 		uio.uio_iov = &iov;
612 		uio.uio_iovcnt = 1;
613 		iov.iov_base = buf;
614 		iov.iov_len = MAXBSIZE;
615 		uio.uio_resid = iov.iov_len;
616 		uio.uio_rw = UIO_READ;
617 		error = VOP_READ(fvp, &uio, 0, cred);
618 
619 		if (error == 0) {
620 			uio.uio_iov = &iov;
621 			uio.uio_iovcnt = 1;
622 			iov.iov_base = buf;
623 			iov.iov_len = MAXBSIZE - uio.uio_resid;
624 			uio.uio_offset = offset;
625 			uio.uio_rw = UIO_WRITE;
626 			uio.uio_resid = iov.iov_len;
627 
628 			if (uio.uio_resid == 0)
629 				break;
630 
631 			do {
632 				error = VOP_WRITE(tvp, &uio, 0, cred);
633 			} while ((uio.uio_resid > 0) && (error == 0));
634 		}
635 
636 	} while (error == 0);
637 
638 	free(buf, M_TEMP);
639 	return (error);
640 }
641 
642 /*
643  * (un) is assumed to be locked on entry and remains
644  * locked on exit.
645  */
646 int
647 union_copyup(un, docopy, cred, p)
648 	struct union_node *un;
649 	int docopy;
650 	struct ucred *cred;
651 	struct proc *p;
652 {
653 	int error;
654 	struct vnode *lvp, *uvp;
655 
656 	error = union_vn_create(&uvp, un, p);
657 	if (error)
658 		return (error);
659 
660 	/* at this point, uppervp is locked */
661 	union_newupper(un, uvp);
662 	un->un_flags |= UN_ULOCK;
663 
664 	lvp = un->un_lowervp;
665 
666 	if (docopy) {
667 		/*
668 		 * XX - should not ignore errors
669 		 * from VOP_CLOSE
670 		 */
671 		vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY, p);
672 		error = VOP_OPEN(lvp, FREAD, cred, p);
673 		if (error == 0) {
674 			error = union_copyfile(lvp, uvp, cred, p);
675 			VOP_UNLOCK(lvp, 0, p);
676 			(void) VOP_CLOSE(lvp, FREAD, cred, p);
677 		}
678 #ifdef UNION_DIAGNOSTIC
679 		if (error == 0)
680 			uprintf("union: copied up %s\n", un->un_path);
681 #endif
682 
683 	}
684 	un->un_flags &= ~UN_ULOCK;
685 	VOP_UNLOCK(uvp, 0, p);
686 	union_vn_close(uvp, FWRITE, cred, p);
687 	vn_lock(uvp, LK_EXCLUSIVE | LK_RETRY, p);
688 	un->un_flags |= UN_ULOCK;
689 
690 	/*
691 	 * Subsequent IOs will go to the top layer, so
692 	 * call close on the lower vnode and open on the
693 	 * upper vnode to ensure that the filesystem keeps
694 	 * its references counts right.  This doesn't do
695 	 * the right thing with (cred) and (FREAD) though.
696 	 * Ignoring error returns is not right, either.
697 	 */
698 	if (error == 0) {
699 		int i;
700 
701 		for (i = 0; i < un->un_openl; i++) {
702 			(void) VOP_CLOSE(lvp, FREAD, cred, p);
703 			(void) VOP_OPEN(uvp, FREAD, cred, p);
704 		}
705 		un->un_openl = 0;
706 	}
707 
708 	return (error);
709 
710 }
711 
712 static int
713 union_relookup(um, dvp, vpp, cnp, cn, path, pathlen)
714 	struct union_mount *um;
715 	struct vnode *dvp;
716 	struct vnode **vpp;
717 	struct componentname *cnp;
718 	struct componentname *cn;
719 	char *path;
720 	int pathlen;
721 {
722 	int error;
723 
724 	/*
725 	 * A new componentname structure must be faked up because
726 	 * there is no way to know where the upper level cnp came
727 	 * from or what it is being used for.  This must duplicate
728 	 * some of the work done by NDINIT, some of the work done
729 	 * by namei, some of the work done by lookup and some of
730 	 * the work done by VOP_LOOKUP when given a CREATE flag.
731 	 * Conclusion: Horrible.
732 	 *
733 	 * The pathname buffer will be FREEed by VOP_MKDIR.
734 	 */
735 	cn->cn_namelen = pathlen;
736 	cn->cn_pnbuf = malloc(cn->cn_namelen+1, M_NAMEI, M_WAITOK);
737 	bcopy(path, cn->cn_pnbuf, cn->cn_namelen);
738 	cn->cn_pnbuf[cn->cn_namelen] = '\0';
739 
740 	cn->cn_nameiop = CREATE;
741 	cn->cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
742 	cn->cn_proc = cnp->cn_proc;
743 	if (um->um_op == UNMNT_ABOVE)
744 		cn->cn_cred = cnp->cn_cred;
745 	else
746 		cn->cn_cred = um->um_cred;
747 	cn->cn_nameptr = cn->cn_pnbuf;
748 	cn->cn_hash = cnp->cn_hash;
749 	cn->cn_consume = cnp->cn_consume;
750 
751 	VREF(dvp);
752 	error = relookup(dvp, vpp, cn);
753 	if (!error)
754 		vrele(dvp);
755 	else {
756 		free(cn->cn_pnbuf, M_NAMEI);
757 		cn->cn_pnbuf = '\0';
758 	}
759 
760 	return (error);
761 }
762 
763 /*
764  * Create a shadow directory in the upper layer.
765  * The new vnode is returned locked.
766  *
767  * (um) points to the union mount structure for access to the
768  * the mounting process's credentials.
769  * (dvp) is the directory in which to create the shadow directory.
770  * it is unlocked on entry and exit.
771  * (cnp) is the componentname to be created.
772  * (vpp) is the returned newly created shadow directory, which
773  * is returned locked.
774  */
775 int
776 union_mkshadow(um, dvp, cnp, vpp)
777 	struct union_mount *um;
778 	struct vnode *dvp;
779 	struct componentname *cnp;
780 	struct vnode **vpp;
781 {
782 	int error;
783 	struct vattr va;
784 	struct proc *p = cnp->cn_proc;
785 	struct componentname cn;
786 
787 	error = union_relookup(um, dvp, vpp, cnp, &cn,
788 			cnp->cn_nameptr, cnp->cn_namelen);
789 	if (error)
790 		return (error);
791 
792 	if (*vpp) {
793 		VOP_ABORTOP(dvp, &cn);
794 		VOP_UNLOCK(dvp, 0, p);
795 		vrele(*vpp);
796 		*vpp = NULLVP;
797 		return (EEXIST);
798 	}
799 
800 	/*
801 	 * policy: when creating the shadow directory in the
802 	 * upper layer, create it owned by the user who did
803 	 * the mount, group from parent directory, and mode
804 	 * 777 modified by umask (ie mostly identical to the
805 	 * mkdir syscall).  (jsp, kb)
806 	 */
807 
808 	VATTR_NULL(&va);
809 	va.va_type = VDIR;
810 	va.va_mode = um->um_cmode;
811 
812 	/* VOP_LEASE: dvp is locked */
813 	VOP_LEASE(dvp, p, cn.cn_cred, LEASE_WRITE);
814 
815 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
816 	return (error);
817 }
818 
819 /*
820  * Create a whiteout entry in the upper layer.
821  *
822  * (um) points to the union mount structure for access to the
823  * the mounting process's credentials.
824  * (dvp) is the directory in which to create the whiteout.
825  * it is locked on entry and exit.
826  * (cnp) is the componentname to be created.
827  */
828 int
829 union_mkwhiteout(um, dvp, cnp, path)
830 	struct union_mount *um;
831 	struct vnode *dvp;
832 	struct componentname *cnp;
833 	char *path;
834 {
835 	int error;
836 	struct vattr va;
837 	struct proc *p = cnp->cn_proc;
838 	struct vnode *wvp;
839 	struct componentname cn;
840 
841 	VOP_UNLOCK(dvp, 0, p);
842 	error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
843 	if (error) {
844 		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY, p);
845 		return (error);
846 	}
847 
848 	if (wvp) {
849 		VOP_ABORTOP(dvp, &cn);
850 		vrele(dvp);
851 		vrele(wvp);
852 		return (EEXIST);
853 	}
854 
855 	/* VOP_LEASE: dvp is locked */
856 	VOP_LEASE(dvp, p, p->p_ucred, LEASE_WRITE);
857 
858 	error = VOP_WHITEOUT(dvp, &cn, CREATE);
859 	if (error)
860 		VOP_ABORTOP(dvp, &cn);
861 
862 	vrele(dvp);
863 
864 	return (error);
865 }
866 
867 /*
868  * union_vn_create: creates and opens a new shadow file
869  * on the upper union layer.  this function is similar
870  * in spirit to calling vn_open but it avoids calling namei().
871  * the problem with calling namei is that a) it locks too many
872  * things, and b) it doesn't start at the "right" directory,
873  * whereas relookup is told where to start.
874  */
875 int
876 union_vn_create(vpp, un, p)
877 	struct vnode **vpp;
878 	struct union_node *un;
879 	struct proc *p;
880 {
881 	struct vnode *vp;
882 	struct ucred *cred = p->p_ucred;
883 	struct vattr vat;
884 	struct vattr *vap = &vat;
885 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
886 	int error;
887 	int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
888 	struct componentname cn;
889 
890 	*vpp = NULLVP;
891 
892 	/*
893 	 * Build a new componentname structure (for the same
894 	 * reasons outlines in union_mkshadow).
895 	 * The difference here is that the file is owned by
896 	 * the current user, rather than by the person who
897 	 * did the mount, since the current user needs to be
898 	 * able to write the file (that's why it is being
899 	 * copied in the first place).
900 	 */
901 	cn.cn_namelen = strlen(un->un_path);
902 	cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen+1, M_NAMEI, M_WAITOK);
903 	bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
904 	cn.cn_nameiop = CREATE;
905 	cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
906 	cn.cn_proc = p;
907 	cn.cn_cred = p->p_ucred;
908 	cn.cn_nameptr = cn.cn_pnbuf;
909 	cn.cn_hash = un->un_hash;
910 	cn.cn_consume = 0;
911 
912 	VREF(un->un_dirvp);
913 	error = relookup(un->un_dirvp, &vp, &cn);
914 	if (error)
915 		return (error);
916 	vrele(un->un_dirvp);
917 
918 	if (vp) {
919 		VOP_ABORTOP(un->un_dirvp, &cn);
920 		if (un->un_dirvp == vp)
921 			vrele(un->un_dirvp);
922 		else
923 			vput(un->un_dirvp);
924 		vrele(vp);
925 		return (EEXIST);
926 	}
927 
928 	/*
929 	 * Good - there was no race to create the file
930 	 * so go ahead and create it.  The permissions
931 	 * on the file will be 0666 modified by the
932 	 * current user's umask.  Access to the file, while
933 	 * it is unioned, will require access to the top *and*
934 	 * bottom files.  Access when not unioned will simply
935 	 * require access to the top-level file.
936 	 * TODO: confirm choice of access permissions.
937 	 */
938 	VATTR_NULL(vap);
939 	vap->va_type = VREG;
940 	vap->va_mode = cmode;
941 	VOP_LEASE(un->un_dirvp, p, cred, LEASE_WRITE);
942 	if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap))
943 		return (error);
944 
945 	error = VOP_OPEN(vp, fmode, cred, p);
946 	if (error) {
947 		vput(vp);
948 		return (error);
949 	}
950 
951 	vp->v_writecount++;
952 	*vpp = vp;
953 	return (0);
954 }
955 
956 int
957 union_vn_close(vp, fmode, cred, p)
958 	struct vnode *vp;
959 	int fmode;
960 	struct ucred *cred;
961 	struct proc *p;
962 {
963 
964 	if (fmode & FWRITE)
965 		--vp->v_writecount;
966 	return (VOP_CLOSE(vp, fmode, cred, p));
967 }
968 
969 void
970 union_removed_upper(un)
971 	struct union_node *un;
972 {
973 	struct proc *p = curproc;	/* XXX */
974 	struct vnode **vpp;
975 
976 	/*
977 	 * Do not set the uppervp to NULLVP.  If lowervp is NULLVP,
978 	 * union node will have neither uppervp nor lowervp.  We romove
979 	 * the union node from cache, so that it will not be referrenced.
980 	 */
981 #if 0
982 	union_newupper(un, NULLVP);
983 #endif
984 	if (un->un_dircache != 0) {
985 		for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
986 			vrele(*vpp);
987 		free(un->un_dircache, M_TEMP);
988 		un->un_dircache = 0;
989 	}
990 
991 	if (un->un_flags & UN_CACHED) {
992 		un->un_flags &= ~UN_CACHED;
993 		LIST_REMOVE(un, un_cache);
994 	}
995 
996 	if (un->un_flags & UN_ULOCK) {
997 		un->un_flags &= ~UN_ULOCK;
998 		VOP_UNLOCK(un->un_uppervp, 0, p);
999 	}
1000 }
1001 
1002 #if 0
1003 struct vnode *
1004 union_lowervp(vp)
1005 	struct vnode *vp;
1006 {
1007 	struct union_node *un = VTOUNION(vp);
1008 
1009 	if ((un->un_lowervp != NULLVP) &&
1010 	    (vp->v_type == un->un_lowervp->v_type)) {
1011 		if (vget(un->un_lowervp, 0) == 0)
1012 			return (un->un_lowervp);
1013 	}
1014 
1015 	return (NULLVP);
1016 }
1017 #endif
1018 
1019 /*
1020  * determine whether a whiteout is needed
1021  * during a remove/rmdir operation.
1022  */
1023 int
1024 union_dowhiteout(un, cred, p)
1025 	struct union_node *un;
1026 	struct ucred *cred;
1027 	struct proc *p;
1028 {
1029 	struct vattr va;
1030 
1031 	if (un->un_lowervp != NULLVP)
1032 		return (1);
1033 
1034 	if (VOP_GETATTR(un->un_uppervp, &va, cred, p) == 0 &&
1035 	    (va.va_flags & OPAQUE))
1036 		return (1);
1037 
1038 	return (0);
1039 }
1040 
1041 static void
1042 union_dircache_r(vp, vppp, cntp)
1043 	struct vnode *vp;
1044 	struct vnode ***vppp;
1045 	int *cntp;
1046 {
1047 	struct union_node *un;
1048 
1049 	if (vp->v_op != union_vnodeop_p) {
1050 		if (vppp) {
1051 			VREF(vp);
1052 			*(*vppp)++ = vp;
1053 			if (--(*cntp) == 0)
1054 				panic("union: dircache table too small");
1055 		} else {
1056 			(*cntp)++;
1057 		}
1058 
1059 		return;
1060 	}
1061 
1062 	un = VTOUNION(vp);
1063 	if (un->un_uppervp != NULLVP)
1064 		union_dircache_r(un->un_uppervp, vppp, cntp);
1065 	if (un->un_lowervp != NULLVP)
1066 		union_dircache_r(un->un_lowervp, vppp, cntp);
1067 }
1068 
1069 struct vnode *
1070 union_dircache(vp, p)
1071 	struct vnode *vp;
1072 	struct proc *p;
1073 {
1074 	int cnt;
1075 	struct vnode *nvp;
1076 	struct vnode **vpp;
1077 	struct vnode **dircache;
1078 	struct union_node *un;
1079 	int error;
1080 
1081 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1082 	dircache = VTOUNION(vp)->un_dircache;
1083 
1084 	nvp = NULLVP;
1085 
1086 	if (dircache == 0) {
1087 		cnt = 0;
1088 		union_dircache_r(vp, 0, &cnt);
1089 		cnt++;
1090 		dircache = (struct vnode **)
1091 				malloc(cnt * sizeof(struct vnode *),
1092 					M_TEMP, M_WAITOK);
1093 		vpp = dircache;
1094 		union_dircache_r(vp, &vpp, &cnt);
1095 		*vpp = NULLVP;
1096 		vpp = dircache + 1;
1097 	} else {
1098 		vpp = dircache;
1099 		do {
1100 			if (*vpp++ == VTOUNION(vp)->un_uppervp)
1101 				break;
1102 		} while (*vpp != NULLVP);
1103 	}
1104 
1105 	if (*vpp == NULLVP)
1106 		goto out;
1107 
1108 	vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY, p);
1109 	VREF(*vpp);
1110 	error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0);
1111 	if (error)
1112 		goto out;
1113 
1114 	VTOUNION(vp)->un_dircache = 0;
1115 	un = VTOUNION(nvp);
1116 	un->un_dircache = dircache;
1117 
1118 out:
1119 	VOP_UNLOCK(vp, 0, p);
1120 	return (nvp);
1121 }
1122