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