mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] Clean up expand_fdtable() and expand_files().
@ 2006-09-05  5:08 Vadim Lobanov
  2006-09-05 16:55 ` Randy.Dunlap
  0 siblings, 1 reply; 4+ messages in thread
From: Vadim Lobanov @ 2006-09-05  5:08 UTC (permalink / raw)
  To: akpm, linux-kernel

Hi,

This patch performs 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. The patch was generated against 2.6.18-rc5-mm1, and 
was successfully run live. Please apply.

(I'm trying out KMail for this patch. I tested this mailer beforehand to make 
sure the patch will come out unmangled, but, as with all things software, 
success is far from guaranteed. :) Please yell if the patch is borked.)

Signed-off-by: Vadim Lobanov <vlobanov@speakeasy.net>

diff -Npru linux-a/fs/file.c linux-b/fs/file.c
--- linux-a/fs/file.c	2006-09-01 20:34:12.000000000 -0700
+++ linux-b/fs/file.c	2006-09-04 16:42:33.000000000 -0700
@@ -296,37 +296,30 @@ static int expand_fdtable(struct files_s
 	__releases(files->file_lock)
 	__acquires(files->file_lock)
 {
-	int error = 0;
-	struct fdtable *fdt;
-	struct fdtable *nfdt = NULL;
+	struct fdtable *new_fdt, *cur_fdt;
 
 	spin_unlock(&files->file_lock);
-	nfdt = alloc_fdtable(nr);
-	if (!nfdt) {
-		error = -ENOMEM;
-		spin_lock(&files->file_lock);
-		goto out;
-	}
-
+	new_fdt = alloc_fdtable(nr);
 	spin_lock(&files->file_lock);
-	fdt = files_fdtable(files);
+	if (!new_fdt)
+		return -ENOMEM;
 	/*
-	 * Check again since another task may have expanded the
-	 * fd table while we dropped the lock
+	 * Check again since another task may have expanded the fd table while
+	 * we dropped the lock
 	 */
-	if (nr >= fdt->max_fds || nr >= fdt->max_fdset) {
-		copy_fdtable(nfdt, fdt);
+	cur_fdt = files_fdtable(files);
+	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 {
-		/* Somebody expanded while we dropped file_lock */
+		/* Somebody else expanded, so undo our attempt */
 		spin_unlock(&files->file_lock);
-		__free_fdtable(nfdt);
+		__free_fdtable(new_fdt);
 		spin_lock(&files->file_lock);
-		goto out;
 	}
-	rcu_assign_pointer(files->fdt, nfdt);
-	free_fdtable(fdt);
-out:
-	return error;
+	return 1;
 }
 
 /*
@@ -336,23 +329,19 @@ out:
  */
 int expand_files(struct files_struct *files, int nr)
 {
-	int err, expand = 0;
 	struct fdtable *fdt;
 
 	fdt = files_fdtable(files);
-	if (nr >= fdt->max_fdset || nr >= fdt->max_fds) {
-		if (fdt->max_fdset >= NR_OPEN ||
-			fdt->max_fds >= NR_OPEN || nr >= NR_OPEN) {
-			err = -EMFILE;
-			goto out;
-		}
-		expand = 1;
-		if ((err = expand_fdtable(files, nr)))
-			goto out;
-	}
-	err = expand;
-out:
-	return err;
+	/* Do we need to expand? */
+	if (nr < fdt->max_fdset && nr < fdt->max_fds)
+		return 0;
+	/* Can we expand? */
+	if (fdt->max_fdset >= NR_OPEN || fdt->max_fds >= NR_OPEN ||
+	    nr >= NR_OPEN)
+		return -EMFILE;
+
+	/* All good, so we try */
+	return expand_fdtable(files, nr);
 }
 
 static void __devinit fdtable_defer_list_init(int cpu)

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-09-05 18:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-05  5:08 [PATCH] Clean up expand_fdtable() and expand_files() Vadim Lobanov
2006-09-05 16:55 ` Randy.Dunlap
2006-09-05 18:56   ` Vadim Lobanov
2006-09-05 19:01     ` Randy.Dunlap

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®