blob: a7bbe032447864f21c6cd9ceb16a7e7503dd6359 [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 Virofe17f222012-08-21 11:48:11 -04009#include <linux/syscalls.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/fs.h>
12#include <linux/mm.h>
Andrew Morton6d4831c2011-04-27 15:26:41 -070013#include <linux/mmzone.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/time.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/slab.h>
17#include <linux/vmalloc.h>
18#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040019#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/bitops.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070021#include <linux/interrupt.h>
22#include <linux/spinlock.h>
23#include <linux/rcupdate.h>
24#include <linux/workqueue.h>
25
26struct fdtable_defer {
27 spinlock_t lock;
28 struct work_struct wq;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070029 struct fdtable *next;
30};
31
Eric Dumazet9cfe0152008-02-06 01:37:16 -080032int sysctl_nr_open __read_mostly = 1024*1024;
Al Viroeceea0b2008-05-10 10:08:32 -040033int sysctl_nr_open_min = BITS_PER_LONG;
34int sysctl_nr_open_max = 1024 * 1024; /* raised later */
Eric Dumazet9cfe0152008-02-06 01:37:16 -080035
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070036/*
37 * We use this list to defer free fdtables that have vmalloced
38 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
39 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
40 * this per-task structure.
41 */
42static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
David Howells1fd36ad2012-02-16 17:49:54 +000044static void *alloc_fdmem(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Andrew Morton6d4831c2011-04-27 15:26:41 -070046 /*
47 * Very large allocations can stress page reclaim, so fall back to
48 * vmalloc() if the allocation size will be considered "large" by the VM.
49 */
50 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
51 void *data = kmalloc(size, GFP_KERNEL|__GFP_NOWARN);
52 if (data != NULL)
53 return data;
54 }
Changli Gaoa892e2d2010-08-10 18:01:35 -070055 return vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Changli Gaoa892e2d2010-08-10 18:01:35 -070058static void free_fdmem(void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Changli Gaoa892e2d2010-08-10 18:01:35 -070060 is_vmalloc_addr(ptr) ? vfree(ptr) : kfree(ptr);
Vadim Lobanov5466b452006-12-10 02:21:22 -080061}
62
Changli Gaoa892e2d2010-08-10 18:01:35 -070063static void __free_fdtable(struct fdtable *fdt)
Vadim Lobanov5466b452006-12-10 02:21:22 -080064{
Changli Gaoa892e2d2010-08-10 18:01:35 -070065 free_fdmem(fdt->fd);
66 free_fdmem(fdt->open_fds);
67 kfree(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070068}
69
David Howells65f27f32006-11-22 14:55:48 +000070static void free_fdtable_work(struct work_struct *work)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070071{
David Howells65f27f32006-11-22 14:55:48 +000072 struct fdtable_defer *f =
73 container_of(work, struct fdtable_defer, wq);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070074 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070076 spin_lock_bh(&f->lock);
77 fdt = f->next;
78 f->next = NULL;
79 spin_unlock_bh(&f->lock);
80 while(fdt) {
81 struct fdtable *next = fdt->next;
Changli Gaoa892e2d2010-08-10 18:01:35 -070082
83 __free_fdtable(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070084 fdt = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070086}
87
Al Viro7cf4dc32012-08-15 19:56:12 -040088static void free_fdtable_rcu(struct rcu_head *rcu)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070089{
90 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070091 struct fdtable_defer *fddef;
92
93 BUG_ON(!fdt);
Al Viro1983e782012-08-15 20:06:36 -040094 BUG_ON(fdt->max_fds <= NR_OPEN_DEFAULT);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070095
Changli Gaoa892e2d2010-08-10 18:01:35 -070096 if (!is_vmalloc_addr(fdt->fd) && !is_vmalloc_addr(fdt->open_fds)) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070097 kfree(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -080098 kfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070099 kfree(fdt);
100 } else {
101 fddef = &get_cpu_var(fdtable_defer_list);
102 spin_lock(&fddef->lock);
103 fdt->next = fddef->next;
104 fddef->next = fdt;
Tejun Heo593be072006-12-06 20:36:01 -0800105 /* vmallocs are handled from the workqueue context */
106 schedule_work(&fddef->wq);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700107 spin_unlock(&fddef->lock);
108 put_cpu_var(fdtable_defer_list);
109 }
110}
111
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700112/*
113 * Expand the fdset in the files_struct. Called with the files spinlock
114 * held for write.
115 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800116static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700117{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800118 unsigned int cpy, set;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700119
Vadim Lobanov5466b452006-12-10 02:21:22 -0800120 BUG_ON(nfdt->max_fds < ofdt->max_fds);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800121
122 cpy = ofdt->max_fds * sizeof(struct file *);
123 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
124 memcpy(nfdt->fd, ofdt->fd, cpy);
125 memset((char *)(nfdt->fd) + cpy, 0, set);
126
127 cpy = ofdt->max_fds / BITS_PER_BYTE;
128 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
129 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
130 memset((char *)(nfdt->open_fds) + cpy, 0, set);
131 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
132 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Vadim Lobanov5466b452006-12-10 02:21:22 -0800135static struct fdtable * alloc_fdtable(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800137 struct fdtable *fdt;
David Howells1fd36ad2012-02-16 17:49:54 +0000138 void *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700140 /*
Vadim Lobanov5466b452006-12-10 02:21:22 -0800141 * Figure out how many fds we actually want to support in this fdtable.
142 * Allocation steps are keyed to the size of the fdarray, since it
143 * grows far faster than any of the other dynamic data. We try to fit
144 * the fdarray into comfortable page-tuned chunks: starting at 1024B
145 * and growing in powers of two from there on.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700146 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800147 nr /= (1024 / sizeof(struct file *));
148 nr = roundup_pow_of_two(nr + 1);
149 nr *= (1024 / sizeof(struct file *));
Al Viro5c598b32008-04-27 20:04:15 -0400150 /*
151 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
152 * had been set lower between the check in expand_files() and here. Deal
153 * with that in caller, it's cheaper that way.
154 *
155 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
156 * bitmaps handling below becomes unpleasant, to put it mildly...
157 */
158 if (unlikely(nr > sysctl_nr_open))
159 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800160
Vadim Lobanov5466b452006-12-10 02:21:22 -0800161 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
162 if (!fdt)
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800163 goto out;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800164 fdt->max_fds = nr;
165 data = alloc_fdmem(nr * sizeof(struct file *));
166 if (!data)
167 goto out_fdt;
David Howells1fd36ad2012-02-16 17:49:54 +0000168 fdt->fd = data;
169
170 data = alloc_fdmem(max_t(size_t,
Vadim Lobanov5466b452006-12-10 02:21:22 -0800171 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
172 if (!data)
173 goto out_arr;
David Howells1fd36ad2012-02-16 17:49:54 +0000174 fdt->open_fds = data;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800175 data += nr / BITS_PER_BYTE;
David Howells1fd36ad2012-02-16 17:49:54 +0000176 fdt->close_on_exec = data;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800177 fdt->next = NULL;
178
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700179 return fdt;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800180
181out_arr:
Changli Gaoa892e2d2010-08-10 18:01:35 -0700182 free_fdmem(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800183out_fdt:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700184 kfree(fdt);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800185out:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700186 return NULL;
187}
188
189/*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700190 * Expand the file descriptor table.
191 * This function will allocate a new fdtable and both fd array and fdset, of
192 * the given size.
193 * Return <0 error code on error; 1 on successful completion.
194 * The files->file_lock should be held on entry, and will be held on exit.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700195 */
196static int expand_fdtable(struct files_struct *files, int nr)
197 __releases(files->file_lock)
198 __acquires(files->file_lock)
199{
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700200 struct fdtable *new_fdt, *cur_fdt;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 spin_unlock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700203 new_fdt = alloc_fdtable(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 spin_lock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700205 if (!new_fdt)
206 return -ENOMEM;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700207 /*
Al Viro5c598b32008-04-27 20:04:15 -0400208 * extremely unlikely race - sysctl_nr_open decreased between the check in
209 * caller and alloc_fdtable(). Cheaper to catch it here...
210 */
211 if (unlikely(new_fdt->max_fds <= nr)) {
Changli Gaoa892e2d2010-08-10 18:01:35 -0700212 __free_fdtable(new_fdt);
Al Viro5c598b32008-04-27 20:04:15 -0400213 return -EMFILE;
214 }
215 /*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700216 * Check again since another task may have expanded the fd table while
217 * we dropped the lock
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700218 */
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700219 cur_fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800220 if (nr >= cur_fdt->max_fds) {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700221 /* Continue as planned */
222 copy_fdtable(new_fdt, cur_fdt);
223 rcu_assign_pointer(files->fdt, new_fdt);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800224 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
Al Viro1983e782012-08-15 20:06:36 -0400225 call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700226 } else {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700227 /* Somebody else expanded, so undo our attempt */
Changli Gaoa892e2d2010-08-10 18:01:35 -0700228 __free_fdtable(new_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700229 }
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700230 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233/*
234 * Expand files.
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700235 * This function will expand the file structures, if the requested size exceeds
236 * the current capacity and there is room for expansion.
237 * Return <0 error code on error; 0 when nothing done; 1 when files were
238 * expanded and execution may have blocked.
239 * The files->file_lock should be held on entry, and will be held on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
241int expand_files(struct files_struct *files, int nr)
242{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700243 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700245 fdt = files_fdtable(files);
Al Viro4e1e0182008-07-26 16:01:20 -0400246
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700247 /* Do we need to expand? */
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800248 if (nr < fdt->max_fds)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700249 return 0;
Al Viro4e1e0182008-07-26 16:01:20 -0400250
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700251 /* Can we expand? */
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800252 if (nr >= sysctl_nr_open)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700253 return -EMFILE;
254
255 /* All good, so we try */
256 return expand_fdtable(files, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700258
Al Viro02afc6262008-05-08 19:42:56 -0400259static int count_open_files(struct fdtable *fdt)
260{
261 int size = fdt->max_fds;
262 int i;
263
264 /* Find the last open fd */
David Howells1fd36ad2012-02-16 17:49:54 +0000265 for (i = size / BITS_PER_LONG; i > 0; ) {
266 if (fdt->open_fds[--i])
Al Viro02afc6262008-05-08 19:42:56 -0400267 break;
268 }
David Howells1fd36ad2012-02-16 17:49:54 +0000269 i = (i + 1) * BITS_PER_LONG;
Al Viro02afc6262008-05-08 19:42:56 -0400270 return i;
271}
272
Al Viro02afc6262008-05-08 19:42:56 -0400273/*
274 * Allocate a new files structure and copy contents from the
275 * passed in files structure.
276 * errorp will be valid only when the returned files_struct is NULL.
277 */
278struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
279{
280 struct files_struct *newf;
281 struct file **old_fds, **new_fds;
282 int open_files, size, i;
283 struct fdtable *old_fdt, *new_fdt;
284
285 *errorp = -ENOMEM;
Al Viroafbec7f2008-05-08 21:11:17 -0400286 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
Al Viro02afc6262008-05-08 19:42:56 -0400287 if (!newf)
288 goto out;
289
Al Viroafbec7f2008-05-08 21:11:17 -0400290 atomic_set(&newf->count, 1);
291
292 spin_lock_init(&newf->file_lock);
293 newf->next_fd = 0;
294 new_fdt = &newf->fdtab;
295 new_fdt->max_fds = NR_OPEN_DEFAULT;
David Howells1fd36ad2012-02-16 17:49:54 +0000296 new_fdt->close_on_exec = newf->close_on_exec_init;
297 new_fdt->open_fds = newf->open_fds_init;
Al Viroafbec7f2008-05-08 21:11:17 -0400298 new_fdt->fd = &newf->fd_array[0];
Al Viroafbec7f2008-05-08 21:11:17 -0400299 new_fdt->next = NULL;
300
Al Viro02afc6262008-05-08 19:42:56 -0400301 spin_lock(&oldf->file_lock);
302 old_fdt = files_fdtable(oldf);
Al Viro02afc6262008-05-08 19:42:56 -0400303 open_files = count_open_files(old_fdt);
304
305 /*
306 * Check whether we need to allocate a larger fd array and fd set.
Al Viro02afc6262008-05-08 19:42:56 -0400307 */
Al Viroadbecb12008-05-08 21:19:42 -0400308 while (unlikely(open_files > new_fdt->max_fds)) {
Al Viro02afc6262008-05-08 19:42:56 -0400309 spin_unlock(&oldf->file_lock);
Al Viro9dec3c42008-05-08 21:02:45 -0400310
Changli Gaoa892e2d2010-08-10 18:01:35 -0700311 if (new_fdt != &newf->fdtab)
312 __free_fdtable(new_fdt);
Al Viroadbecb12008-05-08 21:19:42 -0400313
Al Viro9dec3c42008-05-08 21:02:45 -0400314 new_fdt = alloc_fdtable(open_files - 1);
315 if (!new_fdt) {
316 *errorp = -ENOMEM;
Al Viro02afc6262008-05-08 19:42:56 -0400317 goto out_release;
Al Viro9dec3c42008-05-08 21:02:45 -0400318 }
319
320 /* beyond sysctl_nr_open; nothing to do */
321 if (unlikely(new_fdt->max_fds < open_files)) {
Changli Gaoa892e2d2010-08-10 18:01:35 -0700322 __free_fdtable(new_fdt);
Al Viro9dec3c42008-05-08 21:02:45 -0400323 *errorp = -EMFILE;
324 goto out_release;
325 }
Al Viro9dec3c42008-05-08 21:02:45 -0400326
Al Viro02afc6262008-05-08 19:42:56 -0400327 /*
328 * Reacquire the oldf lock and a pointer to its fd table
329 * who knows it may have a new bigger fd table. We need
330 * the latest pointer.
331 */
332 spin_lock(&oldf->file_lock);
333 old_fdt = files_fdtable(oldf);
Al Viroadbecb12008-05-08 21:19:42 -0400334 open_files = count_open_files(old_fdt);
Al Viro02afc6262008-05-08 19:42:56 -0400335 }
336
337 old_fds = old_fdt->fd;
338 new_fds = new_fdt->fd;
339
David Howells1fd36ad2012-02-16 17:49:54 +0000340 memcpy(new_fdt->open_fds, old_fdt->open_fds, open_files / 8);
341 memcpy(new_fdt->close_on_exec, old_fdt->close_on_exec, open_files / 8);
Al Viro02afc6262008-05-08 19:42:56 -0400342
343 for (i = open_files; i != 0; i--) {
344 struct file *f = *old_fds++;
345 if (f) {
346 get_file(f);
347 } else {
348 /*
349 * The fd may be claimed in the fd bitmap but not yet
350 * instantiated in the files array if a sibling thread
351 * is partway through open(). So make sure that this
352 * fd is available to the new process.
353 */
David Howells1dce27c2012-02-16 17:49:42 +0000354 __clear_open_fd(open_files - i, new_fdt);
Al Viro02afc6262008-05-08 19:42:56 -0400355 }
356 rcu_assign_pointer(*new_fds++, f);
357 }
358 spin_unlock(&oldf->file_lock);
359
360 /* compute the remainder to be cleared */
361 size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
362
363 /* This is long word aligned thus could use a optimized version */
364 memset(new_fds, 0, size);
365
366 if (new_fdt->max_fds > open_files) {
David Howells1fd36ad2012-02-16 17:49:54 +0000367 int left = (new_fdt->max_fds - open_files) / 8;
368 int start = open_files / BITS_PER_LONG;
Al Viro02afc6262008-05-08 19:42:56 -0400369
David Howells1fd36ad2012-02-16 17:49:54 +0000370 memset(&new_fdt->open_fds[start], 0, left);
371 memset(&new_fdt->close_on_exec[start], 0, left);
Al Viro02afc6262008-05-08 19:42:56 -0400372 }
373
Al Viroafbec7f2008-05-08 21:11:17 -0400374 rcu_assign_pointer(newf->fdt, new_fdt);
375
Al Viro02afc6262008-05-08 19:42:56 -0400376 return newf;
377
378out_release:
379 kmem_cache_free(files_cachep, newf);
380out:
381 return NULL;
382}
383
Al Viro7cf4dc32012-08-15 19:56:12 -0400384static void close_files(struct files_struct * files)
385{
386 int i, j;
387 struct fdtable *fdt;
388
389 j = 0;
390
391 /*
392 * It is safe to dereference the fd table without RCU or
393 * ->file_lock because this is the last reference to the
394 * files structure. But use RCU to shut RCU-lockdep up.
395 */
396 rcu_read_lock();
397 fdt = files_fdtable(files);
398 rcu_read_unlock();
399 for (;;) {
400 unsigned long set;
401 i = j * BITS_PER_LONG;
402 if (i >= fdt->max_fds)
403 break;
404 set = fdt->open_fds[j++];
405 while (set) {
406 if (set & 1) {
407 struct file * file = xchg(&fdt->fd[i], NULL);
408 if (file) {
409 filp_close(file, files);
410 cond_resched();
411 }
412 }
413 i++;
414 set >>= 1;
415 }
416 }
417}
418
419struct files_struct *get_files_struct(struct task_struct *task)
420{
421 struct files_struct *files;
422
423 task_lock(task);
424 files = task->files;
425 if (files)
426 atomic_inc(&files->count);
427 task_unlock(task);
428
429 return files;
430}
431
432void put_files_struct(struct files_struct *files)
433{
434 struct fdtable *fdt;
435
436 if (atomic_dec_and_test(&files->count)) {
437 close_files(files);
Al Virob9e02af2012-08-15 20:00:58 -0400438 /* not really needed, since nobody can see us */
Al Viro7cf4dc32012-08-15 19:56:12 -0400439 rcu_read_lock();
440 fdt = files_fdtable(files);
Al Viro7cf4dc32012-08-15 19:56:12 -0400441 rcu_read_unlock();
Al Virob9e02af2012-08-15 20:00:58 -0400442 /* free the arrays if they are not embedded */
443 if (fdt != &files->fdtab)
444 __free_fdtable(fdt);
445 kmem_cache_free(files_cachep, files);
Al Viro7cf4dc32012-08-15 19:56:12 -0400446 }
447}
448
449void reset_files_struct(struct files_struct *files)
450{
451 struct task_struct *tsk = current;
452 struct files_struct *old;
453
454 old = tsk->files;
455 task_lock(tsk);
456 tsk->files = files;
457 task_unlock(tsk);
458 put_files_struct(old);
459}
460
461void exit_files(struct task_struct *tsk)
462{
463 struct files_struct * files = tsk->files;
464
465 if (files) {
466 task_lock(tsk);
467 tsk->files = NULL;
468 task_unlock(tsk);
469 put_files_struct(files);
470 }
471}
472
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700473static void __devinit fdtable_defer_list_init(int cpu)
474{
475 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
476 spin_lock_init(&fddef->lock);
David Howells65f27f32006-11-22 14:55:48 +0000477 INIT_WORK(&fddef->wq, free_fdtable_work);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700478 fddef->next = NULL;
479}
480
481void __init files_defer_init(void)
482{
483 int i;
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800484 for_each_possible_cpu(i)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700485 fdtable_defer_list_init(i);
Al Viroeceea0b2008-05-10 10:08:32 -0400486 sysctl_nr_open_max = min((size_t)INT_MAX, ~(size_t)0/sizeof(void *)) &
487 -BITS_PER_LONG;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700488}
Al Virof52111b2008-05-08 18:19:16 -0400489
490struct files_struct init_files = {
491 .count = ATOMIC_INIT(1),
492 .fdt = &init_files.fdtab,
493 .fdtab = {
494 .max_fds = NR_OPEN_DEFAULT,
495 .fd = &init_files.fd_array[0],
David Howells1fd36ad2012-02-16 17:49:54 +0000496 .close_on_exec = init_files.close_on_exec_init,
497 .open_fds = init_files.open_fds_init,
Al Virof52111b2008-05-08 18:19:16 -0400498 },
499 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
500};
Al Viro1027abe2008-07-30 04:13:04 -0400501
502/*
503 * allocate a file descriptor, mark it busy.
504 */
Al Virodcfadfa2012-08-12 17:27:30 -0400505int __alloc_fd(struct files_struct *files,
506 unsigned start, unsigned end, unsigned flags)
Al Viro1027abe2008-07-30 04:13:04 -0400507{
Al Viro1027abe2008-07-30 04:13:04 -0400508 unsigned int fd;
509 int error;
510 struct fdtable *fdt;
511
512 spin_lock(&files->file_lock);
513repeat:
514 fdt = files_fdtable(files);
515 fd = start;
516 if (fd < files->next_fd)
517 fd = files->next_fd;
518
519 if (fd < fdt->max_fds)
David Howells1fd36ad2012-02-16 17:49:54 +0000520 fd = find_next_zero_bit(fdt->open_fds, fdt->max_fds, fd);
Al Viro1027abe2008-07-30 04:13:04 -0400521
Al Virof33ff992012-08-12 16:17:59 -0400522 /*
523 * N.B. For clone tasks sharing a files structure, this test
524 * will limit the total number of files that can be opened.
525 */
526 error = -EMFILE;
527 if (fd >= end)
528 goto out;
529
Al Viro1027abe2008-07-30 04:13:04 -0400530 error = expand_files(files, fd);
531 if (error < 0)
532 goto out;
533
534 /*
535 * If we needed to expand the fs array we
536 * might have blocked - try again.
537 */
538 if (error)
539 goto repeat;
540
541 if (start <= files->next_fd)
542 files->next_fd = fd + 1;
543
David Howells1dce27c2012-02-16 17:49:42 +0000544 __set_open_fd(fd, fdt);
Al Viro1027abe2008-07-30 04:13:04 -0400545 if (flags & O_CLOEXEC)
David Howells1dce27c2012-02-16 17:49:42 +0000546 __set_close_on_exec(fd, fdt);
Al Viro1027abe2008-07-30 04:13:04 -0400547 else
David Howells1dce27c2012-02-16 17:49:42 +0000548 __clear_close_on_exec(fd, fdt);
Al Viro1027abe2008-07-30 04:13:04 -0400549 error = fd;
550#if 1
551 /* Sanity check */
Paul E. McKenney7dc52152010-02-22 17:04:52 -0800552 if (rcu_dereference_raw(fdt->fd[fd]) != NULL) {
Al Viro1027abe2008-07-30 04:13:04 -0400553 printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
554 rcu_assign_pointer(fdt->fd[fd], NULL);
555 }
556#endif
557
558out:
559 spin_unlock(&files->file_lock);
560 return error;
561}
562
Al Virodcfadfa2012-08-12 17:27:30 -0400563int alloc_fd(unsigned start, unsigned flags)
564{
565 return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
566}
567
Al Viro1a7bd222012-08-12 17:18:05 -0400568int get_unused_fd_flags(unsigned flags)
Al Viro1027abe2008-07-30 04:13:04 -0400569{
Al Virodcfadfa2012-08-12 17:27:30 -0400570 return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
Al Viro1027abe2008-07-30 04:13:04 -0400571}
Al Viro1a7bd222012-08-12 17:18:05 -0400572EXPORT_SYMBOL(get_unused_fd_flags);
Al Viro56007ca2012-08-15 21:03:26 -0400573
574static void __put_unused_fd(struct files_struct *files, unsigned int fd)
575{
576 struct fdtable *fdt = files_fdtable(files);
577 __clear_open_fd(fd, fdt);
578 if (fd < files->next_fd)
579 files->next_fd = fd;
580}
581
582void put_unused_fd(unsigned int fd)
583{
584 struct files_struct *files = current->files;
585 spin_lock(&files->file_lock);
586 __put_unused_fd(files, fd);
587 spin_unlock(&files->file_lock);
588}
589
590EXPORT_SYMBOL(put_unused_fd);
591
592/*
593 * Install a file pointer in the fd array.
594 *
595 * The VFS is full of places where we drop the files lock between
596 * setting the open_fds bitmap and installing the file in the file
597 * array. At any such point, we are vulnerable to a dup2() race
598 * installing a file in the array before us. We need to detect this and
599 * fput() the struct file we are about to overwrite in this case.
600 *
601 * It should never happen - if we allow dup2() do it, _really_ bad things
602 * will follow.
Al Virof869e8a2012-08-15 21:06:33 -0400603 *
604 * NOTE: __fd_install() variant is really, really low-level; don't
605 * use it unless you are forced to by truly lousy API shoved down
606 * your throat. 'files' *MUST* be either current->files or obtained
607 * by get_files_struct(current) done by whoever had given it to you,
608 * or really bad things will happen. Normally you want to use
609 * fd_install() instead.
Al Viro56007ca2012-08-15 21:03:26 -0400610 */
611
Al Virof869e8a2012-08-15 21:06:33 -0400612void __fd_install(struct files_struct *files, unsigned int fd,
613 struct file *file)
Al Viro56007ca2012-08-15 21:03:26 -0400614{
Al Viro56007ca2012-08-15 21:03:26 -0400615 struct fdtable *fdt;
616 spin_lock(&files->file_lock);
617 fdt = files_fdtable(files);
618 BUG_ON(fdt->fd[fd] != NULL);
619 rcu_assign_pointer(fdt->fd[fd], file);
620 spin_unlock(&files->file_lock);
621}
622
Al Virof869e8a2012-08-15 21:06:33 -0400623void fd_install(unsigned int fd, struct file *file)
624{
625 __fd_install(current->files, fd, file);
626}
627
Al Viro56007ca2012-08-15 21:03:26 -0400628EXPORT_SYMBOL(fd_install);
Al Viro0ee8cdf2012-08-15 21:12:10 -0400629
Al Viro483ce1d2012-08-19 12:04:24 -0400630/*
631 * The same warnings as for __alloc_fd()/__fd_install() apply here...
632 */
633int __close_fd(struct files_struct *files, unsigned fd)
634{
635 struct file *file;
636 struct fdtable *fdt;
637
638 spin_lock(&files->file_lock);
639 fdt = files_fdtable(files);
640 if (fd >= fdt->max_fds)
641 goto out_unlock;
642 file = fdt->fd[fd];
643 if (!file)
644 goto out_unlock;
645 rcu_assign_pointer(fdt->fd[fd], NULL);
646 __clear_close_on_exec(fd, fdt);
647 __put_unused_fd(files, fd);
648 spin_unlock(&files->file_lock);
649 return filp_close(file, files);
650
651out_unlock:
652 spin_unlock(&files->file_lock);
653 return -EBADF;
654}
655
Al Viro6a6d27d2012-08-21 09:56:33 -0400656void do_close_on_exec(struct files_struct *files)
657{
658 unsigned i;
659 struct fdtable *fdt;
660
661 /* exec unshares first */
662 BUG_ON(atomic_read(&files->count) != 1);
663 spin_lock(&files->file_lock);
664 for (i = 0; ; i++) {
665 unsigned long set;
666 unsigned fd = i * BITS_PER_LONG;
667 fdt = files_fdtable(files);
668 if (fd >= fdt->max_fds)
669 break;
670 set = fdt->close_on_exec[i];
671 if (!set)
672 continue;
673 fdt->close_on_exec[i] = 0;
674 for ( ; set ; fd++, set >>= 1) {
675 struct file *file;
676 if (!(set & 1))
677 continue;
678 file = fdt->fd[fd];
679 if (!file)
680 continue;
681 rcu_assign_pointer(fdt->fd[fd], NULL);
682 __put_unused_fd(files, fd);
683 spin_unlock(&files->file_lock);
684 filp_close(file, files);
685 cond_resched();
686 spin_lock(&files->file_lock);
687 }
688
689 }
690 spin_unlock(&files->file_lock);
691}
692
Al Viro0ee8cdf2012-08-15 21:12:10 -0400693struct file *fget(unsigned int fd)
694{
695 struct file *file;
696 struct files_struct *files = current->files;
697
698 rcu_read_lock();
699 file = fcheck_files(files, fd);
700 if (file) {
701 /* File object ref couldn't be taken */
702 if (file->f_mode & FMODE_PATH ||
703 !atomic_long_inc_not_zero(&file->f_count))
704 file = NULL;
705 }
706 rcu_read_unlock();
707
708 return file;
709}
710
711EXPORT_SYMBOL(fget);
712
713struct file *fget_raw(unsigned int fd)
714{
715 struct file *file;
716 struct files_struct *files = current->files;
717
718 rcu_read_lock();
719 file = fcheck_files(files, fd);
720 if (file) {
721 /* File object ref couldn't be taken */
722 if (!atomic_long_inc_not_zero(&file->f_count))
723 file = NULL;
724 }
725 rcu_read_unlock();
726
727 return file;
728}
729
730EXPORT_SYMBOL(fget_raw);
731
732/*
733 * Lightweight file lookup - no refcnt increment if fd table isn't shared.
734 *
735 * You can use this instead of fget if you satisfy all of the following
736 * conditions:
737 * 1) You must call fput_light before exiting the syscall and returning control
738 * to userspace (i.e. you cannot remember the returned struct file * after
739 * returning to userspace).
740 * 2) You must not call filp_close on the returned struct file * in between
741 * calls to fget_light and fput_light.
742 * 3) You must not clone the current task in between the calls to fget_light
743 * and fput_light.
744 *
745 * The fput_needed flag returned by fget_light should be passed to the
746 * corresponding fput_light.
747 */
748struct file *fget_light(unsigned int fd, int *fput_needed)
749{
750 struct file *file;
751 struct files_struct *files = current->files;
752
753 *fput_needed = 0;
754 if (atomic_read(&files->count) == 1) {
755 file = fcheck_files(files, fd);
756 if (file && (file->f_mode & FMODE_PATH))
757 file = NULL;
758 } else {
759 rcu_read_lock();
760 file = fcheck_files(files, fd);
761 if (file) {
762 if (!(file->f_mode & FMODE_PATH) &&
763 atomic_long_inc_not_zero(&file->f_count))
764 *fput_needed = 1;
765 else
766 /* Didn't get the reference, someone's freed */
767 file = NULL;
768 }
769 rcu_read_unlock();
770 }
771
772 return file;
773}
774
775struct file *fget_raw_light(unsigned int fd, int *fput_needed)
776{
777 struct file *file;
778 struct files_struct *files = current->files;
779
780 *fput_needed = 0;
781 if (atomic_read(&files->count) == 1) {
782 file = fcheck_files(files, fd);
783 } else {
784 rcu_read_lock();
785 file = fcheck_files(files, fd);
786 if (file) {
787 if (atomic_long_inc_not_zero(&file->f_count))
788 *fput_needed = 1;
789 else
790 /* Didn't get the reference, someone's freed */
791 file = NULL;
792 }
793 rcu_read_unlock();
794 }
795
796 return file;
797}
Al Virofe17f222012-08-21 11:48:11 -0400798
799void set_close_on_exec(unsigned int fd, int flag)
800{
801 struct files_struct *files = current->files;
802 struct fdtable *fdt;
803 spin_lock(&files->file_lock);
804 fdt = files_fdtable(files);
805 if (flag)
806 __set_close_on_exec(fd, fdt);
807 else
808 __clear_close_on_exec(fd, fdt);
809 spin_unlock(&files->file_lock);
810}
811
812bool get_close_on_exec(unsigned int fd)
813{
814 struct files_struct *files = current->files;
815 struct fdtable *fdt;
816 bool res;
817 rcu_read_lock();
818 fdt = files_fdtable(files);
819 res = close_on_exec(fd, fdt);
820 rcu_read_unlock();
821 return res;
822}
823
Al Viro8280d162012-08-21 12:11:46 -0400824static int do_dup2(struct files_struct *files,
825 struct file *file, unsigned fd, unsigned flags)
826{
827 struct file *tofree;
828 struct fdtable *fdt;
829
830 /*
831 * We need to detect attempts to do dup2() over allocated but still
832 * not finished descriptor. NB: OpenBSD avoids that at the price of
833 * extra work in their equivalent of fget() - they insert struct
834 * file immediately after grabbing descriptor, mark it larval if
835 * more work (e.g. actual opening) is needed and make sure that
836 * fget() treats larval files as absent. Potentially interesting,
837 * but while extra work in fget() is trivial, locking implications
838 * and amount of surgery on open()-related paths in VFS are not.
839 * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
840 * deadlocks in rather amusing ways, AFAICS. All of that is out of
841 * scope of POSIX or SUS, since neither considers shared descriptor
842 * tables and this condition does not arise without those.
843 */
844 fdt = files_fdtable(files);
845 tofree = fdt->fd[fd];
846 if (!tofree && fd_is_open(fd, fdt))
847 goto Ebusy;
848 get_file(file);
849 rcu_assign_pointer(fdt->fd[fd], file);
850 __set_open_fd(fd, fdt);
851 if (flags & O_CLOEXEC)
852 __set_close_on_exec(fd, fdt);
853 else
854 __clear_close_on_exec(fd, fdt);
855 spin_unlock(&files->file_lock);
856
857 if (tofree)
858 filp_close(tofree, files);
859
860 return fd;
861
862Ebusy:
863 spin_unlock(&files->file_lock);
864 return -EBUSY;
865}
866
867int replace_fd(unsigned fd, struct file *file, unsigned flags)
868{
869 int err;
870 struct files_struct *files = current->files;
871
872 if (!file)
873 return __close_fd(files, fd);
874
875 if (fd >= rlimit(RLIMIT_NOFILE))
876 return -EMFILE;
877
878 spin_lock(&files->file_lock);
879 err = expand_files(files, fd);
880 if (unlikely(err < 0))
881 goto out_unlock;
882 return do_dup2(files, file, fd, flags);
883
884out_unlock:
885 spin_unlock(&files->file_lock);
886 return err;
887}
888
Al Virofe17f222012-08-21 11:48:11 -0400889SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
890{
891 int err = -EBADF;
Al Viro8280d162012-08-21 12:11:46 -0400892 struct file *file;
893 struct files_struct *files = current->files;
Al Virofe17f222012-08-21 11:48:11 -0400894
895 if ((flags & ~O_CLOEXEC) != 0)
896 return -EINVAL;
897
898 if (newfd >= rlimit(RLIMIT_NOFILE))
899 return -EMFILE;
900
901 spin_lock(&files->file_lock);
902 err = expand_files(files, newfd);
903 file = fcheck(oldfd);
904 if (unlikely(!file))
905 goto Ebadf;
906 if (unlikely(err < 0)) {
907 if (err == -EMFILE)
908 goto Ebadf;
909 goto out_unlock;
910 }
Al Viro8280d162012-08-21 12:11:46 -0400911 return do_dup2(files, file, newfd, flags);
Al Virofe17f222012-08-21 11:48:11 -0400912
913Ebadf:
914 err = -EBADF;
915out_unlock:
916 spin_unlock(&files->file_lock);
917 return err;
918}
919
920SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
921{
922 if (unlikely(newfd == oldfd)) { /* corner case */
923 struct files_struct *files = current->files;
924 int retval = oldfd;
925
926 rcu_read_lock();
927 if (!fcheck_files(files, oldfd))
928 retval = -EBADF;
929 rcu_read_unlock();
930 return retval;
931 }
932 return sys_dup3(oldfd, newfd, 0);
933}
934
935SYSCALL_DEFINE1(dup, unsigned int, fildes)
936{
937 int ret = -EBADF;
938 struct file *file = fget_raw(fildes);
939
940 if (file) {
941 ret = get_unused_fd();
942 if (ret >= 0)
943 fd_install(ret, file);
944 else
945 fput(file);
946 }
947 return ret;
948}
949
950int f_dupfd(unsigned int from, struct file *file, unsigned flags)
951{
952 int err;
953 if (from >= rlimit(RLIMIT_NOFILE))
954 return -EINVAL;
955 err = alloc_fd(from, flags);
956 if (err >= 0) {
957 get_file(file);
958 fd_install(err, file);
959 }
960 return err;
961}