linux-updates-20060811
[openafs.git] / src / afs / LINUX / osi_cred.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * osi_cred.c - Linux cred handling routines.
12  *
13  */
14 #include <afsconfig.h>
15 #include "afs/param.h"
16
17 RCSID
18     ("$Header$");
19
20 #include "afs/sysincludes.h"
21 #include "afsincludes.h"
22
23 /* Setup a pool for creds. Allocate several at a time. */
24 #define CRED_ALLOC_STEP 29      /* at 140 bytes/cred = 4060 bytes. */
25
26
27 static cred_t *cred_pool = NULL;
28 int cred_allocs = 0;
29 int ncreds_inuse = 0;
30
31 /* Cred locking assumes current single threaded non-preemptive kernel.
32  * Also assuming a fast path through both down and up if no waiters. Otherwise,
33  * test if no creds in pool before grabbing lock in crfree().
34  */
35 #if defined(AFS_LINUX24_ENV)
36 static DECLARE_MUTEX(linux_cred_pool_lock);
37 #else
38 static struct semaphore linux_cred_pool_lock = MUTEX;
39 #endif
40 #define CRED_LOCK() down(&linux_cred_pool_lock)
41 #define CRED_UNLOCK() up(&linux_cred_pool_lock)
42
43 cred_t *
44 crget(void)
45 {
46     cred_t *tmp;
47     int i;
48
49     CRED_LOCK();
50     if (!cred_pool) {
51         cred_allocs++;
52         cred_pool = (cred_t *) osi_Alloc(CRED_ALLOC_STEP * sizeof(cred_t));
53         if (!cred_pool)
54             osi_Panic("crget: No more memory for creds!\n");
55
56         for (i = 0; i < CRED_ALLOC_STEP - 1; i++)
57             cred_pool[i].cr_next = (cred_t *) &cred_pool[i + 1];
58         cred_pool[i].cr_next = NULL;
59     }
60     tmp = cred_pool;
61     cred_pool = (cred_t *) tmp->cr_next;
62     ncreds_inuse++;
63     CRED_UNLOCK();
64
65     memset(tmp, 0, sizeof(cred_t));
66     tmp->cr_ref = 1;
67     return tmp;
68 }
69
70 void
71 crfree(cred_t * cr)
72 {
73     if (cr->cr_ref > 1) {
74         cr->cr_ref--;
75         return;
76     }
77
78 #if defined(AFS_LINUX26_ENV)
79     put_group_info(cr->cr_group_info);
80 #endif
81     CRED_LOCK();
82     cr->cr_next = (cred_t *) cred_pool;
83     cred_pool = cr;
84     CRED_UNLOCK();
85     ncreds_inuse--;
86 }
87
88
89 /* Return a duplicate of the cred. */
90 cred_t *
91 crdup(cred_t * cr)
92 {
93     cred_t *tmp = crget();
94
95     tmp->cr_uid = cr->cr_uid;
96     tmp->cr_ruid = cr->cr_ruid;
97     tmp->cr_gid = cr->cr_gid;
98
99 #if defined(AFS_LINUX26_ENV)
100     get_group_info(cr->cr_group_info);
101     tmp->cr_group_info = cr->cr_group_info;
102 #else
103     memcpy(tmp->cr_groups, cr->cr_groups, NGROUPS * sizeof(gid_t));
104     tmp->cr_ngroups = cr->cr_ngroups;
105 #endif
106
107     tmp->cr_ref = 1;
108     return tmp;
109 }
110
111 cred_t *
112 crref(void)
113 {
114     cred_t *cr = crget();
115
116     cr->cr_uid = current->fsuid;
117     cr->cr_ruid = current->uid;
118     cr->cr_gid = current->fsgid;
119     cr->cr_rgid = current->gid;
120
121 #if defined(AFS_LINUX26_ENV)
122     task_lock(current);
123     get_group_info(current->group_info);
124     cr->cr_group_info = current->group_info;
125     task_unlock(current);
126 #else
127     memcpy(cr->cr_groups, current->groups, NGROUPS * sizeof(gid_t));
128     cr->cr_ngroups = current->ngroups;
129 #endif
130     return cr;
131 }
132
133
134 /* Set the cred info into the current task */
135 void
136 crset(cred_t * cr)
137 {
138     current->fsuid = cr->cr_uid;
139     current->uid = cr->cr_ruid;
140     current->fsgid = cr->cr_gid;
141     current->gid = cr->cr_rgid;
142 #if defined(AFS_LINUX26_ENV)
143 {
144     struct group_info *old_info;
145
146     /* using set_current_groups() will sort the groups */
147     get_group_info(cr->cr_group_info);
148
149     task_lock(current);
150     old_info = current->group_info;
151     current->group_info = cr->cr_group_info;
152     task_unlock(current);
153
154     put_group_info(old_info);
155 }
156 #else
157     memcpy(current->groups, cr->cr_groups, NGROUPS * sizeof(gid_t));
158     current->ngroups = cr->cr_ngroups;
159 #endif
160 }