All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sidong Yang <realwakka@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Sidong Yang <realwakka@gmail.com>
Subject: [RFC PATCH 2/2] btrfs-progs: fi resize: support all option for resize
Date: Mon, 15 Aug 2022 12:36:52 +0000	[thread overview]
Message-ID: <20220815123652.52314-3-realwakka@gmail.com> (raw)
In-Reply-To: <20220815123652.52314-1-realwakka@gmail.com>

This patch make resize command support "all" option. Also, it resolves
github issue #471. If user sets device as "all", it iterates all devices
with resizing for each device. For that, this patch make a function
do_resize_with_args() for avoiding duplicated code that call ioctl() and
handling an error.

Issue: #471

Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
 cmds/filesystem.c | 91 ++++++++++++++++++++++++++++++-----------------
 1 file changed, 58 insertions(+), 33 deletions(-)

diff --git a/cmds/filesystem.c b/cmds/filesystem.c
index ea1b0c84..9ce84f8f 100644
--- a/cmds/filesystem.c
+++ b/cmds/filesystem.c
@@ -1150,14 +1150,47 @@ out:
 	return ret;
 }
 
+static int do_resize_with_args(int fd, const char *amount, const char *path) {
+	int ret, e;
+	struct btrfs_ioctl_vol_args	args;
+
+	memset(&args, 0, sizeof(args));
+	strncpy_null(args.name, amount);
+	ret = ioctl(fd, BTRFS_IOC_RESIZE, &args);
+	e = errno;
+	if(ret < 0){
+		switch (e) {
+		case EFBIG:
+			error("unable to resize '%s': no enough free space",
+				  path);
+			break;
+		default:
+			error("unable to resize '%s': %m", path);
+			break;
+		}
+		return 1;
+	} else if (ret > 0) {
+		const char *err_str = btrfs_err_str(ret);
+
+		if (err_str) {
+			error("resizing of '%s' failed: %s", path, err_str);
+		} else {
+			error("resizing of '%s' failed: unknown error %d",
+				  path, ret);
+		}
+		return 1;
+	}
+
+	return 0;
+}
+
 static int resize_with_args(const char *amount, const char *path, int fd) {
 	struct btrfs_ioctl_fs_info_args fi_args;
 	struct btrfs_ioctl_dev_info_args *di_args = NULL;
-	int ret, i, e, dev_idx = -1;
+	int ret, i, dev_idx = -1;
 	u64 devid = 1;
 	char *devstr = NULL, *sizestr = NULL;
 	char amount_dup[BTRFS_VOL_NAME_MAX];
-	struct btrfs_ioctl_vol_args	args;
 	ret = get_fs_info(path, &fi_args, &di_args);
 
 	if (ret) {
@@ -1185,13 +1218,29 @@ static int resize_with_args(const char *amount, const char *path, int fd) {
 		*devstr = 0;
 		devstr = amount_dup;
 
-		errno = 0;
-		devid = strtoull(devstr, NULL, 10);
+		if (strncmp(devstr, "all", 3) == 0) {
+			for(i = 0; i < fi_args.num_devices; i++) {
+				char amount_tmp[BTRFS_VOL_NAME_MAX];
+				devid = di_args[i].devid;
+				ret = check_resize_args_sizestr(sizestr, devid, i, di_args, &fi_args);
+				if (ret)
+					return 1;
 
-		if (errno) {
-			error("failed to parse devid %s: %m", devstr);
-			ret = 1;
-			goto out;
+				snprintf(amount_tmp, BTRFS_VOL_NAME_MAX, "%llu:%s", devid, sizestr);
+				if (do_resize_with_args(fd, amount_tmp, path))
+					return 1;
+			}
+
+			return 0;
+		} else {
+			errno = 0;
+			devid = strtoull(devstr, NULL, 10);
+
+			if (errno) {
+				error("failed to parse devid %s: %m", devstr);
+				ret = 1;
+				goto out;
+			}
 		}
 	}
 
@@ -1213,32 +1262,8 @@ static int resize_with_args(const char *amount, const char *path, int fd) {
 	if (ret)
 		return 1;
 
-	memset(&args, 0, sizeof(args));
-	strncpy_null(args.name, amount);
-	ret = ioctl(fd, BTRFS_IOC_RESIZE, &args);
-	e = errno;
-	if(ret < 0){
-		switch (e) {
-		case EFBIG:
-			error("unable to resize '%s': no enough free space",
-				  path);
-			break;
-		default:
-			error("unable to resize '%s': %m", path);
-			break;
-		}
-		return 1;
-	} else if (ret > 0) {
-		const char *err_str = btrfs_err_str(ret);
-
-		if (err_str) {
-			error("resizing of '%s' failed: %s", path, err_str);
-		} else {
-			error("resizing of '%s' failed: unknown error %d",
-				  path, ret);
-		}
+	if (do_resize_with_args(fd, amount, path))
 		return 1;
-	}
 
 out:
 	free(di_args);
-- 
2.34.1


      parent reply	other threads:[~2022-08-15 12:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-15 12:36 [RFC PATCH 0/2] support all option for resize command Sidong Yang
2022-08-15 12:36 ` [RFC PATCH 1/2] btrfs-progs: fi resize: refactor function check_resize_args() Sidong Yang
2022-08-15 12:36 ` Sidong Yang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220815123652.52314-3-realwakka@gmail.com \
    --to=realwakka@gmail.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.