Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / file.c
blob92b5f25985d2f75d09022ebbe1e4a6008cd2132d
1 /*
2 * linux/fs/file.c
4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
6 * Manage the dynamic fd arrays in the process files_struct.
7 */
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>
19 * Allocate an fd array, using kmalloc or vmalloc.
20 * Note: the array isn't cleared at allocation time.
22 struct file ** alloc_fd_array(int num)
24 struct file **new_fds;
25 int size = num * sizeof(struct file *);
27 if (size <= PAGE_SIZE)
28 new_fds = (struct file **) kmalloc(size, GFP_KERNEL);
29 else
30 new_fds = (struct file **) vmalloc(size);
31 return new_fds;
34 void free_fd_array(struct file **array, int num)
36 int size = num * sizeof(struct file *);
38 if (!array) {
39 printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
40 return;
43 if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
44 return;
45 else if (size <= PAGE_SIZE)
46 kfree(array);
47 else
48 vfree(array);
52 * Expand the fd array in the files_struct. Called with the files
53 * spinlock held for write.
56 static int expand_fd_array(struct files_struct *files, int nr)
57 __releases(files->file_lock)
58 __acquires(files->file_lock)
60 struct file **new_fds;
61 int error, nfds;
64 error = -EMFILE;
65 if (files->max_fds >= NR_OPEN || nr >= NR_OPEN)
66 goto out;
68 nfds = files->max_fds;
69 spin_unlock(&files->file_lock);
71 /*
72 * Expand to the max in easy steps, and keep expanding it until
73 * we have enough for the requested fd array size.
76 do {
77 #if NR_OPEN_DEFAULT < 256
78 if (nfds < 256)
79 nfds = 256;
80 else
81 #endif
82 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
83 nfds = PAGE_SIZE / sizeof(struct file *);
84 else {
85 nfds = nfds * 2;
86 if (nfds > NR_OPEN)
87 nfds = NR_OPEN;
89 } while (nfds <= nr);
91 error = -ENOMEM;
92 new_fds = alloc_fd_array(nfds);
93 spin_lock(&files->file_lock);
94 if (!new_fds)
95 goto out;
97 /* Copy the existing array and install the new pointer */
99 if (nfds > files->max_fds) {
100 struct file **old_fds;
101 int i;
103 old_fds = xchg(&files->fd, new_fds);
104 i = xchg(&files->max_fds, nfds);
106 /* Don't copy/clear the array if we are creating a new
107 fd array for fork() */
108 if (i) {
109 memcpy(new_fds, old_fds, i * sizeof(struct file *));
110 /* clear the remainder of the array */
111 memset(&new_fds[i], 0,
112 (nfds-i) * sizeof(struct file *));
114 spin_unlock(&files->file_lock);
115 free_fd_array(old_fds, i);
116 spin_lock(&files->file_lock);
118 } else {
119 /* Somebody expanded the array while we slept ... */
120 spin_unlock(&files->file_lock);
121 free_fd_array(new_fds, nfds);
122 spin_lock(&files->file_lock);
124 error = 0;
125 out:
126 return error;
130 * Allocate an fdset array, using kmalloc or vmalloc.
131 * Note: the array isn't cleared at allocation time.
133 fd_set * alloc_fdset(int num)
135 fd_set *new_fdset;
136 int size = num / 8;
138 if (size <= PAGE_SIZE)
139 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
140 else
141 new_fdset = (fd_set *) vmalloc(size);
142 return new_fdset;
145 void free_fdset(fd_set *array, int num)
147 int size = num / 8;
149 if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
150 return;
151 else if (size <= PAGE_SIZE)
152 kfree(array);
153 else
154 vfree(array);
158 * Expand the fdset in the files_struct. Called with the files spinlock
159 * held for write.
161 static int expand_fdset(struct files_struct *files, int nr)
162 __releases(file->file_lock)
163 __acquires(file->file_lock)
165 fd_set *new_openset = NULL, *new_execset = NULL;
166 int error, nfds = 0;
168 error = -EMFILE;
169 if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN)
170 goto out;
172 nfds = files->max_fdset;
173 spin_unlock(&files->file_lock);
175 /* Expand to the max in easy steps */
176 do {
177 if (nfds < (PAGE_SIZE * 8))
178 nfds = PAGE_SIZE * 8;
179 else {
180 nfds = nfds * 2;
181 if (nfds > NR_OPEN)
182 nfds = NR_OPEN;
184 } while (nfds <= nr);
186 error = -ENOMEM;
187 new_openset = alloc_fdset(nfds);
188 new_execset = alloc_fdset(nfds);
189 spin_lock(&files->file_lock);
190 if (!new_openset || !new_execset)
191 goto out;
193 error = 0;
195 /* Copy the existing tables and install the new pointers */
196 if (nfds > files->max_fdset) {
197 int i = files->max_fdset / (sizeof(unsigned long) * 8);
198 int count = (nfds - files->max_fdset) / 8;
201 * Don't copy the entire array if the current fdset is
202 * not yet initialised.
204 if (i) {
205 memcpy (new_openset, files->open_fds, files->max_fdset/8);
206 memcpy (new_execset, files->close_on_exec, files->max_fdset/8);
207 memset (&new_openset->fds_bits[i], 0, count);
208 memset (&new_execset->fds_bits[i], 0, count);
211 nfds = xchg(&files->max_fdset, nfds);
212 new_openset = xchg(&files->open_fds, new_openset);
213 new_execset = xchg(&files->close_on_exec, new_execset);
214 spin_unlock(&files->file_lock);
215 free_fdset (new_openset, nfds);
216 free_fdset (new_execset, nfds);
217 spin_lock(&files->file_lock);
218 return 0;
220 /* Somebody expanded the array while we slept ... */
222 out:
223 spin_unlock(&files->file_lock);
224 if (new_openset)
225 free_fdset(new_openset, nfds);
226 if (new_execset)
227 free_fdset(new_execset, nfds);
228 spin_lock(&files->file_lock);
229 return error;
233 * Expand files.
234 * Return <0 on error; 0 nothing done; 1 files expanded, we may have blocked.
235 * Should be called with the files->file_lock spinlock held for write.
237 int expand_files(struct files_struct *files, int nr)
239 int err, expand = 0;
241 if (nr >= files->max_fdset) {
242 expand = 1;
243 if ((err = expand_fdset(files, nr)))
244 goto out;
246 if (nr >= files->max_fds) {
247 expand = 1;
248 if ((err = expand_fd_array(files, nr)))
249 goto out;
251 err = expand;
252 out:
253 return err;