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