vserver 1.9.3
[linux-2.6.git] / kernel / user.c
1 /*
2  * The "user cache".
3  *
4  * (C) Copyright 1991-2000 Linus Torvalds
5  *
6  * We have a per-user structure to keep track of how many
7  * processes, files etc the user has claimed, in order to be
8  * able to have per-user limits for system resources. 
9  */
10
11 #include <linux/init.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/bitops.h>
15
16 /*
17  * UID task count cache, to get fast user lookup in "alloc_uid"
18  * when changing user ID's (ie setuid() and friends).
19  */
20 #define UIDHASH_BITS            8
21 #define UIDHASH_SZ              (1 << UIDHASH_BITS)
22 #define UIDHASH_MASK            (UIDHASH_SZ - 1)
23 #define __uidhashfn(xid,uid)    ((((uid) >> UIDHASH_BITS) + ((uid)^(xid))) & UIDHASH_MASK)
24 #define uidhashentry(xid,uid)   (uidhash_table + __uidhashfn((xid),(uid)))
25
26 static kmem_cache_t *uid_cachep;
27 static struct list_head uidhash_table[UIDHASH_SZ];
28 static spinlock_t uidhash_lock = SPIN_LOCK_UNLOCKED;
29
30 struct user_struct root_user = {
31         .__count        = ATOMIC_INIT(1),
32         .processes      = ATOMIC_INIT(1),
33         .files          = ATOMIC_INIT(0),
34         .sigpending     = ATOMIC_INIT(0),
35         .mq_bytes       = 0,
36         .locked_shm     = 0,
37 };
38
39 /*
40  * These routines must be called with the uidhash spinlock held!
41  */
42 static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
43 {
44         list_add(&up->uidhash_list, hashent);
45 }
46
47 static inline void uid_hash_remove(struct user_struct *up)
48 {
49         list_del(&up->uidhash_list);
50 }
51
52 static inline struct user_struct *uid_hash_find(xid_t xid, uid_t uid, struct list_head *hashent)
53 {
54         struct list_head *up;
55
56         list_for_each(up, hashent) {
57                 struct user_struct *user;
58
59                 user = list_entry(up, struct user_struct, uidhash_list);
60
61                 if(user->uid == uid && user->xid == xid) {
62                         atomic_inc(&user->__count);
63                         return user;
64                 }
65         }
66
67         return NULL;
68 }
69
70 /*
71  * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
72  * caller must undo that ref with free_uid().
73  *
74  * If the user_struct could not be found, return NULL.
75  */
76 struct user_struct *find_user(xid_t xid, uid_t uid)
77 {
78         struct user_struct *ret;
79
80         spin_lock(&uidhash_lock);
81         ret = uid_hash_find(xid, uid, uidhashentry(xid, uid));
82         spin_unlock(&uidhash_lock);
83         return ret;
84 }
85
86 void free_uid(struct user_struct *up)
87 {
88         if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
89                 uid_hash_remove(up);
90                 kmem_cache_free(uid_cachep, up);
91                 spin_unlock(&uidhash_lock);
92         }
93 }
94
95 struct user_struct * alloc_uid(xid_t xid, uid_t uid)
96 {
97         struct list_head *hashent = uidhashentry(xid, uid);
98         struct user_struct *up;
99
100         spin_lock(&uidhash_lock);
101         up = uid_hash_find(xid, uid, hashent);
102         spin_unlock(&uidhash_lock);
103
104         if (!up) {
105                 struct user_struct *new;
106
107                 new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
108                 if (!new)
109                         return NULL;
110                 new->uid = uid;
111                 new->xid = xid;
112                 atomic_set(&new->__count, 1);
113                 atomic_set(&new->processes, 0);
114                 atomic_set(&new->files, 0);
115                 atomic_set(&new->sigpending, 0);
116
117                 new->mq_bytes = 0;
118                 new->locked_shm = 0;
119
120                 /*
121                  * Before adding this, check whether we raced
122                  * on adding the same user already..
123                  */
124                 spin_lock(&uidhash_lock);
125                 up = uid_hash_find(xid, uid, hashent);
126                 if (up) {
127                         kmem_cache_free(uid_cachep, new);
128                 } else {
129                         uid_hash_insert(new, hashent);
130                         up = new;
131                 }
132                 spin_unlock(&uidhash_lock);
133
134         }
135         return up;
136 }
137
138 void switch_uid(struct user_struct *new_user)
139 {
140         struct user_struct *old_user;
141
142         /* What if a process setreuid()'s and this brings the
143          * new uid over his NPROC rlimit?  We can check this now
144          * cheaply with the new uid cache, so if it matters
145          * we should be checking for it.  -DaveM
146          */
147         old_user = current->user;
148         atomic_inc(&new_user->processes);
149         atomic_dec(&old_user->processes);
150         current->user = new_user;
151         free_uid(old_user);
152 }
153
154
155 static int __init uid_cache_init(void)
156 {
157         int n;
158
159         uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
160                         0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
161
162         for(n = 0; n < UIDHASH_SZ; ++n)
163                 INIT_LIST_HEAD(uidhash_table + n);
164
165         /* Insert the root user immediately (init already runs as root) */
166         spin_lock(&uidhash_lock);
167         uid_hash_insert(&root_user, uidhashentry(0,0));
168         spin_unlock(&uidhash_lock);
169
170         return 0;
171 }
172
173 module_init(uid_cache_init);