Commit badf1662 authored by Dipankar Sarma's avatar Dipankar Sarma Committed by Linus Torvalds

[PATCH] files: break up files struct

In order for the RCU to work, the file table array, sets and their sizes must
be updated atomically.  Instead of ensuring this through too many memory
barriers, we put the arrays and their sizes in a separate structure.  This
patch takes the first step of putting the file table elements in a separate
structure fdtable that is embedded withing files_struct.  It also changes all
the users to refer to the file table using files_fdtable() macro.  Subsequent
applciation of RCU becomes easier after this.
Signed-off-by: default avatarDipankar Sarma <dipankar@in.ibm.com>
Signed-Off-By: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent c0dfb290
...@@ -974,6 +974,7 @@ osf_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, ...@@ -974,6 +974,7 @@ osf_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp,
size_t size; size_t size;
long timeout; long timeout;
int ret = -EINVAL; int ret = -EINVAL;
struct fdtable *fdt;
timeout = MAX_SCHEDULE_TIMEOUT; timeout = MAX_SCHEDULE_TIMEOUT;
if (tvp) { if (tvp) {
...@@ -995,7 +996,8 @@ osf_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, ...@@ -995,7 +996,8 @@ osf_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp,
} }
} }
if (n < 0 || n > current->files->max_fdset) fdt = files_fdtable(current->files);
if (n < 0 || n > fdt->max_fdset)
goto out_nofds; goto out_nofds;
/* /*
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <linux/pagemap.h> #include <linux/pagemap.h>
#include <linux/mount.h> #include <linux/mount.h>
#include <linux/bitops.h> #include <linux/bitops.h>
#include <linux/rcupdate.h>
#include <asm/errno.h> #include <asm/errno.h>
#include <asm/intrinsics.h> #include <asm/intrinsics.h>
...@@ -2217,15 +2218,17 @@ static void ...@@ -2217,15 +2218,17 @@ static void
pfm_free_fd(int fd, struct file *file) pfm_free_fd(int fd, struct file *file)
{ {
struct files_struct *files = current->files; struct files_struct *files = current->files;
struct fdtable *fdt = files_fdtable(files);
/* /*
* there ie no fd_uninstall(), so we do it here * there ie no fd_uninstall(), so we do it here
*/ */
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
files->fd[fd] = NULL; rcu_assign_pointer(fdt->fd[fd], NULL);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
if (file) put_filp(file); if (file)
put_filp(file);
put_unused_fd(fd); put_unused_fd(fd);
} }
......
...@@ -293,11 +293,13 @@ static struct module_info { ...@@ -293,11 +293,13 @@ static struct module_info {
static inline int solaris_sockmod(unsigned int fd, unsigned int cmd, u32 arg) static inline int solaris_sockmod(unsigned int fd, unsigned int cmd, u32 arg)
{ {
struct inode *ino; struct inode *ino;
struct fdtable *fdt;
/* I wonder which of these tests are superfluous... --patrik */ /* I wonder which of these tests are superfluous... --patrik */
spin_lock(&current->files->file_lock); spin_lock(&current->files->file_lock);
if (! current->files->fd[fd] || fdt = files_fdtable(current->files);
! current->files->fd[fd]->f_dentry || if (! fdt->fd[fd] ||
! (ino = current->files->fd[fd]->f_dentry->d_inode) || ! fdt->fd[fd]->f_dentry ||
! (ino = fdt->fd[fd]->f_dentry->d_inode) ||
! S_ISSOCK(ino->i_mode)) { ! S_ISSOCK(ino->i_mode)) {
spin_unlock(&current->files->file_lock); spin_unlock(&current->files->file_lock);
return TBADF; return TBADF;
......
...@@ -2454,6 +2454,7 @@ static void __do_SAK(void *arg) ...@@ -2454,6 +2454,7 @@ static void __do_SAK(void *arg)
int i; int i;
struct file *filp; struct file *filp;
struct tty_ldisc *disc; struct tty_ldisc *disc;
struct fdtable *fdt;
if (!tty) if (!tty)
return; return;
...@@ -2480,7 +2481,8 @@ static void __do_SAK(void *arg) ...@@ -2480,7 +2481,8 @@ static void __do_SAK(void *arg)
task_lock(p); task_lock(p);
if (p->files) { if (p->files) {
spin_lock(&p->files->file_lock); spin_lock(&p->files->file_lock);
for (i=0; i < p->files->max_fds; i++) { fdt = files_fdtable(p->files);
for (i=0; i < fdt->max_fds; i++) {
filp = fcheck_files(p->files, i); filp = fcheck_files(p->files, i);
if (!filp) if (!filp)
continue; continue;
......
...@@ -798,6 +798,7 @@ static inline int de_thread(struct task_struct *tsk) ...@@ -798,6 +798,7 @@ static inline int de_thread(struct task_struct *tsk)
static inline void flush_old_files(struct files_struct * files) static inline void flush_old_files(struct files_struct * files)
{ {
long j = -1; long j = -1;
struct fdtable *fdt;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
for (;;) { for (;;) {
...@@ -805,12 +806,13 @@ static inline void flush_old_files(struct files_struct * files) ...@@ -805,12 +806,13 @@ static inline void flush_old_files(struct files_struct * files)
j++; j++;
i = j * __NFDBITS; i = j * __NFDBITS;
if (i >= files->max_fds || i >= files->max_fdset) fdt = files_fdtable(files);
if (i >= fdt->max_fds || i >= fdt->max_fdset)
break; break;
set = files->close_on_exec->fds_bits[j]; set = fdt->close_on_exec->fds_bits[j];
if (!set) if (!set)
continue; continue;
files->close_on_exec->fds_bits[j] = 0; fdt->close_on_exec->fds_bits[j] = 0;
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
for ( ; set ; i++,set >>= 1) { for ( ; set ; i++,set >>= 1) {
if (set & 1) { if (set & 1) {
......
...@@ -24,20 +24,24 @@ ...@@ -24,20 +24,24 @@
void fastcall set_close_on_exec(unsigned int fd, int flag) void fastcall set_close_on_exec(unsigned int fd, int flag)
{ {
struct files_struct *files = current->files; struct files_struct *files = current->files;
struct fdtable *fdt;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
fdt = files_fdtable(files);
if (flag) if (flag)
FD_SET(fd, files->close_on_exec); FD_SET(fd, fdt->close_on_exec);
else else
FD_CLR(fd, files->close_on_exec); FD_CLR(fd, fdt->close_on_exec);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
} }
static inline int get_close_on_exec(unsigned int fd) static inline int get_close_on_exec(unsigned int fd)
{ {
struct files_struct *files = current->files; struct files_struct *files = current->files;
struct fdtable *fdt;
int res; int res;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
res = FD_ISSET(fd, files->close_on_exec); fdt = files_fdtable(files);
res = FD_ISSET(fd, fdt->close_on_exec);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
return res; return res;
} }
...@@ -54,24 +58,26 @@ static int locate_fd(struct files_struct *files, ...@@ -54,24 +58,26 @@ static int locate_fd(struct files_struct *files,
unsigned int newfd; unsigned int newfd;
unsigned int start; unsigned int start;
int error; int error;
struct fdtable *fdt;
error = -EINVAL; error = -EINVAL;
if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur) if (orig_start >= current->signal->rlim[RLIMIT_NOFILE].rlim_cur)
goto out; goto out;
fdt = files_fdtable(files);
repeat: repeat:
/* /*
* Someone might have closed fd's in the range * Someone might have closed fd's in the range
* orig_start..files->next_fd * orig_start..fdt->next_fd
*/ */
start = orig_start; start = orig_start;
if (start < files->next_fd) if (start < fdt->next_fd)
start = files->next_fd; start = fdt->next_fd;
newfd = start; newfd = start;
if (start < files->max_fdset) { if (start < fdt->max_fdset) {
newfd = find_next_zero_bit(files->open_fds->fds_bits, newfd = find_next_zero_bit(fdt->open_fds->fds_bits,
files->max_fdset, start); fdt->max_fdset, start);
} }
error = -EMFILE; error = -EMFILE;
...@@ -89,8 +95,8 @@ static int locate_fd(struct files_struct *files, ...@@ -89,8 +95,8 @@ static int locate_fd(struct files_struct *files,
if (error) if (error)
goto repeat; goto repeat;
if (start <= files->next_fd) if (start <= fdt->next_fd)
files->next_fd = newfd + 1; fdt->next_fd = newfd + 1;
error = newfd; error = newfd;
...@@ -101,13 +107,16 @@ static int locate_fd(struct files_struct *files, ...@@ -101,13 +107,16 @@ static int locate_fd(struct files_struct *files,
static int dupfd(struct file *file, unsigned int start) static int dupfd(struct file *file, unsigned int start)
{ {
struct files_struct * files = current->files; struct files_struct * files = current->files;
struct fdtable *fdt;
int fd; int fd;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
fd = locate_fd(files, file, start); fd = locate_fd(files, file, start);
if (fd >= 0) { if (fd >= 0) {
FD_SET(fd, files->open_fds); /* locate_fd() may have expanded fdtable, load the ptr */
FD_CLR(fd, files->close_on_exec); fdt = files_fdtable(files);
FD_SET(fd, fdt->open_fds);
FD_CLR(fd, fdt->close_on_exec);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
fd_install(fd, file); fd_install(fd, file);
} else { } else {
...@@ -123,6 +132,7 @@ asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd) ...@@ -123,6 +132,7 @@ asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
int err = -EBADF; int err = -EBADF;
struct file * file, *tofree; struct file * file, *tofree;
struct files_struct * files = current->files; struct files_struct * files = current->files;
struct fdtable *fdt;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
if (!(file = fcheck(oldfd))) if (!(file = fcheck(oldfd)))
...@@ -148,13 +158,14 @@ asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd) ...@@ -148,13 +158,14 @@ asmlinkage long sys_dup2(unsigned int oldfd, unsigned int newfd)
/* Yes. It's a race. In user space. Nothing sane to do */ /* Yes. It's a race. In user space. Nothing sane to do */
err = -EBUSY; err = -EBUSY;
tofree = files->fd[newfd]; fdt = files_fdtable(files);
if (!tofree && FD_ISSET(newfd, files->open_fds)) tofree = fdt->fd[newfd];
if (!tofree && FD_ISSET(newfd, fdt->open_fds))
goto out_fput; goto out_fput;
files->fd[newfd] = file; fdt->fd[newfd] = file;
FD_SET(newfd, files->open_fds); FD_SET(newfd, fdt->open_fds);
FD_CLR(newfd, files->close_on_exec); FD_CLR(newfd, fdt->close_on_exec);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
if (tofree) if (tofree)
......
...@@ -59,13 +59,15 @@ static int expand_fd_array(struct files_struct *files, int nr) ...@@ -59,13 +59,15 @@ static int expand_fd_array(struct files_struct *files, int nr)
{ {
struct file **new_fds; struct file **new_fds;
int error, nfds; int error, nfds;
struct fdtable *fdt;
error = -EMFILE; error = -EMFILE;
if (files->max_fds >= NR_OPEN || nr >= NR_OPEN) fdt = files_fdtable(files);
if (fdt->max_fds >= NR_OPEN || nr >= NR_OPEN)
goto out; goto out;
nfds = files->max_fds; nfds = fdt->max_fds;
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
/* /*
...@@ -95,13 +97,14 @@ static int expand_fd_array(struct files_struct *files, int nr) ...@@ -95,13 +97,14 @@ static int expand_fd_array(struct files_struct *files, int nr)
goto out; goto out;
/* Copy the existing array and install the new pointer */ /* Copy the existing array and install the new pointer */
fdt = files_fdtable(files);
if (nfds > files->max_fds) { if (nfds > fdt->max_fds) {
struct file **old_fds; struct file **old_fds;
int i; int i;
old_fds = xchg(&files->fd, new_fds); old_fds = xchg(&fdt->fd, new_fds);
i = xchg(&files->max_fds, nfds); i = xchg(&fdt->max_fds, nfds);
/* Don't copy/clear the array if we are creating a new /* Don't copy/clear the array if we are creating a new
fd array for fork() */ fd array for fork() */
...@@ -164,12 +167,14 @@ static int expand_fdset(struct files_struct *files, int nr) ...@@ -164,12 +167,14 @@ static int expand_fdset(struct files_struct *files, int nr)
{ {
fd_set *new_openset = NULL, *new_execset = NULL; fd_set *new_openset = NULL, *new_execset = NULL;
int error, nfds = 0; int error, nfds = 0;
struct fdtable *fdt;
error = -EMFILE; error = -EMFILE;
if (files->max_fdset >= NR_OPEN || nr >= NR_OPEN) fdt = files_fdtable(files);
if (fdt->max_fdset >= NR_OPEN || nr >= NR_OPEN)
goto out; goto out;
nfds = files->max_fdset; nfds = fdt->max_fdset;
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
/* Expand to the max in easy steps */ /* Expand to the max in easy steps */
...@@ -193,24 +198,25 @@ static int expand_fdset(struct files_struct *files, int nr) ...@@ -193,24 +198,25 @@ static int expand_fdset(struct files_struct *files, int nr)
error = 0; error = 0;
/* Copy the existing tables and install the new pointers */ /* Copy the existing tables and install the new pointers */
if (nfds > files->max_fdset) { fdt = files_fdtable(files);
int i = files->max_fdset / (sizeof(unsigned long) * 8); if (nfds > fdt->max_fdset) {
int count = (nfds - files->max_fdset) / 8; int i = fdt->max_fdset / (sizeof(unsigned long) * 8);
int count = (nfds - fdt->max_fdset) / 8;
/* /*
* Don't copy the entire array if the current fdset is * Don't copy the entire array if the current fdset is
* not yet initialised. * not yet initialised.
*/ */
if (i) { if (i) {
memcpy (new_openset, files->open_fds, files->max_fdset/8); memcpy (new_openset, fdt->open_fds, fdt->max_fdset/8);
memcpy (new_execset, files->close_on_exec, files->max_fdset/8); memcpy (new_execset, fdt->close_on_exec, fdt->max_fdset/8);
memset (&new_openset->fds_bits[i], 0, count); memset (&new_openset->fds_bits[i], 0, count);
memset (&new_execset->fds_bits[i], 0, count); memset (&new_execset->fds_bits[i], 0, count);
} }
nfds = xchg(&files->max_fdset, nfds); nfds = xchg(&fdt->max_fdset, nfds);
new_openset = xchg(&files->open_fds, new_openset); new_openset = xchg(&fdt->open_fds, new_openset);
new_execset = xchg(&files->close_on_exec, new_execset); new_execset = xchg(&fdt->close_on_exec, new_execset);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
free_fdset (new_openset, nfds); free_fdset (new_openset, nfds);
free_fdset (new_execset, nfds); free_fdset (new_execset, nfds);
...@@ -237,13 +243,15 @@ static int expand_fdset(struct files_struct *files, int nr) ...@@ -237,13 +243,15 @@ static int expand_fdset(struct files_struct *files, int nr)
int expand_files(struct files_struct *files, int nr) int expand_files(struct files_struct *files, int nr)
{ {
int err, expand = 0; int err, expand = 0;
struct fdtable *fdt;
if (nr >= files->max_fdset) { fdt = files_fdtable(files);
if (nr >= fdt->max_fdset) {
expand = 1; expand = 1;
if ((err = expand_fdset(files, nr))) if ((err = expand_fdset(files, nr)))
goto out; goto out;
} }
if (nr >= files->max_fds) { if (nr >= fdt->max_fds) {
expand = 1; expand = 1;
if ((err = expand_fd_array(files, nr))) if ((err = expand_fd_array(files, nr)))
goto out; goto out;
......
...@@ -2198,21 +2198,23 @@ void steal_locks(fl_owner_t from) ...@@ -2198,21 +2198,23 @@ void steal_locks(fl_owner_t from)
{ {
struct files_struct *files = current->files; struct files_struct *files = current->files;
int i, j; int i, j;
struct fdtable *fdt;
if (from == files) if (from == files)
return; return;
lock_kernel(); lock_kernel();
j = 0; j = 0;
fdt = files_fdtable(files);
for (;;) { for (;;) {
unsigned long set; unsigned long set;
i = j * __NFDBITS; i = j * __NFDBITS;
if (i >= files->max_fdset || i >= files->max_fds) if (i >= fdt->max_fdset || i >= fdt->max_fds)
break; break;
set = files->open_fds->fds_bits[j++]; set = fdt->open_fds->fds_bits[j++];
while (set) { while (set) {
if (set & 1) { if (set & 1) {
struct file *file = files->fd[i]; struct file *file = fdt->fd[i];
if (file) if (file)
__steal_locks(file, from); __steal_locks(file, from);
} }
......
...@@ -842,14 +842,16 @@ int get_unused_fd(void) ...@@ -842,14 +842,16 @@ int get_unused_fd(void)
{ {
struct files_struct * files = current->files; struct files_struct * files = current->files;
int fd, error; int fd, error;
struct fdtable *fdt;
error = -EMFILE; error = -EMFILE;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
repeat: repeat:
fd = find_next_zero_bit(files->open_fds->fds_bits, fdt = files_fdtable(files);
files->max_fdset, fd = find_next_zero_bit(fdt->open_fds->fds_bits,
files->next_fd); fdt->max_fdset,
fdt->next_fd);
/* /*
* N.B. For clone tasks sharing a files structure, this test * N.B. For clone tasks sharing a files structure, this test
...@@ -872,14 +874,14 @@ int get_unused_fd(void) ...@@ -872,14 +874,14 @@ int get_unused_fd(void)
goto repeat; goto repeat;
} }
FD_SET(fd, files->open_fds); FD_SET(fd, fdt->open_fds);
FD_CLR(fd, files->close_on_exec); FD_CLR(fd, fdt->close_on_exec);
files->next_fd = fd + 1; fdt->next_fd = fd + 1;
#if 1 #if 1
/* Sanity check */ /* Sanity check */
if (files->fd[fd] != NULL) { if (fdt->fd[fd] != NULL) {
printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd); printk(KERN_WARNING "get_unused_fd: slot %d not NULL!\n", fd);
files->fd[fd] = NULL; fdt->fd[fd] = NULL;
} }
#endif #endif
error = fd; error = fd;
...@@ -893,9 +895,10 @@ EXPORT_SYMBOL(get_unused_fd); ...@@ -893,9 +895,10 @@ EXPORT_SYMBOL(get_unused_fd);
static inline void __put_unused_fd(struct files_struct *files, unsigned int fd) static inline void __put_unused_fd(struct files_struct *files, unsigned int fd)
{ {
__FD_CLR(fd, files->open_fds); struct fdtable *fdt = files_fdtable(files);
if (fd < files->next_fd) __FD_CLR(fd, fdt->open_fds);
files->next_fd = fd; if (fd < fdt->next_fd)
fdt->next_fd = fd;
} }
void fastcall put_unused_fd(unsigned int fd) void fastcall put_unused_fd(unsigned int fd)
...@@ -924,10 +927,12 @@ EXPORT_SYMBOL(put_unused_fd); ...@@ -924,10 +927,12 @@ EXPORT_SYMBOL(put_unused_fd);
void fastcall fd_install(unsigned int fd, struct file * file) void fastcall fd_install(unsigned int fd, struct file * file)
{ {
struct files_struct *files = current->files; struct files_struct *files = current->files;
struct fdtable *fdt;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
if (unlikely(files->fd[fd] != NULL)) fdt = files_fdtable(files);
if (unlikely(fdt->fd[fd] != NULL))
BUG(); BUG();
files->fd[fd] = file; fdt->fd[fd] = file;
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
} }
...@@ -1010,15 +1015,17 @@ asmlinkage long sys_close(unsigned int fd) ...@@ -1010,15 +1015,17 @@ asmlinkage long sys_close(unsigned int fd)
{ {
struct file * filp; struct file * filp;
struct files_struct *files = current->files; struct files_struct *files = current->files;
struct fdtable *fdt;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
if (fd >= files->max_fds) fdt = files_fdtable(files);
if (fd >= fdt->max_fds)
goto out_unlock; goto out_unlock;
filp = files->fd[fd]; filp = fdt->fd[fd];
if (!filp) if (!filp)
goto out_unlock; goto out_unlock;
files->fd[fd] = NULL; fdt->fd[fd] = NULL;
FD_CLR(fd, files->close_on_exec); FD_CLR(fd, fdt->close_on_exec);
__put_unused_fd(files, fd); __put_unused_fd(files, fd);
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
return filp_close(filp, files); return filp_close(filp, files);
......
...@@ -159,6 +159,7 @@ static inline char * task_state(struct task_struct *p, char *buffer) ...@@ -159,6 +159,7 @@ static inline char * task_state(struct task_struct *p, char *buffer)
{ {
struct group_info *group_info; struct group_info *group_info;
int g; int g;
struct fdtable *fdt = NULL;
read_lock(&tasklist_lock); read_lock(&tasklist_lock);
buffer += sprintf(buffer, buffer += sprintf(buffer,
...@@ -179,10 +180,12 @@ static inline char * task_state(struct task_struct *p, char *buffer) ...@@ -179,10 +180,12 @@ static inline char * task_state(struct task_struct *p, char *buffer)
p->gid, p->egid, p->sgid, p->fsgid); p->gid, p->egid, p->sgid, p->fsgid);
read_unlock(&tasklist_lock); read_unlock(&tasklist_lock);
task_lock(p); task_lock(p);
if (p->files)
fdt = files_fdtable(p->files);
buffer += sprintf(buffer, buffer += sprintf(buffer,
"FDSize:\t%d\n" "FDSize:\t%d\n"
"Groups:\t", "Groups:\t",
p->files ? p->files->max_fds : 0); fdt ? fdt->max_fds : 0);
group_info = p->group_info; group_info = p->group_info;
get_group_info(group_info); get_group_info(group_info);
......
...@@ -1039,6 +1039,7 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) ...@@ -1039,6 +1039,7 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
int retval; int retval;
char buf[NUMBUF]; char buf[NUMBUF];
struct files_struct * files; struct files_struct * files;
struct fdtable *fdt;
retval = -ENOENT; retval = -ENOENT;
if (!pid_alive(p)) if (!pid_alive(p))
...@@ -1062,8 +1063,9 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir) ...@@ -1062,8 +1063,9 @@ static int proc_readfd(struct file * filp, void * dirent, filldir_t filldir)
if (!files) if (!files)
goto out; goto out;
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
fdt = files_fdtable(files);
for (fd = filp->f_pos-2; for (fd = filp->f_pos-2;
fd < files->max_fds; fd < fdt->max_fds;
fd++, filp->f_pos++) { fd++, filp->f_pos++) {
unsigned int i,j; unsigned int i,j;
......
...@@ -132,11 +132,13 @@ static int max_select_fd(unsigned long n, fd_set_bits *fds) ...@@ -132,11 +132,13 @@ static int max_select_fd(unsigned long n, fd_set_bits *fds)
unsigned long *open_fds; unsigned long *open_fds;
unsigned long set; unsigned long set;
int max; int max;
struct fdtable *fdt;
/* handle last in-complete long-word first */ /* handle last in-complete long-word first */
set = ~(~0UL << (n & (__NFDBITS-1))); set = ~(~0UL << (n & (__NFDBITS-1)));
n /= __NFDBITS; n /= __NFDBITS;
open_fds = current->files->open_fds->fds_bits+n; fdt = files_fdtable(current->files);
open_fds = fdt->open_fds->fds_bits+n;
max = 0; max = 0;
if (set) { if (set) {
set &= BITS(fds, n); set &= BITS(fds, n);
...@@ -299,6 +301,7 @@ sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, s ...@@ -299,6 +301,7 @@ sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, s
char *bits; char *bits;
long timeout; long timeout;
int ret, size, max_fdset; int ret, size, max_fdset;
struct fdtable *fdt;
timeout = MAX_SCHEDULE_TIMEOUT; timeout = MAX_SCHEDULE_TIMEOUT;
if (tvp) { if (tvp) {
...@@ -326,7 +329,8 @@ sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, s ...@@ -326,7 +329,8 @@ sys_select(int n, fd_set __user *inp, fd_set __user *outp, fd_set __user *exp, s
goto out_nofds; goto out_nofds;
/* max_fdset can increase, so grab it once to avoid race */ /* max_fdset can increase, so grab it once to avoid race */
max_fdset = current->files->max_fdset; fdt = files_fdtable(current->files);
max_fdset = fdt->max_fdset;
if (n > max_fdset) if (n > max_fdset)
n = max_fdset; n = max_fdset;
...@@ -464,9 +468,11 @@ asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long ti ...@@ -464,9 +468,11 @@ asmlinkage long sys_poll(struct pollfd __user * ufds, unsigned int nfds, long ti
unsigned int i; unsigned int i;
struct poll_list *head; struct poll_list *head;
struct poll_list *walk; struct poll_list *walk;
struct fdtable *fdt;
/* Do a sanity check on nfds ... */ /* Do a sanity check on nfds ... */
if (nfds > current->files->max_fdset && nfds > OPEN_MAX) fdt = files_fdtable(current->files);
if (nfds > fdt->max_fdset && nfds > OPEN_MAX)
return -EINVAL; return -EINVAL;
if (timeout) { if (timeout) {
......
...@@ -16,23 +16,29 @@ ...@@ -16,23 +16,29 @@
*/ */
#define NR_OPEN_DEFAULT BITS_PER_LONG #define NR_OPEN_DEFAULT BITS_PER_LONG
struct fdtable {
unsigned int max_fds;
int max_fdset;
int next_fd;
struct file ** fd; /* current fd array */
fd_set *close_on_exec;
fd_set *open_fds;
};
/* /*
* Open file table structure * Open file table structure
*/ */
struct files_struct { struct files_struct {
atomic_t count; atomic_t count;
spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */ spinlock_t file_lock; /* Protects all the below members. Nests inside tsk->alloc_lock */
int max_fds; struct fdtable fdtab;
int max_fdset;
int next_fd;
struct file ** fd; /* current fd array */
fd_set *close_on_exec;
fd_set *open_fds;
fd_set close_on_exec_init; fd_set close_on_exec_init;
fd_set open_fds_init; fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT]; struct file * fd_array[NR_OPEN_DEFAULT];
}; };
#define files_fdtable(files) (&(files)->fdtab)
extern void FASTCALL(__fput(struct file *)); extern void FASTCALL(__fput(struct file *));
extern void FASTCALL(fput(struct file *)); extern void FASTCALL(fput(struct file *));
...@@ -63,9 +69,10 @@ extern int expand_files(struct files_struct *, int nr); ...@@ -63,9 +69,10 @@ extern int expand_files(struct files_struct *, int nr);
static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd) static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
{ {
struct file * file = NULL; struct file * file = NULL;
struct fdtable *fdt = files_fdtable(files);
if (fd < files->max_fds) if (fd < fdt->max_fds)
file = files->fd[fd]; file = fdt->fd[fd];
return file; return file;
} }
......
...@@ -3,16 +3,21 @@ ...@@ -3,16 +3,21 @@
#include <linux/file.h> #include <linux/file.h>
#define INIT_FILES \ #define INIT_FDTABLE \
{ \ { \
.count = ATOMIC_INIT(1), \
.file_lock = SPIN_LOCK_UNLOCKED, \
.max_fds = NR_OPEN_DEFAULT, \ .max_fds = NR_OPEN_DEFAULT, \
.max_fdset = __FD_SETSIZE, \ .max_fdset = __FD_SETSIZE, \
.next_fd = 0, \ .next_fd = 0, \
.fd = &init_files.fd_array[0], \ .fd = &init_files.fd_array[0], \
.close_on_exec = &init_files.close_on_exec_init, \ .close_on_exec = &init_files.close_on_exec_init, \
.open_fds = &init_files.open_fds_init, \ .open_fds = &init_files.open_fds_init, \
}
#define INIT_FILES \
{ \
.count = ATOMIC_INIT(1), \
.file_lock = SPIN_LOCK_UNLOCKED, \
.fdtab = INIT_FDTABLE, \
.close_on_exec_init = { { 0, } }, \ .close_on_exec_init = { { 0, } }, \
.open_fds_init = { { 0, } }, \ .open_fds_init = { { 0, } }, \
.fd_array = { NULL, } \ .fd_array = { NULL, } \
......
...@@ -368,17 +368,19 @@ EXPORT_SYMBOL(daemonize); ...@@ -368,17 +368,19 @@ EXPORT_SYMBOL(daemonize);
static inline void close_files(struct files_struct * files) static inline void close_files(struct files_struct * files)
{ {
int i, j; int i, j;
struct fdtable *fdt;
j = 0; j = 0;
fdt = files_fdtable(files);
for (;;) { for (;;) {
unsigned long set; unsigned long set;
i = j * __NFDBITS; i = j * __NFDBITS;
if (i >= files->max_fdset || i >= files->max_fds) if (i >= fdt->max_fdset || i >= fdt->max_fds)
break; break;
set = files->open_fds->fds_bits[j++]; set = fdt->open_fds->fds_bits[j++];
while (set) { while (set) {
if (set & 1) { if (set & 1) {
struct file * file = xchg(&files->fd[i], NULL); struct file * file = xchg(&fdt->fd[i], NULL);
if (file) if (file)
filp_close(file, files); filp_close(file, files);
} }
...@@ -403,16 +405,19 @@ struct files_struct *get_files_struct(struct task_struct *task) ...@@ -403,16 +405,19 @@ struct files_struct *get_files_struct(struct task_struct *task)
void fastcall put_files_struct(struct files_struct *files) void fastcall put_files_struct(struct files_struct *files)
{ {
struct fdtable *fdt;
if (atomic_dec_and_test(&files->count)) { if (atomic_dec_and_test(&files->count)) {
close_files(files); close_files(files);
/* /*
* Free the fd and fdset arrays if we expanded them. * Free the fd and fdset arrays if we expanded them.
*/ */
if (files->fd != &files->fd_array[0]) fdt = files_fdtable(files);
free_fd_array(files->fd, files->max_fds); if (fdt->fd != &files->fd_array[0])
if (files->max_fdset > __FD_SETSIZE) { free_fd_array(fdt->fd, fdt->max_fds);
free_fdset(files->open_fds, files->max_fdset); if (fdt->max_fdset > __FD_SETSIZE) {
free_fdset(files->close_on_exec, files->max_fdset); free_fdset(fdt->open_fds, fdt->max_fdset);
free_fdset(fdt->close_on_exec, fdt->max_fdset);
} }
kmem_cache_free(files_cachep, files); kmem_cache_free(files_cachep, files);
} }
......
...@@ -568,21 +568,47 @@ static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk) ...@@ -568,21 +568,47 @@ static inline int copy_fs(unsigned long clone_flags, struct task_struct * tsk)
static int count_open_files(struct files_struct *files, int size) static int count_open_files(struct files_struct *files, int size)
{ {
int i; int i;
struct fdtable *fdt;
/* Find the last open fd */ /* Find the last open fd */
fdt = files_fdtable(files);
for (i = size/(8*sizeof(long)); i > 0; ) { for (i = size/(8*sizeof(long)); i > 0; ) {
if (files->open_fds->fds_bits[--i]) if (fdt->open_fds->fds_bits[--i])
break; break;
} }
i = (i+1) * 8 * sizeof(long); i = (i+1) * 8 * sizeof(long);
return i; return i;
} }
static struct files_struct *alloc_files(void)
{
struct files_struct *newf;
struct fdtable *fdt;
newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL);
if (!newf)
goto out;
atomic_set(&newf->count, 1);
spin_lock_init(&newf->file_lock);
fdt = files_fdtable(newf);
fdt->next_fd = 0;
fdt->max_fds = NR_OPEN_DEFAULT;
fdt->max_fdset = __FD_SETSIZE;
fdt->close_on_exec = &newf->close_on_exec_init;
fdt->open_fds = &newf->open_fds_init;
fdt->fd = &newf->fd_array[0];
out:
return newf;
}
static int copy_files(unsigned long clone_flags, struct task_struct * tsk) static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
{ {
struct files_struct *oldf, *newf; struct files_struct *oldf, *newf;
struct file **old_fds, **new_fds; struct file **old_fds, **new_fds;
int open_files, size, i, error = 0, expand; int open_files, size, i, error = 0, expand;
struct fdtable *old_fdt, *new_fdt;
/* /*
* A background process may not have any files ... * A background process may not have any files ...
...@@ -603,35 +629,27 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk) ...@@ -603,35 +629,27 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
*/ */
tsk->files = NULL; tsk->files = NULL;
error = -ENOMEM; error = -ENOMEM;
newf = kmem_cache_alloc(files_cachep, SLAB_KERNEL); newf = alloc_files();
if (!newf) if (!newf)
goto out; goto out;
atomic_set(&newf->count, 1);
spin_lock_init(&newf->file_lock);
newf->next_fd = 0;
newf->max_fds = NR_OPEN_DEFAULT;
newf->max_fdset = __FD_SETSIZE;
newf->close_on_exec = &newf->close_on_exec_init;
newf->open_fds = &newf->open_fds_init;
newf->fd = &newf->fd_array[0];
spin_lock(&oldf->file_lock); spin_lock(&oldf->file_lock);
old_fdt = files_fdtable(oldf);
open_files = count_open_files(oldf, oldf->max_fdset); new_fdt = files_fdtable(newf);
size = old_fdt->max_fdset;
open_files = count_open_files(oldf, old_fdt->max_fdset);
expand = 0; expand = 0;
/* /*
* Check whether we need to allocate a larger fd array or fd set. * Check whether we need to allocate a larger fd array or fd set.
* Note: we're not a clone task, so the open count won't change. * Note: we're not a clone task, so the open count won't change.
*/ */
if (open_files > newf->max_fdset) { if (open_files > new_fdt->max_fdset) {
newf->max_fdset = 0; new_fdt->max_fdset = 0;
expand = 1; expand = 1;
} }
if (open_files > newf->max_fds) { if (open_files > new_fdt->max_fds) {
newf->max_fds = 0; new_fdt->max_fds = 0;
expand = 1; expand = 1;
} }
...@@ -646,11 +664,11 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk) ...@@ -646,11 +664,11 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
spin_lock(&oldf->file_lock); spin_lock(&oldf->file_lock);
} }
old_fds = oldf->fd; old_fds = old_fdt->fd;
new_fds = newf->fd; new_fds = new_fdt->fd;
memcpy(newf->open_fds->fds_bits, oldf->open_fds->fds_bits, open_files/8); memcpy(new_fdt->open_fds->fds_bits, old_fdt->open_fds->fds_bits, open_files/8);
memcpy(newf->close_on_exec->fds_bits, oldf->close_on_exec->fds_bits, open_files/8); memcpy(new_fdt->close_on_exec->fds_bits, old_fdt->close_on_exec->fds_bits, open_files/8);
for (i = open_files; i != 0; i--) { for (i = open_files; i != 0; i--) {
struct file *f = *old_fds++; struct file *f = *old_fds++;
...@@ -663,24 +681,24 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk) ...@@ -663,24 +681,24 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
* is partway through open(). So make sure that this * is partway through open(). So make sure that this
* fd is available to the new process. * fd is available to the new process.
*/ */
FD_CLR(open_files - i, newf->open_fds); FD_CLR(open_files - i, new_fdt->open_fds);
} }
*new_fds++ = f; *new_fds++ = f;
} }
spin_unlock(&oldf->file_lock); spin_unlock(&oldf->file_lock);
/* compute the remainder to be cleared */ /* compute the remainder to be cleared */
size = (newf->max_fds - open_files) * sizeof(struct file *); size = (new_fdt->max_fds - open_files) * sizeof(struct file *);
/* This is long word aligned thus could use a optimized version */ /* This is long word aligned thus could use a optimized version */
memset(new_fds, 0, size); memset(new_fds, 0, size);
if (newf->max_fdset > open_files) { if (new_fdt->max_fdset > open_files) {
int left = (newf->max_fdset-open_files)/8; int left = (new_fdt->max_fdset-open_files)/8;
int start = open_files / (8 * sizeof(unsigned long)); int start = open_files / (8 * sizeof(unsigned long));
memset(&newf->open_fds->fds_bits[start], 0, left); memset(&new_fdt->open_fds->fds_bits[start], 0, left);
memset(&newf->close_on_exec->fds_bits[start], 0, left); memset(&new_fdt->close_on_exec->fds_bits[start], 0, left);
} }
tsk->files = newf; tsk->files = newf;
...@@ -689,9 +707,9 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk) ...@@ -689,9 +707,9 @@ static int copy_files(unsigned long clone_flags, struct task_struct * tsk)
return error; return error;
out_release: out_release:
free_fdset (newf->close_on_exec, newf->max_fdset); free_fdset (new_fdt->close_on_exec, new_fdt->max_fdset);
free_fdset (newf->open_fds, newf->max_fdset); free_fdset (new_fdt->open_fds, new_fdt->max_fdset);
free_fd_array(newf->fd, newf->max_fds); free_fd_array(new_fdt->fd, new_fdt->max_fds);
kmem_cache_free(files_cachep, newf); kmem_cache_free(files_cachep, newf);
goto out; goto out;
} }
......
...@@ -1594,6 +1594,7 @@ static inline void flush_unauthorized_files(struct files_struct * files) ...@@ -1594,6 +1594,7 @@ static inline void flush_unauthorized_files(struct files_struct * files)
struct avc_audit_data ad; struct avc_audit_data ad;
struct file *file, *devnull = NULL; struct file *file, *devnull = NULL;
struct tty_struct *tty = current->signal->tty; struct tty_struct *tty = current->signal->tty;
struct fdtable *fdt;
long j = -1; long j = -1;
if (tty) { if (tty) {
...@@ -1627,9 +1628,10 @@ static inline void flush_unauthorized_files(struct files_struct * files) ...@@ -1627,9 +1628,10 @@ static inline void flush_unauthorized_files(struct files_struct * files)
j++; j++;
i = j * __NFDBITS; i = j * __NFDBITS;
if (i >= files->max_fds || i >= files->max_fdset) fdt = files_fdtable(files);
if (i >= fdt->max_fds || i >= fdt->max_fdset)
break; break;
set = files->open_fds->fds_bits[j]; set = fdt->open_fds->fds_bits[j];
if (!set) if (!set)
continue; continue;
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment