- pre1: (for ISDN synchronization _ONLY_! Not complete!)
[davej-history.git] / kernel / user.c
blobd033c9659717f2cb6ce36e092f74561b9005941b
1 /*
2 * The "user cache".
4 * (C) Copyright 1991-2000 Linus Torvalds
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 */
11 #include <linux/config.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
17 * UID task count cache, to get fast user lookup in "alloc_uid"
18 * when changing user ID's (ie setuid() and friends).
20 #define UIDHASH_BITS 8
21 #define UIDHASH_SZ (1 << UIDHASH_BITS)
22 #define UIDHASH_MASK (UIDHASH_SZ - 1)
23 #define __uidhashfn(uid) (((uid >> UIDHASH_BITS) ^ uid) & UIDHASH_MASK)
24 #define uidhashentry(uid) (uidhash_table + __uidhashfn(uid))
26 static kmem_cache_t *uid_cachep;
27 static struct user_struct *uidhash_table[UIDHASH_SZ];
28 static spinlock_t uidhash_lock = SPIN_LOCK_UNLOCKED;
30 struct user_struct root_user = {
31 __count: ATOMIC_INIT(1),
32 processes: ATOMIC_INIT(1),
33 files: ATOMIC_INIT(0)
37 * These routines must be called with the uidhash spinlock held!
39 static inline void uid_hash_insert(struct user_struct *up, struct user_struct **hashent)
41 struct user_struct *next = *hashent;
43 up->next = next;
44 if (next)
45 next->pprev = &up->next;
46 up->pprev = hashent;
47 *hashent = up;
50 static inline void uid_hash_remove(struct user_struct *up)
52 struct user_struct *next = up->next;
53 struct user_struct **pprev = up->pprev;
55 if (next)
56 next->pprev = pprev;
57 *pprev = next;
60 static inline struct user_struct *uid_hash_find(uid_t uid, struct user_struct **hashent)
62 struct user_struct *next;
64 next = *hashent;
65 for (;;) {
66 struct user_struct *up = next;
67 if (next) {
68 next = up->next;
69 if (up->uid != uid)
70 continue;
71 atomic_inc(&up->__count);
73 return up;
78 * For SMP, we need to re-test the user struct counter
79 * after having acquired the spinlock. This allows us to do
80 * the common case (not freeing anything) without having
81 * any locking.
83 #ifdef CONFIG_SMP
84 #define uid_hash_free(up) (!atomic_read(&(up)->__count))
85 #else
86 #define uid_hash_free(up) (1)
87 #endif
89 void free_uid(struct user_struct *up)
91 if (up) {
92 if (atomic_dec_and_test(&up->__count)) {
93 spin_lock(&uidhash_lock);
94 if (uid_hash_free(up)) {
95 uid_hash_remove(up);
96 kmem_cache_free(uid_cachep, up);
98 spin_unlock(&uidhash_lock);
103 struct user_struct * alloc_uid(uid_t uid)
105 struct user_struct **hashent = uidhashentry(uid);
106 struct user_struct *up;
108 spin_lock(&uidhash_lock);
109 up = uid_hash_find(uid, hashent);
110 spin_unlock(&uidhash_lock);
112 if (!up) {
113 struct user_struct *new;
115 new = kmem_cache_alloc(uid_cachep, SLAB_KERNEL);
116 if (!new)
117 return NULL;
118 new->uid = uid;
119 atomic_set(&new->__count, 1);
120 atomic_set(&new->processes, 0);
121 atomic_set(&new->files, 0);
124 * Before adding this, check whether we raced
125 * on adding the same user already..
127 spin_lock(&uidhash_lock);
128 up = uid_hash_find(uid, hashent);
129 if (up) {
130 kmem_cache_free(uid_cachep, new);
131 } else {
132 uid_hash_insert(new, hashent);
133 up = new;
135 spin_unlock(&uidhash_lock);
138 return up;
142 static int __init uid_cache_init(void)
144 uid_cachep = kmem_cache_create("uid_cache", sizeof(struct user_struct),
146 SLAB_HWCACHE_ALIGN, NULL, NULL);
147 if(!uid_cachep)
148 panic("Cannot create uid taskcount SLAB cache\n");
150 /* Insert the root user immediately - init already runs with this */
151 uid_hash_insert(&root_user, uidhashentry(0));
152 return 0;
155 module_init(uid_cache_init);