reindent-20030715
[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_ref = (long)&cred_pool[i + 1];
58         cred_pool[i].cr_ref = 0;
59     }
60     tmp = cred_pool;
61     cred_pool = (cred_t *) tmp->cr_ref;
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     CRED_LOCK();
79     cr->cr_ref = (long)cred_pool;
80     cred_pool = cr;
81     CRED_UNLOCK();
82     ncreds_inuse--;
83 }
84
85
86 /* Return a duplicate of the cred. */
87 cred_t *
88 crdup(cred_t * cr)
89 {
90     cred_t *tmp = crget();
91     *tmp = *cr;
92     tmp->cr_ref = 1;
93     return tmp;
94 }
95
96 cred_t *
97 crref(void)
98 {
99     cred_t *cr = crget();
100     cr->cr_uid = current->fsuid;
101     cr->cr_ruid = current->uid;
102     cr->cr_gid = current->fsgid;
103     cr->cr_rgid = current->gid;
104     memcpy(cr->cr_groups, current->groups, NGROUPS * sizeof(gid_t));
105     cr->cr_ngroups = current->ngroups;
106     return cr;
107 }
108
109
110 /* Set the cred info into the current task */
111 void
112 crset(cred_t * cr)
113 {
114     current->fsuid = cr->cr_uid;
115     current->uid = cr->cr_ruid;
116     current->fsgid = cr->cr_gid;
117     current->gid = cr->cr_rgid;
118     memcpy(current->groups, cr->cr_groups, NGROUPS * sizeof(gid_t));
119     current->ngroups = cr->cr_ngroups;
120 }