Merge from vendor branch TEXINFO:
[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.14 2005/02/15 08:32:19 joerg 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/proc.h>
52 #include <sys/namei.h>
53 #include <sys/malloc.h>
54 #include <sys/buf.h>
55 #include "umap.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 (struct vop_generic_args *ap);
62 static int      umap_getattr (struct vop_getattr_args *ap);
63 static int      umap_inactive (struct vop_inactive_args *ap);
64 static int      umap_lock (struct vop_lock_args *ap);
65 static int      umap_print (struct vop_print_args *ap);
66 static int      umap_reclaim (struct vop_reclaim_args *ap);
67 static int      umap_rename (struct vop_rename_args *ap);
68 static int      umap_unlock (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  * umap_bypass(struct vnodeop_desc *a_desc, ...)
75  */
76 static int
77 umap_bypass(struct vop_generic_args *ap)
78 {
79         struct ucred **credpp = 0, *credp = 0;
80         struct ucred *savecredp = 0, *savecompcredp = 0;
81         struct ucred *compcredp = 0;
82         struct vnode **this_vp_p;
83         int error;
84         struct vnode *old_vps[VDESC_MAX_VPS];
85         struct vnode *vp1 = 0;
86         struct vnode **vps_p[VDESC_MAX_VPS];
87         struct vnode ***vppp;
88         struct vnodeop_desc *descp = ap->a_desc;
89         int reles, i;
90         struct componentname **compnamepp = 0;
91
92         if (umap_bug_bypass)
93                 printf ("umap_bypass: %s\n", descp->vdesc_name);
94
95 #ifdef DIAGNOSTIC
96         /*
97          * We require at least one vp.
98          */
99         if (descp->vdesc_vp_offsets == NULL ||
100             descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
101                 panic ("umap_bypass: no vp's in map");
102 #endif
103
104         /*
105          * Map the vnodes going in.
106          * Later, we'll invoke the operation based on
107          * the first mapped vnode's operation vector.
108          */
109         reles = descp->vdesc_flags;
110         for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
111                 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
112                         break;   /* bail out at end of list */
113                 vps_p[i] = this_vp_p =
114                         VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap);
115
116                 if (i == 0) {
117                         vp1 = *vps_p[0];
118                 }
119
120                 /*
121                  * We're not guaranteed that any but the first vnode
122                  * are of our type.  Check for and don't map any
123                  * that aren't.  (Must map first vp or vclean fails.)
124                  */
125
126                 if (i && (*this_vp_p)->v_tag != VT_UMAP) {
127                         old_vps[i] = NULL;
128                 } else {
129                         old_vps[i] = *this_vp_p;
130                         *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
131                         if (reles & 1)
132                                 vref(*this_vp_p);
133                 }
134
135         }
136
137         /*
138          * Fix the credentials.  (That's the purpose of this layer.)
139          */
140
141         if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
142
143                 credpp = VOPARG_OFFSETTO(struct ucred**,
144                     descp->vdesc_cred_offset, ap);
145
146                 /* Save old values */
147
148                 savecredp = (*credpp);
149                 if (savecredp != NOCRED)
150                         (*credpp) = crdup(savecredp);
151                 credp = *credpp;
152
153                 if (umap_bug_bypass && credp->cr_uid != 0)
154                         printf("umap_bypass: user was %lu, group %lu\n",
155                             (u_long)credp->cr_uid, (u_long)credp->cr_gid);
156
157                 /* Map all ids in the credential structure. */
158
159                 umap_mapids(vp1->v_mount, credp);
160
161                 if (umap_bug_bypass && credp->cr_uid != 0)
162                         printf("umap_bypass: user now %lu, group %lu\n",
163                             (u_long)credp->cr_uid, (u_long)credp->cr_gid);
164         }
165
166         /* BSD often keeps a credential in the componentname structure
167          * for speed.  If there is one, it better get mapped, too.
168          */
169
170         if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
171
172                 compnamepp = VOPARG_OFFSETTO(struct componentname**,
173                     descp->vdesc_componentname_offset, ap);
174
175                 compcredp = (*compnamepp)->cn_cred;
176                 savecompcredp = compcredp;
177                 if (savecompcredp != NOCRED)
178                         (*compnamepp)->cn_cred = crdup(savecompcredp);
179                 compcredp = (*compnamepp)->cn_cred;
180
181                 if (umap_bug_bypass && compcredp->cr_uid != 0)
182                         printf(
183                     "umap_bypass: component credit user was %lu, group %lu\n",
184                             (u_long)compcredp->cr_uid,
185                             (u_long)compcredp->cr_gid);
186
187                 /* Map all ids in the credential structure. */
188
189                 umap_mapids(vp1->v_mount, compcredp);
190
191                 if (umap_bug_bypass && compcredp->cr_uid != 0)
192                         printf(
193                     "umap_bypass: component credit user now %lu, group %lu\n",
194                             (u_long)compcredp->cr_uid,
195                             (u_long)compcredp->cr_gid);
196         }
197
198         /*
199          * Call the operation on the lower layer with the modified argument
200          * structure.  We have to adjust a_fm to point at the lower layer.
201          */
202         ap->a_ops = *(*(vps_p[0]))->v_ops;
203         error = vop_vnoperate_ap(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  * umap_getattr(struct vnode *a_vp, struct vattr *a_vap, struct ucred *a_cred,
280  *              struct thread *a_td)
281  */
282 static int
283 umap_getattr(struct vop_getattr_args *ap)
284 {
285         short uid, gid;
286         int error, tmpid, nentries, gnentries;
287         u_long (*mapdata)[2], (*gmapdata)[2];
288         struct vnode **vp1p;
289         struct vnodeop_desc *descp = ap->a_head.a_desc;
290
291         error = umap_bypass(&ap->a_head);
292         if (error)
293                 return (error);
294
295         /*
296          * Umap needs to map the uid and gid returned by a stat
297          * into the proper values for this site.  This involves
298          * finding the returned uid in the mapping information,
299          * translating it into the uid on the other end,
300          * and filling in the proper field in the vattr
301          * structure pointed to by ap->a_vap.  The group
302          * is easier, since currently all groups will be
303          * translate to the NULLGROUP.
304          */
305
306         /* Find entry in map */
307
308         uid = ap->a_vap->va_uid;
309         gid = ap->a_vap->va_gid;
310         if (umap_bug_bypass)
311                 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
312                     gid);
313
314         vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
315         nentries =  MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
316         mapdata =  (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
317         gnentries =  MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
318         gmapdata =  (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
319
320         /* Reverse map the uid for the vnode.  Since it's a reverse
321                 map, we can't use umap_mapids() to do it. */
322
323         tmpid = umap_reverse_findid(uid, mapdata, nentries);
324
325         if (tmpid != -1) {
326
327                 ap->a_vap->va_uid = (uid_t) tmpid;
328                 if (umap_bug_bypass)
329                         printf("umap_getattr: original uid = %d\n", uid);
330         } else
331                 ap->a_vap->va_uid = (uid_t) NOBODY;
332
333         /* Reverse map the gid for the vnode. */
334
335         tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
336
337         if (tmpid != -1) {
338
339                 ap->a_vap->va_gid = (gid_t) tmpid;
340                 if (umap_bug_bypass)
341                         printf("umap_getattr: original gid = %d\n", gid);
342         } else
343                 ap->a_vap->va_gid = (gid_t) NULLGROUP;
344
345         return (0);
346 }
347
348 /*
349  * We need to process our own vnode lock and then clear the
350  * interlock flag as it applies only to our vnode, not the
351  * vnodes below us on the stack.
352  *
353  * umap_lock(struct vnode *a_vp, int a_flags, struct thread *a_td)
354  */
355 static int
356 umap_lock(struct vop_lock_args *ap)
357 {
358         if ((ap->a_flags & LK_TYPE_MASK) == LK_DRAIN)
359                 return (0);
360         return (null_bypass(&ap->a_head));
361 }
362
363 /*
364  * We need to process our own vnode unlock and then clear the
365  * interlock flag as it applies only to our vnode, not the
366  * vnodes below us on the stack.
367  *
368  * umap_unlock(struct vnode *a_vp, int a_flags, struct thread *a_td)
369  */
370 int
371 umap_unlock(struct vop_unlock_args *ap)
372 {
373         return (null_bypass(&ap->a_head));
374 }
375
376 /*
377  * umap_inactive(struct vnode *a_vp, struct thread *a_td)
378  */
379 static int
380 umap_inactive(struct vop_inactive_args *ap)
381 {
382         struct vnode *vp = ap->a_vp;
383         struct umap_node *xp = VTOUMAP(vp);
384         struct vnode *lowervp = xp->umap_lowervp;
385         /*
386          * Do nothing (and _don't_ bypass).
387          * Wait to vrele lowervp until reclaim,
388          * so that until then our umap_node is in the
389          * cache and reusable.
390          *
391          */
392         VOP_INACTIVE(lowervp, ap->a_td);
393         return (0);
394 }
395
396 /*
397  * umap_reclaim(struct vnode *a_vp)
398  */
399 static int
400 umap_reclaim(struct vop_reclaim_args *ap)
401 {
402         struct vnode *vp = ap->a_vp;
403         struct umap_node *xp = VTOUMAP(vp);
404         struct vnode *lowervp = xp->umap_lowervp;
405
406         /* After this assignment, this node will not be re-used. */
407         vp->v_data = NULL;
408         umap_node_delete(xp);
409         xp->umap_lowervp = NULL;
410         free(xp, M_TEMP);
411         vrele(lowervp);
412         return (0);
413 }
414
415 /*
416  * umap_print(struct vop_print_args *ap)
417  */
418 static int
419 umap_print(struct vop_print_args *ap)
420 {
421         struct vnode *vp = ap->a_vp;
422         printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, UMAPVPTOLOWERVP(vp));
423         return (0);
424 }
425
426 /*
427  * umap_rename(struct vnode *a_fdvp, struct vnode *a_fvp,
428  *              struct componentname *a_fcnp, struct vnode *a_tdvp,
429  *              struct vnode *a_tvp, struct componentname *a_tcnp)
430  */
431 static int
432 umap_rename(struct vop_rename_args *ap)
433 {
434         int error;
435         struct componentname *compnamep;
436         struct ucred *compcredp, *savecompcredp;
437         struct vnode *vp;
438
439         /*
440          * Rename is irregular, having two componentname structures.
441          * We need to map the cre in the second structure,
442          * and then bypass takes care of the rest.
443          */
444
445         vp = ap->a_fdvp;
446         compnamep = ap->a_tcnp;
447         compcredp = compnamep->cn_cred;
448
449         savecompcredp = compcredp;
450         compcredp = compnamep->cn_cred = crdup(savecompcredp);
451
452         if (umap_bug_bypass && compcredp->cr_uid != 0)
453                 printf(
454             "umap_rename: rename component credit user was %lu, group %lu\n",
455                     (u_long)compcredp->cr_uid, (u_long)compcredp->cr_gid);
456
457         /* Map all ids in the credential structure. */
458
459         umap_mapids(vp->v_mount, compcredp);
460
461         if (umap_bug_bypass && compcredp->cr_uid != 0)
462                 printf(
463             "umap_rename: rename component credit user now %lu, group %lu\n",
464                     (u_long)compcredp->cr_uid, (u_long)compcredp->cr_gid);
465
466         error = umap_bypass(&ap->a_head);
467
468         /* Restore the additional mapped componentname cred structure. */
469
470         crfree(compcredp);
471         compnamep->cn_cred = savecompcredp;
472
473         return error;
474 }
475
476 /*
477  * Global vfs data structures
478  */
479 /*
480  * XXX - strategy, bwrite are hand coded currently.  They should
481  * go away with a merged buffer/block cache.
482  *
483  */
484 struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
485         { &vop_default_desc,            (vnodeopv_entry_t) umap_bypass },
486         { &vop_getattr_desc,            (vnodeopv_entry_t) umap_getattr },
487         { &vop_inactive_desc,           (vnodeopv_entry_t) umap_inactive },
488         { &vop_lock_desc,               (vnodeopv_entry_t) umap_lock },
489         { &vop_print_desc,              (vnodeopv_entry_t) umap_print },
490         { &vop_reclaim_desc,            (vnodeopv_entry_t) umap_reclaim },
491         { &vop_rename_desc,             (vnodeopv_entry_t) umap_rename },
492         { &vop_unlock_desc,             (vnodeopv_entry_t) umap_unlock },
493         { NULL, NULL }
494 };
495