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