btrfs-progs: make superblock reading/scanning api more generic

We'll add more modes that affect scanning.

Signed-off-by: David Sterba <dsterba@suse.com>
master
David Sterba 2016-08-19 16:36:40 +02:00
parent c11bd9cfd2
commit 059832da5f
8 changed files with 38 additions and 24 deletions

View File

@ -1470,7 +1470,8 @@ open_ctree_with_broken_chunk(struct recover_control *rc)
disk_super = fs_info->super_copy;
ret = btrfs_read_dev_super(fs_info->fs_devices->latest_bdev,
disk_super, fs_info->super_bytenr, 1);
disk_super, fs_info->super_bytenr,
SBREAD_RECOVER);
if (ret) {
fprintf(stderr, "No valid btrfs found\n");
goto out_devices;
@ -1531,7 +1532,8 @@ static int recover_prepare(struct recover_control *rc, char *path)
}
sb = (struct btrfs_super_block*)buf;
ret = btrfs_read_dev_super(fd, sb, BTRFS_SUPER_INFO_OFFSET, 1);
ret = btrfs_read_dev_super(fd, sb, BTRFS_SUPER_INFO_OFFSET,
SBREAD_RECOVER);
if (ret) {
fprintf(stderr, "read super block error\n");
goto out_close_fd;
@ -1550,7 +1552,7 @@ static int recover_prepare(struct recover_control *rc, char *path)
goto out_close_fd;
}
ret = btrfs_scan_fs_devices(fd, path, &fs_devices, 0, 1, 0);
ret = btrfs_scan_fs_devices(fd, path, &fs_devices, 0, SBREAD_RECOVER, 0);
if (ret)
goto out_close_fd;

View File

@ -524,7 +524,7 @@ static int dev_to_fsid(char *dev, __u8 *fsid)
disk_super = (struct btrfs_super_block *)buf;
ret = btrfs_read_dev_super(fd, disk_super,
BTRFS_SUPER_INFO_OFFSET, 0);
BTRFS_SUPER_INFO_OFFSET, SBREAD_DEFAULT);
if (ret)
goto out;

View File

@ -1114,7 +1114,7 @@ void btrfs_cleanup_all_caches(struct btrfs_fs_info *fs_info)
int btrfs_scan_fs_devices(int fd, const char *path,
struct btrfs_fs_devices **fs_devices,
u64 sb_bytenr, int super_recover,
u64 sb_bytenr, unsigned sbflags,
int skip_devices)
{
u64 total_devs;
@ -1136,7 +1136,7 @@ int btrfs_scan_fs_devices(int fd, const char *path,
}
ret = btrfs_scan_one_device(fd, path, fs_devices,
&total_devs, sb_bytenr, super_recover);
&total_devs, sb_bytenr, sbflags);
if (ret) {
fprintf(stderr, "No valid Btrfs found on %s\n", path);
return ret;
@ -1248,8 +1248,8 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
fs_info->ignore_chunk_tree_error = 1;
ret = btrfs_scan_fs_devices(fp, path, &fs_devices, sb_bytenr,
(flags & OPEN_CTREE_RECOVER_SUPER),
(flags & OPEN_CTREE_NO_DEVICES));
(flags & OPEN_CTREE_RECOVER_SUPER) ? SBREAD_RECOVER : SBREAD_DEFAULT,
(flags & OPEN_CTREE_NO_DEVICES));
if (ret)
goto out;
@ -1268,10 +1268,11 @@ static struct btrfs_fs_info *__open_ctree_fd(int fp, const char *path,
disk_super = fs_info->super_copy;
if (flags & OPEN_CTREE_RECOVER_SUPER)
ret = btrfs_read_dev_super(fs_devices->latest_bdev,
disk_super, sb_bytenr, 1);
ret = btrfs_read_dev_super(fs_devices->latest_bdev, disk_super,
sb_bytenr, SBREAD_RECOVER);
else
ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr, 0);
ret = btrfs_read_dev_super(fp, disk_super, sb_bytenr,
SBREAD_DEFAULT);
if (ret) {
printk("No valid btrfs found\n");
goto out_devices;
@ -1392,7 +1393,7 @@ struct btrfs_root *open_ctree_fd(int fp, const char *path, u64 sb_bytenr,
* - number of devices - something sane
* - sys array size - maximum
*/
static int check_super(struct btrfs_super_block *sb)
static int check_super(struct btrfs_super_block *sb, unsigned sbflags)
{
char result[BTRFS_CSUM_SIZE];
u32 crc;
@ -1533,7 +1534,7 @@ error_out:
}
int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
int super_recover)
unsigned sbflags)
{
u8 fsid[BTRFS_FSID_SIZE];
int fsid_is_initialized = 0;
@ -1541,7 +1542,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
struct btrfs_super_block *buf = (struct btrfs_super_block *)tmp;
int i;
int ret;
int max_super = super_recover ? BTRFS_SUPER_MIRROR_MAX : 1;
int max_super = sbflags & SBREAD_RECOVER ? BTRFS_SUPER_MIRROR_MAX : 1;
u64 transid = 0;
u64 bytenr;
@ -1553,7 +1554,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
if (btrfs_super_bytenr(buf) != sb_bytenr)
return -1;
if (check_super(buf))
if (check_super(buf, sbflags))
return -1;
memcpy(sb, buf, BTRFS_SUPER_INFO_SIZE);
return 0;
@ -1577,7 +1578,7 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
/* if magic is NULL, the device was removed */
if (btrfs_super_magic(buf) == 0 && i == 0)
break;
if (check_super(buf))
if (check_super(buf, sbflags))
continue;
if (!fsid_is_initialized) {

View File

@ -64,6 +64,15 @@ enum btrfs_open_ctree_flags {
OPEN_CTREE_IGNORE_CHUNK_TREE_ERROR = (1 << 11)
};
/*
* Modes of superblock access
*/
enum btrfs_read_sb_flags {
SBREAD_DEFAULT = 0,
/* Reading superblock during recovery */
SBREAD_RECOVER = (1 << 0),
};
static inline u64 btrfs_sb_offset(int mirror)
{
u64 start = 16 * 1024;
@ -108,7 +117,7 @@ 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,
struct btrfs_fs_devices **fs_devices, u64 sb_bytenr,
int super_recover, int skip_devices);
unsigned sbflags, int skip_devices);
int btrfs_setup_chunk_tree_and_device_map(struct btrfs_fs_info *fs_info,
u64 chunk_root_bytenr);
@ -132,7 +141,7 @@ int write_all_supers(struct btrfs_root *root);
int write_ctree_super(struct btrfs_trans_handle *trans,
struct btrfs_root *root);
int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
int super_recover);
unsigned sbflags);
int btrfs_map_bh_to_logical(struct btrfs_root *root, struct extent_buffer *bh,
u64 logical);
struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,

View File

@ -279,7 +279,8 @@ int btrfs_recover_superblocks(const char *dname,
}
init_recover_superblock(&recover);
ret = btrfs_scan_fs_devices(fd, dname, &recover.fs_devices, 0, 1, 0);
ret = btrfs_scan_fs_devices(fd, dname, &recover.fs_devices, 0,
SBREAD_RECOVER, 0);
close(fd);
if (ret) {
ret = 1;

View File

@ -2272,7 +2272,7 @@ int check_mounted_where(int fd, const char *file, char *where, int size,
/* scan the initial device */
ret = btrfs_scan_one_device(fd, file, &fs_devices_mnt,
&total_devs, BTRFS_SUPER_INFO_OFFSET, 0);
&total_devs, BTRFS_SUPER_INFO_OFFSET, SBREAD_DEFAULT);
is_btrfs = (ret >= 0);
/* scan other devices */
@ -3419,7 +3419,8 @@ int btrfs_scan_lblkid(void)
continue;
}
ret = btrfs_scan_one_device(fd, path, &tmp_devices,
&num_devices, BTRFS_SUPER_INFO_OFFSET, 0);
&num_devices, BTRFS_SUPER_INFO_OFFSET,
SBREAD_DEFAULT);
if (ret) {
error("cannot scan %s: %s", path, strerror(-ret));
close (fd);

View File

@ -251,7 +251,7 @@ fail:
int btrfs_scan_one_device(int fd, const char *path,
struct btrfs_fs_devices **fs_devices_ret,
u64 *total_devs, u64 super_offset, int super_recover)
u64 *total_devs, u64 super_offset, unsigned sbflags)
{
struct btrfs_super_block *disk_super;
char buf[BTRFS_SUPER_INFO_SIZE];
@ -259,7 +259,7 @@ int btrfs_scan_one_device(int fd, const char *path,
u64 devid;
disk_super = (struct btrfs_super_block *)buf;
ret = btrfs_read_dev_super(fd, disk_super, super_offset, super_recover);
ret = btrfs_read_dev_super(fd, disk_super, super_offset, sbflags);
if (ret < 0)
return -EIO;
devid = btrfs_stack_device_id(&disk_super->dev_item);

View File

@ -210,7 +210,7 @@ int btrfs_update_device(struct btrfs_trans_handle *trans,
struct btrfs_device *device);
int btrfs_scan_one_device(int fd, const char *path,
struct btrfs_fs_devices **fs_devices_ret,
u64 *total_devs, u64 super_offset, int super_recover);
u64 *total_devs, u64 super_offset, unsigned sbflags);
int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len);
struct list_head *btrfs_scanned_uuids(void);
int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,