Skip to contentSkip to conversation

e4defrag: Add force option for e4defrag

PATCH New Kazuya Mio 1 participant 1 message Nov 14, 2011

lore
Share & tools
lore

Kazuya Mio

author
#0 lore raw

PATCH v3 5/11 e4defrag: Add force option for e4defrag

Currently, e4defrag calls EXT4_IOC_MOVE_EXT ioctl if the fragmentation score of a donor file is zero. However, it is difficult sometimes to create the file that has the average of 4096 blocks per extent. We can use e4defrag -F to defrag in this case.

---
 misc/e4defrag.8.in |    5 ++++-
 misc/e4defrag.c    |   14 ++++++++++----
 2 files changed, 14 insertions(+), 5 deletions(-)
misc/e4defrag.8.in +4 -1
index 3997f75..e91ac85 100644
@@ -4,7 +4,7 @@ e4defrag \- online defragmenter for ext4 filesystem
 .SH SYNOPSIS
 .B e4defrag
 [
-.B \-v
+.B \-Fv
 ]
 .I target
 \&...
@@ -31,6 +31,9 @@ gets the mount point of it and reduces fragmentation of all files in this mount
 point.
 .SH OPTIONS
 .TP
+.B \-F
+Force defrag if the fragmentation gets better.
+.TP
 .B \-v
 Print error messages and the fragmentation count before and after defrag for
 each file.
misc/e4defrag.c +10 -4
index c1599fd..8b19776 100644
@@ -81,6 +81,7 @@
 
 /* The mode of defrag */
 #define DETAIL			0x01
+#define FORCE			0x02
 
 #define DEVNAME			0
 #define DIRNAME			1
@@ -108,7 +109,7 @@
 
 /* The following macros are error message */
 #define MSG_USAGE		\
-"Usage	: e4defrag [-v] file...| directory...| device...\n"
+"Usage	: e4defrag [-Fv] file...| directory...| device...\n"
 
 #define NGMSG_EXT4		"Filesystem is not ext4 filesystem"
 #define NGMSG_FILE_EXTENT	"Failed to get file extents"
@@ -1157,7 +1158,8 @@ check_improvement:
 		extents_before_defrag += file_frags_start;
 	}
 
-	if (orig_score == 0 || donor_score > 0) {
+	if (orig_score == 0 || (donor_score > 0 && !(mode_flag & FORCE)) ||
+			(orig_score <= donor_score && (mode_flag & FORCE))) {
 		printf("\033[79;0H\033[K[%u/%u]%s:\t%3d%%",
 			defraged_file_count, total_count, file, 100);
 		if (mode_flag & DETAIL)
@@ -1236,8 +1238,12 @@ int main(int argc, char *argv[])
 	if (argc == 1)
 		goto out;
 
-	while ((opt = getopt(argc, argv, "v")) != EOF) {
+	while ((opt = getopt(argc, argv, "Fv")) != EOF) {
 		switch (opt) {
+		case 'F':
+			/* Force defrag if the fragmentation gets better */
+			mode_flag |= FORCE;
+			break;
 		case 'v':
 			mode_flag |= DETAIL;
 			break;
@@ -1250,7 +1256,7 @@ int main(int argc, char *argv[])
 		goto out;
 
 	current_uid = getuid();
-	threshold = DEFAULT_THRESHOLD;
+	threshold = (mode_flag & FORCE) ? ~0U : DEFAULT_THRESHOLD;
 
 	/* Main process */
 	for (i = optind; i < argc; i++) {