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