kernel - Finish implementing PG_RAM / pipelined mmap operation
[dragonfly.git] / sys / vfs / union / union_vnops.c
blobef36d616ce5c33a7fb773db0944706e1addb7cbf
1 /*
2 * Copyright (c) 1992, 1993, 1994, 1995 Jan-Simon Pendry.
3 * Copyright (c) 1992, 1993, 1994, 1995
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
37 * @(#)union_vnops.c 8.32 (Berkeley) 6/23/95
38 * $FreeBSD: src/sys/miscfs/union/union_vnops.c,v 1.72 1999/12/15 23:02:14 eivind Exp $
39 * $DragonFly: src/sys/vfs/union/union_vnops.c,v 1.39 2007/11/20 21:03:51 dillon Exp $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/fcntl.h>
46 #include <sys/stat.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/namei.h>
51 #include <sys/malloc.h>
52 #include <sys/buf.h>
53 #include <sys/lock.h>
54 #include <sys/sysctl.h>
55 #include "union.h"
57 #include <vm/vm.h>
58 #include <vm/vnode_pager.h>
60 #include <vm/vm_page.h>
61 #include <vm/vm_object.h>
63 int uniondebug = 0;
65 #if UDEBUG_ENABLED
66 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RW, &uniondebug, 0, "");
67 #else
68 SYSCTL_INT(_vfs, OID_AUTO, uniondebug, CTLFLAG_RD, &uniondebug, 0, "");
69 #endif
71 static int union_access (struct vop_access_args *ap);
72 static int union_advlock (struct vop_advlock_args *ap);
73 static int union_bmap (struct vop_bmap_args *ap);
74 static int union_close (struct vop_close_args *ap);
75 static int union_create (struct vop_old_create_args *ap);
76 static int union_fsync (struct vop_fsync_args *ap);
77 static int union_getattr (struct vop_getattr_args *ap);
78 static int union_inactive (struct vop_inactive_args *ap);
79 static int union_ioctl (struct vop_ioctl_args *ap);
80 static int union_link (struct vop_old_link_args *ap);
81 static int union_lookup (struct vop_old_lookup_args *ap);
82 static int union_lookup1 (struct vnode *udvp, struct vnode **dvp,
83 struct vnode **vpp,
84 struct componentname *cnp);
85 static int union_mkdir (struct vop_old_mkdir_args *ap);
86 static int union_mknod (struct vop_old_mknod_args *ap);
87 static int union_mmap (struct vop_mmap_args *ap);
88 static int union_open (struct vop_open_args *ap);
89 static int union_pathconf (struct vop_pathconf_args *ap);
90 static int union_print (struct vop_print_args *ap);
91 static int union_read (struct vop_read_args *ap);
92 static int union_readdir (struct vop_readdir_args *ap);
93 static int union_readlink (struct vop_readlink_args *ap);
94 static int union_reclaim (struct vop_reclaim_args *ap);
95 static int union_remove (struct vop_old_remove_args *ap);
96 static int union_rename (struct vop_old_rename_args *ap);
97 static int union_rmdir (struct vop_old_rmdir_args *ap);
98 static int union_poll (struct vop_poll_args *ap);
99 static int union_setattr (struct vop_setattr_args *ap);
100 static int union_strategy (struct vop_strategy_args *ap);
101 static int union_getpages (struct vop_getpages_args *ap);
102 static int union_putpages (struct vop_putpages_args *ap);
103 static int union_symlink (struct vop_old_symlink_args *ap);
104 static int union_whiteout (struct vop_old_whiteout_args *ap);
105 static int union_write (struct vop_read_args *ap);
107 static __inline
108 struct vnode *
109 union_lock_upper(struct union_node *un, struct thread *td)
111 struct vnode *uppervp;
113 if ((uppervp = un->un_uppervp) != NULL) {
114 vref(uppervp);
115 vn_lock(uppervp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
117 KASSERT((uppervp == NULL || uppervp->v_sysref.refcnt > 0), ("uppervp usecount is 0"));
118 return(uppervp);
121 static __inline
122 struct vnode *
123 union_ref_upper(struct union_node *un)
125 struct vnode *uppervp;
127 if ((uppervp = un->un_uppervp) != NULL) {
128 vref(uppervp);
129 if (uppervp->v_flag & VRECLAIMED) {
130 vrele(uppervp);
131 return (NULLVP);
134 return (uppervp);
137 static __inline
138 void
139 union_unlock_upper(struct vnode *uppervp, struct thread *td)
141 vput(uppervp);
144 static __inline
145 struct vnode *
146 union_lock_other(struct union_node *un, struct thread *td)
148 struct vnode *vp;
150 if (un->un_uppervp != NULL) {
151 vp = union_lock_upper(un, td);
152 } else if ((vp = un->un_lowervp) != NULL) {
153 vref(vp);
154 vn_lock(vp, LK_EXCLUSIVE | LK_CANRECURSE | LK_RETRY);
156 return(vp);
159 static __inline
160 void
161 union_unlock_other(struct vnode *vp, struct thread *td)
163 vput(vp);
167 * union_lookup:
169 * udvp must be exclusively locked on call and will remain
170 * exclusively locked on return. This is the mount point
171 * for out filesystem.
173 * dvp Our base directory, locked and referenced.
174 * The passed dvp will be dereferenced and unlocked on return
175 * and a new dvp will be returned which is locked and
176 * referenced in the same variable.
178 * vpp is filled in with the result if no error occured,
179 * locked and ref'd.
181 * If an error is returned, *vpp is set to NULLVP. If no
182 * error occurs, *vpp is returned with a reference and an
183 * exclusive lock.
186 static int
187 union_lookup1(struct vnode *udvp, struct vnode **pdvp, struct vnode **vpp,
188 struct componentname *cnp)
190 int error;
191 struct thread *td = cnp->cn_td;
192 struct vnode *dvp = *pdvp;
193 struct vnode *tdvp;
194 struct mount *mp;
197 * If stepping up the directory tree, check for going
198 * back across the mount point, in which case do what
199 * lookup would do by stepping back down the mount
200 * hierarchy.
202 if (cnp->cn_flags & CNP_ISDOTDOT) {
203 while ((dvp != udvp) && (dvp->v_flag & VROOT)) {
205 * Don't do the NOCROSSMOUNT check
206 * at this level. By definition,
207 * union fs deals with namespaces, not
208 * filesystems.
210 tdvp = dvp;
211 dvp = dvp->v_mount->mnt_vnodecovered;
212 vref(dvp);
213 vput(tdvp);
214 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
219 * Set return dvp to be the upperdvp 'parent directory.
221 *pdvp = dvp;
224 * If the VOP_LOOKUP call generates an error, tdvp is invalid and no
225 * changes will have been made to dvp, so we are set to return.
228 error = VOP_LOOKUP(dvp, &tdvp, cnp);
229 if (error) {
230 UDEBUG(("dvp %p error %d flags %lx\n", dvp, error, cnp->cn_flags));
231 *vpp = NULL;
232 return (error);
236 * The parent directory will have been unlocked, unless lookup
237 * found the last component or if dvp == tdvp (tdvp must be locked).
239 * We want our dvp to remain locked and ref'd. We also want tdvp
240 * to remain locked and ref'd.
242 UDEBUG(("parentdir %p result %p flag %lx\n", dvp, tdvp, cnp->cn_flags));
244 #if 0
245 if (dvp != tdvp && (cnp->cn_flags & CNP_XXXISLASTCN) == 0)
246 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
247 #endif
250 * Lastly check if the current node is a mount point in
251 * which case walk up the mount hierarchy making sure not to
252 * bump into the root of the mount tree (ie. dvp != udvp).
254 * We use dvp as a temporary variable here, it is no longer related
255 * to the dvp above. However, we have to ensure that both *pdvp and
256 * tdvp are locked on return.
259 dvp = tdvp;
260 while (
261 dvp != udvp &&
262 (dvp->v_type == VDIR) &&
263 (mp = dvp->v_mountedhere)
265 int relock_pdvp = 0;
267 if (vfs_busy(mp, 0))
268 continue;
270 if (dvp == *pdvp)
271 relock_pdvp = 1;
272 vput(dvp);
273 dvp = NULL;
274 error = VFS_ROOT(mp, &dvp);
276 vfs_unbusy(mp);
278 if (relock_pdvp)
279 vn_lock(*pdvp, LK_EXCLUSIVE | LK_RETRY);
281 if (error) {
282 *vpp = NULL;
283 return (error);
286 *vpp = dvp;
287 return (0);
291 * union_lookup(struct vnode *a_dvp, struct vnode **a_vpp,
292 * struct componentname *a_cnp)
294 static int
295 union_lookup(struct vop_old_lookup_args *ap)
297 int error;
298 int uerror, lerror;
299 struct vnode *uppervp, *lowervp;
300 struct vnode *upperdvp, *lowerdvp;
301 struct vnode *dvp = ap->a_dvp; /* starting dir */
302 struct union_node *dun = VTOUNION(dvp); /* associated union node */
303 struct componentname *cnp = ap->a_cnp;
304 struct thread *td = cnp->cn_td;
305 int lockparent = cnp->cn_flags & CNP_LOCKPARENT;
306 struct union_mount *um = MOUNTTOUNIONMOUNT(dvp->v_mount);
307 struct ucred *saved_cred = NULL;
308 int iswhiteout;
309 struct vattr va;
311 *ap->a_vpp = NULLVP;
314 * Disallow write attemps to the filesystem mounted read-only.
316 if ((dvp->v_mount->mnt_flag & MNT_RDONLY) &&
317 (cnp->cn_nameiop == NAMEI_DELETE || cnp->cn_nameiop == NAMEI_RENAME)) {
318 return (EROFS);
322 * For any lookup's we do, always return with the parent locked
324 cnp->cn_flags |= CNP_LOCKPARENT;
326 lowerdvp = dun->un_lowervp;
327 uppervp = NULLVP;
328 lowervp = NULLVP;
329 iswhiteout = 0;
331 uerror = ENOENT;
332 lerror = ENOENT;
335 * Get a private lock on uppervp and a reference, effectively
336 * taking it out of the union_node's control.
338 * We must lock upperdvp while holding our lock on dvp
339 * to avoid a deadlock.
341 upperdvp = union_lock_upper(dun, td);
344 * do the lookup in the upper level.
345 * if that level comsumes additional pathnames,
346 * then assume that something special is going
347 * on and just return that vnode.
349 if (upperdvp != NULLVP) {
351 * We do not have to worry about the DOTDOT case, we've
352 * already unlocked dvp.
354 UDEBUG(("A %p\n", upperdvp));
357 * Do the lookup. We must supply a locked and referenced
358 * upperdvp to the function and will get a new locked and
359 * referenced upperdvp back with the old having been
360 * dereferenced.
362 * If an error is returned, uppervp will be NULLVP. If no
363 * error occurs, uppervp will be the locked and referenced
364 * return vnode or possibly NULL, depending on what is being
365 * requested. It is possible that the returned uppervp
366 * will be the same as upperdvp.
368 uerror = union_lookup1(um->um_uppervp, &upperdvp, &uppervp, cnp);
369 UDEBUG((
370 "uerror %d upperdvp %p %d/%d, uppervp %p ref=%d/lck=%d\n",
371 uerror,
372 upperdvp,
373 upperdvp->v_sysref.refcnt,
374 vn_islocked(upperdvp),
375 uppervp,
376 (uppervp ? uppervp->v_sysref.refcnt : -99),
377 (uppervp ? vn_islocked(uppervp) : -99)
381 * Disallow write attemps to the filesystem mounted read-only.
383 if (uerror == EJUSTRETURN &&
384 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
385 (cnp->cn_nameiop == NAMEI_CREATE || cnp->cn_nameiop == NAMEI_RENAME)) {
386 error = EROFS;
387 goto out;
391 * Special case. If cn_consume != 0 skip out. The result
392 * of the lookup is transfered to our return variable. If
393 * an error occured we have to throw away the results.
396 if (cnp->cn_consume != 0) {
397 if ((error = uerror) == 0) {
398 *ap->a_vpp = uppervp;
399 uppervp = NULL;
401 goto out;
405 * Calculate whiteout, fall through
408 if (uerror == ENOENT || uerror == EJUSTRETURN) {
409 if (cnp->cn_flags & CNP_ISWHITEOUT) {
410 iswhiteout = 1;
411 } else if (lowerdvp != NULLVP) {
412 int terror;
414 terror = VOP_GETATTR(upperdvp, &va);
415 if (terror == 0 && (va.va_flags & OPAQUE))
416 iswhiteout = 1;
422 * in a similar way to the upper layer, do the lookup
423 * in the lower layer. this time, if there is some
424 * component magic going on, then vput whatever we got
425 * back from the upper layer and return the lower vnode
426 * instead.
429 if (lowerdvp != NULLVP && !iswhiteout) {
430 int nameiop;
432 UDEBUG(("B %p\n", lowerdvp));
435 * Force only LOOKUPs on the lower node, since
436 * we won't be making changes to it anyway.
438 nameiop = cnp->cn_nameiop;
439 cnp->cn_nameiop = NAMEI_LOOKUP;
440 if (um->um_op == UNMNT_BELOW) {
441 saved_cred = cnp->cn_cred;
442 cnp->cn_cred = um->um_cred;
446 * We shouldn't have to worry about locking interactions
447 * between the lower layer and our union layer (w.r.t.
448 * `..' processing) because we don't futz with lowervp
449 * locks in the union-node instantiation code path.
451 * union_lookup1() requires lowervp to be locked on entry,
452 * and it will be unlocked on return. The ref count will
453 * not change. On return lowervp doesn't represent anything
454 * to us so we NULL it out.
456 vref(lowerdvp);
457 vn_lock(lowerdvp, LK_EXCLUSIVE | LK_RETRY);
458 lerror = union_lookup1(um->um_lowervp, &lowerdvp, &lowervp, cnp);
459 if (lowerdvp == lowervp)
460 vrele(lowerdvp);
461 else
462 vput(lowerdvp);
463 lowerdvp = NULL; /* lowerdvp invalid after vput */
465 if (um->um_op == UNMNT_BELOW)
466 cnp->cn_cred = saved_cred;
467 cnp->cn_nameiop = nameiop;
469 if (cnp->cn_consume != 0 || lerror == EACCES) {
470 if ((error = lerror) == 0) {
471 *ap->a_vpp = lowervp;
472 lowervp = NULL;
474 goto out;
476 } else {
477 UDEBUG(("C %p\n", lowerdvp));
478 if ((cnp->cn_flags & CNP_ISDOTDOT) && dun->un_pvp != NULLVP) {
479 if ((lowervp = LOWERVP(dun->un_pvp)) != NULL) {
480 vref(lowervp);
481 vn_lock(lowervp, LK_EXCLUSIVE | LK_RETRY);
482 lerror = 0;
488 * Ok. Now we have uerror, uppervp, upperdvp, lerror, and lowervp.
490 * 1. If both layers returned an error, select the upper layer.
492 * 2. If the upper layer faile and the bottom layer succeeded,
493 * two subcases occur:
495 * a. The bottom vnode is not a directory, in which case
496 * just return a new union vnode referencing an
497 * empty top layer and the existing bottom layer.
499 * b. The button vnode is a directory, in which case
500 * create a new directory in the top layer and
501 * and fall through to case 3.
503 * 3. If the top layer succeeded then return a new union
504 * vnode referencing whatever the new top layer and
505 * whatever the bottom layer returned.
508 /* case 1. */
509 if ((uerror != 0) && (lerror != 0)) {
510 error = uerror;
511 goto out;
514 /* case 2. */
515 if (uerror != 0 /* && (lerror == 0) */ ) {
516 if (lowervp->v_type == VDIR) { /* case 2b. */
517 KASSERT(uppervp == NULL, ("uppervp unexpectedly non-NULL"));
519 * oops, uppervp has a problem, we may have to shadow.
521 uerror = union_mkshadow(um, upperdvp, cnp, &uppervp);
522 if (uerror) {
523 error = uerror;
524 goto out;
530 * Must call union_allocvp with both the upper and lower vnodes
531 * referenced and the upper vnode locked. ap->a_vpp is returned
532 * referenced and locked. lowervp, uppervp, and upperdvp are
533 * absorbed by union_allocvp() whether it succeeds or fails.
535 * upperdvp is the parent directory of uppervp which may be
536 * different, depending on the path, from dvp->un_uppervp. That's
537 * why it is a separate argument. Note that it must be unlocked.
539 * dvp must be locked on entry to the call and will be locked on
540 * return.
543 if (uppervp && uppervp != upperdvp)
544 vn_unlock(uppervp);
545 if (lowervp)
546 vn_unlock(lowervp);
547 if (upperdvp)
548 vn_unlock(upperdvp);
550 error = union_allocvp(ap->a_vpp, dvp->v_mount, dvp, upperdvp, cnp,
551 uppervp, lowervp, 1);
553 UDEBUG(("Create %p = %p %p refs=%d\n", *ap->a_vpp, uppervp, lowervp, (*ap->a_vpp) ? ((*ap->a_vpp)->v_sysref.refcnt) : -99));
555 uppervp = NULL;
556 upperdvp = NULL;
557 lowervp = NULL;
560 * Termination Code
562 * - put away any extra junk laying around. Note that lowervp
563 * (if not NULL) will never be the same as *ap->a_vp and
564 * neither will uppervp, because when we set that state we
565 * NULL-out lowervp or uppervp. On the otherhand, upperdvp
566 * may match uppervp or *ap->a_vpp.
568 * - relock/unlock dvp if appropriate.
571 out:
572 if (upperdvp) {
573 if (upperdvp == uppervp || upperdvp == *ap->a_vpp)
574 vrele(upperdvp);
575 else
576 vput(upperdvp);
579 if (uppervp)
580 vput(uppervp);
582 if (lowervp)
583 vput(lowervp);
586 * Restore LOCKPARENT state
589 if (!lockparent)
590 cnp->cn_flags &= ~CNP_LOCKPARENT;
592 UDEBUG(("Out %d vpp %p/%d lower %p upper %p\n", error, *ap->a_vpp,
593 ((*ap->a_vpp) ? (*ap->a_vpp)->v_sysref.refcnt : -99),
594 lowervp, uppervp));
597 * dvp lock state, determine whether to relock dvp. dvp is expected
598 * to be locked on return if:
600 * - there was an error (except not EJUSTRETURN), or
601 * - we hit the last component and lockparent is true
603 * dvp_is_locked is the current state of the dvp lock, not counting
604 * the possibility that *ap->a_vpp == dvp (in which case it is locked
605 * anyway). Note that *ap->a_vpp == dvp only if no error occured.
608 if (*ap->a_vpp != dvp) {
609 if ((error == 0 || error == EJUSTRETURN) && !lockparent) {
610 vn_unlock(dvp);
615 * Diagnostics
618 #ifdef DIAGNOSTIC
619 if (cnp->cn_namelen == 1 &&
620 cnp->cn_nameptr[0] == '.' &&
621 *ap->a_vpp != dvp) {
622 panic("union_lookup returning . (%p) not same as startdir (%p)", ap->a_vpp, dvp);
624 #endif
626 return (error);
630 * union_create:
632 * a_dvp is locked on entry and remains locked on return. a_vpp is returned
633 * locked if no error occurs, otherwise it is garbage.
635 * union_create(struct vnode *a_dvp, struct vnode **a_vpp,
636 * struct componentname *a_cnp, struct vattr *a_vap)
638 static int
639 union_create(struct vop_old_create_args *ap)
641 struct union_node *dun = VTOUNION(ap->a_dvp);
642 struct componentname *cnp = ap->a_cnp;
643 struct thread *td = cnp->cn_td;
644 struct vnode *dvp;
645 int error = EROFS;
647 if ((dvp = union_lock_upper(dun, td)) != NULL) {
648 struct vnode *vp;
649 struct mount *mp;
651 error = VOP_CREATE(dvp, &vp, cnp, ap->a_vap);
652 if (error == 0) {
653 mp = ap->a_dvp->v_mount;
654 vn_unlock(vp);
655 UDEBUG(("ALLOCVP-1 FROM %p REFS %d\n", vp, vp->v_sysref.refcnt));
656 error = union_allocvp(ap->a_vpp, mp, NULLVP, NULLVP,
657 cnp, vp, NULLVP, 1);
658 UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_sysref.refcnt));
660 union_unlock_upper(dvp, td);
662 return (error);
666 * union_whiteout(struct vnode *a_dvp, struct componentname *a_cnp,
667 * int a_flags)
669 static int
670 union_whiteout(struct vop_old_whiteout_args *ap)
672 struct union_node *un = VTOUNION(ap->a_dvp);
673 struct componentname *cnp = ap->a_cnp;
674 struct vnode *uppervp;
675 int error = EOPNOTSUPP;
677 if ((uppervp = union_lock_upper(un, cnp->cn_td)) != NULLVP) {
678 error = VOP_WHITEOUT(un->un_uppervp, cnp, ap->a_flags);
679 union_unlock_upper(uppervp, cnp->cn_td);
681 return(error);
685 * union_mknod:
687 * a_dvp is locked on entry and should remain locked on return.
688 * a_vpp is garbagre whether an error occurs or not.
690 * union_mknod(struct vnode *a_dvp, struct vnode **a_vpp,
691 * struct componentname *a_cnp, struct vattr *a_vap)
693 static int
694 union_mknod(struct vop_old_mknod_args *ap)
696 struct union_node *dun = VTOUNION(ap->a_dvp);
697 struct componentname *cnp = ap->a_cnp;
698 struct vnode *dvp;
699 int error = EROFS;
701 if ((dvp = union_lock_upper(dun, cnp->cn_td)) != NULL) {
702 error = VOP_MKNOD(dvp, ap->a_vpp, cnp, ap->a_vap);
703 union_unlock_upper(dvp, cnp->cn_td);
705 return (error);
709 * union_open:
711 * run open VOP. When opening the underlying vnode we have to mimic
712 * vn_open. What we *really* need to do to avoid screwups if the
713 * open semantics change is to call vn_open(). For example, ufs blows
714 * up if you open a file but do not vmio it prior to writing.
716 * union_open(struct vnode *a_vp, int a_mode,
717 * struct ucred *a_cred, struct thread *a_td)
719 static int
720 union_open(struct vop_open_args *ap)
722 struct union_node *un = VTOUNION(ap->a_vp);
723 struct vnode *tvp;
724 int mode = ap->a_mode;
725 struct ucred *cred = ap->a_cred;
726 struct thread *td = ap->a_td;
727 int error = 0;
728 int tvpisupper = 1;
731 * If there is an existing upper vp then simply open that.
732 * The upper vp takes precedence over the lower vp. When opening
733 * a lower vp for writing copy it to the uppervp and then open the
734 * uppervp.
736 * At the end of this section tvp will be left locked.
738 if ((tvp = union_lock_upper(un, td)) == NULLVP) {
740 * If the lower vnode is being opened for writing, then
741 * copy the file contents to the upper vnode and open that,
742 * otherwise can simply open the lower vnode.
744 tvp = un->un_lowervp;
745 if ((ap->a_mode & FWRITE) && (tvp->v_type == VREG)) {
746 int docopy = !(mode & O_TRUNC);
747 error = union_copyup(un, docopy, cred, td);
748 tvp = union_lock_upper(un, td);
749 } else {
750 un->un_openl++;
751 vref(tvp);
752 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY);
753 tvpisupper = 0;
758 * We are holding the correct vnode, open it. Note
759 * that in DragonFly, VOP_OPEN is responsible for associating
760 * a VM object with the vnode if the vnode is mappable or the
761 * underlying filesystem uses buffer cache calls on it.
763 if (error == 0)
764 error = VOP_OPEN(tvp, mode, cred, NULL);
767 * Release any locks held
769 if (tvpisupper) {
770 if (tvp)
771 union_unlock_upper(tvp, td);
772 } else {
773 vput(tvp);
775 return (error);
779 * union_close:
781 * It is unclear whether a_vp is passed locked or unlocked. Whatever
782 * the case we do not change it.
784 * union_close(struct vnode *a_vp, int a_fflag, struct ucred *a_cred,
785 * struct thread *a_td)
787 static int
788 union_close(struct vop_close_args *ap)
790 struct union_node *un = VTOUNION(ap->a_vp);
791 struct vnode *vp;
793 if ((vp = un->un_uppervp) == NULLVP) {
794 #ifdef UNION_DIAGNOSTIC
795 if (un->un_openl <= 0)
796 panic("union: un_openl cnt");
797 #endif
798 --un->un_openl;
799 vp = un->un_lowervp;
801 ap->a_head.a_ops = *vp->v_ops;
802 ap->a_vp = vp;
803 return(vop_close_ap(ap));
807 * Check access permission on the union vnode.
808 * The access check being enforced is to check
809 * against both the underlying vnode, and any
810 * copied vnode. This ensures that no additional
811 * file permissions are given away simply because
812 * the user caused an implicit file copy.
814 * union_access(struct vnode *a_vp, int a_mode,
815 * struct ucred *a_cred, struct thread *a_td)
817 static int
818 union_access(struct vop_access_args *ap)
820 struct union_node *un = VTOUNION(ap->a_vp);
821 struct thread *td = ap->a_td;
822 int error = EACCES;
823 struct vnode *vp;
826 * Disallow write attempts on filesystems mounted read-only.
828 if ((ap->a_mode & VWRITE) &&
829 (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)) {
830 switch (ap->a_vp->v_type) {
831 case VREG:
832 case VDIR:
833 case VLNK:
834 return (EROFS);
835 default:
836 break;
840 if ((vp = union_lock_upper(un, td)) != NULLVP) {
841 ap->a_head.a_ops = *vp->v_ops;
842 ap->a_vp = vp;
843 error = vop_access_ap(ap);
844 union_unlock_upper(vp, td);
845 return(error);
848 if ((vp = un->un_lowervp) != NULLVP) {
849 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
850 ap->a_head.a_ops = *vp->v_ops;
851 ap->a_vp = vp;
854 * Remove VWRITE from a_mode if our mount point is RW, because
855 * we want to allow writes and lowervp may be read-only.
857 if ((un->un_vnode->v_mount->mnt_flag & MNT_RDONLY) == 0)
858 ap->a_mode &= ~VWRITE;
860 error = vop_access_ap(ap);
861 if (error == 0) {
862 struct union_mount *um;
864 um = MOUNTTOUNIONMOUNT(un->un_vnode->v_mount);
866 if (um->um_op == UNMNT_BELOW) {
867 ap->a_cred = um->um_cred;
868 error = vop_access_ap(ap);
871 vn_unlock(vp);
873 return(error);
877 * We handle getattr only to change the fsid and
878 * track object sizes
880 * It's not clear whether VOP_GETATTR is to be
881 * called with the vnode locked or not. stat() calls
882 * it with (vp) locked, and fstat calls it with
883 * (vp) unlocked.
885 * Because of this we cannot use our normal locking functions
886 * if we do not intend to lock the main a_vp node. At the moment
887 * we are running without any specific locking at all, but beware
888 * to any programmer that care must be taken if locking is added
889 * to this function.
891 * union_getattr(struct vnode *a_vp, struct vattr *a_vap,
892 * struct ucred *a_cred, struct thread *a_td)
894 static int
895 union_getattr(struct vop_getattr_args *ap)
897 int error;
898 struct union_node *un = VTOUNION(ap->a_vp);
899 struct vnode *vp;
900 struct vattr *vap;
901 struct vattr va;
904 * Some programs walk the filesystem hierarchy by counting
905 * links to directories to avoid stat'ing all the time.
906 * This means the link count on directories needs to be "correct".
907 * The only way to do that is to call getattr on both layers
908 * and fix up the link count. The link count will not necessarily
909 * be accurate but will be large enough to defeat the tree walkers.
912 vap = ap->a_vap;
914 if ((vp = un->un_uppervp) != NULLVP) {
915 error = VOP_GETATTR(vp, vap);
916 if (error)
917 return (error);
918 /* XXX isn't this dangerouso without a lock? */
919 union_newsize(ap->a_vp, vap->va_size, VNOVAL);
922 if (vp == NULLVP) {
923 vp = un->un_lowervp;
924 } else if (vp->v_type == VDIR && un->un_lowervp != NULLVP) {
925 vp = un->un_lowervp;
926 vap = &va;
927 } else {
928 vp = NULLVP;
931 if (vp != NULLVP) {
932 error = VOP_GETATTR(vp, vap);
933 if (error)
934 return (error);
935 /* XXX isn't this dangerous without a lock? */
936 union_newsize(ap->a_vp, VNOVAL, vap->va_size);
939 if ((vap != ap->a_vap) && (vap->va_type == VDIR))
940 ap->a_vap->va_nlink += vap->va_nlink;
941 return (0);
945 * union_setattr(struct vnode *a_vp, struct vattr *a_vap,
946 * struct ucred *a_cred, struct thread *a_td)
948 static int
949 union_setattr(struct vop_setattr_args *ap)
951 struct union_node *un = VTOUNION(ap->a_vp);
952 struct thread *td = ap->a_td;
953 struct vattr *vap = ap->a_vap;
954 struct vnode *uppervp;
955 int error;
958 * Disallow write attempts on filesystems mounted read-only.
960 if ((ap->a_vp->v_mount->mnt_flag & MNT_RDONLY) &&
961 (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
962 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
963 vap->va_mtime.tv_sec != VNOVAL ||
964 vap->va_mode != (mode_t)VNOVAL)) {
965 return (EROFS);
969 * Handle case of truncating lower object to zero size,
970 * by creating a zero length upper object. This is to
971 * handle the case of open with O_TRUNC and O_CREAT.
973 if (un->un_uppervp == NULLVP && (un->un_lowervp->v_type == VREG)) {
974 error = union_copyup(un, (ap->a_vap->va_size != 0),
975 ap->a_cred, ap->a_td);
976 if (error)
977 return (error);
981 * Try to set attributes in upper layer,
982 * otherwise return read-only filesystem error.
984 error = EROFS;
985 if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
986 error = VOP_SETATTR(un->un_uppervp, ap->a_vap, ap->a_cred);
987 if ((error == 0) && (ap->a_vap->va_size != VNOVAL))
988 union_newsize(ap->a_vp, ap->a_vap->va_size, VNOVAL);
989 union_unlock_upper(uppervp, td);
991 return (error);
995 * union_getpages:
998 static int
999 union_getpages(struct vop_getpages_args *ap)
1001 int r;
1003 r = vnode_pager_generic_getpages(ap->a_vp, ap->a_m,
1004 ap->a_count, ap->a_reqpage,
1005 ap->a_seqaccess);
1006 return(r);
1010 * union_putpages:
1013 static int
1014 union_putpages(struct vop_putpages_args *ap)
1016 int r;
1018 r = vnode_pager_generic_putpages(ap->a_vp, ap->a_m, ap->a_count,
1019 ap->a_sync, ap->a_rtvals);
1020 return(r);
1024 * union_read(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1025 * struct ucred *a_cred)
1027 static int
1028 union_read(struct vop_read_args *ap)
1030 struct union_node *un = VTOUNION(ap->a_vp);
1031 struct thread *td = ap->a_uio->uio_td;
1032 struct vnode *uvp;
1033 int error;
1035 uvp = union_lock_other(un, td);
1036 KASSERT(uvp != NULL, ("union_read: backing vnode missing!"));
1038 if (ap->a_vp->v_flag & VOBJBUF)
1039 union_vm_coherency(ap->a_vp, ap->a_uio, 0);
1041 error = VOP_READ(uvp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1042 union_unlock_other(uvp, td);
1045 * XXX
1046 * perhaps the size of the underlying object has changed under
1047 * our feet. take advantage of the offset information present
1048 * in the uio structure.
1050 if (error == 0) {
1051 struct union_node *un = VTOUNION(ap->a_vp);
1052 off_t cur = ap->a_uio->uio_offset;
1054 if (uvp == un->un_uppervp) {
1055 if (cur > un->un_uppersz)
1056 union_newsize(ap->a_vp, cur, VNOVAL);
1057 } else {
1058 if (cur > un->un_lowersz)
1059 union_newsize(ap->a_vp, VNOVAL, cur);
1062 return (error);
1066 * union_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1067 * struct ucred *a_cred)
1069 static int
1070 union_write(struct vop_read_args *ap)
1072 struct union_node *un = VTOUNION(ap->a_vp);
1073 struct thread *td = ap->a_uio->uio_td;
1074 struct vnode *uppervp;
1075 int error;
1077 if ((uppervp = union_lock_upper(un, td)) == NULLVP)
1078 panic("union: missing upper layer in write");
1081 * Since our VM pages are associated with our vnode rather then
1082 * the real vnode, and since we do not run our reads and writes
1083 * through our own VM cache, we have a VM/VFS coherency problem.
1084 * We solve them by invalidating or flushing the associated VM
1085 * pages prior to allowing a normal read or write to occur.
1087 * VM-backed writes (UIO_NOCOPY) have to be converted to normal
1088 * writes because we are not cache-coherent. Normal writes need
1089 * to be made coherent with our VM-backing store, which we do by
1090 * first flushing any dirty VM pages associated with the write
1091 * range, and then destroying any clean VM pages associated with
1092 * the write range.
1095 if (ap->a_uio->uio_segflg == UIO_NOCOPY) {
1096 ap->a_uio->uio_segflg = UIO_SYSSPACE;
1097 } else if (ap->a_vp->v_flag & VOBJBUF) {
1098 union_vm_coherency(ap->a_vp, ap->a_uio, 1);
1101 error = VOP_WRITE(uppervp, ap->a_uio, ap->a_ioflag, ap->a_cred);
1104 * the size of the underlying object may be changed by the
1105 * write.
1107 if (error == 0) {
1108 off_t cur = ap->a_uio->uio_offset;
1110 if (cur > un->un_uppersz)
1111 union_newsize(ap->a_vp, cur, VNOVAL);
1113 union_unlock_upper(uppervp, td);
1114 return (error);
1118 * union_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data, int a_fflag,
1119 * struct ucred *a_cred, struct thread *a_td)
1121 static int
1122 union_ioctl(struct vop_ioctl_args *ap)
1124 struct vnode *ovp = OTHERVP(ap->a_vp);
1126 ap->a_head.a_ops = *ovp->v_ops;
1127 ap->a_vp = ovp;
1128 return(vop_ioctl_ap(ap));
1132 * union_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred,
1133 * struct thread *a_td)
1135 static int
1136 union_poll(struct vop_poll_args *ap)
1138 struct vnode *ovp = OTHERVP(ap->a_vp);
1140 ap->a_head.a_ops = *ovp->v_ops;
1141 ap->a_vp = ovp;
1142 return(vop_poll_ap(ap));
1146 * union_mmap(struct vnode *a_vp, int a_fflags, struct ucred *a_cred,
1147 * struct thread *a_td)
1149 static int
1150 union_mmap(struct vop_mmap_args *ap)
1152 struct vnode *ovp = OTHERVP(ap->a_vp);
1154 ap->a_head.a_ops = *ovp->v_ops;
1155 ap->a_vp = ovp;
1156 return (vop_mmap_ap(ap));
1160 * union_fsync(struct vnode *a_vp, struct ucred *a_cred, int a_waitfor,
1161 * struct thread *a_td)
1163 static int
1164 union_fsync(struct vop_fsync_args *ap)
1166 int error = 0;
1167 struct thread *td = ap->a_td;
1168 struct vnode *targetvp;
1169 struct union_node *un = VTOUNION(ap->a_vp);
1171 if ((targetvp = union_lock_other(un, td)) != NULLVP) {
1172 error = VOP_FSYNC(targetvp, ap->a_waitfor, 0);
1173 union_unlock_other(targetvp, td);
1176 return (error);
1180 * union_remove:
1182 * Remove the specified cnp. The dvp and vp are passed to us locked
1183 * and must remain locked on return.
1185 * union_remove(struct vnode *a_dvp, struct vnode *a_vp,
1186 * struct componentname *a_cnp)
1188 static int
1189 union_remove(struct vop_old_remove_args *ap)
1191 struct union_node *dun = VTOUNION(ap->a_dvp);
1192 struct union_node *un = VTOUNION(ap->a_vp);
1193 struct componentname *cnp = ap->a_cnp;
1194 struct thread *td = cnp->cn_td;
1195 struct vnode *uppervp;
1196 struct vnode *upperdvp;
1197 int error;
1199 if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1200 panic("union remove: null upper vnode");
1202 if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1203 if (union_dowhiteout(un, cnp->cn_cred, td))
1204 cnp->cn_flags |= CNP_DOWHITEOUT;
1205 error = VOP_REMOVE(upperdvp, uppervp, cnp);
1206 #if 0
1207 /* XXX */
1208 if (!error)
1209 union_removed_upper(un);
1210 #endif
1211 union_unlock_upper(uppervp, td);
1212 } else {
1213 error = union_mkwhiteout(
1214 MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1215 upperdvp, ap->a_cnp, un->un_path);
1217 union_unlock_upper(upperdvp, td);
1218 return (error);
1222 * union_link:
1224 * tdvp will be locked on entry, vp will not be locked on entry.
1225 * tdvp should remain locked on return and vp should remain unlocked
1226 * on return.
1228 * union_link(struct vnode *a_tdvp, struct vnode *a_vp,
1229 * struct componentname *a_cnp)
1231 static int
1232 union_link(struct vop_old_link_args *ap)
1234 struct componentname *cnp = ap->a_cnp;
1235 struct thread *td = cnp->cn_td;
1236 struct union_node *dun = VTOUNION(ap->a_tdvp);
1237 struct vnode *vp;
1238 struct vnode *tdvp;
1239 int error = 0;
1241 if (ap->a_tdvp->v_ops != ap->a_vp->v_ops) {
1242 vp = ap->a_vp;
1243 } else {
1244 struct union_node *tun = VTOUNION(ap->a_vp);
1246 if (tun->un_uppervp == NULLVP) {
1247 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
1248 #if 0
1249 if (dun->un_uppervp == tun->un_dirvp) {
1250 if (dun->un_flags & UN_ULOCK) {
1251 dun->un_flags &= ~UN_ULOCK;
1252 vn_unlock(dun->un_uppervp);
1255 #endif
1256 error = union_copyup(tun, 1, cnp->cn_cred, td);
1257 #if 0
1258 if (dun->un_uppervp == tun->un_dirvp) {
1259 vn_lock(dun->un_uppervp,
1260 LK_EXCLUSIVE | LK_RETRY);
1261 dun->un_flags |= UN_ULOCK;
1263 #endif
1264 vn_unlock(ap->a_vp);
1266 vp = tun->un_uppervp;
1269 if (error)
1270 return (error);
1273 * Make sure upper is locked, then unlock the union directory we were
1274 * called with to avoid a deadlock while we are calling VOP_LINK on
1275 * the upper (with tdvp locked and vp not locked). Our ap->a_tdvp
1276 * is expected to be locked on return.
1279 if ((tdvp = union_lock_upper(dun, td)) == NULLVP)
1280 return (EROFS);
1282 vn_unlock(ap->a_tdvp); /* unlock calling node */
1283 error = VOP_LINK(tdvp, vp, cnp); /* call link on upper */
1286 * We have to unlock tdvp prior to relocking our calling node in
1287 * order to avoid a deadlock.
1289 union_unlock_upper(tdvp, td);
1290 vn_lock(ap->a_tdvp, LK_EXCLUSIVE | LK_RETRY);
1291 return (error);
1295 * union_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
1296 * struct componentname *a_fcnp, struct vnode *a_tdvp,
1297 * struct vnode *a_tvp, struct componentname *a_tcnp)
1299 static int
1300 union_rename(struct vop_old_rename_args *ap)
1302 int error;
1303 struct vnode *fdvp = ap->a_fdvp;
1304 struct vnode *fvp = ap->a_fvp;
1305 struct vnode *tdvp = ap->a_tdvp;
1306 struct vnode *tvp = ap->a_tvp;
1309 * Figure out what fdvp to pass to our upper or lower vnode. If we
1310 * replace the fdvp, release the original one and ref the new one.
1313 if (fdvp->v_tag == VT_UNION) { /* always true */
1314 struct union_node *un = VTOUNION(fdvp);
1315 if (un->un_uppervp == NULLVP) {
1317 * this should never happen in normal
1318 * operation but might if there was
1319 * a problem creating the top-level shadow
1320 * directory.
1322 error = EXDEV;
1323 goto bad;
1325 fdvp = un->un_uppervp;
1326 vref(fdvp);
1327 vrele(ap->a_fdvp);
1331 * Figure out what fvp to pass to our upper or lower vnode. If we
1332 * replace the fvp, release the original one and ref the new one.
1335 if (fvp->v_tag == VT_UNION) { /* always true */
1336 struct union_node *un = VTOUNION(fvp);
1337 #if 0
1338 struct union_mount *um = MOUNTTOUNIONMOUNT(fvp->v_mount);
1339 #endif
1341 if (un->un_uppervp == NULLVP) {
1342 switch(fvp->v_type) {
1343 case VREG:
1344 vn_lock(un->un_vnode, LK_EXCLUSIVE | LK_RETRY);
1345 error = union_copyup(un, 1, ap->a_fcnp->cn_cred, ap->a_fcnp->cn_td);
1346 vn_unlock(un->un_vnode);
1347 if (error)
1348 goto bad;
1349 break;
1350 case VDIR:
1352 * XXX not yet.
1354 * There is only one way to rename a directory
1355 * based in the lowervp, and that is to copy
1356 * the entire directory hierarchy. Otherwise
1357 * it would not last across a reboot.
1359 #if 0
1360 vrele(fvp);
1361 fvp = NULL;
1362 vn_lock(fdvp, LK_EXCLUSIVE | LK_RETRY);
1363 error = union_mkshadow(um, fdvp,
1364 ap->a_fcnp, &un->un_uppervp);
1365 vn_unlock(fdvp);
1366 if (un->un_uppervp)
1367 vn_unlock(un->un_uppervp);
1368 if (error)
1369 goto bad;
1370 break;
1371 #endif
1372 default:
1373 error = EXDEV;
1374 goto bad;
1378 if (un->un_lowervp != NULLVP)
1379 ap->a_fcnp->cn_flags |= CNP_DOWHITEOUT;
1380 fvp = un->un_uppervp;
1381 vref(fvp);
1382 vrele(ap->a_fvp);
1386 * Figure out what tdvp (destination directory) to pass to the
1387 * lower level. If we replace it with uppervp, we need to vput the
1388 * old one. The exclusive lock is transfered to what we will pass
1389 * down in the VOP_RENAME and we replace uppervp with a simple
1390 * reference.
1393 if (tdvp->v_tag == VT_UNION) {
1394 struct union_node *un = VTOUNION(tdvp);
1396 if (un->un_uppervp == NULLVP) {
1398 * this should never happen in normal
1399 * operation but might if there was
1400 * a problem creating the top-level shadow
1401 * directory.
1403 error = EXDEV;
1404 goto bad;
1408 * new tdvp is a lock and reference on uppervp, put away
1409 * the old tdvp.
1411 tdvp = union_lock_upper(un, ap->a_tcnp->cn_td);
1412 vput(ap->a_tdvp);
1416 * Figure out what tvp (destination file) to pass to the
1417 * lower level.
1419 * If the uppervp file does not exist put away the (wrong)
1420 * file and change tvp to NULL.
1423 if (tvp != NULLVP && tvp->v_tag == VT_UNION) {
1424 struct union_node *un = VTOUNION(tvp);
1426 tvp = union_lock_upper(un, ap->a_tcnp->cn_td);
1427 vput(ap->a_tvp);
1428 /* note: tvp may be NULL */
1432 * VOP_RENAME releases/vputs prior to returning, so we have no
1433 * cleanup to do.
1436 return (VOP_RENAME(fdvp, fvp, ap->a_fcnp, tdvp, tvp, ap->a_tcnp));
1439 * Error. We still have to release / vput the various elements.
1442 bad:
1443 vrele(fdvp);
1444 if (fvp)
1445 vrele(fvp);
1446 vput(tdvp);
1447 if (tvp != NULLVP) {
1448 if (tvp != tdvp)
1449 vput(tvp);
1450 else
1451 vrele(tvp);
1453 return (error);
1457 * union_mkdir(struct vnode *a_dvp, struct vnode **a_vpp,
1458 * struct componentname *a_cnp, struct vattr *a_vap)
1460 static int
1461 union_mkdir(struct vop_old_mkdir_args *ap)
1463 struct union_node *dun = VTOUNION(ap->a_dvp);
1464 struct componentname *cnp = ap->a_cnp;
1465 struct thread *td = cnp->cn_td;
1466 struct vnode *upperdvp;
1467 int error = EROFS;
1469 if ((upperdvp = union_lock_upper(dun, td)) != NULLVP) {
1470 struct vnode *vp;
1472 error = VOP_MKDIR(upperdvp, &vp, cnp, ap->a_vap);
1473 union_unlock_upper(upperdvp, td);
1475 if (error == 0) {
1476 vn_unlock(vp);
1477 UDEBUG(("ALLOCVP-2 FROM %p REFS %d\n", vp, vp->v_sysref.refcnt));
1478 error = union_allocvp(ap->a_vpp, ap->a_dvp->v_mount,
1479 ap->a_dvp, NULLVP, cnp, vp, NULLVP, 1);
1480 UDEBUG(("ALLOCVP-2B FROM %p REFS %d\n", *ap->a_vpp, vp->v_sysref.refcnt));
1483 return (error);
1487 * union_rmdir(struct vnode *a_dvp, struct vnode *a_vp,
1488 * struct componentname *a_cnp)
1490 static int
1491 union_rmdir(struct vop_old_rmdir_args *ap)
1493 struct union_node *dun = VTOUNION(ap->a_dvp);
1494 struct union_node *un = VTOUNION(ap->a_vp);
1495 struct componentname *cnp = ap->a_cnp;
1496 struct thread *td = cnp->cn_td;
1497 struct vnode *upperdvp;
1498 struct vnode *uppervp;
1499 int error;
1501 if ((upperdvp = union_lock_upper(dun, td)) == NULLVP)
1502 panic("union rmdir: null upper vnode");
1504 if ((uppervp = union_lock_upper(un, td)) != NULLVP) {
1505 if (union_dowhiteout(un, cnp->cn_cred, td))
1506 cnp->cn_flags |= CNP_DOWHITEOUT;
1507 error = VOP_RMDIR(upperdvp, uppervp, ap->a_cnp);
1508 union_unlock_upper(uppervp, td);
1509 } else {
1510 error = union_mkwhiteout(
1511 MOUNTTOUNIONMOUNT(ap->a_dvp->v_mount),
1512 dun->un_uppervp, ap->a_cnp, un->un_path);
1514 union_unlock_upper(upperdvp, td);
1515 return (error);
1519 * union_symlink:
1521 * dvp is locked on entry and remains locked on return. a_vpp is garbage
1522 * (unused).
1524 * union_symlink(struct vnode *a_dvp, struct vnode **a_vpp,
1525 * struct componentname *a_cnp, struct vattr *a_vap,
1526 * char *a_target)
1528 static int
1529 union_symlink(struct vop_old_symlink_args *ap)
1531 struct union_node *dun = VTOUNION(ap->a_dvp);
1532 struct componentname *cnp = ap->a_cnp;
1533 struct thread *td = cnp->cn_td;
1534 struct vnode *dvp;
1535 int error = EROFS;
1537 if ((dvp = union_lock_upper(dun, td)) != NULLVP) {
1538 error = VOP_SYMLINK(dvp, ap->a_vpp, cnp, ap->a_vap,
1539 ap->a_target);
1540 union_unlock_upper(dvp, td);
1542 return (error);
1546 * union_readdir works in concert with getdirentries and
1547 * readdir(3) to provide a list of entries in the unioned
1548 * directories. getdirentries is responsible for walking
1549 * down the union stack. readdir(3) is responsible for
1550 * eliminating duplicate names from the returned data stream.
1552 * union_readdir(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred,
1553 * int *a_eofflag, off_t *a_cookies, int a_ncookies)
1555 static int
1556 union_readdir(struct vop_readdir_args *ap)
1558 struct union_node *un = VTOUNION(ap->a_vp);
1559 struct thread *td = ap->a_uio->uio_td;
1560 struct vnode *uvp;
1561 int error = 0;
1563 if ((uvp = union_ref_upper(un)) != NULLVP) {
1564 ap->a_head.a_ops = *uvp->v_ops;
1565 ap->a_vp = uvp;
1566 error = vop_readdir_ap(ap);
1567 vrele(uvp);
1569 return(error);
1573 * union_readlink(struct vnode *a_vp, struct uio *a_uio, struct ucred *a_cred)
1575 static int
1576 union_readlink(struct vop_readlink_args *ap)
1578 int error;
1579 struct union_node *un = VTOUNION(ap->a_vp);
1580 struct uio *uio = ap->a_uio;
1581 struct thread *td = uio->uio_td;
1582 struct vnode *vp;
1584 vp = union_lock_other(un, td);
1585 KASSERT(vp != NULL, ("union_readlink: backing vnode missing!"));
1587 ap->a_head.a_ops = *vp->v_ops;
1588 ap->a_vp = vp;
1589 error = vop_readlink_ap(ap);
1590 union_unlock_other(vp, td);
1592 return (error);
1596 * union_inactive:
1598 * Called with the vnode locked. We are expected to unlock the vnode.
1600 * union_inactive(struct vnode *a_vp, struct thread *a_td)
1602 static int
1603 union_inactive(struct vop_inactive_args *ap)
1605 struct vnode *vp = ap->a_vp;
1606 /*struct thread *td = ap->a_td;*/
1607 struct union_node *un = VTOUNION(vp);
1608 struct vnode **vpp;
1611 * Do nothing (and _don't_ bypass).
1612 * Wait to vrele lowervp until reclaim,
1613 * so that until then our union_node is in the
1614 * cache and reusable.
1616 * NEEDSWORK: Someday, consider inactive'ing
1617 * the lowervp and then trying to reactivate it
1618 * with capabilities (v_id)
1619 * like they do in the name lookup cache code.
1620 * That's too much work for now.
1623 if (un->un_dircache != 0) {
1624 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1625 vrele(*vpp);
1626 kfree (un->un_dircache, M_TEMP);
1627 un->un_dircache = 0;
1630 #if 0
1631 if ((un->un_flags & UN_ULOCK) && un->un_uppervp) {
1632 un->un_flags &= ~UN_ULOCK;
1633 vn_unlock(un->un_uppervp);
1635 #endif
1637 if ((un->un_flags & UN_CACHED) == 0)
1638 vgone_vxlocked(vp);
1640 return (0);
1644 * union_reclaim(struct vnode *a_vp)
1646 static int
1647 union_reclaim(struct vop_reclaim_args *ap)
1649 union_freevp(ap->a_vp);
1651 return (0);
1655 * union_bmap:
1657 * There isn't much we can do. We cannot push through to the real vnode
1658 * to get to the underlying device because this will bypass data
1659 * cached by the real vnode.
1661 * For some reason we cannot return the 'real' vnode either, it seems
1662 * to blow up memory maps.
1664 * union_bmap(struct vnode *a_vp, off_t a_loffset,
1665 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1667 static int
1668 union_bmap(struct vop_bmap_args *ap)
1670 return(EOPNOTSUPP);
1674 * union_print(struct vnode *a_vp)
1676 static int
1677 union_print(struct vop_print_args *ap)
1679 struct vnode *vp = ap->a_vp;
1681 kprintf("\ttag VT_UNION, vp=%p, uppervp=%p, lowervp=%p\n",
1682 vp, UPPERVP(vp), LOWERVP(vp));
1683 if (UPPERVP(vp) != NULLVP)
1684 vprint("union: upper", UPPERVP(vp));
1685 if (LOWERVP(vp) != NULLVP)
1686 vprint("union: lower", LOWERVP(vp));
1688 return (0);
1692 * union_pathconf(struct vnode *a_vp, int a_name, int *a_retval)
1694 static int
1695 union_pathconf(struct vop_pathconf_args *ap)
1697 int error;
1698 struct thread *td = curthread; /* XXX */
1699 struct union_node *un = VTOUNION(ap->a_vp);
1700 struct vnode *vp;
1702 vp = union_lock_other(un, td);
1703 KASSERT(vp != NULL, ("union_pathconf: backing vnode missing!"));
1705 ap->a_head.a_ops = *vp->v_ops;
1706 ap->a_vp = vp;
1707 error = vop_pathconf_ap(ap);
1708 union_unlock_other(vp, td);
1710 return (error);
1714 * union_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1715 * struct flock *a_fl, int a_flags)
1717 static int
1718 union_advlock(struct vop_advlock_args *ap)
1720 struct vnode *ovp = OTHERVP(ap->a_vp);
1722 ap->a_head.a_ops = *ovp->v_ops;
1723 ap->a_vp = ovp;
1724 return (vop_advlock_ap(ap));
1729 * XXX - vop_strategy must be hand coded because it has no
1730 * YYY - and it is not coherent with anything
1732 * vnode in its arguments.
1733 * This goes away with a merged VM/buffer cache.
1735 * union_strategy(struct vnode *a_vp, struct bio *a_bio)
1737 static int
1738 union_strategy(struct vop_strategy_args *ap)
1740 struct bio *bio = ap->a_bio;
1741 struct buf *bp = bio->bio_buf;
1742 struct vnode *othervp = OTHERVP(ap->a_vp);
1744 #ifdef DIAGNOSTIC
1745 if (othervp == NULLVP)
1746 panic("union_strategy: nil vp");
1747 if (bp->b_cmd != BUF_CMD_READ && (othervp == LOWERVP(ap->a_vp)))
1748 panic("union_strategy: writing to lowervp");
1749 #endif
1750 return (vn_strategy(othervp, bio));
1754 * Global vfs data structures
1756 struct vop_ops union_vnode_vops = {
1757 .vop_default = vop_defaultop,
1758 .vop_access = union_access,
1759 .vop_advlock = union_advlock,
1760 .vop_bmap = union_bmap,
1761 .vop_close = union_close,
1762 .vop_old_create = union_create,
1763 .vop_fsync = union_fsync,
1764 .vop_getpages = union_getpages,
1765 .vop_putpages = union_putpages,
1766 .vop_getattr = union_getattr,
1767 .vop_inactive = union_inactive,
1768 .vop_ioctl = union_ioctl,
1769 .vop_old_link = union_link,
1770 .vop_old_lookup = union_lookup,
1771 .vop_old_mkdir = union_mkdir,
1772 .vop_old_mknod = union_mknod,
1773 .vop_mmap = union_mmap,
1774 .vop_open = union_open,
1775 .vop_pathconf = union_pathconf,
1776 .vop_poll = union_poll,
1777 .vop_print = union_print,
1778 .vop_read = union_read,
1779 .vop_readdir = union_readdir,
1780 .vop_readlink = union_readlink,
1781 .vop_reclaim = union_reclaim,
1782 .vop_old_remove = union_remove,
1783 .vop_old_rename = union_rename,
1784 .vop_old_rmdir = union_rmdir,
1785 .vop_setattr = union_setattr,
1786 .vop_strategy = union_strategy,
1787 .vop_old_symlink = union_symlink,
1788 .vop_old_whiteout = union_whiteout,
1789 .vop_write = union_write