Merge tag 'mfd-next-4.19' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[linux-2.6-microblaze.git] / fs / btrfs / extent-tree.c
index 16b916a..de6f75f 100644 (file)
@@ -4226,7 +4226,7 @@ commit_trans:
                                      data_sinfo->flags, bytes, 1);
        spin_unlock(&data_sinfo->lock);
 
-       return ret;
+       return 0;
 }
 
 int btrfs_check_data_free_space(struct inode *inode,
@@ -5529,11 +5529,6 @@ void btrfs_free_block_rsv(struct btrfs_fs_info *fs_info,
        kfree(rsv);
 }
 
-void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv)
-{
-       kfree(rsv);
-}
-
 int btrfs_block_rsv_add(struct btrfs_root *root,
                        struct btrfs_block_rsv *block_rsv, u64 num_bytes,
                        enum btrfs_reserve_flush_enum flush)
@@ -8330,7 +8325,6 @@ struct walk_control {
        int keep_locks;
        int reada_slot;
        int reada_count;
-       int for_reloc;
 };
 
 #define DROP_REFERENCE 1
@@ -8967,7 +8961,6 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
        wc->stage = DROP_REFERENCE;
        wc->update_ref = update_ref;
        wc->keep_locks = 0;
-       wc->for_reloc = for_reloc;
        wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
 
        while (1) {
@@ -9030,7 +9023,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
        if (err)
                goto out_end_trans;
 
-       ret = btrfs_del_root(trans, fs_info, &root->root_key);
+       ret = btrfs_del_root(trans, &root->root_key);
        if (ret) {
                btrfs_abort_transaction(trans, ret);
                err = ret;
@@ -9133,7 +9126,6 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
        wc->stage = DROP_REFERENCE;
        wc->update_ref = 0;
        wc->keep_locks = 1;
-       wc->for_reloc = 1;
        wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
 
        while (1) {
@@ -9540,6 +9532,8 @@ static int find_first_block_group(struct btrfs_fs_info *fs_info,
        int ret = 0;
        struct btrfs_key found_key;
        struct extent_buffer *leaf;
+       struct btrfs_block_group_item bg;
+       u64 flags;
        int slot;
 
        ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
@@ -9574,8 +9568,32 @@ static int find_first_block_group(struct btrfs_fs_info *fs_info,
                        "logical %llu len %llu found bg but no related chunk",
                                          found_key.objectid, found_key.offset);
                                ret = -ENOENT;
+                       } else if (em->start != found_key.objectid ||
+                                  em->len != found_key.offset) {
+                               btrfs_err(fs_info,
+               "block group %llu len %llu mismatch with chunk %llu len %llu",
+                                         found_key.objectid, found_key.offset,
+                                         em->start, em->len);
+                               ret = -EUCLEAN;
                        } else {
-                               ret = 0;
+                               read_extent_buffer(leaf, &bg,
+                                       btrfs_item_ptr_offset(leaf, slot),
+                                       sizeof(bg));
+                               flags = btrfs_block_group_flags(&bg) &
+                                       BTRFS_BLOCK_GROUP_TYPE_MASK;
+
+                               if (flags != (em->map_lookup->type &
+                                             BTRFS_BLOCK_GROUP_TYPE_MASK)) {
+                                       btrfs_err(fs_info,
+"block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx",
+                                               found_key.objectid,
+                                               found_key.offset, flags,
+                                               (BTRFS_BLOCK_GROUP_TYPE_MASK &
+                                                em->map_lookup->type));
+                                       ret = -EUCLEAN;
+                               } else {
+                                       ret = 0;
+                               }
                        }
                        free_extent_map(em);
                        goto out;
@@ -9826,6 +9844,62 @@ btrfs_create_block_group_cache(struct btrfs_fs_info *fs_info,
        return cache;
 }
 
+
+/*
+ * Iterate all chunks and verify that each of them has the corresponding block
+ * group
+ */
+static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info)
+{
+       struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
+       struct extent_map *em;
+       struct btrfs_block_group_cache *bg;
+       u64 start = 0;
+       int ret = 0;
+
+       while (1) {
+               read_lock(&map_tree->map_tree.lock);
+               /*
+                * lookup_extent_mapping will return the first extent map
+                * intersecting the range, so setting @len to 1 is enough to
+                * get the first chunk.
+                */
+               em = lookup_extent_mapping(&map_tree->map_tree, start, 1);
+               read_unlock(&map_tree->map_tree.lock);
+               if (!em)
+                       break;
+
+               bg = btrfs_lookup_block_group(fs_info, em->start);
+               if (!bg) {
+                       btrfs_err(fs_info,
+       "chunk start=%llu len=%llu doesn't have corresponding block group",
+                                    em->start, em->len);
+                       ret = -EUCLEAN;
+                       free_extent_map(em);
+                       break;
+               }
+               if (bg->key.objectid != em->start ||
+                   bg->key.offset != em->len ||
+                   (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) !=
+                   (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) {
+                       btrfs_err(fs_info,
+"chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx",
+                               em->start, em->len,
+                               em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK,
+                               bg->key.objectid, bg->key.offset,
+                               bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK);
+                       ret = -EUCLEAN;
+                       free_extent_map(em);
+                       btrfs_put_block_group(bg);
+                       break;
+               }
+               start = em->start + em->len;
+               free_extent_map(em);
+               btrfs_put_block_group(bg);
+       }
+       return ret;
+}
+
 int btrfs_read_block_groups(struct btrfs_fs_info *info)
 {
        struct btrfs_path *path;
@@ -9992,7 +10066,7 @@ int btrfs_read_block_groups(struct btrfs_fs_info *info)
 
        btrfs_add_raid_kobjects(info);
        init_global_block_rsv(info);
-       ret = 0;
+       ret = check_chunk_block_group_mappings(info);
 error:
        btrfs_free_path(path);
        return ret;