Commit 74d392aa authored by Vadim Lobanov's avatar Vadim Lobanov Committed by Linus Torvalds

[PATCH] Clean up expand_fdtable() and expand_files()

Perform a code cleanup against the expand_fdtable() and expand_files()
functions inside fs/file.c.  It aims to make the flow of code within these
functions simpler and easier to understand, via added comments and modest
refactoring.
Signed-off-by: default avatarVadim Lobanov <vlobanov@speakeasy.net>
Signed-off-by: default avatarAndrew Morton <akpm@osdl.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@osdl.org>
parent 3706baa8
...@@ -288,71 +288,65 @@ out: ...@@ -288,71 +288,65 @@ out:
} }
/* /*
* Expands the file descriptor table - it will allocate a new fdtable and * Expand the file descriptor table.
* both fd array and fdset. It is expected to be called with the * This function will allocate a new fdtable and both fd array and fdset, of
* files_lock held. * the given size.
* Return <0 error code on error; 1 on successful completion.
* The files->file_lock should be held on entry, and will be held on exit.
*/ */
static int expand_fdtable(struct files_struct *files, int nr) static int expand_fdtable(struct files_struct *files, int nr)
__releases(files->file_lock) __releases(files->file_lock)
__acquires(files->file_lock) __acquires(files->file_lock)
{ {
int error = 0; struct fdtable *new_fdt, *cur_fdt;
struct fdtable *fdt;
struct fdtable *nfdt = NULL;
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
nfdt = alloc_fdtable(nr); new_fdt = alloc_fdtable(nr);
if (!nfdt) {
error = -ENOMEM;
spin_lock(&files->file_lock);
goto out;
}
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
fdt = files_fdtable(files); if (!new_fdt)
return -ENOMEM;
/* /*
* Check again since another task may have expanded the * Check again since another task may have expanded the fd table while
* fd table while we dropped the lock * we dropped the lock
*/ */
if (nr >= fdt->max_fds || nr >= fdt->max_fdset) { cur_fdt = files_fdtable(files);
copy_fdtable(nfdt, fdt); if (nr >= cur_fdt->max_fds || nr >= cur_fdt->max_fdset) {
/* Continue as planned */
copy_fdtable(new_fdt, cur_fdt);
rcu_assign_pointer(files->fdt, new_fdt);
free_fdtable(cur_fdt);
} else { } else {
/* Somebody expanded while we dropped file_lock */ /* Somebody else expanded, so undo our attempt */
spin_unlock(&files->file_lock); spin_unlock(&files->file_lock);
__free_fdtable(nfdt); __free_fdtable(new_fdt);
spin_lock(&files->file_lock); spin_lock(&files->file_lock);
goto out;
} }
rcu_assign_pointer(files->fdt, nfdt); return 1;
free_fdtable(fdt);
out:
return error;
} }
/* /*
* Expand files. * Expand files.
* Return <0 on error; 0 nothing done; 1 files expanded, we may have blocked. * This function will expand the file structures, if the requested size exceeds
* Should be called with the files->file_lock spinlock held for write. * the current capacity and there is room for expansion.
* Return <0 error code on error; 0 when nothing done; 1 when files were
* expanded and execution may have blocked.
* The files->file_lock should be held on entry, and will be held on exit.
*/ */
int expand_files(struct files_struct *files, int nr) int expand_files(struct files_struct *files, int nr)
{ {
int err, expand = 0;
struct fdtable *fdt; struct fdtable *fdt;
fdt = files_fdtable(files); fdt = files_fdtable(files);
if (nr >= fdt->max_fdset || nr >= fdt->max_fds) { /* Do we need to expand? */
if (fdt->max_fdset >= NR_OPEN || if (nr < fdt->max_fdset && nr < fdt->max_fds)
fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) { return 0;
err = -EMFILE; /* Can we expand? */
goto out; if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
} nr >= NR_OPEN)
expand = 1; return -EMFILE;
if ((err = expand_fdtable(files, nr)))
goto out; /* All good, so we try */
} return expand_fdtable(files, nr);
err = expand;
out:
return err;
} }
static void __devinit fdtable_defer_list_init(int cpu) static void __devinit fdtable_defer_list_init(int cpu)
......
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