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