blob: 92197dd9fdc8469fc161082a034d460a27b147a1 [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
Paul Gortmaker630d9c42011-11-16 23:57:37 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/fs.h>
11#include <linux/mm.h>
Andrew Morton6d4831c2011-04-27 15:26:41 -070012#include <linux/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/time.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040014#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/slab.h>
16#include <linux/vmalloc.h>
17#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040018#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/bitops.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070020#include <linux/interrupt.h>
21#include <linux/spinlock.h>
22#include <linux/rcupdate.h>
23#include <linux/workqueue.h>
24
25struct fdtable_defer {
26 spinlock_t lock;
27 struct work_struct wq;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070028 struct fdtable *next;
29};
30
Eric Dumazet9cfe0152008-02-06 01:37:16 -080031int sysctl_nr_open __read_mostly = 1024*1024;
Al Viroeceea0b2008-05-10 10:08:32 -040032int sysctl_nr_open_min = BITS_PER_LONG;
33int sysctl_nr_open_max = 1024 * 1024; /* raised later */
Eric Dumazet9cfe0152008-02-06 01:37:16 -080034
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070035/*
36 * We use this list to defer free fdtables that have vmalloced
37 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
38 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
39 * this per-task structure.
40 */
41static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
David Howells1fd36ad2012-02-16 17:49:54 +000043static void *alloc_fdmem(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Andrew Morton6d4831c2011-04-27 15:26:41 -070045 /*
46 * Very large allocations can stress page reclaim, so fall back to
47 * vmalloc() if the allocation size will be considered "large" by the VM.
48 */
49 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
50 void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN);
51 if (data != NULL)
52 return data;
53 }
Changli Gaoa892e2d2010-08-10 18:01:35 -070054 return vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
Changli Gaoa892e2d2010-08-10 18:01:35 -070057static void free_fdmem(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Changli Gaoa892e2d2010-08-10 18:01:35 -070059 is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
Vadim Lobanov5466b452006-12-10 02:21:22 -080060}
61
Changli Gaoa892e2d2010-08-10 18:01:35 -070062static void __free_fdtable(struct fdtable *fdt)
Vadim Lobanov5466b452006-12-10 02:21:22 -080063{
Changli Gaoa892e2d2010-08-10 18:01:35 -070064 free_fdmem(fdt->fd);
65 free_fdmem(fdt->open_fds);
66 kfree(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070067}
68
David Howells65f27f32006-11-22 14:55:48 +000069static void free_fdtable_work(struct work_struct *work)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070070{
David Howells65f27f32006-11-22 14:55:48 +000071 struct fdtable_defer *f =
72 container_of(work, struct fdtable_defer, wq);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070073 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070075 spin_lock_bh(&f->lock);
76 fdt = f->next;
77 f->next = NULL;
78 spin_unlock_bh(&f->lock);
79 while(fdt) {
80 struct fdtable *next = fdt->next;
Changli Gaoa892e2d2010-08-10 18:01:35 -070081
82 __free_fdtable(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070083 fdt = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070085}
86
Al Viro7cf4dc32012-08-15 19:56:12 -040087static void free_fdtable_rcu(struct rcu_head *rcu)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070088{
89 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070090 struct fdtable_defer *fddef;
91
92 BUG_ON(!fdt);
Al Viro1983e782012-08-15 20:06:36 -040093 BUG_ON(fdt->max_fds <= NR_OPEN_DEFAULT);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070094
Changli Gaoa892e2d2010-08-10 18:01:35 -070095 if (!is_vmalloc_addr(fdt->fd) && !is_vmalloc_addr(fdt->open_fds)) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070096 kfree(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -080097 kfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070098 kfree(fdt);
99 } else {
100 fddef = &get_cpu_var(fdtable_defer_list);
101 spin_lock(&fddef->lock);
102 fdt->next = fddef->next;
103 fddef->next = fdt;
Tejun Heo593be072006-12-06 20:36:01 -0800104 /* vmallocs are handled from the workqueue context */
105 schedule_work(&fddef->wq);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700106 spin_unlock(&fddef->lock);
107 put_cpu_var(fdtable_defer_list);
108 }
109}
110
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700111/*
112 * Expand the fdset in the files_struct. Called with the files spinlock
113 * held for write.
114 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800115static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700116{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800117 unsigned int cpy, set;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700118
Vadim Lobanov5466b452006-12-10 02:21:22 -0800119 BUG_ON(nfdt->max_fds < ofdt->max_fds);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800120
121 cpy = ofdt->max_fds * sizeof(struct file *);
122 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
123 memcpy(nfdt->fd, ofdt->fd, cpy);
124 memset((char *)(nfdt->fd) + cpy, 0, set);
125
126 cpy = ofdt->max_fds / BITS_PER_BYTE;
127 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
128 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
129 memset((char *)(nfdt->open_fds) + cpy, 0, set);
130 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
131 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Vadim Lobanov5466b452006-12-10 02:21:22 -0800134static struct fdtable * alloc_fdtable(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800136 struct fdtable *fdt;
David Howells1fd36ad2012-02-16 17:49:54 +0000137 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700139 /*
Vadim Lobanov5466b452006-12-10 02:21:22 -0800140 * Figure out how many fds we actually want to support in this fdtable.
141 * Allocation steps are keyed to the size of the fdarray, since it
142 * grows far faster than any of the other dynamic data. We try to fit
143 * the fdarray into comfortable page-tuned chunks: starting at 1024B
144 * and growing in powers of two from there on.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700145 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800146 nr /= (1024 / sizeof(struct file *));
147 nr = roundup_pow_of_two(nr + 1);
148 nr *= (1024 / sizeof(struct file *));
Al Viro5c598b32008-04-27 20:04:15 -0400149 /*
150 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
151 * had been set lower between the check in expand_files() and here. Deal
152 * with that in caller, it's cheaper that way.
153 *
154 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
155 * bitmaps handling below becomes unpleasant, to put it mildly...
156 */
157 if (unlikely(nr > sysctl_nr_open))
158 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800159
Vadim Lobanov5466b452006-12-10 02:21:22 -0800160 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
161 if (!fdt)
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800162 goto out;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800163 fdt->max_fds = nr;
164 data = alloc_fdmem(nr * sizeof(struct file *));
165 if (!data)
166 goto out_fdt;
David Howells1fd36ad2012-02-16 17:49:54 +0000167 fdt->fd = data;
168
169 data = alloc_fdmem(max_t(size_t,
Vadim Lobanov5466b452006-12-10 02:21:22 -0800170 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
171 if (!data)
172 goto out_arr;
David Howells1fd36ad2012-02-16 17:49:54 +0000173 fdt->open_fds = data;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800174 data += nr / BITS_PER_BYTE;
David Howells1fd36ad2012-02-16 17:49:54 +0000175 fdt->close_on_exec = data;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800176 fdt->next = NULL;
177
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700178 return fdt;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800179
180out_arr:
Changli Gaoa892e2d2010-08-10 18:01:35 -0700181 free_fdmem(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800182out_fdt:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700183 kfree(fdt);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800184out:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700185 return NULL;
186}
187
188/*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700189 * Expand the file descriptor table.
190 * This function will allocate a new fdtable and both fd array and fdset, of
191 * the given size.
192 * Return <0 error code on error; 1 on successful completion.
193 * The files->file_lock should be held on entry, and will be held on exit.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700194 */
195static int expand_fdtable(struct files_struct *files, int nr)
196 __releases(files->file_lock)
197 __acquires(files->file_lock)
198{
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700199 struct fdtable *new_fdt, *cur_fdt;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 spin_unlock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700202 new_fdt = alloc_fdtable(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 spin_lock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700204 if (!new_fdt)
205 return -ENOMEM;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700206 /*
Al Viro5c598b32008-04-27 20:04:15 -0400207 * extremely unlikely race - sysctl_nr_open decreased between the check in
208 * caller and alloc_fdtable(). Cheaper to catch it here...
209 */
210 if (unlikely(new_fdt->max_fds <= nr)) {
Changli Gaoa892e2d2010-08-10 18:01:35 -0700211 __free_fdtable(new_fdt);
Al Viro5c598b32008-04-27 20:04:15 -0400212 return -EMFILE;
213 }
214 /*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700215 * Check again since another task may have expanded the fd table while
216 * we dropped the lock
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700217 */
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700218 cur_fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800219 if (nr >= cur_fdt->max_fds) {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700220 /* Continue as planned */
221 copy_fdtable(new_fdt, cur_fdt);
222 rcu_assign_pointer(files->fdt, new_fdt);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800223 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
Al Viro1983e782012-08-15 20:06:36 -0400224 call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700225 } else {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700226 /* Somebody else expanded, so undo our attempt */
Changli Gaoa892e2d2010-08-10 18:01:35 -0700227 __free_fdtable(new_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700228 }
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700229 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232/*
233 * Expand files.
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700234 * This function will expand the file structures, if the requested size exceeds
235 * the current capacity and there is room for expansion.
236 * Return <0 error code on error; 0 when nothing done; 1 when files were
237 * expanded and execution may have blocked.
238 * The files->file_lock should be held on entry, and will be held on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
240int expand_files(struct files_struct *files, int nr)
241{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700242 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700244 fdt = files_fdtable(files);
Al Viro4e1e0182008-07-26 16:01:20 -0400245
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700246 /* Do we need to expand? */
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800247 if (nr < fdt->max_fds)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700248 return 0;
Al Viro4e1e0182008-07-26 16:01:20 -0400249
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700250 /* Can we expand? */
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800251 if (nr >= sysctl_nr_open)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700252 return -EMFILE;
253
254 /* All good, so we try */
255 return expand_fdtable(files, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700257
Al Viro02afc6262008-05-08 19:42:56 -0400258static int count_open_files(struct fdtable *fdt)
259{
260 int size = fdt->max_fds;
261 int i;
262
263 /* Find the last open fd */
David Howells1fd36ad2012-02-16 17:49:54 +0000264 for (i = size / BITS_PER_LONG; i > 0; ) {
265 if (fdt->open_fds[--i])
Al Viro02afc6262008-05-08 19:42:56 -0400266 break;
267 }
David Howells1fd36ad2012-02-16 17:49:54 +0000268 i = (i + 1) * BITS_PER_LONG;
Al Viro02afc6262008-05-08 19:42:56 -0400269 return i;
270}
271
Al Viro02afc6262008-05-08 19:42:56 -0400272/*
273 * Allocate a new files structure and copy contents from the
274 * passed in files structure.
275 * errorp will be valid only when the returned files_struct is NULL.
276 */
277struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
278{
279 struct files_struct *newf;
280 struct file **old_fds, **new_fds;
281 int open_files, size, i;
282 struct fdtable *old_fdt, *new_fdt;
283
284 *errorp = -ENOMEM;
Al Viroafbec7f2008-05-08 21:11:17 -0400285 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
Al Viro02afc6262008-05-08 19:42:56 -0400286 if (!newf)
287 goto out;
288
Al Viroafbec7f2008-05-08 21:11:17 -0400289 atomic_set(&newf->count, 1);
290
291 spin_lock_init(&newf->file_lock);
292 newf->next_fd = 0;
293 new_fdt = &newf->fdtab;
294 new_fdt->max_fds = NR_OPEN_DEFAULT;
David Howells1fd36ad2012-02-16 17:49:54 +0000295 new_fdt->close_on_exec = newf->close_on_exec_init;
296 new_fdt->open_fds = newf->open_fds_init;
Al Viroafbec7f2008-05-08 21:11:17 -0400297 new_fdt->fd = &newf->fd_array[0];
Al Viroafbec7f2008-05-08 21:11:17 -0400298 new_fdt->next = NULL;
299
Al Viro02afc6262008-05-08 19:42:56 -0400300 spin_lock(&oldf->file_lock);
301 old_fdt = files_fdtable(oldf);
Al Viro02afc6262008-05-08 19:42:56 -0400302 open_files = count_open_files(old_fdt);
303
304 /*
305 * Check whether we need to allocate a larger fd array and fd set.
Al Viro02afc6262008-05-08 19:42:56 -0400306 */
Al Viroadbecb12008-05-08 21:19:42 -0400307 while (unlikely(open_files > new_fdt->max_fds)) {
Al Viro02afc6262008-05-08 19:42:56 -0400308 spin_unlock(&oldf->file_lock);
Al Viro9dec3c42008-05-08 21:02:45 -0400309
Changli Gaoa892e2d2010-08-10 18:01:35 -0700310 if (new_fdt != &newf->fdtab)
311 __free_fdtable(new_fdt);
Al Viroadbecb12008-05-08 21:19:42 -0400312
Al Viro9dec3c42008-05-08 21:02:45 -0400313 new_fdt = alloc_fdtable(open_files - 1);
314 if (!new_fdt) {
315 *errorp = -ENOMEM;
Al Viro02afc6262008-05-08 19:42:56 -0400316 goto out_release;
Al Viro9dec3c42008-05-08 21:02:45 -0400317 }
318
319 /* beyond sysctl_nr_open; nothing to do */
320 if (unlikely(new_fdt->max_fds < open_files)) {
Changli Gaoa892e2d2010-08-10 18:01:35 -0700321 __free_fdtable(new_fdt);
Al Viro9dec3c42008-05-08 21:02:45 -0400322 *errorp = -EMFILE;
323 goto out_release;
324 }
Al Viro9dec3c42008-05-08 21:02:45 -0400325
Al Viro02afc6262008-05-08 19:42:56 -0400326 /*
327 * Reacquire the oldf lock and a pointer to its fd table
328 * who knows it may have a new bigger fd table. We need
329 * the latest pointer.
330 */
331 spin_lock(&oldf->file_lock);
332 old_fdt = files_fdtable(oldf);
Al Viroadbecb12008-05-08 21:19:42 -0400333 open_files = count_open_files(old_fdt);
Al Viro02afc6262008-05-08 19:42:56 -0400334 }
335
336 old_fds = old_fdt->fd;
337 new_fds = new_fdt->fd;
338
David Howells1fd36ad2012-02-16 17:49:54 +0000339 memcpy(new_fdt->open_fds, old_fdt->open_fds, open_files / 8);
340 memcpy(new_fdt->close_on_exec, old_fdt->close_on_exec, open_files / 8);
Al Viro02afc6262008-05-08 19:42:56 -0400341
342 for (i = open_files; i != 0; i--) {
343 struct file *f = *old_fds++;
344 if (f) {
345 get_file(f);
346 } else {
347 /*
348 * The fd may be claimed in the fd bitmap but not yet
349 * instantiated in the files array if a sibling thread
350 * is partway through open(). So make sure that this
351 * fd is available to the new process.
352 */
David Howells1dce27c2012-02-16 17:49:42 +0000353 __clear_open_fd(open_files - i, new_fdt);
Al Viro02afc6262008-05-08 19:42:56 -0400354 }
355 rcu_assign_pointer(*new_fds++, f);
356 }
357 spin_unlock(&oldf->file_lock);
358
359 /* compute the remainder to be cleared */
360 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
361
362 /* This is long word aligned thus could use a optimized version */
363 memset(new_fds, 0, size);
364
365 if (new_fdt->max_fds > open_files) {
David Howells1fd36ad2012-02-16 17:49:54 +0000366 int left = (new_fdt->max_fds - open_files) / 8;
367 int start = open_files / BITS_PER_LONG;
Al Viro02afc6262008-05-08 19:42:56 -0400368
David Howells1fd36ad2012-02-16 17:49:54 +0000369 memset(&new_fdt->open_fds[start], 0, left);
370 memset(&new_fdt->close_on_exec[start], 0, left);
Al Viro02afc6262008-05-08 19:42:56 -0400371 }
372
Al Viroafbec7f2008-05-08 21:11:17 -0400373 rcu_assign_pointer(newf->fdt, new_fdt);
374
Al Viro02afc6262008-05-08 19:42:56 -0400375 return newf;
376
377out_release:
378 kmem_cache_free(files_cachep, newf);
379out:
380 return NULL;
381}
382
Al Viro7cf4dc32012-08-15 19:56:12 -0400383static void close_files(struct files_struct * files)
384{
385 int i, j;
386 struct fdtable *fdt;
387
388 j = 0;
389
390 /*
391 * It is safe to dereference the fd table without RCU or
392 * ->file_lock because this is the last reference to the
393 * files structure. But use RCU to shut RCU-lockdep up.
394 */
395 rcu_read_lock();
396 fdt = files_fdtable(files);
397 rcu_read_unlock();
398 for (;;) {
399 unsigned long set;
400 i = j * BITS_PER_LONG;
401 if (i >= fdt->max_fds)
402 break;
403 set = fdt->open_fds[j++];
404 while (set) {
405 if (set & 1) {
406 struct file * file = xchg(&fdt->fd[i], NULL);
407 if (file) {
408 filp_close(file, files);
409 cond_resched();
410 }
411 }
412 i++;
413 set >>= 1;
414 }
415 }
416}
417
418struct files_struct *get_files_struct(struct task_struct *task)
419{
420 struct files_struct *files;
421
422 task_lock(task);
423 files = task->files;
424 if (files)
425 atomic_inc(&files->count);
426 task_unlock(task);
427
428 return files;
429}
430
431void put_files_struct(struct files_struct *files)
432{
433 struct fdtable *fdt;
434
435 if (atomic_dec_and_test(&files->count)) {
436 close_files(files);
Al Virob9e02af2012-08-15 20:00:58 -0400437 /* not really needed, since nobody can see us */
Al Viro7cf4dc32012-08-15 19:56:12 -0400438 rcu_read_lock();
439 fdt = files_fdtable(files);
Al Viro7cf4dc32012-08-15 19:56:12 -0400440 rcu_read_unlock();
Al Virob9e02af2012-08-15 20:00:58 -0400441 /* free the arrays if they are not embedded */
442 if (fdt != &files->fdtab)
443 __free_fdtable(fdt);
444 kmem_cache_free(files_cachep, files);
Al Viro7cf4dc32012-08-15 19:56:12 -0400445 }
446}
447
448void reset_files_struct(struct files_struct *files)
449{
450 struct task_struct *tsk = current;
451 struct files_struct *old;
452
453 old = tsk->files;
454 task_lock(tsk);
455 tsk->files = files;
456 task_unlock(tsk);
457 put_files_struct(old);
458}
459
460void exit_files(struct task_struct *tsk)
461{
462 struct files_struct * files = tsk->files;
463
464 if (files) {
465 task_lock(tsk);
466 tsk->files = NULL;
467 task_unlock(tsk);
468 put_files_struct(files);
469 }
470}
471
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700472static void __devinit fdtable_defer_list_init(int cpu)
473{
474 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
475 spin_lock_init(&fddef->lock);
David Howells65f27f32006-11-22 14:55:48 +0000476 INIT_WORK(&fddef->wq, free_fdtable_work);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700477 fddef->next = NULL;
478}
479
480void __init files_defer_init(void)
481{
482 int i;
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800483 for_each_possible_cpu(i)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700484 fdtable_defer_list_init(i);
Al Viroeceea0b2008-05-10 10:08:32 -0400485 sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
486 -BITS_PER_LONG;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700487}
Al Virof52111b2008-05-08 18:19:16 -0400488
489struct files_struct init_files = {
490 .count = ATOMIC_INIT(1),
491 .fdt = &init_files.fdtab,
492 .fdtab = {
493 .max_fds = NR_OPEN_DEFAULT,
494 .fd = &init_files.fd_array[0],
David Howells1fd36ad2012-02-16 17:49:54 +0000495 .close_on_exec = init_files.close_on_exec_init,
496 .open_fds = init_files.open_fds_init,
Al Virof52111b2008-05-08 18:19:16 -0400497 },
498 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
499};
Al Viro1027abe2008-07-30 04:13:04 -0400500
501/*
502 * allocate a file descriptor, mark it busy.
503 */
Al Virodcfadfa2012-08-12 17:27:30 -0400504int __alloc_fd(struct files_struct *files,
505 unsigned start, unsigned end, unsigned flags)
Al Viro1027abe2008-07-30 04:13:04 -0400506{
Al Viro1027abe2008-07-30 04:13:04 -0400507 unsigned int fd;
508 int error;
509 struct fdtable *fdt;
510
511 spin_lock(&files->file_lock);
512repeat:
513 fdt = files_fdtable(files);
514 fd = start;
515 if (fd < files->next_fd)
516 fd = files->next_fd;
517
518 if (fd < fdt->max_fds)
David Howells1fd36ad2012-02-16 17:49:54 +0000519 fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);
Al Viro1027abe2008-07-30 04:13:04 -0400520
Al Virof33ff992012-08-12 16:17:59 -0400521 /*
522 * N.B. For clone tasks sharing a files structure, this test
523 * will limit the total number of files that can be opened.
524 */
525 error = -EMFILE;
526 if (fd >= end)
527 goto out;
528
Al Viro1027abe2008-07-30 04:13:04 -0400529 error = expand_files(files, fd);
530 if (error < 0)
531 goto out;
532
533 /*
534 * If we needed to expand the fs array we
535 * might have blocked - try again.
536 */
537 if (error)
538 goto repeat;
539
540 if (start <= files->next_fd)
541 files->next_fd = fd + 1;
542
David Howells1dce27c2012-02-16 17:49:42 +0000543 __set_open_fd(fd, fdt);
Al Viro1027abe2008-07-30 04:13:04 -0400544 if (flags & O_CLOEXEC)
David Howells1dce27c2012-02-16 17:49:42 +0000545 __set_close_on_exec(fd, fdt);
Al Viro1027abe2008-07-30 04:13:04 -0400546 else
David Howells1dce27c2012-02-16 17:49:42 +0000547 __clear_close_on_exec(fd, fdt);
Al Viro1027abe2008-07-30 04:13:04 -0400548 error = fd;
549#if 1
550 /* Sanity check */
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800551 if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
Al Viro1027abe2008-07-30 04:13:04 -0400552 printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
553 rcu_assign_pointer(fdt->fd[fd], NULL);
554 }
555#endif
556
557out:
558 spin_unlock(&files->file_lock);
559 return error;
560}
561
Al Virodcfadfa2012-08-12 17:27:30 -0400562int alloc_fd(unsigned start, unsigned flags)
563{
564 return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
565}
566
Al Viro1a7bd222012-08-12 17:18:05 -0400567int get_unused_fd_flags(unsigned flags)
Al Viro1027abe2008-07-30 04:13:04 -0400568{
Al Virodcfadfa2012-08-12 17:27:30 -0400569 return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
Al Viro1027abe2008-07-30 04:13:04 -0400570}
Al Viro1a7bd222012-08-12 17:18:05 -0400571EXPORT_SYMBOL(get_unused_fd_flags);
Al Viro56007ca2012-08-15 21:03:26 -0400572
573static void __put_unused_fd(struct files_struct *files, unsigned int fd)
574{
575 struct fdtable *fdt = files_fdtable(files);
576 __clear_open_fd(fd, fdt);
577 if (fd < files->next_fd)
578 files->next_fd = fd;
579}
580
581void put_unused_fd(unsigned int fd)
582{
583 struct files_struct *files = current->files;
584 spin_lock(&files->file_lock);
585 __put_unused_fd(files, fd);
586 spin_unlock(&files->file_lock);
587}
588
589EXPORT_SYMBOL(put_unused_fd);
590
591/*
592 * Install a file pointer in the fd array.
593 *
594 * The VFS is full of places where we drop the files lock between
595 * setting the open_fds bitmap and installing the file in the file
596 * array. At any such point, we are vulnerable to a dup2() race
597 * installing a file in the array before us. We need to detect this and
598 * fput() the struct file we are about to overwrite in this case.
599 *
600 * It should never happen - if we allow dup2() do it, _really_ bad things
601 * will follow.
Al Virof869e8a2012-08-15 21:06:33 -0400602 *
603 * NOTE: __fd_install() variant is really, really low-level; don't
604 * use it unless you are forced to by truly lousy API shoved down
605 * your throat. 'files' *MUST* be either current->files or obtained
606 * by get_files_struct(current) done by whoever had given it to you,
607 * or really bad things will happen. Normally you want to use
608 * fd_install() instead.
Al Viro56007ca2012-08-15 21:03:26 -0400609 */
610
Al Virof869e8a2012-08-15 21:06:33 -0400611void __fd_install(struct files_struct *files, unsigned int fd,
612 struct file *file)
Al Viro56007ca2012-08-15 21:03:26 -0400613{
Al Viro56007ca2012-08-15 21:03:26 -0400614 struct fdtable *fdt;
615 spin_lock(&files->file_lock);
616 fdt = files_fdtable(files);
617 BUG_ON(fdt->fd[fd] != NULL);
618 rcu_assign_pointer(fdt->fd[fd], file);
619 spin_unlock(&files->file_lock);
620}
621
Al Virof869e8a2012-08-15 21:06:33 -0400622void fd_install(unsigned int fd, struct file *file)
623{
624 __fd_install(current->files, fd, file);
625}
626
Al Viro56007ca2012-08-15 21:03:26 -0400627EXPORT_SYMBOL(fd_install);
Al Viro0ee8cdf2012-08-15 21:12:10 -0400628
Al Viro483ce1d2012-08-19 12:04:24 -0400629/*
630 * The same warnings as for __alloc_fd()/__fd_install() apply here...
631 */
632int __close_fd(struct files_struct *files, unsigned fd)
633{
634 struct file *file;
635 struct fdtable *fdt;
636
637 spin_lock(&files->file_lock);
638 fdt = files_fdtable(files);
639 if (fd >= fdt->max_fds)
640 goto out_unlock;
641 file = fdt->fd[fd];
642 if (!file)
643 goto out_unlock;
644 rcu_assign_pointer(fdt->fd[fd], NULL);
645 __clear_close_on_exec(fd, fdt);
646 __put_unused_fd(files, fd);
647 spin_unlock(&files->file_lock);
648 return filp_close(file, files);
649
650out_unlock:
651 spin_unlock(&files->file_lock);
652 return -EBADF;
653}
654
Al Viro6a6d27d2012-08-21 09:56:33 -0400655void do_close_on_exec(struct files_struct *files)
656{
657 unsigned i;
658 struct fdtable *fdt;
659
660 /* exec unshares first */
661 BUG_ON(atomic_read(&files->count) != 1);
662 spin_lock(&files->file_lock);
663 for (i = 0; ; i++) {
664 unsigned long set;
665 unsigned fd = i * BITS_PER_LONG;
666 fdt = files_fdtable(files);
667 if (fd >= fdt->max_fds)
668 break;
669 set = fdt->close_on_exec[i];
670 if (!set)
671 continue;
672 fdt->close_on_exec[i] = 0;
673 for ( ; set ; fd++, set >>= 1) {
674 struct file *file;
675 if (!(set & 1))
676 continue;
677 file = fdt->fd[fd];
678 if (!file)
679 continue;
680 rcu_assign_pointer(fdt->fd[fd], NULL);
681 __put_unused_fd(files, fd);
682 spin_unlock(&files->file_lock);
683 filp_close(file, files);
684 cond_resched();
685 spin_lock(&files->file_lock);
686 }
687
688 }
689 spin_unlock(&files->file_lock);
690}
691
Al Viro0ee8cdf2012-08-15 21:12:10 -0400692struct file *fget(unsigned int fd)
693{
694 struct file *file;
695 struct files_struct *files = current->files;
696
697 rcu_read_lock();
698 file = fcheck_files(files, fd);
699 if (file) {
700 /* File object ref couldn't be taken */
701 if (file->f_mode & FMODE_PATH ||
702 !atomic_long_inc_not_zero(&file->f_count))
703 file = NULL;
704 }
705 rcu_read_unlock();
706
707 return file;
708}
709
710EXPORT_SYMBOL(fget);
711
712struct file *fget_raw(unsigned int fd)
713{
714 struct file *file;
715 struct files_struct *files = current->files;
716
717 rcu_read_lock();
718 file = fcheck_files(files, fd);
719 if (file) {
720 /* File object ref couldn't be taken */
721 if (!atomic_long_inc_not_zero(&file->f_count))
722 file = NULL;
723 }
724 rcu_read_unlock();
725
726 return file;
727}
728
729EXPORT_SYMBOL(fget_raw);
730
731/*
732 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
733 *
734 * You can use this instead of fget if you satisfy all of the following
735 * conditions:
736 * 1) You must call fput_light before exiting the syscall and returning control
737 * to userspace (i.e. you cannot remember the returned struct file * after
738 * returning to userspace).
739 * 2) You must not call filp_close on the returned struct file * in between
740 * calls to fget_light and fput_light.
741 * 3) You must not clone the current task in between the calls to fget_light
742 * and fput_light.
743 *
744 * The fput_needed flag returned by fget_light should be passed to the
745 * corresponding fput_light.
746 */
747struct file *fget_light(unsigned int fd, int *fput_needed)
748{
749 struct file *file;
750 struct files_struct *files = current->files;
751
752 *fput_needed = 0;
753 if (atomic_read(&files->count) == 1) {
754 file = fcheck_files(files, fd);
755 if (file && (file->f_mode & FMODE_PATH))
756 file = NULL;
757 } else {
758 rcu_read_lock();
759 file = fcheck_files(files, fd);
760 if (file) {
761 if (!(file->f_mode & FMODE_PATH) &&
762 atomic_long_inc_not_zero(&file->f_count))
763 *fput_needed = 1;
764 else
765 /* Didn't get the reference, someone's freed */
766 file = NULL;
767 }
768 rcu_read_unlock();
769 }
770
771 return file;
772}
773
774struct file *fget_raw_light(unsigned int fd, int *fput_needed)
775{
776 struct file *file;
777 struct files_struct *files = current->files;
778
779 *fput_needed = 0;
780 if (atomic_read(&files->count) == 1) {
781 file = fcheck_files(files, fd);
782 } else {
783 rcu_read_lock();
784 file = fcheck_files(files, fd);
785 if (file) {
786 if (atomic_long_inc_not_zero(&file->f_count))
787 *fput_needed = 1;
788 else
789 /* Didn't get the reference, someone's freed */
790 file = NULL;
791 }
792 rcu_read_unlock();
793 }
794
795 return file;
796}