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