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