btrfs-progs: fix error checking in load_device_info
[platform/upstream/btrfs-progs.git] / cmds-check.c
index 31ed589..4225b21 100644 (file)
@@ -186,7 +186,7 @@ static u64 first_extent_gap(struct rb_root *holes)
        return hole->start;
 }
 
-int compare_hole(struct rb_node *node1, struct rb_node *node2)
+static int compare_hole(struct rb_node *node1, struct rb_node *node2)
 {
        struct file_extent_hole *hole1;
        struct file_extent_hole *hole2;
@@ -2404,7 +2404,7 @@ static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
                                  BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino,
                                  mode);
                if (ret < 0) {
-                       fprintf(stderr, "Failed to create '%s' dir: %s",
+                       fprintf(stderr, "Failed to create '%s' dir: %s\n",
                                dir_name, strerror(-ret));
                        goto out;
                }
@@ -2432,7 +2432,7 @@ static int repair_inode_nlinks(struct btrfs_trans_handle *trans,
                }
                if (ret < 0) {
                        fprintf(stderr,
-                               "Failed to link the inode %llu to %s dir: %s",
+                               "Failed to link the inode %llu to %s dir: %s\n",
                                rec->ino, dir_name, strerror(-ret));
                        goto out;
                }
@@ -2665,11 +2665,13 @@ static int repair_inode_discount_extent(struct btrfs_trans_handle *trans,
 {
        struct rb_node *node;
        struct file_extent_hole *hole;
+       int found = 0;
        int ret = 0;
 
        node = rb_first(&rec->holes);
 
        while (node) {
+               found = 1;
                hole = rb_entry(node, struct file_extent_hole, node);
                ret = btrfs_punch_hole(trans, root, rec->ino,
                                       hole->start, hole->len);
@@ -2683,6 +2685,13 @@ static int repair_inode_discount_extent(struct btrfs_trans_handle *trans,
                        rec->errors &= ~I_ERR_FILE_EXTENT_DISCOUNT;
                node = rb_first(&rec->holes);
        }
+       /* special case for a file losing all its file extent */
+       if (!found) {
+               ret = btrfs_punch_hole(trans, root, rec->ino, 0,
+                                      round_up(rec->isize, root->sectorsize));
+               if (ret < 0)
+                       goto out;
+       }
        printf("Fixed discount file extents for inode: %llu in root: %llu\n",
               rec->ino, root->objectid);
 out:
@@ -2960,7 +2969,7 @@ static struct root_backref *get_root_backref(struct root_record *rec,
        }
 
        backref = malloc(sizeof(*backref) + namelen + 1);
-       memset(backref, 0, sizeof(*backref));
+       memset(backref, 0, sizeof(*backref) + namelen + 1);
        backref->ref_root = ref_root;
        backref->dir = dir;
        backref->index = index;
@@ -4410,6 +4419,7 @@ static int add_extent_rec(struct cache_tree *extent_cache,
        rec->metadata = metadata;
        rec->flag_block_full_backref = -1;
        rec->bad_full_backref = 0;
+       rec->crossing_stripes = 0;
        INIT_LIST_HEAD(&rec->backrefs);
        INIT_LIST_HEAD(&rec->dups);
        INIT_LIST_HEAD(&rec->list);
@@ -9217,8 +9227,7 @@ next:
        ret = 0;
 out:
        free_roots_info_cache();
-       if (path)
-               btrfs_free_path(path);
+       btrfs_free_path(path);
        if (trans)
                btrfs_commit_transaction(trans, info->tree_root);
        if (ret < 0)
@@ -9579,6 +9588,7 @@ out:
        free_root_recs_tree(&root_cache);
 close_out:
        close_ctree(root);
+       btrfs_close_all_devices();
 err_out:
        return ret;
 }