union_vnops.c revision 1.1 1 /* $NetBSD: union_vnops.c,v 1.1 2003/03/16 08:26:54 jdolecek Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
5 * Copyright (c) 1992, 1993, 1994, 1995
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)union_vnops.c 8.33 (Berkeley) 7/31/95
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: union_vnops.c,v 1.1 2003/03/16 08:26:54 jdolecek Exp $");
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/proc.h>
48 #include <sys/file.h>
49 #include <sys/time.h>
50 #include <sys/stat.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/namei.h>
54 #include <sys/malloc.h>
55 #include <sys/buf.h>
56 #include <sys/queue.h>
57 #include <sys/lock.h>
58 #include <fs/union/union.h>
59 #include <miscfs/genfs/genfs.h>
60
61 int union_lookup __P((void *));
62 int union_create __P((void *));
63 int union_whiteout __P((void *));
64 int union_mknod __P((void *));
65 int union_open __P((void *));
66 int union_close __P((void *));
67 int union_access __P((void *));
68 int union_getattr __P((void *));
69 int union_setattr __P((void *));
70 int union_read __P((void *));
71 int union_write __P((void *));
72 int union_lease __P((void *));
73 int union_ioctl __P((void *));
74 int union_poll __P((void *));
75 int union_revoke __P((void *));
76 int union_mmap __P((void *));
77 int union_fsync __P((void *));
78 int union_seek __P((void *));
79 int union_remove __P((void *));
80 int union_link __P((void *));
81 int union_rename __P((void *));
82 int union_mkdir __P((void *));
83 int union_rmdir __P((void *));
84 int union_symlink __P((void *));
85 int union_readdir __P((void *));
86 int union_readlink __P((void *));
87 int union_abortop __P((void *));
88 int union_inactive __P((void *));
89 int union_reclaim __P((void *));
90 int union_lock __P((void *));
91 int union_unlock __P((void *));
92 int union_bmap __P((void *));
93 int union_print __P((void *));
94 int union_islocked __P((void *));
95 int union_pathconf __P((void *));
96 int union_advlock __P((void *));
97 int union_strategy __P((void *));
98 int union_getpages __P((void *));
99 int union_putpages __P((void *));
100
101 static void union_fixup __P((struct union_node *));
102 static int union_lookup1 __P((struct vnode *, struct vnode **,
103 struct vnode **, struct componentname *));
104
105
106 /*
107 * Global vfs data structures
108 */
109 int (**union_vnodeop_p) __P((void *));
110 const struct vnodeopv_entry_desc union_vnodeop_entries[] = {
111 { &vop_default_desc, vn_default_error },
112 { &vop_lookup_desc, union_lookup }, /* lookup */
113 { &vop_create_desc, union_create }, /* create */
114 { &vop_whiteout_desc, union_whiteout }, /* whiteout */
115 { &vop_mknod_desc, union_mknod }, /* mknod */
116 { &vop_open_desc, union_open }, /* open */
117 { &vop_close_desc, union_close }, /* close */
118 { &vop_access_desc, union_access }, /* access */
119 { &vop_getattr_desc, union_getattr }, /* getattr */
120 { &vop_setattr_desc, union_setattr }, /* setattr */
121 { &vop_read_desc, union_read }, /* read */
122 { &vop_write_desc, union_write }, /* write */
123 { &vop_lease_desc, union_lease }, /* lease */
124 { &vop_ioctl_desc, union_ioctl }, /* ioctl */
125 { &vop_poll_desc, union_poll }, /* select */
126 { &vop_revoke_desc, union_revoke }, /* revoke */
127 { &vop_mmap_desc, union_mmap }, /* mmap */
128 { &vop_fsync_desc, union_fsync }, /* fsync */
129 { &vop_seek_desc, union_seek }, /* seek */
130 { &vop_remove_desc, union_remove }, /* remove */
131 { &vop_link_desc, union_link }, /* link */
132 { &vop_rename_desc, union_rename }, /* rename */
133 { &vop_mkdir_desc, union_mkdir }, /* mkdir */
134 { &vop_rmdir_desc, union_rmdir }, /* rmdir */
135 { &vop_symlink_desc, union_symlink }, /* symlink */
136 { &vop_readdir_desc, union_readdir }, /* readdir */
137 { &vop_readlink_desc, union_readlink }, /* readlink */
138 { &vop_abortop_desc, union_abortop }, /* abortop */
139 { &vop_inactive_desc, union_inactive }, /* inactive */
140 { &vop_reclaim_desc, union_reclaim }, /* reclaim */
141 { &vop_lock_desc, union_lock }, /* lock */
142 { &vop_unlock_desc, union_unlock }, /* unlock */
143 { &vop_bmap_desc, union_bmap }, /* bmap */
144 { &vop_strategy_desc, union_strategy }, /* strategy */
145 { &vop_print_desc, union_print }, /* print */
146 { &vop_islocked_desc, union_islocked }, /* islocked */
147 { &vop_pathconf_desc, union_pathconf }, /* pathconf */
148 { &vop_advlock_desc, union_advlock }, /* advlock */
149 { &vop_getpages_desc, union_getpages }, /* getpages */
150 { &vop_putpages_desc, union_putpages }, /* putpages */
151 #ifdef notdef
152 { &vop_blkatoff_desc, union_blkatoff }, /* blkatoff */
153 { &vop_valloc_desc, union_valloc }, /* valloc */
154 { &vop_vfree_desc, union_vfree }, /* vfree */
155 { &vop_truncate_desc, union_truncate }, /* truncate */
156 { &vop_update_desc, union_update }, /* update */
157 { &vop_bwrite_desc, union_bwrite }, /* bwrite */
158 #endif
159 { NULL, NULL }
160 };
161 const struct vnodeopv_desc union_vnodeop_opv_desc =
162 { &union_vnodeop_p, union_vnodeop_entries };
163
164 #define FIXUP(un) { \
165 if (((un)->un_flags & UN_ULOCK) == 0) { \
166 union_fixup(un); \
167 } \
168 }
169
170 static void
171 union_fixup(un)
172 struct union_node *un;
173 {
174
175 vn_lock(un->un_uppervp, LK_EXCLUSIVE | LK_RETRY);
176 un->un_flags |= UN_ULOCK;
177 }
178
179 static int
180 union_lookup1(udvp, dvpp, vpp, cnp)
181 struct vnode *udvp;
182 struct vnode **dvpp;
183 struct vnode **vpp;
184 struct componentname *cnp;
185 {
186 int error;
187 struct vnode *tdvp;
188 struct vnode *dvp;
189 struct mount *mp;
190
191 dvp = *dvpp;
192
193 /*
194 * If stepping up the directory tree, check for going
195 * back across the mount point, in which case do what
196 * lookup would do by stepping back down the mount
197 * hierarchy.
198 */
199 if (cnp->cn_flags & ISDOTDOT) {
200 while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
201 /*
202 * Don't do the NOCROSSMOUNT check
203 * at this level. By definition,
204 * union fs deals with namespaces, not
205 * filesystems.
206 */
207 tdvp = dvp;
208 *dvpp = dvp = dvp->v_mount->mnt_vnodecovered;
209 vput(tdvp);
210 VREF(dvp);
211 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
212 }
213 }
214
215 error = VOP_LOOKUP(dvp, &tdvp, cnp);
216 if (error)
217 return (error);
218
219 /*
220 * The parent directory will have been unlocked, unless lookup
221 * found the last component. In which case, re-lock the node
222 * here to allow it to be unlocked again (phew) in union_lookup.
223 */
224 if (dvp != tdvp && !(cnp->cn_flags & ISLASTCN))
225 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
226
227 dvp = tdvp;
228
229 /*
230 * Lastly check if the current node is a mount point in
231 * which case walk up the mount hierarchy making sure not to
232 * bump into the root of the mount tree (ie. dvp != udvp).
233 */
234 while (dvp != udvp && (dvp->v_type == VDIR) &&
235 (mp = dvp->v_mountedhere)) {
236
237 if (vfs_busy(mp, 0, 0))
238 continue;
239
240 error = VFS_ROOT(mp, &tdvp);
241 vfs_unbusy(mp);
242 if (error) {
243 vput(dvp);
244 return (error);
245 }
246
247 vput(dvp);
248 dvp = tdvp;
249 }
250
251 *vpp = dvp;
252 return (0);
253 }
254
255 int
256 union_lookup(v)
257 void *v;
258 {
259 struct vop_lookup_args /* {
260 struct vnodeop_desc *a_desc;
261 struct vnode *a_dvp;
262 struct vnode **a_vpp;
263 struct componentname *a_cnp;
264 } */ *ap = v;
265 int error;
266 int uerror, lerror;
267 struct vnode *uppervp, *lowervp;
268 struct vnode *upperdvp, *lowerdvp;
269 struct vnode *dvp = ap->a_dvp;
270 struct union_node *dun = VTOUNION(dvp);
271 struct componentname *cnp = ap->a_cnp;
272 int lockparent = cnp->cn_flags & LOCKPARENT;
273 struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
274 struct ucred *saved_cred = NULL;
275 int iswhiteout;
276 struct vattr va;
277
278 #ifdef notyet
279 if (cnp->cn_namelen == 3 &&
280 cnp->cn_nameptr[2] == '.' &&
281 cnp->cn_nameptr[1] == '.' &&
282 cnp->cn_nameptr[0] == '.') {
283 dvp = *ap->a_vpp = LOWERVP(ap->a_dvp);
284 if (dvp == NULLVP)
285 return (ENOENT);
286 VREF(dvp);
287 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
288 if (!lockparent || !(cnp->cn_flags & ISLASTCN))
289 VOP_UNLOCK(ap->a_dvp, 0);
290 return (0);
291 }
292 #endif
293
294 if ((cnp->cn_flags & ISLASTCN) &&
295 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
296 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
297 return (EROFS);
298
299 cnp->cn_flags |= LOCKPARENT;
300
301 upperdvp = dun->un_uppervp;
302 lowerdvp = dun->un_lowervp;
303 uppervp = NULLVP;
304 lowervp = NULLVP;
305 iswhiteout = 0;
306
307 /*
308 * do the lookup in the upper level.
309 * if that level comsumes additional pathnames,
310 * then assume that something special is going
311 * on and just return that vnode.
312 */
313 if (upperdvp != NULLVP) {
314 FIXUP(dun);
315 /*
316 * If we're doing `..' in the underlying filesystem,
317 * we must drop our lock on the union node before
318 * going up the tree in the lower file system--if we block
319 * on the lowervp lock, and that's held by someone else
320 * coming down the tree and who's waiting for our lock,
321 * we would be hosed.
322 */
323 if (cnp->cn_flags & ISDOTDOT) {
324 /* retain lock on underlying VP */
325 dun->un_flags |= UN_KLOCK;
326 VOP_UNLOCK(dvp, 0);
327 }
328 uerror = union_lookup1(um->um_uppervp, &upperdvp,
329 &uppervp, cnp);
330
331 if (cnp->cn_flags & ISDOTDOT) {
332 if (dun->un_uppervp == upperdvp) {
333 /*
334 * we got the underlying bugger back locked...
335 * now take back the union node lock. Since we
336 * hold the uppervp lock, we can diddle union
337 * locking flags at will. :)
338 */
339 dun->un_flags |= UN_ULOCK;
340 }
341 /*
342 * if upperdvp got swapped out, it means we did
343 * some mount point magic, and we do not have
344 * dun->un_uppervp locked currently--so we get it
345 * locked here (don't set the UN_ULOCK flag).
346 */
347 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
348 }
349 if (cnp->cn_consume != 0) {
350 *ap->a_vpp = uppervp;
351 if (!lockparent)
352 cnp->cn_flags &= ~LOCKPARENT;
353 return (uerror);
354 }
355 if (uerror == ENOENT || uerror == EJUSTRETURN) {
356 if (cnp->cn_flags & ISWHITEOUT) {
357 iswhiteout = 1;
358 } else if (lowerdvp != NULLVP) {
359 lerror = VOP_GETATTR(upperdvp, &va,
360 cnp->cn_cred, cnp->cn_proc);
361 if (lerror == 0 && (va.va_flags & OPAQUE))
362 iswhiteout = 1;
363 }
364 }
365 } else {
366 uerror = ENOENT;
367 }
368
369 /*
370 * in a similar way to the upper layer, do the lookup
371 * in the lower layer. this time, if there is some
372 * component magic going on, then vput whatever we got
373 * back from the upper layer and return the lower vnode
374 * instead.
375 */
376 if (lowerdvp != NULLVP && !iswhiteout) {
377 int nameiop;
378
379 vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY);
380
381 /*
382 * Only do a LOOKUP on the bottom node, since
383 * we won't be making changes to it anyway.
384 */
385 nameiop = cnp->cn_nameiop;
386 cnp->cn_nameiop = LOOKUP;
387 if (um->um_op == UNMNT_BELOW) {
388 saved_cred = cnp->cn_cred;
389 cnp->cn_cred = um->um_cred;
390 }
391 /*
392 * we shouldn't have to worry about locking interactions
393 * between the lower layer and our union layer (w.r.t.
394 * `..' processing) because we don't futz with lowervp
395 * locks in the union-node instantiation code path.
396 */
397 lerror = union_lookup1(um->um_lowervp, &lowerdvp,
398 &lowervp, cnp);
399 if (um->um_op == UNMNT_BELOW)
400 cnp->cn_cred = saved_cred;
401 cnp->cn_nameiop = nameiop;
402
403 if (lowervp != lowerdvp)
404 VOP_UNLOCK(lowerdvp, 0);
405
406 if (cnp->cn_consume != 0) {
407 if (uppervp != NULLVP) {
408 if (uppervp == upperdvp)
409 vrele(uppervp);
410 else
411 vput(uppervp);
412 uppervp = NULLVP;
413 }
414 *ap->a_vpp = lowervp;
415 if (!lockparent)
416 cnp->cn_flags &= ~LOCKPARENT;
417 return (lerror);
418 }
419 } else {
420 lerror = ENOENT;
421 if ((cnp->cn_flags & ISDOTDOT) && dun->un_pvp != NULLVP) {
422 lowervp = LOWERVP(dun->un_pvp);
423 if (lowervp != NULLVP) {
424 VREF(lowervp);
425 vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY);
426 lerror = 0;
427 }
428 }
429 }
430
431 if (!lockparent)
432 cnp->cn_flags &= ~LOCKPARENT;
433
434 /*
435 * EJUSTRETURN is used by underlying filesystems to indicate that
436 * a directory modification op was started successfully.
437 * This will only happen in the upper layer, since
438 * the lower layer only does LOOKUPs.
439 * If this union is mounted read-only, bounce it now.
440 */
441
442 if ((uerror == EJUSTRETURN) && (cnp->cn_flags & ISLASTCN) &&
443 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
444 ((cnp->cn_nameiop == CREATE) || (cnp->cn_nameiop == RENAME)))
445 uerror = EROFS;
446
447 /*
448 * at this point, we have uerror and lerror indicating
449 * possible errors with the lookups in the upper and lower
450 * layers. additionally, uppervp and lowervp are (locked)
451 * references to existing vnodes in the upper and lower layers.
452 *
453 * there are now three cases to consider.
454 * 1. if both layers returned an error, then return whatever
455 * error the upper layer generated.
456 *
457 * 2. if the top layer failed and the bottom layer succeeded
458 * then two subcases occur.
459 * a. the bottom vnode is not a directory, in which
460 * case just return a new union vnode referencing
461 * an empty top layer and the existing bottom layer.
462 * b. the bottom vnode is a directory, in which case
463 * create a new directory in the top-level and
464 * continue as in case 3.
465 *
466 * 3. if the top layer succeeded then return a new union
467 * vnode referencing whatever the new top layer and
468 * whatever the bottom layer returned.
469 */
470
471 *ap->a_vpp = NULLVP;
472
473
474 /* case 1. */
475 if ((uerror != 0) && (lerror != 0)) {
476 return (uerror);
477 }
478
479 /* case 2. */
480 if (uerror != 0 /* && (lerror == 0) */ ) {
481 if (lowervp->v_type == VDIR) { /* case 2b. */
482 /*
483 * We may be racing another process to make the
484 * upper-level shadow directory. Be careful with
485 * locks/etc!
486 */
487 dun->un_flags &= ~UN_ULOCK;
488 VOP_UNLOCK(upperdvp, 0);
489 uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
490 vn_lock(upperdvp, LK_EXCLUSIVE | LK_RETRY);
491 dun->un_flags |= UN_ULOCK;
492
493 if (uerror) {
494 if (lowervp != NULLVP) {
495 vput(lowervp);
496 lowervp = NULLVP;
497 }
498 return (uerror);
499 }
500 }
501 }
502
503 if (lowervp != NULLVP)
504 VOP_UNLOCK(lowervp, 0);
505
506 error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
507 uppervp, lowervp, 1);
508
509 if (error) {
510 if (uppervp != NULLVP)
511 vput(uppervp);
512 if (lowervp != NULLVP)
513 vrele(lowervp);
514 } else {
515 if (*ap->a_vpp != dvp)
516 if (!lockparent || !(cnp->cn_flags & ISLASTCN))
517 VOP_UNLOCK(dvp, 0);
518 if (cnp->cn_namelen == 1 &&
519 cnp->cn_nameptr[0] == '.' &&
520 *ap->a_vpp != dvp) {
521 panic("union_lookup -> . (%p) != startdir (%p)",
522 ap->a_vpp, dvp);
523 }
524 }
525
526 return (error);
527 }
528
529 int
530 union_create(v)
531 void *v;
532 {
533 struct vop_create_args /* {
534 struct vnode *a_dvp;
535 struct vnode **a_vpp;
536 struct componentname *a_cnp;
537 struct vattr *a_vap;
538 } */ *ap = v;
539 struct union_node *un = VTOUNION(ap->a_dvp);
540 struct vnode *dvp = un->un_uppervp;
541 struct componentname *cnp = ap->a_cnp;
542
543 if (dvp != NULLVP) {
544 int error;
545 struct vnode *vp;
546 struct mount *mp;
547
548 FIXUP(un);
549
550 VREF(dvp);
551 un->un_flags |= UN_KLOCK;
552 mp = ap->a_dvp->v_mount;
553 vput(ap->a_dvp);
554 error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
555 if (error)
556 return (error);
557
558 error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP, cnp, vp,
559 NULLVP, 1);
560 if (error)
561 vput(vp);
562 return (error);
563 }
564
565 vput(ap->a_dvp);
566 return (EROFS);
567 }
568
569 int
570 union_whiteout(v)
571 void *v;
572 {
573 struct vop_whiteout_args /* {
574 struct vnode *a_dvp;
575 struct componentname *a_cnp;
576 int a_flags;
577 } */ *ap = v;
578 struct union_node *un = VTOUNION(ap->a_dvp);
579 struct componentname *cnp = ap->a_cnp;
580
581 if (un->un_uppervp == NULLVP)
582 return (EOPNOTSUPP);
583
584 FIXUP(un);
585 return (VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags));
586 }
587
588 int
589 union_mknod(v)
590 void *v;
591 {
592 struct vop_mknod_args /* {
593 struct vnode *a_dvp;
594 struct vnode **a_vpp;
595 struct componentname *a_cnp;
596 struct vattr *a_vap;
597 } */ *ap = v;
598 struct union_node *un = VTOUNION(ap->a_dvp);
599 struct vnode *dvp = un->un_uppervp;
600 struct componentname *cnp = ap->a_cnp;
601
602 if (dvp != NULLVP) {
603 int error;
604 struct vnode *vp;
605 struct mount *mp;
606
607 FIXUP(un);
608
609 VREF(dvp);
610 un->un_flags |= UN_KLOCK;
611 mp = ap->a_dvp->v_mount;
612 vput(ap->a_dvp);
613 error = VOP_MKNOD(dvp, &vp, cnp, ap->a_vap);
614 if (error)
615 return (error);
616
617 error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
618 cnp, vp, NULLVP, 1);
619 if (error)
620 vput(vp);
621 return (error);
622 }
623
624 vput(ap->a_dvp);
625 return (EROFS);
626 }
627
628 int
629 union_open(v)
630 void *v;
631 {
632 struct vop_open_args /* {
633 struct vnodeop_desc *a_desc;
634 struct vnode *a_vp;
635 int a_mode;
636 struct ucred *a_cred;
637 struct proc *a_p;
638 } */ *ap = v;
639 struct union_node *un = VTOUNION(ap->a_vp);
640 struct vnode *tvp;
641 int mode = ap->a_mode;
642 struct ucred *cred = ap->a_cred;
643 struct proc *p = ap->a_p;
644 int error;
645
646 /*
647 * If there is an existing upper vp then simply open that.
648 */
649 tvp = un->un_uppervp;
650 if (tvp == NULLVP) {
651 /*
652 * If the lower vnode is being opened for writing, then
653 * copy the file contents to the upper vnode and open that,
654 * otherwise can simply open the lower vnode.
655 */
656 tvp = un->un_lowervp;
657 if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
658 error = union_copyup(un, (mode&O_TRUNC) == 0, cred, p);
659 if (error == 0)
660 error = VOP_OPEN(un->un_uppervp, mode, cred, p);
661 return (error);
662 }
663
664 /*
665 * Just open the lower vnode, but check for nodev mount flag
666 */
667 if ((tvp->v_type == VBLK || tvp->v_type == VCHR) &&
668 (ap->a_vp->v_mount->mnt_flag & MNT_NODEV))
669 return ENXIO;
670 un->un_openl++;
671 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
672 error = VOP_OPEN(tvp, mode, cred, p);
673 VOP_UNLOCK(tvp, 0);
674
675 return (error);
676 }
677 /*
678 * Just open the upper vnode, checking for nodev mount flag first
679 */
680 if ((tvp->v_type == VBLK || tvp->v_type == VCHR) &&
681 (ap->a_vp->v_mount->mnt_flag & MNT_NODEV))
682 return ENXIO;
683
684 FIXUP(un);
685
686 error = VOP_OPEN(tvp, mode, cred, p);
687
688 return (error);
689 }
690
691 int
692 union_close(v)
693 void *v;
694 {
695 struct vop_close_args /* {
696 struct vnode *a_vp;
697 int a_fflag;
698 struct ucred *a_cred;
699 struct proc *a_p;
700 } */ *ap = v;
701 struct union_node *un = VTOUNION(ap->a_vp);
702 struct vnode *vp;
703
704 vp = un->un_uppervp;
705 if (vp == NULLVP) {
706 #ifdef UNION_DIAGNOSTIC
707 if (un->un_openl <= 0)
708 panic("union: un_openl cnt");
709 #endif
710 --un->un_openl;
711 vp = un->un_lowervp;
712 }
713
714 #ifdef DIAGNOSTIC
715 if (vp == NULLVP) {
716 vprint("empty union vnode", vp);
717 panic("union_close empty vnode");
718 }
719 #endif
720
721 ap->a_vp = vp;
722 return (VCALL(vp, VOFFSET(vop_close), ap));
723 }
724
725 /*
726 * Check access permission on the union vnode.
727 * The access check being enforced is to check
728 * against both the underlying vnode, and any
729 * copied vnode. This ensures that no additional
730 * file permissions are given away simply because
731 * the user caused an implicit file copy.
732 */
733 int
734 union_access(v)
735 void *v;
736 {
737 struct vop_access_args /* {
738 struct vnodeop_desc *a_desc;
739 struct vnode *a_vp;
740 int a_mode;
741 struct ucred *a_cred;
742 struct proc *a_p;
743 } */ *ap = v;
744 struct vnode *vp = ap->a_vp;
745 struct union_node *un = VTOUNION(vp);
746 int error = EACCES;
747 struct union_mount *um = MOUNTTOUNIONMOUNT(vp->v_mount);
748
749 /*
750 * Disallow write attempts on read-only file systems;
751 * unless the file is a socket, fifo, or a block or
752 * character device resident on the file system.
753 */
754 if (ap->a_mode & VWRITE) {
755 switch (vp->v_type) {
756 case VDIR:
757 case VLNK:
758 case VREG:
759 if (vp->v_mount->mnt_flag & MNT_RDONLY)
760 return (EROFS);
761 break;
762 case VBAD:
763 case VBLK:
764 case VCHR:
765 case VSOCK:
766 case VFIFO:
767 case VNON:
768 default:
769 break;
770 }
771 }
772
773
774 if ((vp = un->un_uppervp) != NULLVP) {
775 FIXUP(un);
776 ap->a_vp = vp;
777 return (VCALL(vp, VOFFSET(vop_access), ap));
778 }
779
780 if ((vp = un->un_lowervp) != NULLVP) {
781 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
782 ap->a_vp = vp;
783 error = VCALL(vp, VOFFSET(vop_access), ap);
784 if (error == 0) {
785 if (um->um_op == UNMNT_BELOW) {
786 ap->a_cred = um->um_cred;
787 error = VCALL(vp, VOFFSET(vop_access), ap);
788 }
789 }
790 VOP_UNLOCK(vp, 0);
791 if (error)
792 return (error);
793 }
794
795 return (error);
796 }
797
798 /*
799 * We handle getattr only to change the fsid and
800 * track object sizes
801 */
802 int
803 union_getattr(v)
804 void *v;
805 {
806 struct vop_getattr_args /* {
807 struct vnode *a_vp;
808 struct vattr *a_vap;
809 struct ucred *a_cred;
810 struct proc *a_p;
811 } */ *ap = v;
812 int error;
813 struct union_node *un = VTOUNION(ap->a_vp);
814 struct vnode *vp = un->un_uppervp;
815 struct vattr *vap;
816 struct vattr va;
817
818
819 /*
820 * Some programs walk the filesystem hierarchy by counting
821 * links to directories to avoid stat'ing all the time.
822 * This means the link count on directories needs to be "correct".
823 * The only way to do that is to call getattr on both layers
824 * and fix up the link count. The link count will not necessarily
825 * be accurate but will be large enough to defeat the tree walkers.
826 *
827 * To make life more interesting, some filesystems don't keep
828 * track of link counts in the expected way, and return a
829 * link count of `1' for those directories; if either of the
830 * component directories returns a link count of `1', we return a 1.
831 */
832
833 vap = ap->a_vap;
834
835 vp = un->un_uppervp;
836 if (vp != NULLVP) {
837 /*
838 * It's not clear whether VOP_GETATTR is to be
839 * called with the vnode locked or not. stat() calls
840 * it with (vp) locked, and fstat calls it with
841 * (vp) unlocked.
842 * In the mean time, compensate here by checking
843 * the union_node's lock flag.
844 */
845 if (un->un_flags & UN_LOCKED)
846 FIXUP(un);
847
848 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
849 if (error)
850 return (error);
851 union_newsize(ap->a_vp, vap->va_size, VNOVAL);
852 }
853
854 if (vp == NULLVP) {
855 vp = un->un_lowervp;
856 } else if (vp->v_type == VDIR) {
857 vp = un->un_lowervp;
858 if (vp != NULLVP)
859 vap = &va;
860 } else {
861 vp = NULLVP;
862 }
863
864 if (vp != NULLVP) {
865 error = VOP_GETATTR(vp, vap, ap->a_cred, ap->a_p);
866 if (error)
867 return (error);
868 union_newsize(ap->a_vp, VNOVAL, vap->va_size);
869 }
870
871 if ((vap != ap->a_vap) && (vap->va_type == VDIR)) {
872 /*
873 * Link count manipulation:
874 * - If both return "2", return 2 (no subdirs)
875 * - If one or the other return "1", return "1" (ENOCLUE)
876 */
877 if ((ap->a_vap->va_nlink == 2) &&
878 (vap->va_nlink == 2))
879 ;
880 else if (ap->a_vap->va_nlink != 1) {
881 if (vap->va_nlink == 1)
882 ap->a_vap->va_nlink = 1;
883 else
884 ap->a_vap->va_nlink += vap->va_nlink;
885 }
886 }
887 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
888 return (0);
889 }
890
891 int
892 union_setattr(v)
893 void *v;
894 {
895 struct vop_setattr_args /* {
896 struct vnode *a_vp;
897 struct vattr *a_vap;
898 struct ucred *a_cred;
899 struct proc *a_p;
900 } */ *ap = v;
901 struct vattr *vap = ap->a_vap;
902 struct vnode *vp = ap->a_vp;
903 struct union_node *un = VTOUNION(vp);
904 int error;
905
906 if ((vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
907 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
908 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL) &&
909 (vp->v_mount->mnt_flag & MNT_RDONLY))
910 return (EROFS);
911 if (vap->va_size != VNOVAL) {
912 switch (vp->v_type) {
913 case VDIR:
914 return (EISDIR);
915 case VCHR:
916 case VBLK:
917 case VSOCK:
918 case VFIFO:
919 break;
920 case VREG:
921 case VLNK:
922 default:
923 /*
924 * Disallow write attempts if the filesystem is
925 * mounted read-only.
926 */
927 if (vp->v_mount->mnt_flag & MNT_RDONLY)
928 return (EROFS);
929 }
930 }
931
932 /*
933 * Handle case of truncating lower object to zero size,
934 * by creating a zero length upper object. This is to
935 * handle the case of open with O_TRUNC and O_CREAT.
936 */
937 if ((un->un_uppervp == NULLVP) &&
938 /* assert(un->un_lowervp != NULLVP) */
939 (un->un_lowervp->v_type == VREG)) {
940 error = union_copyup(un, (vap->va_size != 0),
941 ap->a_cred, ap->a_p);
942 if (error)
943 return (error);
944 }
945
946 /*
947 * Try to set attributes in upper layer,
948 * otherwise return read-only filesystem error.
949 */
950 if (un->un_uppervp != NULLVP) {
951 FIXUP(un);
952 error = VOP_SETATTR(un->un_uppervp, vap,
953 ap->a_cred, ap->a_p);
954 if ((error == 0) && (vap->va_size != VNOVAL))
955 union_newsize(ap->a_vp, vap->va_size, VNOVAL);
956 } else {
957 error = EROFS;
958 }
959
960 return (error);
961 }
962
963 int
964 union_read(v)
965 void *v;
966 {
967 struct vop_read_args /* {
968 struct vnode *a_vp;
969 struct uio *a_uio;
970 int a_ioflag;
971 struct ucred *a_cred;
972 } */ *ap = v;
973 int error;
974 struct vnode *vp = OTHERVP(ap->a_vp);
975 int dolock = (vp == LOWERVP(ap->a_vp));
976
977 if (dolock)
978 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
979 else
980 FIXUP(VTOUNION(ap->a_vp));
981 error = VOP_READ(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
982 if (dolock)
983 VOP_UNLOCK(vp, 0);
984
985 /*
986 * XXX
987 * perhaps the size of the underlying object has changed under
988 * our feet. take advantage of the offset information present
989 * in the uio structure.
990 */
991 if (error == 0) {
992 struct union_node *un = VTOUNION(ap->a_vp);
993 off_t cur = ap->a_uio->uio_offset;
994
995 if (vp == un->un_uppervp) {
996 if (cur > un->un_uppersz)
997 union_newsize(ap->a_vp, cur, VNOVAL);
998 } else {
999 if (cur > un->un_lowersz)
1000 union_newsize(ap->a_vp, VNOVAL, cur);
1001 }
1002 }
1003
1004 return (error);
1005 }
1006
1007 int
1008 union_write(v)
1009 void *v;
1010 {
1011 struct vop_read_args /* {
1012 struct vnode *a_vp;
1013 struct uio *a_uio;
1014 int a_ioflag;
1015 struct ucred *a_cred;
1016 } */ *ap = v;
1017 int error;
1018 struct vnode *vp;
1019 struct union_node *un = VTOUNION(ap->a_vp);
1020
1021 vp = UPPERVP(ap->a_vp);
1022 if (vp == NULLVP)
1023 panic("union: missing upper layer in write");
1024
1025 FIXUP(un);
1026 error = VOP_WRITE(vp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1027
1028 /*
1029 * the size of the underlying object may be changed by the
1030 * write.
1031 */
1032 if (error == 0) {
1033 off_t cur = ap->a_uio->uio_offset;
1034
1035 if (cur > un->un_uppersz)
1036 union_newsize(ap->a_vp, cur, VNOVAL);
1037 }
1038
1039 return (error);
1040 }
1041
1042 int
1043 union_lease(v)
1044 void *v;
1045 {
1046 struct vop_lease_args /* {
1047 struct vnode *a_vp;
1048 struct proc *a_p;
1049 struct ucred *a_cred;
1050 int a_flag;
1051 } */ *ap = v;
1052 struct vnode *ovp = OTHERVP(ap->a_vp);
1053
1054 ap->a_vp = ovp;
1055 return (VCALL(ovp, VOFFSET(vop_lease), ap));
1056 }
1057
1058 int
1059 union_ioctl(v)
1060 void *v;
1061 {
1062 struct vop_ioctl_args /* {
1063 struct vnode *a_vp;
1064 int a_command;
1065 caddr_t a_data;
1066 int a_fflag;
1067 struct ucred *a_cred;
1068 struct proc *a_p;
1069 } */ *ap = v;
1070 struct vnode *ovp = OTHERVP(ap->a_vp);
1071
1072 ap->a_vp = ovp;
1073 return (VCALL(ovp, VOFFSET(vop_ioctl), ap));
1074 }
1075
1076 int
1077 union_poll(v)
1078 void *v;
1079 {
1080 struct vop_poll_args /* {
1081 struct vnode *a_vp;
1082 int a_events;
1083 struct proc *a_p;
1084 } */ *ap = v;
1085 struct vnode *ovp = OTHERVP(ap->a_vp);
1086
1087 ap->a_vp = ovp;
1088 return (VCALL(ovp, VOFFSET(vop_poll), ap));
1089 }
1090
1091 int
1092 union_revoke(v)
1093 void *v;
1094 {
1095 struct vop_revoke_args /* {
1096 struct vnode *a_vp;
1097 int a_flags;
1098 struct proc *a_p;
1099 } */ *ap = v;
1100 struct vnode *vp = ap->a_vp;
1101
1102 if (UPPERVP(vp))
1103 VOP_REVOKE(UPPERVP(vp), ap->a_flags);
1104 if (LOWERVP(vp))
1105 VOP_REVOKE(LOWERVP(vp), ap->a_flags);
1106 vgone(vp);
1107 return (0);
1108 }
1109
1110 int
1111 union_mmap(v)
1112 void *v;
1113 {
1114 struct vop_mmap_args /* {
1115 struct vnode *a_vp;
1116 int a_fflags;
1117 struct ucred *a_cred;
1118 struct proc *a_p;
1119 } */ *ap = v;
1120 struct vnode *ovp = OTHERVP(ap->a_vp);
1121
1122 ap->a_vp = ovp;
1123 return (VCALL(ovp, VOFFSET(vop_mmap), ap));
1124 }
1125
1126 int
1127 union_fsync(v)
1128 void *v;
1129 {
1130 struct vop_fsync_args /* {
1131 struct vnode *a_vp;
1132 struct ucred *a_cred;
1133 int a_flags;
1134 off_t offhi;
1135 off_t offlo;
1136 struct proc *a_p;
1137 } */ *ap = v;
1138 int error = 0;
1139 struct proc *p;
1140 struct vnode *targetvp;
1141
1142 /*
1143 * If vinvalbuf is calling us, it's a "shallow fsync" -- don't
1144 * bother syncing the underlying vnodes, since (a) they'll be
1145 * fsync'ed when reclaimed and (b) we could deadlock if
1146 * they're locked; otherwise, pass it through to the
1147 * underlying layer.
1148 */
1149 if (ap->a_flags & FSYNC_RECLAIM)
1150 return 0;
1151
1152 targetvp = OTHERVP(ap->a_vp);
1153 p = ap->a_p;
1154
1155 if (targetvp != NULLVP) {
1156 int dolock = (targetvp == LOWERVP(ap->a_vp));
1157
1158 if (dolock)
1159 vn_lock(targetvp, LK_EXCLUSIVE | LK_RETRY);
1160 else
1161 FIXUP(VTOUNION(ap->a_vp));
1162 error = VOP_FSYNC(targetvp, ap->a_cred, ap->a_flags,
1163 ap->a_offlo, ap->a_offhi, p);
1164 if (dolock)
1165 VOP_UNLOCK(targetvp, 0);
1166 }
1167
1168 return (error);
1169 }
1170
1171 int
1172 union_seek(v)
1173 void *v;
1174 {
1175 struct vop_seek_args /* {
1176 struct vnode *a_vp;
1177 off_t a_oldoff;
1178 off_t a_newoff;
1179 struct ucred *a_cred;
1180 } */ *ap = v;
1181 struct vnode *ovp = OTHERVP(ap->a_vp);
1182
1183 ap->a_vp = ovp;
1184 return (VCALL(ovp, VOFFSET(vop_seek), ap));
1185 }
1186
1187 int
1188 union_remove(v)
1189 void *v;
1190 {
1191 struct vop_remove_args /* {
1192 struct vnode *a_dvp;
1193 struct vnode *a_vp;
1194 struct componentname *a_cnp;
1195 } */ *ap = v;
1196 int error;
1197 struct union_node *dun = VTOUNION(ap->a_dvp);
1198 struct union_node *un = VTOUNION(ap->a_vp);
1199 struct componentname *cnp = ap->a_cnp;
1200
1201 if (dun->un_uppervp == NULLVP)
1202 panic("union remove: null upper vnode");
1203
1204 if (un->un_uppervp != NULLVP) {
1205 struct vnode *dvp = dun->un_uppervp;
1206 struct vnode *vp = un->un_uppervp;
1207
1208 FIXUP(dun);
1209 VREF(dvp);
1210 dun->un_flags |= UN_KLOCK;
1211 vput(ap->a_dvp);
1212 FIXUP(un);
1213 VREF(vp);
1214 un->un_flags |= UN_KLOCK;
1215 vput(ap->a_vp);
1216
1217 if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc))
1218 cnp->cn_flags |= DOWHITEOUT;
1219 error = VOP_REMOVE(dvp, vp, cnp);
1220 if (!error)
1221 union_removed_upper(un);
1222 } else {
1223 FIXUP(dun);
1224 error = union_mkwhiteout(
1225 MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount),
1226 dun->un_uppervp, ap->a_cnp, un->un_path);
1227 vput(ap->a_dvp);
1228 vput(ap->a_vp);
1229 }
1230
1231 return (error);
1232 }
1233
1234 int
1235 union_link(v)
1236 void *v;
1237 {
1238 struct vop_link_args /* {
1239 struct vnode *a_dvp;
1240 struct vnode *a_vp;
1241 struct componentname *a_cnp;
1242 } */ *ap = v;
1243 int error = 0;
1244 struct componentname *cnp = ap->a_cnp;
1245 struct proc *p = cnp->cn_proc;
1246 struct union_node *dun;
1247 struct vnode *vp;
1248 struct vnode *dvp;
1249
1250 dun = VTOUNION(ap->a_dvp);
1251
1252 #ifdef DIAGNOSTIC
1253 if (!(ap->a_cnp->cn_flags & LOCKPARENT)) {
1254 printf("union_link called without LOCKPARENT set!\n");
1255 error = EIO; /* need some error code for "caller is a bozo" */
1256 } else
1257 #endif
1258
1259
1260 if (ap->a_dvp->v_op != ap->a_vp->v_op) {
1261 vp = ap->a_vp;
1262 } else {
1263 struct union_node *un = VTOUNION(ap->a_vp);
1264 if (un->un_uppervp == NULLVP) {
1265 /*
1266 * Needs to be copied before we can link it.
1267 */
1268 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1269 if (dun->un_uppervp == un->un_dirvp) {
1270 dun->un_flags &= ~UN_ULOCK;
1271 VOP_UNLOCK(dun->un_uppervp, 0);
1272 }
1273 error = union_copyup(un, 1, cnp->cn_cred, p);
1274 if (dun->un_uppervp == un->un_dirvp) {
1275 /*
1276 * During copyup, we dropped the lock on the
1277 * dir and invalidated any saved namei lookup
1278 * state for the directory we'll be entering
1279 * the link in. We need to re-run the lookup
1280 * in that directory to reset any state needed
1281 * for VOP_LINK.
1282 * Call relookup on the union-layer to reset
1283 * the state.
1284 */
1285 vp = NULLVP;
1286 if (dun->un_uppervp == NULLVP)
1287 panic("union: null upperdvp?");
1288 /*
1289 * relookup starts with an unlocked node,
1290 * and since LOCKPARENT is set returns
1291 * the starting directory locked.
1292 */
1293 VOP_UNLOCK(ap->a_dvp, 0);
1294 error = relookup(ap->a_dvp, &vp, ap->a_cnp);
1295 if (error) {
1296 vrele(ap->a_dvp);
1297 VOP_UNLOCK(ap->a_vp, 0);
1298 return EROFS; /* ? */
1299 }
1300 if (vp != NULLVP) {
1301 /*
1302 * The name we want to create has
1303 * mysteriously appeared (a race?)
1304 */
1305 error = EEXIST;
1306 VOP_UNLOCK(ap->a_vp, 0);
1307 goto croak;
1308 }
1309 }
1310 VOP_UNLOCK(ap->a_vp, 0);
1311 }
1312 vp = un->un_uppervp;
1313 }
1314
1315 dvp = dun->un_uppervp;
1316 if (dvp == NULLVP)
1317 error = EROFS;
1318
1319 if (error) {
1320 croak:
1321 vput(ap->a_dvp);
1322 return (error);
1323 }
1324
1325 FIXUP(dun);
1326 VREF(dvp);
1327 dun->un_flags |= UN_KLOCK;
1328 vput(ap->a_dvp);
1329
1330 return (VOP_LINK(dvp, vp, cnp));
1331 }
1332
1333 int
1334 union_rename(v)
1335 void *v;
1336 {
1337 struct vop_rename_args /* {
1338 struct vnode *a_fdvp;
1339 struct vnode *a_fvp;
1340 struct componentname *a_fcnp;
1341 struct vnode *a_tdvp;
1342 struct vnode *a_tvp;
1343 struct componentname *a_tcnp;
1344 } */ *ap = v;
1345 int error;
1346
1347 struct vnode *fdvp = ap->a_fdvp;
1348 struct vnode *fvp = ap->a_fvp;
1349 struct vnode *tdvp = ap->a_tdvp;
1350 struct vnode *tvp = ap->a_tvp;
1351
1352 if (fdvp->v_op == union_vnodeop_p) { /* always true */
1353 struct union_node *un = VTOUNION(fdvp);
1354 if (un->un_uppervp == NULLVP) {
1355 /*
1356 * this should never happen in normal
1357 * operation but might if there was
1358 * a problem creating the top-level shadow
1359 * directory.
1360 */
1361 error = EXDEV;
1362 goto bad;
1363 }
1364
1365 fdvp = un->un_uppervp;
1366 VREF(fdvp);
1367 vrele(ap->a_fdvp);
1368 }
1369
1370 if (fvp->v_op == union_vnodeop_p) { /* always true */
1371 struct union_node *un = VTOUNION(fvp);
1372 if (un->un_uppervp == NULLVP) {
1373 /* XXX: should do a copyup */
1374 error = EXDEV;
1375 goto bad;
1376 }
1377
1378 if (un->un_lowervp != NULLVP)
1379 ap->a_fcnp->cn_flags |= DOWHITEOUT;
1380
1381 fvp = un->un_uppervp;
1382 VREF(fvp);
1383 vrele(ap->a_fvp);
1384 }
1385
1386 if (tdvp->v_op == union_vnodeop_p) {
1387 struct union_node *un = VTOUNION(tdvp);
1388 if (un->un_uppervp == NULLVP) {
1389 /*
1390 * this should never happen in normal
1391 * operation but might if there was
1392 * a problem creating the top-level shadow
1393 * directory.
1394 */
1395 error = EXDEV;
1396 goto bad;
1397 }
1398
1399 tdvp = un->un_uppervp;
1400 VREF(tdvp);
1401 un->un_flags |= UN_KLOCK;
1402 vput(ap->a_tdvp);
1403 }
1404
1405 if (tvp != NULLVP && tvp->v_op == union_vnodeop_p) {
1406 struct union_node *un = VTOUNION(tvp);
1407
1408 tvp = un->un_uppervp;
1409 if (tvp != NULLVP) {
1410 VREF(tvp);
1411 un->un_flags |= UN_KLOCK;
1412 }
1413 vput(ap->a_tvp);
1414 }
1415
1416 return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1417
1418 bad:
1419 vrele(fdvp);
1420 vrele(fvp);
1421 vput(tdvp);
1422 if (tvp != NULLVP)
1423 vput(tvp);
1424
1425 return (error);
1426 }
1427
1428 int
1429 union_mkdir(v)
1430 void *v;
1431 {
1432 struct vop_mkdir_args /* {
1433 struct vnode *a_dvp;
1434 struct vnode **a_vpp;
1435 struct componentname *a_cnp;
1436 struct vattr *a_vap;
1437 } */ *ap = v;
1438 struct union_node *un = VTOUNION(ap->a_dvp);
1439 struct vnode *dvp = un->un_uppervp;
1440 struct componentname *cnp = ap->a_cnp;
1441
1442 if (dvp != NULLVP) {
1443 int error;
1444 struct vnode *vp;
1445
1446 FIXUP(un);
1447 VREF(dvp);
1448 un->un_flags |= UN_KLOCK;
1449 VOP_UNLOCK(ap->a_dvp, 0);
1450 error = VOP_MKDIR(dvp, &vp, cnp, ap->a_vap);
1451 if (error) {
1452 vrele(ap->a_dvp);
1453 return (error);
1454 }
1455
1456 error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount, ap->a_dvp,
1457 NULLVP, cnp, vp, NULLVP, 1);
1458 vrele(ap->a_dvp);
1459 if (error)
1460 vput(vp);
1461 return (error);
1462 }
1463
1464 vput(ap->a_dvp);
1465 return (EROFS);
1466 }
1467
1468 int
1469 union_rmdir(v)
1470 void *v;
1471 {
1472 struct vop_rmdir_args /* {
1473 struct vnode *a_dvp;
1474 struct vnode *a_vp;
1475 struct componentname *a_cnp;
1476 } */ *ap = v;
1477 int error;
1478 struct union_node *dun = VTOUNION(ap->a_dvp);
1479 struct union_node *un = VTOUNION(ap->a_vp);
1480 struct componentname *cnp = ap->a_cnp;
1481
1482 if (dun->un_uppervp == NULLVP)
1483 panic("union rmdir: null upper vnode");
1484
1485 if (un->un_uppervp != NULLVP) {
1486 struct vnode *dvp = dun->un_uppervp;
1487 struct vnode *vp = un->un_uppervp;
1488
1489 FIXUP(dun);
1490 VREF(dvp);
1491 dun->un_flags |= UN_KLOCK;
1492 vput(ap->a_dvp);
1493 FIXUP(un);
1494 VREF(vp);
1495 un->un_flags |= UN_KLOCK;
1496 vput(ap->a_vp);
1497
1498 if (union_dowhiteout(un, cnp->cn_cred, cnp->cn_proc))
1499 cnp->cn_flags |= DOWHITEOUT;
1500 error = VOP_RMDIR(dvp, vp, ap->a_cnp);
1501 if (!error)
1502 union_removed_upper(un);
1503 } else {
1504 FIXUP(dun);
1505 error = union_mkwhiteout(
1506 MOUNTTOUNIONMOUNT(UNIONTOV(dun)->v_mount),
1507 dun->un_uppervp, ap->a_cnp, un->un_path);
1508 vput(ap->a_dvp);
1509 vput(ap->a_vp);
1510 }
1511
1512 return (error);
1513 }
1514
1515 int
1516 union_symlink(v)
1517 void *v;
1518 {
1519 struct vop_symlink_args /* {
1520 struct vnode *a_dvp;
1521 struct vnode **a_vpp;
1522 struct componentname *a_cnp;
1523 struct vattr *a_vap;
1524 char *a_target;
1525 } */ *ap = v;
1526 struct union_node *un = VTOUNION(ap->a_dvp);
1527 struct vnode *dvp = un->un_uppervp;
1528 struct componentname *cnp = ap->a_cnp;
1529
1530 if (dvp != NULLVP) {
1531 int error;
1532
1533 FIXUP(un);
1534 VREF(dvp);
1535 un->un_flags |= UN_KLOCK;
1536 vput(ap->a_dvp);
1537 error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap,
1538 ap->a_target);
1539 return (error);
1540 }
1541
1542 vput(ap->a_dvp);
1543 return (EROFS);
1544 }
1545
1546 /*
1547 * union_readdir works in concert with getdirentries and
1548 * readdir(3) to provide a list of entries in the unioned
1549 * directories. getdirentries is responsible for walking
1550 * down the union stack. readdir(3) is responsible for
1551 * eliminating duplicate names from the returned data stream.
1552 */
1553 int
1554 union_readdir(v)
1555 void *v;
1556 {
1557 struct vop_readdir_args /* {
1558 struct vnodeop_desc *a_desc;
1559 struct vnode *a_vp;
1560 struct uio *a_uio;
1561 struct ucred *a_cred;
1562 int *a_eofflag;
1563 u_long *a_cookies;
1564 int a_ncookies;
1565 } */ *ap = v;
1566 struct union_node *un = VTOUNION(ap->a_vp);
1567 struct vnode *uvp = un->un_uppervp;
1568
1569 if (uvp == NULLVP)
1570 return (0);
1571
1572 FIXUP(un);
1573 ap->a_vp = uvp;
1574 return (VCALL(uvp, VOFFSET(vop_readdir), ap));
1575 }
1576
1577 int
1578 union_readlink(v)
1579 void *v;
1580 {
1581 struct vop_readlink_args /* {
1582 struct vnode *a_vp;
1583 struct uio *a_uio;
1584 struct ucred *a_cred;
1585 } */ *ap = v;
1586 int error;
1587 struct vnode *vp = OTHERVP(ap->a_vp);
1588 int dolock = (vp == LOWERVP(ap->a_vp));
1589
1590 if (dolock)
1591 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1592 else
1593 FIXUP(VTOUNION(ap->a_vp));
1594 ap->a_vp = vp;
1595 error = VCALL(vp, VOFFSET(vop_readlink), ap);
1596 if (dolock)
1597 VOP_UNLOCK(vp, 0);
1598
1599 return (error);
1600 }
1601
1602 int
1603 union_abortop(v)
1604 void *v;
1605 {
1606 struct vop_abortop_args /* {
1607 struct vnode *a_dvp;
1608 struct componentname *a_cnp;
1609 } */ *ap = v;
1610 int error;
1611 struct vnode *vp = OTHERVP(ap->a_dvp);
1612 struct union_node *un = VTOUNION(ap->a_dvp);
1613 int islocked = un->un_flags & UN_LOCKED;
1614 int dolock = (vp == LOWERVP(ap->a_dvp));
1615
1616 if (islocked) {
1617 if (dolock)
1618 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1619 else
1620 FIXUP(VTOUNION(ap->a_dvp));
1621 }
1622 ap->a_dvp = vp;
1623 error = VCALL(vp, VOFFSET(vop_abortop), ap);
1624 if (islocked && dolock)
1625 VOP_UNLOCK(vp, 0);
1626
1627 return (error);
1628 }
1629
1630 int
1631 union_inactive(v)
1632 void *v;
1633 {
1634 struct vop_inactive_args /* {
1635 struct vnode *a_vp;
1636 struct proc *a_p;
1637 } */ *ap = v;
1638 struct vnode *vp = ap->a_vp;
1639 struct union_node *un = VTOUNION(vp);
1640 struct vnode **vpp;
1641
1642 /*
1643 * Do nothing (and _don't_ bypass).
1644 * Wait to vrele lowervp until reclaim,
1645 * so that until then our union_node is in the
1646 * cache and reusable.
1647 *
1648 * NEEDSWORK: Someday, consider inactive'ing
1649 * the lowervp and then trying to reactivate it
1650 * with capabilities (v_id)
1651 * like they do in the name lookup cache code.
1652 * That's too much work for now.
1653 */
1654
1655 if (un->un_dircache != 0) {
1656 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1657 vrele(*vpp);
1658 free(un->un_dircache, M_TEMP);
1659 un->un_dircache = 0;
1660 }
1661
1662 VOP_UNLOCK(vp, 0);
1663
1664 if ((un->un_flags & UN_CACHED) == 0)
1665 vgone(vp);
1666
1667 return (0);
1668 }
1669
1670 int
1671 union_reclaim(v)
1672 void *v;
1673 {
1674 struct vop_reclaim_args /* {
1675 struct vnode *a_vp;
1676 } */ *ap = v;
1677
1678 union_freevp(ap->a_vp);
1679
1680 return (0);
1681 }
1682
1683 int
1684 union_lock(v)
1685 void *v;
1686 {
1687 struct vop_lock_args /* {
1688 struct vnode *a_vp;
1689 int a_flags;
1690 } */ *ap = v;
1691 struct vnode *vp = ap->a_vp;
1692 int flags = ap->a_flags;
1693 struct union_node *un;
1694 int error;
1695 #ifdef DIAGNOSTIC
1696 int drain = 0;
1697 #endif
1698
1699 genfs_nolock(ap);
1700 /*
1701 * Need to do real lockmgr-style locking here.
1702 * in the mean time, draining won't work quite right,
1703 * which could lead to a few race conditions.
1704 * the following test was here, but is not quite right, we
1705 * still need to take the lock:
1706 if ((flags & LK_TYPE_MASK) == LK_DRAIN)
1707 return (0);
1708 */
1709 flags &= ~LK_INTERLOCK;
1710
1711 un = VTOUNION(vp);
1712 #ifdef DIAGNOSTIC
1713 if (un->un_flags & (UN_DRAINING|UN_DRAINED)) {
1714 if (un->un_flags & UN_DRAINED)
1715 panic("union: %p: warning: locking decommissioned lock", vp);
1716 if ((flags & LK_TYPE_MASK) != LK_RELEASE)
1717 panic("union: %p: non-release on draining lock: %d",
1718 vp, flags & LK_TYPE_MASK);
1719 un->un_flags &= ~UN_DRAINING;
1720 if ((flags & LK_REENABLE) == 0)
1721 un->un_flags |= UN_DRAINED;
1722 }
1723 #endif
1724
1725 /*
1726 * Don't pass DRAIN through to sub-vnode lock; keep track of
1727 * DRAIN state at this level, and just get an exclusive lock
1728 * on the underlying vnode.
1729 */
1730 if ((flags & LK_TYPE_MASK) == LK_DRAIN) {
1731 #ifdef DIAGNOSTIC
1732 drain = 1;
1733 #endif
1734 flags = LK_EXCLUSIVE | (flags & ~LK_TYPE_MASK);
1735 }
1736 start:
1737 un = VTOUNION(vp);
1738
1739 if (un->un_uppervp != NULLVP) {
1740 if (((un->un_flags & UN_ULOCK) == 0) &&
1741 (vp->v_usecount != 0)) {
1742 /*
1743 * We MUST always use the order of: take upper
1744 * vp lock, manipulate union node flags, drop
1745 * upper vp lock. This code must not be an
1746 */
1747 error = vn_lock(un->un_uppervp, flags);
1748 if (error)
1749 return (error);
1750 un->un_flags |= UN_ULOCK;
1751 }
1752 #ifdef DIAGNOSTIC
1753 if (un->un_flags & UN_KLOCK) {
1754 vprint("union: dangling klock", vp);
1755 panic("union: dangling upper lock (%p)", vp);
1756 }
1757 #endif
1758 }
1759
1760 /* XXX ignores LK_NOWAIT */
1761 if (un->un_flags & UN_LOCKED) {
1762 #ifdef DIAGNOSTIC
1763 if (curproc && un->un_pid == curproc->p_pid &&
1764 un->un_pid > -1 && curproc->p_pid > -1)
1765 panic("union: locking against myself");
1766 #endif
1767 un->un_flags |= UN_WANTED;
1768 tsleep((caddr_t)&un->un_flags, PINOD, "unionlk2", 0);
1769 goto start;
1770 }
1771
1772 #ifdef DIAGNOSTIC
1773 if (curproc)
1774 un->un_pid = curproc->p_pid;
1775 else
1776 un->un_pid = -1;
1777 if (drain)
1778 un->un_flags |= UN_DRAINING;
1779 #endif
1780
1781 un->un_flags |= UN_LOCKED;
1782 return (0);
1783 }
1784
1785 /*
1786 * When operations want to vput() a union node yet retain a lock on
1787 * the upper vnode (say, to do some further operations like link(),
1788 * mkdir(), ...), they set UN_KLOCK on the union node, then call
1789 * vput() which calls VOP_UNLOCK() and comes here. union_unlock()
1790 * unlocks the union node (leaving the upper vnode alone), clears the
1791 * KLOCK flag, and then returns to vput(). The caller then does whatever
1792 * is left to do with the upper vnode, and ensures that it gets unlocked.
1793 *
1794 * If UN_KLOCK isn't set, then the upper vnode is unlocked here.
1795 */
1796 int
1797 union_unlock(v)
1798 void *v;
1799 {
1800 struct vop_unlock_args /* {
1801 struct vnode *a_vp;
1802 int a_flags;
1803 } */ *ap = v;
1804 struct union_node *un = VTOUNION(ap->a_vp);
1805
1806 #ifdef DIAGNOSTIC
1807 if ((un->un_flags & UN_LOCKED) == 0)
1808 panic("union: unlock unlocked node");
1809 if (curproc && un->un_pid != curproc->p_pid &&
1810 curproc->p_pid > -1 && un->un_pid > -1)
1811 panic("union: unlocking other process's union node");
1812 if (un->un_flags & UN_DRAINED)
1813 panic("union: %p: warning: unlocking decommissioned lock", ap->a_vp);
1814 #endif
1815
1816 un->un_flags &= ~UN_LOCKED;
1817
1818 if ((un->un_flags & (UN_ULOCK|UN_KLOCK)) == UN_ULOCK)
1819 VOP_UNLOCK(un->un_uppervp, 0);
1820
1821 un->un_flags &= ~(UN_ULOCK|UN_KLOCK);
1822
1823 if (un->un_flags & UN_WANTED) {
1824 un->un_flags &= ~UN_WANTED;
1825 wakeup((caddr_t) &un->un_flags);
1826 }
1827
1828 #ifdef DIAGNOSTIC
1829 un->un_pid = 0;
1830 if (un->un_flags & UN_DRAINING) {
1831 un->un_flags |= UN_DRAINED;
1832 un->un_flags &= ~UN_DRAINING;
1833 }
1834 #endif
1835 genfs_nounlock(ap);
1836
1837 return (0);
1838 }
1839
1840 int
1841 union_bmap(v)
1842 void *v;
1843 {
1844 struct vop_bmap_args /* {
1845 struct vnode *a_vp;
1846 daddr_t a_bn;
1847 struct vnode **a_vpp;
1848 daddr_t *a_bnp;
1849 int *a_runp;
1850 } */ *ap = v;
1851 int error;
1852 struct vnode *vp = OTHERVP(ap->a_vp);
1853 int dolock = (vp == LOWERVP(ap->a_vp));
1854
1855 if (dolock)
1856 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1857 else
1858 FIXUP(VTOUNION(ap->a_vp));
1859 ap->a_vp = vp;
1860 error = VCALL(vp, VOFFSET(vop_bmap), ap);
1861 if (dolock)
1862 VOP_UNLOCK(vp, 0);
1863
1864 return (error);
1865 }
1866
1867 int
1868 union_print(v)
1869 void *v;
1870 {
1871 struct vop_print_args /* {
1872 struct vnode *a_vp;
1873 } */ *ap = v;
1874 struct vnode *vp = ap->a_vp;
1875
1876 printf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1877 vp, UPPERVP(vp), LOWERVP(vp));
1878 if (UPPERVP(vp) != NULLVP)
1879 vprint("union: upper", UPPERVP(vp));
1880 if (LOWERVP(vp) != NULLVP)
1881 vprint("union: lower", LOWERVP(vp));
1882 if (VTOUNION(vp)->un_dircache) {
1883 struct vnode **vpp;
1884 for (vpp = VTOUNION(vp)->un_dircache; *vpp != NULLVP; vpp++)
1885 vprint("dircache:", *vpp);
1886 }
1887
1888 return (0);
1889 }
1890
1891 int
1892 union_islocked(v)
1893 void *v;
1894 {
1895 struct vop_islocked_args /* {
1896 struct vnode *a_vp;
1897 } */ *ap = v;
1898
1899 return ((VTOUNION(ap->a_vp)->un_flags & UN_LOCKED) ? 1 : 0);
1900 }
1901
1902 int
1903 union_pathconf(v)
1904 void *v;
1905 {
1906 struct vop_pathconf_args /* {
1907 struct vnode *a_vp;
1908 int a_name;
1909 int *a_retval;
1910 } */ *ap = v;
1911 int error;
1912 struct vnode *vp = OTHERVP(ap->a_vp);
1913 int dolock = (vp == LOWERVP(ap->a_vp));
1914
1915 if (dolock)
1916 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1917 else
1918 FIXUP(VTOUNION(ap->a_vp));
1919 ap->a_vp = vp;
1920 error = VCALL(vp, VOFFSET(vop_pathconf), ap);
1921 if (dolock)
1922 VOP_UNLOCK(vp, 0);
1923
1924 return (error);
1925 }
1926
1927 int
1928 union_advlock(v)
1929 void *v;
1930 {
1931 struct vop_advlock_args /* {
1932 struct vnode *a_vp;
1933 caddr_t a_id;
1934 int a_op;
1935 struct flock *a_fl;
1936 int a_flags;
1937 } */ *ap = v;
1938 struct vnode *ovp = OTHERVP(ap->a_vp);
1939
1940 ap->a_vp = ovp;
1941 return (VCALL(ovp, VOFFSET(vop_advlock), ap));
1942 }
1943
1944
1945 /*
1946 * XXX - vop_strategy must be hand coded because it has no
1947 * vnode in its arguments.
1948 * This goes away with a merged VM/buffer cache.
1949 */
1950 int
1951 union_strategy(v)
1952 void *v;
1953 {
1954 struct vop_strategy_args /* {
1955 struct buf *a_bp;
1956 } */ *ap = v;
1957 struct buf *bp = ap->a_bp;
1958 int error;
1959 struct vnode *savedvp;
1960
1961 savedvp = bp->b_vp;
1962 bp->b_vp = OTHERVP(bp->b_vp);
1963
1964 #ifdef DIAGNOSTIC
1965 if (bp->b_vp == NULLVP)
1966 panic("union_strategy: nil vp");
1967 if (((bp->b_flags & B_READ) == 0) &&
1968 (bp->b_vp == LOWERVP(savedvp)))
1969 panic("union_strategy: writing to lowervp");
1970 #endif
1971
1972 error = VOP_STRATEGY(bp);
1973 bp->b_vp = savedvp;
1974
1975 return (error);
1976 }
1977
1978 int
1979 union_getpages(v)
1980 void *v;
1981 {
1982 struct vop_getpages_args /* {
1983 struct vnode *a_vp;
1984 voff_t a_offset;
1985 struct vm_page **a_m;
1986 int *a_count;
1987 int a_centeridx;
1988 vm_prot_t a_access_type;
1989 int a_advice;
1990 int a_flags;
1991 } */ *ap = v;
1992 struct vnode *vp = ap->a_vp;
1993 int error;
1994
1995 /*
1996 * just pass the request on to the underlying layer.
1997 */
1998
1999 if (ap->a_flags & PGO_LOCKED) {
2000 return EBUSY;
2001 }
2002 ap->a_vp = OTHERVP(vp);
2003 simple_unlock(&vp->v_interlock);
2004 simple_lock(&ap->a_vp->v_interlock);
2005 error = VCALL(ap->a_vp, VOFFSET(vop_getpages), ap);
2006 return error;
2007 }
2008
2009 int
2010 union_putpages(v)
2011 void *v;
2012 {
2013 struct vop_putpages_args /* {
2014 struct vnode *a_vp;
2015 voff_t a_offlo;
2016 voff_t a_offhi;
2017 int a_flags;
2018 } */ *ap = v;
2019 struct vnode *vp = ap->a_vp;
2020 int error;
2021
2022 /*
2023 * just pass the request on to the underlying layer.
2024 */
2025
2026 ap->a_vp = OTHERVP(vp);
2027 simple_unlock(&vp->v_interlock);
2028 simple_lock(&ap->a_vp->v_interlock);
2029 error = VCALL(ap->a_vp, VOFFSET(vop_putpages), ap);
2030 return error;
2031 }
2032