union_subr.c revision 1.7 1 /* $NetBSD: union_subr.c,v 1.7 2003/08/07 16:31:40 agc Exp $ */
2
3 /*
4 * Copyright (c) 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
35 */
36
37 /*
38 * Copyright (c) 1994 Jan-Simon Pendry
39 *
40 * This code is derived from software contributed to Berkeley by
41 * Jan-Simon Pendry.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
70 *
71 * @(#)union_subr.c 8.20 (Berkeley) 5/20/95
72 */
73
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: union_subr.c,v 1.7 2003/08/07 16:31:40 agc Exp $");
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/proc.h>
80 #include <sys/time.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
83 #include <sys/namei.h>
84 #include <sys/malloc.h>
85 #include <sys/file.h>
86 #include <sys/filedesc.h>
87 #include <sys/queue.h>
88 #include <sys/mount.h>
89 #include <sys/stat.h>
90
91 #include <uvm/uvm_extern.h>
92
93 #include <fs/union/union.h>
94
95 #ifdef DIAGNOSTIC
96 #include <sys/proc.h>
97 #endif
98
99 /* must be power of two, otherwise change UNION_HASH() */
100 #define NHASH 32
101
102 /* unsigned int ... */
103 #define UNION_HASH(u, l) \
104 (((((unsigned long) (u)) + ((unsigned long) l)) >> 8) & (NHASH-1))
105
106 static LIST_HEAD(unhead, union_node) unhead[NHASH];
107 static int unvplock[NHASH];
108
109 static int union_list_lock __P((int));
110 static void union_list_unlock __P((int));
111 void union_updatevp __P((struct union_node *, struct vnode *, struct vnode *));
112 static int union_relookup __P((struct union_mount *, struct vnode *,
113 struct vnode **, struct componentname *,
114 struct componentname *, const char *, int));
115 int union_vn_close __P((struct vnode *, int, struct ucred *, struct proc *));
116 static void union_dircache_r __P((struct vnode *, struct vnode ***, int *));
117 struct vnode *union_dircache __P((struct vnode *, struct proc *));
118
119 void
120 union_init()
121 {
122 int i;
123
124 for (i = 0; i < NHASH; i++)
125 LIST_INIT(&unhead[i]);
126 memset((caddr_t) unvplock, 0, sizeof(unvplock));
127 }
128
129 /*
130 * Free global unionfs resources.
131 */
132 void
133 union_done()
134 {
135
136 /* Make sure to unset the readdir hook. */
137 vn_union_readdir_hook = NULL;
138 }
139
140 static int
141 union_list_lock(ix)
142 int ix;
143 {
144
145 if (unvplock[ix] & UN_LOCKED) {
146 unvplock[ix] |= UN_WANTED;
147 (void) tsleep(&unvplock[ix], PINOD, "unionlk", 0);
148 return (1);
149 }
150
151 unvplock[ix] |= UN_LOCKED;
152
153 return (0);
154 }
155
156 static void
157 union_list_unlock(ix)
158 int ix;
159 {
160
161 unvplock[ix] &= ~UN_LOCKED;
162
163 if (unvplock[ix] & UN_WANTED) {
164 unvplock[ix] &= ~UN_WANTED;
165 wakeup((caddr_t) &unvplock[ix]);
166 }
167 }
168
169 void
170 union_updatevp(un, uppervp, lowervp)
171 struct union_node *un;
172 struct vnode *uppervp;
173 struct vnode *lowervp;
174 {
175 int ohash = UNION_HASH(un->un_uppervp, un->un_lowervp);
176 int nhash = UNION_HASH(uppervp, lowervp);
177 int docache = (lowervp != NULLVP || uppervp != NULLVP);
178 int lhash, uhash;
179
180 /*
181 * Ensure locking is ordered from lower to higher
182 * to avoid deadlocks.
183 */
184 if (nhash < ohash) {
185 lhash = nhash;
186 uhash = ohash;
187 } else {
188 lhash = ohash;
189 uhash = nhash;
190 }
191
192 if (lhash != uhash)
193 while (union_list_lock(lhash))
194 continue;
195
196 while (union_list_lock(uhash))
197 continue;
198
199 if (ohash != nhash || !docache) {
200 if (un->un_flags & UN_CACHED) {
201 un->un_flags &= ~UN_CACHED;
202 LIST_REMOVE(un, un_cache);
203 }
204 }
205
206 if (ohash != nhash)
207 union_list_unlock(ohash);
208
209 if (un->un_lowervp != lowervp) {
210 if (un->un_lowervp) {
211 vrele(un->un_lowervp);
212 if (un->un_path) {
213 free(un->un_path, M_TEMP);
214 un->un_path = 0;
215 }
216 if (un->un_dirvp) {
217 vrele(un->un_dirvp);
218 un->un_dirvp = NULLVP;
219 }
220 }
221 un->un_lowervp = lowervp;
222 un->un_lowersz = VNOVAL;
223 }
224
225 if (un->un_uppervp != uppervp) {
226 if (un->un_uppervp)
227 vrele(un->un_uppervp);
228
229 un->un_uppervp = uppervp;
230 un->un_uppersz = VNOVAL;
231 }
232
233 if (docache && (ohash != nhash)) {
234 LIST_INSERT_HEAD(&unhead[nhash], un, un_cache);
235 un->un_flags |= UN_CACHED;
236 }
237
238 union_list_unlock(nhash);
239 }
240
241 void
242 union_newlower(un, lowervp)
243 struct union_node *un;
244 struct vnode *lowervp;
245 {
246
247 union_updatevp(un, un->un_uppervp, lowervp);
248 }
249
250 void
251 union_newupper(un, uppervp)
252 struct union_node *un;
253 struct vnode *uppervp;
254 {
255
256 union_updatevp(un, uppervp, un->un_lowervp);
257 }
258
259 /*
260 * Keep track of size changes in the underlying vnodes.
261 * If the size changes, then callback to the vm layer
262 * giving priority to the upper layer size.
263 */
264 void
265 union_newsize(vp, uppersz, lowersz)
266 struct vnode *vp;
267 off_t uppersz, lowersz;
268 {
269 struct union_node *un;
270 off_t sz;
271
272 /* only interested in regular files */
273 if (vp->v_type != VREG)
274 return;
275
276 un = VTOUNION(vp);
277 sz = VNOVAL;
278
279 if ((uppersz != VNOVAL) && (un->un_uppersz != uppersz)) {
280 un->un_uppersz = uppersz;
281 if (sz == VNOVAL)
282 sz = un->un_uppersz;
283 }
284
285 if ((lowersz != VNOVAL) && (un->un_lowersz != lowersz)) {
286 un->un_lowersz = lowersz;
287 if (sz == VNOVAL)
288 sz = un->un_lowersz;
289 }
290
291 if (sz != VNOVAL) {
292 #ifdef UNION_DIAGNOSTIC
293 printf("union: %s size now %qd\n",
294 uppersz != VNOVAL ? "upper" : "lower", sz);
295 #endif
296 uvm_vnp_setsize(vp, sz);
297 }
298 }
299
300 /*
301 * allocate a union_node/vnode pair. the vnode is
302 * referenced and locked. the new vnode is returned
303 * via (vpp). (mp) is the mountpoint of the union filesystem,
304 * (dvp) is the parent directory where the upper layer object
305 * should exist (but doesn't) and (cnp) is the componentname
306 * information which is partially copied to allow the upper
307 * layer object to be created at a later time. (uppervp)
308 * and (lowervp) reference the upper and lower layer objects
309 * being mapped. either, but not both, can be nil.
310 * if supplied, (uppervp) is locked.
311 * the reference is either maintained in the new union_node
312 * object which is allocated, or they are vrele'd.
313 *
314 * all union_nodes are maintained on a singly-linked
315 * list. new nodes are only allocated when they cannot
316 * be found on this list. entries on the list are
317 * removed when the vfs reclaim entry is called.
318 *
319 * a single lock is kept for the entire list. this is
320 * needed because the getnewvnode() function can block
321 * waiting for a vnode to become free, in which case there
322 * may be more than one process trying to get the same
323 * vnode. this lock is only taken if we are going to
324 * call getnewvnode, since the kernel itself is single-threaded.
325 *
326 * if an entry is found on the list, then call vget() to
327 * take a reference. this is done because there may be
328 * zero references to it and so it needs to removed from
329 * the vnode free list.
330 */
331 int
332 union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp, docache)
333 struct vnode **vpp;
334 struct mount *mp;
335 struct vnode *undvp; /* parent union vnode */
336 struct vnode *dvp; /* may be null */
337 struct componentname *cnp; /* may be null */
338 struct vnode *uppervp; /* may be null */
339 struct vnode *lowervp; /* may be null */
340 int docache;
341 {
342 int error;
343 struct union_node *un = NULL;
344 struct vnode *xlowervp = NULLVP;
345 struct union_mount *um = MOUNTTOUNIONMOUNT(mp);
346 int hash = 0;
347 int vflag;
348 int try;
349
350 if (uppervp == NULLVP && lowervp == NULLVP)
351 panic("union: unidentifiable allocation");
352
353 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
354 xlowervp = lowervp;
355 lowervp = NULLVP;
356 }
357
358 /* detect the root vnode (and aliases) */
359 vflag = VLAYER;
360 if ((uppervp == um->um_uppervp) &&
361 ((lowervp == NULLVP) || lowervp == um->um_lowervp)) {
362 if (lowervp == NULLVP) {
363 lowervp = um->um_lowervp;
364 if (lowervp != NULLVP)
365 VREF(lowervp);
366 }
367 vflag = VROOT;
368 }
369
370 loop:
371 if (!docache) {
372 un = 0;
373 } else for (try = 0; try < 3; try++) {
374 switch (try) {
375 case 0:
376 if (lowervp == NULLVP)
377 continue;
378 hash = UNION_HASH(uppervp, lowervp);
379 break;
380
381 case 1:
382 if (uppervp == NULLVP)
383 continue;
384 hash = UNION_HASH(uppervp, NULLVP);
385 break;
386
387 case 2:
388 if (lowervp == NULLVP)
389 continue;
390 hash = UNION_HASH(NULLVP, lowervp);
391 break;
392 }
393
394 while (union_list_lock(hash))
395 continue;
396
397 for (un = unhead[hash].lh_first; un != 0;
398 un = un->un_cache.le_next) {
399 if ((un->un_lowervp == lowervp ||
400 un->un_lowervp == NULLVP) &&
401 (un->un_uppervp == uppervp ||
402 un->un_uppervp == NULLVP) &&
403 (UNIONTOV(un)->v_mount == mp)) {
404 if (vget(UNIONTOV(un), 0)) {
405 union_list_unlock(hash);
406 goto loop;
407 }
408 break;
409 }
410 }
411
412 union_list_unlock(hash);
413
414 if (un)
415 break;
416 }
417
418 if (un) {
419 /*
420 * Obtain a lock on the union_node.
421 * uppervp is locked, though un->un_uppervp
422 * may not be. this doesn't break the locking
423 * hierarchy since in the case that un->un_uppervp
424 * is not yet locked it will be vrele'd and replaced
425 * with uppervp.
426 */
427
428 if ((dvp != NULLVP) && (uppervp == dvp)) {
429 /*
430 * Access ``.'', so (un) will already
431 * be locked. Since this process has
432 * the lock on (uppervp) no other
433 * process can hold the lock on (un).
434 */
435 #ifdef DIAGNOSTIC
436 if ((un->un_flags & UN_LOCKED) == 0)
437 panic("union: . not locked");
438 else if (curproc && un->un_pid != curproc->p_pid &&
439 un->un_pid > -1 && curproc->p_pid > -1)
440 panic("union: allocvp not lock owner");
441 #endif
442 } else {
443 if (un->un_flags & UN_LOCKED) {
444 vrele(UNIONTOV(un));
445 un->un_flags |= UN_WANTED;
446 (void) tsleep(&un->un_flags, PINOD,
447 "unionalloc", 0);
448 goto loop;
449 }
450 un->un_flags |= UN_LOCKED;
451
452 #ifdef DIAGNOSTIC
453 if (curproc)
454 un->un_pid = curproc->p_pid;
455 else
456 un->un_pid = -1;
457 #endif
458 }
459
460 /*
461 * At this point, the union_node is locked,
462 * un->un_uppervp may not be locked, and uppervp
463 * is locked or nil.
464 */
465
466 /*
467 * Save information about the upper layer.
468 */
469 if (uppervp != un->un_uppervp) {
470 union_newupper(un, uppervp);
471 } else if (uppervp) {
472 vrele(uppervp);
473 }
474
475 if (un->un_uppervp) {
476 un->un_flags |= UN_ULOCK;
477 un->un_flags &= ~UN_KLOCK;
478 }
479
480 /*
481 * Save information about the lower layer.
482 * This needs to keep track of pathname
483 * and directory information which union_vn_create
484 * might need.
485 */
486 if (lowervp != un->un_lowervp) {
487 union_newlower(un, lowervp);
488 if (cnp && (lowervp != NULLVP)) {
489 un->un_hash = cnp->cn_hash;
490 un->un_path = malloc(cnp->cn_namelen+1,
491 M_TEMP, M_WAITOK);
492 memcpy(un->un_path, cnp->cn_nameptr,
493 cnp->cn_namelen);
494 un->un_path[cnp->cn_namelen] = '\0';
495 VREF(dvp);
496 un->un_dirvp = dvp;
497 }
498 } else if (lowervp) {
499 vrele(lowervp);
500 }
501 *vpp = UNIONTOV(un);
502 return (0);
503 }
504
505 if (docache) {
506 /*
507 * otherwise lock the vp list while we call getnewvnode
508 * since that can block.
509 */
510 hash = UNION_HASH(uppervp, lowervp);
511
512 if (union_list_lock(hash))
513 goto loop;
514 }
515
516 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
517 if (error) {
518 if (uppervp) {
519 if (dvp == uppervp)
520 vrele(uppervp);
521 else
522 vput(uppervp);
523 }
524 if (lowervp)
525 vrele(lowervp);
526
527 goto out;
528 }
529
530 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
531 M_TEMP, M_WAITOK);
532
533 (*vpp)->v_flag |= vflag;
534 (*vpp)->v_vnlock = NULL; /* Make upper layers call VOP_LOCK */
535 if (uppervp)
536 (*vpp)->v_type = uppervp->v_type;
537 else
538 (*vpp)->v_type = lowervp->v_type;
539 un = VTOUNION(*vpp);
540 un->un_vnode = *vpp;
541 un->un_uppervp = uppervp;
542 un->un_uppersz = VNOVAL;
543 un->un_lowervp = lowervp;
544 un->un_lowersz = VNOVAL;
545 un->un_pvp = undvp;
546 if (undvp != NULLVP)
547 VREF(undvp);
548 un->un_dircache = 0;
549 un->un_openl = 0;
550 un->un_flags = UN_LOCKED;
551 if (un->un_uppervp)
552 un->un_flags |= UN_ULOCK;
553 #ifdef DIAGNOSTIC
554 if (curproc)
555 un->un_pid = curproc->p_pid;
556 else
557 un->un_pid = -1;
558 #endif
559 if (cnp && (lowervp != NULLVP)) {
560 un->un_hash = cnp->cn_hash;
561 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
562 memcpy(un->un_path, cnp->cn_nameptr, cnp->cn_namelen);
563 un->un_path[cnp->cn_namelen] = '\0';
564 VREF(dvp);
565 un->un_dirvp = dvp;
566 } else {
567 un->un_hash = 0;
568 un->un_path = 0;
569 un->un_dirvp = 0;
570 }
571
572 if (docache) {
573 LIST_INSERT_HEAD(&unhead[hash], un, un_cache);
574 un->un_flags |= UN_CACHED;
575 }
576
577 if (xlowervp)
578 vrele(xlowervp);
579
580 out:
581 if (docache)
582 union_list_unlock(hash);
583
584 return (error);
585 }
586
587 int
588 union_freevp(vp)
589 struct vnode *vp;
590 {
591 struct union_node *un = VTOUNION(vp);
592
593 if (un->un_flags & UN_CACHED) {
594 un->un_flags &= ~UN_CACHED;
595 LIST_REMOVE(un, un_cache);
596 }
597
598 if (un->un_pvp != NULLVP)
599 vrele(un->un_pvp);
600 if (un->un_uppervp != NULLVP)
601 vrele(un->un_uppervp);
602 if (un->un_lowervp != NULLVP)
603 vrele(un->un_lowervp);
604 if (un->un_dirvp != NULLVP)
605 vrele(un->un_dirvp);
606 if (un->un_path)
607 free(un->un_path, M_TEMP);
608
609 FREE(vp->v_data, M_TEMP);
610 vp->v_data = 0;
611
612 return (0);
613 }
614
615 /*
616 * copyfile. copy the vnode (fvp) to the vnode (tvp)
617 * using a sequence of reads and writes. both (fvp)
618 * and (tvp) are locked on entry and exit.
619 */
620 int
621 union_copyfile(fvp, tvp, cred, p)
622 struct vnode *fvp;
623 struct vnode *tvp;
624 struct ucred *cred;
625 struct proc *p;
626 {
627 char *buf;
628 struct uio uio;
629 struct iovec iov;
630 int error = 0;
631
632 /*
633 * strategy:
634 * allocate a buffer of size MAXBSIZE.
635 * loop doing reads and writes, keeping track
636 * of the current uio offset.
637 * give up at the first sign of trouble.
638 */
639
640 uio.uio_procp = p;
641 uio.uio_segflg = UIO_SYSSPACE;
642 uio.uio_offset = 0;
643
644 VOP_UNLOCK(fvp, 0); /* XXX */
645 VOP_LEASE(fvp, p, cred, LEASE_READ);
646 vn_lock(fvp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
647 VOP_UNLOCK(tvp, 0); /* XXX */
648 VOP_LEASE(tvp, p, cred, LEASE_WRITE);
649 vn_lock(tvp, LK_EXCLUSIVE | LK_RETRY); /* XXX */
650
651 buf = malloc(MAXBSIZE, M_TEMP, M_WAITOK);
652
653 /* ugly loop follows... */
654 do {
655 off_t offset = uio.uio_offset;
656
657 uio.uio_iov = &iov;
658 uio.uio_iovcnt = 1;
659 iov.iov_base = buf;
660 iov.iov_len = MAXBSIZE;
661 uio.uio_resid = iov.iov_len;
662 uio.uio_rw = UIO_READ;
663 error = VOP_READ(fvp, &uio, 0, cred);
664
665 if (error == 0) {
666 uio.uio_iov = &iov;
667 uio.uio_iovcnt = 1;
668 iov.iov_base = buf;
669 iov.iov_len = MAXBSIZE - uio.uio_resid;
670 uio.uio_offset = offset;
671 uio.uio_rw = UIO_WRITE;
672 uio.uio_resid = iov.iov_len;
673
674 if (uio.uio_resid == 0)
675 break;
676
677 do {
678 error = VOP_WRITE(tvp, &uio, 0, cred);
679 } while ((uio.uio_resid > 0) && (error == 0));
680 }
681
682 } while (error == 0);
683
684 free(buf, M_TEMP);
685 return (error);
686 }
687
688 /*
689 * (un) is assumed to be locked on entry and remains
690 * locked on exit.
691 */
692 int
693 union_copyup(un, docopy, cred, p)
694 struct union_node *un;
695 int docopy;
696 struct ucred *cred;
697 struct proc *p;
698 {
699 int error;
700 struct vnode *lvp, *uvp;
701 struct vattr lvattr, uvattr;
702
703 error = union_vn_create(&uvp, un, p);
704 if (error)
705 return (error);
706
707 /* at this point, uppervp is locked */
708 union_newupper(un, uvp);
709 un->un_flags |= UN_ULOCK;
710
711 lvp = un->un_lowervp;
712
713 if (docopy) {
714 /*
715 * XX - should not ignore errors
716 * from VOP_CLOSE
717 */
718 vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
719
720 error = VOP_GETATTR(lvp, &lvattr, cred, p);
721 if (error == 0)
722 error = VOP_OPEN(lvp, FREAD, cred, p);
723 if (error == 0) {
724 error = union_copyfile(lvp, uvp, cred, p);
725 (void) VOP_CLOSE(lvp, FREAD, cred, p);
726 }
727 if (error == 0) {
728 /* Copy permissions up too */
729 VATTR_NULL(&uvattr);
730 uvattr.va_mode = lvattr.va_mode;
731 uvattr.va_flags = lvattr.va_flags;
732 error = VOP_SETATTR(uvp, &uvattr, cred, p);
733 }
734 VOP_UNLOCK(lvp, 0);
735 #ifdef UNION_DIAGNOSTIC
736 if (error == 0)
737 uprintf("union: copied up %s\n", un->un_path);
738 #endif
739
740 }
741 union_vn_close(uvp, FWRITE, cred, p);
742
743 /*
744 * Subsequent IOs will go to the top layer, so
745 * call close on the lower vnode and open on the
746 * upper vnode to ensure that the filesystem keeps
747 * its references counts right. This doesn't do
748 * the right thing with (cred) and (FREAD) though.
749 * Ignoring error returns is not right, either.
750 */
751 if (error == 0) {
752 int i;
753
754 vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
755 for (i = 0; i < un->un_openl; i++) {
756 (void) VOP_CLOSE(lvp, FREAD, cred, p);
757 (void) VOP_OPEN(uvp, FREAD, cred, p);
758 }
759 un->un_openl = 0;
760 VOP_UNLOCK(lvp, 0);
761 }
762
763 return (error);
764
765 }
766
767 static int
768 union_relookup(um, dvp, vpp, cnp, cn, path, pathlen)
769 struct union_mount *um;
770 struct vnode *dvp;
771 struct vnode **vpp;
772 struct componentname *cnp;
773 struct componentname *cn;
774 const char *path;
775 int pathlen;
776 {
777 int error;
778
779 /*
780 * A new componentname structure must be faked up because
781 * there is no way to know where the upper level cnp came
782 * from or what it is being used for. This must duplicate
783 * some of the work done by NDINIT, some of the work done
784 * by namei, some of the work done by lookup and some of
785 * the work done by VOP_LOOKUP when given a CREATE flag.
786 * Conclusion: Horrible.
787 *
788 * The pathname buffer will be PNBUF_PUT'd by VOP_MKDIR.
789 */
790 cn->cn_namelen = pathlen;
791 if ((cn->cn_namelen + 1) > MAXPATHLEN)
792 return (ENAMETOOLONG);
793 cn->cn_pnbuf = PNBUF_GET();
794 memcpy(cn->cn_pnbuf, path, cn->cn_namelen);
795 cn->cn_pnbuf[cn->cn_namelen] = '\0';
796
797 cn->cn_nameiop = CREATE;
798 cn->cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
799 cn->cn_proc = cnp->cn_proc;
800 if (um->um_op == UNMNT_ABOVE)
801 cn->cn_cred = cnp->cn_cred;
802 else
803 cn->cn_cred = um->um_cred;
804 cn->cn_nameptr = cn->cn_pnbuf;
805 cn->cn_hash = cnp->cn_hash;
806 cn->cn_consume = cnp->cn_consume;
807
808 VREF(dvp);
809 error = relookup(dvp, vpp, cn);
810 if (!error)
811 vrele(dvp);
812 else {
813 PNBUF_PUT(cn->cn_pnbuf);
814 cn->cn_pnbuf = 0;
815 }
816
817 return (error);
818 }
819
820 /*
821 * Create a shadow directory in the upper layer.
822 * The new vnode is returned locked.
823 *
824 * (um) points to the union mount structure for access to the
825 * the mounting process's credentials.
826 * (dvp) is the directory in which to create the shadow directory.
827 * it is unlocked on entry and exit.
828 * (cnp) is the componentname to be created.
829 * (vpp) is the returned newly created shadow directory, which
830 * is returned locked.
831 *
832 * N.B. We still attempt to create shadow directories even if the union
833 * is mounted read-only, which is a little nonintuitive.
834 */
835 int
836 union_mkshadow(um, dvp, cnp, vpp)
837 struct union_mount *um;
838 struct vnode *dvp;
839 struct componentname *cnp;
840 struct vnode **vpp;
841 {
842 int error;
843 struct vattr va;
844 struct proc *p = cnp->cn_proc;
845 struct componentname cn;
846
847 error = union_relookup(um, dvp, vpp, cnp, &cn,
848 cnp->cn_nameptr, cnp->cn_namelen);
849 if (error)
850 return (error);
851
852 if (*vpp) {
853 VOP_ABORTOP(dvp, &cn);
854 VOP_UNLOCK(dvp, 0);
855 vrele(*vpp);
856 *vpp = NULLVP;
857 return (EEXIST);
858 }
859
860 /*
861 * policy: when creating the shadow directory in the
862 * upper layer, create it owned by the user who did
863 * the mount, group from parent directory, and mode
864 * 777 modified by umask (ie mostly identical to the
865 * mkdir syscall). (jsp, kb)
866 */
867
868 VATTR_NULL(&va);
869 va.va_type = VDIR;
870 va.va_mode = um->um_cmode;
871
872 /* VOP_LEASE: dvp is locked */
873 VOP_LEASE(dvp, p, cn.cn_cred, LEASE_WRITE);
874
875 error = VOP_MKDIR(dvp, vpp, &cn, &va);
876 return (error);
877 }
878
879 /*
880 * Create a whiteout entry in the upper layer.
881 *
882 * (um) points to the union mount structure for access to the
883 * the mounting process's credentials.
884 * (dvp) is the directory in which to create the whiteout.
885 * it is locked on entry and exit.
886 * (cnp) is the componentname to be created.
887 */
888 int
889 union_mkwhiteout(um, dvp, cnp, path)
890 struct union_mount *um;
891 struct vnode *dvp;
892 struct componentname *cnp;
893 char *path;
894 {
895 int error;
896 struct proc *p = cnp->cn_proc;
897 struct vnode *wvp;
898 struct componentname cn;
899
900 VOP_UNLOCK(dvp, 0);
901 error = union_relookup(um, dvp, &wvp, cnp, &cn, path, strlen(path));
902 if (error) {
903 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
904 return (error);
905 }
906
907 if (wvp) {
908 VOP_ABORTOP(dvp, &cn);
909 vrele(dvp);
910 vrele(wvp);
911 return (EEXIST);
912 }
913
914 /* VOP_LEASE: dvp is locked */
915 VOP_LEASE(dvp, p, p->p_ucred, LEASE_WRITE);
916
917 error = VOP_WHITEOUT(dvp, &cn, CREATE);
918 if (error)
919 VOP_ABORTOP(dvp, &cn);
920
921 vrele(dvp);
922
923 return (error);
924 }
925
926 /*
927 * union_vn_create: creates and opens a new shadow file
928 * on the upper union layer. this function is similar
929 * in spirit to calling vn_open but it avoids calling namei().
930 * the problem with calling namei is that a) it locks too many
931 * things, and b) it doesn't start at the "right" directory,
932 * whereas relookup is told where to start.
933 */
934 int
935 union_vn_create(vpp, un, p)
936 struct vnode **vpp;
937 struct union_node *un;
938 struct proc *p;
939 {
940 struct vnode *vp;
941 struct ucred *cred = p->p_ucred;
942 struct vattr vat;
943 struct vattr *vap = &vat;
944 int fmode = FFLAGS(O_WRONLY|O_CREAT|O_TRUNC|O_EXCL);
945 int error;
946 int cmode = UN_FILEMODE & ~p->p_cwdi->cwdi_cmask;
947 struct componentname cn;
948
949 *vpp = NULLVP;
950
951 /*
952 * Build a new componentname structure (for the same
953 * reasons outlines in union_mkshadow).
954 * The difference here is that the file is owned by
955 * the current user, rather than by the person who
956 * did the mount, since the current user needs to be
957 * able to write the file (that's why it is being
958 * copied in the first place).
959 */
960 cn.cn_namelen = strlen(un->un_path);
961 if ((cn.cn_namelen + 1) > MAXPATHLEN)
962 return (ENAMETOOLONG);
963 cn.cn_pnbuf = PNBUF_GET();
964 memcpy(cn.cn_pnbuf, un->un_path, cn.cn_namelen+1);
965 cn.cn_nameiop = CREATE;
966 cn.cn_flags = (LOCKPARENT|HASBUF|SAVENAME|SAVESTART|ISLASTCN);
967 cn.cn_proc = p;
968 cn.cn_cred = p->p_ucred;
969 cn.cn_nameptr = cn.cn_pnbuf;
970 cn.cn_hash = un->un_hash;
971 cn.cn_consume = 0;
972
973 VREF(un->un_dirvp);
974 if ((error = relookup(un->un_dirvp, &vp, &cn)) != 0)
975 return (error);
976 vrele(un->un_dirvp);
977
978 if (vp) {
979 VOP_ABORTOP(un->un_dirvp, &cn);
980 if (un->un_dirvp == vp)
981 vrele(un->un_dirvp);
982 else
983 vput(un->un_dirvp);
984 vrele(vp);
985 return (EEXIST);
986 }
987
988 /*
989 * Good - there was no race to create the file
990 * so go ahead and create it. The permissions
991 * on the file will be 0666 modified by the
992 * current user's umask. Access to the file, while
993 * it is unioned, will require access to the top *and*
994 * bottom files. Access when not unioned will simply
995 * require access to the top-level file.
996 * TODO: confirm choice of access permissions.
997 */
998 VATTR_NULL(vap);
999 vap->va_type = VREG;
1000 vap->va_mode = cmode;
1001 VOP_LEASE(un->un_dirvp, p, cred, LEASE_WRITE);
1002 if ((error = VOP_CREATE(un->un_dirvp, &vp, &cn, vap)) != 0)
1003 return (error);
1004
1005 if ((error = VOP_OPEN(vp, fmode, cred, p)) != 0) {
1006 vput(vp);
1007 return (error);
1008 }
1009
1010 vp->v_writecount++;
1011 *vpp = vp;
1012 return (0);
1013 }
1014
1015 int
1016 union_vn_close(vp, fmode, cred, p)
1017 struct vnode *vp;
1018 int fmode;
1019 struct ucred *cred;
1020 struct proc *p;
1021 {
1022
1023 if (fmode & FWRITE)
1024 --vp->v_writecount;
1025 return (VOP_CLOSE(vp, fmode, cred, p));
1026 }
1027
1028 void
1029 union_removed_upper(un)
1030 struct union_node *un;
1031 {
1032 #if 1
1033 /*
1034 * We do not set the uppervp to NULLVP here, because lowervp
1035 * may also be NULLVP, so this routine would end up creating
1036 * a bogus union node with no upper or lower VP (that causes
1037 * pain in many places that assume at least one VP exists).
1038 * Since we've removed this node from the cache hash chains,
1039 * it won't be found again. When all current holders
1040 * release it, union_inactive() will vgone() it.
1041 */
1042 union_diruncache(un);
1043 #else
1044 union_newupper(un, NULLVP);
1045 #endif
1046
1047 if (un->un_flags & UN_CACHED) {
1048 un->un_flags &= ~UN_CACHED;
1049 LIST_REMOVE(un, un_cache);
1050 }
1051
1052 if (un->un_flags & UN_ULOCK) {
1053 un->un_flags &= ~UN_ULOCK;
1054 VOP_UNLOCK(un->un_uppervp, 0);
1055 }
1056 }
1057
1058 #if 0
1059 struct vnode *
1060 union_lowervp(vp)
1061 struct vnode *vp;
1062 {
1063 struct union_node *un = VTOUNION(vp);
1064
1065 if ((un->un_lowervp != NULLVP) &&
1066 (vp->v_type == un->un_lowervp->v_type)) {
1067 if (vget(un->un_lowervp, 0) == 0)
1068 return (un->un_lowervp);
1069 }
1070
1071 return (NULLVP);
1072 }
1073 #endif
1074
1075 /*
1076 * determine whether a whiteout is needed
1077 * during a remove/rmdir operation.
1078 */
1079 int
1080 union_dowhiteout(un, cred, p)
1081 struct union_node *un;
1082 struct ucred *cred;
1083 struct proc *p;
1084 {
1085 struct vattr va;
1086
1087 if (un->un_lowervp != NULLVP)
1088 return (1);
1089
1090 if (VOP_GETATTR(un->un_uppervp, &va, cred, p) == 0 &&
1091 (va.va_flags & OPAQUE))
1092 return (1);
1093
1094 return (0);
1095 }
1096
1097 static void
1098 union_dircache_r(vp, vppp, cntp)
1099 struct vnode *vp;
1100 struct vnode ***vppp;
1101 int *cntp;
1102 {
1103 struct union_node *un;
1104
1105 if (vp->v_op != union_vnodeop_p) {
1106 if (vppp) {
1107 VREF(vp);
1108 *(*vppp)++ = vp;
1109 if (--(*cntp) == 0)
1110 panic("union: dircache table too small");
1111 } else {
1112 (*cntp)++;
1113 }
1114
1115 return;
1116 }
1117
1118 un = VTOUNION(vp);
1119 if (un->un_uppervp != NULLVP)
1120 union_dircache_r(un->un_uppervp, vppp, cntp);
1121 if (un->un_lowervp != NULLVP)
1122 union_dircache_r(un->un_lowervp, vppp, cntp);
1123 }
1124
1125 struct vnode *
1126 union_dircache(vp, p)
1127 struct vnode *vp;
1128 struct proc *p;
1129 {
1130 int cnt;
1131 struct vnode *nvp = NULLVP;
1132 struct vnode **vpp;
1133 struct vnode **dircache;
1134 int error;
1135
1136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1137 dircache = VTOUNION(vp)->un_dircache;
1138
1139 nvp = NULLVP;
1140
1141 if (dircache == 0) {
1142 cnt = 0;
1143 union_dircache_r(vp, 0, &cnt);
1144 cnt++;
1145 dircache = (struct vnode **)
1146 malloc(cnt * sizeof(struct vnode *),
1147 M_TEMP, M_WAITOK);
1148 vpp = dircache;
1149 union_dircache_r(vp, &vpp, &cnt);
1150 VTOUNION(vp)->un_dircache = dircache;
1151 *vpp = NULLVP;
1152 vpp = dircache + 1;
1153 } else {
1154 vpp = dircache;
1155 do {
1156 if (*vpp++ == VTOUNION(vp)->un_uppervp)
1157 break;
1158 } while (*vpp != NULLVP);
1159 }
1160
1161 if (*vpp == NULLVP)
1162 goto out;
1163
1164 vn_lock(*vpp, LK_EXCLUSIVE | LK_RETRY);
1165 VREF(*vpp);
1166 error = union_allocvp(&nvp, vp->v_mount, NULLVP, NULLVP, 0, *vpp, NULLVP, 0);
1167 if (!error) {
1168 VTOUNION(vp)->un_dircache = 0;
1169 VTOUNION(nvp)->un_dircache = dircache;
1170 }
1171
1172 out:
1173 VOP_UNLOCK(vp, 0);
1174 return (nvp);
1175 }
1176
1177 void
1178 union_diruncache(un)
1179 struct union_node *un;
1180 {
1181 struct vnode **vpp;
1182
1183 if (un->un_dircache != 0) {
1184 for (vpp = un->un_dircache; *vpp != NULLVP; vpp++)
1185 vrele(*vpp);
1186 free(un->un_dircache, M_TEMP);
1187 un->un_dircache = 0;
1188 }
1189 }
1190
1191 /*
1192 * This hook is called from vn_readdir() to switch to lower directory
1193 * entry after the upper directory is read.
1194 */
1195 int
1196 union_readdirhook(struct vnode **vpp, struct file *fp, struct proc *p)
1197 {
1198 struct vnode *vp = *vpp, *lvp;
1199 struct vattr va;
1200 int error;
1201
1202 if (vp->v_op != union_vnodeop_p)
1203 return (0);
1204
1205 if ((lvp = union_dircache(vp, p)) == NULLVP)
1206 return (0);
1207
1208 /*
1209 * If the directory is opaque,
1210 * then don't show lower entries
1211 */
1212 error = VOP_GETATTR(vp, &va, fp->f_cred, p);
1213 if (error || (va.va_flags & OPAQUE)) {
1214 vput(lvp);
1215 return (error);
1216 }
1217
1218 error = VOP_OPEN(lvp, FREAD, fp->f_cred, p);
1219 if (error) {
1220 vput(lvp);
1221 return (error);
1222 }
1223 VOP_UNLOCK(lvp, 0);
1224 fp->f_data = (caddr_t) lvp;
1225 fp->f_offset = 0;
1226 error = vn_close(vp, FREAD, fp->f_cred, p);
1227 if (error)
1228 return (error);
1229 *vpp = lvp;
1230 return (0);
1231 }
1232