4 * Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
6 * Manage the dynamic fd arrays in the process files_struct.
11 #include <linux/sched.h>
12 #include <linux/slab.h>
13 #include <linux/vmalloc.h>
15 #include <asm/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 "%s array = 0 (num = %d)\n",
44 if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
46 else if (size <= PAGE_SIZE)
53 * Expand the fd array in the files_struct. Called with the files
54 * spinlock held for write.
57 int expand_fd_array(struct files_struct *files, int nr)
59 struct file **new_fds;
64 if (files->max_fds >= NR_OPEN || nr >= NR_OPEN)
67 nfds = files->max_fds;
68 write_unlock(&files->file_lock);
71 * Expand to the max in easy steps, and keep expanding it until
72 * we have enough for the requested fd array size.
76 #if NR_OPEN_DEFAULT < 256
81 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
82 nfds = PAGE_SIZE / sizeof(struct file *);
91 new_fds = alloc_fd_array(nfds);
92 write_lock(&files->file_lock);
96 /* Copy the existing array and install the new pointer */
98 if (nfds > files->max_fds) {
99 struct file **old_fds;
102 old_fds = xchg(&files->fd, new_fds);
103 i = xchg(&files->max_fds, nfds);
105 /* Don't copy/clear the array if we are creating a new
106 fd array for fork() */
108 memcpy(new_fds, old_fds, i * sizeof(struct file *));
109 /* clear the remainder of the array */
110 memset(&new_fds[i], 0,
111 (nfds-i) * sizeof(struct file *));
113 write_unlock(&files->file_lock);
114 free_fd_array(old_fds, i);
115 write_lock(&files->file_lock);
118 /* Somebody expanded the array while we slept ... */
119 write_unlock(&files->file_lock);
120 free_fd_array(new_fds, nfds);
121 write_lock(&files->file_lock);
129 * Allocate an fdset array, using kmalloc or vmalloc.
130 * Note: the array isn't cleared at allocation time.
132 fd_set * alloc_fdset(int num)
137 if (size <= PAGE_SIZE)
138 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
140 new_fdset = (fd_set *) vmalloc(size);
144 void free_fdset(fd_set *array, int num)
149 printk(KERN_ERR "%s array = 0 (num = %d)\n",
154 if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
156 else if (size <= PAGE_SIZE)
163 * Expand the fdset in the files_struct. Called with the files spinlock
166 int expand_fdset(struct files_struct *files, int nr)
168 fd_set *new_openset = 0, *new_execset = 0;
172 if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN)
175 nfds = files->max_fdset;
176 write_unlock(&files->file_lock);
178 /* Expand to the max in easy steps */
180 if (nfds < (PAGE_SIZE * 8))
181 nfds = PAGE_SIZE * 8;
187 } while (nfds <= nr);
190 new_openset = alloc_fdset(nfds);
191 new_execset = alloc_fdset(nfds);
192 write_lock(&files->file_lock);
193 if (!new_openset || !new_execset)
198 /* Copy the existing tables and install the new pointers */
199 if (nfds > files->max_fdset) {
200 int i = files->max_fdset / (sizeof(unsigned long) * 8);
201 int count = (nfds - files->max_fdset) / 8;
204 * Don't copy the entire array if the current fdset is
205 * not yet initialised.
208 memcpy (new_openset, files->open_fds, files->max_fdset/8);
209 memcpy (new_execset, files->close_on_exec, files->max_fdset/8);
210 memset (&new_openset->fds_bits[i], 0, count);
211 memset (&new_execset->fds_bits[i], 0, count);
214 nfds = xchg(&files->max_fdset, nfds);
215 new_openset = xchg(&files->open_fds, new_openset);
216 new_execset = xchg(&files->close_on_exec, new_execset);
217 write_unlock(&files->file_lock);
218 free_fdset (new_openset, nfds);
219 free_fdset (new_execset, nfds);
220 write_lock(&files->file_lock);
223 /* Somebody expanded the array while we slept ... */
226 write_unlock(&files->file_lock);
228 free_fdset(new_openset, nfds);
230 free_fdset(new_execset, nfds);
231 write_lock(&files->file_lock);