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