linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/3] btrfs-progs: fi defrag: clean up duplicate code if find errors
@ 2017-11-28  9:14 Su Yue
  2017-11-28  9:14 ` [PATCH v2 2/3] btrfs-progs: fi defrag: do not exit if defrag range ioctl is unsupported Su Yue
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Su Yue @ 2017-11-28  9:14 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

In function cmd_filesystem_defrag(), lines of code for error handling
are duplicate and hard to expand in further.

Create a jump label for errors.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
Changelog:
v2:	Record -errno to return value if open() and fstat() failed.
	Move change "do no exit if defrag range ioctl is unsupported"
	to another patch.
	Suggested by David.
---
 cmds-filesystem.c | 44 ++++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 7728430f16a1..17d399d58adf 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -1029,23 +1029,22 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 		if (fd < 0) {
 			error("cannot open %s: %s", argv[i],
 					strerror(errno));
-			defrag_global_errors++;
-			close_file_or_dir(fd, dirstream);
-			continue;
+			ret = -errno;
+			goto next;
 		}
-		if (fstat(fd, &st)) {
+
+		ret = fstat(fd, &st);
+		if (ret) {
 			error("failed to stat %s: %s",
 					argv[i], strerror(errno));
-			defrag_global_errors++;
-			close_file_or_dir(fd, dirstream);
-			continue;
+			ret = -errno;
+			goto next;
 		}
 		if (!(S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) {
 			error("%s is not a directory or a regular file",
 					argv[i]);
-			defrag_global_errors++;
-			close_file_or_dir(fd, dirstream);
-			continue;
+			ret = -EINVAL;
+			goto next;
 		}
 		if (recursive && S_ISDIR(st.st_mode)) {
 			ret = nftw(argv[i], defrag_callback, 10,
@@ -1060,20 +1059,25 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 			ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE,
 					&defrag_global_range);
 			defrag_err = errno;
-		}
-		close_file_or_dir(fd, dirstream);
-		if (ret && defrag_err == ENOTTY) {
-			error(
+			if (ret && defrag_err == ENOTTY) {
+				error(
 "defrag range ioctl not supported in this kernel version, 2.6.33 and newer is required");
-			defrag_global_errors++;
-			break;
+				defrag_global_errors++;
+				close_file_or_dir(fd, dirstream);
+				break;
+			}
+			if (ret) {
+				error("defrag failed on %s: %s", argv[i],
+				      strerror(defrag_err));
+				goto next;
+			}
 		}
-		if (ret) {
-			error("defrag failed on %s: %s", argv[i],
-					strerror(defrag_err));
+next:
+		if (ret)
 			defrag_global_errors++;
-		}
+		close_file_or_dir(fd, dirstream);
 	}
+
 	if (defrag_global_errors)
 		fprintf(stderr, "total %d failures\n", defrag_global_errors);
 
-- 
2.15.0




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

end of thread, other threads:[~2017-11-30 18:32 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-28  9:14 [PATCH v2 1/3] btrfs-progs: fi defrag: clean up duplicate code if find errors Su Yue
2017-11-28  9:14 ` [PATCH v2 2/3] btrfs-progs: fi defrag: do not exit if defrag range ioctl is unsupported Su Yue
2017-11-28 15:57   ` David Sterba
2017-11-28  9:14 ` [PATCH v2 3/3] btrfs-progs: fi defrag: extend -c to drop nocompress flag on files Su Yue
2017-11-28 16:07   ` David Sterba
2017-11-29  1:54     ` Su Yue
2017-11-30 18:30       ` David Sterba
2017-11-28 16:07 ` [PATCH v2 1/3] btrfs-progs: fi defrag: clean up duplicate code if find errors David Sterba
2017-11-29  2:12 ` [PATCH v3 2/3] btrfs-progs: fi defrag: do not exit if defrag range ioctl is unsupported Su Yue
2017-11-30  6:37   ` Qu Wenruo
2017-11-30  6:49     ` Qu Wenruo
2017-11-30  6:47 ` [PATCH v2 1/3] btrfs-progs: fi defrag: clean up duplicate code if find errors Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).