suppress whiteouts when reading a non-unioned directory.
[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 *
b84b63e6 11 * @(#)union_subr.c 8.13 (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>
8a9c17f6 25#include <vm/vm.h> /* for vnode_pager_setsize */
2e1ae99e 26#include <miscfs/union/union.h>
b991bc2d 27
79f80c71
JSP
28#ifdef DIAGNOSTIC
29#include <sys/proc.h>
30#endif
31
58572fc0
JSP
32/* must be power of two, otherwise change UNION_HASH() */
33#define NHASH 32
34
35/* unsigned int ... */
36#define UNION_HASH(u, l) \
37 (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
38
39static LIST_HEAD(unhead, union_node) unhead[NHASH];
40static int unvplock[NHASH];
b991bc2d
JSP
41
42int
43union_init()
44{
58572fc0
JSP
45 int i;
46
47 for (i = 0; i < NHASH; i++)
48 LIST_INIT(&unhead[i]);
49 bzero((caddr_t) unvplock, sizeof(unvplock));
50}
51
52static int
53union_list_lock(ix)
54 int ix;
55{
56
57 if (unvplock[ix] & UN_LOCKED) {
58 unvplock[ix] |= UN_WANT;
59 sleep((caddr_t) &unvplock[ix], PINOD);
60 return (1);
61 }
b991bc2d 62
58572fc0
JSP
63 unvplock[ix] |= UN_LOCKED;
64
65 return (0);
b991bc2d
JSP
66}
67
e2a2f3a7 68static void
58572fc0
JSP
69union_list_unlock(ix)
70 int ix;
71{
72
73 unvplock[ix] &= ~UN_LOCKED;
74
75 if (unvplock[ix] & UN_WANT) {
76 unvplock[ix] &= ~UN_WANT;
77 wakeup((caddr_t) &unvplock[ix]);
78 }
79}
80
81void
82union_updatevp(un, uppervp, lowervp)
e2a2f3a7 83 struct union_node *un;
58572fc0
JSP
84 struct vnode *uppervp;
85 struct vnode *lowervp;
e2a2f3a7 86{
58572fc0
JSP
87 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
88 int nhash = UNION_HASH(uppervp, lowervp);
b5b5f291 89 int docache = (lowervp != NULLVP || uppervp != NULLVP);
e2a2f3a7 90
b5b5f291
JSP
91 /*
92 * Ensure locking is ordered from lower to higher
93 * to avoid deadlocks.
94 */
95 if (nhash < ohash) {
96 int t = ohash;
97 ohash = nhash;
98 nhash = t;
99 }
58572fc0 100
b5b5f291 101 if (ohash != nhash)
58572fc0
JSP
102 while (union_list_lock(ohash))
103 continue;
104
b5b5f291
JSP
105 while (union_list_lock(nhash))
106 continue;
58572fc0 107
b5b5f291
JSP
108 if (ohash != nhash || !docache) {
109 if (un->un_flags & UN_CACHED) {
b5b5f291 110 un->un_flags &= ~UN_CACHED;
233b9c0e 111 LIST_REMOVE(un, un_cache);
b5b5f291 112 }
58572fc0
JSP
113 }
114
b5b5f291
JSP
115 if (ohash != nhash)
116 union_list_unlock(ohash);
117
58572fc0
JSP
118 if (un->un_lowervp != lowervp) {
119 if (un->un_lowervp) {
120 vrele(un->un_lowervp);
121 if (un->un_path) {
122 free(un->un_path, M_TEMP);
123 un->un_path = 0;
124 }
125 if (un->un_dirvp) {
126 vrele(un->un_dirvp);
127 un->un_dirvp = NULLVP;
128 }
e2a2f3a7 129 }
58572fc0 130 un->un_lowervp = lowervp;
8a9c17f6 131 un->un_lowersz = VNOVAL;
58572fc0
JSP
132 }
133
134 if (un->un_uppervp != uppervp) {
135 if (un->un_uppervp)
136 vrele(un->un_uppervp);
137
138 un->un_uppervp = uppervp;
8a9c17f6 139 un->un_uppersz = VNOVAL;
e2a2f3a7 140 }
58572fc0 141
b5b5f291 142 if (docache && (ohash != nhash)) {
58572fc0 143 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
b5b5f291
JSP
144 un->un_flags |= UN_CACHED;
145 }
58572fc0
JSP
146
147 union_list_unlock(nhash);
148}
149
150void
151union_newlower(un, lowervp)
152 struct union_node *un;
153 struct vnode *lowervp;
154{
155
156 union_updatevp(un, un->un_uppervp, lowervp);
157}
158
159void
160union_newupper(un, uppervp)
161 struct union_node *un;
162 struct vnode *uppervp;
163{
164
165 union_updatevp(un, uppervp, un->un_lowervp);
e2a2f3a7
JSP
166}
167
8a9c17f6
JSP
168/*
169 * Keep track of size changes in the underlying vnodes.
170 * If the size changes, then callback to the vm layer
171 * giving priority to the upper layer size.
172 */
173void
174union_newsize(vp, uppersz, lowersz)
175 struct vnode *vp;
176 off_t uppersz, lowersz;
177{
178 struct union_node *un;
179 off_t sz;
180
181 /* only interested in regular files */
182 if (vp->v_type != VREG)
183 return;
184
185 un = VTOUNION(vp);
186 sz = VNOVAL;
187
188 if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
189 un->un_uppersz = uppersz;
190 if (sz == VNOVAL)
191 sz = un->un_uppersz;
192 }
193
194 if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
195 un->un_lowersz = lowersz;
196 if (sz == VNOVAL)
197 sz = un->un_lowersz;
198 }
199
200 if (sz != VNOVAL) {
201#ifdef UNION_DIAGNOSTIC
202 printf("union: %s size now %ld\n",
203 uppersz != VNOVAL ? "upper" : "lower", (long) sz);
204#endif
205 vnode_pager_setsize(vp, sz);
206 }
207}
208
b991bc2d
JSP
209/*
210 * allocate a union_node/vnode pair. the vnode is
01dac67e
JSP
211 * referenced and locked. the new vnode is returned
212 * via (vpp). (mp) is the mountpoint of the union filesystem,
213 * (dvp) is the parent directory where the upper layer object
214 * should exist (but doesn't) and (cnp) is the componentname
215 * information which is partially copied to allow the upper
216 * layer object to be created at a later time. (uppervp)
217 * and (lowervp) reference the upper and lower layer objects
218 * being mapped. either, but not both, can be nil.
e2a2f3a7 219 * if supplied, (uppervp) is locked.
3b293975
JSP
220 * the reference is either maintained in the new union_node
221 * object which is allocated, or they are vrele'd.
b991bc2d
JSP
222 *
223 * all union_nodes are maintained on a singly-linked
224 * list. new nodes are only allocated when they cannot
225 * be found on this list. entries on the list are
226 * removed when the vfs reclaim entry is called.
227 *
228 * a single lock is kept for the entire list. this is
229 * needed because the getnewvnode() function can block
230 * waiting for a vnode to become free, in which case there
231 * may be more than one process trying to get the same
232 * vnode. this lock is only taken if we are going to
233 * call getnewvnode, since the kernel itself is single-threaded.
234 *
235 * if an entry is found on the list, then call vget() to
236 * take a reference. this is done because there may be
237 * zero references to it and so it needs to removed from
238 * the vnode free list.
239 */
240int
79f80c71 241union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
b991bc2d
JSP
242 struct vnode **vpp;
243 struct mount *mp;
64c9fefc 244 struct vnode *undvp; /* parent union vnode */
b991bc2d
JSP
245 struct vnode *dvp; /* may be null */
246 struct componentname *cnp; /* may be null */
247 struct vnode *uppervp; /* may be null */
248 struct vnode *lowervp; /* may be null */
249{
250 int error;
251 struct union_node *un;
252 struct union_node **pp;
58572fc0 253 struct vnode *xlowervp = NULLVP;
c93d29a8 254 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
58572fc0 255 int hash;
c93d29a8 256 int vflag;
58572fc0 257 int try;
01dac67e 258
58572fc0 259 if (uppervp == NULLVP && lowervp == NULLVP)
01dac67e
JSP
260 panic("union: unidentifiable allocation");
261
262 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
263 xlowervp = lowervp;
58572fc0 264 lowervp = NULLVP;
01dac67e 265 }
b991bc2d 266
c93d29a8
JSP
267 /* detect the root vnode (and aliases) */
268 vflag = 0;
269 if ((uppervp == um->um_uppervp) &&
270 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
271 if (lowervp == NULLVP) {
272 lowervp = um->um_lowervp;
0b870cc8
JSP
273 if (lowervp != NULLVP)
274 VREF(lowervp);
c93d29a8
JSP
275 }
276 vflag = VROOT;
277 }
278
b991bc2d 279loop:
58572fc0
JSP
280 for (try = 0; try < 3; try++) {
281 switch (try) {
282 case 0:
283 if (lowervp == NULLVP)
284 continue;
285 hash = UNION_HASH(uppervp, lowervp);
e2a2f3a7 286 break;
58572fc0
JSP
287
288 case 1:
289 if (uppervp == NULLVP)
290 continue;
291 hash = UNION_HASH(uppervp, NULLVP);
292 break;
293
294 case 2:
295 if (lowervp == NULLVP)
296 continue;
297 hash = UNION_HASH(NULLVP, lowervp);
298 break;
299 }
300
301 while (union_list_lock(hash))
302 continue;
303
304 for (un = unhead[hash].lh_first; un != 0;
305 un = un->un_cache.le_next) {
306 if ((un->un_lowervp == lowervp ||
307 un->un_lowervp == NULLVP) &&
308 (un->un_uppervp == uppervp ||
309 un->un_uppervp == NULLVP) &&
310 (UNIONTOV(un)->v_mount == mp)) {
311 if (vget(UNIONTOV(un), 0)) {
312 union_list_unlock(hash);
313 goto loop;
314 }
315 break;
316 }
e2a2f3a7 317 }
58572fc0
JSP
318
319 union_list_unlock(hash);
320
321 if (un)
322 break;
e2a2f3a7 323 }
ed5969c8 324
e2a2f3a7
JSP
325 if (un) {
326 /*
327 * Obtain a lock on the union_node.
328 * uppervp is locked, though un->un_uppervp
329 * may not be. this doesn't break the locking
330 * hierarchy since in the case that un->un_uppervp
331 * is not yet locked it will be vrele'd and replaced
332 * with uppervp.
333 */
334
335 if ((dvp != NULLVP) && (uppervp == dvp)) {
ed5969c8 336 /*
e2a2f3a7
JSP
337 * Access ``.'', so (un) will already
338 * be locked. Since this process has
339 * the lock on (uppervp) no other
340 * process can hold the lock on (un).
ed5969c8 341 */
e2a2f3a7
JSP
342#ifdef DIAGNOSTIC
343 if ((un->un_flags & UN_LOCKED) == 0)
344 panic("union: . not locked");
345 else if (curproc && un->un_pid != curproc->p_pid &&
346 un->un_pid > -1 && curproc->p_pid > -1)
347 panic("union: allocvp not lock owner");
348#endif
349 } else {
350 if (un->un_flags & UN_LOCKED) {
351 vrele(UNIONTOV(un));
352 un->un_flags |= UN_WANT;
353 sleep((caddr_t) &un->un_flags, PINOD);
354 goto loop;
79f80c71 355 }
e2a2f3a7 356 un->un_flags |= UN_LOCKED;
ed5969c8 357
e2a2f3a7
JSP
358#ifdef DIAGNOSTIC
359 if (curproc)
360 un->un_pid = curproc->p_pid;
361 else
362 un->un_pid = -1;
363#endif
364 }
365
366 /*
367 * At this point, the union_node is locked,
368 * un->un_uppervp may not be locked, and uppervp
369 * is locked or nil.
370 */
371
372 /*
373 * Save information about the upper layer.
374 */
375 if (uppervp != un->un_uppervp) {
58572fc0 376 union_newupper(un, uppervp);
e2a2f3a7
JSP
377 } else if (uppervp) {
378 vrele(uppervp);
379 }
380
381 if (un->un_uppervp) {
382 un->un_flags |= UN_ULOCK;
383 un->un_flags &= ~UN_KLOCK;
384 }
385
386 /*
387 * Save information about the lower layer.
388 * This needs to keep track of pathname
389 * and directory information which union_vn_create
390 * might need.
391 */
392 if (lowervp != un->un_lowervp) {
58572fc0 393 union_newlower(un, lowervp);
88edbcd4 394 if (cnp && (lowervp != NULLVP)) {
e2a2f3a7
JSP
395 un->un_hash = cnp->cn_hash;
396 un->un_path = malloc(cnp->cn_namelen+1,
397 M_TEMP, M_WAITOK);
398 bcopy(cnp->cn_nameptr, un->un_path,
399 cnp->cn_namelen);
400 un->un_path[cnp->cn_namelen] = '\0';
401 VREF(dvp);
402 un->un_dirvp = dvp;
79f80c71 403 }
e2a2f3a7
JSP
404 } else if (lowervp) {
405 vrele(lowervp);
b991bc2d 406 }
e2a2f3a7
JSP
407 *vpp = UNIONTOV(un);
408 return (0);
b991bc2d
JSP
409 }
410
411 /*
412 * otherwise lock the vp list while we call getnewvnode
413 * since that can block.
414 */
58572fc0
JSP
415 hash = UNION_HASH(uppervp, lowervp);
416
417 if (union_list_lock(hash))
b991bc2d 418 goto loop;
b991bc2d
JSP
419
420 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
e2a2f3a7
JSP
421 if (error) {
422 if (uppervp) {
423 if (dvp == uppervp)
424 vrele(uppervp);
425 else
426 vput(uppervp);
427 }
428 if (lowervp)
429 vrele(lowervp);
430
b991bc2d 431 goto out;
e2a2f3a7 432 }
b991bc2d
JSP
433
434 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
435 M_TEMP, M_WAITOK);
436
c93d29a8 437 (*vpp)->v_flag |= vflag;
01dac67e
JSP
438 if (uppervp)
439 (*vpp)->v_type = uppervp->v_type;
440 else
441 (*vpp)->v_type = lowervp->v_type;
b991bc2d 442 un = VTOUNION(*vpp);
79f80c71 443 un->un_vnode = *vpp;
b991bc2d 444 un->un_uppervp = uppervp;
8a9c17f6 445 un->un_uppersz = VNOVAL;
b991bc2d 446 un->un_lowervp = lowervp;
8a9c17f6 447 un->un_lowersz = VNOVAL;
64c9fefc
JSP
448 un->un_pvp = undvp;
449 if (undvp != NULLVP)
450 VREF(undvp);
15a86f7e 451 un->un_openl = 0;
e2a2f3a7
JSP
452 un->un_flags = UN_LOCKED;
453 if (un->un_uppervp)
454 un->un_flags |= UN_ULOCK;
455#ifdef DIAGNOSTIC
456 if (curproc)
457 un->un_pid = curproc->p_pid;
458 else
459 un->un_pid = -1;
460#endif
88edbcd4 461 if (cnp && (lowervp != NULLVP)) {
ed5969c8 462 un->un_hash = cnp->cn_hash;
b991bc2d
JSP
463 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
464 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
465 un->un_path[cnp->cn_namelen] = '\0';
01dac67e
JSP
466 VREF(dvp);
467 un->un_dirvp = dvp;
b991bc2d 468 } else {
ed5969c8 469 un->un_hash = 0;
b991bc2d 470 un->un_path = 0;
01dac67e 471 un->un_dirvp = 0;
b991bc2d
JSP
472 }
473
58572fc0 474 LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
b5b5f291 475 un->un_flags |= UN_CACHED;
b991bc2d 476
01dac67e
JSP
477 if (xlowervp)
478 vrele(xlowervp);
479
b991bc2d 480out:
58572fc0 481 union_list_unlock(hash);
b991bc2d 482
b991bc2d
JSP
483 return (error);
484}
485
486int
487union_freevp(vp)
488 struct vnode *vp;
489{
b991bc2d
JSP
490 struct union_node *un = VTOUNION(vp);
491
b5b5f291 492 if (un->un_flags & UN_CACHED) {
b5b5f291 493 un->un_flags &= ~UN_CACHED;
233b9c0e 494 LIST_REMOVE(un, un_cache);
b5b5f291 495 }
58572fc0 496
64c9fefc
JSP
497 if (un->un_pvp != NULLVP)
498 vrele(un->un_pvp);
b5b5f291 499 if (un->un_uppervp != NULLVP)
58572fc0 500 vrele(un->un_uppervp);
b5b5f291 501 if (un->un_lowervp != NULLVP)
58572fc0 502 vrele(un->un_lowervp);
b5b5f291 503 if (un->un_dirvp != NULLVP)
58572fc0
JSP
504 vrele(un->un_dirvp);
505 if (un->un_path)
506 free(un->un_path, M_TEMP);
b991bc2d 507
b991bc2d
JSP
508 FREE(vp->v_data, M_TEMP);
509 vp->v_data = 0;
58572fc0 510
b991bc2d
JSP
511 return (0);
512}
81be6ee0
JSP
513
514/*
515 * copyfile. copy the vnode (fvp) to the vnode (tvp)
516 * using a sequence of reads and writes. both (fvp)
517 * and (tvp) are locked on entry and exit.
518 */
519int
f8ee1df5 520union_copyfile(fvp, tvp, cred, p)
81be6ee0
JSP
521 struct vnode *fvp;
522 struct vnode *tvp;
f8ee1df5
JSP
523 struct ucred *cred;
524 struct proc *p;
81be6ee0
JSP
525{
526 char *buf;
527 struct uio uio;
528 struct iovec iov;
529 int error = 0;
530
531 /*
532 * strategy:
533 * allocate a buffer of size MAXBSIZE.
534 * loop doing reads and writes, keeping track
535 * of the current uio offset.
536 * give up at the first sign of trouble.
537 */
538
539 uio.uio_procp = p;
540 uio.uio_segflg = UIO_SYSSPACE;
541 uio.uio_offset = 0;
542
543 VOP_UNLOCK(fvp); /* XXX */
b84b63e6 544 VOP_LEASE(fvp, p, cred, LEASE_READ);
81be6ee0
JSP
545 VOP_LOCK(fvp); /* XXX */
546 VOP_UNLOCK(tvp); /* XXX */
b84b63e6 547 VOP_LEASE(tvp, p, cred, LEASE_WRITE);
81be6ee0
JSP
548 VOP_LOCK(tvp); /* XXX */
549
550 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
551
552 /* ugly loop follows... */
553 do {
554 off_t offset = uio.uio_offset;
555
556 uio.uio_iov = &iov;
557 uio.uio_iovcnt = 1;
558 iov.iov_base = buf;
559 iov.iov_len = MAXBSIZE;
560 uio.uio_resid = iov.iov_len;
561 uio.uio_rw = UIO_READ;
562 error = VOP_READ(fvp, &uio, 0, cred);
563
564 if (error == 0) {
565 uio.uio_iov = &iov;
566 uio.uio_iovcnt = 1;
567 iov.iov_base = buf;
568 iov.iov_len = MAXBSIZE - uio.uio_resid;
569 uio.uio_offset = offset;
570 uio.uio_rw = UIO_WRITE;
571 uio.uio_resid = iov.iov_len;
572
573 if (uio.uio_resid == 0)
574 break;
575
576 do {
577 error = VOP_WRITE(tvp, &uio, 0, cred);
578 } while ((uio.uio_resid > 0) && (error == 0));
579 }
580
581 } while (error == 0);
582
583 free(buf, M_TEMP);
584 return (error);
585}
586
f8ee1df5
JSP
587/*
588 * (un) is assumed to be locked on entry and remains
589 * locked on exit.
590 */
591int
592union_copyup(un, docopy, cred, p)
593 struct union_node *un;
594 int docopy;
595 struct ucred *cred;
596 struct proc *p;
597{
598 int error;
599 struct vnode *lvp, *uvp;
600
601 error = union_vn_create(&uvp, un, p);
602 if (error)
603 return (error);
604
605 /* at this point, uppervp is locked */
606 union_newupper(un, uvp);
607 un->un_flags |= UN_ULOCK;
608
609 lvp = un->un_lowervp;
610
611 if (docopy) {
612 /*
613 * XX - should not ignore errors
614 * from VOP_CLOSE
615 */
616 VOP_LOCK(lvp);
617 error = VOP_OPEN(lvp, FREAD, cred, p);
618 if (error == 0) {
619 error = union_copyfile(lvp, uvp, cred, p);
620 VOP_UNLOCK(lvp);
621 (void) VOP_CLOSE(lvp, FREAD);
622 }
623#ifdef UNION_DIAGNOSTIC
624 if (error == 0)
625 uprintf("union: copied up %s\n", un->un_path);
626#endif
627
628 }
629 un->un_flags &= ~UN_ULOCK;
630 VOP_UNLOCK(uvp);
631 union_vn_close(uvp, FWRITE, cred, p);
632 VOP_LOCK(uvp);
633 un->un_flags |= UN_ULOCK;
634
635 /*
636 * Subsequent IOs will go to the top layer, so
637 * call close on the lower vnode and open on the
638 * upper vnode to ensure that the filesystem keeps
639 * its references counts right. This doesn't do
640 * the right thing with (cred) and (FREAD) though.
641 * Ignoring error returns is not right, either.
642 */
643 if (error == 0) {
644 int i;
645
646 for (i = 0; i < un->un_openl; i++) {
647 (void) VOP_CLOSE(lvp, FREAD);
648 (void) VOP_OPEN(uvp, FREAD, cred, p);
649 }
650 un->un_openl = 0;
651 }
652
653 return (error);
654
655}
656
88edbcd4 657static int
b84b63e6 658union_relookup(um, dvp, vpp, cnp, cn, path, pathlen)
88edbcd4
JSP
659 struct union_mount *um;
660 struct vnode *dvp;
661 struct vnode **vpp;
662 struct componentname *cnp;
663 struct componentname *cn;
664 char *path;
b84b63e6 665 int pathlen;
88edbcd4
JSP
666{
667 int error;
668
669 /*
670 * A new componentname structure must be faked up because
671 * there is no way to know where the upper level cnp came
672 * from or what it is being used for. This must duplicate
673 * some of the work done by NDINIT, some of the work done
674 * by namei, some of the work done by lookup and some of
675 * the work done by VOP_LOOKUP when given a CREATE flag.
676 * Conclusion: Horrible.
677 *
678 * The pathname buffer will be FREEed by VOP_MKDIR.
679 */
b84b63e6 680 cn->cn_namelen = pathlen;
88edbcd4
JSP
681 cn->cn_pnbuf = malloc(cn->cn_namelen+1, M_NAMEI, M_WAITOK);
682 bcopy(path, cn->cn_pnbuf, cn->cn_namelen);
683 cn->cn_pnbuf[cn->cn_namelen] = '\0';
684
685 cn->cn_nameiop = CREATE;
686 cn->cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
687 cn->cn_proc = cnp->cn_proc;
688 if (um->um_op == UNMNT_ABOVE)
689 cn->cn_cred = cnp->cn_cred;
690 else
691 cn->cn_cred = um->um_cred;
692 cn->cn_nameptr = cn->cn_pnbuf;
693 cn->cn_hash = cnp->cn_hash;
694 cn->cn_consume = cnp->cn_consume;
695
696 VREF(dvp);
697 error = relookup(dvp, vpp, cn);
698 if (!error)
699 vrele(dvp);
700
701 return (error);
702}
703
3b293975
JSP
704/*
705 * Create a shadow directory in the upper layer.
706 * The new vnode is returned locked.
707 *
708 * (um) points to the union mount structure for access to the
709 * the mounting process's credentials.
710 * (dvp) is the directory in which to create the shadow directory.
711 * it is unlocked on entry and exit.
712 * (cnp) is the componentname to be created.
713 * (vpp) is the returned newly created shadow directory, which
714 * is returned locked.
715 */
716int
717union_mkshadow(um, dvp, cnp, vpp)
718 struct union_mount *um;
719 struct vnode *dvp;
720 struct componentname *cnp;
721 struct vnode **vpp;
722{
723 int error;
724 struct vattr va;
725 struct proc *p = cnp->cn_proc;
726 struct componentname cn;
727
b84b63e6
JSP
728 error = union_relookup(um, dvp, vpp, cnp, &cn,
729 cnp->cn_nameptr, cnp->cn_namelen);
88edbcd4
JSP
730 if (error)
731 return (error);
732
733 if (*vpp) {
734 VOP_ABORTOP(dvp, &cn);
735 VOP_UNLOCK(dvp);
736 vrele(*vpp);
737 *vpp = NULLVP;
738 return (EEXIST);
739 }
740
3b293975
JSP
741 /*
742 * policy: when creating the shadow directory in the
692a90df
JSP
743 * upper layer, create it owned by the user who did
744 * the mount, group from parent directory, and mode
745 * 777 modified by umask (ie mostly identical to the
746 * mkdir syscall). (jsp, kb)
3b293975
JSP
747 */
748
88edbcd4
JSP
749 VATTR_NULL(&va);
750 va.va_type = VDIR;
751 va.va_mode = um->um_cmode;
3b293975 752
b84b63e6
JSP
753 /* VOP_LEASE: dvp is locked */
754 VOP_LEASE(dvp, p, cn.cn_cred, LEASE_WRITE);
3b293975 755
88edbcd4
JSP
756 error = VOP_MKDIR(dvp, vpp, &cn, &va);
757 return (error);
758}
759
760/*
761 * Create a whiteout entry in the upper layer.
762 *
763 * (um) points to the union mount structure for access to the
764 * the mounting process's credentials.
765 * (dvp) is the directory in which to create the whiteout.
766 * it is locked on entry and exit.
767 * (cnp) is the componentname to be created.
768 */
769int
770union_mkwhiteout(um, dvp, cnp, path)
771 struct union_mount *um;
772 struct vnode *dvp;
773 struct componentname *cnp;
774 char *path;
775{
776 int error;
777 struct vattr va;
778 struct proc *p = cnp->cn_proc;
779 struct vnode **vpp;
780 struct componentname cn;
781
782 VOP_UNLOCK(dvp);
b84b63e6
JSP
783 error = union_relookup(um, dvp, vpp, cnp, &cn, path, strlen(path));
784 if (error) {
785 VOP_LOCK(dvp);
3b293975 786 return (error);
b84b63e6 787 }
3b293975
JSP
788
789 if (*vpp) {
790 VOP_ABORTOP(dvp, &cn);
88edbcd4 791 vrele(dvp);
3b293975
JSP
792 vrele(*vpp);
793 *vpp = NULLVP;
794 return (EEXIST);
795 }
796
b84b63e6
JSP
797 /* VOP_LEASE: dvp is locked */
798 VOP_LEASE(dvp, p, p->p_ucred, LEASE_WRITE);
3b293975 799
88edbcd4 800 error = VOP_WHITEOUT(dvp, &cn, CREATE);
b84b63e6 801 if (error)
88edbcd4
JSP
802 VOP_ABORTOP(dvp, &cn);
803
804 vrele(dvp);
805
3b293975
JSP
806 return (error);
807}
808
81be6ee0
JSP
809/*
810 * union_vn_create: creates and opens a new shadow file
811 * on the upper union layer. this function is similar
812 * in spirit to calling vn_open but it avoids calling namei().
813 * the problem with calling namei is that a) it locks too many
814 * things, and b) it doesn't start at the "right" directory,
815 * whereas relookup is told where to start.
816 */
817int
3b293975 818union_vn_create(vpp, un, p)
81be6ee0
JSP
819 struct vnode **vpp;
820 struct union_node *un;
81be6ee0
JSP
821 struct proc *p;
822{
823 struct vnode *vp;
824 struct ucred *cred = p->p_ucred;
825 struct vattr vat;
826 struct vattr *vap = &vat;
827 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
828 int error;
ed5969c8 829 int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
81be6ee0
JSP
830 char *cp;
831 struct componentname cn;
832
833 *vpp = NULLVP;
834
692a90df
JSP
835 /*
836 * Build a new componentname structure (for the same
837 * reasons outlines in union_mkshadow).
838 * The difference here is that the file is owned by
839 * the current user, rather than by the person who
840 * did the mount, since the current user needs to be
841 * able to write the file (that's why it is being
842 * copied in the first place).
843 */
81be6ee0
JSP
844 cn.cn_namelen = strlen(un->un_path);
845 cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
846 bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
847 cn.cn_nameiop = CREATE;
bf006388 848 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
81be6ee0
JSP
849 cn.cn_proc = p;
850 cn.cn_cred = p->p_ucred;
851 cn.cn_nameptr = cn.cn_pnbuf;
ed5969c8 852 cn.cn_hash = un->un_hash;
81be6ee0
JSP
853 cn.cn_consume = 0;
854
ed5969c8 855 VREF(un->un_dirvp);
81be6ee0
JSP
856 if (error = relookup(un->un_dirvp, &vp, &cn))
857 return (error);
ed5969c8
JSP
858 vrele(un->un_dirvp);
859
bf006388 860 if (vp) {
81be6ee0
JSP
861 VOP_ABORTOP(un->un_dirvp, &cn);
862 if (un->un_dirvp == vp)
863 vrele(un->un_dirvp);
864 else
bf006388
JSP
865 vput(un->un_dirvp);
866 vrele(vp);
867 return (EEXIST);
81be6ee0
JSP
868 }
869
bf006388
JSP
870 /*
871 * Good - there was no race to create the file
872 * so go ahead and create it. The permissions
873 * on the file will be 0666 modified by the
874 * current user's umask. Access to the file, while
875 * it is unioned, will require access to the top *and*
876 * bottom files. Access when not unioned will simply
877 * require access to the top-level file.
878 * TODO: confirm choice of access permissions.
879 */
81be6ee0 880 VATTR_NULL(vap);
bf006388
JSP
881 vap->va_type = VREG;
882 vap->va_mode = cmode;
b84b63e6 883 VOP_LEASE(un->un_dirvp, p, cred, LEASE_WRITE);
bf006388
JSP
884 if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap))
885 return (error);
81be6ee0 886
bf006388
JSP
887 if (error = VOP_OPEN(vp, fmode, cred, p)) {
888 vput(vp);
889 return (error);
890 }
81be6ee0
JSP
891
892 vp->v_writecount++;
893 *vpp = vp;
894 return (0);
81be6ee0 895}
ed5969c8
JSP
896
897int
15a86f7e 898union_vn_close(vp, fmode, cred, p)
ed5969c8
JSP
899 struct vnode *vp;
900 int fmode;
15a86f7e
JSP
901 struct ucred *cred;
902 struct proc *p;
ed5969c8 903{
88edbcd4 904
ed5969c8
JSP
905 if (fmode & FWRITE)
906 --vp->v_writecount;
907 return (VOP_CLOSE(vp, fmode));
908}
909
910void
911union_removed_upper(un)
912 struct union_node *un;
913{
233b9c0e 914
e2a2f3a7
JSP
915 if (un->un_flags & UN_ULOCK) {
916 un->un_flags &= ~UN_ULOCK;
58572fc0 917 VOP_UNLOCK(un->un_uppervp);
e2a2f3a7 918 }
58572fc0 919
233b9c0e
JSP
920 if (un->un_flags & UN_CACHED) {
921 un->un_flags &= ~UN_CACHED;
922 LIST_REMOVE(un, un_cache);
923 }
ed5969c8 924}
15a86f7e
JSP
925
926struct vnode *
927union_lowervp(vp)
928 struct vnode *vp;
929{
930 struct union_node *un = VTOUNION(vp);
931
f8ee1df5
JSP
932 if ((un->un_lowervp != NULLVP) &&
933 (vp->v_type == un->un_lowervp->v_type)) {
934 if (vget(un->un_lowervp, 0) == 0)
935 return (un->un_lowervp);
15a86f7e
JSP
936 }
937
f8ee1df5 938 return (NULLVP);
15a86f7e 939}