Import 2.3.12pre9
[davej-history.git] / fs / file.c
blobfd33dc8b8482f64bfa1dd365783772a94a239460
1 /*
2 * linux/fs/open.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/sched.h>
12 #include <linux/malloc.h>
13 #include <linux/vmalloc.h>
15 #include <asm/bitops.h>
19 * Allocate an fd array, using get_free_page() if possible.
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 if (size == PAGE_SIZE)
30 new_fds = (struct file **) __get_free_page(GFP_KERNEL);
31 else
32 new_fds = (struct file **) vmalloc(size);
33 return new_fds;
36 void free_fd_array(struct file **array, int num)
38 int size = num * sizeof(struct file *);
40 if (!array) {
41 printk (KERN_ERR __FUNCTION__ "array = 0 (num = %d)\n", num);
42 return;
45 if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
46 return;
47 else if (size < PAGE_SIZE)
48 kfree(array);
49 else if (size == PAGE_SIZE)
50 free_page((unsigned long) array);
51 else
52 vfree(array);
56 * Expand the fd array in the files_struct. Called with the files
57 * spinlock held for write.
60 int expand_fd_array(struct files_struct *files, int nr)
62 struct file **new_fds;
63 int error, nfds;
66 error = -EMFILE;
67 if (files->max_fds >= NR_OPEN || nr > NR_OPEN)
68 goto out;
70 nfds = files->max_fds;
71 write_unlock(&files->file_lock);
73 /*
74 * Expand to the max in easy steps, and keep expanding it until
75 * we have enough for the requested fd array size.
78 do {
79 #if NR_OPEN_DEFAULT < 256
80 if (nfds < 256)
81 nfds = 256;
82 else
83 #endif
84 if (nfds < (PAGE_SIZE / sizeof(struct file *)))
85 nfds = PAGE_SIZE / sizeof(struct file *);
86 else {
87 nfds = nfds * 2;
88 if (nfds > NR_OPEN)
89 nfds = NR_OPEN;
91 } while (nfds < nr);
93 error = -ENOMEM;
94 new_fds = alloc_fd_array(nfds);
95 write_lock(&files->file_lock);
96 if (!new_fds)
97 goto out;
99 /* Copy the existing array and install the new pointer */
101 if (nfds > files->max_fds) {
102 struct file **old_fds;
103 int i;
105 old_fds = xchg(&files->fd, new_fds);
106 i = xchg(&files->max_fds, nfds);
108 /* Don't copy/clear the array if we are creating a new
109 fd array for fork() */
110 if (i) {
111 memcpy(new_fds, old_fds, i * sizeof(struct file *));
112 /* clear the remainder of the array */
113 memset(&new_fds[i], 0,
114 (nfds-i) * sizeof(struct file *));
116 write_unlock(&files->file_lock);
117 free_fd_array(old_fds, i);
118 write_lock(&files->file_lock);
120 } else {
121 /* Somebody expanded the array while we slept ... */
122 write_unlock(&files->file_lock);
123 free_fd_array(new_fds, nfds);
124 write_lock(&files->file_lock);
126 error = 0;
127 out:
128 return error;
132 * Allocate an fdset array, using get_free_page() if possible.
133 * Note: the array isn't cleared at allocation time.
135 fd_set * alloc_fdset(int num)
137 fd_set *new_fdset;
138 int size = num / 8;
140 if (size < PAGE_SIZE)
141 new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
142 else if (size == PAGE_SIZE)
143 new_fdset = (fd_set *) __get_free_page(GFP_KERNEL);
144 else
145 new_fdset = (fd_set *) vmalloc(size);
146 return new_fdset;
149 void free_fdset(fd_set *array, int num)
151 int size = num / 8;
153 if (!array) {
154 printk (KERN_ERR __FUNCTION__ "array = 0 (num = %d)\n", num);
155 return;
158 if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
159 return;
160 else if (size < PAGE_SIZE)
161 kfree(array);
162 else if (size == PAGE_SIZE)
163 free_page((unsigned long) array);
164 else
165 vfree(array);
169 * Expand the fdset in the files_struct. Called with the files spinlock
170 * held for write.
172 int expand_fdset(struct files_struct *files, int nr)
174 fd_set *new_openset = 0, *new_execset = 0;
175 int error, nfds = 0;
177 error = -EMFILE;
178 if (files->max_fdset >= NR_OPEN || nr > NR_OPEN)
179 goto out;
181 nfds = files->max_fdset;
182 write_unlock(&files->file_lock);
184 /* Expand to the max in easy steps */
185 do {
186 if (nfds < (PAGE_SIZE * 8))
187 nfds = PAGE_SIZE * 8;
188 else {
189 nfds = nfds * 2;
190 if (nfds > NR_OPEN)
191 nfds = NR_OPEN;
193 } while (nfds < nr);
195 error = -ENOMEM;
196 new_openset = alloc_fdset(nfds);
197 new_execset = alloc_fdset(nfds);
198 write_lock(&files->file_lock);
199 if (!new_openset || !new_execset)
200 goto out;
202 error = 0;
204 /* Copy the existing tables and install the new pointers */
205 if (nfds > files->max_fdset) {
206 int i = files->max_fdset / (sizeof(unsigned long) * 8);
207 int count = (nfds - files->max_fdset) / 8;
210 * Don't copy the entire array if the current fdset is
211 * not yet initialised.
213 if (i) {
214 memcpy (new_openset, files->open_fds, files->max_fdset/8);
215 memcpy (new_execset, files->close_on_exec, files->max_fdset/8);
216 memset (&new_openset->fds_bits[i], 0, count);
217 memset (&new_execset->fds_bits[i], 0, count);
220 nfds = xchg(&files->max_fdset, nfds);
221 new_openset = xchg(&files->open_fds, new_openset);
222 new_execset = xchg(&files->close_on_exec, new_execset);
223 write_unlock(&files->file_lock);
224 free_fdset (new_openset, nfds);
225 free_fdset (new_execset, nfds);
226 write_lock(&files->file_lock);
227 return 0;
229 /* Somebody expanded the array while we slept ... */
231 out:
232 write_unlock(&files->file_lock);
233 if (new_openset)
234 free_fdset(new_openset, nfds);
235 if (new_execset)
236 free_fdset(new_execset, nfds);
237 write_lock(&files->file_lock);
238 return error;