xref: /netbsd-src/sys/miscfs/umapfs/umap_vnops.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1 /*	$NetBSD: umap_vnops.c,v 1.4 1995/04/15 01:57:39 cgd Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software donated to Berkeley by
8  * the UCLA Ficus project.
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  *	@(#)umap_vnops.c	8.3 (Berkeley) 1/5/94
39  */
40 
41 /*
42  * Umap Layer
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/time.h>
48 #include <sys/types.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
53 #include <sys/buf.h>
54 #include <miscfs/umapfs/umap.h>
55 
56 
57 int umap_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
58 
59 /*
60  * This is the 10-Apr-92 bypass routine.
61  * See null_vnops.c:null_bypass for more details.
62  */
63 int
64 umap_bypass(ap)
65 	struct vop_generic_args /* {
66 		struct vnodeop_desc *a_desc;
67 		<other random data follows, presumably>
68 	} */ *ap;
69 {
70 	extern int (**umap_vnodeop_p)();  /* not extern, really "forward" */
71 	struct ucred **credpp = 0, *credp = 0;
72 	struct ucred *savecredp, *savecompcredp = 0;
73 	struct ucred *compcredp = 0;
74 	struct vnode **this_vp_p;
75 	int error;
76 	struct vnode *old_vps[VDESC_MAX_VPS];
77 	struct vnode *vp1 = 0;
78 	struct vnode **vps_p[VDESC_MAX_VPS];
79 	struct vnode ***vppp;
80 	struct vnodeop_desc *descp = ap->a_desc;
81 	int reles, i;
82 	struct componentname **compnamepp = 0;
83 
84 	if (umap_bug_bypass)
85 		printf ("umap_bypass: %s\n", descp->vdesc_name);
86 
87 #ifdef SAFETY
88 	/*
89 	 * We require at least one vp.
90 	 */
91 	if (descp->vdesc_vp_offsets == NULL ||
92 	    descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
93 		panic ("umap_bypass: no vp's in map.\n");
94 #endif
95 
96 	/*
97 	 * Map the vnodes going in.
98 	 * Later, we'll invoke the operation based on
99 	 * the first mapped vnode's operation vector.
100 	 */
101 	reles = descp->vdesc_flags;
102 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
103 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
104 			break;   /* bail out at end of list */
105 		vps_p[i] = this_vp_p =
106 			VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap);
107 
108 		if (i == 0) {
109 			vp1 = *vps_p[0];
110 		}
111 
112 		/*
113 		 * We're not guaranteed that any but the first vnode
114 		 * are of our type.  Check for and don't map any
115 		 * that aren't.  (Must map first vp or vclean fails.)
116 		 */
117 
118 		if (i && (*this_vp_p)->v_op != umap_vnodeop_p) {
119 			old_vps[i] = NULL;
120 		} else {
121 			old_vps[i] = *this_vp_p;
122 			*(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
123 			if (reles & 1)
124 				VREF(*this_vp_p);
125 		}
126 
127 	}
128 
129 	/*
130 	 * Fix the credentials.  (That's the purpose of this layer.)
131 	 */
132 
133 	if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
134 
135 		credpp = VOPARG_OFFSETTO(struct ucred**,
136 		    descp->vdesc_cred_offset, ap);
137 
138 		/* Save old values */
139 
140 		savecredp = *credpp;
141 		if (savecredp != NOCRED)
142 			*credpp = crdup(savecredp);
143 		credp = *credpp;
144 
145 		if (umap_bug_bypass && credp->cr_uid != 0)
146 			printf("umap_bypass: user was %d, group %d\n",
147 			    credp->cr_uid, credp->cr_gid);
148 
149 		/* Map all ids in the credential structure. */
150 
151 		umap_mapids(vp1->v_mount, credp);
152 
153 		if (umap_bug_bypass && credp->cr_uid != 0)
154 			printf("umap_bypass: user now %d, group %d\n",
155 			    credp->cr_uid, credp->cr_gid);
156 	}
157 
158 	/* BSD often keeps a credential in the componentname structure
159 	 * for speed.  If there is one, it better get mapped, too.
160 	 */
161 
162 	if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
163 
164 		compnamepp = VOPARG_OFFSETTO(struct componentname**,
165 		    descp->vdesc_componentname_offset, ap);
166 
167 		savecompcredp = (*compnamepp)->cn_cred;
168 		if (savecompcredp != NOCRED)
169 			(*compnamepp)->cn_cred = crdup(savecompcredp);
170 		compcredp = (*compnamepp)->cn_cred;
171 
172 		if (umap_bug_bypass && compcredp->cr_uid != 0)
173 			printf("umap_bypass: component credit user was %d, group %d\n",
174 			    compcredp->cr_uid, compcredp->cr_gid);
175 
176 		/* Map all ids in the credential structure. */
177 
178 		umap_mapids(vp1->v_mount, compcredp);
179 
180 		if (umap_bug_bypass && compcredp->cr_uid != 0)
181 			printf("umap_bypass: component credit user now %d, group %d\n",
182 			    compcredp->cr_uid, compcredp->cr_gid);
183 	}
184 
185 	/*
186 	 * Call the operation on the lower layer
187 	 * with the modified argument structure.
188 	 */
189 	error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
190 
191 	/*
192 	 * Maintain the illusion of call-by-value
193 	 * by restoring vnodes in the argument structure
194 	 * to their original value.
195 	 */
196 	reles = descp->vdesc_flags;
197 	for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
198 		if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
199 			break;   /* bail out at end of list */
200 		if (old_vps[i]) {
201 			*(vps_p[i]) = old_vps[i];
202 			if (reles & 1)
203 				vrele(*(vps_p[i]));
204 		};
205 	};
206 
207 	/*
208 	 * Map the possible out-going vpp
209 	 * (Assumes that the lower layer always returns
210 	 * a VREF'ed vpp unless it gets an error.)
211 	 */
212 	if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
213 	    !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
214 	    !error) {
215 		if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
216 			goto out;
217 		vppp = VOPARG_OFFSETTO(struct vnode***,
218 				 descp->vdesc_vpp_offset, ap);
219 		error = umap_node_create(old_vps[0]->v_mount, **vppp, *vppp);
220 	};
221 
222  out:
223 	/*
224 	 * Free duplicate cred structure and restore old one.
225 	 */
226 	if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
227 		if (umap_bug_bypass && credp && credp->cr_uid != 0)
228 			printf("umap_bypass: returning-user was %d\n",
229 					credp->cr_uid);
230 
231 		if (savecredp != NOCRED) {
232 			crfree(credp);
233 			*credpp = savecredp;
234 			if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
235 			 	printf("umap_bypass: returning-user now %d\n\n",
236 				    savecredp->cr_uid);
237 		}
238 	}
239 
240 	if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
241 		if (umap_bug_bypass && compcredp && compcredp->cr_uid != 0)
242 			printf("umap_bypass: returning-component-user was %d\n",
243 				compcredp->cr_uid);
244 
245 		if (savecompcredp != NOCRED) {
246 			crfree(compcredp);
247 			(*compnamepp)->cn_cred = savecompcredp;
248 			if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
249 			 	printf("umap_bypass: returning-component-user now %d\n",
250 				    savecompcredp->cr_uid);
251 		}
252 	}
253 
254 	return (error);
255 }
256 
257 
258 /*
259  *  We handle getattr to change the fsid.
260  */
261 int
262 umap_getattr(ap)
263 	struct vop_getattr_args /* {
264 		struct vnode *a_vp;
265 		struct vattr *a_vap;
266 		struct ucred *a_cred;
267 		struct proc *a_p;
268 	} */ *ap;
269 {
270 	uid_t uid;
271 	gid_t gid;
272 	int error, tmpid, nentries, gnentries;
273 	u_long (*mapdata)[2];
274 	u_long (*gmapdata)[2];
275 	struct vnode **vp1p;
276 	struct vnodeop_desc *descp = ap->a_desc;
277 
278 	if (error = umap_bypass(ap))
279 		return (error);
280 	/* Requires that arguments be restored. */
281 	ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
282 
283 	/*
284 	 * Umap needs to map the uid and gid returned by a stat
285 	 * into the proper values for this site.  This involves
286 	 * finding the returned uid in the mapping information,
287 	 * translating it into the uid on the other end,
288 	 * and filling in the proper field in the vattr
289 	 * structure pointed to by ap->a_vap.  The group
290 	 * is easier, since currently all groups will be
291 	 * translate to the NULLGROUP.
292 	 */
293 
294 	/* Find entry in map */
295 
296 	uid = ap->a_vap->va_uid;
297 	gid = ap->a_vap->va_gid;
298 	if (umap_bug_bypass)
299 		printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
300 		    gid);
301 
302 	vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
303 	nentries =  MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
304 	mapdata =  (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
305 	gnentries =  MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
306 	gmapdata =  (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
307 
308 	/* Reverse map the uid for the vnode.  Since it's a reverse
309 		map, we can't use umap_mapids() to do it. */
310 
311 	tmpid = umap_reverse_findid(uid, mapdata, nentries);
312 
313 	if (tmpid != -1) {
314 		ap->a_vap->va_uid = (uid_t) tmpid;
315 		if (umap_bug_bypass)
316 			printf("umap_getattr: original uid = %d\n", uid);
317 	} else
318 		ap->a_vap->va_uid = (uid_t) NOBODY;
319 
320 	/* Reverse map the gid for the vnode. */
321 
322 	tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
323 
324 	if (tmpid != -1) {
325 		ap->a_vap->va_gid = (gid_t) tmpid;
326 		if (umap_bug_bypass)
327 			printf("umap_getattr: original gid = %d\n", gid);
328 	} else
329 		ap->a_vap->va_gid = (gid_t) NULLGROUP;
330 
331 	return (0);
332 }
333 
334 int
335 umap_inactive(ap)
336 	struct vop_inactive_args /* {
337 		struct vnode *a_vp;
338 	} */ *ap;
339 {
340 	/*
341 	 * Do nothing (and _don't_ bypass).
342 	 * Wait to vrele lowervp until reclaim,
343 	 * so that until then our umap_node is in the
344 	 * cache and reusable.
345 	 *
346 	 */
347 	return (0);
348 }
349 
350 int
351 umap_reclaim(ap)
352 	struct vop_reclaim_args /* {
353 		struct vnode *a_vp;
354 	} */ *ap;
355 {
356 	struct vnode *vp = ap->a_vp;
357 	struct umap_node *xp = VTOUMAP(vp);
358 	struct vnode *lowervp = xp->umap_lowervp;
359 
360 	/* After this assignment, this node will not be re-used. */
361 	xp->umap_lowervp = NULL;
362 	LIST_REMOVE(xp, umap_hash);
363 	FREE(vp->v_data, M_TEMP);
364 	vp->v_data = NULL;
365 	vrele(lowervp);
366 	return (0);
367 }
368 
369 int
370 umap_strategy(ap)
371 	struct vop_strategy_args /* {
372 		struct buf *a_bp;
373 	} */ *ap;
374 {
375 	struct buf *bp = ap->a_bp;
376 	int error;
377 	struct vnode *savedvp;
378 
379 	savedvp = bp->b_vp;
380 	bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp);
381 
382 	error = VOP_STRATEGY(ap->a_bp);
383 
384 	bp->b_vp = savedvp;
385 
386 	return (error);
387 }
388 
389 int
390 umap_bwrite(ap)
391 	struct vop_bwrite_args /* {
392 		struct buf *a_bp;
393 	} */ *ap;
394 {
395 	struct buf *bp = ap->a_bp;
396 	int error;
397 	struct vnode *savedvp;
398 
399 	savedvp = bp->b_vp;
400 	bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp);
401 
402 	error = VOP_BWRITE(ap->a_bp);
403 
404 	bp->b_vp = savedvp;
405 
406 	return (error);
407 }
408 
409 
410 int
411 umap_print(ap)
412 	struct vop_print_args /* {
413 		struct vnode *a_vp;
414 	} */ *ap;
415 {
416 	struct vnode *vp = ap->a_vp;
417 	printf("\ttag VT_UMAPFS, vp=%x, lowervp=%x\n", vp, UMAPVPTOLOWERVP(vp));
418 	return (0);
419 }
420 
421 int
422 umap_rename(ap)
423 	struct vop_rename_args  /* {
424 		struct vnode *a_fdvp;
425 		struct vnode *a_fvp;
426 		struct componentname *a_fcnp;
427 		struct vnode *a_tdvp;
428 		struct vnode *a_tvp;
429 		struct componentname *a_tcnp;
430 	} */ *ap;
431 {
432 	int error;
433 	struct componentname *compnamep;
434 	struct ucred *compcredp, *savecompcredp;
435 	struct vnode *vp;
436 
437 	/*
438 	 * Rename is irregular, having two componentname structures.
439 	 * We need to map the cre in the second structure,
440 	 * and then bypass takes care of the rest.
441 	 */
442 
443 	vp = ap->a_fdvp;
444 	compnamep = ap->a_tcnp;
445 	compcredp = compnamep->cn_cred;
446 
447 	savecompcredp = compcredp;
448 	compcredp = compnamep->cn_cred = crdup(savecompcredp);
449 
450 	if (umap_bug_bypass && compcredp->cr_uid != 0)
451 		printf("umap_rename: rename component credit user was %d, group %d\n",
452 		    compcredp->cr_uid, compcredp->cr_gid);
453 
454 	/* Map all ids in the credential structure. */
455 
456 	umap_mapids(vp->v_mount, compcredp);
457 
458 	if (umap_bug_bypass && compcredp->cr_uid != 0)
459 		printf("umap_rename: rename component credit user now %d, group %d\n",
460 		    compcredp->cr_uid, compcredp->cr_gid);
461 
462 	error = umap_bypass(ap);
463 
464 	/* Restore the additional mapped componentname cred structure. */
465 
466 	crfree(compcredp);
467 	compnamep->cn_cred = savecompcredp;
468 
469 	return error;
470 }
471 
472 /*
473  * Global vfs data structures
474  */
475 /*
476  * XXX - strategy, bwrite are hand coded currently.  They should
477  * go away with a merged buffer/block cache.
478  *
479  */
480 int (**umap_vnodeop_p)();
481 struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
482 	{ &vop_default_desc, umap_bypass },
483 
484 	{ &vop_getattr_desc, umap_getattr },
485 	{ &vop_inactive_desc, umap_inactive },
486 	{ &vop_reclaim_desc, umap_reclaim },
487 	{ &vop_print_desc, umap_print },
488 	{ &vop_rename_desc, umap_rename },
489 
490 	{ &vop_strategy_desc, umap_strategy },
491 	{ &vop_bwrite_desc, umap_bwrite },
492 
493 	{ (struct vnodeop_desc*) NULL, (int(*)()) NULL }
494 };
495 struct vnodeopv_desc umap_vnodeop_opv_desc =
496 	{ &umap_vnodeop_p, umap_vnodeop_entries };
497