VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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 };
37
38 /*
39  * These routines must be called with the uidhash spinlock held!
40  */
41 static inline void uid_hash_insert(struct user_struct *up, struct list_head *hashent)
42 {
43         list_add(&up->uidhash_list, hashent);
44 }
45
46 static inline void uid_hash_remove(struct user_struct *up)
47 {
48         list_del(&up->uidhash_list);
49 }
50
51 static inline struct user_struct *uid_hash_find(xid_t xid, uid_t uid, struct list_head *hashent)
52 {
53         struct list_head *up;
54
55         list_for_each(up, hashent) {
56                 struct user_struct *user;
57
58                 user = list_entry(up, struct user_struct, uidhash_list);
59
60                 if(user->uid == uid && user->xid == xid) {
61                         atomic_inc(&user->__count);
62                         return user;
63                 }
64         }
65
66         return NULL;
67 }
68
69 /*
70  * Locate the user_struct for the passed UID.  If found, take a ref on it.  The
71  * caller must undo that ref with free_uid().
72  *
73  * If the user_struct could not be found, return NULL.
74  */
75 struct user_struct *find_user(xid_t xid, uid_t uid)
76 {
77         struct user_struct *ret;
78
79         spin_lock(&uidhash_lock);
80         ret = uid_hash_find(xid, uid, uidhashentry(xid, uid));
81         spin_unlock(&uidhash_lock);
82         return ret;
83 }
84
85 void free_uid(struct user_struct *up)
86 {
87         if (up && atomic_dec_and_lock(&up->__count, &uidhash_lock)) {
88                 uid_hash_remove(up);
89                 kmem_cache_free(uid_cachep, up);
90                 spin_unlock(&uidhash_lock);
91         }
92 }
93
94 struct user_struct * alloc_uid(xid_t xid, uid_t uid)
95 {
96         struct list_head *hashent = uidhashentry(xid, uid);
97         struct user_struct *up;
98
99         spin_lock(&uidhash_lock);
100         up = uid_hash_find(xid, uid, hashent);
101         spin_unlock(&uidhash_lock);
102
103         if (!up) {
104                 struct user_struct *new;
105
106                 new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
107                 if (!new)
108                         return NULL;
109                 new->uid = uid;
110                 new->xid = xid;
111                 atomic_set(&new->__count, 1);
112                 atomic_set(&new->processes, 0);
113                 atomic_set(&new->files, 0);
114                 atomic_set(&new->sigpending, 0);
115
116                 new->mq_bytes = 0;
117
118                 /*
119                  * Before adding this, check whether we raced
120                  * on adding the same user already..
121                  */
122                 spin_lock(&uidhash_lock);
123                 up = uid_hash_find(xid, uid, hashent);
124                 if (up) {
125                         kmem_cache_free(uid_cachep, new);
126                 } else {
127                         uid_hash_insert(new, hashent);
128                         up = new;
129                 }
130                 spin_unlock(&uidhash_lock);
131
132         }
133         return up;
134 }
135
136 void switch_uid(struct user_struct *new_user)
137 {
138         struct user_struct *old_user;
139
140         /* What if a process setreuid()'s and this brings the
141          * new uid over his NPROC rlimit?  We can check this now
142          * cheaply with the new uid cache, so if it matters
143          * we should be checking for it.  -DaveM
144          */
145         old_user = current->user;
146         atomic_inc(&new_user->processes);
147         atomic_dec(&old_user->processes);
148         current->user = new_user;
149         free_uid(old_user);
150 }
151
152
153 static int __init uid_cache_init(void)
154 {
155         int n;
156
157         uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
158                         0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
159
160         for(n = 0; n < UIDHASH_SZ; ++n)
161                 INIT_LIST_HEAD(uidhash_table + n);
162
163         /* Insert the root user immediately (init already runs as root) */
164         spin_lock(&uidhash_lock);
165         uid_hash_insert(&root_user, uidhashentry(0,0));
166         spin_unlock(&uidhash_lock);
167
168         return 0;
169 }
170
171 module_init(uid_cache_init);