add caching flag
[unix-history] / usr / src / sys / miscfs / union / union_subr.c
CommitLineData
b991bc2d
JSP
1/*
2 * Copyright (c) 1994 Jan-Simon Pendry
3 * Copyright (c) 1994
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Jan-Simon Pendry.
8 *
9 * %sccs.include.redist.c%
10 *
c93d29a8 11 * @(#)union_subr.c 8.5 (Berkeley) %G%
b991bc2d
JSP
12 */
13
14#include <sys/param.h>
15#include <sys/systm.h>
16#include <sys/time.h>
17#include <sys/kernel.h>
18#include <sys/vnode.h>
19#include <sys/namei.h>
20#include <sys/malloc.h>
81be6ee0 21#include <sys/file.h>
3b293975 22#include <sys/filedesc.h>
58572fc0 23#include <sys/queue.h>
c93d29a8 24#include <sys/mount.h>
2e1ae99e 25#include <miscfs/union/union.h>
b991bc2d 26
79f80c71
JSP
27#ifdef DIAGNOSTIC
28#include <sys/proc.h>
29#endif
30
58572fc0
JSP
31/* must be power of two, otherwise change UNION_HASH() */
32#define NHASH 32
33
34/* unsigned int ... */
35#define UNION_HASH(u, l) \
36 (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
37
38static LIST_HEAD(unhead, union_node) unhead[NHASH];
39static int unvplock[NHASH];
b991bc2d
JSP
40
41int
42union_init()
43{
58572fc0
JSP
44 int i;
45
46 for (i = 0; i < NHASH; i++)
47 LIST_INIT(&unhead[i]);
48 bzero((caddr_t) unvplock, sizeof(unvplock));
49}
50
51static int
52union_list_lock(ix)
53 int ix;
54{
55
56 if (unvplock[ix] & UN_LOCKED) {
57 unvplock[ix] |= UN_WANT;
58 sleep((caddr_t) &unvplock[ix], PINOD);
59 return (1);
60 }
b991bc2d 61
58572fc0
JSP
62 unvplock[ix] |= UN_LOCKED;
63
64 return (0);
b991bc2d
JSP
65}
66
e2a2f3a7 67static void
58572fc0
JSP
68union_list_unlock(ix)
69 int ix;
70{
71
72 unvplock[ix] &= ~UN_LOCKED;
73
74 if (unvplock[ix] & UN_WANT) {
75 unvplock[ix] &= ~UN_WANT;
76 wakeup((caddr_t) &unvplock[ix]);
77 }
78}
79
80void
81union_updatevp(un, uppervp, lowervp)
e2a2f3a7 82 struct union_node *un;
58572fc0
JSP
83 struct vnode *uppervp;
84 struct vnode *lowervp;
e2a2f3a7 85{
58572fc0
JSP
86 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
87 int nhash = UNION_HASH(uppervp, lowervp);
e2a2f3a7 88
58572fc0
JSP
89 if (ohash != nhash) {
90 /*
91 * Ensure locking is ordered from lower to higher
92 * to avoid deadlocks.
93 */
94 if (nhash < ohash) {
95 int t = ohash;
96 ohash = nhash;
97 nhash = t;
98 }
99
100 while (union_list_lock(ohash))
101 continue;
102
103 while (union_list_lock(nhash))
104 continue;
105
106 LIST_REMOVE(un, un_cache);
107 union_list_unlock(ohash);
108 } else {
109 while (union_list_lock(nhash))
110 continue;
111 }
112
113 if (un->un_lowervp != lowervp) {
114 if (un->un_lowervp) {
115 vrele(un->un_lowervp);
116 if (un->un_path) {
117 free(un->un_path, M_TEMP);
118 un->un_path = 0;
119 }
120 if (un->un_dirvp) {
121 vrele(un->un_dirvp);
122 un->un_dirvp = NULLVP;
123 }
e2a2f3a7 124 }
58572fc0
JSP
125 un->un_lowervp = lowervp;
126 }
127
128 if (un->un_uppervp != uppervp) {
129 if (un->un_uppervp)
130 vrele(un->un_uppervp);
131
132 un->un_uppervp = uppervp;
e2a2f3a7 133 }
58572fc0
JSP
134
135 if (ohash != nhash)
136 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
137
138 union_list_unlock(nhash);
139}
140
141void
142union_newlower(un, lowervp)
143 struct union_node *un;
144 struct vnode *lowervp;
145{
146
147 union_updatevp(un, un->un_uppervp, lowervp);
148}
149
150void
151union_newupper(un, uppervp)
152 struct union_node *un;
153 struct vnode *uppervp;
154{
155
156 union_updatevp(un, uppervp, un->un_lowervp);
e2a2f3a7
JSP
157}
158
b991bc2d
JSP
159/*
160 * allocate a union_node/vnode pair. the vnode is
01dac67e
JSP
161 * referenced and locked. the new vnode is returned
162 * via (vpp). (mp) is the mountpoint of the union filesystem,
163 * (dvp) is the parent directory where the upper layer object
164 * should exist (but doesn't) and (cnp) is the componentname
165 * information which is partially copied to allow the upper
166 * layer object to be created at a later time. (uppervp)
167 * and (lowervp) reference the upper and lower layer objects
168 * being mapped. either, but not both, can be nil.
e2a2f3a7 169 * if supplied, (uppervp) is locked.
3b293975
JSP
170 * the reference is either maintained in the new union_node
171 * object which is allocated, or they are vrele'd.
b991bc2d
JSP
172 *
173 * all union_nodes are maintained on a singly-linked
174 * list. new nodes are only allocated when they cannot
175 * be found on this list. entries on the list are
176 * removed when the vfs reclaim entry is called.
177 *
178 * a single lock is kept for the entire list. this is
179 * needed because the getnewvnode() function can block
180 * waiting for a vnode to become free, in which case there
181 * may be more than one process trying to get the same
182 * vnode. this lock is only taken if we are going to
183 * call getnewvnode, since the kernel itself is single-threaded.
184 *
185 * if an entry is found on the list, then call vget() to
186 * take a reference. this is done because there may be
187 * zero references to it and so it needs to removed from
188 * the vnode free list.
189 */
190int
79f80c71 191union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
b991bc2d
JSP
192 struct vnode **vpp;
193 struct mount *mp;
79f80c71 194 struct vnode *undvp;
b991bc2d
JSP
195 struct vnode *dvp; /* may be null */
196 struct componentname *cnp; /* may be null */
197 struct vnode *uppervp; /* may be null */
198 struct vnode *lowervp; /* may be null */
199{
200 int error;
201 struct union_node *un;
202 struct union_node **pp;
58572fc0 203 struct vnode *xlowervp = NULLVP;
c93d29a8 204 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
58572fc0 205 int hash;
c93d29a8 206 int vflag;
58572fc0 207 int try;
01dac67e 208
58572fc0 209 if (uppervp == NULLVP && lowervp == NULLVP)
01dac67e
JSP
210 panic("union: unidentifiable allocation");
211
212 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
213 xlowervp = lowervp;
58572fc0 214 lowervp = NULLVP;
01dac67e 215 }
b991bc2d 216
c93d29a8
JSP
217 /* detect the root vnode (and aliases) */
218 vflag = 0;
219 if ((uppervp == um->um_uppervp) &&
220 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
221 if (lowervp == NULLVP) {
222 lowervp = um->um_lowervp;
223 VREF(lowervp);
224 }
225 vflag = VROOT;
226 }
227
b991bc2d 228loop:
58572fc0
JSP
229 for (try = 0; try < 3; try++) {
230 switch (try) {
231 case 0:
232 if (lowervp == NULLVP)
233 continue;
234 hash = UNION_HASH(uppervp, lowervp);
e2a2f3a7 235 break;
58572fc0
JSP
236
237 case 1:
238 if (uppervp == NULLVP)
239 continue;
240 hash = UNION_HASH(uppervp, NULLVP);
241 break;
242
243 case 2:
244 if (lowervp == NULLVP)
245 continue;
246 hash = UNION_HASH(NULLVP, lowervp);
247 break;
248 }
249
250 while (union_list_lock(hash))
251 continue;
252
253 for (un = unhead[hash].lh_first; un != 0;
254 un = un->un_cache.le_next) {
255 if ((un->un_lowervp == lowervp ||
256 un->un_lowervp == NULLVP) &&
257 (un->un_uppervp == uppervp ||
258 un->un_uppervp == NULLVP) &&
259 (UNIONTOV(un)->v_mount == mp)) {
260 if (vget(UNIONTOV(un), 0)) {
261 union_list_unlock(hash);
262 goto loop;
263 }
264 break;
265 }
e2a2f3a7 266 }
58572fc0
JSP
267
268 union_list_unlock(hash);
269
270 if (un)
271 break;
e2a2f3a7 272 }
ed5969c8 273
e2a2f3a7
JSP
274 if (un) {
275 /*
276 * Obtain a lock on the union_node.
277 * uppervp is locked, though un->un_uppervp
278 * may not be. this doesn't break the locking
279 * hierarchy since in the case that un->un_uppervp
280 * is not yet locked it will be vrele'd and replaced
281 * with uppervp.
282 */
283
284 if ((dvp != NULLVP) && (uppervp == dvp)) {
ed5969c8 285 /*
e2a2f3a7
JSP
286 * Access ``.'', so (un) will already
287 * be locked. Since this process has
288 * the lock on (uppervp) no other
289 * process can hold the lock on (un).
ed5969c8 290 */
e2a2f3a7
JSP
291#ifdef DIAGNOSTIC
292 if ((un->un_flags & UN_LOCKED) == 0)
293 panic("union: . not locked");
294 else if (curproc && un->un_pid != curproc->p_pid &&
295 un->un_pid > -1 && curproc->p_pid > -1)
296 panic("union: allocvp not lock owner");
297#endif
298 } else {
299 if (un->un_flags & UN_LOCKED) {
300 vrele(UNIONTOV(un));
301 un->un_flags |= UN_WANT;
302 sleep((caddr_t) &un->un_flags, PINOD);
303 goto loop;
79f80c71 304 }
e2a2f3a7 305 un->un_flags |= UN_LOCKED;
ed5969c8 306
e2a2f3a7
JSP
307#ifdef DIAGNOSTIC
308 if (curproc)
309 un->un_pid = curproc->p_pid;
310 else
311 un->un_pid = -1;
312#endif
313 }
314
315 /*
316 * At this point, the union_node is locked,
317 * un->un_uppervp may not be locked, and uppervp
318 * is locked or nil.
319 */
320
321 /*
322 * Save information about the upper layer.
323 */
324 if (uppervp != un->un_uppervp) {
58572fc0 325 union_newupper(un, uppervp);
e2a2f3a7
JSP
326 } else if (uppervp) {
327 vrele(uppervp);
328 }
329
330 if (un->un_uppervp) {
331 un->un_flags |= UN_ULOCK;
332 un->un_flags &= ~UN_KLOCK;
333 }
334
335 /*
336 * Save information about the lower layer.
337 * This needs to keep track of pathname
338 * and directory information which union_vn_create
339 * might need.
340 */
341 if (lowervp != un->un_lowervp) {
58572fc0 342 union_newlower(un, lowervp);
e2a2f3a7
JSP
343 if (cnp && (lowervp != NULLVP) &&
344 (lowervp->v_type == VREG)) {
345 un->un_hash = cnp->cn_hash;
346 un->un_path = malloc(cnp->cn_namelen+1,
347 M_TEMP, M_WAITOK);
348 bcopy(cnp->cn_nameptr, un->un_path,
349 cnp->cn_namelen);
350 un->un_path[cnp->cn_namelen] = '\0';
351 VREF(dvp);
352 un->un_dirvp = dvp;
79f80c71 353 }
e2a2f3a7
JSP
354 } else if (lowervp) {
355 vrele(lowervp);
b991bc2d 356 }
e2a2f3a7
JSP
357 *vpp = UNIONTOV(un);
358 return (0);
b991bc2d
JSP
359 }
360
361 /*
362 * otherwise lock the vp list while we call getnewvnode
363 * since that can block.
364 */
58572fc0
JSP
365 hash = UNION_HASH(uppervp, lowervp);
366
367 if (union_list_lock(hash))
b991bc2d 368 goto loop;
b991bc2d
JSP
369
370 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
e2a2f3a7
JSP
371 if (error) {
372 if (uppervp) {
373 if (dvp == uppervp)
374 vrele(uppervp);
375 else
376 vput(uppervp);
377 }
378 if (lowervp)
379 vrele(lowervp);
380
b991bc2d 381 goto out;
e2a2f3a7 382 }
b991bc2d
JSP
383
384 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
385 M_TEMP, M_WAITOK);
386
c93d29a8 387 (*vpp)->v_flag |= vflag;
01dac67e
JSP
388 if (uppervp)
389 (*vpp)->v_type = uppervp->v_type;
390 else
391 (*vpp)->v_type = lowervp->v_type;
b991bc2d 392 un = VTOUNION(*vpp);
79f80c71 393 un->un_vnode = *vpp;
b991bc2d
JSP
394 un->un_uppervp = uppervp;
395 un->un_lowervp = lowervp;
15a86f7e 396 un->un_openl = 0;
e2a2f3a7
JSP
397 un->un_flags = UN_LOCKED;
398 if (un->un_uppervp)
399 un->un_flags |= UN_ULOCK;
400#ifdef DIAGNOSTIC
401 if (curproc)
402 un->un_pid = curproc->p_pid;
403 else
404 un->un_pid = -1;
405#endif
ed5969c8
JSP
406 if (cnp && (lowervp != NULLVP) && (lowervp->v_type == VREG)) {
407 un->un_hash = cnp->cn_hash;
b991bc2d
JSP
408 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
409 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
410 un->un_path[cnp->cn_namelen] = '\0';
01dac67e
JSP
411 VREF(dvp);
412 un->un_dirvp = dvp;
b991bc2d 413 } else {
ed5969c8 414 un->un_hash = 0;
b991bc2d 415 un->un_path = 0;
01dac67e 416 un->un_dirvp = 0;
b991bc2d
JSP
417 }
418
58572fc0 419 LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
b991bc2d 420
01dac67e
JSP
421 if (xlowervp)
422 vrele(xlowervp);
423
b991bc2d 424out:
58572fc0 425 union_list_unlock(hash);
b991bc2d 426
b991bc2d
JSP
427 return (error);
428}
429
430int
431union_freevp(vp)
432 struct vnode *vp;
433{
b991bc2d
JSP
434 struct union_node *un = VTOUNION(vp);
435
58572fc0
JSP
436 LIST_REMOVE(un, un_cache);
437
438 if (un->un_uppervp)
439 vrele(un->un_uppervp);
440 if (un->un_lowervp)
441 vrele(un->un_lowervp);
442 if (un->un_dirvp)
443 vrele(un->un_dirvp);
444 if (un->un_path)
445 free(un->un_path, M_TEMP);
b991bc2d 446
b991bc2d
JSP
447 FREE(vp->v_data, M_TEMP);
448 vp->v_data = 0;
58572fc0 449
b991bc2d
JSP
450 return (0);
451}
81be6ee0
JSP
452
453/*
454 * copyfile. copy the vnode (fvp) to the vnode (tvp)
455 * using a sequence of reads and writes. both (fvp)
456 * and (tvp) are locked on entry and exit.
457 */
458int
459union_copyfile(p, cred, fvp, tvp)
460 struct proc *p;
461 struct ucred *cred;
462 struct vnode *fvp;
463 struct vnode *tvp;
464{
465 char *buf;
466 struct uio uio;
467 struct iovec iov;
468 int error = 0;
469
470 /*
471 * strategy:
472 * allocate a buffer of size MAXBSIZE.
473 * loop doing reads and writes, keeping track
474 * of the current uio offset.
475 * give up at the first sign of trouble.
476 */
477
478 uio.uio_procp = p;
479 uio.uio_segflg = UIO_SYSSPACE;
480 uio.uio_offset = 0;
481
482 VOP_UNLOCK(fvp); /* XXX */
483 LEASE_CHECK(fvp, p, cred, LEASE_READ);
484 VOP_LOCK(fvp); /* XXX */
485 VOP_UNLOCK(tvp); /* XXX */
486 LEASE_CHECK(tvp, p, cred, LEASE_WRITE);
487 VOP_LOCK(tvp); /* XXX */
488
489 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
490
491 /* ugly loop follows... */
492 do {
493 off_t offset = uio.uio_offset;
494
495 uio.uio_iov = &iov;
496 uio.uio_iovcnt = 1;
497 iov.iov_base = buf;
498 iov.iov_len = MAXBSIZE;
499 uio.uio_resid = iov.iov_len;
500 uio.uio_rw = UIO_READ;
501 error = VOP_READ(fvp, &uio, 0, cred);
502
503 if (error == 0) {
504 uio.uio_iov = &iov;
505 uio.uio_iovcnt = 1;
506 iov.iov_base = buf;
507 iov.iov_len = MAXBSIZE - uio.uio_resid;
508 uio.uio_offset = offset;
509 uio.uio_rw = UIO_WRITE;
510 uio.uio_resid = iov.iov_len;
511
512 if (uio.uio_resid == 0)
513 break;
514
515 do {
516 error = VOP_WRITE(tvp, &uio, 0, cred);
517 } while ((uio.uio_resid > 0) && (error == 0));
518 }
519
520 } while (error == 0);
521
522 free(buf, M_TEMP);
523 return (error);
524}
525
3b293975
JSP
526/*
527 * Create a shadow directory in the upper layer.
528 * The new vnode is returned locked.
529 *
530 * (um) points to the union mount structure for access to the
531 * the mounting process's credentials.
532 * (dvp) is the directory in which to create the shadow directory.
533 * it is unlocked on entry and exit.
534 * (cnp) is the componentname to be created.
535 * (vpp) is the returned newly created shadow directory, which
536 * is returned locked.
537 */
538int
539union_mkshadow(um, dvp, cnp, vpp)
540 struct union_mount *um;
541 struct vnode *dvp;
542 struct componentname *cnp;
543 struct vnode **vpp;
544{
545 int error;
546 struct vattr va;
547 struct proc *p = cnp->cn_proc;
548 struct componentname cn;
549
550 /*
551 * policy: when creating the shadow directory in the
692a90df
JSP
552 * upper layer, create it owned by the user who did
553 * the mount, group from parent directory, and mode
554 * 777 modified by umask (ie mostly identical to the
555 * mkdir syscall). (jsp, kb)
3b293975
JSP
556 */
557
558 /*
559 * A new componentname structure must be faked up because
560 * there is no way to know where the upper level cnp came
561 * from or what it is being used for. This must duplicate
562 * some of the work done by NDINIT, some of the work done
563 * by namei, some of the work done by lookup and some of
564 * the work done by VOP_LOOKUP when given a CREATE flag.
565 * Conclusion: Horrible.
566 *
567 * The pathname buffer will be FREEed by VOP_MKDIR.
568 */
569 cn.cn_pnbuf = malloc(cnp->cn_namelen+1, M_NAMEI, M_WAITOK);
ed5969c8
JSP
570 bcopy(cnp->cn_nameptr, cn.cn_pnbuf, cnp->cn_namelen);
571 cn.cn_pnbuf[cnp->cn_namelen] = '\0';
3b293975
JSP
572
573 cn.cn_nameiop = CREATE;
bf006388 574 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
3b293975 575 cn.cn_proc = cnp->cn_proc;
c1b204a8
JSP
576 if (um->um_op == UNMNT_ABOVE)
577 cn.cn_cred = cnp->cn_cred;
578 else
579 cn.cn_cred = um->um_cred;
3b293975
JSP
580 cn.cn_nameptr = cn.cn_pnbuf;
581 cn.cn_namelen = cnp->cn_namelen;
582 cn.cn_hash = cnp->cn_hash;
583 cn.cn_consume = cnp->cn_consume;
584
ed5969c8 585 VREF(dvp);
3b293975
JSP
586 if (error = relookup(dvp, vpp, &cn))
587 return (error);
ed5969c8 588 vrele(dvp);
3b293975
JSP
589
590 if (*vpp) {
591 VOP_ABORTOP(dvp, &cn);
592 VOP_UNLOCK(dvp);
593 vrele(*vpp);
594 *vpp = NULLVP;
595 return (EEXIST);
596 }
597
598 VATTR_NULL(&va);
599 va.va_type = VDIR;
692a90df 600 va.va_mode = um->um_cmode;
3b293975
JSP
601
602 /* LEASE_CHECK: dvp is locked */
603 LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
604
3b293975
JSP
605 error = VOP_MKDIR(dvp, vpp, &cn, &va);
606 return (error);
607}
608
81be6ee0
JSP
609/*
610 * union_vn_create: creates and opens a new shadow file
611 * on the upper union layer. this function is similar
612 * in spirit to calling vn_open but it avoids calling namei().
613 * the problem with calling namei is that a) it locks too many
614 * things, and b) it doesn't start at the "right" directory,
615 * whereas relookup is told where to start.
616 */
617int
3b293975 618union_vn_create(vpp, un, p)
81be6ee0
JSP
619 struct vnode **vpp;
620 struct union_node *un;
81be6ee0
JSP
621 struct proc *p;
622{
623 struct vnode *vp;
624 struct ucred *cred = p->p_ucred;
625 struct vattr vat;
626 struct vattr *vap = &vat;
627 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
628 int error;
ed5969c8 629 int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
81be6ee0
JSP
630 char *cp;
631 struct componentname cn;
632
633 *vpp = NULLVP;
634
692a90df
JSP
635 /*
636 * Build a new componentname structure (for the same
637 * reasons outlines in union_mkshadow).
638 * The difference here is that the file is owned by
639 * the current user, rather than by the person who
640 * did the mount, since the current user needs to be
641 * able to write the file (that's why it is being
642 * copied in the first place).
643 */
81be6ee0
JSP
644 cn.cn_namelen = strlen(un->un_path);
645 cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
646 bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
647 cn.cn_nameiop = CREATE;
bf006388 648 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
81be6ee0
JSP
649 cn.cn_proc = p;
650 cn.cn_cred = p->p_ucred;
651 cn.cn_nameptr = cn.cn_pnbuf;
ed5969c8 652 cn.cn_hash = un->un_hash;
81be6ee0
JSP
653 cn.cn_consume = 0;
654
ed5969c8 655 VREF(un->un_dirvp);
81be6ee0
JSP
656 if (error = relookup(un->un_dirvp, &vp, &cn))
657 return (error);
ed5969c8
JSP
658 vrele(un->un_dirvp);
659
bf006388 660 if (vp) {
81be6ee0
JSP
661 VOP_ABORTOP(un->un_dirvp, &cn);
662 if (un->un_dirvp == vp)
663 vrele(un->un_dirvp);
664 else
bf006388
JSP
665 vput(un->un_dirvp);
666 vrele(vp);
667 return (EEXIST);
81be6ee0
JSP
668 }
669
bf006388
JSP
670 /*
671 * Good - there was no race to create the file
672 * so go ahead and create it. The permissions
673 * on the file will be 0666 modified by the
674 * current user's umask. Access to the file, while
675 * it is unioned, will require access to the top *and*
676 * bottom files. Access when not unioned will simply
677 * require access to the top-level file.
678 * TODO: confirm choice of access permissions.
679 */
81be6ee0 680 VATTR_NULL(vap);
bf006388
JSP
681 vap->va_type = VREG;
682 vap->va_mode = cmode;
683 LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE);
684 if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap))
685 return (error);
81be6ee0 686
bf006388
JSP
687 if (error = VOP_OPEN(vp, fmode, cred, p)) {
688 vput(vp);
689 return (error);
690 }
81be6ee0
JSP
691
692 vp->v_writecount++;
693 *vpp = vp;
694 return (0);
81be6ee0 695}
ed5969c8
JSP
696
697int
15a86f7e 698union_vn_close(vp, fmode, cred, p)
ed5969c8
JSP
699 struct vnode *vp;
700 int fmode;
15a86f7e
JSP
701 struct ucred *cred;
702 struct proc *p;
ed5969c8
JSP
703{
704 if (fmode & FWRITE)
705 --vp->v_writecount;
706 return (VOP_CLOSE(vp, fmode));
707}
708
709void
710union_removed_upper(un)
711 struct union_node *un;
712{
e2a2f3a7
JSP
713 if (un->un_flags & UN_ULOCK) {
714 un->un_flags &= ~UN_ULOCK;
58572fc0 715 VOP_UNLOCK(un->un_uppervp);
e2a2f3a7 716 }
58572fc0
JSP
717
718 union_newupper(un, NULLVP);
ed5969c8 719}
15a86f7e
JSP
720
721struct vnode *
722union_lowervp(vp)
723 struct vnode *vp;
724{
725 struct union_node *un = VTOUNION(vp);
726
727 if (un->un_lowervp && (vp->v_type == un->un_lowervp->v_type)) {
728 if (vget(un->un_lowervp, 0))
729 return (NULLVP);
730 }
731
732 return (un->un_lowervp);
733}