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