Merge branch 'cleanup/blocksize-diet-part2' of git://git.kernel.org/pub/scm/linux...
authorChris Mason <clm@fb.com>
Thu, 22 Jan 2015 01:49:35 +0000 (17:49 -0800)
committerChris Mason <clm@fb.com>
Thu, 22 Jan 2015 01:49:35 +0000 (17:49 -0800)
1  2 
fs/btrfs/ctree.c
fs/btrfs/disk-io.c
fs/btrfs/extent-tree.c
fs/btrfs/tree-log.c
fs/btrfs/volumes.c

diff --combined fs/btrfs/ctree.c
@@@ -213,19 -213,11 +213,19 @@@ static struct extent_buffer *btrfs_read
   */
  static void add_root_to_dirty_list(struct btrfs_root *root)
  {
 +      if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
 +          !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
 +              return;
 +
        spin_lock(&root->fs_info->trans_lock);
 -      if (test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state) &&
 -          list_empty(&root->dirty_list)) {
 -              list_add(&root->dirty_list,
 -                       &root->fs_info->dirty_cowonly_roots);
 +      if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
 +              /* Want the extent tree to be the last on the list */
 +              if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
 +                      list_move_tail(&root->dirty_list,
 +                                     &root->fs_info->dirty_cowonly_roots);
 +              else
 +                      list_move(&root->dirty_list,
 +                                &root->fs_info->dirty_cowonly_roots);
        }
        spin_unlock(&root->fs_info->trans_lock);
  }
@@@ -1371,8 -1363,7 +1371,7 @@@ tree_mod_log_rewind(struct btrfs_fs_inf
  
        if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
                BUG_ON(tm->slot != 0);
-               eb_rewin = alloc_dummy_extent_buffer(eb->start,
-                                               fs_info->tree_root->nodesize);
+               eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
                if (!eb_rewin) {
                        btrfs_tree_read_unlock_blocking(eb);
                        free_extent_buffer(eb);
@@@ -1452,7 -1443,7 +1451,7 @@@ get_old_root(struct btrfs_root *root, u
        } else if (old_root) {
                btrfs_tree_read_unlock(eb_root);
                free_extent_buffer(eb_root);
-               eb = alloc_dummy_extent_buffer(logical, root->nodesize);
+               eb = alloc_dummy_extent_buffer(root->fs_info, logical);
        } else {
                btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
                eb = btrfs_clone_extent_buffer(eb_root);
@@@ -2290,7 -2281,7 +2289,7 @@@ static void reada_for_search(struct btr
                if ((search <= target && target - search <= 65536) ||
                    (search > target && search - target <= 65536)) {
                        gen = btrfs_node_ptr_generation(node, nr);
-                       readahead_tree_block(root, search, blocksize);
+                       readahead_tree_block(root, search);
                        nread += blocksize;
                }
                nscan++;
@@@ -2309,7 -2300,6 +2308,6 @@@ static noinline void reada_for_balance(
        u64 gen;
        u64 block1 = 0;
        u64 block2 = 0;
-       int blocksize;
  
        parent = path->nodes[level + 1];
        if (!parent)
  
        nritems = btrfs_header_nritems(parent);
        slot = path->slots[level + 1];
-       blocksize = root->nodesize;
  
        if (slot > 0) {
                block1 = btrfs_node_blockptr(parent, slot - 1);
        }
  
        if (block1)
-               readahead_tree_block(root, block1, blocksize);
+               readahead_tree_block(root, block1);
        if (block2)
-               readahead_tree_block(root, block2, blocksize);
+               readahead_tree_block(root, block2);
  }
  
  
@@@ -2617,24 -2606,32 +2614,24 @@@ static int key_search(struct extent_buf
        return 0;
  }
  
 -int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
 +int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
                u64 iobjectid, u64 ioff, u8 key_type,
                struct btrfs_key *found_key)
  {
        int ret;
        struct btrfs_key key;
        struct extent_buffer *eb;
 -      struct btrfs_path *path;
 +
 +      ASSERT(path);
 +      ASSERT(found_key);
  
        key.type = key_type;
        key.objectid = iobjectid;
        key.offset = ioff;
  
 -      if (found_path == NULL) {
 -              path = btrfs_alloc_path();
 -              if (!path)
 -                      return -ENOMEM;
 -      } else
 -              path = found_path;
 -
        ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
 -      if ((ret < 0) || (found_key == NULL)) {
 -              if (path != found_path)
 -                      btrfs_free_path(path);
 +      if (ret < 0)
                return ret;
 -      }
  
        eb = path->nodes[0];
        if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
diff --combined fs/btrfs/disk-io.c
@@@ -1073,12 -1073,12 +1073,12 @@@ static const struct address_space_opera
        .set_page_dirty = btree_set_page_dirty,
  };
  
- void readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
+ void readahead_tree_block(struct btrfs_root *root, u64 bytenr)
  {
        struct extent_buffer *buf = NULL;
        struct inode *btree_inode = root->fs_info->btree_inode;
  
-       buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
+       buf = btrfs_find_create_tree_block(root, bytenr);
        if (!buf)
                return;
        read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
        free_extent_buffer(buf);
  }
  
- int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize,
+ int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr,
                         int mirror_num, struct extent_buffer **eb)
  {
        struct extent_buffer *buf = NULL;
        struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
        int ret;
  
-       buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
+       buf = btrfs_find_create_tree_block(root, bytenr);
        if (!buf)
                return 0;
  
@@@ -1125,12 -1125,11 +1125,11 @@@ struct extent_buffer *btrfs_find_tree_b
  }
  
  struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
-                                                u64 bytenr, u32 blocksize)
+                                                u64 bytenr)
  {
        if (btrfs_test_is_dummy_root(root))
-               return alloc_test_extent_buffer(root->fs_info, bytenr,
-                                               blocksize);
-       return alloc_extent_buffer(root->fs_info, bytenr, blocksize);
+               return alloc_test_extent_buffer(root->fs_info, bytenr);
+       return alloc_extent_buffer(root->fs_info, bytenr);
  }
  
  
@@@ -1152,7 -1151,7 +1151,7 @@@ struct extent_buffer *read_tree_block(s
        struct extent_buffer *buf = NULL;
        int ret;
  
-       buf = btrfs_find_create_tree_block(root, bytenr, root->nodesize);
+       buf = btrfs_find_create_tree_block(root, bytenr);
        if (!buf)
                return NULL;
  
@@@ -1630,8 -1629,6 +1629,8 @@@ struct btrfs_root *btrfs_get_fs_root(st
                                     bool check_ref)
  {
        struct btrfs_root *root;
 +      struct btrfs_path *path;
 +      struct btrfs_key key;
        int ret;
  
        if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
@@@ -1671,17 -1668,8 +1670,17 @@@ again
        if (ret)
                goto fail;
  
 -      ret = btrfs_find_item(fs_info->tree_root, NULL, BTRFS_ORPHAN_OBJECTID,
 -                      location->objectid, BTRFS_ORPHAN_ITEM_KEY, NULL);
 +      path = btrfs_alloc_path();
 +      if (!path) {
 +              ret = -ENOMEM;
 +              goto fail;
 +      }
 +      key.objectid = BTRFS_ORPHAN_OBJECTID;
 +      key.type = BTRFS_ORPHAN_ITEM_KEY;
 +      key.offset = location->objectid;
 +
 +      ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
 +      btrfs_free_path(path);
        if (ret < 0)
                goto fail;
        if (ret == 0)
@@@ -4132,6 -4120,12 +4131,6 @@@ again
                if (ret)
                        break;
  
 -              /* opt_discard */
 -              if (btrfs_test_opt(root, DISCARD))
 -                      ret = btrfs_error_discard_extent(root, start,
 -                                                       end + 1 - start,
 -                                                       NULL);
 -
                clear_extent_dirty(unpin, start, end, GFP_NOFS);
                btrfs_error_unpin_extent_range(root, start, end);
                cond_resched();
diff --combined fs/btrfs/extent-tree.c
@@@ -74,9 -74,8 +74,9 @@@ enum 
        RESERVE_ALLOC_NO_ACCOUNT = 2,
  };
  
 -static int update_block_group(struct btrfs_root *root,
 -                            u64 bytenr, u64 num_bytes, int alloc);
 +static int update_block_group(struct btrfs_trans_handle *trans,
 +                            struct btrfs_root *root, u64 bytenr,
 +                            u64 num_bytes, int alloc);
  static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
                                struct btrfs_root *root,
                                u64 bytenr, u64 num_bytes, u64 parent,
@@@ -1890,8 -1889,8 +1890,8 @@@ static int btrfs_issue_discard(struct b
        return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
  }
  
 -static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
 -                              u64 num_bytes, u64 *actual_bytes)
 +int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
 +                       u64 num_bytes, u64 *actual_bytes)
  {
        int ret;
        u64 discarded_bytes = 0;
@@@ -3140,11 -3139,9 +3140,11 @@@ static int write_one_cache_group(struc
        struct extent_buffer *leaf;
  
        ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
 -      if (ret < 0)
 +      if (ret) {
 +              if (ret > 0)
 +                      ret = -ENOENT;
                goto fail;
 -      BUG_ON(ret); /* Corruption */
 +      }
  
        leaf = path->nodes[0];
        bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
        btrfs_mark_buffer_dirty(leaf);
        btrfs_release_path(path);
  fail:
 -      if (ret) {
 +      if (ret)
                btrfs_abort_transaction(trans, root, ret);
 -              return ret;
 -      }
 -      return 0;
 +      return ret;
  
  }
  
@@@ -3316,42 -3315,120 +3316,42 @@@ int btrfs_write_dirty_block_groups(stru
                                   struct btrfs_root *root)
  {
        struct btrfs_block_group_cache *cache;
 -      int err = 0;
 +      struct btrfs_transaction *cur_trans = trans->transaction;
 +      int ret = 0;
        struct btrfs_path *path;
 -      u64 last = 0;
 +
 +      if (list_empty(&cur_trans->dirty_bgs))
 +              return 0;
  
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
  
 -again:
 -      while (1) {
 -              cache = btrfs_lookup_first_block_group(root->fs_info, last);
 -              while (cache) {
 -                      if (cache->disk_cache_state == BTRFS_DC_CLEAR)
 -                              break;
 -                      cache = next_block_group(root, cache);
 -              }
 -              if (!cache) {
 -                      if (last == 0)
 -                              break;
 -                      last = 0;
 -                      continue;
 -              }
 -              err = cache_save_setup(cache, trans, path);
 -              last = cache->key.objectid + cache->key.offset;
 -              btrfs_put_block_group(cache);
 -      }
 -
 -      while (1) {
 -              if (last == 0) {
 -                      err = btrfs_run_delayed_refs(trans, root,
 -                                                   (unsigned long)-1);
 -                      if (err) /* File system offline */
 -                              goto out;
 -              }
 -
 -              cache = btrfs_lookup_first_block_group(root->fs_info, last);
 -              while (cache) {
 -                      if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
 -                              btrfs_put_block_group(cache);
 -                              goto again;
 -                      }
 -
 -                      if (cache->dirty)
 -                              break;
 -                      cache = next_block_group(root, cache);
 -              }
 -              if (!cache) {
 -                      if (last == 0)
 -                              break;
 -                      last = 0;
 -                      continue;
 -              }
 -
 -              if (cache->disk_cache_state == BTRFS_DC_SETUP)
 -                      cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
 -              cache->dirty = 0;
 -              last = cache->key.objectid + cache->key.offset;
 -
 -              err = write_one_cache_group(trans, root, path, cache);
 -              btrfs_put_block_group(cache);
 -              if (err) /* File system offline */
 -                      goto out;
 -      }
 -
 -      while (1) {
 -              /*
 -               * I don't think this is needed since we're just marking our
 -               * preallocated extent as written, but just in case it can't
 -               * hurt.
 -               */
 -              if (last == 0) {
 -                      err = btrfs_run_delayed_refs(trans, root,
 -                                                   (unsigned long)-1);
 -                      if (err) /* File system offline */
 -                              goto out;
 -              }
 -
 -              cache = btrfs_lookup_first_block_group(root->fs_info, last);
 -              while (cache) {
 -                      /*
 -                       * Really this shouldn't happen, but it could if we
 -                       * couldn't write the entire preallocated extent and
 -                       * splitting the extent resulted in a new block.
 -                       */
 -                      if (cache->dirty) {
 -                              btrfs_put_block_group(cache);
 -                              goto again;
 -                      }
 -                      if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
 -                              break;
 -                      cache = next_block_group(root, cache);
 -              }
 -              if (!cache) {
 -                      if (last == 0)
 -                              break;
 -                      last = 0;
 -                      continue;
 -              }
 -
 -              err = btrfs_write_out_cache(root, trans, cache, path);
 -
 -              /*
 -               * If we didn't have an error then the cache state is still
 -               * NEED_WRITE, so we can set it to WRITTEN.
 -               */
 -              if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
 -                      cache->disk_cache_state = BTRFS_DC_WRITTEN;
 -              last = cache->key.objectid + cache->key.offset;
 +      /*
 +       * We don't need the lock here since we are protected by the transaction
 +       * commit.  We want to do the cache_save_setup first and then run the
 +       * delayed refs to make sure we have the best chance at doing this all
 +       * in one shot.
 +       */
 +      while (!list_empty(&cur_trans->dirty_bgs)) {
 +              cache = list_first_entry(&cur_trans->dirty_bgs,
 +                                       struct btrfs_block_group_cache,
 +                                       dirty_list);
 +              list_del_init(&cache->dirty_list);
 +              if (cache->disk_cache_state == BTRFS_DC_CLEAR)
 +                      cache_save_setup(cache, trans, path);
 +              if (!ret)
 +                      ret = btrfs_run_delayed_refs(trans, root,
 +                                                   (unsigned long) -1);
 +              if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP)
 +                      btrfs_write_out_cache(root, trans, cache, path);
 +              if (!ret)
 +                      ret = write_one_cache_group(trans, root, path, cache);
                btrfs_put_block_group(cache);
        }
 -out:
  
        btrfs_free_path(path);
 -      return err;
 +      return ret;
  }
  
  int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
@@@ -5298,9 -5375,8 +5298,9 @@@ void btrfs_delalloc_release_space(struc
        btrfs_free_reserved_data_space(inode, num_bytes);
  }
  
 -static int update_block_group(struct btrfs_root *root,
 -                            u64 bytenr, u64 num_bytes, int alloc)
 +static int update_block_group(struct btrfs_trans_handle *trans,
 +                            struct btrfs_root *root, u64 bytenr,
 +                            u64 num_bytes, int alloc)
  {
        struct btrfs_block_group_cache *cache = NULL;
        struct btrfs_fs_info *info = root->fs_info;
                if (!alloc && cache->cached == BTRFS_CACHE_NO)
                        cache_block_group(cache, 1);
  
 +              spin_lock(&trans->transaction->dirty_bgs_lock);
 +              if (list_empty(&cache->dirty_list)) {
 +                      list_add_tail(&cache->dirty_list,
 +                                    &trans->transaction->dirty_bgs);
 +                      btrfs_get_block_group(cache);
 +              }
 +              spin_unlock(&trans->transaction->dirty_bgs_lock);
 +
                byte_in_group = bytenr - cache->key.objectid;
                WARN_ON(byte_in_group > cache->key.offset);
  
                    cache->disk_cache_state < BTRFS_DC_CLEAR)
                        cache->disk_cache_state = BTRFS_DC_CLEAR;
  
 -              cache->dirty = 1;
                old_val = btrfs_block_group_used(&cache->item);
                num_bytes = min(total, cache->key.offset - byte_in_group);
                if (alloc) {
@@@ -5658,8 -5727,7 +5658,8 @@@ void btrfs_prepare_extent_commit(struc
        update_global_block_rsv(fs_info);
  }
  
 -static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
 +static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end,
 +                            const bool return_free_space)
  {
        struct btrfs_fs_info *fs_info = root->fs_info;
        struct btrfs_block_group_cache *cache = NULL;
  
                if (start < cache->last_byte_to_unpin) {
                        len = min(len, cache->last_byte_to_unpin - start);
 -                      btrfs_add_free_space(cache, start, len);
 +                      if (return_free_space)
 +                              btrfs_add_free_space(cache, start, len);
                }
  
                start += len;
@@@ -5748,7 -5815,7 +5748,7 @@@ int btrfs_finish_extent_commit(struct b
                                                   end + 1 - start, NULL);
  
                clear_extent_dirty(unpin, start, end, GFP_NOFS);
 -              unpin_extent_range(root, start, end);
 +              unpin_extent_range(root, start, end, true);
                cond_resched();
        }
  
@@@ -6034,7 -6101,7 +6034,7 @@@ static int __btrfs_free_extent(struct b
                        }
                }
  
 -              ret = update_block_group(root, bytenr, num_bytes, 0);
 +              ret = update_block_group(trans, root, bytenr, num_bytes, 0);
                if (ret) {
                        btrfs_abort_transaction(trans, extent_root, ret);
                        goto out;
@@@ -6994,7 -7061,7 +6994,7 @@@ static int alloc_reserved_file_extent(s
        if (ret)
                return ret;
  
 -      ret = update_block_group(root, ins->objectid, ins->offset, 1);
 +      ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
        if (ret) { /* -ENOENT, logic error */
                btrfs_err(fs_info, "update block group failed for %llu %llu",
                        ins->objectid, ins->offset);
@@@ -7083,8 -7150,7 +7083,8 @@@ static int alloc_reserved_tree_block(st
                        return ret;
        }
  
 -      ret = update_block_group(root, ins->objectid, root->nodesize, 1);
 +      ret = update_block_group(trans, root, ins->objectid, root->nodesize,
 +                               1);
        if (ret) { /* -ENOENT, logic error */
                btrfs_err(fs_info, "update block group failed for %llu %llu",
                        ins->objectid, ins->offset);
@@@ -7149,11 -7215,11 +7149,11 @@@ int btrfs_alloc_logged_file_extent(stru
  
  static struct extent_buffer *
  btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
-                     u64 bytenr, u32 blocksize, int level)
+                     u64 bytenr, int level)
  {
        struct extent_buffer *buf;
  
-       buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
+       buf = btrfs_find_create_tree_block(root, bytenr);
        if (!buf)
                return ERR_PTR(-ENOMEM);
        btrfs_set_header_generation(buf, trans->transid);
@@@ -7272,7 -7338,7 +7272,7 @@@ struct extent_buffer *btrfs_alloc_tree_
  
        if (btrfs_test_is_dummy_root(root)) {
                buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
-                                           blocksize, level);
+                                           level);
                if (!IS_ERR(buf))
                        root->alloc_bytenr += blocksize;
                return buf;
                return ERR_PTR(ret);
        }
  
-       buf = btrfs_init_new_buffer(trans, root, ins.objectid,
-                                   blocksize, level);
+       buf = btrfs_init_new_buffer(trans, root, ins.objectid, level);
        BUG_ON(IS_ERR(buf)); /* -ENOMEM */
  
        if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
@@@ -7419,7 -7484,7 +7418,7 @@@ static noinline void reada_walk_down(st
                                continue;
                }
  reada:
-               readahead_tree_block(root, bytenr, blocksize);
+               readahead_tree_block(root, bytenr);
                nread++;
        }
        wc->reada_slot = slot;
@@@ -7760,7 -7825,7 +7759,7 @@@ static noinline int do_walk_down(struc
  
        next = btrfs_find_tree_block(root, bytenr);
        if (!next) {
-               next = btrfs_find_create_tree_block(root, bytenr, blocksize);
+               next = btrfs_find_create_tree_block(root, bytenr);
                if (!next)
                        return -ENOMEM;
                btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
@@@ -8806,7 -8871,6 +8805,7 @@@ int btrfs_free_block_groups(struct btrf
                                       cache_node);
                rb_erase(&block_group->cache_node,
                         &info->block_group_cache_tree);
 +              RB_CLEAR_NODE(&block_group->cache_node);
                spin_unlock(&info->block_group_cache_lock);
  
                down_write(&block_group->space_info->groups_sem);
@@@ -8937,7 -9001,6 +8936,7 @@@ btrfs_create_block_group_cache(struct b
        INIT_LIST_HEAD(&cache->cluster_list);
        INIT_LIST_HEAD(&cache->bg_list);
        INIT_LIST_HEAD(&cache->ro_list);
 +      INIT_LIST_HEAD(&cache->dirty_list);
        btrfs_init_free_space_ctl(cache);
        atomic_set(&cache->trimming, 0);
  
@@@ -9001,8 -9064,9 +9000,8 @@@ int btrfs_read_block_groups(struct btrf
                         * b) Setting 'dirty flag' makes sure that we flush
                         *    the new space cache info onto disk.
                         */
 -                      cache->disk_cache_state = BTRFS_DC_CLEAR;
                        if (btrfs_test_opt(root, SPACE_CACHE))
 -                              cache->dirty = 1;
 +                              cache->disk_cache_state = BTRFS_DC_CLEAR;
                }
  
                read_extent_buffer(leaf, &cache->item,
                        spin_lock(&info->block_group_cache_lock);
                        rb_erase(&cache->cache_node,
                                 &info->block_group_cache_tree);
 +                      RB_CLEAR_NODE(&cache->cache_node);
                        spin_unlock(&info->block_group_cache_lock);
                        btrfs_put_block_group(cache);
                        goto error;
@@@ -9207,7 -9270,6 +9206,7 @@@ int btrfs_make_block_group(struct btrfs
                spin_lock(&root->fs_info->block_group_cache_lock);
                rb_erase(&cache->cache_node,
                         &root->fs_info->block_group_cache_tree);
 +              RB_CLEAR_NODE(&cache->cache_node);
                spin_unlock(&root->fs_info->block_group_cache_lock);
                btrfs_put_block_group(cache);
                return ret;
@@@ -9393,13 -9455,6 +9392,13 @@@ int btrfs_remove_block_group(struct btr
                }
        }
  
 +      spin_lock(&trans->transaction->dirty_bgs_lock);
 +      if (!list_empty(&block_group->dirty_list)) {
 +              list_del_init(&block_group->dirty_list);
 +              btrfs_put_block_group(block_group);
 +      }
 +      spin_unlock(&trans->transaction->dirty_bgs_lock);
 +
        btrfs_remove_free_space_cache(block_group);
  
        spin_lock(&block_group->space_info->lock);
@@@ -9634,7 -9689,13 +9633,7 @@@ out
  
  int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
  {
 -      return unpin_extent_range(root, start, end);
 -}
 -
 -int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
 -                             u64 num_bytes, u64 *actual_bytes)
 -{
 -      return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
 +      return unpin_extent_range(root, start, end, false);
  }
  
  int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
diff --combined fs/btrfs/tree-log.c
  }
  
  static int insert_orphan_item(struct btrfs_trans_handle *trans,
 -                            struct btrfs_root *root, u64 offset)
 +                            struct btrfs_root *root, u64 ino)
  {
        int ret;
 -      ret = btrfs_find_item(root, NULL, BTRFS_ORPHAN_OBJECTID,
 -                      offset, BTRFS_ORPHAN_ITEM_KEY, NULL);
 -      if (ret > 0)
 -              ret = btrfs_insert_orphan_item(trans, root, offset);
 +
 +      ret = btrfs_insert_orphan_item(trans, root, ino);
 +      if (ret == -EEXIST)
 +              ret = 0;
 +
        return ret;
  }
  
@@@ -2165,7 -2164,7 +2165,7 @@@ static noinline int walk_down_log_tree(
                parent = path->nodes[*level];
                root_owner = btrfs_header_owner(parent);
  
-               next = btrfs_find_create_tree_block(root, bytenr, blocksize);
+               next = btrfs_find_create_tree_block(root, bytenr);
                if (!next)
                        return -ENOMEM;
  
diff --combined fs/btrfs/volumes.c
@@@ -1485,7 -1485,7 +1485,7 @@@ static void update_dev_time(char *path_
        struct file *filp;
  
        filp = filp_open(path_name, O_RDWR, 0);
 -      if (!filp)
 +      if (IS_ERR(filp))
                return;
        file_update_time(filp);
        filp_close(filp, NULL);
@@@ -6247,8 -6247,13 +6247,13 @@@ int btrfs_read_sys_array(struct btrfs_r
        u32 cur;
        struct btrfs_key key;
  
-       sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
-                                         BTRFS_SUPER_INFO_SIZE);
+       ASSERT(BTRFS_SUPER_INFO_SIZE <= root->nodesize);
+       /*
+        * This will create extent buffer of nodesize, superblock size is
+        * fixed to BTRFS_SUPER_INFO_SIZE. If nodesize > sb size, this will
+        * overallocate but we can keep it as-is, only the first page is used.
+        */
+       sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET);
        if (!sb)
                return -ENOMEM;
        btrfs_set_buffer_uptodate(sb);