umap_vnops.c revision 1.13 1 /* $NetBSD: umap_vnops.c,v 1.13 1999/03/25 13:05:42 bouyer 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.6 (Berkeley) 5/22/95
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 #include <miscfs/genfs/genfs.h>
56
57
58 int umap_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
59
60 int umap_bypass __P((void *));
61 int umap_getattr __P((void *));
62 int umap_inactive __P((void *));
63 int umap_reclaim __P((void *));
64 int umap_print __P((void *));
65 int umap_rename __P((void *));
66 int umap_strategy __P((void *));
67 int umap_bwrite __P((void *));
68 int umap_lock __P((void *));
69 int umap_unlock __P((void *));
70 int umap_open __P((void *));
71 int umap_fsync __P((void *));
72
73 extern int null_bypass __P((void *));
74
75 /*
76 * Global vfs data structures
77 */
78 /*
79 * XXX - strategy, bwrite are hand coded currently. They should
80 * go away with a merged buffer/block cache.
81 *
82 */
83 int (**umap_vnodeop_p) __P((void *));
84 struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
85 { &vop_default_desc, umap_bypass },
86
87 { &vop_getattr_desc, umap_getattr },
88 { &vop_lock_desc, umap_lock },
89 { &vop_unlock_desc, umap_unlock },
90 { &vop_fsync_desc, umap_fsync },
91 { &vop_inactive_desc, umap_inactive },
92 { &vop_reclaim_desc, umap_reclaim },
93 { &vop_print_desc, umap_print },
94
95 { &vop_open_desc, umap_open }, /* mount option handling */
96
97 { &vop_rename_desc, umap_rename },
98
99 { &vop_strategy_desc, umap_strategy },
100 { &vop_bwrite_desc, umap_bwrite },
101
102 { (struct vnodeop_desc*) NULL, (int(*) __P((void *))) NULL }
103 };
104 struct vnodeopv_desc umapfs_vnodeop_opv_desc =
105 { &umap_vnodeop_p, umap_vnodeop_entries };
106
107 /*
108 * This is the 10-Apr-92 bypass routine.
109 * See null_vnops.c:null_bypass for more details.
110 */
111 int
112 umap_bypass(v)
113 void *v;
114 {
115 struct vop_generic_args /* {
116 struct vnodeop_desc *a_desc;
117 <other random data follows, presumably>
118 } */ *ap = v;
119 struct ucred **credpp = 0, *credp = 0;
120 struct ucred *savecredp = 0, *savecompcredp = 0;
121 struct ucred *compcredp = 0;
122 struct vnode **this_vp_p;
123 int error;
124 struct vnode *old_vps[VDESC_MAX_VPS];
125 struct vnode *vp1 = 0;
126 struct vnode **vps_p[VDESC_MAX_VPS];
127 struct vnode ***vppp;
128 struct vnodeop_desc *descp = ap->a_desc;
129 int reles, i;
130 struct componentname **compnamepp = 0;
131
132 if (umap_bug_bypass)
133 printf("umap_bypass: %s\n", descp->vdesc_name);
134
135 #ifdef SAFETY
136 /*
137 * We require at least one vp.
138 */
139 if (descp->vdesc_vp_offsets == NULL ||
140 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
141 panic ("umap_bypass: no vp's in map.\n");
142 #endif
143
144 /*
145 * Map the vnodes going in.
146 * Later, we'll invoke the operation based on
147 * the first mapped vnode's operation vector.
148 */
149 reles = descp->vdesc_flags;
150 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
151 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
152 break; /* bail out at end of list */
153 vps_p[i] = this_vp_p =
154 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap);
155
156 if (i == 0) {
157 vp1 = *vps_p[0];
158 }
159
160 /*
161 * We're not guaranteed that any but the first vnode
162 * are of our type. Check for and don't map any
163 * that aren't. (Must map first vp or vclean fails.)
164 */
165
166 if (i && ((*this_vp_p)==NULL || (*this_vp_p)->v_op != umap_vnodeop_p)) {
167 old_vps[i] = NULL;
168 } else {
169 old_vps[i] = *this_vp_p;
170 *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
171 if (reles & 1)
172 VREF(*this_vp_p);
173 }
174
175 }
176
177 /*
178 * Fix the credentials. (That's the purpose of this layer.)
179 */
180
181 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
182
183 credpp = VOPARG_OFFSETTO(struct ucred**,
184 descp->vdesc_cred_offset, ap);
185
186 /* Save old values */
187
188 savecredp = *credpp;
189 if (savecredp != NOCRED)
190 *credpp = crdup(savecredp);
191 credp = *credpp;
192
193 if (umap_bug_bypass && credp->cr_uid != 0)
194 printf("umap_bypass: user was %d, group %d\n",
195 credp->cr_uid, credp->cr_gid);
196
197 /* Map all ids in the credential structure. */
198
199 umap_mapids(vp1->v_mount, credp);
200
201 if (umap_bug_bypass && credp->cr_uid != 0)
202 printf("umap_bypass: user now %d, group %d\n",
203 credp->cr_uid, credp->cr_gid);
204 }
205
206 /* BSD often keeps a credential in the componentname structure
207 * for speed. If there is one, it better get mapped, too.
208 */
209
210 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
211
212 compnamepp = VOPARG_OFFSETTO(struct componentname**,
213 descp->vdesc_componentname_offset, ap);
214
215 savecompcredp = (*compnamepp)->cn_cred;
216 if (savecompcredp != NOCRED)
217 (*compnamepp)->cn_cred = crdup(savecompcredp);
218 compcredp = (*compnamepp)->cn_cred;
219
220 if (umap_bug_bypass && compcredp->cr_uid != 0)
221 printf("umap_bypass: component credit user was %d, group %d\n",
222 compcredp->cr_uid, compcredp->cr_gid);
223
224 /* Map all ids in the credential structure. */
225
226 umap_mapids(vp1->v_mount, compcredp);
227
228 if (umap_bug_bypass && compcredp->cr_uid != 0)
229 printf("umap_bypass: component credit user now %d, group %d\n",
230 compcredp->cr_uid, compcredp->cr_gid);
231 }
232
233 /*
234 * Call the operation on the lower layer
235 * with the modified argument structure.
236 */
237 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
238
239 /*
240 * Maintain the illusion of call-by-value
241 * by restoring vnodes in the argument structure
242 * to their original value.
243 */
244 reles = descp->vdesc_flags;
245 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
246 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
247 break; /* bail out at end of list */
248 if (old_vps[i]) {
249 *(vps_p[i]) = old_vps[i];
250 if (reles & 1)
251 vrele(*(vps_p[i]));
252 };
253 };
254
255 /*
256 * Map the possible out-going vpp
257 * (Assumes that the lower layer always returns
258 * a VREF'ed vpp unless it gets an error.)
259 */
260 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
261 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
262 !error) {
263 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
264 goto out;
265 vppp = VOPARG_OFFSETTO(struct vnode***,
266 descp->vdesc_vpp_offset, ap);
267 error = umap_node_create(old_vps[0]->v_mount, **vppp, *vppp);
268 };
269
270 out:
271 /*
272 * Free duplicate cred structure and restore old one.
273 */
274 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
275 if (umap_bug_bypass && credp && credp->cr_uid != 0)
276 printf("umap_bypass: returning-user was %d\n",
277 credp->cr_uid);
278
279 if (savecredp != NOCRED) {
280 crfree(credp);
281 *credpp = savecredp;
282 if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
283 printf("umap_bypass: returning-user now %d\n\n",
284 savecredp->cr_uid);
285 }
286 }
287
288 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
289 if (umap_bug_bypass && compcredp && compcredp->cr_uid != 0)
290 printf("umap_bypass: returning-component-user was %d\n",
291 compcredp->cr_uid);
292
293 if (savecompcredp != NOCRED) {
294 crfree(compcredp);
295 (*compnamepp)->cn_cred = savecompcredp;
296 if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
297 printf("umap_bypass: returning-component-user now %d\n",
298 savecompcredp->cr_uid);
299 }
300 }
301
302 return (error);
303 }
304
305 /*
306 * We need to process our own vnode lock and then clear the
307 * interlock flag as it applies only to our vnode, not the
308 * vnodes below us on the stack.
309 */
310 int
311 umap_lock(v)
312 void *v;
313 {
314 struct vop_lock_args /* {
315 struct vnode *a_vp;
316 int a_flags;
317 struct proc *a_p;
318 } */ *ap = v;
319
320 genfs_nolock(ap);
321 if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
322 return (0);
323 ap->a_flags &= ~LK_INTERLOCK;
324 return (null_bypass(ap));
325 }
326
327 /*
328 * We need to process our own vnode unlock and then clear the
329 * interlock flag as it applies only to our vnode, not the
330 * vnodes below us on the stack.
331 */
332 int
333 umap_unlock(v)
334 void *v;
335 {
336 struct vop_unlock_args /* {
337 struct vnode *a_vp;
338 int a_flags;
339 struct proc *a_p;
340 } */ *ap = v;
341
342 genfs_nounlock(ap);
343 ap->a_flags &= ~LK_INTERLOCK;
344 return (null_bypass(ap));
345 }
346
347 /*
348 * If vinvalbuf is calling us, it's a "shallow fsync" -- don't bother
349 * syncing the underlying vnodes, since (a) they'll be fsync'ed when
350 * reclaimed and (b) we could deadlock if they're locked; otherwise,
351 * pass it through to the underlying layer.
352 */
353
354 int
355 umap_fsync(v)
356 void *v;
357 {
358 struct vop_fsync_args /* {
359 struct vnode *a_vp;
360 struct ucred *a_cred;
361 int a_flags;
362 struct proc *a_p;
363 } */ *ap = v;
364
365 if (ap->a_flags & FSYNC_RECLAIM)
366 return 0;
367
368 return (umap_bypass(ap));
369 }
370
371 /*
372 * We handle getattr to change the fsid.
373 */
374 int
375 umap_getattr(v)
376 void *v;
377 {
378 struct vop_getattr_args /* {
379 struct vnode *a_vp;
380 struct vattr *a_vap;
381 struct ucred *a_cred;
382 struct proc *a_p;
383 } */ *ap = v;
384 uid_t uid;
385 gid_t gid;
386 int error, tmpid, nentries, gnentries;
387 u_long (*mapdata)[2];
388 u_long (*gmapdata)[2];
389 struct vnode **vp1p;
390 struct vnodeop_desc *descp = ap->a_desc;
391
392 if ((error = umap_bypass(ap)) != 0)
393 return (error);
394 /* Requires that arguments be restored. */
395 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
396
397 /*
398 * Umap needs to map the uid and gid returned by a stat
399 * into the proper values for this site. This involves
400 * finding the returned uid in the mapping information,
401 * translating it into the uid on the other end,
402 * and filling in the proper field in the vattr
403 * structure pointed to by ap->a_vap. The group
404 * is easier, since currently all groups will be
405 * translate to the NULLGROUP.
406 */
407
408 /* Find entry in map */
409
410 uid = ap->a_vap->va_uid;
411 gid = ap->a_vap->va_gid;
412 if (umap_bug_bypass)
413 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
414 gid);
415
416 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
417 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
418 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
419 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
420 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
421
422 /* Reverse map the uid for the vnode. Since it's a reverse
423 map, we can't use umap_mapids() to do it. */
424
425 tmpid = umap_reverse_findid(uid, mapdata, nentries);
426
427 if (tmpid != -1) {
428 ap->a_vap->va_uid = (uid_t) tmpid;
429 if (umap_bug_bypass)
430 printf("umap_getattr: original uid = %d\n", uid);
431 } else
432 ap->a_vap->va_uid = (uid_t) NOBODY;
433
434 /* Reverse map the gid for the vnode. */
435
436 tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
437
438 if (tmpid != -1) {
439 ap->a_vap->va_gid = (gid_t) tmpid;
440 if (umap_bug_bypass)
441 printf("umap_getattr: original gid = %d\n", gid);
442 } else
443 ap->a_vap->va_gid = (gid_t) NULLGROUP;
444
445 return (0);
446 }
447
448 /*
449 * We must handle open to be able to catch MNT_NODEV and friends.
450 */
451 int
452 umap_open(v)
453 void *v;
454 {
455 struct vop_open_args *ap = v;
456 struct vnode *vp = ap->a_vp;
457 enum vtype lower_type = UMAPVPTOLOWERVP(vp)->v_type;
458
459
460 if (((lower_type == VBLK) || (lower_type == VCHR)) &&
461 (vp->v_mount->mnt_flag & MNT_NODEV))
462 return ENXIO;
463
464 return umap_bypass(ap);
465 }
466
467 /*ARGSUSED*/
468 int
469 umap_inactive(v)
470 void *v;
471 {
472 struct vop_inactive_args /* {
473 struct vnode *a_vp;
474 struct proc *a_p;
475 } */ *ap = v;
476 /*
477 * Do nothing (and _don't_ bypass).
478 * Wait to vrele lowervp until reclaim,
479 * so that until then our umap_node is in the
480 * cache and reusable.
481 *
482 */
483 VOP_UNLOCK(ap->a_vp, 0);
484 return (0);
485 }
486
487 int
488 umap_reclaim(v)
489 void *v;
490 {
491 struct vop_reclaim_args /* {
492 struct vnode *a_vp;
493 } */ *ap = v;
494 struct vnode *vp = ap->a_vp;
495 struct umap_node *xp = VTOUMAP(vp);
496 struct vnode *lowervp = xp->umap_lowervp;
497
498 /* After this assignment, this node will not be re-used. */
499 xp->umap_lowervp = NULL;
500 LIST_REMOVE(xp, umap_hash);
501 FREE(vp->v_data, M_TEMP);
502 vp->v_data = NULL;
503 vrele(lowervp);
504 return (0);
505 }
506
507 int
508 umap_strategy(v)
509 void *v;
510 {
511 struct vop_strategy_args /* {
512 struct buf *a_bp;
513 } */ *ap = v;
514 struct buf *bp = ap->a_bp;
515 int error;
516 struct vnode *savedvp;
517
518 savedvp = bp->b_vp;
519 bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp);
520
521 error = VOP_STRATEGY(ap->a_bp);
522
523 bp->b_vp = savedvp;
524
525 return (error);
526 }
527
528 int
529 umap_bwrite(v)
530 void *v;
531 {
532 struct vop_bwrite_args /* {
533 struct buf *a_bp;
534 } */ *ap = v;
535 struct buf *bp = ap->a_bp;
536 int error;
537 struct vnode *savedvp;
538
539 savedvp = bp->b_vp;
540 bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp);
541
542 error = VOP_BWRITE(ap->a_bp);
543
544 bp->b_vp = savedvp;
545
546 return (error);
547 }
548
549
550 int
551 umap_print(v)
552 void *v;
553 {
554 struct vop_print_args /* {
555 struct vnode *a_vp;
556 } */ *ap = v;
557 struct vnode *vp = ap->a_vp;
558 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp,
559 UMAPVPTOLOWERVP(vp));
560 return (0);
561 }
562
563 int
564 umap_rename(v)
565 void *v;
566 {
567 struct vop_rename_args /* {
568 struct vnode *a_fdvp;
569 struct vnode *a_fvp;
570 struct componentname *a_fcnp;
571 struct vnode *a_tdvp;
572 struct vnode *a_tvp;
573 struct componentname *a_tcnp;
574 } */ *ap = v;
575 int error;
576 struct componentname *compnamep;
577 struct ucred *compcredp, *savecompcredp;
578 struct vnode *vp;
579
580 /*
581 * Rename is irregular, having two componentname structures.
582 * We need to map the cre in the second structure,
583 * and then bypass takes care of the rest.
584 */
585
586 vp = ap->a_fdvp;
587 compnamep = ap->a_tcnp;
588 compcredp = compnamep->cn_cred;
589
590 savecompcredp = compcredp;
591 compcredp = compnamep->cn_cred = crdup(savecompcredp);
592
593 if (umap_bug_bypass && compcredp->cr_uid != 0)
594 printf("umap_rename: rename component credit user was %d, group %d\n",
595 compcredp->cr_uid, compcredp->cr_gid);
596
597 /* Map all ids in the credential structure. */
598
599 umap_mapids(vp->v_mount, compcredp);
600
601 if (umap_bug_bypass && compcredp->cr_uid != 0)
602 printf("umap_rename: rename component credit user now %d, group %d\n",
603 compcredp->cr_uid, compcredp->cr_gid);
604
605 error = umap_bypass(ap);
606
607 /* Restore the additional mapped componentname cred structure. */
608
609 crfree(compcredp);
610 compnamep->cn_cred = savecompcredp;
611
612 return error;
613 }
614