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