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