Linux: Use atomics for credential reference counts
[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
18 #include "afs/sysincludes.h"
19 #include "afsincludes.h"
20
21 /* Copy one credential structure to another, being careful about references */
22 static inline void
23 afs_copy_creds(cred_t *to_cred, const cred_t *from_cred) {
24     afs_set_cr_uid(to_cred, afs_cr_uid(from_cred));
25     afs_set_cr_gid(to_cred, afs_cr_gid(from_cred));
26     afs_set_cr_ruid(to_cred, afs_cr_ruid(from_cred));
27     afs_set_cr_rgid(to_cred, afs_cr_rgid(from_cred));
28     get_group_info(afs_cr_group_info(from_cred));
29     afs_set_cr_group_info(to_cred, afs_cr_group_info(from_cred));
30 }
31
32 cred_t *
33 crget(void)
34 {
35     cred_t *tmp;
36     
37 #if !defined(GFP_NOFS)
38 #define GFP_NOFS GFP_KERNEL
39 #endif
40     tmp = kmalloc(sizeof(cred_t), GFP_NOFS);
41     memset(tmp, 0, sizeof(cred_t));
42     if (!tmp)
43         osi_Panic("crget: No more memory for creds!\n");
44
45 #if defined(STRUCT_TASK_HAS_CRED)
46     get_cred(tmp);
47 #else
48     atomic_set(&tmp->cr_ref, 1);
49 #endif
50     return tmp;
51 }
52
53 void
54 crfree(cred_t * cr)
55 {
56 #if defined(STRUCT_TASK_HAS_CRED)
57     put_cred(cr);
58 #else
59     if (atomic_dec_and_test(&cr->cr_ref)) {
60         put_group_info(afs_cr_group_info(cr));
61         kfree(cr);
62     }
63 #endif
64 }
65
66
67 /* Return a duplicate of the cred. */
68 cred_t *
69 crdup(cred_t * cr)
70 {
71     cred_t *tmp = crget();
72 #if defined(STRUCT_TASK_HAS_CRED)
73     afs_copy_creds(tmp, cr);
74 #else
75     afs_set_cr_uid(tmp, afs_cr_uid(cr));
76     afs_set_cr_ruid(tmp, afs_cr_ruid(cr));
77     afs_set_cr_gid(tmp, afs_cr_gid(cr));
78     afs_set_cr_rgid(tmp, afs_cr_rgid(cr));
79
80     get_group_info(afs_cr_group_info(cr));
81     afs_set_cr_group_info(tmp, afs_cr_group_info(cr));
82 #endif
83     return tmp;
84 }
85
86 cred_t *
87 crref(void)
88 {
89 #if defined(STRUCT_TASK_HAS_CRED)
90     return (cred_t *)get_current_cred();
91 #else
92     cred_t *cr = crget();
93
94     afs_set_cr_uid(cr, current_fsuid());
95     afs_set_cr_ruid(cr, current_uid());
96     afs_set_cr_gid(cr, current_fsgid());
97     afs_set_cr_rgid(cr, current_gid());
98
99     task_lock(current);
100     get_group_info(current_group_info());
101     afs_set_cr_group_info(cr, current_group_info());
102     task_unlock(current);
103
104     return cr;
105 #endif
106 }
107
108 /* Set the cred info into the current task */
109 void
110 crset(cred_t * cr)
111 {
112 #if defined(STRUCT_TASK_HAS_CRED)
113     struct cred *new_creds;
114
115     /* If our current task doesn't have identical real and effective
116      * credentials, commit_cred won't let us change them, so we just
117      * bail here.
118      */
119     if (current->cred != current->real_cred)
120         return;
121     new_creds = prepare_creds();
122     /* Drop the reference to group_info - we'll overwrite it in afs_copy_creds */
123     put_group_info(new_creds->group_info);
124     afs_copy_creds(new_creds, current_cred());
125
126     commit_creds(new_creds);
127 #else
128     struct group_info *old_info;
129
130     current->fsuid = afs_cr_uid(cr);
131     current->uid = afs_cr_ruid(cr);
132     current->fsgid = afs_cr_gid(cr);
133     current->gid = afs_cr_rgid(cr);
134
135     get_group_info(afs_cr_group_info(cr));
136     task_lock(current);
137     old_info = current->group_info;
138     current->group_info = afs_cr_group_info(cr);
139     task_unlock(current);
140     put_group_info(old_info);
141 #endif
142 }