* [PATCH v4] btrfs-progs: filesystem-resize: make output more readable
@ 2021-02-20 12:41 Sidong Yang
2021-03-09 15:37 ` David Sterba
2021-03-09 15:38 ` David Sterba
0 siblings, 2 replies; 3+ messages in thread
From: Sidong Yang @ 2021-02-20 12:41 UTC (permalink / raw)
To: linux-btrfs, dsterba; +Cc: Sidong Yang
This patch make output of filesystem-resize command more readable and
give detail information for users. This patch provides more information
about filesystem like below.
Before:
Resize '/mnt' of '1:-1G'
After:
Resize device id 1 (/dev/vdb) from 4.00GiB to 3.00GiB
Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
v2:
- print more detailed error
- covers all the possibilities format provides
v3:
- use snprintf than strcpy for safety
- add diff variable for code readability
v4:
- fix bugs for argument that has no devid
---
cmds/filesystem.c | 120 +++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 119 insertions(+), 1 deletion(-)
diff --git a/cmds/filesystem.c b/cmds/filesystem.c
index 0d23daf4..7ddf5880 100644
--- a/cmds/filesystem.c
+++ b/cmds/filesystem.c
@@ -28,6 +28,7 @@
#include <linux/limits.h>
#include <linux/version.h>
#include <getopt.h>
+#include <limits.h>
#include <btrfsutil.h>
@@ -1074,6 +1075,117 @@ static const char * const cmd_filesystem_resize_usage[] = {
NULL
};
+static int check_resize_args(const char *amount, const char *path) {
+ struct btrfs_ioctl_fs_info_args fi_args;
+ struct btrfs_ioctl_dev_info_args *di_args = NULL;
+ int ret, i, dev_idx = -1;
+ u64 devid = 1;
+ const char *res_str = NULL;
+ char *devstr = NULL, *sizestr = NULL;
+ u64 new_size = 0, old_size = 0, diff = 0;
+ int mod = 0;
+ char amount_dup[BTRFS_VOL_NAME_MAX];
+
+ ret = get_fs_info(path, &fi_args, &di_args);
+
+ if (ret) {
+ error("unable to retrieve fs info");
+ return 1;
+ }
+
+ if (!fi_args.num_devices) {
+ error("no devices found");
+ free(di_args);
+ return 1;
+ }
+
+ ret = snprintf(amount_dup, BTRFS_VOL_NAME_MAX, "%s", amount);
+ if (strlen(amount) != ret) {
+ error("newsize argument is too long");
+ free(di_args);
+ return 1;
+ }
+
+ sizestr = amount_dup;
+ devstr = strchr(sizestr, ':');
+ if (devstr) {
+ sizestr = devstr + 1;
+ *devstr = '\0';
+ devstr = amount_dup;
+
+ errno = 0;
+ devid = strtoull(devstr, NULL, 10);
+
+ if (errno) {
+ error("failed to parse devid %s", devstr);
+ free(di_args);
+ return 1;
+ }
+ }
+
+ dev_idx = -1;
+ for(i = 0; i < fi_args.num_devices; i++) {
+ if (di_args[i].devid == devid) {
+ dev_idx = i;
+ break;
+ }
+ }
+
+ if (dev_idx < 0) {
+ error("cannot find devid : %lld", devid);
+ free(di_args);
+ return 1;
+ }
+
+ if (!strcmp(sizestr, "max")) {
+ res_str = "max";
+ }
+ else {
+ if (sizestr[0] == '-') {
+ mod = -1;
+ sizestr++;
+ } else if (sizestr[0] == '+') {
+ mod = 1;
+ sizestr++;
+ }
+ diff = parse_size_from_string(sizestr);
+ if (!diff) {
+ error("failed to parse size %s", sizestr);
+ free(di_args);
+ return 1;
+ }
+ old_size = di_args[dev_idx].total_bytes;
+
+ if (mod < 0) {
+ if (diff > old_size) {
+ error("current size is %s which is smaller than %s",
+ pretty_size_mode(old_size, UNITS_DEFAULT),
+ pretty_size_mode(diff, UNITS_DEFAULT));
+ free(di_args);
+ return 1;
+ }
+ new_size = old_size - diff;
+ } else if (mod > 0) {
+ if (diff > ULLONG_MAX - old_size) {
+ error("increasing %s is out of range",
+ pretty_size_mode(diff, UNITS_DEFAULT));
+ free(di_args);
+ return 1;
+ }
+ new_size = old_size + diff;
+ }
+ new_size = round_down(new_size, fi_args.sectorsize);
+ res_str = pretty_size_mode(new_size, UNITS_DEFAULT);
+ }
+
+ printf("Resize device id %lld (%s) from %s to %s\n", devid, di_args[dev_idx].path,
+ pretty_size_mode(di_args[dev_idx].total_bytes, UNITS_DEFAULT),
+ res_str);
+
+ free(di_args);
+ return 0;
+}
+
static int cmd_filesystem_resize(const struct cmd_struct *cmd,
int argc, char **argv)
{
@@ -1134,7 +1246,13 @@ static int cmd_filesystem_resize(const struct cmd_struct *cmd,
return 1;
}
- printf("Resize '%s' of '%s'\n", path, amount);
+ ret = check_resize_args(amount, path);
+ if (ret != 0) {
+ close_file_or_dir(fd, dirstream);
+ return 1;
+ }
+
+
memset(&args, 0, sizeof(args));
strncpy_null(args.name, amount);
res = ioctl(fd, BTRFS_IOC_RESIZE, &args);
--
2.25.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v4] btrfs-progs: filesystem-resize: make output more readable
2021-02-20 12:41 [PATCH v4] btrfs-progs: filesystem-resize: make output more readable Sidong Yang
@ 2021-03-09 15:37 ` David Sterba
2021-03-09 15:38 ` David Sterba
1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2021-03-09 15:37 UTC (permalink / raw)
To: Sidong Yang; +Cc: linux-btrfs, dsterba
On Sat, Feb 20, 2021 at 12:41:17PM +0000, Sidong Yang wrote:
> This patch make output of filesystem-resize command more readable and
> give detail information for users. This patch provides more information
> about filesystem like below.
>
> Before:
> Resize '/mnt' of '1:-1G'
>
> After:
> Resize device id 1 (/dev/vdb) from 4.00GiB to 3.00GiB
>
> Signed-off-by: Sidong Yang <realwakka@gmail.com>
Added to devel, thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v4] btrfs-progs: filesystem-resize: make output more readable
2021-02-20 12:41 [PATCH v4] btrfs-progs: filesystem-resize: make output more readable Sidong Yang
2021-03-09 15:37 ` David Sterba
@ 2021-03-09 15:38 ` David Sterba
1 sibling, 0 replies; 3+ messages in thread
From: David Sterba @ 2021-03-09 15:38 UTC (permalink / raw)
To: Sidong Yang; +Cc: linux-btrfs, dsterba
On Sat, Feb 20, 2021 at 12:41:17PM +0000, Sidong Yang wrote:
> This patch make output of filesystem-resize command more readable and
> give detail information for users. This patch provides more information
> about filesystem like below.
>
> Before:
> Resize '/mnt' of '1:-1G'
>
> After:
> Resize device id 1 (/dev/vdb) from 4.00GiB to 3.00GiB
>
> Signed-off-by: Sidong Yang <realwakka@gmail.com>
> ---
> v2:
> - print more detailed error
> - covers all the possibilities format provides
> v3:
> - use snprintf than strcpy for safety
> - add diff variable for code readability
> v4:
> - fix bugs for argument that has no devid
> ---
> cmds/filesystem.c | 120 +++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 119 insertions(+), 1 deletion(-)
>
> diff --git a/cmds/filesystem.c b/cmds/filesystem.c
> index 0d23daf4..7ddf5880 100644
> --- a/cmds/filesystem.c
> +++ b/cmds/filesystem.c
> @@ -28,6 +28,7 @@
> #include <linux/limits.h>
> #include <linux/version.h>
> #include <getopt.h>
> +#include <limits.h>
>
> #include <btrfsutil.h>
>
> @@ -1074,6 +1075,117 @@ static const char * const cmd_filesystem_resize_usage[] = {
> NULL
> };
>
> +static int check_resize_args(const char *amount, const char *path) {
> + struct btrfs_ioctl_fs_info_args fi_args;
> + struct btrfs_ioctl_dev_info_args *di_args = NULL;
> + int ret, i, dev_idx = -1;
> + u64 devid = 1;
> + const char *res_str = NULL;
> + char *devstr = NULL, *sizestr = NULL;
> + u64 new_size = 0, old_size = 0, diff = 0;
> + int mod = 0;
> + char amount_dup[BTRFS_VOL_NAME_MAX];
> +
> + ret = get_fs_info(path, &fi_args, &di_args);
> +
> + if (ret) {
> + error("unable to retrieve fs info");
> + return 1;
> + }
> +
> + if (!fi_args.num_devices) {
> + error("no devices found");
> + free(di_args);
> + return 1;
Btw I changed all the free/return to ret = 1/goto out pattern so the
cleanup does not need to repeated next to each return.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-03-09 15:41 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-02-20 12:41 [PATCH v4] btrfs-progs: filesystem-resize: make output more readable Sidong Yang
2021-03-09 15:37 ` David Sterba
2021-03-09 15:38 ` David Sterba
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).