2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
6 * the UCLA Ficus project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)umap_vnops.c 8.6 (Berkeley) 5/22/95
37 * $FreeBSD: src/sys/miscfs/umapfs/umap_vnops.c,v 1.30 1999/08/30 07:08:04 bde Exp $
38 * $DragonFly: src/sys/vfs/umapfs/Attic/umap_vnops.c,v 1.6 2004/03/01 06:33:23 dillon Exp $
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
56 #include <vfs/nullfs/null.h>
58 static int umap_bug_bypass = 0; /* for debugging: enables bypass printf'ing */
59 SYSCTL_INT(_debug, OID_AUTO, umapfs_bug_bypass, CTLFLAG_RW,
60 &umap_bug_bypass, 0, "");
62 static int umap_bypass (struct vop_generic_args *ap);
63 static int umap_getattr (struct vop_getattr_args *ap);
64 static int umap_inactive (struct vop_inactive_args *ap);
65 static int umap_lock (struct vop_lock_args *ap);
66 static int umap_print (struct vop_print_args *ap);
67 static int umap_reclaim (struct vop_reclaim_args *ap);
68 static int umap_rename (struct vop_rename_args *ap);
69 static int umap_unlock (struct vop_unlock_args *ap);
72 * This is the 10-Apr-92 bypass routine.
73 * See null_vnops.c:null_bypass for more details.
77 struct vop_generic_args /* {
78 struct vnodeop_desc *a_desc;
79 <other random data follows, presumably>
82 struct ucred **credpp = 0, *credp = 0;
83 struct ucred *savecredp = 0, *savecompcredp = 0;
84 struct ucred *compcredp = 0;
85 struct vnode **this_vp_p;
87 struct vnode *old_vps[VDESC_MAX_VPS];
88 struct vnode *vp1 = 0;
89 struct vnode **vps_p[VDESC_MAX_VPS];
91 struct vnodeop_desc *descp = ap->a_desc;
93 struct componentname **compnamepp = 0;
96 printf ("umap_bypass: %s\n", descp->vdesc_name);
100 * We require at least one vp.
102 if (descp->vdesc_vp_offsets == NULL ||
103 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
104 panic ("umap_bypass: no vp's in map");
108 * Map the vnodes going in.
109 * Later, we'll invoke the operation based on
110 * the first mapped vnode's operation vector.
112 reles = descp->vdesc_flags;
113 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
114 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
115 break; /* bail out at end of list */
116 vps_p[i] = this_vp_p =
117 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap);
124 * We're not guaranteed that any but the first vnode
125 * are of our type. Check for and don't map any
126 * that aren't. (Must map first vp or vclean fails.)
129 if (i && (*this_vp_p)->v_op != umap_vnodeop_p) {
132 old_vps[i] = *this_vp_p;
133 *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
141 * Fix the credentials. (That's the purpose of this layer.)
144 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
146 credpp = VOPARG_OFFSETTO(struct ucred**,
147 descp->vdesc_cred_offset, ap);
149 /* Save old values */
151 savecredp = (*credpp);
152 if (savecredp != NOCRED)
153 (*credpp) = crdup(savecredp);
156 if (umap_bug_bypass && credp->cr_uid != 0)
157 printf("umap_bypass: user was %lu, group %lu\n",
158 (u_long)credp->cr_uid, (u_long)credp->cr_gid);
160 /* Map all ids in the credential structure. */
162 umap_mapids(vp1->v_mount, credp);
164 if (umap_bug_bypass && credp->cr_uid != 0)
165 printf("umap_bypass: user now %lu, group %lu\n",
166 (u_long)credp->cr_uid, (u_long)credp->cr_gid);
169 /* BSD often keeps a credential in the componentname structure
170 * for speed. If there is one, it better get mapped, too.
173 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
175 compnamepp = VOPARG_OFFSETTO(struct componentname**,
176 descp->vdesc_componentname_offset, ap);
178 compcredp = (*compnamepp)->cn_cred;
179 savecompcredp = compcredp;
180 if (savecompcredp != NOCRED)
181 (*compnamepp)->cn_cred = crdup(savecompcredp);
182 compcredp = (*compnamepp)->cn_cred;
184 if (umap_bug_bypass && compcredp->cr_uid != 0)
186 "umap_bypass: component credit user was %lu, group %lu\n",
187 (u_long)compcredp->cr_uid,
188 (u_long)compcredp->cr_gid);
190 /* Map all ids in the credential structure. */
192 umap_mapids(vp1->v_mount, compcredp);
194 if (umap_bug_bypass && compcredp->cr_uid != 0)
196 "umap_bypass: component credit user now %lu, group %lu\n",
197 (u_long)compcredp->cr_uid,
198 (u_long)compcredp->cr_gid);
202 * Call the operation on the lower layer
203 * with the modified argument structure.
205 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
208 * Maintain the illusion of call-by-value
209 * by restoring vnodes in the argument structure
210 * to their original value.
212 reles = descp->vdesc_flags;
213 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
214 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
215 break; /* bail out at end of list */
217 *(vps_p[i]) = old_vps[i];
224 * Map the possible out-going vpp
225 * (Assumes that the lower layer always returns
226 * a VREF'ed vpp unless it gets an error.)
228 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
229 !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
231 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
233 vppp = VOPARG_OFFSETTO(struct vnode***,
234 descp->vdesc_vpp_offset, ap);
236 error = umap_node_create(old_vps[0]->v_mount, **vppp, *vppp);
241 * Free duplicate cred structure and restore old one.
243 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
244 if (umap_bug_bypass && credp && credp->cr_uid != 0)
245 printf("umap_bypass: returning-user was %lu\n",
246 (u_long)credp->cr_uid);
248 if (savecredp != NOCRED) {
250 (*credpp) = savecredp;
251 if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
253 "umap_bypass: returning-user now %lu\n\n",
254 (u_long)(*credpp)->cr_uid);
258 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
259 if (umap_bug_bypass && compcredp && compcredp->cr_uid != 0)
261 "umap_bypass: returning-component-user was %lu\n",
262 (u_long)compcredp->cr_uid);
264 if (savecompcredp != NOCRED) {
266 (*compnamepp)->cn_cred = savecompcredp;
267 if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
269 "umap_bypass: returning-component-user now %lu\n",
270 (u_long)compcredp->cr_uid);
279 * We handle getattr to change the fsid.
283 struct vop_getattr_args /* {
286 struct ucred *a_cred;
291 int error, tmpid, nentries, gnentries;
292 u_long (*mapdata)[2], (*gmapdata)[2];
294 struct vnodeop_desc *descp = ap->a_desc;
296 error = umap_bypass((struct vop_generic_args *)ap);
301 * Umap needs to map the uid and gid returned by a stat
302 * into the proper values for this site. This involves
303 * finding the returned uid in the mapping information,
304 * translating it into the uid on the other end,
305 * and filling in the proper field in the vattr
306 * structure pointed to by ap->a_vap. The group
307 * is easier, since currently all groups will be
308 * translate to the NULLGROUP.
311 /* Find entry in map */
313 uid = ap->a_vap->va_uid;
314 gid = ap->a_vap->va_gid;
316 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
319 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
320 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
321 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
322 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
323 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
325 /* Reverse map the uid for the vnode. Since it's a reverse
326 map, we can't use umap_mapids() to do it. */
328 tmpid = umap_reverse_findid(uid, mapdata, nentries);
332 ap->a_vap->va_uid = (uid_t) tmpid;
334 printf("umap_getattr: original uid = %d\n", uid);
336 ap->a_vap->va_uid = (uid_t) NOBODY;
338 /* Reverse map the gid for the vnode. */
340 tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
344 ap->a_vap->va_gid = (gid_t) tmpid;
346 printf("umap_getattr: original gid = %d\n", gid);
348 ap->a_vap->va_gid = (gid_t) NULLGROUP;
354 * We need to process our own vnode lock and then clear the
355 * interlock flag as it applies only to our vnode, not the
356 * vnodes below us on the stack.
360 struct vop_lock_args /* {
362 lwkt_tokref_t a_vlock;
369 if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
371 ap->a_flags &= ~LK_INTERLOCK;
372 return (null_bypass((struct vop_generic_args *)ap));
376 * We need to process our own vnode unlock and then clear the
377 * interlock flag as it applies only to our vnode, not the
378 * vnodes below us on the stack.
382 struct vop_unlock_args /* {
389 ap->a_flags &= ~LK_INTERLOCK;
390 return (null_bypass((struct vop_generic_args *)ap));
395 struct vop_inactive_args /* {
400 struct vnode *vp = ap->a_vp;
401 struct umap_node *xp = VTOUMAP(vp);
402 struct vnode *lowervp = xp->umap_lowervp;
404 * Do nothing (and _don't_ bypass).
405 * Wait to vrele lowervp until reclaim,
406 * so that until then our umap_node is in the
407 * cache and reusable.
410 VOP_INACTIVE(lowervp, ap->a_td);
411 VOP_UNLOCK(ap->a_vp, NULL, 0, ap->a_td);
417 struct vop_reclaim_args /* {
421 struct vnode *vp = ap->a_vp;
422 struct umap_node *xp = VTOUMAP(vp);
423 struct vnode *lowervp = xp->umap_lowervp;
425 /* After this assignment, this node will not be re-used. */
426 xp->umap_lowervp = NULL;
427 LIST_REMOVE(xp, umap_hash);
428 FREE(vp->v_data, M_TEMP);
436 struct vop_print_args /* {
440 struct vnode *vp = ap->a_vp;
441 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, UMAPVPTOLOWERVP(vp));
447 struct vop_rename_args /* {
448 struct vnode *a_fdvp;
450 struct componentname *a_fcnp;
451 struct vnode *a_tdvp;
453 struct componentname *a_tcnp;
457 struct componentname *compnamep;
458 struct ucred *compcredp, *savecompcredp;
462 * Rename is irregular, having two componentname structures.
463 * We need to map the cre in the second structure,
464 * and then bypass takes care of the rest.
468 compnamep = ap->a_tcnp;
469 compcredp = compnamep->cn_cred;
471 savecompcredp = compcredp;
472 compcredp = compnamep->cn_cred = crdup(savecompcredp);
474 if (umap_bug_bypass && compcredp->cr_uid != 0)
476 "umap_rename: rename component credit user was %lu, group %lu\n",
477 (u_long)compcredp->cr_uid, (u_long)compcredp->cr_gid);
479 /* Map all ids in the credential structure. */
481 umap_mapids(vp->v_mount, compcredp);
483 if (umap_bug_bypass && compcredp->cr_uid != 0)
485 "umap_rename: rename component credit user now %lu, group %lu\n",
486 (u_long)compcredp->cr_uid, (u_long)compcredp->cr_gid);
488 error = umap_bypass((struct vop_generic_args *)ap);
490 /* Restore the additional mapped componentname cred structure. */
493 compnamep->cn_cred = savecompcredp;
499 * Global vfs data structures
502 * XXX - strategy, bwrite are hand coded currently. They should
503 * go away with a merged buffer/block cache.
506 vop_t **umap_vnodeop_p;
507 static struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
508 { &vop_default_desc, (vop_t *) umap_bypass },
509 { &vop_getattr_desc, (vop_t *) umap_getattr },
510 { &vop_inactive_desc, (vop_t *) umap_inactive },
511 { &vop_lock_desc, (vop_t *) umap_lock },
512 { &vop_print_desc, (vop_t *) umap_print },
513 { &vop_reclaim_desc, (vop_t *) umap_reclaim },
514 { &vop_rename_desc, (vop_t *) umap_rename },
515 { &vop_unlock_desc, (vop_t *) umap_unlock },
518 static struct vnodeopv_desc umap_vnodeop_opv_desc =
519 { &umap_vnodeop_p, umap_vnodeop_entries };
521 VNODEOP_SET(umap_vnodeop_opv_desc);