4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
6 * Manage the dynamic fd arrays in the process files_struct.
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);
30 new_fds = (struct file **) vmalloc(size);
34 void free_fd_array(struct file **array, int num)
36 int size = num * sizeof(struct file *);
39 printk (KERN_ERR "free_fd_array: array = 0 (num = %d)\n", num);
43 if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
45 else if (size <= PAGE_SIZE)
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;
66 fdt = files_fdtable(files);
67 if (fdt->max_fds >= NR_OPEN || nr >= NR_OPEN)
71 spin_unlock(&files->file_lock);
74 * Expand to the max in easy steps, and keep expanding it until
75 * we have enough for the requested fd array size.
79 #if NR_OPEN_DEFAULT < 256
84 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
85 nfds = PAGE_SIZE / sizeof(struct file *);
94 new_fds = alloc_fd_array(nfds);
95 spin_lock(&files->file_lock);
99 /* Copy the existing array and install the new pointer */
100 fdt = files_fdtable(files);
102 if (nfds > fdt->max_fds) {
103 struct file **old_fds;
106 old_fds = xchg(&fdt->fd, new_fds);
107 i = xchg(&fdt->max_fds, nfds);
109 /* Don't copy/clear the array if we are creating a new
110 fd array for fork() */
112 memcpy(new_fds, old_fds, i * sizeof(struct file *));
113 /* clear the remainder of the array */
114 memset(&new_fds[i], 0,
115 (nfds-i) * sizeof(struct file *));
117 spin_unlock(&files->file_lock);
118 free_fd_array(old_fds, i);
119 spin_lock(&files->file_lock);
122 /* Somebody expanded the array while we slept ... */
123 spin_unlock(&files->file_lock);
124 free_fd_array(new_fds, nfds);
125 spin_lock(&files->file_lock);
133 * Allocate an fdset array, using kmalloc or vmalloc.
134 * Note: the array isn't cleared at allocation time.
136 fd_set * alloc_fdset(int num)
141 if (size <= PAGE_SIZE)
142 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
144 new_fdset = (fd_set *) vmalloc(size);
148 void free_fdset(fd_set *array, int num)
152 if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
154 else if (size <= PAGE_SIZE)
161 * Expand the fdset in the files_struct. Called with the files spinlock
164 static int expand_fdset(struct files_struct *files, int nr)
165 __releases(file->file_lock)
166 __acquires(file->file_lock)
168 fd_set *new_openset = NULL, *new_execset = NULL;
173 fdt = files_fdtable(files);
174 if (fdt->max_fdset >= NR_OPEN || nr >= NR_OPEN)
177 nfds = fdt->max_fdset;
178 spin_unlock(&files->file_lock);
180 /* Expand to the max in easy steps */
182 if (nfds < (PAGE_SIZE * 8))
183 nfds = PAGE_SIZE * 8;
189 } while (nfds <= nr);
192 new_openset = alloc_fdset(nfds);
193 new_execset = alloc_fdset(nfds);
194 spin_lock(&files->file_lock);
195 if (!new_openset || !new_execset)
200 /* Copy the existing tables and install the new pointers */
201 fdt = files_fdtable(files);
202 if (nfds > fdt->max_fdset) {
203 int i = fdt->max_fdset / (sizeof(unsigned long) * 8);
204 int count = (nfds - fdt->max_fdset) / 8;
207 * Don't copy the entire array if the current fdset is
208 * not yet initialised.
211 memcpy (new_openset, fdt->open_fds, fdt->max_fdset/8);
212 memcpy (new_execset, fdt->close_on_exec, fdt->max_fdset/8);
213 memset (&new_openset->fds_bits[i], 0, count);
214 memset (&new_execset->fds_bits[i], 0, count);
217 nfds = xchg(&fdt->max_fdset, nfds);
218 new_openset = xchg(&fdt->open_fds, new_openset);
219 new_execset = xchg(&fdt->close_on_exec, new_execset);
220 spin_unlock(&files->file_lock);
221 free_fdset (new_openset, nfds);
222 free_fdset (new_execset, nfds);
223 spin_lock(&files->file_lock);
226 /* Somebody expanded the array while we slept ... */
229 spin_unlock(&files->file_lock);
231 free_fdset(new_openset, nfds);
233 free_fdset(new_execset, nfds);
234 spin_lock(&files->file_lock);
240 * Return <0 on error; 0 nothing done; 1 files expanded, we may have blocked.
241 * Should be called with the files->file_lock spinlock held for write.
243 int expand_files(struct files_struct *files, int nr)
248 fdt = files_fdtable(files);
249 if (nr >= fdt->max_fdset) {
251 if ((err = expand_fdset(files, nr)))
254 if (nr >= fdt->max_fds) {
256 if ((err = expand_fd_array(files, nr)))