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/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
*);
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
);
32 new_fds
= (struct file
**) vmalloc(size
);
36 void free_fd_array(struct file
**array
, int num
)
38 int size
= num
* sizeof(struct file
*);
41 printk (KERN_ERR __FUNCTION__
"array = 0 (num = %d)\n", num
);
45 if (num
<= NR_OPEN_DEFAULT
) /* Don't free the embedded fd array! */
47 else if (size
< PAGE_SIZE
)
49 else if (size
== PAGE_SIZE
)
50 free_page((unsigned long) 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
;
67 if (files
->max_fds
>= NR_OPEN
|| nr
> NR_OPEN
)
70 nfds
= files
->max_fds
;
71 write_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 write_lock(&files
->file_lock
);
99 /* Copy the existing array and install the new pointer */
101 if (nfds
> files
->max_fds
) {
102 struct file
**old_fds
;
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() */
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
);
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
);
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
)
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
);
145 new_fdset
= (fd_set
*) vmalloc(size
);
149 void free_fdset(fd_set
*array
, int num
)
154 printk (KERN_ERR __FUNCTION__
"array = 0 (num = %d)\n", num
);
158 if (num
<= __FD_SETSIZE
) /* Don't free an embedded fdset */
160 else if (size
< PAGE_SIZE
)
162 else if (size
== PAGE_SIZE
)
163 free_page((unsigned long) array
);
169 * Expand the fdset in the files_struct. Called with the files spinlock
172 int expand_fdset(struct files_struct
*files
, int nr
)
174 fd_set
*new_openset
= 0, *new_execset
= 0;
178 if (files
->max_fdset
>= NR_OPEN
|| nr
> NR_OPEN
)
181 nfds
= files
->max_fdset
;
182 write_unlock(&files
->file_lock
);
184 /* Expand to the max in easy steps */
186 if (nfds
< (PAGE_SIZE
* 8))
187 nfds
= PAGE_SIZE
* 8;
196 new_openset
= alloc_fdset(nfds
);
197 new_execset
= alloc_fdset(nfds
);
198 write_lock(&files
->file_lock
);
199 if (!new_openset
|| !new_execset
)
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.
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
);
229 /* Somebody expanded the array while we slept ... */
232 write_unlock(&files
->file_lock
);
234 free_fdset(new_openset
, nfds
);
236 free_fdset(new_execset
, nfds
);
237 write_lock(&files
->file_lock
);