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