xref: /original-bsd/sys/miscfs/union/union_vnops.c (revision 2d4381ed)
1 /*
2  * Copyright (c) 1992, 1993, 1994 The Regents of the University of California.
3  * Copyright (c) 1992, 1993, 1994 Jan-Simon Pendry.
4  * 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_vnops.c	8.3 (Berkeley) 02/10/94
12  */
13 
14 #include <sys/param.h>
15 #include <sys/systm.h>
16 #include <sys/proc.h>
17 #include <sys/file.h>
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <sys/vnode.h>
21 #include <sys/mount.h>
22 #include <sys/namei.h>
23 #include <sys/malloc.h>
24 #include <sys/buf.h>
25 #include <sys/queue.h>
26 #include <miscfs/union/union.h>
27 
28 static int
29 union_lookup1(udvp, dvp, vpp, cnp)
30 	struct vnode *udvp;
31 	struct vnode *dvp;
32 	struct vnode **vpp;
33 	struct componentname *cnp;
34 {
35 	int error;
36 	struct vnode *tdvp;
37 	struct mount *mp;
38 
39 	/*
40 	 * If stepping up the directory tree, check for going
41 	 * back across the mount point, in which case do what
42 	 * lookup would do by stepping back down the mount
43 	 * hierarchy.
44 	 */
45 	if (cnp->cn_flags & ISDOTDOT) {
46 		for (;;) {
47 			/*
48 			 * Don't do the NOCROSSMOUNT check
49 			 * at this level.  By definition,
50 			 * union fs deals with namespaces, not
51 			 * filesystems.
52 			 */
53 			if ((dvp->v_flag & VROOT) == 0)
54 				break;
55 
56 			tdvp = dvp;
57 			dvp = dvp->v_mount->mnt_vnodecovered;
58 			vput(tdvp);
59 			VREF(dvp);
60 			VOP_LOCK(dvp);
61 		}
62 	}
63 
64         error = VOP_LOOKUP(dvp, &tdvp, cnp);
65 	if (error)
66 		return (error);
67 
68 	/*
69 	 * The parent directory will have been unlocked, unless lookup
70 	 * found the last component.  In which case, re-lock the node
71 	 * here to allow it to be unlocked again (phew) in union_lookup.
72 	 */
73 	if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
74 		VOP_LOCK(dvp);
75 
76 	dvp = tdvp;
77 
78 	/*
79 	 * Lastly check if the current node is a mount point in
80 	 * which case walk up the mount hierarchy making sure not to
81 	 * bump into the root of the mount tree (ie. dvp != udvp).
82 	 */
83 	while (dvp != udvp && (dvp->v_type == VDIR) &&
84 	       (mp = dvp->v_mountedhere)) {
85 
86 		if (mp->mnt_flag & MNT_MLOCK) {
87 			mp->mnt_flag |= MNT_MWAIT;
88 			sleep((caddr_t) mp, PVFS);
89 			continue;
90 		}
91 
92 		if (error = VFS_ROOT(mp, &tdvp)) {
93 			vput(dvp);
94 			return (error);
95 		}
96 
97 		vput(dvp);
98 		dvp = tdvp;
99 	}
100 
101 	*vpp = dvp;
102 	return (0);
103 }
104 
105 int
106 union_lookup(ap)
107 	struct vop_lookup_args /* {
108 		struct vnodeop_desc *a_desc;
109 		struct vnode *a_dvp;
110 		struct vnode **a_vpp;
111 		struct componentname *a_cnp;
112 	} */ *ap;
113 {
114 	int error;
115 	int uerror, lerror;
116 	struct vnode *uppervp, *lowervp;
117 	struct vnode *upperdvp, *lowerdvp;
118 	struct vnode *dvp = ap->a_dvp;
119 	struct union_node *dun = VTOUNION(dvp);
120 	struct componentname *cnp = ap->a_cnp;
121 	int lockparent = cnp->cn_flags & LOCKPARENT;
122 	int rdonly = cnp->cn_flags & RDONLY;
123 	struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
124 
125 	cnp->cn_flags |= LOCKPARENT;
126 
127 	upperdvp = dun->un_uppervp;
128 	lowerdvp = dun->un_lowervp;
129 	uppervp = NULLVP;
130 	lowervp = NULLVP;
131 
132 	/*
133 	 * do the lookup in the upper level.
134 	 * if that level comsumes additional pathnames,
135 	 * then assume that something special is going
136 	 * on and just return that vnode.
137 	 */
138 	if (upperdvp) {
139 		uerror = union_lookup1(um->um_uppervp, upperdvp,
140 					&uppervp, cnp);
141 		/*if (uppervp == upperdvp)
142 			dun->un_flags |= UN_KLOCK;*/
143 
144 		if (cnp->cn_consume != 0) {
145 			*ap->a_vpp = uppervp;
146 			if (!lockparent)
147 				cnp->cn_flags &= ~LOCKPARENT;
148 			return (uerror);
149 		}
150 	} else {
151 		uerror = ENOENT;
152 	}
153 
154 	/*
155 	 * in a similar way to the upper layer, do the lookup
156 	 * in the lower layer.   this time, if there is some
157 	 * component magic going on, then vput whatever we got
158 	 * back from the upper layer and return the lower vnode
159 	 * instead.
160 	 */
161 	if (lowerdvp) {
162 		int nameiop;
163 
164 		VOP_LOCK(lowerdvp);
165 
166 		/*
167 		 * Only do a LOOKUP on the bottom node, since
168 		 * we won't be making changes to it anyway.
169 		 */
170 		nameiop = cnp->cn_nameiop;
171 		cnp->cn_nameiop = LOOKUP;
172 		lerror = union_lookup1(um->um_lowervp, lowerdvp,
173 				&lowervp, cnp);
174 		cnp->cn_nameiop = nameiop;
175 
176 		if (lowervp != lowerdvp)
177 			VOP_UNLOCK(lowerdvp);
178 
179 		if (cnp->cn_consume != 0) {
180 			if (uppervp) {
181 				if (uppervp == upperdvp)
182 					vrele(uppervp);
183 				else
184 					vput(uppervp);
185 				uppervp = NULLVP;
186 			}
187 			*ap->a_vpp = lowervp;
188 			if (!lockparent)
189 				cnp->cn_flags &= ~LOCKPARENT;
190 			return (lerror);
191 		}
192 	} else {
193 		lerror = ENOENT;
194 	}
195 
196 	if (!lockparent)
197 		cnp->cn_flags &= ~LOCKPARENT;
198 
199 	/*
200 	 * at this point, we have uerror and lerror indicating
201 	 * possible errors with the lookups in the upper and lower
202 	 * layers.  additionally, uppervp and lowervp are (locked)
203 	 * references to existing vnodes in the upper and lower layers.
204 	 *
205 	 * there are now three cases to consider.
206 	 * 1. if both layers returned an error, then return whatever
207 	 *    error the upper layer generated.
208 	 *
209 	 * 2. if the top layer failed and the bottom layer succeeded
210 	 *    then two subcases occur.
211 	 *    a.  the bottom vnode is not a directory, in which
212 	 *	  case just return a new union vnode referencing
213 	 *	  an empty top layer and the existing bottom layer.
214 	 *    b.  the bottom vnode is a directory, in which case
215 	 *	  create a new directory in the top-level and
216 	 *	  continue as in case 3.
217 	 *
218 	 * 3. if the top layer succeeded then return a new union
219 	 *    vnode referencing whatever the new top layer and
220 	 *    whatever the bottom layer returned.
221 	 */
222 
223 	*ap->a_vpp = NULLVP;
224 
225 	/* case 1. */
226 	if ((uerror != 0) && (lerror != 0)) {
227 		return (uerror);
228 	}
229 
230 	/* case 2. */
231 	if (uerror != 0 /* && (lerror == 0) */ ) {
232 		if (lowervp->v_type == VDIR) { /* case 2b. */
233 			dun->un_flags &= ~UN_ULOCK;
234 			VOP_UNLOCK(upperdvp);
235 			uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
236 			VOP_LOCK(upperdvp);
237 			dun->un_flags |= UN_ULOCK;
238 
239 			if (uerror) {
240 				if (lowervp) {
241 					vput(lowervp);
242 					lowervp = NULLVP;
243 				}
244 				return (uerror);
245 			}
246 		}
247 	}
248 
249 	if (lowervp)
250 		VOP_UNLOCK(lowervp);
251 
252 	error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
253 			      uppervp, lowervp);
254 
255 	if (error) {
256 		if (uppervp)
257 			vput(uppervp);
258 		if (lowervp)
259 			vrele(lowervp);
260 	} else {
261 		if (*ap->a_vpp != dvp)
262 			if (!lockparent || !(cnp->cn_flags & ISLASTCN))
263 				VOP_UNLOCK(dvp);
264 	}
265 
266 	return (error);
267 }
268 
269 int
270 union_create(ap)
271 	struct vop_create_args /* {
272 		struct vnode *a_dvp;
273 		struct vnode **a_vpp;
274 		struct componentname *a_cnp;
275 		struct vattr *a_vap;
276 	} */ *ap;
277 {
278 	struct union_node *un = VTOUNION(ap->a_dvp);
279 	struct vnode *dvp = un->un_uppervp;
280 
281 	if (dvp) {
282 		int error;
283 		struct vnode *vp;
284 
285 		VREF(dvp);
286 		un->un_flags |= UN_KLOCK;
287 		vput(ap->a_dvp);
288 		error = VOP_CREATE(dvp, &vp, ap->a_cnp, ap->a_vap);
289 		if (error)
290 			return (error);
291 
292 		error = union_allocvp(
293 				ap->a_vpp,
294 				ap->a_dvp->v_mount,
295 				ap->a_dvp,
296 				NULLVP,
297 				ap->a_cnp,
298 				vp,
299 				NULLVP);
300 		if (error)
301 			vput(vp);
302 		return (error);
303 	}
304 
305 	vput(ap->a_dvp);
306 	return (EROFS);
307 }
308 
309 int
310 union_mknod(ap)
311 	struct vop_mknod_args /* {
312 		struct vnode *a_dvp;
313 		struct vnode **a_vpp;
314 		struct componentname *a_cnp;
315 		struct vattr *a_vap;
316 	} */ *ap;
317 {
318 	struct union_node *un = VTOUNION(ap->a_dvp);
319 	struct vnode *dvp = un->un_uppervp;
320 
321 	if (dvp) {
322 		int error;
323 		struct vnode *vp;
324 
325 		VREF(dvp);
326 		un->un_flags |= UN_KLOCK;
327 		vput(ap->a_dvp);
328 		error = VOP_MKNOD(dvp, &vp, ap->a_cnp, ap->a_vap);
329 		if (error)
330 			return (error);
331 
332 		if (vp) {
333 			error = union_allocvp(
334 					ap->a_vpp,
335 					ap->a_dvp->v_mount,
336 					ap->a_dvp,
337 					NULLVP,
338 					ap->a_cnp,
339 					vp,
340 					NULLVP);
341 			if (error)
342 				vput(vp);
343 		}
344 		return (error);
345 	}
346 
347 	vput(ap->a_dvp);
348 	return (EROFS);
349 }
350 
351 int
352 union_open(ap)
353 	struct vop_open_args /* {
354 		struct vnodeop_desc *a_desc;
355 		struct vnode *a_vp;
356 		int a_mode;
357 		struct ucred *a_cred;
358 		struct proc *a_p;
359 	} */ *ap;
360 {
361 	struct union_node *un = VTOUNION(ap->a_vp);
362 	struct vnode *tvp;
363 	int mode = ap->a_mode;
364 	struct ucred *cred = ap->a_cred;
365 	struct proc *p = ap->a_p;
366 	int error;
367 
368 	/*
369 	 * If there is an existing upper vp then simply open that.
370 	 */
371 	tvp = un->un_uppervp;
372 	if (tvp == NULLVP) {
373 		/*
374 		 * If the lower vnode is being opened for writing, then
375 		 * copy the file contents to the upper vnode and open that,
376 		 * otherwise can simply open the lower vnode.
377 		 */
378 		tvp = un->un_lowervp;
379 		if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
380 			struct vnode *vp;
381 			int i;
382 
383 			/*
384 			 * Open the named file in the upper layer.  Note that
385 			 * the file may have come into existence *since* the
386 			 * lookup was done, since the upper layer may really
387 			 * be a loopback mount of some other filesystem...
388 			 * so open the file with exclusive create and barf if
389 			 * it already exists.
390 			 * XXX - perhaps should re-lookup the node (once more
391 			 * with feeling) and simply open that.  Who knows.
392 			 */
393 			error = union_vn_create(&vp, un, p);
394 			if (error)
395 				return (error);
396 
397 			/* at this point, uppervp is locked */
398 			union_newupper(un, vp);
399 			un->un_flags |= UN_ULOCK;
400 
401 			/*
402 			 * Now, if the file is being opened with truncation,
403 			 * then the (new) upper vnode is ready to fly,
404 			 * otherwise the data from the lower vnode must be
405 			 * copied to the upper layer first.  This only works
406 			 * for regular files (check is made above).
407 			 */
408 			if ((mode & O_TRUNC) == 0) {
409 				/*
410 				 * XXX - should not ignore errors
411 				 * from VOP_CLOSE
412 				 */
413 				VOP_LOCK(tvp);
414 				error = VOP_OPEN(tvp, FREAD, cred, p);
415 				if (error == 0) {
416 					error = union_copyfile(p, cred,
417 						       tvp, un->un_uppervp);
418 					VOP_UNLOCK(tvp);
419 					(void) VOP_CLOSE(tvp, FREAD);
420 				} else {
421 					VOP_UNLOCK(tvp);
422 				}
423 
424 				if (!error)
425 					uprintf("union: copied up %s\n",
426 								un->un_path);
427 			}
428 
429 			un->un_flags &= ~UN_ULOCK;
430 			VOP_UNLOCK(un->un_uppervp);
431 			union_vn_close(un->un_uppervp, FWRITE, cred, p);
432 			VOP_LOCK(un->un_uppervp);
433 			un->un_flags |= UN_ULOCK;
434 
435 			/*
436 			 * Subsequent IOs will go to the top layer, so
437 			 * call close on the lower vnode and open on the
438 			 * upper vnode to ensure that the filesystem keeps
439 			 * its references counts right.  This doesn't do
440 			 * the right thing with (cred) and (FREAD) though.
441 			 * Ignoring error returns is not righ, either.
442 			 */
443 			for (i = 0; i < un->un_openl; i++) {
444 				(void) VOP_CLOSE(tvp, FREAD);
445 				(void) VOP_OPEN(un->un_uppervp, FREAD, cred, p);
446 			}
447 			un->un_openl = 0;
448 
449 			if (error == 0)
450 				error = VOP_OPEN(un->un_uppervp, mode, cred, p);
451 			return (error);
452 		}
453 
454 		/*
455 		 * Just open the lower vnode
456 		 */
457 		un->un_openl++;
458 		VOP_LOCK(tvp);
459 		error = VOP_OPEN(tvp, mode, cred, p);
460 		VOP_UNLOCK(tvp);
461 
462 		return (error);
463 	}
464 
465 	error = VOP_OPEN(tvp, mode, cred, p);
466 
467 	return (error);
468 }
469 
470 int
471 union_close(ap)
472 	struct vop_close_args /* {
473 		struct vnode *a_vp;
474 		int  a_fflag;
475 		struct ucred *a_cred;
476 		struct proc *a_p;
477 	} */ *ap;
478 {
479 	struct union_node *un = VTOUNION(ap->a_vp);
480 	struct vnode *vp;
481 
482 	if (un->un_uppervp) {
483 		vp = un->un_uppervp;
484 	} else {
485 #ifdef UNION_DIAGNOSTIC
486 		if (un->un_openl <= 0)
487 			panic("union: un_openl cnt");
488 #endif
489 		--un->un_openl;
490 		vp = un->un_lowervp;
491 	}
492 
493 	return (VOP_CLOSE(vp, ap->a_fflag, ap->a_cred, ap->a_p));
494 }
495 
496 /*
497  * Check access permission on the union vnode.
498  * The access check being enforced is to check
499  * against both the underlying vnode, and any
500  * copied vnode.  This ensures that no additional
501  * file permissions are given away simply because
502  * the user caused an implicit file copy.
503  */
504 int
505 union_access(ap)
506 	struct vop_access_args /* {
507 		struct vnodeop_desc *a_desc;
508 		struct vnode *a_vp;
509 		int a_mode;
510 		struct ucred *a_cred;
511 		struct proc *a_p;
512 	} */ *ap;
513 {
514 	struct union_node *un = VTOUNION(ap->a_vp);
515 	int error = 0;
516 	struct vnode *vp;
517 
518 	if (vp = un->un_lowervp) {
519 		VOP_LOCK(vp);
520 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
521 		VOP_UNLOCK(vp);
522 		if (error)
523 			return (error);
524 	}
525 
526 	if (vp = un->un_uppervp)
527 		error = VOP_ACCESS(vp, ap->a_mode, ap->a_cred, ap->a_p);
528 
529 	return (error);
530 }
531 
532 /*
533  *  We handle getattr only to change the fsid.
534  */
535 int
536 union_getattr(ap)
537 	struct vop_getattr_args /* {
538 		struct vnode *a_vp;
539 		struct vattr *a_vap;
540 		struct ucred *a_cred;
541 		struct proc *a_p;
542 	} */ *ap;
543 {
544 	int error;
545 	struct vnode *vp = OTHERVP(ap->a_vp);
546 	int dolock = (vp == LOWERVP(ap->a_vp));
547 
548 	if (dolock)
549 		VOP_LOCK(vp);
550 	error = VOP_GETATTR(vp, ap->a_vap, ap->a_cred, ap->a_p);
551 	if (dolock)
552 		VOP_UNLOCK(vp);
553 
554 	/* Requires that arguments be restored. */
555 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
556 	return (0);
557 }
558 
559 int
560 union_setattr(ap)
561 	struct vop_setattr_args /* {
562 		struct vnode *a_vp;
563 		struct vattr *a_vap;
564 		struct ucred *a_cred;
565 		struct proc *a_p;
566 	} */ *ap;
567 {
568 	struct union_node *un = VTOUNION(ap->a_vp);
569 	int error;
570 
571 	/*
572 	 * Handle case of truncating lower object to zero size,
573 	 * by creating a zero length upper object.  This is to
574 	 * handle the case of open with O_TRUNC and O_CREAT.
575 	 */
576 	if ((un->un_uppervp == NULLVP) &&
577 	    /* assert(un->un_lowervp != NULLVP) */
578 	    (un->un_lowervp->v_type == VREG) &&
579 	    (ap->a_vap->va_size == 0)) {
580 		struct vnode *vp;
581 
582 		error = union_vn_create(&vp, un, ap->a_p);
583 		if (error)
584 			return (error);
585 
586 		/* at this point, uppervp is locked */
587 		union_newupper(un, vp);
588 
589 		VOP_UNLOCK(vp);
590 		union_vn_close(un->un_uppervp, FWRITE, ap->a_cred, ap->a_p);
591 		VOP_LOCK(vp);
592 		un->un_flags |= UN_ULOCK;
593 	}
594 
595 	/*
596 	 * Try to set attributes in upper layer,
597 	 * otherwise return read-only filesystem error.
598 	 */
599 	if (un->un_uppervp != NULLVP) {
600 		error = VOP_SETATTR(un->un_uppervp, ap->a_vap,
601 					ap->a_cred, ap->a_p);
602 	} else {
603 		error = EROFS;
604 	}
605 
606 	return (error);
607 }
608 
609 int
610 union_read(ap)
611 	struct vop_read_args /* {
612 		struct vnode *a_vp;
613 		struct uio *a_uio;
614 		int  a_ioflag;
615 		struct ucred *a_cred;
616 	} */ *ap;
617 {
618 	int error;
619 	struct vnode *vp = OTHERVP(ap->a_vp);
620 	int dolock = (vp == LOWERVP(ap->a_vp));
621 
622 	if (dolock)
623 		VOP_LOCK(vp);
624 	error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
625 	if (dolock)
626 		VOP_UNLOCK(vp);
627 
628 	return (error);
629 }
630 
631 int
632 union_write(ap)
633 	struct vop_read_args /* {
634 		struct vnode *a_vp;
635 		struct uio *a_uio;
636 		int  a_ioflag;
637 		struct ucred *a_cred;
638 	} */ *ap;
639 {
640 	int error;
641 	struct vnode *vp = OTHERVP(ap->a_vp);
642 	int dolock = (vp == LOWERVP(ap->a_vp));
643 
644 	if (dolock)
645 		VOP_LOCK(vp);
646 	error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
647 	if (dolock)
648 		VOP_UNLOCK(vp);
649 
650 	return (error);
651 }
652 
653 int
654 union_ioctl(ap)
655 	struct vop_ioctl_args /* {
656 		struct vnode *a_vp;
657 		int  a_command;
658 		caddr_t  a_data;
659 		int  a_fflag;
660 		struct ucred *a_cred;
661 		struct proc *a_p;
662 	} */ *ap;
663 {
664 
665 	return (VOP_IOCTL(OTHERVP(ap->a_vp), ap->a_command, ap->a_data,
666 				ap->a_fflag, ap->a_cred, ap->a_p));
667 }
668 
669 int
670 union_select(ap)
671 	struct vop_select_args /* {
672 		struct vnode *a_vp;
673 		int  a_which;
674 		int  a_fflags;
675 		struct ucred *a_cred;
676 		struct proc *a_p;
677 	} */ *ap;
678 {
679 
680 	return (VOP_SELECT(OTHERVP(ap->a_vp), ap->a_which, ap->a_fflags,
681 				ap->a_cred, ap->a_p));
682 }
683 
684 int
685 union_mmap(ap)
686 	struct vop_mmap_args /* {
687 		struct vnode *a_vp;
688 		int  a_fflags;
689 		struct ucred *a_cred;
690 		struct proc *a_p;
691 	} */ *ap;
692 {
693 
694 	return (VOP_MMAP(OTHERVP(ap->a_vp), ap->a_fflags,
695 				ap->a_cred, ap->a_p));
696 }
697 
698 int
699 union_fsync(ap)
700 	struct vop_fsync_args /* {
701 		struct vnode *a_vp;
702 		struct ucred *a_cred;
703 		int  a_waitfor;
704 		struct proc *a_p;
705 	} */ *ap;
706 {
707 	int error = 0;
708 	struct vnode *targetvp = OTHERVP(ap->a_vp);
709 
710 	if (targetvp) {
711 		int dolock = (targetvp == LOWERVP(ap->a_vp));
712 
713 		if (dolock)
714 			VOP_LOCK(targetvp);
715 		error = VOP_FSYNC(targetvp, ap->a_cred,
716 					ap->a_waitfor, ap->a_p);
717 		if (dolock)
718 			VOP_UNLOCK(targetvp);
719 	}
720 
721 	return (error);
722 }
723 
724 int
725 union_seek(ap)
726 	struct vop_seek_args /* {
727 		struct vnode *a_vp;
728 		off_t  a_oldoff;
729 		off_t  a_newoff;
730 		struct ucred *a_cred;
731 	} */ *ap;
732 {
733 
734 	return (VOP_SEEK(OTHERVP(ap->a_vp), ap->a_oldoff, ap->a_newoff, ap->a_cred));
735 }
736 
737 int
738 union_remove(ap)
739 	struct vop_remove_args /* {
740 		struct vnode *a_dvp;
741 		struct vnode *a_vp;
742 		struct componentname *a_cnp;
743 	} */ *ap;
744 {
745 	int error;
746 	struct union_node *dun = VTOUNION(ap->a_dvp);
747 	struct union_node *un = VTOUNION(ap->a_vp);
748 
749 	if (dun->un_uppervp && un->un_uppervp) {
750 		struct vnode *dvp = dun->un_uppervp;
751 		struct vnode *vp = un->un_uppervp;
752 
753 		VREF(dvp);
754 		dun->un_flags |= UN_KLOCK;
755 		vput(ap->a_dvp);
756 		VREF(vp);
757 		un->un_flags |= UN_KLOCK;
758 		vput(ap->a_vp);
759 
760 		error = VOP_REMOVE(dvp, vp, ap->a_cnp);
761 		if (!error)
762 			union_removed_upper(un);
763 
764 		/*
765 		 * XXX: should create a whiteout here
766 		 */
767 	} else {
768 		/*
769 		 * XXX: should create a whiteout here
770 		 */
771 		vput(ap->a_dvp);
772 		vput(ap->a_vp);
773 		error = EROFS;
774 	}
775 
776 	return (error);
777 }
778 
779 int
780 union_link(ap)
781 	struct vop_link_args /* {
782 		struct vnode *a_vp;
783 		struct vnode *a_tdvp;
784 		struct componentname *a_cnp;
785 	} */ *ap;
786 {
787 	int error;
788 	struct union_node *dun = VTOUNION(ap->a_vp);
789 	struct union_node *un = VTOUNION(ap->a_tdvp);
790 
791 	if (dun->un_uppervp && un->un_uppervp) {
792 		struct vnode *dvp = dun->un_uppervp;
793 		struct vnode *vp = un->un_uppervp;
794 
795 		VREF(dvp);
796 		dun->un_flags |= UN_KLOCK;
797 		vput(ap->a_vp);
798 		VREF(vp);
799 		vrele(ap->a_tdvp);
800 
801 		error = VOP_LINK(dvp, vp, ap->a_cnp);
802 	} else {
803 		/*
804 		 * XXX: need to copy to upper layer
805 		 * and do the link there.
806 		 */
807 		vput(ap->a_vp);
808 		vrele(ap->a_tdvp);
809 		error = EROFS;
810 	}
811 
812 	return (error);
813 }
814 
815 int
816 union_rename(ap)
817 	struct vop_rename_args  /* {
818 		struct vnode *a_fdvp;
819 		struct vnode *a_fvp;
820 		struct componentname *a_fcnp;
821 		struct vnode *a_tdvp;
822 		struct vnode *a_tvp;
823 		struct componentname *a_tcnp;
824 	} */ *ap;
825 {
826 	int error;
827 
828 	struct vnode *fdvp = ap->a_fdvp;
829 	struct vnode *fvp = ap->a_fvp;
830 	struct vnode *tdvp = ap->a_tdvp;
831 	struct vnode *tvp = ap->a_tvp;
832 
833 	if (fdvp->v_op == union_vnodeop_p) {	/* always true */
834 		struct union_node *un = VTOUNION(fdvp);
835 		if (un->un_uppervp == NULLVP) {
836 			error = EROFS;
837 			goto bad;
838 		}
839 
840 		fdvp = un->un_uppervp;
841 		VREF(fdvp);
842 		vrele(ap->a_fdvp);
843 	}
844 
845 	if (fvp->v_op == union_vnodeop_p) {	/* always true */
846 		struct union_node *un = VTOUNION(fvp);
847 		if (un->un_uppervp == NULLVP) {
848 			error = EROFS;
849 			goto bad;
850 		}
851 
852 		fvp = un->un_uppervp;
853 		VREF(fvp);
854 		vrele(ap->a_fvp);
855 	}
856 
857 	if (tdvp->v_op == union_vnodeop_p) {
858 		struct union_node *un = VTOUNION(tdvp);
859 		if (un->un_uppervp == NULLVP) {
860 			error = EROFS;
861 			goto bad;
862 		}
863 
864 		tdvp = un->un_uppervp;
865 		VREF(tdvp);
866 		un->un_flags |= UN_KLOCK;
867 		vput(ap->a_tdvp);
868 	}
869 
870 	if (tvp && tvp->v_op == union_vnodeop_p) {
871 		struct union_node *un = VTOUNION(tvp);
872 		if (un->un_uppervp == NULLVP) {
873 			error = EROFS;
874 			goto bad;
875 		}
876 
877 		tvp = un->un_uppervp;
878 		VREF(tvp);
879 		un->un_flags |= UN_KLOCK;
880 		vput(ap->a_tvp);
881 	}
882 
883 	return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
884 
885 bad:
886 	vrele(fdvp);
887 	vrele(fvp);
888 	vput(tdvp);
889 	if (tvp)
890 		vput(tvp);
891 
892 	return (error);
893 }
894 
895 int
896 union_mkdir(ap)
897 	struct vop_mkdir_args /* {
898 		struct vnode *a_dvp;
899 		struct vnode **a_vpp;
900 		struct componentname *a_cnp;
901 		struct vattr *a_vap;
902 	} */ *ap;
903 {
904 	struct union_node *un = VTOUNION(ap->a_dvp);
905 	struct vnode *dvp = un->un_uppervp;
906 
907 	if (dvp) {
908 		int error;
909 		struct vnode *vp;
910 
911 		VREF(dvp);
912 		un->un_flags |= UN_KLOCK;
913 		vput(ap->a_dvp);
914 		error = VOP_MKDIR(dvp, &vp, ap->a_cnp, ap->a_vap);
915 		if (error)
916 			return (error);
917 
918 		error = union_allocvp(
919 				ap->a_vpp,
920 				ap->a_dvp->v_mount,
921 				ap->a_dvp,
922 				NULLVP,
923 				ap->a_cnp,
924 				vp,
925 				NULLVP);
926 		if (error)
927 			vput(vp);
928 		return (error);
929 	}
930 
931 	vput(ap->a_dvp);
932 	return (EROFS);
933 }
934 
935 int
936 union_rmdir(ap)
937 	struct vop_rmdir_args /* {
938 		struct vnode *a_dvp;
939 		struct vnode *a_vp;
940 		struct componentname *a_cnp;
941 	} */ *ap;
942 {
943 	int error;
944 	struct union_node *dun = VTOUNION(ap->a_dvp);
945 	struct union_node *un = VTOUNION(ap->a_vp);
946 
947 	if (dun->un_uppervp && un->un_uppervp) {
948 		struct vnode *dvp = dun->un_uppervp;
949 		struct vnode *vp = un->un_uppervp;
950 
951 		VREF(dvp);
952 		dun->un_flags |= UN_KLOCK;
953 		vput(ap->a_dvp);
954 		VREF(vp);
955 		un->un_flags |= UN_KLOCK;
956 		vput(ap->a_vp);
957 
958 		error = VOP_RMDIR(dvp, vp, ap->a_cnp);
959 		if (!error)
960 			union_removed_upper(un);
961 
962 		/*
963 		 * XXX: should create a whiteout here
964 		 */
965 	} else {
966 		/*
967 		 * XXX: should create a whiteout here
968 		 */
969 		vput(ap->a_dvp);
970 		vput(ap->a_vp);
971 		error = EROFS;
972 	}
973 
974 	return (error);
975 }
976 
977 int
978 union_symlink(ap)
979 	struct vop_symlink_args /* {
980 		struct vnode *a_dvp;
981 		struct vnode **a_vpp;
982 		struct componentname *a_cnp;
983 		struct vattr *a_vap;
984 		char *a_target;
985 	} */ *ap;
986 {
987 	struct union_node *un = VTOUNION(ap->a_dvp);
988 	struct vnode *dvp = un->un_uppervp;
989 
990 	if (dvp) {
991 		int error;
992 		struct vnode *vp;
993 		struct mount *mp = ap->a_dvp->v_mount;
994 
995 		VREF(dvp);
996 		un->un_flags |= UN_KLOCK;
997 		vput(ap->a_dvp);
998 		error = VOP_SYMLINK(dvp, &vp, ap->a_cnp,
999 					ap->a_vap, ap->a_target);
1000 		*ap->a_vpp = NULLVP;
1001 		return (error);
1002 	}
1003 
1004 	vput(ap->a_dvp);
1005 	return (EROFS);
1006 }
1007 
1008 /*
1009  * union_readdir works in concert with getdirentries and
1010  * readdir(3) to provide a list of entries in the unioned
1011  * directories.  getdirentries is responsible for walking
1012  * down the union stack.  readdir(3) is responsible for
1013  * eliminating duplicate names from the returned data stream.
1014  */
1015 int
1016 union_readdir(ap)
1017 	struct vop_readdir_args /* {
1018 		struct vnodeop_desc *a_desc;
1019 		struct vnode *a_vp;
1020 		struct uio *a_uio;
1021 		struct ucred *a_cred;
1022 	} */ *ap;
1023 {
1024 	int error = 0;
1025 	struct union_node *un = VTOUNION(ap->a_vp);
1026 
1027 	if (un->un_uppervp)
1028 		error = VOP_READDIR(un->un_uppervp, ap->a_uio, ap->a_cred);
1029 
1030 	return (error);
1031 }
1032 
1033 int
1034 union_readlink(ap)
1035 	struct vop_readlink_args /* {
1036 		struct vnode *a_vp;
1037 		struct uio *a_uio;
1038 		struct ucred *a_cred;
1039 	} */ *ap;
1040 {
1041 	int error;
1042 	struct vnode *vp = OTHERVP(ap->a_vp);
1043 	int dolock = (vp == LOWERVP(ap->a_vp));
1044 
1045 	if (dolock)
1046 		VOP_LOCK(vp);
1047 	error = VOP_READLINK(vp, ap->a_uio, ap->a_cred);
1048 	if (dolock)
1049 		VOP_UNLOCK(vp);
1050 
1051 	return (error);
1052 }
1053 
1054 int
1055 union_abortop(ap)
1056 	struct vop_abortop_args /* {
1057 		struct vnode *a_dvp;
1058 		struct componentname *a_cnp;
1059 	} */ *ap;
1060 {
1061 	int error;
1062 	struct vnode *vp = OTHERVP(ap->a_dvp);
1063 	struct union_node *un = VTOUNION(ap->a_dvp);
1064 	int islocked = un->un_flags & UN_LOCKED;
1065 	int dolock = (vp == LOWERVP(ap->a_dvp));
1066 
1067 	if (islocked && dolock)
1068 		VOP_LOCK(vp);
1069 	error = VOP_ABORTOP(vp, ap->a_cnp);
1070 	if (islocked && dolock)
1071 		VOP_UNLOCK(vp);
1072 
1073 	return (error);
1074 }
1075 
1076 int
1077 union_inactive(ap)
1078 	struct vop_inactive_args /* {
1079 		struct vnode *a_vp;
1080 	} */ *ap;
1081 {
1082 
1083 	/*
1084 	 * Do nothing (and _don't_ bypass).
1085 	 * Wait to vrele lowervp until reclaim,
1086 	 * so that until then our union_node is in the
1087 	 * cache and reusable.
1088 	 *
1089 	 * NEEDSWORK: Someday, consider inactive'ing
1090 	 * the lowervp and then trying to reactivate it
1091 	 * with capabilities (v_id)
1092 	 * like they do in the name lookup cache code.
1093 	 * That's too much work for now.
1094 	 */
1095 
1096 #ifdef UNION_DIAGNOSTIC
1097 	struct union_node *un = VTOUNION(ap->a_vp);
1098 
1099 	if (un->un_flags & UN_LOCKED)
1100 		panic("union: inactivating locked node");
1101 #endif
1102 
1103 	return (0);
1104 }
1105 
1106 int
1107 union_reclaim(ap)
1108 	struct vop_reclaim_args /* {
1109 		struct vnode *a_vp;
1110 	} */ *ap;
1111 {
1112 
1113 	union_freevp(ap->a_vp);
1114 
1115 	return (0);
1116 }
1117 
1118 int
1119 union_lock(ap)
1120 	struct vop_lock_args *ap;
1121 {
1122 	struct union_node *un = VTOUNION(ap->a_vp);
1123 
1124 	if (un->un_uppervp) {
1125 		if ((un->un_flags & UN_ULOCK) == 0) {
1126 			VOP_LOCK(un->un_uppervp);
1127 			un->un_flags |= UN_ULOCK;
1128 		}
1129 #ifdef DIAGNOSTIC
1130 		if (un->un_flags & UN_KLOCK)
1131 			panic("union: dangling upper lock");
1132 #endif
1133 	}
1134 
1135 	while (un->un_flags & UN_LOCKED) {
1136 #ifdef DIAGNOSTIC
1137 		if (curproc && un->un_pid == curproc->p_pid &&
1138 			    un->un_pid > -1 && curproc->p_pid > -1)
1139 			panic("union: locking against myself");
1140 #endif
1141 		un->un_flags |= UN_WANT;
1142 		sleep((caddr_t) &un->un_flags, PINOD);
1143 	}
1144 	un->un_flags |= UN_LOCKED;
1145 
1146 #ifdef DIAGNOSTIC
1147 	if (curproc)
1148 		un->un_pid = curproc->p_pid;
1149 	else
1150 		un->un_pid = -1;
1151 #endif
1152 
1153 	return (0);
1154 }
1155 
1156 int
1157 union_unlock(ap)
1158 	struct vop_lock_args *ap;
1159 {
1160 	struct union_node *un = VTOUNION(ap->a_vp);
1161 
1162 #ifdef DIAGNOSTIC
1163 	if ((un->un_flags & UN_LOCKED) == 0)
1164 		panic("union: unlock unlocked node");
1165 	if (curproc && un->un_pid != curproc->p_pid &&
1166 			curproc->p_pid > -1 && un->un_pid > -1)
1167 		panic("union: unlocking other process's union node");
1168 #endif
1169 
1170 	un->un_flags &= ~UN_LOCKED;
1171 
1172 	if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
1173 		VOP_UNLOCK(un->un_uppervp);
1174 
1175 	un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
1176 
1177 	if (un->un_flags & UN_WANT) {
1178 		un->un_flags &= ~UN_WANT;
1179 		wakeup((caddr_t) &un->un_flags);
1180 	}
1181 
1182 #ifdef DIAGNOSTIC
1183 	un->un_pid = 0;
1184 #endif
1185 
1186 	return (0);
1187 }
1188 
1189 int
1190 union_bmap(ap)
1191 	struct vop_bmap_args /* {
1192 		struct vnode *a_vp;
1193 		daddr_t  a_bn;
1194 		struct vnode **a_vpp;
1195 		daddr_t *a_bnp;
1196 		int *a_runp;
1197 	} */ *ap;
1198 {
1199 	int error;
1200 	struct vnode *vp = OTHERVP(ap->a_vp);
1201 	int dolock = (vp == LOWERVP(ap->a_vp));
1202 
1203 	if (dolock)
1204 		VOP_LOCK(vp);
1205 	error = VOP_BMAP(vp, ap->a_bn, ap->a_vpp, ap->a_bnp, ap->a_runp);
1206 	if (dolock)
1207 		VOP_UNLOCK(vp);
1208 
1209 	return (error);
1210 }
1211 
1212 int
1213 union_print(ap)
1214 	struct vop_print_args /* {
1215 		struct vnode *a_vp;
1216 	} */ *ap;
1217 {
1218 	struct vnode *vp = ap->a_vp;
1219 
1220 	printf("\ttag VT_UNION, vp=%x, uppervp=%x, lowervp=%x\n",
1221 			vp, UPPERVP(vp), LOWERVP(vp));
1222 	return (0);
1223 }
1224 
1225 int
1226 union_islocked(ap)
1227 	struct vop_islocked_args /* {
1228 		struct vnode *a_vp;
1229 	} */ *ap;
1230 {
1231 
1232 	return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
1233 }
1234 
1235 int
1236 union_pathconf(ap)
1237 	struct vop_pathconf_args /* {
1238 		struct vnode *a_vp;
1239 		int a_name;
1240 		int *a_retval;
1241 	} */ *ap;
1242 {
1243 	int error;
1244 	struct vnode *vp = OTHERVP(ap->a_vp);
1245 	int dolock = (vp == LOWERVP(ap->a_vp));
1246 
1247 	if (dolock)
1248 		VOP_LOCK(vp);
1249 	error = VOP_PATHCONF(vp, ap->a_name, ap->a_retval);
1250 	if (dolock)
1251 		VOP_UNLOCK(vp);
1252 
1253 	return (error);
1254 }
1255 
1256 int
1257 union_advlock(ap)
1258 	struct vop_advlock_args /* {
1259 		struct vnode *a_vp;
1260 		caddr_t  a_id;
1261 		int  a_op;
1262 		struct flock *a_fl;
1263 		int  a_flags;
1264 	} */ *ap;
1265 {
1266 
1267 	return (VOP_ADVLOCK(OTHERVP(ap->a_vp), ap->a_id, ap->a_op,
1268 				ap->a_fl, ap->a_flags));
1269 }
1270 
1271 
1272 /*
1273  * XXX - vop_strategy must be hand coded because it has no
1274  * vnode in its arguments.
1275  * This goes away with a merged VM/buffer cache.
1276  */
1277 int
1278 union_strategy(ap)
1279 	struct vop_strategy_args /* {
1280 		struct buf *a_bp;
1281 	} */ *ap;
1282 {
1283 	struct buf *bp = ap->a_bp;
1284 	int error;
1285 	struct vnode *savedvp;
1286 
1287 	savedvp = bp->b_vp;
1288 	bp->b_vp = OTHERVP(bp->b_vp);
1289 
1290 #ifdef DIAGNOSTIC
1291 	if (bp->b_vp == NULLVP)
1292 		panic("union_strategy: nil vp");
1293 	if (((bp->b_flags & B_READ) == 0) &&
1294 	    (bp->b_vp == LOWERVP(savedvp)))
1295 		panic("union_strategy: writing to lowervp");
1296 #endif
1297 
1298 	error = VOP_STRATEGY(bp);
1299 	bp->b_vp = savedvp;
1300 
1301 	return (error);
1302 }
1303 
1304 /*
1305  * Global vfs data structures
1306  */
1307 int (**union_vnodeop_p)();
1308 struct vnodeopv_entry_desc union_vnodeop_entries[] = {
1309 	{ &vop_default_desc, vn_default_error },
1310 	{ &vop_lookup_desc, union_lookup },		/* lookup */
1311 	{ &vop_create_desc, union_create },		/* create */
1312 	{ &vop_mknod_desc, union_mknod },		/* mknod */
1313 	{ &vop_open_desc, union_open },			/* open */
1314 	{ &vop_close_desc, union_close },		/* close */
1315 	{ &vop_access_desc, union_access },		/* access */
1316 	{ &vop_getattr_desc, union_getattr },		/* getattr */
1317 	{ &vop_setattr_desc, union_setattr },		/* setattr */
1318 	{ &vop_read_desc, union_read },			/* read */
1319 	{ &vop_write_desc, union_write },		/* write */
1320 	{ &vop_ioctl_desc, union_ioctl },		/* ioctl */
1321 	{ &vop_select_desc, union_select },		/* select */
1322 	{ &vop_mmap_desc, union_mmap },			/* mmap */
1323 	{ &vop_fsync_desc, union_fsync },		/* fsync */
1324 	{ &vop_seek_desc, union_seek },			/* seek */
1325 	{ &vop_remove_desc, union_remove },		/* remove */
1326 	{ &vop_link_desc, union_link },			/* link */
1327 	{ &vop_rename_desc, union_rename },		/* rename */
1328 	{ &vop_mkdir_desc, union_mkdir },		/* mkdir */
1329 	{ &vop_rmdir_desc, union_rmdir },		/* rmdir */
1330 	{ &vop_symlink_desc, union_symlink },		/* symlink */
1331 	{ &vop_readdir_desc, union_readdir },		/* readdir */
1332 	{ &vop_readlink_desc, union_readlink },		/* readlink */
1333 	{ &vop_abortop_desc, union_abortop },		/* abortop */
1334 	{ &vop_inactive_desc, union_inactive },		/* inactive */
1335 	{ &vop_reclaim_desc, union_reclaim },		/* reclaim */
1336 	{ &vop_lock_desc, union_lock },			/* lock */
1337 	{ &vop_unlock_desc, union_unlock },		/* unlock */
1338 	{ &vop_bmap_desc, union_bmap },			/* bmap */
1339 	{ &vop_strategy_desc, union_strategy },		/* strategy */
1340 	{ &vop_print_desc, union_print },		/* print */
1341 	{ &vop_islocked_desc, union_islocked },		/* islocked */
1342 	{ &vop_pathconf_desc, union_pathconf },		/* pathconf */
1343 	{ &vop_advlock_desc, union_advlock },		/* advlock */
1344 #ifdef notdef
1345 	{ &vop_blkatoff_desc, union_blkatoff },		/* blkatoff */
1346 	{ &vop_valloc_desc, union_valloc },		/* valloc */
1347 	{ &vop_vfree_desc, union_vfree },		/* vfree */
1348 	{ &vop_truncate_desc, union_truncate },		/* truncate */
1349 	{ &vop_update_desc, union_update },		/* update */
1350 	{ &vop_bwrite_desc, union_bwrite },		/* bwrite */
1351 #endif
1352 	{ (struct vnodeop_desc*)NULL, (int(*)())NULL }
1353 };
1354 struct vnodeopv_desc union_vnodeop_opv_desc =
1355 	{ &union_vnodeop_p, union_vnodeop_entries };
1356