umap_vnops.c revision 1.30 1 /* $NetBSD: umap_vnops.c,v 1.30 2004/06/16 12:39:07 yamt 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)umap_vnops.c 8.6 (Berkeley) 5/22/95
35 */
36
37 /*
38 * Umap Layer
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: umap_vnops.c,v 1.30 2004/06/16 12:39:07 yamt Exp $");
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/time.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
49 #include <sys/namei.h>
50 #include <sys/malloc.h>
51 #include <sys/buf.h>
52 #include <miscfs/umapfs/umap.h>
53 #include <miscfs/genfs/genfs.h>
54 #include <miscfs/genfs/layer_extern.h>
55
56 int umap_lookup __P((void *));
57 int umap_getattr __P((void *));
58 int umap_print __P((void *));
59 int umap_rename __P((void *));
60
61 /*
62 * Global vfs data structures
63 */
64 /*
65 * XXX - strategy, bwrite are hand coded currently. They should
66 * go away with a merged buffer/block cache.
67 *
68 */
69 int (**umap_vnodeop_p) __P((void *));
70 const struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
71 { &vop_default_desc, umap_bypass },
72
73 { &vop_lookup_desc, umap_lookup },
74 { &vop_getattr_desc, umap_getattr },
75 { &vop_print_desc, umap_print },
76 { &vop_rename_desc, umap_rename },
77
78 { &vop_lock_desc, layer_lock },
79 { &vop_unlock_desc, layer_unlock },
80 { &vop_islocked_desc, layer_islocked },
81 { &vop_fsync_desc, layer_fsync },
82 { &vop_inactive_desc, layer_inactive },
83 { &vop_reclaim_desc, layer_reclaim },
84 { &vop_open_desc, layer_open },
85 { &vop_setattr_desc, layer_setattr },
86 { &vop_access_desc, layer_access },
87 { &vop_remove_desc, layer_remove },
88
89 { &vop_bwrite_desc, layer_bwrite },
90 { &vop_bmap_desc, layer_bmap },
91 { &vop_getpages_desc, layer_getpages },
92 { &vop_putpages_desc, layer_putpages },
93
94 { NULL, NULL }
95 };
96 const struct vnodeopv_desc umapfs_vnodeop_opv_desc =
97 { &umap_vnodeop_p, umap_vnodeop_entries };
98
99 /*
100 * This is the 08-June-1999 bypass routine.
101 * See layer_vnops.c:layer_bypass for more details.
102 */
103 int
104 umap_bypass(v)
105 void *v;
106 {
107 struct vop_generic_args /* {
108 struct vnodeop_desc *a_desc;
109 <other random data follows, presumably>
110 } */ *ap = v;
111 int (**our_vnodeop_p) __P((void *));
112 struct ucred **credpp = 0, *credp = 0;
113 struct ucred *savecredp = 0, *savecompcredp = 0;
114 struct ucred *compcredp = 0;
115 struct vnode **this_vp_p;
116 int error, error1;
117 struct vnode *old_vps[VDESC_MAX_VPS], *vp0;
118 struct vnode **vps_p[VDESC_MAX_VPS];
119 struct vnode ***vppp;
120 struct vnodeop_desc *descp = ap->a_desc;
121 int reles, i, flags;
122 struct componentname **compnamepp = 0;
123
124 #ifdef SAFETY
125 /*
126 * We require at least one vp.
127 */
128 if (descp->vdesc_vp_offsets == NULL ||
129 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
130 panic("%s: no vp's in map.\n", __func__);
131 #endif
132
133 vps_p[0] =
134 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
135 vp0 = *vps_p[0];
136 flags = MOUNTTOUMAPMOUNT(vp0->v_mount)->umapm_flags;
137 our_vnodeop_p = vp0->v_op;
138
139 if (flags & LAYERFS_MBYPASSDEBUG)
140 printf("%s: %s\n", __func__, descp->vdesc_name);
141
142 /*
143 * Map the vnodes going in.
144 * Later, we'll invoke the operation based on
145 * the first mapped vnode's operation vector.
146 */
147 reles = descp->vdesc_flags;
148 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
149 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
150 break; /* bail out at end of list */
151 vps_p[i] = this_vp_p =
152 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i],
153 ap);
154 /*
155 * We're not guaranteed that any but the first vnode
156 * are of our type. Check for and don't map any
157 * that aren't. (We must always map first vp or vclean fails.)
158 */
159 if (i && (*this_vp_p == NULL ||
160 (*this_vp_p)->v_op != our_vnodeop_p)) {
161 old_vps[i] = NULL;
162 } else {
163 old_vps[i] = *this_vp_p;
164 *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
165 /*
166 * XXX - Several operations have the side effect
167 * of vrele'ing their vp's. We must account for
168 * that. (This should go away in the future.)
169 */
170 if (reles & VDESC_VP0_WILLRELE)
171 VREF(*this_vp_p);
172 }
173
174 }
175
176 /*
177 * Fix the credentials. (That's the purpose of this layer.)
178 */
179
180 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
181
182 credpp = VOPARG_OFFSETTO(struct ucred**,
183 descp->vdesc_cred_offset, ap);
184
185 /* Save old values */
186
187 savecredp = *credpp;
188 if (savecredp != NOCRED)
189 *credpp = crdup(savecredp);
190 credp = *credpp;
191
192 if ((flags & LAYERFS_MBYPASSDEBUG) && credp->cr_uid != 0)
193 printf("umap_bypass: user was %d, group %d\n",
194 credp->cr_uid, credp->cr_gid);
195
196 /* Map all ids in the credential structure. */
197
198 umap_mapids(vp0->v_mount, credp);
199
200 if ((flags & LAYERFS_MBYPASSDEBUG) && credp->cr_uid != 0)
201 printf("umap_bypass: user now %d, group %d\n",
202 credp->cr_uid, credp->cr_gid);
203 }
204
205 /* BSD often keeps a credential in the componentname structure
206 * for speed. If there is one, it better get mapped, too.
207 */
208
209 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
210
211 compnamepp = VOPARG_OFFSETTO(struct componentname**,
212 descp->vdesc_componentname_offset, ap);
213
214 savecompcredp = (*compnamepp)->cn_cred;
215 if (savecompcredp != NOCRED)
216 (*compnamepp)->cn_cred = crdup(savecompcredp);
217 compcredp = (*compnamepp)->cn_cred;
218
219 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0)
220 printf("umap_bypass: component credit user was %d, group %d\n",
221 compcredp->cr_uid, compcredp->cr_gid);
222
223 /* Map all ids in the credential structure. */
224
225 umap_mapids(vp0->v_mount, compcredp);
226
227 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0)
228 printf("umap_bypass: component credit user now %d, group %d\n",
229 compcredp->cr_uid, compcredp->cr_gid);
230 }
231
232 /*
233 * Call the operation on the lower layer
234 * with the modified argument structure.
235 */
236 error = VCALL(*vps_p[0], descp->vdesc_offset, ap);
237
238 /*
239 * Maintain the illusion of call-by-value
240 * by restoring vnodes in the argument structure
241 * to their original value.
242 */
243 reles = descp->vdesc_flags;
244 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
245 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
246 break; /* bail out at end of list */
247 if (old_vps[i]) {
248 *(vps_p[i]) = old_vps[i];
249 if (reles & VDESC_VP0_WILLUNLOCK)
250 LAYERFS_UPPERUNLOCK(*(vps_p[i]), 0, error1);
251 if (reles & VDESC_VP0_WILLRELE)
252 vrele(*(vps_p[i]));
253 }
254 }
255
256 /*
257 * Map the possible out-going vpp
258 * (Assumes that the lower layer always returns
259 * a VREF'ed vpp unless it gets an error.)
260 */
261 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
262 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
263 !error) {
264 /*
265 * XXX - even though some ops have vpp returned vp's,
266 * several ops actually vrele this before returning.
267 * We must avoid these ops.
268 * (This should go away when these ops are regularized.)
269 */
270 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
271 goto out;
272 vppp = VOPARG_OFFSETTO(struct vnode***,
273 descp->vdesc_vpp_offset, ap);
274 /*
275 * Only vop_lookup, vop_create, vop_makedir, vop_bmap,
276 * vop_mknod, and vop_symlink return vpp's. vop_bmap
277 * doesn't call bypass as the lower vpp is fine (we're just
278 * going to do i/o on it). vop_lookup doesn't call bypass
279 * as a lookup on "." would generate a locking error.
280 * So all the calls which get us here have a locked vpp. :-)
281 */
282 error = layer_node_create(old_vps[0]->v_mount, **vppp, *vppp);
283 if (error) {
284 vput(**vppp);
285 **vppp = NULL;
286 }
287 }
288
289 out:
290 /*
291 * Free duplicate cred structure and restore old one.
292 */
293 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
294 if ((flags & LAYERFS_MBYPASSDEBUG) && credp &&
295 credp->cr_uid != 0)
296 printf("umap_bypass: returning-user was %d\n",
297 credp->cr_uid);
298
299 if (savecredp != NOCRED) {
300 crfree(credp);
301 *credpp = savecredp;
302 if ((flags & LAYERFS_MBYPASSDEBUG) && credpp &&
303 (*credpp)->cr_uid != 0)
304 printf("umap_bypass: returning-user now %d\n\n",
305 savecredp->cr_uid);
306 }
307 }
308
309 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
310 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp &&
311 compcredp->cr_uid != 0)
312 printf("umap_bypass: returning-component-user was %d\n",
313 compcredp->cr_uid);
314
315 if (savecompcredp != NOCRED) {
316 crfree(compcredp);
317 (*compnamepp)->cn_cred = savecompcredp;
318 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp &&
319 savecompcredp->cr_uid != 0)
320 printf("umap_bypass: returning-component-user now %d\n",
321 savecompcredp->cr_uid);
322 }
323 }
324
325 return (error);
326 }
327
328 /*
329 * This is based on the 08-June-1999 bypass routine.
330 * See layer_vnops.c:layer_bypass for more details.
331 */
332 int
333 umap_lookup(v)
334 void *v;
335 {
336 struct vop_lookup_args /* {
337 struct vnodeop_desc *a_desc;
338 struct vnode * a_dvp;
339 struct vnode ** a_vpp;
340 struct componentname * a_cnp;
341 } */ *ap = v;
342 struct componentname *cnp = ap->a_cnp;
343 struct ucred *savecompcredp = NULL;
344 struct ucred *compcredp = NULL;
345 struct vnode *dvp, *vp, *ldvp;
346 struct mount *mp;
347 int error;
348 int i, flags, cnf = cnp->cn_flags;
349
350 dvp = ap->a_dvp;
351 mp = dvp->v_mount;
352
353 if ((cnf & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
354 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
355 return (EROFS);
356
357 flags = MOUNTTOUMAPMOUNT(mp)->umapm_flags;
358 ldvp = UMAPVPTOLOWERVP(dvp);
359
360 if (flags & LAYERFS_MBYPASSDEBUG)
361 printf("umap_lookup\n");
362
363 /*
364 * Fix the credentials. (That's the purpose of this layer.)
365 *
366 * BSD often keeps a credential in the componentname structure
367 * for speed. If there is one, it better get mapped, too.
368 */
369
370 if ((savecompcredp = cnp->cn_cred)) {
371 compcredp = crdup(savecompcredp);
372 cnp->cn_cred = compcredp;
373
374 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0)
375 printf("umap_lookup: component credit user was %d, group %d\n",
376 compcredp->cr_uid, compcredp->cr_gid);
377
378 /* Map all ids in the credential structure. */
379 umap_mapids(mp, compcredp);
380 }
381
382 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0)
383 printf("umap_lookup: component credit user now %d, group %d\n",
384 compcredp->cr_uid, compcredp->cr_gid);
385
386 ap->a_dvp = ldvp;
387 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap);
388 vp = *ap->a_vpp;
389 *ap->a_vpp = NULL;
390
391 if (error == EJUSTRETURN && (cnf & ISLASTCN) &&
392 (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
393 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
394 error = EROFS;
395
396 /* Do locking fixup as appropriate. See layer_lookup() for info */
397 if ((cnp->cn_flags & PDIRUNLOCK)) {
398 LAYERFS_UPPERUNLOCK(dvp, 0, i);
399 }
400 if (ldvp == vp) {
401 *ap->a_vpp = dvp;
402 VREF(dvp);
403 vrele(vp);
404 } else if (vp != NULL) {
405 error = layer_node_create(mp, vp, ap->a_vpp);
406 if (error) {
407 vput(vp);
408 if (cnp->cn_flags & PDIRUNLOCK) {
409 vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
410 cnp->cn_flags &= ~PDIRUNLOCK;
411 }
412 }
413 }
414
415 /*
416 * Free duplicate cred structure and restore old one.
417 */
418 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp &&
419 compcredp->cr_uid != 0)
420 printf("umap_lookup: returning-component-user was %d\n",
421 compcredp->cr_uid);
422
423 if (savecompcredp != NOCRED) {
424 crfree(compcredp);
425 cnp->cn_cred = savecompcredp;
426 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp &&
427 savecompcredp->cr_uid != 0)
428 printf("umap_lookup: returning-component-user now %d\n",
429 savecompcredp->cr_uid);
430 }
431
432 return (error);
433 }
434
435 /*
436 * We handle getattr to change the fsid.
437 */
438 int
439 umap_getattr(v)
440 void *v;
441 {
442 struct vop_getattr_args /* {
443 struct vnode *a_vp;
444 struct vattr *a_vap;
445 struct ucred *a_cred;
446 struct proc *a_p;
447 } */ *ap = v;
448 uid_t uid;
449 gid_t gid;
450 int error, tmpid, nentries, gnentries, flags;
451 u_long (*mapdata)[2];
452 u_long (*gmapdata)[2];
453 struct vnode **vp1p;
454 const struct vnodeop_desc *descp = ap->a_desc;
455
456 if ((error = umap_bypass(ap)) != 0)
457 return (error);
458 /* Requires that arguments be restored. */
459 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
460
461 flags = MOUNTTOUMAPMOUNT(ap->a_vp->v_mount)->umapm_flags;
462 /*
463 * Umap needs to map the uid and gid returned by a stat
464 * into the proper values for this site. This involves
465 * finding the returned uid in the mapping information,
466 * translating it into the uid on the other end,
467 * and filling in the proper field in the vattr
468 * structure pointed to by ap->a_vap. The group
469 * is easier, since currently all groups will be
470 * translate to the NULLGROUP.
471 */
472
473 /* Find entry in map */
474
475 uid = ap->a_vap->va_uid;
476 gid = ap->a_vap->va_gid;
477 if ((flags & LAYERFS_MBYPASSDEBUG))
478 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
479 gid);
480
481 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
482 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
483 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
484 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
485 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
486
487 /* Reverse map the uid for the vnode. Since it's a reverse
488 map, we can't use umap_mapids() to do it. */
489
490 tmpid = umap_reverse_findid(uid, mapdata, nentries);
491
492 if (tmpid != -1) {
493 ap->a_vap->va_uid = (uid_t) tmpid;
494 if ((flags & LAYERFS_MBYPASSDEBUG))
495 printf("umap_getattr: original uid = %d\n", uid);
496 } else
497 ap->a_vap->va_uid = (uid_t) NOBODY;
498
499 /* Reverse map the gid for the vnode. */
500
501 tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
502
503 if (tmpid != -1) {
504 ap->a_vap->va_gid = (gid_t) tmpid;
505 if ((flags & LAYERFS_MBYPASSDEBUG))
506 printf("umap_getattr: original gid = %d\n", gid);
507 } else
508 ap->a_vap->va_gid = (gid_t) NULLGROUP;
509
510 return (0);
511 }
512
513 int
514 umap_print(v)
515 void *v;
516 {
517 struct vop_print_args /* {
518 struct vnode *a_vp;
519 } */ *ap = v;
520 struct vnode *vp = ap->a_vp;
521 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp,
522 UMAPVPTOLOWERVP(vp));
523 return (0);
524 }
525
526 int
527 umap_rename(v)
528 void *v;
529 {
530 struct vop_rename_args /* {
531 struct vnode *a_fdvp;
532 struct vnode *a_fvp;
533 struct componentname *a_fcnp;
534 struct vnode *a_tdvp;
535 struct vnode *a_tvp;
536 struct componentname *a_tcnp;
537 } */ *ap = v;
538 int error, flags;
539 struct componentname *compnamep;
540 struct ucred *compcredp, *savecompcredp;
541 struct vnode *vp;
542 struct vnode *tvp;
543
544 /*
545 * Rename is irregular, having two componentname structures.
546 * We need to map the cre in the second structure,
547 * and then bypass takes care of the rest.
548 */
549
550 vp = ap->a_fdvp;
551 flags = MOUNTTOUMAPMOUNT(vp->v_mount)->umapm_flags;
552 compnamep = ap->a_tcnp;
553 compcredp = compnamep->cn_cred;
554
555 savecompcredp = compcredp;
556 compcredp = compnamep->cn_cred = crdup(savecompcredp);
557
558 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0)
559 printf("umap_rename: rename component credit user was %d, group %d\n",
560 compcredp->cr_uid, compcredp->cr_gid);
561
562 /* Map all ids in the credential structure. */
563
564 umap_mapids(vp->v_mount, compcredp);
565
566 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0)
567 printf("umap_rename: rename component credit user now %d, group %d\n",
568 compcredp->cr_uid, compcredp->cr_gid);
569
570 tvp = ap->a_tvp;
571 if (tvp) {
572 if (tvp->v_mount != vp->v_mount)
573 tvp = NULL;
574 else
575 vref(tvp);
576 }
577 error = umap_bypass(ap);
578 if (tvp) {
579 if (error == 0)
580 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED;
581 vrele(tvp);
582 }
583
584 /* Restore the additional mapped componentname cred structure. */
585
586 crfree(compcredp);
587 compnamep->cn_cred = savecompcredp;
588
589 return error;
590 }
591