Skip to contentSkip to conversation

btrfs-progs: add recursive subvol delete

PATCH In review Mark Harmstone 4 participants 5 messages Jun 27, 2024 – Aug 11, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs-progs: add recursive subvol delete

From: Omar Sandoval <[email protected]>

Adds a --recursive option to btrfs subvol delete, causing it to pass the BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE flag through to libbtrfsutil.

This is a resubmission of part of a patch that Omar Sandoval sent in 2018, which appears to have been overlooked:
https://lore.kernel.org/linux-btrfs/e42cdc5d5287269faf4d09e8c9786d0b3adeb658.1516991902.git.osandov@fb.com/

---
 Documentation/btrfs-subvolume.rst |  4 ++++
 cmds/subvolume.c                  | 15 +++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)
Documentation/btrfs-subvolume.rst +4 -0
index d1e89f15..b1f22344 100644
@@ -112,6 +112,10 @@ delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid>
         -i|--subvolid <subvolid>
                 subvolume id to be removed instead of the <path> that should point to the
                 filesystem with the subvolume
+
+        -R|--recursive
+                delete subvolumes beneath each subvolume recursively
+
         -v|--verbose
                 (deprecated) alias for global *-v* option
 
cmds/subvolume.c +13 -2
index 52bc8850..b4151af2 100644
@@ -347,6 +347,7 @@ static const char * const cmd_subvolume_delete_usage[] = {
 	OPTLINE("-c|--commit-after", "wait for transaction commit at the end of the operation"),
 	OPTLINE("-C|--commit-each", "wait for transaction commit after deleting each subvolume"),
 	OPTLINE("-i|--subvolid", "subvolume id of the to be removed subvolume"),
+	OPTLINE("-R|--recursive", "delete subvolumes beneath each subvolume recursively"),
 	OPTLINE("-v|--verbose", "deprecated, alias for global -v option"),
 	HELPINFO_INSERT_GLOBALS,
 	HELPINFO_INSERT_VERBOSE,
@@ -367,6 +368,7 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
 	char	*path = NULL;
 	int commit_mode = 0;
 	bool subvol_path_not_found = false;
+	int flags = 0;
 	u8 fsid[BTRFS_FSID_SIZE];
 	u64 subvolid = 0;
 	char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
@@ -383,11 +385,12 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
 			{"commit-after", no_argument, NULL, 'c'},
 			{"commit-each", no_argument, NULL, 'C'},
 			{"subvolid", required_argument, NULL, 'i'},
+			{"recursive", no_argument, NULL, 'R'},
 			{"verbose", no_argument, NULL, 'v'},
 			{NULL, 0, NULL, 0}
 		};
 
-		c = getopt_long(argc, argv, "cCi:v", long_options, NULL);
+		c = getopt_long(argc, argv, "cCi:Rv", long_options, NULL);
 		if (c < 0)
 			break;
 
@@ -401,6 +404,9 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
 		case 'i':
 			subvolid = arg_strtou64(optarg);
 			break;
+		case 'R':
+			flags |= BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE;
+			break;
 		case 'v':
 			bconf_be_verbose();
 			break;
@@ -416,6 +422,11 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
 	if (subvolid > 0 && check_argc_exact(argc - optind, 1))
 		return 1;
 
+	if (subvolid > 0 && flags & BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE) {
+		error("option --recursive not supported with --subvolid");
+		return 1;
+	}
+
 	pr_verbose(LOG_INFO, "Transaction commit: %s\n",
 		   !commit_mode ? "none (default)" :
 		   commit_mode == COMMIT_AFTER ? "at the end" : "after each");
@@ -528,7 +539,7 @@ again:
 
 	/* Start deleting. */
 	if (subvolid == 0)
-		err = btrfs_util_delete_subvolume_fd(fd, vname, 0);
+		err = btrfs_util_delete_subvolume_fd(fd, vname, flags);
 	else
 		err = btrfs_util_delete_subvolume_by_id_fd(fd, subvolid);
 	if (err) {
Signature · 1 line
2.44.2
#1 lore raw

Would someone mind having a look at this please? I think it's been missed.

Thanks

Quoted 96 lines · Mark Harmstone

From: Omar Sandoval <[email protected]>

Adds a --recursive option to btrfs subvol delete, causing it to pass the BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE flag through to libbtrfsutil.

This is a resubmission of part of a patch that Omar Sandoval sent in 2018, which appears to have been overlooked:
https://lore.kernel.org/linux-btrfs/e42cdc5d5287269faf4d09e8c9786d0b3adeb658.1516991902.git.osandov@fb.com/

---
  Documentation/btrfs-subvolume.rst |  4 ++++
  cmds/subvolume.c                  | 15 +++++++++++++--
  2 files changed, 17 insertions(+), 2 deletions(-)
Documentation/btrfs-subvolume.rst +4 -0
index d1e89f15..b1f22344 100644
@@ -112,6 +112,10 @@ delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid>
          -i|--subvolid <subvolid>
                  subvolume id to be removed instead of the <path> that should point to the
                  filesystem with the subvolume
+
+        -R|--recursive
+                delete subvolumes beneath each subvolume recursively
+
          -v|--verbose
                  (deprecated) alias for global *-v* option
 
cmds/subvolume.c +13 -2
index 52bc8850..b4151af2 100644
@@ -347,6 +347,7 @@ static const char * const cmd_subvolume_delete_usage[] = {
  	OPTLINE("-c|--commit-after", "wait for transaction commit at the end of the operation"),
  	OPTLINE("-C|--commit-each", "wait for transaction commit after deleting each subvolume"),
  	OPTLINE("-i|--subvolid", "subvolume id of the to be removed subvolume"),
+	OPTLINE("-R|--recursive", "delete subvolumes beneath each subvolume recursively"),
  	OPTLINE("-v|--verbose", "deprecated, alias for global -v option"),
  	HELPINFO_INSERT_GLOBALS,
  	HELPINFO_INSERT_VERBOSE,
@@ -367,6 +368,7 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  	char	*path = NULL;
  	int commit_mode = 0;
  	bool subvol_path_not_found = false;
+	int flags = 0;
  	u8 fsid[BTRFS_FSID_SIZE];
  	u64 subvolid = 0;
  	char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
@@ -383,11 +385,12 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  			{"commit-after", no_argument, NULL, 'c'},
  			{"commit-each", no_argument, NULL, 'C'},
  			{"subvolid", required_argument, NULL, 'i'},
+			{"recursive", no_argument, NULL, 'R'},
  			{"verbose", no_argument, NULL, 'v'},
  			{NULL, 0, NULL, 0}
  		};
 
-		c = getopt_long(argc, argv, "cCi:v", long_options, NULL);
+		c = getopt_long(argc, argv, "cCi:Rv", long_options, NULL);
  		if (c < 0)
  			break;
 
@@ -401,6 +404,9 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  		case 'i':
  			subvolid = arg_strtou64(optarg);
  			break;
+		case 'R':
+			flags |= BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE;
+			break;
  		case 'v':
  			bconf_be_verbose();
  			break;
@@ -416,6 +422,11 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  	if (subvolid > 0 && check_argc_exact(argc - optind, 1))
  		return 1;
 
+	if (subvolid > 0 && flags & BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE) {
+		error("option --recursive not supported with --subvolid");
+		return 1;
+	}
+
  	pr_verbose(LOG_INFO, "Transaction commit: %s\n",
  		   !commit_mode ? "none (default)" :
  		   commit_mode == COMMIT_AFTER ? "at the end" : "after each");
@@ -528,7 +539,7 @@ again:
 
  	/* Start deleting. */
  	if (subvolid == 0)
-		err = btrfs_util_delete_subvolume_fd(fd, vname, 0);
+		err = btrfs_util_delete_subvolume_fd(fd, vname, flags);
  	else
  		err = btrfs_util_delete_subvolume_by_id_fd(fd, subvolid);
  	if (err) {
Reviewed-by on v1 1/1
#2 lore raw
In reply to Mark Harmstone
Quoted 11 lines · Mark Harmstone

From: Omar Sandoval <[email protected]>

Adds a --recursive option to btrfs subvol delete, causing it to pass the BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE flag through to libbtrfsutil.

This is a resubmission of part of a patch that Omar Sandoval sent in 2018, which appears to have been overlooked:
https://lore.kernel.org/linux-btrfs/e42cdc5d5287269faf4d09e8c9786d0b3adeb658.1516991902.git.osandov@fb.com/

Looks very reasonable to me, especially the flag is already there.

Thanks,
Qu

Quoted 85 lines
---
  Documentation/btrfs-subvolume.rst |  4 ++++
  cmds/subvolume.c                  | 15 +++++++++++++--
  2 files changed, 17 insertions(+), 2 deletions(-)
Documentation/btrfs-subvolume.rst +4 -0
index d1e89f15..b1f22344 100644
@@ -112,6 +112,10 @@ delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid>
          -i|--subvolid <subvolid>
                  subvolume id to be removed instead of the <path> that should point to the
                  filesystem with the subvolume
+
+        -R|--recursive
+                delete subvolumes beneath each subvolume recursively
+
          -v|--verbose
                  (deprecated) alias for global *-v* option
 
cmds/subvolume.c +13 -2
index 52bc8850..b4151af2 100644
@@ -347,6 +347,7 @@ static const char * const cmd_subvolume_delete_usage[] = {
  	OPTLINE("-c|--commit-after", "wait for transaction commit at the end of the operation"),
  	OPTLINE("-C|--commit-each", "wait for transaction commit after deleting each subvolume"),
  	OPTLINE("-i|--subvolid", "subvolume id of the to be removed subvolume"),
+	OPTLINE("-R|--recursive", "delete subvolumes beneath each subvolume recursively"),
  	OPTLINE("-v|--verbose", "deprecated, alias for global -v option"),
  	HELPINFO_INSERT_GLOBALS,
  	HELPINFO_INSERT_VERBOSE,
@@ -367,6 +368,7 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  	char	*path = NULL;
  	int commit_mode = 0;
  	bool subvol_path_not_found = false;
+	int flags = 0;
  	u8 fsid[BTRFS_FSID_SIZE];
  	u64 subvolid = 0;
  	char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
@@ -383,11 +385,12 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  			{"commit-after", no_argument, NULL, 'c'},
  			{"commit-each", no_argument, NULL, 'C'},
  			{"subvolid", required_argument, NULL, 'i'},
+			{"recursive", no_argument, NULL, 'R'},
  			{"verbose", no_argument, NULL, 'v'},
  			{NULL, 0, NULL, 0}
  		};
 
-		c = getopt_long(argc, argv, "cCi:v", long_options, NULL);
+		c = getopt_long(argc, argv, "cCi:Rv", long_options, NULL);
  		if (c < 0)
  			break;
 
@@ -401,6 +404,9 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  		case 'i':
  			subvolid = arg_strtou64(optarg);
  			break;
+		case 'R':
+			flags |= BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE;
+			break;
  		case 'v':
  			bconf_be_verbose();
  			break;
@@ -416,6 +422,11 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
  	if (subvolid > 0 && check_argc_exact(argc - optind, 1))
  		return 1;
 
+	if (subvolid > 0 && flags & BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE) {
+		error("option --recursive not supported with --subvolid");
+		return 1;
+	}
+
  	pr_verbose(LOG_INFO, "Transaction commit: %s\n",
  		   !commit_mode ? "none (default)" :
  		   commit_mode == COMMIT_AFTER ? "at the end" : "after each");
@@ -528,7 +539,7 @@ again:
 
  	/* Start deleting. */
  	if (subvolid == 0)
-		err = btrfs_util_delete_subvolume_fd(fd, vname, 0);
+		err = btrfs_util_delete_subvolume_fd(fd, vname, flags);
  	else
  		err = btrfs_util_delete_subvolume_by_id_fd(fd, subvolid);
  	if (err) {
#3 lore raw
Quoted 12 lines · Qu Wenruo
Quoted 9 lines · Mark Harmstone

From: Omar Sandoval <[email protected]>

Adds a --recursive option to btrfs subvol delete, causing it to pass the BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE flag through to libbtrfsutil.

This is a resubmission of part of a patch that Omar Sandoval sent in 2018, which appears to have been overlooked:
https://lore.kernel.org/linux-btrfs/e42cdc5d5287269faf4d09e8c9786d0b3adeb658.1516991902.git.osandov@fb.com/

Quoted 6 lines
Quoted 2 lines

Looks very reasonable to me, especially the flag is already there.

Sorry to bother you, although this patch already has two reviewed-by tags, I thought it would be an easy push thus created the PR for you:

https://github.com/kdave/btrfs-progs/pull/861

But I got some resistance from David, focusing on certain technical details on the help string (which I strongly disagree).

I think you may want to contribute your opinions to that. (The thread on cmds/subvolumes.c)

And if possible, you may want to send an updated version, and create your PR to the repo (and I will close my PR).

Furthermore, you may want to submit your PR to btrfs-progs repo, to get some more direct feedback.

Thanks,
Qu

Quoted 101 lines

Thanks,
Qu

Quoted 97 lines

---
  Documentation/btrfs-subvolume.rst |  4 ++++ cmds/subvolume.c                  | 15 +++++++++++++--

  2 files changed, 17 insertions(+), 2 deletions(-)
Documentation/btrfs-subvolume.rst +0 -0
index d1e89f15..b1f22344 100644
@@ -112,6 +112,10 @@ delete [options] [<subvolume> [<subvolume>...]],

delete -i|--subvolid <subvolid>
          -i|--subvolid <subvolid>
                  subvolume id to be removed instead of the <path> that should point to the
                  filesystem with the subvolume

Documentation/btrfs-subvolume.rst +4 -0
+
+        -R|--recursive
+                delete subvolumes beneath each subvolume recursively
+

          -v|--verbose
                  (deprecated) alias for global *-v* option

cmds/subvolume.c +0 -0
index 52bc8850..b4151af2 100644
@@ -347,6 +347,7 @@ static const char * const
 cmd_subvolume_delete_usage[] = {

      OPTLINE("-c|--commit-after", "wait for transaction commit at the end of the operation"),
      OPTLINE("-C|--commit-each", "wait for transaction commit after deleting each subvolume"),
      OPTLINE("-i|--subvolid", "subvolume id of the to be removed subvolume"),
+    OPTLINE("-R|--recursive", "delete subvolumes beneath each subvolume recursively"),
      OPTLINE("-v|--verbose", "deprecated, alias for global -v option"), HELPINFO_INSERT_GLOBALS,
      HELPINFO_INSERT_VERBOSE,

cmds/subvolume.c +0 -0
@@ -367,6 +368,7 @@ static int cmd_subvolume_delete(const struct

cmd_struct *cmd, int argc, char **a
      char    *path = NULL;
      int commit_mode = 0;
      bool subvol_path_not_found = false;
+    int flags = 0;
      u8 fsid[BTRFS_FSID_SIZE];
      u64 subvolid = 0;
      char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];

cmds/subvolume.c +0 -0
@@ -383,11 +385,12 @@ static int cmd_subvolume_delete(const struct

cmd_struct *cmd, int argc, char **a {"commit-after", no_argument, NULL, 'c'}, {"commit-each", no_argument, NULL, 'C'}, {"subvolid", required_argument, NULL, 'i'}, +            {"recursive", no_argument, NULL, 'R'}, {"verbose", no_argument, NULL, 'v'}, {NULL, 0, NULL, 0}
          };

-        c = getopt_long(argc, argv, "cCi:v", long_options, NULL); +        c = getopt_long(argc, argv, "cCi:Rv", long_options, NULL); if (c < 0)
              break;

cmds/subvolume.c +0 -0
@@ -401,6 +404,9 @@ static int cmd_subvolume_delete(const struct

cmd_struct *cmd, int argc, char **a
          case 'i':
              subvolid = arg_strtou64(optarg); break;
+        case 'R':
+            flags |= BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE; +            break;
          case 'v':
              bconf_be_verbose();
              break;

cmds/subvolume.c +0 -0
@@ -416,6 +422,11 @@ static int cmd_subvolume_delete(const struct

cmd_struct *cmd, int argc, char **a
      if (subvolid > 0 && check_argc_exact(argc - optind, 1)) return 1;

+    if (subvolid > 0 && flags & BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE) { +        error("option --recursive not supported with --subvolid"); +        return 1;
+    }
+
      pr_verbose(LOG_INFO, "Transaction commit: %s\n", !commit_mode ? "none (default)" :
             commit_mode == COMMIT_AFTER ? "at the end" : "after each");

cmds/subvolume.c +1 -1
@@ -528,7 +539,7 @@ again:
 
       /* Start deleting. */
       if (subvolid == 0)
-        err = btrfs_util_delete_subvolume_fd(fd, vname, 0);
+        err = btrfs_util_delete_subvolume_fd(fd, vname, flags);

      else
          err = btrfs_util_delete_subvolume_by_id_fd(fd, subvolid); if (err) {

Reviewed-by on v1 1/1
#4 lore raw
In reply to Mark Harmstone
Quoted 98 lines · Mark Harmstone

From: Omar Sandoval <[email protected]>

Adds a --recursive option to btrfs subvol delete, causing it to pass the BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE flag through to libbtrfsutil.

This is a resubmission of part of a patch that Omar Sandoval sent in 2018, which appears to have been overlooked:
https://lore.kernel.org/linux-btrfs/e42cdc5d5287269faf4d09e8c9786d0b3adeb658.1516991902.git.osandov@fb.com/

---
 Documentation/btrfs-subvolume.rst |  4 ++++
 cmds/subvolume.c                  | 15 +++++++++++++--
 2 files changed, 17 insertions(+), 2 deletions(-)
Documentation/btrfs-subvolume.rst +4 -0
index d1e89f15..b1f22344 100644
@@ -112,6 +112,10 @@ delete [options] [<subvolume> [<subvolume>...]], delete -i|--subvolid <subvolid>
         -i|--subvolid <subvolid>
                 subvolume id to be removed instead of the <path> that should point to the
                 filesystem with the subvolume
+
+        -R|--recursive
+                delete subvolumes beneath each subvolume recursively
+
         -v|--verbose
                 (deprecated) alias for global *-v* option
 
cmds/subvolume.c +13 -3
index 52bc8850..b4151af2 100644
@@ -347,6 +347,7 @@ static const char * const cmd_subvolume_delete_usage[] = {
        OPTLINE("-c|--commit-after", "wait for transaction commit at the end of the operation"),
        OPTLINE("-C|--commit-each", "wait for transaction commit after deleting each subvolume"),
        OPTLINE("-i|--subvolid", "subvolume id of the to be removed subvolume"),
+       OPTLINE("-R|--recursive", "delete subvolumes beneath each subvolume recursively"),
        OPTLINE("-v|--verbose", "deprecated, alias for global -v option"),
        HELPINFO_INSERT_GLOBALS,
        HELPINFO_INSERT_VERBOSE,
@@ -367,6 +368,7 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
        char    *path = NULL;
        int commit_mode = 0;
        bool subvol_path_not_found = false;
+       int flags = 0;
        u8 fsid[BTRFS_FSID_SIZE];
        u64 subvolid = 0;
        char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
@@ -383,11 +385,12 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
                        {"commit-after", no_argument, NULL, 'c'},
                        {"commit-each", no_argument, NULL, 'C'},
                        {"subvolid", required_argument, NULL, 'i'},
+                       {"recursive", no_argument, NULL, 'R'},
                        {"verbose", no_argument, NULL, 'v'},
                        {NULL, 0, NULL, 0}
                };
 
-               c = getopt_long(argc, argv, "cCi:v", long_options, NULL);
+               c = getopt_long(argc, argv, "cCi:Rv", long_options, NULL);
                if (c < 0)
                        break;
 
@@ -401,6 +404,9 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
                case 'i':
                        subvolid = arg_strtou64(optarg);
                        break;
+               case 'R':
+                       flags |= BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE;
+                       break;
                case 'v':
                        bconf_be_verbose();
                        break;
@@ -416,6 +422,11 @@ static int cmd_subvolume_delete(const struct cmd_struct *cmd, int argc, char **a
        if (subvolid > 0 && check_argc_exact(argc - optind, 1))
                return 1;
 
+       if (subvolid > 0 && flags & BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE) {
+               error("option --recursive not supported with --subvolid");
+               return 1;
+       }
+
        pr_verbose(LOG_INFO, "Transaction commit: %s\n",
                   !commit_mode ? "none (default)" :
                   commit_mode == COMMIT_AFTER ? "at the end" : "after each");
@@ -528,7 +539,7 @@ again:
 
        /* Start deleting. */
        if (subvolid == 0)
-               err = btrfs_util_delete_subvolume_fd(fd, vname, 0);
+               err = btrfs_util_delete_subvolume_fd(fd, vname, flags);
        else
                err = btrfs_util_delete_subvolume_by_id_fd(fd, subvolid);
        if (err) {
--
Quoted 3 lines

2.44.2

Looks good to me.

Signature · 1 line
真実はいつも一つ!/ Always, there's only one truth!