blob: 5110acb1c9ef59bb046a69f79fa601a07218b61a [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>
15#include <linux/bitops.h>
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070016#include <linux/interrupt.h>
17#include <linux/spinlock.h>
18#include <linux/rcupdate.h>
19#include <linux/workqueue.h>
20
21struct fdtable_defer {
22 spinlock_t lock;
23 struct work_struct wq;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070024 struct fdtable *next;
25};
26
Eric Dumazet9cfe0152008-02-06 01:37:16 -080027int sysctl_nr_open __read_mostly = 1024*1024;
28
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070029/*
30 * We use this list to defer free fdtables that have vmalloced
31 * sets/arrays. By keeping a per-cpu list, we avoid having to embed
32 * the work_struct in fdtable itself which avoids a 64 byte (i386) increase in
33 * this per-task structure.
34 */
35static DEFINE_PER_CPU(struct fdtable_defer, fdtable_defer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Vadim Lobanov5466b452006-12-10 02:21:22 -080037static inline void * alloc_fdmem(unsigned int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 if (size <= PAGE_SIZE)
Vadim Lobanov5466b452006-12-10 02:21:22 -080040 return kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 else
Vadim Lobanov5466b452006-12-10 02:21:22 -080042 return vmalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
Vadim Lobanov5466b452006-12-10 02:21:22 -080045static inline void free_fdarr(struct fdtable *fdt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Vadim Lobanov5466b452006-12-10 02:21:22 -080047 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *)))
48 kfree(fdt->fd);
49 else
50 vfree(fdt->fd);
51}
52
53static inline void free_fdset(struct fdtable *fdt)
54{
55 if (fdt->max_fds <= (PAGE_SIZE * BITS_PER_BYTE / 2))
56 kfree(fdt->open_fds);
57 else
58 vfree(fdt->open_fds);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070059}
60
David Howells65f27f32006-11-22 14:55:48 +000061static void free_fdtable_work(struct work_struct *work)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070062{
David Howells65f27f32006-11-22 14:55:48 +000063 struct fdtable_defer *f =
64 container_of(work, struct fdtable_defer, wq);
Dipankar Sarmabadf1662005-09-09 13:04:10 -070065 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070067 spin_lock_bh(&f->lock);
68 fdt = f->next;
69 f->next = NULL;
70 spin_unlock_bh(&f->lock);
71 while(fdt) {
72 struct fdtable *next = fdt->next;
Vadim Lobanov5466b452006-12-10 02:21:22 -080073 vfree(fdt->fd);
74 free_fdset(fdt);
75 kfree(fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070076 fdt = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 }
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070078}
79
Vadim Lobanov4fd45812006-12-10 02:21:17 -080080void free_fdtable_rcu(struct rcu_head *rcu)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070081{
82 struct fdtable *fdt = container_of(rcu, struct fdtable, rcu);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070083 struct fdtable_defer *fddef;
84
85 BUG_ON(!fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070086
Vadim Lobanov4fd45812006-12-10 02:21:17 -080087 if (fdt->max_fds <= NR_OPEN_DEFAULT) {
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070088 /*
Vadim Lobanov4fd45812006-12-10 02:21:17 -080089 * This fdtable is embedded in the files structure and that
90 * structure itself is getting destroyed.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070091 */
Vadim Lobanov4fd45812006-12-10 02:21:17 -080092 kmem_cache_free(files_cachep,
93 container_of(fdt, struct files_struct, fdtab));
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -070094 return;
95 }
Vadim Lobanov5466b452006-12-10 02:21:22 -080096 if (fdt->max_fds <= (PAGE_SIZE / sizeof(struct file *))) {
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);
121 if (ofdt->max_fds == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 return;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800123
124 cpy = ofdt->max_fds * sizeof(struct file *);
125 set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
126 memcpy(nfdt->fd, ofdt->fd, cpy);
127 memset((char *)(nfdt->fd) + cpy, 0, set);
128
129 cpy = ofdt->max_fds / BITS_PER_BYTE;
130 set = (nfdt->max_fds - ofdt->max_fds) / BITS_PER_BYTE;
131 memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
132 memset((char *)(nfdt->open_fds) + cpy, 0, set);
133 memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
134 memset((char *)(nfdt->close_on_exec) + cpy, 0, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Vadim Lobanov5466b452006-12-10 02:21:22 -0800137static struct fdtable * alloc_fdtable(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Vadim Lobanov5466b452006-12-10 02:21:22 -0800139 struct fdtable *fdt;
140 char *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700142 /*
Vadim Lobanov5466b452006-12-10 02:21:22 -0800143 * Figure out how many fds we actually want to support in this fdtable.
144 * Allocation steps are keyed to the size of the fdarray, since it
145 * grows far faster than any of the other dynamic data. We try to fit
146 * the fdarray into comfortable page-tuned chunks: starting at 1024B
147 * and growing in powers of two from there on.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700148 */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800149 nr /= (1024 / sizeof(struct file *));
150 nr = roundup_pow_of_two(nr + 1);
151 nr *= (1024 / sizeof(struct file *));
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800152 if (nr > sysctl_nr_open)
153 nr = sysctl_nr_open;
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800154
Vadim Lobanov5466b452006-12-10 02:21:22 -0800155 fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL);
156 if (!fdt)
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800157 goto out;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800158 fdt->max_fds = nr;
159 data = alloc_fdmem(nr * sizeof(struct file *));
160 if (!data)
161 goto out_fdt;
162 fdt->fd = (struct file **)data;
163 data = alloc_fdmem(max_t(unsigned int,
164 2 * nr / BITS_PER_BYTE, L1_CACHE_BYTES));
165 if (!data)
166 goto out_arr;
167 fdt->open_fds = (fd_set *)data;
168 data += nr / BITS_PER_BYTE;
169 fdt->close_on_exec = (fd_set *)data;
170 INIT_RCU_HEAD(&fdt->rcu);
171 fdt->next = NULL;
172
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700173 return fdt;
Vadim Lobanov5466b452006-12-10 02:21:22 -0800174
175out_arr:
176 free_fdarr(fdt);
177out_fdt:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700178 kfree(fdt);
Vadim Lobanov5466b452006-12-10 02:21:22 -0800179out:
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700180 return NULL;
181}
182
183/*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700184 * Expand the file descriptor table.
185 * This function will allocate a new fdtable and both fd array and fdset, of
186 * the given size.
187 * Return <0 error code on error; 1 on successful completion.
188 * The files->file_lock should be held on entry, and will be held on exit.
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700189 */
190static int expand_fdtable(struct files_struct *files, int nr)
191 __releases(files->file_lock)
192 __acquires(files->file_lock)
193{
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700194 struct fdtable *new_fdt, *cur_fdt;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 spin_unlock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700197 new_fdt = alloc_fdtable(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spin_lock(&files->file_lock);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700199 if (!new_fdt)
200 return -ENOMEM;
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700201 /*
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700202 * Check again since another task may have expanded the fd table while
203 * we dropped the lock
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700204 */
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700205 cur_fdt = files_fdtable(files);
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800206 if (nr >= cur_fdt->max_fds) {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700207 /* Continue as planned */
208 copy_fdtable(new_fdt, cur_fdt);
209 rcu_assign_pointer(files->fdt, new_fdt);
Vadim Lobanov4fd45812006-12-10 02:21:17 -0800210 if (cur_fdt->max_fds > NR_OPEN_DEFAULT)
Vadim Lobanov01b2d932006-12-22 01:10:43 -0800211 free_fdtable(cur_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700212 } else {
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700213 /* Somebody else expanded, so undo our attempt */
Vadim Lobanov5466b452006-12-10 02:21:22 -0800214 free_fdarr(new_fdt);
215 free_fdset(new_fdt);
216 kfree(new_fdt);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700217 }
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700218 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
222 * Expand files.
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700223 * This function will expand the file structures, if the requested size exceeds
224 * the current capacity and there is room for expansion.
225 * Return <0 error code on error; 0 when nothing done; 1 when files were
226 * expanded and execution may have blocked.
227 * The files->file_lock should be held on entry, and will be held on exit.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 */
229int expand_files(struct files_struct *files, int nr)
230{
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700231 struct fdtable *fdt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Dipankar Sarmabadf1662005-09-09 13:04:10 -0700233 fdt = files_fdtable(files);
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700234 /* Do we need to expand? */
Vadim Lobanovbbea9f62006-12-10 02:21:12 -0800235 if (nr < fdt->max_fds)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700236 return 0;
237 /* Can we expand? */
Eric Dumazet9cfe0152008-02-06 01:37:16 -0800238 if (nr >= sysctl_nr_open)
Vadim Lobanov74d392a2006-09-29 02:01:43 -0700239 return -EMFILE;
240
241 /* All good, so we try */
242 return expand_fdtable(files, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700244
245static void __devinit fdtable_defer_list_init(int cpu)
246{
247 struct fdtable_defer *fddef = &per_cpu(fdtable_defer_list, cpu);
248 spin_lock_init(&fddef->lock);
David Howells65f27f32006-11-22 14:55:48 +0000249 INIT_WORK(&fddef->wq, free_fdtable_work);
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700250 fddef->next = NULL;
251}
252
253void __init files_defer_init(void)
254{
255 int i;
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -0800256 for_each_possible_cpu(i)
Dipankar Sarmaab2af1f2005-09-09 13:04:13 -0700257 fdtable_defer_list_init(i);
258}