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