xref: /original-bsd/sys/miscfs/umapfs/umap_vnops.c (revision 0ac4996f)
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.5 (Berkeley) 05/14/95
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 /*
301  * We need to verify that we are not being vgoned and then clear
302  * the interlock flag as it applies only to our vnode, not the
303  * vnodes below us on the stack.
304  */
305 int
306 umap_lock(ap)
307 	struct vop_lock_args *ap;
308 {
309 	struct vnode *vp = ap->a_vp;
310 	int error;
311 
312 	if ((ap->a_flags & LK_INTERLOCK) == 0)
313 		simple_lock(&vp->v_interlock);
314 	if (vp->v_flag & VXLOCK) {
315 		vp->v_flag |= VXWANT;
316 		simple_unlock(&vp->v_interlock);
317 		tsleep((caddr_t)vp, PINOD, "unionlk1", 0);
318 		return (ENOENT);
319 	}
320 	simple_unlock(&vp->v_interlock);
321 	ap->a_flags &= ~LK_INTERLOCK;
322 	if (error = umap_bypass(ap))
323 		return (error);
324 	return (0);
325 }
326 
327 int
328 umap_inactive(ap)
329 	struct vop_inactive_args /* {
330 		struct vnode *a_vp;
331 	} */ *ap;
332 {
333 	/*
334 	 * Do nothing (and _don't_ bypass).
335 	 * Wait to vrele lowervp until reclaim,
336 	 * so that until then our umap_node is in the
337 	 * cache and reusable.
338 	 *
339 	 */
340 	return (0);
341 }
342 
343 int
344 umap_reclaim(ap)
345 	struct vop_reclaim_args /* {
346 		struct vnode *a_vp;
347 	} */ *ap;
348 {
349 	struct vnode *vp = ap->a_vp;
350 	struct umap_node *xp = VTOUMAP(vp);
351 	struct vnode *lowervp = xp->umap_lowervp;
352 
353 	/* After this assignment, this node will not be re-used. */
354 	xp->umap_lowervp = NULL;
355 	LIST_REMOVE(xp, umap_hash);
356 	FREE(vp->v_data, M_TEMP);
357 	vp->v_data = NULL;
358 	vrele(lowervp);
359 	return (0);
360 }
361 
362 int
363 umap_strategy(ap)
364 	struct vop_strategy_args /* {
365 		struct buf *a_bp;
366 	} */ *ap;
367 {
368 	struct buf *bp = ap->a_bp;
369 	int error;
370 	struct vnode *savedvp;
371 
372 	savedvp = bp->b_vp;
373 	bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp);
374 
375 	error = VOP_STRATEGY(ap->a_bp);
376 
377 	bp->b_vp = savedvp;
378 
379 	return (error);
380 }
381 
382 int
383 umap_bwrite(ap)
384 	struct vop_bwrite_args /* {
385 		struct buf *a_bp;
386 	} */ *ap;
387 {
388 	struct buf *bp = ap->a_bp;
389 	int error;
390 	struct vnode *savedvp;
391 
392 	savedvp = bp->b_vp;
393 	bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp);
394 
395 	error = VOP_BWRITE(ap->a_bp);
396 
397 	bp->b_vp = savedvp;
398 
399 	return (error);
400 }
401 
402 
403 int
404 umap_print(ap)
405 	struct vop_print_args /* {
406 		struct vnode *a_vp;
407 	} */ *ap;
408 {
409 	struct vnode *vp = ap->a_vp;
410 	printf("\ttag VT_UMAPFS, vp=%x, lowervp=%x\n", vp, UMAPVPTOLOWERVP(vp));
411 	return (0);
412 }
413 
414 int
415 umap_rename(ap)
416 	struct vop_rename_args  /* {
417 		struct vnode *a_fdvp;
418 		struct vnode *a_fvp;
419 		struct componentname *a_fcnp;
420 		struct vnode *a_tdvp;
421 		struct vnode *a_tvp;
422 		struct componentname *a_tcnp;
423 	} */ *ap;
424 {
425 	int error;
426 	struct componentname *compnamep;
427 	struct ucred *compcredp, *savecompcredp;
428 	struct vnode *vp;
429 
430 	/*
431 	 * Rename is irregular, having two componentname structures.
432 	 * We need to map the cre in the second structure,
433 	 * and then bypass takes care of the rest.
434 	 */
435 
436 	vp = ap->a_fdvp;
437 	compnamep = ap->a_tcnp;
438 	compcredp = compnamep->cn_cred;
439 
440 	savecompcredp = compcredp;
441 	compcredp = compnamep->cn_cred = crdup(savecompcredp);
442 
443 	if (umap_bug_bypass && compcredp->cr_uid != 0)
444 		printf("umap_rename: rename component credit user was %d, group %d\n",
445 		    compcredp->cr_uid, compcredp->cr_gid);
446 
447 	/* Map all ids in the credential structure. */
448 
449 	umap_mapids(vp->v_mount, compcredp);
450 
451 	if (umap_bug_bypass && compcredp->cr_uid != 0)
452 		printf("umap_rename: rename component credit user now %d, group %d\n",
453 		    compcredp->cr_uid, compcredp->cr_gid);
454 
455 	error = umap_bypass(ap);
456 
457 	/* Restore the additional mapped componentname cred structure. */
458 
459 	crfree(compcredp);
460 	compnamep->cn_cred = savecompcredp;
461 
462 	return error;
463 }
464 
465 /*
466  * Global vfs data structures
467  */
468 /*
469  * XXX - strategy, bwrite are hand coded currently.  They should
470  * go away with a merged buffer/block cache.
471  *
472  */
473 int (**umap_vnodeop_p)();
474 struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
475 	{ &vop_default_desc, umap_bypass },
476 
477 	{ &vop_getattr_desc, umap_getattr },
478 	{ &vop_lock_desc, umap_lock },
479 	{ &vop_inactive_desc, umap_inactive },
480 	{ &vop_reclaim_desc, umap_reclaim },
481 	{ &vop_print_desc, umap_print },
482 	{ &vop_rename_desc, umap_rename },
483 
484 	{ &vop_strategy_desc, umap_strategy },
485 	{ &vop_bwrite_desc, umap_bwrite },
486 
487 	{ (struct vnodeop_desc*) NULL, (int(*)()) NULL }
488 };
489 struct vnodeopv_desc umap_vnodeop_opv_desc =
490 	{ &umap_vnodeop_p, umap_vnodeop_entries };
491