+static int swap_values(struct btrfs_root *root, struct btrfs_path *path,
+ struct extent_buffer *buf, int slot)
+{
+ if (btrfs_header_level(buf)) {
+ struct btrfs_key_ptr ptr1, ptr2;
+
+ read_extent_buffer(buf, &ptr1, btrfs_node_key_ptr_offset(slot),
+ sizeof(struct btrfs_key_ptr));
+ read_extent_buffer(buf, &ptr2,
+ btrfs_node_key_ptr_offset(slot + 1),
+ sizeof(struct btrfs_key_ptr));
+ write_extent_buffer(buf, &ptr1,
+ btrfs_node_key_ptr_offset(slot + 1),
+ sizeof(struct btrfs_key_ptr));
+ write_extent_buffer(buf, &ptr2,
+ btrfs_node_key_ptr_offset(slot),
+ sizeof(struct btrfs_key_ptr));
+ if (slot == 0) {
+ struct btrfs_disk_key key;
+ btrfs_node_key(buf, &key, 0);
+ btrfs_fixup_low_keys(root, path, &key,
+ btrfs_header_level(buf) + 1);
+ }
+ } else {
+ struct btrfs_item *item1, *item2;
+ struct btrfs_key k1, k2;
+ char *item1_data, *item2_data;
+ u32 item1_offset, item2_offset, item1_size, item2_size;
+
+ item1 = btrfs_item_nr(slot);
+ item2 = btrfs_item_nr(slot + 1);
+ btrfs_item_key_to_cpu(buf, &k1, slot);
+ btrfs_item_key_to_cpu(buf, &k2, slot + 1);
+ item1_offset = btrfs_item_offset(buf, item1);
+ item2_offset = btrfs_item_offset(buf, item2);
+ item1_size = btrfs_item_size(buf, item1);
+ item2_size = btrfs_item_size(buf, item2);
+
+ item1_data = malloc(item1_size);
+ if (!item1_data)
+ return -ENOMEM;
+ item2_data = malloc(item2_size);
+ if (!item2_data) {
+ free(item2_data);
+ return -ENOMEM;
+ }
+
+ read_extent_buffer(buf, item1_data, item1_offset, item1_size);
+ read_extent_buffer(buf, item2_data, item2_offset, item2_size);
+
+ write_extent_buffer(buf, item1_data, item2_offset, item2_size);
+ write_extent_buffer(buf, item2_data, item1_offset, item1_size);
+ free(item1_data);
+ free(item2_data);
+
+ btrfs_set_item_offset(buf, item1, item2_offset);
+ btrfs_set_item_offset(buf, item2, item1_offset);
+ btrfs_set_item_size(buf, item1, item2_size);
+ btrfs_set_item_size(buf, item2, item1_size);
+
+ path->slots[0] = slot;
+ btrfs_set_item_key_unsafe(root, path, &k2);
+ path->slots[0] = slot + 1;
+ btrfs_set_item_key_unsafe(root, path, &k1);
+ }
+ return 0;
+}
+
+/*
+ * Attempt to fix basic block failures. Currently we only handle bad key
+ * orders, we will cycle through the keys and swap them if necessary.
+ */
+static int try_to_fix_bad_block(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,
+ struct extent_buffer *buf,
+ struct btrfs_disk_key *parent_key,
+ enum btrfs_tree_block_status status)
+{
+ struct btrfs_path *path;
+ struct btrfs_key k1, k2;
+ int i;
+ int ret;
+
+ if (status != BTRFS_TREE_BLOCK_BAD_KEY_ORDER)
+ return -EIO;
+
+ k1.objectid = btrfs_header_owner(buf);
+ k1.type = BTRFS_ROOT_ITEM_KEY;
+ k1.offset = (u64)-1;
+
+ root = btrfs_read_fs_root(root->fs_info, &k1);
+ if (IS_ERR(root))
+ return -EIO;
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -EIO;
+
+ path->lowest_level = btrfs_header_level(buf);
+ path->skip_check_block = 1;
+ if (btrfs_header_level(buf))
+ btrfs_node_key_to_cpu(buf, &k1, 0);
+ else
+ btrfs_item_key_to_cpu(buf, &k1, 0);
+
+ ret = btrfs_search_slot(trans, root, &k1, path, 0, 1);
+ if (ret) {
+ btrfs_free_path(path);
+ return -EIO;
+ }
+
+ buf = path->nodes[0];
+ for (i = 0; i < btrfs_header_nritems(buf) - 1; i++) {
+ if (btrfs_header_level(buf)) {
+ btrfs_node_key_to_cpu(buf, &k1, i);
+ btrfs_node_key_to_cpu(buf, &k2, i + 1);
+ } else {
+ btrfs_item_key_to_cpu(buf, &k1, i);
+ btrfs_item_key_to_cpu(buf, &k2, i + 1);
+ }
+ if (btrfs_comp_cpu_keys(&k1, &k2) < 0)
+ continue;
+ ret = swap_values(root, path, buf, i);
+ if (ret)
+ break;
+ btrfs_mark_buffer_dirty(buf);
+ i = 0;
+ }
+
+ btrfs_free_path(path);
+ return ret;
+}
+
+static int check_block(struct btrfs_trans_handle *trans,
+ struct btrfs_root *root,