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 | 4d3edf7 | 2021-01-14 14:02:46 -0500 | [diff] [blame^] | 224 | if (ret) { |
| 225 | btrfs_abort_transaction(trans, ret); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 226 | return ret; |
Josef Bacik | 4d3edf7 | 2021-01-14 14:02:46 -0500 | [diff] [blame^] | 227 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 228 | |
| 229 | btrfs_mark_buffer_dirty(cow); |
| 230 | *cow_ret = cow; |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 234 | enum mod_log_op { |
| 235 | MOD_LOG_KEY_REPLACE, |
| 236 | MOD_LOG_KEY_ADD, |
| 237 | MOD_LOG_KEY_REMOVE, |
| 238 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, |
| 239 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, |
| 240 | MOD_LOG_MOVE_KEYS, |
| 241 | MOD_LOG_ROOT_REPLACE, |
| 242 | }; |
| 243 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 244 | struct tree_mod_root { |
| 245 | u64 logical; |
| 246 | u8 level; |
| 247 | }; |
| 248 | |
| 249 | struct tree_mod_elem { |
| 250 | struct rb_node node; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 251 | u64 logical; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 252 | u64 seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 253 | enum mod_log_op op; |
| 254 | |
| 255 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ |
| 256 | int slot; |
| 257 | |
| 258 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ |
| 259 | u64 generation; |
| 260 | |
| 261 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ |
| 262 | struct btrfs_disk_key key; |
| 263 | u64 blockptr; |
| 264 | |
| 265 | /* this is used for op == MOD_LOG_MOVE_KEYS */ |
David Sterba | b6dfa35 | 2018-03-05 15:31:18 +0100 | [diff] [blame] | 266 | struct { |
| 267 | int dst_slot; |
| 268 | int nr_items; |
| 269 | } move; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 270 | |
| 271 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ |
| 272 | struct tree_mod_root old_root; |
| 273 | }; |
| 274 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 275 | /* |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 276 | * Pull a new tree mod seq number for our operation. |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 277 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 278 | 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] | 279 | { |
| 280 | return atomic64_inc_return(&fs_info->tree_mod_seq); |
| 281 | } |
| 282 | |
| 283 | /* |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 284 | * This adds a new blocker to the tree mod log's blocker list if the @elem |
| 285 | * passed does not already have a sequence number set. So when a caller expects |
| 286 | * to record tree modifications, it should ensure to set elem->seq to zero |
| 287 | * before calling btrfs_get_tree_mod_seq. |
| 288 | * Returns a fresh, unused tree log modification sequence number, even if no new |
| 289 | * blocker was added. |
| 290 | */ |
| 291 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 292 | struct seq_list *elem) |
| 293 | { |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 294 | write_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 295 | if (!elem->seq) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 296 | elem->seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 297 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 298 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 299 | write_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 300 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 301 | return elem->seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 305 | struct seq_list *elem) |
| 306 | { |
| 307 | struct rb_root *tm_root; |
| 308 | struct rb_node *node; |
| 309 | struct rb_node *next; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 310 | struct tree_mod_elem *tm; |
| 311 | u64 min_seq = (u64)-1; |
| 312 | u64 seq_putting = elem->seq; |
| 313 | |
| 314 | if (!seq_putting) |
| 315 | return; |
| 316 | |
Filipe Manana | 7227ff4 | 2020-01-22 12:23:20 +0000 | [diff] [blame] | 317 | write_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 318 | list_del(&elem->list); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 319 | elem->seq = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 320 | |
Filipe Manana | 42836cf | 2020-01-22 12:23:54 +0000 | [diff] [blame] | 321 | if (!list_empty(&fs_info->tree_mod_seq_list)) { |
| 322 | struct seq_list *first; |
| 323 | |
| 324 | first = list_first_entry(&fs_info->tree_mod_seq_list, |
| 325 | struct seq_list, list); |
| 326 | if (seq_putting > first->seq) { |
| 327 | /* |
| 328 | * Blocker with lower sequence number exists, we |
| 329 | * cannot remove anything from the log. |
| 330 | */ |
| 331 | write_unlock(&fs_info->tree_mod_log_lock); |
| 332 | return; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 333 | } |
Filipe Manana | 42836cf | 2020-01-22 12:23:54 +0000 | [diff] [blame] | 334 | min_seq = first->seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 335 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 336 | |
| 337 | /* |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 338 | * anything that's lower than the lowest existing (read: blocked) |
| 339 | * sequence number can be removed from the tree. |
| 340 | */ |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 341 | tm_root = &fs_info->tree_mod_log; |
| 342 | for (node = rb_first(tm_root); node; node = next) { |
| 343 | next = rb_next(node); |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 344 | tm = rb_entry(node, struct tree_mod_elem, node); |
Filipe Manana | 6609fee | 2019-12-06 12:27:39 +0000 | [diff] [blame] | 345 | if (tm->seq >= min_seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 346 | continue; |
| 347 | rb_erase(node, tm_root); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 348 | kfree(tm); |
| 349 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 350 | write_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * key order of the log: |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 355 | * node/leaf start address -> sequence |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 356 | * |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 357 | * The 'start address' is the logical address of the *new* root node |
| 358 | * for root replace operations, or the logical address of the affected |
| 359 | * block for all other operations. |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 360 | */ |
| 361 | static noinline int |
| 362 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 363 | { |
| 364 | struct rb_root *tm_root; |
| 365 | struct rb_node **new; |
| 366 | struct rb_node *parent = NULL; |
| 367 | struct tree_mod_elem *cur; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 368 | |
David Sterba | 73e82fe | 2019-03-27 16:19:55 +0100 | [diff] [blame] | 369 | lockdep_assert_held_write(&fs_info->tree_mod_log_lock); |
| 370 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 371 | tm->seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 372 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 373 | tm_root = &fs_info->tree_mod_log; |
| 374 | new = &tm_root->rb_node; |
| 375 | while (*new) { |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 376 | cur = rb_entry(*new, struct tree_mod_elem, node); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 377 | parent = *new; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 378 | if (cur->logical < tm->logical) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 379 | new = &((*new)->rb_left); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 380 | else if (cur->logical > tm->logical) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 381 | new = &((*new)->rb_right); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 382 | else if (cur->seq < tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 383 | new = &((*new)->rb_left); |
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_right); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 386 | else |
| 387 | return -EEXIST; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | rb_link_node(&tm->node, parent, new); |
| 391 | rb_insert_color(&tm->node, tm_root); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 392 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 393 | } |
| 394 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 395 | /* |
| 396 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it |
| 397 | * returns zero with the tree_mod_log_lock acquired. The caller must hold |
| 398 | * 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] | 399 | * write unlock fs_info::tree_mod_log_lock. |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 400 | */ |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 401 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 402 | struct extent_buffer *eb) { |
| 403 | smp_mb(); |
| 404 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 405 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 406 | if (eb && btrfs_header_level(eb) == 0) |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 407 | return 1; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 408 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 409 | write_lock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 410 | if (list_empty(&(fs_info)->tree_mod_seq_list)) { |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 411 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 412 | return 1; |
| 413 | } |
| 414 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 415 | return 0; |
| 416 | } |
| 417 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 418 | /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */ |
| 419 | static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info, |
| 420 | struct extent_buffer *eb) |
| 421 | { |
| 422 | smp_mb(); |
| 423 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 424 | return 0; |
| 425 | if (eb && btrfs_header_level(eb) == 0) |
| 426 | return 0; |
| 427 | |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | static struct tree_mod_elem * |
| 432 | alloc_tree_mod_elem(struct extent_buffer *eb, int slot, |
| 433 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 434 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 435 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 436 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 437 | tm = kzalloc(sizeof(*tm), flags); |
| 438 | if (!tm) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 439 | return NULL; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 440 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 441 | tm->logical = eb->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 442 | if (op != MOD_LOG_KEY_ADD) { |
| 443 | btrfs_node_key(eb, &tm->key, slot); |
| 444 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 445 | } |
| 446 | tm->op = op; |
| 447 | tm->slot = slot; |
| 448 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 449 | RB_CLEAR_NODE(&tm->node); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 450 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 451 | return tm; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 452 | } |
| 453 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 454 | static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot, |
| 455 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 456 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 457 | struct tree_mod_elem *tm; |
| 458 | int ret; |
| 459 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 460 | if (!tree_mod_need_log(eb->fs_info, eb)) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 463 | tm = alloc_tree_mod_elem(eb, slot, op, flags); |
| 464 | if (!tm) |
| 465 | return -ENOMEM; |
| 466 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 467 | if (tree_mod_dont_log(eb->fs_info, eb)) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 468 | kfree(tm); |
| 469 | return 0; |
| 470 | } |
| 471 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 472 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 473 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 474 | if (ret) |
| 475 | kfree(tm); |
| 476 | |
| 477 | return ret; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 478 | } |
| 479 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 480 | static noinline int tree_mod_log_insert_move(struct extent_buffer *eb, |
| 481 | int dst_slot, int src_slot, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 482 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 483 | struct tree_mod_elem *tm = NULL; |
| 484 | struct tree_mod_elem **tm_list = NULL; |
| 485 | int ret = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 486 | int i; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 487 | int locked = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 488 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 489 | if (!tree_mod_need_log(eb->fs_info, eb)) |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 490 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 491 | |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 492 | 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] | 493 | if (!tm_list) |
| 494 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 495 | |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 496 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 497 | if (!tm) { |
| 498 | ret = -ENOMEM; |
| 499 | goto free_tms; |
| 500 | } |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 501 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 502 | tm->logical = eb->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 503 | tm->slot = src_slot; |
| 504 | tm->move.dst_slot = dst_slot; |
| 505 | tm->move.nr_items = nr_items; |
| 506 | tm->op = MOD_LOG_MOVE_KEYS; |
| 507 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 508 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
| 509 | tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot, |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 510 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 511 | if (!tm_list[i]) { |
| 512 | ret = -ENOMEM; |
| 513 | goto free_tms; |
| 514 | } |
| 515 | } |
| 516 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 517 | if (tree_mod_dont_log(eb->fs_info, eb)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 518 | goto free_tms; |
| 519 | locked = 1; |
| 520 | |
| 521 | /* |
| 522 | * When we override something during the move, we log these removals. |
| 523 | * This can only happen when we move towards the beginning of the |
| 524 | * buffer, i.e. dst_slot < src_slot. |
| 525 | */ |
| 526 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 527 | 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] | 528 | if (ret) |
| 529 | goto free_tms; |
| 530 | } |
| 531 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 532 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 533 | if (ret) |
| 534 | goto free_tms; |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 535 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 536 | kfree(tm_list); |
| 537 | |
| 538 | return 0; |
| 539 | free_tms: |
| 540 | for (i = 0; i < nr_items; i++) { |
| 541 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 542 | 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] | 543 | kfree(tm_list[i]); |
| 544 | } |
| 545 | if (locked) |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 546 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 547 | kfree(tm_list); |
| 548 | kfree(tm); |
| 549 | |
| 550 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 551 | } |
| 552 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 553 | static inline int |
| 554 | __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, |
| 555 | struct tree_mod_elem **tm_list, |
| 556 | int nritems) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 557 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 558 | int i, j; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 559 | int ret; |
| 560 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 561 | for (i = nritems - 1; i >= 0; i--) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 562 | ret = __tree_mod_log_insert(fs_info, tm_list[i]); |
| 563 | if (ret) { |
| 564 | for (j = nritems - 1; j > i; j--) |
| 565 | rb_erase(&tm_list[j]->node, |
| 566 | &fs_info->tree_mod_log); |
| 567 | return ret; |
| 568 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 569 | } |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 570 | |
| 571 | return 0; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 572 | } |
| 573 | |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 574 | static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root, |
| 575 | struct extent_buffer *new_root, int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 576 | { |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 577 | struct btrfs_fs_info *fs_info = old_root->fs_info; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 578 | struct tree_mod_elem *tm = NULL; |
| 579 | struct tree_mod_elem **tm_list = NULL; |
| 580 | int nritems = 0; |
| 581 | int ret = 0; |
| 582 | int i; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 583 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 584 | if (!tree_mod_need_log(fs_info, NULL)) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 585 | return 0; |
| 586 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 587 | if (log_removal && btrfs_header_level(old_root) > 0) { |
| 588 | nritems = btrfs_header_nritems(old_root); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 589 | tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 590 | GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 591 | if (!tm_list) { |
| 592 | ret = -ENOMEM; |
| 593 | goto free_tms; |
| 594 | } |
| 595 | for (i = 0; i < nritems; i++) { |
| 596 | tm_list[i] = alloc_tree_mod_elem(old_root, i, |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 597 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 598 | if (!tm_list[i]) { |
| 599 | ret = -ENOMEM; |
| 600 | goto free_tms; |
| 601 | } |
| 602 | } |
| 603 | } |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 604 | |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 605 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 606 | if (!tm) { |
| 607 | ret = -ENOMEM; |
| 608 | goto free_tms; |
| 609 | } |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 610 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 611 | tm->logical = new_root->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 612 | tm->old_root.logical = old_root->start; |
| 613 | tm->old_root.level = btrfs_header_level(old_root); |
| 614 | tm->generation = btrfs_header_generation(old_root); |
| 615 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 616 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 617 | if (tree_mod_dont_log(fs_info, NULL)) |
| 618 | goto free_tms; |
| 619 | |
| 620 | if (tm_list) |
| 621 | ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems); |
| 622 | if (!ret) |
| 623 | ret = __tree_mod_log_insert(fs_info, tm); |
| 624 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 625 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 626 | if (ret) |
| 627 | goto free_tms; |
| 628 | kfree(tm_list); |
| 629 | |
| 630 | return ret; |
| 631 | |
| 632 | free_tms: |
| 633 | if (tm_list) { |
| 634 | for (i = 0; i < nritems; i++) |
| 635 | kfree(tm_list[i]); |
| 636 | kfree(tm_list); |
| 637 | } |
| 638 | kfree(tm); |
| 639 | |
| 640 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | static struct tree_mod_elem * |
| 644 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 645 | int smallest) |
| 646 | { |
| 647 | struct rb_root *tm_root; |
| 648 | struct rb_node *node; |
| 649 | struct tree_mod_elem *cur = NULL; |
| 650 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 651 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 652 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 653 | tm_root = &fs_info->tree_mod_log; |
| 654 | node = tm_root->rb_node; |
| 655 | while (node) { |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 656 | cur = rb_entry(node, struct tree_mod_elem, node); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 657 | if (cur->logical < start) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 658 | node = node->rb_left; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 659 | } else if (cur->logical > start) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 660 | node = node->rb_right; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 661 | } else if (cur->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 662 | node = node->rb_left; |
| 663 | } else if (!smallest) { |
| 664 | /* we want the node with the highest seq */ |
| 665 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 666 | BUG_ON(found->seq > cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 667 | found = cur; |
| 668 | node = node->rb_left; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 669 | } else if (cur->seq > min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 670 | /* we want the node with the smallest seq */ |
| 671 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 672 | BUG_ON(found->seq < cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 673 | found = cur; |
| 674 | node = node->rb_right; |
| 675 | } else { |
| 676 | found = cur; |
| 677 | break; |
| 678 | } |
| 679 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 680 | read_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 681 | |
| 682 | return found; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * this returns the element from the log with the smallest time sequence |
| 687 | * value that's in the log (the oldest log item). any element with a time |
| 688 | * sequence lower than min_seq will be ignored. |
| 689 | */ |
| 690 | static struct tree_mod_elem * |
| 691 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 692 | u64 min_seq) |
| 693 | { |
| 694 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * this returns the element from the log with the largest time sequence |
| 699 | * value that's in the log (the most recent log item). any element with |
| 700 | * a time sequence lower than min_seq will be ignored. |
| 701 | */ |
| 702 | static struct tree_mod_elem * |
| 703 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 704 | { |
| 705 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 706 | } |
| 707 | |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 708 | static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst, |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 709 | struct extent_buffer *src, unsigned long dst_offset, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 710 | unsigned long src_offset, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 711 | { |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 712 | struct btrfs_fs_info *fs_info = dst->fs_info; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 713 | int ret = 0; |
| 714 | struct tree_mod_elem **tm_list = NULL; |
| 715 | struct tree_mod_elem **tm_list_add, **tm_list_rem; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 716 | int i; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 717 | int locked = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 718 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 719 | if (!tree_mod_need_log(fs_info, NULL)) |
| 720 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 721 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 722 | 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] | 723 | return 0; |
| 724 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 725 | 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] | 726 | GFP_NOFS); |
| 727 | if (!tm_list) |
| 728 | return -ENOMEM; |
| 729 | |
| 730 | tm_list_add = tm_list; |
| 731 | tm_list_rem = tm_list + nr_items; |
| 732 | for (i = 0; i < nr_items; i++) { |
| 733 | tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset, |
| 734 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
| 735 | if (!tm_list_rem[i]) { |
| 736 | ret = -ENOMEM; |
| 737 | goto free_tms; |
| 738 | } |
| 739 | |
| 740 | tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset, |
| 741 | MOD_LOG_KEY_ADD, GFP_NOFS); |
| 742 | if (!tm_list_add[i]) { |
| 743 | ret = -ENOMEM; |
| 744 | goto free_tms; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | if (tree_mod_dont_log(fs_info, NULL)) |
| 749 | goto free_tms; |
| 750 | locked = 1; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 751 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 752 | for (i = 0; i < nr_items; i++) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 753 | ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]); |
| 754 | if (ret) |
| 755 | goto free_tms; |
| 756 | ret = __tree_mod_log_insert(fs_info, tm_list_add[i]); |
| 757 | if (ret) |
| 758 | goto free_tms; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 759 | } |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 760 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 761 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 762 | kfree(tm_list); |
| 763 | |
| 764 | return 0; |
| 765 | |
| 766 | free_tms: |
| 767 | for (i = 0; i < nr_items * 2; i++) { |
| 768 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) |
| 769 | rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log); |
| 770 | kfree(tm_list[i]); |
| 771 | } |
| 772 | if (locked) |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 773 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 774 | kfree(tm_list); |
| 775 | |
| 776 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 777 | } |
| 778 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 779 | static noinline int tree_mod_log_free_eb(struct extent_buffer *eb) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 780 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 781 | struct tree_mod_elem **tm_list = NULL; |
| 782 | int nritems = 0; |
| 783 | int i; |
| 784 | int ret = 0; |
| 785 | |
| 786 | if (btrfs_header_level(eb) == 0) |
| 787 | return 0; |
| 788 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 789 | if (!tree_mod_need_log(eb->fs_info, NULL)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 790 | return 0; |
| 791 | |
| 792 | nritems = btrfs_header_nritems(eb); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 793 | 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] | 794 | if (!tm_list) |
| 795 | return -ENOMEM; |
| 796 | |
| 797 | for (i = 0; i < nritems; i++) { |
| 798 | tm_list[i] = alloc_tree_mod_elem(eb, i, |
| 799 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
| 800 | if (!tm_list[i]) { |
| 801 | ret = -ENOMEM; |
| 802 | goto free_tms; |
| 803 | } |
| 804 | } |
| 805 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 806 | if (tree_mod_dont_log(eb->fs_info, eb)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 807 | goto free_tms; |
| 808 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 809 | ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 810 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 811 | if (ret) |
| 812 | goto free_tms; |
| 813 | kfree(tm_list); |
| 814 | |
| 815 | return 0; |
| 816 | |
| 817 | free_tms: |
| 818 | for (i = 0; i < nritems; i++) |
| 819 | kfree(tm_list[i]); |
| 820 | kfree(tm_list); |
| 821 | |
| 822 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 823 | } |
| 824 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 825 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 826 | * check if the tree block can be shared by multiple trees |
| 827 | */ |
| 828 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 829 | struct extent_buffer *buf) |
| 830 | { |
| 831 | /* |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 832 | * Tree blocks not in shareable trees and tree roots are never shared. |
| 833 | * If a block was allocated after the last snapshot and the block was |
| 834 | * not allocated by tree relocation, we know the block is not shared. |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 835 | */ |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 836 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 837 | buf != root->node && buf != root->commit_root && |
| 838 | (btrfs_header_generation(buf) <= |
| 839 | btrfs_root_last_snapshot(&root->root_item) || |
| 840 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 841 | return 1; |
Nikolay Borisov | a79865c | 2018-06-21 09:45:00 +0300 | [diff] [blame] | 842 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 847 | struct btrfs_root *root, |
| 848 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 849 | struct extent_buffer *cow, |
| 850 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 851 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 852 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 853 | u64 refs; |
| 854 | u64 owner; |
| 855 | u64 flags; |
| 856 | u64 new_flags = 0; |
| 857 | int ret; |
| 858 | |
| 859 | /* |
| 860 | * Backrefs update rules: |
| 861 | * |
| 862 | * Always use full backrefs for extent pointers in tree block |
| 863 | * allocated by tree relocation. |
| 864 | * |
| 865 | * If a shared tree block is no longer referenced by its owner |
| 866 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 867 | * use full backrefs for extent pointers in tree block. |
| 868 | * |
| 869 | * If a tree block is been relocating |
| 870 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 871 | * use full backrefs for extent pointers in tree block. |
| 872 | * The reason for this is some operations (such as drop tree) |
| 873 | * are only allowed for blocks use full backrefs. |
| 874 | */ |
| 875 | |
| 876 | if (btrfs_block_can_be_shared(root, buf)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 877 | ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 878 | btrfs_header_level(buf), 1, |
| 879 | &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 880 | if (ret) |
| 881 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 882 | if (refs == 0) { |
| 883 | ret = -EROFS; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 884 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 885 | return ret; |
| 886 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 887 | } else { |
| 888 | refs = 1; |
| 889 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 890 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 891 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 892 | else |
| 893 | flags = 0; |
| 894 | } |
| 895 | |
| 896 | owner = btrfs_header_owner(buf); |
| 897 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 898 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 899 | |
| 900 | if (refs > 1) { |
| 901 | if ((owner == root->root_key.objectid || |
| 902 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 903 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 904 | ret = btrfs_inc_ref(trans, root, buf, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 905 | if (ret) |
| 906 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 907 | |
| 908 | if (root->root_key.objectid == |
| 909 | BTRFS_TREE_RELOC_OBJECTID) { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 910 | ret = btrfs_dec_ref(trans, root, buf, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 911 | if (ret) |
| 912 | return ret; |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 913 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 914 | if (ret) |
| 915 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 916 | } |
| 917 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 918 | } else { |
| 919 | |
| 920 | if (root->root_key.objectid == |
| 921 | BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 922 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 923 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 924 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 925 | if (ret) |
| 926 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 927 | } |
| 928 | if (new_flags != 0) { |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 929 | int level = btrfs_header_level(buf); |
| 930 | |
David Sterba | 42c9d0b | 2019-03-20 11:54:13 +0100 | [diff] [blame] | 931 | ret = btrfs_set_disk_extent_flags(trans, buf, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 932 | new_flags, level, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 933 | if (ret) |
| 934 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 935 | } |
| 936 | } else { |
| 937 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 938 | if (root->root_key.objectid == |
| 939 | BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 940 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 941 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 942 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 943 | if (ret) |
| 944 | return ret; |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 945 | ret = btrfs_dec_ref(trans, root, buf, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 946 | if (ret) |
| 947 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 948 | } |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 949 | btrfs_clean_tree_block(buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 950 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 951 | } |
| 952 | return 0; |
| 953 | } |
| 954 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 955 | static struct extent_buffer *alloc_tree_block_no_bg_flush( |
| 956 | struct btrfs_trans_handle *trans, |
| 957 | struct btrfs_root *root, |
| 958 | u64 parent_start, |
| 959 | const struct btrfs_disk_key *disk_key, |
| 960 | int level, |
| 961 | u64 hint, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 962 | u64 empty_size, |
| 963 | enum btrfs_lock_nesting nest) |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 964 | { |
| 965 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 966 | struct extent_buffer *ret; |
| 967 | |
| 968 | /* |
| 969 | * If we are COWing a node/leaf from the extent, chunk, device or free |
| 970 | * space trees, make sure that we do not finish block group creation of |
| 971 | * pending block groups. We do this to avoid a deadlock. |
| 972 | * COWing can result in allocation of a new chunk, and flushing pending |
| 973 | * block groups (btrfs_create_pending_block_groups()) can be triggered |
| 974 | * when finishing allocation of a new chunk. Creation of a pending block |
| 975 | * group modifies the extent, chunk, device and free space trees, |
| 976 | * therefore we could deadlock with ourselves since we are holding a |
| 977 | * lock on an extent buffer that btrfs_create_pending_block_groups() may |
| 978 | * try to COW later. |
| 979 | * For similar reasons, we also need to delay flushing pending block |
| 980 | * groups when splitting a leaf or node, from one of those trees, since |
| 981 | * we are holding a write lock on it and its parent or when inserting a |
| 982 | * new root node for one of those trees. |
| 983 | */ |
| 984 | if (root == fs_info->extent_root || |
| 985 | root == fs_info->chunk_root || |
| 986 | root == fs_info->dev_root || |
| 987 | root == fs_info->free_space_root) |
| 988 | trans->can_flush_pending_bgs = false; |
| 989 | |
| 990 | ret = btrfs_alloc_tree_block(trans, root, parent_start, |
| 991 | root->root_key.objectid, disk_key, level, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 992 | hint, empty_size, nest); |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 993 | trans->can_flush_pending_bgs = true; |
| 994 | |
| 995 | return ret; |
| 996 | } |
| 997 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 998 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 999 | * does the dirty work in cow of a single block. The parent block (if |
| 1000 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 1001 | * dirty and returned locked. If you modify the block it needs to be marked |
| 1002 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1003 | * |
| 1004 | * search_start -- an allocation hint for the new block |
| 1005 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1006 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 1007 | * bytes the allocator should try to find free next to the block it returns. |
| 1008 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1009 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1010 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1011 | struct btrfs_root *root, |
| 1012 | struct extent_buffer *buf, |
| 1013 | struct extent_buffer *parent, int parent_slot, |
| 1014 | struct extent_buffer **cow_ret, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1015 | u64 search_start, u64 empty_size, |
| 1016 | enum btrfs_lock_nesting nest) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1017 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1018 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1019 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1020 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1021 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1022 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1023 | int unlock_orig = 0; |
Goldwyn Rodrigues | 0f5053e | 2016-09-22 14:11:34 -0500 | [diff] [blame] | 1024 | u64 parent_start = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1025 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1026 | if (*cow_ret == buf) |
| 1027 | unlock_orig = 1; |
| 1028 | |
Chris Mason | b9447ef8 | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 1029 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1030 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 1031 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1032 | trans->transid != fs_info->running_transaction->transid); |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 1033 | WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1034 | trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1035 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1036 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1037 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1038 | if (level == 0) |
| 1039 | btrfs_item_key(buf, &disk_key, 0); |
| 1040 | else |
| 1041 | btrfs_node_key(buf, &disk_key, 0); |
| 1042 | |
Goldwyn Rodrigues | 0f5053e | 2016-09-22 14:11:34 -0500 | [diff] [blame] | 1043 | if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) |
| 1044 | parent_start = parent->start; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1045 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 1046 | 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] | 1047 | level, search_start, empty_size, nest); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1048 | if (IS_ERR(cow)) |
| 1049 | return PTR_ERR(cow); |
| 1050 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1051 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 1052 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 1053 | copy_extent_buffer_full(cow, buf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1054 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1055 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1056 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 1057 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 1058 | BTRFS_HEADER_FLAG_RELOC); |
| 1059 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1060 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 1061 | else |
| 1062 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1063 | |
Nikolay Borisov | de37aa5 | 2018-10-30 16:43:24 +0200 | [diff] [blame] | 1064 | write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1065 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1066 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1067 | if (ret) { |
Josef Bacik | 572c83a | 2020-09-29 08:53:54 -0400 | [diff] [blame] | 1068 | btrfs_tree_unlock(cow); |
| 1069 | free_extent_buffer(cow); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1070 | btrfs_abort_transaction(trans, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1071 | return ret; |
| 1072 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1073 | |
Qu Wenruo | 92a7cc4 | 2020-05-15 14:01:40 +0800 | [diff] [blame] | 1074 | if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) { |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1075 | ret = btrfs_reloc_cow_block(trans, root, buf, cow); |
Zhaolei | 93314e3 | 2015-08-06 21:56:58 +0800 | [diff] [blame] | 1076 | if (ret) { |
Josef Bacik | 572c83a | 2020-09-29 08:53:54 -0400 | [diff] [blame] | 1077 | btrfs_tree_unlock(cow); |
| 1078 | free_extent_buffer(cow); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1079 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1080 | return ret; |
Zhaolei | 93314e3 | 2015-08-06 21:56:58 +0800 | [diff] [blame] | 1081 | } |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1082 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1083 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1084 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1085 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1086 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1087 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1088 | parent_start = buf->start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1089 | |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 1090 | atomic_inc(&cow->refs); |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 1091 | ret = tree_mod_log_insert_root(root->node, cow, 1); |
| 1092 | BUG_ON(ret < 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1093 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1094 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1095 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1096 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1097 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1098 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1099 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1100 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 1101 | tree_mod_log_insert_key(parent, parent_slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 1102 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1103 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1104 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1105 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1106 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1107 | btrfs_mark_buffer_dirty(parent); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1108 | if (last_ref) { |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 1109 | ret = tree_mod_log_free_eb(buf); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1110 | if (ret) { |
Josef Bacik | 572c83a | 2020-09-29 08:53:54 -0400 | [diff] [blame] | 1111 | btrfs_tree_unlock(cow); |
| 1112 | free_extent_buffer(cow); |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1113 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1114 | return ret; |
| 1115 | } |
| 1116 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1117 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1118 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1119 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1120 | if (unlock_orig) |
| 1121 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1122 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1123 | btrfs_mark_buffer_dirty(cow); |
| 1124 | *cow_ret = cow; |
| 1125 | return 0; |
| 1126 | } |
| 1127 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1128 | /* |
| 1129 | * returns the logical address of the oldest predecessor of the given root. |
| 1130 | * entries older than time_seq are ignored. |
| 1131 | */ |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1132 | static struct tree_mod_elem *__tree_mod_log_oldest_root( |
| 1133 | struct extent_buffer *eb_root, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1134 | { |
| 1135 | struct tree_mod_elem *tm; |
| 1136 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1137 | u64 root_logical = eb_root->start; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1138 | int looped = 0; |
| 1139 | |
| 1140 | if (!time_seq) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1141 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1142 | |
| 1143 | /* |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 1144 | * the very last operation that's logged for a root is the |
| 1145 | * replacement operation (if it is replaced at all). this has |
| 1146 | * the logical address of the *new* root, making it the very |
| 1147 | * first operation that's logged for this root. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1148 | */ |
| 1149 | while (1) { |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1150 | tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1151 | time_seq); |
| 1152 | if (!looped && !tm) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1153 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1154 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1155 | * if there are no tree operation for the oldest root, we simply |
| 1156 | * return it. this should only happen if that (old) root is at |
| 1157 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1158 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1159 | if (!tm) |
| 1160 | break; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1161 | |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1162 | /* |
| 1163 | * if there's an operation that's not a root replacement, we |
| 1164 | * found the oldest version of our root. normally, we'll find a |
| 1165 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1166 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1167 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1168 | break; |
| 1169 | |
| 1170 | found = tm; |
| 1171 | root_logical = tm->old_root.logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1172 | looped = 1; |
| 1173 | } |
| 1174 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1175 | /* if there's no old root to return, return what we found instead */ |
| 1176 | if (!found) |
| 1177 | found = tm; |
| 1178 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1179 | return found; |
| 1180 | } |
| 1181 | |
| 1182 | /* |
| 1183 | * 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] | 1184 | * previous operations will be rewound (until we reach something older than |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1185 | * time_seq). |
| 1186 | */ |
| 1187 | static void |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1188 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1189 | u64 time_seq, struct tree_mod_elem *first_tm) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1190 | { |
| 1191 | u32 n; |
| 1192 | struct rb_node *next; |
| 1193 | struct tree_mod_elem *tm = first_tm; |
| 1194 | unsigned long o_dst; |
| 1195 | unsigned long o_src; |
| 1196 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1197 | |
| 1198 | n = btrfs_header_nritems(eb); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 1199 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1200 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1201 | /* |
| 1202 | * all the operations are recorded with the operator used for |
| 1203 | * the modification. as we're going backwards, we do the |
| 1204 | * opposite of each operation here. |
| 1205 | */ |
| 1206 | switch (tm->op) { |
| 1207 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1208 | BUG_ON(tm->slot < n); |
Marcos Paulo de Souza | c730ae0 | 2020-06-16 15:54:29 -0300 | [diff] [blame] | 1209 | fallthrough; |
Liu Bo | 95c80bb | 2012-10-19 09:50:52 +0000 | [diff] [blame] | 1210 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1211 | case MOD_LOG_KEY_REMOVE: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1212 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1213 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1214 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1215 | tm->generation); |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1216 | n++; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1217 | break; |
| 1218 | case MOD_LOG_KEY_REPLACE: |
| 1219 | BUG_ON(tm->slot >= n); |
| 1220 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1221 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1222 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1223 | tm->generation); |
| 1224 | break; |
| 1225 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1226 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1227 | n--; |
| 1228 | break; |
| 1229 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1230 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1231 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1232 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1233 | tm->move.nr_items * p_size); |
| 1234 | break; |
| 1235 | case MOD_LOG_ROOT_REPLACE: |
| 1236 | /* |
| 1237 | * this operation is special. for roots, this must be |
| 1238 | * handled explicitly before rewinding. |
| 1239 | * for non-roots, this operation may exist if the node |
| 1240 | * was a root: root A -> child B; then A gets empty and |
| 1241 | * B is promoted to the new root. in the mod log, we'll |
| 1242 | * have a root-replace operation for B, a tree block |
| 1243 | * that is no root. we simply ignore that operation. |
| 1244 | */ |
| 1245 | break; |
| 1246 | } |
| 1247 | next = rb_next(&tm->node); |
| 1248 | if (!next) |
| 1249 | break; |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 1250 | tm = rb_entry(next, struct tree_mod_elem, node); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 1251 | if (tm->logical != first_tm->logical) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1252 | break; |
| 1253 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 1254 | read_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1255 | btrfs_set_header_nritems(eb, n); |
| 1256 | } |
| 1257 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1258 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1259 | * 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] | 1260 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
| 1261 | * returned buffer is always read-locked. If the returned buffer is not the |
| 1262 | * input buffer, the lock on the input buffer is released and the input buffer |
| 1263 | * is freed (its refcount is decremented). |
| 1264 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1265 | static struct extent_buffer * |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1266 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 1267 | struct extent_buffer *eb, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1268 | { |
| 1269 | struct extent_buffer *eb_rewin; |
| 1270 | struct tree_mod_elem *tm; |
| 1271 | |
| 1272 | if (!time_seq) |
| 1273 | return eb; |
| 1274 | |
| 1275 | if (btrfs_header_level(eb) == 0) |
| 1276 | return eb; |
| 1277 | |
| 1278 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1279 | if (!tm) |
| 1280 | return eb; |
| 1281 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1282 | btrfs_set_path_blocking(path); |
David Sterba | 300aa89 | 2018-04-04 02:00:17 +0200 | [diff] [blame] | 1283 | btrfs_set_lock_blocking_read(eb); |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1284 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1285 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1286 | BUG_ON(tm->slot != 0); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1287 | eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1288 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1289 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1290 | free_extent_buffer(eb); |
| 1291 | return NULL; |
| 1292 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1293 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1294 | btrfs_set_header_backref_rev(eb_rewin, |
| 1295 | btrfs_header_backref_rev(eb)); |
| 1296 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1297 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1298 | } else { |
| 1299 | eb_rewin = btrfs_clone_extent_buffer(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1300 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1301 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1302 | free_extent_buffer(eb); |
| 1303 | return NULL; |
| 1304 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1305 | } |
| 1306 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1307 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1308 | free_extent_buffer(eb); |
| 1309 | |
Josef Bacik | d3beaa2 | 2020-08-10 11:42:31 -0400 | [diff] [blame] | 1310 | btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin), |
| 1311 | eb_rewin, btrfs_header_level(eb_rewin)); |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1312 | btrfs_tree_read_lock(eb_rewin); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1313 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1314 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1315 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1316 | |
| 1317 | return eb_rewin; |
| 1318 | } |
| 1319 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1320 | /* |
| 1321 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1322 | * value. If there are no changes, the current root->root_node is returned. If |
| 1323 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1324 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1325 | * Returns NULL on error (with no locks held). |
| 1326 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1327 | static inline struct extent_buffer * |
| 1328 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1329 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1330 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1331 | struct tree_mod_elem *tm; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1332 | struct extent_buffer *eb = NULL; |
| 1333 | struct extent_buffer *eb_root; |
Filipe Manana | efad8a8 | 2019-08-12 19:14:29 +0100 | [diff] [blame] | 1334 | u64 eb_root_owner = 0; |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1335 | struct extent_buffer *old; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1336 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1337 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1338 | u64 logical; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1339 | int level; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1340 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1341 | eb_root = btrfs_read_lock_root_node(root); |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1342 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1343 | if (!tm) |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1344 | return eb_root; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1345 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1346 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1347 | old_root = &tm->old_root; |
| 1348 | old_generation = tm->generation; |
| 1349 | logical = old_root->logical; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1350 | level = old_root->level; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1351 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1352 | logical = eb_root->start; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1353 | level = btrfs_header_level(eb_root); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1354 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1355 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1356 | tm = tree_mod_log_search(fs_info, logical, time_seq); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1357 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1358 | btrfs_tree_read_unlock(eb_root); |
| 1359 | free_extent_buffer(eb_root); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1360 | old = read_tree_block(fs_info, logical, 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 { |
David Sterba | 300aa89 | 2018-04-04 02:00:17 +0200 | [diff] [blame] | 1377 | btrfs_set_lock_blocking_read(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1378 | eb = btrfs_clone_extent_buffer(eb_root); |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1379 | btrfs_tree_read_unlock_blocking(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1380 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1381 | } |
| 1382 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1383 | if (!eb) |
| 1384 | return NULL; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1385 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1386 | btrfs_set_header_bytenr(eb, eb->start); |
| 1387 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
Filipe Manana | efad8a8 | 2019-08-12 19:14:29 +0100 | [diff] [blame] | 1388 | btrfs_set_header_owner(eb, eb_root_owner); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1389 | btrfs_set_header_level(eb, old_root->level); |
| 1390 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1391 | } |
Josef Bacik | d3beaa2 | 2020-08-10 11:42:31 -0400 | [diff] [blame] | 1392 | btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb, |
| 1393 | btrfs_header_level(eb)); |
| 1394 | btrfs_tree_read_lock(eb); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1395 | if (tm) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1396 | __tree_mod_log_rewind(fs_info, eb, time_seq, tm); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1397 | else |
| 1398 | WARN_ON(btrfs_header_level(eb) != 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1399 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1400 | |
| 1401 | return eb; |
| 1402 | } |
| 1403 | |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1404 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
| 1405 | { |
| 1406 | struct tree_mod_elem *tm; |
| 1407 | int level; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1408 | struct extent_buffer *eb_root = btrfs_root_node(root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1409 | |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1410 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1411 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1412 | level = tm->old_root.level; |
| 1413 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1414 | level = btrfs_header_level(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1415 | } |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1416 | free_extent_buffer(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1417 | |
| 1418 | return level; |
| 1419 | } |
| 1420 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1421 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1422 | struct btrfs_root *root, |
| 1423 | struct extent_buffer *buf) |
| 1424 | { |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1425 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 1426 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 1427 | |
David Sterba | d198013 | 2018-03-16 02:39:40 +0100 | [diff] [blame] | 1428 | /* Ensure we can see the FORCE_COW bit */ |
| 1429 | smp_mb__before_atomic(); |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1430 | |
| 1431 | /* |
| 1432 | * We do not need to cow a block if |
| 1433 | * 1) this block is not created or changed in this transaction; |
| 1434 | * 2) this block does not belong to TREE_RELOC tree; |
| 1435 | * 3) the root is not forced COW. |
| 1436 | * |
| 1437 | * What is forced COW: |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1438 | * when we create snapshot during committing the transaction, |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 1439 | * after we've finished copying src root, we must COW the shared |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1440 | * block to ensure the metadata consistency. |
| 1441 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1442 | if (btrfs_header_generation(buf) == trans->transid && |
| 1443 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1444 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1445 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1446 | !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1447 | return 0; |
| 1448 | return 1; |
| 1449 | } |
| 1450 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1451 | /* |
| 1452 | * 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] | 1453 | * 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] | 1454 | * once per transaction, as long as it hasn't been written yet |
| 1455 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1456 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1457 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1458 | struct extent_buffer *parent, int parent_slot, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1459 | struct extent_buffer **cow_ret, |
| 1460 | enum btrfs_lock_nesting nest) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1461 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1462 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1463 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1464 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1465 | |
Josef Bacik | 83354f0 | 2018-11-30 11:52:13 -0500 | [diff] [blame] | 1466 | if (test_bit(BTRFS_ROOT_DELETING, &root->state)) |
| 1467 | btrfs_err(fs_info, |
| 1468 | "COW'ing blocks on a fs root that's being dropped"); |
| 1469 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1470 | if (trans->transaction != fs_info->running_transaction) |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1471 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1472 | trans->transid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1473 | fs_info->running_transaction->transid); |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1474 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1475 | if (trans->transid != fs_info->generation) |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1476 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1477 | trans->transid, fs_info->generation); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1478 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1479 | if (!should_cow_block(trans, root, buf)) { |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 1480 | trans->dirty = true; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1481 | *cow_ret = buf; |
| 1482 | return 0; |
| 1483 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1484 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1485 | search_start = buf->start & ~((u64)SZ_1G - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1486 | |
| 1487 | if (parent) |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 1488 | btrfs_set_lock_blocking_write(parent); |
| 1489 | btrfs_set_lock_blocking_write(buf); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1490 | |
Qu Wenruo | f616f5c | 2019-01-23 15:15:17 +0800 | [diff] [blame] | 1491 | /* |
| 1492 | * Before CoWing this block for later modification, check if it's |
| 1493 | * the subtree root and do the delayed subtree trace if needed. |
| 1494 | * |
| 1495 | * Also We don't care about the error, as it's handled internally. |
| 1496 | */ |
| 1497 | btrfs_qgroup_trace_subtree_after_cow(trans, root, buf); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1498 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1499 | parent_slot, cow_ret, search_start, 0, nest); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1500 | |
| 1501 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1502 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1503 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1506 | /* |
| 1507 | * helper function for defrag to decide if two blocks pointed to by a |
| 1508 | * node are actually close by |
| 1509 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1510 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1511 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1512 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1513 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1514 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1515 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1516 | return 0; |
| 1517 | } |
| 1518 | |
David Sterba | ce6ef5a | 2020-06-08 16:06:07 +0200 | [diff] [blame] | 1519 | #ifdef __LITTLE_ENDIAN |
| 1520 | |
| 1521 | /* |
| 1522 | * Compare two keys, on little-endian the disk order is same as CPU order and |
| 1523 | * we can avoid the conversion. |
| 1524 | */ |
| 1525 | static int comp_keys(const struct btrfs_disk_key *disk_key, |
| 1526 | const struct btrfs_key *k2) |
| 1527 | { |
| 1528 | const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key; |
| 1529 | |
| 1530 | return btrfs_comp_cpu_keys(k1, k2); |
| 1531 | } |
| 1532 | |
| 1533 | #else |
| 1534 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1535 | /* |
| 1536 | * compare two keys in a memcmp fashion |
| 1537 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1538 | static int comp_keys(const struct btrfs_disk_key *disk, |
| 1539 | const struct btrfs_key *k2) |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1540 | { |
| 1541 | struct btrfs_key k1; |
| 1542 | |
| 1543 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1544 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1545 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1546 | } |
David Sterba | ce6ef5a | 2020-06-08 16:06:07 +0200 | [diff] [blame] | 1547 | #endif |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1548 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1549 | /* |
| 1550 | * same as comp_keys only with two btrfs_key's |
| 1551 | */ |
David Sterba | e1f60a6 | 2019-10-01 19:57:39 +0200 | [diff] [blame] | 1552 | 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] | 1553 | { |
| 1554 | if (k1->objectid > k2->objectid) |
| 1555 | return 1; |
| 1556 | if (k1->objectid < k2->objectid) |
| 1557 | return -1; |
| 1558 | if (k1->type > k2->type) |
| 1559 | return 1; |
| 1560 | if (k1->type < k2->type) |
| 1561 | return -1; |
| 1562 | if (k1->offset > k2->offset) |
| 1563 | return 1; |
| 1564 | if (k1->offset < k2->offset) |
| 1565 | return -1; |
| 1566 | return 0; |
| 1567 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1568 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1569 | /* |
| 1570 | * this is used by the defrag code to go through all the |
| 1571 | * leaves pointed to by a node and reallocate them so that |
| 1572 | * disk order is close to key order |
| 1573 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1574 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1575 | struct btrfs_root *root, struct extent_buffer *parent, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1576 | int start_slot, u64 *last_ret, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1577 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1578 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1579 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1580 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1581 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1582 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1583 | u64 search_start = *last_ret; |
| 1584 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1585 | u64 other; |
| 1586 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1587 | int end_slot; |
| 1588 | int i; |
| 1589 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1590 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1591 | int uptodate; |
| 1592 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1593 | int progress_passed = 0; |
| 1594 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1595 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1596 | parent_level = btrfs_header_level(parent); |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1597 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1598 | WARN_ON(trans->transaction != fs_info->running_transaction); |
| 1599 | WARN_ON(trans->transid != fs_info->generation); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1600 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1601 | parent_nritems = btrfs_header_nritems(parent); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1602 | blocksize = fs_info->nodesize; |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1603 | end_slot = parent_nritems - 1; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1604 | |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1605 | if (parent_nritems <= 1) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1606 | return 0; |
| 1607 | |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 1608 | btrfs_set_lock_blocking_write(parent); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1609 | |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1610 | for (i = start_slot; i <= end_slot; i++) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1611 | struct btrfs_key first_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1612 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1613 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1614 | btrfs_node_key(parent, &disk_key, i); |
| 1615 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1616 | continue; |
| 1617 | |
| 1618 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1619 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1620 | gen = btrfs_node_ptr_generation(parent, i); |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1621 | btrfs_node_key_to_cpu(parent, &first_key, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1622 | if (last_block == 0) |
| 1623 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1624 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1625 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1626 | other = btrfs_node_blockptr(parent, i - 1); |
| 1627 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1628 | } |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1629 | if (!close && i < end_slot) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1630 | other = btrfs_node_blockptr(parent, i + 1); |
| 1631 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1632 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1633 | if (close) { |
| 1634 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1635 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1636 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1637 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1638 | cur = find_extent_buffer(fs_info, blocknr); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1639 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1640 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1641 | else |
| 1642 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1643 | if (!cur || !uptodate) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1644 | if (!cur) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1645 | cur = read_tree_block(fs_info, blocknr, gen, |
| 1646 | parent_level - 1, |
| 1647 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 1648 | if (IS_ERR(cur)) { |
| 1649 | return PTR_ERR(cur); |
| 1650 | } else if (!extent_buffer_uptodate(cur)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1651 | free_extent_buffer(cur); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1652 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1653 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1654 | } else if (!uptodate) { |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1655 | err = btrfs_read_buffer(cur, gen, |
| 1656 | parent_level - 1,&first_key); |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 1657 | if (err) { |
| 1658 | free_extent_buffer(cur); |
| 1659 | return err; |
| 1660 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1661 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1662 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1663 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1664 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1665 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1666 | btrfs_tree_lock(cur); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 1667 | btrfs_set_lock_blocking_write(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1668 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1669 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1670 | min(16 * blocksize, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1671 | (end_slot - i) * blocksize), |
| 1672 | BTRFS_NESTING_COW); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1673 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1674 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1675 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1676 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1677 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1678 | search_start = cur->start; |
| 1679 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1680 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1681 | btrfs_tree_unlock(cur); |
| 1682 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1683 | } |
| 1684 | return err; |
| 1685 | } |
| 1686 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1687 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1688 | * search for key in the extent_buffer. The items start at offset p, |
| 1689 | * and they are item_size apart. There are 'max' items in p. |
| 1690 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1691 | * the slot in the array is returned via slot, and it points to |
| 1692 | * the place where you would insert key if it is not found in |
| 1693 | * the array. |
| 1694 | * |
| 1695 | * slot may point to max if the key is bigger than all of the keys |
| 1696 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1697 | static noinline int generic_bin_search(struct extent_buffer *eb, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1698 | unsigned long p, int item_size, |
| 1699 | const struct btrfs_key *key, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1700 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1701 | { |
| 1702 | int low = 0; |
| 1703 | int high = max; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1704 | int ret; |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1705 | const int key_size = sizeof(struct btrfs_disk_key); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1706 | |
Liu Bo | 5e24e9a | 2016-06-23 16:32:45 -0700 | [diff] [blame] | 1707 | if (low > high) { |
| 1708 | btrfs_err(eb->fs_info, |
| 1709 | "%s: low (%d) > high (%d) eb %llu owner %llu level %d", |
| 1710 | __func__, low, high, eb->start, |
| 1711 | btrfs_header_owner(eb), btrfs_header_level(eb)); |
| 1712 | return -EINVAL; |
| 1713 | } |
| 1714 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1715 | while (low < high) { |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1716 | unsigned long oip; |
| 1717 | unsigned long offset; |
| 1718 | struct btrfs_disk_key *tmp; |
| 1719 | struct btrfs_disk_key unaligned; |
| 1720 | int mid; |
| 1721 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1722 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1723 | offset = p + mid * item_size; |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1724 | oip = offset_in_page(offset); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1725 | |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1726 | if (oip + key_size <= PAGE_SIZE) { |
| 1727 | const unsigned long idx = offset >> PAGE_SHIFT; |
| 1728 | char *kaddr = page_address(eb->pages[idx]); |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1729 | |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1730 | tmp = (struct btrfs_disk_key *)(kaddr + oip); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1731 | } else { |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1732 | read_extent_buffer(eb, &unaligned, offset, key_size); |
| 1733 | tmp = &unaligned; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1734 | } |
David Sterba | 5cd17f3 | 2020-04-29 23:23:37 +0200 | [diff] [blame] | 1735 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1736 | ret = comp_keys(tmp, key); |
| 1737 | |
| 1738 | if (ret < 0) |
| 1739 | low = mid + 1; |
| 1740 | else if (ret > 0) |
| 1741 | high = mid; |
| 1742 | else { |
| 1743 | *slot = mid; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | } |
| 1747 | *slot = low; |
| 1748 | return 1; |
| 1749 | } |
| 1750 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 1751 | /* |
| 1752 | * simple bin_search frontend that does the right thing for |
| 1753 | * leaves vs nodes |
| 1754 | */ |
Nikolay Borisov | a74b35e | 2017-12-08 16:27:43 +0200 | [diff] [blame] | 1755 | 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] | 1756 | int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1757 | { |
Qu Wenruo | e3b8336 | 2020-04-17 15:08:21 +0800 | [diff] [blame] | 1758 | if (btrfs_header_level(eb) == 0) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1759 | return generic_bin_search(eb, |
| 1760 | offsetof(struct btrfs_leaf, items), |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 1761 | sizeof(struct btrfs_item), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1762 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1763 | slot); |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1764 | else |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1765 | return generic_bin_search(eb, |
| 1766 | offsetof(struct btrfs_node, ptrs), |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1767 | sizeof(struct btrfs_key_ptr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1768 | key, btrfs_header_nritems(eb), |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 1769 | slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1770 | } |
| 1771 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1772 | static void root_add_used(struct btrfs_root *root, u32 size) |
| 1773 | { |
| 1774 | spin_lock(&root->accounting_lock); |
| 1775 | btrfs_set_root_used(&root->root_item, |
| 1776 | btrfs_root_used(&root->root_item) + size); |
| 1777 | spin_unlock(&root->accounting_lock); |
| 1778 | } |
| 1779 | |
| 1780 | static void root_sub_used(struct btrfs_root *root, u32 size) |
| 1781 | { |
| 1782 | spin_lock(&root->accounting_lock); |
| 1783 | btrfs_set_root_used(&root->root_item, |
| 1784 | btrfs_root_used(&root->root_item) - size); |
| 1785 | spin_unlock(&root->accounting_lock); |
| 1786 | } |
| 1787 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1788 | /* given a node and slot number, this reads the blocks it points to. The |
| 1789 | * extent buffer is returned with a reference taken (but unlocked). |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1790 | */ |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1791 | struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent, |
| 1792 | int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1793 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1794 | int level = btrfs_header_level(parent); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1795 | struct extent_buffer *eb; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1796 | struct btrfs_key first_key; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1797 | |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1798 | if (slot < 0 || slot >= btrfs_header_nritems(parent)) |
| 1799 | return ERR_PTR(-ENOENT); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1800 | |
| 1801 | BUG_ON(level == 0); |
| 1802 | |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1803 | btrfs_node_key_to_cpu(parent, &first_key, slot); |
David Sterba | d0d20b0 | 2019-03-20 14:54:01 +0100 | [diff] [blame] | 1804 | eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot), |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 1805 | btrfs_node_ptr_generation(parent, slot), |
| 1806 | level - 1, &first_key); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1807 | if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { |
| 1808 | free_extent_buffer(eb); |
| 1809 | eb = ERR_PTR(-EIO); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | return eb; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1813 | } |
| 1814 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1815 | /* |
| 1816 | * node level balancing, used to make sure nodes are in proper order for |
| 1817 | * item deletion. We balance from the top down, so we have to make sure |
| 1818 | * that a deletion won't leave an node completely empty later on. |
| 1819 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1820 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1821 | struct btrfs_root *root, |
| 1822 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1823 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1824 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1825 | struct extent_buffer *right = NULL; |
| 1826 | struct extent_buffer *mid; |
| 1827 | struct extent_buffer *left = NULL; |
| 1828 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1829 | int ret = 0; |
| 1830 | int wret; |
| 1831 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1832 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1833 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1834 | |
Liu Bo | 98e6b1e | 2018-09-12 06:06:23 +0800 | [diff] [blame] | 1835 | ASSERT(level > 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1836 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1837 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1838 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1839 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1840 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1841 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1842 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1843 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1844 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1845 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1846 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1847 | pslot = path->slots[level + 1]; |
| 1848 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1849 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1850 | /* |
| 1851 | * deal with the case where there is only one pointer in the root |
| 1852 | * by promoting the node below to a root |
| 1853 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1854 | if (!parent) { |
| 1855 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1856 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1857 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1858 | return 0; |
| 1859 | |
| 1860 | /* promote the child to a root */ |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1861 | child = btrfs_read_node_slot(mid, 0); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1862 | if (IS_ERR(child)) { |
| 1863 | ret = PTR_ERR(child); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1864 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1865 | goto enospc; |
| 1866 | } |
| 1867 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1868 | btrfs_tree_lock(child); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 1869 | btrfs_set_lock_blocking_write(child); |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1870 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child, |
| 1871 | BTRFS_NESTING_COW); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1872 | if (ret) { |
| 1873 | btrfs_tree_unlock(child); |
| 1874 | free_extent_buffer(child); |
| 1875 | goto enospc; |
| 1876 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1877 | |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 1878 | ret = tree_mod_log_insert_root(root->node, child, 1); |
| 1879 | BUG_ON(ret < 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1880 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1881 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1882 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1883 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1884 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1885 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1886 | path->nodes[level] = NULL; |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1887 | btrfs_clean_tree_block(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1888 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1889 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1890 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1891 | |
| 1892 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1893 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1894 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1895 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1896 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1897 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1898 | if (btrfs_header_nritems(mid) > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1899 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1900 | return 0; |
| 1901 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1902 | left = btrfs_read_node_slot(parent, pslot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1903 | if (IS_ERR(left)) |
| 1904 | left = NULL; |
| 1905 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1906 | if (left) { |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 1907 | __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 1908 | btrfs_set_lock_blocking_write(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1909 | wret = btrfs_cow_block(trans, root, left, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1910 | parent, pslot - 1, &left, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 1911 | BTRFS_NESTING_LEFT_COW); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1912 | if (wret) { |
| 1913 | ret = wret; |
| 1914 | goto enospc; |
| 1915 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1916 | } |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1917 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 1918 | right = btrfs_read_node_slot(parent, pslot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1919 | if (IS_ERR(right)) |
| 1920 | right = NULL; |
| 1921 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1922 | if (right) { |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 1923 | __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 1924 | btrfs_set_lock_blocking_write(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1925 | wret = btrfs_cow_block(trans, root, right, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 1926 | parent, pslot + 1, &right, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 1927 | BTRFS_NESTING_RIGHT_COW); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1928 | if (wret) { |
| 1929 | ret = wret; |
| 1930 | goto enospc; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1935 | if (left) { |
| 1936 | orig_slot += btrfs_header_nritems(left); |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 1937 | wret = push_node_left(trans, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1938 | if (wret < 0) |
| 1939 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1940 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1941 | |
| 1942 | /* |
| 1943 | * then try to empty the right most buffer into the middle |
| 1944 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1945 | if (right) { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 1946 | wret = push_node_left(trans, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1947 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1948 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1949 | if (btrfs_header_nritems(right) == 0) { |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1950 | btrfs_clean_tree_block(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1951 | btrfs_tree_unlock(right); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1952 | del_ptr(root, path, level + 1, pslot + 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1953 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1954 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1955 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1956 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1957 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1958 | struct btrfs_disk_key right_key; |
| 1959 | btrfs_node_key(right, &right_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 1960 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
| 1961 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 1962 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1963 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1964 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1965 | } |
| 1966 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1967 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1968 | /* |
| 1969 | * we're not allowed to leave a node with one item in the |
| 1970 | * tree during a delete. A deletion from lower in the tree |
| 1971 | * could try to delete the only pointer in this node. |
| 1972 | * So, pull some keys from the left. |
| 1973 | * There has to be a left pointer at this point because |
| 1974 | * otherwise we would have pulled some pointers from the |
| 1975 | * right |
| 1976 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1977 | if (!left) { |
| 1978 | ret = -EROFS; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1979 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1980 | goto enospc; |
| 1981 | } |
David Sterba | 55d32ed | 2019-03-20 14:18:06 +0100 | [diff] [blame] | 1982 | wret = balance_node_right(trans, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1983 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1984 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1985 | goto enospc; |
| 1986 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1987 | if (wret == 1) { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 1988 | wret = push_node_left(trans, left, mid, 1); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1989 | if (wret < 0) |
| 1990 | ret = wret; |
| 1991 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1992 | BUG_ON(wret == 1); |
| 1993 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1994 | if (btrfs_header_nritems(mid) == 0) { |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 1995 | btrfs_clean_tree_block(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1996 | btrfs_tree_unlock(mid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1997 | del_ptr(root, path, level + 1, pslot); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1998 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1999 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 2000 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 2001 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2002 | } else { |
| 2003 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2004 | struct btrfs_disk_key mid_key; |
| 2005 | btrfs_node_key(mid, &mid_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 2006 | ret = tree_mod_log_insert_key(parent, pslot, |
| 2007 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2008 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2009 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 2010 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2011 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2012 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2013 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2014 | if (left) { |
| 2015 | if (btrfs_header_nritems(left) > orig_slot) { |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 2016 | atomic_inc(&left->refs); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2017 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2018 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2019 | path->slots[level + 1] -= 1; |
| 2020 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2021 | if (mid) { |
| 2022 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2023 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2024 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2025 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2026 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2027 | path->slots[level] = orig_slot; |
| 2028 | } |
| 2029 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2030 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2031 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2032 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2033 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2034 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2035 | if (right) { |
| 2036 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2037 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2038 | } |
| 2039 | if (left) { |
| 2040 | if (path->nodes[level] != left) |
| 2041 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2042 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2043 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2044 | return ret; |
| 2045 | } |
| 2046 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2047 | /* Node balancing for insertion. Here we only split or push nodes around |
| 2048 | * when they are completely full. This is also done top down, so we |
| 2049 | * have to be pessimistic. |
| 2050 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2051 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2052 | struct btrfs_root *root, |
| 2053 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2054 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2055 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2056 | struct extent_buffer *right = NULL; |
| 2057 | struct extent_buffer *mid; |
| 2058 | struct extent_buffer *left = NULL; |
| 2059 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2060 | int ret = 0; |
| 2061 | int wret; |
| 2062 | int pslot; |
| 2063 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2064 | |
| 2065 | if (level == 0) |
| 2066 | return 1; |
| 2067 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2068 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2069 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2070 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 2071 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2072 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 2073 | pslot = path->slots[level + 1]; |
| 2074 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2075 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2076 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2077 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2078 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 2079 | left = btrfs_read_node_slot(parent, pslot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 2080 | if (IS_ERR(left)) |
| 2081 | left = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2082 | |
| 2083 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2084 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2085 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2086 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 2087 | __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 2088 | btrfs_set_lock_blocking_write(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2089 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2090 | left_nr = btrfs_header_nritems(left); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2091 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2092 | wret = 1; |
| 2093 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2094 | ret = btrfs_cow_block(trans, root, left, parent, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 2095 | pslot - 1, &left, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 2096 | BTRFS_NESTING_LEFT_COW); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2097 | if (ret) |
| 2098 | wret = 1; |
| 2099 | else { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 2100 | wret = push_node_left(trans, left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2101 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2102 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2103 | if (wret < 0) |
| 2104 | ret = wret; |
| 2105 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2106 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2107 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2108 | btrfs_node_key(mid, &disk_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 2109 | ret = tree_mod_log_insert_key(parent, pslot, |
| 2110 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2111 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2112 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 2113 | btrfs_mark_buffer_dirty(parent); |
| 2114 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2115 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2116 | path->slots[level + 1] -= 1; |
| 2117 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2118 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2119 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2120 | } else { |
| 2121 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2122 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2123 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2124 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2125 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2126 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2127 | return 0; |
| 2128 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2129 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2130 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2131 | } |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 2132 | right = btrfs_read_node_slot(parent, pslot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 2133 | if (IS_ERR(right)) |
| 2134 | right = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2135 | |
| 2136 | /* |
| 2137 | * then try to empty the right most buffer into the middle |
| 2138 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2139 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2140 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2141 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 2142 | __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 2143 | btrfs_set_lock_blocking_write(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2144 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2145 | right_nr = btrfs_header_nritems(right); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2146 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2147 | wret = 1; |
| 2148 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2149 | ret = btrfs_cow_block(trans, root, right, |
| 2150 | parent, pslot + 1, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 2151 | &right, BTRFS_NESTING_RIGHT_COW); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2152 | if (ret) |
| 2153 | wret = 1; |
| 2154 | else { |
David Sterba | 55d32ed | 2019-03-20 14:18:06 +0100 | [diff] [blame] | 2155 | wret = balance_node_right(trans, right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2156 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2157 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2158 | if (wret < 0) |
| 2159 | ret = wret; |
| 2160 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2161 | struct btrfs_disk_key disk_key; |
| 2162 | |
| 2163 | btrfs_node_key(right, &disk_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 2164 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
| 2165 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2166 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2167 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2168 | btrfs_mark_buffer_dirty(parent); |
| 2169 | |
| 2170 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2171 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2172 | path->slots[level + 1] += 1; |
| 2173 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2174 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2175 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2176 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2177 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2178 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2179 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2180 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2181 | return 0; |
| 2182 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2183 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2184 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2185 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2186 | return 1; |
| 2187 | } |
| 2188 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2189 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2190 | * readahead one full node of leaves, finding things that are close |
| 2191 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2192 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2193 | static void reada_for_search(struct btrfs_fs_info *fs_info, |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2194 | struct btrfs_path *path, |
| 2195 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2196 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2197 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2198 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2199 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2200 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2201 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2202 | u64 nread = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2203 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2204 | u32 nr; |
| 2205 | u32 blocksize; |
| 2206 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2207 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2208 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2209 | return; |
| 2210 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2211 | if (!path->nodes[level]) |
| 2212 | return; |
| 2213 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2214 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2215 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2216 | search = btrfs_node_blockptr(node, slot); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2217 | blocksize = fs_info->nodesize; |
| 2218 | eb = find_extent_buffer(fs_info, search); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2219 | if (eb) { |
| 2220 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2221 | return; |
| 2222 | } |
| 2223 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2224 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2225 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2226 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2227 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2228 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2229 | while (1) { |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2230 | if (path->reada == READA_BACK) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2231 | if (nr == 0) |
| 2232 | break; |
| 2233 | nr--; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2234 | } else if (path->reada == READA_FORWARD) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2235 | nr++; |
| 2236 | if (nr >= nritems) |
| 2237 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2238 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2239 | if (path->reada == READA_BACK && objectid) { |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2240 | btrfs_node_key(node, &disk_key, nr); |
| 2241 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2242 | break; |
| 2243 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2244 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2245 | if ((search <= target && target - search <= 65536) || |
| 2246 | (search > target && search - target <= 65536)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2247 | readahead_tree_block(fs_info, search); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2248 | nread += blocksize; |
| 2249 | } |
| 2250 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2251 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2252 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2253 | } |
| 2254 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2255 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2256 | static noinline void reada_for_balance(struct btrfs_fs_info *fs_info, |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2257 | struct btrfs_path *path, int level) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2258 | { |
| 2259 | int slot; |
| 2260 | int nritems; |
| 2261 | struct extent_buffer *parent; |
| 2262 | struct extent_buffer *eb; |
| 2263 | u64 gen; |
| 2264 | u64 block1 = 0; |
| 2265 | u64 block2 = 0; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2266 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2267 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2268 | if (!parent) |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2269 | return; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2270 | |
| 2271 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2272 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2273 | |
| 2274 | if (slot > 0) { |
| 2275 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2276 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2277 | eb = find_extent_buffer(fs_info, block1); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2278 | /* |
| 2279 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2280 | * don't want to return eagain here. That will loop |
| 2281 | * forever |
| 2282 | */ |
| 2283 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2284 | block1 = 0; |
| 2285 | free_extent_buffer(eb); |
| 2286 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2287 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2288 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2289 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2290 | eb = find_extent_buffer(fs_info, block2); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2291 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2292 | block2 = 0; |
| 2293 | free_extent_buffer(eb); |
| 2294 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2295 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2296 | if (block1) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2297 | readahead_tree_block(fs_info, block1); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2298 | if (block2) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2299 | readahead_tree_block(fs_info, block2); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | |
| 2303 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2304 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2305 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2306 | * operations on the tree might require changing key pointers higher up in the |
| 2307 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2308 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2309 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2310 | * the lock if the path points to the last slot in the block. This is part of |
| 2311 | * 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] | 2312 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2313 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2314 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2315 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2316 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2317 | int lowest_unlock, int min_write_lock_level, |
| 2318 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2319 | { |
| 2320 | int i; |
| 2321 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2322 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2323 | struct extent_buffer *t; |
| 2324 | |
| 2325 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2326 | if (!path->nodes[i]) |
| 2327 | break; |
| 2328 | if (!path->locks[i]) |
| 2329 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2330 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2331 | skip_level = i + 1; |
| 2332 | continue; |
| 2333 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2334 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2335 | u32 nritems; |
| 2336 | t = path->nodes[i]; |
| 2337 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2338 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2339 | skip_level = i + 1; |
| 2340 | continue; |
| 2341 | } |
| 2342 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2343 | if (skip_level < i && i >= lowest_unlock) |
| 2344 | no_skips = 1; |
| 2345 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2346 | t = path->nodes[i]; |
Liu Bo | d80bb3f | 2018-05-18 11:00:24 +0800 | [diff] [blame] | 2347 | if (i >= lowest_unlock && i > skip_level) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2348 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2349 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2350 | if (write_lock_level && |
| 2351 | i > min_write_lock_level && |
| 2352 | i <= *write_lock_level) { |
| 2353 | *write_lock_level = i - 1; |
| 2354 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2355 | } |
| 2356 | } |
| 2357 | } |
| 2358 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2359 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2360 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2361 | * in cache without setting the path to blocking. If we find the block |
| 2362 | * we return zero and the path is unchanged. |
| 2363 | * |
| 2364 | * If we can't find the block, we set the path blocking and do some |
| 2365 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2366 | */ |
| 2367 | static int |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 2368 | read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, |
| 2369 | struct extent_buffer **eb_ret, int level, int slot, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 2370 | const struct btrfs_key *key) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2371 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2372 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2373 | u64 blocknr; |
| 2374 | u64 gen; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2375 | struct extent_buffer *tmp; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2376 | struct btrfs_key first_key; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2377 | int ret; |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2378 | int parent_level; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2379 | |
Nikolay Borisov | 213ff4b | 2020-05-27 13:10:59 +0300 | [diff] [blame] | 2380 | blocknr = btrfs_node_blockptr(*eb_ret, slot); |
| 2381 | gen = btrfs_node_ptr_generation(*eb_ret, slot); |
| 2382 | parent_level = btrfs_header_level(*eb_ret); |
| 2383 | btrfs_node_key_to_cpu(*eb_ret, &first_key, slot); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2384 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2385 | tmp = find_extent_buffer(fs_info, blocknr); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2386 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2387 | /* first we do an atomic uptodate check */ |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2388 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
Qu Wenruo | 448de47 | 2019-03-12 17:10:40 +0800 | [diff] [blame] | 2389 | /* |
| 2390 | * Do extra check for first_key, eb can be stale due to |
| 2391 | * being cached, read from scrub, or have multiple |
| 2392 | * parents (shared tree blocks). |
| 2393 | */ |
David Sterba | e064d5e | 2019-03-20 14:58:13 +0100 | [diff] [blame] | 2394 | if (btrfs_verify_level_key(tmp, |
Qu Wenruo | 448de47 | 2019-03-12 17:10:40 +0800 | [diff] [blame] | 2395 | parent_level - 1, &first_key, gen)) { |
| 2396 | free_extent_buffer(tmp); |
| 2397 | return -EUCLEAN; |
| 2398 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2399 | *eb_ret = tmp; |
| 2400 | return 0; |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2401 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2402 | |
| 2403 | /* the pages were up to date, but we failed |
| 2404 | * the generation number check. Do a full |
| 2405 | * read for the generation number that is correct. |
| 2406 | * We must do this without dropping locks so |
| 2407 | * we can trust our generation number |
| 2408 | */ |
| 2409 | btrfs_set_path_blocking(p); |
| 2410 | |
| 2411 | /* now we're allowed to do a blocking uptodate check */ |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2412 | ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key); |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2413 | if (!ret) { |
| 2414 | *eb_ret = tmp; |
| 2415 | return 0; |
| 2416 | } |
| 2417 | free_extent_buffer(tmp); |
| 2418 | btrfs_release_path(p); |
| 2419 | return -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2420 | } |
| 2421 | |
| 2422 | /* |
| 2423 | * reduce lock contention at high levels |
| 2424 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2425 | * we read. Don't release the lock on the current |
| 2426 | * level because we need to walk this node to figure |
| 2427 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2428 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2429 | btrfs_unlock_up_safe(p, level + 1); |
| 2430 | btrfs_set_path_blocking(p); |
| 2431 | |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2432 | if (p->reada != READA_NONE) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2433 | reada_for_search(fs_info, p, level, slot, key->objectid); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2434 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2435 | ret = -EAGAIN; |
Liu Bo | 02a3307 | 2018-05-16 01:37:36 +0800 | [diff] [blame] | 2436 | tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1, |
Qu Wenruo | 581c176 | 2018-03-29 09:08:11 +0800 | [diff] [blame] | 2437 | &first_key); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 2438 | if (!IS_ERR(tmp)) { |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2439 | /* |
| 2440 | * If the read above didn't mark this buffer up to date, |
| 2441 | * it will never end up being up to date. Set ret to EIO now |
| 2442 | * and give up so that our caller doesn't loop forever |
| 2443 | * on our EAGAINs. |
| 2444 | */ |
Liu Bo | e6a1d6f | 2018-05-18 11:00:20 +0800 | [diff] [blame] | 2445 | if (!extent_buffer_uptodate(tmp)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2446 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2447 | free_extent_buffer(tmp); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2448 | } else { |
| 2449 | ret = PTR_ERR(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2450 | } |
Liu Bo | 02a3307 | 2018-05-16 01:37:36 +0800 | [diff] [blame] | 2451 | |
| 2452 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2453 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * helper function for btrfs_search_slot. This does all of the checks |
| 2458 | * for node-level blocks and does any balancing required based on |
| 2459 | * the ins_len. |
| 2460 | * |
| 2461 | * If no extra work was required, zero is returned. If we had to |
| 2462 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2463 | * start over |
| 2464 | */ |
| 2465 | static int |
| 2466 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2467 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2468 | struct extent_buffer *b, int level, int ins_len, |
| 2469 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2470 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2471 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2472 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2473 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2474 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2475 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2476 | int sret; |
| 2477 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2478 | if (*write_lock_level < level + 1) { |
| 2479 | *write_lock_level = level + 1; |
| 2480 | btrfs_release_path(p); |
| 2481 | goto again; |
| 2482 | } |
| 2483 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2484 | btrfs_set_path_blocking(p); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2485 | reada_for_balance(fs_info, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2486 | sret = split_node(trans, root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2487 | |
| 2488 | BUG_ON(sret > 0); |
| 2489 | if (sret) { |
| 2490 | ret = sret; |
| 2491 | goto done; |
| 2492 | } |
| 2493 | b = p->nodes[level]; |
| 2494 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2495 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2496 | int sret; |
| 2497 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2498 | if (*write_lock_level < level + 1) { |
| 2499 | *write_lock_level = level + 1; |
| 2500 | btrfs_release_path(p); |
| 2501 | goto again; |
| 2502 | } |
| 2503 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2504 | btrfs_set_path_blocking(p); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2505 | reada_for_balance(fs_info, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2506 | sret = balance_level(trans, root, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2507 | |
| 2508 | if (sret) { |
| 2509 | ret = sret; |
| 2510 | goto done; |
| 2511 | } |
| 2512 | b = p->nodes[level]; |
| 2513 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2514 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2515 | goto again; |
| 2516 | } |
| 2517 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2518 | } |
| 2519 | return 0; |
| 2520 | |
| 2521 | again: |
| 2522 | ret = -EAGAIN; |
| 2523 | done: |
| 2524 | return ret; |
| 2525 | } |
| 2526 | |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 2527 | 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] | 2528 | u64 iobjectid, u64 ioff, u8 key_type, |
| 2529 | struct btrfs_key *found_key) |
| 2530 | { |
| 2531 | int ret; |
| 2532 | struct btrfs_key key; |
| 2533 | struct extent_buffer *eb; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 2534 | |
| 2535 | ASSERT(path); |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 2536 | ASSERT(found_key); |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2537 | |
| 2538 | key.type = key_type; |
| 2539 | key.objectid = iobjectid; |
| 2540 | key.offset = ioff; |
| 2541 | |
| 2542 | ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 2543 | if (ret < 0) |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2544 | return ret; |
| 2545 | |
| 2546 | eb = path->nodes[0]; |
| 2547 | if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { |
| 2548 | ret = btrfs_next_leaf(fs_root, path); |
| 2549 | if (ret) |
| 2550 | return ret; |
| 2551 | eb = path->nodes[0]; |
| 2552 | } |
| 2553 | |
| 2554 | btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); |
| 2555 | if (found_key->type != key.type || |
| 2556 | found_key->objectid != key.objectid) |
| 2557 | return 1; |
| 2558 | |
| 2559 | return 0; |
| 2560 | } |
| 2561 | |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2562 | static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root, |
| 2563 | struct btrfs_path *p, |
| 2564 | int write_lock_level) |
| 2565 | { |
| 2566 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 2567 | struct extent_buffer *b; |
| 2568 | int root_lock; |
| 2569 | int level = 0; |
| 2570 | |
| 2571 | /* We try very hard to do read locks on the root */ |
| 2572 | root_lock = BTRFS_READ_LOCK; |
| 2573 | |
| 2574 | if (p->search_commit_root) { |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2575 | /* |
| 2576 | * The commit roots are read only so we always do read locks, |
| 2577 | * and we always must hold the commit_root_sem when doing |
| 2578 | * searches on them, the only exception is send where we don't |
| 2579 | * want to block transaction commits for a long time, so |
| 2580 | * we need to clone the commit root in order to avoid races |
| 2581 | * with transaction commits that create a snapshot of one of |
| 2582 | * the roots used by a send operation. |
| 2583 | */ |
| 2584 | if (p->need_commit_sem) { |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2585 | down_read(&fs_info->commit_root_sem); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2586 | b = btrfs_clone_extent_buffer(root->commit_root); |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2587 | up_read(&fs_info->commit_root_sem); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2588 | if (!b) |
| 2589 | return ERR_PTR(-ENOMEM); |
| 2590 | |
| 2591 | } else { |
| 2592 | b = root->commit_root; |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 2593 | atomic_inc(&b->refs); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2594 | } |
| 2595 | level = btrfs_header_level(b); |
Liu Bo | f9ddfd0 | 2018-05-29 21:27:06 +0800 | [diff] [blame] | 2596 | /* |
| 2597 | * Ensure that all callers have set skip_locking when |
| 2598 | * p->search_commit_root = 1. |
| 2599 | */ |
| 2600 | ASSERT(p->skip_locking == 1); |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2601 | |
| 2602 | goto out; |
| 2603 | } |
| 2604 | |
| 2605 | if (p->skip_locking) { |
| 2606 | b = btrfs_root_node(root); |
| 2607 | level = btrfs_header_level(b); |
| 2608 | goto out; |
| 2609 | } |
| 2610 | |
| 2611 | /* |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2612 | * If the level is set to maximum, we can skip trying to get the read |
| 2613 | * lock. |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2614 | */ |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2615 | if (write_lock_level < BTRFS_MAX_LEVEL) { |
| 2616 | /* |
| 2617 | * We don't know the level of the root node until we actually |
| 2618 | * have it read locked |
| 2619 | */ |
Josef Bacik | 5189941 | 2020-08-20 11:46:01 -0400 | [diff] [blame] | 2620 | b = __btrfs_read_lock_root_node(root, p->recurse); |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2621 | level = btrfs_header_level(b); |
| 2622 | if (level > write_lock_level) |
| 2623 | goto out; |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2624 | |
Liu Bo | 662c653 | 2018-05-18 11:00:23 +0800 | [diff] [blame] | 2625 | /* Whoops, must trade for write lock */ |
| 2626 | btrfs_tree_read_unlock(b); |
| 2627 | free_extent_buffer(b); |
| 2628 | } |
| 2629 | |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2630 | b = btrfs_lock_root_node(root); |
| 2631 | root_lock = BTRFS_WRITE_LOCK; |
| 2632 | |
| 2633 | /* The level might have changed, check again */ |
| 2634 | level = btrfs_header_level(b); |
| 2635 | |
| 2636 | out: |
| 2637 | p->nodes[level] = b; |
| 2638 | if (!p->skip_locking) |
| 2639 | p->locks[level] = root_lock; |
| 2640 | /* |
| 2641 | * Callers are responsible for dropping b's references. |
| 2642 | */ |
| 2643 | return b; |
| 2644 | } |
| 2645 | |
| 2646 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2647 | /* |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2648 | * btrfs_search_slot - look for a key in a tree and perform necessary |
| 2649 | * modifications to preserve tree invariants. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2650 | * |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2651 | * @trans: Handle of transaction, used when modifying the tree |
| 2652 | * @p: Holds all btree nodes along the search path |
| 2653 | * @root: The root node of the tree |
| 2654 | * @key: The key we are looking for |
| 2655 | * @ins_len: Indicates purpose of search, for inserts it is 1, for |
| 2656 | * deletions it's -1. 0 for plain searches |
| 2657 | * @cow: boolean should CoW operations be performed. Must always be 1 |
| 2658 | * when modifying the tree. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2659 | * |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2660 | * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. |
| 2661 | * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) |
| 2662 | * |
| 2663 | * If @key is found, 0 is returned and you can find the item in the leaf level |
| 2664 | * of the path (level 0) |
| 2665 | * |
| 2666 | * If @key isn't found, 1 is returned and the leaf level of the path (level 0) |
| 2667 | * points to the slot where it should be inserted |
| 2668 | * |
| 2669 | * If an error is encountered while searching the tree a negative error number |
| 2670 | * is returned |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2671 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2672 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2673 | const struct btrfs_key *key, struct btrfs_path *p, |
| 2674 | int ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2675 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2676 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2677 | int slot; |
| 2678 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2679 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2680 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2681 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2682 | /* everything at write_lock_level or lower must be write locked */ |
| 2683 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2684 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2685 | int min_write_lock_level; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2686 | int prev_cmp; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2687 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2688 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2689 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2690 | WARN_ON(p->nodes[0] != NULL); |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2691 | BUG_ON(!cow && ins_len); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2692 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2693 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2694 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2695 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2696 | /* when we are removing items, we might have to go up to level |
| 2697 | * two as we update tree pointers Make sure we keep write |
| 2698 | * for those levels as well |
| 2699 | */ |
| 2700 | write_lock_level = 2; |
| 2701 | } else if (ins_len > 0) { |
| 2702 | /* |
| 2703 | * for inserting items, make sure we have a write lock on |
| 2704 | * level 1 so we can update keys |
| 2705 | */ |
| 2706 | write_lock_level = 1; |
| 2707 | } |
| 2708 | |
| 2709 | if (!cow) |
| 2710 | write_lock_level = -1; |
| 2711 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2712 | if (cow && (p->keep_locks || p->lowest_level)) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2713 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2714 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2715 | min_write_lock_level = write_lock_level; |
| 2716 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2717 | again: |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2718 | prev_cmp = -1; |
Liu Bo | 1fc28d8 | 2018-05-18 11:00:21 +0800 | [diff] [blame] | 2719 | b = btrfs_search_slot_get_root(root, p, write_lock_level); |
Filipe Manana | be6821f | 2018-12-11 10:19:45 +0000 | [diff] [blame] | 2720 | if (IS_ERR(b)) { |
| 2721 | ret = PTR_ERR(b); |
| 2722 | goto done; |
| 2723 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2724 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2725 | while (b) { |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2726 | int dec = 0; |
| 2727 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2728 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2729 | |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2730 | if (cow) { |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2731 | bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); |
| 2732 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2733 | /* |
| 2734 | * if we don't really need to cow this block |
| 2735 | * then we don't want to set the path blocking, |
| 2736 | * so we test it here |
| 2737 | */ |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 2738 | if (!should_cow_block(trans, root, b)) { |
| 2739 | trans->dirty = true; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2740 | goto cow_done; |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 2741 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2742 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2743 | /* |
| 2744 | * must have write locks on this node and the |
| 2745 | * parent |
| 2746 | */ |
Josef Bacik | 5124e00 | 2012-11-07 13:44:13 -0500 | [diff] [blame] | 2747 | if (level > write_lock_level || |
| 2748 | (level + 1 > write_lock_level && |
| 2749 | level + 1 < BTRFS_MAX_LEVEL && |
| 2750 | p->nodes[level + 1])) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2751 | write_lock_level = level + 1; |
| 2752 | btrfs_release_path(p); |
| 2753 | goto again; |
| 2754 | } |
| 2755 | |
Filipe Manana | 160f408 | 2014-07-28 19:37:17 +0100 | [diff] [blame] | 2756 | btrfs_set_path_blocking(p); |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2757 | if (last_level) |
| 2758 | err = btrfs_cow_block(trans, root, b, NULL, 0, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 2759 | &b, |
| 2760 | BTRFS_NESTING_COW); |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2761 | else |
| 2762 | err = btrfs_cow_block(trans, root, b, |
| 2763 | p->nodes[level + 1], |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 2764 | p->slots[level + 1], &b, |
| 2765 | BTRFS_NESTING_COW); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2766 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2767 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2768 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2769 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2770 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2771 | cow_done: |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2772 | p->nodes[level] = b; |
Liu Bo | 5239834 | 2018-08-22 05:54:37 +0800 | [diff] [blame] | 2773 | /* |
| 2774 | * Leave path with blocking locks to avoid massive |
| 2775 | * lock context switch, this is made on purpose. |
| 2776 | */ |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2777 | |
| 2778 | /* |
| 2779 | * we have a lock on b and as long as we aren't changing |
| 2780 | * the tree, there is no way to for the items in b to change. |
| 2781 | * It is safe to drop the lock on our parent before we |
| 2782 | * go through the expensive btree search on b. |
| 2783 | * |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2784 | * If we're inserting or deleting (ins_len != 0), then we might |
| 2785 | * be changing slot zero, which may require changing the parent. |
| 2786 | * So, we can't drop the lock until after we know which slot |
| 2787 | * we're operating on. |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2788 | */ |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2789 | if (!ins_len && !p->keep_locks) { |
| 2790 | int u = level + 1; |
| 2791 | |
| 2792 | if (u < BTRFS_MAX_LEVEL && p->locks[u]) { |
| 2793 | btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); |
| 2794 | p->locks[u] = 0; |
| 2795 | } |
| 2796 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2797 | |
Nikolay Borisov | 995e9a1 | 2020-05-27 13:10:53 +0300 | [diff] [blame] | 2798 | /* |
| 2799 | * If btrfs_bin_search returns an exact match (prev_cmp == 0) |
| 2800 | * we can safely assume the target key will always be in slot 0 |
| 2801 | * on lower levels due to the invariants BTRFS' btree provides, |
| 2802 | * namely that a btrfs_key_ptr entry always points to the |
| 2803 | * lowest key in the child node, thus we can skip searching |
| 2804 | * lower levels |
| 2805 | */ |
| 2806 | if (prev_cmp == 0) { |
| 2807 | slot = 0; |
| 2808 | ret = 0; |
| 2809 | } else { |
| 2810 | ret = btrfs_bin_search(b, key, &slot); |
| 2811 | prev_cmp = ret; |
| 2812 | if (ret < 0) |
| 2813 | goto done; |
| 2814 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2815 | |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2816 | if (level == 0) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2817 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2818 | if (ins_len > 0 && |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 2819 | btrfs_leaf_free_space(b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2820 | if (write_lock_level < 1) { |
| 2821 | write_lock_level = 1; |
| 2822 | btrfs_release_path(p); |
| 2823 | goto again; |
| 2824 | } |
| 2825 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2826 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2827 | err = split_leaf(trans, root, key, |
| 2828 | p, ins_len, ret == 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2829 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2830 | BUG_ON(err > 0); |
| 2831 | if (err) { |
| 2832 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2833 | goto done; |
| 2834 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2835 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2836 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2837 | unlock_up(p, level, lowest_unlock, |
Liu Bo | 4b6f8e9 | 2018-08-14 10:46:53 +0800 | [diff] [blame] | 2838 | min_write_lock_level, NULL); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2839 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2840 | } |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2841 | if (ret && slot > 0) { |
| 2842 | dec = 1; |
| 2843 | slot--; |
| 2844 | } |
| 2845 | p->slots[level] = slot; |
| 2846 | err = setup_nodes_for_search(trans, root, p, b, level, ins_len, |
| 2847 | &write_lock_level); |
| 2848 | if (err == -EAGAIN) |
| 2849 | goto again; |
| 2850 | if (err) { |
| 2851 | ret = err; |
| 2852 | goto done; |
| 2853 | } |
| 2854 | b = p->nodes[level]; |
| 2855 | slot = p->slots[level]; |
| 2856 | |
| 2857 | /* |
| 2858 | * Slot 0 is special, if we change the key we have to update |
| 2859 | * the parent pointer which means we must have a write lock on |
| 2860 | * the parent |
| 2861 | */ |
| 2862 | if (slot == 0 && ins_len && write_lock_level < level + 1) { |
| 2863 | write_lock_level = level + 1; |
| 2864 | btrfs_release_path(p); |
| 2865 | goto again; |
| 2866 | } |
| 2867 | |
| 2868 | unlock_up(p, level, lowest_unlock, min_write_lock_level, |
| 2869 | &write_lock_level); |
| 2870 | |
| 2871 | if (level == lowest_level) { |
| 2872 | if (dec) |
| 2873 | p->slots[level]++; |
| 2874 | goto done; |
| 2875 | } |
| 2876 | |
| 2877 | err = read_block_for_search(root, p, &b, level, slot, key); |
| 2878 | if (err == -EAGAIN) |
| 2879 | goto again; |
| 2880 | if (err) { |
| 2881 | ret = err; |
| 2882 | goto done; |
| 2883 | } |
| 2884 | |
| 2885 | if (!p->skip_locking) { |
| 2886 | level = btrfs_header_level(b); |
| 2887 | if (level <= write_lock_level) { |
| 2888 | if (!btrfs_try_tree_write_lock(b)) { |
| 2889 | btrfs_set_path_blocking(p); |
| 2890 | btrfs_tree_lock(b); |
| 2891 | } |
| 2892 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2893 | } else { |
| 2894 | if (!btrfs_tree_read_lock_atomic(b)) { |
| 2895 | btrfs_set_path_blocking(p); |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 2896 | __btrfs_tree_read_lock(b, BTRFS_NESTING_NORMAL, |
| 2897 | p->recurse); |
Qu Wenruo | f624d97 | 2019-09-10 15:40:17 +0800 | [diff] [blame] | 2898 | } |
| 2899 | p->locks[level] = BTRFS_READ_LOCK; |
| 2900 | } |
| 2901 | p->nodes[level] = b; |
| 2902 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2903 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2904 | ret = 1; |
| 2905 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2906 | /* |
| 2907 | * we don't really know what they plan on doing with the path |
| 2908 | * from here on, so for now just mark it as blocking |
| 2909 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2910 | if (!p->leave_spinning) |
| 2911 | btrfs_set_path_blocking(p); |
Filipe Manana | 5f5bc6b | 2014-11-09 08:38:39 +0000 | [diff] [blame] | 2912 | if (ret < 0 && !p->skip_release_on_error) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2913 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2914 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2915 | } |
| 2916 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2917 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2918 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2919 | * current state of the tree together with the operations recorded in the tree |
| 2920 | * modification log to search for the key in a previous version of this tree, as |
| 2921 | * denoted by the time_seq parameter. |
| 2922 | * |
| 2923 | * Naturally, there is no support for insert, delete or cow operations. |
| 2924 | * |
| 2925 | * The resulting path and return value will be set up as if we called |
| 2926 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2927 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2928 | 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] | 2929 | struct btrfs_path *p, u64 time_seq) |
| 2930 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2931 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2932 | struct extent_buffer *b; |
| 2933 | int slot; |
| 2934 | int ret; |
| 2935 | int err; |
| 2936 | int level; |
| 2937 | int lowest_unlock = 1; |
| 2938 | u8 lowest_level = 0; |
| 2939 | |
| 2940 | lowest_level = p->lowest_level; |
| 2941 | WARN_ON(p->nodes[0] != NULL); |
| 2942 | |
| 2943 | if (p->search_commit_root) { |
| 2944 | BUG_ON(time_seq); |
| 2945 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2946 | } |
| 2947 | |
| 2948 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2949 | b = get_old_root(root, time_seq); |
Nikolay Borisov | 315bed4 | 2018-09-13 11:35:10 +0300 | [diff] [blame] | 2950 | if (!b) { |
| 2951 | ret = -EIO; |
| 2952 | goto done; |
| 2953 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2954 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2955 | p->locks[level] = BTRFS_READ_LOCK; |
| 2956 | |
| 2957 | while (b) { |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2958 | int dec = 0; |
| 2959 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2960 | level = btrfs_header_level(b); |
| 2961 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2962 | |
| 2963 | /* |
| 2964 | * we have a lock on b and as long as we aren't changing |
| 2965 | * the tree, there is no way to for the items in b to change. |
| 2966 | * It is safe to drop the lock on our parent before we |
| 2967 | * go through the expensive btree search on b. |
| 2968 | */ |
| 2969 | btrfs_unlock_up_safe(p, level + 1); |
| 2970 | |
Nikolay Borisov | 995e9a1 | 2020-05-27 13:10:53 +0300 | [diff] [blame] | 2971 | ret = btrfs_bin_search(b, key, &slot); |
Filipe Manana | cbca7d5 | 2019-02-18 16:57:26 +0000 | [diff] [blame] | 2972 | if (ret < 0) |
| 2973 | goto done; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2974 | |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2975 | if (level == 0) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2976 | p->slots[level] = slot; |
| 2977 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2978 | goto done; |
| 2979 | } |
Qu Wenruo | abe9339 | 2019-09-10 15:40:18 +0800 | [diff] [blame] | 2980 | |
| 2981 | if (ret && slot > 0) { |
| 2982 | dec = 1; |
| 2983 | slot--; |
| 2984 | } |
| 2985 | p->slots[level] = slot; |
| 2986 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2987 | |
| 2988 | if (level == lowest_level) { |
| 2989 | if (dec) |
| 2990 | p->slots[level]++; |
| 2991 | goto done; |
| 2992 | } |
| 2993 | |
| 2994 | err = read_block_for_search(root, p, &b, level, slot, key); |
| 2995 | if (err == -EAGAIN) |
| 2996 | goto again; |
| 2997 | if (err) { |
| 2998 | ret = err; |
| 2999 | goto done; |
| 3000 | } |
| 3001 | |
| 3002 | level = btrfs_header_level(b); |
| 3003 | if (!btrfs_tree_read_lock_atomic(b)) { |
| 3004 | btrfs_set_path_blocking(p); |
| 3005 | btrfs_tree_read_lock(b); |
| 3006 | } |
| 3007 | b = tree_mod_log_rewind(fs_info, p, b, time_seq); |
| 3008 | if (!b) { |
| 3009 | ret = -ENOMEM; |
| 3010 | goto done; |
| 3011 | } |
| 3012 | p->locks[level] = BTRFS_READ_LOCK; |
| 3013 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 3014 | } |
| 3015 | ret = 1; |
| 3016 | done: |
| 3017 | if (!p->leave_spinning) |
| 3018 | btrfs_set_path_blocking(p); |
| 3019 | if (ret < 0) |
| 3020 | btrfs_release_path(p); |
| 3021 | |
| 3022 | return ret; |
| 3023 | } |
| 3024 | |
| 3025 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3026 | * helper to use instead of search slot if no exact match is needed but |
| 3027 | * instead the next or previous item should be returned. |
| 3028 | * When find_higher is true, the next higher item is returned, the next lower |
| 3029 | * otherwise. |
| 3030 | * When return_any and find_higher are both true, and no higher item is found, |
| 3031 | * return the next lower instead. |
| 3032 | * When return_any is true and find_higher is false, and no lower item is found, |
| 3033 | * return the next higher instead. |
| 3034 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 3035 | * < 0 on error |
| 3036 | */ |
| 3037 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 3038 | const struct btrfs_key *key, |
| 3039 | struct btrfs_path *p, int find_higher, |
| 3040 | int return_any) |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3041 | { |
| 3042 | int ret; |
| 3043 | struct extent_buffer *leaf; |
| 3044 | |
| 3045 | again: |
| 3046 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 3047 | if (ret <= 0) |
| 3048 | return ret; |
| 3049 | /* |
| 3050 | * a return value of 1 means the path is at the position where the |
| 3051 | * item should be inserted. Normally this is the next bigger item, |
| 3052 | * but in case the previous item is the last in a leaf, path points |
| 3053 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 3054 | * item. |
| 3055 | */ |
| 3056 | leaf = p->nodes[0]; |
| 3057 | |
| 3058 | if (find_higher) { |
| 3059 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 3060 | ret = btrfs_next_leaf(root, p); |
| 3061 | if (ret <= 0) |
| 3062 | return ret; |
| 3063 | if (!return_any) |
| 3064 | return 1; |
| 3065 | /* |
| 3066 | * no higher item found, return the next |
| 3067 | * lower instead |
| 3068 | */ |
| 3069 | return_any = 0; |
| 3070 | find_higher = 0; |
| 3071 | btrfs_release_path(p); |
| 3072 | goto again; |
| 3073 | } |
| 3074 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3075 | if (p->slots[0] == 0) { |
| 3076 | ret = btrfs_prev_leaf(root, p); |
| 3077 | if (ret < 0) |
| 3078 | return ret; |
| 3079 | if (!ret) { |
Filipe David Borba Manana | 23c6bf6 | 2014-01-11 21:28:54 +0000 | [diff] [blame] | 3080 | leaf = p->nodes[0]; |
| 3081 | if (p->slots[0] == btrfs_header_nritems(leaf)) |
| 3082 | p->slots[0]--; |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3083 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3084 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3085 | if (!return_any) |
| 3086 | return 1; |
| 3087 | /* |
| 3088 | * no lower item found, return the next |
| 3089 | * higher instead |
| 3090 | */ |
| 3091 | return_any = 0; |
| 3092 | find_higher = 1; |
| 3093 | btrfs_release_path(p); |
| 3094 | goto again; |
| 3095 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3096 | --p->slots[0]; |
| 3097 | } |
| 3098 | } |
| 3099 | return 0; |
| 3100 | } |
| 3101 | |
| 3102 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3103 | * adjust the pointers going up the tree, starting at level |
| 3104 | * making sure the right key of each node is points to 'key'. |
| 3105 | * This is used after shifting pointers to the left, so it stops |
| 3106 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 3107 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3108 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3109 | */ |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 3110 | static void fixup_low_keys(struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3111 | struct btrfs_disk_key *key, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3112 | { |
| 3113 | int i; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3114 | struct extent_buffer *t; |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 3115 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3116 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 3117 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3118 | int tslot = path->slots[i]; |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 3119 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3120 | if (!path->nodes[i]) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3121 | break; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3122 | t = path->nodes[i]; |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame] | 3123 | ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE, |
| 3124 | GFP_ATOMIC); |
| 3125 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3126 | btrfs_set_node_key(t, key, tslot); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3127 | btrfs_mark_buffer_dirty(path->nodes[i]); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3128 | if (tslot != 0) |
| 3129 | break; |
| 3130 | } |
| 3131 | } |
| 3132 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3133 | /* |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3134 | * update item key. |
| 3135 | * |
| 3136 | * This function isn't completely safe. It's the caller's responsibility |
| 3137 | * that the new key won't break the order |
| 3138 | */ |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 3139 | void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info, |
| 3140 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 3141 | const struct btrfs_key *new_key) |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3142 | { |
| 3143 | struct btrfs_disk_key disk_key; |
| 3144 | struct extent_buffer *eb; |
| 3145 | int slot; |
| 3146 | |
| 3147 | eb = path->nodes[0]; |
| 3148 | slot = path->slots[0]; |
| 3149 | if (slot > 0) { |
| 3150 | btrfs_item_key(eb, &disk_key, slot - 1); |
Qu Wenruo | 7c15d41 | 2019-04-25 08:55:53 +0800 | [diff] [blame] | 3151 | if (unlikely(comp_keys(&disk_key, new_key) >= 0)) { |
| 3152 | btrfs_crit(fs_info, |
| 3153 | "slot %u key (%llu %u %llu) new key (%llu %u %llu)", |
| 3154 | slot, btrfs_disk_key_objectid(&disk_key), |
| 3155 | btrfs_disk_key_type(&disk_key), |
| 3156 | btrfs_disk_key_offset(&disk_key), |
| 3157 | new_key->objectid, new_key->type, |
| 3158 | new_key->offset); |
| 3159 | btrfs_print_leaf(eb); |
| 3160 | BUG(); |
| 3161 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3162 | } |
| 3163 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 3164 | btrfs_item_key(eb, &disk_key, slot + 1); |
Qu Wenruo | 7c15d41 | 2019-04-25 08:55:53 +0800 | [diff] [blame] | 3165 | if (unlikely(comp_keys(&disk_key, new_key) <= 0)) { |
| 3166 | btrfs_crit(fs_info, |
| 3167 | "slot %u key (%llu %u %llu) new key (%llu %u %llu)", |
| 3168 | slot, btrfs_disk_key_objectid(&disk_key), |
| 3169 | btrfs_disk_key_type(&disk_key), |
| 3170 | btrfs_disk_key_offset(&disk_key), |
| 3171 | new_key->objectid, new_key->type, |
| 3172 | new_key->offset); |
| 3173 | btrfs_print_leaf(eb); |
| 3174 | BUG(); |
| 3175 | } |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 3179 | btrfs_set_item_key(eb, &disk_key, slot); |
| 3180 | btrfs_mark_buffer_dirty(eb); |
| 3181 | if (slot == 0) |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 3182 | fixup_low_keys(path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | /* |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3186 | * Check key order of two sibling extent buffers. |
| 3187 | * |
| 3188 | * Return true if something is wrong. |
| 3189 | * Return false if everything is fine. |
| 3190 | * |
| 3191 | * Tree-checker only works inside one tree block, thus the following |
| 3192 | * corruption can not be detected by tree-checker: |
| 3193 | * |
| 3194 | * Leaf @left | Leaf @right |
| 3195 | * -------------------------------------------------------------- |
| 3196 | * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 | |
| 3197 | * |
| 3198 | * Key f6 in leaf @left itself is valid, but not valid when the next |
| 3199 | * key in leaf @right is 7. |
| 3200 | * This can only be checked at tree block merge time. |
| 3201 | * And since tree checker has ensured all key order in each tree block |
| 3202 | * is correct, we only need to bother the last key of @left and the first |
| 3203 | * key of @right. |
| 3204 | */ |
| 3205 | static bool check_sibling_keys(struct extent_buffer *left, |
| 3206 | struct extent_buffer *right) |
| 3207 | { |
| 3208 | struct btrfs_key left_last; |
| 3209 | struct btrfs_key right_first; |
| 3210 | int level = btrfs_header_level(left); |
| 3211 | int nr_left = btrfs_header_nritems(left); |
| 3212 | int nr_right = btrfs_header_nritems(right); |
| 3213 | |
| 3214 | /* No key to check in one of the tree blocks */ |
| 3215 | if (!nr_left || !nr_right) |
| 3216 | return false; |
| 3217 | |
| 3218 | if (level) { |
| 3219 | btrfs_node_key_to_cpu(left, &left_last, nr_left - 1); |
| 3220 | btrfs_node_key_to_cpu(right, &right_first, 0); |
| 3221 | } else { |
| 3222 | btrfs_item_key_to_cpu(left, &left_last, nr_left - 1); |
| 3223 | btrfs_item_key_to_cpu(right, &right_first, 0); |
| 3224 | } |
| 3225 | |
| 3226 | if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) { |
| 3227 | btrfs_crit(left->fs_info, |
| 3228 | "bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)", |
| 3229 | left_last.objectid, left_last.type, |
| 3230 | left_last.offset, right_first.objectid, |
| 3231 | right_first.type, right_first.offset); |
| 3232 | return true; |
| 3233 | } |
| 3234 | return false; |
| 3235 | } |
| 3236 | |
| 3237 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3238 | * 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] | 3239 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3240 | * |
| 3241 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 3242 | * 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] | 3243 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3244 | static int push_node_left(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3245 | struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3246 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3247 | { |
David Sterba | d30a668 | 2019-03-20 14:16:45 +0100 | [diff] [blame] | 3248 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3249 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3250 | int src_nritems; |
| 3251 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3252 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3253 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3254 | src_nritems = btrfs_header_nritems(src); |
| 3255 | dst_nritems = btrfs_header_nritems(dst); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3256 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3257 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3258 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3259 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3260 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3261 | return 1; |
| 3262 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3263 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3264 | return 1; |
| 3265 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3266 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3267 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3268 | if (push_items < src_nritems) { |
| 3269 | /* leave at least 8 pointers in the node if |
| 3270 | * we aren't going to empty it |
| 3271 | */ |
| 3272 | if (src_nritems - push_items < 8) { |
| 3273 | if (push_items <= 8) |
| 3274 | return 1; |
| 3275 | push_items -= 8; |
| 3276 | } |
| 3277 | } |
| 3278 | } else |
| 3279 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3280 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3281 | /* dst is the left eb, src is the middle eb */ |
| 3282 | if (check_sibling_keys(dst, src)) { |
| 3283 | ret = -EUCLEAN; |
| 3284 | btrfs_abort_transaction(trans, ret); |
| 3285 | return ret; |
| 3286 | } |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 3287 | 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] | 3288 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3289 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3290 | return ret; |
| 3291 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3292 | copy_extent_buffer(dst, src, |
| 3293 | btrfs_node_key_ptr_offset(dst_nritems), |
| 3294 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3295 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3296 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3297 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3298 | /* |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3299 | * Don't call tree_mod_log_insert_move here, key removal was |
| 3300 | * already fully logged by tree_mod_log_eb_copy above. |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3301 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3302 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 3303 | btrfs_node_key_ptr_offset(push_items), |
| 3304 | (src_nritems - push_items) * |
| 3305 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3306 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3307 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3308 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 3309 | btrfs_mark_buffer_dirty(src); |
| 3310 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3311 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3312 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3313 | } |
| 3314 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3315 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3316 | * try to push data from one node into the next node right in the |
| 3317 | * tree. |
| 3318 | * |
| 3319 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 3320 | * error, and > 0 if there was no room in the right hand block. |
| 3321 | * |
| 3322 | * this will only push up to 1/2 the contents of the left node over |
| 3323 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3324 | static int balance_node_right(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3325 | struct extent_buffer *dst, |
| 3326 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3327 | { |
David Sterba | 55d32ed | 2019-03-20 14:18:06 +0100 | [diff] [blame] | 3328 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3329 | int push_items = 0; |
| 3330 | int max_push; |
| 3331 | int src_nritems; |
| 3332 | int dst_nritems; |
| 3333 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3334 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3335 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3336 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3337 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3338 | src_nritems = btrfs_header_nritems(src); |
| 3339 | dst_nritems = btrfs_header_nritems(dst); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3340 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3341 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3342 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3343 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3344 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3345 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3346 | |
| 3347 | max_push = src_nritems / 2 + 1; |
| 3348 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3349 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3350 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3351 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3352 | if (max_push < push_items) |
| 3353 | push_items = max_push; |
| 3354 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3355 | /* dst is the right eb, src is the middle eb */ |
| 3356 | if (check_sibling_keys(src, dst)) { |
| 3357 | ret = -EUCLEAN; |
| 3358 | btrfs_abort_transaction(trans, ret); |
| 3359 | return ret; |
| 3360 | } |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3361 | ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems); |
| 3362 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3363 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3364 | btrfs_node_key_ptr_offset(0), |
| 3365 | (dst_nritems) * |
| 3366 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3367 | |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 3368 | ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items, |
| 3369 | push_items); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3370 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3371 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3372 | return ret; |
| 3373 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3374 | copy_extent_buffer(dst, src, |
| 3375 | btrfs_node_key_ptr_offset(0), |
| 3376 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3377 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3378 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3379 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3380 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3381 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3382 | btrfs_mark_buffer_dirty(src); |
| 3383 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3384 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3385 | return ret; |
| 3386 | } |
| 3387 | |
| 3388 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3389 | * helper function to insert a new root level in the tree. |
| 3390 | * A new node is allocated, and a single item is inserted to |
| 3391 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3392 | * |
| 3393 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3394 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3395 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3396 | struct btrfs_root *root, |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3397 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3398 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3399 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3400 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3401 | struct extent_buffer *lower; |
| 3402 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3403 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3404 | struct btrfs_disk_key lower_key; |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 3405 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3406 | |
| 3407 | BUG_ON(path->nodes[level]); |
| 3408 | BUG_ON(path->nodes[level-1] != root->node); |
| 3409 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3410 | lower = path->nodes[level-1]; |
| 3411 | if (level == 1) |
| 3412 | btrfs_item_key(lower, &lower_key, 0); |
| 3413 | else |
| 3414 | btrfs_node_key(lower, &lower_key, 0); |
| 3415 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 3416 | 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] | 3417 | root->node->start, 0, |
Josef Bacik | cf6f34a | 2020-08-20 11:46:07 -0400 | [diff] [blame] | 3418 | BTRFS_NESTING_NEW_ROOT); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3419 | if (IS_ERR(c)) |
| 3420 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3421 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3422 | root_add_used(root, fs_info->nodesize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3423 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3424 | btrfs_set_header_nritems(c, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3425 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3426 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3427 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3428 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3429 | |
| 3430 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3431 | |
| 3432 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3433 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3434 | old = root->node; |
David Sterba | d9d19a0 | 2018-03-05 16:35:29 +0100 | [diff] [blame] | 3435 | ret = tree_mod_log_insert_root(root->node, c, 0); |
| 3436 | BUG_ON(ret < 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3437 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3438 | |
| 3439 | /* the super has an extra ref to root->node */ |
| 3440 | free_extent_buffer(old); |
| 3441 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3442 | add_root_to_dirty_list(root); |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 3443 | atomic_inc(&c->refs); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3444 | path->nodes[level] = c; |
chandan | 95449a1 | 2015-01-15 12:22:03 +0530 | [diff] [blame] | 3445 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3446 | path->slots[level] = 0; |
| 3447 | return 0; |
| 3448 | } |
| 3449 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3450 | /* |
| 3451 | * worker function to insert a single pointer in a node. |
| 3452 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3453 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3454 | * slot and level indicate where you want the key to go, and |
| 3455 | * blocknr is the block the key points to. |
| 3456 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3457 | static void insert_ptr(struct btrfs_trans_handle *trans, |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 3458 | struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3459 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3460 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3461 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3462 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3463 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3464 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3465 | |
| 3466 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3467 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3468 | lower = path->nodes[level]; |
| 3469 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3470 | BUG_ON(slot > nritems); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 3471 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3472 | if (slot != nritems) { |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3473 | if (level) { |
| 3474 | ret = tree_mod_log_insert_move(lower, slot + 1, slot, |
David Sterba | a446a97 | 2018-03-05 15:26:29 +0100 | [diff] [blame] | 3475 | nritems - slot); |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3476 | BUG_ON(ret < 0); |
| 3477 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3478 | memmove_extent_buffer(lower, |
| 3479 | btrfs_node_key_ptr_offset(slot + 1), |
| 3480 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3481 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3482 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3483 | if (level) { |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 3484 | ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD, |
| 3485 | GFP_NOFS); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3486 | BUG_ON(ret < 0); |
| 3487 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3488 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3489 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3490 | WARN_ON(trans->transid == 0); |
| 3491 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3492 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3493 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3494 | } |
| 3495 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3496 | /* |
| 3497 | * split the node at the specified level in path in two. |
| 3498 | * The path is corrected to point to the appropriate node after the split |
| 3499 | * |
| 3500 | * Before splitting this tries to make some room in the node by pushing |
| 3501 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3502 | * |
| 3503 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3504 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3505 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3506 | struct btrfs_root *root, |
| 3507 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3508 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3509 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3510 | struct extent_buffer *c; |
| 3511 | struct extent_buffer *split; |
| 3512 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3513 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3514 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3515 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3516 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3517 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3518 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3519 | if (c == root->node) { |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3520 | /* |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3521 | * trying to split the root, lets make a new one |
| 3522 | * |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3523 | * tree mod log: We don't log_removal old root in |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3524 | * insert_new_root, because that root buffer will be kept as a |
| 3525 | * normal node. We are going to log removal of half of the |
| 3526 | * elements below with tree_mod_log_eb_copy. We're holding a |
| 3527 | * tree lock on the buffer, which is why we cannot race with |
| 3528 | * other tree_mod_log users. |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3529 | */ |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3530 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3531 | if (ret) |
| 3532 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3533 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3534 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3535 | c = path->nodes[level]; |
| 3536 | if (!ret && btrfs_header_nritems(c) < |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3537 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3538 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3539 | if (ret < 0) |
| 3540 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3541 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3542 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3543 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3544 | mid = (c_nritems + 1) / 2; |
| 3545 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3546 | |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 3547 | 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] | 3548 | c->start, 0, BTRFS_NESTING_SPLIT); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3549 | if (IS_ERR(split)) |
| 3550 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3551 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3552 | root_add_used(root, fs_info->nodesize); |
Nikolay Borisov | bc877d2 | 2018-06-18 14:13:19 +0300 | [diff] [blame] | 3553 | ASSERT(btrfs_header_level(c) == level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3554 | |
David Sterba | ed874f0 | 2019-03-20 14:22:04 +0100 | [diff] [blame] | 3555 | 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] | 3556 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3557 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3558 | return ret; |
| 3559 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3560 | copy_extent_buffer(split, c, |
| 3561 | btrfs_node_key_ptr_offset(0), |
| 3562 | btrfs_node_key_ptr_offset(mid), |
| 3563 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3564 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3565 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3566 | ret = 0; |
| 3567 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3568 | btrfs_mark_buffer_dirty(c); |
| 3569 | btrfs_mark_buffer_dirty(split); |
| 3570 | |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 3571 | insert_ptr(trans, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3572 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3573 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3574 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3575 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3576 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3577 | free_extent_buffer(c); |
| 3578 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3579 | path->slots[level + 1] += 1; |
| 3580 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3581 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3582 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3583 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3584 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3585 | } |
| 3586 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3587 | /* |
| 3588 | * how many bytes are required to store the items in a leaf. start |
| 3589 | * and nr indicate which items in the leaf to check. This totals up the |
| 3590 | * space used both by the item structs and the item data |
| 3591 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3592 | 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] | 3593 | { |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3594 | struct btrfs_item *start_item; |
| 3595 | struct btrfs_item *end_item; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3596 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3597 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3598 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3599 | |
| 3600 | if (!nr) |
| 3601 | return 0; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3602 | start_item = btrfs_item_nr(start); |
| 3603 | end_item = btrfs_item_nr(end); |
David Sterba | a31356b | 2020-04-29 22:56:01 +0200 | [diff] [blame] | 3604 | data_len = btrfs_item_offset(l, start_item) + |
| 3605 | btrfs_item_size(l, start_item); |
| 3606 | data_len = data_len - btrfs_item_offset(l, end_item); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3607 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3608 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3609 | return data_len; |
| 3610 | } |
| 3611 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3612 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3613 | * The space between the end of the leaf items and |
| 3614 | * the start of the leaf data. IOW, how much room |
| 3615 | * the leaf has left for both items and data |
| 3616 | */ |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3617 | noinline int btrfs_leaf_free_space(struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3618 | { |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3619 | struct btrfs_fs_info *fs_info = leaf->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3620 | int nritems = btrfs_header_nritems(leaf); |
| 3621 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3622 | |
| 3623 | 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] | 3624 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3625 | btrfs_crit(fs_info, |
| 3626 | "leaf free space ret %d, leaf data size %lu, used %d nritems %d", |
| 3627 | ret, |
| 3628 | (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), |
| 3629 | leaf_space_used(leaf, 0, nritems), nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3630 | } |
| 3631 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3632 | } |
| 3633 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3634 | /* |
| 3635 | * min slot controls the lowest index we're willing to push to the |
| 3636 | * right. We'll push up to and including min_slot, but no lower |
| 3637 | */ |
David Sterba | f72f001 | 2019-03-20 14:39:45 +0100 | [diff] [blame] | 3638 | static noinline int __push_leaf_right(struct btrfs_path *path, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3639 | int data_size, int empty, |
| 3640 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3641 | int free_space, u32 left_nritems, |
| 3642 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3643 | { |
David Sterba | f72f001 | 2019-03-20 14:39:45 +0100 | [diff] [blame] | 3644 | struct btrfs_fs_info *fs_info = right->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3645 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3646 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3647 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3648 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3649 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3650 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3651 | int push_space = 0; |
| 3652 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3653 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3654 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3655 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3656 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3657 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3658 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3659 | if (empty) |
| 3660 | nr = 0; |
| 3661 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3662 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3663 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3664 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3665 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3666 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3667 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3668 | i = left_nritems - 1; |
| 3669 | while (i >= nr) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3670 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3671 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3672 | if (!empty && push_items > 0) { |
| 3673 | if (path->slots[0] > i) |
| 3674 | break; |
| 3675 | if (path->slots[0] == i) { |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3676 | int space = btrfs_leaf_free_space(left); |
| 3677 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3678 | if (space + push_space * 2 > free_space) |
| 3679 | break; |
| 3680 | } |
| 3681 | } |
| 3682 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3683 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3684 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3685 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3686 | this_item_size = btrfs_item_size(left, item); |
| 3687 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3688 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3689 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3690 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3691 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3692 | if (i == 0) |
| 3693 | break; |
| 3694 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3695 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3696 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3697 | if (push_items == 0) |
| 3698 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3699 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 3700 | WARN_ON(!empty && push_items == left_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3701 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3702 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3703 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3704 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3705 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3706 | push_space -= leaf_data_end(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3707 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3708 | /* make room in the right data area */ |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3709 | data_end = leaf_data_end(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3710 | memmove_extent_buffer(right, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3711 | BTRFS_LEAF_DATA_OFFSET + data_end - push_space, |
| 3712 | BTRFS_LEAF_DATA_OFFSET + data_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3713 | BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3714 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3715 | /* copy from the left data area */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3716 | copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3717 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3718 | BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3719 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3720 | |
| 3721 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3722 | btrfs_item_nr_offset(0), |
| 3723 | right_nritems * sizeof(struct btrfs_item)); |
| 3724 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3725 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3726 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3727 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3728 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3729 | |
| 3730 | /* update the item pointers */ |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3731 | btrfs_init_map_token(&token, right); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3732 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3733 | btrfs_set_header_nritems(right, right_nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3734 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3735 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3736 | item = btrfs_item_nr(i); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3737 | push_space -= btrfs_token_item_size(&token, item); |
| 3738 | btrfs_set_token_item_offset(&token, item, push_space); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3739 | } |
| 3740 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3741 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3742 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3743 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3744 | if (left_nritems) |
| 3745 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3746 | else |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 3747 | btrfs_clean_tree_block(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3748 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3749 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3750 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3751 | btrfs_item_key(right, &disk_key, 0); |
| 3752 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3753 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3754 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3755 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3756 | if (path->slots[0] >= left_nritems) { |
| 3757 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3758 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 3759 | btrfs_clean_tree_block(path->nodes[0]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3760 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3761 | free_extent_buffer(path->nodes[0]); |
| 3762 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3763 | path->slots[1] += 1; |
| 3764 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3765 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3766 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3767 | } |
| 3768 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3769 | |
| 3770 | out_unlock: |
| 3771 | btrfs_tree_unlock(right); |
| 3772 | free_extent_buffer(right); |
| 3773 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3774 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3775 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3776 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3777 | * push some data in the path leaf to the right, trying to free up at |
| 3778 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3779 | * |
| 3780 | * returns 1 if the push failed because the other node didn't have enough |
| 3781 | * 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] | 3782 | * |
| 3783 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3784 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3785 | */ |
| 3786 | 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] | 3787 | *root, struct btrfs_path *path, |
| 3788 | int min_data_size, int data_size, |
| 3789 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3790 | { |
| 3791 | struct extent_buffer *left = path->nodes[0]; |
| 3792 | struct extent_buffer *right; |
| 3793 | struct extent_buffer *upper; |
| 3794 | int slot; |
| 3795 | int free_space; |
| 3796 | u32 left_nritems; |
| 3797 | int ret; |
| 3798 | |
| 3799 | if (!path->nodes[1]) |
| 3800 | return 1; |
| 3801 | |
| 3802 | slot = path->slots[1]; |
| 3803 | upper = path->nodes[1]; |
| 3804 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3805 | return 1; |
| 3806 | |
| 3807 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3808 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 3809 | right = btrfs_read_node_slot(upper, slot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 3810 | /* |
| 3811 | * slot + 1 is not valid or we fail to read the right node, |
| 3812 | * no big deal, just return. |
| 3813 | */ |
| 3814 | if (IS_ERR(right)) |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3815 | return 1; |
| 3816 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 3817 | __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 3818 | btrfs_set_lock_blocking_write(right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3819 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3820 | free_space = btrfs_leaf_free_space(right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3821 | if (free_space < data_size) |
| 3822 | goto out_unlock; |
| 3823 | |
| 3824 | /* cow and double check */ |
| 3825 | ret = btrfs_cow_block(trans, root, right, upper, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 3826 | slot + 1, &right, BTRFS_NESTING_RIGHT_COW); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3827 | if (ret) |
| 3828 | goto out_unlock; |
| 3829 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3830 | free_space = btrfs_leaf_free_space(right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3831 | if (free_space < data_size) |
| 3832 | goto out_unlock; |
| 3833 | |
| 3834 | left_nritems = btrfs_header_nritems(left); |
| 3835 | if (left_nritems == 0) |
| 3836 | goto out_unlock; |
| 3837 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 3838 | if (check_sibling_keys(left, right)) { |
| 3839 | ret = -EUCLEAN; |
| 3840 | btrfs_tree_unlock(right); |
| 3841 | free_extent_buffer(right); |
| 3842 | return ret; |
| 3843 | } |
Filipe David Borba Manana | 2ef1fed | 2013-12-04 22:17:39 +0000 | [diff] [blame] | 3844 | if (path->slots[0] == left_nritems && !empty) { |
| 3845 | /* Key greater than all keys in the leaf, right neighbor has |
| 3846 | * enough room for it and we're not emptying our leaf to delete |
| 3847 | * it, therefore use right neighbor to insert the new item and |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 3848 | * no need to touch/dirty our left leaf. */ |
Filipe David Borba Manana | 2ef1fed | 2013-12-04 22:17:39 +0000 | [diff] [blame] | 3849 | btrfs_tree_unlock(left); |
| 3850 | free_extent_buffer(left); |
| 3851 | path->nodes[0] = right; |
| 3852 | path->slots[0] = 0; |
| 3853 | path->slots[1]++; |
| 3854 | return 0; |
| 3855 | } |
| 3856 | |
David Sterba | f72f001 | 2019-03-20 14:39:45 +0100 | [diff] [blame] | 3857 | return __push_leaf_right(path, min_data_size, empty, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3858 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3859 | out_unlock: |
| 3860 | btrfs_tree_unlock(right); |
| 3861 | free_extent_buffer(right); |
| 3862 | return 1; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3866 | * push some data in the path leaf to the left, trying to free up at |
| 3867 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3868 | * |
| 3869 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3870 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3871 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3872 | */ |
David Sterba | 8087c19 | 2019-03-20 14:40:41 +0100 | [diff] [blame] | 3873 | 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] | 3874 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3875 | int free_space, u32 right_nritems, |
| 3876 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3877 | { |
David Sterba | 8087c19 | 2019-03-20 14:40:41 +0100 | [diff] [blame] | 3878 | struct btrfs_fs_info *fs_info = left->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3879 | struct btrfs_disk_key disk_key; |
| 3880 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3881 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3882 | int push_space = 0; |
| 3883 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3884 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3885 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3886 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3887 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3888 | u32 this_item_size; |
| 3889 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3890 | struct btrfs_map_token token; |
| 3891 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3892 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3893 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3894 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3895 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3896 | |
| 3897 | for (i = 0; i < nr; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3898 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3899 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3900 | if (!empty && push_items > 0) { |
| 3901 | if (path->slots[0] < i) |
| 3902 | break; |
| 3903 | if (path->slots[0] == i) { |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 3904 | int space = btrfs_leaf_free_space(right); |
| 3905 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3906 | if (space + push_space * 2 > free_space) |
| 3907 | break; |
| 3908 | } |
| 3909 | } |
| 3910 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3911 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3912 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3913 | |
| 3914 | this_item_size = btrfs_item_size(right, item); |
| 3915 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3916 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3917 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3918 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3919 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3920 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3921 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3922 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3923 | ret = 1; |
| 3924 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3925 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3926 | WARN_ON(!empty && push_items == btrfs_header_nritems(right)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3927 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3928 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3929 | copy_extent_buffer(left, right, |
| 3930 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3931 | btrfs_item_nr_offset(0), |
| 3932 | push_items * sizeof(struct btrfs_item)); |
| 3933 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3934 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3935 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3936 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3937 | copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET + |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3938 | leaf_data_end(left) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3939 | BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3940 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3941 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3942 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3943 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3944 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3945 | btrfs_init_map_token(&token, left); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3946 | 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] | 3947 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3948 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3949 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3950 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3951 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3952 | ioff = btrfs_token_item_offset(&token, item); |
| 3953 | btrfs_set_token_item_offset(&token, item, |
| 3954 | ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3955 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3956 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3957 | |
| 3958 | /* fixup right node */ |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3959 | if (push_items > right_nritems) |
| 3960 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3961 | right_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3962 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3963 | if (push_items < right_nritems) { |
| 3964 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3965 | leaf_data_end(right); |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3966 | memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3967 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3968 | BTRFS_LEAF_DATA_OFFSET + |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 3969 | leaf_data_end(right), push_space); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3970 | |
| 3971 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3972 | btrfs_item_nr_offset(push_items), |
| 3973 | (btrfs_header_nritems(right) - push_items) * |
| 3974 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3975 | } |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 3976 | |
| 3977 | btrfs_init_map_token(&token, right); |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3978 | right_nritems -= push_items; |
| 3979 | btrfs_set_header_nritems(right, right_nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3980 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3981 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3982 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3983 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 3984 | push_space = push_space - btrfs_token_item_size(&token, item); |
| 3985 | btrfs_set_token_item_offset(&token, item, push_space); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3986 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3987 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3988 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3989 | if (right_nritems) |
| 3990 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3991 | else |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 3992 | btrfs_clean_tree_block(right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3993 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3994 | btrfs_item_key(right, &disk_key, 0); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 3995 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3996 | |
| 3997 | /* then fixup the leaf pointer in the path */ |
| 3998 | if (path->slots[0] < push_items) { |
| 3999 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4000 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4001 | free_extent_buffer(path->nodes[0]); |
| 4002 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4003 | path->slots[1] -= 1; |
| 4004 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4005 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4006 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4007 | path->slots[0] -= push_items; |
| 4008 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4009 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4010 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4011 | out: |
| 4012 | btrfs_tree_unlock(left); |
| 4013 | free_extent_buffer(left); |
| 4014 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4015 | } |
| 4016 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4017 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4018 | * push some data in the path leaf to the left, trying to free up at |
| 4019 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4020 | * |
| 4021 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 4022 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 4023 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4024 | */ |
| 4025 | 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] | 4026 | *root, struct btrfs_path *path, int min_data_size, |
| 4027 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4028 | { |
| 4029 | struct extent_buffer *right = path->nodes[0]; |
| 4030 | struct extent_buffer *left; |
| 4031 | int slot; |
| 4032 | int free_space; |
| 4033 | u32 right_nritems; |
| 4034 | int ret = 0; |
| 4035 | |
| 4036 | slot = path->slots[1]; |
| 4037 | if (slot == 0) |
| 4038 | return 1; |
| 4039 | if (!path->nodes[1]) |
| 4040 | return 1; |
| 4041 | |
| 4042 | right_nritems = btrfs_header_nritems(right); |
| 4043 | if (right_nritems == 0) |
| 4044 | return 1; |
| 4045 | |
| 4046 | btrfs_assert_tree_locked(path->nodes[1]); |
| 4047 | |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 4048 | left = btrfs_read_node_slot(path->nodes[1], slot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 4049 | /* |
| 4050 | * slot - 1 is not valid or we fail to read the left node, |
| 4051 | * no big deal, just return. |
| 4052 | */ |
| 4053 | if (IS_ERR(left)) |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 4054 | return 1; |
| 4055 | |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 4056 | __btrfs_tree_lock(left, BTRFS_NESTING_LEFT); |
David Sterba | 8bead25 | 2018-04-04 02:03:48 +0200 | [diff] [blame] | 4057 | btrfs_set_lock_blocking_write(left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4058 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4059 | free_space = btrfs_leaf_free_space(left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4060 | if (free_space < data_size) { |
| 4061 | ret = 1; |
| 4062 | goto out; |
| 4063 | } |
| 4064 | |
| 4065 | /* cow and double check */ |
| 4066 | ret = btrfs_cow_block(trans, root, left, |
Josef Bacik | 9631e4c | 2020-08-20 11:46:03 -0400 | [diff] [blame] | 4067 | path->nodes[1], slot - 1, &left, |
Josef Bacik | bf59a5a | 2020-08-20 11:46:05 -0400 | [diff] [blame] | 4068 | BTRFS_NESTING_LEFT_COW); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4069 | if (ret) { |
| 4070 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4071 | if (ret == -ENOSPC) |
| 4072 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4073 | goto out; |
| 4074 | } |
| 4075 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4076 | free_space = btrfs_leaf_free_space(left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4077 | if (free_space < data_size) { |
| 4078 | ret = 1; |
| 4079 | goto out; |
| 4080 | } |
| 4081 | |
Qu Wenruo | d16c702 | 2020-08-19 14:35:50 +0800 | [diff] [blame] | 4082 | if (check_sibling_keys(left, right)) { |
| 4083 | ret = -EUCLEAN; |
| 4084 | goto out; |
| 4085 | } |
David Sterba | 8087c19 | 2019-03-20 14:40:41 +0100 | [diff] [blame] | 4086 | return __push_leaf_left(path, min_data_size, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4087 | empty, left, free_space, right_nritems, |
| 4088 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4089 | out: |
| 4090 | btrfs_tree_unlock(left); |
| 4091 | free_extent_buffer(left); |
| 4092 | return ret; |
| 4093 | } |
| 4094 | |
| 4095 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4096 | * split the path's leaf in two, making sure there is at least data_size |
| 4097 | * available for the resulting leaf level of the path. |
| 4098 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4099 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4100 | struct btrfs_path *path, |
| 4101 | struct extent_buffer *l, |
| 4102 | struct extent_buffer *right, |
| 4103 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4104 | { |
David Sterba | 94f94ad | 2019-03-20 14:42:33 +0100 | [diff] [blame] | 4105 | struct btrfs_fs_info *fs_info = trans->fs_info; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4106 | int data_copy_size; |
| 4107 | int rt_data_off; |
| 4108 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4109 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4110 | struct btrfs_map_token token; |
| 4111 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4112 | nritems = nritems - mid; |
| 4113 | btrfs_set_header_nritems(right, nritems); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4114 | 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] | 4115 | |
| 4116 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 4117 | btrfs_item_nr_offset(mid), |
| 4118 | nritems * sizeof(struct btrfs_item)); |
| 4119 | |
| 4120 | copy_extent_buffer(right, l, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4121 | BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) - |
| 4122 | data_copy_size, BTRFS_LEAF_DATA_OFFSET + |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4123 | leaf_data_end(l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4124 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4125 | 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] | 4126 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4127 | btrfs_init_map_token(&token, right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4128 | for (i = 0; i < nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4129 | struct btrfs_item *item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4130 | u32 ioff; |
| 4131 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4132 | ioff = btrfs_token_item_offset(&token, item); |
| 4133 | btrfs_set_token_item_offset(&token, item, ioff + rt_data_off); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4134 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4135 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4136 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4137 | btrfs_item_key(right, &disk_key, 0); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 4138 | 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] | 4139 | |
| 4140 | btrfs_mark_buffer_dirty(right); |
| 4141 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4142 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4143 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4144 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4145 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4146 | free_extent_buffer(path->nodes[0]); |
| 4147 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4148 | path->slots[0] -= mid; |
| 4149 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4150 | } else { |
| 4151 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4152 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4153 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4154 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4155 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4159 | * double splits happen when we need to insert a big item in the middle |
| 4160 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 4161 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 4162 | * A B C |
| 4163 | * |
| 4164 | * We avoid this by trying to push the items on either side of our target |
| 4165 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 4166 | * completely. |
| 4167 | */ |
| 4168 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 4169 | struct btrfs_root *root, |
| 4170 | struct btrfs_path *path, |
| 4171 | int data_size) |
| 4172 | { |
| 4173 | int ret; |
| 4174 | int progress = 0; |
| 4175 | int slot; |
| 4176 | u32 nritems; |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4177 | int space_needed = data_size; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4178 | |
| 4179 | slot = path->slots[0]; |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4180 | if (slot < btrfs_header_nritems(path->nodes[0])) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4181 | space_needed -= btrfs_leaf_free_space(path->nodes[0]); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4182 | |
| 4183 | /* |
| 4184 | * try to push all the items after our slot into the |
| 4185 | * right leaf |
| 4186 | */ |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4187 | ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4188 | if (ret < 0) |
| 4189 | return ret; |
| 4190 | |
| 4191 | if (ret == 0) |
| 4192 | progress++; |
| 4193 | |
| 4194 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 4195 | /* |
| 4196 | * our goal is to get our slot at the start or end of a leaf. If |
| 4197 | * we've done so we're done |
| 4198 | */ |
| 4199 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 4200 | return 0; |
| 4201 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4202 | if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4203 | return 0; |
| 4204 | |
| 4205 | /* try to push all the items before our slot into the next leaf */ |
| 4206 | slot = path->slots[0]; |
Filipe Manana | 263d399 | 2017-02-17 18:43:57 +0000 | [diff] [blame] | 4207 | space_needed = data_size; |
| 4208 | if (slot > 0) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4209 | space_needed -= btrfs_leaf_free_space(path->nodes[0]); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4210 | ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4211 | if (ret < 0) |
| 4212 | return ret; |
| 4213 | |
| 4214 | if (ret == 0) |
| 4215 | progress++; |
| 4216 | |
| 4217 | if (progress) |
| 4218 | return 0; |
| 4219 | return 1; |
| 4220 | } |
| 4221 | |
| 4222 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4223 | * split the path's leaf in two, making sure there is at least data_size |
| 4224 | * available for the resulting leaf level of the path. |
| 4225 | * |
| 4226 | * returns 0 if all went well and < 0 on failure. |
| 4227 | */ |
| 4228 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 4229 | struct btrfs_root *root, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4230 | const struct btrfs_key *ins_key, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4231 | struct btrfs_path *path, int data_size, |
| 4232 | int extend) |
| 4233 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4234 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4235 | struct extent_buffer *l; |
| 4236 | u32 nritems; |
| 4237 | int mid; |
| 4238 | int slot; |
| 4239 | struct extent_buffer *right; |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 4240 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4241 | int ret = 0; |
| 4242 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4243 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4244 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4245 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4246 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4247 | l = path->nodes[0]; |
| 4248 | slot = path->slots[0]; |
| 4249 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4250 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4251 | return -EOVERFLOW; |
| 4252 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4253 | /* first try to make some room by pushing left and right */ |
Liu Bo | 33157e0 | 2013-05-22 12:07:06 +0000 | [diff] [blame] | 4254 | if (data_size && path->nodes[1]) { |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4255 | int space_needed = data_size; |
| 4256 | |
| 4257 | if (slot < btrfs_header_nritems(l)) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4258 | space_needed -= btrfs_leaf_free_space(l); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4259 | |
| 4260 | wret = push_leaf_right(trans, root, path, space_needed, |
| 4261 | space_needed, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4262 | if (wret < 0) |
| 4263 | return wret; |
| 4264 | if (wret) { |
Filipe Manana | 263d399 | 2017-02-17 18:43:57 +0000 | [diff] [blame] | 4265 | space_needed = data_size; |
| 4266 | if (slot > 0) |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4267 | space_needed -= btrfs_leaf_free_space(l); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4268 | wret = push_leaf_left(trans, root, path, space_needed, |
| 4269 | space_needed, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4270 | if (wret < 0) |
| 4271 | return wret; |
| 4272 | } |
| 4273 | l = path->nodes[0]; |
| 4274 | |
| 4275 | /* did the pushes work? */ |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4276 | if (btrfs_leaf_free_space(l) >= data_size) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4277 | return 0; |
| 4278 | } |
| 4279 | |
| 4280 | if (!path->nodes[1]) { |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 4281 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4282 | if (ret) |
| 4283 | return ret; |
| 4284 | } |
| 4285 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4286 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4287 | l = path->nodes[0]; |
| 4288 | slot = path->slots[0]; |
| 4289 | nritems = btrfs_header_nritems(l); |
| 4290 | mid = (nritems + 1) / 2; |
| 4291 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4292 | if (mid <= slot) { |
| 4293 | if (nritems == 1 || |
| 4294 | leaf_space_used(l, mid, nritems - mid) + data_size > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4295 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4296 | if (slot >= nritems) { |
| 4297 | split = 0; |
| 4298 | } else { |
| 4299 | mid = slot; |
| 4300 | if (mid != nritems && |
| 4301 | leaf_space_used(l, mid, nritems - mid) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4302 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4303 | if (data_size && !tried_avoid_double) |
| 4304 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4305 | split = 2; |
| 4306 | } |
| 4307 | } |
| 4308 | } |
| 4309 | } else { |
| 4310 | if (leaf_space_used(l, 0, mid) + data_size > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4311 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4312 | if (!extend && data_size && slot == 0) { |
| 4313 | split = 0; |
| 4314 | } else if ((extend || !data_size) && slot == 0) { |
| 4315 | mid = 1; |
| 4316 | } else { |
| 4317 | mid = slot; |
| 4318 | if (mid != nritems && |
| 4319 | leaf_space_used(l, mid, nritems - mid) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4320 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4321 | if (data_size && !tried_avoid_double) |
| 4322 | goto push_for_double; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4323 | split = 2; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4324 | } |
| 4325 | } |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | if (split == 0) |
| 4330 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 4331 | else |
| 4332 | btrfs_item_key(l, &disk_key, mid); |
| 4333 | |
Josef Bacik | ca9d473 | 2020-08-20 11:46:08 -0400 | [diff] [blame] | 4334 | /* |
| 4335 | * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double |
| 4336 | * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES |
| 4337 | * subclasses, which is 8 at the time of this patch, and we've maxed it |
| 4338 | * out. In the future we could add a |
| 4339 | * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just |
| 4340 | * use BTRFS_NESTING_NEW_ROOT. |
| 4341 | */ |
Filipe Manana | a627947 | 2019-01-25 11:48:51 +0000 | [diff] [blame] | 4342 | 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] | 4343 | l->start, 0, num_doubles ? |
| 4344 | BTRFS_NESTING_NEW_ROOT : |
| 4345 | BTRFS_NESTING_SPLIT); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4346 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4347 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4348 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4349 | root_add_used(root, fs_info->nodesize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4350 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4351 | if (split == 0) { |
| 4352 | if (mid <= slot) { |
| 4353 | btrfs_set_header_nritems(right, 0); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 4354 | insert_ptr(trans, path, &disk_key, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4355 | right->start, path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4356 | btrfs_tree_unlock(path->nodes[0]); |
| 4357 | free_extent_buffer(path->nodes[0]); |
| 4358 | path->nodes[0] = right; |
| 4359 | path->slots[0] = 0; |
| 4360 | path->slots[1] += 1; |
| 4361 | } else { |
| 4362 | btrfs_set_header_nritems(right, 0); |
David Sterba | 6ad3cf6 | 2019-03-20 14:32:45 +0100 | [diff] [blame] | 4363 | insert_ptr(trans, path, &disk_key, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4364 | right->start, path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4365 | btrfs_tree_unlock(path->nodes[0]); |
| 4366 | free_extent_buffer(path->nodes[0]); |
| 4367 | path->nodes[0] = right; |
| 4368 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4369 | if (path->slots[1] == 0) |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4370 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4371 | } |
Liu Bo | 196e024 | 2016-09-07 14:48:28 -0700 | [diff] [blame] | 4372 | /* |
| 4373 | * We create a new leaf 'right' for the required ins_len and |
| 4374 | * we'll do btrfs_mark_buffer_dirty() on this leaf after copying |
| 4375 | * the content of ins_len to 'right'. |
| 4376 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4377 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4378 | } |
| 4379 | |
David Sterba | 94f94ad | 2019-03-20 14:42:33 +0100 | [diff] [blame] | 4380 | copy_for_split(trans, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4381 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4382 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4383 | BUG_ON(num_doubles != 0); |
| 4384 | num_doubles++; |
| 4385 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4386 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4387 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4388 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4389 | |
| 4390 | push_for_double: |
| 4391 | push_for_double_split(trans, root, path, data_size); |
| 4392 | tried_avoid_double = 1; |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4393 | if (btrfs_leaf_free_space(path->nodes[0]) >= data_size) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4394 | return 0; |
| 4395 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4396 | } |
| 4397 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4398 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4399 | struct btrfs_root *root, |
| 4400 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4401 | { |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4402 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4403 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4404 | struct btrfs_file_extent_item *fi; |
| 4405 | u64 extent_len = 0; |
| 4406 | u32 item_size; |
| 4407 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4408 | |
| 4409 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4410 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4411 | |
| 4412 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4413 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4414 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4415 | if (btrfs_leaf_free_space(leaf) >= ins_len) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4416 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4417 | |
| 4418 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4419 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4420 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4421 | struct btrfs_file_extent_item); |
| 4422 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4423 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4424 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4425 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4426 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4427 | path->search_for_split = 1; |
| 4428 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4429 | path->search_for_split = 0; |
Filipe Manana | a8df6fe | 2015-01-20 12:40:53 +0000 | [diff] [blame] | 4430 | if (ret > 0) |
| 4431 | ret = -EAGAIN; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4432 | if (ret < 0) |
| 4433 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4434 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4435 | ret = -EAGAIN; |
| 4436 | leaf = path->nodes[0]; |
Filipe Manana | a8df6fe | 2015-01-20 12:40:53 +0000 | [diff] [blame] | 4437 | /* if our item isn't there, return now */ |
| 4438 | if (item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4439 | goto err; |
| 4440 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4441 | /* the leaf has changed, it now has room. return now */ |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4442 | if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len) |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4443 | goto err; |
| 4444 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4445 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4446 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4447 | struct btrfs_file_extent_item); |
| 4448 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4449 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4450 | } |
| 4451 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4452 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4453 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4454 | if (ret) |
| 4455 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4456 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4457 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4458 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4459 | return 0; |
| 4460 | err: |
| 4461 | path->keep_locks = 0; |
| 4462 | return ret; |
| 4463 | } |
| 4464 | |
David Sterba | 25263cd | 2019-03-20 14:44:57 +0100 | [diff] [blame] | 4465 | static noinline int split_item(struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4466 | const struct btrfs_key *new_key, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4467 | unsigned long split_offset) |
| 4468 | { |
| 4469 | struct extent_buffer *leaf; |
| 4470 | struct btrfs_item *item; |
| 4471 | struct btrfs_item *new_item; |
| 4472 | int slot; |
| 4473 | char *buf; |
| 4474 | u32 nritems; |
| 4475 | u32 item_size; |
| 4476 | u32 orig_offset; |
| 4477 | struct btrfs_disk_key disk_key; |
| 4478 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4479 | leaf = path->nodes[0]; |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4480 | BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4481 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4482 | btrfs_set_path_blocking(path); |
| 4483 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4484 | item = btrfs_item_nr(path->slots[0]); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4485 | orig_offset = btrfs_item_offset(leaf, item); |
| 4486 | item_size = btrfs_item_size(leaf, item); |
| 4487 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4488 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4489 | if (!buf) |
| 4490 | return -ENOMEM; |
| 4491 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4492 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4493 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4494 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4495 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4496 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4497 | if (slot != nritems) { |
| 4498 | /* shift the items */ |
| 4499 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4500 | btrfs_item_nr_offset(slot), |
| 4501 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4505 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4506 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4507 | new_item = btrfs_item_nr(slot); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4508 | |
| 4509 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4510 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4511 | |
| 4512 | btrfs_set_item_offset(leaf, item, |
| 4513 | orig_offset + item_size - split_offset); |
| 4514 | btrfs_set_item_size(leaf, item, split_offset); |
| 4515 | |
| 4516 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4517 | |
| 4518 | /* write the data for the start of the original item */ |
| 4519 | write_extent_buffer(leaf, buf, |
| 4520 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4521 | split_offset); |
| 4522 | |
| 4523 | /* write the data for the new item */ |
| 4524 | write_extent_buffer(leaf, buf + split_offset, |
| 4525 | btrfs_item_ptr_offset(leaf, slot), |
| 4526 | item_size - split_offset); |
| 4527 | btrfs_mark_buffer_dirty(leaf); |
| 4528 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4529 | BUG_ON(btrfs_leaf_free_space(leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4530 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4531 | return 0; |
| 4532 | } |
| 4533 | |
| 4534 | /* |
| 4535 | * This function splits a single item into two items, |
| 4536 | * giving 'new_key' to the new item and splitting the |
| 4537 | * old one at split_offset (from the start of the item). |
| 4538 | * |
| 4539 | * The path may be released by this operation. After |
| 4540 | * the split, the path is pointing to the old item. The |
| 4541 | * new item is going to be in the same node as the old one. |
| 4542 | * |
| 4543 | * Note, the item being split must be smaller enough to live alone on |
| 4544 | * a tree block with room for one extra struct btrfs_item |
| 4545 | * |
| 4546 | * This allows us to split the item in place, keeping a lock on the |
| 4547 | * leaf the entire time. |
| 4548 | */ |
| 4549 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4550 | struct btrfs_root *root, |
| 4551 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4552 | const struct btrfs_key *new_key, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4553 | unsigned long split_offset) |
| 4554 | { |
| 4555 | int ret; |
| 4556 | ret = setup_leaf_for_split(trans, root, path, |
| 4557 | sizeof(struct btrfs_item)); |
| 4558 | if (ret) |
| 4559 | return ret; |
| 4560 | |
David Sterba | 25263cd | 2019-03-20 14:44:57 +0100 | [diff] [blame] | 4561 | ret = split_item(path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4562 | return ret; |
| 4563 | } |
| 4564 | |
| 4565 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4566 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4567 | * It guarantees both items live in the same tree leaf and the new item |
| 4568 | * is contiguous with the original item. |
| 4569 | * |
| 4570 | * This allows us to split file extent in place, keeping a lock on the |
| 4571 | * leaf the entire time. |
| 4572 | */ |
| 4573 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4574 | struct btrfs_root *root, |
| 4575 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4576 | const struct btrfs_key *new_key) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4577 | { |
| 4578 | struct extent_buffer *leaf; |
| 4579 | int ret; |
| 4580 | u32 item_size; |
| 4581 | |
| 4582 | leaf = path->nodes[0]; |
| 4583 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4584 | ret = setup_leaf_for_split(trans, root, path, |
| 4585 | item_size + sizeof(struct btrfs_item)); |
| 4586 | if (ret) |
| 4587 | return ret; |
| 4588 | |
| 4589 | path->slots[0]++; |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4590 | setup_items_for_insert(root, path, new_key, &item_size, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4591 | leaf = path->nodes[0]; |
| 4592 | memcpy_extent_buffer(leaf, |
| 4593 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4594 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4595 | item_size); |
| 4596 | return 0; |
| 4597 | } |
| 4598 | |
| 4599 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4600 | * make the item pointed to by the path smaller. new_size indicates |
| 4601 | * how small to make it, and from_end tells us if we just chop bytes |
| 4602 | * off the end of the item or if we shift the item to chop bytes off |
| 4603 | * the front. |
| 4604 | */ |
David Sterba | 78ac4f9 | 2019-03-20 14:49:12 +0100 | [diff] [blame] | 4605 | 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] | 4606 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4607 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4608 | struct extent_buffer *leaf; |
| 4609 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4610 | u32 nritems; |
| 4611 | unsigned int data_end; |
| 4612 | unsigned int old_data_start; |
| 4613 | unsigned int old_size; |
| 4614 | unsigned int size_diff; |
| 4615 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4616 | struct btrfs_map_token token; |
| 4617 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4618 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4619 | slot = path->slots[0]; |
| 4620 | |
| 4621 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4622 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4623 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4624 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4625 | nritems = btrfs_header_nritems(leaf); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4626 | data_end = leaf_data_end(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4627 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4628 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4629 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4630 | size_diff = old_size - new_size; |
| 4631 | |
| 4632 | BUG_ON(slot < 0); |
| 4633 | BUG_ON(slot >= nritems); |
| 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 | b18c668 | 2007-04-17 13:26:50 -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 + size_diff); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4646 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4647 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4648 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4649 | if (from_end) { |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4650 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4651 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4652 | data_end, old_data_start + new_size - data_end); |
| 4653 | } else { |
| 4654 | struct btrfs_disk_key disk_key; |
| 4655 | u64 offset; |
| 4656 | |
| 4657 | btrfs_item_key(leaf, &disk_key, slot); |
| 4658 | |
| 4659 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4660 | unsigned long ptr; |
| 4661 | struct btrfs_file_extent_item *fi; |
| 4662 | |
| 4663 | fi = btrfs_item_ptr(leaf, slot, |
| 4664 | struct btrfs_file_extent_item); |
| 4665 | fi = (struct btrfs_file_extent_item *)( |
| 4666 | (unsigned long)fi - size_diff); |
| 4667 | |
| 4668 | if (btrfs_file_extent_type(leaf, fi) == |
| 4669 | BTRFS_FILE_EXTENT_INLINE) { |
| 4670 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4671 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4672 | (unsigned long)fi, |
David Sterba | 7ec20af | 2014-07-24 17:34:58 +0200 | [diff] [blame] | 4673 | BTRFS_FILE_EXTENT_INLINE_DATA_START); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4674 | } |
| 4675 | } |
| 4676 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4677 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4678 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4679 | data_end, old_data_start - data_end); |
| 4680 | |
| 4681 | offset = btrfs_disk_key_offset(&disk_key); |
| 4682 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4683 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4684 | if (slot == 0) |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4685 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4686 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4687 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4688 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | btrfs_set_item_size(leaf, item, new_size); |
| 4690 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4691 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4692 | if (btrfs_leaf_free_space(leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4693 | btrfs_print_leaf(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4694 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4695 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4696 | } |
| 4697 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4698 | /* |
Stefan Behrens | 8f69dbd | 2013-05-07 10:23:30 +0000 | [diff] [blame] | 4699 | * 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] | 4700 | */ |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 4701 | void btrfs_extend_item(struct btrfs_path *path, u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4702 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4703 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4704 | struct extent_buffer *leaf; |
| 4705 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4706 | u32 nritems; |
| 4707 | unsigned int data_end; |
| 4708 | unsigned int old_data; |
| 4709 | unsigned int old_size; |
| 4710 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4711 | struct btrfs_map_token token; |
| 4712 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4713 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4714 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4715 | nritems = btrfs_header_nritems(leaf); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4716 | data_end = leaf_data_end(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4717 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4718 | if (btrfs_leaf_free_space(leaf) < data_size) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4719 | btrfs_print_leaf(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4720 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4721 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4722 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4723 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4724 | |
| 4725 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4726 | if (slot >= nritems) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4727 | btrfs_print_leaf(leaf); |
David Sterba | c71dd88 | 2019-03-20 14:51:10 +0100 | [diff] [blame] | 4728 | btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4729 | slot, nritems); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4730 | BUG(); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4731 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4732 | |
| 4733 | /* |
| 4734 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4735 | */ |
| 4736 | /* first correct the data pointers */ |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4737 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4738 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4739 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4740 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4741 | |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4742 | ioff = btrfs_token_item_offset(&token, item); |
| 4743 | btrfs_set_token_item_offset(&token, item, ioff - data_size); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4744 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4745 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4746 | /* shift the data */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4747 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4748 | data_end - data_size, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4749 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4750 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4751 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4752 | old_size = btrfs_item_size_nr(leaf, slot); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4753 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4754 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4755 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4756 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4757 | if (btrfs_leaf_free_space(leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4758 | btrfs_print_leaf(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4759 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4760 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4761 | } |
| 4762 | |
Nikolay Borisov | da9ffb2 | 2020-09-01 17:40:00 +0300 | [diff] [blame] | 4763 | /** |
| 4764 | * setup_items_for_insert - Helper called before inserting one or more items |
| 4765 | * to a leaf. Main purpose is to save stack depth by doing the bulk of the work |
| 4766 | * in a function that doesn't call btrfs_search_slot |
| 4767 | * |
| 4768 | * @root: root we are inserting items to |
| 4769 | * @path: points to the leaf/slot where we are going to insert new items |
| 4770 | * @cpu_key: array of keys for items to be inserted |
| 4771 | * @data_size: size of the body of each item we are going to insert |
| 4772 | * @nr: size of @cpu_key/@data_size arrays |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4773 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4774 | 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] | 4775 | const struct btrfs_key *cpu_key, u32 *data_size, |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4776 | int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4777 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4778 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4779 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4780 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4781 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4782 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4783 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4784 | struct extent_buffer *leaf; |
| 4785 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4786 | struct btrfs_map_token token; |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4787 | u32 total_size; |
| 4788 | u32 total_data = 0; |
| 4789 | |
| 4790 | for (i = 0; i < nr; i++) |
| 4791 | total_data += data_size[i]; |
| 4792 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4793 | |
Filipe Manana | 24cdc84 | 2014-07-28 19:34:35 +0100 | [diff] [blame] | 4794 | if (path->slots[0] == 0) { |
| 4795 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4796 | fixup_low_keys(path, &disk_key, 1); |
Filipe Manana | 24cdc84 | 2014-07-28 19:34:35 +0100 | [diff] [blame] | 4797 | } |
| 4798 | btrfs_unlock_up_safe(path, 1); |
| 4799 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4800 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4801 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4802 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4803 | nritems = btrfs_header_nritems(leaf); |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 4804 | data_end = leaf_data_end(leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4805 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4806 | if (btrfs_leaf_free_space(leaf) < total_size) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4807 | btrfs_print_leaf(leaf); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4808 | btrfs_crit(fs_info, "not enough freespace need %u have %d", |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4809 | total_size, btrfs_leaf_free_space(leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4810 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4811 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4812 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 4813 | btrfs_init_map_token(&token, leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4814 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4815 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4816 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4817 | if (old_data < data_end) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4818 | btrfs_print_leaf(leaf); |
Nikolay Borisov | 7269ddd | 2020-09-01 17:40:01 +0300 | [diff] [blame] | 4819 | btrfs_crit(fs_info, |
| 4820 | "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] | 4821 | slot, old_data, data_end); |
Arnd Bergmann | 290342f | 2019-03-25 14:02:25 +0100 | [diff] [blame] | 4822 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4823 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4824 | /* |
| 4825 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4826 | */ |
| 4827 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4828 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4829 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4830 | |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 4831 | item = btrfs_item_nr(i); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4832 | ioff = btrfs_token_item_offset(&token, item); |
| 4833 | btrfs_set_token_item_offset(&token, item, |
| 4834 | ioff - total_data); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4835 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4836 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4837 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4838 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4839 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4840 | |
| 4841 | /* shift the data */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4842 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4843 | data_end - total_data, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4844 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4845 | data_end = old_data; |
| 4846 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4847 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4848 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4849 | for (i = 0; i < nr; i++) { |
| 4850 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4851 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4852 | item = btrfs_item_nr(slot + i); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4853 | data_end -= data_size[i]; |
Nikolay Borisov | fc0716c | 2020-09-01 17:39:57 +0300 | [diff] [blame] | 4854 | btrfs_set_token_item_offset(&token, item, data_end); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 4855 | btrfs_set_token_item_size(&token, item, data_size[i]); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4856 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4857 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4858 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4859 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4860 | |
David Sterba | e902baa | 2019-03-20 14:36:46 +0100 | [diff] [blame] | 4861 | if (btrfs_leaf_free_space(leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4862 | btrfs_print_leaf(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4863 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4864 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4865 | } |
| 4866 | |
| 4867 | /* |
| 4868 | * Given a key and some data, insert items into the tree. |
| 4869 | * This does all the path init required, making room in the tree if needed. |
| 4870 | */ |
| 4871 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4872 | struct btrfs_root *root, |
| 4873 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4874 | const struct btrfs_key *cpu_key, u32 *data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4875 | int nr) |
| 4876 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4877 | int ret = 0; |
| 4878 | int slot; |
| 4879 | int i; |
| 4880 | u32 total_size = 0; |
| 4881 | u32 total_data = 0; |
| 4882 | |
| 4883 | for (i = 0; i < nr; i++) |
| 4884 | total_data += data_size[i]; |
| 4885 | |
| 4886 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4887 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4888 | if (ret == 0) |
| 4889 | return -EEXIST; |
| 4890 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4891 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4892 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4893 | slot = path->slots[0]; |
| 4894 | BUG_ON(slot < 0); |
| 4895 | |
Nikolay Borisov | fc0d82e | 2020-09-01 17:39:59 +0300 | [diff] [blame] | 4896 | setup_items_for_insert(root, path, cpu_key, data_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4897 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | /* |
| 4901 | * Given a key and some data, insert an item into the tree. |
| 4902 | * This does all the path init required, making room in the tree if needed. |
| 4903 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4904 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4905 | const struct btrfs_key *cpu_key, void *data, |
| 4906 | u32 data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4907 | { |
| 4908 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4909 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4910 | struct extent_buffer *leaf; |
| 4911 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4912 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4913 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4914 | if (!path) |
| 4915 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4916 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4917 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4918 | leaf = path->nodes[0]; |
| 4919 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4920 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4921 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4922 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4923 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4924 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4925 | } |
| 4926 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4927 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4928 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4929 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4930 | * the tree should have been previously balanced so the deletion does not |
| 4931 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4932 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4933 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 4934 | int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4935 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4936 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4937 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4938 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4939 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4940 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4941 | if (slot != nritems - 1) { |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 4942 | if (level) { |
| 4943 | ret = tree_mod_log_insert_move(parent, slot, slot + 1, |
David Sterba | a446a97 | 2018-03-05 15:26:29 +0100 | [diff] [blame] | 4944 | nritems - slot - 1); |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 4945 | BUG_ON(ret < 0); |
| 4946 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4947 | memmove_extent_buffer(parent, |
| 4948 | btrfs_node_key_ptr_offset(slot), |
| 4949 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4950 | sizeof(struct btrfs_key_ptr) * |
| 4951 | (nritems - slot - 1)); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4952 | } else if (level) { |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 4953 | ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE, |
| 4954 | GFP_NOFS); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4955 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4956 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4957 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4958 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4959 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4960 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4961 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4962 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4963 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4964 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4965 | struct btrfs_disk_key disk_key; |
| 4966 | |
| 4967 | btrfs_node_key(parent, &disk_key, 0); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 4968 | fixup_low_keys(path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4969 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4970 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4971 | } |
| 4972 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4973 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4974 | * 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] | 4975 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4976 | * |
| 4977 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4978 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4979 | * |
| 4980 | * The path must have already been setup for deleting the leaf, including |
| 4981 | * all the proper balancing. path->nodes[1] must be locked. |
| 4982 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4983 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4984 | struct btrfs_root *root, |
| 4985 | struct btrfs_path *path, |
| 4986 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4987 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4988 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4989 | del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4990 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4991 | /* |
| 4992 | * btrfs_free_extent is expensive, we want to make sure we |
| 4993 | * aren't holding any locks when we call it |
| 4994 | */ |
| 4995 | btrfs_unlock_up_safe(path, 0); |
| 4996 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4997 | root_sub_used(root, leaf->len); |
| 4998 | |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 4999 | atomic_inc(&leaf->refs); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 5000 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 5001 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 5002 | } |
| 5003 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 5004 | * delete the item at the leaf level in path. If that empties |
| 5005 | * the leaf, remove it from the tree |
| 5006 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5007 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 5008 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5009 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5010 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5011 | struct extent_buffer *leaf; |
| 5012 | struct btrfs_item *item; |
Alexandru Moise | ce0eac2 | 2015-08-23 16:01:42 +0000 | [diff] [blame] | 5013 | u32 last_off; |
| 5014 | u32 dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5015 | int ret = 0; |
| 5016 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5017 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 5018 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5019 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5020 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5021 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 5022 | |
| 5023 | for (i = 0; i < nr; i++) |
| 5024 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 5025 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5026 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5027 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5028 | if (slot + nr != nritems) { |
David Sterba | 8f881e8 | 2019-03-20 11:33:10 +0100 | [diff] [blame] | 5029 | int data_end = leaf_data_end(leaf); |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 5030 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5031 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 5032 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 5033 | data_end + dsize, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 5034 | BTRFS_LEAF_DATA_OFFSET + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5035 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5036 | |
David Sterba | c82f823 | 2019-08-09 17:48:21 +0200 | [diff] [blame] | 5037 | btrfs_init_map_token(&token, leaf); |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5038 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5039 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5040 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 5041 | item = btrfs_item_nr(i); |
David Sterba | cc4c13d | 2020-04-29 02:15:56 +0200 | [diff] [blame] | 5042 | ioff = btrfs_token_item_offset(&token, item); |
| 5043 | btrfs_set_token_item_offset(&token, item, ioff + dsize); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 5044 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 5045 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5046 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5047 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 5048 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5049 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5050 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 5051 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 5052 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5053 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 5054 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 5055 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5056 | if (leaf == root->node) { |
| 5057 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 5058 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 5059 | btrfs_set_path_blocking(path); |
David Sterba | 6a884d7d | 2019-03-20 14:30:02 +0100 | [diff] [blame] | 5060 | btrfs_clean_tree_block(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5061 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 5062 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5063 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 5064 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5065 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5066 | struct btrfs_disk_key disk_key; |
| 5067 | |
| 5068 | btrfs_item_key(leaf, &disk_key, 0); |
Nikolay Borisov | b167fa9 | 2018-06-20 15:48:47 +0300 | [diff] [blame] | 5069 | fixup_low_keys(path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5070 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5071 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 5072 | /* delete the leaf if it is mostly empty */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5073 | if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5074 | /* push_leaf_left fixes the path. |
| 5075 | * make sure the path still points to our leaf |
| 5076 | * for possible call to del_ptr below |
| 5077 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 5078 | slot = path->slots[1]; |
David Sterba | 67439da | 2019-10-08 13:28:47 +0200 | [diff] [blame] | 5079 | atomic_inc(&leaf->refs); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5080 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5081 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 5082 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 5083 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5084 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5085 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5086 | |
| 5087 | if (path->nodes[0] == leaf && |
| 5088 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 5089 | wret = push_leaf_right(trans, root, path, 1, |
| 5090 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5091 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5092 | ret = wret; |
| 5093 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5094 | |
| 5095 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 5096 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5097 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5098 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5099 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 5100 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5101 | /* if we're still in the path, make sure |
| 5102 | * we're dirty. Otherwise, one of the |
| 5103 | * push_leaf functions must have already |
| 5104 | * dirtied this buffer |
| 5105 | */ |
| 5106 | if (path->nodes[0] == leaf) |
| 5107 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5108 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5109 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 5110 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5111 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5112 | } |
| 5113 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5114 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5115 | } |
| 5116 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5117 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5118 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5119 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 5120 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5121 | * |
| 5122 | * This may release the path, and so you may lose any locks held at the |
| 5123 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5124 | */ |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5125 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5126 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5127 | struct btrfs_key key; |
| 5128 | struct btrfs_disk_key found_key; |
| 5129 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5130 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5131 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5132 | |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5133 | if (key.offset > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5134 | key.offset--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5135 | } else if (key.type > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5136 | key.type--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5137 | key.offset = (u64)-1; |
| 5138 | } else if (key.objectid > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5139 | key.objectid--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5140 | key.type = (u8)-1; |
| 5141 | key.offset = (u64)-1; |
| 5142 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5143 | return 1; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5144 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5145 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5146 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5147 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5148 | if (ret < 0) |
| 5149 | return ret; |
| 5150 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 5151 | ret = comp_keys(&found_key, &key); |
Filipe Manana | 337c6f6 | 2014-06-09 13:22:13 +0100 | [diff] [blame] | 5152 | /* |
| 5153 | * We might have had an item with the previous key in the tree right |
| 5154 | * before we released our path. And after we released our path, that |
| 5155 | * item might have been pushed to the first slot (0) of the leaf we |
| 5156 | * were holding due to a tree balance. Alternatively, an item with the |
| 5157 | * previous key can exist as the only element of a leaf (big fat item). |
| 5158 | * Therefore account for these 2 cases, so that our callers (like |
| 5159 | * btrfs_previous_item) don't miss an existing item with a key matching |
| 5160 | * the previous key we computed above. |
| 5161 | */ |
| 5162 | if (ret <= 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5163 | return 0; |
| 5164 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5165 | } |
| 5166 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5167 | /* |
| 5168 | * 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] | 5169 | * for nodes or leaves that are have a minimum transaction id. |
| 5170 | * This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5171 | * |
| 5172 | * This does not cow, but it does stuff the starting key it finds back |
| 5173 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 5174 | * key and get a writable path. |
| 5175 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5176 | * This honors path->lowest_level to prevent descent past a given level |
| 5177 | * of the tree. |
| 5178 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5179 | * min_trans indicates the oldest transaction that you are interested |
| 5180 | * in walking through. Any nodes or leaves older than min_trans are |
| 5181 | * skipped over (without reading them). |
| 5182 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5183 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 5184 | * was nothing in the tree that matched the search criteria. |
| 5185 | */ |
| 5186 | 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] | 5187 | struct btrfs_path *path, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5188 | u64 min_trans) |
| 5189 | { |
| 5190 | struct extent_buffer *cur; |
| 5191 | struct btrfs_key found_key; |
| 5192 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 5193 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5194 | u32 nritems; |
| 5195 | int level; |
| 5196 | int ret = 1; |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5197 | int keep_locks = path->keep_locks; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5198 | |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5199 | path->keep_locks = 1; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5200 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5201 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5202 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5203 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5204 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5205 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5206 | |
| 5207 | if (btrfs_header_generation(cur) < min_trans) { |
| 5208 | ret = 1; |
| 5209 | goto out; |
| 5210 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5211 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5212 | nritems = btrfs_header_nritems(cur); |
| 5213 | level = btrfs_header_level(cur); |
Qu Wenruo | e3b8336 | 2020-04-17 15:08:21 +0800 | [diff] [blame] | 5214 | sret = btrfs_bin_search(cur, min_key, &slot); |
Filipe Manana | cbca7d5 | 2019-02-18 16:57:26 +0000 | [diff] [blame] | 5215 | if (sret < 0) { |
| 5216 | ret = sret; |
| 5217 | goto out; |
| 5218 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5219 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 5220 | /* at the lowest level, we're done, setup the path and exit */ |
| 5221 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5222 | if (slot >= nritems) |
| 5223 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5224 | ret = 0; |
| 5225 | path->slots[level] = slot; |
| 5226 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 5227 | goto out; |
| 5228 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 5229 | if (sret && slot > 0) |
| 5230 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5231 | /* |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5232 | * check this node pointer against the min_trans parameters. |
Randy Dunlap | 260db43 | 2020-08-04 19:48:34 -0700 | [diff] [blame] | 5233 | * If it is too old, skip to the next one. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5234 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5235 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5236 | u64 gen; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5237 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5238 | gen = btrfs_node_ptr_generation(cur, slot); |
| 5239 | if (gen < min_trans) { |
| 5240 | slot++; |
| 5241 | continue; |
| 5242 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5243 | break; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5244 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5245 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5246 | /* |
| 5247 | * we didn't find a candidate key in this node, walk forward |
| 5248 | * and find another one |
| 5249 | */ |
| 5250 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5251 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5252 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5253 | sret = btrfs_find_next_key(root, path, min_key, level, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5254 | min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5255 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5256 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5257 | goto again; |
| 5258 | } else { |
| 5259 | goto out; |
| 5260 | } |
| 5261 | } |
| 5262 | /* save our key for returning back */ |
| 5263 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 5264 | path->slots[level] = slot; |
| 5265 | if (level == path->lowest_level) { |
| 5266 | ret = 0; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5267 | goto out; |
| 5268 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5269 | btrfs_set_path_blocking(path); |
David Sterba | 4b231ae | 2019-08-21 19:16:27 +0200 | [diff] [blame] | 5270 | cur = btrfs_read_node_slot(cur, slot); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5271 | if (IS_ERR(cur)) { |
| 5272 | ret = PTR_ERR(cur); |
| 5273 | goto out; |
| 5274 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5275 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5276 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5277 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5278 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5279 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5280 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5281 | } |
| 5282 | out: |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5283 | path->keep_locks = keep_locks; |
| 5284 | if (ret == 0) { |
| 5285 | btrfs_unlock_up_safe(path, path->lowest_level + 1); |
| 5286 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5287 | memcpy(min_key, &found_key, sizeof(found_key)); |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5288 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5289 | return ret; |
| 5290 | } |
| 5291 | |
| 5292 | /* |
| 5293 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5294 | * 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] | 5295 | * tree based on the current path and the min_trans parameters. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5296 | * |
| 5297 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5298 | * and 1 is returned if there are no higher keys in the tree |
| 5299 | * |
| 5300 | * path->keep_locks should be set to 1 on the search made before |
| 5301 | * calling this function. |
| 5302 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5303 | 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] | 5304 | struct btrfs_key *key, int level, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5305 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5306 | int slot; |
| 5307 | struct extent_buffer *c; |
| 5308 | |
Josef Bacik | 6a9fb46 | 2019-06-20 15:37:52 -0400 | [diff] [blame] | 5309 | WARN_ON(!path->keep_locks && !path->skip_locking); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5310 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5311 | if (!path->nodes[level]) |
| 5312 | return 1; |
| 5313 | |
| 5314 | slot = path->slots[level] + 1; |
| 5315 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5316 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5317 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5318 | int ret; |
| 5319 | int orig_lowest; |
| 5320 | struct btrfs_key cur_key; |
| 5321 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5322 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5323 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5324 | |
Josef Bacik | 6a9fb46 | 2019-06-20 15:37:52 -0400 | [diff] [blame] | 5325 | if (path->locks[level + 1] || path->skip_locking) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5326 | level++; |
| 5327 | continue; |
| 5328 | } |
| 5329 | |
| 5330 | slot = btrfs_header_nritems(c) - 1; |
| 5331 | if (level == 0) |
| 5332 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5333 | else |
| 5334 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5335 | |
| 5336 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5337 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5338 | path->lowest_level = level; |
| 5339 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5340 | 0, 0); |
| 5341 | path->lowest_level = orig_lowest; |
| 5342 | if (ret < 0) |
| 5343 | return ret; |
| 5344 | |
| 5345 | c = path->nodes[level]; |
| 5346 | slot = path->slots[level]; |
| 5347 | if (ret == 0) |
| 5348 | slot++; |
| 5349 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5350 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5351 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5352 | if (level == 0) |
| 5353 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5354 | else { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5355 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5356 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5357 | if (gen < min_trans) { |
| 5358 | slot++; |
| 5359 | goto next; |
| 5360 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5361 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5362 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5363 | return 0; |
| 5364 | } |
| 5365 | return 1; |
| 5366 | } |
| 5367 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5368 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5369 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5370 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5371 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5372 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5373 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5374 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5375 | return btrfs_next_old_leaf(root, path, 0); |
| 5376 | } |
| 5377 | |
| 5378 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5379 | u64 time_seq) |
| 5380 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5381 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5382 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5383 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5384 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5385 | struct btrfs_key key; |
| 5386 | u32 nritems; |
| 5387 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5388 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5389 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5390 | |
| 5391 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5392 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5393 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5394 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5395 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5396 | again: |
| 5397 | level = 1; |
| 5398 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5399 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5400 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5401 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5402 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5403 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5404 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5405 | if (time_seq) |
| 5406 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5407 | else |
| 5408 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5409 | path->keep_locks = 0; |
| 5410 | |
| 5411 | if (ret < 0) |
| 5412 | return ret; |
| 5413 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5414 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5415 | /* |
| 5416 | * by releasing the path above we dropped all our locks. A balance |
| 5417 | * could have added more items next to the key that used to be |
| 5418 | * at the very end of the block. So, check again here and |
| 5419 | * advance the path if there are now more items available. |
| 5420 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5421 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5422 | if (ret == 0) |
| 5423 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5424 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5425 | goto done; |
| 5426 | } |
Liu Bo | 0b43e04 | 2014-06-09 11:04:49 +0800 | [diff] [blame] | 5427 | /* |
| 5428 | * So the above check misses one case: |
| 5429 | * - after releasing the path above, someone has removed the item that |
| 5430 | * used to be at the very end of the block, and balance between leafs |
| 5431 | * gets another one with bigger key.offset to replace it. |
| 5432 | * |
| 5433 | * This one should be returned as well, or we can get leaf corruption |
| 5434 | * later(esp. in __btrfs_drop_extents()). |
| 5435 | * |
| 5436 | * And a bit more explanation about this check, |
| 5437 | * with ret > 0, the key isn't found, the path points to the slot |
| 5438 | * where it should be inserted, so the path->slots[0] item must be the |
| 5439 | * bigger one. |
| 5440 | */ |
| 5441 | if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { |
| 5442 | ret = 0; |
| 5443 | goto done; |
| 5444 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5445 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5446 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5447 | if (!path->nodes[level]) { |
| 5448 | ret = 1; |
| 5449 | goto done; |
| 5450 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5451 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5452 | slot = path->slots[level] + 1; |
| 5453 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5454 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5455 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5456 | if (level == BTRFS_MAX_LEVEL) { |
| 5457 | ret = 1; |
| 5458 | goto done; |
| 5459 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5460 | continue; |
| 5461 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5462 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5463 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5464 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5465 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5466 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5467 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5468 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5469 | next_rw_lock = path->locks[level]; |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 5470 | ret = read_block_for_search(root, path, &next, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 5471 | slot, &key); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5472 | if (ret == -EAGAIN) |
| 5473 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5474 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5475 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5476 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5477 | goto done; |
| 5478 | } |
| 5479 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5480 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5481 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5482 | if (!ret && time_seq) { |
| 5483 | /* |
| 5484 | * If we don't get the lock, we may be racing |
| 5485 | * with push_leaf_left, holding that lock while |
| 5486 | * itself waiting for the leaf we've currently |
| 5487 | * locked. To solve this situation, we give up |
| 5488 | * on our lock and cycle. |
| 5489 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5490 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5491 | btrfs_release_path(path); |
| 5492 | cond_resched(); |
| 5493 | goto again; |
| 5494 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5495 | if (!ret) { |
| 5496 | btrfs_set_path_blocking(path); |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 5497 | __btrfs_tree_read_lock(next, |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 5498 | BTRFS_NESTING_RIGHT, |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 5499 | path->recurse); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5500 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5501 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5502 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5503 | break; |
| 5504 | } |
| 5505 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5506 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5507 | level--; |
| 5508 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5509 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5510 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5511 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5512 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5513 | path->nodes[level] = next; |
| 5514 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5515 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5516 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5517 | if (!level) |
| 5518 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5519 | |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 5520 | ret = read_block_for_search(root, path, &next, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 5521 | 0, &key); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5522 | if (ret == -EAGAIN) |
| 5523 | goto again; |
| 5524 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5525 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5526 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5527 | goto done; |
| 5528 | } |
| 5529 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5530 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5531 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5532 | if (!ret) { |
| 5533 | btrfs_set_path_blocking(path); |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 5534 | __btrfs_tree_read_lock(next, |
Josef Bacik | bf77467 | 2020-08-20 11:46:04 -0400 | [diff] [blame] | 5535 | BTRFS_NESTING_RIGHT, |
Josef Bacik | fd7ba1c | 2020-08-20 11:46:02 -0400 | [diff] [blame] | 5536 | path->recurse); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5537 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5538 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5539 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5540 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5541 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5542 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5543 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5544 | path->leave_spinning = old_spinning; |
| 5545 | if (!old_spinning) |
| 5546 | btrfs_set_path_blocking(path); |
| 5547 | |
| 5548 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5549 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5550 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5551 | /* |
| 5552 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5553 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5554 | * |
| 5555 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5556 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5557 | int btrfs_previous_item(struct btrfs_root *root, |
| 5558 | struct btrfs_path *path, u64 min_objectid, |
| 5559 | int type) |
| 5560 | { |
| 5561 | struct btrfs_key found_key; |
| 5562 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5563 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5564 | int ret; |
| 5565 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5566 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5567 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5568 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5569 | ret = btrfs_prev_leaf(root, path); |
| 5570 | if (ret != 0) |
| 5571 | return ret; |
| 5572 | } else { |
| 5573 | path->slots[0]--; |
| 5574 | } |
| 5575 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5576 | nritems = btrfs_header_nritems(leaf); |
| 5577 | if (nritems == 0) |
| 5578 | return 1; |
| 5579 | if (path->slots[0] == nritems) |
| 5580 | path->slots[0]--; |
| 5581 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5582 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5583 | if (found_key.objectid < min_objectid) |
| 5584 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5585 | if (found_key.type == type) |
| 5586 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5587 | if (found_key.objectid == min_objectid && |
| 5588 | found_key.type < type) |
| 5589 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5590 | } |
| 5591 | return 1; |
| 5592 | } |
Wang Shilong | ade2e0b | 2014-01-12 21:38:33 +0800 | [diff] [blame] | 5593 | |
| 5594 | /* |
| 5595 | * search in extent tree to find a previous Metadata/Data extent item with |
| 5596 | * min objecitd. |
| 5597 | * |
| 5598 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5599 | */ |
| 5600 | int btrfs_previous_extent_item(struct btrfs_root *root, |
| 5601 | struct btrfs_path *path, u64 min_objectid) |
| 5602 | { |
| 5603 | struct btrfs_key found_key; |
| 5604 | struct extent_buffer *leaf; |
| 5605 | u32 nritems; |
| 5606 | int ret; |
| 5607 | |
| 5608 | while (1) { |
| 5609 | if (path->slots[0] == 0) { |
| 5610 | btrfs_set_path_blocking(path); |
| 5611 | ret = btrfs_prev_leaf(root, path); |
| 5612 | if (ret != 0) |
| 5613 | return ret; |
| 5614 | } else { |
| 5615 | path->slots[0]--; |
| 5616 | } |
| 5617 | leaf = path->nodes[0]; |
| 5618 | nritems = btrfs_header_nritems(leaf); |
| 5619 | if (nritems == 0) |
| 5620 | return 1; |
| 5621 | if (path->slots[0] == nritems) |
| 5622 | path->slots[0]--; |
| 5623 | |
| 5624 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5625 | if (found_key.objectid < min_objectid) |
| 5626 | break; |
| 5627 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY || |
| 5628 | found_key.type == BTRFS_METADATA_ITEM_KEY) |
| 5629 | return 0; |
| 5630 | if (found_key.objectid == min_objectid && |
| 5631 | found_key.type < BTRFS_EXTENT_ITEM_KEY) |
| 5632 | break; |
| 5633 | } |
| 5634 | return 1; |
| 5635 | } |