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;
65 if (files->max_fds >= NR_OPEN || nr >= NR_OPEN)
68 nfds = files->max_fds;
69 spin_unlock(&files->file_lock);
72 * Expand to the max in easy steps, and keep expanding it until
73 * we have enough for the requested fd array size.
77 #if NR_OPEN_DEFAULT < 256
82 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
83 nfds = PAGE_SIZE / sizeof(struct file *);
92 new_fds = alloc_fd_array(nfds);
93 spin_lock(&files->file_lock);
97 /* Copy the existing array and install the new pointer */
99 if (nfds > files->max_fds) {
100 struct file **old_fds;
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() */
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);
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);
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)
138 if (size <= PAGE_SIZE)
139 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
141 new_fdset = (fd_set *) vmalloc(size);
145 void free_fdset(fd_set *array, int num)
149 if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
151 else if (size <= PAGE_SIZE)
158 * Expand the fdset in the files_struct. Called with the files spinlock
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;
169 if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN)
172 nfds = files->max_fdset;
173 spin_unlock(&files->file_lock);
175 /* Expand to the max in easy steps */
177 if (nfds < (PAGE_SIZE * 8))
178 nfds = PAGE_SIZE * 8;
184 } while (nfds <= nr);
187 new_openset = alloc_fdset(nfds);
188 new_execset = alloc_fdset(nfds);
189 spin_lock(&files->file_lock);
190 if (!new_openset || !new_execset)
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.
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);
220 /* Somebody expanded the array while we slept ... */
223 spin_unlock(&files->file_lock);
225 free_fdset(new_openset, nfds);
227 free_fdset(new_execset, nfds);
228 spin_lock(&files->file_lock);
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)
241 if (nr >= files->max_fdset) {
243 if ((err = expand_fdset(files, nr)))
246 if (nr >= files->max_fds) {
248 if ((err = expand_fd_array(files, nr)))