i = j * __NFDBITS;
- if (i >= files->max_fds)
+ if (i >= files->max_fds || i >= files->max_fdset)
break;
- set = files->close_on_exec.fds_bits[j];
- files->close_on_exec.fds_bits[j] = 0;
+ set = files->close_on_exec->fds_bits[j];
+ files->close_on_exec->fds_bits[j] = 0;
j++;
for ( ; set ; i++,set >>= 1) {
if (set & 1)
--- fs/fcntl.c.~1~ Fri Nov 13 18:07:26 1998
+++ fs/fcntl.c Fri Mar 19 01:00:56 1999
@@ -12,14 +12,15 @@
extern int sock_fcntl (struct file *, unsigned int cmd, unsigned long arg);
-static inline int dupfd(unsigned int fd, unsigned int arg)
+static inline int dupfd(unsigned int fd, unsigned int start)
{
struct files_struct * files = current->files;
struct file * file;
+ unsigned int newfd;
int error;
error = -EINVAL;
- if (arg >= NR_OPEN)
+ if (start >= NR_OPEN)
goto out;
+ /* We must be able to do the fd setting inside dupfd() without
+ blocking after the sys_close(). */
+ if ((err = expand_files(current->files, newfd)) < 0)
+ goto out;
+
sys_close(newfd);
err = dupfd(oldfd, newfd);
out:
+#ifdef FDSET_DEBUG
+ printk (KERN_ERR __FUNCTION__ ": return %d\n", err);
+#endif
unlock_kernel();
return err;
}
@@ -71,6 +108,10 @@
lock_kernel();
ret = dupfd(fildes, 0);
unlock_kernel();
+#ifdef FDSET_DEBUG
+ if (ret < 0)
+ printk (KERN_ERR __FUNCTION__ ": return %d\n", ret);
+#endif
return ret;
}
@@ -111,19 +152,20 @@
filp = fget(fd);
if (!filp)
goto out;
+
err = 0;
switch (cmd) {
case F_DUPFD:
err = dupfd(fd, arg);
break;
case F_GETFD:
- err = FD_ISSET(fd, ¤t->files->close_on_exec);
+ err = FD_ISSET(fd, current->files->close_on_exec);
break;
case F_SETFD:
if (arg&1)
- FD_SET(fd, ¤t->files->close_on_exec);
+ FD_SET(fd, current->files->close_on_exec);
else
- FD_CLR(fd, ¤t->files->close_on_exec);
+ FD_CLR(fd, current->files->close_on_exec);
break;
case F_GETFL:
err = filp->f_flags;
@@ -151,7 +193,6 @@
err = filp->f_owner.pid;
break;
case F_SETOWN:
- err = 0;
filp->f_owner.pid = arg;
filp->f_owner.uid = current->uid;
filp->f_owner.euid = current->euid;
@@ -171,10 +212,9 @@
break;
default:
/* sockets need a few special fcntls. */
+ err = -EINVAL;
if (S_ISSOCK (filp->f_dentry->d_inode->i_mode))
err = sock_fcntl (filp, cmd, arg);
- else
- err = -EINVAL;
break;
}
fput(filp);
--- fs/file.c.~1~ Fri Mar 19 01:00:56 1999
+++ fs/file.c Fri Mar 19 01:00:56 1999
@@ -0,0 +1,224 @@
+/*
+ * linux/fs/open.c
+ *
+ * Copyright (C) 1998, Stephen Tweedie and Bill Hawes
+ *
+ * Manage the dynamic fd arrays in the process files_struct.
+ */
+
+#include <linux/fs.h>
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <linux/malloc.h>
+#include <linux/vmalloc.h>
+
+#include <asm/bitops.h>
+
+
+/*
+ * Allocate an fd array, using get_free_page() if possible.
+ * Note: the array isn't cleared at allocation time.
+ */
+struct file ** alloc_fd_array(int num)
+{
+ struct file **new_fds;
+ int size = num * sizeof(struct file *);
+
+ if (size < PAGE_SIZE)
+ new_fds = (struct file **) kmalloc(size, GFP_KERNEL);
+ else if (size == PAGE_SIZE)
+ new_fds = (struct file **) __get_free_page(GFP_KERNEL);
+ else
+ new_fds = (struct file **) vmalloc(size);
+ return new_fds;
+}
+
+void free_fd_array(struct file **array, int num)
+{
+ int size = num * sizeof(struct file *);
+
+ if (!array) {
+ printk (KERN_ERR __FUNCTION__ "array = 0 (num = %d)\n", num);
+ return;
+ }
+
+ if (num <= NR_OPEN_DEFAULT) /* Don't free the embedded fd array! */
+ return;
+ else if (size < PAGE_SIZE)
+ kfree(array);
+ else if (size == PAGE_SIZE)
+ free_page((unsigned long) array);
+ else
+ vfree(array);
+}
+
+/*
+ * Expand the fd array in the files_struct.
+ */
+
+int expand_fd_array(struct files_struct *files, int nr)
+{
+ struct file **new_fds;
+ int error, nfds;
+
+
+ error = -EMFILE;
+ if (files->max_fds >= NR_OPEN || nr > NR_OPEN)
+ goto out;
+
+ nfds = files->max_fds;
+
+ /*
+ * Expand to the max in easy steps, and keep expanding it until
+ * we have enough for the requested fd array size.
+ */
+
+ do {
+#if NR_OPEN_DEFAULT < 256
+ if (nfds < 256)
+ nfds = 256;
+ else
+#endif
+ if (nfds < (PAGE_SIZE / sizeof(struct file *)))
+ nfds = PAGE_SIZE / sizeof(struct file *);
+ else {
+ nfds = nfds * 2;
+ if (nfds > NR_OPEN)
+ nfds = NR_OPEN;
+ }
+ } while (nfds < nr);
+
+ error = -ENOMEM;
+ new_fds = alloc_fd_array(nfds);
+ if (!new_fds)
+ goto out;
+
+ /* Copy the existing array and install the new pointer */
+
+ if (nfds > files->max_fds) {
+ struct file **old_fds;
+ int i = files->max_fds;
+
+ old_fds = files->fd;
+ files->fd = new_fds;
+ files->max_fds = nfds;
+ /* Don't copy/clear the array if we are creating a new
+ fd array for fork() */
+ if (i) {
+ memcpy(new_fds, old_fds, i * sizeof(struct file *));
+ /* clear the remainder of the array */
+ memset(&new_fds[i], 0,
+ (nfds-i) * sizeof(struct file *));
+ free_fd_array(old_fds, i);
+ }
+ } else {
+ /* Somebody expanded the array while we slept ... */
+ free_fd_array(new_fds, nfds);
+ }
+ error = 0;
+out:
+ return error;
+}
+
+/*
+ * Allocate an fdset array, using get_free_page() if possible.
+ * Note: the array isn't cleared at allocation time.
+ */
+fd_set * alloc_fdset(int num)
+{
+ fd_set *new_fdset;
+ int size = num / 8;
+
+ if (size < PAGE_SIZE)
+ new_fdset = (fd_set *) kmalloc(size, GFP_KERNEL);
+ else if (size == PAGE_SIZE)
+ new_fdset = (fd_set *) __get_free_page(GFP_KERNEL);
+ else
+ new_fdset = (fd_set *) vmalloc(size);
+ return new_fdset;
+}
+
+void free_fdset(fd_set *array, int num)
+{
+ int size = num / 8;
+
+ if (!array) {
+ printk (KERN_ERR __FUNCTION__ "array = 0 (num = %d)\n", num);
+ return;
+ }
+
+ if (num <= __FD_SETSIZE) /* Don't free an embedded fdset */
+ return;
+ else if (size < PAGE_SIZE)
+ kfree(array);
+ else if (size == PAGE_SIZE)
+ free_page((unsigned long) array);
+ else
+ vfree(array);
+}
+
+/*
+ * Expand the fdset in the files_struct.
+ */
+int expand_fdset(struct files_struct *files, int nr)
+{
+ fd_set *new_openset = 0, *new_execset = 0;
+ int error, nfds = 0;
+
+ error = -EMFILE;
+ if (files->max_fdset >= NR_OPEN || nr > NR_OPEN)
+ goto out;
+
+ nfds = files->max_fdset;
+ /* Expand to the max in easy steps */
+ do {
+ if (nfds < (PAGE_SIZE * 8))
+ nfds = PAGE_SIZE * 8;
+ else {
+ nfds = nfds * 2;
+ if (nfds > NR_OPEN)
+ nfds = NR_OPEN;
+ }
+ } while (nfds < nr);
+
+ error = -ENOMEM;
+ new_openset = alloc_fdset(nfds);
+ new_execset = alloc_fdset(nfds);
+ if (!new_openset || !new_execset)
+ goto out;
+
+ error = 0;
+
+ /* Copy the existing tables and install the new pointers */
+ if (nfds > files->max_fdset) {
+ int i = files->max_fdset / (sizeof(unsigned long) * 8);
+ int count = (nfds - files->max_fdset) / 8;
+
+ /*
+ * Don't copy the entire array if the current fdset is
+ * not yet initialised.
+ */
+ if (i) {
+ memcpy (new_openset, files->open_fds, files->max_fdset/8);
+ memcpy (new_execset, files->close_on_exec, files->max_fdset/8);
+ memset (&new_openset->fds_bits[i], 0, count);
+ memset (&new_execset->fds_bits[i], 0, count);
+ }
+
+ free_fdset (files->close_on_exec, files->max_fdset);
+ free_fdset (files->open_fds, files->max_fdset);
+ files->max_fdset = nfds;
+ files->open_fds = new_openset;
+ files->close_on_exec = new_execset;
+ return 0;
+ }
+ /* Somebody expanded the array while we slept ... */
+
+out:
+ if (new_openset)
+ free_fdset(new_openset, nfds);
+ if (new_execset)
+ free_fdset(new_execset, nfds);
+ return error;
+}
+
--- fs/ioctl.c.~1~ Fri Nov 13 18:07:26 1998
+++ fs/ioctl.c Fri Mar 19 01:00:56 1999
@@ -52,11 +52,11 @@
error = 0;
switch (cmd) {
case FIOCLEX:
- FD_SET(fd, ¤t->files->close_on_exec);
+ FD_SET(fd, current->files->close_on_exec);
break;
case FIONCLEX:
- FD_CLR(fd, ¤t->files->close_on_exec);
+ FD_CLR(fd, current->files->close_on_exec);
break;
case FIONBIO:
--- fs/open.c.~1~ Wed Mar 17 16:12:22 1999
+++ fs/open.c Fri Mar 19 01:00:56 1999
@@ -682,9 +682,13 @@
{
struct files_struct * files = current->files;
int fd, error;
-
+
+repeat:
error = -EMFILE;
- fd = find_first_zero_bit(&files->open_fds, NR_OPEN);
+
+ fd = find_next_zero_bit(files->open_fds,
+ current->files->max_fdset,
+ files->next_fd);
/*
* N.B. For clone tasks sharing a files structure, this test
* will limit the total number of files that can be opened.
@@ -692,10 +696,27 @@
if (fd >= current->rlim[RLIMIT_NOFILE].rlim_cur)
goto out;
- /* Check here for fd > files->max_fds to do dynamic expansion */
+ /* Do we need to expand the fdset array? */
+ if (fd >= current->files->max_fdset) {
+ error = expand_fdset(files, 0);
+ if (!error)
+ goto repeat;
+ goto out;
+ }
+
+ /*
+ * Check whether we need to expand the fd array.
+ */
+ if (fd >= files->max_fds) {
+ error = expand_fd_array(files, 0);
+ if (!error)
+ goto repeat;
+ goto out;
+ }
--- include/linux/fs.h.~1~ Thu Mar 18 15:18:01 1999
+++ include/linux/fs.h Fri Mar 19 01:00:56 1999
@@ -27,17 +27,19 @@
/*
- * It's silly to have NR_OPEN bigger than NR_FILE, but I'll fix
- * that later. Anyway, now the file code is no longer dependent
- * on bitmaps in unsigned longs, but uses the new fd_set structure..
+ * It's silly to have NR_OPEN bigger than NR_FILE, but you can change
+ * the file limit at runtime and only root can increase the per-process
+ * nr_file rlimit, so it's safe to set up a ridiculously high absolute
+ * upper limit on files-per-process.
*
* Some programs (notably those using select()) may have to be
- * recompiled to take full advantage of the new limits..
+ * recompiled to take full advantage of the new limits..
*/