btrfs-progs: Cleanup old btrfs-convert
[platform/upstream/btrfs-progs.git] / cmds-check.c
index 4040f7e..ec0bbfd 100644 (file)
@@ -35,6 +35,7 @@
 #include "utils.h"
 #include "commands.h"
 #include "free-space-cache.h"
+#include "free-space-tree.h"
 #include "btrfsck.h"
 #include "qgroup-verify.h"
 #include "rbtree-utils.h"
@@ -72,40 +73,7 @@ static int init_extent_tree = 0;
 static int check_data_csum = 0;
 static struct btrfs_fs_info *global_info;
 static struct task_ctx ctx = { 0 };
-
-static void *print_status_check(void *p)
-{
-       struct task_ctx *priv = p;
-       const char work_indicator[] = { '.', 'o', 'O', 'o' };
-       uint32_t count = 0;
-       static char *task_position_string[] = {
-               "checking extents",
-               "checking free space cache",
-               "checking fs roots",
-       };
-
-       task_period_start(priv->info, 1000 /* 1s */);
-
-       if (priv->tp == TASK_NOTHING)
-               return NULL;
-
-       while (1) {
-               printf("%s [%c]\r", task_position_string[priv->tp],
-                               work_indicator[count % 4]);
-               count++;
-               fflush(stdout);
-               task_period_wait(priv->info);
-       }
-       return NULL;
-}
-
-static int print_status_return(void *p)
-{
-       printf("\n");
-       fflush(stdout);
-
-       return 0;
-}
+static struct cache_tree *roots_info_cache = NULL;
 
 struct extent_backref {
        struct list_head list;
@@ -154,6 +122,9 @@ struct tree_backref {
        };
 };
 
+/* Explicit initialization for extent_record::flag_block_full_backref */
+enum { FLAG_UNSET = 2 };
+
 struct extent_record {
        struct list_head backrefs;
        struct list_head dups;
@@ -170,7 +141,7 @@ struct extent_record {
        u64 info_objectid;
        u32 num_duplicates;
        u8 info_level;
-       int flag_block_full_backref;
+       unsigned int flag_block_full_backref:2;
        unsigned int found_rec:1;
        unsigned int content_checked:1;
        unsigned int owner_ref_checked:1;
@@ -226,6 +197,156 @@ struct file_extent_hole {
        u64 len;
 };
 
+struct inode_record {
+       struct list_head backrefs;
+       unsigned int checked:1;
+       unsigned int merging:1;
+       unsigned int found_inode_item:1;
+       unsigned int found_dir_item:1;
+       unsigned int found_file_extent:1;
+       unsigned int found_csum_item:1;
+       unsigned int some_csum_missing:1;
+       unsigned int nodatasum:1;
+       int errors;
+
+       u64 ino;
+       u32 nlink;
+       u32 imode;
+       u64 isize;
+       u64 nbytes;
+
+       u32 found_link;
+       u64 found_size;
+       u64 extent_start;
+       u64 extent_end;
+       struct rb_root holes;
+       struct list_head orphan_extents;
+
+       u32 refs;
+};
+
+#define I_ERR_NO_INODE_ITEM            (1 << 0)
+#define I_ERR_NO_ORPHAN_ITEM           (1 << 1)
+#define I_ERR_DUP_INODE_ITEM           (1 << 2)
+#define I_ERR_DUP_DIR_INDEX            (1 << 3)
+#define I_ERR_ODD_DIR_ITEM             (1 << 4)
+#define I_ERR_ODD_FILE_EXTENT          (1 << 5)
+#define I_ERR_BAD_FILE_EXTENT          (1 << 6)
+#define I_ERR_FILE_EXTENT_OVERLAP      (1 << 7)
+#define I_ERR_FILE_EXTENT_DISCOUNT     (1 << 8) // 100
+#define I_ERR_DIR_ISIZE_WRONG          (1 << 9)
+#define I_ERR_FILE_NBYTES_WRONG                (1 << 10) // 400
+#define I_ERR_ODD_CSUM_ITEM            (1 << 11)
+#define I_ERR_SOME_CSUM_MISSING                (1 << 12)
+#define I_ERR_LINK_COUNT_WRONG         (1 << 13)
+#define I_ERR_FILE_EXTENT_ORPHAN       (1 << 14)
+
+struct root_backref {
+       struct list_head list;
+       unsigned int found_dir_item:1;
+       unsigned int found_dir_index:1;
+       unsigned int found_back_ref:1;
+       unsigned int found_forward_ref:1;
+       unsigned int reachable:1;
+       int errors;
+       u64 ref_root;
+       u64 dir;
+       u64 index;
+       u16 namelen;
+       char name[0];
+};
+
+struct root_record {
+       struct list_head backrefs;
+       struct cache_extent cache;
+       unsigned int found_root_item:1;
+       u64 objectid;
+       u32 found_ref;
+};
+
+struct ptr_node {
+       struct cache_extent cache;
+       void *data;
+};
+
+struct shared_node {
+       struct cache_extent cache;
+       struct cache_tree root_cache;
+       struct cache_tree inode_cache;
+       struct inode_record *current;
+       u32 refs;
+};
+
+struct block_info {
+       u64 start;
+       u32 size;
+};
+
+struct walk_control {
+       struct cache_tree shared;
+       struct shared_node *nodes[BTRFS_MAX_LEVEL];
+       int active_node;
+       int root_level;
+};
+
+struct bad_item {
+       struct btrfs_key key;
+       u64 root_id;
+       struct list_head list;
+};
+
+struct extent_entry {
+       u64 bytenr;
+       u64 bytes;
+       int count;
+       int broken;
+       struct list_head list;
+};
+
+struct root_item_info {
+       /* level of the root */
+       u8 level;
+       /* number of nodes at this level, must be 1 for a root */
+       int node_count;
+       u64 bytenr;
+       u64 gen;
+       struct cache_extent cache_extent;
+};
+
+static void *print_status_check(void *p)
+{
+       struct task_ctx *priv = p;
+       const char work_indicator[] = { '.', 'o', 'O', 'o' };
+       uint32_t count = 0;
+       static char *task_position_string[] = {
+               "checking extents",
+               "checking free space cache",
+               "checking fs roots",
+       };
+
+       task_period_start(priv->info, 1000 /* 1s */);
+
+       if (priv->tp == TASK_NOTHING)
+               return NULL;
+
+       while (1) {
+               printf("%s [%c]\r", task_position_string[priv->tp],
+                               work_indicator[count % 4]);
+               count++;
+               fflush(stdout);
+               task_period_wait(priv->info);
+       }
+       return NULL;
+}
+
+static int print_status_return(void *p)
+{
+       printf("\n");
+       fflush(stdout);
+
+       return 0;
+}
+
 /* Compatible function to allow reuse of old codes */
 static u64 first_extent_gap(struct rb_root *holes)
 {
@@ -358,7 +479,7 @@ static int del_file_extent_hole(struct rb_root *holes,
                return -EEXIST;
 
        /*
-        * Now there will be no overflap, delete the hole and re-add the
+        * Now there will be no overlap, delete the hole and re-add the
         * split(s) if they exists.
         */
        if (start > hole->start) {
@@ -418,104 +539,6 @@ static void free_file_extent_holes(struct rb_root *holes)
        }
 }
 
-struct inode_record {
-       struct list_head backrefs;
-       unsigned int checked:1;
-       unsigned int merging:1;
-       unsigned int found_inode_item:1;
-       unsigned int found_dir_item:1;
-       unsigned int found_file_extent:1;
-       unsigned int found_csum_item:1;
-       unsigned int some_csum_missing:1;
-       unsigned int nodatasum:1;
-       int errors;
-
-       u64 ino;
-       u32 nlink;
-       u32 imode;
-       u64 isize;
-       u64 nbytes;
-
-       u32 found_link;
-       u64 found_size;
-       u64 extent_start;
-       u64 extent_end;
-       struct rb_root holes;
-       struct list_head orphan_extents;
-
-       u32 refs;
-};
-
-#define I_ERR_NO_INODE_ITEM            (1 << 0)
-#define I_ERR_NO_ORPHAN_ITEM           (1 << 1)
-#define I_ERR_DUP_INODE_ITEM           (1 << 2)
-#define I_ERR_DUP_DIR_INDEX            (1 << 3)
-#define I_ERR_ODD_DIR_ITEM             (1 << 4)
-#define I_ERR_ODD_FILE_EXTENT          (1 << 5)
-#define I_ERR_BAD_FILE_EXTENT          (1 << 6)
-#define I_ERR_FILE_EXTENT_OVERLAP      (1 << 7)
-#define I_ERR_FILE_EXTENT_DISCOUNT     (1 << 8) // 100
-#define I_ERR_DIR_ISIZE_WRONG          (1 << 9)
-#define I_ERR_FILE_NBYTES_WRONG                (1 << 10) // 400
-#define I_ERR_ODD_CSUM_ITEM            (1 << 11)
-#define I_ERR_SOME_CSUM_MISSING                (1 << 12)
-#define I_ERR_LINK_COUNT_WRONG         (1 << 13)
-#define I_ERR_FILE_EXTENT_ORPHAN       (1 << 14)
-
-struct root_backref {
-       struct list_head list;
-       unsigned int found_dir_item:1;
-       unsigned int found_dir_index:1;
-       unsigned int found_back_ref:1;
-       unsigned int found_forward_ref:1;
-       unsigned int reachable:1;
-       int errors;
-       u64 ref_root;
-       u64 dir;
-       u64 index;
-       u16 namelen;
-       char name[0];
-};
-
-struct root_record {
-       struct list_head backrefs;
-       struct cache_extent cache;
-       unsigned int found_root_item:1;
-       u64 objectid;
-       u32 found_ref;
-};
-
-struct ptr_node {
-       struct cache_extent cache;
-       void *data;
-};
-
-struct shared_node {
-       struct cache_extent cache;
-       struct cache_tree root_cache;
-       struct cache_tree inode_cache;
-       struct inode_record *current;
-       u32 refs;
-};
-
-struct block_info {
-       u64 start;
-       u32 size;
-};
-
-struct walk_control {
-       struct cache_tree shared;
-       struct shared_node *nodes[BTRFS_MAX_LEVEL];
-       int active_node;
-       int root_level;
-};
-
-struct bad_item {
-       struct btrfs_key key;
-       u64 root_id;
-       struct list_head list;
-};
-
 static void reset_cached_block_groups(struct btrfs_fs_info *fs_info);
 
 static void record_root_in_trans(struct btrfs_trans_handle *trans,
@@ -566,12 +589,15 @@ static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
        struct inode_record *rec;
        struct inode_backref *backref;
        struct inode_backref *orig;
+       struct inode_backref *tmp;
        struct orphan_data_extent *src_orphan;
        struct orphan_data_extent *dst_orphan;
        size_t size;
        int ret;
 
        rec = malloc(sizeof(*rec));
+       if (!rec)
+               return ERR_PTR(-ENOMEM);
        memcpy(rec, orig_rec, sizeof(*rec));
        rec->refs = 1;
        INIT_LIST_HEAD(&rec->backrefs);
@@ -581,13 +607,19 @@ static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
        list_for_each_entry(orig, &orig_rec->backrefs, list) {
                size = sizeof(*orig) + orig->namelen + 1;
                backref = malloc(size);
+               if (!backref) {
+                       ret = -ENOMEM;
+                       goto cleanup;
+               }
                memcpy(backref, orig, size);
                list_add_tail(&backref->list, &rec->backrefs);
        }
        list_for_each_entry(src_orphan, &orig_rec->orphan_extents, list) {
                dst_orphan = malloc(sizeof(*dst_orphan));
-               /* TODO: Fix all the HELL of un-catched -ENOMEM case */
-               BUG_ON(!dst_orphan);
+               if (!dst_orphan) {
+                       ret = -ENOMEM;
+                       goto cleanup;
+               }
                memcpy(dst_orphan, src_orphan, sizeof(*src_orphan));
                list_add_tail(&dst_orphan->list, &rec->orphan_extents);
        }
@@ -595,6 +627,23 @@ static struct inode_record *clone_inode_rec(struct inode_record *orig_rec)
        BUG_ON(ret < 0);
 
        return rec;
+
+cleanup:
+       if (!list_empty(&rec->backrefs))
+               list_for_each_entry_safe(orig, tmp, &rec->backrefs, list) {
+                       list_del(&orig->list);
+                       free(orig);
+               }
+
+       if (!list_empty(&rec->orphan_extents))
+               list_for_each_entry_safe(orig, tmp, &rec->orphan_extents, list) {
+                       list_del(&orig->list);
+                       free(orig);
+               }
+
+       free(rec);
+
+       return ERR_PTR(ret);
 }
 
 static void print_orphan_data_extents(struct list_head *orphan_extents,
@@ -700,9 +749,9 @@ static void print_ref_error(int errors)
        if (errors & REF_ERR_DUP_INODE_REF)
                fprintf(stderr, ", dup inode ref");
        if (errors & REF_ERR_INDEX_UNMATCH)
-               fprintf(stderr, ", index unmatch");
+               fprintf(stderr, ", index mismatch");
        if (errors & REF_ERR_FILETYPE_UNMATCH)
-               fprintf(stderr, ", filetype unmatch");
+               fprintf(stderr, ", filetype mismatch");
        if (errors & REF_ERR_NAME_TOO_LONG)
                fprintf(stderr, ", name too long");
        if (errors & REF_ERR_NO_ROOT_REF)
@@ -730,11 +779,15 @@ static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
                rec = node->data;
                if (mod && rec->refs > 1) {
                        node->data = clone_inode_rec(rec);
+                       if (IS_ERR(node->data))
+                               return node->data;
                        rec->refs--;
                        rec = node->data;
                }
        } else if (mod) {
                rec = calloc(1, sizeof(*rec));
+               if (!rec)
+                       return ERR_PTR(-ENOMEM);
                rec->ino = ino;
                rec->extent_start = (u64)-1;
                rec->refs = 1;
@@ -743,6 +796,10 @@ static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
                rec->holes = RB_ROOT;
 
                node = malloc(sizeof(*node));
+               if (!node) {
+                       free(rec);
+                       return ERR_PTR(-ENOMEM);
+               }
                node->cache.start = ino;
                node->cache.size = 1;
                node->data = rec;
@@ -751,7 +808,8 @@ static struct inode_record *get_inode_rec(struct cache_tree *inode_cache,
                        rec->found_link = 1;
 
                ret = insert_cache_extent(inode_cache, &node->cache);
-               BUG_ON(ret);
+               if (ret)
+                       return ERR_PTR(-EEXIST);
        }
        return rec;
 }
@@ -810,7 +868,8 @@ static void maybe_free_inode_rec(struct cache_tree *inode_cache,
                if (backref->found_dir_item && backref->found_dir_index) {
                        if (backref->filetype != filetype)
                                backref->errors |= REF_ERR_FILETYPE_UNMATCH;
-                       if (!backref->errors && backref->found_inode_ref) {
+                       if (!backref->errors && backref->found_inode_ref &&
+                           rec->nlink == rec->found_link) {
                                list_del(&backref->list);
                                free(backref);
                        }
@@ -916,6 +975,8 @@ static struct inode_backref *get_inode_backref(struct inode_record *rec,
        }
 
        backref = malloc(sizeof(*backref) + namelen + 1);
+       if (!backref)
+               return NULL;
        memset(backref, 0, sizeof(*backref));
        backref->dir = dir;
        backref->namelen = namelen;
@@ -934,7 +995,9 @@ static int add_inode_backref(struct cache_tree *inode_cache,
        struct inode_backref *backref;
 
        rec = get_inode_rec(inode_cache, ino, 1);
+       BUG_ON(IS_ERR(rec));
        backref = get_inode_backref(rec, name, namelen, dir);
+       BUG_ON(!backref);
        if (errors)
                backref->errors |= errors;
        if (itemtype == BTRFS_DIR_INDEX_KEY) {
@@ -1088,6 +1151,7 @@ again:
                        ins = node;
                } else {
                        ins = malloc(sizeof(*ins));
+                       BUG_ON(!ins);
                        ins->cache.start = node->cache.start;
                        ins->cache.size = node->cache.size;
                        ins->data = rec;
@@ -1096,6 +1160,7 @@ again:
                ret = insert_cache_extent(dst, &ins->cache);
                if (ret == -EEXIST) {
                        conflict = get_inode_rec(dst, rec->ino, 1);
+                       BUG_ON(IS_ERR(conflict));
                        merge_inode_recs(rec, conflict, dst);
                        if (rec->checked) {
                                conflict->checked = 1;
@@ -1123,6 +1188,7 @@ again:
                        maybe_free_inode_rec(dst, dst_node->current);
                }
                dst_node->current = get_inode_rec(dst, current_ino, 1);
+               BUG_ON(IS_ERR(dst_node->current));
        }
        return 0;
 }
@@ -1160,6 +1226,8 @@ static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
        struct shared_node *node;
 
        node = calloc(1, sizeof(*node));
+       if (!node)
+               return -ENOMEM;
        node->cache.start = bytenr;
        node->cache.size = 1;
        cache_tree_init(&node->root_cache);
@@ -1167,8 +1235,8 @@ static int add_shared_node(struct cache_tree *shared, u64 bytenr, u32 refs)
        node->refs = refs;
 
        ret = insert_cache_extent(shared, &node->cache);
-       BUG_ON(ret);
-       return 0;
+
+       return ret;
 }
 
 static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
@@ -1176,6 +1244,7 @@ static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
 {
        struct shared_node *node;
        struct shared_node *dest;
+       int ret;
 
        if (level == wc->active_node)
                return 0;
@@ -1183,7 +1252,8 @@ static int enter_shared_node(struct btrfs_root *root, u64 bytenr, u32 refs,
        BUG_ON(wc->active_node <= level);
        node = find_shared_node(&wc->shared, bytenr);
        if (!node) {
-               add_shared_node(&wc->shared, bytenr, refs);
+               ret = add_shared_node(&wc->shared, bytenr, refs);
+               BUG_ON(ret);
                node = find_shared_node(&wc->shared, bytenr);
                wc->nodes[level] = node;
                wc->active_node = level;
@@ -1663,6 +1733,7 @@ static int process_one_leaf(struct btrfs_root *root, struct extent_buffer *eb,
                        }
                        active_node->current = get_inode_rec(inode_cache,
                                                             key.objectid, 1);
+                       BUG_ON(IS_ERR(active_node->current));
                }
                switch (key.type) {
                case BTRFS_DIR_ITEM_KEY:
@@ -1704,7 +1775,7 @@ static void reada_walk_down(struct btrfs_root *root,
                return;
 
        nritems = btrfs_header_nritems(node);
-       blocksize = btrfs_level_size(root, level - 1);
+       blocksize = root->nodesize;
        for (i = slot; i < nritems; i++) {
                bytenr = btrfs_node_blockptr(node, i);
                ptr_gen = btrfs_node_ptr_generation(node, i);
@@ -1811,7 +1882,7 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
                }
                bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
                ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
-               blocksize = btrfs_level_size(root, *level - 1);
+               blocksize = root->nodesize;
                ret = btrfs_lookup_extent_info(NULL, root, bytenr, *level - 1,
                                               1, &refs, NULL);
                if (ret < 0)
@@ -1841,7 +1912,7 @@ static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
                                btrfs_add_corrupt_extent_record(root->fs_info,
                                                &node_key,
                                                path->nodes[*level]->start,
-                                               root->leafsize, *level);
+                                               root->nodesize, *level);
                                err = -EIO;
                                goto out;
                        }
@@ -2063,6 +2134,7 @@ static int add_missing_dir_index(struct btrfs_root *root,
 
        backref->found_dir_index = 1;
        dir_rec = get_inode_rec(inode_cache, backref->dir, 0);
+       BUG_ON(IS_ERR(dir_rec));
        if (!dir_rec)
                return 0;
        dir_rec->found_size += backref->namelen;
@@ -2392,7 +2464,7 @@ static int reset_nlink(struct btrfs_trans_handle *trans,
        list_for_each_entry(backref, &rec->backrefs, list) {
                ret = btrfs_add_link(trans, root, rec->ino, backref->dir,
                                     backref->name, backref->namelen,
-                                    backref->ref_type, &backref->index, 1);
+                                    backref->filetype, &backref->index, 1);
                if (ret < 0)
                        goto out;
        }
@@ -2620,7 +2692,7 @@ static int repair_inode_no_item(struct btrfs_trans_handle *trans,
                        type_recovered = 1;
                        filetype = BTRFS_FT_REG_FILE;
                } else{
-                       printf("Can't determint the filetype for inode %llu, assume it is a normal file\n",
+                       printf("Can't determine the filetype for inode %llu, assume it is a normal file\n",
                               rec->ino);
                        type_recovered = 1;
                        filetype = BTRFS_FT_REG_FILE;
@@ -2823,7 +2895,7 @@ static int check_inode_recs(struct btrfs_root *root,
        /*
         * We need to record the highest inode number for later 'lost+found'
         * dir creation.
-        * We must select a ino not used/refered by any existing inode, or
+        * We must select an ino not used/referred by any existing inode, or
         * 'lost+found' ino may be a missing ino in a corrupted leaf,
         * this may cause 'lost+found' dir has wrong nlinks.
         */
@@ -2887,6 +2959,7 @@ static int check_inode_recs(struct btrfs_root *root,
                return err;
 
        rec = get_inode_rec(inode_cache, root_dirid, 0);
+       BUG_ON(IS_ERR(rec));
        if (rec) {
                ret = check_root_dir(rec);
                if (ret) {
@@ -2994,13 +3067,16 @@ static struct root_record *get_root_rec(struct cache_tree *root_cache,
                rec = container_of(cache, struct root_record, cache);
        } else {
                rec = calloc(1, sizeof(*rec));
+               if (!rec)
+                       return ERR_PTR(-ENOMEM);
                rec->objectid = objectid;
                INIT_LIST_HEAD(&rec->backrefs);
                rec->cache.start = objectid;
                rec->cache.size = 1;
 
                ret = insert_cache_extent(root_cache, &rec->cache);
-               BUG_ON(ret);
+               if (ret)
+                       return ERR_PTR(-EEXIST);
        }
        return rec;
 }
@@ -3021,6 +3097,8 @@ static struct root_backref *get_root_backref(struct root_record *rec,
        }
 
        backref = calloc(1, sizeof(*backref) + namelen + 1);
+       if (!backref)
+               return NULL;
        backref->ref_root = ref_root;
        backref->dir = dir;
        backref->index = index;
@@ -3058,7 +3136,9 @@ static int add_root_backref(struct cache_tree *root_cache,
        struct root_backref *backref;
 
        rec = get_root_rec(root_cache, root_id);
+       BUG_ON(IS_ERR(rec));
        backref = get_root_backref(rec, ref_root, dir, index, name, namelen);
+       BUG_ON(!backref);
 
        backref->errors |= errors;
 
@@ -3163,6 +3243,7 @@ static int check_root_refs(struct btrfs_root *root,
        int errors = 0;
 
        rec = get_root_rec(root_cache, BTRFS_FS_TREE_OBJECTID);
+       BUG_ON(IS_ERR(rec));
        rec->found_ref = 1;
 
        /* fixme: this can not detect circular references */
@@ -3184,6 +3265,7 @@ static int check_root_refs(struct btrfs_root *root,
 
                                ref_root = get_root_rec(root_cache,
                                                        backref->ref_root);
+                               BUG_ON(IS_ERR(ref_root));
                                if (ref_root->found_ref > 0)
                                        continue;
 
@@ -3430,6 +3512,7 @@ static int check_fs_root(struct btrfs_root *root,
 
        if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
                rec = get_root_rec(root_cache, root->root_key.objectid);
+               BUG_ON(IS_ERR(rec));
                if (btrfs_root_refs(root_item) > 0)
                        rec->found_root_item = 1;
        }
@@ -3446,6 +3529,7 @@ static int check_fs_root(struct btrfs_root *root,
 
                inode = get_inode_rec(&root_node.inode_cache, orphan->objectid,
                                      1);
+               BUG_ON(IS_ERR(inode));
                inode->errors |= I_ERR_FILE_EXTENT_ORPHAN;
                list_move(&orphan->list, &inode->orphan_extents);
        }
@@ -4242,7 +4326,7 @@ static int check_block(struct btrfs_root *root,
                } else {
                        /*
                         * Signal to callers we need to start the scan over
-                        * again since we'll have cow'ed blocks.
+                        * again since we'll have cowed blocks.
                         */
                        ret = -EAGAIN;
                }
@@ -4293,6 +4377,9 @@ static struct tree_backref *alloc_tree_backref(struct extent_record *rec,
                                                u64 parent, u64 root)
 {
        struct tree_backref *ref = malloc(sizeof(*ref));
+
+       if (!ref)
+               return NULL;
        memset(&ref->node, 0, sizeof(ref->node));
        if (parent > 0) {
                ref->parent = parent;
@@ -4349,6 +4436,9 @@ static struct data_backref *alloc_data_backref(struct extent_record *rec,
                                                u64 max_size)
 {
        struct data_backref *ref = malloc(sizeof(*ref));
+
+       if (!ref)
+               return NULL;
        memset(&ref->node, 0, sizeof(ref->node));
        ref->node.is_data = 1;
 
@@ -4422,32 +4512,86 @@ static void check_extent_type(struct extent_record *rec)
        }
 }
 
+/*
+ * Allocate a new extent record, fill default values from @tmpl and insert int
+ * @extent_cache. Caller is supposed to make sure the [start,nr) is not in
+ * the cache, otherwise it fails.
+ */
+static int add_extent_rec_nolookup(struct cache_tree *extent_cache,
+               struct extent_record *tmpl)
+{
+       struct extent_record *rec;
+       int ret = 0;
+
+       rec = malloc(sizeof(*rec));
+       if (!rec)
+               return -ENOMEM;
+       rec->start = tmpl->start;
+       rec->max_size = tmpl->max_size;
+       rec->nr = max(tmpl->nr, tmpl->max_size);
+       rec->found_rec = tmpl->found_rec;
+       rec->content_checked = tmpl->content_checked;
+       rec->owner_ref_checked = tmpl->owner_ref_checked;
+       rec->num_duplicates = 0;
+       rec->metadata = tmpl->metadata;
+       rec->flag_block_full_backref = FLAG_UNSET;
+       rec->bad_full_backref = 0;
+       rec->crossing_stripes = 0;
+       rec->wrong_chunk_type = 0;
+       rec->is_root = tmpl->is_root;
+       rec->refs = tmpl->refs;
+       rec->extent_item_refs = tmpl->extent_item_refs;
+       rec->parent_generation = tmpl->parent_generation;
+       INIT_LIST_HEAD(&rec->backrefs);
+       INIT_LIST_HEAD(&rec->dups);
+       INIT_LIST_HEAD(&rec->list);
+       memcpy(&rec->parent_key, &tmpl->parent_key, sizeof(tmpl->parent_key));
+       rec->cache.start = tmpl->start;
+       rec->cache.size = tmpl->nr;
+       ret = insert_cache_extent(extent_cache, &rec->cache);
+       BUG_ON(ret);
+       bytes_used += rec->nr;
+
+       if (tmpl->metadata)
+               rec->crossing_stripes = check_crossing_stripes(rec->start,
+                               global_info->tree_root->nodesize);
+       check_extent_type(rec);
+       return ret;
+}
+
+/*
+ * Lookup and modify an extent, some values of @tmpl are interpreted verbatim,
+ * some are hints:
+ * - refs              - if found, increase refs
+ * - is_root           - if found, set
+ * - content_checked   - if found, set
+ * - owner_ref_checked - if found, set
+ *
+ * If not found, create a new one, initialize and insert.
+ */
 static int add_extent_rec(struct cache_tree *extent_cache,
-                         struct btrfs_key *parent_key, u64 parent_gen,
-                         u64 start, u64 nr, u64 extent_item_refs,
-                         int is_root, int inc_ref, int set_checked,
-                         int metadata, int extent_rec, u64 max_size)
+               struct extent_record *tmpl)
 {
        struct extent_record *rec;
        struct cache_extent *cache;
        int ret = 0;
        int dup = 0;
 
-       cache = lookup_cache_extent(extent_cache, start, nr);
+       cache = lookup_cache_extent(extent_cache, tmpl->start, tmpl->nr);
        if (cache) {
                rec = container_of(cache, struct extent_record, cache);
-               if (inc_ref)
+               if (tmpl->refs)
                        rec->refs++;
                if (rec->nr == 1)
-                       rec->nr = max(nr, max_size);
+                       rec->nr = max(tmpl->nr, tmpl->max_size);
 
                /*
                 * We need to make sure to reset nr to whatever the extent
                 * record says was the real size, this way we can compare it to
                 * the backrefs.
                 */
-               if (extent_rec) {
-                       if (start != rec->start || rec->found_rec) {
+               if (tmpl->found_rec) {
+                       if (tmpl->start != rec->start || rec->found_rec) {
                                struct extent_record *tmp;
 
                                dup = 1;
@@ -4464,46 +4608,44 @@ static int add_extent_rec(struct cache_tree *extent_cache,
                                tmp = malloc(sizeof(*tmp));
                                if (!tmp)
                                        return -ENOMEM;
-                               tmp->start = start;
-                               tmp->max_size = max_size;
-                               tmp->nr = nr;
+                               tmp->start = tmpl->start;
+                               tmp->max_size = tmpl->max_size;
+                               tmp->nr = tmpl->nr;
                                tmp->found_rec = 1;
-                               tmp->metadata = metadata;
-                               tmp->extent_item_refs = extent_item_refs;
+                               tmp->metadata = tmpl->metadata;
+                               tmp->extent_item_refs = tmpl->extent_item_refs;
                                INIT_LIST_HEAD(&tmp->list);
                                list_add_tail(&tmp->list, &rec->dups);
                                rec->num_duplicates++;
                        } else {
-                               rec->nr = nr;
+                               rec->nr = tmpl->nr;
                                rec->found_rec = 1;
                        }
                }
 
-               if (extent_item_refs && !dup) {
+               if (tmpl->extent_item_refs && !dup) {
                        if (rec->extent_item_refs) {
                                fprintf(stderr, "block %llu rec "
                                        "extent_item_refs %llu, passed %llu\n",
-                                       (unsigned long long)start,
+                                       (unsigned long long)tmpl->start,
                                        (unsigned long long)
                                                        rec->extent_item_refs,
-                                       (unsigned long long)extent_item_refs);
+                                       (unsigned long long)tmpl->extent_item_refs);
                        }
-                       rec->extent_item_refs = extent_item_refs;
+                       rec->extent_item_refs = tmpl->extent_item_refs;
                }
-               if (is_root)
+               if (tmpl->is_root)
                        rec->is_root = 1;
-               if (set_checked) {
+               if (tmpl->content_checked)
                        rec->content_checked = 1;
+               if (tmpl->owner_ref_checked)
                        rec->owner_ref_checked = 1;
-               }
-
-               if (parent_key)
-                       btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
-               if (parent_gen)
-                       rec->parent_generation = parent_gen;
-
-               if (rec->max_size < max_size)
-                       rec->max_size = max_size;
+               memcpy(&rec->parent_key, &tmpl->parent_key,
+                               sizeof(tmpl->parent_key));
+               if (tmpl->parent_generation)
+                       rec->parent_generation = tmpl->parent_generation;
+               if (rec->max_size < tmpl->max_size)
+                       rec->max_size = tmpl->max_size;
 
                /*
                 * A metadata extent can't cross stripe_len boundary, otherwise
@@ -4511,69 +4653,16 @@ static int add_extent_rec(struct cache_tree *extent_cache,
                 * As now stripe_len is fixed to BTRFS_STRIPE_LEN, just check
                 * it.
                 */
-               if (metadata && check_crossing_stripes(rec->start,
-                                                      rec->max_size))
-                               rec->crossing_stripes = 1;
+               if (tmpl->metadata)
+                       rec->crossing_stripes = check_crossing_stripes(
+                               rec->start, global_info->tree_root->nodesize);
                check_extent_type(rec);
                maybe_free_extent_rec(extent_cache, rec);
                return ret;
        }
-       rec = malloc(sizeof(*rec));
-       rec->start = start;
-       rec->max_size = max_size;
-       rec->nr = max(nr, max_size);
-       rec->found_rec = !!extent_rec;
-       rec->content_checked = 0;
-       rec->owner_ref_checked = 0;
-       rec->num_duplicates = 0;
-       rec->metadata = metadata;
-       rec->flag_block_full_backref = -1;
-       rec->bad_full_backref = 0;
-       rec->crossing_stripes = 0;
-       rec->wrong_chunk_type = 0;
-       INIT_LIST_HEAD(&rec->backrefs);
-       INIT_LIST_HEAD(&rec->dups);
-       INIT_LIST_HEAD(&rec->list);
-
-       if (is_root)
-               rec->is_root = 1;
-       else
-               rec->is_root = 0;
-
-       if (inc_ref)
-               rec->refs = 1;
-       else
-               rec->refs = 0;
-
-       if (extent_item_refs)
-               rec->extent_item_refs = extent_item_refs;
-       else
-               rec->extent_item_refs = 0;
 
-       if (parent_key)
-               btrfs_cpu_key_to_disk(&rec->parent_key, parent_key);
-       else
-               memset(&rec->parent_key, 0, sizeof(*parent_key));
-
-       if (parent_gen)
-               rec->parent_generation = parent_gen;
-       else
-               rec->parent_generation = 0;
+       ret = add_extent_rec_nolookup(extent_cache, tmpl);
 
-       rec->cache.start = start;
-       rec->cache.size = nr;
-       ret = insert_cache_extent(extent_cache, &rec->cache);
-       BUG_ON(ret);
-       bytes_used += nr;
-       if (set_checked) {
-               rec->content_checked = 1;
-               rec->owner_ref_checked = 1;
-       }
-
-       if (metadata)
-               if (check_crossing_stripes(rec->start, rec->max_size))
-                       rec->crossing_stripes = 1;
-       check_extent_type(rec);
        return ret;
 }
 
@@ -4586,8 +4675,15 @@ static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
 
        cache = lookup_cache_extent(extent_cache, bytenr, 1);
        if (!cache) {
-               add_extent_rec(extent_cache, NULL, 0, bytenr,
-                              1, 0, 0, 0, 0, 1, 0, 0);
+               struct extent_record tmpl;
+
+               memset(&tmpl, 0, sizeof(tmpl));
+               tmpl.start = bytenr;
+               tmpl.nr = 1;
+               tmpl.metadata = 1;
+
+               add_extent_rec_nolookup(extent_cache, &tmpl);
+
                cache = lookup_cache_extent(extent_cache, bytenr, 1);
                if (!cache)
                        abort();
@@ -4599,8 +4695,10 @@ static int add_tree_backref(struct cache_tree *extent_cache, u64 bytenr,
        }
 
        back = find_tree_backref(rec, parent, root);
-       if (!back)
+       if (!back) {
                back = alloc_tree_backref(rec, parent, root);
+               BUG_ON(!back);
+       }
 
        if (found_ref) {
                if (back->node.found_ref) {
@@ -4636,8 +4734,15 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
 
        cache = lookup_cache_extent(extent_cache, bytenr, 1);
        if (!cache) {
-               add_extent_rec(extent_cache, NULL, 0, bytenr, 1, 0, 0, 0, 0,
-                              0, 0, max_size);
+               struct extent_record tmpl;
+
+               memset(&tmpl, 0, sizeof(tmpl));
+               tmpl.start = bytenr;
+               tmpl.nr = 1;
+               tmpl.max_size = max_size;
+
+               add_extent_rec_nolookup(extent_cache, &tmpl);
+
                cache = lookup_cache_extent(extent_cache, bytenr, 1);
                if (!cache)
                        abort();
@@ -4659,9 +4764,11 @@ static int add_data_backref(struct cache_tree *extent_cache, u64 bytenr,
         */
        back = find_data_backref(rec, parent, root, owner, offset, found_ref,
                                 bytenr, max_size);
-       if (!back)
+       if (!back) {
                back = alloc_data_backref(rec, parent, root, owner, offset,
                                          max_size);
+               BUG_ON(!back);
+       }
 
        if (found_ref) {
                BUG_ON(num_refs != 1);
@@ -5100,6 +5207,7 @@ static int process_extent_item(struct btrfs_root *root,
        struct btrfs_extent_data_ref *dref;
        struct btrfs_shared_data_ref *sref;
        struct btrfs_key key;
+       struct extent_record tmpl;
        unsigned long end;
        unsigned long ptr;
        int type;
@@ -5113,7 +5221,7 @@ static int process_extent_item(struct btrfs_root *root,
 
        if (key.type == BTRFS_METADATA_ITEM_KEY) {
                metadata = 1;
-               num_bytes = root->leafsize;
+               num_bytes = root->nodesize;
        } else {
                num_bytes = key.offset;
        }
@@ -5127,16 +5235,32 @@ static int process_extent_item(struct btrfs_root *root,
 #else
                BUG();
 #endif
-               return add_extent_rec(extent_cache, NULL, 0, key.objectid,
-                                     num_bytes, refs, 0, 0, 0, metadata, 1,
-                                     num_bytes);
+               memset(&tmpl, 0, sizeof(tmpl));
+               tmpl.start = key.objectid;
+               tmpl.nr = num_bytes;
+               tmpl.extent_item_refs = refs;
+               tmpl.metadata = metadata;
+               tmpl.found_rec = 1;
+               tmpl.max_size = num_bytes;
+
+               return add_extent_rec(extent_cache, &tmpl);
        }
 
        ei = btrfs_item_ptr(eb, slot, struct btrfs_extent_item);
        refs = btrfs_extent_refs(eb, ei);
+       if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK)
+               metadata = 1;
+       else
+               metadata = 0;
 
-       add_extent_rec(extent_cache, NULL, 0, key.objectid, num_bytes,
-                      refs, 0, 0, 0, metadata, 1, num_bytes);
+       memset(&tmpl, 0, sizeof(tmpl));
+       tmpl.start = key.objectid;
+       tmpl.nr = num_bytes;
+       tmpl.extent_item_refs = refs;
+       tmpl.metadata = metadata;
+       tmpl.found_rec = 1;
+       tmpl.max_size = num_bytes;
+       add_extent_rec(extent_cache, &tmpl);
 
        ptr = (unsigned long)(ei + 1);
        if (btrfs_extent_flags(eb, ei) & BTRFS_EXTENT_FLAG_TREE_BLOCK &&
@@ -5329,7 +5453,7 @@ static int verify_space_cache(struct btrfs_root *root,
                        if (key.type == BTRFS_EXTENT_ITEM_KEY)
                                last = key.objectid + key.offset;
                        else
-                               last = key.objectid + root->leafsize;
+                               last = key.objectid + root->nodesize;
                        path->slots[0]++;
                        continue;
                }
@@ -5341,7 +5465,7 @@ static int verify_space_cache(struct btrfs_root *root,
                if (key.type == BTRFS_EXTENT_ITEM_KEY)
                        last = key.objectid + key.offset;
                else
-                       last = key.objectid + root->leafsize;
+                       last = key.objectid + root->nodesize;
                path->slots[0]++;
        }
 
@@ -5399,13 +5523,33 @@ static int check_space_cache(struct btrfs_root *root)
                        btrfs_remove_free_space_cache(cache);
                }
 
-               ret = load_free_space_cache(root->fs_info, cache);
-               if (!ret)
-                       continue;
+               if (btrfs_fs_compat_ro(root->fs_info,
+                                      BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE)) {
+                       ret = exclude_super_stripes(root, cache);
+                       if (ret) {
+                               fprintf(stderr, "could not exclude super stripes: %s\n",
+                                       strerror(-ret));
+                               error++;
+                               continue;
+                       }
+                       ret = load_free_space_tree(root->fs_info, cache);
+                       free_excluded_extents(root, cache);
+                       if (ret < 0) {
+                               fprintf(stderr, "could not load free space tree: %s\n",
+                                       strerror(-ret));
+                               error++;
+                               continue;
+                       }
+                       error += ret;
+               } else {
+                       ret = load_free_space_cache(root->fs_info, cache);
+                       if (!ret)
+                               continue;
+               }
 
                ret = verify_space_cache(root, cache);
                if (ret) {
-                       fprintf(stderr, "cache appears valid but isnt %Lu\n",
+                       fprintf(stderr, "cache appears valid but isn't %Lu\n",
                                cache->key.objectid);
                        error++;
                }
@@ -5495,7 +5639,7 @@ static int check_extent_exists(struct btrfs_root *root, u64 bytenr,
 
        path = btrfs_alloc_path();
        if (!path) {
-               fprintf(stderr, "Error allocing path\n");
+               fprintf(stderr, "Error allocating path\n");
                return -ENOMEM;
        }
 
@@ -5528,7 +5672,7 @@ again:
 
        /*
         * Block group items come before extent items if they have the same
-        * bytenr, so walk back one more just in case.  Dear future traveler,
+        * bytenr, so walk back one more just in case.  Dear future traveller,
         * first congrats on mastering time travel.  Now if it's not too much
         * trouble could you go back to 2006 and tell Chris to make the
         * BLOCK_GROUP_ITEM_KEY (and BTRFS_*_REF_KEY) lower than the
@@ -5737,7 +5881,7 @@ static int is_dropped_key(struct btrfs_key *key,
  * 1) If BTRFS_HEADER_FLAG_RELOC is set then we have FULL_BACKREF set.
  * 2) If btrfs_header_owner(buf) no longer points to buf then we have
  *     FULL_BACKREF set.
- * 3) We cow'ed the block walking down a reloc tree.  This is impossible to tell
+ * 3) We cowed the block walking down a reloc tree.  This is impossible to tell
  *    if it happened after the relocation occurred since we'll have dropped the
  *    reloc root, so it's entirely possible to have FULL_BACKREF set on buf and
  *    have no real way to know for sure.
@@ -5791,13 +5935,13 @@ static int calc_extent_flag(struct btrfs_root *root,
                goto full_backref;
 normal:
        *flags = 0;
-       if (rec->flag_block_full_backref != -1 &&
+       if (rec->flag_block_full_backref != FLAG_UNSET &&
            rec->flag_block_full_backref != 0)
                rec->bad_full_backref = 1;
        return 0;
 full_backref:
        *flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
-       if (rec->flag_block_full_backref != -1 &&
+       if (rec->flag_block_full_backref != FLAG_UNSET &&
            rec->flag_block_full_backref != 1)
                rec->bad_full_backref = 1;
        return 0;
@@ -6079,8 +6223,10 @@ static int run_next_block(struct btrfs_root *root,
                        btrfs_item_key_to_cpu(buf, &first_key, 0);
                level = btrfs_header_level(buf);
                for (i = 0; i < nritems; i++) {
+                       struct extent_record tmpl;
+
                        ptr = btrfs_node_blockptr(buf, i);
-                       size = btrfs_level_size(root, level - 1);
+                       size = root->nodesize;
                        btrfs_node_key_to_cpu(buf, &key, i);
                        if (ri != NULL) {
                                if ((level == ri->drop_level)
@@ -6088,10 +6234,16 @@ static int run_next_block(struct btrfs_root *root,
                                        continue;
                                }
                        }
-                       ret = add_extent_rec(extent_cache, &key,
-                                            btrfs_node_ptr_generation(buf, i),
-                                            ptr, size, 0, 0, 1, 0, 1, 0,
-                                            size);
+
+                       memset(&tmpl, 0, sizeof(tmpl));
+                       btrfs_cpu_key_to_disk(&tmpl.parent_key, &key);
+                       tmpl.parent_generation = btrfs_node_ptr_generation(buf, i);
+                       tmpl.start = ptr;
+                       tmpl.nr = size;
+                       tmpl.refs = 1;
+                       tmpl.metadata = 1;
+                       tmpl.max_size = size;
+                       ret = add_extent_rec(extent_cache, &tmpl);
                        BUG_ON(ret);
 
                        add_tree_backref(extent_cache, ptr, parent, owner, 1);
@@ -6127,12 +6279,21 @@ static int add_root_to_pending(struct extent_buffer *buf,
                               struct cache_tree *nodes,
                               u64 objectid)
 {
+       struct extent_record tmpl;
+
        if (btrfs_header_level(buf) > 0)
                add_pending(nodes, seen, buf->start, buf->len);
        else
                add_pending(pending, seen, buf->start, buf->len);
-       add_extent_rec(extent_cache, NULL, 0, buf->start, buf->len,
-                      0, 1, 1, 0, 1, 0, buf->len);
+
+       memset(&tmpl, 0, sizeof(tmpl));
+       tmpl.start = buf->start;
+       tmpl.nr = buf->len;
+       tmpl.is_root = 1;
+       tmpl.refs = 1;
+       tmpl.metadata = 1;
+       tmpl.max_size = buf->len;
+       add_extent_rec(extent_cache, &tmpl);
 
        if (objectid == BTRFS_TREE_RELOC_OBJECTID ||
            btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
@@ -6281,7 +6442,7 @@ static int delete_extent_records(struct btrfs_trans_handle *trans,
                if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
                    found_key.type == BTRFS_METADATA_ITEM_KEY) {
                        u64 bytes = (found_key.type == BTRFS_EXTENT_ITEM_KEY) ?
-                               found_key.offset : root->leafsize;
+                               found_key.offset : root->nodesize;
 
                        ret = btrfs_update_block_group(trans, root, bytenr,
                                                       bytes, 0, 0);
@@ -6316,7 +6477,7 @@ static int record_extent(struct btrfs_trans_handle *trans,
 
        if (!back->is_data)
                rec->max_size = max_t(u64, rec->max_size,
-                                   info->extent_root->leafsize);
+                                   info->extent_root->nodesize);
 
        if (!allocated) {
                u32 item_size = sizeof(*ei);
@@ -6431,14 +6592,6 @@ fail:
        return ret;
 }
 
-struct extent_entry {
-       u64 bytenr;
-       u64 bytes;
-       int count;
-       int broken;
-       struct list_head list;
-};
-
 static struct extent_entry *find_entry(struct list_head *entries,
                                       u64 bytenr, u64 bytes)
 {
@@ -6937,7 +7090,7 @@ static int delete_duplicate_records(struct btrfs_root *root,
 
                if (tmp->start + tmp->nr < good->start + good->nr) {
                        fprintf(stderr, "Ok we have overlapping extents that "
-                               "aren't completely covered by eachother, this "
+                               "aren't completely covered by each other, this "
                                "is going to require more careful thought.  "
                                "The extents are [%Lu-%Lu] and [%Lu-%Lu]\n",
                                tmp->start, tmp->nr, good->start, good->nr);
@@ -8103,14 +8256,14 @@ again:
        level = btrfs_header_level(root1->node);
        ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
                                    root1->node->start, 0, level, 0,
-                                   btrfs_level_size(root1, level), NULL);
+                                   root1->nodesize, NULL);
        if (ret < 0)
                goto out;
        root1 = root->fs_info->chunk_root;
        level = btrfs_header_level(root1->node);
        ret = add_root_item_to_list(&normal_trees, root1->root_key.objectid,
                                    root1->node->start, 0, level, 0,
-                                   btrfs_level_size(root1, level), NULL);
+                                   root1->nodesize, NULL);
        if (ret < 0)
                goto out;
        btrfs_init_path(&path);
@@ -8141,7 +8294,7 @@ again:
                        last_snapshot = btrfs_root_last_snapshot(&ri);
                        if (btrfs_disk_key_objectid(&ri.drop_progress) == 0) {
                                level = btrfs_root_level(&ri);
-                               level_size = btrfs_level_size(root, level);
+                               level_size = root->nodesize;
                                ret = add_root_item_to_list(&normal_trees,
                                                found_key.objectid,
                                                btrfs_root_bytenr(&ri),
@@ -8151,7 +8304,7 @@ again:
                                        goto out;
                        } else {
                                level = btrfs_root_level(&ri);
-                               level_size = btrfs_level_size(root, level);
+                               level_size = root->nodesize;
                                objectid = found_key.objectid;
                                btrfs_disk_key_to_cpu(&found_key,
                                                      &ri.drop_progress);
@@ -8266,7 +8419,7 @@ static int btrfs_fsck_reinit_root(struct btrfs_trans_handle *trans,
                goto init;
        }
        c = btrfs_alloc_free_block(trans, root,
-                                  btrfs_level_size(root, 0),
+                                  root->nodesize,
                                   root->root_key.objectid,
                                   &disk_key, level, 0, 0);
        if (IS_ERR(c)) {
@@ -8323,7 +8476,7 @@ static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
        struct btrfs_root_item *ri;
        struct btrfs_key key;
        u64 bytenr;
-       u32 leafsize;
+       u32 nodesize;
        int level = btrfs_header_level(eb);
        int nritems;
        int ret;
@@ -8340,7 +8493,7 @@ static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
 
        btrfs_pin_extent(fs_info, eb->start, eb->len);
 
-       leafsize = btrfs_super_leafsize(fs_info->super_copy);
+       nodesize = btrfs_super_nodesize(fs_info->super_copy);
        nritems = btrfs_header_nritems(eb);
        for (i = 0; i < nritems; i++) {
                if (level == 0) {
@@ -8362,7 +8515,7 @@ static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
                         * just pass in extent_root.
                         */
                        tmp = read_tree_block(fs_info->extent_root, bytenr,
-                                             leafsize, 0);
+                                             nodesize, 0);
                        if (!extent_buffer_uptodate(tmp)) {
                                fprintf(stderr, "Error reading root block\n");
                                return -EIO;
@@ -8376,12 +8529,12 @@ static int pin_down_tree_blocks(struct btrfs_fs_info *fs_info,
 
                        /* If we aren't the tree root don't read the block */
                        if (level == 1 && !tree_root) {
-                               btrfs_pin_extent(fs_info, bytenr, leafsize);
+                               btrfs_pin_extent(fs_info, bytenr, nodesize);
                                continue;
                        }
 
                        tmp = read_tree_block(fs_info->extent_root, bytenr,
-                                             leafsize, 0);
+                                             nodesize, 0);
                        if (!extent_buffer_uptodate(tmp)) {
                                fprintf(stderr, "Error reading tree block\n");
                                return -EIO;
@@ -8671,7 +8824,7 @@ static int reinit_extent_tree(struct btrfs_trans_handle *trans,
 
        ret = reset_balance(trans, fs_info);
        if (ret)
-               fprintf(stderr, "error reseting the pending balance\n");
+               fprintf(stderr, "error resetting the pending balance\n");
 
        return ret;
 }
@@ -9019,18 +9172,6 @@ static int fill_csum_tree(struct btrfs_trans_handle *trans,
                return fill_csum_tree_from_extent(trans, csum_root);
 }
 
-struct root_item_info {
-       /* level of the root */
-       u8 level;
-       /* number of nodes at this level, must be 1 for a root */
-       int node_count;
-       u64 bytenr;
-       u64 gen;
-       struct cache_extent cache_extent;
-};
-
-static struct cache_tree *roots_info_cache = NULL;
-
 static void free_roots_info_cache(void)
 {
        if (!roots_info_cache)
@@ -9120,11 +9261,11 @@ static int build_roots_info_cache(struct btrfs_fs_info *info)
                        iref = (struct btrfs_extent_inline_ref *)(ei + 1);
                        level = found_key.offset;
                } else {
-                       struct btrfs_tree_block_info *info;
+                       struct btrfs_tree_block_info *binfo;
 
-                       info = (struct btrfs_tree_block_info *)(ei + 1);
-                       iref = (struct btrfs_extent_inline_ref *)(info + 1);
-                       level = btrfs_tree_block_level(leaf, info);
+                       binfo = (struct btrfs_tree_block_info *)(ei + 1);
+                       iref = (struct btrfs_extent_inline_ref *)(binfo + 1);
+                       level = btrfs_tree_block_level(leaf, binfo);
                }
 
                /*
@@ -9365,23 +9506,24 @@ out:
 
 const char * const cmd_check_usage[] = {
        "btrfs check [options] <device>",
-       "Check structural inegrity of a filesystem (unmounted).",
-       "Check structural inegrity of an unmounted filesystem. Verify internal",
+       "Check structural integrity of a filesystem (unmounted).",
+       "Check structural integrity of an unmounted filesystem. Verify internal",
        "trees' consistency and item connectivity. In the repair mode try to",
        "fix the problems found.",
        "WARNING: the repair mode is considered dangerous",
        "",
        "-s|--super <superblock>     use this superblock copy",
-       "-b|--backup                 use the backup root copy",
+       "-b|--backup                 use the first valid backup root copy",
        "--repair                    try to repair the filesystem",
        "--readonly                  run in read-only mode (default)",
        "--init-csum-tree            create a new CRC tree",
        "--init-extent-tree          create a new extent tree",
-       "--check-data-csum           verify checkums of data blocks",
+       "--check-data-csum           verify checksums of data blocks",
        "-Q|--qgroup-report           print a report on qgroup consistency",
        "-E|--subvol-extents <subvolid>",
        "                            print subvolume extents and sharing state",
        "-r|--tree-root <bytenr>     use the given bytenr for the tree root",
+       "--chunk-root <bytenr>       use the given bytenr for the chunk tree root",
        "-p|--progress               indicate progress",
        NULL
 };
@@ -9394,6 +9536,7 @@ int cmd_check(int argc, char **argv)
        u64 bytenr = 0;
        u64 subvolid = 0;
        u64 tree_root_bytenr = 0;
+       u64 chunk_root_bytenr = 0;
        char uuidbuf[BTRFS_UUID_UNPARSED_SIZE];
        int ret;
        u64 num;
@@ -9404,19 +9547,25 @@ int cmd_check(int argc, char **argv)
 
        while(1) {
                int c;
-               enum { OPT_REPAIR = 257, OPT_INIT_CSUM, OPT_INIT_EXTENT,
-                       OPT_CHECK_CSUM, OPT_READONLY };
+               enum { GETOPT_VAL_REPAIR = 257, GETOPT_VAL_INIT_CSUM,
+                       GETOPT_VAL_INIT_EXTENT, GETOPT_VAL_CHECK_CSUM,
+                       GETOPT_VAL_READONLY, GETOPT_VAL_CHUNK_TREE };
                static const struct option long_options[] = {
                        { "super", required_argument, NULL, 's' },
-                       { "repair", no_argument, NULL, OPT_REPAIR },
-                       { "readonly", no_argument, NULL, OPT_READONLY },
-                       { "init-csum-tree", no_argument, NULL, OPT_INIT_CSUM },
-                       { "init-extent-tree", no_argument, NULL, OPT_INIT_EXTENT },
-                       { "check-data-csum", no_argument, NULL, OPT_CHECK_CSUM },
+                       { "repair", no_argument, NULL, GETOPT_VAL_REPAIR },
+                       { "readonly", no_argument, NULL, GETOPT_VAL_READONLY },
+                       { "init-csum-tree", no_argument, NULL,
+                               GETOPT_VAL_INIT_CSUM },
+                       { "init-extent-tree", no_argument, NULL,
+                               GETOPT_VAL_INIT_EXTENT },
+                       { "check-data-csum", no_argument, NULL,
+                               GETOPT_VAL_CHECK_CSUM },
                        { "backup", no_argument, NULL, 'b' },
                        { "subvol-extents", required_argument, NULL, 'E' },
                        { "qgroup-report", no_argument, NULL, 'Q' },
                        { "tree-root", required_argument, NULL, 'r' },
+                       { "chunk-root", required_argument, NULL,
+                               GETOPT_VAL_CHUNK_TREE },
                        { "progress", no_argument, NULL, 'p' },
                        { NULL, 0, NULL, 0}
                };
@@ -9450,40 +9599,42 @@ int cmd_check(int argc, char **argv)
                        case 'r':
                                tree_root_bytenr = arg_strtou64(optarg);
                                break;
+                       case GETOPT_VAL_CHUNK_TREE:
+                               chunk_root_bytenr = arg_strtou64(optarg);
+                               break;
                        case 'p':
                                ctx.progress_enabled = true;
                                break;
                        case '?':
                        case 'h':
                                usage(cmd_check_usage);
-                       case OPT_REPAIR:
+                       case GETOPT_VAL_REPAIR:
                                printf("enabling repair mode\n");
                                repair = 1;
                                ctree_flags |= OPEN_CTREE_WRITES;
                                break;
-                       case OPT_READONLY:
+                       case GETOPT_VAL_READONLY:
                                readonly = 1;
                                break;
-                       case OPT_INIT_CSUM:
+                       case GETOPT_VAL_INIT_CSUM:
                                printf("Creating a new CRC tree\n");
                                init_csum_tree = 1;
                                repair = 1;
                                ctree_flags |= OPEN_CTREE_WRITES;
                                break;
-                       case OPT_INIT_EXTENT:
+                       case GETOPT_VAL_INIT_EXTENT:
                                init_extent_tree = 1;
                                ctree_flags |= (OPEN_CTREE_WRITES |
                                                OPEN_CTREE_NO_BLOCK_GROUPS);
                                repair = 1;
                                break;
-                       case OPT_CHECK_CSUM:
+                       case GETOPT_VAL_CHECK_CSUM:
                                check_data_csum = 1;
                                break;
                }
        }
-       argc = argc - optind;
 
-       if (check_argc_exact(argc, 1))
+       if (check_argc_exact(argc - optind, 1))
                usage(cmd_check_usage);
 
        if (ctx.progress_enabled) {
@@ -9514,7 +9665,7 @@ int cmd_check(int argc, char **argv)
                ctree_flags |= OPEN_CTREE_PARTIAL;
 
        info = open_ctree_fs_info(argv[optind], bytenr, tree_root_bytenr,
-                                 ctree_flags);
+                                 chunk_root_bytenr, ctree_flags);
        if (!info) {
                fprintf(stderr, "Couldn't open file system\n");
                ret = -EIO;
@@ -9547,7 +9698,7 @@ int cmd_check(int argc, char **argv)
                       uuidbuf);
                ret = qgroup_verify_all(info);
                if (ret == 0)
-                       print_qgroup_report(1);
+                       ret = report_qgroups(1);
                goto close_out;
        }
        if (subvolid) {
@@ -9640,8 +9791,12 @@ int cmd_check(int argc, char **argv)
                goto close_out;
        }
 
-       if (!ctx.progress_enabled)
-               fprintf(stderr, "checking free space cache\n");
+       if (!ctx.progress_enabled) {
+               if (btrfs_fs_compat_ro(info, BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE))
+                       fprintf(stderr, "checking free space tree\n");
+               else
+                       fprintf(stderr, "checking free space cache\n");
+       }
        ret = check_space_cache(root);
        if (ret)
                goto out;
@@ -9704,7 +9859,11 @@ int cmd_check(int argc, char **argv)
                ret = 1;
        }
 out:
-       print_qgroup_report(0);
+       /* Don't override original ret */
+       if (ret)
+               report_qgroups(0);
+       else
+               ret = report_qgroups(0);
        if (found_old_backref) { /*
                 * there was a disk format change when mixed
                 * backref was in testing tree. The old format
@@ -9730,12 +9889,10 @@ out:
        printf("file data blocks allocated: %llu\n referenced %llu\n",
                (unsigned long long)data_bytes_allocated,
                (unsigned long long)data_bytes_referenced);
-       printf("%s\n", PACKAGE_STRING);
 
        free_root_recs_tree(&root_cache);
 close_out:
        close_ctree(root);
-       btrfs_close_all_devices();
 err_out:
        if (ctx.progress_enabled)
                task_deinit(ctx.info);