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