btrfs-progs: factor open_ctree parameters to a structure

Extending open_ctree with more parameters would be difficult, we'll need
to add more so factor out the parameters to a structure for easier
extension.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2021-02-18 01:27:21 +01:00
parent f0d226fa48
commit 6bb6d1215d
10 changed files with 77 additions and 49 deletions

View File

@ -339,6 +339,7 @@ int main(int argc, char **argv)
struct btrfs_find_root_filter filter = {0};
struct cache_tree result;
struct cache_extent *found;
struct open_ctree_flags ocf = { 0 };
int ret;
/* Default to search root tree */
@ -381,9 +382,9 @@ int main(int argc, char **argv)
if (check_argc_min(argc - optind, 1))
return 1;
fs_info = open_ctree_fs_info(argv[optind], 0, 0, 0,
OPEN_CTREE_CHUNK_ROOT_ONLY |
OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
ocf.filename = argv[optind];
ocf.flags = OPEN_CTREE_CHUNK_ROOT_ONLY | OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR;
fs_info = open_ctree_fs_info(&ocf);
if (!fs_info) {
error("open ctree failed");
return 1;

View File

@ -10182,6 +10182,7 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
{
struct cache_tree root_cache;
struct btrfs_root *root;
struct open_ctree_flags ocf = { 0 };
u64 bytenr = 0;
u64 subvolid = 0;
u64 tree_root_bytenr = 0;
@ -10401,8 +10402,12 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
if (repair)
ctree_flags |= OPEN_CTREE_PARTIAL;
gfs_info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
chunk_root_bytenr, ctree_flags);
ocf.filename = argv[optind];
ocf.sb_bytenr = bytenr;
ocf.root_tree_bytenr = tree_root_bytenr;
ocf.chunk_tree_bytenr = chunk_root_bytenr;
ocf.flags = ctree_flags;
gfs_info = open_ctree_fs_info(&ocf);
if (!gfs_info) {
error("cannot open file system");
ret = -EIO;

View File

@ -574,6 +574,8 @@ static int map_seed_devices(struct list_head *all_uuids)
fs_uuids = btrfs_scanned_uuids();
list_for_each_entry(cur_fs, all_uuids, list) {
struct open_ctree_flags ocf = { 0 };
device = list_first_entry(&cur_fs->devices,
struct btrfs_device, dev_list);
if (!device)
@ -586,8 +588,9 @@ static int map_seed_devices(struct list_head *all_uuids)
/*
* open_ctree_* detects seed/sprout mapping
*/
fs_info = open_ctree_fs_info(device->name, 0, 0, 0,
OPEN_CTREE_PARTIAL);
ocf.filename = device->name;
ocf.flags = OPEN_CTREE_PARTIAL;
fs_info = open_ctree_fs_info(&ocf);
if (!fs_info)
continue;

View File

@ -302,6 +302,7 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
struct btrfs_disk_key disk_key;
struct btrfs_key found_key;
struct cache_tree block_root; /* for multiple --block parameters */
struct open_ctree_flags ocf = { 0 };
char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
int ret = 0;
int slot;
@ -459,7 +460,9 @@ static int cmd_inspect_dump_tree(const struct cmd_struct *cmd,
printf("%s\n", PACKAGE_STRING);
info = open_ctree_fs_info(argv[optind], 0, 0, 0, open_ctree_flags);
ocf.filename = argv[optind];
ocf.flags = open_ctree_flags;
info = open_ctree_fs_info(&ocf);
if (!info) {
error("unable to open %s", argv[optind]);
goto out;

View File

@ -228,6 +228,7 @@ static int cmd_rescue_fix_device_size(const struct cmd_struct *cmd,
int argc, char **argv)
{
struct btrfs_fs_info *fs_info;
struct open_ctree_flags ocf = { 0 };
char *devname;
int ret;
@ -248,8 +249,9 @@ static int cmd_rescue_fix_device_size(const struct cmd_struct *cmd,
goto out;
}
fs_info = open_ctree_fs_info(devname, 0, 0, 0, OPEN_CTREE_WRITES |
OPEN_CTREE_PARTIAL);
ocf.filename = devname;
ocf.flags = OPEN_CTREE_WRITES | OPEN_CTREE_PARTIAL;
fs_info = open_ctree_fs_info(&ocf);
if (!fs_info) {
error("could not open btrfs");
ret = -EIO;

View File

@ -1258,6 +1258,7 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location,
{
struct btrfs_fs_info *fs_info = NULL;
struct btrfs_root *root = NULL;
struct open_ctree_flags ocf = { 0 };
u64 bytenr;
int i;
@ -1269,9 +1270,11 @@ static struct btrfs_root *open_fs(const char *dev, u64 root_location,
* in extent tree. Skip block group item search will allow
* restore to be executed on heavily damaged fs.
*/
fs_info = open_ctree_fs_info(dev, bytenr, root_location, 0,
OPEN_CTREE_PARTIAL |
OPEN_CTREE_NO_BLOCK_GROUPS);
ocf.filename = dev;
ocf.sb_bytenr = bytenr;
ocf.root_tree_bytenr = root_location;
ocf.flags = OPEN_CTREE_PARTIAL | OPEN_CTREE_NO_BLOCK_GROUPS;
fs_info = open_ctree_fs_info(&ocf);
if (fs_info)
break;
fprintf(stderr, "Could not open root, trying backup super\n");

View File

@ -2627,10 +2627,11 @@ static int restore_metadump(const char *input, FILE *out, int old_restore,
/* NOTE: open with write mode */
if (fixup_offset) {
info = open_ctree_fs_info(target, 0, 0, 0,
OPEN_CTREE_WRITES |
OPEN_CTREE_RESTORE |
OPEN_CTREE_PARTIAL);
struct open_ctree_flags ocf = { 0 };
ocf.filename = target;
ocf.flags = OPEN_CTREE_WRITES | OPEN_CTREE_RESTORE | OPEN_CTREE_PARTIAL;
info = open_ctree_fs_info(&ocf);
if (!info) {
error("open ctree failed");
ret = -EIO;
@ -2989,13 +2990,14 @@ int BOX_MAIN(image)(int argc, char *argv[])
/* extended support for multiple devices */
if (!create && multi_devices) {
struct open_ctree_flags ocf = { 0 };
struct btrfs_fs_info *info;
u64 total_devs;
int i;
info = open_ctree_fs_info(target, 0, 0, 0,
OPEN_CTREE_PARTIAL |
OPEN_CTREE_RESTORE);
ocf.filename = target;
ocf.flags = OPEN_CTREE_PARTIAL | OPEN_CTREE_RESTORE;
info = open_ctree_fs_info(&ocf);
if (!info) {
error("open ctree failed at %s", target);
return 1;

View File

@ -1207,11 +1207,7 @@ int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
return 0;
}
static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
u64 sb_bytenr,
u64 root_tree_bytenr,
u64 chunk_root_bytenr,
unsigned flags)
static struct btrfs_fs_info *__open_ctree_fd(int fp, struct open_ctree_flags *ocf)
{
struct btrfs_fs_info *fs_info;
struct btrfs_super_block *disk_super;
@ -1220,6 +1216,8 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
int ret;
int oflags;
unsigned sbflags = SBREAD_DEFAULT;
unsigned flags = ocf->flags;
u64 sb_bytenr = ocf->sb_bytenr;
if (sb_bytenr == 0)
sb_bytenr = BTRFS_SUPER_INFO_OFFSET;
@ -1257,8 +1255,8 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
if (flags & OPEN_CTREE_IGNORE_FSID_MISMATCH)
sbflags |= SBREAD_IGNORE_FSID_MISMATCH;
ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr, sbflags,
(flags & OPEN_CTREE_NO_DEVICES));
ret = btrfs_scan_fs_devices(fp, ocf->filename, &fs_devices, sb_bytenr,
sbflags, (flags & OPEN_CTREE_NO_DEVICES));
if (ret)
goto out;
@ -1306,7 +1304,7 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
if (ret)
goto out_devices;
ret = btrfs_setup_chunk_tree_and_device_map(fs_info, chunk_root_bytenr);
ret = btrfs_setup_chunk_tree_and_device_map(fs_info, ocf->chunk_tree_bytenr);
if (ret)
goto out_chunk;
@ -1319,7 +1317,7 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
btrfs_header_chunk_tree_uuid(eb),
BTRFS_UUID_SIZE);
ret = btrfs_setup_all_roots(fs_info, root_tree_bytenr, flags);
ret = btrfs_setup_all_roots(fs_info, ocf->root_tree_bytenr, flags);
if (ret && !(flags & __OPEN_CTREE_RETURN_CHUNK_ROOT) &&
!fs_info->ignore_chunk_tree_error)
goto out_chunk;
@ -1336,10 +1334,7 @@ out:
return NULL;
}
struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
u64 sb_bytenr, u64 root_tree_bytenr,
u64 chunk_root_bytenr,
unsigned flags)
struct btrfs_fs_info *open_ctree_fs_info(struct open_ctree_flags *ocf)
{
int fp;
int ret;
@ -1347,26 +1342,25 @@ struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
int oflags = O_RDWR;
struct stat st;
ret = stat(filename, &st);
ret = stat(ocf->filename, &st);
if (ret < 0) {
error("cannot stat '%s': %m", filename);
error("cannot stat '%s': %m", ocf->filename);
return NULL;
}
if (!(((st.st_mode & S_IFMT) == S_IFREG) || ((st.st_mode & S_IFMT) == S_IFBLK))) {
error("not a regular file or block device: %s", filename);
error("not a regular file or block device: %s", ocf->filename);
return NULL;
}
if (!(flags & OPEN_CTREE_WRITES))
if (!(ocf->flags & OPEN_CTREE_WRITES))
oflags = O_RDONLY;
fp = open(filename, oflags);
fp = open(ocf->filename, oflags);
if (fp < 0) {
error("cannot open '%s': %m", filename);
error("cannot open '%s': %m", ocf->filename);
return NULL;
}
info = __open_ctree_fd(fp, filename, sb_bytenr, root_tree_bytenr,
chunk_root_bytenr, flags);
info = __open_ctree_fd(fp, ocf);
close(fp);
return info;
}
@ -1375,10 +1369,14 @@ struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
unsigned flags)
{
struct btrfs_fs_info *info;
struct open_ctree_flags ocf = { 0 };
/* This flags may not return fs_info with any valid root */
BUG_ON(flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR);
info = open_ctree_fs_info(filename, sb_bytenr, 0, 0, flags);
ocf.filename = filename;
ocf.sb_bytenr = sb_bytenr;
ocf.flags = flags;
info = open_ctree_fs_info(&ocf);
if (!info)
return NULL;
if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)
@ -1390,6 +1388,7 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
unsigned flags)
{
struct btrfs_fs_info *info;
struct open_ctree_flags ocf = { 0 };
/* This flags may not return fs_info with any valid root */
if (flags & OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR) {
@ -1397,7 +1396,10 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
(unsigned long long)flags);
return NULL;
}
info = __open_ctree_fd(fp, path, sb_bytenr, 0, 0, flags);
ocf.filename = path;
ocf.sb_bytenr = sb_bytenr;
ocf.flags = flags;
info = __open_ctree_fd(fp, &ocf);
if (!info)
return NULL;
if (flags & __OPEN_CTREE_RETURN_CHUNK_ROOT)

View File

@ -160,10 +160,15 @@ struct btrfs_root *open_ctree(const char *filename, u64 sb_bytenr,
unsigned flags);
struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
unsigned flags);
struct btrfs_fs_info *open_ctree_fs_info(const char *filename,
u64 sb_bytenr, u64 root_tree_bytenr,
u64 chunk_root_bytenr,
unsigned flags);
struct open_ctree_flags {
const char *filename;
u64 sb_bytenr;
u64 root_tree_bytenr;
u64 chunk_tree_bytenr;
unsigned flags;
};
struct btrfs_fs_info *open_ctree_fs_info(struct open_ctree_flags *ocf);
int close_ctree_fs_info(struct btrfs_fs_info *fs_info);
static inline int close_ctree(struct btrfs_root *root)
{

View File

@ -917,6 +917,7 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
struct btrfs_root *root;
struct btrfs_fs_info *fs_info;
struct btrfs_trans_handle *trans;
struct open_ctree_flags ocf = { 0 };
char *label = NULL;
u64 block_count = 0;
u64 dev_block_count = 0;
@ -1353,8 +1354,9 @@ int BOX_MAIN(mkfs)(int argc, char **argv)
goto error;
}
fs_info = open_ctree_fs_info(file, 0, 0, 0,
OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER);
ocf.filename = file;
ocf.flags = OPEN_CTREE_WRITES | OPEN_CTREE_TEMPORARY_SUPER;
fs_info = open_ctree_fs_info(&ocf);
if (!fs_info) {
error("open ctree failed");
goto error;