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