Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / vfs / umapfs / umap_vnops.c
1 /*
2  * Copyright (c) 1992, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * the UCLA Ficus project.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
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.2 2003/06/17 04:28:43 dillon Exp $
39  */
40
41 /*
42  * Umap Layer
43  */
44
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>
51 #include <sys/namei.h>
52 #include <sys/malloc.h>
53 #include <sys/buf.h>
54 #include <miscfs/umapfs/umap.h>
55 #include <miscfs/nullfs/null.h>
56
57 static int umap_bug_bypass = 0;   /* for debugging: enables bypass printf'ing */
58 SYSCTL_INT(_debug, OID_AUTO, umapfs_bug_bypass, CTLFLAG_RW,
59         &umap_bug_bypass, 0, "");
60
61 static int      umap_bypass __P((struct vop_generic_args *ap));
62 static int      umap_getattr __P((struct vop_getattr_args *ap));
63 static int      umap_inactive __P((struct vop_inactive_args *ap));
64 static int      umap_lock __P((struct vop_lock_args *ap));
65 static int      umap_print __P((struct vop_print_args *ap));
66 static int      umap_reclaim __P((struct vop_reclaim_args *ap));
67 static int      umap_rename __P((struct vop_rename_args *ap));
68 static int      umap_unlock __P((struct vop_unlock_args *ap));
69
70 /*
71  * This is the 10-Apr-92 bypass routine.
72  * See null_vnops.c:null_bypass for more details.
73  */
74 static int
75 umap_bypass(ap)
76         struct vop_generic_args /* {
77                 struct vnodeop_desc *a_desc;
78                 <other random data follows, presumably>
79         } */ *ap;
80 {
81         struct ucred **credpp = 0, *credp = 0;
82         struct ucred *savecredp = 0, *savecompcredp = 0;
83         struct ucred *compcredp = 0;
84         struct vnode **this_vp_p;
85         int error;
86         struct vnode *old_vps[VDESC_MAX_VPS];
87         struct vnode *vp1 = 0;
88         struct vnode **vps_p[VDESC_MAX_VPS];
89         struct vnode ***vppp;
90         struct vnodeop_desc *descp = ap->a_desc;
91         int reles, i;
92         struct componentname **compnamepp = 0;
93
94         if (umap_bug_bypass)
95                 printf ("umap_bypass: %s\n", descp->vdesc_name);
96
97 #ifdef DIAGNOSTIC
98         /*
99          * We require at least one vp.
100          */
101         if (descp->vdesc_vp_offsets == NULL ||
102             descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
103                 panic ("umap_bypass: no vp's in map");
104 #endif
105
106         /*
107          * Map the vnodes going in.
108          * Later, we'll invoke the operation based on
109          * the first mapped vnode's operation vector.
110          */
111         reles = descp->vdesc_flags;
112         for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
113                 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
114                         break;   /* bail out at end of list */
115                 vps_p[i] = this_vp_p =
116                         VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap);
117
118                 if (i == 0) {
119                         vp1 = *vps_p[0];
120                 }
121
122                 /*
123                  * We're not guaranteed that any but the first vnode
124                  * are of our type.  Check for and don't map any
125                  * that aren't.  (Must map first vp or vclean fails.)
126                  */
127
128                 if (i && (*this_vp_p)->v_op != umap_vnodeop_p) {
129                         old_vps[i] = NULL;
130                 } else {
131                         old_vps[i] = *this_vp_p;
132                         *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
133                         if (reles & 1)
134                                 VREF(*this_vp_p);
135                 }
136
137         }
138
139         /*
140          * Fix the credentials.  (That's the purpose of this layer.)
141          */
142
143         if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
144
145                 credpp = VOPARG_OFFSETTO(struct ucred**,
146                     descp->vdesc_cred_offset, ap);
147
148                 /* Save old values */
149
150                 savecredp = (*credpp);
151                 if (savecredp != NOCRED)
152                         (*credpp) = crdup(savecredp);
153                 credp = *credpp;
154
155                 if (umap_bug_bypass && credp->cr_uid != 0)
156                         printf("umap_bypass: user was %lu, group %lu\n",
157                             (u_long)credp->cr_uid, (u_long)credp->cr_gid);
158
159                 /* Map all ids in the credential structure. */
160
161                 umap_mapids(vp1->v_mount, credp);
162
163                 if (umap_bug_bypass && credp->cr_uid != 0)
164                         printf("umap_bypass: user now %lu, group %lu\n",
165                             (u_long)credp->cr_uid, (u_long)credp->cr_gid);
166         }
167
168         /* BSD often keeps a credential in the componentname structure
169          * for speed.  If there is one, it better get mapped, too.
170          */
171
172         if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
173
174                 compnamepp = VOPARG_OFFSETTO(struct componentname**,
175                     descp->vdesc_componentname_offset, ap);
176
177                 compcredp = (*compnamepp)->cn_cred;
178                 savecompcredp = compcredp;
179                 if (savecompcredp != NOCRED)
180                         (*compnamepp)->cn_cred = crdup(savecompcredp);
181                 compcredp = (*compnamepp)->cn_cred;
182
183                 if (umap_bug_bypass && compcredp->cr_uid != 0)
184                         printf(
185                     "umap_bypass: component credit user was %lu, group %lu\n",
186                             (u_long)compcredp->cr_uid,
187                             (u_long)compcredp->cr_gid);
188
189                 /* Map all ids in the credential structure. */
190
191                 umap_mapids(vp1->v_mount, compcredp);
192
193                 if (umap_bug_bypass && compcredp->cr_uid != 0)
194                         printf(
195                     "umap_bypass: component credit user now %lu, group %lu\n",
196                             (u_long)compcredp->cr_uid,
197                             (u_long)compcredp->cr_gid);
198         }
199
200         /*
201          * Call the operation on the lower layer
202          * with the modified argument structure.
203          */
204         error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap);
205
206         /*
207          * Maintain the illusion of call-by-value
208          * by restoring vnodes in the argument structure
209          * to their original value.
210          */
211         reles = descp->vdesc_flags;
212         for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
213                 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
214                         break;   /* bail out at end of list */
215                 if (old_vps[i]) {
216                         *(vps_p[i]) = old_vps[i];
217                         if (reles & 1)
218                                 vrele(*(vps_p[i]));
219                 };
220         };
221
222         /*
223          * Map the possible out-going vpp
224          * (Assumes that the lower layer always returns
225          * a VREF'ed vpp unless it gets an error.)
226          */
227         if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET &&
228             !(descp->vdesc_flags & VDESC_NOMAP_VPP) &&
229             !error) {
230                 if (descp->vdesc_flags & VDESC_VPP_WILLRELE)
231                         goto out;
232                 vppp = VOPARG_OFFSETTO(struct vnode***,
233                                  descp->vdesc_vpp_offset, ap);
234                 if (*vppp)
235                         error = umap_node_create(old_vps[0]->v_mount, **vppp, *vppp);
236         };
237
238  out:
239         /*
240          * Free duplicate cred structure and restore old one.
241          */
242         if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
243                 if (umap_bug_bypass && credp && credp->cr_uid != 0)
244                         printf("umap_bypass: returning-user was %lu\n",
245                             (u_long)credp->cr_uid);
246
247                 if (savecredp != NOCRED) {
248                         crfree(credp);
249                         (*credpp) = savecredp;
250                         if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
251                                 printf(
252                                     "umap_bypass: returning-user now %lu\n\n",
253                                     (u_long)(*credpp)->cr_uid);
254                 }
255         }
256
257         if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
258                 if (umap_bug_bypass && compcredp && compcredp->cr_uid != 0)
259                         printf(
260                             "umap_bypass: returning-component-user was %lu\n",
261                             (u_long)compcredp->cr_uid);
262
263                 if (savecompcredp != NOCRED) {
264                         crfree(compcredp);
265                         (*compnamepp)->cn_cred = savecompcredp;
266                         if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0)
267                                 printf(
268                             "umap_bypass: returning-component-user now %lu\n",
269                                     (u_long)compcredp->cr_uid);
270                 }
271         }
272
273         return (error);
274 }
275
276
277 /*
278  *  We handle getattr to change the fsid.
279  */
280 static int
281 umap_getattr(ap)
282         struct vop_getattr_args /* {
283                 struct vnode *a_vp;
284                 struct vattr *a_vap;
285                 struct ucred *a_cred;
286                 struct proc *a_p;
287         } */ *ap;
288 {
289         short uid, gid;
290         int error, tmpid, nentries, gnentries;
291         u_long (*mapdata)[2], (*gmapdata)[2];
292         struct vnode **vp1p;
293         struct vnodeop_desc *descp = ap->a_desc;
294
295         error = umap_bypass((struct vop_generic_args *)ap);
296         if (error)
297                 return (error);
298
299         /*
300          * Umap needs to map the uid and gid returned by a stat
301          * into the proper values for this site.  This involves
302          * finding the returned uid in the mapping information,
303          * translating it into the uid on the other end,
304          * and filling in the proper field in the vattr
305          * structure pointed to by ap->a_vap.  The group
306          * is easier, since currently all groups will be
307          * translate to the NULLGROUP.
308          */
309
310         /* Find entry in map */
311
312         uid = ap->a_vap->va_uid;
313         gid = ap->a_vap->va_gid;
314         if (umap_bug_bypass)
315                 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
316                     gid);
317
318         vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
319         nentries =  MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
320         mapdata =  (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
321         gnentries =  MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
322         gmapdata =  (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
323
324         /* Reverse map the uid for the vnode.  Since it's a reverse
325                 map, we can't use umap_mapids() to do it. */
326
327         tmpid = umap_reverse_findid(uid, mapdata, nentries);
328
329         if (tmpid != -1) {
330
331                 ap->a_vap->va_uid = (uid_t) tmpid;
332                 if (umap_bug_bypass)
333                         printf("umap_getattr: original uid = %d\n", uid);
334         } else
335                 ap->a_vap->va_uid = (uid_t) NOBODY;
336
337         /* Reverse map the gid for the vnode. */
338
339         tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
340
341         if (tmpid != -1) {
342
343                 ap->a_vap->va_gid = (gid_t) tmpid;
344                 if (umap_bug_bypass)
345                         printf("umap_getattr: original gid = %d\n", gid);
346         } else
347                 ap->a_vap->va_gid = (gid_t) NULLGROUP;
348
349         return (0);
350 }
351
352 /*
353  * We need to process our own vnode lock and then clear the
354  * interlock flag as it applies only to our vnode, not the
355  * vnodes below us on the stack.
356  */
357 static int
358 umap_lock(ap)
359         struct vop_lock_args /* {
360                 struct vnode *a_vp;
361                 int a_flags;
362                 struct proc *a_p;
363         } */ *ap;
364 {
365
366         vop_nolock(ap);
367         if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
368                 return (0);
369         ap->a_flags &= ~LK_INTERLOCK;
370         return (null_bypass((struct vop_generic_args *)ap));
371 }
372
373 /*
374  * We need to process our own vnode unlock and then clear the
375  * interlock flag as it applies only to our vnode, not the
376  * vnodes below us on the stack.
377  */
378 int
379 umap_unlock(ap)
380         struct vop_unlock_args /* {
381                 struct vnode *a_vp;
382                 int a_flags;
383                 struct proc *a_p;
384         } */ *ap;
385 {
386         vop_nounlock(ap);
387         ap->a_flags &= ~LK_INTERLOCK;
388         return (null_bypass((struct vop_generic_args *)ap));
389 }
390
391 static int
392 umap_inactive(ap)
393         struct vop_inactive_args /* {
394                 struct vnode *a_vp;
395                 struct proc *a_p;
396         } */ *ap;
397 {
398         struct vnode *vp = ap->a_vp;
399         struct umap_node *xp = VTOUMAP(vp);
400         struct vnode *lowervp = xp->umap_lowervp;
401         /*
402          * Do nothing (and _don't_ bypass).
403          * Wait to vrele lowervp until reclaim,
404          * so that until then our umap_node is in the
405          * cache and reusable.
406          *
407          */
408         VOP_INACTIVE(lowervp, ap->a_p);
409         VOP_UNLOCK(ap->a_vp, 0, ap->a_p);
410         return (0);
411 }
412
413 static int
414 umap_reclaim(ap)
415         struct vop_reclaim_args /* {
416                 struct vnode *a_vp;
417         } */ *ap;
418 {
419         struct vnode *vp = ap->a_vp;
420         struct umap_node *xp = VTOUMAP(vp);
421         struct vnode *lowervp = xp->umap_lowervp;
422
423         /* After this assignment, this node will not be re-used. */
424         xp->umap_lowervp = NULL;
425         LIST_REMOVE(xp, umap_hash);
426         FREE(vp->v_data, M_TEMP);
427         vp->v_data = NULL;
428         vrele(lowervp);
429         return (0);
430 }
431
432 static int
433 umap_print(ap)
434         struct vop_print_args /* {
435                 struct vnode *a_vp;
436         } */ *ap;
437 {
438         struct vnode *vp = ap->a_vp;
439         printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, UMAPVPTOLOWERVP(vp));
440         return (0);
441 }
442
443 static int
444 umap_rename(ap)
445         struct vop_rename_args  /* {
446                 struct vnode *a_fdvp;
447                 struct vnode *a_fvp;
448                 struct componentname *a_fcnp;
449                 struct vnode *a_tdvp;
450                 struct vnode *a_tvp;
451                 struct componentname *a_tcnp;
452         } */ *ap;
453 {
454         int error;
455         struct componentname *compnamep;
456         struct ucred *compcredp, *savecompcredp;
457         struct vnode *vp;
458
459         /*
460          * Rename is irregular, having two componentname structures.
461          * We need to map the cre in the second structure,
462          * and then bypass takes care of the rest.
463          */
464
465         vp = ap->a_fdvp;
466         compnamep = ap->a_tcnp;
467         compcredp = compnamep->cn_cred;
468
469         savecompcredp = compcredp;
470         compcredp = compnamep->cn_cred = crdup(savecompcredp);
471
472         if (umap_bug_bypass && compcredp->cr_uid != 0)
473                 printf(
474             "umap_rename: rename component credit user was %lu, group %lu\n",
475                     (u_long)compcredp->cr_uid, (u_long)compcredp->cr_gid);
476
477         /* Map all ids in the credential structure. */
478
479         umap_mapids(vp->v_mount, compcredp);
480
481         if (umap_bug_bypass && compcredp->cr_uid != 0)
482                 printf(
483             "umap_rename: rename component credit user now %lu, group %lu\n",
484                     (u_long)compcredp->cr_uid, (u_long)compcredp->cr_gid);
485
486         error = umap_bypass((struct vop_generic_args *)ap);
487
488         /* Restore the additional mapped componentname cred structure. */
489
490         crfree(compcredp);
491         compnamep->cn_cred = savecompcredp;
492
493         return error;
494 }
495
496 /*
497  * Global vfs data structures
498  */
499 /*
500  * XXX - strategy, bwrite are hand coded currently.  They should
501  * go away with a merged buffer/block cache.
502  *
503  */
504 vop_t **umap_vnodeop_p;
505 static struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
506         { &vop_default_desc,            (vop_t *) umap_bypass },
507         { &vop_getattr_desc,            (vop_t *) umap_getattr },
508         { &vop_inactive_desc,           (vop_t *) umap_inactive },
509         { &vop_lock_desc,               (vop_t *) umap_lock },
510         { &vop_print_desc,              (vop_t *) umap_print },
511         { &vop_reclaim_desc,            (vop_t *) umap_reclaim },
512         { &vop_rename_desc,             (vop_t *) umap_rename },
513         { &vop_unlock_desc,             (vop_t *) umap_unlock },
514         { NULL, NULL }
515 };
516 static struct vnodeopv_desc umap_vnodeop_opv_desc =
517         { &umap_vnodeop_p, umap_vnodeop_entries };
518
519 VNODEOP_SET(umap_vnodeop_opv_desc);