linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Omar Sandoval <osandov@osandov.com>
To: linux-btrfs@vger.kernel.org
Cc: kernel-team@fb.com
Subject: [PATCH v2 23/27] btrfs-progs: use libbtrfsutil for subvol sync
Date: Thu, 15 Feb 2018 11:05:08 -0800	[thread overview]
Message-ID: <f39e48b3ed0f860ec47b31fbfe08f6455d6fbaa8.1518720598.git.osandov@fb.com> (raw)
In-Reply-To: <cover.1518720598.git.osandov@fb.com>
In-Reply-To: <cover.1518720598.git.osandov@fb.com>

From: Omar Sandoval <osandov@fb.com>

btrfs_util_f_deleted_subvolumes() replaces enumerate_dead_subvols() and
btrfs_util_f_subvolume_info() replaces is_subvolume_cleaned().

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 cmds-subvolume.c | 217 ++++++-------------------------------------------------
 1 file changed, 21 insertions(+), 196 deletions(-)

diff --git a/cmds-subvolume.c b/cmds-subvolume.c
index 49c9c8cf..9bab9312 100644
--- a/cmds-subvolume.c
+++ b/cmds-subvolume.c
@@ -42,38 +42,11 @@
 #include "utils.h"
 #include "help.h"
 
-static int is_subvolume_cleaned(int fd, u64 subvolid)
+static int wait_for_subvolume_cleaning(int fd, size_t count, uint64_t *ids,
+				       int sleep_interval)
 {
-	int ret;
-	struct btrfs_ioctl_search_args args;
-	struct btrfs_ioctl_search_key *sk = &args.key;
-
-	sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
-	sk->min_objectid = subvolid;
-	sk->max_objectid = subvolid;
-	sk->min_type = BTRFS_ROOT_ITEM_KEY;
-	sk->max_type = BTRFS_ROOT_ITEM_KEY;
-	sk->min_offset = 0;
-	sk->max_offset = (u64)-1;
-	sk->min_transid = 0;
-	sk->max_transid = (u64)-1;
-	sk->nr_items = 1;
-
-	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
-	if (ret < 0)
-		return -errno;
-
-	if (sk->nr_items == 0)
-		return 1;
-
-	return 0;
-}
-
-static int wait_for_subvolume_cleaning(int fd, int count, u64 *ids,
-		int sleep_interval)
-{
-	int ret;
-	int i;
+	size_t i;
+	enum btrfs_util_error err;
 
 	while (1) {
 		int clean = 1;
@@ -81,16 +54,14 @@ static int wait_for_subvolume_cleaning(int fd, int count, u64 *ids,
 		for (i = 0; i < count; i++) {
 			if (!ids[i])
 				continue;
-			ret = is_subvolume_cleaned(fd, ids[i]);
-			if (ret < 0) {
-				error(
-			    "cannot read status of dead subvolume %llu: %s",
-					(unsigned long long)ids[i], strerror(-ret));
-				return ret;
-			}
-			if (ret) {
-				printf("Subvolume id %llu is gone\n", ids[i]);
+			err = btrfs_util_subvolume_info_fd(fd, ids[i], NULL);
+			if (err == BTRFS_UTIL_ERROR_SUBVOLUME_NOT_FOUND) {
+				printf("Subvolume id %" PRIu64 " is gone\n",
+				       ids[i]);
 				ids[i] = 0;
+			} else if (err) {
+				error_btrfs_util(err);
+				return -errno;
 			} else {
 				clean = 0;
 			}
@@ -1028,160 +999,15 @@ static const char * const cmd_subvol_sync_usage[] = {
 	NULL
 };
 
-#if 0
-/*
- * If we're looking for any dead subvolume, take a shortcut and look
- * for any ORPHAN_ITEMs in the tree root
- */
-static int fs_has_dead_subvolumes(int fd)
-{
-	int ret;
-	struct btrfs_ioctl_search_args args;
-	struct btrfs_ioctl_search_key *sk = &args.key;
-	struct btrfs_ioctl_search_header sh;
-	u64 min_subvolid = 0;
-
-again:
-	sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
-	sk->min_objectid = BTRFS_ORPHAN_OBJECTID;
-	sk->max_objectid = BTRFS_ORPHAN_OBJECTID;
-	sk->min_type = BTRFS_ORPHAN_ITEM_KEY;
-	sk->max_type = BTRFS_ORPHAN_ITEM_KEY;
-	sk->min_offset = min_subvolid;
-	sk->max_offset = (u64)-1;
-	sk->min_transid = 0;
-	sk->max_transid = (u64)-1;
-	sk->nr_items = 1;
-
-	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
-	if (ret < 0)
-		return -errno;
-
-	if (!sk->nr_items)
-		return 0;
-
-	memcpy(&sh, args.buf, sizeof(sh));
-	min_subvolid = sh.offset;
-
-	/*
-	 * Verify that the root item is really there and we haven't hit
-	 * a stale orphan
-	 */
-	sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
-	sk->min_objectid = min_subvolid;
-	sk->max_objectid = min_subvolid;
-	sk->min_type = BTRFS_ROOT_ITEM_KEY;
-	sk->max_type = BTRFS_ROOT_ITEM_KEY;
-	sk->min_offset = 0;
-	sk->max_offset = (u64)-1;
-	sk->min_transid = 0;
-	sk->max_transid = (u64)-1;
-	sk->nr_items = 1;
-
-	ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
-	if (ret < 0)
-		return -errno;
-
-	/*
-	 * Stale orphan, try the next one
-	 */
-	if (!sk->nr_items) {
-		min_subvolid++;
-		goto again;
-	}
-
-	return 1;
-}
-#endif
-
-#define SUBVOL_ID_BATCH		1024
-
-/*
- * Enumerate all dead subvolumes that exist in the filesystem.
- * Fill @ids and reallocate to bigger size if needed.
- */
-static int enumerate_dead_subvols(int fd, u64 **ids)
-{
-	int ret;
-	struct btrfs_ioctl_search_args args;
-	struct btrfs_ioctl_search_key *sk = &args.key;
-	int idx = 0;
-	int count = 0;
-
-	memset(&args, 0, sizeof(args));
-
-	sk->tree_id = BTRFS_ROOT_TREE_OBJECTID;
-	sk->min_objectid = BTRFS_ORPHAN_OBJECTID;
-	sk->max_objectid = BTRFS_ORPHAN_OBJECTID;
-	sk->min_type = BTRFS_ORPHAN_ITEM_KEY;
-	sk->max_type = BTRFS_ORPHAN_ITEM_KEY;
-	sk->min_offset = 0;
-	sk->max_offset = (u64)-1;
-	sk->min_transid = 0;
-	sk->max_transid = (u64)-1;
-	sk->nr_items = 4096;
-
-	*ids = NULL;
-	while (1) {
-		struct btrfs_ioctl_search_header *sh;
-		unsigned long off;
-		int i;
-
-		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
-		if (ret < 0)
-			return -errno;
-
-		if (!sk->nr_items)
-			return idx;
-
-		off = 0;
-		for (i = 0; i < sk->nr_items; i++) {
-			sh = (struct btrfs_ioctl_search_header*)(args.buf + off);
-			off += sizeof(*sh);
-
-			if (btrfs_search_header_type(sh)
-			    == BTRFS_ORPHAN_ITEM_KEY) {
-				if (idx >= count) {
-					u64 *newids;
-
-					count += SUBVOL_ID_BATCH;
-					newids = (u64*)realloc(*ids,
-							count * sizeof(u64));
-					if (!newids)
-						return -ENOMEM;
-					*ids = newids;
-				}
-				(*ids)[idx] = btrfs_search_header_offset(sh);
-				idx++;
-			}
-			off += btrfs_search_header_len(sh);
-
-			sk->min_objectid = btrfs_search_header_objectid(sh);
-			sk->min_type = btrfs_search_header_type(sh);
-			sk->min_offset = btrfs_search_header_offset(sh);
-		}
-		if (sk->min_offset < (u64)-1)
-			sk->min_offset++;
-		else
-			break;
-		if (sk->min_type != BTRFS_ORPHAN_ITEM_KEY)
-			break;
-		if (sk->min_objectid != BTRFS_ORPHAN_OBJECTID)
-			break;
-	}
-
-	return idx;
-}
-
 static int cmd_subvol_sync(int argc, char **argv)
 {
 	int fd = -1;
-	int i;
 	int ret = 1;
 	DIR *dirstream = NULL;
-	u64 *ids = NULL;
-	int id_count;
+	uint64_t *ids;
+	size_t id_count, i;
 	int sleep_interval = 1;
+	enum btrfs_util_error err;
 
 	while (1) {
 		int c = getopt(argc, argv, "s:");
@@ -1215,10 +1041,9 @@ static int cmd_subvol_sync(int argc, char **argv)
 
 	id_count = argc - optind;
 	if (!id_count) {
-		id_count = enumerate_dead_subvols(fd, &ids);
-		if (id_count < 0) {
-			error("can't enumerate dead subvolumes: %s",
-					strerror(-id_count));
+		err = btrfs_util_deleted_subvolumes_fd(fd, &ids, &id_count);
+		if (err) {
+			error_btrfs_util(err);
 			ret = 1;
 			goto out;
 		}
@@ -1227,7 +1052,7 @@ static int cmd_subvol_sync(int argc, char **argv)
 			goto out;
 		}
 	} else {
-		ids = (u64*)malloc(id_count * sizeof(u64));
+		ids = malloc(id_count * sizeof(uint64_t));
 		if (!ids) {
 			error("not enough memory");
 			ret = 1;
@@ -1241,13 +1066,13 @@ static int cmd_subvol_sync(int argc, char **argv)
 			arg = argv[optind + i];
 			errno = 0;
 			id = strtoull(arg, NULL, 10);
-			if (errno < 0) {
+			if (errno) {
 				error("unrecognized subvolume id %s", arg);
 				ret = 1;
 				goto out;
 			}
-			if (id < BTRFS_FIRST_FREE_OBJECTID
-					|| id > BTRFS_LAST_FREE_OBJECTID) {
+			if (id < BTRFS_FIRST_FREE_OBJECTID ||
+			    id > BTRFS_LAST_FREE_OBJECTID) {
 				error("subvolume id %s out of range", arg);
 				ret = 1;
 				goto out;
-- 
2.16.1


  parent reply	other threads:[~2018-02-15 19:05 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-15 19:04 [PATCH v2 00/27] btrfs-progs: introduce libbtrfsutil, "btrfs-progs as a library" Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 01/27] btrfs-progs: get rid of undocumented qgroup inheritance options Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 02/27] Add libbtrfsutil Omar Sandoval
2018-02-20 17:32   ` Liu Bo
2018-02-20 18:34     ` David Sterba
2018-02-15 19:04 ` [PATCH v2 03/27] libbtrfsutil: add Python bindings Omar Sandoval
2018-02-21 13:47   ` David Sterba
2018-02-21 18:08     ` Omar Sandoval
2018-02-22  1:44   ` Misono, Tomohiro
2018-02-15 19:04 ` [PATCH v2 04/27] libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id() Omar Sandoval
2018-02-21 11:43   ` David Sterba
2018-02-21 13:02     ` David Sterba
2018-02-21 18:13       ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 05/27] libbtrfsutil: add qgroup inheritance helpers Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 06/27] libbtrfsutil: add btrfs_util_create_subvolume() Omar Sandoval
2018-02-23  8:24   ` Misono, Tomohiro
2018-02-23 22:58     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 07/27] libbtrfsutil: add btrfs_util_subvolume_path() Omar Sandoval
2018-02-23  6:27   ` Misono, Tomohiro
2018-02-23 22:44     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 08/27] libbtrfsutil: add btrfs_util_subvolume_info() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 09/27] libbtrfsutil: add btrfs_util_[gs]et_read_only() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 10/27] libbtrfsutil: add btrfs_util_[gs]et_default_subvolume() Omar Sandoval
2018-02-22  1:55   ` Misono, Tomohiro
2018-02-23 22:40     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 11/27] libbtrfsutil: add subvolume iterator helpers Omar Sandoval
2018-02-23  7:40   ` Misono, Tomohiro
2018-02-23 22:49     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 12/27] libbtrfsutil: add btrfs_util_create_snapshot() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 13/27] libbtrfsutil: add btrfs_util_delete_subvolume() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 14/27] libbtrfsutil: add btrfs_util_deleted_subvolumes() Omar Sandoval
2018-02-23  2:12   ` Misono, Tomohiro
2018-02-23 23:33     ` Omar Sandoval
2018-02-28  4:11       ` Misono, Tomohiro
2018-02-15 19:05 ` [PATCH v2 15/27] libbtrfsutil: add filesystem sync helpers Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 16/27] btrfs-progs: use libbtrfsutil for read-only property Omar Sandoval
2018-02-22  4:23   ` Misono, Tomohiro
2018-02-23 22:41     ` Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 17/27] btrfs-progs: use libbtrfsutil for sync ioctls Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 18/27] btrfs-progs: use libbtrfsutil for set-default Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 19/27] btrfs-progs: use libbtrfsutil for get-default Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 20/27] btrfs-progs: use libbtrfsutil for subvol create and snapshot Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 21/27] btrfs-progs: use libbtrfsutil for subvol delete Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 22/27] btrfs-progs: use libbtrfsutil for subvol show Omar Sandoval
2018-02-15 19:05 ` Omar Sandoval [this message]
2018-02-22  2:03   ` [PATCH v2 23/27] btrfs-progs: use libbtrfsutil for subvol sync Misono, Tomohiro
2018-02-23 22:41     ` Omar Sandoval
2018-02-23 23:22       ` David Sterba
2018-02-22  2:09   ` Misono, Tomohiro
2018-02-15 19:05 ` [PATCH v2 24/27] btrfs-progs: replace test_issubvolume() with btrfs_util_is_subvolume() Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 25/27] btrfs-progs: add recursive snapshot/delete using libbtrfsutil Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 26/27] btrfs-progs: use libbtrfsutil for subvolume list Omar Sandoval
2018-02-23  2:26   ` Misono, Tomohiro
2018-02-23 23:05     ` Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 27/27] btrfs-progs: deprecate libbtrfs helpers Omar Sandoval
2018-02-21 15:04   ` David Sterba
2018-02-21 18:19     ` Omar Sandoval
2018-02-20 18:50 ` [PATCH v2 00/27] btrfs-progs: introduce libbtrfsutil, "btrfs-progs as a library" David Sterba
2018-02-21 15:13   ` David Sterba
2018-02-21 18:50     ` Omar Sandoval
2018-02-23 20:28       ` David Sterba
2018-02-26 23:36         ` Omar Sandoval
2018-02-27 15:04           ` David Sterba
2018-02-27 20:48             ` Omar Sandoval

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=f39e48b3ed0f860ec47b31fbfe08f6455d6fbaa8.1518720598.git.osandov@fb.com \
    --to=osandov@osandov.com \
    --cc=kernel-team@fb.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 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).