David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 2 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 3 | * Copyright (C) 2007,2008 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 4 | */ |
| 5 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 6 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 7 | #include <linux/slab.h> |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 8 | #include <linux/rbtree.h> |
David Sterba | adf0212 | 2017-05-31 19:44:31 +0200 | [diff] [blame] | 9 | #include <linux/mm.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 10 | #include "ctree.h" |
| 11 | #include "disk-io.h" |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 12 | #include "transaction.h" |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 13 | #include "print-tree.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 14 | #include "locking.h" |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 15 | #include "volumes.h" |
Qu Wenruo | f616f5c | 2019-01-23 15:15:17 +0800 | [diff] [blame] | 16 | #include "qgroup.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 17 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 18 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 19 | *root, struct btrfs_path *path, int level); |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 20 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 21 | const struct btrfs_key *ins_key, struct btrfs_path *path, |
| 22 | int data_size, int extend); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 23 | static int push_node_left(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 24 | struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 25 | struct extent_buffer *src, int empty); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 26 | static int balance_node_right(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 27 | struct extent_buffer *dst_buf, |
| 28 | struct extent_buffer *src_buf); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 29 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 30 | int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 31 | |
Johannes Thumshirn | af024ed | 2019-08-30 13:36:09 +0200 | [diff] [blame] | 32 | static const struct btrfs_csums { |
| 33 | u16 size; |
David Sterba | 59a0fcd | 2020-02-27 21:00:45 +0100 | [diff] [blame] | 34 | const char name[10]; |
| 35 | const char driver[12]; |
Johannes Thumshirn | af024ed | 2019-08-30 13:36:09 +0200 | [diff] [blame] | 36 | } btrfs_csums[] = { |
| 37 | [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" }, |
Johannes Thumshirn | 3951e7f | 2019-10-07 11:11:01 +0200 | [diff] [blame] | 38 | [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" }, |
Johannes Thumshirn | 3831bf0 | 2019-10-07 11:11:02 +0200 | [diff] [blame] | 39 | [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" }, |
David Sterba | 352ae07 | 2019-10-07 11:11:02 +0200 | [diff] [blame] | 40 | [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b", |
| 41 | .driver = "blake2b-256" }, |
Johannes Thumshirn | af024ed | 2019-08-30 13:36:09 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | int btrfs_super_csum_size(const struct btrfs_super_block *s) |
| 45 | { |
| 46 | u16 t = btrfs_super_csum_type(s); |
| 47 | /* |
| 48 | * csum type is validated at mount time |
| 49 | */ |
| 50 | return btrfs_csums[t].size; |
| 51 | } |
| 52 | |
| 53 | const char *btrfs_super_csum_name(u16 csum_type) |
| 54 | { |
| 55 | /* csum type is validated at mount time */ |
| 56 | return btrfs_csums[csum_type].name; |
| 57 | } |
| 58 | |
David Sterba | b4e967b | 2019-10-08 18:41:33 +0200 | [diff] [blame] | 59 | /* |
| 60 | * Return driver name if defined, otherwise the name that's also a valid driver |
| 61 | * name |
| 62 | */ |
| 63 | const char *btrfs_super_csum_driver(u16 csum_type) |
| 64 | { |
| 65 | /* csum type is validated at mount time */ |
David Sterba | 59a0fcd | 2020-02-27 21:00:45 +0100 | [diff] [blame] | 66 | return btrfs_csums[csum_type].driver[0] ? |
| 67 | btrfs_csums[csum_type].driver : |
David Sterba | b4e967b | 2019-10-08 18:41:33 +0200 | [diff] [blame] | 68 | btrfs_csums[csum_type].name; |
| 69 | } |
| 70 | |
David Sterba | 604997b | 2020-07-27 17:38:19 +0200 | [diff] [blame] | 71 | size_t __attribute_const__ btrfs_get_num_csums(void) |
David Sterba | f7cea56 | 2019-10-07 11:11:03 +0200 | [diff] [blame] | 72 | { |
| 73 | return ARRAY_SIZE(btrfs_csums); |
| 74 | } |
| 75 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 76 | struct btrfs_path *btrfs_alloc_path(void) |
| 77 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 78 | return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 81 | /* this also releases the path */ |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 82 | void btrfs_free_path(struct btrfs_path *p) |
| 83 | { |
Jesper Juhl | ff175d5 | 2010-12-25 21:22:30 +0000 | [diff] [blame] | 84 | if (!p) |
| 85 | return; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 86 | btrfs_release_path(p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 87 | kmem_cache_free(btrfs_path_cachep, p); |
| 88 | } |
| 89 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 90 | /* |
| 91 | * path release drops references on the extent buffers in the path |
| 92 | * and it drops any locks held by this path |
| 93 | * |
| 94 | * It is safe to call this on paths that no locks or extent buffers held. |
| 95 | */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 96 | noinline void btrfs_release_path(struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 97 | { |
| 98 | int i; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 99 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 100 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 101 | p->slots[i] = 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 102 | if (!p->nodes[i]) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 103 | continue; |
| 104 | if (p->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 105 | btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 106 | p->locks[i] = 0; |
| 107 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 108 | free_extent_buffer(p->nodes[i]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 109 | p->nodes[i] = NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 113 | /* |
| 114 | * safely gets a reference on the root node of a tree. A lock |
| 115 | * is not taken, so a concurrent writer may put a different node |
| 116 | * at the root of the tree. See btrfs_lock_root_node for the |
| 117 | * looping required. |
| 118 | * |
| 119 | * The extent buffer returned by this has a reference taken, so |
| 120 | * it won't disappear. It may stop being the root of the tree |
| 121 | * at any time because there are no locks held. |
| 122 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 123 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
| 124 | { |
| 125 | struct extent_buffer *eb; |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 126 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 127 | while (1) { |
| 128 | rcu_read_lock(); |
| 129 | eb = rcu_dereference(root->node); |
| 130 | |
| 131 | /* |
| 132 | * RCU really hurts here, we could free up the root node because |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 133 | * it was COWed but we may not get the new root node yet so do |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 134 | * the inc_not_zero dance and if it doesn't work then |
| 135 | * synchronize_rcu and try again. |
| 136 | */ |
| 137 | if (atomic_inc_not_zero(&eb->refs)) { |
| 138 | rcu_read_unlock(); |
| 139 | break; |
| 140 | } |
| 141 | rcu_read_unlock(); |
| 142 | synchronize_rcu(); |
| 143 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 144 | return eb; |
| 145 | } |
| 146 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 147 | /* |
| 148 | * Cowonly root (not-shareable trees, everything not subvolume or reloc roots), |
| 149 | * just get put onto a simple dirty list. Transaction walks this list to make |
| 150 | * sure they get properly updated on disk. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 151 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 152 | static void add_root_to_dirty_list(struct btrfs_root *root) |
| 153 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 154 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 155 | |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 156 | if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || |
| 157 | !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) |
| 158 | return; |
| 159 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 160 | spin_lock(&fs_info->trans_lock); |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 161 | if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { |
| 162 | /* Want the extent tree to be the last on the list */ |
Misono Tomohiro | 4fd786e | 2018-08-06 14:25:24 +0900 | [diff] [blame] | 163 | if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID) |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 164 | list_move_tail(&root->dirty_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 165 | &fs_info->dirty_cowonly_roots); |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 166 | else |
| 167 | list_move(&root->dirty_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 168 | &fs_info->dirty_cowonly_roots); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 169 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 170 | spin_unlock(&fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 171 | } |
| 172 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 173 | /* |
| 174 | * used by snapshot creation to make a copy of a root for a tree with |
| 175 | * a given objectid. The buffer with the new root node is returned in |
| 176 | * cow_ret, and this func returns zero on success or a negative error code. |
| 177 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 178 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
| 179 | struct btrfs_root *root, |
| 180 | struct extent_buffer *buf, |
| 181 | struct extent_buffer **cow_ret, u64 new_root_objectid) |
| 182 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 183 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 184 | struct extent_buffer *cow; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 185 | int ret = 0; |
| 186 | int level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 187 | struct btrfs_disk_key disk_key; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 188 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 189 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 190 | trans->transid != fs_info->running_transaction->transid); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 191 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 192 | trans->transid != root->last_trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 193 | |
| 194 | level = btrfs_header_level(buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 195 | if (level == 0) |
| 196 | btrfs_item_key(buf, &disk_key, 0); |
| 197 | else |
| 198 | btrfs_node_key(buf, &disk_key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 199 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 200 | cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, |
Josef Bacik | cf6f34a | 2020-08-20 11:46:07 -0400 | [diff] [blame] | 201 | &disk_key, level, buf->start, 0, |
| 202 | BTRFS_NESTING_NEW_ROOT); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 203 | if (IS_ERR(cow)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 204 | return PTR_ERR(cow); |
| 205 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 206 | copy_extent_buffer_full(cow, buf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 207 | btrfs_set_header_bytenr(cow, cow->start); |
| 208 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 209 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 210 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 211 | BTRFS_HEADER_FLAG_RELOC); |
| 212 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 213 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 214 | else |
| 215 | btrfs_set_header_owner(cow, new_root_objectid); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 216 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 217 | write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 218 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 219 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 220 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 221 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 222 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 223 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Josef Bacik | 867ed32 | 2021-01-14 14:02:46 -0500 | [diff] [blame] | 224 | if (ret) { |
Filipe Manana | 72c9925 | 2021-02-04 14:35:44 +0000 | [diff] [blame] | 225 | btrfs_tree_unlock(cow); |
| 226 | free_extent_buffer(cow); |
Josef Bacik | 867ed32 | 2021-01-14 14:02:46 -0500 | [diff] [blame] | 227 | btrfs_abort_transaction(trans, ret); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 228 | return ret; |
Josef Bacik | 867ed32 | 2021-01-14 14:02:46 -0500 | [diff] [blame] | 229 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 230 | |
| 231 | btrfs_mark_buffer_dirty(cow); |
| 232 | *cow_ret = cow; |
| 233 | return 0; |
| 234 | } |
| 235 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 236 | enum mod_log_op { |
| 237 | MOD_LOG_KEY_REPLACE, |
| 238 | MOD_LOG_KEY_ADD, |
| 239 | MOD_LOG_KEY_REMOVE, |
| 240 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, |
| 241 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, |
| 242 | MOD_LOG_MOVE_KEYS, |
| 243 | MOD_LOG_ROOT_REPLACE, |
| 244 | }; |
| 245 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 246 | struct tree_mod_root { |
| 247 | u64 logical; |
| 248 | u8 level; |
| 249 | }; |
| 250 | |
| 251 | struct tree_mod_elem { |
| 252 | struct rb_node node; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 253 | u64 logical; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 254 | u64 seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 255 | enum mod_log_op op; |
| 256 | |
| 257 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ |
| 258 | int slot; |
| 259 | |
| 260 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ |
| 261 | u64 generation; |
| 262 | |
| 263 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ |
| 264 | struct btrfs_disk_key key; |
| 265 | u64 blockptr; |
| 266 | |
| 267 | /* this is used for op == MOD_LOG_MOVE_KEYS */ |
David Sterba | b6dfa35 | 2018-03-05 15:31:18 +0100 | [diff] [blame] | 268 | struct { |
| 269 | int dst_slot; |
| 270 | int nr_items; |
| 271 | } move; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 272 | |
| 273 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ |
| 274 | struct tree_mod_root old_root; |
| 275 | }; |
| 276 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 277 | /* |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 278 | * Pull a new tree mod seq number for our operation. |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 279 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 280 | static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info) |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 281 | { |
| 282 | return atomic64_inc_return(&fs_info->tree_mod_seq); |
| 283 | } |
| 284 | |
| 285 | /* |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 286 | * This adds a new blocker to the tree mod log's blocker list if the @elem |
| 287 | * passed does not already have a sequence number set. So when a caller expects |
| 288 | * to record tree modifications, it should ensure to set elem->seq to zero |
| 289 | * before calling btrfs_get_tree_mod_seq. |
| 290 | * Returns a fresh, unused tree log modification sequence number, even if no new |
| 291 | * blocker was added. |
| 292 | */ |
| 293 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 294 | struct seq_list *elem) |
| 295 | { |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 296 | write_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 297 | if (!elem->seq) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 298 | elem->seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 299 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 300 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 301 | write_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 302 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 303 | return elem->seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 307 | struct seq_list *elem) |
| 308 | { |
| 309 | struct rb_root *tm_root; |
| 310 | struct rb_node *node; |
| 311 | struct rb_node *next; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 312 | struct tree_mod_elem *tm; |
| 313 | u64 min_seq = (u64)-1; |
| 314 | u64 seq_putting = elem->seq; |
| 315 | |
| 316 | if (!seq_putting) |
| 317 | return; |
| 318 | |
Filipe Manana | 7227ff4 | 2020-01-22 12:23:20 +0000 | [diff] [blame] | 319 | write_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 320 | list_del(&elem->list); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 321 | elem->seq = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 322 | |
Filipe Manana | 42836cf | 2020-01-22 12:23:54 +0000 | [diff] [blame] | 323 | if (!list_empty(&fs_info->tree_mod_seq_list)) { |
| 324 | struct seq_list *first; |
| 325 | |
| 326 | first = list_first_entry(&fs_info->tree_mod_seq_list, |
| 327 | struct seq_list, list); |
| 328 | if (seq_putting > first->seq) { |
| 329 | /* |
| 330 | * Blocker with lower sequence number exists, we |
| 331 | * cannot remove anything from the log. |
| 332 | */ |
| 333 | write_unlock(&fs_info->tree_mod_log_lock); |
| 334 | return; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 335 | } |
Filipe Manana | 42836cf | 2020-01-22 12:23:54 +0000 | [diff] [blame] | 336 | min_seq = first->seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 337 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 338 | |
| 339 | /* |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 340 | * anything that's lower than the lowest existing (read: blocked) |
| 341 | * sequence number can be removed from the tree. |
| 342 | */ |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 343 | tm_root = &fs_info->tree_mod_log; |
| 344 | for (node = rb_first(tm_root); node; node = next) { |
| 345 | next = rb_next(node); |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 346 | tm = rb_entry(node, struct tree_mod_elem, node); |
Filipe Manana | 6609fee | 2019-12-06 12:27:39 +0000 | [diff] [blame] | 347 | if (tm->seq >= min_seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 348 | continue; |
| 349 | rb_erase(node, tm_root); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 350 | kfree(tm); |
| 351 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 352 | write_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /* |
| 356 | * key order of the log: |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 357 | * node/leaf start address -> sequence |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 358 | * |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 359 | * The 'start address' is the logical address of the *new* root node |
| 360 | * for root replace operations, or the logical address of the affected |
| 361 | * block for all other operations. |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 362 | */ |
| 363 | static noinline int |
| 364 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 365 | { |
| 366 | struct rb_root *tm_root; |
| 367 | struct rb_node **new; |
| 368 | struct rb_node *parent = NULL; |
| 369 | struct tree_mod_elem *cur; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 370 | |
David Sterba | 73e82fe | 2019-03-27 16:19:55 +0100 | [diff] [blame] | 371 | lockdep_assert_held_write(&fs_info->tree_mod_log_lock); |
| 372 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 373 | tm->seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 374 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 375 | tm_root = &fs_info->tree_mod_log; |
| 376 | new = &tm_root->rb_node; |
| 377 | while (*new) { |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 378 | cur = rb_entry(*new, struct tree_mod_elem, node); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 379 | parent = *new; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 380 | if (cur->logical < tm->logical) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 381 | new = &((*new)->rb_left); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 382 | else if (cur->logical > tm->logical) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 383 | new = &((*new)->rb_right); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 384 | else if (cur->seq < tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 385 | new = &((*new)->rb_left); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 386 | else if (cur->seq > tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 387 | new = &((*new)->rb_right); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 388 | else |
| 389 | return -EEXIST; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | rb_link_node(&tm->node, parent, new); |
| 393 | rb_insert_color(&tm->node, tm_root); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 394 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 395 | } |
| 396 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 397 | /* |
| 398 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it |
| 399 | * returns zero with the tree_mod_log_lock acquired. The caller must hold |
| 400 | * this until all tree mod log insertions are recorded in the rb tree and then |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 401 | * write unlock fs_info::tree_mod_log_lock. |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 402 | */ |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 403 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 404 | struct extent_buffer *eb) { |
| 405 | smp_mb(); |
| 406 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 407 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 408 | if (eb && btrfs_header_level(eb) == 0) |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 409 | return 1; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 410 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 411 | write_lock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 412 | if (list_empty(&(fs_info)->tree_mod_seq_list)) { |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 413 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 414 | return 1; |
| 415 | } |
| 416 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 420 | /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */ |
| 421 | static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info, |
| 422 | struct extent_buffer *eb) |
| 423 | { |
| 424 | smp_mb(); |
| 425 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 426 | return 0; |
| 427 | if (eb && btrfs_header_level(eb) == 0) |
| 428 | return 0; |
| 429 | |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | static struct tree_mod_elem * |
| 434 | alloc_tree_mod_elem(struct extent_buffer *eb, int slot, |
| 435 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 436 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 437 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 438 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 439 | tm = kzalloc(sizeof(*tm), flags); |
| 440 | if (!tm) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 441 | return NULL; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 442 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 443 | tm->logical = eb->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 444 | if (op != MOD_LOG_KEY_ADD) { |
| 445 | btrfs_node_key(eb, &tm->key, slot); |
| 446 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 447 | } |
| 448 | tm->op = op; |
| 449 | tm->slot = slot; |
| 450 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 451 | RB_CLEAR_NODE(&tm->node); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 452 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 453 | return tm; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 454 | } |
| 455 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 456 | static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot, |
| 457 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 458 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 459 | struct tree_mod_elem *tm; |
| 460 | int ret; |
| 461 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 462 | if (!tree_mod_need_log(eb->fs_info, eb)) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 463 | return 0; |
| 464 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 465 | tm = alloc_tree_mod_elem(eb, slot, op, flags); |
| 466 | if (!tm) |
| 467 | return -ENOMEM; |
| 468 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 469 | if (tree_mod_dont_log(eb->fs_info, eb)) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 470 | kfree(tm); |
| 471 | return 0; |
| 472 | } |
| 473 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 474 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 475 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 476 | if (ret) |
| 477 | kfree(tm); |
| 478 | |
| 479 | return ret; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 480 | } |
| 481 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 482 | static noinline int tree_mod_log_insert_move(struct extent_buffer *eb, |
| 483 | int dst_slot, int src_slot, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 484 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 485 | struct tree_mod_elem *tm = NULL; |
| 486 | struct tree_mod_elem **tm_list = NULL; |
| 487 | int ret = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 488 | int i; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 489 | int locked = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 490 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 491 | if (!tree_mod_need_log(eb->fs_info, eb)) |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 492 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 493 | |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 494 | tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 495 | if (!tm_list) |
| 496 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 497 | |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 498 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 499 | if (!tm) { |
| 500 | ret = -ENOMEM; |
| 501 | goto free_tms; |
| 502 | } |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 503 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 504 | tm->logical = eb->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 505 | tm->slot = src_slot; |
| 506 | tm->move.dst_slot = dst_slot; |
| 507 | tm->move.nr_items = nr_items; |
| 508 | tm->op = MOD_LOG_MOVE_KEYS; |
| 509 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 510 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
| 511 | tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot, |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 512 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 513 | if (!tm_list[i]) { |
| 514 | ret = -ENOMEM; |
| 515 | goto free_tms; |
| 516 | } |
| 517 | } |
| 518 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 519 | if (tree_mod_dont_log(eb->fs_info, eb)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 520 | goto free_tms; |
| 521 | locked = 1; |
| 522 | |
| 523 | /* |
| 524 | * When we override something during the move, we log these removals. |
| 525 | * This can only happen when we move towards the beginning of the |
| 526 | * buffer, i.e. dst_slot < src_slot. |
| 527 | */ |
| 528 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 529 | ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 530 | if (ret) |
| 531 | goto free_tms; |
| 532 | } |
| 533 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 534 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 535 | if (ret) |
| 536 | goto free_tms; |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 537 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 538 | kfree(tm_list); |
| 539 | |
| 540 | return 0; |
| 541 | free_tms: |
| 542 | for (i = 0; i < nr_items; i++) { |
| 543 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 544 | rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 545 | kfree(tm_list[i]); |
| 546 | } |
| 547 | if (locked) |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 548 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 549 | kfree(tm_list); |
| 550 | kfree(tm); |
| 551 | |
| 552 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 555 | static inline int |
| 556 | __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, |
| 557 | struct tree_mod_elem **tm_list, |
| 558 | int nritems) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 559 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 560 | int i, j; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 561 | int ret; |
| 562 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 563 | for (i = nritems - 1; i >= 0; i--) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 564 | ret = __tree_mod_log_insert(fs_info, tm_list[i]); |
| 565 | if (ret) { |
| 566 | for (j = nritems - 1; j > i; j--) |
| 567 | rb_erase(&tm_list[j]->node, |
| 568 | &fs_info->tree_mod_log); |
| 569 | return ret; |
| 570 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 571 | } |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 572 | |
| 573 | return 0; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 574 | } |
| 575 | |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 576 | static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root, |
| 577 | struct extent_buffer *new_root, int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 578 | { |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 579 | struct btrfs_fs_info *fs_info = old_root->fs_info; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 580 | struct tree_mod_elem *tm = NULL; |
| 581 | struct tree_mod_elem **tm_list = NULL; |
| 582 | int nritems = 0; |
| 583 | int ret = 0; |
| 584 | int i; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 585 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 586 | if (!tree_mod_need_log(fs_info, NULL)) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 587 | return 0; |
| 588 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 589 | if (log_removal && btrfs_header_level(old_root) > 0) { |
| 590 | nritems = btrfs_header_nritems(old_root); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 591 | tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 592 | GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 593 | if (!tm_list) { |
| 594 | ret = -ENOMEM; |
| 595 | goto free_tms; |
| 596 | } |
| 597 | for (i = 0; i < nritems; i++) { |
| 598 | tm_list[i] = alloc_tree_mod_elem(old_root, i, |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 599 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 600 | if (!tm_list[i]) { |
| 601 | ret = -ENOMEM; |
| 602 | goto free_tms; |
| 603 | } |
| 604 | } |
| 605 | } |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 606 | |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 607 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 608 | if (!tm) { |
| 609 | ret = -ENOMEM; |
| 610 | goto free_tms; |
| 611 | } |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 612 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 613 | tm->logical = new_root->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 614 | tm->old_root.logical = old_root->start; |
| 615 | tm->old_root.level = btrfs_header_level(old_root); |
| 616 | tm->generation = btrfs_header_generation(old_root); |
| 617 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 618 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 619 | if (tree_mod_dont_log(fs_info, NULL)) |
| 620 | goto free_tms; |
| 621 | |
| 622 | if (tm_list) |
| 623 | ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems); |
| 624 | if (!ret) |
| 625 | ret = __tree_mod_log_insert(fs_info, tm); |
| 626 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 627 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 628 | if (ret) |
| 629 | goto free_tms; |
| 630 | kfree(tm_list); |
| 631 | |
| 632 | return ret; |
| 633 | |
| 634 | free_tms: |
| 635 | if (tm_list) { |
| 636 | for (i = 0; i < nritems; i++) |
| 637 | kfree(tm_list[i]); |
| 638 | kfree(tm_list); |
| 639 | } |
| 640 | kfree(tm); |
| 641 | |
| 642 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static struct tree_mod_elem * |
| 646 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 647 | int smallest) |
| 648 | { |
| 649 | struct rb_root *tm_root; |
| 650 | struct rb_node *node; |
| 651 | struct tree_mod_elem *cur = NULL; |
| 652 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 653 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 654 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 655 | tm_root = &fs_info->tree_mod_log; |
| 656 | node = tm_root->rb_node; |
| 657 | while (node) { |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 658 | cur = rb_entry(node, struct tree_mod_elem, node); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 659 | if (cur->logical < start) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 660 | node = node->rb_left; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 661 | } else if (cur->logical > start) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 662 | node = node->rb_right; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 663 | } else if (cur->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 664 | node = node->rb_left; |
| 665 | } else if (!smallest) { |
| 666 | /* we want the node with the highest seq */ |
| 667 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 668 | BUG_ON(found->seq > cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 669 | found = cur; |
| 670 | node = node->rb_left; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 671 | } else if (cur->seq > min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 672 | /* we want the node with the smallest seq */ |
| 673 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 674 | BUG_ON(found->seq < cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 675 | found = cur; |
| 676 | node = node->rb_right; |
| 677 | } else { |
| 678 | found = cur; |
| 679 | break; |
| 680 | } |
| 681 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 682 | read_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 683 | |
| 684 | return found; |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * this returns the element from the log with the smallest time sequence |
| 689 | * value that's in the log (the oldest log item). any element with a time |
| 690 | * sequence lower than min_seq will be ignored. |
| 691 | */ |
| 692 | static struct tree_mod_elem * |
| 693 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 694 | u64 min_seq) |
| 695 | { |
| 696 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 697 | } |
| 698 | |
| 699 | /* |
| 700 | * this returns the element from the log with the largest time sequence |
| 701 | * value that's in the log (the most recent log item). any element with |
| 702 | * a time sequence lower than min_seq will be ignored. |
| 703 | */ |
| 704 | static struct tree_mod_elem * |
| 705 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 706 | { |
| 707 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 708 | } |
| 709 | |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 710 | static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst, |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 711 | struct extent_buffer *src, unsigned long dst_offset, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 712 | unsigned long src_offset, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 713 | { |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 714 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 715 | int ret = 0; |
| 716 | struct tree_mod_elem **tm_list = NULL; |
| 717 | struct tree_mod_elem **tm_list_add, **tm_list_rem; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 718 | int i; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 719 | int locked = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 720 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 721 | if (!tree_mod_need_log(fs_info, NULL)) |
| 722 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 723 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 724 | if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 725 | return 0; |
| 726 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 727 | tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *), |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 728 | GFP_NOFS); |
| 729 | if (!tm_list) |
| 730 | return -ENOMEM; |
| 731 | |
| 732 | tm_list_add = tm_list; |
| 733 | tm_list_rem = tm_list + nr_items; |
| 734 | for (i = 0; i < nr_items; i++) { |
| 735 | tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset, |
| 736 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
| 737 | if (!tm_list_rem[i]) { |
| 738 | ret = -ENOMEM; |
| 739 | goto free_tms; |
| 740 | } |
| 741 | |
| 742 | tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset, |
| 743 | MOD_LOG_KEY_ADD, GFP_NOFS); |
| 744 | if (!tm_list_add[i]) { |
| 745 | ret = -ENOMEM; |
| 746 | goto free_tms; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (tree_mod_dont_log(fs_info, NULL)) |
| 751 | goto free_tms; |
| 752 | locked = 1; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 753 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 754 | for (i = 0; i < nr_items; i++) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 755 | ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]); |
| 756 | if (ret) |
| 757 | goto free_tms; |
| 758 | ret = __tree_mod_log_insert(fs_info, tm_list_add[i]); |
| 759 | if (ret) |
| 760 | goto free_tms; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 761 | } |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 762 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 763 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 764 | kfree(tm_list); |
| 765 | |
| 766 | return 0; |
| 767 | |
| 768 | free_tms: |
| 769 | for (i = 0; i < nr_items * 2; i++) { |
| 770 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) |
| 771 | rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log); |
| 772 | kfree(tm_list[i]); |
| 773 | } |
| 774 | if (locked) |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 775 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 776 | kfree(tm_list); |
| 777 | |
| 778 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 779 | } |
| 780 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 781 | static noinline int tree_mod_log_free_eb(struct extent_buffer *eb) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 782 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 783 | struct tree_mod_elem **tm_list = NULL; |
| 784 | int nritems = 0; |
| 785 | int i; |
| 786 | int ret = 0; |
| 787 | |
| 788 | if (btrfs_header_level(eb) == 0) |
| 789 | return 0; |
| 790 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 791 | if (!tree_mod_need_log(eb->fs_info, NULL)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 792 | return 0; |
| 793 | |
| 794 | nritems = btrfs_header_nritems(eb); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 795 | tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 796 | if (!tm_list) |
| 797 | return -ENOMEM; |
| 798 | |
| 799 | for (i = 0; i < nritems; i++) { |
| 800 | tm_list[i] = alloc_tree_mod_elem(eb, i, |
| 801 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
| 802 | if (!tm_list[i]) { |
| 803 | ret = -ENOMEM; |
| 804 | goto free_tms; |
| 805 | } |
| 806 | } |
| 807 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 808 | if (tree_mod_dont_log(eb->fs_info, eb)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 809 | goto free_tms; |
| 810 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 811 | ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 812 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 813 | if (ret) |
| 814 | goto free_tms; |
| 815 | kfree(tm_list); |
| 816 | |
| 817 | return 0; |
| 818 | |
| 819 | free_tms: |
| 820 | for (i = 0; i < nritems; i++) |
| 821 | kfree(tm_list[i]); |
| 822 | kfree(tm_list); |
| 823 | |
| 824 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 825 | } |
| 826 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 827 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 828 | * check if the tree block can be shared by multiple trees |
| 829 | */ |
| 830 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 831 | struct extent_buffer *buf) |
| 832 | { |
| 833 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 834 | * Tree blocks not in shareable trees and tree roots are never shared. |
| 835 | * If a block was allocated after the last snapshot and the block was |
| 836 | * not allocated by tree relocation, we know the block is not shared. |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 837 | */ |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 838 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 839 | buf != root->node && buf != root->commit_root && |
| 840 | (btrfs_header_generation(buf) <= |
| 841 | btrfs_root_last_snapshot(&root->root_item) || |
| 842 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 843 | return 1; |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 844 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 849 | struct btrfs_root *root, |
| 850 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 851 | struct extent_buffer *cow, |
| 852 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 853 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 854 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 855 | u64 refs; |
| 856 | u64 owner; |
| 857 | u64 flags; |
| 858 | u64 new_flags = 0; |
| 859 | int ret; |
| 860 | |
| 861 | /* |
| 862 | * Backrefs update rules: |
| 863 | * |
| 864 | * Always use full backrefs for extent pointers in tree block |
| 865 | * allocated by tree relocation. |
| 866 | * |
| 867 | * If a shared tree block is no longer referenced by its owner |
| 868 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 869 | * use full backrefs for extent pointers in tree block. |
| 870 | * |
| 871 | * If a tree block is been relocating |
| 872 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 873 | * use full backrefs for extent pointers in tree block. |
| 874 | * The reason for this is some operations (such as drop tree) |
| 875 | * are only allowed for blocks use full backrefs. |
| 876 | */ |
| 877 | |
| 878 | if (btrfs_block_can_be_shared(root, buf)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 879 | ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 880 | btrfs_header_level(buf), 1, |
| 881 | &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 882 | if (ret) |
| 883 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 884 | if (refs == 0) { |
| 885 | ret = -EROFS; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 886 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 887 | return ret; |
| 888 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 889 | } else { |
| 890 | refs = 1; |
| 891 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 892 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 893 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 894 | else |
| 895 | flags = 0; |
| 896 | } |
| 897 | |
| 898 | owner = btrfs_header_owner(buf); |
| 899 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 900 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 901 | |
| 902 | if (refs > 1) { |
| 903 | if ((owner == root->root_key.objectid || |
| 904 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 905 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 906 | ret = btrfs_inc_ref(trans, root, buf, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 907 | if (ret) |
| 908 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 909 | |
| 910 | if (root->root_key.objectid == |
| 911 | BTRFS_TREE_RELOC_OBJECTID) { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 912 | ret = btrfs_dec_ref(trans, root, buf, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 913 | if (ret) |
| 914 | return ret; |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 915 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 916 | if (ret) |
| 917 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 918 | } |
| 919 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 920 | } else { |
| 921 | |
| 922 | if (root->root_key.objectid == |
| 923 | BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 924 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 925 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 926 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 927 | if (ret) |
| 928 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 929 | } |
| 930 | if (new_flags != 0) { |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 931 | int level = btrfs_header_level(buf); |
| 932 | |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 933 | ret = btrfs_set_disk_extent_flags(trans, buf, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 934 | new_flags, level, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 935 | if (ret) |
| 936 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 937 | } |
| 938 | } else { |
| 939 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 940 | if (root->root_key.objectid == |
| 941 | BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 942 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 943 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 944 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 945 | if (ret) |
| 946 | return ret; |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 947 | ret = btrfs_dec_ref(trans, root, buf, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 948 | if (ret) |
| 949 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 950 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 951 | btrfs_clean_tree_block(buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 952 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 953 | } |
| 954 | return 0; |
| 955 | } |
| 956 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 957 | static struct extent_buffer *alloc_tree_block_no_bg_flush( |
| 958 | struct btrfs_trans_handle *trans, |
| 959 | struct btrfs_root *root, |
| 960 | u64 parent_start, |
| 961 | const struct btrfs_disk_key *disk_key, |
| 962 | int level, |
| 963 | u64 hint, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 964 | u64 empty_size, |
| 965 | enum btrfs_lock_nesting nest) |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 966 | { |
| 967 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 968 | struct extent_buffer *ret; |
| 969 | |
| 970 | /* |
| 971 | * If we are COWing a node/leaf from the extent, chunk, device or free |
| 972 | * space trees, make sure that we do not finish block group creation of |
| 973 | * pending block groups. We do this to avoid a deadlock. |
| 974 | * COWing can result in allocation of a new chunk, and flushing pending |
| 975 | * block groups (btrfs_create_pending_block_groups()) can be triggered |
| 976 | * when finishing allocation of a new chunk. Creation of a pending block |
| 977 | * group modifies the extent, chunk, device and free space trees, |
| 978 | * therefore we could deadlock with ourselves since we are holding a |
| 979 | * lock on an extent buffer that btrfs_create_pending_block_groups() may |
| 980 | * try to COW later. |
| 981 | * For similar reasons, we also need to delay flushing pending block |
| 982 | * groups when splitting a leaf or node, from one of those trees, since |
| 983 | * we are holding a write lock on it and its parent or when inserting a |
| 984 | * new root node for one of those trees. |
| 985 | */ |
| 986 | if (root == fs_info->extent_root || |
| 987 | root == fs_info->chunk_root || |
| 988 | root == fs_info->dev_root || |
| 989 | root == fs_info->free_space_root) |
| 990 | trans->can_flush_pending_bgs = false; |
| 991 | |
| 992 | ret = btrfs_alloc_tree_block(trans, root, parent_start, |
| 993 | root->root_key.objectid, disk_key, level, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 994 | hint, empty_size, nest); |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 995 | trans->can_flush_pending_bgs = true; |
| 996 | |
| 997 | return ret; |
| 998 | } |
| 999 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1000 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1001 | * does the dirty work in cow of a single block. The parent block (if |
| 1002 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 1003 | * dirty and returned locked. If you modify the block it needs to be marked |
| 1004 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1005 | * |
| 1006 | * search_start -- an allocation hint for the new block |
| 1007 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1008 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 1009 | * bytes the allocator should try to find free next to the block it returns. |
| 1010 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1011 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1012 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1013 | struct btrfs_root *root, |
| 1014 | struct extent_buffer *buf, |
| 1015 | struct extent_buffer *parent, int parent_slot, |
| 1016 | struct extent_buffer **cow_ret, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1017 | u64 search_start, u64 empty_size, |
| 1018 | enum btrfs_lock_nesting nest) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1019 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1020 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1021 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1022 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1023 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1024 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1025 | int unlock_orig = 0; |
Goldwyn Rodrigues | 0f5053e | 2016-09-22 14:11:34 -0500 | [diff] [blame] | 1026 | u64 parent_start = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1027 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1028 | if (*cow_ret == buf) |
| 1029 | unlock_orig = 1; |
| 1030 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 1031 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1032 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 1033 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1034 | trans->transid != fs_info->running_transaction->transid); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 1035 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1036 | trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1037 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1038 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1039 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1040 | if (level == 0) |
| 1041 | btrfs_item_key(buf, &disk_key, 0); |
| 1042 | else |
| 1043 | btrfs_node_key(buf, &disk_key, 0); |
| 1044 | |
Goldwyn Rodrigues | 0f5053e | 2016-09-22 14:11:34 -0500 | [diff] [blame] | 1045 | if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) |
| 1046 | parent_start = parent->start; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1047 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 1048 | cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1049 | level, search_start, empty_size, nest); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1050 | if (IS_ERR(cow)) |
| 1051 | return PTR_ERR(cow); |
| 1052 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1053 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 1054 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 1055 | copy_extent_buffer_full(cow, buf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1056 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1057 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1058 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 1059 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 1060 | BTRFS_HEADER_FLAG_RELOC); |
| 1061 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1062 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 1063 | else |
| 1064 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1065 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1066 | write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1067 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1068 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1069 | if (ret) { |
Josef Bacik | 572c83a | 2020-09-29 08:53:54 -0400 | [diff] [blame] | 1070 | btrfs_tree_unlock(cow); |
| 1071 | free_extent_buffer(cow); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1072 | btrfs_abort_transaction(trans, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1073 | return ret; |
| 1074 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1075 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 1076 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1077 | ret = btrfs_reloc_cow_block(trans, root, buf, cow); |
Zhaolei | 93314e3 | 2015-08-06 21:56:58 +0800 | [diff] [blame] | 1078 | if (ret) { |
Josef Bacik | 572c83a | 2020-09-29 08:53:54 -0400 | [diff] [blame] | 1079 | btrfs_tree_unlock(cow); |
| 1080 | free_extent_buffer(cow); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1081 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1082 | return ret; |
Zhaolei | 93314e3 | 2015-08-06 21:56:58 +0800 | [diff] [blame] | 1083 | } |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1084 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1085 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1086 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1087 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1088 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1089 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1090 | parent_start = buf->start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1091 | |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 1092 | atomic_inc(&cow->refs); |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 1093 | ret = tree_mod_log_insert_root(root->node, cow, 1); |
| 1094 | BUG_ON(ret < 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1095 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1096 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1097 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1098 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1099 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1100 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1101 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1102 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 1103 | tree_mod_log_insert_key(parent, parent_slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 1104 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1105 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1106 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1107 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1108 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1109 | btrfs_mark_buffer_dirty(parent); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1110 | if (last_ref) { |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 1111 | ret = tree_mod_log_free_eb(buf); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1112 | if (ret) { |
Josef Bacik | 572c83a | 2020-09-29 08:53:54 -0400 | [diff] [blame] | 1113 | btrfs_tree_unlock(cow); |
| 1114 | free_extent_buffer(cow); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1115 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1116 | return ret; |
| 1117 | } |
| 1118 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1119 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1120 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1121 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1122 | if (unlock_orig) |
| 1123 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1124 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1125 | btrfs_mark_buffer_dirty(cow); |
| 1126 | *cow_ret = cow; |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1130 | /* |
| 1131 | * returns the logical address of the oldest predecessor of the given root. |
| 1132 | * entries older than time_seq are ignored. |
| 1133 | */ |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1134 | static struct tree_mod_elem *__tree_mod_log_oldest_root( |
| 1135 | struct extent_buffer *eb_root, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1136 | { |
| 1137 | struct tree_mod_elem *tm; |
| 1138 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1139 | u64 root_logical = eb_root->start; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1140 | int looped = 0; |
| 1141 | |
| 1142 | if (!time_seq) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1143 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1144 | |
| 1145 | /* |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 1146 | * the very last operation that's logged for a root is the |
| 1147 | * replacement operation (if it is replaced at all). this has |
| 1148 | * the logical address of the *new* root, making it the very |
| 1149 | * first operation that's logged for this root. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1150 | */ |
| 1151 | while (1) { |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1152 | tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1153 | time_seq); |
| 1154 | if (!looped && !tm) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1155 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1156 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1157 | * if there are no tree operation for the oldest root, we simply |
| 1158 | * return it. this should only happen if that (old) root is at |
| 1159 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1160 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1161 | if (!tm) |
| 1162 | break; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1163 | |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1164 | /* |
| 1165 | * if there's an operation that's not a root replacement, we |
| 1166 | * found the oldest version of our root. normally, we'll find a |
| 1167 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1168 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1169 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1170 | break; |
| 1171 | |
| 1172 | found = tm; |
| 1173 | root_logical = tm->old_root.logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1174 | looped = 1; |
| 1175 | } |
| 1176 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1177 | /* if there's no old root to return, return what we found instead */ |
| 1178 | if (!found) |
| 1179 | found = tm; |
| 1180 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1181 | return found; |
| 1182 | } |
| 1183 | |
| 1184 | /* |
| 1185 | * tm is a pointer to the first operation to rewind within eb. then, all |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1186 | * previous operations will be rewound (until we reach something older than |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1187 | * time_seq). |
| 1188 | */ |
| 1189 | static void |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1190 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1191 | u64 time_seq, struct tree_mod_elem *first_tm) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1192 | { |
| 1193 | u32 n; |
| 1194 | struct rb_node *next; |
| 1195 | struct tree_mod_elem *tm = first_tm; |
| 1196 | unsigned long o_dst; |
| 1197 | unsigned long o_src; |
| 1198 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1199 | |
| 1200 | n = btrfs_header_nritems(eb); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 1201 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1202 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1203 | /* |
| 1204 | * all the operations are recorded with the operator used for |
| 1205 | * the modification. as we're going backwards, we do the |
| 1206 | * opposite of each operation here. |
| 1207 | */ |
| 1208 | switch (tm->op) { |
| 1209 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1210 | BUG_ON(tm->slot < n); |
Marcos Paulo de Souza | c730ae0 | 2020-06-16 15:54:29 -0300 | [diff] [blame] | 1211 | fallthrough; |
Liu Bo | 95c80bb | 2012-10-19 09:50:52 +0000 | [diff] [blame] | 1212 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1213 | case MOD_LOG_KEY_REMOVE: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1214 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1215 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1216 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1217 | tm->generation); |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1218 | n++; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1219 | break; |
| 1220 | case MOD_LOG_KEY_REPLACE: |
| 1221 | BUG_ON(tm->slot >= n); |
| 1222 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1223 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1224 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1225 | tm->generation); |
| 1226 | break; |
| 1227 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1228 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1229 | n--; |
| 1230 | break; |
| 1231 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1232 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1233 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1234 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1235 | tm->move.nr_items * p_size); |
| 1236 | break; |
| 1237 | case MOD_LOG_ROOT_REPLACE: |
| 1238 | /* |
| 1239 | * this operation is special. for roots, this must be |
| 1240 | * handled explicitly before rewinding. |
| 1241 | * for non-roots, this operation may exist if the node |
| 1242 | * was a root: root A -> child B; then A gets empty and |
| 1243 | * B is promoted to the new root. in the mod log, we'll |
| 1244 | * have a root-replace operation for B, a tree block |
| 1245 | * that is no root. we simply ignore that operation. |
| 1246 | */ |
| 1247 | break; |
| 1248 | } |
| 1249 | next = rb_next(&tm->node); |
| 1250 | if (!next) |
| 1251 | break; |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 1252 | tm = rb_entry(next, struct tree_mod_elem, node); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 1253 | if (tm->logical != first_tm->logical) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1254 | break; |
| 1255 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 1256 | read_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1257 | btrfs_set_header_nritems(eb, n); |
| 1258 | } |
| 1259 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1260 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1261 | * Called with eb read locked. If the buffer cannot be rewound, the same buffer |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1262 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
| 1263 | * returned buffer is always read-locked. If the returned buffer is not the |
| 1264 | * input buffer, the lock on the input buffer is released and the input buffer |
| 1265 | * is freed (its refcount is decremented). |
| 1266 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1267 | static struct extent_buffer * |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1268 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 1269 | struct extent_buffer *eb, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1270 | { |
| 1271 | struct extent_buffer *eb_rewin; |
| 1272 | struct tree_mod_elem *tm; |
| 1273 | |
| 1274 | if (!time_seq) |
| 1275 | return eb; |
| 1276 | |
| 1277 | if (btrfs_header_level(eb) == 0) |
| 1278 | return eb; |
| 1279 | |
| 1280 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1281 | if (!tm) |
| 1282 | return eb; |
| 1283 | |
| 1284 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1285 | BUG_ON(tm->slot != 0); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1286 | eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1287 | if (!eb_rewin) { |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 1288 | btrfs_tree_read_unlock(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1289 | free_extent_buffer(eb); |
| 1290 | return NULL; |
| 1291 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1292 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1293 | btrfs_set_header_backref_rev(eb_rewin, |
| 1294 | btrfs_header_backref_rev(eb)); |
| 1295 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1296 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1297 | } else { |
| 1298 | eb_rewin = btrfs_clone_extent_buffer(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1299 | if (!eb_rewin) { |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 1300 | btrfs_tree_read_unlock(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1301 | free_extent_buffer(eb); |
| 1302 | return NULL; |
| 1303 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1304 | } |
| 1305 | |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 1306 | btrfs_tree_read_unlock(eb); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1307 | free_extent_buffer(eb); |
| 1308 | |
Josef Bacik | d3beaa2 | 2020-08-10 11:42:31 -0400 | [diff] [blame] | 1309 | btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin), |
| 1310 | eb_rewin, btrfs_header_level(eb_rewin)); |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1311 | btrfs_tree_read_lock(eb_rewin); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1312 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1313 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1314 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1315 | |
| 1316 | return eb_rewin; |
| 1317 | } |
| 1318 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1319 | /* |
| 1320 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1321 | * value. If there are no changes, the current root->root_node is returned. If |
| 1322 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1323 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1324 | * Returns NULL on error (with no locks held). |
| 1325 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1326 | static inline struct extent_buffer * |
| 1327 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1328 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1329 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1330 | struct tree_mod_elem *tm; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1331 | struct extent_buffer *eb = NULL; |
| 1332 | struct extent_buffer *eb_root; |
Filipe Manana | efad8a8 | 2019-08-12 19:14:29 +0100 | [diff] [blame] | 1333 | u64 eb_root_owner = 0; |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1334 | struct extent_buffer *old; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1335 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1336 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1337 | u64 logical; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1338 | int level; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1339 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1340 | eb_root = btrfs_read_lock_root_node(root); |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1341 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1342 | if (!tm) |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1343 | return eb_root; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1344 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1345 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1346 | old_root = &tm->old_root; |
| 1347 | old_generation = tm->generation; |
| 1348 | logical = old_root->logical; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1349 | level = old_root->level; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1350 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1351 | logical = eb_root->start; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1352 | level = btrfs_header_level(eb_root); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1353 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1354 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1355 | tm = tree_mod_log_search(fs_info, logical, time_seq); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1356 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1357 | btrfs_tree_read_unlock(eb_root); |
| 1358 | free_extent_buffer(eb_root); |
Josef Bacik | 1b7ec85 | 2020-11-05 10:45:18 -0500 | [diff] [blame] | 1359 | old = read_tree_block(fs_info, logical, root->root_key.objectid, |
| 1360 | 0, level, NULL); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 1361 | if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { |
| 1362 | if (!IS_ERR(old)) |
| 1363 | free_extent_buffer(old); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1364 | btrfs_warn(fs_info, |
| 1365 | "failed to read tree block %llu from get_old_root", |
| 1366 | logical); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1367 | } else { |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1368 | eb = btrfs_clone_extent_buffer(old); |
| 1369 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1370 | } |
| 1371 | } else if (old_root) { |
Filipe Manana | efad8a8 | 2019-08-12 19:14:29 +0100 | [diff] [blame] | 1372 | eb_root_owner = btrfs_header_owner(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1373 | btrfs_tree_read_unlock(eb_root); |
| 1374 | free_extent_buffer(eb_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1375 | eb = alloc_dummy_extent_buffer(fs_info, logical); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1376 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1377 | eb = btrfs_clone_extent_buffer(eb_root); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 1378 | btrfs_tree_read_unlock(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1379 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1382 | if (!eb) |
| 1383 | return NULL; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1384 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1385 | btrfs_set_header_bytenr(eb, eb->start); |
| 1386 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
Filipe Manana | efad8a8 | 2019-08-12 19:14:29 +0100 | [diff] [blame] | 1387 | btrfs_set_header_owner(eb, eb_root_owner); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1388 | btrfs_set_header_level(eb, old_root->level); |
| 1389 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1390 | } |
Josef Bacik | d3beaa2 | 2020-08-10 11:42:31 -0400 | [diff] [blame] | 1391 | btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb, |
| 1392 | btrfs_header_level(eb)); |
| 1393 | btrfs_tree_read_lock(eb); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1394 | if (tm) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1395 | __tree_mod_log_rewind(fs_info, eb, time_seq, tm); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1396 | else |
| 1397 | WARN_ON(btrfs_header_level(eb) != 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1398 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1399 | |
| 1400 | return eb; |
| 1401 | } |
| 1402 | |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1403 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
| 1404 | { |
| 1405 | struct tree_mod_elem *tm; |
| 1406 | int level; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1407 | struct extent_buffer *eb_root = btrfs_root_node(root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1408 | |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1409 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1410 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1411 | level = tm->old_root.level; |
| 1412 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1413 | level = btrfs_header_level(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1414 | } |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1415 | free_extent_buffer(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1416 | |
| 1417 | return level; |
| 1418 | } |
| 1419 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1420 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1421 | struct btrfs_root *root, |
| 1422 | struct extent_buffer *buf) |
| 1423 | { |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1424 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 1425 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 1426 | |
David Sterba | d198013 | 2018-03-16 02:39:40 +0100 | [diff] [blame] | 1427 | /* Ensure we can see the FORCE_COW bit */ |
| 1428 | smp_mb__before_atomic(); |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1429 | |
| 1430 | /* |
| 1431 | * We do not need to cow a block if |
| 1432 | * 1) this block is not created or changed in this transaction; |
| 1433 | * 2) this block does not belong to TREE_RELOC tree; |
| 1434 | * 3) the root is not forced COW. |
| 1435 | * |
| 1436 | * What is forced COW: |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1437 | * when we create snapshot during committing the transaction, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1438 | * after we've finished copying src root, we must COW the shared |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1439 | * block to ensure the metadata consistency. |
| 1440 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1441 | if (btrfs_header_generation(buf) == trans->transid && |
| 1442 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1443 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1444 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1445 | !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1446 | return 0; |
| 1447 | return 1; |
| 1448 | } |
| 1449 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1450 | /* |
| 1451 | * cows a single block, see __btrfs_cow_block for the real work. |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1452 | * This version of it has extra checks so that a block isn't COWed more than |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1453 | * once per transaction, as long as it hasn't been written yet |
| 1454 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1455 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1456 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1457 | struct extent_buffer *parent, int parent_slot, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1458 | struct extent_buffer **cow_ret, |
| 1459 | enum btrfs_lock_nesting nest) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1460 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1461 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1462 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1463 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1464 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 1465 | if (test_bit(BTRFS_ROOT_DELETING, &root->state)) |
| 1466 | btrfs_err(fs_info, |
| 1467 | "COW'ing blocks on a fs root that's being dropped"); |
| 1468 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1469 | if (trans->transaction != fs_info->running_transaction) |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1470 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1471 | trans->transid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1472 | fs_info->running_transaction->transid); |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1473 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1474 | if (trans->transid != fs_info->generation) |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1475 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1476 | trans->transid, fs_info->generation); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1477 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1478 | if (!should_cow_block(trans, root, buf)) { |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 1479 | trans->dirty = true; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1480 | *cow_ret = buf; |
| 1481 | return 0; |
| 1482 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1483 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1484 | search_start = buf->start & ~((u64)SZ_1G - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1485 | |
Qu Wenruo | f616f5c | 2019-01-23 15:15:17 +0800 | [diff] [blame] | 1486 | /* |
| 1487 | * Before CoWing this block for later modification, check if it's |
| 1488 | * the subtree root and do the delayed subtree trace if needed. |
| 1489 | * |
| 1490 | * Also We don't care about the error, as it's handled internally. |
| 1491 | */ |
| 1492 | btrfs_qgroup_trace_subtree_after_cow(trans, root, buf); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1493 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1494 | parent_slot, cow_ret, search_start, 0, nest); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1495 | |
| 1496 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1497 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1498 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1499 | } |
Josef Bacik | f75e2b7 | 2020-12-16 11:18:43 -0500 | [diff] [blame] | 1500 | ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1501 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1502 | /* |
| 1503 | * helper function for defrag to decide if two blocks pointed to by a |
| 1504 | * node are actually close by |
| 1505 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1506 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1507 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1508 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1509 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1510 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1511 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1512 | return 0; |
| 1513 | } |
| 1514 | |
David Sterba | ce6ef5a | 2020-06-08 16:06:07 +0200 | [diff] [blame] | 1515 | #ifdef __LITTLE_ENDIAN |
| 1516 | |
| 1517 | /* |
| 1518 | * Compare two keys, on little-endian the disk order is same as CPU order and |
| 1519 | * we can avoid the conversion. |
| 1520 | */ |
| 1521 | static int comp_keys(const struct btrfs_disk_key *disk_key, |
| 1522 | const struct btrfs_key *k2) |
| 1523 | { |
| 1524 | const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key; |
| 1525 | |
| 1526 | return btrfs_comp_cpu_keys(k1, k2); |
| 1527 | } |
| 1528 | |
| 1529 | #else |
| 1530 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1531 | /* |
| 1532 | * compare two keys in a memcmp fashion |
| 1533 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1534 | static int comp_keys(const struct btrfs_disk_key *disk, |
| 1535 | const struct btrfs_key *k2) |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1536 | { |
| 1537 | struct btrfs_key k1; |
| 1538 | |
| 1539 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1540 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1541 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1542 | } |
David Sterba | ce6ef5a | 2020-06-08 16:06:07 +0200 | [diff] [blame] | 1543 | #endif |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1544 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1545 | /* |
| 1546 | * same as comp_keys only with two btrfs_key's |
| 1547 | */ |
David Sterba | e1f60a6 | 2019-10-01 19:57:39 +0200 | [diff] [blame] | 1548 | int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2) |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1549 | { |
| 1550 | if (k1->objectid > k2->objectid) |
| 1551 | return 1; |
| 1552 | if (k1->objectid < k2->objectid) |
| 1553 | return -1; |
| 1554 | if (k1->type > k2->type) |
| 1555 | return 1; |
| 1556 | if (k1->type < k2->type) |
| 1557 | return -1; |
| 1558 | if (k1->offset > k2->offset) |
| 1559 | return 1; |
| 1560 | if (k1->offset < k2->offset) |
| 1561 | return -1; |
| 1562 | return 0; |
| 1563 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1564 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1565 | /* |
| 1566 | * this is used by the defrag code to go through all the |
| 1567 | * leaves pointed to by a node and reallocate them so that |
| 1568 | * disk order is close to key order |
| 1569 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1570 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1571 | struct btrfs_root *root, struct extent_buffer *parent, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1572 | int start_slot, u64 *last_ret, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1573 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1574 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1575 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1576 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1577 | u64 blocknr; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1578 | u64 search_start = *last_ret; |
| 1579 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1580 | u64 other; |
| 1581 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1582 | int end_slot; |
| 1583 | int i; |
| 1584 | int err = 0; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1585 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1586 | int progress_passed = 0; |
| 1587 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1588 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1589 | WARN_ON(trans->transaction != fs_info->running_transaction); |
| 1590 | WARN_ON(trans->transid != fs_info->generation); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1591 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1592 | parent_nritems = btrfs_header_nritems(parent); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1593 | blocksize = fs_info->nodesize; |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1594 | end_slot = parent_nritems - 1; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1595 | |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1596 | if (parent_nritems <= 1) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1597 | return 0; |
| 1598 | |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1599 | for (i = start_slot; i <= end_slot; i++) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1600 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1601 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1602 | btrfs_node_key(parent, &disk_key, i); |
| 1603 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1604 | continue; |
| 1605 | |
| 1606 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1607 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1608 | if (last_block == 0) |
| 1609 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1610 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1611 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1612 | other = btrfs_node_blockptr(parent, i - 1); |
| 1613 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1614 | } |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1615 | if (!close && i < end_slot) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1616 | other = btrfs_node_blockptr(parent, i + 1); |
| 1617 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1618 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1619 | if (close) { |
| 1620 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1621 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1622 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1623 | |
Josef Bacik | 206983b | 2020-11-05 10:45:10 -0500 | [diff] [blame] | 1624 | cur = btrfs_read_node_slot(parent, i); |
| 1625 | if (IS_ERR(cur)) |
| 1626 | return PTR_ERR(cur); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1627 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1628 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1629 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1630 | btrfs_tree_lock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1631 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1632 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1633 | min(16 * blocksize, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1634 | (end_slot - i) * blocksize), |
| 1635 | BTRFS_NESTING_COW); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1636 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1637 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1638 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1639 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1640 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1641 | search_start = cur->start; |
| 1642 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1643 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1644 | btrfs_tree_unlock(cur); |
| 1645 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1646 | } |
| 1647 | return err; |
| 1648 | } |
| 1649 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1650 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1651 | * search for key in the extent_buffer. The items start at offset p, |
| 1652 | * and they are item_size apart. There are 'max' items in p. |
| 1653 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1654 | * the slot in the array is returned via slot, and it points to |
| 1655 | * the place where you would insert key if it is not found in |
| 1656 | * the array. |
| 1657 | * |
| 1658 | * slot may point to max if the key is bigger than all of the keys |
| 1659 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1660 | static noinline int generic_bin_search(struct extent_buffer *eb, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1661 | unsigned long p, int item_size, |
| 1662 | const struct btrfs_key *key, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1663 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1664 | { |
| 1665 | int low = 0; |
| 1666 | int high = max; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1667 | int ret; |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1668 | const int key_size = sizeof(struct btrfs_disk_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1669 | |
Liu Bo | 5e24e9a | 2016-06-23 16:32:45 -0700 | [diff] [blame] | 1670 | if (low > high) { |
| 1671 | btrfs_err(eb->fs_info, |
| 1672 | "%s: low (%d) > high (%d) eb %llu owner %llu level %d", |
| 1673 | __func__, low, high, eb->start, |
| 1674 | btrfs_header_owner(eb), btrfs_header_level(eb)); |
| 1675 | return -EINVAL; |
| 1676 | } |
| 1677 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1678 | while (low < high) { |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1679 | unsigned long oip; |
| 1680 | unsigned long offset; |
| 1681 | struct btrfs_disk_key *tmp; |
| 1682 | struct btrfs_disk_key unaligned; |
| 1683 | int mid; |
| 1684 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1685 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1686 | offset = p + mid * item_size; |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1687 | oip = offset_in_page(offset); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1688 | |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1689 | if (oip + key_size <= PAGE_SIZE) { |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 1690 | const unsigned long idx = get_eb_page_index(offset); |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1691 | char *kaddr = page_address(eb->pages[idx]); |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1692 | |
Qu Wenruo | 884b07d | 2020-12-02 14:48:04 +0800 | [diff] [blame] | 1693 | oip = get_eb_offset_in_page(eb, offset); |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1694 | tmp = (struct btrfs_disk_key *)(kaddr + oip); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1695 | } else { |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1696 | read_extent_buffer(eb, &unaligned, offset, key_size); |
| 1697 | tmp = &unaligned; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1698 | } |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1699 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1700 | ret = comp_keys(tmp, key); |
| 1701 | |
| 1702 | if (ret < 0) |
| 1703 | low = mid + 1; |
| 1704 | else if (ret > 0) |
| 1705 | high = mid; |
| 1706 | else { |
| 1707 | *slot = mid; |
| 1708 | return 0; |
| 1709 | } |
| 1710 | } |
| 1711 | *slot = low; |
| 1712 | return 1; |
| 1713 | } |
| 1714 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1715 | /* |
| 1716 | * simple bin_search frontend that does the right thing for |
| 1717 | * leaves vs nodes |
| 1718 | */ |
Nikolay Borisov | a74b35e | 2017-12-08 16:27:43 +0200 | [diff] [blame] | 1719 | int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key, |
Qu Wenruo | e3b8336 | 2020-04-17 15:08:21 +0800 | [diff] [blame] | 1720 | int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1721 | { |
Qu Wenruo | e3b8336 | 2020-04-17 15:08:21 +0800 | [diff] [blame] | 1722 | if (btrfs_header_level(eb) == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1723 | return generic_bin_search(eb, |
| 1724 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1725 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1726 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1727 | slot); |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1728 | else |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1729 | return generic_bin_search(eb, |
| 1730 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1731 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1732 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1733 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1734 | } |
| 1735 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1736 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1737 | { |
| 1738 | spin_lock(&root->accounting_lock); |
| 1739 | btrfs_set_root_used(&root->root_item, |
| 1740 | btrfs_root_used(&root->root_item) + size); |
| 1741 | spin_unlock(&root->accounting_lock); |
| 1742 | } |
| 1743 | |
| 1744 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1745 | { |
| 1746 | spin_lock(&root->accounting_lock); |
| 1747 | btrfs_set_root_used(&root->root_item, |
| 1748 | btrfs_root_used(&root->root_item) - size); |
| 1749 | spin_unlock(&root->accounting_lock); |
| 1750 | } |
| 1751 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1752 | /* given a node and slot number, this reads the blocks it points to. The |
| 1753 | * extent buffer is returned with a reference taken (but unlocked). |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1754 | */ |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1755 | struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent, |
| 1756 | int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1757 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1758 | int level = btrfs_header_level(parent); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1759 | struct extent_buffer *eb; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1760 | struct btrfs_key first_key; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1761 | |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1762 | if (slot < 0 || slot >= btrfs_header_nritems(parent)) |
| 1763 | return ERR_PTR(-ENOENT); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1764 | |
| 1765 | BUG_ON(level == 0); |
| 1766 | |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1767 | btrfs_node_key_to_cpu(parent, &first_key, slot); |
David Sterba | d0d20b0 | 2019-03-20 14:54:01 +0100 | [diff] [blame] | 1768 | eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot), |
Josef Bacik | 1b7ec85 | 2020-11-05 10:45:18 -0500 | [diff] [blame] | 1769 | btrfs_header_owner(parent), |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1770 | btrfs_node_ptr_generation(parent, slot), |
| 1771 | level - 1, &first_key); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1772 | if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { |
| 1773 | free_extent_buffer(eb); |
| 1774 | eb = ERR_PTR(-EIO); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | return eb; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1778 | } |
| 1779 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1780 | /* |
| 1781 | * node level balancing, used to make sure nodes are in proper order for |
| 1782 | * item deletion. We balance from the top down, so we have to make sure |
| 1783 | * that a deletion won't leave an node completely empty later on. |
| 1784 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1785 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1786 | struct btrfs_root *root, |
| 1787 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1788 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1789 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1790 | struct extent_buffer *right = NULL; |
| 1791 | struct extent_buffer *mid; |
| 1792 | struct extent_buffer *left = NULL; |
| 1793 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1794 | int ret = 0; |
| 1795 | int wret; |
| 1796 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1797 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1798 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1799 | |
Liu Bo | 98e6b1e | 2018-09-12 06:06:23 +0800 | [diff] [blame] | 1800 | ASSERT(level > 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1801 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1802 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1803 | |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 1804 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1805 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1806 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1807 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1808 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1809 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1810 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1811 | pslot = path->slots[level + 1]; |
| 1812 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1813 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1814 | /* |
| 1815 | * deal with the case where there is only one pointer in the root |
| 1816 | * by promoting the node below to a root |
| 1817 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1818 | if (!parent) { |
| 1819 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1820 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1821 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1822 | return 0; |
| 1823 | |
| 1824 | /* promote the child to a root */ |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1825 | child = btrfs_read_node_slot(mid, 0); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1826 | if (IS_ERR(child)) { |
| 1827 | ret = PTR_ERR(child); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1828 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1829 | goto enospc; |
| 1830 | } |
| 1831 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1832 | btrfs_tree_lock(child); |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1833 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child, |
| 1834 | BTRFS_NESTING_COW); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1835 | if (ret) { |
| 1836 | btrfs_tree_unlock(child); |
| 1837 | free_extent_buffer(child); |
| 1838 | goto enospc; |
| 1839 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1840 | |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 1841 | ret = tree_mod_log_insert_root(root->node, child, 1); |
| 1842 | BUG_ON(ret < 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1843 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1844 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1845 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1846 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1847 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1848 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1849 | path->nodes[level] = NULL; |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1850 | btrfs_clean_tree_block(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1851 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1852 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1853 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1854 | |
| 1855 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1856 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1857 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1858 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1859 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1860 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1861 | if (btrfs_header_nritems(mid) > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1862 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1863 | return 0; |
| 1864 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1865 | left = btrfs_read_node_slot(parent, pslot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1866 | if (IS_ERR(left)) |
| 1867 | left = NULL; |
| 1868 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1869 | if (left) { |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 1870 | __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1871 | wret = btrfs_cow_block(trans, root, left, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1872 | parent, pslot - 1, &left, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 1873 | BTRFS_NESTING_LEFT_COW); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1874 | if (wret) { |
| 1875 | ret = wret; |
| 1876 | goto enospc; |
| 1877 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1878 | } |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1879 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1880 | right = btrfs_read_node_slot(parent, pslot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1881 | if (IS_ERR(right)) |
| 1882 | right = NULL; |
| 1883 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1884 | if (right) { |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 1885 | __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1886 | wret = btrfs_cow_block(trans, root, right, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1887 | parent, pslot + 1, &right, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 1888 | BTRFS_NESTING_RIGHT_COW); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1889 | if (wret) { |
| 1890 | ret = wret; |
| 1891 | goto enospc; |
| 1892 | } |
| 1893 | } |
| 1894 | |
| 1895 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1896 | if (left) { |
| 1897 | orig_slot += btrfs_header_nritems(left); |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 1898 | wret = push_node_left(trans, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1899 | if (wret < 0) |
| 1900 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1901 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1902 | |
| 1903 | /* |
| 1904 | * then try to empty the right most buffer into the middle |
| 1905 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1906 | if (right) { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 1907 | wret = push_node_left(trans, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1908 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1909 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1910 | if (btrfs_header_nritems(right) == 0) { |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1911 | btrfs_clean_tree_block(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1912 | btrfs_tree_unlock(right); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1913 | del_ptr(root, path, level + 1, pslot + 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1914 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1915 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1916 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1917 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1918 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1919 | struct btrfs_disk_key right_key; |
| 1920 | btrfs_node_key(right, &right_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 1921 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
| 1922 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 1923 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1924 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1925 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1926 | } |
| 1927 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1928 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1929 | /* |
| 1930 | * we're not allowed to leave a node with one item in the |
| 1931 | * tree during a delete. A deletion from lower in the tree |
| 1932 | * could try to delete the only pointer in this node. |
| 1933 | * So, pull some keys from the left. |
| 1934 | * There has to be a left pointer at this point because |
| 1935 | * otherwise we would have pulled some pointers from the |
| 1936 | * right |
| 1937 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1938 | if (!left) { |
| 1939 | ret = -EROFS; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1940 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1941 | goto enospc; |
| 1942 | } |
David Sterba | 55d32ed | 2019-03-20 14:18:06 +0100 | [diff] [blame] | 1943 | wret = balance_node_right(trans, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1944 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1945 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1946 | goto enospc; |
| 1947 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1948 | if (wret == 1) { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 1949 | wret = push_node_left(trans, left, mid, 1); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1950 | if (wret < 0) |
| 1951 | ret = wret; |
| 1952 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1953 | BUG_ON(wret == 1); |
| 1954 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1955 | if (btrfs_header_nritems(mid) == 0) { |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1956 | btrfs_clean_tree_block(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1957 | btrfs_tree_unlock(mid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1958 | del_ptr(root, path, level + 1, pslot); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1959 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1960 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1961 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1962 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1963 | } else { |
| 1964 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1965 | struct btrfs_disk_key mid_key; |
| 1966 | btrfs_node_key(mid, &mid_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 1967 | ret = tree_mod_log_insert_key(parent, pslot, |
| 1968 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 1969 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1970 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 1971 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1972 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1973 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1974 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1975 | if (left) { |
| 1976 | if (btrfs_header_nritems(left) > orig_slot) { |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 1977 | atomic_inc(&left->refs); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1978 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1979 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1980 | path->slots[level + 1] -= 1; |
| 1981 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1982 | if (mid) { |
| 1983 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1984 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1985 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1986 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1987 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1988 | path->slots[level] = orig_slot; |
| 1989 | } |
| 1990 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1991 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1992 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1993 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1994 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1995 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1996 | if (right) { |
| 1997 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1998 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1999 | } |
| 2000 | if (left) { |
| 2001 | if (path->nodes[level] != left) |
| 2002 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2003 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2004 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2005 | return ret; |
| 2006 | } |
| 2007 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2008 | /* Node balancing for insertion. Here we only split or push nodes around |
| 2009 | * when they are completely full. This is also done top down, so we |
| 2010 | * have to be pessimistic. |
| 2011 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2012 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2013 | struct btrfs_root *root, |
| 2014 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2015 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2016 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2017 | struct extent_buffer *right = NULL; |
| 2018 | struct extent_buffer *mid; |
| 2019 | struct extent_buffer *left = NULL; |
| 2020 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2021 | int ret = 0; |
| 2022 | int wret; |
| 2023 | int pslot; |
| 2024 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2025 | |
| 2026 | if (level == 0) |
| 2027 | return 1; |
| 2028 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2029 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2030 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2031 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 2032 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2033 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 2034 | pslot = path->slots[level + 1]; |
| 2035 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2036 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2037 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2038 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2039 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 2040 | left = btrfs_read_node_slot(parent, pslot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 2041 | if (IS_ERR(left)) |
| 2042 | left = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2043 | |
| 2044 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2045 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2046 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2047 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 2048 | __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2049 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2050 | left_nr = btrfs_header_nritems(left); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2051 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2052 | wret = 1; |
| 2053 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2054 | ret = btrfs_cow_block(trans, root, left, parent, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 2055 | pslot - 1, &left, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 2056 | BTRFS_NESTING_LEFT_COW); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2057 | if (ret) |
| 2058 | wret = 1; |
| 2059 | else { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 2060 | wret = push_node_left(trans, left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2061 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2062 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2063 | if (wret < 0) |
| 2064 | ret = wret; |
| 2065 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2066 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2067 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2068 | btrfs_node_key(mid, &disk_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 2069 | ret = tree_mod_log_insert_key(parent, pslot, |
| 2070 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2071 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2072 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 2073 | btrfs_mark_buffer_dirty(parent); |
| 2074 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2075 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2076 | path->slots[level + 1] -= 1; |
| 2077 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2078 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2079 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2080 | } else { |
| 2081 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2082 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2083 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2084 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2085 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2086 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2087 | return 0; |
| 2088 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2089 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2090 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2091 | } |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 2092 | right = btrfs_read_node_slot(parent, pslot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 2093 | if (IS_ERR(right)) |
| 2094 | right = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2095 | |
| 2096 | /* |
| 2097 | * then try to empty the right most buffer into the middle |
| 2098 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2099 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2100 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2101 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 2102 | __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2103 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2104 | right_nr = btrfs_header_nritems(right); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2105 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2106 | wret = 1; |
| 2107 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2108 | ret = btrfs_cow_block(trans, root, right, |
| 2109 | parent, pslot + 1, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 2110 | &right, BTRFS_NESTING_RIGHT_COW); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2111 | if (ret) |
| 2112 | wret = 1; |
| 2113 | else { |
David Sterba | 55d32ed | 2019-03-20 14:18:06 +0100 | [diff] [blame] | 2114 | wret = balance_node_right(trans, right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2115 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2116 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2117 | if (wret < 0) |
| 2118 | ret = wret; |
| 2119 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2120 | struct btrfs_disk_key disk_key; |
| 2121 | |
| 2122 | btrfs_node_key(right, &disk_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 2123 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
| 2124 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2125 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2126 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2127 | btrfs_mark_buffer_dirty(parent); |
| 2128 | |
| 2129 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2130 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2131 | path->slots[level + 1] += 1; |
| 2132 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2133 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2134 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2135 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2136 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2137 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2138 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2139 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2140 | return 0; |
| 2141 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2142 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2143 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2144 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2145 | return 1; |
| 2146 | } |
| 2147 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2148 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2149 | * readahead one full node of leaves, finding things that are close |
| 2150 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2151 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2152 | static void reada_for_search(struct btrfs_fs_info *fs_info, |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2153 | struct btrfs_path *path, |
| 2154 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2155 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2156 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2157 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2158 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2159 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2160 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2161 | u64 nread = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2162 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2163 | u32 nr; |
| 2164 | u32 blocksize; |
| 2165 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2166 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2167 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2168 | return; |
| 2169 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2170 | if (!path->nodes[level]) |
| 2171 | return; |
| 2172 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2173 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2174 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2175 | search = btrfs_node_blockptr(node, slot); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2176 | blocksize = fs_info->nodesize; |
| 2177 | eb = find_extent_buffer(fs_info, search); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2178 | if (eb) { |
| 2179 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2180 | return; |
| 2181 | } |
| 2182 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2183 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2184 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2185 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2186 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2187 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2188 | while (1) { |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2189 | if (path->reada == READA_BACK) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2190 | if (nr == 0) |
| 2191 | break; |
| 2192 | nr--; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2193 | } else if (path->reada == READA_FORWARD) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2194 | nr++; |
| 2195 | if (nr >= nritems) |
| 2196 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2197 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2198 | if (path->reada == READA_BACK && objectid) { |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2199 | btrfs_node_key(node, &disk_key, nr); |
| 2200 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2201 | break; |
| 2202 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2203 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2204 | if ((search <= target && target - search <= 65536) || |
| 2205 | (search > target && search - target <= 65536)) { |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 2206 | btrfs_readahead_node_child(node, nr); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2207 | nread += blocksize; |
| 2208 | } |
| 2209 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2210 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2211 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2212 | } |
| 2213 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2214 | |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 2215 | static noinline void reada_for_balance(struct btrfs_path *path, int level) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2216 | { |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 2217 | struct extent_buffer *parent; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2218 | int slot; |
| 2219 | int nritems; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2220 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2221 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2222 | if (!parent) |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2223 | return; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2224 | |
| 2225 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2226 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2227 | |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 2228 | if (slot > 0) |
| 2229 | btrfs_readahead_node_child(parent, slot - 1); |
| 2230 | if (slot + 1 < nritems) |
| 2231 | btrfs_readahead_node_child(parent, slot + 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2236 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2237 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2238 | * operations on the tree might require changing key pointers higher up in the |
| 2239 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2240 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2241 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2242 | * the lock if the path points to the last slot in the block. This is part of |
| 2243 | * walking through the tree, and selecting the next slot in the higher block. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2244 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2245 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2246 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2247 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2248 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2249 | int lowest_unlock, int min_write_lock_level, |
| 2250 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2251 | { |
| 2252 | int i; |
| 2253 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2254 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2255 | struct extent_buffer *t; |
| 2256 | |
| 2257 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2258 | if (!path->nodes[i]) |
| 2259 | break; |
| 2260 | if (!path->locks[i]) |
| 2261 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2262 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2263 | skip_level = i + 1; |
| 2264 | continue; |
| 2265 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2266 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2267 | u32 nritems; |
| 2268 | t = path->nodes[i]; |
| 2269 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2270 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2271 | skip_level = i + 1; |
| 2272 | continue; |
| 2273 | } |
| 2274 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2275 | if (skip_level < i && i >= lowest_unlock) |
| 2276 | no_skips = 1; |
| 2277 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2278 | t = path->nodes[i]; |
Liu Bo | d80bb3f | 2018-05-18 11:00:24 +0800 | [diff] [blame] | 2279 | if (i >= lowest_unlock && i > skip_level) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2280 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2281 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2282 | if (write_lock_level && |
| 2283 | i > min_write_lock_level && |
| 2284 | i <= *write_lock_level) { |
| 2285 | *write_lock_level = i - 1; |
| 2286 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2287 | } |
| 2288 | } |
| 2289 | } |
| 2290 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2291 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2292 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2293 | * in cache without setting the path to blocking. If we find the block |
| 2294 | * we return zero and the path is unchanged. |
| 2295 | * |
| 2296 | * If we can't find the block, we set the path blocking and do some |
| 2297 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2298 | */ |
| 2299 | static int |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 2300 | read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, |
| 2301 | struct extent_buffer **eb_ret, int level, int slot, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 2302 | const struct btrfs_key *key) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2303 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2304 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2305 | u64 blocknr; |
| 2306 | u64 gen; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2307 | struct extent_buffer *tmp; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2308 | struct btrfs_key first_key; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2309 | int ret; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2310 | int parent_level; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2311 | |
Nikolay Borisov | 213ff4b | 2020-05-27 13:10:59 +0300 | [diff] [blame] | 2312 | blocknr = btrfs_node_blockptr(*eb_ret, slot); |
| 2313 | gen = btrfs_node_ptr_generation(*eb_ret, slot); |
| 2314 | parent_level = btrfs_header_level(*eb_ret); |
| 2315 | btrfs_node_key_to_cpu(*eb_ret, &first_key, slot); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2316 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2317 | tmp = find_extent_buffer(fs_info, blocknr); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2318 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2319 | /* first we do an atomic uptodate check */ |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2320 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
Qu Wenruo | 448de47 | 2019-03-12 17:10:40 +0800 | [diff] [blame] | 2321 | /* |
| 2322 | * Do extra check for first_key, eb can be stale due to |
| 2323 | * being cached, read from scrub, or have multiple |
| 2324 | * parents (shared tree blocks). |
| 2325 | */ |
David Sterba | e064d5e | 2019-03-20 14:58:13 +0100 | [diff] [blame] | 2326 | if (btrfs_verify_level_key(tmp, |
Qu Wenruo | 448de47 | 2019-03-12 17:10:40 +0800 | [diff] [blame] | 2327 | parent_level - 1, &first_key, gen)) { |
| 2328 | free_extent_buffer(tmp); |
| 2329 | return -EUCLEAN; |
| 2330 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2331 | *eb_ret = tmp; |
| 2332 | return 0; |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2333 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2334 | |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2335 | /* now we're allowed to do a blocking uptodate check */ |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2336 | ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key); |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2337 | if (!ret) { |
| 2338 | *eb_ret = tmp; |
| 2339 | return 0; |
| 2340 | } |
| 2341 | free_extent_buffer(tmp); |
| 2342 | btrfs_release_path(p); |
| 2343 | return -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2344 | } |
| 2345 | |
| 2346 | /* |
| 2347 | * reduce lock contention at high levels |
| 2348 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2349 | * we read. Don't release the lock on the current |
| 2350 | * level because we need to walk this node to figure |
| 2351 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2352 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2353 | btrfs_unlock_up_safe(p, level + 1); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2354 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2355 | if (p->reada != READA_NONE) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2356 | reada_for_search(fs_info, p, level, slot, key->objectid); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2357 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2358 | ret = -EAGAIN; |
Josef Bacik | 1b7ec85 | 2020-11-05 10:45:18 -0500 | [diff] [blame] | 2359 | tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid, |
| 2360 | gen, parent_level - 1, &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 2361 | if (!IS_ERR(tmp)) { |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2362 | /* |
| 2363 | * If the read above didn't mark this buffer up to date, |
| 2364 | * it will never end up being up to date. Set ret to EIO now |
| 2365 | * and give up so that our caller doesn't loop forever |
| 2366 | * on our EAGAINs. |
| 2367 | */ |
Liu Bo | e6a1d6f | 2018-05-18 11:00:20 +0800 | [diff] [blame] | 2368 | if (!extent_buffer_uptodate(tmp)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2369 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2370 | free_extent_buffer(tmp); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2371 | } else { |
| 2372 | ret = PTR_ERR(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2373 | } |
Liu Bo | 02a3307 | 2018-05-16 01:37:36 +0800 | [diff] [blame] | 2374 | |
| 2375 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2376 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | /* |
| 2380 | * helper function for btrfs_search_slot. This does all of the checks |
| 2381 | * for node-level blocks and does any balancing required based on |
| 2382 | * the ins_len. |
| 2383 | * |
| 2384 | * If no extra work was required, zero is returned. If we had to |
| 2385 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2386 | * start over |
| 2387 | */ |
| 2388 | static int |
| 2389 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2390 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2391 | struct extent_buffer *b, int level, int ins_len, |
| 2392 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2393 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2394 | struct btrfs_fs_info *fs_info = root->fs_info; |
Nikolay Borisov | 95b982d | 2020-11-13 09:29:40 +0200 | [diff] [blame] | 2395 | int ret = 0; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2396 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2397 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2398 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2399 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2400 | if (*write_lock_level < level + 1) { |
| 2401 | *write_lock_level = level + 1; |
| 2402 | btrfs_release_path(p); |
Nikolay Borisov | 95b982d | 2020-11-13 09:29:40 +0200 | [diff] [blame] | 2403 | return -EAGAIN; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2404 | } |
| 2405 | |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 2406 | reada_for_balance(p, level); |
Nikolay Borisov | 95b982d | 2020-11-13 09:29:40 +0200 | [diff] [blame] | 2407 | ret = split_node(trans, root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2408 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2409 | b = p->nodes[level]; |
| 2410 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2411 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2412 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2413 | if (*write_lock_level < level + 1) { |
| 2414 | *write_lock_level = level + 1; |
| 2415 | btrfs_release_path(p); |
Nikolay Borisov | 95b982d | 2020-11-13 09:29:40 +0200 | [diff] [blame] | 2416 | return -EAGAIN; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2417 | } |
| 2418 | |
Josef Bacik | bfb484d | 2020-11-05 10:45:09 -0500 | [diff] [blame] | 2419 | reada_for_balance(p, level); |
Nikolay Borisov | 95b982d | 2020-11-13 09:29:40 +0200 | [diff] [blame] | 2420 | ret = balance_level(trans, root, p, level); |
| 2421 | if (ret) |
| 2422 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2423 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2424 | b = p->nodes[level]; |
| 2425 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2426 | btrfs_release_path(p); |
Nikolay Borisov | 95b982d | 2020-11-13 09:29:40 +0200 | [diff] [blame] | 2427 | return -EAGAIN; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2428 | } |
| 2429 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2430 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2431 | return ret; |
| 2432 | } |
| 2433 | |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 2434 | int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path, |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2435 | u64 iobjectid, u64 ioff, u8 key_type, |
| 2436 | struct btrfs_key *found_key) |
| 2437 | { |
| 2438 | int ret; |
| 2439 | struct btrfs_key key; |
| 2440 | struct extent_buffer *eb; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 2441 | |
| 2442 | ASSERT(path); |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 2443 | ASSERT(found_key); |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2444 | |
| 2445 | key.type = key_type; |
| 2446 | key.objectid = iobjectid; |
| 2447 | key.offset = ioff; |
| 2448 | |
| 2449 | ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 2450 | if (ret < 0) |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2451 | return ret; |
| 2452 | |
| 2453 | eb = path->nodes[0]; |
| 2454 | if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { |
| 2455 | ret = btrfs_next_leaf(fs_root, path); |
| 2456 | if (ret) |
| 2457 | return ret; |
| 2458 | eb = path->nodes[0]; |
| 2459 | } |
| 2460 | |
| 2461 | btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); |
| 2462 | if (found_key->type != key.type || |
| 2463 | found_key->objectid != key.objectid) |
| 2464 | return 1; |
| 2465 | |
| 2466 | return 0; |
| 2467 | } |
| 2468 | |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2469 | static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root, |
| 2470 | struct btrfs_path *p, |
| 2471 | int write_lock_level) |
| 2472 | { |
| 2473 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2474 | struct extent_buffer *b; |
| 2475 | int root_lock; |
| 2476 | int level = 0; |
| 2477 | |
| 2478 | /* We try very hard to do read locks on the root */ |
| 2479 | root_lock = BTRFS_READ_LOCK; |
| 2480 | |
| 2481 | if (p->search_commit_root) { |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2482 | /* |
| 2483 | * The commit roots are read only so we always do read locks, |
| 2484 | * and we always must hold the commit_root_sem when doing |
| 2485 | * searches on them, the only exception is send where we don't |
| 2486 | * want to block transaction commits for a long time, so |
| 2487 | * we need to clone the commit root in order to avoid races |
| 2488 | * with transaction commits that create a snapshot of one of |
| 2489 | * the roots used by a send operation. |
| 2490 | */ |
| 2491 | if (p->need_commit_sem) { |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2492 | down_read(&fs_info->commit_root_sem); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2493 | b = btrfs_clone_extent_buffer(root->commit_root); |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2494 | up_read(&fs_info->commit_root_sem); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2495 | if (!b) |
| 2496 | return ERR_PTR(-ENOMEM); |
| 2497 | |
| 2498 | } else { |
| 2499 | b = root->commit_root; |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 2500 | atomic_inc(&b->refs); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2501 | } |
| 2502 | level = btrfs_header_level(b); |
Liu Bo | f9ddfd0 | 2018-05-29 21:27:06 +0800 | [diff] [blame] | 2503 | /* |
| 2504 | * Ensure that all callers have set skip_locking when |
| 2505 | * p->search_commit_root = 1. |
| 2506 | */ |
| 2507 | ASSERT(p->skip_locking == 1); |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2508 | |
| 2509 | goto out; |
| 2510 | } |
| 2511 | |
| 2512 | if (p->skip_locking) { |
| 2513 | b = btrfs_root_node(root); |
| 2514 | level = btrfs_header_level(b); |
| 2515 | goto out; |
| 2516 | } |
| 2517 | |
| 2518 | /* |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2519 | * If the level is set to maximum, we can skip trying to get the read |
| 2520 | * lock. |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2521 | */ |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2522 | if (write_lock_level < BTRFS_MAX_LEVEL) { |
| 2523 | /* |
| 2524 | * We don't know the level of the root node until we actually |
| 2525 | * have it read locked |
| 2526 | */ |
Josef Bacik | 1bb9659 | 2020-11-06 16:27:33 -0500 | [diff] [blame] | 2527 | b = btrfs_read_lock_root_node(root); |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2528 | level = btrfs_header_level(b); |
| 2529 | if (level > write_lock_level) |
| 2530 | goto out; |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2531 | |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2532 | /* Whoops, must trade for write lock */ |
| 2533 | btrfs_tree_read_unlock(b); |
| 2534 | free_extent_buffer(b); |
| 2535 | } |
| 2536 | |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2537 | b = btrfs_lock_root_node(root); |
| 2538 | root_lock = BTRFS_WRITE_LOCK; |
| 2539 | |
| 2540 | /* The level might have changed, check again */ |
| 2541 | level = btrfs_header_level(b); |
| 2542 | |
| 2543 | out: |
| 2544 | p->nodes[level] = b; |
| 2545 | if (!p->skip_locking) |
| 2546 | p->locks[level] = root_lock; |
| 2547 | /* |
| 2548 | * Callers are responsible for dropping b's references. |
| 2549 | */ |
| 2550 | return b; |
| 2551 | } |
| 2552 | |
| 2553 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2554 | /* |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2555 | * btrfs_search_slot - look for a key in a tree and perform necessary |
| 2556 | * modifications to preserve tree invariants. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2557 | * |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2558 | * @trans: Handle of transaction, used when modifying the tree |
| 2559 | * @p: Holds all btree nodes along the search path |
| 2560 | * @root: The root node of the tree |
| 2561 | * @key: The key we are looking for |
ethanwu | 9a66497 | 2020-12-01 17:25:12 +0800 | [diff] [blame] | 2562 | * @ins_len: Indicates purpose of search: |
| 2563 | * >0 for inserts it's size of item inserted (*) |
| 2564 | * <0 for deletions |
| 2565 | * 0 for plain searches, not modifying the tree |
| 2566 | * |
| 2567 | * (*) If size of item inserted doesn't include |
| 2568 | * sizeof(struct btrfs_item), then p->search_for_extension must |
| 2569 | * be set. |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2570 | * @cow: boolean should CoW operations be performed. Must always be 1 |
| 2571 | * when modifying the tree. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2572 | * |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2573 | * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. |
| 2574 | * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) |
| 2575 | * |
| 2576 | * If @key is found, 0 is returned and you can find the item in the leaf level |
| 2577 | * of the path (level 0) |
| 2578 | * |
| 2579 | * If @key isn't found, 1 is returned and the leaf level of the path (level 0) |
| 2580 | * points to the slot where it should be inserted |
| 2581 | * |
| 2582 | * If an error is encountered while searching the tree a negative error number |
| 2583 | * is returned |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2584 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2585 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2586 | const struct btrfs_key *key, struct btrfs_path *p, |
| 2587 | int ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2588 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2589 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2590 | int slot; |
| 2591 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2592 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2593 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2594 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2595 | /* everything at write_lock_level or lower must be write locked */ |
| 2596 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2597 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2598 | int min_write_lock_level; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2599 | int prev_cmp; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2600 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2601 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2602 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2603 | WARN_ON(p->nodes[0] != NULL); |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2604 | BUG_ON(!cow && ins_len); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2605 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2606 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2607 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2608 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2609 | /* when we are removing items, we might have to go up to level |
| 2610 | * two as we update tree pointers Make sure we keep write |
| 2611 | * for those levels as well |
| 2612 | */ |
| 2613 | write_lock_level = 2; |
| 2614 | } else if (ins_len > 0) { |
| 2615 | /* |
| 2616 | * for inserting items, make sure we have a write lock on |
| 2617 | * level 1 so we can update keys |
| 2618 | */ |
| 2619 | write_lock_level = 1; |
| 2620 | } |
| 2621 | |
| 2622 | if (!cow) |
| 2623 | write_lock_level = -1; |
| 2624 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2625 | if (cow && (p->keep_locks || p->lowest_level)) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2626 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2627 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2628 | min_write_lock_level = write_lock_level; |
| 2629 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2630 | again: |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2631 | prev_cmp = -1; |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2632 | b = btrfs_search_slot_get_root(root, p, write_lock_level); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2633 | if (IS_ERR(b)) { |
| 2634 | ret = PTR_ERR(b); |
| 2635 | goto done; |
| 2636 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2637 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2638 | while (b) { |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2639 | int dec = 0; |
| 2640 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2641 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2642 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2643 | if (cow) { |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2644 | bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); |
| 2645 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2646 | /* |
| 2647 | * if we don't really need to cow this block |
| 2648 | * then we don't want to set the path blocking, |
| 2649 | * so we test it here |
| 2650 | */ |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 2651 | if (!should_cow_block(trans, root, b)) { |
| 2652 | trans->dirty = true; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2653 | goto cow_done; |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 2654 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2655 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2656 | /* |
| 2657 | * must have write locks on this node and the |
| 2658 | * parent |
| 2659 | */ |
Josef Bacik | 5124e00 | 2012-11-07 13:44:13 -0500 | [diff] [blame] | 2660 | if (level > write_lock_level || |
| 2661 | (level + 1 > write_lock_level && |
| 2662 | level + 1 < BTRFS_MAX_LEVEL && |
| 2663 | p->nodes[level + 1])) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2664 | write_lock_level = level + 1; |
| 2665 | btrfs_release_path(p); |
| 2666 | goto again; |
| 2667 | } |
| 2668 | |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2669 | if (last_level) |
| 2670 | err = btrfs_cow_block(trans, root, b, NULL, 0, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 2671 | &b, |
| 2672 | BTRFS_NESTING_COW); |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2673 | else |
| 2674 | err = btrfs_cow_block(trans, root, b, |
| 2675 | p->nodes[level + 1], |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 2676 | p->slots[level + 1], &b, |
| 2677 | BTRFS_NESTING_COW); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2678 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2679 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2680 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2681 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2682 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2683 | cow_done: |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2684 | p->nodes[level] = b; |
Liu Bo | 5239834 | 2018-08-22 05:54:37 +0800 | [diff] [blame] | 2685 | /* |
| 2686 | * Leave path with blocking locks to avoid massive |
| 2687 | * lock context switch, this is made on purpose. |
| 2688 | */ |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2689 | |
| 2690 | /* |
| 2691 | * we have a lock on b and as long as we aren't changing |
| 2692 | * the tree, there is no way to for the items in b to change. |
| 2693 | * It is safe to drop the lock on our parent before we |
| 2694 | * go through the expensive btree search on b. |
| 2695 | * |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2696 | * If we're inserting or deleting (ins_len != 0), then we might |
| 2697 | * be changing slot zero, which may require changing the parent. |
| 2698 | * So, we can't drop the lock until after we know which slot |
| 2699 | * we're operating on. |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2700 | */ |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2701 | if (!ins_len && !p->keep_locks) { |
| 2702 | int u = level + 1; |
| 2703 | |
| 2704 | if (u < BTRFS_MAX_LEVEL && p->locks[u]) { |
| 2705 | btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); |
| 2706 | p->locks[u] = 0; |
| 2707 | } |
| 2708 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2709 | |
Nikolay Borisov | 995e9a1 | 2020-05-27 13:10:53 +0300 | [diff] [blame] | 2710 | /* |
| 2711 | * If btrfs_bin_search returns an exact match (prev_cmp == 0) |
| 2712 | * we can safely assume the target key will always be in slot 0 |
| 2713 | * on lower levels due to the invariants BTRFS' btree provides, |
| 2714 | * namely that a btrfs_key_ptr entry always points to the |
| 2715 | * lowest key in the child node, thus we can skip searching |
| 2716 | * lower levels |
| 2717 | */ |
| 2718 | if (prev_cmp == 0) { |
| 2719 | slot = 0; |
| 2720 | ret = 0; |
| 2721 | } else { |
| 2722 | ret = btrfs_bin_search(b, key, &slot); |
| 2723 | prev_cmp = ret; |
| 2724 | if (ret < 0) |
| 2725 | goto done; |
| 2726 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2727 | |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2728 | if (level == 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2729 | p->slots[level] = slot; |
ethanwu | 9a66497 | 2020-12-01 17:25:12 +0800 | [diff] [blame] | 2730 | /* |
| 2731 | * Item key already exists. In this case, if we are |
| 2732 | * allowed to insert the item (for example, in dir_item |
| 2733 | * case, item key collision is allowed), it will be |
| 2734 | * merged with the original item. Only the item size |
| 2735 | * grows, no new btrfs item will be added. If |
| 2736 | * search_for_extension is not set, ins_len already |
| 2737 | * accounts the size btrfs_item, deduct it here so leaf |
| 2738 | * space check will be correct. |
| 2739 | */ |
| 2740 | if (ret == 0 && ins_len > 0 && !p->search_for_extension) { |
| 2741 | ASSERT(ins_len >= sizeof(struct btrfs_item)); |
| 2742 | ins_len -= sizeof(struct btrfs_item); |
| 2743 | } |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2744 | if (ins_len > 0 && |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 2745 | btrfs_leaf_free_space(b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2746 | if (write_lock_level < 1) { |
| 2747 | write_lock_level = 1; |
| 2748 | btrfs_release_path(p); |
| 2749 | goto again; |
| 2750 | } |
| 2751 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2752 | err = split_leaf(trans, root, key, |
| 2753 | p, ins_len, ret == 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2754 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2755 | BUG_ON(err > 0); |
| 2756 | if (err) { |
| 2757 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2758 | goto done; |
| 2759 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2760 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2761 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2762 | unlock_up(p, level, lowest_unlock, |
Liu Bo | 4b6f8e9 | 2018-08-14 10:46:53 +0800 | [diff] [blame] | 2763 | min_write_lock_level, NULL); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2764 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2765 | } |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2766 | if (ret && slot > 0) { |
| 2767 | dec = 1; |
| 2768 | slot--; |
| 2769 | } |
| 2770 | p->slots[level] = slot; |
| 2771 | err = setup_nodes_for_search(trans, root, p, b, level, ins_len, |
| 2772 | &write_lock_level); |
| 2773 | if (err == -EAGAIN) |
| 2774 | goto again; |
| 2775 | if (err) { |
| 2776 | ret = err; |
| 2777 | goto done; |
| 2778 | } |
| 2779 | b = p->nodes[level]; |
| 2780 | slot = p->slots[level]; |
| 2781 | |
| 2782 | /* |
| 2783 | * Slot 0 is special, if we change the key we have to update |
| 2784 | * the parent pointer which means we must have a write lock on |
| 2785 | * the parent |
| 2786 | */ |
| 2787 | if (slot == 0 && ins_len && write_lock_level < level + 1) { |
| 2788 | write_lock_level = level + 1; |
| 2789 | btrfs_release_path(p); |
| 2790 | goto again; |
| 2791 | } |
| 2792 | |
| 2793 | unlock_up(p, level, lowest_unlock, min_write_lock_level, |
| 2794 | &write_lock_level); |
| 2795 | |
| 2796 | if (level == lowest_level) { |
| 2797 | if (dec) |
| 2798 | p->slots[level]++; |
| 2799 | goto done; |
| 2800 | } |
| 2801 | |
| 2802 | err = read_block_for_search(root, p, &b, level, slot, key); |
| 2803 | if (err == -EAGAIN) |
| 2804 | goto again; |
| 2805 | if (err) { |
| 2806 | ret = err; |
| 2807 | goto done; |
| 2808 | } |
| 2809 | |
| 2810 | if (!p->skip_locking) { |
| 2811 | level = btrfs_header_level(b); |
| 2812 | if (level <= write_lock_level) { |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 2813 | btrfs_tree_lock(b); |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2814 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2815 | } else { |
Josef Bacik | fe596ca | 2020-11-06 16:27:34 -0500 | [diff] [blame] | 2816 | btrfs_tree_read_lock(b); |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2817 | p->locks[level] = BTRFS_READ_LOCK; |
| 2818 | } |
| 2819 | p->nodes[level] = b; |
| 2820 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2821 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2822 | ret = 1; |
| 2823 | done: |
Filipe Manana | 5f5bc6b | 2014-11-09 08:38:39 +0000 | [diff] [blame] | 2824 | if (ret < 0 && !p->skip_release_on_error) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2825 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2826 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2827 | } |
Josef Bacik | f75e2b7 | 2020-12-16 11:18:43 -0500 | [diff] [blame] | 2828 | ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2829 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2830 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2831 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2832 | * current state of the tree together with the operations recorded in the tree |
| 2833 | * modification log to search for the key in a previous version of this tree, as |
| 2834 | * denoted by the time_seq parameter. |
| 2835 | * |
| 2836 | * Naturally, there is no support for insert, delete or cow operations. |
| 2837 | * |
| 2838 | * The resulting path and return value will be set up as if we called |
| 2839 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2840 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2841 | int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2842 | struct btrfs_path *p, u64 time_seq) |
| 2843 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2844 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2845 | struct extent_buffer *b; |
| 2846 | int slot; |
| 2847 | int ret; |
| 2848 | int err; |
| 2849 | int level; |
| 2850 | int lowest_unlock = 1; |
| 2851 | u8 lowest_level = 0; |
| 2852 | |
| 2853 | lowest_level = p->lowest_level; |
| 2854 | WARN_ON(p->nodes[0] != NULL); |
| 2855 | |
| 2856 | if (p->search_commit_root) { |
| 2857 | BUG_ON(time_seq); |
| 2858 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2859 | } |
| 2860 | |
| 2861 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2862 | b = get_old_root(root, time_seq); |
Nikolay Borisov | 315bed4 | 2018-09-13 11:35:10 +0300 | [diff] [blame] | 2863 | if (!b) { |
| 2864 | ret = -EIO; |
| 2865 | goto done; |
| 2866 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2867 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2868 | p->locks[level] = BTRFS_READ_LOCK; |
| 2869 | |
| 2870 | while (b) { |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2871 | int dec = 0; |
| 2872 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2873 | level = btrfs_header_level(b); |
| 2874 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2875 | |
| 2876 | /* |
| 2877 | * we have a lock on b and as long as we aren't changing |
| 2878 | * the tree, there is no way to for the items in b to change. |
| 2879 | * It is safe to drop the lock on our parent before we |
| 2880 | * go through the expensive btree search on b. |
| 2881 | */ |
| 2882 | btrfs_unlock_up_safe(p, level + 1); |
| 2883 | |
Nikolay Borisov | 995e9a1 | 2020-05-27 13:10:53 +0300 | [diff] [blame] | 2884 | ret = btrfs_bin_search(b, key, &slot); |
Filipe Manana | cbca7d5 | 2019-02-18 16:57:26 +0000 | [diff] [blame] | 2885 | if (ret < 0) |
| 2886 | goto done; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2887 | |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2888 | if (level == 0) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2889 | p->slots[level] = slot; |
| 2890 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2891 | goto done; |
| 2892 | } |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2893 | |
| 2894 | if (ret && slot > 0) { |
| 2895 | dec = 1; |
| 2896 | slot--; |
| 2897 | } |
| 2898 | p->slots[level] = slot; |
| 2899 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2900 | |
| 2901 | if (level == lowest_level) { |
| 2902 | if (dec) |
| 2903 | p->slots[level]++; |
| 2904 | goto done; |
| 2905 | } |
| 2906 | |
| 2907 | err = read_block_for_search(root, p, &b, level, slot, key); |
| 2908 | if (err == -EAGAIN) |
| 2909 | goto again; |
| 2910 | if (err) { |
| 2911 | ret = err; |
| 2912 | goto done; |
| 2913 | } |
| 2914 | |
| 2915 | level = btrfs_header_level(b); |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 2916 | btrfs_tree_read_lock(b); |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2917 | b = tree_mod_log_rewind(fs_info, p, b, time_seq); |
| 2918 | if (!b) { |
| 2919 | ret = -ENOMEM; |
| 2920 | goto done; |
| 2921 | } |
| 2922 | p->locks[level] = BTRFS_READ_LOCK; |
| 2923 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2924 | } |
| 2925 | ret = 1; |
| 2926 | done: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2927 | if (ret < 0) |
| 2928 | btrfs_release_path(p); |
| 2929 | |
| 2930 | return ret; |
| 2931 | } |
| 2932 | |
| 2933 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2934 | * helper to use instead of search slot if no exact match is needed but |
| 2935 | * instead the next or previous item should be returned. |
| 2936 | * When find_higher is true, the next higher item is returned, the next lower |
| 2937 | * otherwise. |
| 2938 | * When return_any and find_higher are both true, and no higher item is found, |
| 2939 | * return the next lower instead. |
| 2940 | * When return_any is true and find_higher is false, and no lower item is found, |
| 2941 | * return the next higher instead. |
| 2942 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 2943 | * < 0 on error |
| 2944 | */ |
| 2945 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2946 | const struct btrfs_key *key, |
| 2947 | struct btrfs_path *p, int find_higher, |
| 2948 | int return_any) |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2949 | { |
| 2950 | int ret; |
| 2951 | struct extent_buffer *leaf; |
| 2952 | |
| 2953 | again: |
| 2954 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2955 | if (ret <= 0) |
| 2956 | return ret; |
| 2957 | /* |
| 2958 | * a return value of 1 means the path is at the position where the |
| 2959 | * item should be inserted. Normally this is the next bigger item, |
| 2960 | * but in case the previous item is the last in a leaf, path points |
| 2961 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 2962 | * item. |
| 2963 | */ |
| 2964 | leaf = p->nodes[0]; |
| 2965 | |
| 2966 | if (find_higher) { |
| 2967 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 2968 | ret = btrfs_next_leaf(root, p); |
| 2969 | if (ret <= 0) |
| 2970 | return ret; |
| 2971 | if (!return_any) |
| 2972 | return 1; |
| 2973 | /* |
| 2974 | * no higher item found, return the next |
| 2975 | * lower instead |
| 2976 | */ |
| 2977 | return_any = 0; |
| 2978 | find_higher = 0; |
| 2979 | btrfs_release_path(p); |
| 2980 | goto again; |
| 2981 | } |
| 2982 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2983 | if (p->slots[0] == 0) { |
| 2984 | ret = btrfs_prev_leaf(root, p); |
| 2985 | if (ret < 0) |
| 2986 | return ret; |
| 2987 | if (!ret) { |
Filipe David Borba Manana | 23c6bf6 | 2014-01-11 21:28:54 +0000 | [diff] [blame] | 2988 | leaf = p->nodes[0]; |
| 2989 | if (p->slots[0] == btrfs_header_nritems(leaf)) |
| 2990 | p->slots[0]--; |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2991 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2992 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 2993 | if (!return_any) |
| 2994 | return 1; |
| 2995 | /* |
| 2996 | * no lower item found, return the next |
| 2997 | * higher instead |
| 2998 | */ |
| 2999 | return_any = 0; |
| 3000 | find_higher = 1; |
| 3001 | btrfs_release_path(p); |
| 3002 | goto again; |
| 3003 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3004 | --p->slots[0]; |
| 3005 | } |
| 3006 | } |
| 3007 | return 0; |
| 3008 | } |
| 3009 | |
| 3010 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3011 | * adjust the pointers going up the tree, starting at level |
| 3012 | * making sure the right key of each node is points to 'key'. |
| 3013 | * This is used after shifting pointers to the left, so it stops |
| 3014 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 3015 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3016 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3017 | */ |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 3018 | static void fixup_low_keys(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3019 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3020 | { |
| 3021 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3022 | struct extent_buffer *t; |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 3023 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3024 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 3025 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3026 | int tslot = path->slots[i]; |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 3027 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3028 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3029 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3030 | t = path->nodes[i]; |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 3031 | ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE, |
| 3032 | GFP_ATOMIC); |
| 3033 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3034 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3035 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3036 | if (tslot != 0) |
| 3037 | break; |
| 3038 | } |
| 3039 | } |
| 3040 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3041 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3042 | * update item key. |
| 3043 | * |
| 3044 | * This function isn't completely safe. It's the caller's responsibility |
| 3045 | * that the new key won't break the order |
| 3046 | */ |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 3047 | void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, |
| 3048 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 3049 | const struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3050 | { |
| 3051 | struct btrfs_disk_key disk_key; |
| 3052 | struct extent_buffer *eb; |
| 3053 | int slot; |
| 3054 | |
| 3055 | eb = path->nodes[0]; |
| 3056 | slot = path->slots[0]; |
| 3057 | if (slot > 0) { |
| 3058 | btrfs_item_key(eb, &disk_key, slot - 1); |
Qu Wenruo | 7c15d41 | 2019-04-25 08:55:53 +0800 | [diff] [blame] | 3059 | if (unlikely(comp_keys(&disk_key, new_key) >= 0)) { |
| 3060 | btrfs_crit(fs_info, |
| 3061 | "slot %u key (%llu %u %llu) new key (%llu %u %llu)", |
| 3062 | slot, btrfs_disk_key_objectid(&disk_key), |
| 3063 | btrfs_disk_key_type(&disk_key), |
| 3064 | btrfs_disk_key_offset(&disk_key), |
| 3065 | new_key->objectid, new_key->type, |
| 3066 | new_key->offset); |
| 3067 | btrfs_print_leaf(eb); |
| 3068 | BUG(); |
| 3069 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3070 | } |
| 3071 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 3072 | btrfs_item_key(eb, &disk_key, slot + 1); |
Qu Wenruo | 7c15d41 | 2019-04-25 08:55:53 +0800 | [diff] [blame] | 3073 | if (unlikely(comp_keys(&disk_key, new_key) <= 0)) { |
| 3074 | btrfs_crit(fs_info, |
| 3075 | "slot %u key (%llu %u %llu) new key (%llu %u %llu)", |
| 3076 | slot, btrfs_disk_key_objectid(&disk_key), |
| 3077 | btrfs_disk_key_type(&disk_key), |
| 3078 | btrfs_disk_key_offset(&disk_key), |
| 3079 | new_key->objectid, new_key->type, |
| 3080 | new_key->offset); |
| 3081 | btrfs_print_leaf(eb); |
| 3082 | BUG(); |
| 3083 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3084 | } |
| 3085 | |
| 3086 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 3087 | btrfs_set_item_key(eb, &disk_key, slot); |
| 3088 | btrfs_mark_buffer_dirty(eb); |
| 3089 | if (slot == 0) |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 3090 | fixup_low_keys(path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3091 | } |
| 3092 | |
| 3093 | /* |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3094 | * Check key order of two sibling extent buffers. |
| 3095 | * |
| 3096 | * Return true if something is wrong. |
| 3097 | * Return false if everything is fine. |
| 3098 | * |
| 3099 | * Tree-checker only works inside one tree block, thus the following |
| 3100 | * corruption can not be detected by tree-checker: |
| 3101 | * |
| 3102 | * Leaf @left | Leaf @right |
| 3103 | * -------------------------------------------------------------- |
| 3104 | * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 | |
| 3105 | * |
| 3106 | * Key f6 in leaf @left itself is valid, but not valid when the next |
| 3107 | * key in leaf @right is 7. |
| 3108 | * This can only be checked at tree block merge time. |
| 3109 | * And since tree checker has ensured all key order in each tree block |
| 3110 | * is correct, we only need to bother the last key of @left and the first |
| 3111 | * key of @right. |
| 3112 | */ |
| 3113 | static bool check_sibling_keys(struct extent_buffer *left, |
| 3114 | struct extent_buffer *right) |
| 3115 | { |
| 3116 | struct btrfs_key left_last; |
| 3117 | struct btrfs_key right_first; |
| 3118 | int level = btrfs_header_level(left); |
| 3119 | int nr_left = btrfs_header_nritems(left); |
| 3120 | int nr_right = btrfs_header_nritems(right); |
| 3121 | |
| 3122 | /* No key to check in one of the tree blocks */ |
| 3123 | if (!nr_left || !nr_right) |
| 3124 | return false; |
| 3125 | |
| 3126 | if (level) { |
| 3127 | btrfs_node_key_to_cpu(left, &left_last, nr_left - 1); |
| 3128 | btrfs_node_key_to_cpu(right, &right_first, 0); |
| 3129 | } else { |
| 3130 | btrfs_item_key_to_cpu(left, &left_last, nr_left - 1); |
| 3131 | btrfs_item_key_to_cpu(right, &right_first, 0); |
| 3132 | } |
| 3133 | |
| 3134 | if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) { |
| 3135 | btrfs_crit(left->fs_info, |
| 3136 | "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)", |
| 3137 | left_last.objectid, left_last.type, |
| 3138 | left_last.offset, right_first.objectid, |
| 3139 | right_first.type, right_first.offset); |
| 3140 | return true; |
| 3141 | } |
| 3142 | return false; |
| 3143 | } |
| 3144 | |
| 3145 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3146 | * try to push data from one node into the next node left in the |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3147 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3148 | * |
| 3149 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 3150 | * error, and > 0 if there was no room in the left hand block. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3151 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3152 | static int push_node_left(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3153 | struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3154 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3155 | { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 3156 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3157 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3158 | int src_nritems; |
| 3159 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3160 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3161 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3162 | src_nritems = btrfs_header_nritems(src); |
| 3163 | dst_nritems = btrfs_header_nritems(dst); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3164 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3165 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3166 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3167 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3168 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3169 | return 1; |
| 3170 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3171 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3172 | return 1; |
| 3173 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3174 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3175 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3176 | if (push_items < src_nritems) { |
| 3177 | /* leave at least 8 pointers in the node if |
| 3178 | * we aren't going to empty it |
| 3179 | */ |
| 3180 | if (src_nritems - push_items < 8) { |
| 3181 | if (push_items <= 8) |
| 3182 | return 1; |
| 3183 | push_items -= 8; |
| 3184 | } |
| 3185 | } |
| 3186 | } else |
| 3187 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3188 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3189 | /* dst is the left eb, src is the middle eb */ |
| 3190 | if (check_sibling_keys(dst, src)) { |
| 3191 | ret = -EUCLEAN; |
| 3192 | btrfs_abort_transaction(trans, ret); |
| 3193 | return ret; |
| 3194 | } |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 3195 | ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3196 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3197 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3198 | return ret; |
| 3199 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3200 | copy_extent_buffer(dst, src, |
| 3201 | btrfs_node_key_ptr_offset(dst_nritems), |
| 3202 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3203 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3204 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3205 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3206 | /* |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3207 | * Don't call tree_mod_log_insert_move here, key removal was |
| 3208 | * already fully logged by tree_mod_log_eb_copy above. |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3209 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3210 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 3211 | btrfs_node_key_ptr_offset(push_items), |
| 3212 | (src_nritems - push_items) * |
| 3213 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3214 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3215 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3216 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 3217 | btrfs_mark_buffer_dirty(src); |
| 3218 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3219 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3220 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3221 | } |
| 3222 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3223 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3224 | * try to push data from one node into the next node right in the |
| 3225 | * tree. |
| 3226 | * |
| 3227 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 3228 | * error, and > 0 if there was no room in the right hand block. |
| 3229 | * |
| 3230 | * this will only push up to 1/2 the contents of the left node over |
| 3231 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3232 | static int balance_node_right(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3233 | struct extent_buffer *dst, |
| 3234 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3235 | { |
David Sterba | 55d32ed | 2019-03-20 14:18:06 +0100 | [diff] [blame] | 3236 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3237 | int push_items = 0; |
| 3238 | int max_push; |
| 3239 | int src_nritems; |
| 3240 | int dst_nritems; |
| 3241 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3242 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3243 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3244 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3245 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3246 | src_nritems = btrfs_header_nritems(src); |
| 3247 | dst_nritems = btrfs_header_nritems(dst); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3248 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3249 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3250 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3251 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3252 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3253 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3254 | |
| 3255 | max_push = src_nritems / 2 + 1; |
| 3256 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3257 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3258 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3259 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3260 | if (max_push < push_items) |
| 3261 | push_items = max_push; |
| 3262 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3263 | /* dst is the right eb, src is the middle eb */ |
| 3264 | if (check_sibling_keys(src, dst)) { |
| 3265 | ret = -EUCLEAN; |
| 3266 | btrfs_abort_transaction(trans, ret); |
| 3267 | return ret; |
| 3268 | } |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3269 | ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems); |
| 3270 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3271 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3272 | btrfs_node_key_ptr_offset(0), |
| 3273 | (dst_nritems) * |
| 3274 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3275 | |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 3276 | ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items, |
| 3277 | push_items); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3278 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3279 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3280 | return ret; |
| 3281 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3282 | copy_extent_buffer(dst, src, |
| 3283 | btrfs_node_key_ptr_offset(0), |
| 3284 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3285 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3286 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3287 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3288 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3289 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3290 | btrfs_mark_buffer_dirty(src); |
| 3291 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3292 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3293 | return ret; |
| 3294 | } |
| 3295 | |
| 3296 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3297 | * helper function to insert a new root level in the tree. |
| 3298 | * A new node is allocated, and a single item is inserted to |
| 3299 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3300 | * |
| 3301 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3302 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3303 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3304 | struct btrfs_root *root, |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3305 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3306 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3307 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3308 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3309 | struct extent_buffer *lower; |
| 3310 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3311 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3312 | struct btrfs_disk_key lower_key; |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 3313 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3314 | |
| 3315 | BUG_ON(path->nodes[level]); |
| 3316 | BUG_ON(path->nodes[level-1] != root->node); |
| 3317 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3318 | lower = path->nodes[level-1]; |
| 3319 | if (level == 1) |
| 3320 | btrfs_item_key(lower, &lower_key, 0); |
| 3321 | else |
| 3322 | btrfs_node_key(lower, &lower_key, 0); |
| 3323 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 3324 | c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 3325 | root->node->start, 0, |
Josef Bacik | cf6f34a | 2020-08-20 11:46:07 -0400 | [diff] [blame] | 3326 | BTRFS_NESTING_NEW_ROOT); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3327 | if (IS_ERR(c)) |
| 3328 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3329 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3330 | root_add_used(root, fs_info->nodesize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3331 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3332 | btrfs_set_header_nritems(c, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3333 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3334 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3335 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3336 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3337 | |
| 3338 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3339 | |
| 3340 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3341 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3342 | old = root->node; |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 3343 | ret = tree_mod_log_insert_root(root->node, c, 0); |
| 3344 | BUG_ON(ret < 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3345 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3346 | |
| 3347 | /* the super has an extra ref to root->node */ |
| 3348 | free_extent_buffer(old); |
| 3349 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3350 | add_root_to_dirty_list(root); |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 3351 | atomic_inc(&c->refs); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3352 | path->nodes[level] = c; |
Josef Bacik | ac5887c | 2020-08-20 11:46:10 -0400 | [diff] [blame] | 3353 | path->locks[level] = BTRFS_WRITE_LOCK; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3354 | path->slots[level] = 0; |
| 3355 | return 0; |
| 3356 | } |
| 3357 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3358 | /* |
| 3359 | * worker function to insert a single pointer in a node. |
| 3360 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3361 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3362 | * slot and level indicate where you want the key to go, and |
| 3363 | * blocknr is the block the key points to. |
| 3364 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3365 | static void insert_ptr(struct btrfs_trans_handle *trans, |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 3366 | struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3367 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3368 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3369 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3370 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3371 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3372 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3373 | |
| 3374 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3375 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3376 | lower = path->nodes[level]; |
| 3377 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3378 | BUG_ON(slot > nritems); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 3379 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3380 | if (slot != nritems) { |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3381 | if (level) { |
| 3382 | ret = tree_mod_log_insert_move(lower, slot + 1, slot, |
David Sterba | a446a97 | 2018-03-05 15:26:29 +0100 | [diff] [blame] | 3383 | nritems - slot); |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3384 | BUG_ON(ret < 0); |
| 3385 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3386 | memmove_extent_buffer(lower, |
| 3387 | btrfs_node_key_ptr_offset(slot + 1), |
| 3388 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3389 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3390 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3391 | if (level) { |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 3392 | ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD, |
| 3393 | GFP_NOFS); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3394 | BUG_ON(ret < 0); |
| 3395 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3396 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3397 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3398 | WARN_ON(trans->transid == 0); |
| 3399 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3400 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3401 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3402 | } |
| 3403 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3404 | /* |
| 3405 | * split the node at the specified level in path in two. |
| 3406 | * The path is corrected to point to the appropriate node after the split |
| 3407 | * |
| 3408 | * Before splitting this tries to make some room in the node by pushing |
| 3409 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3410 | * |
| 3411 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3412 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3413 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3414 | struct btrfs_root *root, |
| 3415 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3416 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3417 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3418 | struct extent_buffer *c; |
| 3419 | struct extent_buffer *split; |
| 3420 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3421 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3422 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3423 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3424 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3425 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3426 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3427 | if (c == root->node) { |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3428 | /* |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3429 | * trying to split the root, lets make a new one |
| 3430 | * |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3431 | * tree mod log: We don't log_removal old root in |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3432 | * insert_new_root, because that root buffer will be kept as a |
| 3433 | * normal node. We are going to log removal of half of the |
| 3434 | * elements below with tree_mod_log_eb_copy. We're holding a |
| 3435 | * tree lock on the buffer, which is why we cannot race with |
| 3436 | * other tree_mod_log users. |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3437 | */ |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3438 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3439 | if (ret) |
| 3440 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3441 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3442 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3443 | c = path->nodes[level]; |
| 3444 | if (!ret && btrfs_header_nritems(c) < |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3445 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3446 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3447 | if (ret < 0) |
| 3448 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3449 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3450 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3451 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3452 | mid = (c_nritems + 1) / 2; |
| 3453 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3454 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 3455 | split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level, |
Josef Bacik | 4dff97e | 2020-08-20 11:46:06 -0400 | [diff] [blame] | 3456 | c->start, 0, BTRFS_NESTING_SPLIT); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3457 | if (IS_ERR(split)) |
| 3458 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3459 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3460 | root_add_used(root, fs_info->nodesize); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 3461 | ASSERT(btrfs_header_level(c) == level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3462 | |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 3463 | ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3464 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3465 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3466 | return ret; |
| 3467 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3468 | copy_extent_buffer(split, c, |
| 3469 | btrfs_node_key_ptr_offset(0), |
| 3470 | btrfs_node_key_ptr_offset(mid), |
| 3471 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3472 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3473 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3474 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3475 | btrfs_mark_buffer_dirty(c); |
| 3476 | btrfs_mark_buffer_dirty(split); |
| 3477 | |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 3478 | insert_ptr(trans, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3479 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3480 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3481 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3482 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3483 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3484 | free_extent_buffer(c); |
| 3485 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3486 | path->slots[level + 1] += 1; |
| 3487 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3488 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3489 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3490 | } |
Nikolay Borisov | d5286a92 | 2020-11-12 13:24:02 +0200 | [diff] [blame] | 3491 | return 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3492 | } |
| 3493 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3494 | /* |
| 3495 | * how many bytes are required to store the items in a leaf. start |
| 3496 | * and nr indicate which items in the leaf to check. This totals up the |
| 3497 | * space used both by the item structs and the item data |
| 3498 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3499 | static int leaf_space_used(struct extent_buffer *l, int start, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3500 | { |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3501 | struct btrfs_item *start_item; |
| 3502 | struct btrfs_item *end_item; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3503 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3504 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3505 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3506 | |
| 3507 | if (!nr) |
| 3508 | return 0; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3509 | start_item = btrfs_item_nr(start); |
| 3510 | end_item = btrfs_item_nr(end); |
David Sterba | a31356b | 2020-04-29 22:56:01 +0200 | [diff] [blame] | 3511 | data_len = btrfs_item_offset(l, start_item) + |
| 3512 | btrfs_item_size(l, start_item); |
| 3513 | data_len = data_len - btrfs_item_offset(l, end_item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3514 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3515 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3516 | return data_len; |
| 3517 | } |
| 3518 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3519 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3520 | * The space between the end of the leaf items and |
| 3521 | * the start of the leaf data. IOW, how much room |
| 3522 | * the leaf has left for both items and data |
| 3523 | */ |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3524 | noinline int btrfs_leaf_free_space(struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3525 | { |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3526 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3527 | int nritems = btrfs_header_nritems(leaf); |
| 3528 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3529 | |
| 3530 | ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3531 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3532 | btrfs_crit(fs_info, |
| 3533 | "leaf free space ret %d, leaf data size %lu, used %d nritems %d", |
| 3534 | ret, |
| 3535 | (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), |
| 3536 | leaf_space_used(leaf, 0, nritems), nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3537 | } |
| 3538 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3539 | } |
| 3540 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3541 | /* |
| 3542 | * min slot controls the lowest index we're willing to push to the |
| 3543 | * right. We'll push up to and including min_slot, but no lower |
| 3544 | */ |
David Sterba | f72f001 | 2019-03-20 14:39:45 +0100 | [diff] [blame] | 3545 | static noinline int __push_leaf_right(struct btrfs_path *path, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3546 | int data_size, int empty, |
| 3547 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3548 | int free_space, u32 left_nritems, |
| 3549 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3550 | { |
David Sterba | f72f001 | 2019-03-20 14:39:45 +0100 | [diff] [blame] | 3551 | struct btrfs_fs_info *fs_info = right->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3552 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3553 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3554 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3555 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3556 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3557 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3558 | int push_space = 0; |
| 3559 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3560 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3561 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3562 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3563 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3564 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3565 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3566 | if (empty) |
| 3567 | nr = 0; |
| 3568 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3569 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3570 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3571 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3572 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3573 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3574 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3575 | i = left_nritems - 1; |
| 3576 | while (i >= nr) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3577 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3578 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3579 | if (!empty && push_items > 0) { |
| 3580 | if (path->slots[0] > i) |
| 3581 | break; |
| 3582 | if (path->slots[0] == i) { |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3583 | int space = btrfs_leaf_free_space(left); |
| 3584 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3585 | if (space + push_space * 2 > free_space) |
| 3586 | break; |
| 3587 | } |
| 3588 | } |
| 3589 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3590 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3591 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3592 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3593 | this_item_size = btrfs_item_size(left, item); |
| 3594 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3595 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3596 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3597 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3598 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3599 | if (i == 0) |
| 3600 | break; |
| 3601 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3602 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3603 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3604 | if (push_items == 0) |
| 3605 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3606 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 3607 | WARN_ON(!empty && push_items == left_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3608 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3609 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3610 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3611 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3612 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3613 | push_space -= leaf_data_end(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3614 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3615 | /* make room in the right data area */ |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3616 | data_end = leaf_data_end(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3617 | memmove_extent_buffer(right, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3618 | BTRFS_LEAF_DATA_OFFSET + data_end - push_space, |
| 3619 | BTRFS_LEAF_DATA_OFFSET + data_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3620 | BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3621 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3622 | /* copy from the left data area */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3623 | copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3624 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3625 | BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3626 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3627 | |
| 3628 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3629 | btrfs_item_nr_offset(0), |
| 3630 | right_nritems * sizeof(struct btrfs_item)); |
| 3631 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3632 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3633 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3634 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3635 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3636 | |
| 3637 | /* update the item pointers */ |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3638 | btrfs_init_map_token(&token, right); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3639 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3640 | btrfs_set_header_nritems(right, right_nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3641 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3642 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3643 | item = btrfs_item_nr(i); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3644 | push_space -= btrfs_token_item_size(&token, item); |
| 3645 | btrfs_set_token_item_offset(&token, item, push_space); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3646 | } |
| 3647 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3648 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3649 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3650 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3651 | if (left_nritems) |
| 3652 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3653 | else |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 3654 | btrfs_clean_tree_block(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3655 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3657 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3658 | btrfs_item_key(right, &disk_key, 0); |
| 3659 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3660 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3661 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3662 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3663 | if (path->slots[0] >= left_nritems) { |
| 3664 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3665 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 3666 | btrfs_clean_tree_block(path->nodes[0]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3667 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3668 | free_extent_buffer(path->nodes[0]); |
| 3669 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3670 | path->slots[1] += 1; |
| 3671 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3672 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3673 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3674 | } |
| 3675 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3676 | |
| 3677 | out_unlock: |
| 3678 | btrfs_tree_unlock(right); |
| 3679 | free_extent_buffer(right); |
| 3680 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3681 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3682 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3683 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3684 | * push some data in the path leaf to the right, trying to free up at |
| 3685 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3686 | * |
| 3687 | * returns 1 if the push failed because the other node didn't have enough |
| 3688 | * room, 0 if everything worked out and < 0 if there were major errors. |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3689 | * |
| 3690 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3691 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3692 | */ |
| 3693 | static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3694 | *root, struct btrfs_path *path, |
| 3695 | int min_data_size, int data_size, |
| 3696 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3697 | { |
| 3698 | struct extent_buffer *left = path->nodes[0]; |
| 3699 | struct extent_buffer *right; |
| 3700 | struct extent_buffer *upper; |
| 3701 | int slot; |
| 3702 | int free_space; |
| 3703 | u32 left_nritems; |
| 3704 | int ret; |
| 3705 | |
| 3706 | if (!path->nodes[1]) |
| 3707 | return 1; |
| 3708 | |
| 3709 | slot = path->slots[1]; |
| 3710 | upper = path->nodes[1]; |
| 3711 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3712 | return 1; |
| 3713 | |
| 3714 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3715 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 3716 | right = btrfs_read_node_slot(upper, slot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 3717 | /* |
| 3718 | * slot + 1 is not valid or we fail to read the right node, |
| 3719 | * no big deal, just return. |
| 3720 | */ |
| 3721 | if (IS_ERR(right)) |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3722 | return 1; |
| 3723 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 3724 | __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3725 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3726 | free_space = btrfs_leaf_free_space(right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3727 | if (free_space < data_size) |
| 3728 | goto out_unlock; |
| 3729 | |
| 3730 | /* cow and double check */ |
| 3731 | ret = btrfs_cow_block(trans, root, right, upper, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 3732 | slot + 1, &right, BTRFS_NESTING_RIGHT_COW); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3733 | if (ret) |
| 3734 | goto out_unlock; |
| 3735 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3736 | free_space = btrfs_leaf_free_space(right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3737 | if (free_space < data_size) |
| 3738 | goto out_unlock; |
| 3739 | |
| 3740 | left_nritems = btrfs_header_nritems(left); |
| 3741 | if (left_nritems == 0) |
| 3742 | goto out_unlock; |
| 3743 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3744 | if (check_sibling_keys(left, right)) { |
| 3745 | ret = -EUCLEAN; |
| 3746 | btrfs_tree_unlock(right); |
| 3747 | free_extent_buffer(right); |
| 3748 | return ret; |
| 3749 | } |
Filipe David Borba Manana | 2ef1fed | 2013-12-04 22:17:39 +0000 | [diff] [blame] | 3750 | if (path->slots[0] == left_nritems && !empty) { |
| 3751 | /* Key greater than all keys in the leaf, right neighbor has |
| 3752 | * enough room for it and we're not emptying our leaf to delete |
| 3753 | * it, therefore use right neighbor to insert the new item and |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 3754 | * no need to touch/dirty our left leaf. */ |
Filipe David Borba Manana | 2ef1fed | 2013-12-04 22:17:39 +0000 | [diff] [blame] | 3755 | btrfs_tree_unlock(left); |
| 3756 | free_extent_buffer(left); |
| 3757 | path->nodes[0] = right; |
| 3758 | path->slots[0] = 0; |
| 3759 | path->slots[1]++; |
| 3760 | return 0; |
| 3761 | } |
| 3762 | |
David Sterba | f72f001 | 2019-03-20 14:39:45 +0100 | [diff] [blame] | 3763 | return __push_leaf_right(path, min_data_size, empty, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3764 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3765 | out_unlock: |
| 3766 | btrfs_tree_unlock(right); |
| 3767 | free_extent_buffer(right); |
| 3768 | return 1; |
| 3769 | } |
| 3770 | |
| 3771 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3772 | * push some data in the path leaf to the left, trying to free up at |
| 3773 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3774 | * |
| 3775 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3776 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3777 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3778 | */ |
David Sterba | 8087c19 | 2019-03-20 14:40:41 +0100 | [diff] [blame] | 3779 | static noinline int __push_leaf_left(struct btrfs_path *path, int data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3780 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3781 | int free_space, u32 right_nritems, |
| 3782 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3783 | { |
David Sterba | 8087c19 | 2019-03-20 14:40:41 +0100 | [diff] [blame] | 3784 | struct btrfs_fs_info *fs_info = left->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3785 | struct btrfs_disk_key disk_key; |
| 3786 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3787 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3788 | int push_space = 0; |
| 3789 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3790 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3791 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3792 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3793 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3794 | u32 this_item_size; |
| 3795 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3796 | struct btrfs_map_token token; |
| 3797 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3798 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3799 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3800 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3801 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3802 | |
| 3803 | for (i = 0; i < nr; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3804 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3805 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3806 | if (!empty && push_items > 0) { |
| 3807 | if (path->slots[0] < i) |
| 3808 | break; |
| 3809 | if (path->slots[0] == i) { |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3810 | int space = btrfs_leaf_free_space(right); |
| 3811 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3812 | if (space + push_space * 2 > free_space) |
| 3813 | break; |
| 3814 | } |
| 3815 | } |
| 3816 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3817 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3818 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3819 | |
| 3820 | this_item_size = btrfs_item_size(right, item); |
| 3821 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3822 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3823 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3824 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3825 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3826 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3827 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3828 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3829 | ret = 1; |
| 3830 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3831 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3832 | WARN_ON(!empty && push_items == btrfs_header_nritems(right)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3833 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3834 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3835 | copy_extent_buffer(left, right, |
| 3836 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3837 | btrfs_item_nr_offset(0), |
| 3838 | push_items * sizeof(struct btrfs_item)); |
| 3839 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3840 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3841 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3842 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3843 | copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET + |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3844 | leaf_data_end(left) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3845 | BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3846 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3847 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3848 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3849 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3850 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3851 | btrfs_init_map_token(&token, left); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3852 | old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3853 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3854 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3855 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3856 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3857 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3858 | ioff = btrfs_token_item_offset(&token, item); |
| 3859 | btrfs_set_token_item_offset(&token, item, |
| 3860 | ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3861 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3862 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3863 | |
| 3864 | /* fixup right node */ |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3865 | if (push_items > right_nritems) |
| 3866 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3867 | right_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3868 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3869 | if (push_items < right_nritems) { |
| 3870 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3871 | leaf_data_end(right); |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3872 | memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3873 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3874 | BTRFS_LEAF_DATA_OFFSET + |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3875 | leaf_data_end(right), push_space); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3876 | |
| 3877 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3878 | btrfs_item_nr_offset(push_items), |
| 3879 | (btrfs_header_nritems(right) - push_items) * |
| 3880 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3881 | } |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3882 | |
| 3883 | btrfs_init_map_token(&token, right); |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3884 | right_nritems -= push_items; |
| 3885 | btrfs_set_header_nritems(right, right_nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3886 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3887 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3888 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3889 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3890 | push_space = push_space - btrfs_token_item_size(&token, item); |
| 3891 | btrfs_set_token_item_offset(&token, item, push_space); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3892 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3893 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3894 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3895 | if (right_nritems) |
| 3896 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3897 | else |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 3898 | btrfs_clean_tree_block(right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3899 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3900 | btrfs_item_key(right, &disk_key, 0); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 3901 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3902 | |
| 3903 | /* then fixup the leaf pointer in the path */ |
| 3904 | if (path->slots[0] < push_items) { |
| 3905 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3906 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3907 | free_extent_buffer(path->nodes[0]); |
| 3908 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3909 | path->slots[1] -= 1; |
| 3910 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3911 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3912 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3913 | path->slots[0] -= push_items; |
| 3914 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3915 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3916 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3917 | out: |
| 3918 | btrfs_tree_unlock(left); |
| 3919 | free_extent_buffer(left); |
| 3920 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3921 | } |
| 3922 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3923 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3924 | * push some data in the path leaf to the left, trying to free up at |
| 3925 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3926 | * |
| 3927 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3928 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3929 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3930 | */ |
| 3931 | static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3932 | *root, struct btrfs_path *path, int min_data_size, |
| 3933 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3934 | { |
| 3935 | struct extent_buffer *right = path->nodes[0]; |
| 3936 | struct extent_buffer *left; |
| 3937 | int slot; |
| 3938 | int free_space; |
| 3939 | u32 right_nritems; |
| 3940 | int ret = 0; |
| 3941 | |
| 3942 | slot = path->slots[1]; |
| 3943 | if (slot == 0) |
| 3944 | return 1; |
| 3945 | if (!path->nodes[1]) |
| 3946 | return 1; |
| 3947 | |
| 3948 | right_nritems = btrfs_header_nritems(right); |
| 3949 | if (right_nritems == 0) |
| 3950 | return 1; |
| 3951 | |
| 3952 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3953 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 3954 | left = btrfs_read_node_slot(path->nodes[1], slot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 3955 | /* |
| 3956 | * slot - 1 is not valid or we fail to read the left node, |
| 3957 | * no big deal, just return. |
| 3958 | */ |
| 3959 | if (IS_ERR(left)) |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3960 | return 1; |
| 3961 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 3962 | __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3963 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3964 | free_space = btrfs_leaf_free_space(left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3965 | if (free_space < data_size) { |
| 3966 | ret = 1; |
| 3967 | goto out; |
| 3968 | } |
| 3969 | |
| 3970 | /* cow and double check */ |
| 3971 | ret = btrfs_cow_block(trans, root, left, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 3972 | path->nodes[1], slot - 1, &left, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 3973 | BTRFS_NESTING_LEFT_COW); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3974 | if (ret) { |
| 3975 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 3976 | if (ret == -ENOSPC) |
| 3977 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3978 | goto out; |
| 3979 | } |
| 3980 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3981 | free_space = btrfs_leaf_free_space(left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3982 | if (free_space < data_size) { |
| 3983 | ret = 1; |
| 3984 | goto out; |
| 3985 | } |
| 3986 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3987 | if (check_sibling_keys(left, right)) { |
| 3988 | ret = -EUCLEAN; |
| 3989 | goto out; |
| 3990 | } |
David Sterba | 8087c19 | 2019-03-20 14:40:41 +0100 | [diff] [blame] | 3991 | return __push_leaf_left(path, min_data_size, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3992 | empty, left, free_space, right_nritems, |
| 3993 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3994 | out: |
| 3995 | btrfs_tree_unlock(left); |
| 3996 | free_extent_buffer(left); |
| 3997 | return ret; |
| 3998 | } |
| 3999 | |
| 4000 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4001 | * split the path's leaf in two, making sure there is at least data_size |
| 4002 | * available for the resulting leaf level of the path. |
| 4003 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4004 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4005 | struct btrfs_path *path, |
| 4006 | struct extent_buffer *l, |
| 4007 | struct extent_buffer *right, |
| 4008 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4009 | { |
David Sterba | 94f94ad | 2019-03-20 14:42:33 +0100 | [diff] [blame] | 4010 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4011 | int data_copy_size; |
| 4012 | int rt_data_off; |
| 4013 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4014 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4015 | struct btrfs_map_token token; |
| 4016 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4017 | nritems = nritems - mid; |
| 4018 | btrfs_set_header_nritems(right, nritems); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4019 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4020 | |
| 4021 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 4022 | btrfs_item_nr_offset(mid), |
| 4023 | nritems * sizeof(struct btrfs_item)); |
| 4024 | |
| 4025 | copy_extent_buffer(right, l, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4026 | BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) - |
| 4027 | data_copy_size, BTRFS_LEAF_DATA_OFFSET + |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4028 | leaf_data_end(l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4029 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4030 | rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4031 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4032 | btrfs_init_map_token(&token, right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4033 | for (i = 0; i < nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4034 | struct btrfs_item *item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4035 | u32 ioff; |
| 4036 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4037 | ioff = btrfs_token_item_offset(&token, item); |
| 4038 | btrfs_set_token_item_offset(&token, item, ioff + rt_data_off); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4039 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4040 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4041 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4042 | btrfs_item_key(right, &disk_key, 0); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 4043 | insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4044 | |
| 4045 | btrfs_mark_buffer_dirty(right); |
| 4046 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4047 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4048 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4049 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4050 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4051 | free_extent_buffer(path->nodes[0]); |
| 4052 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4053 | path->slots[0] -= mid; |
| 4054 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4055 | } else { |
| 4056 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4057 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4058 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4059 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4060 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4064 | * double splits happen when we need to insert a big item in the middle |
| 4065 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 4066 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 4067 | * A B C |
| 4068 | * |
| 4069 | * We avoid this by trying to push the items on either side of our target |
| 4070 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 4071 | * completely. |
| 4072 | */ |
| 4073 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 4074 | struct btrfs_root *root, |
| 4075 | struct btrfs_path *path, |
| 4076 | int data_size) |
| 4077 | { |
| 4078 | int ret; |
| 4079 | int progress = 0; |
| 4080 | int slot; |
| 4081 | u32 nritems; |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4082 | int space_needed = data_size; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4083 | |
| 4084 | slot = path->slots[0]; |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4085 | if (slot < btrfs_header_nritems(path->nodes[0])) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4086 | space_needed -= btrfs_leaf_free_space(path->nodes[0]); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4087 | |
| 4088 | /* |
| 4089 | * try to push all the items after our slot into the |
| 4090 | * right leaf |
| 4091 | */ |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4092 | ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4093 | if (ret < 0) |
| 4094 | return ret; |
| 4095 | |
| 4096 | if (ret == 0) |
| 4097 | progress++; |
| 4098 | |
| 4099 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 4100 | /* |
| 4101 | * our goal is to get our slot at the start or end of a leaf. If |
| 4102 | * we've done so we're done |
| 4103 | */ |
| 4104 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 4105 | return 0; |
| 4106 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4107 | if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4108 | return 0; |
| 4109 | |
| 4110 | /* try to push all the items before our slot into the next leaf */ |
| 4111 | slot = path->slots[0]; |
Filipe Manana | 263d399 | 2017-02-17 18:43:57 +0000 | [diff] [blame] | 4112 | space_needed = data_size; |
| 4113 | if (slot > 0) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4114 | space_needed -= btrfs_leaf_free_space(path->nodes[0]); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4115 | ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4116 | if (ret < 0) |
| 4117 | return ret; |
| 4118 | |
| 4119 | if (ret == 0) |
| 4120 | progress++; |
| 4121 | |
| 4122 | if (progress) |
| 4123 | return 0; |
| 4124 | return 1; |
| 4125 | } |
| 4126 | |
| 4127 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4128 | * split the path's leaf in two, making sure there is at least data_size |
| 4129 | * available for the resulting leaf level of the path. |
| 4130 | * |
| 4131 | * returns 0 if all went well and < 0 on failure. |
| 4132 | */ |
| 4133 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 4134 | struct btrfs_root *root, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4135 | const struct btrfs_key *ins_key, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4136 | struct btrfs_path *path, int data_size, |
| 4137 | int extend) |
| 4138 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4139 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4140 | struct extent_buffer *l; |
| 4141 | u32 nritems; |
| 4142 | int mid; |
| 4143 | int slot; |
| 4144 | struct extent_buffer *right; |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 4145 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4146 | int ret = 0; |
| 4147 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4148 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4149 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4150 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4151 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4152 | l = path->nodes[0]; |
| 4153 | slot = path->slots[0]; |
| 4154 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4155 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4156 | return -EOVERFLOW; |
| 4157 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4158 | /* first try to make some room by pushing left and right */ |
Liu Bo | 33157e0 | 2013-05-22 12:07:06 +0000 | [diff] [blame] | 4159 | if (data_size && path->nodes[1]) { |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4160 | int space_needed = data_size; |
| 4161 | |
| 4162 | if (slot < btrfs_header_nritems(l)) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4163 | space_needed -= btrfs_leaf_free_space(l); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4164 | |
| 4165 | wret = push_leaf_right(trans, root, path, space_needed, |
| 4166 | space_needed, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4167 | if (wret < 0) |
| 4168 | return wret; |
| 4169 | if (wret) { |
Filipe Manana | 263d399 | 2017-02-17 18:43:57 +0000 | [diff] [blame] | 4170 | space_needed = data_size; |
| 4171 | if (slot > 0) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4172 | space_needed -= btrfs_leaf_free_space(l); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4173 | wret = push_leaf_left(trans, root, path, space_needed, |
| 4174 | space_needed, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4175 | if (wret < 0) |
| 4176 | return wret; |
| 4177 | } |
| 4178 | l = path->nodes[0]; |
| 4179 | |
| 4180 | /* did the pushes work? */ |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4181 | if (btrfs_leaf_free_space(l) >= data_size) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4182 | return 0; |
| 4183 | } |
| 4184 | |
| 4185 | if (!path->nodes[1]) { |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 4186 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4187 | if (ret) |
| 4188 | return ret; |
| 4189 | } |
| 4190 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4191 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4192 | l = path->nodes[0]; |
| 4193 | slot = path->slots[0]; |
| 4194 | nritems = btrfs_header_nritems(l); |
| 4195 | mid = (nritems + 1) / 2; |
| 4196 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4197 | if (mid <= slot) { |
| 4198 | if (nritems == 1 || |
| 4199 | leaf_space_used(l, mid, nritems - mid) + data_size > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4200 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4201 | if (slot >= nritems) { |
| 4202 | split = 0; |
| 4203 | } else { |
| 4204 | mid = slot; |
| 4205 | if (mid != nritems && |
| 4206 | leaf_space_used(l, mid, nritems - mid) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4207 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4208 | if (data_size && !tried_avoid_double) |
| 4209 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4210 | split = 2; |
| 4211 | } |
| 4212 | } |
| 4213 | } |
| 4214 | } else { |
| 4215 | if (leaf_space_used(l, 0, mid) + data_size > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4216 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4217 | if (!extend && data_size && slot == 0) { |
| 4218 | split = 0; |
| 4219 | } else if ((extend || !data_size) && slot == 0) { |
| 4220 | mid = 1; |
| 4221 | } else { |
| 4222 | mid = slot; |
| 4223 | if (mid != nritems && |
| 4224 | leaf_space_used(l, mid, nritems - mid) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4225 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4226 | if (data_size && !tried_avoid_double) |
| 4227 | goto push_for_double; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4228 | split = 2; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4229 | } |
| 4230 | } |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | if (split == 0) |
| 4235 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 4236 | else |
| 4237 | btrfs_item_key(l, &disk_key, mid); |
| 4238 | |
Josef Bacik | ca9d473 | 2020-08-20 11:46:08 -0400 | [diff] [blame] | 4239 | /* |
| 4240 | * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double |
| 4241 | * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES |
| 4242 | * subclasses, which is 8 at the time of this patch, and we've maxed it |
| 4243 | * out. In the future we could add a |
| 4244 | * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just |
| 4245 | * use BTRFS_NESTING_NEW_ROOT. |
| 4246 | */ |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 4247 | right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0, |
Josef Bacik | ca9d473 | 2020-08-20 11:46:08 -0400 | [diff] [blame] | 4248 | l->start, 0, num_doubles ? |
| 4249 | BTRFS_NESTING_NEW_ROOT : |
| 4250 | BTRFS_NESTING_SPLIT); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4251 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4252 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4253 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4254 | root_add_used(root, fs_info->nodesize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4255 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4256 | if (split == 0) { |
| 4257 | if (mid <= slot) { |
| 4258 | btrfs_set_header_nritems(right, 0); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 4259 | insert_ptr(trans, path, &disk_key, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4260 | right->start, path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4261 | btrfs_tree_unlock(path->nodes[0]); |
| 4262 | free_extent_buffer(path->nodes[0]); |
| 4263 | path->nodes[0] = right; |
| 4264 | path->slots[0] = 0; |
| 4265 | path->slots[1] += 1; |
| 4266 | } else { |
| 4267 | btrfs_set_header_nritems(right, 0); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 4268 | insert_ptr(trans, path, &disk_key, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4269 | right->start, path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4270 | btrfs_tree_unlock(path->nodes[0]); |
| 4271 | free_extent_buffer(path->nodes[0]); |
| 4272 | path->nodes[0] = right; |
| 4273 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4274 | if (path->slots[1] == 0) |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4275 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4276 | } |
Liu Bo | 196e024 | 2016-09-07 14:48:28 -0700 | [diff] [blame] | 4277 | /* |
| 4278 | * We create a new leaf 'right' for the required ins_len and |
| 4279 | * we'll do btrfs_mark_buffer_dirty() on this leaf after copying |
| 4280 | * the content of ins_len to 'right'. |
| 4281 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4282 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4283 | } |
| 4284 | |
David Sterba | 94f94ad | 2019-03-20 14:42:33 +0100 | [diff] [blame] | 4285 | copy_for_split(trans, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4286 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4287 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4288 | BUG_ON(num_doubles != 0); |
| 4289 | num_doubles++; |
| 4290 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4291 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4292 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4293 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4294 | |
| 4295 | push_for_double: |
| 4296 | push_for_double_split(trans, root, path, data_size); |
| 4297 | tried_avoid_double = 1; |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4298 | if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4299 | return 0; |
| 4300 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4301 | } |
| 4302 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4303 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4304 | struct btrfs_root *root, |
| 4305 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4306 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4307 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4308 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4309 | struct btrfs_file_extent_item *fi; |
| 4310 | u64 extent_len = 0; |
| 4311 | u32 item_size; |
| 4312 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4313 | |
| 4314 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4315 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4316 | |
| 4317 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4318 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4319 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4320 | if (btrfs_leaf_free_space(leaf) >= ins_len) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4321 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4322 | |
| 4323 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4324 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4325 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4326 | struct btrfs_file_extent_item); |
| 4327 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4328 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4329 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4330 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4331 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4332 | path->search_for_split = 1; |
| 4333 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4334 | path->search_for_split = 0; |
Filipe Manana | a8df6fe | 2015-01-20 12:40:53 +0000 | [diff] [blame] | 4335 | if (ret > 0) |
| 4336 | ret = -EAGAIN; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4337 | if (ret < 0) |
| 4338 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4339 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4340 | ret = -EAGAIN; |
| 4341 | leaf = path->nodes[0]; |
Filipe Manana | a8df6fe | 2015-01-20 12:40:53 +0000 | [diff] [blame] | 4342 | /* if our item isn't there, return now */ |
| 4343 | if (item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4344 | goto err; |
| 4345 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4346 | /* the leaf has changed, it now has room. return now */ |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4347 | if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len) |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4348 | goto err; |
| 4349 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4350 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4351 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4352 | struct btrfs_file_extent_item); |
| 4353 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4354 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4355 | } |
| 4356 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4357 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4358 | if (ret) |
| 4359 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4360 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4361 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4362 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4363 | return 0; |
| 4364 | err: |
| 4365 | path->keep_locks = 0; |
| 4366 | return ret; |
| 4367 | } |
| 4368 | |
David Sterba | 25263cd | 2019-03-20 14:44:57 +0100 | [diff] [blame] | 4369 | static noinline int split_item(struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4370 | const struct btrfs_key *new_key, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4371 | unsigned long split_offset) |
| 4372 | { |
| 4373 | struct extent_buffer *leaf; |
| 4374 | struct btrfs_item *item; |
| 4375 | struct btrfs_item *new_item; |
| 4376 | int slot; |
| 4377 | char *buf; |
| 4378 | u32 nritems; |
| 4379 | u32 item_size; |
| 4380 | u32 orig_offset; |
| 4381 | struct btrfs_disk_key disk_key; |
| 4382 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4383 | leaf = path->nodes[0]; |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4384 | BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4385 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4386 | item = btrfs_item_nr(path->slots[0]); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4387 | orig_offset = btrfs_item_offset(leaf, item); |
| 4388 | item_size = btrfs_item_size(leaf, item); |
| 4389 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4390 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4391 | if (!buf) |
| 4392 | return -ENOMEM; |
| 4393 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4394 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4395 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4396 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4397 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4398 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4399 | if (slot != nritems) { |
| 4400 | /* shift the items */ |
| 4401 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4402 | btrfs_item_nr_offset(slot), |
| 4403 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4407 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4408 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4409 | new_item = btrfs_item_nr(slot); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4410 | |
| 4411 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4412 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4413 | |
| 4414 | btrfs_set_item_offset(leaf, item, |
| 4415 | orig_offset + item_size - split_offset); |
| 4416 | btrfs_set_item_size(leaf, item, split_offset); |
| 4417 | |
| 4418 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4419 | |
| 4420 | /* write the data for the start of the original item */ |
| 4421 | write_extent_buffer(leaf, buf, |
| 4422 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4423 | split_offset); |
| 4424 | |
| 4425 | /* write the data for the new item */ |
| 4426 | write_extent_buffer(leaf, buf + split_offset, |
| 4427 | btrfs_item_ptr_offset(leaf, slot), |
| 4428 | item_size - split_offset); |
| 4429 | btrfs_mark_buffer_dirty(leaf); |
| 4430 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4431 | BUG_ON(btrfs_leaf_free_space(leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4432 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4433 | return 0; |
| 4434 | } |
| 4435 | |
| 4436 | /* |
| 4437 | * This function splits a single item into two items, |
| 4438 | * giving 'new_key' to the new item and splitting the |
| 4439 | * old one at split_offset (from the start of the item). |
| 4440 | * |
| 4441 | * The path may be released by this operation. After |
| 4442 | * the split, the path is pointing to the old item. The |
| 4443 | * new item is going to be in the same node as the old one. |
| 4444 | * |
| 4445 | * Note, the item being split must be smaller enough to live alone on |
| 4446 | * a tree block with room for one extra struct btrfs_item |
| 4447 | * |
| 4448 | * This allows us to split the item in place, keeping a lock on the |
| 4449 | * leaf the entire time. |
| 4450 | */ |
| 4451 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4452 | struct btrfs_root *root, |
| 4453 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4454 | const struct btrfs_key *new_key, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4455 | unsigned long split_offset) |
| 4456 | { |
| 4457 | int ret; |
| 4458 | ret = setup_leaf_for_split(trans, root, path, |
| 4459 | sizeof(struct btrfs_item)); |
| 4460 | if (ret) |
| 4461 | return ret; |
| 4462 | |
David Sterba | 25263cd | 2019-03-20 14:44:57 +0100 | [diff] [blame] | 4463 | ret = split_item(path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4464 | return ret; |
| 4465 | } |
| 4466 | |
| 4467 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4468 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4469 | * It guarantees both items live in the same tree leaf and the new item |
| 4470 | * is contiguous with the original item. |
| 4471 | * |
| 4472 | * This allows us to split file extent in place, keeping a lock on the |
| 4473 | * leaf the entire time. |
| 4474 | */ |
| 4475 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4476 | struct btrfs_root *root, |
| 4477 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4478 | const struct btrfs_key *new_key) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4479 | { |
| 4480 | struct extent_buffer *leaf; |
| 4481 | int ret; |
| 4482 | u32 item_size; |
| 4483 | |
| 4484 | leaf = path->nodes[0]; |
| 4485 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4486 | ret = setup_leaf_for_split(trans, root, path, |
| 4487 | item_size + sizeof(struct btrfs_item)); |
| 4488 | if (ret) |
| 4489 | return ret; |
| 4490 | |
| 4491 | path->slots[0]++; |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4492 | setup_items_for_insert(root, path, new_key, &item_size, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4493 | leaf = path->nodes[0]; |
| 4494 | memcpy_extent_buffer(leaf, |
| 4495 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4496 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4497 | item_size); |
| 4498 | return 0; |
| 4499 | } |
| 4500 | |
| 4501 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4502 | * make the item pointed to by the path smaller. new_size indicates |
| 4503 | * how small to make it, and from_end tells us if we just chop bytes |
| 4504 | * off the end of the item or if we shift the item to chop bytes off |
| 4505 | * the front. |
| 4506 | */ |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4507 | void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4508 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4509 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4510 | struct extent_buffer *leaf; |
| 4511 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4512 | u32 nritems; |
| 4513 | unsigned int data_end; |
| 4514 | unsigned int old_data_start; |
| 4515 | unsigned int old_size; |
| 4516 | unsigned int size_diff; |
| 4517 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4518 | struct btrfs_map_token token; |
| 4519 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4520 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4521 | slot = path->slots[0]; |
| 4522 | |
| 4523 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4524 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4525 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4526 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4527 | nritems = btrfs_header_nritems(leaf); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4528 | data_end = leaf_data_end(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4529 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4530 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4531 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4532 | size_diff = old_size - new_size; |
| 4533 | |
| 4534 | BUG_ON(slot < 0); |
| 4535 | BUG_ON(slot >= nritems); |
| 4536 | |
| 4537 | /* |
| 4538 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4539 | */ |
| 4540 | /* first correct the data pointers */ |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4541 | btrfs_init_map_token(&token, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4542 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4543 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4544 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4545 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4546 | ioff = btrfs_token_item_offset(&token, item); |
| 4547 | btrfs_set_token_item_offset(&token, item, ioff + size_diff); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4548 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4549 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4550 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4551 | if (from_end) { |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4552 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4553 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4554 | data_end, old_data_start + new_size - data_end); |
| 4555 | } else { |
| 4556 | struct btrfs_disk_key disk_key; |
| 4557 | u64 offset; |
| 4558 | |
| 4559 | btrfs_item_key(leaf, &disk_key, slot); |
| 4560 | |
| 4561 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4562 | unsigned long ptr; |
| 4563 | struct btrfs_file_extent_item *fi; |
| 4564 | |
| 4565 | fi = btrfs_item_ptr(leaf, slot, |
| 4566 | struct btrfs_file_extent_item); |
| 4567 | fi = (struct btrfs_file_extent_item *)( |
| 4568 | (unsigned long)fi - size_diff); |
| 4569 | |
| 4570 | if (btrfs_file_extent_type(leaf, fi) == |
| 4571 | BTRFS_FILE_EXTENT_INLINE) { |
| 4572 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4573 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4574 | (unsigned long)fi, |
David Sterba | 7ec20af | 2014-07-24 17:34:58 +0200 | [diff] [blame] | 4575 | BTRFS_FILE_EXTENT_INLINE_DATA_START); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4576 | } |
| 4577 | } |
| 4578 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4579 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4580 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4581 | data_end, old_data_start - data_end); |
| 4582 | |
| 4583 | offset = btrfs_disk_key_offset(&disk_key); |
| 4584 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4585 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4586 | if (slot == 0) |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4587 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4588 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4589 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4590 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4591 | btrfs_set_item_size(leaf, item, new_size); |
| 4592 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4593 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4594 | if (btrfs_leaf_free_space(leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4595 | btrfs_print_leaf(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4596 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4597 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4598 | } |
| 4599 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4600 | /* |
Stefan Behrens | 8f69dbd | 2013-05-07 10:23:30 +0000 | [diff] [blame] | 4601 | * make the item pointed to by the path bigger, data_size is the added size. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4602 | */ |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 4603 | void btrfs_extend_item(struct btrfs_path *path, u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4604 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4605 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4606 | struct extent_buffer *leaf; |
| 4607 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4608 | u32 nritems; |
| 4609 | unsigned int data_end; |
| 4610 | unsigned int old_data; |
| 4611 | unsigned int old_size; |
| 4612 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4613 | struct btrfs_map_token token; |
| 4614 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4615 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4616 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4617 | nritems = btrfs_header_nritems(leaf); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4618 | data_end = leaf_data_end(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4619 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4620 | if (btrfs_leaf_free_space(leaf) < data_size) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4621 | btrfs_print_leaf(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4622 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4623 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4624 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4625 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4626 | |
| 4627 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4628 | if (slot >= nritems) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4629 | btrfs_print_leaf(leaf); |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 4630 | btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4631 | slot, nritems); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4632 | BUG(); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4633 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4634 | |
| 4635 | /* |
| 4636 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4637 | */ |
| 4638 | /* first correct the data pointers */ |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4639 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4640 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4641 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4642 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4643 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4644 | ioff = btrfs_token_item_offset(&token, item); |
| 4645 | btrfs_set_token_item_offset(&token, item, ioff - data_size); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4646 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4647 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4648 | /* shift the data */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4649 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4650 | data_end - data_size, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4651 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4652 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4653 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4654 | old_size = btrfs_item_size_nr(leaf, slot); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4655 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4656 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4657 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4658 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4659 | if (btrfs_leaf_free_space(leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4660 | btrfs_print_leaf(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4661 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4662 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4663 | } |
| 4664 | |
Nikolay Borisov | da9ffb2 | 2020-09-01 17:40:00 +0300 | [diff] [blame] | 4665 | /** |
| 4666 | * setup_items_for_insert - Helper called before inserting one or more items |
| 4667 | * to a leaf. Main purpose is to save stack depth by doing the bulk of the work |
| 4668 | * in a function that doesn't call btrfs_search_slot |
| 4669 | * |
| 4670 | * @root: root we are inserting items to |
| 4671 | * @path: points to the leaf/slot where we are going to insert new items |
| 4672 | * @cpu_key: array of keys for items to be inserted |
| 4673 | * @data_size: size of the body of each item we are going to insert |
| 4674 | * @nr: size of @cpu_key/@data_size arrays |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4675 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4676 | void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4677 | const struct btrfs_key *cpu_key, u32 *data_size, |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4678 | int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4679 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4680 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4681 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4682 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4683 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4684 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4685 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4686 | struct extent_buffer *leaf; |
| 4687 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4688 | struct btrfs_map_token token; |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4689 | u32 total_size; |
| 4690 | u32 total_data = 0; |
| 4691 | |
| 4692 | for (i = 0; i < nr; i++) |
| 4693 | total_data += data_size[i]; |
| 4694 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4695 | |
Filipe Manana | 24cdc84 | 2014-07-28 19:34:35 +0100 | [diff] [blame] | 4696 | if (path->slots[0] == 0) { |
| 4697 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4698 | fixup_low_keys(path, &disk_key, 1); |
Filipe Manana | 24cdc84 | 2014-07-28 19:34:35 +0100 | [diff] [blame] | 4699 | } |
| 4700 | btrfs_unlock_up_safe(path, 1); |
| 4701 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4702 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4703 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4704 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4705 | nritems = btrfs_header_nritems(leaf); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4706 | data_end = leaf_data_end(leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4707 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4708 | if (btrfs_leaf_free_space(leaf) < total_size) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4709 | btrfs_print_leaf(leaf); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4710 | btrfs_crit(fs_info, "not enough freespace need %u have %d", |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4711 | total_size, btrfs_leaf_free_space(leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4712 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4713 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4714 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4715 | btrfs_init_map_token(&token, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4716 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4717 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4718 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4719 | if (old_data < data_end) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4720 | btrfs_print_leaf(leaf); |
Nikolay Borisov | 7269ddd | 2020-09-01 17:40:01 +0300 | [diff] [blame] | 4721 | btrfs_crit(fs_info, |
| 4722 | "item at slot %d with data offset %u beyond data end of leaf %u", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4723 | slot, old_data, data_end); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4724 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4725 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4726 | /* |
| 4727 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4728 | */ |
| 4729 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4730 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4731 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4732 | |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 4733 | item = btrfs_item_nr(i); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4734 | ioff = btrfs_token_item_offset(&token, item); |
| 4735 | btrfs_set_token_item_offset(&token, item, |
| 4736 | ioff - total_data); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4737 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4738 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4739 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4740 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4741 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4742 | |
| 4743 | /* shift the data */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4744 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4745 | data_end - total_data, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4746 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4747 | data_end = old_data; |
| 4748 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4749 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4750 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4751 | for (i = 0; i < nr; i++) { |
| 4752 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4753 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4754 | item = btrfs_item_nr(slot + i); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4755 | data_end -= data_size[i]; |
Nikolay Borisov | fc0716c | 2020-09-01 17:39:57 +0300 | [diff] [blame] | 4756 | btrfs_set_token_item_offset(&token, item, data_end); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4757 | btrfs_set_token_item_size(&token, item, data_size[i]); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4758 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4759 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4760 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4761 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4762 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4763 | if (btrfs_leaf_free_space(leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4764 | btrfs_print_leaf(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4765 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4766 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * Given a key and some data, insert items into the tree. |
| 4771 | * This does all the path init required, making room in the tree if needed. |
| 4772 | */ |
| 4773 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4774 | struct btrfs_root *root, |
| 4775 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4776 | const struct btrfs_key *cpu_key, u32 *data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4777 | int nr) |
| 4778 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4779 | int ret = 0; |
| 4780 | int slot; |
| 4781 | int i; |
| 4782 | u32 total_size = 0; |
| 4783 | u32 total_data = 0; |
| 4784 | |
| 4785 | for (i = 0; i < nr; i++) |
| 4786 | total_data += data_size[i]; |
| 4787 | |
| 4788 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4789 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4790 | if (ret == 0) |
| 4791 | return -EEXIST; |
| 4792 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4793 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4794 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4795 | slot = path->slots[0]; |
| 4796 | BUG_ON(slot < 0); |
| 4797 | |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4798 | setup_items_for_insert(root, path, cpu_key, data_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4799 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | /* |
| 4803 | * Given a key and some data, insert an item into the tree. |
| 4804 | * This does all the path init required, making room in the tree if needed. |
| 4805 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4806 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4807 | const struct btrfs_key *cpu_key, void *data, |
| 4808 | u32 data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4809 | { |
| 4810 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4811 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4812 | struct extent_buffer *leaf; |
| 4813 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4814 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4815 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4816 | if (!path) |
| 4817 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4818 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4819 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4820 | leaf = path->nodes[0]; |
| 4821 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4822 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4823 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4824 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4825 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4826 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4827 | } |
| 4828 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4829 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4830 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4831 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4832 | * the tree should have been previously balanced so the deletion does not |
| 4833 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4834 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4835 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 4836 | int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4837 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4838 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4839 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4840 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4841 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4842 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4843 | if (slot != nritems - 1) { |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 4844 | if (level) { |
| 4845 | ret = tree_mod_log_insert_move(parent, slot, slot + 1, |
David Sterba | a446a97 | 2018-03-05 15:26:29 +0100 | [diff] [blame] | 4846 | nritems - slot - 1); |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 4847 | BUG_ON(ret < 0); |
| 4848 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4849 | memmove_extent_buffer(parent, |
| 4850 | btrfs_node_key_ptr_offset(slot), |
| 4851 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4852 | sizeof(struct btrfs_key_ptr) * |
| 4853 | (nritems - slot - 1)); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4854 | } else if (level) { |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 4855 | ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE, |
| 4856 | GFP_NOFS); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4857 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4858 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4859 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4860 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4861 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4862 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4863 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4864 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4865 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4866 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4867 | struct btrfs_disk_key disk_key; |
| 4868 | |
| 4869 | btrfs_node_key(parent, &disk_key, 0); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4870 | fixup_low_keys(path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4871 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4872 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4873 | } |
| 4874 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4875 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4876 | * a helper function to delete the leaf pointed to by path->slots[1] and |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4877 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4878 | * |
| 4879 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4880 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4881 | * |
| 4882 | * The path must have already been setup for deleting the leaf, including |
| 4883 | * all the proper balancing. path->nodes[1] must be locked. |
| 4884 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4885 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4886 | struct btrfs_root *root, |
| 4887 | struct btrfs_path *path, |
| 4888 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4889 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4890 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4891 | del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4892 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4893 | /* |
| 4894 | * btrfs_free_extent is expensive, we want to make sure we |
| 4895 | * aren't holding any locks when we call it |
| 4896 | */ |
| 4897 | btrfs_unlock_up_safe(path, 0); |
| 4898 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4899 | root_sub_used(root, leaf->len); |
| 4900 | |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 4901 | atomic_inc(&leaf->refs); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4902 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4903 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4904 | } |
| 4905 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4906 | * delete the item at the leaf level in path. If that empties |
| 4907 | * the leaf, remove it from the tree |
| 4908 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4909 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4910 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4911 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4912 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4913 | struct extent_buffer *leaf; |
| 4914 | struct btrfs_item *item; |
Alexandru Moise | ce0eac2 | 2015-08-23 16:01:42 +0000 | [diff] [blame] | 4915 | u32 last_off; |
| 4916 | u32 dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4917 | int ret = 0; |
| 4918 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4919 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4920 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4921 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4922 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4923 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4924 | |
| 4925 | for (i = 0; i < nr; i++) |
| 4926 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4927 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4928 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4929 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4930 | if (slot + nr != nritems) { |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4931 | int data_end = leaf_data_end(leaf); |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4932 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4933 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4934 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4935 | data_end + dsize, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4936 | BTRFS_LEAF_DATA_OFFSET + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4937 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4938 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4939 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4940 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4941 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4942 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4943 | item = btrfs_item_nr(i); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4944 | ioff = btrfs_token_item_offset(&token, item); |
| 4945 | btrfs_set_token_item_offset(&token, item, ioff + dsize); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4946 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4947 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4948 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4949 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4950 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4951 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4952 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4953 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4954 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4955 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4956 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4957 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4958 | if (leaf == root->node) { |
| 4959 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4960 | } else { |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 4961 | btrfs_clean_tree_block(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4962 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4963 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4964 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4965 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4966 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4967 | struct btrfs_disk_key disk_key; |
| 4968 | |
| 4969 | btrfs_item_key(leaf, &disk_key, 0); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4970 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4971 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4972 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4973 | /* delete the leaf if it is mostly empty */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4974 | if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4975 | /* push_leaf_left fixes the path. |
| 4976 | * make sure the path still points to our leaf |
| 4977 | * for possible call to del_ptr below |
| 4978 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 4979 | slot = path->slots[1]; |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 4980 | atomic_inc(&leaf->refs); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4981 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4982 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 4983 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4984 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4985 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4986 | |
| 4987 | if (path->nodes[0] == leaf && |
| 4988 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4989 | wret = push_leaf_right(trans, root, path, 1, |
| 4990 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 4991 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4992 | ret = wret; |
| 4993 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4994 | |
| 4995 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4996 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4997 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4998 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4999 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 5000 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5001 | /* if we're still in the path, make sure |
| 5002 | * we're dirty. Otherwise, one of the |
| 5003 | * push_leaf functions must have already |
| 5004 | * dirtied this buffer |
| 5005 | */ |
| 5006 | if (path->nodes[0] == leaf) |
| 5007 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5008 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5009 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 5010 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5011 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5012 | } |
| 5013 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5014 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5015 | } |
| 5016 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5017 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5018 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5019 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 5020 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5021 | * |
| 5022 | * This may release the path, and so you may lose any locks held at the |
| 5023 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5024 | */ |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5025 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5026 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5027 | struct btrfs_key key; |
| 5028 | struct btrfs_disk_key found_key; |
| 5029 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5030 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5031 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5032 | |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5033 | if (key.offset > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5034 | key.offset--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5035 | } else if (key.type > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5036 | key.type--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5037 | key.offset = (u64)-1; |
| 5038 | } else if (key.objectid > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5039 | key.objectid--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5040 | key.type = (u8)-1; |
| 5041 | key.offset = (u64)-1; |
| 5042 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5043 | return 1; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5044 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5045 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5046 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5047 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5048 | if (ret < 0) |
| 5049 | return ret; |
| 5050 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 5051 | ret = comp_keys(&found_key, &key); |
Filipe Manana | 337c6f6 | 2014-06-09 13:22:13 +0100 | [diff] [blame] | 5052 | /* |
| 5053 | * We might have had an item with the previous key in the tree right |
| 5054 | * before we released our path. And after we released our path, that |
| 5055 | * item might have been pushed to the first slot (0) of the leaf we |
| 5056 | * were holding due to a tree balance. Alternatively, an item with the |
| 5057 | * previous key can exist as the only element of a leaf (big fat item). |
| 5058 | * Therefore account for these 2 cases, so that our callers (like |
| 5059 | * btrfs_previous_item) don't miss an existing item with a key matching |
| 5060 | * the previous key we computed above. |
| 5061 | */ |
| 5062 | if (ret <= 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5063 | return 0; |
| 5064 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5065 | } |
| 5066 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5067 | /* |
| 5068 | * A helper function to walk down the tree starting at min_key, and looking |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5069 | * for nodes or leaves that are have a minimum transaction id. |
| 5070 | * This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5071 | * |
| 5072 | * This does not cow, but it does stuff the starting key it finds back |
| 5073 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 5074 | * key and get a writable path. |
| 5075 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5076 | * This honors path->lowest_level to prevent descent past a given level |
| 5077 | * of the tree. |
| 5078 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5079 | * min_trans indicates the oldest transaction that you are interested |
| 5080 | * in walking through. Any nodes or leaves older than min_trans are |
| 5081 | * skipped over (without reading them). |
| 5082 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5083 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 5084 | * was nothing in the tree that matched the search criteria. |
| 5085 | */ |
| 5086 | int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5087 | struct btrfs_path *path, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5088 | u64 min_trans) |
| 5089 | { |
| 5090 | struct extent_buffer *cur; |
| 5091 | struct btrfs_key found_key; |
| 5092 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 5093 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5094 | u32 nritems; |
| 5095 | int level; |
| 5096 | int ret = 1; |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5097 | int keep_locks = path->keep_locks; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5098 | |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5099 | path->keep_locks = 1; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5100 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5101 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5102 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5103 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5104 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5105 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5106 | |
| 5107 | if (btrfs_header_generation(cur) < min_trans) { |
| 5108 | ret = 1; |
| 5109 | goto out; |
| 5110 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5111 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5112 | nritems = btrfs_header_nritems(cur); |
| 5113 | level = btrfs_header_level(cur); |
Qu Wenruo | e3b8336 | 2020-04-17 15:08:21 +0800 | [diff] [blame] | 5114 | sret = btrfs_bin_search(cur, min_key, &slot); |
Filipe Manana | cbca7d5 | 2019-02-18 16:57:26 +0000 | [diff] [blame] | 5115 | if (sret < 0) { |
| 5116 | ret = sret; |
| 5117 | goto out; |
| 5118 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5119 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 5120 | /* at the lowest level, we're done, setup the path and exit */ |
| 5121 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5122 | if (slot >= nritems) |
| 5123 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5124 | ret = 0; |
| 5125 | path->slots[level] = slot; |
| 5126 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 5127 | goto out; |
| 5128 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 5129 | if (sret && slot > 0) |
| 5130 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5131 | /* |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5132 | * check this node pointer against the min_trans parameters. |
Randy Dunlap | 260db43 | 2020-08-04 19:48:34 -0700 | [diff] [blame] | 5133 | * If it is too old, skip to the next one. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5134 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5135 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5136 | u64 gen; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5137 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5138 | gen = btrfs_node_ptr_generation(cur, slot); |
| 5139 | if (gen < min_trans) { |
| 5140 | slot++; |
| 5141 | continue; |
| 5142 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5143 | break; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5144 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5145 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5146 | /* |
| 5147 | * we didn't find a candidate key in this node, walk forward |
| 5148 | * and find another one |
| 5149 | */ |
| 5150 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5151 | path->slots[level] = slot; |
| 5152 | sret = btrfs_find_next_key(root, path, min_key, level, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5153 | min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5154 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5155 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5156 | goto again; |
| 5157 | } else { |
| 5158 | goto out; |
| 5159 | } |
| 5160 | } |
| 5161 | /* save our key for returning back */ |
| 5162 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 5163 | path->slots[level] = slot; |
| 5164 | if (level == path->lowest_level) { |
| 5165 | ret = 0; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5166 | goto out; |
| 5167 | } |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 5168 | cur = btrfs_read_node_slot(cur, slot); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5169 | if (IS_ERR(cur)) { |
| 5170 | ret = PTR_ERR(cur); |
| 5171 | goto out; |
| 5172 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5173 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5174 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5175 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5176 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5177 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5178 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5179 | } |
| 5180 | out: |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5181 | path->keep_locks = keep_locks; |
| 5182 | if (ret == 0) { |
| 5183 | btrfs_unlock_up_safe(path, path->lowest_level + 1); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5184 | memcpy(min_key, &found_key, sizeof(found_key)); |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5185 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5186 | return ret; |
| 5187 | } |
| 5188 | |
| 5189 | /* |
| 5190 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5191 | * and fixup the path. It looks for and returns the next key in the |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5192 | * tree based on the current path and the min_trans parameters. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5193 | * |
| 5194 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5195 | * and 1 is returned if there are no higher keys in the tree |
| 5196 | * |
| 5197 | * path->keep_locks should be set to 1 on the search made before |
| 5198 | * calling this function. |
| 5199 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5200 | int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5201 | struct btrfs_key *key, int level, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5202 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5203 | int slot; |
| 5204 | struct extent_buffer *c; |
| 5205 | |
Josef Bacik | 6a9fb46 | 2019-06-20 15:37:52 -0400 | [diff] [blame] | 5206 | WARN_ON(!path->keep_locks && !path->skip_locking); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5207 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5208 | if (!path->nodes[level]) |
| 5209 | return 1; |
| 5210 | |
| 5211 | slot = path->slots[level] + 1; |
| 5212 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5213 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5214 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5215 | int ret; |
| 5216 | int orig_lowest; |
| 5217 | struct btrfs_key cur_key; |
| 5218 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5219 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5220 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5221 | |
Josef Bacik | 6a9fb46 | 2019-06-20 15:37:52 -0400 | [diff] [blame] | 5222 | if (path->locks[level + 1] || path->skip_locking) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5223 | level++; |
| 5224 | continue; |
| 5225 | } |
| 5226 | |
| 5227 | slot = btrfs_header_nritems(c) - 1; |
| 5228 | if (level == 0) |
| 5229 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5230 | else |
| 5231 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5232 | |
| 5233 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5234 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5235 | path->lowest_level = level; |
| 5236 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5237 | 0, 0); |
| 5238 | path->lowest_level = orig_lowest; |
| 5239 | if (ret < 0) |
| 5240 | return ret; |
| 5241 | |
| 5242 | c = path->nodes[level]; |
| 5243 | slot = path->slots[level]; |
| 5244 | if (ret == 0) |
| 5245 | slot++; |
| 5246 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5247 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5248 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5249 | if (level == 0) |
| 5250 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5251 | else { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5252 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5253 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5254 | if (gen < min_trans) { |
| 5255 | slot++; |
| 5256 | goto next; |
| 5257 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5258 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5259 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5260 | return 0; |
| 5261 | } |
| 5262 | return 1; |
| 5263 | } |
| 5264 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5265 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5266 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5267 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5268 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5269 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5270 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5271 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5272 | return btrfs_next_old_leaf(root, path, 0); |
| 5273 | } |
| 5274 | |
| 5275 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5276 | u64 time_seq) |
| 5277 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5278 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5279 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5280 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5281 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5282 | struct btrfs_key key; |
| 5283 | u32 nritems; |
| 5284 | int ret; |
Josef Bacik | 0e46318 | 2020-11-06 16:27:30 -0500 | [diff] [blame] | 5285 | int i; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5286 | |
| 5287 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5288 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5289 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5290 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5291 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5292 | again: |
| 5293 | level = 1; |
| 5294 | next = NULL; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5295 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5296 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5297 | path->keep_locks = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5298 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5299 | if (time_seq) |
| 5300 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5301 | else |
| 5302 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5303 | path->keep_locks = 0; |
| 5304 | |
| 5305 | if (ret < 0) |
| 5306 | return ret; |
| 5307 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5308 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5309 | /* |
| 5310 | * by releasing the path above we dropped all our locks. A balance |
| 5311 | * could have added more items next to the key that used to be |
| 5312 | * at the very end of the block. So, check again here and |
| 5313 | * advance the path if there are now more items available. |
| 5314 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5315 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5316 | if (ret == 0) |
| 5317 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5318 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5319 | goto done; |
| 5320 | } |
Liu Bo | 0b43e04 | 2014-06-09 11:04:49 +0800 | [diff] [blame] | 5321 | /* |
| 5322 | * So the above check misses one case: |
| 5323 | * - after releasing the path above, someone has removed the item that |
| 5324 | * used to be at the very end of the block, and balance between leafs |
| 5325 | * gets another one with bigger key.offset to replace it. |
| 5326 | * |
| 5327 | * This one should be returned as well, or we can get leaf corruption |
| 5328 | * later(esp. in __btrfs_drop_extents()). |
| 5329 | * |
| 5330 | * And a bit more explanation about this check, |
| 5331 | * with ret > 0, the key isn't found, the path points to the slot |
| 5332 | * where it should be inserted, so the path->slots[0] item must be the |
| 5333 | * bigger one. |
| 5334 | */ |
| 5335 | if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { |
| 5336 | ret = 0; |
| 5337 | goto done; |
| 5338 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5339 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5340 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5341 | if (!path->nodes[level]) { |
| 5342 | ret = 1; |
| 5343 | goto done; |
| 5344 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5345 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5346 | slot = path->slots[level] + 1; |
| 5347 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5348 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5349 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5350 | if (level == BTRFS_MAX_LEVEL) { |
| 5351 | ret = 1; |
| 5352 | goto done; |
| 5353 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5354 | continue; |
| 5355 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5356 | |
Josef Bacik | 0e46318 | 2020-11-06 16:27:30 -0500 | [diff] [blame] | 5357 | |
| 5358 | /* |
| 5359 | * Our current level is where we're going to start from, and to |
| 5360 | * make sure lockdep doesn't complain we need to drop our locks |
| 5361 | * and nodes from 0 to our current level. |
| 5362 | */ |
| 5363 | for (i = 0; i < level; i++) { |
| 5364 | if (path->locks[level]) { |
| 5365 | btrfs_tree_read_unlock(path->nodes[i]); |
| 5366 | path->locks[i] = 0; |
| 5367 | } |
| 5368 | free_extent_buffer(path->nodes[i]); |
| 5369 | path->nodes[i] = NULL; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5370 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5371 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5372 | next = c; |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 5373 | ret = read_block_for_search(root, path, &next, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 5374 | slot, &key); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5375 | if (ret == -EAGAIN) |
| 5376 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5377 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5378 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5379 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5380 | goto done; |
| 5381 | } |
| 5382 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5383 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5384 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5385 | if (!ret && time_seq) { |
| 5386 | /* |
| 5387 | * If we don't get the lock, we may be racing |
| 5388 | * with push_leaf_left, holding that lock while |
| 5389 | * itself waiting for the leaf we've currently |
| 5390 | * locked. To solve this situation, we give up |
| 5391 | * on our lock and cycle. |
| 5392 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5393 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5394 | btrfs_release_path(path); |
| 5395 | cond_resched(); |
| 5396 | goto again; |
| 5397 | } |
Josef Bacik | 0e46318 | 2020-11-06 16:27:30 -0500 | [diff] [blame] | 5398 | if (!ret) |
| 5399 | btrfs_tree_read_lock(next); |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5400 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5401 | break; |
| 5402 | } |
| 5403 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5404 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5405 | level--; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5406 | path->nodes[level] = next; |
| 5407 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5408 | if (!path->skip_locking) |
Josef Bacik | ffeb03c | 2020-11-06 16:27:29 -0500 | [diff] [blame] | 5409 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5410 | if (!level) |
| 5411 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5412 | |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 5413 | ret = read_block_for_search(root, path, &next, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 5414 | 0, &key); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5415 | if (ret == -EAGAIN) |
| 5416 | goto again; |
| 5417 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5418 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5419 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5420 | goto done; |
| 5421 | } |
| 5422 | |
Josef Bacik | ffeb03c | 2020-11-06 16:27:29 -0500 | [diff] [blame] | 5423 | if (!path->skip_locking) |
Josef Bacik | 0e46318 | 2020-11-06 16:27:30 -0500 | [diff] [blame] | 5424 | btrfs_tree_read_lock(next); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5425 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5426 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5427 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5428 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5429 | |
| 5430 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5431 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5432 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5433 | /* |
| 5434 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5435 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5436 | * |
| 5437 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5438 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5439 | int btrfs_previous_item(struct btrfs_root *root, |
| 5440 | struct btrfs_path *path, u64 min_objectid, |
| 5441 | int type) |
| 5442 | { |
| 5443 | struct btrfs_key found_key; |
| 5444 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5445 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5446 | int ret; |
| 5447 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5448 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5449 | if (path->slots[0] == 0) { |
| 5450 | ret = btrfs_prev_leaf(root, path); |
| 5451 | if (ret != 0) |
| 5452 | return ret; |
| 5453 | } else { |
| 5454 | path->slots[0]--; |
| 5455 | } |
| 5456 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5457 | nritems = btrfs_header_nritems(leaf); |
| 5458 | if (nritems == 0) |
| 5459 | return 1; |
| 5460 | if (path->slots[0] == nritems) |
| 5461 | path->slots[0]--; |
| 5462 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5463 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5464 | if (found_key.objectid < min_objectid) |
| 5465 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5466 | if (found_key.type == type) |
| 5467 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5468 | if (found_key.objectid == min_objectid && |
| 5469 | found_key.type < type) |
| 5470 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5471 | } |
| 5472 | return 1; |
| 5473 | } |
Wang Shilong | ade2e0b | 2014-01-12 21:38:33 +0800 | [diff] [blame] | 5474 | |
| 5475 | /* |
| 5476 | * search in extent tree to find a previous Metadata/Data extent item with |
| 5477 | * min objecitd. |
| 5478 | * |
| 5479 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5480 | */ |
| 5481 | int btrfs_previous_extent_item(struct btrfs_root *root, |
| 5482 | struct btrfs_path *path, u64 min_objectid) |
| 5483 | { |
| 5484 | struct btrfs_key found_key; |
| 5485 | struct extent_buffer *leaf; |
| 5486 | u32 nritems; |
| 5487 | int ret; |
| 5488 | |
| 5489 | while (1) { |
| 5490 | if (path->slots[0] == 0) { |
Wang Shilong | ade2e0b | 2014-01-12 21:38:33 +0800 | [diff] [blame] | 5491 | ret = btrfs_prev_leaf(root, path); |
| 5492 | if (ret != 0) |
| 5493 | return ret; |
| 5494 | } else { |
| 5495 | path->slots[0]--; |
| 5496 | } |
| 5497 | leaf = path->nodes[0]; |
| 5498 | nritems = btrfs_header_nritems(leaf); |
| 5499 | if (nritems == 0) |
| 5500 | return 1; |
| 5501 | if (path->slots[0] == nritems) |
| 5502 | path->slots[0]--; |
| 5503 | |
| 5504 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5505 | if (found_key.objectid < min_objectid) |
| 5506 | break; |
| 5507 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY || |
| 5508 | found_key.type == BTRFS_METADATA_ITEM_KEY) |
| 5509 | return 0; |
| 5510 | if (found_key.objectid == min_objectid && |
| 5511 | found_key.type < BTRFS_EXTENT_ITEM_KEY) |
| 5512 | break; |
| 5513 | } |
| 5514 | return 1; |
| 5515 | } |