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