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