add forward declaration of struct fpreg (like struct reg)
[unix-history] / usr / src / sys / miscfs / union / union_subr.c
CommitLineData
b991bc2d
JSP
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 * %sccs.include.redist.c%
10 *
79f80c71 11 * @(#)union_subr.c 1.4 (Berkeley) %G%
b991bc2d
JSP
12 */
13
14#include <sys/param.h>
15#include <sys/systm.h>
16#include <sys/time.h>
17#include <sys/kernel.h>
18#include <sys/vnode.h>
19#include <sys/namei.h>
20#include <sys/malloc.h>
21#include "union.h" /*<miscfs/union/union.h>*/
22
79f80c71
JSP
23#ifdef DIAGNOSTIC
24#include <sys/proc.h>
25#endif
26
b991bc2d
JSP
27static struct union_node *unhead;
28static int unvplock;
29
30int
31union_init()
32{
33
34 unhead = 0;
35 unvplock = 0;
36}
37
38/*
39 * allocate a union_node/vnode pair. the vnode is
01dac67e
JSP
40 * referenced and locked. the new vnode is returned
41 * via (vpp). (mp) is the mountpoint of the union filesystem,
42 * (dvp) is the parent directory where the upper layer object
43 * should exist (but doesn't) and (cnp) is the componentname
44 * information which is partially copied to allow the upper
45 * layer object to be created at a later time. (uppervp)
46 * and (lowervp) reference the upper and lower layer objects
47 * being mapped. either, but not both, can be nil.
b991bc2d
JSP
48 *
49 * all union_nodes are maintained on a singly-linked
50 * list. new nodes are only allocated when they cannot
51 * be found on this list. entries on the list are
52 * removed when the vfs reclaim entry is called.
53 *
54 * a single lock is kept for the entire list. this is
55 * needed because the getnewvnode() function can block
56 * waiting for a vnode to become free, in which case there
57 * may be more than one process trying to get the same
58 * vnode. this lock is only taken if we are going to
59 * call getnewvnode, since the kernel itself is single-threaded.
60 *
61 * if an entry is found on the list, then call vget() to
62 * take a reference. this is done because there may be
63 * zero references to it and so it needs to removed from
64 * the vnode free list.
65 */
66int
79f80c71 67union_allocvp(vpp, mp, undvp, dvp, cnp, uppervp, lowervp)
b991bc2d
JSP
68 struct vnode **vpp;
69 struct mount *mp;
79f80c71 70 struct vnode *undvp;
b991bc2d
JSP
71 struct vnode *dvp; /* may be null */
72 struct componentname *cnp; /* may be null */
73 struct vnode *uppervp; /* may be null */
74 struct vnode *lowervp; /* may be null */
75{
76 int error;
77 struct union_node *un;
78 struct union_node **pp;
01dac67e
JSP
79 struct vnode *xlowervp = 0;
80
81 if (uppervp == 0 && lowervp == 0)
82 panic("union: unidentifiable allocation");
83
84 if (uppervp && lowervp && (uppervp->v_type != lowervp->v_type)) {
85 xlowervp = lowervp;
86 lowervp = 0;
87 }
b991bc2d
JSP
88
89loop:
90 for (un = unhead; un != 0; un = un->un_next) {
91 if ((un->un_lowervp == lowervp ||
92 un->un_lowervp == 0) &&
93 (un->un_uppervp == uppervp ||
94 un->un_uppervp == 0) &&
95 (UNIONTOV(un)->v_mount == mp)) {
79f80c71 96 if (vget(UNIONTOV(un), 0))
b991bc2d 97 goto loop;
79f80c71
JSP
98 if (UNIONTOV(un) != undvp)
99 VOP_LOCK(UNIONTOV(un));
100 if (uppervp != un->un_uppervp) {
101 if (un->un_uppervp)
102 vrele(un->un_uppervp);
103 un->un_uppervp = uppervp;
104 }
105 if (lowervp != un->un_lowervp) {
106 if (un->un_lowervp)
107 vrele(un->un_lowervp);
108 un->un_lowervp = lowervp;
109 }
110 *vpp = UNIONTOV(un);
b991bc2d
JSP
111 return (0);
112 }
113 }
114
115 /*
116 * otherwise lock the vp list while we call getnewvnode
117 * since that can block.
118 */
119 if (unvplock & UN_LOCKED) {
120 unvplock |= UN_WANT;
121 sleep((caddr_t) &unvplock, PINOD);
122 goto loop;
123 }
124 unvplock |= UN_LOCKED;
125
126 error = getnewvnode(VT_UNION, mp, union_vnodeop_p, vpp);
127 if (error)
128 goto out;
129
130 MALLOC((*vpp)->v_data, void *, sizeof(struct union_node),
131 M_TEMP, M_WAITOK);
132
01dac67e
JSP
133 if (uppervp)
134 (*vpp)->v_type = uppervp->v_type;
135 else
136 (*vpp)->v_type = lowervp->v_type;
b991bc2d 137 un = VTOUNION(*vpp);
79f80c71 138 un->un_vnode = *vpp;
b991bc2d 139 un->un_next = 0;
b991bc2d
JSP
140 un->un_uppervp = uppervp;
141 un->un_lowervp = lowervp;
b991bc2d 142 un->un_flags = 0;
01dac67e 143 if (uppervp == 0 && cnp) {
b991bc2d
JSP
144 un->un_path = malloc(cnp->cn_namelen+1, M_TEMP, M_WAITOK);
145 bcopy(cnp->cn_nameptr, un->un_path, cnp->cn_namelen);
146 un->un_path[cnp->cn_namelen] = '\0';
01dac67e
JSP
147 VREF(dvp);
148 un->un_dirvp = dvp;
b991bc2d
JSP
149 } else {
150 un->un_path = 0;
01dac67e 151 un->un_dirvp = 0;
b991bc2d
JSP
152 }
153
b991bc2d
JSP
154 /* add to union vnode list */
155 for (pp = &unhead; *pp; pp = &(*pp)->un_next)
156 continue;
157 *pp = un;
158
79f80c71
JSP
159 un->un_flags |= UN_LOCKED;
160
161#ifdef DIAGNOSTIC
162 un->un_pid = curproc->p_pid;
163#endif
01dac67e
JSP
164
165 if (xlowervp)
166 vrele(xlowervp);
167
b991bc2d
JSP
168out:
169 unvplock &= ~UN_LOCKED;
170
171 if (unvplock & UN_WANT) {
172 unvplock &= ~UN_WANT;
173 wakeup((caddr_t) &unvplock);
174 }
175
b991bc2d
JSP
176 return (error);
177}
178
179int
180union_freevp(vp)
181 struct vnode *vp;
182{
183 struct union_node **unpp;
184 struct union_node *un = VTOUNION(vp);
185
186 for (unpp = &unhead; *unpp != 0; unpp = &(*unpp)->un_next) {
187 if (*unpp == un) {
188 *unpp = un->un_next;
189 break;
190 }
191 }
192
193 if (un->un_path)
194 FREE(un->un_path, M_TEMP);
195
196 FREE(vp->v_data, M_TEMP);
197 vp->v_data = 0;
198 return (0);
199}