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