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