xref: /csrg-svn/sys/miscfs/union/union_subr.c (revision 67166)
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  * %sccs.include.redist.c%
10  *
11  *	@(#)union_subr.c	8.8 (Berkeley) 05/17/94
12  */
13 
14 #include <sys/param.h>
15 #include <sys/systm.h>
16 #include <sys/time.h>
17 #include <sys/kernel.h>
18 #include <sys/vnode.h>
19 #include <sys/namei.h>
20 #include <sys/malloc.h>
21 #include <sys/file.h>
22 #include <sys/filedesc.h>
23 #include <sys/queue.h>
24 #include <sys/mount.h>
25 #include <vm/vm.h>		/* for vnode_pager_setsize */
26 #include <miscfs/union/union.h>
27 
28 #ifdef DIAGNOSTIC
29 #include <sys/proc.h>
30 #endif
31 
32 /* must be power of two, otherwise change UNION_HASH() */
33 #define NHASH 32
34 
35 /* unsigned int ... */
36 #define UNION_HASH(u, l) \
37 	(((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
38 
39 static LIST_HEAD(unhead, union_node) unhead[NHASH];
40 static int unvplock[NHASH];
41 
42 int
43 union_init()
44 {
45 	int i;
46 
47 	for (i = 0; i < NHASH; i++)
48 		LIST_INIT(&unhead[i]);
49 	bzero((caddr_t) unvplock, sizeof(unvplock));
50 }
51 
52 static int
53 union_list_lock(ix)
54 	int ix;
55 {
56 
57 	if (unvplock[ix] & UN_LOCKED) {
58 		unvplock[ix] |= UN_WANT;
59 		sleep((caddr_t) &unvplock[ix], PINOD);
60 		return (1);
61 	}
62 
63 	unvplock[ix] |= UN_LOCKED;
64 
65 	return (0);
66 }
67 
68 static void
69 union_list_unlock(ix)
70 	int ix;
71 {
72 
73 	unvplock[ix] &= ~UN_LOCKED;
74 
75 	if (unvplock[ix] & UN_WANT) {
76 		unvplock[ix] &= ~UN_WANT;
77 		wakeup((caddr_t) &unvplock[ix]);
78 	}
79 }
80 
81 void
82 union_updatevp(un, uppervp, lowervp)
83 	struct union_node *un;
84 	struct vnode *uppervp;
85 	struct vnode *lowervp;
86 {
87 	int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
88 	int nhash = UNION_HASH(uppervp, lowervp);
89 	int docache = (lowervp != NULLVP || uppervp != NULLVP);
90 
91 	/*
92 	 * Ensure locking is ordered from lower to higher
93 	 * to avoid deadlocks.
94 	 */
95 	if (nhash < ohash) {
96 		int t = ohash;
97 		ohash = nhash;
98 		nhash = t;
99 	}
100 
101 	if (ohash != nhash)
102 		while (union_list_lock(ohash))
103 			continue;
104 
105 	while (union_list_lock(nhash))
106 		continue;
107 
108 	if (ohash != nhash || !docache) {
109 		if (un->un_flags & UN_CACHED) {
110 			LIST_REMOVE(un, un_cache);
111 			un->un_flags &= ~UN_CACHED;
112 		}
113 	}
114 
115 	if (ohash != nhash)
116 		union_list_unlock(ohash);
117 
118 	if (un->un_lowervp != lowervp) {
119 		if (un->un_lowervp) {
120 			vrele(un->un_lowervp);
121 			if (un->un_path) {
122 				free(un->un_path, M_TEMP);
123 				un->un_path = 0;
124 			}
125 			if (un->un_dirvp) {
126 				vrele(un->un_dirvp);
127 				un->un_dirvp = NULLVP;
128 			}
129 		}
130 		un->un_lowervp = lowervp;
131 		un->un_lowersz = VNOVAL;
132 	}
133 
134 	if (un->un_uppervp != uppervp) {
135 		if (un->un_uppervp)
136 			vrele(un->un_uppervp);
137 
138 		un->un_uppervp = uppervp;
139 		un->un_uppersz = VNOVAL;
140 	}
141 
142 	if (docache && (ohash != nhash)) {
143 		LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
144 		un->un_flags |= UN_CACHED;
145 	}
146 
147 	union_list_unlock(nhash);
148 }
149 
150 void
151 union_newlower(un, lowervp)
152 	struct union_node *un;
153 	struct vnode *lowervp;
154 {
155 
156 	union_updatevp(un, un->un_uppervp, lowervp);
157 }
158 
159 void
160 union_newupper(un, uppervp)
161 	struct union_node *un;
162 	struct vnode *uppervp;
163 {
164 
165 	union_updatevp(un, uppervp, un->un_lowervp);
166 }
167 
168 /*
169  * Keep track of size changes in the underlying vnodes.
170  * If the size changes, then callback to the vm layer
171  * giving priority to the upper layer size.
172  */
173 void
174 union_newsize(vp, uppersz, lowersz)
175 	struct vnode *vp;
176 	off_t uppersz, lowersz;
177 {
178 	struct union_node *un;
179 	off_t sz;
180 
181 	/* only interested in regular files */
182 	if (vp->v_type != VREG)
183 		return;
184 
185 	un = VTOUNION(vp);
186 	sz = VNOVAL;
187 
188 	if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
189 		un->un_uppersz = uppersz;
190 		if (sz == VNOVAL)
191 			sz = un->un_uppersz;
192 	}
193 
194 	if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
195 		un->un_lowersz = lowersz;
196 		if (sz == VNOVAL)
197 			sz = un->un_lowersz;
198 	}
199 
200 	if (sz != VNOVAL) {
201 #ifdef UNION_DIAGNOSTIC
202 		printf("union: %s size now %ld\n",
203 			uppersz != VNOVAL ? "upper" : "lower", (long) sz);
204 #endif
205 		vnode_pager_setsize(vp, sz);
206 	}
207 }
208 
209 /*
210  * allocate a union_node/vnode pair.  the vnode is
211  * referenced and locked.  the new vnode is returned
212  * via (vpp).  (mp) is the mountpoint of the union filesystem,
213  * (dvp) is the parent directory where the upper layer object
214  * should exist (but doesn't) and (cnp) is the componentname
215  * information which is partially copied to allow the upper
216  * layer object to be created at a later time.  (uppervp)
217  * and (lowervp) reference the upper and lower layer objects
218  * being mapped.  either, but not both, can be nil.
219  * if supplied, (uppervp) is locked.
220  * the reference is either maintained in the new union_node
221  * object which is allocated, or they are vrele'd.
222  *
223  * all union_nodes are maintained on a singly-linked
224  * list.  new nodes are only allocated when they cannot
225  * be found on this list.  entries on the list are
226  * removed when the vfs reclaim entry is called.
227  *
228  * a single lock is kept for the entire list.  this is
229  * needed because the getnewvnode() function can block
230  * waiting for a vnode to become free, in which case there
231  * may be more than one process trying to get the same
232  * vnode.  this lock is only taken if we are going to
233  * call getnewvnode, since the kernel itself is single-threaded.
234  *
235  * if an entry is found on the list, then call vget() to
236  * take a reference.  this is done because there may be
237  * zero references to it and so it needs to removed from
238  * the vnode free list.
239  */
240 int
241 union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
242 	struct vnode **vpp;
243 	struct mount *mp;
244 	struct vnode *undvp;
245 	struct vnode *dvp;		/* may be null */
246 	struct componentname *cnp;	/* may be null */
247 	struct vnode *uppervp;		/* may be null */
248 	struct vnode *lowervp;		/* may be null */
249 {
250 	int error;
251 	struct union_node *un;
252 	struct union_node **pp;
253 	struct vnode *xlowervp = NULLVP;
254 	struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
255 	int hash;
256 	int vflag;
257 	int try;
258 
259 	if (uppervp == NULLVP && lowervp == NULLVP)
260 		panic("union: unidentifiable allocation");
261 
262 	if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
263 		xlowervp = lowervp;
264 		lowervp = NULLVP;
265 	}
266 
267 	/* detect the root vnode (and aliases) */
268 	vflag = 0;
269 	if ((uppervp == um->um_uppervp) &&
270 	    ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
271 		if (lowervp == NULLVP) {
272 			lowervp = um->um_lowervp;
273 			VREF(lowervp);
274 		}
275 		vflag = VROOT;
276 	}
277 
278 loop:
279 	for (try = 0; try < 3; try++) {
280 		switch (try) {
281 		case 0:
282 			if (lowervp == NULLVP)
283 				continue;
284 			hash = UNION_HASH(uppervp, lowervp);
285 			break;
286 
287 		case 1:
288 			if (uppervp == NULLVP)
289 				continue;
290 			hash = UNION_HASH(uppervp, NULLVP);
291 			break;
292 
293 		case 2:
294 			if (lowervp == NULLVP)
295 				continue;
296 			hash = UNION_HASH(NULLVP, lowervp);
297 			break;
298 		}
299 
300 		while (union_list_lock(hash))
301 			continue;
302 
303 		for (un = unhead[hash].lh_first; un != 0;
304 					un = un->un_cache.le_next) {
305 			if ((un->un_lowervp == lowervp ||
306 			     un->un_lowervp == NULLVP) &&
307 			    (un->un_uppervp == uppervp ||
308 			     un->un_uppervp == NULLVP) &&
309 			    (UNIONTOV(un)->v_mount == mp)) {
310 				if (vget(UNIONTOV(un), 0)) {
311 					union_list_unlock(hash);
312 					goto loop;
313 				}
314 				break;
315 			}
316 		}
317 
318 		union_list_unlock(hash);
319 
320 		if (un)
321 			break;
322 	}
323 
324 	if (un) {
325 		/*
326 		 * Obtain a lock on the union_node.
327 		 * uppervp is locked, though un->un_uppervp
328 		 * may not be.  this doesn't break the locking
329 		 * hierarchy since in the case that un->un_uppervp
330 		 * is not yet locked it will be vrele'd and replaced
331 		 * with uppervp.
332 		 */
333 
334 		if ((dvp != NULLVP) && (uppervp == dvp)) {
335 			/*
336 			 * Access ``.'', so (un) will already
337 			 * be locked.  Since this process has
338 			 * the lock on (uppervp) no other
339 			 * process can hold the lock on (un).
340 			 */
341 #ifdef DIAGNOSTIC
342 			if ((un->un_flags & UN_LOCKED) == 0)
343 				panic("union: . not locked");
344 			else if (curproc && un->un_pid != curproc->p_pid &&
345 				    un->un_pid > -1 && curproc->p_pid > -1)
346 				panic("union: allocvp not lock owner");
347 #endif
348 		} else {
349 			if (un->un_flags & UN_LOCKED) {
350 				vrele(UNIONTOV(un));
351 				un->un_flags |= UN_WANT;
352 				sleep((caddr_t) &un->un_flags, PINOD);
353 				goto loop;
354 			}
355 			un->un_flags |= UN_LOCKED;
356 
357 #ifdef DIAGNOSTIC
358 			if (curproc)
359 				un->un_pid = curproc->p_pid;
360 			else
361 				un->un_pid = -1;
362 #endif
363 		}
364 
365 		/*
366 		 * At this point, the union_node is locked,
367 		 * un->un_uppervp may not be locked, and uppervp
368 		 * is locked or nil.
369 		 */
370 
371 		/*
372 		 * Save information about the upper layer.
373 		 */
374 		if (uppervp != un->un_uppervp) {
375 			union_newupper(un, uppervp);
376 		} else if (uppervp) {
377 			vrele(uppervp);
378 		}
379 
380 		if (un->un_uppervp) {
381 			un->un_flags |= UN_ULOCK;
382 			un->un_flags &= ~UN_KLOCK;
383 		}
384 
385 		/*
386 		 * Save information about the lower layer.
387 		 * This needs to keep track of pathname
388 		 * and directory information which union_vn_create
389 		 * might need.
390 		 */
391 		if (lowervp != un->un_lowervp) {
392 			union_newlower(un, lowervp);
393 			if (cnp && (lowervp != NULLVP) &&
394 			    (lowervp->v_type == VREG)) {
395 				un->un_hash = cnp->cn_hash;
396 				un->un_path = malloc(cnp->cn_namelen+1,
397 						M_TEMP, M_WAITOK);
398 				bcopy(cnp->cn_nameptr, un->un_path,
399 						cnp->cn_namelen);
400 				un->un_path[cnp->cn_namelen] = '\0';
401 				VREF(dvp);
402 				un->un_dirvp = dvp;
403 			}
404 		} else if (lowervp) {
405 			vrele(lowervp);
406 		}
407 		*vpp = UNIONTOV(un);
408 		return (0);
409 	}
410 
411 	/*
412 	 * otherwise lock the vp list while we call getnewvnode
413 	 * since that can block.
414 	 */
415 	hash = UNION_HASH(uppervp, lowervp);
416 
417 	if (union_list_lock(hash))
418 		goto loop;
419 
420 	error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
421 	if (error) {
422 		if (uppervp) {
423 			if (dvp == uppervp)
424 				vrele(uppervp);
425 			else
426 				vput(uppervp);
427 		}
428 		if (lowervp)
429 			vrele(lowervp);
430 
431 		goto out;
432 	}
433 
434 	MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
435 		M_TEMP, M_WAITOK);
436 
437 	(*vpp)->v_flag |= vflag;
438 	if (uppervp)
439 		(*vpp)->v_type = uppervp->v_type;
440 	else
441 		(*vpp)->v_type = lowervp->v_type;
442 	un = VTOUNION(*vpp);
443 	un->un_vnode = *vpp;
444 	un->un_uppervp = uppervp;
445 	un->un_uppersz = VNOVAL;
446 	un->un_lowervp = lowervp;
447 	un->un_lowersz = VNOVAL;
448 	un->un_openl = 0;
449 	un->un_flags = UN_LOCKED;
450 	if (un->un_uppervp)
451 		un->un_flags |= UN_ULOCK;
452 #ifdef DIAGNOSTIC
453 	if (curproc)
454 		un->un_pid = curproc->p_pid;
455 	else
456 		un->un_pid = -1;
457 #endif
458 	if (cnp && (lowervp != NULLVP) && (lowervp->v_type == VREG)) {
459 		un->un_hash = cnp->cn_hash;
460 		un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
461 		bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
462 		un->un_path[cnp->cn_namelen] = '\0';
463 		VREF(dvp);
464 		un->un_dirvp = dvp;
465 	} else {
466 		un->un_hash = 0;
467 		un->un_path = 0;
468 		un->un_dirvp = 0;
469 	}
470 
471 	LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
472 	un->un_flags |= UN_CACHED;
473 
474 	if (xlowervp)
475 		vrele(xlowervp);
476 
477 out:
478 	union_list_unlock(hash);
479 
480 	return (error);
481 }
482 
483 int
484 union_freevp(vp)
485 	struct vnode *vp;
486 {
487 	struct union_node *un = VTOUNION(vp);
488 
489 	if (un->un_flags & UN_CACHED) {
490 		LIST_REMOVE(un, un_cache);
491 		un->un_flags &= ~UN_CACHED;
492 	}
493 
494 	if (un->un_uppervp != NULLVP)
495 		vrele(un->un_uppervp);
496 	if (un->un_lowervp != NULLVP)
497 		vrele(un->un_lowervp);
498 	if (un->un_dirvp != NULLVP)
499 		vrele(un->un_dirvp);
500 	if (un->un_path)
501 		free(un->un_path, M_TEMP);
502 
503 	FREE(vp->v_data, M_TEMP);
504 	vp->v_data = 0;
505 
506 	return (0);
507 }
508 
509 /*
510  * copyfile.  copy the vnode (fvp) to the vnode (tvp)
511  * using a sequence of reads and writes.  both (fvp)
512  * and (tvp) are locked on entry and exit.
513  */
514 int
515 union_copyfile(fvp, tvp, cred, p)
516 	struct vnode *fvp;
517 	struct vnode *tvp;
518 	struct ucred *cred;
519 	struct proc *p;
520 {
521 	char *buf;
522 	struct uio uio;
523 	struct iovec iov;
524 	int error = 0;
525 
526 	/*
527 	 * strategy:
528 	 * allocate a buffer of size MAXBSIZE.
529 	 * loop doing reads and writes, keeping track
530 	 * of the current uio offset.
531 	 * give up at the first sign of trouble.
532 	 */
533 
534 	uio.uio_procp = p;
535 	uio.uio_segflg = UIO_SYSSPACE;
536 	uio.uio_offset = 0;
537 
538 	VOP_UNLOCK(fvp);				/* XXX */
539 	LEASE_CHECK(fvp, p, cred, LEASE_READ);
540 	VOP_LOCK(fvp);					/* XXX */
541 	VOP_UNLOCK(tvp);				/* XXX */
542 	LEASE_CHECK(tvp, p, cred, LEASE_WRITE);
543 	VOP_LOCK(tvp);					/* XXX */
544 
545 	buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
546 
547 	/* ugly loop follows... */
548 	do {
549 		off_t offset = uio.uio_offset;
550 
551 		uio.uio_iov = &iov;
552 		uio.uio_iovcnt = 1;
553 		iov.iov_base = buf;
554 		iov.iov_len = MAXBSIZE;
555 		uio.uio_resid = iov.iov_len;
556 		uio.uio_rw = UIO_READ;
557 		error = VOP_READ(fvp, &uio, 0, cred);
558 
559 		if (error == 0) {
560 			uio.uio_iov = &iov;
561 			uio.uio_iovcnt = 1;
562 			iov.iov_base = buf;
563 			iov.iov_len = MAXBSIZE - uio.uio_resid;
564 			uio.uio_offset = offset;
565 			uio.uio_rw = UIO_WRITE;
566 			uio.uio_resid = iov.iov_len;
567 
568 			if (uio.uio_resid == 0)
569 				break;
570 
571 			do {
572 				error = VOP_WRITE(tvp, &uio, 0, cred);
573 			} while ((uio.uio_resid > 0) && (error == 0));
574 		}
575 
576 	} while (error == 0);
577 
578 	free(buf, M_TEMP);
579 	return (error);
580 }
581 
582 /*
583  * (un) is assumed to be locked on entry and remains
584  * locked on exit.
585  */
586 int
587 union_copyup(un, docopy, cred, p)
588 	struct union_node *un;
589 	int docopy;
590 	struct ucred *cred;
591 	struct proc *p;
592 {
593 	int error;
594 	struct vnode *lvp, *uvp;
595 
596 	error = union_vn_create(&uvp, un, p);
597 	if (error)
598 		return (error);
599 
600 	/* at this point, uppervp is locked */
601 	union_newupper(un, uvp);
602 	un->un_flags |= UN_ULOCK;
603 
604 	lvp = un->un_lowervp;
605 
606 	if (docopy) {
607 		/*
608 		 * XX - should not ignore errors
609 		 * from VOP_CLOSE
610 		 */
611 		VOP_LOCK(lvp);
612 		error = VOP_OPEN(lvp, FREAD, cred, p);
613 		if (error == 0) {
614 			error = union_copyfile(lvp, uvp, cred, p);
615 			VOP_UNLOCK(lvp);
616 			(void) VOP_CLOSE(lvp, FREAD);
617 		}
618 #ifdef UNION_DIAGNOSTIC
619 		if (error == 0)
620 			uprintf("union: copied up %s\n", un->un_path);
621 #endif
622 
623 	}
624 	un->un_flags &= ~UN_ULOCK;
625 	VOP_UNLOCK(uvp);
626 	union_vn_close(uvp, FWRITE, cred, p);
627 	VOP_LOCK(uvp);
628 	un->un_flags |= UN_ULOCK;
629 
630 	/*
631 	 * Subsequent IOs will go to the top layer, so
632 	 * call close on the lower vnode and open on the
633 	 * upper vnode to ensure that the filesystem keeps
634 	 * its references counts right.  This doesn't do
635 	 * the right thing with (cred) and (FREAD) though.
636 	 * Ignoring error returns is not right, either.
637 	 */
638 	if (error == 0) {
639 		int i;
640 
641 		for (i = 0; i < un->un_openl; i++) {
642 			(void) VOP_CLOSE(lvp, FREAD);
643 			(void) VOP_OPEN(uvp, FREAD, cred, p);
644 		}
645 		un->un_openl = 0;
646 	}
647 
648 	return (error);
649 
650 }
651 
652 /*
653  * Create a shadow directory in the upper layer.
654  * The new vnode is returned locked.
655  *
656  * (um) points to the union mount structure for access to the
657  * the mounting process's credentials.
658  * (dvp) is the directory in which to create the shadow directory.
659  * it is unlocked on entry and exit.
660  * (cnp) is the componentname to be created.
661  * (vpp) is the returned newly created shadow directory, which
662  * is returned locked.
663  */
664 int
665 union_mkshadow(um, dvp, cnp, vpp)
666 	struct union_mount *um;
667 	struct vnode *dvp;
668 	struct componentname *cnp;
669 	struct vnode **vpp;
670 {
671 	int error;
672 	struct vattr va;
673 	struct proc *p = cnp->cn_proc;
674 	struct componentname cn;
675 
676 	/*
677 	 * policy: when creating the shadow directory in the
678 	 * upper layer, create it owned by the user who did
679 	 * the mount, group from parent directory, and mode
680 	 * 777 modified by umask (ie mostly identical to the
681 	 * mkdir syscall).  (jsp, kb)
682 	 */
683 
684 	/*
685 	 * A new componentname structure must be faked up because
686 	 * there is no way to know where the upper level cnp came
687 	 * from or what it is being used for.  This must duplicate
688 	 * some of the work done by NDINIT, some of the work done
689 	 * by namei, some of the work done by lookup and some of
690 	 * the work done by VOP_LOOKUP when given a CREATE flag.
691 	 * Conclusion: Horrible.
692 	 *
693 	 * The pathname buffer will be FREEed by VOP_MKDIR.
694 	 */
695 	cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK);
696 	bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen);
697 	cn.cn_pnbuf[cnp->cn_namelen] = '\0';
698 
699 	cn.cn_nameiop = CREATE;
700 	cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
701 	cn.cn_proc = cnp->cn_proc;
702 	if (um->um_op == UNMNT_ABOVE)
703 		cn.cn_cred = cnp->cn_cred;
704 	else
705 		cn.cn_cred = um->um_cred;
706 	cn.cn_nameptr = cn.cn_pnbuf;
707 	cn.cn_namelen = cnp->cn_namelen;
708 	cn.cn_hash = cnp->cn_hash;
709 	cn.cn_consume = cnp->cn_consume;
710 
711 	VREF(dvp);
712 	if (error = relookup(dvp, vpp, &cn))
713 		return (error);
714 	vrele(dvp);
715 
716 	if (*vpp) {
717 		VOP_ABORTOP(dvp, &cn);
718 		VOP_UNLOCK(dvp);
719 		vrele(*vpp);
720 		*vpp = NULLVP;
721 		return (EEXIST);
722 	}
723 
724 	VATTR_NULL(&va);
725 	va.va_type = VDIR;
726 	va.va_mode = um->um_cmode;
727 
728 	/* LEASE_CHECK: dvp is locked */
729 	LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
730 
731 	error = VOP_MKDIR(dvp, vpp, &cn, &va);
732 	return (error);
733 }
734 
735 /*
736  * union_vn_create: creates and opens a new shadow file
737  * on the upper union layer.  this function is similar
738  * in spirit to calling vn_open but it avoids calling namei().
739  * the problem with calling namei is that a) it locks too many
740  * things, and b) it doesn't start at the "right" directory,
741  * whereas relookup is told where to start.
742  */
743 int
744 union_vn_create(vpp, un, p)
745 	struct vnode **vpp;
746 	struct union_node *un;
747 	struct proc *p;
748 {
749 	struct vnode *vp;
750 	struct ucred *cred = p->p_ucred;
751 	struct vattr vat;
752 	struct vattr *vap = &vat;
753 	int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
754 	int error;
755 	int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
756 	char *cp;
757 	struct componentname cn;
758 
759 	*vpp = NULLVP;
760 
761 	/*
762 	 * Build a new componentname structure (for the same
763 	 * reasons outlines in union_mkshadow).
764 	 * The difference here is that the file is owned by
765 	 * the current user, rather than by the person who
766 	 * did the mount, since the current user needs to be
767 	 * able to write the file (that's why it is being
768 	 * copied in the first place).
769 	 */
770 	cn.cn_namelen = strlen(un->un_path);
771 	cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
772 	bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
773 	cn.cn_nameiop = CREATE;
774 	cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
775 	cn.cn_proc = p;
776 	cn.cn_cred = p->p_ucred;
777 	cn.cn_nameptr = cn.cn_pnbuf;
778 	cn.cn_hash = un->un_hash;
779 	cn.cn_consume = 0;
780 
781 	VREF(un->un_dirvp);
782 	if (error = relookup(un->un_dirvp, &vp, &cn))
783 		return (error);
784 	vrele(un->un_dirvp);
785 
786 	if (vp) {
787 		VOP_ABORTOP(un->un_dirvp, &cn);
788 		if (un->un_dirvp == vp)
789 			vrele(un->un_dirvp);
790 		else
791 			vput(un->un_dirvp);
792 		vrele(vp);
793 		return (EEXIST);
794 	}
795 
796 	/*
797 	 * Good - there was no race to create the file
798 	 * so go ahead and create it.  The permissions
799 	 * on the file will be 0666 modified by the
800 	 * current user's umask.  Access to the file, while
801 	 * it is unioned, will require access to the top *and*
802 	 * bottom files.  Access when not unioned will simply
803 	 * require access to the top-level file.
804 	 * TODO: confirm choice of access permissions.
805 	 */
806 	VATTR_NULL(vap);
807 	vap->va_type = VREG;
808 	vap->va_mode = cmode;
809 	LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE);
810 	if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap))
811 		return (error);
812 
813 	if (error = VOP_OPEN(vp, fmode, cred, p)) {
814 		vput(vp);
815 		return (error);
816 	}
817 
818 	vp->v_writecount++;
819 	*vpp = vp;
820 	return (0);
821 }
822 
823 int
824 union_vn_close(vp, fmode, cred, p)
825 	struct vnode *vp;
826 	int fmode;
827 	struct ucred *cred;
828 	struct proc *p;
829 {
830 	if (fmode & FWRITE)
831 		--vp->v_writecount;
832 	return (VOP_CLOSE(vp, fmode));
833 }
834 
835 void
836 union_removed_upper(un)
837 	struct union_node *un;
838 {
839 	if (un->un_flags & UN_ULOCK) {
840 		un->un_flags &= ~UN_ULOCK;
841 		VOP_UNLOCK(un->un_uppervp);
842 	}
843 
844 	union_newupper(un, NULLVP);
845 }
846 
847 struct vnode *
848 union_lowervp(vp)
849 	struct vnode *vp;
850 {
851 	struct union_node *un = VTOUNION(vp);
852 
853 	if ((un->un_lowervp != NULLVP) &&
854 	    (vp->v_type == un->un_lowervp->v_type)) {
855 		if (vget(un->un_lowervp, 0) == 0)
856 			return (un->un_lowervp);
857 	}
858 
859 	return (NULLVP);
860 }
861