Skip to contentSkip to conversation

Btrfs-progs: rework open_ctree to take flags, add a new one V2

PATCH New Josef Bacik 1 participant 1 message Oct 28, 2013 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs-progs: rework open_ctree to take flags, add a new one V2

So I needed to add a flag to not try to read block groups when doing --init-extent-tree since we could hang there, but that meant adding a whole other 0/1 type flag to open_ctree_fs_info. So instead I've converted it all over to using a flags setting and added the flag that I needed. This has been tested with xfstests and make test. Thanks,

---
V1->V2: forgot to make clean first, I missed converting one of the users

 btrfs-convert.c       |   6 +--
 btrfs-corrupt-block.c |   2 +-
 btrfs-debug-tree.c    |   2 +-
 btrfs-image.c         |   9 ++++-
 btrfs-zero-log.c      |   2 +-
 btrfstune.c           |   2 +-
 cmds-check.c          |  14 +++----
 cmds-restore.c        |   3 +-
 disk-io.c             | 107 ++++++++++++++------------------------------------
 disk-io.h             |  26 ++++++------
 mkfs.c                |   2 +-
 quick-test.c          |  10 ++---
 super-recover.c       |   4 +-
 utils.c               |   2 +-
 14 files changed, 77 insertions(+), 114 deletions(-)
btrfs-convert.c +3 -3
index 26c7b5f..ae10eed 100644
@@ -2256,7 +2256,7 @@ static int do_convert(const char *devname, int datacsum, int packing,
 		fprintf(stderr, "unable to update system chunk\n");
 		goto fail;
 	}
-	root = open_ctree_fd(fd, devname, super_bytenr, O_RDWR);
+	root = open_ctree_fd(fd, devname, super_bytenr, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "unable to open ctree\n");
 		goto fail;
@@ -2317,7 +2317,7 @@ static int do_convert(const char *devname, int datacsum, int packing,
 		goto fail;
 	}
 
-	root = open_ctree_fd(fd, devname, 0, O_RDWR);
+	root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "unable to open ctree\n");
 		goto fail;
@@ -2418,7 +2418,7 @@ static int do_rollback(const char *devname, int force)
 		fprintf(stderr, "unable to open %s\n", devname);
 		goto fail;
 	}
-	root = open_ctree_fd(fd, devname, 0, O_RDWR);
+	root = open_ctree_fd(fd, devname, 0, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "unable to open ctree\n");
 		goto fail;
btrfs-corrupt-block.c +1 -1
index b40a529..f0c14a9 100644
@@ -799,7 +799,7 @@ int main(int ac, char **av)
 	radix_tree_init();
 	cache_tree_init(&root_cache);
 
-	root = open_ctree(dev, 0, 1);
+	root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		exit(1);
btrfs-debug-tree.c +1 -1
index 4a9d89d..4a83770 100644
@@ -171,7 +171,7 @@ int main(int ac, char **av)
 	if (ac != 1)
 		print_usage();
 
-	info = open_ctree_fs_info(av[optind], 0, 0, 0, 1, 0);
+	info = open_ctree_fs_info(av[optind], 0, 0, OPEN_CTREE_PARTIAL);
 	if (!info) {
 		fprintf(stderr, "unable to open %s\n", av[optind]);
 		exit(1);
btrfs-image.c +7 -2
index 40ed483..7bcfc06 100644
@@ -2267,7 +2267,10 @@ static int __restore_metadump(const char *input, FILE *out, int old_restore,
 	/* NOTE: open with write mode */
 	if (fixup_offset) {
 		BUG_ON(!target);
-		info = open_ctree_fs_info_restore(target, 0, 0, 1, 1);
+		info = open_ctree_fs_info(target, 0, 0,
+					  OPEN_CTREE_WRITES |
+					  OPEN_CTREE_RESTORE |
+					  OPEN_CTREE_PARTIAL);
 		if (!info) {
 			fprintf(stderr, "%s: open ctree failed\n", __func__);
 			ret = -EIO;
@@ -2555,7 +2558,9 @@ int main(int argc, char *argv[])
 		u64 total_devs;
 		int i;
 
-		info = open_ctree_fs_info_restore(target, 0, 0, 0, 1);
+		info = open_ctree_fs_info(target, 0, 0,
+					  OPEN_CTREE_PARTIAL |
+					  OPEN_CTREE_RESTORE);
 		if (!info) {
 			int e = errno;
 			fprintf(stderr, "unable to open %s error = %s\n",
btrfs-zero-log.c +1 -1
index 432adff..ab7f418 100644
@@ -60,7 +60,7 @@ int main(int ac, char **av)
 		goto out;
 	}
 
-	root = open_ctree(av[1], 0, 1);
+	root = open_ctree(av[1], 0, OPEN_CTREE_WRITES);
 
 	if (root == NULL)
 		return 1;
btrfstune.c +1 -1
index 1cf6a68..50724ba 100644
@@ -148,7 +148,7 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	root = open_ctree(device, 0, 1);
+	root = open_ctree(device, 0, OPEN_CTREE_WRITES);
 
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
cmds-check.c +7 -7
index 8ed7baa..b27154f 100644
@@ -6047,13 +6047,12 @@ int cmd_check(int argc, char **argv)
 	struct btrfs_fs_info *info;
 	u64 bytenr = 0;
 	char uuidbuf[37];
-	int backup_root = 0;
 	int ret;
 	int num;
 	int option_index = 0;
 	int init_csum_tree = 0;
 	int init_extent_tree = 0;
-	int rw = 0;
+	enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_PARTIAL;
 
 	while(1) {
 		int c;
@@ -6064,7 +6063,7 @@ int cmd_check(int argc, char **argv)
 		switch(c) {
 			case 'a': /* ignored */ break;
 			case 'b':
-				backup_root = 1;
+				ctree_flags |= OPEN_CTREE_BACKUP_ROOT;
 				break;
 			case 's':
 				num = atol(optarg);
@@ -6079,14 +6078,15 @@ int cmd_check(int argc, char **argv)
 		if (option_index == 1) {
 			printf("enabling repair mode\n");
 			repair = 1;
-			rw = 1;
+			ctree_flags |= OPEN_CTREE_WRITES;
 		} else if (option_index == 2) {
 			printf("Creating a new CRC tree\n");
 			init_csum_tree = 1;
-			rw = 1;
+			ctree_flags |= OPEN_CTREE_WRITES;
 		} else if (option_index == 3) {
 			init_extent_tree = 1;
-			rw = 1;
+			ctree_flags |= (OPEN_CTREE_WRITES |
+					OPEN_CTREE_NO_BLOCK_GROUPS);
 			repair = 1;
 		}
 
@@ -6107,7 +6107,7 @@ int cmd_check(int argc, char **argv)
 		return -EBUSY;
 	}
 
-	info = open_ctree_fs_info(argv[optind], bytenr, 0, rw, 1, backup_root);
+	info = open_ctree_fs_info(argv[optind], bytenr, 0, ctree_flags);
 	if (!info) {
 		fprintf(stderr, "Couldn't open file system\n");
 		return -EIO;
cmds-restore.c +2 -1
index ae0527b..1748262 100644
@@ -974,7 +974,8 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location,
 
 	for (i = super_mirror; i < BTRFS_SUPER_MIRROR_MAX; i++) {
 		bytenr = btrfs_sb_offset(i);
-		fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0, 1);
+		fs_info = open_ctree_fs_info(dev, bytenr, root_location,
+					     OPEN_CTREE_PARTIAL);
 		if (fs_info)
 			break;
 		fprintf(stderr, "Could not open root, trying backup super\n");
disk-io.c +30 -77

218 lines not on this page · raw message

disk-io.h +15 -11
index b0292db..ca6af2d 100644
@@ -25,6 +25,15 @@
 #define BTRFS_SUPER_MIRROR_MAX	 3
 #define BTRFS_SUPER_MIRROR_SHIFT 12
 
+enum btrfs_open_ctree_flags {
+	OPEN_CTREE_WRITES		= 1,
+	OPEN_CTREE_PARTIAL		= 2,
+	OPEN_CTREE_BACKUP_ROOT		= 4,
+	OPEN_CTREE_RECOVER_SUPER	= 8,
+	OPEN_CTREE_RESTORE		= 16,
+	OPEN_CTREE_NO_BLOCK_GROUPS	= 32,
+};
+
 static inline u64 btrfs_sb_offset(int mirror)
 {
 	u64 start = 16 * 1024;
@@ -52,8 +61,8 @@ int clean_tree_block(struct btrfs_trans_handle *trans,
 void btrfs_free_fs_info(struct btrfs_fs_info *fs_info);
 struct btrfs_fs_info *btrfs_new_fs_info(int writable, u64 sb_bytenr);
 int btrfs_check_fs_compatibility(struct btrfs_super_block *sb, int writable);
-int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info,
-			  u64 root_tree_bytenr, int partial, int backup_root);
+int btrfs_setup_all_roots(struct btrfs_fs_info *fs_info, u64 root_tree_bytenr,
+			  enum btrfs_open_ctree_flags flags);
 void btrfs_release_all_roots(struct btrfs_fs_info *fs_info);
 void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info);
 int btrfs_scan_fs_devices(int fd, const char *path,
@@ -61,18 +70,13 @@ int btrfs_scan_fs_devices(int fd, const char *path,
 			  int run_ioctl);
 int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info);
 
-struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr, int writes);
+struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
+			      enum btrfs_open_ctree_flags flags);
 struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
-				 int writes);
-struct btrfs_fs_info *open_ctree_fs_info_restore(const char *filename,
-					 u64 sb_bytenr, u64 root_tree_bytenr,
-					 int writes, int partial);
+				 enum btrfs_open_ctree_flags flags);
 struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
 					 u64 sb_bytenr, u64 root_tree_bytenr,
-					 int writes, int partial,
-					 int backup_root);
-struct btrfs_root *open_ctree_with_broken_super(const char *filename,
-					u64 sb_bytenr, int writes);
+					 enum btrfs_open_ctree_flags flags);
 int close_ctree(struct btrfs_root *root);
 int write_all_supers(struct btrfs_root *root);
 int write_ctree_super(struct btrfs_trans_handle *trans,
mkfs.c +1 -1
index 770a4c0..ec717be 100644
@@ -1478,7 +1478,7 @@ int main(int ac, char **av)
 		exit(1);
 	}
 
-	root = open_ctree(file, 0, O_RDWR);
+	root = open_ctree(file, 0, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		close(fd);
quick-test.c +5 -5
index b12b9ef..5dfb2fe 100644
@@ -51,7 +51,7 @@ int main(int ac, char **av) {
 
 	radix_tree_init();
 
-	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
+	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		exit(1);
@@ -78,7 +78,7 @@ int main(int ac, char **av) {
 	btrfs_commit_transaction(trans, root);
 	close_ctree(root);
 	exit(1);
-	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
+	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		exit(1);
@@ -101,7 +101,7 @@ int main(int ac, char **av) {
 	}
 	close_ctree(root);
 
-	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
+	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		exit(1);
@@ -133,7 +133,7 @@ int main(int ac, char **av) {
 	btrfs_commit_transaction(trans, root);
 	close_ctree(root);
 
-	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
+	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		exit(1);
@@ -153,7 +153,7 @@ int main(int ac, char **av) {
 	btrfs_commit_transaction(trans, root);
 	close_ctree(root);
 
-	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, O_RDWR);
+	root = open_ctree(av[1], BTRFS_SUPER_INFO_OFFSET, OPEN_CTREE_WRITES);
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
 		exit(1);
super-recover.c +2 -2
index 5920ea6..a6271a2 100644
@@ -323,8 +323,8 @@ int btrfs_recover_superblocks(const char *dname,
 		}
 	}
 	record = recover_get_good_super(&recover);
-	root = open_ctree_with_broken_super(record->device_name,
-					record->bytenr, 1);
+	root = open_ctree(record->device_name, record->bytenr,
+			  OPEN_CTREE_RECOVER_SUPER | OPEN_CTREE_WRITES);
 	if (!root) {
 		ret = 3;
 		goto no_recover;
utils.c +1 -1
index f8bc94f..0b23af4 100644
@@ -1279,7 +1279,7 @@ static int set_label_unmounted(const char *dev, const char *label)
 	/* Open the super_block at the default location
 	 * and as read-write.
 	 */
-	root = open_ctree(dev, 0, 1);
+	root = open_ctree(dev, 0, OPEN_CTREE_WRITES);
 	if (!root) /* errors are printed by open_ctree() */
 		return -1;
 
Signature · 1 line
1.8.3.1