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