blob: 0f705c7cfefed4417ce507da9629282120931ae7 [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
9#include <linux/fs.h>
10#include <linux/mm.h>
11#include <linux/time.h>
12#include <linux/slab.h>
13#include <linux/vmalloc.h>
14#include <linux/file.h>
Al Viro9f3acc32008-04-24 07:44:08 -040015#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/bitops.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070017#include <linux/interrupt.h>
18#include <linux/spinlock.h>
19#include <linux/rcupdate.h>
20#include <linux/workqueue.h>
21
22struct fdtable_defer {
23 spinlock_t lock;
24 struct work_struct wq;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070025 struct fdtable *next;
26};
27
Eric Dumazet9cfe0152008-02-06 01:37:16 -080028int sysctl_nr_open __read_mostly = 1024*1024;
29
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070030/*
31 * We use this list to defer free fdtables that have vmalloced
32 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
33 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
34 * this per-task structure.
35 */
36static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Vadim Lobanov5466b452006-12-10 02:21:22 -080038static inline void * alloc_fdmem(unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (size <= PAGE_SIZE)
Vadim Lobanov5466b452006-12-10 02:21:22 -080041 return kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 else
Vadim Lobanov5466b452006-12-10 02:21:22 -080043 return vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
Vadim Lobanov5466b452006-12-10 02:21:22 -080046static inline void free_fdarr(struct fdtable *fdt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Vadim Lobanov5466b452006-12-10 02:21:22 -080048 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
49 kfree(fdt->fd);
50 else
51 vfree(fdt->fd);
52}
53
54static inline void free_fdset(struct fdtable *fdt)
55{
56 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
57 kfree(fdt->open_fds);
58 else
59 vfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070060}
61
David Howells65f27f32006-11-22 14:55:48 +000062static void free_fdtable_work(struct work_struct *work)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070063{
David Howells65f27f32006-11-22 14:55:48 +000064 struct fdtable_defer *f =
65 container_of(work, struct fdtable_defer, wq);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070066 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070068 spin_lock_bh(&f->lock);
69 fdt = f->next;
70 f->next = NULL;
71 spin_unlock_bh(&f->lock);
72 while(fdt) {
73 struct fdtable *next = fdt->next;
Vadim Lobanov5466b452006-12-10 02:21:22 -080074 vfree(fdt->fd);
75 free_fdset(fdt);
76 kfree(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070077 fdt = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070079}
80
Vadim Lobanov4fd45812006-12-10 02:21:17 -080081void free_fdtable_rcu(struct rcu_head *rcu)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070082{
83 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070084 struct fdtable_defer *fddef;
85
86 BUG_ON(!fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070087
Vadim Lobanov4fd45812006-12-10 02:21:17 -080088 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070089 /*
Vadim Lobanov4fd45812006-12-10 02:21:17 -080090 * This fdtable is embedded in the files structure and that
91 * structure itself is getting destroyed.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070092 */
Vadim Lobanov4fd45812006-12-10 02:21:17 -080093 kmem_cache_free(files_cachep,
94 container_of(fdt, struct files_struct, fdtab));
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070095 return;
96 }
Vadim Lobanov5466b452006-12-10 02:21:22 -080097 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070098 kfree(fdt->fd);
Vadim Lobanov5466b452006-12-10 02:21:22 -080099 kfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700100 kfree(fdt);
101 } else {
102 fddef = &get_cpu_var(fdtable_defer_list);
103 spin_lock(&fddef->lock);
104 fdt->next = fddef->next;
105 fddef->next = fdt;
Tejun Heo593be072006-12-06 20:36:01 -0800106 /* vmallocs are handled from the workqueue context */
107 schedule_work(&fddef->wq);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700108 spin_unlock(&fddef->lock);
109 put_cpu_var(fdtable_defer_list);
110 }
111}
112
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700113/*
114 * Expand the fdset in the files_struct. Called with the files spinlock
115 * held for write.
116 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800117static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700118{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800119 unsigned int cpy, set;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700120
Vadim Lobanov5466b452006-12-10 02:21:22 -0800121 BUG_ON(nfdt->max_fds < ofdt->max_fds);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800122
123 cpy = ofdt->max_fds * sizeof(struct file *);
124 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
125 memcpy(nfdt->fd, ofdt->fd, cpy);
126 memset((char *)(nfdt->fd) + cpy, 0, set);
127
128 cpy = ofdt->max_fds / BITS_PER_BYTE;
129 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
130 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
131 memset((char *)(nfdt->open_fds) + cpy, 0, set);
132 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
133 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Vadim Lobanov5466b452006-12-10 02:21:22 -0800136static struct fdtable * alloc_fdtable(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800138 struct fdtable *fdt;
139 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700141 /*
Vadim Lobanov5466b452006-12-10 02:21:22 -0800142 * Figure out how many fds we actually want to support in this fdtable.
143 * Allocation steps are keyed to the size of the fdarray, since it
144 * grows far faster than any of the other dynamic data. We try to fit
145 * the fdarray into comfortable page-tuned chunks: starting at 1024B
146 * and growing in powers of two from there on.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700147 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800148 nr /= (1024 / sizeof(struct file *));
149 nr = roundup_pow_of_two(nr + 1);
150 nr *= (1024 / sizeof(struct file *));
Al Viro5c598b32008-04-27 20:04:15 -0400151 /*
152 * Note that this can drive nr *below* what we had passed if sysctl_nr_open
153 * had been set lower between the check in expand_files() and here. Deal
154 * with that in caller, it's cheaper that way.
155 *
156 * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
157 * bitmaps handling below becomes unpleasant, to put it mildly...
158 */
159 if (unlikely(nr > sysctl_nr_open))
160 nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800161
Vadim Lobanov5466b452006-12-10 02:21:22 -0800162 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
163 if (!fdt)
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800164 goto out;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800165 fdt->max_fds = nr;
166 data = alloc_fdmem(nr * sizeof(struct file *));
167 if (!data)
168 goto out_fdt;
169 fdt->fd = (struct file **)data;
170 data = alloc_fdmem(max_t(unsigned int,
171 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
172 if (!data)
173 goto out_arr;
174 fdt->open_fds = (fd_set *)data;
175 data += nr / BITS_PER_BYTE;
176 fdt->close_on_exec = (fd_set *)data;
177 INIT_RCU_HEAD(&fdt->rcu);
178 fdt->next = NULL;
179
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700180 return fdt;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800181
182out_arr:
183 free_fdarr(fdt);
184out_fdt:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700185 kfree(fdt);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800186out:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700187 return NULL;
188}
189
190/*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700191 * Expand the file descriptor table.
192 * This function will allocate a new fdtable and both fd array and fdset, of
193 * the given size.
194 * Return <0 error code on error; 1 on successful completion.
195 * The files->file_lock should be held on entry, and will be held on exit.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700196 */
197static int expand_fdtable(struct files_struct *files, int nr)
198 __releases(files->file_lock)
199 __acquires(files->file_lock)
200{
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700201 struct fdtable *new_fdt, *cur_fdt;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 spin_unlock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700204 new_fdt = alloc_fdtable(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 spin_lock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700206 if (!new_fdt)
207 return -ENOMEM;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700208 /*
Al Viro5c598b32008-04-27 20:04:15 -0400209 * extremely unlikely race - sysctl_nr_open decreased between the check in
210 * caller and alloc_fdtable(). Cheaper to catch it here...
211 */
212 if (unlikely(new_fdt->max_fds <= nr)) {
213 free_fdarr(new_fdt);
214 free_fdset(new_fdt);
215 kfree(new_fdt);
216 return -EMFILE;
217 }
218 /*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700219 * Check again since another task may have expanded the fd table while
220 * we dropped the lock
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700221 */
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700222 cur_fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800223 if (nr >= cur_fdt->max_fds) {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700224 /* Continue as planned */
225 copy_fdtable(new_fdt, cur_fdt);
226 rcu_assign_pointer(files->fdt, new_fdt);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800227 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
Vadim Lobanov01b2d932006-12-22 01:10:43 -0800228 free_fdtable(cur_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700229 } else {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700230 /* Somebody else expanded, so undo our attempt */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800231 free_fdarr(new_fdt);
232 free_fdset(new_fdt);
233 kfree(new_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700234 }
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700235 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238/*
239 * Expand files.
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700240 * This function will expand the file structures, if the requested size exceeds
241 * the current capacity and there is room for expansion.
242 * Return <0 error code on error; 0 when nothing done; 1 when files were
243 * expanded and execution may have blocked.
244 * The files->file_lock should be held on entry, and will be held on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
246int expand_files(struct files_struct *files, int nr)
247{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700248 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700250 fdt = files_fdtable(files);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700251 /* Do we need to expand? */
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800252 if (nr < fdt->max_fds)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700253 return 0;
254 /* Can we expand? */
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800255 if (nr >= sysctl_nr_open)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700256 return -EMFILE;
257
258 /* All good, so we try */
259 return expand_fdtable(files, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700261
Al Viro02afc6262008-05-08 19:42:56 -0400262static int count_open_files(struct fdtable *fdt)
263{
264 int size = fdt->max_fds;
265 int i;
266
267 /* Find the last open fd */
268 for (i = size/(8*sizeof(long)); i > 0; ) {
269 if (fdt->open_fds->fds_bits[--i])
270 break;
271 }
272 i = (i+1) * 8 * sizeof(long);
273 return i;
274}
275
Al Viro02afc6262008-05-08 19:42:56 -0400276/*
277 * Allocate a new files structure and copy contents from the
278 * passed in files structure.
279 * errorp will be valid only when the returned files_struct is NULL.
280 */
281struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
282{
283 struct files_struct *newf;
284 struct file **old_fds, **new_fds;
285 int open_files, size, i;
286 struct fdtable *old_fdt, *new_fdt;
287
288 *errorp = -ENOMEM;
Al Viroafbec7f2008-05-08 21:11:17 -0400289 newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
Al Viro02afc6262008-05-08 19:42:56 -0400290 if (!newf)
291 goto out;
292
Al Viroafbec7f2008-05-08 21:11:17 -0400293 atomic_set(&newf->count, 1);
294
295 spin_lock_init(&newf->file_lock);
296 newf->next_fd = 0;
297 new_fdt = &newf->fdtab;
298 new_fdt->max_fds = NR_OPEN_DEFAULT;
299 new_fdt->close_on_exec = (fd_set *)&newf->close_on_exec_init;
300 new_fdt->open_fds = (fd_set *)&newf->open_fds_init;
301 new_fdt->fd = &newf->fd_array[0];
302 INIT_RCU_HEAD(&new_fdt->rcu);
303 new_fdt->next = NULL;
304
Al Viro02afc6262008-05-08 19:42:56 -0400305 spin_lock(&oldf->file_lock);
306 old_fdt = files_fdtable(oldf);
Al Viro02afc6262008-05-08 19:42:56 -0400307 open_files = count_open_files(old_fdt);
308
309 /*
310 * Check whether we need to allocate a larger fd array and fd set.
Al Viro02afc6262008-05-08 19:42:56 -0400311 */
Al Viroadbecb12008-05-08 21:19:42 -0400312 while (unlikely(open_files > new_fdt->max_fds)) {
Al Viro02afc6262008-05-08 19:42:56 -0400313 spin_unlock(&oldf->file_lock);
Al Viro9dec3c42008-05-08 21:02:45 -0400314
Al Viroadbecb12008-05-08 21:19:42 -0400315 if (new_fdt != &newf->fdtab) {
316 free_fdarr(new_fdt);
317 free_fdset(new_fdt);
318 kfree(new_fdt);
319 }
320
Al Viro9dec3c42008-05-08 21:02:45 -0400321 new_fdt = alloc_fdtable(open_files - 1);
322 if (!new_fdt) {
323 *errorp = -ENOMEM;
Al Viro02afc6262008-05-08 19:42:56 -0400324 goto out_release;
Al Viro9dec3c42008-05-08 21:02:45 -0400325 }
326
327 /* beyond sysctl_nr_open; nothing to do */
328 if (unlikely(new_fdt->max_fds < open_files)) {
329 free_fdarr(new_fdt);
330 free_fdset(new_fdt);
331 kfree(new_fdt);
332 *errorp = -EMFILE;
333 goto out_release;
334 }
Al Viro9dec3c42008-05-08 21:02:45 -0400335
Al Viro02afc6262008-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 Viro02afc6262008-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 Viro02afc6262008-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);
408}
Al Virof52111b2008-05-08 18:19:16 -0400409
410struct files_struct init_files = {
411 .count = ATOMIC_INIT(1),
412 .fdt = &init_files.fdtab,
413 .fdtab = {
414 .max_fds = NR_OPEN_DEFAULT,
415 .fd = &init_files.fd_array[0],
416 .close_on_exec = (fd_set *)&init_files.close_on_exec_init,
417 .open_fds = (fd_set *)&init_files.open_fds_init,
418 .rcu = RCU_HEAD_INIT,
419 },
420 .file_lock = __SPIN_LOCK_UNLOCKED(init_task.file_lock),
421};