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