Linux 2.6.18.4
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / file.c
blobb3c6b82e6a9d8629446e77268a83a2c5c4eb1b62
1 /*
2 * linux/fs/file.c
4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
6 * Manage the dynamic fd arrays in the process files_struct.
7 */
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/time.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
14 #include <linux/file.h>
15 #include <linux/bitops.h>
16 #include <linux/interrupt.h>
17 #include <linux/spinlock.h>
18 #include <linux/rcupdate.h>
19 #include <linux/workqueue.h>
21 struct fdtable_defer {
22 spinlock_t lock;
23 struct work_struct wq;
24 struct timer_list timer;
25 struct fdtable *next;
29 * We use this list to defer free fdtables that have vmalloced
30 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
31 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
32 * this per-task structure.
34 static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
38 * Allocate an fd array, using kmalloc or vmalloc.
39 * Note: the array isn't cleared at allocation time.
41 struct file ** alloc_fd_array(int num)
43 struct file **new_fds;
44 int size = num * sizeof(struct file *);
46 if (size <= PAGE_SIZE)
47 new_fds = (struct file **) kmalloc(size, GFP_KERNEL);
48 else
49 new_fds = (struct file **) vmalloc(size);
50 return new_fds;
53 void free_fd_array(struct file **array, int num)
55 int size = num * sizeof(struct file *);
57 if (!array) {
58 printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
59 return;
62 if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
63 return;
64 else if (size <= PAGE_SIZE)
65 kfree(array);
66 else
67 vfree(array);
70 static void __free_fdtable(struct fdtable *fdt)
72 free_fdset(fdt->open_fds, fdt->max_fdset);
73 free_fdset(fdt->close_on_exec, fdt->max_fdset);
74 free_fd_array(fdt->fd, fdt->max_fds);
75 kfree(fdt);
78 static void fdtable_timer(unsigned long data)
80 struct fdtable_defer *fddef = (struct fdtable_defer *)data;
82 spin_lock(&fddef->lock);
84 * If someone already emptied the queue return.
86 if (!fddef->next)
87 goto out;
88 if (!schedule_work(&fddef->wq))
89 mod_timer(&fddef->timer, 5);
90 out:
91 spin_unlock(&fddef->lock);
94 static void free_fdtable_work(struct fdtable_defer *f)
96 struct fdtable *fdt;
98 spin_lock_bh(&f->lock);
99 fdt = f->next;
100 f->next = NULL;
101 spin_unlock_bh(&f->lock);
102 while(fdt) {
103 struct fdtable *next = fdt->next;
104 __free_fdtable(fdt);
105 fdt = next;
109 static void free_fdtable_rcu(struct rcu_head *rcu)
111 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
112 int fdset_size, fdarray_size;
113 struct fdtable_defer *fddef;
115 BUG_ON(!fdt);
116 fdset_size = fdt->max_fdset / 8;
117 fdarray_size = fdt->max_fds * sizeof(struct file *);
119 if (fdt->free_files) {
121 * The this fdtable was embedded in the files structure
122 * and the files structure itself was getting destroyed.
123 * It is now safe to free the files structure.
125 kmem_cache_free(files_cachep, fdt->free_files);
126 return;
128 if (fdt->max_fdset <= EMBEDDED_FD_SET_SIZE &&
129 fdt->max_fds <= NR_OPEN_DEFAULT) {
131 * The fdtable was embedded
133 return;
135 if (fdset_size <= PAGE_SIZE && fdarray_size <= PAGE_SIZE) {
136 kfree(fdt->open_fds);
137 kfree(fdt->close_on_exec);
138 kfree(fdt->fd);
139 kfree(fdt);
140 } else {
141 fddef = &get_cpu_var(fdtable_defer_list);
142 spin_lock(&fddef->lock);
143 fdt->next = fddef->next;
144 fddef->next = fdt;
146 * vmallocs are handled from the workqueue context.
147 * If the per-cpu workqueue is running, then we
148 * defer work scheduling through a timer.
150 if (!schedule_work(&fddef->wq))
151 mod_timer(&fddef->timer, 5);
152 spin_unlock(&fddef->lock);
153 put_cpu_var(fdtable_defer_list);
157 void free_fdtable(struct fdtable *fdt)
159 if (fdt->free_files ||
160 fdt->max_fdset > EMBEDDED_FD_SET_SIZE ||
161 fdt->max_fds > NR_OPEN_DEFAULT)
162 call_rcu(&fdt->rcu, free_fdtable_rcu);
166 * Expand the fdset in the files_struct. Called with the files spinlock
167 * held for write.
169 static void copy_fdtable(struct fdtable *nfdt, struct fdtable *fdt)
171 int i;
172 int count;
174 BUG_ON(nfdt->max_fdset < fdt->max_fdset);
175 BUG_ON(nfdt->max_fds < fdt->max_fds);
176 /* Copy the existing tables and install the new pointers */
178 i = fdt->max_fdset / (sizeof(unsigned long) * 8);
179 count = (nfdt->max_fdset - fdt->max_fdset) / 8;
182 * Don't copy the entire array if the current fdset is
183 * not yet initialised.
185 if (i) {
186 memcpy (nfdt->open_fds, fdt->open_fds,
187 fdt->max_fdset/8);
188 memcpy (nfdt->close_on_exec, fdt->close_on_exec,
189 fdt->max_fdset/8);
190 memset (&nfdt->open_fds->fds_bits[i], 0, count);
191 memset (&nfdt->close_on_exec->fds_bits[i], 0, count);
194 /* Don't copy/clear the array if we are creating a new
195 fd array for fork() */
196 if (fdt->max_fds) {
197 memcpy(nfdt->fd, fdt->fd,
198 fdt->max_fds * sizeof(struct file *));
199 /* clear the remainder of the array */
200 memset(&nfdt->fd[fdt->max_fds], 0,
201 (nfdt->max_fds - fdt->max_fds) *
202 sizeof(struct file *));
207 * Allocate an fdset array, using kmalloc or vmalloc.
208 * Note: the array isn't cleared at allocation time.
210 fd_set * alloc_fdset(int num)
212 fd_set *new_fdset;
213 int size = num / 8;
215 if (size <= PAGE_SIZE)
216 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
217 else
218 new_fdset = (fd_set *) vmalloc(size);
219 return new_fdset;
222 void free_fdset(fd_set *array, int num)
224 if (num <= EMBEDDED_FD_SET_SIZE) /* Don't free an embedded fdset */
225 return;
226 else if (num <= 8 * PAGE_SIZE)
227 kfree(array);
228 else
229 vfree(array);
232 static struct fdtable *alloc_fdtable(int nr)
234 struct fdtable *fdt = NULL;
235 int nfds = 0;
236 fd_set *new_openset = NULL, *new_execset = NULL;
237 struct file **new_fds;
239 fdt = kzalloc(sizeof(*fdt), GFP_KERNEL);
240 if (!fdt)
241 goto out;
243 nfds = max_t(int, 8 * L1_CACHE_BYTES, roundup_pow_of_two(nr + 1));
244 if (nfds > NR_OPEN)
245 nfds = NR_OPEN;
247 new_openset = alloc_fdset(nfds);
248 new_execset = alloc_fdset(nfds);
249 if (!new_openset || !new_execset)
250 goto out;
251 fdt->open_fds = new_openset;
252 fdt->close_on_exec = new_execset;
253 fdt->max_fdset = nfds;
255 nfds = NR_OPEN_DEFAULT;
257 * Expand to the max in easy steps, and keep expanding it until
258 * we have enough for the requested fd array size.
260 do {
261 #if NR_OPEN_DEFAULT < 256
262 if (nfds < 256)
263 nfds = 256;
264 else
265 #endif
266 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
267 nfds = PAGE_SIZE / sizeof(struct file *);
268 else {
269 nfds = nfds * 2;
270 if (nfds > NR_OPEN)
271 nfds = NR_OPEN;
273 } while (nfds <= nr);
274 new_fds = alloc_fd_array(nfds);
275 if (!new_fds)
276 goto out2;
277 fdt->fd = new_fds;
278 fdt->max_fds = nfds;
279 fdt->free_files = NULL;
280 return fdt;
281 out2:
282 nfds = fdt->max_fdset;
283 out:
284 if (new_openset)
285 free_fdset(new_openset, nfds);
286 if (new_execset)
287 free_fdset(new_execset, nfds);
288 kfree(fdt);
289 return NULL;
293 * Expands the file descriptor table - it will allocate a new fdtable and
294 * both fd array and fdset. It is expected to be called with the
295 * files_lock held.
297 static int expand_fdtable(struct files_struct *files, int nr)
298 __releases(files->file_lock)
299 __acquires(files->file_lock)
301 int error = 0;
302 struct fdtable *fdt;
303 struct fdtable *nfdt = NULL;
305 spin_unlock(&files->file_lock);
306 nfdt = alloc_fdtable(nr);
307 if (!nfdt) {
308 error = -ENOMEM;
309 spin_lock(&files->file_lock);
310 goto out;
313 spin_lock(&files->file_lock);
314 fdt = files_fdtable(files);
316 * Check again since another task may have expanded the
317 * fd table while we dropped the lock
319 if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
320 copy_fdtable(nfdt, fdt);
321 } else {
322 /* Somebody expanded while we dropped file_lock */
323 spin_unlock(&files->file_lock);
324 __free_fdtable(nfdt);
325 spin_lock(&files->file_lock);
326 goto out;
328 rcu_assign_pointer(files->fdt, nfdt);
329 free_fdtable(fdt);
330 out:
331 return error;
335 * Expand files.
336 * Return <0 on error; 0 nothing done; 1 files expanded, we may have blocked.
337 * Should be called with the files->file_lock spinlock held for write.
339 int expand_files(struct files_struct *files, int nr)
341 int err, expand = 0;
342 struct fdtable *fdt;
344 fdt = files_fdtable(files);
345 if (nr >= fdt->max_fdset || nr >= fdt->max_fds) {
346 if (fdt->max_fdset >= NR_OPEN ||
347 fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
348 err = -EMFILE;
349 goto out;
351 expand = 1;
352 if ((err = expand_fdtable(files, nr)))
353 goto out;
355 err = expand;
356 out:
357 return err;
360 static void __devinit fdtable_defer_list_init(int cpu)
362 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
363 spin_lock_init(&fddef->lock);
364 INIT_WORK(&fddef->wq, (void (*)(void *))free_fdtable_work, fddef);
365 init_timer(&fddef->timer);
366 fddef->timer.data = (unsigned long)fddef;
367 fddef->timer.function = fdtable_timer;
368 fddef->next = NULL;
371 void __init files_defer_init(void)
373 int i;
374 for_each_possible_cpu(i)
375 fdtable_defer_list_init(i);