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