umap_subr.c revision 1.13 1 1.13 fvdl /* $NetBSD: umap_subr.c,v 1.13 1998/03/01 02:21:51 fvdl Exp $ */
2 1.2 cgd
3 1.1 mycroft /*
4 1.13 fvdl * Copyright (c) 1992, 1993, 1995
5 1.1 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 mycroft *
7 1.1 mycroft * This code is derived from software donated to Berkeley by
8 1.1 mycroft * Jan-Simon Pendry.
9 1.1 mycroft *
10 1.1 mycroft * Redistribution and use in source and binary forms, with or without
11 1.1 mycroft * modification, are permitted provided that the following conditions
12 1.1 mycroft * are met:
13 1.1 mycroft * 1. Redistributions of source code must retain the above copyright
14 1.1 mycroft * notice, this list of conditions and the following disclaimer.
15 1.1 mycroft * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mycroft * notice, this list of conditions and the following disclaimer in the
17 1.1 mycroft * documentation and/or other materials provided with the distribution.
18 1.1 mycroft * 3. All advertising materials mentioning features or use of this software
19 1.1 mycroft * must display the following acknowledgement:
20 1.1 mycroft * This product includes software developed by the University of
21 1.1 mycroft * California, Berkeley and its contributors.
22 1.1 mycroft * 4. Neither the name of the University nor the names of its contributors
23 1.1 mycroft * may be used to endorse or promote products derived from this software
24 1.1 mycroft * without specific prior written permission.
25 1.1 mycroft *
26 1.1 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 mycroft * SUCH DAMAGE.
37 1.1 mycroft *
38 1.1 mycroft * from: Id: lofs_subr.c, v 1.11 1992/05/30 10:05:43 jsp Exp
39 1.13 fvdl * @(#)umap_subr.c 8.9 (Berkeley) 5/14/95
40 1.1 mycroft */
41 1.1 mycroft
42 1.1 mycroft #include <sys/param.h>
43 1.1 mycroft #include <sys/systm.h>
44 1.13 fvdl #include <sys/proc.h>
45 1.1 mycroft #include <sys/time.h>
46 1.1 mycroft #include <sys/types.h>
47 1.1 mycroft #include <sys/vnode.h>
48 1.1 mycroft #include <sys/mount.h>
49 1.1 mycroft #include <sys/namei.h>
50 1.1 mycroft #include <sys/malloc.h>
51 1.4 cgd #include <miscfs/specfs/specdev.h>
52 1.1 mycroft #include <miscfs/umapfs/umap.h>
53 1.1 mycroft
54 1.1 mycroft #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
55 1.1 mycroft #define NUMAPNODECACHE 16
56 1.1 mycroft
57 1.1 mycroft /*
58 1.1 mycroft * Null layer cache:
59 1.1 mycroft * Each cache entry holds a reference to the target vnode
60 1.1 mycroft * along with a pointer to the alias vnode. When an
61 1.1 mycroft * entry is added the target vnode is VREF'd. When the
62 1.1 mycroft * alias is removed the target vnode is vrele'd.
63 1.1 mycroft */
64 1.1 mycroft
65 1.3 mycroft #define UMAP_NHASH(vp) \
66 1.3 mycroft (&umap_node_hashtbl[(((u_long)vp)>>LOG2_SIZEVNODE) & umap_node_hash])
67 1.3 mycroft LIST_HEAD(umap_node_hashhead, umap_node) *umap_node_hashtbl;
68 1.3 mycroft u_long umap_node_hash;
69 1.1 mycroft
70 1.7 christos static u_long umap_findid __P((u_long, u_long [][2], int));
71 1.7 christos static struct vnode *umap_node_find __P((struct mount *, struct vnode *));
72 1.7 christos static int umap_node_alloc __P((struct mount *, struct vnode *,
73 1.7 christos struct vnode **));
74 1.7 christos
75 1.1 mycroft /*
76 1.1 mycroft * Initialise cache headers
77 1.1 mycroft */
78 1.7 christos void
79 1.1 mycroft umapfs_init()
80 1.1 mycroft {
81 1.3 mycroft
82 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
83 1.10 christos printf("umapfs_init\n"); /* printed during system boot */
84 1.1 mycroft #endif
85 1.12 chs umap_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, M_WAITOK, &umap_node_hash);
86 1.1 mycroft }
87 1.1 mycroft
88 1.1 mycroft /*
89 1.1 mycroft * umap_findid is called by various routines in umap_vnodeops.c to
90 1.1 mycroft * find a user or group id in a map.
91 1.1 mycroft */
92 1.1 mycroft static u_long
93 1.1 mycroft umap_findid(id, map, nentries)
94 1.1 mycroft u_long id;
95 1.1 mycroft u_long map[][2];
96 1.1 mycroft int nentries;
97 1.1 mycroft {
98 1.1 mycroft int i;
99 1.1 mycroft
100 1.1 mycroft /* Find uid entry in map */
101 1.1 mycroft i = 0;
102 1.1 mycroft while ((i<nentries) && ((map[i][0]) != id))
103 1.1 mycroft i++;
104 1.1 mycroft
105 1.1 mycroft if (i < nentries)
106 1.1 mycroft return (map[i][1]);
107 1.1 mycroft else
108 1.1 mycroft return (-1);
109 1.1 mycroft
110 1.1 mycroft }
111 1.1 mycroft
112 1.1 mycroft /*
113 1.1 mycroft * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
114 1.1 mycroft * find a user or group id in a map, in reverse.
115 1.1 mycroft */
116 1.1 mycroft u_long
117 1.1 mycroft umap_reverse_findid(id, map, nentries)
118 1.1 mycroft u_long id;
119 1.1 mycroft u_long map[][2];
120 1.1 mycroft int nentries;
121 1.1 mycroft {
122 1.1 mycroft int i;
123 1.1 mycroft
124 1.1 mycroft /* Find uid entry in map */
125 1.1 mycroft i = 0;
126 1.1 mycroft while ((i<nentries) && ((map[i][1]) != id))
127 1.1 mycroft i++;
128 1.1 mycroft
129 1.1 mycroft if (i < nentries)
130 1.1 mycroft return (map[i][0]);
131 1.1 mycroft else
132 1.1 mycroft return (-1);
133 1.1 mycroft
134 1.1 mycroft }
135 1.1 mycroft
136 1.1 mycroft /*
137 1.1 mycroft * Return alias for target vnode if already exists, else 0.
138 1.1 mycroft */
139 1.1 mycroft static struct vnode *
140 1.1 mycroft umap_node_find(mp, targetvp)
141 1.1 mycroft struct mount *mp;
142 1.1 mycroft struct vnode *targetvp;
143 1.1 mycroft {
144 1.3 mycroft struct umap_node_hashhead *hd;
145 1.1 mycroft struct umap_node *a;
146 1.1 mycroft struct vnode *vp;
147 1.1 mycroft
148 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
149 1.11 christos printf("umap_node_find(mp = %p, target = %p)\n", mp, targetvp);
150 1.1 mycroft #endif
151 1.1 mycroft
152 1.1 mycroft /*
153 1.1 mycroft * Find hash base, and then search the (two-way) linked
154 1.1 mycroft * list looking for a umap_node structure which is referencing
155 1.1 mycroft * the target vnode. If found, the increment the umap_node
156 1.1 mycroft * reference count (but NOT the target vnode's VREF counter).
157 1.1 mycroft */
158 1.3 mycroft hd = UMAP_NHASH(targetvp);
159 1.3 mycroft loop:
160 1.3 mycroft for (a = hd->lh_first; a != 0; a = a->umap_hash.le_next) {
161 1.1 mycroft if (a->umap_lowervp == targetvp &&
162 1.1 mycroft a->umap_vnode->v_mount == mp) {
163 1.1 mycroft vp = UMAPTOV(a);
164 1.1 mycroft /*
165 1.1 mycroft * We need vget for the VXLOCK
166 1.1 mycroft * stuff, but we don't want to lock
167 1.1 mycroft * the lower node.
168 1.1 mycroft */
169 1.1 mycroft if (vget(vp, 0)) {
170 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
171 1.10 christos printf ("umap_node_find: vget failed.\n");
172 1.1 mycroft #endif
173 1.1 mycroft goto loop;
174 1.1 mycroft }
175 1.1 mycroft return (vp);
176 1.1 mycroft }
177 1.1 mycroft }
178 1.1 mycroft
179 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
180 1.11 christos printf("umap_node_find(%p, %p): NOT found\n", mp, targetvp);
181 1.1 mycroft #endif
182 1.1 mycroft
183 1.1 mycroft return (0);
184 1.1 mycroft }
185 1.1 mycroft
186 1.1 mycroft /*
187 1.1 mycroft * Make a new umap_node node.
188 1.4 cgd * Vp is the alias vnode, lowervp is the target vnode.
189 1.4 cgd * Maintain a reference to lowervp.
190 1.1 mycroft */
191 1.1 mycroft static int
192 1.1 mycroft umap_node_alloc(mp, lowervp, vpp)
193 1.1 mycroft struct mount *mp;
194 1.1 mycroft struct vnode *lowervp;
195 1.1 mycroft struct vnode **vpp;
196 1.1 mycroft {
197 1.3 mycroft struct umap_node_hashhead *hd;
198 1.1 mycroft struct umap_node *xp;
199 1.4 cgd struct vnode *vp, *nvp;
200 1.13 fvdl struct proc *p = curproc; /* XXX */
201 1.1 mycroft int error;
202 1.7 christos extern int (**dead_vnodeop_p) __P((void *));
203 1.1 mycroft
204 1.7 christos if ((error = getnewvnode(VT_UMAP, mp, umap_vnodeop_p, &vp)) != 0)
205 1.1 mycroft return (error);
206 1.4 cgd vp->v_type = lowervp->v_type;
207 1.4 cgd
208 1.4 cgd MALLOC(xp, struct umap_node *, sizeof(struct umap_node), M_TEMP,
209 1.4 cgd M_WAITOK);
210 1.4 cgd if (vp->v_type == VBLK || vp->v_type == VCHR) {
211 1.4 cgd MALLOC(vp->v_specinfo, struct specinfo *,
212 1.4 cgd sizeof(struct specinfo), M_VNODE, M_WAITOK);
213 1.4 cgd vp->v_rdev = lowervp->v_rdev;
214 1.4 cgd }
215 1.1 mycroft
216 1.4 cgd vp->v_data = xp;
217 1.1 mycroft xp->umap_vnode = vp;
218 1.1 mycroft xp->umap_lowervp = lowervp;
219 1.1 mycroft /*
220 1.1 mycroft * Before we insert our new node onto the hash chains,
221 1.1 mycroft * check to see if someone else has beaten us to it.
222 1.1 mycroft * (We could have slept in MALLOC.)
223 1.1 mycroft */
224 1.7 christos if ((nvp = umap_node_find(mp, lowervp)) != NULL) {
225 1.4 cgd *vpp = nvp;
226 1.4 cgd
227 1.4 cgd /* free the substructures we've allocated. */
228 1.1 mycroft FREE(xp, M_TEMP);
229 1.4 cgd if (vp->v_type == VBLK || vp->v_type == VCHR)
230 1.4 cgd FREE(vp->v_specinfo, M_VNODE);
231 1.4 cgd
232 1.4 cgd vp->v_type = VBAD; /* node is discarded */
233 1.4 cgd vp->v_op = dead_vnodeop_p; /* so ops will still work */
234 1.4 cgd vrele(vp); /* get rid of it. */
235 1.1 mycroft return (0);
236 1.1 mycroft }
237 1.4 cgd
238 1.4 cgd /*
239 1.4 cgd * XXX if it's a device node, it needs to be checkalias()ed.
240 1.4 cgd * however, for locking reasons, that's just not possible.
241 1.4 cgd * so we have to do most of the dirty work inline. Note that
242 1.4 cgd * this is a limited case; we know that there's going to be
243 1.4 cgd * an alias, and we know that that alias will be a "real"
244 1.4 cgd * device node, i.e. not tagged VT_NON.
245 1.4 cgd */
246 1.4 cgd if (vp->v_type == VBLK || vp->v_type == VCHR) {
247 1.4 cgd struct vnode *cvp, **cvpp;
248 1.4 cgd
249 1.4 cgd cvpp = &speclisth[SPECHASH(vp->v_rdev)];
250 1.4 cgd loop:
251 1.13 fvdl simple_lock(&spechash_slock);
252 1.4 cgd for (cvp = *cvpp; cvp; cvp = cvp->v_specnext) {
253 1.4 cgd if (vp->v_rdev != cvp->v_rdev ||
254 1.4 cgd vp->v_type != cvp->v_type)
255 1.4 cgd continue;
256 1.4 cgd
257 1.4 cgd /*
258 1.4 cgd * Alias, but not in use, so flush it out.
259 1.4 cgd */
260 1.13 fvdl simple_lock(&cvp->v_interlock);
261 1.4 cgd if (cvp->v_usecount == 0) {
262 1.13 fvdl simple_unlock(&spechash_slock);
263 1.13 fvdl vgonel(cvp, p);
264 1.4 cgd goto loop;
265 1.4 cgd }
266 1.13 fvdl if (vget(cvp, LK_EXCLUSIVE | LK_INTERLOCK)) {
267 1.13 fvdl simple_unlock(&spechash_slock);
268 1.4 cgd goto loop;
269 1.13 fvdl }
270 1.4 cgd break;
271 1.4 cgd }
272 1.4 cgd
273 1.4 cgd vp->v_hashchain = cvpp;
274 1.4 cgd vp->v_specnext = *cvpp;
275 1.4 cgd vp->v_specflags = 0;
276 1.4 cgd *cvpp = vp;
277 1.4 cgd #ifdef DIAGNOSTIC
278 1.4 cgd if (cvp == NULLVP)
279 1.4 cgd panic("umap_node_alloc: no alias for device");
280 1.4 cgd #endif
281 1.4 cgd vp->v_flag |= VALIASED;
282 1.4 cgd cvp->v_flag |= VALIASED;
283 1.13 fvdl simple_unlock(&spechash_slock);
284 1.4 cgd vrele(cvp);
285 1.4 cgd }
286 1.4 cgd /* XXX end of transmogrified checkalias() */
287 1.4 cgd
288 1.4 cgd *vpp = vp;
289 1.4 cgd VREF(lowervp); /* Extra VREF will be vrele'd in umap_node_create */
290 1.3 mycroft hd = UMAP_NHASH(lowervp);
291 1.3 mycroft LIST_INSERT_HEAD(hd, xp, umap_hash);
292 1.1 mycroft return (0);
293 1.1 mycroft }
294 1.1 mycroft
295 1.1 mycroft
296 1.1 mycroft /*
297 1.1 mycroft * Try to find an existing umap_node vnode refering
298 1.1 mycroft * to it, otherwise make a new umap_node vnode which
299 1.1 mycroft * contains a reference to the target vnode.
300 1.1 mycroft */
301 1.1 mycroft int
302 1.1 mycroft umap_node_create(mp, targetvp, newvpp)
303 1.1 mycroft struct mount *mp;
304 1.1 mycroft struct vnode *targetvp;
305 1.1 mycroft struct vnode **newvpp;
306 1.1 mycroft {
307 1.1 mycroft struct vnode *aliasvp;
308 1.1 mycroft
309 1.7 christos if ((aliasvp = umap_node_find(mp, targetvp)) != NULL) {
310 1.1 mycroft /*
311 1.1 mycroft * Take another reference to the alias vnode
312 1.1 mycroft */
313 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
314 1.11 christos vprint("umap_node_create: exists", aliasvp);
315 1.1 mycroft #endif
316 1.1 mycroft /* VREF(aliasvp); */
317 1.1 mycroft } else {
318 1.1 mycroft int error;
319 1.1 mycroft
320 1.1 mycroft /*
321 1.1 mycroft * Get new vnode.
322 1.1 mycroft */
323 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
324 1.10 christos printf("umap_node_create: create new alias vnode\n");
325 1.1 mycroft #endif
326 1.1 mycroft /*
327 1.1 mycroft * Make new vnode reference the umap_node.
328 1.1 mycroft */
329 1.7 christos if ((error = umap_node_alloc(mp, targetvp, &aliasvp)) != 0)
330 1.1 mycroft return (error);
331 1.1 mycroft
332 1.1 mycroft /*
333 1.1 mycroft * aliasvp is already VREF'd by getnewvnode()
334 1.1 mycroft */
335 1.1 mycroft }
336 1.1 mycroft
337 1.1 mycroft vrele(targetvp);
338 1.1 mycroft
339 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
340 1.1 mycroft vprint("umap_node_create: alias", aliasvp);
341 1.1 mycroft vprint("umap_node_create: target", targetvp);
342 1.1 mycroft #endif
343 1.1 mycroft
344 1.1 mycroft *newvpp = aliasvp;
345 1.1 mycroft return (0);
346 1.1 mycroft }
347 1.1 mycroft
348 1.1 mycroft #ifdef UMAPFS_DIAGNOSTIC
349 1.1 mycroft int umap_checkvp_barrier = 1;
350 1.1 mycroft struct vnode *
351 1.1 mycroft umap_checkvp(vp, fil, lno)
352 1.1 mycroft struct vnode *vp;
353 1.1 mycroft char *fil;
354 1.1 mycroft int lno;
355 1.1 mycroft {
356 1.1 mycroft struct umap_node *a = VTOUMAP(vp);
357 1.1 mycroft #if 0
358 1.1 mycroft /*
359 1.1 mycroft * Can't do this check because vop_reclaim runs
360 1.1 mycroft * with funny vop vector.
361 1.1 mycroft */
362 1.1 mycroft if (vp->v_op != umap_vnodeop_p) {
363 1.10 christos printf("umap_checkvp: on non-umap-node\n");
364 1.1 mycroft while (umap_checkvp_barrier) /*WAIT*/ ;
365 1.1 mycroft panic("umap_checkvp");
366 1.1 mycroft }
367 1.1 mycroft #endif
368 1.1 mycroft if (a->umap_lowervp == NULL) {
369 1.1 mycroft /* Should never happen */
370 1.1 mycroft int i; u_long *p;
371 1.11 christos printf("vp = %p, ZERO ptr\n", vp);
372 1.1 mycroft for (p = (u_long *) a, i = 0; i < 8; i++)
373 1.11 christos printf(" %lx", p[i]);
374 1.10 christos printf("\n");
375 1.1 mycroft /* wait for debugger */
376 1.1 mycroft while (umap_checkvp_barrier) /*WAIT*/ ;
377 1.1 mycroft panic("umap_checkvp");
378 1.1 mycroft }
379 1.1 mycroft if (a->umap_lowervp->v_usecount < 1) {
380 1.1 mycroft int i; u_long *p;
381 1.11 christos printf("vp = %p, unref'ed lowervp\n", vp);
382 1.1 mycroft for (p = (u_long *) a, i = 0; i < 8; i++)
383 1.11 christos printf(" %lx", p[i]);
384 1.10 christos printf("\n");
385 1.1 mycroft /* wait for debugger */
386 1.1 mycroft while (umap_checkvp_barrier) /*WAIT*/ ;
387 1.1 mycroft panic ("umap with unref'ed lowervp");
388 1.1 mycroft }
389 1.1 mycroft #if 0
390 1.11 christos printf("umap %p/%d -> %p/%d [%s, %d]\n",
391 1.1 mycroft a->umap_vnode, a->umap_vnode->v_usecount,
392 1.1 mycroft a->umap_lowervp, a->umap_lowervp->v_usecount,
393 1.1 mycroft fil, lno);
394 1.1 mycroft #endif
395 1.1 mycroft return (a->umap_lowervp);
396 1.1 mycroft }
397 1.1 mycroft #endif
398 1.1 mycroft
399 1.1 mycroft /* umap_mapids maps all of the ids in a credential, both user and group. */
400 1.1 mycroft
401 1.1 mycroft void
402 1.1 mycroft umap_mapids(v_mount, credp)
403 1.1 mycroft struct mount *v_mount;
404 1.1 mycroft struct ucred *credp;
405 1.1 mycroft {
406 1.1 mycroft int i, unentries, gnentries;
407 1.5 cgd uid_t uid;
408 1.5 cgd gid_t gid;
409 1.7 christos u_long (*usermap)[2], (*groupmap)[2];
410 1.8 thorpej
411 1.8 thorpej if (credp == NOCRED)
412 1.8 thorpej return;
413 1.1 mycroft
414 1.1 mycroft unentries = MOUNTTOUMAPMOUNT(v_mount)->info_nentries;
415 1.7 christos usermap = MOUNTTOUMAPMOUNT(v_mount)->info_mapdata;
416 1.1 mycroft gnentries = MOUNTTOUMAPMOUNT(v_mount)->info_gnentries;
417 1.7 christos groupmap = MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata;
418 1.1 mycroft
419 1.1 mycroft /* Find uid entry in map */
420 1.1 mycroft
421 1.1 mycroft uid = (uid_t) umap_findid(credp->cr_uid, usermap, unentries);
422 1.1 mycroft
423 1.1 mycroft if (uid != -1)
424 1.1 mycroft credp->cr_uid = uid;
425 1.1 mycroft else
426 1.1 mycroft credp->cr_uid = (uid_t) NOBODY;
427 1.1 mycroft
428 1.6 jtc #if 1
429 1.6 jtc /* cr_gid is the same as cr_groups[0] in 4BSD, but not in NetBSD */
430 1.1 mycroft
431 1.1 mycroft /* Find gid entry in map */
432 1.1 mycroft
433 1.1 mycroft gid = (gid_t) umap_findid(credp->cr_gid, groupmap, gnentries);
434 1.1 mycroft
435 1.1 mycroft if (gid != -1)
436 1.1 mycroft credp->cr_gid = gid;
437 1.1 mycroft else
438 1.1 mycroft credp->cr_gid = NULLGROUP;
439 1.1 mycroft #endif
440 1.1 mycroft
441 1.1 mycroft /* Now we must map each of the set of groups in the cr_groups
442 1.1 mycroft structure. */
443 1.1 mycroft
444 1.1 mycroft i = 0;
445 1.1 mycroft while (credp->cr_groups[i] != 0) {
446 1.1 mycroft gid = (gid_t) umap_findid(credp->cr_groups[i],
447 1.7 christos groupmap, gnentries);
448 1.1 mycroft
449 1.1 mycroft if (gid != -1)
450 1.1 mycroft credp->cr_groups[i++] = gid;
451 1.1 mycroft else
452 1.1 mycroft credp->cr_groups[i++] = NULLGROUP;
453 1.1 mycroft }
454 1.1 mycroft }
455