Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2 | * Copyright (C) 2007,2008 Oracle. All rights reserved. |
Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 21 | #include <linux/rbtree.h> |
David Sterba | adf0212 | 2017-05-31 19:44:31 +0200 | [diff] [blame] | 22 | #include <linux/mm.h> |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 23 | #include "ctree.h" |
| 24 | #include "disk-io.h" |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 25 | #include "transaction.h" |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 26 | #include "print-tree.h" |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 27 | #include "locking.h" |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 28 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 29 | static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root |
| 30 | *root, struct btrfs_path *path, int level); |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 31 | static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 32 | const struct btrfs_key *ins_key, struct btrfs_path *path, |
| 33 | int data_size, int extend); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 34 | static int push_node_left(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 35 | struct btrfs_fs_info *fs_info, |
| 36 | struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 37 | struct extent_buffer *src, int empty); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 38 | static int balance_node_right(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 39 | struct btrfs_fs_info *fs_info, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 40 | struct extent_buffer *dst_buf, |
| 41 | struct extent_buffer *src_buf); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 42 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 43 | int level, int slot); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 44 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 45 | struct btrfs_path *btrfs_alloc_path(void) |
| 46 | { |
Masahiro Yamada | e2c8990 | 2016-09-13 04:35:52 +0900 | [diff] [blame] | 47 | return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 50 | /* |
| 51 | * set all locked nodes in the path to blocking locks. This should |
| 52 | * be done before scheduling |
| 53 | */ |
| 54 | noinline void btrfs_set_path_blocking(struct btrfs_path *p) |
| 55 | { |
| 56 | int i; |
| 57 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 58 | if (!p->nodes[i] || !p->locks[i]) |
| 59 | continue; |
| 60 | btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 61 | if (p->locks[i] == BTRFS_READ_LOCK) |
| 62 | p->locks[i] = BTRFS_READ_LOCK_BLOCKING; |
| 63 | else if (p->locks[i] == BTRFS_WRITE_LOCK) |
| 64 | p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * reset all the locked nodes in the patch to spinning locks. |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 70 | * |
| 71 | * held is used to keep lockdep happy, when lockdep is enabled |
| 72 | * we set held to a blocking lock before we go around and |
| 73 | * retake all the spinlocks in the path. You can safely use NULL |
| 74 | * for held |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 75 | */ |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 76 | noinline void btrfs_clear_path_blocking(struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 77 | struct extent_buffer *held, int held_rw) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 78 | { |
| 79 | int i; |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 80 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 81 | if (held) { |
| 82 | btrfs_set_lock_blocking_rw(held, held_rw); |
| 83 | if (held_rw == BTRFS_WRITE_LOCK) |
| 84 | held_rw = BTRFS_WRITE_LOCK_BLOCKING; |
| 85 | else if (held_rw == BTRFS_READ_LOCK) |
| 86 | held_rw = BTRFS_READ_LOCK_BLOCKING; |
| 87 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 88 | btrfs_set_path_blocking(p); |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 89 | |
| 90 | for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 91 | if (p->nodes[i] && p->locks[i]) { |
| 92 | btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]); |
| 93 | if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING) |
| 94 | p->locks[i] = BTRFS_WRITE_LOCK; |
| 95 | else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING) |
| 96 | p->locks[i] = BTRFS_READ_LOCK; |
| 97 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 98 | } |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 99 | |
Chris Mason | 4008c04 | 2009-02-12 14:09:45 -0500 | [diff] [blame] | 100 | if (held) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 101 | btrfs_clear_lock_blocking_rw(held, held_rw); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 104 | /* this also releases the path */ |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 105 | void btrfs_free_path(struct btrfs_path *p) |
| 106 | { |
Jesper Juhl | ff175d5 | 2010-12-25 21:22:30 +0000 | [diff] [blame] | 107 | if (!p) |
| 108 | return; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 109 | btrfs_release_path(p); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 110 | kmem_cache_free(btrfs_path_cachep, p); |
| 111 | } |
| 112 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 113 | /* |
| 114 | * path release drops references on the extent buffers in the path |
| 115 | * and it drops any locks held by this path |
| 116 | * |
| 117 | * It is safe to call this on paths that no locks or extent buffers held. |
| 118 | */ |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 119 | noinline void btrfs_release_path(struct btrfs_path *p) |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 120 | { |
| 121 | int i; |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 122 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 123 | for (i = 0; i < BTRFS_MAX_LEVEL; i++) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 124 | p->slots[i] = 0; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 125 | if (!p->nodes[i]) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 126 | continue; |
| 127 | if (p->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 128 | btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 129 | p->locks[i] = 0; |
| 130 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 131 | free_extent_buffer(p->nodes[i]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 132 | p->nodes[i] = NULL; |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 136 | /* |
| 137 | * safely gets a reference on the root node of a tree. A lock |
| 138 | * is not taken, so a concurrent writer may put a different node |
| 139 | * at the root of the tree. See btrfs_lock_root_node for the |
| 140 | * looping required. |
| 141 | * |
| 142 | * The extent buffer returned by this has a reference taken, so |
| 143 | * it won't disappear. It may stop being the root of the tree |
| 144 | * at any time because there are no locks held. |
| 145 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 146 | struct extent_buffer *btrfs_root_node(struct btrfs_root *root) |
| 147 | { |
| 148 | struct extent_buffer *eb; |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 149 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 150 | while (1) { |
| 151 | rcu_read_lock(); |
| 152 | eb = rcu_dereference(root->node); |
| 153 | |
| 154 | /* |
| 155 | * 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] | 156 | * 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] | 157 | * the inc_not_zero dance and if it doesn't work then |
| 158 | * synchronize_rcu and try again. |
| 159 | */ |
| 160 | if (atomic_inc_not_zero(&eb->refs)) { |
| 161 | rcu_read_unlock(); |
| 162 | break; |
| 163 | } |
| 164 | rcu_read_unlock(); |
| 165 | synchronize_rcu(); |
| 166 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 167 | return eb; |
| 168 | } |
| 169 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 170 | /* loop around taking references on and locking the root node of the |
| 171 | * tree until you end up with a lock on the root. A locked buffer |
| 172 | * is returned, with a reference held. |
| 173 | */ |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 174 | struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root) |
| 175 | { |
| 176 | struct extent_buffer *eb; |
| 177 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 178 | while (1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 179 | eb = btrfs_root_node(root); |
| 180 | btrfs_tree_lock(eb); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 181 | if (eb == root->node) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 182 | break; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 183 | btrfs_tree_unlock(eb); |
| 184 | free_extent_buffer(eb); |
| 185 | } |
| 186 | return eb; |
| 187 | } |
| 188 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 189 | /* loop around taking references on and locking the root node of the |
| 190 | * tree until you end up with a lock on the root. A locked buffer |
| 191 | * is returned, with a reference held. |
| 192 | */ |
Josef Bacik | 84f7d8e | 2017-09-29 15:43:49 -0400 | [diff] [blame] | 193 | struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 194 | { |
| 195 | struct extent_buffer *eb; |
| 196 | |
| 197 | while (1) { |
| 198 | eb = btrfs_root_node(root); |
| 199 | btrfs_tree_read_lock(eb); |
| 200 | if (eb == root->node) |
| 201 | break; |
| 202 | btrfs_tree_read_unlock(eb); |
| 203 | free_extent_buffer(eb); |
| 204 | } |
| 205 | return eb; |
| 206 | } |
| 207 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 208 | /* cowonly root (everything not a reference counted cow subvolume), just get |
| 209 | * put onto a simple dirty list. transaction.c walks this to make sure they |
| 210 | * get properly updated on disk. |
| 211 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 212 | static void add_root_to_dirty_list(struct btrfs_root *root) |
| 213 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 214 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 215 | |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 216 | if (test_bit(BTRFS_ROOT_DIRTY, &root->state) || |
| 217 | !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state)) |
| 218 | return; |
| 219 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 220 | spin_lock(&fs_info->trans_lock); |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 221 | if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) { |
| 222 | /* Want the extent tree to be the last on the list */ |
| 223 | if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID) |
| 224 | list_move_tail(&root->dirty_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 225 | &fs_info->dirty_cowonly_roots); |
Josef Bacik | e7070be | 2014-12-16 08:54:43 -0800 | [diff] [blame] | 226 | else |
| 227 | list_move(&root->dirty_list, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 228 | &fs_info->dirty_cowonly_roots); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 229 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 230 | spin_unlock(&fs_info->trans_lock); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 231 | } |
| 232 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 233 | /* |
| 234 | * used by snapshot creation to make a copy of a root for a tree with |
| 235 | * a given objectid. The buffer with the new root node is returned in |
| 236 | * cow_ret, and this func returns zero on success or a negative error code. |
| 237 | */ |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 238 | int btrfs_copy_root(struct btrfs_trans_handle *trans, |
| 239 | struct btrfs_root *root, |
| 240 | struct extent_buffer *buf, |
| 241 | struct extent_buffer **cow_ret, u64 new_root_objectid) |
| 242 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 243 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 244 | struct extent_buffer *cow; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 245 | int ret = 0; |
| 246 | int level; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 247 | struct btrfs_disk_key disk_key; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 248 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 249 | WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 250 | trans->transid != fs_info->running_transaction->transid); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 251 | WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
| 252 | trans->transid != root->last_trans); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 253 | |
| 254 | level = btrfs_header_level(buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 255 | if (level == 0) |
| 256 | btrfs_item_key(buf, &disk_key, 0); |
| 257 | else |
| 258 | btrfs_node_key(buf, &disk_key, 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 259 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 260 | cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid, |
| 261 | &disk_key, level, buf->start, 0); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 262 | if (IS_ERR(cow)) |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 263 | return PTR_ERR(cow); |
| 264 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 265 | copy_extent_buffer_full(cow, buf); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 266 | btrfs_set_header_bytenr(cow, cow->start); |
| 267 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 268 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 269 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 270 | BTRFS_HEADER_FLAG_RELOC); |
| 271 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 272 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 273 | else |
| 274 | btrfs_set_header_owner(cow, new_root_objectid); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 275 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 276 | write_extent_buffer_fsid(cow, fs_info->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 277 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 278 | WARN_ON(btrfs_header_generation(buf) > trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 279 | if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 280 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 281 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 282 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Chris Mason | 4aec2b5 | 2007-12-18 16:25:45 -0500 | [diff] [blame] | 283 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 284 | if (ret) |
| 285 | return ret; |
| 286 | |
| 287 | btrfs_mark_buffer_dirty(cow); |
| 288 | *cow_ret = cow; |
| 289 | return 0; |
| 290 | } |
| 291 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 292 | enum mod_log_op { |
| 293 | MOD_LOG_KEY_REPLACE, |
| 294 | MOD_LOG_KEY_ADD, |
| 295 | MOD_LOG_KEY_REMOVE, |
| 296 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, |
| 297 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, |
| 298 | MOD_LOG_MOVE_KEYS, |
| 299 | MOD_LOG_ROOT_REPLACE, |
| 300 | }; |
| 301 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 302 | struct tree_mod_root { |
| 303 | u64 logical; |
| 304 | u8 level; |
| 305 | }; |
| 306 | |
| 307 | struct tree_mod_elem { |
| 308 | struct rb_node node; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 309 | u64 logical; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 310 | u64 seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 311 | enum mod_log_op op; |
| 312 | |
| 313 | /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */ |
| 314 | int slot; |
| 315 | |
| 316 | /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */ |
| 317 | u64 generation; |
| 318 | |
| 319 | /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */ |
| 320 | struct btrfs_disk_key key; |
| 321 | u64 blockptr; |
| 322 | |
| 323 | /* this is used for op == MOD_LOG_MOVE_KEYS */ |
David Sterba | b6dfa35 | 2018-03-05 15:31:18 +0100 | [diff] [blame] | 324 | struct { |
| 325 | int dst_slot; |
| 326 | int nr_items; |
| 327 | } move; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 328 | |
| 329 | /* this is used for op == MOD_LOG_ROOT_REPLACE */ |
| 330 | struct tree_mod_root old_root; |
| 331 | }; |
| 332 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 333 | /* |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 334 | * Pull a new tree mod seq number for our operation. |
Jan Schmidt | fc36ed7e | 2013-04-24 16:57:33 +0000 | [diff] [blame] | 335 | */ |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 336 | 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] | 337 | { |
| 338 | return atomic64_inc_return(&fs_info->tree_mod_seq); |
| 339 | } |
| 340 | |
| 341 | /* |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 342 | * This adds a new blocker to the tree mod log's blocker list if the @elem |
| 343 | * passed does not already have a sequence number set. So when a caller expects |
| 344 | * to record tree modifications, it should ensure to set elem->seq to zero |
| 345 | * before calling btrfs_get_tree_mod_seq. |
| 346 | * Returns a fresh, unused tree log modification sequence number, even if no new |
| 347 | * blocker was added. |
| 348 | */ |
| 349 | u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 350 | struct seq_list *elem) |
| 351 | { |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 352 | write_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 353 | spin_lock(&fs_info->tree_mod_seq_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 354 | if (!elem->seq) { |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 355 | elem->seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 356 | list_add_tail(&elem->list, &fs_info->tree_mod_seq_list); |
| 357 | } |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 358 | spin_unlock(&fs_info->tree_mod_seq_lock); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 359 | write_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 360 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 361 | return elem->seq; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info, |
| 365 | struct seq_list *elem) |
| 366 | { |
| 367 | struct rb_root *tm_root; |
| 368 | struct rb_node *node; |
| 369 | struct rb_node *next; |
| 370 | struct seq_list *cur_elem; |
| 371 | struct tree_mod_elem *tm; |
| 372 | u64 min_seq = (u64)-1; |
| 373 | u64 seq_putting = elem->seq; |
| 374 | |
| 375 | if (!seq_putting) |
| 376 | return; |
| 377 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 378 | spin_lock(&fs_info->tree_mod_seq_lock); |
| 379 | list_del(&elem->list); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 380 | elem->seq = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 381 | |
| 382 | list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 383 | if (cur_elem->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 384 | if (seq_putting > cur_elem->seq) { |
| 385 | /* |
| 386 | * blocker with lower sequence number exists, we |
| 387 | * cannot remove anything from the log |
| 388 | */ |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 389 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 390 | return; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 391 | } |
| 392 | min_seq = cur_elem->seq; |
| 393 | } |
| 394 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 395 | spin_unlock(&fs_info->tree_mod_seq_lock); |
| 396 | |
| 397 | /* |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 398 | * anything that's lower than the lowest existing (read: blocked) |
| 399 | * sequence number can be removed from the tree. |
| 400 | */ |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 401 | write_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 402 | tm_root = &fs_info->tree_mod_log; |
| 403 | for (node = rb_first(tm_root); node; node = next) { |
| 404 | next = rb_next(node); |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 405 | tm = rb_entry(node, struct tree_mod_elem, node); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 406 | if (tm->seq > min_seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 407 | continue; |
| 408 | rb_erase(node, tm_root); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 409 | kfree(tm); |
| 410 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 411 | write_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /* |
| 415 | * key order of the log: |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 416 | * node/leaf start address -> sequence |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 417 | * |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 418 | * The 'start address' is the logical address of the *new* root node |
| 419 | * for root replace operations, or the logical address of the affected |
| 420 | * block for all other operations. |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 421 | * |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 422 | * Note: must be called with write lock for fs_info::tree_mod_log_lock. |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 423 | */ |
| 424 | static noinline int |
| 425 | __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm) |
| 426 | { |
| 427 | struct rb_root *tm_root; |
| 428 | struct rb_node **new; |
| 429 | struct rb_node *parent = NULL; |
| 430 | struct tree_mod_elem *cur; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 431 | |
Josef Bacik | fcebe45 | 2014-05-13 17:30:47 -0700 | [diff] [blame] | 432 | tm->seq = btrfs_inc_tree_mod_seq(fs_info); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 433 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 434 | tm_root = &fs_info->tree_mod_log; |
| 435 | new = &tm_root->rb_node; |
| 436 | while (*new) { |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 437 | cur = rb_entry(*new, struct tree_mod_elem, node); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 438 | parent = *new; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 439 | if (cur->logical < tm->logical) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 440 | new = &((*new)->rb_left); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 441 | else if (cur->logical > tm->logical) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 442 | new = &((*new)->rb_right); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 443 | else if (cur->seq < tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 444 | new = &((*new)->rb_left); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 445 | else if (cur->seq > tm->seq) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 446 | new = &((*new)->rb_right); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 447 | else |
| 448 | return -EEXIST; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | rb_link_node(&tm->node, parent, new); |
| 452 | rb_insert_color(&tm->node, tm_root); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 453 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 454 | } |
| 455 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 456 | /* |
| 457 | * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it |
| 458 | * returns zero with the tree_mod_log_lock acquired. The caller must hold |
| 459 | * 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] | 460 | * write unlock fs_info::tree_mod_log_lock. |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 461 | */ |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 462 | static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info, |
| 463 | struct extent_buffer *eb) { |
| 464 | smp_mb(); |
| 465 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 466 | return 1; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 467 | if (eb && btrfs_header_level(eb) == 0) |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 468 | return 1; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 469 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 470 | write_lock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 471 | if (list_empty(&(fs_info)->tree_mod_seq_list)) { |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 472 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 473 | return 1; |
| 474 | } |
| 475 | |
Jan Schmidt | e9b7fd4 | 2012-05-31 14:59:09 +0200 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 479 | /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */ |
| 480 | static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info, |
| 481 | struct extent_buffer *eb) |
| 482 | { |
| 483 | smp_mb(); |
| 484 | if (list_empty(&(fs_info)->tree_mod_seq_list)) |
| 485 | return 0; |
| 486 | if (eb && btrfs_header_level(eb) == 0) |
| 487 | return 0; |
| 488 | |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | static struct tree_mod_elem * |
| 493 | alloc_tree_mod_elem(struct extent_buffer *eb, int slot, |
| 494 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 495 | { |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 496 | struct tree_mod_elem *tm; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 497 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 498 | tm = kzalloc(sizeof(*tm), flags); |
| 499 | if (!tm) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 500 | return NULL; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +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 | if (op != MOD_LOG_KEY_ADD) { |
| 504 | btrfs_node_key(eb, &tm->key, slot); |
| 505 | tm->blockptr = btrfs_node_blockptr(eb, slot); |
| 506 | } |
| 507 | tm->op = op; |
| 508 | tm->slot = slot; |
| 509 | tm->generation = btrfs_node_ptr_generation(eb, slot); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 510 | RB_CLEAR_NODE(&tm->node); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 511 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 512 | return tm; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 513 | } |
| 514 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 515 | static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot, |
| 516 | enum mod_log_op op, gfp_t flags) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 517 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 518 | struct tree_mod_elem *tm; |
| 519 | int ret; |
| 520 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 521 | if (!tree_mod_need_log(eb->fs_info, eb)) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 522 | return 0; |
| 523 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 524 | tm = alloc_tree_mod_elem(eb, slot, op, flags); |
| 525 | if (!tm) |
| 526 | return -ENOMEM; |
| 527 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 528 | if (tree_mod_dont_log(eb->fs_info, eb)) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 529 | kfree(tm); |
| 530 | return 0; |
| 531 | } |
| 532 | |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 533 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 534 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 535 | if (ret) |
| 536 | kfree(tm); |
| 537 | |
| 538 | return ret; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 539 | } |
| 540 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 541 | static noinline int tree_mod_log_insert_move(struct extent_buffer *eb, |
| 542 | int dst_slot, int src_slot, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 543 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 544 | struct tree_mod_elem *tm = NULL; |
| 545 | struct tree_mod_elem **tm_list = NULL; |
| 546 | int ret = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 547 | int i; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 548 | int locked = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 549 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 550 | if (!tree_mod_need_log(eb->fs_info, eb)) |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 551 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 552 | |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 553 | 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] | 554 | if (!tm_list) |
| 555 | return -ENOMEM; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 556 | |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 557 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 558 | if (!tm) { |
| 559 | ret = -ENOMEM; |
| 560 | goto free_tms; |
| 561 | } |
Jan Schmidt | f395694 | 2012-05-31 15:02:32 +0200 | [diff] [blame] | 562 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 563 | tm->logical = eb->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 564 | tm->slot = src_slot; |
| 565 | tm->move.dst_slot = dst_slot; |
| 566 | tm->move.nr_items = nr_items; |
| 567 | tm->op = MOD_LOG_MOVE_KEYS; |
| 568 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 569 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
| 570 | tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot, |
David Sterba | 176ef8f | 2017-03-28 14:35:01 +0200 | [diff] [blame] | 571 | MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 572 | if (!tm_list[i]) { |
| 573 | ret = -ENOMEM; |
| 574 | goto free_tms; |
| 575 | } |
| 576 | } |
| 577 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 578 | if (tree_mod_dont_log(eb->fs_info, eb)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 579 | goto free_tms; |
| 580 | locked = 1; |
| 581 | |
| 582 | /* |
| 583 | * When we override something during the move, we log these removals. |
| 584 | * This can only happen when we move towards the beginning of the |
| 585 | * buffer, i.e. dst_slot < src_slot. |
| 586 | */ |
| 587 | for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) { |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 588 | 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] | 589 | if (ret) |
| 590 | goto free_tms; |
| 591 | } |
| 592 | |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 593 | ret = __tree_mod_log_insert(eb->fs_info, tm); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 594 | if (ret) |
| 595 | goto free_tms; |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 596 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 597 | kfree(tm_list); |
| 598 | |
| 599 | return 0; |
| 600 | free_tms: |
| 601 | for (i = 0; i < nr_items; i++) { |
| 602 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) |
David Sterba | 6074d45 | 2018-03-05 15:03:52 +0100 | [diff] [blame] | 603 | 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] | 604 | kfree(tm_list[i]); |
| 605 | } |
| 606 | if (locked) |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 607 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 608 | kfree(tm_list); |
| 609 | kfree(tm); |
| 610 | |
| 611 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 614 | static inline int |
| 615 | __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, |
| 616 | struct tree_mod_elem **tm_list, |
| 617 | int nritems) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 618 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 619 | int i, j; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 620 | int ret; |
| 621 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 622 | for (i = nritems - 1; i >= 0; i--) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 623 | ret = __tree_mod_log_insert(fs_info, tm_list[i]); |
| 624 | if (ret) { |
| 625 | for (j = nritems - 1; j > i; j--) |
| 626 | rb_erase(&tm_list[j]->node, |
| 627 | &fs_info->tree_mod_log); |
| 628 | return ret; |
| 629 | } |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 630 | } |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 631 | |
| 632 | return 0; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 633 | } |
| 634 | |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 635 | static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root, |
| 636 | struct extent_buffer *new_root, int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 637 | { |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 638 | struct btrfs_fs_info *fs_info = old_root->fs_info; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 639 | struct tree_mod_elem *tm = NULL; |
| 640 | struct tree_mod_elem **tm_list = NULL; |
| 641 | int nritems = 0; |
| 642 | int ret = 0; |
| 643 | int i; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 644 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 645 | if (!tree_mod_need_log(fs_info, NULL)) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 646 | return 0; |
| 647 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 648 | if (log_removal && btrfs_header_level(old_root) > 0) { |
| 649 | nritems = btrfs_header_nritems(old_root); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 650 | tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 651 | GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 652 | if (!tm_list) { |
| 653 | ret = -ENOMEM; |
| 654 | goto free_tms; |
| 655 | } |
| 656 | for (i = 0; i < nritems; i++) { |
| 657 | tm_list[i] = alloc_tree_mod_elem(old_root, i, |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 658 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 659 | if (!tm_list[i]) { |
| 660 | ret = -ENOMEM; |
| 661 | goto free_tms; |
| 662 | } |
| 663 | } |
| 664 | } |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 665 | |
David Sterba | bcc8e07 | 2017-03-28 14:35:42 +0200 | [diff] [blame] | 666 | tm = kzalloc(sizeof(*tm), GFP_NOFS); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 667 | if (!tm) { |
| 668 | ret = -ENOMEM; |
| 669 | goto free_tms; |
| 670 | } |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 671 | |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 672 | tm->logical = new_root->start; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 673 | tm->old_root.logical = old_root->start; |
| 674 | tm->old_root.level = btrfs_header_level(old_root); |
| 675 | tm->generation = btrfs_header_generation(old_root); |
| 676 | tm->op = MOD_LOG_ROOT_REPLACE; |
| 677 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 678 | if (tree_mod_dont_log(fs_info, NULL)) |
| 679 | goto free_tms; |
| 680 | |
| 681 | if (tm_list) |
| 682 | ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems); |
| 683 | if (!ret) |
| 684 | ret = __tree_mod_log_insert(fs_info, tm); |
| 685 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 686 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 687 | if (ret) |
| 688 | goto free_tms; |
| 689 | kfree(tm_list); |
| 690 | |
| 691 | return ret; |
| 692 | |
| 693 | free_tms: |
| 694 | if (tm_list) { |
| 695 | for (i = 0; i < nritems; i++) |
| 696 | kfree(tm_list[i]); |
| 697 | kfree(tm_list); |
| 698 | } |
| 699 | kfree(tm); |
| 700 | |
| 701 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static struct tree_mod_elem * |
| 705 | __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq, |
| 706 | int smallest) |
| 707 | { |
| 708 | struct rb_root *tm_root; |
| 709 | struct rb_node *node; |
| 710 | struct tree_mod_elem *cur = NULL; |
| 711 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 712 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 713 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 714 | tm_root = &fs_info->tree_mod_log; |
| 715 | node = tm_root->rb_node; |
| 716 | while (node) { |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 717 | cur = rb_entry(node, struct tree_mod_elem, node); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 718 | if (cur->logical < start) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 719 | node = node->rb_left; |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 720 | } else if (cur->logical > start) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 721 | node = node->rb_right; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 722 | } else if (cur->seq < min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 723 | node = node->rb_left; |
| 724 | } else if (!smallest) { |
| 725 | /* we want the node with the highest seq */ |
| 726 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 727 | BUG_ON(found->seq > cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 728 | found = cur; |
| 729 | node = node->rb_left; |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 730 | } else if (cur->seq > min_seq) { |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 731 | /* we want the node with the smallest seq */ |
| 732 | if (found) |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 733 | BUG_ON(found->seq < cur->seq); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 734 | found = cur; |
| 735 | node = node->rb_right; |
| 736 | } else { |
| 737 | found = cur; |
| 738 | break; |
| 739 | } |
| 740 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 741 | read_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 742 | |
| 743 | return found; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * this returns the element from the log with the smallest time sequence |
| 748 | * value that's in the log (the oldest log item). any element with a time |
| 749 | * sequence lower than min_seq will be ignored. |
| 750 | */ |
| 751 | static struct tree_mod_elem * |
| 752 | tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start, |
| 753 | u64 min_seq) |
| 754 | { |
| 755 | return __tree_mod_log_search(fs_info, start, min_seq, 1); |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * this returns the element from the log with the largest time sequence |
| 760 | * value that's in the log (the most recent log item). any element with |
| 761 | * a time sequence lower than min_seq will be ignored. |
| 762 | */ |
| 763 | static struct tree_mod_elem * |
| 764 | tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq) |
| 765 | { |
| 766 | return __tree_mod_log_search(fs_info, start, min_seq, 0); |
| 767 | } |
| 768 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 769 | static noinline int |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 770 | tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst, |
| 771 | struct extent_buffer *src, unsigned long dst_offset, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 772 | unsigned long src_offset, int nr_items) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 773 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 774 | int ret = 0; |
| 775 | struct tree_mod_elem **tm_list = NULL; |
| 776 | struct tree_mod_elem **tm_list_add, **tm_list_rem; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 777 | int i; |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 778 | int locked = 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 779 | |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 780 | if (!tree_mod_need_log(fs_info, NULL)) |
| 781 | return 0; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 782 | |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 783 | 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] | 784 | return 0; |
| 785 | |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 786 | 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] | 787 | GFP_NOFS); |
| 788 | if (!tm_list) |
| 789 | return -ENOMEM; |
| 790 | |
| 791 | tm_list_add = tm_list; |
| 792 | tm_list_rem = tm_list + nr_items; |
| 793 | for (i = 0; i < nr_items; i++) { |
| 794 | tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset, |
| 795 | MOD_LOG_KEY_REMOVE, GFP_NOFS); |
| 796 | if (!tm_list_rem[i]) { |
| 797 | ret = -ENOMEM; |
| 798 | goto free_tms; |
| 799 | } |
| 800 | |
| 801 | tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset, |
| 802 | MOD_LOG_KEY_ADD, GFP_NOFS); |
| 803 | if (!tm_list_add[i]) { |
| 804 | ret = -ENOMEM; |
| 805 | goto free_tms; |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | if (tree_mod_dont_log(fs_info, NULL)) |
| 810 | goto free_tms; |
| 811 | locked = 1; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 812 | |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 813 | for (i = 0; i < nr_items; i++) { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 814 | ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]); |
| 815 | if (ret) |
| 816 | goto free_tms; |
| 817 | ret = __tree_mod_log_insert(fs_info, tm_list_add[i]); |
| 818 | if (ret) |
| 819 | goto free_tms; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 820 | } |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 821 | |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 822 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 823 | kfree(tm_list); |
| 824 | |
| 825 | return 0; |
| 826 | |
| 827 | free_tms: |
| 828 | for (i = 0; i < nr_items * 2; i++) { |
| 829 | if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node)) |
| 830 | rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log); |
| 831 | kfree(tm_list[i]); |
| 832 | } |
| 833 | if (locked) |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 834 | write_unlock(&fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 835 | kfree(tm_list); |
| 836 | |
| 837 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 838 | } |
| 839 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 840 | static noinline int tree_mod_log_free_eb(struct extent_buffer *eb) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 841 | { |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 842 | struct tree_mod_elem **tm_list = NULL; |
| 843 | int nritems = 0; |
| 844 | int i; |
| 845 | int ret = 0; |
| 846 | |
| 847 | if (btrfs_header_level(eb) == 0) |
| 848 | return 0; |
| 849 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 850 | if (!tree_mod_need_log(eb->fs_info, NULL)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 851 | return 0; |
| 852 | |
| 853 | nritems = btrfs_header_nritems(eb); |
David Sterba | 31e818f | 2015-02-20 18:00:26 +0100 | [diff] [blame] | 854 | 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] | 855 | if (!tm_list) |
| 856 | return -ENOMEM; |
| 857 | |
| 858 | for (i = 0; i < nritems; i++) { |
| 859 | tm_list[i] = alloc_tree_mod_elem(eb, i, |
| 860 | MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS); |
| 861 | if (!tm_list[i]) { |
| 862 | ret = -ENOMEM; |
| 863 | goto free_tms; |
| 864 | } |
| 865 | } |
| 866 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 867 | if (tree_mod_dont_log(eb->fs_info, eb)) |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 868 | goto free_tms; |
| 869 | |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 870 | ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 871 | write_unlock(&eb->fs_info->tree_mod_log_lock); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 872 | if (ret) |
| 873 | goto free_tms; |
| 874 | kfree(tm_list); |
| 875 | |
| 876 | return 0; |
| 877 | |
| 878 | free_tms: |
| 879 | for (i = 0; i < nritems; i++) |
| 880 | kfree(tm_list[i]); |
| 881 | kfree(tm_list); |
| 882 | |
| 883 | return ret; |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 884 | } |
| 885 | |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 886 | static noinline void |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 887 | tree_mod_log_set_root_pointer(struct btrfs_root *root, |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 888 | struct extent_buffer *new_root_node, |
| 889 | int log_removal) |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 890 | { |
| 891 | int ret; |
David Sterba | 95b757c | 2018-03-05 15:22:30 +0100 | [diff] [blame] | 892 | ret = tree_mod_log_insert_root(root->node, new_root_node, log_removal); |
Jan Schmidt | bd989ba | 2012-05-16 17:18:50 +0200 | [diff] [blame] | 893 | BUG_ON(ret < 0); |
| 894 | } |
| 895 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 896 | /* |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 897 | * check if the tree block can be shared by multiple trees |
| 898 | */ |
| 899 | int btrfs_block_can_be_shared(struct btrfs_root *root, |
| 900 | struct extent_buffer *buf) |
| 901 | { |
| 902 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 903 | * Tree blocks not in reference counted trees and tree roots |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 904 | * are never shared. If a block was allocated after the last |
| 905 | * snapshot and the block was not allocated by tree relocation, |
| 906 | * we know the block is not shared. |
| 907 | */ |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 908 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 909 | buf != root->node && buf != root->commit_root && |
| 910 | (btrfs_header_generation(buf) <= |
| 911 | btrfs_root_last_snapshot(&root->root_item) || |
| 912 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC))) |
| 913 | return 1; |
| 914 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 915 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 916 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 917 | return 1; |
| 918 | #endif |
| 919 | return 0; |
| 920 | } |
| 921 | |
| 922 | static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans, |
| 923 | struct btrfs_root *root, |
| 924 | struct extent_buffer *buf, |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 925 | struct extent_buffer *cow, |
| 926 | int *last_ref) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 927 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 928 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 929 | u64 refs; |
| 930 | u64 owner; |
| 931 | u64 flags; |
| 932 | u64 new_flags = 0; |
| 933 | int ret; |
| 934 | |
| 935 | /* |
| 936 | * Backrefs update rules: |
| 937 | * |
| 938 | * Always use full backrefs for extent pointers in tree block |
| 939 | * allocated by tree relocation. |
| 940 | * |
| 941 | * If a shared tree block is no longer referenced by its owner |
| 942 | * tree (btrfs_header_owner(buf) == root->root_key.objectid), |
| 943 | * use full backrefs for extent pointers in tree block. |
| 944 | * |
| 945 | * If a tree block is been relocating |
| 946 | * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID), |
| 947 | * use full backrefs for extent pointers in tree block. |
| 948 | * The reason for this is some operations (such as drop tree) |
| 949 | * are only allowed for blocks use full backrefs. |
| 950 | */ |
| 951 | |
| 952 | if (btrfs_block_can_be_shared(root, buf)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 953 | ret = btrfs_lookup_extent_info(trans, fs_info, buf->start, |
Josef Bacik | 3173a18 | 2013-03-07 14:22:04 -0500 | [diff] [blame] | 954 | btrfs_header_level(buf), 1, |
| 955 | &refs, &flags); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 956 | if (ret) |
| 957 | return ret; |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 958 | if (refs == 0) { |
| 959 | ret = -EROFS; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 960 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | e5df957 | 2011-08-29 14:17:04 -0700 | [diff] [blame] | 961 | return ret; |
| 962 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 963 | } else { |
| 964 | refs = 1; |
| 965 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 966 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 967 | flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 968 | else |
| 969 | flags = 0; |
| 970 | } |
| 971 | |
| 972 | owner = btrfs_header_owner(buf); |
| 973 | BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID && |
| 974 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); |
| 975 | |
| 976 | if (refs > 1) { |
| 977 | if ((owner == root->root_key.objectid || |
| 978 | root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && |
| 979 | !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 980 | ret = btrfs_inc_ref(trans, root, buf, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 981 | if (ret) |
| 982 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 983 | |
| 984 | if (root->root_key.objectid == |
| 985 | BTRFS_TREE_RELOC_OBJECTID) { |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 986 | ret = btrfs_dec_ref(trans, root, buf, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 987 | if (ret) |
| 988 | return ret; |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 989 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 990 | if (ret) |
| 991 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 992 | } |
| 993 | new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; |
| 994 | } else { |
| 995 | |
| 996 | if (root->root_key.objectid == |
| 997 | BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 998 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 999 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 1000 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 1001 | if (ret) |
| 1002 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1003 | } |
| 1004 | if (new_flags != 0) { |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1005 | int level = btrfs_header_level(buf); |
| 1006 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1007 | ret = btrfs_set_disk_extent_flags(trans, fs_info, |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1008 | buf->start, |
| 1009 | buf->len, |
Josef Bacik | b1c79e0 | 2013-05-09 13:49:30 -0400 | [diff] [blame] | 1010 | new_flags, level, 0); |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1011 | if (ret) |
| 1012 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1013 | } |
| 1014 | } else { |
| 1015 | if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) { |
| 1016 | if (root->root_key.objectid == |
| 1017 | BTRFS_TREE_RELOC_OBJECTID) |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 1018 | ret = btrfs_inc_ref(trans, root, cow, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1019 | else |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 1020 | ret = btrfs_inc_ref(trans, root, cow, 0); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 1021 | if (ret) |
| 1022 | return ret; |
Josef Bacik | e339a6b | 2014-07-02 10:54:25 -0700 | [diff] [blame] | 1023 | ret = btrfs_dec_ref(trans, root, buf, 1); |
Jeff Mahoney | 692826b | 2017-11-21 13:58:49 -0500 | [diff] [blame] | 1024 | if (ret) |
| 1025 | return ret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1026 | } |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 1027 | clean_tree_block(fs_info, buf); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1028 | *last_ref = 1; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1029 | } |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1034 | * does the dirty work in cow of a single block. The parent block (if |
| 1035 | * supplied) is updated to point to the new cow copy. The new buffer is marked |
| 1036 | * dirty and returned locked. If you modify the block it needs to be marked |
| 1037 | * dirty again. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1038 | * |
| 1039 | * search_start -- an allocation hint for the new block |
| 1040 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1041 | * empty_size -- a hint that you plan on doing more cow. This is the size in |
| 1042 | * bytes the allocator should try to find free next to the block it returns. |
| 1043 | * This is just a hint and may be ignored by the allocator. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1044 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1045 | static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1046 | struct btrfs_root *root, |
| 1047 | struct extent_buffer *buf, |
| 1048 | struct extent_buffer *parent, int parent_slot, |
| 1049 | struct extent_buffer **cow_ret, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1050 | u64 search_start, u64 empty_size) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1051 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1052 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1053 | struct btrfs_disk_key disk_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1054 | struct extent_buffer *cow; |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1055 | int level, ret; |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1056 | int last_ref = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1057 | int unlock_orig = 0; |
Goldwyn Rodrigues | 0f5053e | 2016-09-22 14:11:34 -0500 | [diff] [blame] | 1058 | u64 parent_start = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1059 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1060 | if (*cow_ret == buf) |
| 1061 | unlock_orig = 1; |
| 1062 | |
Chris Mason | b9447ef | 2009-03-09 11:45:38 -0400 | [diff] [blame] | 1063 | btrfs_assert_tree_locked(buf); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1064 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1065 | WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1066 | trans->transid != fs_info->running_transaction->transid); |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1067 | WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) && |
| 1068 | trans->transid != root->last_trans); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1069 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1070 | level = btrfs_header_level(buf); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 1071 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1072 | if (level == 0) |
| 1073 | btrfs_item_key(buf, &disk_key, 0); |
| 1074 | else |
| 1075 | btrfs_node_key(buf, &disk_key, 0); |
| 1076 | |
Goldwyn Rodrigues | 0f5053e | 2016-09-22 14:11:34 -0500 | [diff] [blame] | 1077 | if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent) |
| 1078 | parent_start = parent->start; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1079 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 1080 | cow = btrfs_alloc_tree_block(trans, root, parent_start, |
| 1081 | root->root_key.objectid, &disk_key, level, |
| 1082 | search_start, empty_size); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1083 | if (IS_ERR(cow)) |
| 1084 | return PTR_ERR(cow); |
| 1085 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1086 | /* cow is set to blocking by btrfs_init_new_buffer */ |
| 1087 | |
David Sterba | 58e8012 | 2016-11-08 18:30:31 +0100 | [diff] [blame] | 1088 | copy_extent_buffer_full(cow, buf); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1089 | btrfs_set_header_bytenr(cow, cow->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1090 | btrfs_set_header_generation(cow, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1091 | btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV); |
| 1092 | btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN | |
| 1093 | BTRFS_HEADER_FLAG_RELOC); |
| 1094 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) |
| 1095 | btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC); |
| 1096 | else |
| 1097 | btrfs_set_header_owner(cow, root->root_key.objectid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1098 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1099 | write_extent_buffer_fsid(cow, fs_info->fsid); |
Yan Zheng | 2b82032 | 2008-11-17 21:11:30 -0500 | [diff] [blame] | 1100 | |
Mark Fasheh | be1a556 | 2011-08-08 13:20:18 -0700 | [diff] [blame] | 1101 | ret = update_ref_for_cow(trans, root, buf, cow, &last_ref); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1102 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1103 | btrfs_abort_transaction(trans, ret); |
Mark Fasheh | b68dc2a | 2011-08-29 14:30:39 -0700 | [diff] [blame] | 1104 | return ret; |
| 1105 | } |
Zheng Yan | 1a40e23 | 2008-09-26 10:09:34 -0400 | [diff] [blame] | 1106 | |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1107 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) { |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1108 | ret = btrfs_reloc_cow_block(trans, root, buf, cow); |
Zhaolei | 93314e3 | 2015-08-06 21:56:58 +0800 | [diff] [blame] | 1109 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1110 | btrfs_abort_transaction(trans, ret); |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1111 | return ret; |
Zhaolei | 93314e3 | 2015-08-06 21:56:58 +0800 | [diff] [blame] | 1112 | } |
Josef Bacik | 83d4cfd | 2013-08-30 15:09:51 -0400 | [diff] [blame] | 1113 | } |
Yan, Zheng | 3fd0a55 | 2010-05-16 10:49:59 -0400 | [diff] [blame] | 1114 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1115 | if (buf == root->node) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1116 | WARN_ON(parent && parent != buf); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1117 | if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID || |
| 1118 | btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV) |
| 1119 | parent_start = buf->start; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1120 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1121 | extent_buffer_get(cow); |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1122 | tree_mod_log_set_root_pointer(root, cow, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1123 | rcu_assign_pointer(root->node, cow); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1124 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1125 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1126 | last_ref); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1127 | free_extent_buffer(buf); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1128 | add_root_to_dirty_list(root); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1129 | } else { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1130 | WARN_ON(trans->transid != btrfs_header_generation(parent)); |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 1131 | tree_mod_log_insert_key(parent, parent_slot, |
Josef Bacik | c8cc634 | 2013-07-01 16:18:19 -0400 | [diff] [blame] | 1132 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1133 | btrfs_set_node_blockptr(parent, parent_slot, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1134 | cow->start); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1135 | btrfs_set_node_ptr_generation(parent, parent_slot, |
| 1136 | trans->transid); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1137 | btrfs_mark_buffer_dirty(parent); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1138 | if (last_ref) { |
David Sterba | db7279a | 2018-03-05 15:14:25 +0100 | [diff] [blame] | 1139 | ret = tree_mod_log_free_eb(buf); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1140 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 1141 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 1142 | return ret; |
| 1143 | } |
| 1144 | } |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1145 | btrfs_free_tree_block(trans, root, buf, parent_start, |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1146 | last_ref); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1147 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1148 | if (unlock_orig) |
| 1149 | btrfs_tree_unlock(buf); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1150 | free_extent_buffer_stale(buf); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1151 | btrfs_mark_buffer_dirty(cow); |
| 1152 | *cow_ret = cow; |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1156 | /* |
| 1157 | * returns the logical address of the oldest predecessor of the given root. |
| 1158 | * entries older than time_seq are ignored. |
| 1159 | */ |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1160 | static struct tree_mod_elem *__tree_mod_log_oldest_root( |
| 1161 | struct extent_buffer *eb_root, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1162 | { |
| 1163 | struct tree_mod_elem *tm; |
| 1164 | struct tree_mod_elem *found = NULL; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1165 | u64 root_logical = eb_root->start; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1166 | int looped = 0; |
| 1167 | |
| 1168 | if (!time_seq) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1169 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1170 | |
| 1171 | /* |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 1172 | * the very last operation that's logged for a root is the |
| 1173 | * replacement operation (if it is replaced at all). this has |
| 1174 | * the logical address of the *new* root, making it the very |
| 1175 | * first operation that's logged for this root. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1176 | */ |
| 1177 | while (1) { |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1178 | tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1179 | time_seq); |
| 1180 | if (!looped && !tm) |
Stefan Behrens | 35a3621 | 2013-08-14 18:12:25 +0200 | [diff] [blame] | 1181 | return NULL; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1182 | /* |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1183 | * if there are no tree operation for the oldest root, we simply |
| 1184 | * return it. this should only happen if that (old) root is at |
| 1185 | * level 0. |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1186 | */ |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1187 | if (!tm) |
| 1188 | break; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1189 | |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1190 | /* |
| 1191 | * if there's an operation that's not a root replacement, we |
| 1192 | * found the oldest version of our root. normally, we'll find a |
| 1193 | * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here. |
| 1194 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1195 | if (tm->op != MOD_LOG_ROOT_REPLACE) |
| 1196 | break; |
| 1197 | |
| 1198 | found = tm; |
| 1199 | root_logical = tm->old_root.logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1200 | looped = 1; |
| 1201 | } |
| 1202 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1203 | /* if there's no old root to return, return what we found instead */ |
| 1204 | if (!found) |
| 1205 | found = tm; |
| 1206 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1207 | return found; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
| 1211 | * 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] | 1212 | * previous operations will be rewound (until we reach something older than |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1213 | * time_seq). |
| 1214 | */ |
| 1215 | static void |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1216 | __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb, |
| 1217 | u64 time_seq, struct tree_mod_elem *first_tm) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1218 | { |
| 1219 | u32 n; |
| 1220 | struct rb_node *next; |
| 1221 | struct tree_mod_elem *tm = first_tm; |
| 1222 | unsigned long o_dst; |
| 1223 | unsigned long o_src; |
| 1224 | unsigned long p_size = sizeof(struct btrfs_key_ptr); |
| 1225 | |
| 1226 | n = btrfs_header_nritems(eb); |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 1227 | read_lock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 097b8a7 | 2012-06-21 11:08:04 +0200 | [diff] [blame] | 1228 | while (tm && tm->seq >= time_seq) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1229 | /* |
| 1230 | * all the operations are recorded with the operator used for |
| 1231 | * the modification. as we're going backwards, we do the |
| 1232 | * opposite of each operation here. |
| 1233 | */ |
| 1234 | switch (tm->op) { |
| 1235 | case MOD_LOG_KEY_REMOVE_WHILE_FREEING: |
| 1236 | BUG_ON(tm->slot < n); |
Eric Sandeen | 1c697d4 | 2013-01-31 00:54:56 +0000 | [diff] [blame] | 1237 | /* Fallthrough */ |
Liu Bo | 95c80bb | 2012-10-19 09:50:52 +0000 | [diff] [blame] | 1238 | case MOD_LOG_KEY_REMOVE_WHILE_MOVING: |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1239 | case MOD_LOG_KEY_REMOVE: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1240 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1241 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1242 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1243 | tm->generation); |
Chris Mason | 4c3e696 | 2012-12-18 15:43:18 -0500 | [diff] [blame] | 1244 | n++; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1245 | break; |
| 1246 | case MOD_LOG_KEY_REPLACE: |
| 1247 | BUG_ON(tm->slot >= n); |
| 1248 | btrfs_set_node_key(eb, &tm->key, tm->slot); |
| 1249 | btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr); |
| 1250 | btrfs_set_node_ptr_generation(eb, tm->slot, |
| 1251 | tm->generation); |
| 1252 | break; |
| 1253 | case MOD_LOG_KEY_ADD: |
Jan Schmidt | 19956c7 | 2012-06-22 14:52:13 +0200 | [diff] [blame] | 1254 | /* if a move operation is needed it's in the log */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1255 | n--; |
| 1256 | break; |
| 1257 | case MOD_LOG_MOVE_KEYS: |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1258 | o_dst = btrfs_node_key_ptr_offset(tm->slot); |
| 1259 | o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot); |
| 1260 | memmove_extent_buffer(eb, o_dst, o_src, |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1261 | tm->move.nr_items * p_size); |
| 1262 | break; |
| 1263 | case MOD_LOG_ROOT_REPLACE: |
| 1264 | /* |
| 1265 | * this operation is special. for roots, this must be |
| 1266 | * handled explicitly before rewinding. |
| 1267 | * for non-roots, this operation may exist if the node |
| 1268 | * was a root: root A -> child B; then A gets empty and |
| 1269 | * B is promoted to the new root. in the mod log, we'll |
| 1270 | * have a root-replace operation for B, a tree block |
| 1271 | * that is no root. we simply ignore that operation. |
| 1272 | */ |
| 1273 | break; |
| 1274 | } |
| 1275 | next = rb_next(&tm->node); |
| 1276 | if (!next) |
| 1277 | break; |
Geliang Tang | 6b4df8b | 2016-12-19 22:53:41 +0800 | [diff] [blame] | 1278 | tm = rb_entry(next, struct tree_mod_elem, node); |
Chandan Rajendra | 298cfd3 | 2016-01-21 15:55:59 +0530 | [diff] [blame] | 1279 | if (tm->logical != first_tm->logical) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1280 | break; |
| 1281 | } |
David Sterba | b1a09f1 | 2018-03-05 15:43:41 +0100 | [diff] [blame] | 1282 | read_unlock(&fs_info->tree_mod_log_lock); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1283 | btrfs_set_header_nritems(eb, n); |
| 1284 | } |
| 1285 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1286 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1287 | * 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] | 1288 | * is returned. If rewind operations happen, a fresh buffer is returned. The |
| 1289 | * returned buffer is always read-locked. If the returned buffer is not the |
| 1290 | * input buffer, the lock on the input buffer is released and the input buffer |
| 1291 | * is freed (its refcount is decremented). |
| 1292 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1293 | static struct extent_buffer * |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1294 | tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
| 1295 | struct extent_buffer *eb, u64 time_seq) |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1296 | { |
| 1297 | struct extent_buffer *eb_rewin; |
| 1298 | struct tree_mod_elem *tm; |
| 1299 | |
| 1300 | if (!time_seq) |
| 1301 | return eb; |
| 1302 | |
| 1303 | if (btrfs_header_level(eb) == 0) |
| 1304 | return eb; |
| 1305 | |
| 1306 | tm = tree_mod_log_search(fs_info, eb->start, time_seq); |
| 1307 | if (!tm) |
| 1308 | return eb; |
| 1309 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1310 | btrfs_set_path_blocking(path); |
| 1311 | btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK); |
| 1312 | |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1313 | if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
| 1314 | BUG_ON(tm->slot != 0); |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1315 | eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1316 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1317 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1318 | free_extent_buffer(eb); |
| 1319 | return NULL; |
| 1320 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1321 | btrfs_set_header_bytenr(eb_rewin, eb->start); |
| 1322 | btrfs_set_header_backref_rev(eb_rewin, |
| 1323 | btrfs_header_backref_rev(eb)); |
| 1324 | btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb)); |
Jan Schmidt | c319310 | 2012-05-31 19:24:36 +0200 | [diff] [blame] | 1325 | btrfs_set_header_level(eb_rewin, btrfs_header_level(eb)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1326 | } else { |
| 1327 | eb_rewin = btrfs_clone_extent_buffer(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1328 | if (!eb_rewin) { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1329 | btrfs_tree_read_unlock_blocking(eb); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 1330 | free_extent_buffer(eb); |
| 1331 | return NULL; |
| 1332 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1335 | btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK); |
| 1336 | btrfs_tree_read_unlock_blocking(eb); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1337 | free_extent_buffer(eb); |
| 1338 | |
Jan Schmidt | 47fb091 | 2013-04-13 13:19:55 +0000 | [diff] [blame] | 1339 | extent_buffer_get(eb_rewin); |
| 1340 | btrfs_tree_read_lock(eb_rewin); |
Josef Bacik | f1ca7e98 | 2013-06-29 23:15:19 -0400 | [diff] [blame] | 1341 | __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm); |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 1342 | WARN_ON(btrfs_header_nritems(eb_rewin) > |
Jeff Mahoney | da17066 | 2016-06-15 09:22:56 -0400 | [diff] [blame] | 1343 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1344 | |
| 1345 | return eb_rewin; |
| 1346 | } |
| 1347 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1348 | /* |
| 1349 | * get_old_root() rewinds the state of @root's root node to the given @time_seq |
| 1350 | * value. If there are no changes, the current root->root_node is returned. If |
| 1351 | * anything changed in between, there's a fresh buffer allocated on which the |
| 1352 | * rewind operations are done. In any case, the returned buffer is read locked. |
| 1353 | * Returns NULL on error (with no locks held). |
| 1354 | */ |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1355 | static inline struct extent_buffer * |
| 1356 | get_old_root(struct btrfs_root *root, u64 time_seq) |
| 1357 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1358 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1359 | struct tree_mod_elem *tm; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1360 | struct extent_buffer *eb = NULL; |
| 1361 | struct extent_buffer *eb_root; |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1362 | struct extent_buffer *old; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1363 | struct tree_mod_root *old_root = NULL; |
Chris Mason | 4325edd | 2012-06-15 20:02:02 -0400 | [diff] [blame] | 1364 | u64 old_generation = 0; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1365 | u64 logical; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1366 | |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1367 | eb_root = btrfs_read_lock_root_node(root); |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1368 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1369 | if (!tm) |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1370 | return eb_root; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1371 | |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1372 | if (tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1373 | old_root = &tm->old_root; |
| 1374 | old_generation = tm->generation; |
| 1375 | logical = old_root->logical; |
| 1376 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1377 | logical = eb_root->start; |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1378 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1379 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1380 | tm = tree_mod_log_search(fs_info, logical, time_seq); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1381 | if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1382 | btrfs_tree_read_unlock(eb_root); |
| 1383 | free_extent_buffer(eb_root); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1384 | old = read_tree_block(fs_info, logical, 0); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 1385 | if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) { |
| 1386 | if (!IS_ERR(old)) |
| 1387 | free_extent_buffer(old); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1388 | btrfs_warn(fs_info, |
| 1389 | "failed to read tree block %llu from get_old_root", |
| 1390 | logical); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1391 | } else { |
Liu Bo | 7bfdcf7 | 2012-10-25 07:30:19 -0600 | [diff] [blame] | 1392 | eb = btrfs_clone_extent_buffer(old); |
| 1393 | free_extent_buffer(old); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1394 | } |
| 1395 | } else if (old_root) { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1396 | btrfs_tree_read_unlock(eb_root); |
| 1397 | free_extent_buffer(eb_root); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1398 | eb = alloc_dummy_extent_buffer(fs_info, logical); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1399 | } else { |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1400 | btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1401 | eb = btrfs_clone_extent_buffer(eb_root); |
Josef Bacik | 9ec7267 | 2013-08-07 16:57:23 -0400 | [diff] [blame] | 1402 | btrfs_tree_read_unlock_blocking(eb_root); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1403 | free_extent_buffer(eb_root); |
Jan Schmidt | 834328a | 2012-10-23 11:27:33 +0200 | [diff] [blame] | 1404 | } |
| 1405 | |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1406 | if (!eb) |
| 1407 | return NULL; |
Jan Schmidt | d638108 | 2012-10-23 14:21:05 +0200 | [diff] [blame] | 1408 | extent_buffer_get(eb); |
Jan Schmidt | 8ba97a1 | 2012-06-04 16:54:57 +0200 | [diff] [blame] | 1409 | btrfs_tree_read_lock(eb); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1410 | if (old_root) { |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1411 | btrfs_set_header_bytenr(eb, eb->start); |
| 1412 | btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV); |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1413 | btrfs_set_header_owner(eb, btrfs_header_owner(eb_root)); |
Jan Schmidt | a95236d | 2012-06-05 16:41:24 +0200 | [diff] [blame] | 1414 | btrfs_set_header_level(eb, old_root->level); |
| 1415 | btrfs_set_header_generation(eb, old_generation); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1416 | } |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1417 | if (tm) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1418 | __tree_mod_log_rewind(fs_info, eb, time_seq, tm); |
Jan Schmidt | 28da9fb | 2012-06-21 10:59:13 +0200 | [diff] [blame] | 1419 | else |
| 1420 | WARN_ON(btrfs_header_level(eb) != 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1421 | WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 1422 | |
| 1423 | return eb; |
| 1424 | } |
| 1425 | |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1426 | int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq) |
| 1427 | { |
| 1428 | struct tree_mod_elem *tm; |
| 1429 | int level; |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1430 | struct extent_buffer *eb_root = btrfs_root_node(root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1431 | |
David Sterba | bcd24da | 2018-03-05 15:33:18 +0100 | [diff] [blame] | 1432 | tm = __tree_mod_log_oldest_root(eb_root, time_seq); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1433 | if (tm && tm->op == MOD_LOG_ROOT_REPLACE) { |
| 1434 | level = tm->old_root.level; |
| 1435 | } else { |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1436 | level = btrfs_header_level(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1437 | } |
Jan Schmidt | 30b0463 | 2013-04-13 13:19:54 +0000 | [diff] [blame] | 1438 | free_extent_buffer(eb_root); |
Jan Schmidt | 5b6602e | 2012-10-23 11:28:27 +0200 | [diff] [blame] | 1439 | |
| 1440 | return level; |
| 1441 | } |
| 1442 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1443 | static inline int should_cow_block(struct btrfs_trans_handle *trans, |
| 1444 | struct btrfs_root *root, |
| 1445 | struct extent_buffer *buf) |
| 1446 | { |
Jeff Mahoney | f5ee5c9 | 2016-06-21 09:52:41 -0400 | [diff] [blame] | 1447 | if (btrfs_is_testing(root->fs_info)) |
Josef Bacik | faa2dbf | 2014-05-07 17:06:09 -0400 | [diff] [blame] | 1448 | return 0; |
David Sterba | fccb84c | 2014-09-29 23:53:21 +0200 | [diff] [blame] | 1449 | |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1450 | /* ensure we can see the force_cow */ |
| 1451 | smp_rmb(); |
| 1452 | |
| 1453 | /* |
| 1454 | * We do not need to cow a block if |
| 1455 | * 1) this block is not created or changed in this transaction; |
| 1456 | * 2) this block does not belong to TREE_RELOC tree; |
| 1457 | * 3) the root is not forced COW. |
| 1458 | * |
| 1459 | * What is forced COW: |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 1460 | * when we create snapshot during committing the transaction, |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1461 | * after we've finished coping src root, we must COW the shared |
| 1462 | * block to ensure the metadata consistency. |
| 1463 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1464 | if (btrfs_header_generation(buf) == trans->transid && |
| 1465 | !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) && |
| 1466 | !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID && |
Liu Bo | f1ebcc7 | 2011-11-14 20:48:06 -0500 | [diff] [blame] | 1467 | btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) && |
Miao Xie | 27cdeb7 | 2014-04-02 19:51:05 +0800 | [diff] [blame] | 1468 | !test_bit(BTRFS_ROOT_FORCE_COW, &root->state)) |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1469 | return 0; |
| 1470 | return 1; |
| 1471 | } |
| 1472 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1473 | /* |
| 1474 | * 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] | 1475 | * 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] | 1476 | * once per transaction, as long as it hasn't been written yet |
| 1477 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1478 | noinline int btrfs_cow_block(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1479 | struct btrfs_root *root, struct extent_buffer *buf, |
| 1480 | struct extent_buffer *parent, int parent_slot, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1481 | struct extent_buffer **cow_ret) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1482 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1483 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1484 | u64 search_start; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1485 | int ret; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1486 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1487 | if (trans->transaction != fs_info->running_transaction) |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1488 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Geert Uytterhoeven | c1c9ff7 | 2013-08-20 13:20:07 +0200 | [diff] [blame] | 1489 | trans->transid, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1490 | fs_info->running_transaction->transid); |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1491 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1492 | if (trans->transid != fs_info->generation) |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 1493 | WARN(1, KERN_CRIT "trans %llu running %llu\n", |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1494 | trans->transid, fs_info->generation); |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1495 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 1496 | if (!should_cow_block(trans, root, buf)) { |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 1497 | trans->dirty = true; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1498 | *cow_ret = buf; |
| 1499 | return 0; |
| 1500 | } |
Chris Mason | c487685 | 2009-02-04 09:24:25 -0500 | [diff] [blame] | 1501 | |
Byongho Lee | ee22184 | 2015-12-15 01:42:10 +0900 | [diff] [blame] | 1502 | search_start = buf->start & ~((u64)SZ_1G - 1); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1503 | |
| 1504 | if (parent) |
| 1505 | btrfs_set_lock_blocking(parent); |
| 1506 | btrfs_set_lock_blocking(buf); |
| 1507 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1508 | ret = __btrfs_cow_block(trans, root, buf, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1509 | parent_slot, cow_ret, search_start, 0); |
liubo | 1abe9b8 | 2011-03-24 11:18:59 +0000 | [diff] [blame] | 1510 | |
| 1511 | trace_btrfs_cow_block(root, buf, *cow_ret); |
| 1512 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1513 | return ret; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1516 | /* |
| 1517 | * helper function for defrag to decide if two blocks pointed to by a |
| 1518 | * node are actually close by |
| 1519 | */ |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1520 | static int close_blocks(u64 blocknr, u64 other, u32 blocksize) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1521 | { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1522 | if (blocknr < other && other - (blocknr + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1523 | return 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1524 | if (blocknr > other && blocknr - (other + blocksize) < 32768) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1525 | return 1; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 1526 | return 0; |
| 1527 | } |
| 1528 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1529 | /* |
| 1530 | * compare two keys in a memcmp fashion |
| 1531 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1532 | static int comp_keys(const struct btrfs_disk_key *disk, |
| 1533 | const struct btrfs_key *k2) |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1534 | { |
| 1535 | struct btrfs_key k1; |
| 1536 | |
| 1537 | btrfs_disk_key_to_cpu(&k1, disk); |
| 1538 | |
Diego Calleja | 20736ab | 2009-07-24 11:06:52 -0400 | [diff] [blame] | 1539 | return btrfs_comp_cpu_keys(&k1, k2); |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1540 | } |
| 1541 | |
Josef Bacik | f3465ca | 2008-11-12 14:19:50 -0500 | [diff] [blame] | 1542 | /* |
| 1543 | * same as comp_keys only with two btrfs_key's |
| 1544 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1545 | int 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] | 1546 | { |
| 1547 | if (k1->objectid > k2->objectid) |
| 1548 | return 1; |
| 1549 | if (k1->objectid < k2->objectid) |
| 1550 | return -1; |
| 1551 | if (k1->type > k2->type) |
| 1552 | return 1; |
| 1553 | if (k1->type < k2->type) |
| 1554 | return -1; |
| 1555 | if (k1->offset > k2->offset) |
| 1556 | return 1; |
| 1557 | if (k1->offset < k2->offset) |
| 1558 | return -1; |
| 1559 | return 0; |
| 1560 | } |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1561 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1562 | /* |
| 1563 | * this is used by the defrag code to go through all the |
| 1564 | * leaves pointed to by a node and reallocate them so that |
| 1565 | * disk order is close to key order |
| 1566 | */ |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1567 | int btrfs_realloc_node(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1568 | struct btrfs_root *root, struct extent_buffer *parent, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 1569 | int start_slot, u64 *last_ret, |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1570 | struct btrfs_key *progress) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1571 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1572 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1573 | struct extent_buffer *cur; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1574 | u64 blocknr; |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1575 | u64 gen; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1576 | u64 search_start = *last_ret; |
| 1577 | u64 last_block = 0; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1578 | u64 other; |
| 1579 | u32 parent_nritems; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1580 | int end_slot; |
| 1581 | int i; |
| 1582 | int err = 0; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1583 | int parent_level; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1584 | int uptodate; |
| 1585 | u32 blocksize; |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1586 | int progress_passed = 0; |
| 1587 | struct btrfs_disk_key disk_key; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1588 | |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1589 | parent_level = btrfs_header_level(parent); |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1590 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1591 | WARN_ON(trans->transaction != fs_info->running_transaction); |
| 1592 | WARN_ON(trans->transid != fs_info->generation); |
Chris Mason | 86479a0 | 2007-09-10 19:58:16 -0400 | [diff] [blame] | 1593 | |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1594 | parent_nritems = btrfs_header_nritems(parent); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1595 | blocksize = fs_info->nodesize; |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1596 | end_slot = parent_nritems - 1; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1597 | |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1598 | if (parent_nritems <= 1) |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1599 | return 0; |
| 1600 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1601 | btrfs_set_lock_blocking(parent); |
| 1602 | |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1603 | for (i = start_slot; i <= end_slot; i++) { |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1604 | int close = 1; |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 1605 | |
Chris Mason | 081e957 | 2007-11-06 10:26:24 -0500 | [diff] [blame] | 1606 | btrfs_node_key(parent, &disk_key, i); |
| 1607 | if (!progress_passed && comp_keys(&disk_key, progress) < 0) |
| 1608 | continue; |
| 1609 | |
| 1610 | progress_passed = 1; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1611 | blocknr = btrfs_node_blockptr(parent, i); |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1612 | gen = btrfs_node_ptr_generation(parent, i); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1613 | if (last_block == 0) |
| 1614 | last_block = blocknr; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1615 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1616 | if (i > 0) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1617 | other = btrfs_node_blockptr(parent, i - 1); |
| 1618 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1619 | } |
Filipe Manana | 5dfe2be | 2015-02-23 19:48:52 +0000 | [diff] [blame] | 1620 | if (!close && i < end_slot) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1621 | other = btrfs_node_blockptr(parent, i + 1); |
| 1622 | close = close_blocks(blocknr, other, blocksize); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1623 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1624 | if (close) { |
| 1625 | last_block = blocknr; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1626 | continue; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1627 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1628 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1629 | cur = find_extent_buffer(fs_info, blocknr); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1630 | if (cur) |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 1631 | uptodate = btrfs_buffer_uptodate(cur, gen, 0); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1632 | else |
| 1633 | uptodate = 0; |
Chris Mason | 5708b95 | 2007-10-25 15:43:18 -0400 | [diff] [blame] | 1634 | if (!cur || !uptodate) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1635 | if (!cur) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1636 | cur = read_tree_block(fs_info, blocknr, gen); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 1637 | if (IS_ERR(cur)) { |
| 1638 | return PTR_ERR(cur); |
| 1639 | } else if (!extent_buffer_uptodate(cur)) { |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1640 | free_extent_buffer(cur); |
Tsutomu Itoh | 97d9a8a | 2011-03-24 06:33:21 +0000 | [diff] [blame] | 1641 | return -EIO; |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1642 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1643 | } else if (!uptodate) { |
Tsutomu Itoh | 018642a | 2012-05-29 18:10:13 +0900 | [diff] [blame] | 1644 | err = btrfs_read_buffer(cur, gen); |
| 1645 | if (err) { |
| 1646 | free_extent_buffer(cur); |
| 1647 | return err; |
| 1648 | } |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1649 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1650 | } |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1651 | if (search_start == 0) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1652 | search_start = last_block; |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 1653 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1654 | btrfs_tree_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1655 | btrfs_set_lock_blocking(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1656 | err = __btrfs_cow_block(trans, root, cur, parent, i, |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1657 | &cur, search_start, |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1658 | min(16 * blocksize, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1659 | (end_slot - i) * blocksize)); |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1660 | if (err) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1661 | btrfs_tree_unlock(cur); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 1662 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1663 | break; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 1664 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1665 | search_start = cur->start; |
| 1666 | last_block = cur->start; |
Chris Mason | f2183bd | 2007-08-10 14:42:37 -0400 | [diff] [blame] | 1667 | *last_ret = search_start; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 1668 | btrfs_tree_unlock(cur); |
| 1669 | free_extent_buffer(cur); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1670 | } |
| 1671 | return err; |
| 1672 | } |
| 1673 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1674 | /* |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1675 | * search for key in the extent_buffer. The items start at offset p, |
| 1676 | * and they are item_size apart. There are 'max' items in p. |
| 1677 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 1678 | * the slot in the array is returned via slot, and it points to |
| 1679 | * the place where you would insert key if it is not found in |
| 1680 | * the array. |
| 1681 | * |
| 1682 | * slot may point to max if the key is bigger than all of the keys |
| 1683 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1684 | static noinline int generic_bin_search(struct extent_buffer *eb, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 1685 | unsigned long p, int item_size, |
| 1686 | const struct btrfs_key *key, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1687 | int max, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1688 | { |
| 1689 | int low = 0; |
| 1690 | int high = max; |
| 1691 | int mid; |
| 1692 | int ret; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1693 | struct btrfs_disk_key *tmp = NULL; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1694 | struct btrfs_disk_key unaligned; |
| 1695 | unsigned long offset; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1696 | char *kaddr = NULL; |
| 1697 | unsigned long map_start = 0; |
| 1698 | unsigned long map_len = 0; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1699 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1700 | |
Liu Bo | 5e24e9a | 2016-06-23 16:32:45 -0700 | [diff] [blame] | 1701 | if (low > high) { |
| 1702 | btrfs_err(eb->fs_info, |
| 1703 | "%s: low (%d) > high (%d) eb %llu owner %llu level %d", |
| 1704 | __func__, low, high, eb->start, |
| 1705 | btrfs_header_owner(eb), btrfs_header_level(eb)); |
| 1706 | return -EINVAL; |
| 1707 | } |
| 1708 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 1709 | while (low < high) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1710 | mid = (low + high) / 2; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1711 | offset = p + mid * item_size; |
| 1712 | |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1713 | if (!kaddr || offset < map_start || |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1714 | (offset + sizeof(struct btrfs_disk_key)) > |
| 1715 | map_start + map_len) { |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 1716 | |
| 1717 | err = map_private_extent_buffer(eb, offset, |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1718 | sizeof(struct btrfs_disk_key), |
Chris Mason | a659171 | 2011-07-19 12:04:14 -0400 | [diff] [blame] | 1719 | &kaddr, &map_start, &map_len); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1720 | |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1721 | if (!err) { |
| 1722 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1723 | map_start); |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 1724 | } else if (err == 1) { |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1725 | read_extent_buffer(eb, &unaligned, |
| 1726 | offset, sizeof(unaligned)); |
| 1727 | tmp = &unaligned; |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 1728 | } else { |
| 1729 | return err; |
Chris Mason | 479965d | 2007-10-15 16:14:27 -0400 | [diff] [blame] | 1730 | } |
| 1731 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1732 | } else { |
| 1733 | tmp = (struct btrfs_disk_key *)(kaddr + offset - |
| 1734 | map_start); |
| 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, |
| 1756 | int level, int *slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 1757 | { |
Wang Sheng-Hui | f775738 | 2012-03-30 15:14:27 +0800 | [diff] [blame] | 1758 | if (level == 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 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1791 | static noinline struct extent_buffer * |
| 1792 | read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent, |
| 1793 | int slot) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1794 | { |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 1795 | int level = btrfs_header_level(parent); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1796 | struct extent_buffer *eb; |
| 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 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1803 | eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot), |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1804 | btrfs_node_ptr_generation(parent, slot)); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1805 | if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) { |
| 1806 | free_extent_buffer(eb); |
| 1807 | eb = ERR_PTR(-EIO); |
Josef Bacik | 416bc65 | 2013-04-23 14:17:42 -0400 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | return eb; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1811 | } |
| 1812 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 1813 | /* |
| 1814 | * node level balancing, used to make sure nodes are in proper order for |
| 1815 | * item deletion. We balance from the top down, so we have to make sure |
| 1816 | * that a deletion won't leave an node completely empty later on. |
| 1817 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 1818 | static noinline int balance_level(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1819 | struct btrfs_root *root, |
| 1820 | struct btrfs_path *path, int level) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1821 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1822 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1823 | struct extent_buffer *right = NULL; |
| 1824 | struct extent_buffer *mid; |
| 1825 | struct extent_buffer *left = NULL; |
| 1826 | struct extent_buffer *parent = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1827 | int ret = 0; |
| 1828 | int wret; |
| 1829 | int pslot; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1830 | int orig_slot = path->slots[level]; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1831 | u64 orig_ptr; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1832 | |
| 1833 | if (level == 0) |
| 1834 | return 0; |
| 1835 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1836 | mid = path->nodes[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1837 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 1838 | WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK && |
| 1839 | path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1840 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
| 1841 | |
Chris Mason | 1d4f8a0 | 2007-03-13 09:28:32 -0400 | [diff] [blame] | 1842 | orig_ptr = btrfs_node_blockptr(mid, orig_slot); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1843 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1844 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1845 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 1846 | pslot = path->slots[level + 1]; |
| 1847 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1848 | |
Chris Mason | 4068947 | 2007-03-17 14:29:23 -0400 | [diff] [blame] | 1849 | /* |
| 1850 | * deal with the case where there is only one pointer in the root |
| 1851 | * by promoting the node below to a root |
| 1852 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1853 | if (!parent) { |
| 1854 | struct extent_buffer *child; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1855 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1856 | if (btrfs_header_nritems(mid) != 1) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1857 | return 0; |
| 1858 | |
| 1859 | /* promote the child to a root */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1860 | child = read_node_slot(fs_info, mid, 0); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1861 | if (IS_ERR(child)) { |
| 1862 | ret = PTR_ERR(child); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1863 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1864 | goto enospc; |
| 1865 | } |
| 1866 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1867 | btrfs_tree_lock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1868 | btrfs_set_lock_blocking(child); |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1869 | ret = btrfs_cow_block(trans, root, child, mid, 0, &child); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1870 | if (ret) { |
| 1871 | btrfs_tree_unlock(child); |
| 1872 | free_extent_buffer(child); |
| 1873 | goto enospc; |
| 1874 | } |
Yan | 2f375ab | 2008-02-01 14:58:07 -0500 | [diff] [blame] | 1875 | |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 1876 | tree_mod_log_set_root_pointer(root, child, 1); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 1877 | rcu_assign_pointer(root->node, child); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1878 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1879 | add_root_to_dirty_list(root); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1880 | btrfs_tree_unlock(child); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1881 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1882 | path->locks[level] = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1883 | path->nodes[level] = NULL; |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 1884 | clean_tree_block(fs_info, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1885 | btrfs_tree_unlock(mid); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1886 | /* once for the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1887 | free_extent_buffer(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1888 | |
| 1889 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1890 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1891 | /* once for the root ptr */ |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1892 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1893 | return 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1894 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1895 | if (btrfs_header_nritems(mid) > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1896 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4) |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1897 | return 0; |
| 1898 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1899 | left = read_node_slot(fs_info, parent, pslot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1900 | if (IS_ERR(left)) |
| 1901 | left = NULL; |
| 1902 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1903 | if (left) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1904 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1905 | btrfs_set_lock_blocking(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1906 | wret = btrfs_cow_block(trans, root, left, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1907 | parent, pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1908 | if (wret) { |
| 1909 | ret = wret; |
| 1910 | goto enospc; |
| 1911 | } |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1912 | } |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1913 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1914 | right = read_node_slot(fs_info, parent, pslot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 1915 | if (IS_ERR(right)) |
| 1916 | right = NULL; |
| 1917 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1918 | if (right) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1919 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 1920 | btrfs_set_lock_blocking(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1921 | wret = btrfs_cow_block(trans, root, right, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 1922 | parent, pslot + 1, &right); |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 1923 | if (wret) { |
| 1924 | ret = wret; |
| 1925 | goto enospc; |
| 1926 | } |
| 1927 | } |
| 1928 | |
| 1929 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1930 | if (left) { |
| 1931 | orig_slot += btrfs_header_nritems(left); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1932 | wret = push_node_left(trans, fs_info, left, mid, 1); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1933 | if (wret < 0) |
| 1934 | ret = wret; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1935 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1936 | |
| 1937 | /* |
| 1938 | * then try to empty the right most buffer into the middle |
| 1939 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1940 | if (right) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1941 | wret = push_node_left(trans, fs_info, mid, right, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1942 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1943 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1944 | if (btrfs_header_nritems(right) == 0) { |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 1945 | clean_tree_block(fs_info, right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1946 | btrfs_tree_unlock(right); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1947 | del_ptr(root, path, level + 1, pslot + 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1948 | root_sub_used(root, right->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1949 | btrfs_free_tree_block(trans, root, right, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1950 | free_extent_buffer_stale(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1951 | right = NULL; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1952 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1953 | struct btrfs_disk_key right_key; |
| 1954 | btrfs_node_key(right, &right_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame^] | 1955 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
| 1956 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 1957 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1958 | btrfs_set_node_key(parent, &right_key, pslot + 1); |
| 1959 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 1960 | } |
| 1961 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1962 | if (btrfs_header_nritems(mid) == 1) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1963 | /* |
| 1964 | * we're not allowed to leave a node with one item in the |
| 1965 | * tree during a delete. A deletion from lower in the tree |
| 1966 | * could try to delete the only pointer in this node. |
| 1967 | * So, pull some keys from the left. |
| 1968 | * There has to be a left pointer at this point because |
| 1969 | * otherwise we would have pulled some pointers from the |
| 1970 | * right |
| 1971 | */ |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1972 | if (!left) { |
| 1973 | ret = -EROFS; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 1974 | btrfs_handle_fs_error(fs_info, ret, NULL); |
Mark Fasheh | 305a26a | 2011-09-01 11:27:57 -0700 | [diff] [blame] | 1975 | goto enospc; |
| 1976 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1977 | wret = balance_node_right(trans, fs_info, mid, left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1978 | if (wret < 0) { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1979 | ret = wret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1980 | goto enospc; |
| 1981 | } |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1982 | if (wret == 1) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 1983 | wret = push_node_left(trans, fs_info, left, mid, 1); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 1984 | if (wret < 0) |
| 1985 | ret = wret; |
| 1986 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1987 | BUG_ON(wret == 1); |
| 1988 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1989 | if (btrfs_header_nritems(mid) == 0) { |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 1990 | clean_tree_block(fs_info, mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 1991 | btrfs_tree_unlock(mid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 1992 | del_ptr(root, path, level + 1, pslot); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1993 | root_sub_used(root, mid->len); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 1994 | btrfs_free_tree_block(trans, root, mid, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 1995 | free_extent_buffer_stale(mid); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 1996 | mid = NULL; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 1997 | } else { |
| 1998 | /* update the parent key to reflect our changes */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1999 | struct btrfs_disk_key mid_key; |
| 2000 | btrfs_node_key(mid, &mid_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame^] | 2001 | ret = tree_mod_log_insert_key(parent, pslot, |
| 2002 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2003 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2004 | btrfs_set_node_key(parent, &mid_key, pslot); |
| 2005 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2006 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2007 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2008 | /* update the path */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2009 | if (left) { |
| 2010 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2011 | extent_buffer_get(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2012 | /* left was locked after cow */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2013 | path->nodes[level] = left; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2014 | path->slots[level + 1] -= 1; |
| 2015 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2016 | if (mid) { |
| 2017 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2018 | free_extent_buffer(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2019 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2020 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2021 | orig_slot -= btrfs_header_nritems(left); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2022 | path->slots[level] = orig_slot; |
| 2023 | } |
| 2024 | } |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2025 | /* double check we haven't messed things up */ |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2026 | if (orig_ptr != |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2027 | btrfs_node_blockptr(path->nodes[level], path->slots[level])) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 2028 | BUG(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2029 | enospc: |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2030 | if (right) { |
| 2031 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2032 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2033 | } |
| 2034 | if (left) { |
| 2035 | if (path->nodes[level] != left) |
| 2036 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2037 | free_extent_buffer(left); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2038 | } |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2039 | return ret; |
| 2040 | } |
| 2041 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2042 | /* Node balancing for insertion. Here we only split or push nodes around |
| 2043 | * when they are completely full. This is also done top down, so we |
| 2044 | * have to be pessimistic. |
| 2045 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2046 | static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2047 | struct btrfs_root *root, |
| 2048 | struct btrfs_path *path, int level) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2049 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2050 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2051 | struct extent_buffer *right = NULL; |
| 2052 | struct extent_buffer *mid; |
| 2053 | struct extent_buffer *left = NULL; |
| 2054 | struct extent_buffer *parent = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2055 | int ret = 0; |
| 2056 | int wret; |
| 2057 | int pslot; |
| 2058 | int orig_slot = path->slots[level]; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2059 | |
| 2060 | if (level == 0) |
| 2061 | return 1; |
| 2062 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2063 | mid = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2064 | WARN_ON(btrfs_header_generation(mid) != trans->transid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2065 | |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 2066 | if (level < BTRFS_MAX_LEVEL - 1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2067 | parent = path->nodes[level + 1]; |
Li Zefan | a05a9bb | 2011-09-06 16:55:34 +0800 | [diff] [blame] | 2068 | pslot = path->slots[level + 1]; |
| 2069 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2070 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2071 | if (!parent) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2072 | return 1; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2073 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2074 | left = read_node_slot(fs_info, parent, pslot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 2075 | if (IS_ERR(left)) |
| 2076 | left = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2077 | |
| 2078 | /* first, try to make some room in the middle buffer */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2079 | if (left) { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2080 | u32 left_nr; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2081 | |
| 2082 | btrfs_tree_lock(left); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2083 | btrfs_set_lock_blocking(left); |
| 2084 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2085 | left_nr = btrfs_header_nritems(left); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2086 | if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2087 | wret = 1; |
| 2088 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2089 | ret = btrfs_cow_block(trans, root, left, parent, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 2090 | pslot - 1, &left); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2091 | if (ret) |
| 2092 | wret = 1; |
| 2093 | else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2094 | wret = push_node_left(trans, fs_info, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 2095 | left, mid, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2096 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2097 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2098 | if (wret < 0) |
| 2099 | ret = wret; |
| 2100 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2101 | struct btrfs_disk_key disk_key; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2102 | orig_slot += left_nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2103 | btrfs_node_key(mid, &disk_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame^] | 2104 | ret = tree_mod_log_insert_key(parent, pslot, |
| 2105 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2106 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2107 | btrfs_set_node_key(parent, &disk_key, pslot); |
| 2108 | btrfs_mark_buffer_dirty(parent); |
| 2109 | if (btrfs_header_nritems(left) > orig_slot) { |
| 2110 | path->nodes[level] = left; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2111 | path->slots[level + 1] -= 1; |
| 2112 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2113 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2114 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2115 | } else { |
| 2116 | orig_slot -= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2117 | btrfs_header_nritems(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2118 | path->slots[level] = orig_slot; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2119 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2120 | free_extent_buffer(left); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2121 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2122 | return 0; |
| 2123 | } |
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 | } |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2127 | right = read_node_slot(fs_info, parent, pslot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 2128 | if (IS_ERR(right)) |
| 2129 | right = NULL; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2130 | |
| 2131 | /* |
| 2132 | * then try to empty the right most buffer into the middle |
| 2133 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2134 | if (right) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2135 | u32 right_nr; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2136 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2137 | btrfs_tree_lock(right); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2138 | btrfs_set_lock_blocking(right); |
| 2139 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2140 | right_nr = btrfs_header_nritems(right); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2141 | if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) { |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2142 | wret = 1; |
| 2143 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2144 | ret = btrfs_cow_block(trans, root, right, |
| 2145 | parent, pslot + 1, |
Chris Mason | 9fa8cfe | 2009-03-13 10:24:59 -0400 | [diff] [blame] | 2146 | &right); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2147 | if (ret) |
| 2148 | wret = 1; |
| 2149 | else { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2150 | wret = balance_node_right(trans, fs_info, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2151 | right, mid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2152 | } |
Chris Mason | 33ade1f | 2007-04-20 13:48:57 -0400 | [diff] [blame] | 2153 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2154 | if (wret < 0) |
| 2155 | ret = wret; |
| 2156 | if (wret == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2157 | struct btrfs_disk_key disk_key; |
| 2158 | |
| 2159 | btrfs_node_key(right, &disk_key, 0); |
David Sterba | 0e82bcf | 2018-03-05 16:16:54 +0100 | [diff] [blame^] | 2160 | ret = tree_mod_log_insert_key(parent, pslot + 1, |
| 2161 | MOD_LOG_KEY_REPLACE, GFP_NOFS); |
| 2162 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2163 | btrfs_set_node_key(parent, &disk_key, pslot + 1); |
| 2164 | btrfs_mark_buffer_dirty(parent); |
| 2165 | |
| 2166 | if (btrfs_header_nritems(mid) <= orig_slot) { |
| 2167 | path->nodes[level] = right; |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2168 | path->slots[level + 1] += 1; |
| 2169 | path->slots[level] = orig_slot - |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2170 | btrfs_header_nritems(mid); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2171 | btrfs_tree_unlock(mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2172 | free_extent_buffer(mid); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2173 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2174 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2175 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2176 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2177 | return 0; |
| 2178 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2179 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2180 | free_extent_buffer(right); |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2181 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 2182 | return 1; |
| 2183 | } |
| 2184 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2185 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2186 | * readahead one full node of leaves, finding things that are close |
| 2187 | * to the block in 'slot', and triggering ra on them. |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2188 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2189 | static void reada_for_search(struct btrfs_fs_info *fs_info, |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2190 | struct btrfs_path *path, |
| 2191 | int level, int slot, u64 objectid) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2192 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2193 | struct extent_buffer *node; |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2194 | struct btrfs_disk_key disk_key; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2195 | u32 nritems; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2196 | u64 search; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2197 | u64 target; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2198 | u64 nread = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2199 | struct extent_buffer *eb; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2200 | u32 nr; |
| 2201 | u32 blocksize; |
| 2202 | u32 nscan = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2203 | |
Chris Mason | a6b6e75 | 2007-10-15 16:22:39 -0400 | [diff] [blame] | 2204 | if (level != 1) |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2205 | return; |
| 2206 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2207 | if (!path->nodes[level]) |
| 2208 | return; |
| 2209 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2210 | node = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2211 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2212 | search = btrfs_node_blockptr(node, slot); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2213 | blocksize = fs_info->nodesize; |
| 2214 | eb = find_extent_buffer(fs_info, search); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2215 | if (eb) { |
| 2216 | free_extent_buffer(eb); |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2217 | return; |
| 2218 | } |
| 2219 | |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2220 | target = search; |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2221 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2222 | nritems = btrfs_header_nritems(node); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2223 | nr = slot; |
Josef Bacik | 25b8b93 | 2011-06-08 14:36:54 -0400 | [diff] [blame] | 2224 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2225 | while (1) { |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2226 | if (path->reada == READA_BACK) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2227 | if (nr == 0) |
| 2228 | break; |
| 2229 | nr--; |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2230 | } else if (path->reada == READA_FORWARD) { |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2231 | nr++; |
| 2232 | if (nr >= nritems) |
| 2233 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2234 | } |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2235 | if (path->reada == READA_BACK && objectid) { |
Chris Mason | 01f4665 | 2007-12-21 16:24:26 -0500 | [diff] [blame] | 2236 | btrfs_node_key(node, &disk_key, nr); |
| 2237 | if (btrfs_disk_key_objectid(&disk_key) != objectid) |
| 2238 | break; |
| 2239 | } |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2240 | search = btrfs_node_blockptr(node, nr); |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2241 | if ((search <= target && target - search <= 65536) || |
| 2242 | (search > target && search - target <= 65536)) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2243 | readahead_tree_block(fs_info, search); |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2244 | nread += blocksize; |
| 2245 | } |
| 2246 | nscan++; |
Chris Mason | a717531 | 2009-01-22 09:23:10 -0500 | [diff] [blame] | 2247 | if ((nread > 65536 || nscan > 32)) |
Chris Mason | 6b80053 | 2007-10-15 16:17:34 -0400 | [diff] [blame] | 2248 | break; |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2249 | } |
| 2250 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2251 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2252 | static noinline void reada_for_balance(struct btrfs_fs_info *fs_info, |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2253 | struct btrfs_path *path, int level) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2254 | { |
| 2255 | int slot; |
| 2256 | int nritems; |
| 2257 | struct extent_buffer *parent; |
| 2258 | struct extent_buffer *eb; |
| 2259 | u64 gen; |
| 2260 | u64 block1 = 0; |
| 2261 | u64 block2 = 0; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2262 | |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2263 | parent = path->nodes[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2264 | if (!parent) |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2265 | return; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2266 | |
| 2267 | nritems = btrfs_header_nritems(parent); |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2268 | slot = path->slots[level + 1]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2269 | |
| 2270 | if (slot > 0) { |
| 2271 | block1 = btrfs_node_blockptr(parent, slot - 1); |
| 2272 | gen = btrfs_node_ptr_generation(parent, slot - 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2273 | eb = find_extent_buffer(fs_info, block1); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2274 | /* |
| 2275 | * if we get -eagain from btrfs_buffer_uptodate, we |
| 2276 | * don't want to return eagain here. That will loop |
| 2277 | * forever |
| 2278 | */ |
| 2279 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2280 | block1 = 0; |
| 2281 | free_extent_buffer(eb); |
| 2282 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2283 | if (slot + 1 < nritems) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2284 | block2 = btrfs_node_blockptr(parent, slot + 1); |
| 2285 | gen = btrfs_node_ptr_generation(parent, slot + 1); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2286 | eb = find_extent_buffer(fs_info, block2); |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2287 | if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2288 | block2 = 0; |
| 2289 | free_extent_buffer(eb); |
| 2290 | } |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2291 | |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2292 | if (block1) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2293 | readahead_tree_block(fs_info, block1); |
Josef Bacik | 0b08851 | 2013-06-17 14:23:02 -0400 | [diff] [blame] | 2294 | if (block2) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2295 | readahead_tree_block(fs_info, block2); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2296 | } |
| 2297 | |
| 2298 | |
| 2299 | /* |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2300 | * when we walk down the tree, it is usually safe to unlock the higher layers |
| 2301 | * in the tree. The exceptions are when our path goes through slot 0, because |
| 2302 | * operations on the tree might require changing key pointers higher up in the |
| 2303 | * tree. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2304 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2305 | * callers might also have set path->keep_locks, which tells this code to keep |
| 2306 | * the lock if the path points to the last slot in the block. This is part of |
| 2307 | * 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] | 2308 | * |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 2309 | * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so |
| 2310 | * if lowest_unlock is 1, level 0 won't be unlocked |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 2311 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 2312 | static noinline void unlock_up(struct btrfs_path *path, int level, |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2313 | int lowest_unlock, int min_write_lock_level, |
| 2314 | int *write_lock_level) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2315 | { |
| 2316 | int i; |
| 2317 | int skip_level = level; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2318 | int no_skips = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2319 | struct extent_buffer *t; |
| 2320 | |
| 2321 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2322 | if (!path->nodes[i]) |
| 2323 | break; |
| 2324 | if (!path->locks[i]) |
| 2325 | break; |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2326 | if (!no_skips && path->slots[i] == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2327 | skip_level = i + 1; |
| 2328 | continue; |
| 2329 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2330 | if (!no_skips && path->keep_locks) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2331 | u32 nritems; |
| 2332 | t = path->nodes[i]; |
| 2333 | nritems = btrfs_header_nritems(t); |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2334 | if (nritems < 1 || path->slots[i] >= nritems - 1) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2335 | skip_level = i + 1; |
| 2336 | continue; |
| 2337 | } |
| 2338 | } |
Chris Mason | 051e1b9 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2339 | if (skip_level < i && i >= lowest_unlock) |
| 2340 | no_skips = 1; |
| 2341 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2342 | t = path->nodes[i]; |
| 2343 | if (i >= lowest_unlock && i > skip_level && path->locks[i]) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2344 | btrfs_tree_unlock_rw(t, path->locks[i]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2345 | path->locks[i] = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2346 | if (write_lock_level && |
| 2347 | i > min_write_lock_level && |
| 2348 | i <= *write_lock_level) { |
| 2349 | *write_lock_level = i - 1; |
| 2350 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2351 | } |
| 2352 | } |
| 2353 | } |
| 2354 | |
Chris Mason | 3c69fae | 2007-08-07 15:52:22 -0400 | [diff] [blame] | 2355 | /* |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2356 | * This releases any locks held in the path starting at level and |
| 2357 | * going all the way up to the root. |
| 2358 | * |
| 2359 | * btrfs_search_slot will keep the lock held on higher nodes in a few |
| 2360 | * corner cases, such as COW of the block at slot zero in the node. This |
| 2361 | * ignores those rules, and it should only be called when there are no |
| 2362 | * more updates to be done higher up in the tree. |
| 2363 | */ |
| 2364 | noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level) |
| 2365 | { |
| 2366 | int i; |
| 2367 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2368 | if (path->keep_locks) |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2369 | return; |
| 2370 | |
| 2371 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2372 | if (!path->nodes[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2373 | continue; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2374 | if (!path->locks[i]) |
Chris Mason | 12f4dac | 2009-02-04 09:31:42 -0500 | [diff] [blame] | 2375 | continue; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2376 | btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2377 | path->locks[i] = 0; |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | /* |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2382 | * helper function for btrfs_search_slot. The goal is to find a block |
| 2383 | * in cache without setting the path to blocking. If we find the block |
| 2384 | * we return zero and the path is unchanged. |
| 2385 | * |
| 2386 | * If we can't find the block, we set the path blocking and do some |
| 2387 | * reada. -EAGAIN is returned and the search must be repeated. |
| 2388 | */ |
| 2389 | static int |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 2390 | read_block_for_search(struct btrfs_root *root, struct btrfs_path *p, |
| 2391 | struct extent_buffer **eb_ret, int level, int slot, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 2392 | const struct btrfs_key *key) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2393 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2394 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2395 | u64 blocknr; |
| 2396 | u64 gen; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2397 | struct extent_buffer *b = *eb_ret; |
| 2398 | struct extent_buffer *tmp; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2399 | int ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2400 | |
| 2401 | blocknr = btrfs_node_blockptr(b, slot); |
| 2402 | gen = btrfs_node_ptr_generation(b, slot); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2403 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2404 | tmp = find_extent_buffer(fs_info, blocknr); |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2405 | if (tmp) { |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2406 | /* first we do an atomic uptodate check */ |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2407 | if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) { |
| 2408 | *eb_ret = tmp; |
| 2409 | return 0; |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2410 | } |
Josef Bacik | bdf7c00 | 2013-06-17 13:44:48 -0400 | [diff] [blame] | 2411 | |
| 2412 | /* the pages were up to date, but we failed |
| 2413 | * the generation number check. Do a full |
| 2414 | * read for the generation number that is correct. |
| 2415 | * We must do this without dropping locks so |
| 2416 | * we can trust our generation number |
| 2417 | */ |
| 2418 | btrfs_set_path_blocking(p); |
| 2419 | |
| 2420 | /* now we're allowed to do a blocking uptodate check */ |
| 2421 | ret = btrfs_read_buffer(tmp, gen); |
| 2422 | if (!ret) { |
| 2423 | *eb_ret = tmp; |
| 2424 | return 0; |
| 2425 | } |
| 2426 | free_extent_buffer(tmp); |
| 2427 | btrfs_release_path(p); |
| 2428 | return -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | /* |
| 2432 | * reduce lock contention at high levels |
| 2433 | * of the btree by dropping locks before |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2434 | * we read. Don't release the lock on the current |
| 2435 | * level because we need to walk this node to figure |
| 2436 | * out which blocks to read. |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2437 | */ |
Chris Mason | 8c594ea | 2009-04-20 15:50:10 -0400 | [diff] [blame] | 2438 | btrfs_unlock_up_safe(p, level + 1); |
| 2439 | btrfs_set_path_blocking(p); |
| 2440 | |
Chris Mason | cb44921 | 2010-10-24 11:01:27 -0400 | [diff] [blame] | 2441 | free_extent_buffer(tmp); |
David Sterba | e4058b5 | 2015-11-27 16:31:35 +0100 | [diff] [blame] | 2442 | if (p->reada != READA_NONE) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2443 | reada_for_search(fs_info, p, level, slot, key->objectid); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2444 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2445 | btrfs_release_path(p); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2446 | |
| 2447 | ret = -EAGAIN; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2448 | tmp = read_tree_block(fs_info, blocknr, 0); |
Liu Bo | 64c043d | 2015-05-25 17:30:15 +0800 | [diff] [blame] | 2449 | if (!IS_ERR(tmp)) { |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2450 | /* |
| 2451 | * If the read above didn't mark this buffer up to date, |
| 2452 | * it will never end up being up to date. Set ret to EIO now |
| 2453 | * and give up so that our caller doesn't loop forever |
| 2454 | * on our EAGAINs. |
| 2455 | */ |
Chris Mason | b9fab91 | 2012-05-06 07:23:47 -0400 | [diff] [blame] | 2456 | if (!btrfs_buffer_uptodate(tmp, 0, 0)) |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2457 | ret = -EIO; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2458 | free_extent_buffer(tmp); |
Liu Bo | c871b0f | 2016-06-06 12:01:23 -0700 | [diff] [blame] | 2459 | } else { |
| 2460 | ret = PTR_ERR(tmp); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2461 | } |
| 2462 | return ret; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | /* |
| 2466 | * helper function for btrfs_search_slot. This does all of the checks |
| 2467 | * for node-level blocks and does any balancing required based on |
| 2468 | * the ins_len. |
| 2469 | * |
| 2470 | * If no extra work was required, zero is returned. If we had to |
| 2471 | * drop the path, -EAGAIN is returned and btrfs_search_slot must |
| 2472 | * start over |
| 2473 | */ |
| 2474 | static int |
| 2475 | setup_nodes_for_search(struct btrfs_trans_handle *trans, |
| 2476 | struct btrfs_root *root, struct btrfs_path *p, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2477 | struct extent_buffer *b, int level, int ins_len, |
| 2478 | int *write_lock_level) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2479 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2480 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2481 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2482 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2483 | if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >= |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2484 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2485 | int sret; |
| 2486 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2487 | if (*write_lock_level < level + 1) { |
| 2488 | *write_lock_level = level + 1; |
| 2489 | btrfs_release_path(p); |
| 2490 | goto again; |
| 2491 | } |
| 2492 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2493 | btrfs_set_path_blocking(p); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2494 | reada_for_balance(fs_info, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2495 | sret = split_node(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2496 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2497 | |
| 2498 | BUG_ON(sret > 0); |
| 2499 | if (sret) { |
| 2500 | ret = sret; |
| 2501 | goto done; |
| 2502 | } |
| 2503 | b = p->nodes[level]; |
| 2504 | } else if (ins_len < 0 && btrfs_header_nritems(b) < |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2505 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) { |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2506 | int sret; |
| 2507 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2508 | if (*write_lock_level < level + 1) { |
| 2509 | *write_lock_level = level + 1; |
| 2510 | btrfs_release_path(p); |
| 2511 | goto again; |
| 2512 | } |
| 2513 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2514 | btrfs_set_path_blocking(p); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2515 | reada_for_balance(fs_info, p, level); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2516 | sret = balance_level(trans, root, p, level); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2517 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2518 | |
| 2519 | if (sret) { |
| 2520 | ret = sret; |
| 2521 | goto done; |
| 2522 | } |
| 2523 | b = p->nodes[level]; |
| 2524 | if (!b) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2525 | btrfs_release_path(p); |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2526 | goto again; |
| 2527 | } |
| 2528 | BUG_ON(btrfs_header_nritems(b) == 1); |
| 2529 | } |
| 2530 | return 0; |
| 2531 | |
| 2532 | again: |
| 2533 | ret = -EAGAIN; |
| 2534 | done: |
| 2535 | return ret; |
| 2536 | } |
| 2537 | |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2538 | static void key_search_validate(struct extent_buffer *b, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2539 | const struct btrfs_key *key, |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2540 | int level) |
| 2541 | { |
| 2542 | #ifdef CONFIG_BTRFS_ASSERT |
| 2543 | struct btrfs_disk_key disk_key; |
| 2544 | |
| 2545 | btrfs_cpu_key_to_disk(&disk_key, key); |
| 2546 | |
| 2547 | if (level == 0) |
| 2548 | ASSERT(!memcmp_extent_buffer(b, &disk_key, |
| 2549 | offsetof(struct btrfs_leaf, items[0].key), |
| 2550 | sizeof(disk_key))); |
| 2551 | else |
| 2552 | ASSERT(!memcmp_extent_buffer(b, &disk_key, |
| 2553 | offsetof(struct btrfs_node, ptrs[0].key), |
| 2554 | sizeof(disk_key))); |
| 2555 | #endif |
| 2556 | } |
| 2557 | |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2558 | static int key_search(struct extent_buffer *b, const struct btrfs_key *key, |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2559 | int level, int *prev_cmp, int *slot) |
| 2560 | { |
| 2561 | if (*prev_cmp != 0) { |
Nikolay Borisov | a74b35e | 2017-12-08 16:27:43 +0200 | [diff] [blame] | 2562 | *prev_cmp = btrfs_bin_search(b, key, level, slot); |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2563 | return *prev_cmp; |
| 2564 | } |
| 2565 | |
| 2566 | key_search_validate(b, key, level); |
| 2567 | *slot = 0; |
| 2568 | |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 2572 | 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] | 2573 | u64 iobjectid, u64 ioff, u8 key_type, |
| 2574 | struct btrfs_key *found_key) |
| 2575 | { |
| 2576 | int ret; |
| 2577 | struct btrfs_key key; |
| 2578 | struct extent_buffer *eb; |
David Sterba | 381cf65 | 2015-01-02 18:45:16 +0100 | [diff] [blame] | 2579 | |
| 2580 | ASSERT(path); |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 2581 | ASSERT(found_key); |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2582 | |
| 2583 | key.type = key_type; |
| 2584 | key.objectid = iobjectid; |
| 2585 | key.offset = ioff; |
| 2586 | |
| 2587 | ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0); |
David Sterba | 1d4c08e | 2015-01-02 19:36:14 +0100 | [diff] [blame] | 2588 | if (ret < 0) |
Kelley Nielsen | e33d5c3 | 2013-11-04 19:33:33 -0800 | [diff] [blame] | 2589 | return ret; |
| 2590 | |
| 2591 | eb = path->nodes[0]; |
| 2592 | if (ret && path->slots[0] >= btrfs_header_nritems(eb)) { |
| 2593 | ret = btrfs_next_leaf(fs_root, path); |
| 2594 | if (ret) |
| 2595 | return ret; |
| 2596 | eb = path->nodes[0]; |
| 2597 | } |
| 2598 | |
| 2599 | btrfs_item_key_to_cpu(eb, found_key, path->slots[0]); |
| 2600 | if (found_key->type != key.type || |
| 2601 | found_key->objectid != key.objectid) |
| 2602 | return 1; |
| 2603 | |
| 2604 | return 0; |
| 2605 | } |
| 2606 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2607 | /* |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2608 | * btrfs_search_slot - look for a key in a tree and perform necessary |
| 2609 | * modifications to preserve tree invariants. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2610 | * |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2611 | * @trans: Handle of transaction, used when modifying the tree |
| 2612 | * @p: Holds all btree nodes along the search path |
| 2613 | * @root: The root node of the tree |
| 2614 | * @key: The key we are looking for |
| 2615 | * @ins_len: Indicates purpose of search, for inserts it is 1, for |
| 2616 | * deletions it's -1. 0 for plain searches |
| 2617 | * @cow: boolean should CoW operations be performed. Must always be 1 |
| 2618 | * when modifying the tree. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 2619 | * |
Nikolay Borisov | 4271ece | 2017-12-13 09:38:14 +0200 | [diff] [blame] | 2620 | * If @ins_len > 0, nodes and leaves will be split as we walk down the tree. |
| 2621 | * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible) |
| 2622 | * |
| 2623 | * If @key is found, 0 is returned and you can find the item in the leaf level |
| 2624 | * of the path (level 0) |
| 2625 | * |
| 2626 | * If @key isn't found, 1 is returned and the leaf level of the path (level 0) |
| 2627 | * points to the slot where it should be inserted |
| 2628 | * |
| 2629 | * If an error is encountered while searching the tree a negative error number |
| 2630 | * is returned |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2631 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2632 | int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 2633 | const struct btrfs_key *key, struct btrfs_path *p, |
| 2634 | int ins_len, int cow) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2635 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2636 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2637 | struct extent_buffer *b; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2638 | int slot; |
| 2639 | int ret; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2640 | int err; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2641 | int level; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2642 | int lowest_unlock = 1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2643 | int root_lock; |
| 2644 | /* everything at write_lock_level or lower must be write locked */ |
| 2645 | int write_lock_level = 0; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2646 | u8 lowest_level = 0; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2647 | int min_write_lock_level; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2648 | int prev_cmp; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2649 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2650 | lowest_level = p->lowest_level; |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 2651 | WARN_ON(lowest_level && ins_len > 0); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 2652 | WARN_ON(p->nodes[0] != NULL); |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2653 | BUG_ON(!cow && ins_len); |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 2654 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2655 | if (ins_len < 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2656 | lowest_unlock = 2; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2657 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2658 | /* when we are removing items, we might have to go up to level |
| 2659 | * two as we update tree pointers Make sure we keep write |
| 2660 | * for those levels as well |
| 2661 | */ |
| 2662 | write_lock_level = 2; |
| 2663 | } else if (ins_len > 0) { |
| 2664 | /* |
| 2665 | * for inserting items, make sure we have a write lock on |
| 2666 | * level 1 so we can update keys |
| 2667 | */ |
| 2668 | write_lock_level = 1; |
| 2669 | } |
| 2670 | |
| 2671 | if (!cow) |
| 2672 | write_lock_level = -1; |
| 2673 | |
Josef Bacik | 09a2a8f9 | 2013-04-05 16:51:15 -0400 | [diff] [blame] | 2674 | if (cow && (p->keep_locks || p->lowest_level)) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2675 | write_lock_level = BTRFS_MAX_LEVEL; |
| 2676 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2677 | min_write_lock_level = write_lock_level; |
| 2678 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 2679 | again: |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2680 | prev_cmp = -1; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2681 | /* |
| 2682 | * we try very hard to do read locks on the root |
| 2683 | */ |
| 2684 | root_lock = BTRFS_READ_LOCK; |
| 2685 | level = 0; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2686 | if (p->search_commit_root) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2687 | /* |
| 2688 | * the commit roots are read only |
| 2689 | * so we always do read locks |
| 2690 | */ |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 2691 | if (p->need_commit_sem) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2692 | down_read(&fs_info->commit_root_sem); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2693 | b = root->commit_root; |
| 2694 | extent_buffer_get(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2695 | level = btrfs_header_level(b); |
Josef Bacik | 3f8a18c | 2014-03-28 17:16:01 -0400 | [diff] [blame] | 2696 | if (p->need_commit_sem) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2697 | up_read(&fs_info->commit_root_sem); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2698 | if (!p->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2699 | btrfs_tree_read_lock(b); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2700 | } else { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2701 | if (p->skip_locking) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2702 | b = btrfs_root_node(root); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2703 | level = btrfs_header_level(b); |
| 2704 | } else { |
| 2705 | /* we don't know the level of the root node |
| 2706 | * until we actually have it read locked |
| 2707 | */ |
| 2708 | b = btrfs_read_lock_root_node(root); |
| 2709 | level = btrfs_header_level(b); |
| 2710 | if (level <= write_lock_level) { |
| 2711 | /* whoops, must trade for write lock */ |
| 2712 | btrfs_tree_read_unlock(b); |
| 2713 | free_extent_buffer(b); |
| 2714 | b = btrfs_lock_root_node(root); |
| 2715 | root_lock = BTRFS_WRITE_LOCK; |
| 2716 | |
| 2717 | /* the level might have changed, check again */ |
| 2718 | level = btrfs_header_level(b); |
| 2719 | } |
| 2720 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2721 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2722 | p->nodes[level] = b; |
| 2723 | if (!p->skip_locking) |
| 2724 | p->locks[level] = root_lock; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2725 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2726 | while (b) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2727 | level = btrfs_header_level(b); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2728 | |
| 2729 | /* |
| 2730 | * setup the path here so we can release it under lock |
| 2731 | * contention with the cow code |
| 2732 | */ |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2733 | if (cow) { |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2734 | bool last_level = (level == (BTRFS_MAX_LEVEL - 1)); |
| 2735 | |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2736 | /* |
| 2737 | * if we don't really need to cow this block |
| 2738 | * then we don't want to set the path blocking, |
| 2739 | * so we test it here |
| 2740 | */ |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 2741 | if (!should_cow_block(trans, root, b)) { |
| 2742 | trans->dirty = true; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2743 | goto cow_done; |
Jeff Mahoney | 64c1292 | 2016-06-08 00:36:38 -0400 | [diff] [blame] | 2744 | } |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 2745 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2746 | /* |
| 2747 | * must have write locks on this node and the |
| 2748 | * parent |
| 2749 | */ |
Josef Bacik | 5124e00 | 2012-11-07 13:44:13 -0500 | [diff] [blame] | 2750 | if (level > write_lock_level || |
| 2751 | (level + 1 > write_lock_level && |
| 2752 | level + 1 < BTRFS_MAX_LEVEL && |
| 2753 | p->nodes[level + 1])) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2754 | write_lock_level = level + 1; |
| 2755 | btrfs_release_path(p); |
| 2756 | goto again; |
| 2757 | } |
| 2758 | |
Filipe Manana | 160f408 | 2014-07-28 19:37:17 +0100 | [diff] [blame] | 2759 | btrfs_set_path_blocking(p); |
Nikolay Borisov | 9ea2c7c | 2017-12-12 11:14:49 +0200 | [diff] [blame] | 2760 | if (last_level) |
| 2761 | err = btrfs_cow_block(trans, root, b, NULL, 0, |
| 2762 | &b); |
| 2763 | else |
| 2764 | err = btrfs_cow_block(trans, root, b, |
| 2765 | p->nodes[level + 1], |
| 2766 | p->slots[level + 1], &b); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2767 | if (err) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2768 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2769 | goto done; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2770 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 2771 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2772 | cow_done: |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 2773 | p->nodes[level] = b; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2774 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2775 | |
| 2776 | /* |
| 2777 | * we have a lock on b and as long as we aren't changing |
| 2778 | * the tree, there is no way to for the items in b to change. |
| 2779 | * It is safe to drop the lock on our parent before we |
| 2780 | * go through the expensive btree search on b. |
| 2781 | * |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2782 | * If we're inserting or deleting (ins_len != 0), then we might |
| 2783 | * be changing slot zero, which may require changing the parent. |
| 2784 | * So, we can't drop the lock until after we know which slot |
| 2785 | * we're operating on. |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2786 | */ |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2787 | if (!ins_len && !p->keep_locks) { |
| 2788 | int u = level + 1; |
| 2789 | |
| 2790 | if (u < BTRFS_MAX_LEVEL && p->locks[u]) { |
| 2791 | btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]); |
| 2792 | p->locks[u] = 0; |
| 2793 | } |
| 2794 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2795 | |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2796 | ret = key_search(b, key, level, &prev_cmp, &slot); |
Liu Bo | 415b35a | 2016-06-17 19:16:21 -0700 | [diff] [blame] | 2797 | if (ret < 0) |
| 2798 | goto done; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2799 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2800 | if (level != 0) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2801 | int dec = 0; |
| 2802 | if (ret && slot > 0) { |
| 2803 | dec = 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2804 | slot -= 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2805 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2806 | p->slots[level] = slot; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2807 | err = setup_nodes_for_search(trans, root, p, b, level, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2808 | ins_len, &write_lock_level); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2809 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2810 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2811 | if (err) { |
| 2812 | ret = err; |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2813 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2814 | } |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2815 | b = p->nodes[level]; |
| 2816 | slot = p->slots[level]; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2817 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2818 | /* |
| 2819 | * slot 0 is special, if we change the key |
| 2820 | * we have to update the parent pointer |
| 2821 | * which means we must have a write lock |
| 2822 | * on the parent |
| 2823 | */ |
Filipe David Borba Manana | eb653de | 2013-12-23 11:53:02 +0000 | [diff] [blame] | 2824 | if (slot == 0 && ins_len && |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2825 | write_lock_level < level + 1) { |
| 2826 | write_lock_level = level + 1; |
| 2827 | btrfs_release_path(p); |
| 2828 | goto again; |
| 2829 | } |
| 2830 | |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2831 | unlock_up(p, level, lowest_unlock, |
| 2832 | min_write_lock_level, &write_lock_level); |
Chris Mason | f9efa9c | 2008-06-25 16:14:04 -0400 | [diff] [blame] | 2833 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2834 | if (level == lowest_level) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2835 | if (dec) |
| 2836 | p->slots[level]++; |
Zheng Yan | 5b21f2e | 2008-09-26 10:05:38 -0400 | [diff] [blame] | 2837 | goto done; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 2838 | } |
Chris Mason | ca7a79a | 2008-05-12 12:59:19 -0400 | [diff] [blame] | 2839 | |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 2840 | err = read_block_for_search(root, p, &b, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 2841 | slot, key); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2842 | if (err == -EAGAIN) |
Chris Mason | c8c4286 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 2843 | goto again; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2844 | if (err) { |
| 2845 | ret = err; |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2846 | goto done; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2847 | } |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 2848 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2849 | if (!p->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2850 | level = btrfs_header_level(b); |
| 2851 | if (level <= write_lock_level) { |
| 2852 | err = btrfs_try_tree_write_lock(b); |
| 2853 | if (!err) { |
| 2854 | btrfs_set_path_blocking(p); |
| 2855 | btrfs_tree_lock(b); |
| 2856 | btrfs_clear_path_blocking(p, b, |
| 2857 | BTRFS_WRITE_LOCK); |
| 2858 | } |
| 2859 | p->locks[level] = BTRFS_WRITE_LOCK; |
| 2860 | } else { |
Chris Mason | f82c458 | 2014-11-19 10:25:09 -0800 | [diff] [blame] | 2861 | err = btrfs_tree_read_lock_atomic(b); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2862 | if (!err) { |
| 2863 | btrfs_set_path_blocking(p); |
| 2864 | btrfs_tree_read_lock(b); |
| 2865 | btrfs_clear_path_blocking(p, b, |
| 2866 | BTRFS_READ_LOCK); |
| 2867 | } |
| 2868 | p->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2869 | } |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2870 | p->nodes[level] = b; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2871 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2872 | } else { |
| 2873 | p->slots[level] = slot; |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 2874 | if (ins_len > 0 && |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 2875 | btrfs_leaf_free_space(fs_info, b) < ins_len) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2876 | if (write_lock_level < 1) { |
| 2877 | write_lock_level = 1; |
| 2878 | btrfs_release_path(p); |
| 2879 | goto again; |
| 2880 | } |
| 2881 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2882 | btrfs_set_path_blocking(p); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2883 | err = split_leaf(trans, root, key, |
| 2884 | p, ins_len, ret == 0); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 2885 | btrfs_clear_path_blocking(p, NULL, 0); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2886 | |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 2887 | BUG_ON(err > 0); |
| 2888 | if (err) { |
| 2889 | ret = err; |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2890 | goto done; |
| 2891 | } |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 2892 | } |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 2893 | if (!p->search_for_split) |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 2894 | unlock_up(p, level, lowest_unlock, |
| 2895 | min_write_lock_level, &write_lock_level); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2896 | goto done; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2897 | } |
| 2898 | } |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2899 | ret = 1; |
| 2900 | done: |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 2901 | /* |
| 2902 | * we don't really know what they plan on doing with the path |
| 2903 | * from here on, so for now just mark it as blocking |
| 2904 | */ |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 2905 | if (!p->leave_spinning) |
| 2906 | btrfs_set_path_blocking(p); |
Filipe Manana | 5f5bc6b | 2014-11-09 08:38:39 +0000 | [diff] [blame] | 2907 | if (ret < 0 && !p->skip_release_on_error) |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 2908 | btrfs_release_path(p); |
Chris Mason | 65b51a0 | 2008-08-01 15:11:20 -0400 | [diff] [blame] | 2909 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 2910 | } |
| 2911 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 2912 | /* |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2913 | * Like btrfs_search_slot, this looks for a key in the given tree. It uses the |
| 2914 | * current state of the tree together with the operations recorded in the tree |
| 2915 | * modification log to search for the key in a previous version of this tree, as |
| 2916 | * denoted by the time_seq parameter. |
| 2917 | * |
| 2918 | * Naturally, there is no support for insert, delete or cow operations. |
| 2919 | * |
| 2920 | * The resulting path and return value will be set up as if we called |
| 2921 | * btrfs_search_slot at that point in time with ins_len and cow both set to 0. |
| 2922 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 2923 | 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] | 2924 | struct btrfs_path *p, u64 time_seq) |
| 2925 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 2926 | struct btrfs_fs_info *fs_info = root->fs_info; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2927 | struct extent_buffer *b; |
| 2928 | int slot; |
| 2929 | int ret; |
| 2930 | int err; |
| 2931 | int level; |
| 2932 | int lowest_unlock = 1; |
| 2933 | u8 lowest_level = 0; |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame] | 2934 | int prev_cmp = -1; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2935 | |
| 2936 | lowest_level = p->lowest_level; |
| 2937 | WARN_ON(p->nodes[0] != NULL); |
| 2938 | |
| 2939 | if (p->search_commit_root) { |
| 2940 | BUG_ON(time_seq); |
| 2941 | return btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 2942 | } |
| 2943 | |
| 2944 | again: |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2945 | b = get_old_root(root, time_seq); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2946 | level = btrfs_header_level(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2947 | p->locks[level] = BTRFS_READ_LOCK; |
| 2948 | |
| 2949 | while (b) { |
| 2950 | level = btrfs_header_level(b); |
| 2951 | p->nodes[level] = b; |
| 2952 | btrfs_clear_path_blocking(p, NULL, 0); |
| 2953 | |
| 2954 | /* |
| 2955 | * we have a lock on b and as long as we aren't changing |
| 2956 | * the tree, there is no way to for the items in b to change. |
| 2957 | * It is safe to drop the lock on our parent before we |
| 2958 | * go through the expensive btree search on b. |
| 2959 | */ |
| 2960 | btrfs_unlock_up_safe(p, level + 1); |
| 2961 | |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame] | 2962 | /* |
Nicholas D Steeves | 0132761 | 2016-05-19 21:18:45 -0400 | [diff] [blame] | 2963 | * Since we can unwind ebs we want to do a real search every |
Josef Bacik | d4b4087 | 2013-09-24 14:09:34 -0400 | [diff] [blame] | 2964 | * time. |
| 2965 | */ |
| 2966 | prev_cmp = -1; |
Filipe David Borba Manana | d7396f0 | 2013-08-30 15:46:43 +0100 | [diff] [blame] | 2967 | ret = key_search(b, key, level, &prev_cmp, &slot); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2968 | |
| 2969 | if (level != 0) { |
| 2970 | int dec = 0; |
| 2971 | if (ret && slot > 0) { |
| 2972 | dec = 1; |
| 2973 | slot -= 1; |
| 2974 | } |
| 2975 | p->slots[level] = slot; |
| 2976 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 2977 | |
| 2978 | if (level == lowest_level) { |
| 2979 | if (dec) |
| 2980 | p->slots[level]++; |
| 2981 | goto done; |
| 2982 | } |
| 2983 | |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 2984 | err = read_block_for_search(root, p, &b, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 2985 | slot, key); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2986 | if (err == -EAGAIN) |
| 2987 | goto again; |
| 2988 | if (err) { |
| 2989 | ret = err; |
| 2990 | goto done; |
| 2991 | } |
| 2992 | |
| 2993 | level = btrfs_header_level(b); |
Chris Mason | f82c458 | 2014-11-19 10:25:09 -0800 | [diff] [blame] | 2994 | err = btrfs_tree_read_lock_atomic(b); |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 2995 | if (!err) { |
| 2996 | btrfs_set_path_blocking(p); |
| 2997 | btrfs_tree_read_lock(b); |
| 2998 | btrfs_clear_path_blocking(p, b, |
| 2999 | BTRFS_READ_LOCK); |
| 3000 | } |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3001 | b = tree_mod_log_rewind(fs_info, p, b, time_seq); |
Josef Bacik | db7f343 | 2013-08-07 14:54:37 -0400 | [diff] [blame] | 3002 | if (!b) { |
| 3003 | ret = -ENOMEM; |
| 3004 | goto done; |
| 3005 | } |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 3006 | p->locks[level] = BTRFS_READ_LOCK; |
| 3007 | p->nodes[level] = b; |
Jan Schmidt | 5d9e75c4 | 2012-05-16 18:25:47 +0200 | [diff] [blame] | 3008 | } else { |
| 3009 | p->slots[level] = slot; |
| 3010 | unlock_up(p, level, lowest_unlock, 0, NULL); |
| 3011 | goto done; |
| 3012 | } |
| 3013 | } |
| 3014 | ret = 1; |
| 3015 | done: |
| 3016 | if (!p->leave_spinning) |
| 3017 | btrfs_set_path_blocking(p); |
| 3018 | if (ret < 0) |
| 3019 | btrfs_release_path(p); |
| 3020 | |
| 3021 | return ret; |
| 3022 | } |
| 3023 | |
| 3024 | /* |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3025 | * helper to use instead of search slot if no exact match is needed but |
| 3026 | * instead the next or previous item should be returned. |
| 3027 | * When find_higher is true, the next higher item is returned, the next lower |
| 3028 | * otherwise. |
| 3029 | * When return_any and find_higher are both true, and no higher item is found, |
| 3030 | * return the next lower instead. |
| 3031 | * When return_any is true and find_higher is false, and no lower item is found, |
| 3032 | * return the next higher instead. |
| 3033 | * It returns 0 if any item is found, 1 if none is found (tree empty), and |
| 3034 | * < 0 on error |
| 3035 | */ |
| 3036 | int btrfs_search_slot_for_read(struct btrfs_root *root, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 3037 | const struct btrfs_key *key, |
| 3038 | struct btrfs_path *p, int find_higher, |
| 3039 | int return_any) |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3040 | { |
| 3041 | int ret; |
| 3042 | struct extent_buffer *leaf; |
| 3043 | |
| 3044 | again: |
| 3045 | ret = btrfs_search_slot(NULL, root, key, p, 0, 0); |
| 3046 | if (ret <= 0) |
| 3047 | return ret; |
| 3048 | /* |
| 3049 | * a return value of 1 means the path is at the position where the |
| 3050 | * item should be inserted. Normally this is the next bigger item, |
| 3051 | * but in case the previous item is the last in a leaf, path points |
| 3052 | * to the first free slot in the previous leaf, i.e. at an invalid |
| 3053 | * item. |
| 3054 | */ |
| 3055 | leaf = p->nodes[0]; |
| 3056 | |
| 3057 | if (find_higher) { |
| 3058 | if (p->slots[0] >= btrfs_header_nritems(leaf)) { |
| 3059 | ret = btrfs_next_leaf(root, p); |
| 3060 | if (ret <= 0) |
| 3061 | return ret; |
| 3062 | if (!return_any) |
| 3063 | return 1; |
| 3064 | /* |
| 3065 | * no higher item found, return the next |
| 3066 | * lower instead |
| 3067 | */ |
| 3068 | return_any = 0; |
| 3069 | find_higher = 0; |
| 3070 | btrfs_release_path(p); |
| 3071 | goto again; |
| 3072 | } |
| 3073 | } else { |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3074 | if (p->slots[0] == 0) { |
| 3075 | ret = btrfs_prev_leaf(root, p); |
| 3076 | if (ret < 0) |
| 3077 | return ret; |
| 3078 | if (!ret) { |
Filipe David Borba Manana | 23c6bf6 | 2014-01-11 21:28:54 +0000 | [diff] [blame] | 3079 | leaf = p->nodes[0]; |
| 3080 | if (p->slots[0] == btrfs_header_nritems(leaf)) |
| 3081 | p->slots[0]--; |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3082 | return 0; |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3083 | } |
Arne Jansen | e679376 | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3084 | if (!return_any) |
| 3085 | return 1; |
| 3086 | /* |
| 3087 | * no lower item found, return the next |
| 3088 | * higher instead |
| 3089 | */ |
| 3090 | return_any = 0; |
| 3091 | find_higher = 1; |
| 3092 | btrfs_release_path(p); |
| 3093 | goto again; |
| 3094 | } else { |
Arne Jansen | 2f38b3e | 2011-09-13 11:18:10 +0200 | [diff] [blame] | 3095 | --p->slots[0]; |
| 3096 | } |
| 3097 | } |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
| 3101 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3102 | * adjust the pointers going up the tree, starting at level |
| 3103 | * making sure the right key of each node is points to 'key'. |
| 3104 | * This is used after shifting pointers to the left, so it stops |
| 3105 | * fixing up pointers when a given leaf/node is not in slot 0 of the |
| 3106 | * higher levels |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3107 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3108 | */ |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 3109 | static void fixup_low_keys(struct btrfs_fs_info *fs_info, |
| 3110 | 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); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3151 | BUG_ON(comp_keys(&disk_key, new_key) >= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3152 | } |
| 3153 | if (slot < btrfs_header_nritems(eb) - 1) { |
| 3154 | btrfs_item_key(eb, &disk_key, slot + 1); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3155 | BUG_ON(comp_keys(&disk_key, new_key) <= 0); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3156 | } |
| 3157 | |
| 3158 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 3159 | btrfs_set_item_key(eb, &disk_key, slot); |
| 3160 | btrfs_mark_buffer_dirty(eb); |
| 3161 | if (slot == 0) |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 3162 | fixup_low_keys(fs_info, path, &disk_key, 1); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3166 | * 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] | 3167 | * tree. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3168 | * |
| 3169 | * returns 0 if some ptrs were pushed left, < 0 if there was some horrible |
| 3170 | * 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] | 3171 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 3172 | static int push_node_left(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3173 | struct btrfs_fs_info *fs_info, |
| 3174 | struct extent_buffer *dst, |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3175 | struct extent_buffer *src, int empty) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3176 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3177 | int push_items = 0; |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3178 | int src_nritems; |
| 3179 | int dst_nritems; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3180 | int ret = 0; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3181 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3182 | src_nritems = btrfs_header_nritems(src); |
| 3183 | dst_nritems = btrfs_header_nritems(dst); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3184 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3185 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3186 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3187 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3188 | if (!empty && src_nritems <= 8) |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3189 | return 1; |
| 3190 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3191 | if (push_items <= 0) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3192 | return 1; |
| 3193 | |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3194 | if (empty) { |
Chris Mason | 971a1f6 | 2008-04-24 10:54:32 -0400 | [diff] [blame] | 3195 | push_items = min(src_nritems, push_items); |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3196 | if (push_items < src_nritems) { |
| 3197 | /* leave at least 8 pointers in the node if |
| 3198 | * we aren't going to empty it |
| 3199 | */ |
| 3200 | if (src_nritems - push_items < 8) { |
| 3201 | if (push_items <= 8) |
| 3202 | return 1; |
| 3203 | push_items -= 8; |
| 3204 | } |
| 3205 | } |
| 3206 | } else |
| 3207 | push_items = min(src_nritems - 8, push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3208 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3209 | ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0, |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3210 | push_items); |
| 3211 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3212 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3213 | return ret; |
| 3214 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3215 | copy_extent_buffer(dst, src, |
| 3216 | btrfs_node_key_ptr_offset(dst_nritems), |
| 3217 | btrfs_node_key_ptr_offset(0), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3218 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3219 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3220 | if (push_items < src_nritems) { |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3221 | /* |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3222 | * Don't call tree_mod_log_insert_move here, key removal was |
| 3223 | * already fully logged by tree_mod_log_eb_copy above. |
Jan Schmidt | 57911b8 | 2012-10-19 09:22:03 +0200 | [diff] [blame] | 3224 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3225 | memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0), |
| 3226 | btrfs_node_key_ptr_offset(push_items), |
| 3227 | (src_nritems - push_items) * |
| 3228 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3229 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3230 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3231 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
| 3232 | btrfs_mark_buffer_dirty(src); |
| 3233 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3234 | |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 3235 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3236 | } |
| 3237 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3238 | /* |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3239 | * try to push data from one node into the next node right in the |
| 3240 | * tree. |
| 3241 | * |
| 3242 | * returns 0 if some ptrs were pushed, < 0 if there was some horrible |
| 3243 | * error, and > 0 if there was no room in the right hand block. |
| 3244 | * |
| 3245 | * this will only push up to 1/2 the contents of the left node over |
| 3246 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3247 | static int balance_node_right(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3248 | struct btrfs_fs_info *fs_info, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3249 | struct extent_buffer *dst, |
| 3250 | struct extent_buffer *src) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3251 | { |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3252 | int push_items = 0; |
| 3253 | int max_push; |
| 3254 | int src_nritems; |
| 3255 | int dst_nritems; |
| 3256 | int ret = 0; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3257 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3258 | WARN_ON(btrfs_header_generation(src) != trans->transid); |
| 3259 | WARN_ON(btrfs_header_generation(dst) != trans->transid); |
| 3260 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3261 | src_nritems = btrfs_header_nritems(src); |
| 3262 | dst_nritems = btrfs_header_nritems(dst); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3263 | push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3264 | if (push_items <= 0) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3265 | return 1; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3266 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3267 | if (src_nritems < 4) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 3268 | return 1; |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3269 | |
| 3270 | max_push = src_nritems / 2 + 1; |
| 3271 | /* don't try to empty the node */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3272 | if (max_push >= src_nritems) |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3273 | return 1; |
Yan | 252c38f | 2007-08-29 09:11:44 -0400 | [diff] [blame] | 3274 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3275 | if (max_push < push_items) |
| 3276 | push_items = max_push; |
| 3277 | |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3278 | ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems); |
| 3279 | BUG_ON(ret < 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3280 | memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items), |
| 3281 | btrfs_node_key_ptr_offset(0), |
| 3282 | (dst_nritems) * |
| 3283 | sizeof(struct btrfs_key_ptr)); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3284 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3285 | ret = tree_mod_log_eb_copy(fs_info, dst, src, 0, |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3286 | src_nritems - push_items, push_items); |
| 3287 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3288 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3289 | return ret; |
| 3290 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3291 | copy_extent_buffer(dst, src, |
| 3292 | btrfs_node_key_ptr_offset(0), |
| 3293 | btrfs_node_key_ptr_offset(src_nritems - push_items), |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3294 | push_items * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3295 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3296 | btrfs_set_header_nritems(src, src_nritems - push_items); |
| 3297 | btrfs_set_header_nritems(dst, dst_nritems + push_items); |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3298 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3299 | btrfs_mark_buffer_dirty(src); |
| 3300 | btrfs_mark_buffer_dirty(dst); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3301 | |
Chris Mason | 79f95c8 | 2007-03-01 15:16:26 -0500 | [diff] [blame] | 3302 | return ret; |
| 3303 | } |
| 3304 | |
| 3305 | /* |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3306 | * helper function to insert a new root level in the tree. |
| 3307 | * A new node is allocated, and a single item is inserted to |
| 3308 | * point to the existing root |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3309 | * |
| 3310 | * returns zero on success or < 0 on failure. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3311 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3312 | static noinline int insert_new_root(struct btrfs_trans_handle *trans, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3313 | struct btrfs_root *root, |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3314 | struct btrfs_path *path, int level) |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3315 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3316 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3317 | u64 lower_gen; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3318 | struct extent_buffer *lower; |
| 3319 | struct extent_buffer *c; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3320 | struct extent_buffer *old; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3321 | struct btrfs_disk_key lower_key; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3322 | |
| 3323 | BUG_ON(path->nodes[level]); |
| 3324 | BUG_ON(path->nodes[level-1] != root->node); |
| 3325 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3326 | lower = path->nodes[level-1]; |
| 3327 | if (level == 1) |
| 3328 | btrfs_item_key(lower, &lower_key, 0); |
| 3329 | else |
| 3330 | btrfs_node_key(lower, &lower_key, 0); |
| 3331 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 3332 | c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid, |
| 3333 | &lower_key, level, root->node->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3334 | if (IS_ERR(c)) |
| 3335 | return PTR_ERR(c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3336 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3337 | root_add_used(root, fs_info->nodesize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3338 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3339 | memzero_extent_buffer(c, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3340 | btrfs_set_header_nritems(c, 1); |
| 3341 | btrfs_set_header_level(c, level); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3342 | btrfs_set_header_bytenr(c, c->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3343 | btrfs_set_header_generation(c, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3344 | btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3345 | btrfs_set_header_owner(c, root->root_key.objectid); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3346 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3347 | write_extent_buffer_fsid(c, fs_info->fsid); |
| 3348 | write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 3349 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3350 | btrfs_set_node_key(c, &lower_key, 0); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3351 | btrfs_set_node_blockptr(c, 0, lower->start); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3352 | lower_gen = btrfs_header_generation(lower); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3353 | WARN_ON(lower_gen != trans->transid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3354 | |
| 3355 | btrfs_set_node_ptr_generation(c, 0, lower_gen); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3356 | |
| 3357 | btrfs_mark_buffer_dirty(c); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 3358 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3359 | old = root->node; |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3360 | tree_mod_log_set_root_pointer(root, c, 0); |
Chris Mason | 240f62c | 2011-03-23 14:54:42 -0400 | [diff] [blame] | 3361 | rcu_assign_pointer(root->node, c); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3362 | |
| 3363 | /* the super has an extra ref to root->node */ |
| 3364 | free_extent_buffer(old); |
| 3365 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 3366 | add_root_to_dirty_list(root); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3367 | extent_buffer_get(c); |
| 3368 | path->nodes[level] = c; |
chandan | 95449a1 | 2015-01-15 12:22:03 +0530 | [diff] [blame] | 3369 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3370 | path->slots[level] = 0; |
| 3371 | return 0; |
| 3372 | } |
| 3373 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3374 | /* |
| 3375 | * worker function to insert a single pointer in a node. |
| 3376 | * the node should have enough room for the pointer already |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3377 | * |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3378 | * slot and level indicate where you want the key to go, and |
| 3379 | * blocknr is the block the key points to. |
| 3380 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3381 | static void insert_ptr(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3382 | struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 3383 | struct btrfs_disk_key *key, u64 bytenr, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3384 | int slot, int level) |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3385 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3386 | struct extent_buffer *lower; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3387 | int nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3388 | int ret; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3389 | |
| 3390 | BUG_ON(!path->nodes[level]); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3391 | btrfs_assert_tree_locked(path->nodes[level]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3392 | lower = path->nodes[level]; |
| 3393 | nritems = btrfs_header_nritems(lower); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 3394 | BUG_ON(slot > nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3395 | BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3396 | if (slot != nritems) { |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3397 | if (level) { |
| 3398 | ret = tree_mod_log_insert_move(lower, slot + 1, slot, |
David Sterba | a446a97 | 2018-03-05 15:26:29 +0100 | [diff] [blame] | 3399 | nritems - slot); |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 3400 | BUG_ON(ret < 0); |
| 3401 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3402 | memmove_extent_buffer(lower, |
| 3403 | btrfs_node_key_ptr_offset(slot + 1), |
| 3404 | btrfs_node_key_ptr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3405 | (nritems - slot) * sizeof(struct btrfs_key_ptr)); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3406 | } |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3407 | if (level) { |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 3408 | ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD, |
| 3409 | GFP_NOFS); |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 3410 | BUG_ON(ret < 0); |
| 3411 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3412 | btrfs_set_node_key(lower, key, slot); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3413 | btrfs_set_node_blockptr(lower, slot, bytenr); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3414 | WARN_ON(trans->transid == 0); |
| 3415 | btrfs_set_node_ptr_generation(lower, slot, trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3416 | btrfs_set_header_nritems(lower, nritems + 1); |
| 3417 | btrfs_mark_buffer_dirty(lower); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3418 | } |
| 3419 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3420 | /* |
| 3421 | * split the node at the specified level in path in two. |
| 3422 | * The path is corrected to point to the appropriate node after the split |
| 3423 | * |
| 3424 | * Before splitting this tries to make some room in the node by pushing |
| 3425 | * left and right, if either one works, it returns right away. |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3426 | * |
| 3427 | * returns 0 on success and < 0 on failure |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 3428 | */ |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3429 | static noinline int split_node(struct btrfs_trans_handle *trans, |
| 3430 | struct btrfs_root *root, |
| 3431 | struct btrfs_path *path, int level) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3432 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3433 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3434 | struct extent_buffer *c; |
| 3435 | struct extent_buffer *split; |
| 3436 | struct btrfs_disk_key disk_key; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3437 | int mid; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3438 | int ret; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3439 | u32 c_nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3440 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3441 | c = path->nodes[level]; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3442 | WARN_ON(btrfs_header_generation(c) != trans->transid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3443 | if (c == root->node) { |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3444 | /* |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3445 | * trying to split the root, lets make a new one |
| 3446 | * |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3447 | * tree mod log: We don't log_removal old root in |
Jan Schmidt | 90f8d62 | 2013-04-13 13:19:53 +0000 | [diff] [blame] | 3448 | * insert_new_root, because that root buffer will be kept as a |
| 3449 | * normal node. We are going to log removal of half of the |
| 3450 | * elements below with tree_mod_log_eb_copy. We're holding a |
| 3451 | * tree lock on the buffer, which is why we cannot race with |
| 3452 | * other tree_mod_log users. |
Jan Schmidt | d9abbf1 | 2013-03-20 13:49:48 +0000 | [diff] [blame] | 3453 | */ |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 3454 | ret = insert_new_root(trans, root, path, level + 1); |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3455 | if (ret) |
| 3456 | return ret; |
Chris Mason | b361242 | 2009-05-13 19:12:15 -0400 | [diff] [blame] | 3457 | } else { |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3458 | ret = push_nodes_for_insert(trans, root, path, level); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3459 | c = path->nodes[level]; |
| 3460 | if (!ret && btrfs_header_nritems(c) < |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3461 | BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3462 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3463 | if (ret < 0) |
| 3464 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3465 | } |
Chris Mason | e66f709 | 2007-04-20 13:16:02 -0400 | [diff] [blame] | 3466 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3467 | c_nritems = btrfs_header_nritems(c); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3468 | mid = (c_nritems + 1) / 2; |
| 3469 | btrfs_node_key(c, &disk_key, mid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 3470 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 3471 | split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid, |
| 3472 | &disk_key, level, c->start, 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3473 | if (IS_ERR(split)) |
| 3474 | return PTR_ERR(split); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 3475 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3476 | root_add_used(root, fs_info->nodesize); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3477 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 3478 | memzero_extent_buffer(split, 0, sizeof(struct btrfs_header)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3479 | btrfs_set_header_level(split, btrfs_header_level(c)); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3480 | btrfs_set_header_bytenr(split, split->start); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3481 | btrfs_set_header_generation(split, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 3482 | btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3483 | btrfs_set_header_owner(split, root->root_key.objectid); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3484 | write_extent_buffer_fsid(split, fs_info->fsid); |
| 3485 | write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3486 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3487 | ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3488 | if (ret) { |
Jeff Mahoney | 6664283 | 2016-06-10 18:19:25 -0400 | [diff] [blame] | 3489 | btrfs_abort_transaction(trans, ret); |
Filipe David Borba Manana | 5de865e | 2013-12-20 15:17:46 +0000 | [diff] [blame] | 3490 | return ret; |
| 3491 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3492 | copy_extent_buffer(split, c, |
| 3493 | btrfs_node_key_ptr_offset(0), |
| 3494 | btrfs_node_key_ptr_offset(mid), |
| 3495 | (c_nritems - mid) * sizeof(struct btrfs_key_ptr)); |
| 3496 | btrfs_set_header_nritems(split, c_nritems - mid); |
| 3497 | btrfs_set_header_nritems(c, mid); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3498 | ret = 0; |
| 3499 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3500 | btrfs_mark_buffer_dirty(c); |
| 3501 | btrfs_mark_buffer_dirty(split); |
| 3502 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3503 | insert_ptr(trans, fs_info, path, &disk_key, split->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 3504 | path->slots[level + 1] + 1, level + 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3505 | |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 3506 | if (path->slots[level] >= mid) { |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3507 | path->slots[level] -= mid; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3508 | btrfs_tree_unlock(c); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3509 | free_extent_buffer(c); |
| 3510 | path->nodes[level] = split; |
Chris Mason | 5c680ed | 2007-02-22 11:39:13 -0500 | [diff] [blame] | 3511 | path->slots[level + 1] += 1; |
| 3512 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3513 | btrfs_tree_unlock(split); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3514 | free_extent_buffer(split); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3515 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3516 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3517 | } |
| 3518 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3519 | /* |
| 3520 | * how many bytes are required to store the items in a leaf. start |
| 3521 | * and nr indicate which items in the leaf to check. This totals up the |
| 3522 | * space used both by the item structs and the item data |
| 3523 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3524 | 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] | 3525 | { |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3526 | struct btrfs_item *start_item; |
| 3527 | struct btrfs_item *end_item; |
| 3528 | struct btrfs_map_token token; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3529 | int data_len; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3530 | int nritems = btrfs_header_nritems(l); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3531 | int end = min(nritems, start + nr) - 1; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3532 | |
| 3533 | if (!nr) |
| 3534 | return 0; |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3535 | btrfs_init_map_token(&token); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3536 | start_item = btrfs_item_nr(start); |
| 3537 | end_item = btrfs_item_nr(end); |
Josef Bacik | 41be1f3 | 2012-10-15 13:43:18 -0400 | [diff] [blame] | 3538 | data_len = btrfs_token_item_offset(l, start_item, &token) + |
| 3539 | btrfs_token_item_size(l, start_item, &token); |
| 3540 | data_len = data_len - btrfs_token_item_offset(l, end_item, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3541 | data_len += sizeof(struct btrfs_item) * nr; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3542 | WARN_ON(data_len < 0); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3543 | return data_len; |
| 3544 | } |
| 3545 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3546 | /* |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3547 | * The space between the end of the leaf items and |
| 3548 | * the start of the leaf data. IOW, how much room |
| 3549 | * the leaf has left for both items and data |
| 3550 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3551 | noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info, |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 3552 | struct extent_buffer *leaf) |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3553 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3554 | int nritems = btrfs_header_nritems(leaf); |
| 3555 | int ret; |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3556 | |
| 3557 | 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] | 3558 | if (ret < 0) { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3559 | btrfs_crit(fs_info, |
| 3560 | "leaf free space ret %d, leaf data size %lu, used %d nritems %d", |
| 3561 | ret, |
| 3562 | (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info), |
| 3563 | leaf_space_used(leaf, 0, nritems), nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3564 | } |
| 3565 | return ret; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 3566 | } |
| 3567 | |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3568 | /* |
| 3569 | * min slot controls the lowest index we're willing to push to the |
| 3570 | * right. We'll push up to and including min_slot, but no lower |
| 3571 | */ |
David Sterba | 1e47eef | 2017-02-10 19:13:06 +0100 | [diff] [blame] | 3572 | static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3573 | struct btrfs_path *path, |
| 3574 | int data_size, int empty, |
| 3575 | struct extent_buffer *right, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3576 | int free_space, u32 left_nritems, |
| 3577 | u32 min_slot) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3578 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3579 | struct extent_buffer *left = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3580 | struct extent_buffer *upper = path->nodes[1]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3581 | struct btrfs_map_token token; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3582 | struct btrfs_disk_key disk_key; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3583 | int slot; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3584 | u32 i; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3585 | int push_space = 0; |
| 3586 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3587 | struct btrfs_item *item; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3588 | u32 nr; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3589 | u32 right_nritems; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3590 | u32 data_end; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3591 | u32 this_item_size; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3592 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3593 | btrfs_init_map_token(&token); |
| 3594 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3595 | if (empty) |
| 3596 | nr = 0; |
| 3597 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3598 | nr = max_t(u32, 1, min_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3599 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3600 | if (path->slots[0] >= left_nritems) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3601 | push_space += data_size; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3602 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3603 | slot = path->slots[1]; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3604 | i = left_nritems - 1; |
| 3605 | while (i >= nr) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3606 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3607 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3608 | if (!empty && push_items > 0) { |
| 3609 | if (path->slots[0] > i) |
| 3610 | break; |
| 3611 | if (path->slots[0] == i) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3612 | int space = btrfs_leaf_free_space(fs_info, left); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3613 | if (space + push_space * 2 > free_space) |
| 3614 | break; |
| 3615 | } |
| 3616 | } |
| 3617 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3618 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3619 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3620 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3621 | this_item_size = btrfs_item_size(left, item); |
| 3622 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3623 | break; |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3624 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3625 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3626 | push_space += this_item_size + sizeof(*item); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3627 | if (i == 0) |
| 3628 | break; |
| 3629 | i--; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3630 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3631 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3632 | if (push_items == 0) |
| 3633 | goto out_unlock; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3634 | |
Julia Lawall | 6c1500f | 2012-11-03 20:30:18 +0000 | [diff] [blame] | 3635 | WARN_ON(!empty && push_items == left_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3636 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3637 | /* push left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3638 | right_nritems = btrfs_header_nritems(right); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3639 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3640 | push_space = btrfs_item_end_nr(left, left_nritems - push_items); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3641 | push_space -= leaf_data_end(fs_info, left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3642 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3643 | /* make room in the right data area */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3644 | data_end = leaf_data_end(fs_info, right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3645 | memmove_extent_buffer(right, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3646 | BTRFS_LEAF_DATA_OFFSET + data_end - push_space, |
| 3647 | BTRFS_LEAF_DATA_OFFSET + data_end, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3648 | BTRFS_LEAF_DATA_SIZE(fs_info) - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3649 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3650 | /* copy from the left data area */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3651 | copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3652 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3653 | BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3654 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3655 | |
| 3656 | memmove_extent_buffer(right, btrfs_item_nr_offset(push_items), |
| 3657 | btrfs_item_nr_offset(0), |
| 3658 | right_nritems * sizeof(struct btrfs_item)); |
| 3659 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3660 | /* copy the items from left to right */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3661 | copy_extent_buffer(right, left, btrfs_item_nr_offset(0), |
| 3662 | btrfs_item_nr_offset(left_nritems - push_items), |
| 3663 | push_items * sizeof(struct btrfs_item)); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3664 | |
| 3665 | /* update the item pointers */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3666 | right_nritems += push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3667 | btrfs_set_header_nritems(right, right_nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3668 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3669 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3670 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3671 | push_space -= btrfs_token_item_size(right, item, &token); |
| 3672 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3673 | } |
| 3674 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3675 | left_nritems -= push_items; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3676 | btrfs_set_header_nritems(left, left_nritems); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3677 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3678 | if (left_nritems) |
| 3679 | btrfs_mark_buffer_dirty(left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3680 | else |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 3681 | clean_tree_block(fs_info, left); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3682 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3683 | btrfs_mark_buffer_dirty(right); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 3684 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3685 | btrfs_item_key(right, &disk_key, 0); |
| 3686 | btrfs_set_node_key(upper, &disk_key, slot + 1); |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3687 | btrfs_mark_buffer_dirty(upper); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 3688 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3689 | /* then fixup the leaf pointer in the path */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3690 | if (path->slots[0] >= left_nritems) { |
| 3691 | path->slots[0] -= left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3692 | if (btrfs_header_nritems(path->nodes[0]) == 0) |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 3693 | clean_tree_block(fs_info, path->nodes[0]); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3694 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3695 | free_extent_buffer(path->nodes[0]); |
| 3696 | path->nodes[0] = right; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3697 | path->slots[1] += 1; |
| 3698 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3699 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3700 | free_extent_buffer(right); |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3701 | } |
| 3702 | return 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3703 | |
| 3704 | out_unlock: |
| 3705 | btrfs_tree_unlock(right); |
| 3706 | free_extent_buffer(right); |
| 3707 | return 1; |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3708 | } |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3709 | |
Chris Mason | 00ec4c5 | 2007-02-24 12:47:20 -0500 | [diff] [blame] | 3710 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3711 | * push some data in the path leaf to the right, trying to free up at |
| 3712 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
| 3713 | * |
| 3714 | * returns 1 if the push failed because the other node didn't have enough |
| 3715 | * 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] | 3716 | * |
| 3717 | * this will push starting from min_slot to the end of the leaf. It won't |
| 3718 | * push any slot lower than min_slot |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3719 | */ |
| 3720 | 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] | 3721 | *root, struct btrfs_path *path, |
| 3722 | int min_data_size, int data_size, |
| 3723 | int empty, u32 min_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3724 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3725 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3726 | struct extent_buffer *left = path->nodes[0]; |
| 3727 | struct extent_buffer *right; |
| 3728 | struct extent_buffer *upper; |
| 3729 | int slot; |
| 3730 | int free_space; |
| 3731 | u32 left_nritems; |
| 3732 | int ret; |
| 3733 | |
| 3734 | if (!path->nodes[1]) |
| 3735 | return 1; |
| 3736 | |
| 3737 | slot = path->slots[1]; |
| 3738 | upper = path->nodes[1]; |
| 3739 | if (slot >= btrfs_header_nritems(upper) - 1) |
| 3740 | return 1; |
| 3741 | |
| 3742 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3743 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3744 | right = read_node_slot(fs_info, upper, slot + 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 3745 | /* |
| 3746 | * slot + 1 is not valid or we fail to read the right node, |
| 3747 | * no big deal, just return. |
| 3748 | */ |
| 3749 | if (IS_ERR(right)) |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3750 | return 1; |
| 3751 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3752 | btrfs_tree_lock(right); |
| 3753 | btrfs_set_lock_blocking(right); |
| 3754 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3755 | free_space = btrfs_leaf_free_space(fs_info, right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3756 | if (free_space < data_size) |
| 3757 | goto out_unlock; |
| 3758 | |
| 3759 | /* cow and double check */ |
| 3760 | ret = btrfs_cow_block(trans, root, right, upper, |
| 3761 | slot + 1, &right); |
| 3762 | if (ret) |
| 3763 | goto out_unlock; |
| 3764 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3765 | free_space = btrfs_leaf_free_space(fs_info, right); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3766 | if (free_space < data_size) |
| 3767 | goto out_unlock; |
| 3768 | |
| 3769 | left_nritems = btrfs_header_nritems(left); |
| 3770 | if (left_nritems == 0) |
| 3771 | goto out_unlock; |
| 3772 | |
Filipe David Borba Manana | 2ef1fed | 2013-12-04 22:17:39 +0000 | [diff] [blame] | 3773 | if (path->slots[0] == left_nritems && !empty) { |
| 3774 | /* Key greater than all keys in the leaf, right neighbor has |
| 3775 | * enough room for it and we're not emptying our leaf to delete |
| 3776 | * it, therefore use right neighbor to insert the new item and |
| 3777 | * no need to touch/dirty our left leaft. */ |
| 3778 | btrfs_tree_unlock(left); |
| 3779 | free_extent_buffer(left); |
| 3780 | path->nodes[0] = right; |
| 3781 | path->slots[0] = 0; |
| 3782 | path->slots[1]++; |
| 3783 | return 0; |
| 3784 | } |
| 3785 | |
David Sterba | 1e47eef | 2017-02-10 19:13:06 +0100 | [diff] [blame] | 3786 | return __push_leaf_right(fs_info, path, min_data_size, empty, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3787 | right, free_space, left_nritems, min_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3788 | out_unlock: |
| 3789 | btrfs_tree_unlock(right); |
| 3790 | free_extent_buffer(right); |
| 3791 | return 1; |
| 3792 | } |
| 3793 | |
| 3794 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3795 | * push some data in the path leaf to the left, trying to free up at |
| 3796 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3797 | * |
| 3798 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3799 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the |
| 3800 | * items |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3801 | */ |
David Sterba | 66cb7dd | 2017-02-10 19:14:36 +0100 | [diff] [blame] | 3802 | static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3803 | struct btrfs_path *path, int data_size, |
| 3804 | int empty, struct extent_buffer *left, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3805 | int free_space, u32 right_nritems, |
| 3806 | u32 max_slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3807 | { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3808 | struct btrfs_disk_key disk_key; |
| 3809 | struct extent_buffer *right = path->nodes[0]; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3810 | int i; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3811 | int push_space = 0; |
| 3812 | int push_items = 0; |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 3813 | struct btrfs_item *item; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 3814 | u32 old_left_nritems; |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3815 | u32 nr; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3816 | int ret = 0; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3817 | u32 this_item_size; |
| 3818 | u32 old_left_item_size; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3819 | struct btrfs_map_token token; |
| 3820 | |
| 3821 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3822 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3823 | if (empty) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3824 | nr = min(right_nritems, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3825 | else |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3826 | nr = min(right_nritems - 1, max_slot); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3827 | |
| 3828 | for (i = 0; i < nr; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3829 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3830 | |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3831 | if (!empty && push_items > 0) { |
| 3832 | if (path->slots[0] < i) |
| 3833 | break; |
| 3834 | if (path->slots[0] == i) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3835 | int space = btrfs_leaf_free_space(fs_info, right); |
Zheng Yan | 31840ae | 2008-09-23 13:14:14 -0400 | [diff] [blame] | 3836 | if (space + push_space * 2 > free_space) |
| 3837 | break; |
| 3838 | } |
| 3839 | } |
| 3840 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3841 | if (path->slots[0] == i) |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3842 | push_space += data_size; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3843 | |
| 3844 | this_item_size = btrfs_item_size(right, item); |
| 3845 | if (this_item_size + sizeof(*item) + push_space > free_space) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3846 | break; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3847 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3848 | push_items++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3849 | push_space += this_item_size + sizeof(*item); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3850 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3851 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3852 | if (push_items == 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3853 | ret = 1; |
| 3854 | goto out; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3855 | } |
Dulshani Gunawardhana | fae7f21 | 2013-10-31 10:30:08 +0530 | [diff] [blame] | 3856 | WARN_ON(!empty && push_items == btrfs_header_nritems(right)); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3857 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3858 | /* push data from right to left */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3859 | copy_extent_buffer(left, right, |
| 3860 | btrfs_item_nr_offset(btrfs_header_nritems(left)), |
| 3861 | btrfs_item_nr_offset(0), |
| 3862 | push_items * sizeof(struct btrfs_item)); |
| 3863 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3864 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info) - |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3865 | btrfs_item_offset_nr(right, push_items - 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3866 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3867 | copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3868 | leaf_data_end(fs_info, left) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3869 | BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3870 | btrfs_item_offset_nr(right, push_items - 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 3871 | push_space); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3872 | old_left_nritems = btrfs_header_nritems(left); |
Yan Zheng | 87b29b2 | 2008-12-17 10:21:48 -0500 | [diff] [blame] | 3873 | BUG_ON(old_left_nritems <= 0); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3874 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3875 | 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] | 3876 | for (i = old_left_nritems; i < old_left_nritems + push_items; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3877 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3878 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3879 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3880 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3881 | ioff = btrfs_token_item_offset(left, item, &token); |
| 3882 | btrfs_set_token_item_offset(left, item, |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3883 | ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size), |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3884 | &token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3885 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3886 | btrfs_set_header_nritems(left, old_left_nritems + push_items); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3887 | |
| 3888 | /* fixup right node */ |
Julia Lawall | 31b1a2b | 2012-11-03 10:58:34 +0000 | [diff] [blame] | 3889 | if (push_items > right_nritems) |
| 3890 | WARN(1, KERN_CRIT "push items %d nr %u\n", push_items, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 3891 | right_nritems); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3892 | |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3893 | if (push_items < right_nritems) { |
| 3894 | push_space = btrfs_item_offset_nr(right, push_items - 1) - |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3895 | leaf_data_end(fs_info, right); |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3896 | memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3897 | BTRFS_LEAF_DATA_SIZE(fs_info) - push_space, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 3898 | BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3899 | leaf_data_end(fs_info, right), push_space); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3900 | |
| 3901 | memmove_extent_buffer(right, btrfs_item_nr_offset(0), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3902 | btrfs_item_nr_offset(push_items), |
| 3903 | (btrfs_header_nritems(right) - push_items) * |
| 3904 | sizeof(struct btrfs_item)); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3905 | } |
Yan | eef1c49 | 2007-11-26 10:58:13 -0500 | [diff] [blame] | 3906 | right_nritems -= push_items; |
| 3907 | btrfs_set_header_nritems(right, right_nritems); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3908 | push_space = BTRFS_LEAF_DATA_SIZE(fs_info); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3909 | for (i = 0; i < right_nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 3910 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3911 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 3912 | push_space = push_space - btrfs_token_item_size(right, |
| 3913 | item, &token); |
| 3914 | btrfs_set_token_item_offset(right, item, push_space, &token); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 3915 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3916 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3917 | btrfs_mark_buffer_dirty(left); |
Chris Mason | 34a3821 | 2007-11-07 13:31:03 -0500 | [diff] [blame] | 3918 | if (right_nritems) |
| 3919 | btrfs_mark_buffer_dirty(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 3920 | else |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 3921 | clean_tree_block(fs_info, right); |
Chris Mason | 098f59c | 2007-05-11 11:33:21 -0400 | [diff] [blame] | 3922 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3923 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 3924 | fixup_low_keys(fs_info, path, &disk_key, 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3925 | |
| 3926 | /* then fixup the leaf pointer in the path */ |
| 3927 | if (path->slots[0] < push_items) { |
| 3928 | path->slots[0] += old_left_nritems; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3929 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3930 | free_extent_buffer(path->nodes[0]); |
| 3931 | path->nodes[0] = left; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3932 | path->slots[1] -= 1; |
| 3933 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3934 | btrfs_tree_unlock(left); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 3935 | free_extent_buffer(left); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3936 | path->slots[0] -= push_items; |
| 3937 | } |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 3938 | BUG_ON(path->slots[0] < 0); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 3939 | return ret; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 3940 | out: |
| 3941 | btrfs_tree_unlock(left); |
| 3942 | free_extent_buffer(left); |
| 3943 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 3944 | } |
| 3945 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 3946 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3947 | * push some data in the path leaf to the left, trying to free up at |
| 3948 | * least data_size bytes. returns zero if the push worked, nonzero otherwise |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 3949 | * |
| 3950 | * max_slot can put a limit on how far into the leaf we'll push items. The |
| 3951 | * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the |
| 3952 | * items |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3953 | */ |
| 3954 | 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] | 3955 | *root, struct btrfs_path *path, int min_data_size, |
| 3956 | int data_size, int empty, u32 max_slot) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3957 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3958 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3959 | struct extent_buffer *right = path->nodes[0]; |
| 3960 | struct extent_buffer *left; |
| 3961 | int slot; |
| 3962 | int free_space; |
| 3963 | u32 right_nritems; |
| 3964 | int ret = 0; |
| 3965 | |
| 3966 | slot = path->slots[1]; |
| 3967 | if (slot == 0) |
| 3968 | return 1; |
| 3969 | if (!path->nodes[1]) |
| 3970 | return 1; |
| 3971 | |
| 3972 | right_nritems = btrfs_header_nritems(right); |
| 3973 | if (right_nritems == 0) |
| 3974 | return 1; |
| 3975 | |
| 3976 | btrfs_assert_tree_locked(path->nodes[1]); |
| 3977 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3978 | left = read_node_slot(fs_info, path->nodes[1], slot - 1); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 3979 | /* |
| 3980 | * slot - 1 is not valid or we fail to read the left node, |
| 3981 | * no big deal, just return. |
| 3982 | */ |
| 3983 | if (IS_ERR(left)) |
Tsutomu Itoh | 91ca338 | 2011-01-05 02:32:22 +0000 | [diff] [blame] | 3984 | return 1; |
| 3985 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3986 | btrfs_tree_lock(left); |
| 3987 | btrfs_set_lock_blocking(left); |
| 3988 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 3989 | free_space = btrfs_leaf_free_space(fs_info, left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 3990 | if (free_space < data_size) { |
| 3991 | ret = 1; |
| 3992 | goto out; |
| 3993 | } |
| 3994 | |
| 3995 | /* cow and double check */ |
| 3996 | ret = btrfs_cow_block(trans, root, left, |
| 3997 | path->nodes[1], slot - 1, &left); |
| 3998 | if (ret) { |
| 3999 | /* we hit -ENOSPC, but it isn't fatal here */ |
Jeff Mahoney | 79787ea | 2012-03-12 16:03:00 +0100 | [diff] [blame] | 4000 | if (ret == -ENOSPC) |
| 4001 | ret = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4002 | goto out; |
| 4003 | } |
| 4004 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4005 | free_space = btrfs_leaf_free_space(fs_info, left); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4006 | if (free_space < data_size) { |
| 4007 | ret = 1; |
| 4008 | goto out; |
| 4009 | } |
| 4010 | |
David Sterba | 66cb7dd | 2017-02-10 19:14:36 +0100 | [diff] [blame] | 4011 | return __push_leaf_left(fs_info, path, min_data_size, |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4012 | empty, left, free_space, right_nritems, |
| 4013 | max_slot); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4014 | out: |
| 4015 | btrfs_tree_unlock(left); |
| 4016 | free_extent_buffer(left); |
| 4017 | return ret; |
| 4018 | } |
| 4019 | |
| 4020 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4021 | * split the path's leaf in two, making sure there is at least data_size |
| 4022 | * available for the resulting leaf level of the path. |
| 4023 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4024 | static noinline void copy_for_split(struct btrfs_trans_handle *trans, |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4025 | struct btrfs_fs_info *fs_info, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4026 | struct btrfs_path *path, |
| 4027 | struct extent_buffer *l, |
| 4028 | struct extent_buffer *right, |
| 4029 | int slot, int mid, int nritems) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4030 | { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4031 | int data_copy_size; |
| 4032 | int rt_data_off; |
| 4033 | int i; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4034 | struct btrfs_disk_key disk_key; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4035 | struct btrfs_map_token token; |
| 4036 | |
| 4037 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4038 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4039 | nritems = nritems - mid; |
| 4040 | btrfs_set_header_nritems(right, nritems); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4041 | data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4042 | |
| 4043 | copy_extent_buffer(right, l, btrfs_item_nr_offset(0), |
| 4044 | btrfs_item_nr_offset(mid), |
| 4045 | nritems * sizeof(struct btrfs_item)); |
| 4046 | |
| 4047 | copy_extent_buffer(right, l, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4048 | BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) - |
| 4049 | data_copy_size, BTRFS_LEAF_DATA_OFFSET + |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4050 | leaf_data_end(fs_info, l), data_copy_size); |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4051 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4052 | 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] | 4053 | |
| 4054 | for (i = 0; i < nritems; i++) { |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4055 | struct btrfs_item *item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4056 | u32 ioff; |
| 4057 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4058 | ioff = btrfs_token_item_offset(right, item, &token); |
| 4059 | btrfs_set_token_item_offset(right, item, |
| 4060 | ioff + rt_data_off, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4061 | } |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4062 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4063 | btrfs_set_header_nritems(l, mid); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4064 | btrfs_item_key(right, &disk_key, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4065 | insert_ptr(trans, fs_info, path, &disk_key, right->start, |
Jan Schmidt | c3e0696 | 2012-06-21 11:01:06 +0200 | [diff] [blame] | 4066 | path->slots[1] + 1, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4067 | |
| 4068 | btrfs_mark_buffer_dirty(right); |
| 4069 | btrfs_mark_buffer_dirty(l); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4070 | BUG_ON(path->slots[0] != slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4071 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4072 | if (mid <= slot) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4073 | btrfs_tree_unlock(path->nodes[0]); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4074 | free_extent_buffer(path->nodes[0]); |
| 4075 | path->nodes[0] = right; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4076 | path->slots[0] -= mid; |
| 4077 | path->slots[1] += 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4078 | } else { |
| 4079 | btrfs_tree_unlock(right); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4080 | free_extent_buffer(right); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 4081 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4082 | |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4083 | BUG_ON(path->slots[0] < 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4084 | } |
| 4085 | |
| 4086 | /* |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4087 | * double splits happen when we need to insert a big item in the middle |
| 4088 | * of a leaf. A double split can leave us with 3 mostly empty leaves: |
| 4089 | * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ] |
| 4090 | * A B C |
| 4091 | * |
| 4092 | * We avoid this by trying to push the items on either side of our target |
| 4093 | * into the adjacent leaves. If all goes well we can avoid the double split |
| 4094 | * completely. |
| 4095 | */ |
| 4096 | static noinline int push_for_double_split(struct btrfs_trans_handle *trans, |
| 4097 | struct btrfs_root *root, |
| 4098 | struct btrfs_path *path, |
| 4099 | int data_size) |
| 4100 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4101 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4102 | int ret; |
| 4103 | int progress = 0; |
| 4104 | int slot; |
| 4105 | u32 nritems; |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4106 | int space_needed = data_size; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4107 | |
| 4108 | slot = path->slots[0]; |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4109 | if (slot < btrfs_header_nritems(path->nodes[0])) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4110 | space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4111 | |
| 4112 | /* |
| 4113 | * try to push all the items after our slot into the |
| 4114 | * right leaf |
| 4115 | */ |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4116 | ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4117 | if (ret < 0) |
| 4118 | return ret; |
| 4119 | |
| 4120 | if (ret == 0) |
| 4121 | progress++; |
| 4122 | |
| 4123 | nritems = btrfs_header_nritems(path->nodes[0]); |
| 4124 | /* |
| 4125 | * our goal is to get our slot at the start or end of a leaf. If |
| 4126 | * we've done so we're done |
| 4127 | */ |
| 4128 | if (path->slots[0] == 0 || path->slots[0] == nritems) |
| 4129 | return 0; |
| 4130 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4131 | if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4132 | return 0; |
| 4133 | |
| 4134 | /* try to push all the items before our slot into the next leaf */ |
| 4135 | slot = path->slots[0]; |
Filipe Manana | 263d399 | 2017-02-17 18:43:57 +0000 | [diff] [blame] | 4136 | space_needed = data_size; |
| 4137 | if (slot > 0) |
| 4138 | space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4139 | ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4140 | if (ret < 0) |
| 4141 | return ret; |
| 4142 | |
| 4143 | if (ret == 0) |
| 4144 | progress++; |
| 4145 | |
| 4146 | if (progress) |
| 4147 | return 0; |
| 4148 | return 1; |
| 4149 | } |
| 4150 | |
| 4151 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4152 | * split the path's leaf in two, making sure there is at least data_size |
| 4153 | * available for the resulting leaf level of the path. |
| 4154 | * |
| 4155 | * returns 0 if all went well and < 0 on failure. |
| 4156 | */ |
| 4157 | static noinline int split_leaf(struct btrfs_trans_handle *trans, |
| 4158 | struct btrfs_root *root, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4159 | const struct btrfs_key *ins_key, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4160 | struct btrfs_path *path, int data_size, |
| 4161 | int extend) |
| 4162 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4163 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4164 | struct extent_buffer *l; |
| 4165 | u32 nritems; |
| 4166 | int mid; |
| 4167 | int slot; |
| 4168 | struct extent_buffer *right; |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 4169 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4170 | int ret = 0; |
| 4171 | int wret; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4172 | int split; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4173 | int num_doubles = 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4174 | int tried_avoid_double = 0; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4175 | |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4176 | l = path->nodes[0]; |
| 4177 | slot = path->slots[0]; |
| 4178 | if (extend && data_size + btrfs_item_size_nr(l, slot) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4179 | sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info)) |
Yan, Zheng | a571952 | 2009-09-24 09:17:31 -0400 | [diff] [blame] | 4180 | return -EOVERFLOW; |
| 4181 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4182 | /* first try to make some room by pushing left and right */ |
Liu Bo | 33157e0 | 2013-05-22 12:07:06 +0000 | [diff] [blame] | 4183 | if (data_size && path->nodes[1]) { |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4184 | int space_needed = data_size; |
| 4185 | |
| 4186 | if (slot < btrfs_header_nritems(l)) |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4187 | space_needed -= btrfs_leaf_free_space(fs_info, l); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4188 | |
| 4189 | wret = push_leaf_right(trans, root, path, space_needed, |
| 4190 | space_needed, 0, 0); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4191 | if (wret < 0) |
| 4192 | return wret; |
| 4193 | if (wret) { |
Filipe Manana | 263d399 | 2017-02-17 18:43:57 +0000 | [diff] [blame] | 4194 | space_needed = data_size; |
| 4195 | if (slot > 0) |
| 4196 | space_needed -= btrfs_leaf_free_space(fs_info, |
| 4197 | l); |
Filipe David Borba Manana | 5a4267c | 2013-11-25 03:20:46 +0000 | [diff] [blame] | 4198 | wret = push_leaf_left(trans, root, path, space_needed, |
| 4199 | space_needed, 0, (u32)-1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4200 | if (wret < 0) |
| 4201 | return wret; |
| 4202 | } |
| 4203 | l = path->nodes[0]; |
| 4204 | |
| 4205 | /* did the pushes work? */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4206 | if (btrfs_leaf_free_space(fs_info, l) >= data_size) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4207 | return 0; |
| 4208 | } |
| 4209 | |
| 4210 | if (!path->nodes[1]) { |
Liu Bo | fdd99c7 | 2013-05-22 12:06:51 +0000 | [diff] [blame] | 4211 | ret = insert_new_root(trans, root, path, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4212 | if (ret) |
| 4213 | return ret; |
| 4214 | } |
| 4215 | again: |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4216 | split = 1; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4217 | l = path->nodes[0]; |
| 4218 | slot = path->slots[0]; |
| 4219 | nritems = btrfs_header_nritems(l); |
| 4220 | mid = (nritems + 1) / 2; |
| 4221 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4222 | if (mid <= slot) { |
| 4223 | if (nritems == 1 || |
| 4224 | leaf_space_used(l, mid, nritems - mid) + data_size > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4225 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4226 | if (slot >= nritems) { |
| 4227 | split = 0; |
| 4228 | } else { |
| 4229 | mid = slot; |
| 4230 | if (mid != nritems && |
| 4231 | leaf_space_used(l, mid, nritems - mid) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4232 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4233 | if (data_size && !tried_avoid_double) |
| 4234 | goto push_for_double; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4235 | split = 2; |
| 4236 | } |
| 4237 | } |
| 4238 | } |
| 4239 | } else { |
| 4240 | if (leaf_space_used(l, 0, mid) + data_size > |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4241 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4242 | if (!extend && data_size && slot == 0) { |
| 4243 | split = 0; |
| 4244 | } else if ((extend || !data_size) && slot == 0) { |
| 4245 | mid = 1; |
| 4246 | } else { |
| 4247 | mid = slot; |
| 4248 | if (mid != nritems && |
| 4249 | leaf_space_used(l, mid, nritems - mid) + |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4250 | data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4251 | if (data_size && !tried_avoid_double) |
| 4252 | goto push_for_double; |
Dulshani Gunawardhana | 6787125 | 2013-10-31 10:33:04 +0530 | [diff] [blame] | 4253 | split = 2; |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4254 | } |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | if (split == 0) |
| 4260 | btrfs_cpu_key_to_disk(&disk_key, ins_key); |
| 4261 | else |
| 4262 | btrfs_item_key(l, &disk_key, mid); |
| 4263 | |
David Sterba | 4d75f8a | 2014-06-15 01:54:12 +0200 | [diff] [blame] | 4264 | right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid, |
| 4265 | &disk_key, 0, l->start, 0); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4266 | if (IS_ERR(right)) |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4267 | return PTR_ERR(right); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4268 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4269 | root_add_used(root, fs_info->nodesize); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4270 | |
David Sterba | b159fa2 | 2016-11-08 18:09:03 +0100 | [diff] [blame] | 4271 | memzero_extent_buffer(right, 0, sizeof(struct btrfs_header)); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4272 | btrfs_set_header_bytenr(right, right->start); |
| 4273 | btrfs_set_header_generation(right, trans->transid); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4274 | btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4275 | btrfs_set_header_owner(right, root->root_key.objectid); |
| 4276 | btrfs_set_header_level(right, 0); |
David Sterba | d24ee97 | 2016-11-09 17:44:25 +0100 | [diff] [blame] | 4277 | write_extent_buffer_fsid(right, fs_info->fsid); |
| 4278 | write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4279 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4280 | if (split == 0) { |
| 4281 | if (mid <= slot) { |
| 4282 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4283 | insert_ptr(trans, fs_info, path, &disk_key, |
| 4284 | right->start, path->slots[1] + 1, 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4285 | btrfs_tree_unlock(path->nodes[0]); |
| 4286 | free_extent_buffer(path->nodes[0]); |
| 4287 | path->nodes[0] = right; |
| 4288 | path->slots[0] = 0; |
| 4289 | path->slots[1] += 1; |
| 4290 | } else { |
| 4291 | btrfs_set_header_nritems(right, 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4292 | insert_ptr(trans, fs_info, path, &disk_key, |
| 4293 | right->start, path->slots[1], 1); |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4294 | btrfs_tree_unlock(path->nodes[0]); |
| 4295 | free_extent_buffer(path->nodes[0]); |
| 4296 | path->nodes[0] = right; |
| 4297 | path->slots[0] = 0; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4298 | if (path->slots[1] == 0) |
Daniel Dressler | b7a0365 | 2014-11-12 13:43:09 +0900 | [diff] [blame] | 4299 | fixup_low_keys(fs_info, path, &disk_key, 1); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4300 | } |
Liu Bo | 196e024 | 2016-09-07 14:48:28 -0700 | [diff] [blame] | 4301 | /* |
| 4302 | * We create a new leaf 'right' for the required ins_len and |
| 4303 | * we'll do btrfs_mark_buffer_dirty() on this leaf after copying |
| 4304 | * the content of ins_len to 'right'. |
| 4305 | */ |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4306 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4307 | } |
| 4308 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4309 | copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems); |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4310 | |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4311 | if (split == 2) { |
Chris Mason | cc0c553 | 2007-10-25 15:42:57 -0400 | [diff] [blame] | 4312 | BUG_ON(num_doubles != 0); |
| 4313 | num_doubles++; |
| 4314 | goto again; |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4315 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4316 | |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4317 | return 0; |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4318 | |
| 4319 | push_for_double: |
| 4320 | push_for_double_split(trans, root, path, data_size); |
| 4321 | tried_avoid_double = 1; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4322 | if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size) |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 4323 | return 0; |
| 4324 | goto again; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4325 | } |
| 4326 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4327 | static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans, |
| 4328 | struct btrfs_root *root, |
| 4329 | struct btrfs_path *path, int ins_len) |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4330 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4331 | struct btrfs_fs_info *fs_info = root->fs_info; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4332 | struct btrfs_key key; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4333 | struct extent_buffer *leaf; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4334 | struct btrfs_file_extent_item *fi; |
| 4335 | u64 extent_len = 0; |
| 4336 | u32 item_size; |
| 4337 | int ret; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4338 | |
| 4339 | leaf = path->nodes[0]; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4340 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 4341 | |
| 4342 | BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY && |
| 4343 | key.type != BTRFS_EXTENT_CSUM_KEY); |
| 4344 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4345 | if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4346 | return 0; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4347 | |
| 4348 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4349 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4350 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4351 | struct btrfs_file_extent_item); |
| 4352 | extent_len = btrfs_file_extent_num_bytes(leaf, fi); |
| 4353 | } |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 4354 | btrfs_release_path(path); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4355 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4356 | path->keep_locks = 1; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4357 | path->search_for_split = 1; |
| 4358 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4359 | path->search_for_split = 0; |
Filipe Manana | a8df6fe | 2015-01-20 12:40:53 +0000 | [diff] [blame] | 4360 | if (ret > 0) |
| 4361 | ret = -EAGAIN; |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4362 | if (ret < 0) |
| 4363 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4364 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4365 | ret = -EAGAIN; |
| 4366 | leaf = path->nodes[0]; |
Filipe Manana | a8df6fe | 2015-01-20 12:40:53 +0000 | [diff] [blame] | 4367 | /* if our item isn't there, return now */ |
| 4368 | if (item_size != btrfs_item_size_nr(leaf, path->slots[0])) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4369 | goto err; |
| 4370 | |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4371 | /* the leaf has changed, it now has room. return now */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4372 | if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len) |
Chris Mason | 109f6ae | 2010-04-02 09:20:18 -0400 | [diff] [blame] | 4373 | goto err; |
| 4374 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4375 | if (key.type == BTRFS_EXTENT_DATA_KEY) { |
| 4376 | fi = btrfs_item_ptr(leaf, path->slots[0], |
| 4377 | struct btrfs_file_extent_item); |
| 4378 | if (extent_len != btrfs_file_extent_num_bytes(leaf, fi)) |
| 4379 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4380 | } |
| 4381 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4382 | btrfs_set_path_blocking(path); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4383 | ret = split_leaf(trans, root, &key, path, ins_len, 1); |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4384 | if (ret) |
| 4385 | goto err; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4386 | |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4387 | path->keep_locks = 0; |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4388 | btrfs_unlock_up_safe(path, 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4389 | return 0; |
| 4390 | err: |
| 4391 | path->keep_locks = 0; |
| 4392 | return ret; |
| 4393 | } |
| 4394 | |
David Sterba | 4961e29 | 2017-02-10 18:49:53 +0100 | [diff] [blame] | 4395 | static noinline int split_item(struct btrfs_fs_info *fs_info, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4396 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4397 | const struct btrfs_key *new_key, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4398 | unsigned long split_offset) |
| 4399 | { |
| 4400 | struct extent_buffer *leaf; |
| 4401 | struct btrfs_item *item; |
| 4402 | struct btrfs_item *new_item; |
| 4403 | int slot; |
| 4404 | char *buf; |
| 4405 | u32 nritems; |
| 4406 | u32 item_size; |
| 4407 | u32 orig_offset; |
| 4408 | struct btrfs_disk_key disk_key; |
| 4409 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4410 | leaf = path->nodes[0]; |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4411 | BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item)); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4412 | |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 4413 | btrfs_set_path_blocking(path); |
| 4414 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4415 | item = btrfs_item_nr(path->slots[0]); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4416 | orig_offset = btrfs_item_offset(leaf, item); |
| 4417 | item_size = btrfs_item_size(leaf, item); |
| 4418 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4419 | buf = kmalloc(item_size, GFP_NOFS); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4420 | if (!buf) |
| 4421 | return -ENOMEM; |
| 4422 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4423 | read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf, |
| 4424 | path->slots[0]), item_size); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4425 | |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4426 | slot = path->slots[0] + 1; |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4427 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4428 | if (slot != nritems) { |
| 4429 | /* shift the items */ |
| 4430 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1), |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4431 | btrfs_item_nr_offset(slot), |
| 4432 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | btrfs_cpu_key_to_disk(&disk_key, new_key); |
| 4436 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4437 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4438 | new_item = btrfs_item_nr(slot); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4439 | |
| 4440 | btrfs_set_item_offset(leaf, new_item, orig_offset); |
| 4441 | btrfs_set_item_size(leaf, new_item, item_size - split_offset); |
| 4442 | |
| 4443 | btrfs_set_item_offset(leaf, item, |
| 4444 | orig_offset + item_size - split_offset); |
| 4445 | btrfs_set_item_size(leaf, item, split_offset); |
| 4446 | |
| 4447 | btrfs_set_header_nritems(leaf, nritems + 1); |
| 4448 | |
| 4449 | /* write the data for the start of the original item */ |
| 4450 | write_extent_buffer(leaf, buf, |
| 4451 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4452 | split_offset); |
| 4453 | |
| 4454 | /* write the data for the new item */ |
| 4455 | write_extent_buffer(leaf, buf + split_offset, |
| 4456 | btrfs_item_ptr_offset(leaf, slot), |
| 4457 | item_size - split_offset); |
| 4458 | btrfs_mark_buffer_dirty(leaf); |
| 4459 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4460 | BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4461 | kfree(buf); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4462 | return 0; |
| 4463 | } |
| 4464 | |
| 4465 | /* |
| 4466 | * This function splits a single item into two items, |
| 4467 | * giving 'new_key' to the new item and splitting the |
| 4468 | * old one at split_offset (from the start of the item). |
| 4469 | * |
| 4470 | * The path may be released by this operation. After |
| 4471 | * the split, the path is pointing to the old item. The |
| 4472 | * new item is going to be in the same node as the old one. |
| 4473 | * |
| 4474 | * Note, the item being split must be smaller enough to live alone on |
| 4475 | * a tree block with room for one extra struct btrfs_item |
| 4476 | * |
| 4477 | * This allows us to split the item in place, keeping a lock on the |
| 4478 | * leaf the entire time. |
| 4479 | */ |
| 4480 | int btrfs_split_item(struct btrfs_trans_handle *trans, |
| 4481 | struct btrfs_root *root, |
| 4482 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4483 | const struct btrfs_key *new_key, |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4484 | unsigned long split_offset) |
| 4485 | { |
| 4486 | int ret; |
| 4487 | ret = setup_leaf_for_split(trans, root, path, |
| 4488 | sizeof(struct btrfs_item)); |
| 4489 | if (ret) |
| 4490 | return ret; |
| 4491 | |
David Sterba | 4961e29 | 2017-02-10 18:49:53 +0100 | [diff] [blame] | 4492 | ret = split_item(root->fs_info, path, new_key, split_offset); |
Chris Mason | 459931e | 2008-12-10 09:10:46 -0500 | [diff] [blame] | 4493 | return ret; |
| 4494 | } |
| 4495 | |
| 4496 | /* |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4497 | * This function duplicate a item, giving 'new_key' to the new item. |
| 4498 | * It guarantees both items live in the same tree leaf and the new item |
| 4499 | * is contiguous with the original item. |
| 4500 | * |
| 4501 | * This allows us to split file extent in place, keeping a lock on the |
| 4502 | * leaf the entire time. |
| 4503 | */ |
| 4504 | int btrfs_duplicate_item(struct btrfs_trans_handle *trans, |
| 4505 | struct btrfs_root *root, |
| 4506 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4507 | const struct btrfs_key *new_key) |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4508 | { |
| 4509 | struct extent_buffer *leaf; |
| 4510 | int ret; |
| 4511 | u32 item_size; |
| 4512 | |
| 4513 | leaf = path->nodes[0]; |
| 4514 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 4515 | ret = setup_leaf_for_split(trans, root, path, |
| 4516 | item_size + sizeof(struct btrfs_item)); |
| 4517 | if (ret) |
| 4518 | return ret; |
| 4519 | |
| 4520 | path->slots[0]++; |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4521 | setup_items_for_insert(root, path, new_key, &item_size, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4522 | item_size, item_size + |
| 4523 | sizeof(struct btrfs_item), 1); |
Yan, Zheng | ad48fd75 | 2009-11-12 09:33:58 +0000 | [diff] [blame] | 4524 | leaf = path->nodes[0]; |
| 4525 | memcpy_extent_buffer(leaf, |
| 4526 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 4527 | btrfs_item_ptr_offset(leaf, path->slots[0] - 1), |
| 4528 | item_size); |
| 4529 | return 0; |
| 4530 | } |
| 4531 | |
| 4532 | /* |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4533 | * make the item pointed to by the path smaller. new_size indicates |
| 4534 | * how small to make it, and from_end tells us if we just chop bytes |
| 4535 | * off the end of the item or if we shift the item to chop bytes off |
| 4536 | * the front. |
| 4537 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4538 | void btrfs_truncate_item(struct btrfs_fs_info *fs_info, |
| 4539 | struct btrfs_path *path, u32 new_size, int from_end) |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4540 | { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4541 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4542 | struct extent_buffer *leaf; |
| 4543 | struct btrfs_item *item; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4544 | u32 nritems; |
| 4545 | unsigned int data_end; |
| 4546 | unsigned int old_data_start; |
| 4547 | unsigned int old_size; |
| 4548 | unsigned int size_diff; |
| 4549 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4550 | struct btrfs_map_token token; |
| 4551 | |
| 4552 | btrfs_init_map_token(&token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4553 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4554 | leaf = path->nodes[0]; |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4555 | slot = path->slots[0]; |
| 4556 | |
| 4557 | old_size = btrfs_item_size_nr(leaf, slot); |
| 4558 | if (old_size == new_size) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4559 | return; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4560 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4561 | nritems = btrfs_header_nritems(leaf); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4562 | data_end = leaf_data_end(fs_info, leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4563 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4564 | old_data_start = btrfs_item_offset_nr(leaf, slot); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4565 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4566 | size_diff = old_size - new_size; |
| 4567 | |
| 4568 | BUG_ON(slot < 0); |
| 4569 | BUG_ON(slot >= nritems); |
| 4570 | |
| 4571 | /* |
| 4572 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4573 | */ |
| 4574 | /* first correct the data pointers */ |
| 4575 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4576 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4577 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4578 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4579 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4580 | btrfs_set_token_item_offset(leaf, item, |
| 4581 | ioff + size_diff, &token); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4582 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4583 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4584 | /* shift the data */ |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4585 | if (from_end) { |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4586 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4587 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4588 | data_end, old_data_start + new_size - data_end); |
| 4589 | } else { |
| 4590 | struct btrfs_disk_key disk_key; |
| 4591 | u64 offset; |
| 4592 | |
| 4593 | btrfs_item_key(leaf, &disk_key, slot); |
| 4594 | |
| 4595 | if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) { |
| 4596 | unsigned long ptr; |
| 4597 | struct btrfs_file_extent_item *fi; |
| 4598 | |
| 4599 | fi = btrfs_item_ptr(leaf, slot, |
| 4600 | struct btrfs_file_extent_item); |
| 4601 | fi = (struct btrfs_file_extent_item *)( |
| 4602 | (unsigned long)fi - size_diff); |
| 4603 | |
| 4604 | if (btrfs_file_extent_type(leaf, fi) == |
| 4605 | BTRFS_FILE_EXTENT_INLINE) { |
| 4606 | ptr = btrfs_item_ptr_offset(leaf, slot); |
| 4607 | memmove_extent_buffer(leaf, ptr, |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4608 | (unsigned long)fi, |
David Sterba | 7ec20af | 2014-07-24 17:34:58 +0200 | [diff] [blame] | 4609 | BTRFS_FILE_EXTENT_INLINE_DATA_START); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4610 | } |
| 4611 | } |
| 4612 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4613 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4614 | data_end + size_diff, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4615 | data_end, old_data_start - data_end); |
| 4616 | |
| 4617 | offset = btrfs_disk_key_offset(&disk_key); |
| 4618 | btrfs_set_disk_key_offset(&disk_key, offset + size_diff); |
| 4619 | btrfs_set_item_key(leaf, &disk_key, slot); |
| 4620 | if (slot == 0) |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4621 | fixup_low_keys(fs_info, path, &disk_key, 1); |
Chris Mason | 179e29e | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 4622 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4623 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4624 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4625 | btrfs_set_item_size(leaf, item, new_size); |
| 4626 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4627 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4628 | if (btrfs_leaf_free_space(fs_info, leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4629 | btrfs_print_leaf(leaf); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4630 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4631 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 4632 | } |
| 4633 | |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4634 | /* |
Stefan Behrens | 8f69dbd | 2013-05-07 10:23:30 +0000 | [diff] [blame] | 4635 | * 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] | 4636 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4637 | void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4638 | u32 data_size) |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4639 | { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4640 | int slot; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4641 | struct extent_buffer *leaf; |
| 4642 | struct btrfs_item *item; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4643 | u32 nritems; |
| 4644 | unsigned int data_end; |
| 4645 | unsigned int old_data; |
| 4646 | unsigned int old_size; |
| 4647 | int i; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4648 | struct btrfs_map_token token; |
| 4649 | |
| 4650 | btrfs_init_map_token(&token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4651 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4652 | leaf = path->nodes[0]; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4653 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4654 | nritems = btrfs_header_nritems(leaf); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4655 | data_end = leaf_data_end(fs_info, leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4656 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4657 | if (btrfs_leaf_free_space(fs_info, leaf) < data_size) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4658 | btrfs_print_leaf(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4659 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4660 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4661 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4662 | old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4663 | |
| 4664 | BUG_ON(slot < 0); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4665 | if (slot >= nritems) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4666 | btrfs_print_leaf(leaf); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4667 | btrfs_crit(fs_info, "slot %d too large, nritems %d", |
| 4668 | slot, nritems); |
Chris Mason | 3326d1b | 2007-10-15 16:18:25 -0400 | [diff] [blame] | 4669 | BUG_ON(1); |
| 4670 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4671 | |
| 4672 | /* |
| 4673 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4674 | */ |
| 4675 | /* first correct the data pointers */ |
| 4676 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4677 | u32 ioff; |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4678 | item = btrfs_item_nr(i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4679 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4680 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4681 | btrfs_set_token_item_offset(leaf, item, |
| 4682 | ioff - data_size, &token); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4683 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4684 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4685 | /* shift the data */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4686 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4687 | data_end - data_size, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4688 | data_end, old_data - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4689 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4690 | data_end = old_data; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4691 | old_size = btrfs_item_size_nr(leaf, slot); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4692 | item = btrfs_item_nr(slot); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4693 | btrfs_set_item_size(leaf, item, old_size + data_size); |
| 4694 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4695 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4696 | if (btrfs_leaf_free_space(fs_info, leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4697 | btrfs_print_leaf(leaf); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4698 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4699 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 4700 | } |
| 4701 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4702 | /* |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4703 | * this is a helper for btrfs_insert_empty_items, the main goal here is |
| 4704 | * to save stack depth by doing the bulk of the work in a function |
| 4705 | * that doesn't call btrfs_search_slot |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4706 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4707 | 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] | 4708 | const struct btrfs_key *cpu_key, u32 *data_size, |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4709 | u32 total_data, u32 total_size, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4710 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4711 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4712 | struct btrfs_item *item; |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4713 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4714 | u32 nritems; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4715 | unsigned int data_end; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4716 | struct btrfs_disk_key disk_key; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4717 | struct extent_buffer *leaf; |
| 4718 | int slot; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4719 | struct btrfs_map_token token; |
| 4720 | |
Filipe Manana | 24cdc84 | 2014-07-28 19:34:35 +0100 | [diff] [blame] | 4721 | if (path->slots[0] == 0) { |
| 4722 | btrfs_cpu_key_to_disk(&disk_key, cpu_key); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4723 | fixup_low_keys(fs_info, path, &disk_key, 1); |
Filipe Manana | 24cdc84 | 2014-07-28 19:34:35 +0100 | [diff] [blame] | 4724 | } |
| 4725 | btrfs_unlock_up_safe(path, 1); |
| 4726 | |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4727 | btrfs_init_map_token(&token); |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 4728 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4729 | leaf = path->nodes[0]; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4730 | slot = path->slots[0]; |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4731 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4732 | nritems = btrfs_header_nritems(leaf); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4733 | data_end = leaf_data_end(fs_info, leaf); |
Chris Mason | eb60cea | 2007-02-02 09:18:22 -0500 | [diff] [blame] | 4734 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4735 | if (btrfs_leaf_free_space(fs_info, leaf) < total_size) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4736 | btrfs_print_leaf(leaf); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4737 | btrfs_crit(fs_info, "not enough freespace need %u have %d", |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4738 | total_size, btrfs_leaf_free_space(fs_info, leaf)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4739 | BUG(); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 4740 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4741 | |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4742 | if (slot != nritems) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4743 | unsigned int old_data = btrfs_item_end_nr(leaf, slot); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4744 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4745 | if (old_data < data_end) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4746 | btrfs_print_leaf(leaf); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4747 | btrfs_crit(fs_info, "slot %d old_data %d data_end %d", |
Jeff Mahoney | 5d163e0 | 2016-09-20 10:05:00 -0400 | [diff] [blame] | 4748 | slot, old_data, data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4749 | BUG_ON(1); |
| 4750 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4751 | /* |
| 4752 | * item0..itemN ... dataN.offset..dataN.size .. data0.size |
| 4753 | */ |
| 4754 | /* first correct the data pointers */ |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4755 | for (i = slot; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4756 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4757 | |
Jeff Mahoney | 62e8557 | 2016-09-20 10:05:01 -0400 | [diff] [blame] | 4758 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4759 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4760 | btrfs_set_token_item_offset(leaf, item, |
| 4761 | ioff - total_data, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4762 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4763 | /* shift the items */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4764 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr), |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4765 | btrfs_item_nr_offset(slot), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4766 | (nritems - slot) * sizeof(struct btrfs_item)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4767 | |
| 4768 | /* shift the data */ |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4769 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
| 4770 | data_end - total_data, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4771 | data_end, old_data - data_end); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4772 | data_end = old_data; |
| 4773 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4774 | |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4775 | /* setup the item for the new data */ |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4776 | for (i = 0; i < nr; i++) { |
| 4777 | btrfs_cpu_key_to_disk(&disk_key, cpu_key + i); |
| 4778 | btrfs_set_item_key(leaf, &disk_key, slot + i); |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4779 | item = btrfs_item_nr(slot + i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4780 | btrfs_set_token_item_offset(leaf, item, |
| 4781 | data_end - data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4782 | data_end -= data_size[i]; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4783 | btrfs_set_token_item_size(leaf, item, data_size[i], &token); |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4784 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4785 | |
Chris Mason | 9c58309 | 2008-01-29 15:15:18 -0500 | [diff] [blame] | 4786 | btrfs_set_header_nritems(leaf, nritems + nr); |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 4787 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4788 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4789 | if (btrfs_leaf_free_space(fs_info, leaf) < 0) { |
David Sterba | a4f7875 | 2017-06-29 18:37:49 +0200 | [diff] [blame] | 4790 | btrfs_print_leaf(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4791 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4792 | } |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | /* |
| 4796 | * Given a key and some data, insert items into the tree. |
| 4797 | * This does all the path init required, making room in the tree if needed. |
| 4798 | */ |
| 4799 | int btrfs_insert_empty_items(struct btrfs_trans_handle *trans, |
| 4800 | struct btrfs_root *root, |
| 4801 | struct btrfs_path *path, |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4802 | const struct btrfs_key *cpu_key, u32 *data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4803 | int nr) |
| 4804 | { |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4805 | int ret = 0; |
| 4806 | int slot; |
| 4807 | int i; |
| 4808 | u32 total_size = 0; |
| 4809 | u32 total_data = 0; |
| 4810 | |
| 4811 | for (i = 0; i < nr; i++) |
| 4812 | total_data += data_size[i]; |
| 4813 | |
| 4814 | total_size = total_data + (nr * sizeof(struct btrfs_item)); |
| 4815 | ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1); |
| 4816 | if (ret == 0) |
| 4817 | return -EEXIST; |
| 4818 | if (ret < 0) |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4819 | return ret; |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4820 | |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4821 | slot = path->slots[0]; |
| 4822 | BUG_ON(slot < 0); |
| 4823 | |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4824 | setup_items_for_insert(root, path, cpu_key, data_size, |
Chris Mason | 44871b1 | 2009-03-13 10:04:31 -0400 | [diff] [blame] | 4825 | total_data, total_size, nr); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4826 | return 0; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4827 | } |
| 4828 | |
| 4829 | /* |
| 4830 | * Given a key and some data, insert an item into the tree. |
| 4831 | * This does all the path init required, making room in the tree if needed. |
| 4832 | */ |
Omar Sandoval | 310712b | 2017-01-17 23:24:37 -0800 | [diff] [blame] | 4833 | int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4834 | const struct btrfs_key *cpu_key, void *data, |
| 4835 | u32 data_size) |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4836 | { |
| 4837 | int ret = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4838 | struct btrfs_path *path; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4839 | struct extent_buffer *leaf; |
| 4840 | unsigned long ptr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4841 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4842 | path = btrfs_alloc_path(); |
Tsutomu Itoh | db5b493 | 2011-03-23 08:14:16 +0000 | [diff] [blame] | 4843 | if (!path) |
| 4844 | return -ENOMEM; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4845 | ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4846 | if (!ret) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4847 | leaf = path->nodes[0]; |
| 4848 | ptr = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 4849 | write_extent_buffer(leaf, data, ptr, data_size); |
| 4850 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 4851 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 4852 | btrfs_free_path(path); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4853 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4854 | } |
| 4855 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4856 | /* |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 4857 | * delete the pointer from a given node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4858 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 4859 | * the tree should have been previously balanced so the deletion does not |
| 4860 | * empty a node. |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4861 | */ |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4862 | static void del_ptr(struct btrfs_root *root, struct btrfs_path *path, |
| 4863 | int level, int slot) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4864 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4865 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4866 | struct extent_buffer *parent = path->nodes[level]; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4867 | u32 nritems; |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4868 | int ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4869 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4870 | nritems = btrfs_header_nritems(parent); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 4871 | if (slot != nritems - 1) { |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 4872 | if (level) { |
| 4873 | ret = tree_mod_log_insert_move(parent, slot, slot + 1, |
David Sterba | a446a97 | 2018-03-05 15:26:29 +0100 | [diff] [blame] | 4874 | nritems - slot - 1); |
David Sterba | bf1d342 | 2018-03-05 15:47:39 +0100 | [diff] [blame] | 4875 | BUG_ON(ret < 0); |
| 4876 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4877 | memmove_extent_buffer(parent, |
| 4878 | btrfs_node_key_ptr_offset(slot), |
| 4879 | btrfs_node_key_ptr_offset(slot + 1), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4880 | sizeof(struct btrfs_key_ptr) * |
| 4881 | (nritems - slot - 1)); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4882 | } else if (level) { |
David Sterba | e09c2ef | 2018-03-05 15:09:03 +0100 | [diff] [blame] | 4883 | ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE, |
| 4884 | GFP_NOFS); |
Chris Mason | 57ba86c | 2012-12-18 19:35:32 -0500 | [diff] [blame] | 4885 | BUG_ON(ret < 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4886 | } |
Jan Schmidt | f3ea38d | 2012-05-26 11:45:21 +0200 | [diff] [blame] | 4887 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4888 | nritems--; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4889 | btrfs_set_header_nritems(parent, nritems); |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4890 | if (nritems == 0 && parent == root->node) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4891 | BUG_ON(btrfs_header_level(root->node) != 1); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4892 | /* just turn the root into a leaf and break */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4893 | btrfs_set_header_level(root->node, 0); |
Chris Mason | bb80395 | 2007-03-01 12:04:21 -0500 | [diff] [blame] | 4894 | } else if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4895 | struct btrfs_disk_key disk_key; |
| 4896 | |
| 4897 | btrfs_node_key(parent, &disk_key, 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4898 | fixup_low_keys(fs_info, path, &disk_key, level + 1); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4899 | } |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4900 | btrfs_mark_buffer_dirty(parent); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4901 | } |
| 4902 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4903 | /* |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4904 | * 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] | 4905 | * path->nodes[1]. |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4906 | * |
| 4907 | * This deletes the pointer in path->nodes[1] and frees the leaf |
| 4908 | * block extent. zero is returned if it all worked out, < 0 otherwise. |
| 4909 | * |
| 4910 | * The path must have already been setup for deleting the leaf, including |
| 4911 | * all the proper balancing. path->nodes[1] must be locked. |
| 4912 | */ |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4913 | static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans, |
| 4914 | struct btrfs_root *root, |
| 4915 | struct btrfs_path *path, |
| 4916 | struct extent_buffer *leaf) |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4917 | { |
Yan Zheng | 5d4f98a | 2009-06-10 10:45:14 -0400 | [diff] [blame] | 4918 | WARN_ON(btrfs_header_generation(leaf) != trans->transid); |
Tsutomu Itoh | afe5fea | 2013-04-16 05:18:22 +0000 | [diff] [blame] | 4919 | del_ptr(root, path, 1, path->slots[1]); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4920 | |
Chris Mason | 4d081c4 | 2009-02-04 09:31:28 -0500 | [diff] [blame] | 4921 | /* |
| 4922 | * btrfs_free_extent is expensive, we want to make sure we |
| 4923 | * aren't holding any locks when we call it |
| 4924 | */ |
| 4925 | btrfs_unlock_up_safe(path, 0); |
| 4926 | |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4927 | root_sub_used(root, leaf->len); |
| 4928 | |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4929 | extent_buffer_get(leaf); |
Jan Schmidt | 5581a51 | 2012-05-16 17:04:52 +0200 | [diff] [blame] | 4930 | btrfs_free_tree_block(trans, root, leaf, 0, 1); |
Josef Bacik | 3083ee2 | 2012-03-09 16:01:49 -0500 | [diff] [blame] | 4931 | free_extent_buffer_stale(leaf); |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 4932 | } |
| 4933 | /* |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4934 | * delete the item at the leaf level in path. If that empties |
| 4935 | * the leaf, remove it from the tree |
| 4936 | */ |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4937 | int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
| 4938 | struct btrfs_path *path, int slot, int nr) |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4939 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 4940 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4941 | struct extent_buffer *leaf; |
| 4942 | struct btrfs_item *item; |
Alexandru Moise | ce0eac2 | 2015-08-23 16:01:42 +0000 | [diff] [blame] | 4943 | u32 last_off; |
| 4944 | u32 dsize = 0; |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4945 | int ret = 0; |
| 4946 | int wret; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4947 | int i; |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4948 | u32 nritems; |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4949 | struct btrfs_map_token token; |
| 4950 | |
| 4951 | btrfs_init_map_token(&token); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4952 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4953 | leaf = path->nodes[0]; |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4954 | last_off = btrfs_item_offset_nr(leaf, slot + nr - 1); |
| 4955 | |
| 4956 | for (i = 0; i < nr; i++) |
| 4957 | dsize += btrfs_item_size_nr(leaf, slot + i); |
| 4958 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4959 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4960 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4961 | if (slot + nr != nritems) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 4962 | int data_end = leaf_data_end(fs_info, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4963 | |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4964 | memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET + |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4965 | data_end + dsize, |
Nikolay Borisov | 3d9ec8c | 2017-05-29 09:43:43 +0300 | [diff] [blame] | 4966 | BTRFS_LEAF_DATA_OFFSET + data_end, |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4967 | last_off - data_end); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4968 | |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4969 | for (i = slot + nr; i < nritems; i++) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4970 | u32 ioff; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4971 | |
Ross Kirk | dd3cc16 | 2013-09-16 15:58:09 +0100 | [diff] [blame] | 4972 | item = btrfs_item_nr(i); |
Chris Mason | cfed81a | 2012-03-03 07:40:03 -0500 | [diff] [blame] | 4973 | ioff = btrfs_token_item_offset(leaf, item, &token); |
| 4974 | btrfs_set_token_item_offset(leaf, item, |
| 4975 | ioff + dsize, &token); |
Chris Mason | 0783fcf | 2007-03-12 20:12:07 -0400 | [diff] [blame] | 4976 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 4977 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4978 | memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot), |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4979 | btrfs_item_nr_offset(slot + nr), |
Chris Mason | d602557 | 2007-03-30 14:27:56 -0400 | [diff] [blame] | 4980 | sizeof(struct btrfs_item) * |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4981 | (nritems - slot - nr)); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4982 | } |
Chris Mason | 85e21ba | 2008-01-29 15:11:36 -0500 | [diff] [blame] | 4983 | btrfs_set_header_nritems(leaf, nritems - nr); |
| 4984 | nritems -= nr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4985 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 4986 | /* delete the leaf if we've emptied it */ |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4987 | if (nritems == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4988 | if (leaf == root->node) { |
| 4989 | btrfs_set_header_level(leaf, 0); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4990 | } else { |
Yan, Zheng | f0486c6 | 2010-05-16 10:46:25 -0400 | [diff] [blame] | 4991 | btrfs_set_path_blocking(path); |
David Sterba | 7c302b4 | 2017-02-10 18:47:57 +0100 | [diff] [blame] | 4992 | clean_tree_block(fs_info, leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 4993 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 9a8dd15 | 2007-02-23 08:38:36 -0500 | [diff] [blame] | 4994 | } |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 4995 | } else { |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 4996 | int used = leaf_space_used(leaf, 0, nritems); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 4997 | if (slot == 0) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 4998 | struct btrfs_disk_key disk_key; |
| 4999 | |
| 5000 | btrfs_item_key(leaf, &disk_key, 0); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5001 | fixup_low_keys(fs_info, path, &disk_key, 1); |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5002 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5003 | |
Chris Mason | 74123bd | 2007-02-02 11:05:29 -0500 | [diff] [blame] | 5004 | /* delete the leaf if it is mostly empty */ |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5005 | if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) { |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5006 | /* push_leaf_left fixes the path. |
| 5007 | * make sure the path still points to our leaf |
| 5008 | * for possible call to del_ptr below |
| 5009 | */ |
Chris Mason | 4920c9a | 2007-01-26 16:38:42 -0500 | [diff] [blame] | 5010 | slot = path->slots[1]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5011 | extent_buffer_get(leaf); |
| 5012 | |
Chris Mason | b947343 | 2009-03-13 11:00:37 -0400 | [diff] [blame] | 5013 | btrfs_set_path_blocking(path); |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 5014 | wret = push_leaf_left(trans, root, path, 1, 1, |
| 5015 | 1, (u32)-1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5016 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5017 | ret = wret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5018 | |
| 5019 | if (path->nodes[0] == leaf && |
| 5020 | btrfs_header_nritems(leaf)) { |
Chris Mason | 99d8f83 | 2010-07-07 10:51:48 -0400 | [diff] [blame] | 5021 | wret = push_leaf_right(trans, root, path, 1, |
| 5022 | 1, 1, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 5023 | if (wret < 0 && wret != -ENOSPC) |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5024 | ret = wret; |
| 5025 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5026 | |
| 5027 | if (btrfs_header_nritems(leaf) == 0) { |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 5028 | path->slots[1] = slot; |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5029 | btrfs_del_leaf(trans, root, path, leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5030 | free_extent_buffer(leaf); |
Jeff Mahoney | 143bede | 2012-03-01 14:56:26 +0100 | [diff] [blame] | 5031 | ret = 0; |
Chris Mason | 5de08d7 | 2007-02-24 06:24:44 -0500 | [diff] [blame] | 5032 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5033 | /* if we're still in the path, make sure |
| 5034 | * we're dirty. Otherwise, one of the |
| 5035 | * push_leaf functions must have already |
| 5036 | * dirtied this buffer |
| 5037 | */ |
| 5038 | if (path->nodes[0] == leaf) |
| 5039 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5040 | free_extent_buffer(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5041 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 5042 | } else { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5043 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5044 | } |
| 5045 | } |
Chris Mason | aa5d6be | 2007-02-28 16:35:06 -0500 | [diff] [blame] | 5046 | return ret; |
Chris Mason | be0e5c0 | 2007-01-26 15:51:26 -0500 | [diff] [blame] | 5047 | } |
| 5048 | |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5049 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5050 | * search the tree again to find a leaf with lesser keys |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5051 | * returns 0 if it found something or 1 if there are no lesser leaves. |
| 5052 | * returns < 0 on io errors. |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5053 | * |
| 5054 | * This may release the path, and so you may lose any locks held at the |
| 5055 | * time you call it. |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5056 | */ |
Josef Bacik | 16e7549 | 2013-10-22 12:18:51 -0400 | [diff] [blame] | 5057 | int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5058 | { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5059 | struct btrfs_key key; |
| 5060 | struct btrfs_disk_key found_key; |
| 5061 | int ret; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5062 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5063 | btrfs_item_key_to_cpu(path->nodes[0], &key, 0); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5064 | |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5065 | if (key.offset > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5066 | key.offset--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5067 | } else if (key.type > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5068 | key.type--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5069 | key.offset = (u64)-1; |
| 5070 | } else if (key.objectid > 0) { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5071 | key.objectid--; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5072 | key.type = (u8)-1; |
| 5073 | key.offset = (u64)-1; |
| 5074 | } else { |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5075 | return 1; |
Filipe David Borba Manana | e8b0d724 | 2013-10-15 00:12:27 +0100 | [diff] [blame] | 5076 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5077 | |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5078 | btrfs_release_path(path); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5079 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 5080 | if (ret < 0) |
| 5081 | return ret; |
| 5082 | btrfs_item_key(path->nodes[0], &found_key, 0); |
| 5083 | ret = comp_keys(&found_key, &key); |
Filipe Manana | 337c6f6 | 2014-06-09 13:22:13 +0100 | [diff] [blame] | 5084 | /* |
| 5085 | * We might have had an item with the previous key in the tree right |
| 5086 | * before we released our path. And after we released our path, that |
| 5087 | * item might have been pushed to the first slot (0) of the leaf we |
| 5088 | * were holding due to a tree balance. Alternatively, an item with the |
| 5089 | * previous key can exist as the only element of a leaf (big fat item). |
| 5090 | * Therefore account for these 2 cases, so that our callers (like |
| 5091 | * btrfs_previous_item) don't miss an existing item with a key matching |
| 5092 | * the previous key we computed above. |
| 5093 | */ |
| 5094 | if (ret <= 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5095 | return 0; |
| 5096 | return 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5097 | } |
| 5098 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5099 | /* |
| 5100 | * 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] | 5101 | * for nodes or leaves that are have a minimum transaction id. |
| 5102 | * This is used by the btree defrag code, and tree logging |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5103 | * |
| 5104 | * This does not cow, but it does stuff the starting key it finds back |
| 5105 | * into min_key, so you can call btrfs_search_slot with cow=1 on the |
| 5106 | * key and get a writable path. |
| 5107 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5108 | * This honors path->lowest_level to prevent descent past a given level |
| 5109 | * of the tree. |
| 5110 | * |
Chris Mason | d352ac6 | 2008-09-29 15:18:18 -0400 | [diff] [blame] | 5111 | * min_trans indicates the oldest transaction that you are interested |
| 5112 | * in walking through. Any nodes or leaves older than min_trans are |
| 5113 | * skipped over (without reading them). |
| 5114 | * |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5115 | * returns zero if something useful was found, < 0 on error and 1 if there |
| 5116 | * was nothing in the tree that matched the search criteria. |
| 5117 | */ |
| 5118 | 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] | 5119 | struct btrfs_path *path, |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5120 | u64 min_trans) |
| 5121 | { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5122 | struct btrfs_fs_info *fs_info = root->fs_info; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5123 | struct extent_buffer *cur; |
| 5124 | struct btrfs_key found_key; |
| 5125 | int slot; |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 5126 | int sret; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5127 | u32 nritems; |
| 5128 | int level; |
| 5129 | int ret = 1; |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5130 | int keep_locks = path->keep_locks; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5131 | |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5132 | path->keep_locks = 1; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5133 | again: |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5134 | cur = btrfs_read_lock_root_node(root); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5135 | level = btrfs_header_level(cur); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5136 | WARN_ON(path->nodes[level]); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5137 | path->nodes[level] = cur; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5138 | path->locks[level] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5139 | |
| 5140 | if (btrfs_header_generation(cur) < min_trans) { |
| 5141 | ret = 1; |
| 5142 | goto out; |
| 5143 | } |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5144 | while (1) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5145 | nritems = btrfs_header_nritems(cur); |
| 5146 | level = btrfs_header_level(cur); |
Nikolay Borisov | a74b35e | 2017-12-08 16:27:43 +0200 | [diff] [blame] | 5147 | sret = btrfs_bin_search(cur, min_key, level, &slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5148 | |
Chris Mason | 323ac95 | 2008-10-01 19:05:46 -0400 | [diff] [blame] | 5149 | /* at the lowest level, we're done, setup the path and exit */ |
| 5150 | if (level == path->lowest_level) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5151 | if (slot >= nritems) |
| 5152 | goto find_next_key; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5153 | ret = 0; |
| 5154 | path->slots[level] = slot; |
| 5155 | btrfs_item_key_to_cpu(cur, &found_key, slot); |
| 5156 | goto out; |
| 5157 | } |
Yan | 9652480 | 2008-07-24 12:19:49 -0400 | [diff] [blame] | 5158 | if (sret && slot > 0) |
| 5159 | slot--; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5160 | /* |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5161 | * check this node pointer against the min_trans parameters. |
| 5162 | * If it is too old, old, skip to the next one. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5163 | */ |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5164 | while (slot < nritems) { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5165 | u64 gen; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5166 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5167 | gen = btrfs_node_ptr_generation(cur, slot); |
| 5168 | if (gen < min_trans) { |
| 5169 | slot++; |
| 5170 | continue; |
| 5171 | } |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5172 | break; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5173 | } |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5174 | find_next_key: |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5175 | /* |
| 5176 | * we didn't find a candidate key in this node, walk forward |
| 5177 | * and find another one |
| 5178 | */ |
| 5179 | if (slot >= nritems) { |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5180 | path->slots[level] = slot; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5181 | btrfs_set_path_blocking(path); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5182 | sret = btrfs_find_next_key(root, path, min_key, level, |
Eric Sandeen | de78b51 | 2013-01-31 18:21:12 +0000 | [diff] [blame] | 5183 | min_trans); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5184 | if (sret == 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5185 | btrfs_release_path(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5186 | goto again; |
| 5187 | } else { |
| 5188 | goto out; |
| 5189 | } |
| 5190 | } |
| 5191 | /* save our key for returning back */ |
| 5192 | btrfs_node_key_to_cpu(cur, &found_key, slot); |
| 5193 | path->slots[level] = slot; |
| 5194 | if (level == path->lowest_level) { |
| 5195 | ret = 0; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5196 | goto out; |
| 5197 | } |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5198 | btrfs_set_path_blocking(path); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5199 | cur = read_node_slot(fs_info, cur, slot); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5200 | if (IS_ERR(cur)) { |
| 5201 | ret = PTR_ERR(cur); |
| 5202 | goto out; |
| 5203 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5204 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5205 | btrfs_tree_read_lock(cur); |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5206 | |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5207 | path->locks[level - 1] = BTRFS_READ_LOCK; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5208 | path->nodes[level - 1] = cur; |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5209 | unlock_up(path, level, 1, 0, NULL); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5210 | btrfs_clear_path_blocking(path, NULL, 0); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5211 | } |
| 5212 | out: |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5213 | path->keep_locks = keep_locks; |
| 5214 | if (ret == 0) { |
| 5215 | btrfs_unlock_up_safe(path, path->lowest_level + 1); |
| 5216 | btrfs_set_path_blocking(path); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5217 | memcpy(min_key, &found_key, sizeof(found_key)); |
Filipe Manana | f98de9b | 2014-08-04 19:37:21 +0100 | [diff] [blame] | 5218 | } |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5219 | return ret; |
| 5220 | } |
| 5221 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5222 | static int tree_move_down(struct btrfs_fs_info *fs_info, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5223 | struct btrfs_path *path, |
David Sterba | ab6a43e | 2017-02-10 19:24:53 +0100 | [diff] [blame] | 5224 | int *level) |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5225 | { |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5226 | struct extent_buffer *eb; |
| 5227 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5228 | BUG_ON(*level == 0); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5229 | eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5230 | if (IS_ERR(eb)) |
| 5231 | return PTR_ERR(eb); |
| 5232 | |
| 5233 | path->nodes[*level - 1] = eb; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5234 | path->slots[*level - 1] = 0; |
| 5235 | (*level)--; |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5236 | return 0; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5237 | } |
| 5238 | |
David Sterba | f1e3026 | 2017-02-10 19:25:51 +0100 | [diff] [blame] | 5239 | static int tree_move_next_or_upnext(struct btrfs_path *path, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5240 | int *level, int root_level) |
| 5241 | { |
| 5242 | int ret = 0; |
| 5243 | int nritems; |
| 5244 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 5245 | |
| 5246 | path->slots[*level]++; |
| 5247 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5248 | while (path->slots[*level] >= nritems) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5249 | if (*level == root_level) |
| 5250 | return -1; |
| 5251 | |
| 5252 | /* move upnext */ |
| 5253 | path->slots[*level] = 0; |
| 5254 | free_extent_buffer(path->nodes[*level]); |
| 5255 | path->nodes[*level] = NULL; |
| 5256 | (*level)++; |
| 5257 | path->slots[*level]++; |
| 5258 | |
| 5259 | nritems = btrfs_header_nritems(path->nodes[*level]); |
| 5260 | ret = 1; |
| 5261 | } |
| 5262 | return ret; |
| 5263 | } |
| 5264 | |
| 5265 | /* |
| 5266 | * Returns 1 if it had to move up and next. 0 is returned if it moved only next |
| 5267 | * or down. |
| 5268 | */ |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5269 | static int tree_advance(struct btrfs_fs_info *fs_info, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5270 | struct btrfs_path *path, |
| 5271 | int *level, int root_level, |
| 5272 | int allow_down, |
| 5273 | struct btrfs_key *key) |
| 5274 | { |
| 5275 | int ret; |
| 5276 | |
| 5277 | if (*level == 0 || !allow_down) { |
David Sterba | f1e3026 | 2017-02-10 19:25:51 +0100 | [diff] [blame] | 5278 | ret = tree_move_next_or_upnext(path, level, root_level); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5279 | } else { |
David Sterba | ab6a43e | 2017-02-10 19:24:53 +0100 | [diff] [blame] | 5280 | ret = tree_move_down(fs_info, path, level); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5281 | } |
| 5282 | if (ret >= 0) { |
| 5283 | if (*level == 0) |
| 5284 | btrfs_item_key_to_cpu(path->nodes[*level], key, |
| 5285 | path->slots[*level]); |
| 5286 | else |
| 5287 | btrfs_node_key_to_cpu(path->nodes[*level], key, |
| 5288 | path->slots[*level]); |
| 5289 | } |
| 5290 | return ret; |
| 5291 | } |
| 5292 | |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5293 | static int tree_compare_item(struct btrfs_path *left_path, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5294 | struct btrfs_path *right_path, |
| 5295 | char *tmp_buf) |
| 5296 | { |
| 5297 | int cmp; |
| 5298 | int len1, len2; |
| 5299 | unsigned long off1, off2; |
| 5300 | |
| 5301 | len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]); |
| 5302 | len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]); |
| 5303 | if (len1 != len2) |
| 5304 | return 1; |
| 5305 | |
| 5306 | off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]); |
| 5307 | off2 = btrfs_item_ptr_offset(right_path->nodes[0], |
| 5308 | right_path->slots[0]); |
| 5309 | |
| 5310 | read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1); |
| 5311 | |
| 5312 | cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1); |
| 5313 | if (cmp) |
| 5314 | return 1; |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | #define ADVANCE 1 |
| 5319 | #define ADVANCE_ONLY_NEXT -1 |
| 5320 | |
| 5321 | /* |
| 5322 | * This function compares two trees and calls the provided callback for |
| 5323 | * every changed/new/deleted item it finds. |
| 5324 | * If shared tree blocks are encountered, whole subtrees are skipped, making |
| 5325 | * the compare pretty fast on snapshotted subvolumes. |
| 5326 | * |
| 5327 | * This currently works on commit roots only. As commit roots are read only, |
| 5328 | * we don't do any locking. The commit roots are protected with transactions. |
| 5329 | * Transactions are ended and rejoined when a commit is tried in between. |
| 5330 | * |
| 5331 | * This function checks for modifications done to the trees while comparing. |
| 5332 | * If it detects a change, it aborts immediately. |
| 5333 | */ |
| 5334 | int btrfs_compare_trees(struct btrfs_root *left_root, |
| 5335 | struct btrfs_root *right_root, |
| 5336 | btrfs_changed_cb_t changed_cb, void *ctx) |
| 5337 | { |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5338 | struct btrfs_fs_info *fs_info = left_root->fs_info; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5339 | int ret; |
| 5340 | int cmp; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5341 | struct btrfs_path *left_path = NULL; |
| 5342 | struct btrfs_path *right_path = NULL; |
| 5343 | struct btrfs_key left_key; |
| 5344 | struct btrfs_key right_key; |
| 5345 | char *tmp_buf = NULL; |
| 5346 | int left_root_level; |
| 5347 | int right_root_level; |
| 5348 | int left_level; |
| 5349 | int right_level; |
| 5350 | int left_end_reached; |
| 5351 | int right_end_reached; |
| 5352 | int advance_left; |
| 5353 | int advance_right; |
| 5354 | u64 left_blockptr; |
| 5355 | u64 right_blockptr; |
Filipe Manana | 6baa429 | 2014-02-20 21:15:25 +0000 | [diff] [blame] | 5356 | u64 left_gen; |
| 5357 | u64 right_gen; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5358 | |
| 5359 | left_path = btrfs_alloc_path(); |
| 5360 | if (!left_path) { |
| 5361 | ret = -ENOMEM; |
| 5362 | goto out; |
| 5363 | } |
| 5364 | right_path = btrfs_alloc_path(); |
| 5365 | if (!right_path) { |
| 5366 | ret = -ENOMEM; |
| 5367 | goto out; |
| 5368 | } |
| 5369 | |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 5370 | tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5371 | if (!tmp_buf) { |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 5372 | ret = -ENOMEM; |
| 5373 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5374 | } |
| 5375 | |
| 5376 | left_path->search_commit_root = 1; |
| 5377 | left_path->skip_locking = 1; |
| 5378 | right_path->search_commit_root = 1; |
| 5379 | right_path->skip_locking = 1; |
| 5380 | |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5381 | /* |
| 5382 | * Strategy: Go to the first items of both trees. Then do |
| 5383 | * |
| 5384 | * If both trees are at level 0 |
| 5385 | * Compare keys of current items |
| 5386 | * If left < right treat left item as new, advance left tree |
| 5387 | * and repeat |
| 5388 | * If left > right treat right item as deleted, advance right tree |
| 5389 | * and repeat |
| 5390 | * If left == right do deep compare of items, treat as changed if |
| 5391 | * needed, advance both trees and repeat |
| 5392 | * If both trees are at the same level but not at level 0 |
| 5393 | * Compare keys of current nodes/leafs |
| 5394 | * If left < right advance left tree and repeat |
| 5395 | * If left > right advance right tree and repeat |
| 5396 | * If left == right compare blockptrs of the next nodes/leafs |
| 5397 | * If they match advance both trees but stay at the same level |
| 5398 | * and repeat |
| 5399 | * If they don't match advance both trees while allowing to go |
| 5400 | * deeper and repeat |
| 5401 | * If tree levels are different |
| 5402 | * Advance the tree that needs it and repeat |
| 5403 | * |
| 5404 | * Advancing a tree means: |
| 5405 | * If we are at level 0, try to go to the next slot. If that's not |
| 5406 | * possible, go one level up and repeat. Stop when we found a level |
| 5407 | * where we could go to the next slot. We may at this point be on a |
| 5408 | * node or a leaf. |
| 5409 | * |
| 5410 | * If we are not at level 0 and not on shared tree blocks, go one |
| 5411 | * level deeper. |
| 5412 | * |
| 5413 | * If we are not at level 0 and on shared tree blocks, go one slot to |
| 5414 | * the right if possible or go up and right. |
| 5415 | */ |
| 5416 | |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5417 | down_read(&fs_info->commit_root_sem); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5418 | left_level = btrfs_header_level(left_root->commit_root); |
| 5419 | left_root_level = left_level; |
| 5420 | left_path->nodes[left_level] = left_root->commit_root; |
| 5421 | extent_buffer_get(left_path->nodes[left_level]); |
| 5422 | |
| 5423 | right_level = btrfs_header_level(right_root->commit_root); |
| 5424 | right_root_level = right_level; |
| 5425 | right_path->nodes[right_level] = right_root->commit_root; |
| 5426 | extent_buffer_get(right_path->nodes[right_level]); |
Jeff Mahoney | 0b246af | 2016-06-22 18:54:23 -0400 | [diff] [blame] | 5427 | up_read(&fs_info->commit_root_sem); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5428 | |
| 5429 | if (left_level == 0) |
| 5430 | btrfs_item_key_to_cpu(left_path->nodes[left_level], |
| 5431 | &left_key, left_path->slots[left_level]); |
| 5432 | else |
| 5433 | btrfs_node_key_to_cpu(left_path->nodes[left_level], |
| 5434 | &left_key, left_path->slots[left_level]); |
| 5435 | if (right_level == 0) |
| 5436 | btrfs_item_key_to_cpu(right_path->nodes[right_level], |
| 5437 | &right_key, right_path->slots[right_level]); |
| 5438 | else |
| 5439 | btrfs_node_key_to_cpu(right_path->nodes[right_level], |
| 5440 | &right_key, right_path->slots[right_level]); |
| 5441 | |
| 5442 | left_end_reached = right_end_reached = 0; |
| 5443 | advance_left = advance_right = 0; |
| 5444 | |
| 5445 | while (1) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5446 | if (advance_left && !left_end_reached) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5447 | ret = tree_advance(fs_info, left_path, &left_level, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5448 | left_root_level, |
| 5449 | advance_left != ADVANCE_ONLY_NEXT, |
| 5450 | &left_key); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5451 | if (ret == -1) |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5452 | left_end_reached = ADVANCE; |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5453 | else if (ret < 0) |
| 5454 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5455 | advance_left = 0; |
| 5456 | } |
| 5457 | if (advance_right && !right_end_reached) { |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5458 | ret = tree_advance(fs_info, right_path, &right_level, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5459 | right_root_level, |
| 5460 | advance_right != ADVANCE_ONLY_NEXT, |
| 5461 | &right_key); |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5462 | if (ret == -1) |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5463 | right_end_reached = ADVANCE; |
Liu Bo | fb770ae | 2016-07-05 12:10:14 -0700 | [diff] [blame] | 5464 | else if (ret < 0) |
| 5465 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5466 | advance_right = 0; |
| 5467 | } |
| 5468 | |
| 5469 | if (left_end_reached && right_end_reached) { |
| 5470 | ret = 0; |
| 5471 | goto out; |
| 5472 | } else if (left_end_reached) { |
| 5473 | if (right_level == 0) { |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 5474 | ret = changed_cb(left_path, right_path, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5475 | &right_key, |
| 5476 | BTRFS_COMPARE_TREE_DELETED, |
| 5477 | ctx); |
| 5478 | if (ret < 0) |
| 5479 | goto out; |
| 5480 | } |
| 5481 | advance_right = ADVANCE; |
| 5482 | continue; |
| 5483 | } else if (right_end_reached) { |
| 5484 | if (left_level == 0) { |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 5485 | ret = changed_cb(left_path, right_path, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5486 | &left_key, |
| 5487 | BTRFS_COMPARE_TREE_NEW, |
| 5488 | ctx); |
| 5489 | if (ret < 0) |
| 5490 | goto out; |
| 5491 | } |
| 5492 | advance_left = ADVANCE; |
| 5493 | continue; |
| 5494 | } |
| 5495 | |
| 5496 | if (left_level == 0 && right_level == 0) { |
| 5497 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5498 | if (cmp < 0) { |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 5499 | ret = changed_cb(left_path, right_path, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5500 | &left_key, |
| 5501 | BTRFS_COMPARE_TREE_NEW, |
| 5502 | ctx); |
| 5503 | if (ret < 0) |
| 5504 | goto out; |
| 5505 | advance_left = ADVANCE; |
| 5506 | } else if (cmp > 0) { |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 5507 | ret = changed_cb(left_path, right_path, |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5508 | &right_key, |
| 5509 | BTRFS_COMPARE_TREE_DELETED, |
| 5510 | ctx); |
| 5511 | if (ret < 0) |
| 5512 | goto out; |
| 5513 | advance_right = ADVANCE; |
| 5514 | } else { |
Fabian Frederick | b99d9a6 | 2014-09-25 19:35:02 +0200 | [diff] [blame] | 5515 | enum btrfs_compare_tree_result result; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5516 | |
Chris Mason | 74dd17f | 2012-08-07 16:25:13 -0400 | [diff] [blame] | 5517 | WARN_ON(!extent_buffer_uptodate(left_path->nodes[0])); |
Jeff Mahoney | 2ff7e61 | 2016-06-22 18:54:24 -0400 | [diff] [blame] | 5518 | ret = tree_compare_item(left_path, right_path, |
| 5519 | tmp_buf); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5520 | if (ret) |
Fabian Frederick | b99d9a6 | 2014-09-25 19:35:02 +0200 | [diff] [blame] | 5521 | result = BTRFS_COMPARE_TREE_CHANGED; |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5522 | else |
Fabian Frederick | b99d9a6 | 2014-09-25 19:35:02 +0200 | [diff] [blame] | 5523 | result = BTRFS_COMPARE_TREE_SAME; |
Nikolay Borisov | ee8c494 | 2017-08-21 12:43:45 +0300 | [diff] [blame] | 5524 | ret = changed_cb(left_path, right_path, |
Fabian Frederick | b99d9a6 | 2014-09-25 19:35:02 +0200 | [diff] [blame] | 5525 | &left_key, result, ctx); |
Josef Bacik | ba5e8f2 | 2013-08-16 16:52:55 -0400 | [diff] [blame] | 5526 | if (ret < 0) |
| 5527 | goto out; |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5528 | advance_left = ADVANCE; |
| 5529 | advance_right = ADVANCE; |
| 5530 | } |
| 5531 | } else if (left_level == right_level) { |
| 5532 | cmp = btrfs_comp_cpu_keys(&left_key, &right_key); |
| 5533 | if (cmp < 0) { |
| 5534 | advance_left = ADVANCE; |
| 5535 | } else if (cmp > 0) { |
| 5536 | advance_right = ADVANCE; |
| 5537 | } else { |
| 5538 | left_blockptr = btrfs_node_blockptr( |
| 5539 | left_path->nodes[left_level], |
| 5540 | left_path->slots[left_level]); |
| 5541 | right_blockptr = btrfs_node_blockptr( |
| 5542 | right_path->nodes[right_level], |
| 5543 | right_path->slots[right_level]); |
Filipe Manana | 6baa429 | 2014-02-20 21:15:25 +0000 | [diff] [blame] | 5544 | left_gen = btrfs_node_ptr_generation( |
| 5545 | left_path->nodes[left_level], |
| 5546 | left_path->slots[left_level]); |
| 5547 | right_gen = btrfs_node_ptr_generation( |
| 5548 | right_path->nodes[right_level], |
| 5549 | right_path->slots[right_level]); |
| 5550 | if (left_blockptr == right_blockptr && |
| 5551 | left_gen == right_gen) { |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5552 | /* |
| 5553 | * As we're on a shared block, don't |
| 5554 | * allow to go deeper. |
| 5555 | */ |
| 5556 | advance_left = ADVANCE_ONLY_NEXT; |
| 5557 | advance_right = ADVANCE_ONLY_NEXT; |
| 5558 | } else { |
| 5559 | advance_left = ADVANCE; |
| 5560 | advance_right = ADVANCE; |
| 5561 | } |
| 5562 | } |
| 5563 | } else if (left_level < right_level) { |
| 5564 | advance_right = ADVANCE; |
| 5565 | } else { |
| 5566 | advance_left = ADVANCE; |
| 5567 | } |
| 5568 | } |
| 5569 | |
| 5570 | out: |
| 5571 | btrfs_free_path(left_path); |
| 5572 | btrfs_free_path(right_path); |
David Sterba | 8f282f7 | 2016-03-30 16:01:12 +0200 | [diff] [blame] | 5573 | kvfree(tmp_buf); |
Alexander Block | 7069830 | 2012-06-05 21:07:48 +0200 | [diff] [blame] | 5574 | return ret; |
| 5575 | } |
| 5576 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5577 | /* |
| 5578 | * this is similar to btrfs_next_leaf, but does not try to preserve |
| 5579 | * 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] | 5580 | * tree based on the current path and the min_trans parameters. |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5581 | * |
| 5582 | * 0 is returned if another key is found, < 0 if there are any errors |
| 5583 | * and 1 is returned if there are no higher keys in the tree |
| 5584 | * |
| 5585 | * path->keep_locks should be set to 1 on the search made before |
| 5586 | * calling this function. |
| 5587 | */ |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5588 | 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] | 5589 | struct btrfs_key *key, int level, u64 min_trans) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5590 | { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5591 | int slot; |
| 5592 | struct extent_buffer *c; |
| 5593 | |
Chris Mason | 934d375 | 2008-12-08 16:43:10 -0500 | [diff] [blame] | 5594 | WARN_ON(!path->keep_locks); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5595 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5596 | if (!path->nodes[level]) |
| 5597 | return 1; |
| 5598 | |
| 5599 | slot = path->slots[level] + 1; |
| 5600 | c = path->nodes[level]; |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5601 | next: |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5602 | if (slot >= btrfs_header_nritems(c)) { |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5603 | int ret; |
| 5604 | int orig_lowest; |
| 5605 | struct btrfs_key cur_key; |
| 5606 | if (level + 1 >= BTRFS_MAX_LEVEL || |
| 5607 | !path->nodes[level + 1]) |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5608 | return 1; |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5609 | |
| 5610 | if (path->locks[level + 1]) { |
| 5611 | level++; |
| 5612 | continue; |
| 5613 | } |
| 5614 | |
| 5615 | slot = btrfs_header_nritems(c) - 1; |
| 5616 | if (level == 0) |
| 5617 | btrfs_item_key_to_cpu(c, &cur_key, slot); |
| 5618 | else |
| 5619 | btrfs_node_key_to_cpu(c, &cur_key, slot); |
| 5620 | |
| 5621 | orig_lowest = path->lowest_level; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5622 | btrfs_release_path(path); |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5623 | path->lowest_level = level; |
| 5624 | ret = btrfs_search_slot(NULL, root, &cur_key, path, |
| 5625 | 0, 0); |
| 5626 | path->lowest_level = orig_lowest; |
| 5627 | if (ret < 0) |
| 5628 | return ret; |
| 5629 | |
| 5630 | c = path->nodes[level]; |
| 5631 | slot = path->slots[level]; |
| 5632 | if (ret == 0) |
| 5633 | slot++; |
| 5634 | goto next; |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5635 | } |
Yan Zheng | 33c66f4 | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5636 | |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5637 | if (level == 0) |
| 5638 | btrfs_item_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5639 | else { |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5640 | u64 gen = btrfs_node_ptr_generation(c, slot); |
| 5641 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5642 | if (gen < min_trans) { |
| 5643 | slot++; |
| 5644 | goto next; |
| 5645 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5646 | btrfs_node_key_to_cpu(c, key, slot); |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5647 | } |
Chris Mason | e7a8456 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5648 | return 0; |
| 5649 | } |
| 5650 | return 1; |
| 5651 | } |
| 5652 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 5653 | /* |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5654 | * search the tree again to find a leaf with greater keys |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 5655 | * returns 0 if it found something or 1 if there are no greater leaves. |
| 5656 | * returns < 0 on io errors. |
Chris Mason | 97571fd | 2007-02-24 13:39:08 -0500 | [diff] [blame] | 5657 | */ |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 5658 | int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path) |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5659 | { |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5660 | return btrfs_next_old_leaf(root, path, 0); |
| 5661 | } |
| 5662 | |
| 5663 | int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path, |
| 5664 | u64 time_seq) |
| 5665 | { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5666 | int slot; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5667 | int level; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5668 | struct extent_buffer *c; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5669 | struct extent_buffer *next; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5670 | struct btrfs_key key; |
| 5671 | u32 nritems; |
| 5672 | int ret; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5673 | int old_spinning = path->leave_spinning; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5674 | int next_rw_lock = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5675 | |
| 5676 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5677 | if (nritems == 0) |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5678 | return 1; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5679 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5680 | btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); |
| 5681 | again: |
| 5682 | level = 1; |
| 5683 | next = NULL; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5684 | next_rw_lock = 0; |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5685 | btrfs_release_path(path); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5686 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5687 | path->keep_locks = 1; |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5688 | path->leave_spinning = 1; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5689 | |
Jan Schmidt | 3d7806e | 2012-06-11 08:29:29 +0200 | [diff] [blame] | 5690 | if (time_seq) |
| 5691 | ret = btrfs_search_old_slot(root, &key, path, time_seq); |
| 5692 | else |
| 5693 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5694 | path->keep_locks = 0; |
| 5695 | |
| 5696 | if (ret < 0) |
| 5697 | return ret; |
| 5698 | |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5699 | nritems = btrfs_header_nritems(path->nodes[0]); |
Chris Mason | 168fd7d | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5700 | /* |
| 5701 | * by releasing the path above we dropped all our locks. A balance |
| 5702 | * could have added more items next to the key that used to be |
| 5703 | * at the very end of the block. So, check again here and |
| 5704 | * advance the path if there are now more items available. |
| 5705 | */ |
Chris Mason | a213501 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5706 | if (nritems > 0 && path->slots[0] < nritems - 1) { |
Yan Zheng | e457afe | 2009-07-22 09:59:00 -0400 | [diff] [blame] | 5707 | if (ret == 0) |
| 5708 | path->slots[0]++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5709 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5710 | goto done; |
| 5711 | } |
Liu Bo | 0b43e04 | 2014-06-09 11:04:49 +0800 | [diff] [blame] | 5712 | /* |
| 5713 | * So the above check misses one case: |
| 5714 | * - after releasing the path above, someone has removed the item that |
| 5715 | * used to be at the very end of the block, and balance between leafs |
| 5716 | * gets another one with bigger key.offset to replace it. |
| 5717 | * |
| 5718 | * This one should be returned as well, or we can get leaf corruption |
| 5719 | * later(esp. in __btrfs_drop_extents()). |
| 5720 | * |
| 5721 | * And a bit more explanation about this check, |
| 5722 | * with ret > 0, the key isn't found, the path points to the slot |
| 5723 | * where it should be inserted, so the path->slots[0] item must be the |
| 5724 | * bigger one. |
| 5725 | */ |
| 5726 | if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) { |
| 5727 | ret = 0; |
| 5728 | goto done; |
| 5729 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5730 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5731 | while (level < BTRFS_MAX_LEVEL) { |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5732 | if (!path->nodes[level]) { |
| 5733 | ret = 1; |
| 5734 | goto done; |
| 5735 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5736 | |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5737 | slot = path->slots[level] + 1; |
| 5738 | c = path->nodes[level]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5739 | if (slot >= btrfs_header_nritems(c)) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5740 | level++; |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5741 | if (level == BTRFS_MAX_LEVEL) { |
| 5742 | ret = 1; |
| 5743 | goto done; |
| 5744 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5745 | continue; |
| 5746 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5747 | |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5748 | if (next) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5749 | btrfs_tree_unlock_rw(next, next_rw_lock); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5750 | free_extent_buffer(next); |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5751 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5752 | |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5753 | next = c; |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5754 | next_rw_lock = path->locks[level]; |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 5755 | ret = read_block_for_search(root, path, &next, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 5756 | slot, &key); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5757 | if (ret == -EAGAIN) |
| 5758 | goto again; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5759 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5760 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5761 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5762 | goto done; |
| 5763 | } |
| 5764 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5765 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5766 | ret = btrfs_try_tree_read_lock(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5767 | if (!ret && time_seq) { |
| 5768 | /* |
| 5769 | * If we don't get the lock, we may be racing |
| 5770 | * with push_leaf_left, holding that lock while |
| 5771 | * itself waiting for the leaf we've currently |
| 5772 | * locked. To solve this situation, we give up |
| 5773 | * on our lock and cycle. |
| 5774 | */ |
Jan Schmidt | cf53883 | 2012-07-04 15:42:48 +0200 | [diff] [blame] | 5775 | free_extent_buffer(next); |
Jan Schmidt | d42244a | 2012-06-22 14:51:15 +0200 | [diff] [blame] | 5776 | btrfs_release_path(path); |
| 5777 | cond_resched(); |
| 5778 | goto again; |
| 5779 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5780 | if (!ret) { |
| 5781 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5782 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5783 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5784 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5785 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5786 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5787 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5788 | break; |
| 5789 | } |
| 5790 | path->slots[level] = slot; |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5791 | while (1) { |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5792 | level--; |
| 5793 | c = path->nodes[level]; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5794 | if (path->locks[level]) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5795 | btrfs_tree_unlock_rw(c, path->locks[level]); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5796 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 5797 | free_extent_buffer(c); |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5798 | path->nodes[level] = next; |
| 5799 | path->slots[level] = 0; |
Chris Mason | a74a4b9 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5800 | if (!path->skip_locking) |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5801 | path->locks[level] = next_rw_lock; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5802 | if (!level) |
| 5803 | break; |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5804 | |
Liu Bo | d07b852 | 2017-01-30 12:23:42 -0800 | [diff] [blame] | 5805 | ret = read_block_for_search(root, path, &next, level, |
David Sterba | cda79c5 | 2017-02-10 18:44:32 +0100 | [diff] [blame] | 5806 | 0, &key); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5807 | if (ret == -EAGAIN) |
| 5808 | goto again; |
| 5809 | |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5810 | if (ret < 0) { |
David Sterba | b3b4aa7 | 2011-04-21 01:20:15 +0200 | [diff] [blame] | 5811 | btrfs_release_path(path); |
Chris Mason | 76a05b3 | 2009-05-14 13:24:30 -0400 | [diff] [blame] | 5812 | goto done; |
| 5813 | } |
| 5814 | |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5815 | if (!path->skip_locking) { |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5816 | ret = btrfs_try_tree_read_lock(next); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5817 | if (!ret) { |
| 5818 | btrfs_set_path_blocking(path); |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5819 | btrfs_tree_read_lock(next); |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5820 | btrfs_clear_path_blocking(path, next, |
Chris Mason | bd68151 | 2011-07-16 15:23:14 -0400 | [diff] [blame] | 5821 | BTRFS_READ_LOCK); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5822 | } |
Chris Mason | 31533fb | 2011-07-26 16:01:59 -0400 | [diff] [blame] | 5823 | next_rw_lock = BTRFS_READ_LOCK; |
Chris Mason | 5cd57b2 | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5824 | } |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5825 | } |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5826 | ret = 0; |
Chris Mason | 925baed | 2008-06-25 16:01:30 -0400 | [diff] [blame] | 5827 | done: |
Chris Mason | f7c79f3 | 2012-03-19 15:54:38 -0400 | [diff] [blame] | 5828 | unlock_up(path, 0, 1, 0, NULL); |
Chris Mason | 8e73f27 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 5829 | path->leave_spinning = old_spinning; |
| 5830 | if (!old_spinning) |
| 5831 | btrfs_set_path_blocking(path); |
| 5832 | |
| 5833 | return ret; |
Chris Mason | d97e63b | 2007-02-20 16:40:44 -0500 | [diff] [blame] | 5834 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5835 | |
Chris Mason | 3f157a2 | 2008-06-25 16:01:31 -0400 | [diff] [blame] | 5836 | /* |
| 5837 | * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps |
| 5838 | * searching until it gets past min_objectid or finds an item of 'type' |
| 5839 | * |
| 5840 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5841 | */ |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5842 | int btrfs_previous_item(struct btrfs_root *root, |
| 5843 | struct btrfs_path *path, u64 min_objectid, |
| 5844 | int type) |
| 5845 | { |
| 5846 | struct btrfs_key found_key; |
| 5847 | struct extent_buffer *leaf; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5848 | u32 nritems; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5849 | int ret; |
| 5850 | |
Chris Mason | d397712 | 2009-01-05 21:25:51 -0500 | [diff] [blame] | 5851 | while (1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5852 | if (path->slots[0] == 0) { |
Chris Mason | b4ce94d | 2009-02-04 09:25:08 -0500 | [diff] [blame] | 5853 | btrfs_set_path_blocking(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5854 | ret = btrfs_prev_leaf(root, path); |
| 5855 | if (ret != 0) |
| 5856 | return ret; |
| 5857 | } else { |
| 5858 | path->slots[0]--; |
| 5859 | } |
| 5860 | leaf = path->nodes[0]; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5861 | nritems = btrfs_header_nritems(leaf); |
| 5862 | if (nritems == 0) |
| 5863 | return 1; |
| 5864 | if (path->slots[0] == nritems) |
| 5865 | path->slots[0]--; |
| 5866 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5867 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5868 | if (found_key.objectid < min_objectid) |
| 5869 | break; |
Yan Zheng | 0a4eefb | 2009-07-24 11:06:53 -0400 | [diff] [blame] | 5870 | if (found_key.type == type) |
| 5871 | return 0; |
Chris Mason | e02119d | 2008-09-05 16:13:11 -0400 | [diff] [blame] | 5872 | if (found_key.objectid == min_objectid && |
| 5873 | found_key.type < type) |
| 5874 | break; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 5875 | } |
| 5876 | return 1; |
| 5877 | } |
Wang Shilong | ade2e0b | 2014-01-12 21:38:33 +0800 | [diff] [blame] | 5878 | |
| 5879 | /* |
| 5880 | * search in extent tree to find a previous Metadata/Data extent item with |
| 5881 | * min objecitd. |
| 5882 | * |
| 5883 | * returns 0 if something is found, 1 if nothing was found and < 0 on error |
| 5884 | */ |
| 5885 | int btrfs_previous_extent_item(struct btrfs_root *root, |
| 5886 | struct btrfs_path *path, u64 min_objectid) |
| 5887 | { |
| 5888 | struct btrfs_key found_key; |
| 5889 | struct extent_buffer *leaf; |
| 5890 | u32 nritems; |
| 5891 | int ret; |
| 5892 | |
| 5893 | while (1) { |
| 5894 | if (path->slots[0] == 0) { |
| 5895 | btrfs_set_path_blocking(path); |
| 5896 | ret = btrfs_prev_leaf(root, path); |
| 5897 | if (ret != 0) |
| 5898 | return ret; |
| 5899 | } else { |
| 5900 | path->slots[0]--; |
| 5901 | } |
| 5902 | leaf = path->nodes[0]; |
| 5903 | nritems = btrfs_header_nritems(leaf); |
| 5904 | if (nritems == 0) |
| 5905 | return 1; |
| 5906 | if (path->slots[0] == nritems) |
| 5907 | path->slots[0]--; |
| 5908 | |
| 5909 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 5910 | if (found_key.objectid < min_objectid) |
| 5911 | break; |
| 5912 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY || |
| 5913 | found_key.type == BTRFS_METADATA_ITEM_KEY) |
| 5914 | return 0; |
| 5915 | if (found_key.objectid == min_objectid && |
| 5916 | found_key.type < BTRFS_EXTENT_ITEM_KEY) |
| 5917 | break; |
| 5918 | } |
| 5919 | return 1; |
| 5920 | } |