blob: cccaead962c2efe31c01029ca1ffa34676cffc8a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/file.c
3 *
4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
5 *
6 * Manage the dynamic fd arrays in the process files_struct.
7 */
8
Al Viro1027abe2008-07-30 04:13:04 -04009#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/mm.h>
12#include <linux/time.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040013#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/slab.h>
15#include <linux/vmalloc.h>
16#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040017#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/bitops.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070019#include <linux/interrupt.h>
20#include <linux/spinlock.h>
21#include <linux/rcupdate.h>
22#include <linux/workqueue.h>
23
24struct fdtable_defer {
25 spinlock_t lock;
26 struct work_struct wq;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070027 struct fdtable *next;
28};
29
Eric Dumazet9cfe0152008-02-06 01:37:16 -080030int sysctl_nr_open __read_mostly = 1024*1024;
Al Viroeceea0b2008-05-10 10:08:32 -040031int sysctl_nr_open_min = BITS_PER_LONG;
32int sysctl_nr_open_max = 1024 * 1024; /* raised later */
Eric Dumazet9cfe0152008-02-06 01:37:16 -080033
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070034/*
35 * We use this list to defer free fdtables that have vmalloced
36 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
37 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
38 * this per-task structure.
39 */
40static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Vadim Lobanov5466b452006-12-10 02:21:22 -080042static inline void * alloc_fdmem(unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (size <= PAGE_SIZE)
Vadim Lobanov5466b452006-12-10 02:21:22 -080045 return kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 else
Vadim Lobanov5466b452006-12-10 02:21:22 -080047 return vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Vadim Lobanov5466b452006-12-10 02:21:22 -080050static inline void free_fdarr(struct fdtable *fdt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Vadim Lobanov5466b452006-12-10 02:21:22 -080052 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
53 kfree(fdt->fd);
54 else
55 vfree(fdt->fd);
56}
57
58static inline void free_fdset(struct fdtable *fdt)
59{
60 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
61 kfree(fdt->open_fds);
62 else
63 vfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070064}
65
David Howells65f27f32006-11-22 14:55:48 +000066static void free_fdtable_work(struct work_struct *work)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070067{
David Howells65f27f32006-11-22 14:55:48 +000068 struct fdtable_defer *f =
69 container_of(work, struct fdtable_defer, wq);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070070 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070072 spin_lock_bh(&f->lock);
73 fdt = f->next;
74 f->next = NULL;
75 spin_unlock_bh(&f->lock);
76 while(fdt) {
77 struct fdtable *next = fdt->next;
Vadim Lobanov5466b452006-12-10 02:21:22 -080078 vfree(fdt->fd);
79 free_fdset(fdt);
80 kfree(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070081 fdt = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070083}
84
Vadim Lobanov4fd45812006-12-10 02:21:17 -080085void free_fdtable_rcu(struct rcu_head *rcu)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070086{
87 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070088 struct fdtable_defer *fddef;
89
90 BUG_ON(!fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070091
Vadim Lobanov4fd45812006-12-10 02:21:17 -080092 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070093 /*
Vadim Lobanov4fd45812006-12-10 02:21:17 -080094 * This fdtable is embedded in the files structure and that
95 * structure itself is getting destroyed.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070096 */
Vadim Lobanov4fd45812006-12-10 02:21:17 -080097 kmem_cache_free(files_cachep,
98 container_of(fdt, struct files_struct, fdtab));
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070099 return;
100 }
Vadim Lobanov5466b452006-12-10 02:21:22 -0800101 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700102 kfree(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800103 kfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700104 kfree(fdt);
105 } else {
106 fddef = &get_cpu_var(fdtable_defer_list);
107 spin_lock(&fddef->lock);
108 fdt->next = fddef->next;
109 fddef->next = fdt;
Tejun Heo593be072006-12-06 20:36:01 -0800110 /* vmallocs are handled from the workqueue context */
111 schedule_work(&fddef->wq);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700112 spin_unlock(&fddef->lock);
113 put_cpu_var(fdtable_defer_list);
114 }
115}
116
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700117/*
118 * Expand the fdset in the files_struct. Called with the files spinlock
119 * held for write.
120 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800121static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700122{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800123 unsigned int cpy, set;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700124
Vadim Lobanov5466b452006-12-10 02:21:22 -0800125 BUG_ON(nfdt->max_fds < ofdt->max_fds);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800126
127 cpy = ofdt->max_fds * sizeof(struct file *);
128 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
129 memcpy(nfdt->fd, ofdt->fd, cpy);
130 memset((char *)(nfdt->fd) + cpy, 0, set);
131
132 cpy = ofdt->max_fds / BITS_PER_BYTE;
133 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
134 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
135 memset((char *)(nfdt->open_fds) + cpy, 0, set);
136 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
137 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Vadim Lobanov5466b452006-12-10 02:21:22 -0800140static struct fdtable * alloc_fdtable(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800142 struct fdtable *fdt;
143 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700145 /*
Vadim Lobanov5466b452006-12-10 02:21:22 -0800146 * Figure out how many fds we actually want to support in this fdtable.
147 * Allocation steps are keyed to the size of the fdarray, since it
148 * grows far faster than any of the other dynamic data. We try to fit
149 * the fdarray into comfortable page-tuned chunks: starting at 1024B
150 * and growing in powers of two from there on.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700151 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800152 nr /= (1024 / sizeof(struct file *));
153 nr = roundup_pow_of_two(nr + 1);
154 nr *= (1024 / sizeof(struct file *));
Al Viro5c598b32008-04-27 20:04:15 -0400155 /*
156 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
157 * had been set lower between the check in expand_files() and here. Deal
158 * with that in caller, it's cheaper that way.
159 *
160 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
161 * bitmaps handling below becomes unpleasant, to put it mildly...
162 */
163 if (unlikely(nr > sysctl_nr_open))
164 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800165
Vadim Lobanov5466b452006-12-10 02:21:22 -0800166 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
167 if (!fdt)
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800168 goto out;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800169 fdt->max_fds = nr;
170 data = alloc_fdmem(nr * sizeof(struct file *));
171 if (!data)
172 goto out_fdt;
173 fdt->fd = (struct file **)data;
174 data = alloc_fdmem(max_t(unsigned int,
175 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
176 if (!data)
177 goto out_arr;
178 fdt->open_fds = (fd_set *)data;
179 data += nr / BITS_PER_BYTE;
180 fdt->close_on_exec = (fd_set *)data;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800181 fdt->next = NULL;
182
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700183 return fdt;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800184
185out_arr:
186 free_fdarr(fdt);
187out_fdt:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700188 kfree(fdt);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800189out:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700190 return NULL;
191}
192
193/*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700194 * Expand the file descriptor table.
195 * This function will allocate a new fdtable and both fd array and fdset, of
196 * the given size.
197 * Return <0 error code on error; 1 on successful completion.
198 * The files->file_lock should be held on entry, and will be held on exit.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700199 */
200static int expand_fdtable(struct files_struct *files, int nr)
201 __releases(files->file_lock)
202 __acquires(files->file_lock)
203{
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700204 struct fdtable *new_fdt, *cur_fdt;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 spin_unlock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700207 new_fdt = alloc_fdtable(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_lock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700209 if (!new_fdt)
210 return -ENOMEM;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700211 /*
Al Viro5c598b32008-04-27 20:04:15 -0400212 * extremely unlikely race - sysctl_nr_open decreased between the check in
213 * caller and alloc_fdtable(). Cheaper to catch it here...
214 */
215 if (unlikely(new_fdt->max_fds <= nr)) {
216 free_fdarr(new_fdt);
217 free_fdset(new_fdt);
218 kfree(new_fdt);
219 return -EMFILE;
220 }
221 /*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700222 * Check again since another task may have expanded the fd table while
223 * we dropped the lock
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700224 */
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700225 cur_fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800226 if (nr >= cur_fdt->max_fds) {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700227 /* Continue as planned */
228 copy_fdtable(new_fdt, cur_fdt);
229 rcu_assign_pointer(files->fdt, new_fdt);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800230 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
Vadim Lobanov01b2d932006-12-22 01:10:43 -0800231 free_fdtable(cur_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700232 } else {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700233 /* Somebody else expanded, so undo our attempt */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800234 free_fdarr(new_fdt);
235 free_fdset(new_fdt);
236 kfree(new_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700237 }
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700238 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241/*
242 * Expand files.
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700243 * This function will expand the file structures, if the requested size exceeds
244 * the current capacity and there is room for expansion.
245 * Return <0 error code on error; 0 when nothing done; 1 when files were
246 * expanded and execution may have blocked.
247 * The files->file_lock should be held on entry, and will be held on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249int expand_files(struct files_struct *files, int nr)
250{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700251 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700253 fdt = files_fdtable(files);
Al Viro4e1e0182008-07-26 16:01:20 -0400254
255 /*
256 * N.B. For clone tasks sharing a files structure, this test
257 * will limit the total number of files that can be opened.
258 */
Jiri Slabyd554ed892010-03-05 13:42:42 -0800259 if (nr >= rlimit(RLIMIT_NOFILE))
Al Viro4e1e0182008-07-26 16:01:20 -0400260 return -EMFILE;
261
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700262 /* Do we need to expand? */
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800263 if (nr < fdt->max_fds)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700264 return 0;
Al Viro4e1e0182008-07-26 16:01:20 -0400265
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700266 /* Can we expand? */
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800267 if (nr >= sysctl_nr_open)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700268 return -EMFILE;
269
270 /* All good, so we try */
271 return expand_fdtable(files, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700273
Al Viro02afc6262008-05-08 19:42:56 -0400274static int count_open_files(struct fdtable *fdt)
275{
276 int size = fdt->max_fds;
277 int i;
278
279 /* Find the last open fd */
280 for (i = size/(8*sizeof(long)); i > 0; ) {
281 if (fdt->open_fds->fds_bits[--i])
282 break;
283 }
284 i = (i+1) * 8 * sizeof(long);
285 return i;
286}
287
Al Viro02afc6262008-05-08 19:42:56 -0400288/*
289 * Allocate a new files structure and copy contents from the
290 * passed in files structure.
291 * errorp will be valid only when the returned files_struct is NULL.
292 */
293struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
294{
295 struct files_struct *newf;
296 struct file **old_fds, **new_fds;
297 int open_files, size, i;
298 struct fdtable *old_fdt, *new_fdt;
299
300 *errorp = -ENOMEM;
Al Viroafbec7f2008-05-08 21:11:17 -0400301 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
Al Viro02afc6262008-05-08 19:42:56 -0400302 if (!newf)
303 goto out;
304
Al Viroafbec7f2008-05-08 21:11:17 -0400305 atomic_set(&newf->count, 1);
306
307 spin_lock_init(&newf->file_lock);
308 newf->next_fd = 0;
309 new_fdt = &newf->fdtab;
310 new_fdt->max_fds = NR_OPEN_DEFAULT;
311 new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
312 new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
313 new_fdt->fd = &newf->fd_array[0];
Al Viroafbec7f2008-05-08 21:11:17 -0400314 new_fdt->next = NULL;
315
Al Viro02afc6262008-05-08 19:42:56 -0400316 spin_lock(&oldf->file_lock);
317 old_fdt = files_fdtable(oldf);
Al Viro02afc6262008-05-08 19:42:56 -0400318 open_files = count_open_files(old_fdt);
319
320 /*
321 * Check whether we need to allocate a larger fd array and fd set.
Al Viro02afc6262008-05-08 19:42:56 -0400322 */
Al Viroadbecb12008-05-08 21:19:42 -0400323 while (unlikely(open_files > new_fdt->max_fds)) {
Al Viro02afc6262008-05-08 19:42:56 -0400324 spin_unlock(&oldf->file_lock);
Al Viro9dec3c42008-05-08 21:02:45 -0400325
Al Viroadbecb12008-05-08 21:19:42 -0400326 if (new_fdt != &newf->fdtab) {
327 free_fdarr(new_fdt);
328 free_fdset(new_fdt);
329 kfree(new_fdt);
330 }
331
Al Viro9dec3c42008-05-08 21:02:45 -0400332 new_fdt = alloc_fdtable(open_files - 1);
333 if (!new_fdt) {
334 *errorp = -ENOMEM;
Al Viro02afc6262008-05-08 19:42:56 -0400335 goto out_release;
Al Viro9dec3c42008-05-08 21:02:45 -0400336 }
337
338 /* beyond sysctl_nr_open; nothing to do */
339 if (unlikely(new_fdt->max_fds < open_files)) {
340 free_fdarr(new_fdt);
341 free_fdset(new_fdt);
342 kfree(new_fdt);
343 *errorp = -EMFILE;
344 goto out_release;
345 }
Al Viro9dec3c42008-05-08 21:02:45 -0400346
Al Viro02afc6262008-05-08 19:42:56 -0400347 /*
348 * Reacquire the oldf lock and a pointer to its fd table
349 * who knows it may have a new bigger fd table. We need
350 * the latest pointer.
351 */
352 spin_lock(&oldf->file_lock);
353 old_fdt = files_fdtable(oldf);
Al Viroadbecb12008-05-08 21:19:42 -0400354 open_files = count_open_files(old_fdt);
Al Viro02afc6262008-05-08 19:42:56 -0400355 }
356
357 old_fds = old_fdt->fd;
358 new_fds = new_fdt->fd;
359
360 memcpy(new_fdt->open_fds->fds_bits,
361 old_fdt->open_fds->fds_bits, open_files/8);
362 memcpy(new_fdt->close_on_exec->fds_bits,
363 old_fdt->close_on_exec->fds_bits, open_files/8);
364
365 for (i = open_files; i != 0; i--) {
366 struct file *f = *old_fds++;
367 if (f) {
368 get_file(f);
369 } else {
370 /*
371 * The fd may be claimed in the fd bitmap but not yet
372 * instantiated in the files array if a sibling thread
373 * is partway through open(). So make sure that this
374 * fd is available to the new process.
375 */
376 FD_CLR(open_files - i, new_fdt->open_fds);
377 }
378 rcu_assign_pointer(*new_fds++, f);
379 }
380 spin_unlock(&oldf->file_lock);
381
382 /* compute the remainder to be cleared */
383 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
384
385 /* This is long word aligned thus could use a optimized version */
386 memset(new_fds, 0, size);
387
388 if (new_fdt->max_fds > open_files) {
389 int left = (new_fdt->max_fds-open_files)/8;
390 int start = open_files / (8 * sizeof(unsigned long));
391
392 memset(&new_fdt->open_fds->fds_bits[start], 0, left);
393 memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
394 }
395
Al Viroafbec7f2008-05-08 21:11:17 -0400396 rcu_assign_pointer(newf->fdt, new_fdt);
397
Al Viro02afc6262008-05-08 19:42:56 -0400398 return newf;
399
400out_release:
401 kmem_cache_free(files_cachep, newf);
402out:
403 return NULL;
404}
405
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700406static void __devinit fdtable_defer_list_init(int cpu)
407{
408 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
409 spin_lock_init(&fddef->lock);
David Howells65f27f32006-11-22 14:55:48 +0000410 INIT_WORK(&fddef->wq, free_fdtable_work);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700411 fddef->next = NULL;
412}
413
414void __init files_defer_init(void)
415{
416 int i;
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800417 for_each_possible_cpu(i)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700418 fdtable_defer_list_init(i);
Al Viroeceea0b2008-05-10 10:08:32 -0400419 sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
420 -BITS_PER_LONG;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700421}
Al Virof52111b2008-05-08 18:19:16 -0400422
423struct files_struct init_files = {
424 .count = ATOMIC_INIT(1),
425 .fdt = &init_files.fdtab,
426 .fdtab = {
427 .max_fds = NR_OPEN_DEFAULT,
428 .fd = &init_files.fd_array[0],
429 .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
430 .open_fds = (fd_set *)&init_files.open_fds_init,
Al Virof52111b2008-05-08 18:19:16 -0400431 },
432 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
433};
Al Viro1027abe2008-07-30 04:13:04 -0400434
435/*
436 * allocate a file descriptor, mark it busy.
437 */
438int alloc_fd(unsigned start, unsigned flags)
439{
440 struct files_struct *files = current->files;
441 unsigned int fd;
442 int error;
443 struct fdtable *fdt;
444
445 spin_lock(&files->file_lock);
446repeat:
447 fdt = files_fdtable(files);
448 fd = start;
449 if (fd < files->next_fd)
450 fd = files->next_fd;
451
452 if (fd < fdt->max_fds)
453 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
454 fdt->max_fds, fd);
455
456 error = expand_files(files, fd);
457 if (error < 0)
458 goto out;
459
460 /*
461 * If we needed to expand the fs array we
462 * might have blocked - try again.
463 */
464 if (error)
465 goto repeat;
466
467 if (start <= files->next_fd)
468 files->next_fd = fd + 1;
469
470 FD_SET(fd, fdt->open_fds);
471 if (flags & O_CLOEXEC)
472 FD_SET(fd, fdt->close_on_exec);
473 else
474 FD_CLR(fd, fdt->close_on_exec);
475 error = fd;
476#if 1
477 /* Sanity check */
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800478 if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
Al Viro1027abe2008-07-30 04:13:04 -0400479 printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
480 rcu_assign_pointer(fdt->fd[fd], NULL);
481 }
482#endif
483
484out:
485 spin_unlock(&files->file_lock);
486 return error;
487}
488
489int get_unused_fd(void)
490{
491 return alloc_fd(0, 0);
492}
493EXPORT_SYMBOL(get_unused_fd);