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