blob: 0be344755c020e8de53d26ff89f60d8a4778cf91 [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
Changli Gaoa892e2d2010-08-10 18:01:35 -070042static inline void *alloc_fdmem(unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Changli Gaoa892e2d2010-08-10 18:01:35 -070044 void *data;
45
46 data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN);
47 if (data != NULL)
48 return data;
49
50 return vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
52
Changli Gaoa892e2d2010-08-10 18:01:35 -070053static void free_fdmem(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Changli Gaoa892e2d2010-08-10 18:01:35 -070055 is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
Vadim Lobanov5466b452006-12-10 02:21:22 -080056}
57
Changli Gaoa892e2d2010-08-10 18:01:35 -070058static void __free_fdtable(struct fdtable *fdt)
Vadim Lobanov5466b452006-12-10 02:21:22 -080059{
Changli Gaoa892e2d2010-08-10 18:01:35 -070060 free_fdmem(fdt->fd);
61 free_fdmem(fdt->open_fds);
62 kfree(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070063}
64
David Howells65f27f32006-11-22 14:55:48 +000065static void free_fdtable_work(struct work_struct *work)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070066{
David Howells65f27f32006-11-22 14:55:48 +000067 struct fdtable_defer *f =
68 container_of(work, struct fdtable_defer, wq);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070069 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070071 spin_lock_bh(&f->lock);
72 fdt = f->next;
73 f->next = NULL;
74 spin_unlock_bh(&f->lock);
75 while(fdt) {
76 struct fdtable *next = fdt->next;
Changli Gaoa892e2d2010-08-10 18:01:35 -070077
78 __free_fdtable(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070079 fdt = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070081}
82
Vadim Lobanov4fd45812006-12-10 02:21:17 -080083void free_fdtable_rcu(struct rcu_head *rcu)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070084{
85 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070086 struct fdtable_defer *fddef;
87
88 BUG_ON(!fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070089
Vadim Lobanov4fd45812006-12-10 02:21:17 -080090 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070091 /*
Vadim Lobanov4fd45812006-12-10 02:21:17 -080092 * This fdtable is embedded in the files structure and that
93 * structure itself is getting destroyed.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070094 */
Vadim Lobanov4fd45812006-12-10 02:21:17 -080095 kmem_cache_free(files_cachep,
96 container_of(fdt, struct files_struct, fdtab));
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070097 return;
98 }
Changli Gaoa892e2d2010-08-10 18:01:35 -070099 if (!is_vmalloc_addr(fdt->fd) && !is_vmalloc_addr(fdt->open_fds)) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700100 kfree(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800101 kfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700102 kfree(fdt);
103 } else {
104 fddef = &get_cpu_var(fdtable_defer_list);
105 spin_lock(&fddef->lock);
106 fdt->next = fddef->next;
107 fddef->next = fdt;
Tejun Heo593be072006-12-06 20:36:01 -0800108 /* vmallocs are handled from the workqueue context */
109 schedule_work(&fddef->wq);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700110 spin_unlock(&fddef->lock);
111 put_cpu_var(fdtable_defer_list);
112 }
113}
114
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700115/*
116 * Expand the fdset in the files_struct. Called with the files spinlock
117 * held for write.
118 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800119static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700120{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800121 unsigned int cpy, set;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700122
Vadim Lobanov5466b452006-12-10 02:21:22 -0800123 BUG_ON(nfdt->max_fds < ofdt->max_fds);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800124
125 cpy = ofdt->max_fds * sizeof(struct file *);
126 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
127 memcpy(nfdt->fd, ofdt->fd, cpy);
128 memset((char *)(nfdt->fd) + cpy, 0, set);
129
130 cpy = ofdt->max_fds / BITS_PER_BYTE;
131 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
132 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
133 memset((char *)(nfdt->open_fds) + cpy, 0, set);
134 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
135 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Vadim Lobanov5466b452006-12-10 02:21:22 -0800138static struct fdtable * alloc_fdtable(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800140 struct fdtable *fdt;
141 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700143 /*
Vadim Lobanov5466b452006-12-10 02:21:22 -0800144 * Figure out how many fds we actually want to support in this fdtable.
145 * Allocation steps are keyed to the size of the fdarray, since it
146 * grows far faster than any of the other dynamic data. We try to fit
147 * the fdarray into comfortable page-tuned chunks: starting at 1024B
148 * and growing in powers of two from there on.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700149 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800150 nr /= (1024 / sizeof(struct file *));
151 nr = roundup_pow_of_two(nr + 1);
152 nr *= (1024 / sizeof(struct file *));
Al Viro5c598b32008-04-27 20:04:15 -0400153 /*
154 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
155 * had been set lower between the check in expand_files() and here. Deal
156 * with that in caller, it's cheaper that way.
157 *
158 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
159 * bitmaps handling below becomes unpleasant, to put it mildly...
160 */
161 if (unlikely(nr > sysctl_nr_open))
162 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800163
Vadim Lobanov5466b452006-12-10 02:21:22 -0800164 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
165 if (!fdt)
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800166 goto out;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800167 fdt->max_fds = nr;
168 data = alloc_fdmem(nr * sizeof(struct file *));
169 if (!data)
170 goto out_fdt;
171 fdt->fd = (struct file **)data;
172 data = alloc_fdmem(max_t(unsigned int,
173 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
174 if (!data)
175 goto out_arr;
176 fdt->open_fds = (fd_set *)data;
177 data += nr / BITS_PER_BYTE;
178 fdt->close_on_exec = (fd_set *)data;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800179 fdt->next = NULL;
180
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700181 return fdt;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800182
183out_arr:
Changli Gaoa892e2d2010-08-10 18:01:35 -0700184 free_fdmem(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800185out_fdt:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700186 kfree(fdt);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800187out:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700188 return NULL;
189}
190
191/*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700192 * Expand the file descriptor table.
193 * This function will allocate a new fdtable and both fd array and fdset, of
194 * the given size.
195 * Return <0 error code on error; 1 on successful completion.
196 * The files->file_lock should be held on entry, and will be held on exit.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700197 */
198static int expand_fdtable(struct files_struct *files, int nr)
199 __releases(files->file_lock)
200 __acquires(files->file_lock)
201{
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700202 struct fdtable *new_fdt, *cur_fdt;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 spin_unlock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700205 new_fdt = alloc_fdtable(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 spin_lock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700207 if (!new_fdt)
208 return -ENOMEM;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700209 /*
Al Viro5c598b32008-04-27 20:04:15 -0400210 * extremely unlikely race - sysctl_nr_open decreased between the check in
211 * caller and alloc_fdtable(). Cheaper to catch it here...
212 */
213 if (unlikely(new_fdt->max_fds <= nr)) {
Changli Gaoa892e2d2010-08-10 18:01:35 -0700214 __free_fdtable(new_fdt);
Al Viro5c598b32008-04-27 20:04:15 -0400215 return -EMFILE;
216 }
217 /*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700218 * Check again since another task may have expanded the fd table while
219 * we dropped the lock
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700220 */
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700221 cur_fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800222 if (nr >= cur_fdt->max_fds) {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700223 /* Continue as planned */
224 copy_fdtable(new_fdt, cur_fdt);
225 rcu_assign_pointer(files->fdt, new_fdt);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800226 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
Vadim Lobanov01b2d932006-12-22 01:10:43 -0800227 free_fdtable(cur_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700228 } else {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700229 /* Somebody else expanded, so undo our attempt */
Changli Gaoa892e2d2010-08-10 18:01:35 -0700230 __free_fdtable(new_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700231 }
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700232 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*
236 * Expand files.
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700237 * This function will expand the file structures, if the requested size exceeds
238 * the current capacity and there is room for expansion.
239 * Return <0 error code on error; 0 when nothing done; 1 when files were
240 * expanded and execution may have blocked.
241 * The files->file_lock should be held on entry, and will be held on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 */
243int expand_files(struct files_struct *files, int nr)
244{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700245 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700247 fdt = files_fdtable(files);
Al Viro4e1e0182008-07-26 16:01:20 -0400248
249 /*
250 * N.B. For clone tasks sharing a files structure, this test
251 * will limit the total number of files that can be opened.
252 */
Jiri Slabyd554ed892010-03-05 13:42:42 -0800253 if (nr >= rlimit(RLIMIT_NOFILE))
Al Viro4e1e0182008-07-26 16:01:20 -0400254 return -EMFILE;
255
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700256 /* Do we need to expand? */
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800257 if (nr < fdt->max_fds)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700258 return 0;
Al Viro4e1e0182008-07-26 16:01:20 -0400259
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700260 /* Can we expand? */
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800261 if (nr >= sysctl_nr_open)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700262 return -EMFILE;
263
264 /* All good, so we try */
265 return expand_fdtable(files, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700267
Al Viro02afc622008-05-08 19:42:56 -0400268static int count_open_files(struct fdtable *fdt)
269{
270 int size = fdt->max_fds;
271 int i;
272
273 /* Find the last open fd */
274 for (i = size/(8*sizeof(long)); i > 0; ) {
275 if (fdt->open_fds->fds_bits[--i])
276 break;
277 }
278 i = (i+1) * 8 * sizeof(long);
279 return i;
280}
281
Al Viro02afc622008-05-08 19:42:56 -0400282/*
283 * Allocate a new files structure and copy contents from the
284 * passed in files structure.
285 * errorp will be valid only when the returned files_struct is NULL.
286 */
287struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
288{
289 struct files_struct *newf;
290 struct file **old_fds, **new_fds;
291 int open_files, size, i;
292 struct fdtable *old_fdt, *new_fdt;
293
294 *errorp = -ENOMEM;
Al Viroafbec7f2008-05-08 21:11:17 -0400295 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
Al Viro02afc622008-05-08 19:42:56 -0400296 if (!newf)
297 goto out;
298
Al Viroafbec7f2008-05-08 21:11:17 -0400299 atomic_set(&newf->count, 1);
300
301 spin_lock_init(&newf->file_lock);
302 newf->next_fd = 0;
303 new_fdt = &newf->fdtab;
304 new_fdt->max_fds = NR_OPEN_DEFAULT;
305 new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
306 new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
307 new_fdt->fd = &newf->fd_array[0];
Al Viroafbec7f2008-05-08 21:11:17 -0400308 new_fdt->next = NULL;
309
Al Viro02afc622008-05-08 19:42:56 -0400310 spin_lock(&oldf->file_lock);
311 old_fdt = files_fdtable(oldf);
Al Viro02afc622008-05-08 19:42:56 -0400312 open_files = count_open_files(old_fdt);
313
314 /*
315 * Check whether we need to allocate a larger fd array and fd set.
Al Viro02afc622008-05-08 19:42:56 -0400316 */
Al Viroadbecb12008-05-08 21:19:42 -0400317 while (unlikely(open_files > new_fdt->max_fds)) {
Al Viro02afc622008-05-08 19:42:56 -0400318 spin_unlock(&oldf->file_lock);
Al Viro9dec3c42008-05-08 21:02:45 -0400319
Changli Gaoa892e2d2010-08-10 18:01:35 -0700320 if (new_fdt != &newf->fdtab)
321 __free_fdtable(new_fdt);
Al Viroadbecb12008-05-08 21:19:42 -0400322
Al Viro9dec3c42008-05-08 21:02:45 -0400323 new_fdt = alloc_fdtable(open_files - 1);
324 if (!new_fdt) {
325 *errorp = -ENOMEM;
Al Viro02afc622008-05-08 19:42:56 -0400326 goto out_release;
Al Viro9dec3c42008-05-08 21:02:45 -0400327 }
328
329 /* beyond sysctl_nr_open; nothing to do */
330 if (unlikely(new_fdt->max_fds < open_files)) {
Changli Gaoa892e2d2010-08-10 18:01:35 -0700331 __free_fdtable(new_fdt);
Al Viro9dec3c42008-05-08 21:02:45 -0400332 *errorp = -EMFILE;
333 goto out_release;
334 }
Al Viro9dec3c42008-05-08 21:02:45 -0400335
Al Viro02afc622008-05-08 19:42:56 -0400336 /*
337 * Reacquire the oldf lock and a pointer to its fd table
338 * who knows it may have a new bigger fd table. We need
339 * the latest pointer.
340 */
341 spin_lock(&oldf->file_lock);
342 old_fdt = files_fdtable(oldf);
Al Viroadbecb12008-05-08 21:19:42 -0400343 open_files = count_open_files(old_fdt);
Al Viro02afc622008-05-08 19:42:56 -0400344 }
345
346 old_fds = old_fdt->fd;
347 new_fds = new_fdt->fd;
348
349 memcpy(new_fdt->open_fds->fds_bits,
350 old_fdt->open_fds->fds_bits, open_files/8);
351 memcpy(new_fdt->close_on_exec->fds_bits,
352 old_fdt->close_on_exec->fds_bits, open_files/8);
353
354 for (i = open_files; i != 0; i--) {
355 struct file *f = *old_fds++;
356 if (f) {
357 get_file(f);
358 } else {
359 /*
360 * The fd may be claimed in the fd bitmap but not yet
361 * instantiated in the files array if a sibling thread
362 * is partway through open(). So make sure that this
363 * fd is available to the new process.
364 */
365 FD_CLR(open_files - i, new_fdt->open_fds);
366 }
367 rcu_assign_pointer(*new_fds++, f);
368 }
369 spin_unlock(&oldf->file_lock);
370
371 /* compute the remainder to be cleared */
372 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
373
374 /* This is long word aligned thus could use a optimized version */
375 memset(new_fds, 0, size);
376
377 if (new_fdt->max_fds > open_files) {
378 int left = (new_fdt->max_fds-open_files)/8;
379 int start = open_files / (8 * sizeof(unsigned long));
380
381 memset(&new_fdt->open_fds->fds_bits[start], 0, left);
382 memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
383 }
384
Al Viroafbec7f2008-05-08 21:11:17 -0400385 rcu_assign_pointer(newf->fdt, new_fdt);
386
Al Viro02afc622008-05-08 19:42:56 -0400387 return newf;
388
389out_release:
390 kmem_cache_free(files_cachep, newf);
391out:
392 return NULL;
393}
394
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700395static void __devinit fdtable_defer_list_init(int cpu)
396{
397 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
398 spin_lock_init(&fddef->lock);
David Howells65f27f32006-11-22 14:55:48 +0000399 INIT_WORK(&fddef->wq, free_fdtable_work);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700400 fddef->next = NULL;
401}
402
403void __init files_defer_init(void)
404{
405 int i;
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800406 for_each_possible_cpu(i)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700407 fdtable_defer_list_init(i);
Al Viroeceea0b2008-05-10 10:08:32 -0400408 sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
409 -BITS_PER_LONG;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700410}
Al Virof52111b2008-05-08 18:19:16 -0400411
412struct files_struct init_files = {
413 .count = ATOMIC_INIT(1),
414 .fdt = &init_files.fdtab,
415 .fdtab = {
416 .max_fds = NR_OPEN_DEFAULT,
417 .fd = &init_files.fd_array[0],
418 .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
419 .open_fds = (fd_set *)&init_files.open_fds_init,
Al Virof52111b2008-05-08 18:19:16 -0400420 },
421 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
422};
Al Viro1027abe2008-07-30 04:13:04 -0400423
424/*
425 * allocate a file descriptor, mark it busy.
426 */
427int alloc_fd(unsigned start, unsigned flags)
428{
429 struct files_struct *files = current->files;
430 unsigned int fd;
431 int error;
432 struct fdtable *fdt;
433
434 spin_lock(&files->file_lock);
435repeat:
436 fdt = files_fdtable(files);
437 fd = start;
438 if (fd < files->next_fd)
439 fd = files->next_fd;
440
441 if (fd < fdt->max_fds)
442 fd = find_next_zero_bit(fdt->open_fds->fds_bits,
443 fdt->max_fds, fd);
444
445 error = expand_files(files, fd);
446 if (error < 0)
447 goto out;
448
449 /*
450 * If we needed to expand the fs array we
451 * might have blocked - try again.
452 */
453 if (error)
454 goto repeat;
455
456 if (start <= files->next_fd)
457 files->next_fd = fd + 1;
458
459 FD_SET(fd, fdt->open_fds);
460 if (flags & O_CLOEXEC)
461 FD_SET(fd, fdt->close_on_exec);
462 else
463 FD_CLR(fd, fdt->close_on_exec);
464 error = fd;
465#if 1
466 /* Sanity check */
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800467 if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
Al Viro1027abe2008-07-30 04:13:04 -0400468 printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
469 rcu_assign_pointer(fdt->fd[fd], NULL);
470 }
471#endif
472
473out:
474 spin_unlock(&files->file_lock);
475 return error;
476}
477
478int get_unused_fd(void)
479{
480 return alloc_fd(0, 0);
481}
482EXPORT_SYMBOL(get_unused_fd);