linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] btrfs-progs: resolve property and enhance defragment
@ 2017-11-02  3:23 Su Yue
  2017-11-02  3:23 ` [PATCH 1/5] btrfs-progs: property: divide prop_handler_t into setter,getter,printer Su Yue
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:23 UTC (permalink / raw)
  To: linux-btrfs; +Cc: suy.fnst

The patchset adds an option '--compress-force' to work with
'btrfs fi defrag -c'. Then no-compression files will be set with
compression property specified by '-c' (zlib default).

patch[1-4] divide property handler to setter, getter, and printer.

Then patch[5] could enhance defragment easier.

Su Yue (5):
  btrfs-progs: property: divide prop_handler_t into
    setter,getter,printer
  btrfs-progs: property: set/get/print ro property
  btrfs-progs: property: set/get/print label property
  btrfs-progs: property: set/get/print compression property
  btrfs-progs: fi defrag: extend -c to drop nocompress flag on files

 cmds-filesystem.c |  94 +++++++++++++++++++++++--
 cmds-property.c   |   7 +-
 props.c           | 207 +++++++++++++++++++++++++++++++++++++++++++++---------
 props.h           |  19 +++--
 4 files changed, 283 insertions(+), 44 deletions(-)

-- 
2.15.0




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

* [PATCH 1/5] btrfs-progs: property: divide prop_handler_t into setter,getter,printer
  2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
@ 2017-11-02  3:23 ` Su Yue
  2017-11-02  3:23 ` [PATCH 2/5] btrfs-progs: property: set/get/print ro property Su Yue
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:23 UTC (permalink / raw)
  To: linux-btrfs; +Cc: suy.fnst

Previously, function prototype prop_handler_t() in props.h is
just for setting and getting @value. However, it prints @value
directly instead of returns the value. It was difficult to get
@value by @name.

For code-reuse, here divide prop_handler_t into three handlers:
1) prop_setter_t: set @value as origin.
2) prop_getter_t:
	if arg @value is NULL, return the length of result for
		caller to allocate.
	else copy result to memory space which arg @value points to.

3) prop_printer_t: just call correspond prop_getter_t and
prints result.

Notice:
Since C language has no feature like tpyeinfo in others.
After return of prop_getter_t(), the arg @value will point
to array of characters without null-terminated.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 cmds-property.c |  7 +++++--
 props.c         | 11 +++++++----
 props.h         | 19 +++++++++++++++----
 3 files changed, 27 insertions(+), 10 deletions(-)

diff --git a/cmds-property.c b/cmds-property.c
index 03bafa054701..6795d1795027 100644
--- a/cmds-property.c
+++ b/cmds-property.c
@@ -180,7 +180,7 @@ static int dump_prop(const struct prop_handler *prop,
 
 	if ((types & type) && (prop->types & type)) {
 		if (!name_and_help)
-			ret = prop->handler(type, object, prop->name, NULL);
+			ret = prop->printer(type, object, prop->name);
 		else
 			printf("%-20s%s\n", prop->name, prop->desc);
 	}
@@ -244,7 +244,10 @@ static int setget_prop(int types, const char *object,
 		goto out;
 	}
 
-	ret = prop->handler(types, object, name, value);
+	if (value)
+		ret = prop->setter(types, object, name, value);
+	else
+		ret = prop->printer(types, object, name);
 
 	if (ret < 0)
 		ret = 50;
diff --git a/props.c b/props.c
index cddbd9272fe4..d6249f53887d 100644
--- a/props.c
+++ b/props.c
@@ -188,10 +188,13 @@ out:
 
 const struct prop_handler prop_handlers[] = {
 	{"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
-	 prop_read_only},
+	 NULL, NULL, NULL},
+
 	{"label", "Set/get label of device.", 0,
-	 prop_object_dev | prop_object_root, prop_label},
+	 prop_object_dev | prop_object_root, NULL, NULL, NULL},
+
 	{"compression", "Set/get compression for a file or directory", 0,
-	 prop_object_inode, prop_compression},
-	{NULL, NULL, 0, 0, NULL}
+	 prop_object_inode, NULL, NULL, NULL},
+
+	{NULL, NULL, 0, 0, NULL, NULL, NULL}
 };
diff --git a/props.h b/props.h
index a43cb2537f37..fd5796bff09d 100644
--- a/props.h
+++ b/props.h
@@ -17,6 +17,8 @@
 #ifndef __BTRFS_PROPS_H__
 #define __BTRFS_PROPS_H__
 
+#include <unistd.h>
+
 enum prop_object_type {
 	prop_object_dev		= (1 << 0),
 	prop_object_root	= (1 << 1),
@@ -25,17 +27,26 @@ enum prop_object_type {
 	__prop_object_max,
 };
 
-typedef int (*prop_handler_t)(enum prop_object_type type,
+typedef int (*prop_setter_t)(enum prop_object_type type,
+			     const char *object,
+			     const char *name,
+			     const char *value);
+typedef ssize_t (*prop_getter_t)(enum prop_object_type type,
+				 const char *object,
+				 const char *name,
+				 char *value);
+typedef int (*prop_printer_t)(enum prop_object_type type,
 			      const char *object,
-			      const char *name,
-			      const char *value);
+			      const char *name);
 
 struct prop_handler {
 	const char *name;
 	const char *desc;
 	int read_only;
 	int types;
-	prop_handler_t handler;
+	prop_setter_t setter;
+	prop_getter_t getter;
+	prop_printer_t printer;
 };
 
 extern const struct prop_handler prop_handlers[];
-- 
2.15.0




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

* [PATCH 2/5] btrfs-progs: property: set/get/print ro property
  2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
  2017-11-02  3:23 ` [PATCH 1/5] btrfs-progs: property: divide prop_handler_t into setter,getter,printer Su Yue
@ 2017-11-02  3:23 ` Su Yue
  2017-11-02  3:23 ` [PATCH 3/5] btrfs-progs: property: set/get/print label property Su Yue
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:23 UTC (permalink / raw)
  To: linux-btrfs; +Cc: suy.fnst

Introduce set_prop_read_only(), get_prop_read_only(),
print_prop_read_only() to set, get and print the subvolume
is read-only or not.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 props.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 62 insertions(+), 10 deletions(-)

diff --git a/props.c b/props.c
index d6249f53887d..da69f6e9314c 100644
--- a/props.c
+++ b/props.c
@@ -36,12 +36,16 @@
 #define ENOATTR ENODATA
 #endif
 
-static int prop_read_only(enum prop_object_type type,
-			  const char *object,
-			  const char *name,
-			  const char *value)
+enum prop_operation {
+	set_prop,
+	get_prop,
+};
+
+static ssize_t prop_read_only(enum prop_object_type type, const char *object,
+			      const char *name, char *value,
+			      enum prop_operation operation)
 {
-	int ret = 0;
+	ssize_t ret = 0;
 	int fd = -1;
 	u64 flags = 0;
 
@@ -60,12 +64,19 @@ static int prop_read_only(enum prop_object_type type,
 		goto out;
 	}
 
-	if (!value) {
+	if (operation == get_prop) {
+		const char *is_ro;
 		if (flags & BTRFS_SUBVOL_RDONLY)
-			fprintf(stdout, "ro=true\n");
+			is_ro = "true";
 		else
-			fprintf(stdout, "ro=false\n");
-		ret = 0;
+			is_ro = "false";
+
+		if (value) {
+			memcpy(value, is_ro, strlen(is_ro));
+			ret = 0;
+		} else {
+			ret = strlen(is_ro);
+		}
 		goto out;
 	}
 
@@ -93,6 +104,47 @@ out:
 	return ret;
 }
 
+static int set_prop_read_only(enum prop_object_type type, const char *object,
+			      const char *name, const char *value)
+{
+	return prop_read_only(type, object, name, (char *)value, set_prop);
+}
+
+static ssize_t get_prop_read_only(enum prop_object_type type,
+			  const char *object, const char *name, char *value)
+{
+	return prop_read_only(type, object, name, value, get_prop);
+}
+
+static int print_prop_read_only(enum prop_object_type type, const char *object,
+				const char *name)
+{
+	ssize_t len;
+	char *value = NULL;
+	int ret;
+
+	len = get_prop_read_only(type, object, name, NULL);
+	if (len <= 0) {
+		ret = -1;
+		goto out;
+	}
+	value = malloc(len + 1);
+	if (!value) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = get_prop_read_only(type, object, name, value);
+	if (ret)
+		goto out;
+	value[len] = '\0';
+	printf("ro=%s\n", value);
+out:
+	if (value)
+		free(value);
+	return ret;
+}
+
 static int prop_label(enum prop_object_type type,
 		      const char *object,
 		      const char *name,
@@ -188,7 +240,7 @@ out:
 
 const struct prop_handler prop_handlers[] = {
 	{"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
-	 NULL, NULL, NULL},
+	 set_prop_read_only, get_prop_read_only, print_prop_read_only},
 
 	{"label", "Set/get label of device.", 0,
 	 prop_object_dev | prop_object_root, NULL, NULL, NULL},
-- 
2.15.0




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

* [PATCH 3/5] btrfs-progs: property: set/get/print label property
  2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
  2017-11-02  3:23 ` [PATCH 1/5] btrfs-progs: property: divide prop_handler_t into setter,getter,printer Su Yue
  2017-11-02  3:23 ` [PATCH 2/5] btrfs-progs: property: set/get/print ro property Su Yue
@ 2017-11-02  3:23 ` Su Yue
  2017-11-02  3:23 ` [PATCH 4/5] btrfs-progs: property: set/get/print compression property Su Yue
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:23 UTC (permalink / raw)
  To: linux-btrfs; +Cc: suy.fnst

Introduce set_prop_label(), get_prop_label(), print_prop_label()
to set/get/print label.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 props.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 55 insertions(+), 9 deletions(-)

diff --git a/props.c b/props.c
index da69f6e9314c..28a81b6bdc81 100644
--- a/props.c
+++ b/props.c
@@ -145,23 +145,68 @@ out:
 	return ret;
 }
 
-static int prop_label(enum prop_object_type type,
-		      const char *object,
-		      const char *name,
-		      const char *value)
+static ssize_t prop_label(enum prop_object_type type, const char *object,
+		  const char *name, char *value, enum prop_operation operation)
 {
-	int ret;
+	ssize_t ret;
 
-	if (value) {
+	if (operation == set_prop) {
 		ret = set_label((char *) object, (char *) value);
 	} else {
 		char label[BTRFS_LABEL_SIZE];
 
 		ret = get_label((char *) object, label);
-		if (!ret)
-			fprintf(stdout, "label=%s\n", label);
+		if (ret)
+			goto out;
+
+		if (!value)
+			ret = strlen(label);
+		else
+			memcpy(value, label, strlen(label));
+	}
+out:
+	return ret;
+}
+
+static int set_prop_label(enum prop_object_type type, const char *object,
+			  const char *name, const char *value)
+{
+	return prop_label(type, object, name, (char *)value, set_prop);
+}
+
+static ssize_t get_prop_label(enum prop_object_type type, const char *object,
+			      const char *name, char *value)
+{
+	return prop_label(type, object, name, value, get_prop);
+}
+
+static int print_prop_label(enum prop_object_type type, const char *object,
+			    const char *name)
+{
+	int ret;
+	ssize_t len;
+	char *value = NULL;
+
+	len = get_prop_label(type, object, name, NULL);
+	if (len < 0) {
+		ret = len;
+		goto out;
 	}
 
+	value = malloc(len + 1);
+	if (!value) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	ret = get_prop_label(type, object, name, value);
+	if (ret)
+		goto out;
+
+	value[len] = '\0';
+	printf("label=%s\n", value);
+out:
+	if (value)
+		free(value);
 	return ret;
 }
 
@@ -243,7 +288,8 @@ const struct prop_handler prop_handlers[] = {
 	 set_prop_read_only, get_prop_read_only, print_prop_read_only},
 
 	{"label", "Set/get label of device.", 0,
-	 prop_object_dev | prop_object_root, NULL, NULL, NULL},
+	 prop_object_dev | prop_object_root,
+	 set_prop_label, get_prop_label, print_prop_label},
 
 	{"compression", "Set/get compression for a file or directory", 0,
 	 prop_object_inode, NULL, NULL, NULL},
-- 
2.15.0




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

* [PATCH 4/5] btrfs-progs: property: set/get/print compression property
  2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
                   ` (2 preceding siblings ...)
  2017-11-02  3:23 ` [PATCH 3/5] btrfs-progs: property: set/get/print label property Su Yue
@ 2017-11-02  3:23 ` Su Yue
  2017-11-02  3:23 ` [PATCH 5/5] btrfs-progs: fi defrag: extend -c to drop nocompress flag on files Su Yue
  2017-11-02  3:44 ` [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
  5 siblings, 0 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:23 UTC (permalink / raw)
  To: linux-btrfs; +Cc: suy.fnst

Introduce set_prop_compression(), get_prop_compression()
and print_prop_compression() to set/get/print compression property.

Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 props.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 55 insertions(+), 13 deletions(-)

diff --git a/props.c b/props.c
index 28a81b6bdc81..edddc4b99ebb 100644
--- a/props.c
+++ b/props.c
@@ -210,12 +210,10 @@ out:
 	return ret;
 }
 
-static int prop_compression(enum prop_object_type type,
-			    const char *object,
-			    const char *name,
-			    const char *value)
+static ssize_t prop_compression(enum prop_object_type type, const char *object,
+		const char *name, char *value, enum prop_operation operation)
 {
-	int ret;
+	ssize_t ret;
 	ssize_t sret;
 	int fd = -1;
 	DIR *dirstream = NULL;
@@ -239,7 +237,7 @@ static int prop_compression(enum prop_object_type type,
 	memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
 	xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
 
-	if (value) {
+	if (operation == set_prop) {
 		if (strcmp(value, "no") == 0 || strcmp(value, "none") == 0)
 			value = "";
 		sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
@@ -255,14 +253,14 @@ static int prop_compression(enum prop_object_type type,
 			ret = 0;
 		goto out;
 	}
-	if (!value) {
+	if (operation == get_prop) {
 		size_t len = sret;
 
-		buf = malloc(len);
-		if (!buf) {
-			ret = -ENOMEM;
+		if (!value) {
+			ret = len;
 			goto out;
 		}
+		buf = value;
 		sret = fgetxattr(fd, xattr_name, buf, len);
 		if (sret < 0) {
 			ret = -errno;
@@ -270,19 +268,62 @@ static int prop_compression(enum prop_object_type type,
 			      object, strerror(-ret));
 			goto out;
 		}
-		fprintf(stdout, "compression=%.*s\n", (int)len, buf);
 	}
 
 	ret = 0;
 out:
 	free(xattr_name);
-	free(buf);
 	if (fd >= 0)
 		close_file_or_dir(fd, dirstream);
 
 	return ret;
 }
 
+static int set_prop_compression(enum prop_object_type type, const char *object,
+				const char *name, const char *value)
+{
+	return prop_compression(type, object, name, (char *)value, set_prop);
+}
+
+static ssize_t get_prop_compression(enum prop_object_type type,
+			    const char *object, const char *name, char *value)
+{
+	return prop_compression(type, object, name, value, get_prop);
+}
+
+static int print_prop_compression(enum prop_object_type type,
+				  const char *object, const char *name)
+{
+	int ret = 0;
+	ssize_t len;
+	char *value = NULL;
+
+	len = get_prop_compression(type, object, name, NULL);
+	if (len <= 0) {
+		if (len == -ENOATTR)
+			ret = 0;
+		else
+			ret = len;
+		goto out;
+	}
+	value = malloc(len + 1);
+	if (!value) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	ret = get_prop_compression(type, object, name, value);
+	if (ret)
+		goto out;
+
+	value[len] = '\0';
+	printf("compression=%s\n", value);
+out:
+	if (value)
+		free(value);
+	return ret;
+}
+
 const struct prop_handler prop_handlers[] = {
 	{"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
 	 set_prop_read_only, get_prop_read_only, print_prop_read_only},
@@ -292,7 +333,8 @@ const struct prop_handler prop_handlers[] = {
 	 set_prop_label, get_prop_label, print_prop_label},
 
 	{"compression", "Set/get compression for a file or directory", 0,
-	 prop_object_inode, NULL, NULL, NULL},
+	 prop_object_inode,
+	 set_prop_compression, get_prop_compression, print_prop_compression},
 
 	{NULL, NULL, 0, 0, NULL, NULL, NULL}
 };
-- 
2.15.0




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

* [PATCH 5/5] btrfs-progs: fi defrag: extend -c to drop nocompress flag on files
  2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
                   ` (3 preceding siblings ...)
  2017-11-02  3:23 ` [PATCH 4/5] btrfs-progs: property: set/get/print compression property Su Yue
@ 2017-11-02  3:23 ` Su Yue
  2017-11-02  3:44 ` [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
  5 siblings, 0 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:23 UTC (permalink / raw)
  To: linux-btrfs; +Cc: suy.fnst

Now, files which don't have compession property won't be defraged with
compression.

So add an option '--compress-force' to extend -c to drop nocompress flag
on files.

If the option is enable and a file doesn't have compression property:
First add a compression property which is specified by option '-c'
(default as zlib). Then do defrag.

Suggested-by: David Sterba <dsterba@suse.com>
Suggested-by: Anand Jain <anand.jain@oracle.com>
Signed-off-by: Su Yue <suy.fnst@cn.fujitsu.com>
---
 cmds-filesystem.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 88 insertions(+), 6 deletions(-)

diff --git a/cmds-filesystem.c b/cmds-filesystem.c
index 7728430f16a1..4bbbc86a3b3b 100644
--- a/cmds-filesystem.c
+++ b/cmds-filesystem.c
@@ -37,6 +37,7 @@
 #include "list_sort.h"
 #include "disk-io.h"
 #include "help.h"
+#include "props.h"
 
 /*
  * for btrfs fi show, we maintain a hash of fsids we've already printed.
@@ -855,6 +856,20 @@ static int parse_compress_type(char *s)
 	};
 }
 
+static const char *compress_type_extract(int type)
+{
+	switch (type) {
+	case BTRFS_COMPRESS_ZLIB:
+		return "zlib";
+	case BTRFS_COMPRESS_LZO:
+		return "lzo";
+	case BTRFS_COMPRESS_ZSTD:
+		return "zstd";
+	}
+
+	return NULL;
+}
+
 static const char * const cmd_filesystem_defrag_usage[] = {
 	"btrfs filesystem defragment [options] <file>|<dir> [<file>|<dir>...]",
 	"Defragment a file or a directory",
@@ -867,6 +882,8 @@ static const char * const cmd_filesystem_defrag_usage[] = {
 	"-l len              defragment only up to len bytes",
 	"-t size             target extent size hint (default: 32M)",
 	"",
+	"--compress-force    drop nocompress flag on files, only work with option -c",
+	"",
 	"Warning: most Linux kernels will break up the ref-links of COW data",
 	"(e.g., files copied with 'cp --reflink', snapshots) which may cause",
 	"considerable increase of space usage. See btrfs-filesystem(8) for",
@@ -877,8 +894,49 @@ static const char * const cmd_filesystem_defrag_usage[] = {
 static struct btrfs_ioctl_defrag_range_args defrag_global_range;
 static int defrag_global_verbose;
 static int defrag_global_errors;
+static int defrag_global_compress_force;
+static int drop_nocompress_property(const char *fpath)
+{
+	const char *name = "compression";
+	const char *compress_type;
+	int type = prop_object_inode;
+	int ret = 0;
+	const struct prop_handler *prop;
+
+	for (prop = prop_handlers; prop->name; prop++) {
+		if (!strcmp(prop->name, name))
+			break;
+	}
+	if (!prop->name) {
+		error("can't find handler of compression property");
+		ret = -1;
+		goto out;
+	}
+
+	ret = prop->getter(type, fpath, name, NULL);
+	if (ret) {
+		ret = 0;
+		goto out;
+	}
+
+	compress_type = compress_type_extract(
+		defrag_global_range.compress_type);
+	if (!compress_type) {
+		error("unknown compression type %d",
+		      defrag_global_range.compress_type);
+		ret = -1;
+		goto out;
+	}
+	ret = prop->setter(type, fpath, name, compress_type);
+out:
+	if (ret)
+		error("failed to drop nocompress property on file %s", fpath);
+
+	return ret;
+}
+
 static int defrag_callback(const char *fpath, const struct stat *sb,
-		int typeflag, struct FTW *ftwbuf)
+			   int typeflag, struct FTW *ftwbuf)
 {
 	int ret = 0;
 	int err = 0;
@@ -887,6 +945,11 @@ static int defrag_callback(const char *fpath, const struct stat *sb,
 	if ((typeflag == FTW_F) && S_ISREG(sb->st_mode)) {
 		if (defrag_global_verbose)
 			printf("%s\n", fpath);
+		if (defrag_global_compress_force) {
+			ret = drop_nocompress_property(fpath);
+			if (ret)
+				goto error;
+		}
 		fd = open(fpath, O_RDWR);
 		if (fd < 0) {
 			err = errno;
@@ -926,6 +989,12 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 	int compress_type = BTRFS_COMPRESS_NONE;
 	DIR *dirstream;
 
+	enum { GETOPT_VAL_COMPRESS_FORCE = 257};
+	static const struct option long_options[] = {
+		{ "compress-force", no_argument, NULL,
+		  GETOPT_VAL_COMPRESS_FORCE},
+		{ NULL, 0, NULL, 0}
+	};
 	/*
 	 * Kernel has a different default (256K) that is supposed to be safe,
 	 * but it does not defragment very well. The 32M will likely lead to
@@ -937,8 +1006,10 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 	defrag_global_errors = 0;
 	defrag_global_verbose = 0;
 	defrag_global_errors = 0;
+	defrag_global_compress_force = 0;
 	while(1) {
-		int c = getopt(argc, argv, "vrc::fs:l:t:");
+		int c = getopt_long(argc, argv, "vrc::fs:l:t:", long_options,
+				    NULL);
 		if (c < 0)
 			break;
 
@@ -972,6 +1043,9 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 		case 'r':
 			recursive = 1;
 			break;
+		case GETOPT_VAL_COMPRESS_FORCE:
+			defrag_global_compress_force = 1;
+			break;
 		default:
 			usage(cmd_filesystem_defrag_usage);
 		}
@@ -987,6 +1061,8 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 	if (compress_type) {
 		defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_COMPRESS;
 		defrag_global_range.compress_type = compress_type;
+	} else if (defrag_global_compress_force) {
+		warning("Option --compress-force only works for -c");
 	}
 	if (flush)
 		defrag_global_range.flags |= BTRFS_DEFRAG_RANGE_START_IO;
@@ -1049,7 +1125,7 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 		}
 		if (recursive && S_ISDIR(st.st_mode)) {
 			ret = nftw(argv[i], defrag_callback, 10,
-						FTW_MOUNT | FTW_PHYS);
+				   FTW_MOUNT | FTW_PHYS);
 			if (ret == ENOTTY)
 				exit(1);
 			/* errors are handled in the callback */
@@ -1057,9 +1133,15 @@ static int cmd_filesystem_defrag(int argc, char **argv)
 		} else {
 			if (defrag_global_verbose)
 				printf("%s\n", argv[i]);
-			ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE,
-					&defrag_global_range);
-			defrag_err = errno;
+			if (defrag_global_compress_force)
+				ret = drop_nocompress_property(argv[i]);
+			if (ret) {
+				defrag_err = -ret;
+			} else {
+				ret = ioctl(fd, BTRFS_IOC_DEFRAG_RANGE,
+					    &defrag_global_range);
+				defrag_err = errno;
+			}
 		}
 		close_file_or_dir(fd, dirstream);
 		if (ret && defrag_err == ENOTTY) {
-- 
2.15.0




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

* Re: [PATCH 0/5] btrfs-progs: resolve property and enhance defragment
  2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
                   ` (4 preceding siblings ...)
  2017-11-02  3:23 ` [PATCH 5/5] btrfs-progs: fi defrag: extend -c to drop nocompress flag on files Su Yue
@ 2017-11-02  3:44 ` Su Yue
  2017-11-02  4:13   ` Wang Shilong
  5 siblings, 1 reply; 9+ messages in thread
From: Su Yue @ 2017-11-02  3:44 UTC (permalink / raw)
  To: linux-btrfs

Sorry, the patchset does not work as expected.
Please ignore it.

On 11/02/2017 11:23 AM, Su Yue wrote:
> The patchset adds an option '--compress-force' to work with
> 'btrfs fi defrag -c'. Then no-compression files will be set with
> compression property specified by '-c' (zlib default).
> 
> patch[1-4] divide property handler to setter, getter, and printer.
> 
> Then patch[5] could enhance defragment easier.
> 
> Su Yue (5):
>    btrfs-progs: property: divide prop_handler_t into
>      setter,getter,printer
>    btrfs-progs: property: set/get/print ro property
>    btrfs-progs: property: set/get/print label property
>    btrfs-progs: property: set/get/print compression property
>    btrfs-progs: fi defrag: extend -c to drop nocompress flag on files
> 
>   cmds-filesystem.c |  94 +++++++++++++++++++++++--
>   cmds-property.c   |   7 +-
>   props.c           | 207 +++++++++++++++++++++++++++++++++++++++++++++---------
>   props.h           |  19 +++--
>   4 files changed, 283 insertions(+), 44 deletions(-)
> 



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

* Re: [PATCH 0/5] btrfs-progs: resolve property and enhance defragment
  2017-11-02  3:44 ` [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
@ 2017-11-02  4:13   ` Wang Shilong
  2017-11-02  5:29     ` Su Yue
  0 siblings, 1 reply; 9+ messages in thread
From: Wang Shilong @ 2017-11-02  4:13 UTC (permalink / raw)
  To: Su Yue; +Cc: Wang Shilong, linux-btrfs


在 2017年11月2日,上午11:44,Su Yue <suy.fnst@cn.fujitsu.com> 写道:

Sorry, the patchset does not work as expected.
Please ignore it.

On 11/02/2017 11:23 AM, Su Yue wrote:
> The patchset adds an option '--compress-force' to work with
> 'btrfs fi defrag -c'. Then no-compression files will be set with
> compression property specified by '-c' (zlib default).
> patch[1-4] divide property handler to setter, getter, and printer.
> Then patch[5] could enhance defragment easier.
> Su Yue (5):
>   btrfs-progs: property: divide prop_handler_t into
>     setter,getter,printer
>   btrfs-progs: property: set/get/print ro property
>   btrfs-progs: property: set/get/print label property
>   btrfs-progs: property: set/get/print compression property
>   btrfs-progs: fi defrag: extend -c to drop nocompress flag on files
>  cmds-filesystem.c |  94 +++++++++++++++++++++++--
>  cmds-property.c   |   7 +-
>  props.c           | 207 +++++++++++++++++++++++++++++++++++++++++++++---------
>  props.h           |  19 +++--
>  4 files changed, 283 insertions(+), 44 deletions(-)

FYI, i think it better to add some tests to btrfs-progs if new interface
or function like this is added.

Thanks,
Shilong


--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


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

* Re: [PATCH 0/5] btrfs-progs: resolve property and enhance defragment
  2017-11-02  4:13   ` Wang Shilong
@ 2017-11-02  5:29     ` Su Yue
  0 siblings, 0 replies; 9+ messages in thread
From: Su Yue @ 2017-11-02  5:29 UTC (permalink / raw)
  To: Wang Shilong; +Cc: linux-btrfs



On 11/02/2017 12:13 PM, Wang Shilong wrote:
> 
> 在 2017年11月2日,上午11:44,Su Yue <suy.fnst@cn.fujitsu.com> 写道:
> 
> Sorry, the patchset does not work as expected.
> Please ignore it.
> 
> On 11/02/2017 11:23 AM, Su Yue wrote:
>> The patchset adds an option '--compress-force' to work with
>> 'btrfs fi defrag -c'. Then no-compression files will be set with
>> compression property specified by '-c' (zlib default).
>> patch[1-4] divide property handler to setter, getter, and printer.
>> Then patch[5] could enhance defragment easier.
>> Su Yue (5):
>>    btrfs-progs: property: divide prop_handler_t into
>>      setter,getter,printer
>>    btrfs-progs: property: set/get/print ro property
>>    btrfs-progs: property: set/get/print label property
>>    btrfs-progs: property: set/get/print compression property
>>    btrfs-progs: fi defrag: extend -c to drop nocompress flag on files
>>   cmds-filesystem.c |  94 +++++++++++++++++++++++--
>>   cmds-property.c   |   7 +-
>>   props.c           | 207 +++++++++++++++++++++++++++++++++++++++++++++---------
>>   props.h           |  19 +++--
>>   4 files changed, 283 insertions(+), 44 deletions(-)
> 
> FYI, i think it better to add some tests to btrfs-progs if new interface
> or function like this is added.
> 
Thank you. I will add  a test in next version.

Thanks,
Su
> Thanks,
> Shilong
> 
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 
> 



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

end of thread, other threads:[~2017-11-02  5:25 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-02  3:23 [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
2017-11-02  3:23 ` [PATCH 1/5] btrfs-progs: property: divide prop_handler_t into setter,getter,printer Su Yue
2017-11-02  3:23 ` [PATCH 2/5] btrfs-progs: property: set/get/print ro property Su Yue
2017-11-02  3:23 ` [PATCH 3/5] btrfs-progs: property: set/get/print label property Su Yue
2017-11-02  3:23 ` [PATCH 4/5] btrfs-progs: property: set/get/print compression property Su Yue
2017-11-02  3:23 ` [PATCH 5/5] btrfs-progs: fi defrag: extend -c to drop nocompress flag on files Su Yue
2017-11-02  3:44 ` [PATCH 0/5] btrfs-progs: resolve property and enhance defragment Su Yue
2017-11-02  4:13   ` Wang Shilong
2017-11-02  5:29     ` Su Yue

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).