diff --git a/extent-tree.c b/extent-tree.c index 53be4f4c..6288c8a3 100644 --- a/extent-tree.c +++ b/extent-tree.c @@ -2708,7 +2708,12 @@ static int read_one_block_group(struct btrfs_fs_info *fs_info, bit = BLOCK_GROUP_METADATA; } set_avail_alloc_bits(fs_info, cache->flags); - if (btrfs_chunk_readonly(fs_info, cache->key.objectid)) + ret = btrfs_chunk_readonly(fs_info, cache->key.objectid); + if (ret < 0) { + free(cache); + return ret; + } + if (ret) cache->ro = 1; exclude_super_stripes(fs_info, cache); @@ -2753,7 +2758,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *fs_info) btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]); ret = read_one_block_group(fs_info, &path); - if (ret < 0) + if (ret < 0 && ret != -ENOENT) goto error; if (key.offset == 0) diff --git a/volumes.c b/volumes.c index cc32cfbe..b46bf598 100644 --- a/volumes.c +++ b/volumes.c @@ -1802,6 +1802,11 @@ btrfs_find_device_by_devid(struct btrfs_fs_devices *fs_devices, return NULL; } +/* + * Return 0 if the chunk at @chunk_offset exists and is not read-only. + * Return 1 if the chunk at @chunk_offset exists and is read-only. + * Return <0 if we can't find chunk at @chunk_offset. + */ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) { struct cache_extent *ce; @@ -1814,12 +1819,13 @@ int btrfs_chunk_readonly(struct btrfs_fs_info *fs_info, u64 chunk_offset) * During chunk recovering, we may fail to find block group's * corresponding chunk, we will rebuild it later */ - ce = search_cache_extent(&map_tree->cache_tree, chunk_offset); - if (!fs_info->is_chunk_recover) - BUG_ON(!ce); - else + if (fs_info->is_chunk_recover) return 0; + ce = search_cache_extent(&map_tree->cache_tree, chunk_offset); + if (!ce) + return -ENOENT; + map = container_of(ce, struct map_lookup, ce); for (i = 0; i < map->num_stripes; i++) { if (!map->stripes[i].dev->writeable) {