changes for whiteouts and union filesystem
[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 *
88edbcd4 11 * @(#)union_subr.c 8.12 (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 */
544 LEASE_CHECK(fvp, p, cred, LEASE_READ);
545 VOP_LOCK(fvp); /* XXX */
546 VOP_UNLOCK(tvp); /* XXX */
547 LEASE_CHECK(tvp, p, cred, LEASE_WRITE);
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
JSP
657static int
658union_relookup(um, dvp, vpp, cnp, cn, path)
659 struct union_mount *um;
660 struct vnode *dvp;
661 struct vnode **vpp;
662 struct componentname *cnp;
663 struct componentname *cn;
664 char *path;
665{
666 int error;
667
668 /*
669 * A new componentname structure must be faked up because
670 * there is no way to know where the upper level cnp came
671 * from or what it is being used for. This must duplicate
672 * some of the work done by NDINIT, some of the work done
673 * by namei, some of the work done by lookup and some of
674 * the work done by VOP_LOOKUP when given a CREATE flag.
675 * Conclusion: Horrible.
676 *
677 * The pathname buffer will be FREEed by VOP_MKDIR.
678 */
679 cn->cn_namelen = strlen(path);
680 cn->cn_pnbuf = malloc(cn->cn_namelen+1, M_NAMEI, M_WAITOK);
681 bcopy(path, cn->cn_pnbuf, cn->cn_namelen);
682 cn->cn_pnbuf[cn->cn_namelen] = '\0';
683
684 cn->cn_nameiop = CREATE;
685 cn->cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
686 cn->cn_proc = cnp->cn_proc;
687 if (um->um_op == UNMNT_ABOVE)
688 cn->cn_cred = cnp->cn_cred;
689 else
690 cn->cn_cred = um->um_cred;
691 cn->cn_nameptr = cn->cn_pnbuf;
692 cn->cn_hash = cnp->cn_hash;
693 cn->cn_consume = cnp->cn_consume;
694
695 VREF(dvp);
696 error = relookup(dvp, vpp, cn);
697 if (!error)
698 vrele(dvp);
699
700 return (error);
701}
702
3b293975
JSP
703/*
704 * Create a shadow directory in the upper layer.
705 * The new vnode is returned locked.
706 *
707 * (um) points to the union mount structure for access to the
708 * the mounting process's credentials.
709 * (dvp) is the directory in which to create the shadow directory.
710 * it is unlocked on entry and exit.
711 * (cnp) is the componentname to be created.
712 * (vpp) is the returned newly created shadow directory, which
713 * is returned locked.
714 */
715int
716union_mkshadow(um, dvp, cnp, vpp)
717 struct union_mount *um;
718 struct vnode *dvp;
719 struct componentname *cnp;
720 struct vnode **vpp;
721{
722 int error;
723 struct vattr va;
724 struct proc *p = cnp->cn_proc;
725 struct componentname cn;
726
88edbcd4
JSP
727 error = union_relookup(um, dvp, vpp, cnp, &cn, cnp->cn_nameptr);
728 if (error)
729 return (error);
730
731 if (*vpp) {
732 VOP_ABORTOP(dvp, &cn);
733 VOP_UNLOCK(dvp);
734 vrele(*vpp);
735 *vpp = NULLVP;
736 return (EEXIST);
737 }
738
3b293975
JSP
739 /*
740 * policy: when creating the shadow directory in the
692a90df
JSP
741 * upper layer, create it owned by the user who did
742 * the mount, group from parent directory, and mode
743 * 777 modified by umask (ie mostly identical to the
744 * mkdir syscall). (jsp, kb)
3b293975
JSP
745 */
746
88edbcd4
JSP
747 VATTR_NULL(&va);
748 va.va_type = VDIR;
749 va.va_mode = um->um_cmode;
3b293975 750
88edbcd4
JSP
751 /* LEASE_CHECK: dvp is locked */
752 LEASE_CHECK(dvp, p, cn.cn_cred, LEASE_WRITE);
3b293975 753
88edbcd4
JSP
754 error = VOP_MKDIR(dvp, vpp, &cn, &va);
755 return (error);
756}
757
758/*
759 * Create a whiteout entry in the upper layer.
760 *
761 * (um) points to the union mount structure for access to the
762 * the mounting process's credentials.
763 * (dvp) is the directory in which to create the whiteout.
764 * it is locked on entry and exit.
765 * (cnp) is the componentname to be created.
766 */
767int
768union_mkwhiteout(um, dvp, cnp, path)
769 struct union_mount *um;
770 struct vnode *dvp;
771 struct componentname *cnp;
772 char *path;
773{
774 int error;
775 struct vattr va;
776 struct proc *p = cnp->cn_proc;
777 struct vnode **vpp;
778 struct componentname cn;
779
780 VOP_UNLOCK(dvp);
781 error = union_relookup(um, dvp, vpp, cnp, &cn, path);
782 if (error)
3b293975
JSP
783 return (error);
784
785 if (*vpp) {
786 VOP_ABORTOP(dvp, &cn);
88edbcd4 787 vrele(dvp);
3b293975
JSP
788 vrele(*vpp);
789 *vpp = NULLVP;
790 return (EEXIST);
791 }
792
3b293975
JSP
793 /* LEASE_CHECK: dvp is locked */
794 LEASE_CHECK(dvp, p, p->p_ucred, LEASE_WRITE);
795
88edbcd4
JSP
796 error = VOP_WHITEOUT(dvp, &cn, CREATE);
797 if (error != 0)
798 VOP_ABORTOP(dvp, &cn);
799
800 vrele(dvp);
801
3b293975
JSP
802 return (error);
803}
804
81be6ee0
JSP
805/*
806 * union_vn_create: creates and opens a new shadow file
807 * on the upper union layer. this function is similar
808 * in spirit to calling vn_open but it avoids calling namei().
809 * the problem with calling namei is that a) it locks too many
810 * things, and b) it doesn't start at the "right" directory,
811 * whereas relookup is told where to start.
812 */
813int
3b293975 814union_vn_create(vpp, un, p)
81be6ee0
JSP
815 struct vnode **vpp;
816 struct union_node *un;
81be6ee0
JSP
817 struct proc *p;
818{
819 struct vnode *vp;
820 struct ucred *cred = p->p_ucred;
821 struct vattr vat;
822 struct vattr *vap = &vat;
823 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
824 int error;
ed5969c8 825 int cmode = UN_FILEMODE & ~p->p_fd->fd_cmask;
81be6ee0
JSP
826 char *cp;
827 struct componentname cn;
828
829 *vpp = NULLVP;
830
692a90df
JSP
831 /*
832 * Build a new componentname structure (for the same
833 * reasons outlines in union_mkshadow).
834 * The difference here is that the file is owned by
835 * the current user, rather than by the person who
836 * did the mount, since the current user needs to be
837 * able to write the file (that's why it is being
838 * copied in the first place).
839 */
81be6ee0
JSP
840 cn.cn_namelen = strlen(un->un_path);
841 cn.cn_pnbuf = (caddr_t) malloc(cn.cn_namelen, M_NAMEI, M_WAITOK);
842 bcopy(un->un_path, cn.cn_pnbuf, cn.cn_namelen+1);
843 cn.cn_nameiop = CREATE;
bf006388 844 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
81be6ee0
JSP
845 cn.cn_proc = p;
846 cn.cn_cred = p->p_ucred;
847 cn.cn_nameptr = cn.cn_pnbuf;
ed5969c8 848 cn.cn_hash = un->un_hash;
81be6ee0
JSP
849 cn.cn_consume = 0;
850
ed5969c8 851 VREF(un->un_dirvp);
81be6ee0
JSP
852 if (error = relookup(un->un_dirvp, &vp, &cn))
853 return (error);
ed5969c8
JSP
854 vrele(un->un_dirvp);
855
bf006388 856 if (vp) {
81be6ee0
JSP
857 VOP_ABORTOP(un->un_dirvp, &cn);
858 if (un->un_dirvp == vp)
859 vrele(un->un_dirvp);
860 else
bf006388
JSP
861 vput(un->un_dirvp);
862 vrele(vp);
863 return (EEXIST);
81be6ee0
JSP
864 }
865
bf006388
JSP
866 /*
867 * Good - there was no race to create the file
868 * so go ahead and create it. The permissions
869 * on the file will be 0666 modified by the
870 * current user's umask. Access to the file, while
871 * it is unioned, will require access to the top *and*
872 * bottom files. Access when not unioned will simply
873 * require access to the top-level file.
874 * TODO: confirm choice of access permissions.
875 */
81be6ee0 876 VATTR_NULL(vap);
bf006388
JSP
877 vap->va_type = VREG;
878 vap->va_mode = cmode;
879 LEASE_CHECK(un->un_dirvp, p, cred, LEASE_WRITE);
880 if (error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap))
881 return (error);
81be6ee0 882
bf006388
JSP
883 if (error = VOP_OPEN(vp, fmode, cred, p)) {
884 vput(vp);
885 return (error);
886 }
81be6ee0
JSP
887
888 vp->v_writecount++;
889 *vpp = vp;
890 return (0);
81be6ee0 891}
ed5969c8
JSP
892
893int
15a86f7e 894union_vn_close(vp, fmode, cred, p)
ed5969c8
JSP
895 struct vnode *vp;
896 int fmode;
15a86f7e
JSP
897 struct ucred *cred;
898 struct proc *p;
ed5969c8 899{
88edbcd4 900
ed5969c8
JSP
901 if (fmode & FWRITE)
902 --vp->v_writecount;
903 return (VOP_CLOSE(vp, fmode));
904}
905
906void
907union_removed_upper(un)
908 struct union_node *un;
909{
233b9c0e 910
e2a2f3a7
JSP
911 if (un->un_flags & UN_ULOCK) {
912 un->un_flags &= ~UN_ULOCK;
58572fc0 913 VOP_UNLOCK(un->un_uppervp);
e2a2f3a7 914 }
58572fc0 915
233b9c0e
JSP
916 if (un->un_flags & UN_CACHED) {
917 un->un_flags &= ~UN_CACHED;
918 LIST_REMOVE(un, un_cache);
919 }
ed5969c8 920}
15a86f7e
JSP
921
922struct vnode *
923union_lowervp(vp)
924 struct vnode *vp;
925{
926 struct union_node *un = VTOUNION(vp);
927
f8ee1df5
JSP
928 if ((un->un_lowervp != NULLVP) &&
929 (vp->v_type == un->un_lowervp->v_type)) {
930 if (vget(un->un_lowervp, 0) == 0)
931 return (un->un_lowervp);
15a86f7e
JSP
932 }
933
f8ee1df5 934 return (NULLVP);
15a86f7e 935}