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