blob: 42ca326a0e6b915127d9f570b570d3e7ec7e07da [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
Chris Masond352ac62008-09-29 15:18:18 -04003 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Masona6b6e752007-10-15 16:22:39 -04006#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +02008#include <linux/rbtree.h>
David Sterbaadf02122017-05-31 19:44:31 +02009#include <linux/mm.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050010#include "ctree.h"
11#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040012#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040013#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040014#include "locking.h"
Nikolay Borisovde37aa52018-10-30 16:43:24 +020015#include "volumes.h"
Qu Wenruof616f5c2019-01-23 15:15:17 +080016#include "qgroup.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050017
Chris Masone089f052007-03-16 16:20:31 -040018static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
19 *root, struct btrfs_path *path, int level);
Omar Sandoval310712b2017-01-17 23:24:37 -080020static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
21 const struct btrfs_key *ins_key, struct btrfs_path *path,
22 int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040023static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040024 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040025 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040026static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -040027 struct extent_buffer *dst_buf,
28 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000029static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
30 int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050031
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020032static const struct btrfs_csums {
33 u16 size;
34 const char *name;
35} btrfs_csums[] = {
36 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
37};
38
39int btrfs_super_csum_size(const struct btrfs_super_block *s)
40{
41 u16 t = btrfs_super_csum_type(s);
42 /*
43 * csum type is validated at mount time
44 */
45 return btrfs_csums[t].size;
46}
47
48const char *btrfs_super_csum_name(u16 csum_type)
49{
50 /* csum type is validated at mount time */
51 return btrfs_csums[csum_type].name;
52}
53
Chris Mason2c90e5d2007-04-02 10:50:19 -040054struct btrfs_path *btrfs_alloc_path(void)
55{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090056 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
David Sterba766ece52019-01-23 18:07:14 +010069 /*
70 * If we currently have a spinning reader or writer lock this
71 * will bump the count of blocking holders and drop the
72 * spinlock.
73 */
74 if (p->locks[i] == BTRFS_READ_LOCK) {
75 btrfs_set_lock_blocking_read(p->nodes[i]);
Chris Masonbd681512011-07-16 15:23:14 -040076 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
David Sterba766ece52019-01-23 18:07:14 +010077 } else if (p->locks[i] == BTRFS_WRITE_LOCK) {
78 btrfs_set_lock_blocking_write(p->nodes[i]);
Chris Masonbd681512011-07-16 15:23:14 -040079 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
David Sterba766ece52019-01-23 18:07:14 +010080 }
Chris Masonb4ce94d2009-02-04 09:25:08 -050081 }
82}
83
Chris Masond352ac62008-09-29 15:18:18 -040084/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040085void btrfs_free_path(struct btrfs_path *p)
86{
Jesper Juhlff175d52010-12-25 21:22:30 +000087 if (!p)
88 return;
David Sterbab3b4aa72011-04-21 01:20:15 +020089 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040090 kmem_cache_free(btrfs_path_cachep, p);
91}
92
Chris Masond352ac62008-09-29 15:18:18 -040093/*
94 * path release drops references on the extent buffers in the path
95 * and it drops any locks held by this path
96 *
97 * It is safe to call this on paths that no locks or extent buffers held.
98 */
David Sterbab3b4aa72011-04-21 01:20:15 +020099noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500100{
101 int i;
Chris Masona2135012008-06-25 16:01:30 -0400102
Chris Mason234b63a2007-03-13 10:46:10 -0400103 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400104 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500105 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400106 continue;
107 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400109 p->locks[i] = 0;
110 }
Chris Mason5f39d392007-10-15 16:14:19 -0400111 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400112 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500113 }
114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/*
117 * safely gets a reference on the root node of a tree. A lock
118 * is not taken, so a concurrent writer may put a different node
119 * at the root of the tree. See btrfs_lock_root_node for the
120 * looping required.
121 *
122 * The extent buffer returned by this has a reference taken, so
123 * it won't disappear. It may stop being the root of the tree
124 * at any time because there are no locks held.
125 */
Chris Mason925baed2008-06-25 16:01:30 -0400126struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
127{
128 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400129
Josef Bacik3083ee22012-03-09 16:01:49 -0500130 while (1) {
131 rcu_read_lock();
132 eb = rcu_dereference(root->node);
133
134 /*
135 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400136 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500137 * the inc_not_zero dance and if it doesn't work then
138 * synchronize_rcu and try again.
139 */
140 if (atomic_inc_not_zero(&eb->refs)) {
141 rcu_read_unlock();
142 break;
143 }
144 rcu_read_unlock();
145 synchronize_rcu();
146 }
Chris Mason925baed2008-06-25 16:01:30 -0400147 return eb;
148}
149
Chris Masond352ac62008-09-29 15:18:18 -0400150/* loop around taking references on and locking the root node of the
151 * tree until you end up with a lock on the root. A locked buffer
152 * is returned, with a reference held.
153 */
Chris Mason925baed2008-06-25 16:01:30 -0400154struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
155{
156 struct extent_buffer *eb;
157
Chris Masond3977122009-01-05 21:25:51 -0500158 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400159 eb = btrfs_root_node(root);
160 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400161 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400162 break;
Chris Mason925baed2008-06-25 16:01:30 -0400163 btrfs_tree_unlock(eb);
164 free_extent_buffer(eb);
165 }
166 return eb;
167}
168
Chris Masonbd681512011-07-16 15:23:14 -0400169/* loop around taking references on and locking the root node of the
170 * tree until you end up with a lock on the root. A locked buffer
171 * is returned, with a reference held.
172 */
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400173struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400174{
175 struct extent_buffer *eb;
176
177 while (1) {
178 eb = btrfs_root_node(root);
179 btrfs_tree_read_lock(eb);
180 if (eb == root->node)
181 break;
182 btrfs_tree_read_unlock(eb);
183 free_extent_buffer(eb);
184 }
185 return eb;
186}
187
Chris Masond352ac62008-09-29 15:18:18 -0400188/* cowonly root (everything not a reference counted cow subvolume), just get
189 * put onto a simple dirty list. transaction.c walks this to make sure they
190 * get properly updated on disk.
191 */
Chris Mason0b86a832008-03-24 15:01:56 -0400192static void add_root_to_dirty_list(struct btrfs_root *root)
193{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400194 struct btrfs_fs_info *fs_info = root->fs_info;
195
Josef Bacike7070be2014-12-16 08:54:43 -0800196 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
197 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
198 return;
199
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400200 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800201 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
202 /* Want the extent tree to be the last on the list */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900203 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
Josef Bacike7070be2014-12-16 08:54:43 -0800204 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400205 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800206 else
207 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400208 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400209 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400210 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400211}
212
Chris Masond352ac62008-09-29 15:18:18 -0400213/*
214 * used by snapshot creation to make a copy of a root for a tree with
215 * a given objectid. The buffer with the new root node is returned in
216 * cow_ret, and this func returns zero on success or a negative error code.
217 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500218int btrfs_copy_root(struct btrfs_trans_handle *trans,
219 struct btrfs_root *root,
220 struct extent_buffer *buf,
221 struct extent_buffer **cow_ret, u64 new_root_objectid)
222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400223 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500224 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500225 int ret = 0;
226 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400227 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500228
Miao Xie27cdeb72014-04-02 19:51:05 +0800229 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +0800231 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
232 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500233
234 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400235 if (level == 0)
236 btrfs_item_key(buf, &disk_key, 0);
237 else
238 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400239
David Sterba4d75f8a2014-06-15 01:54:12 +0200240 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
241 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400242 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 return PTR_ERR(cow);
244
David Sterba58e80122016-11-08 18:30:31 +0100245 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 btrfs_set_header_bytenr(cow, cow->start);
247 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
249 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
250 BTRFS_HEADER_FLAG_RELOC);
251 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
252 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
253 else
254 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
Nikolay Borisovde37aa52018-10-30 16:43:24 +0200256 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -0500257
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400259 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700260 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700262 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500263
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 if (ret)
265 return ret;
266
267 btrfs_mark_buffer_dirty(cow);
268 *cow_ret = cow;
269 return 0;
270}
271
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200272enum mod_log_op {
273 MOD_LOG_KEY_REPLACE,
274 MOD_LOG_KEY_ADD,
275 MOD_LOG_KEY_REMOVE,
276 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
277 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
278 MOD_LOG_MOVE_KEYS,
279 MOD_LOG_ROOT_REPLACE,
280};
281
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200282struct tree_mod_root {
283 u64 logical;
284 u8 level;
285};
286
287struct tree_mod_elem {
288 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530289 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200290 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200291 enum mod_log_op op;
292
293 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
294 int slot;
295
296 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
297 u64 generation;
298
299 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
300 struct btrfs_disk_key key;
301 u64 blockptr;
302
303 /* this is used for op == MOD_LOG_MOVE_KEYS */
David Sterbab6dfa352018-03-05 15:31:18 +0100304 struct {
305 int dst_slot;
306 int nr_items;
307 } move;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200308
309 /* this is used for op == MOD_LOG_ROOT_REPLACE */
310 struct tree_mod_root old_root;
311};
312
Jan Schmidt097b8a72012-06-21 11:08:04 +0200313/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700314 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000315 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700316static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000317{
318 return atomic64_inc_return(&fs_info->tree_mod_seq);
319}
320
321/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200322 * This adds a new blocker to the tree mod log's blocker list if the @elem
323 * passed does not already have a sequence number set. So when a caller expects
324 * to record tree modifications, it should ensure to set elem->seq to zero
325 * before calling btrfs_get_tree_mod_seq.
326 * Returns a fresh, unused tree log modification sequence number, even if no new
327 * blocker was added.
328 */
329u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
330 struct seq_list *elem)
331{
David Sterbab1a09f12018-03-05 15:43:41 +0100332 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200333 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200334 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700335 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200336 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
337 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200338 spin_unlock(&fs_info->tree_mod_seq_lock);
David Sterbab1a09f12018-03-05 15:43:41 +0100339 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200340
Josef Bacikfcebe452014-05-13 17:30:47 -0700341 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200342}
343
344void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
345 struct seq_list *elem)
346{
347 struct rb_root *tm_root;
348 struct rb_node *node;
349 struct rb_node *next;
350 struct seq_list *cur_elem;
351 struct tree_mod_elem *tm;
352 u64 min_seq = (u64)-1;
353 u64 seq_putting = elem->seq;
354
355 if (!seq_putting)
356 return;
357
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200358 spin_lock(&fs_info->tree_mod_seq_lock);
359 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200360 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200361
362 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200363 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200364 if (seq_putting > cur_elem->seq) {
365 /*
366 * blocker with lower sequence number exists, we
367 * cannot remove anything from the log
368 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200369 spin_unlock(&fs_info->tree_mod_seq_lock);
370 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200371 }
372 min_seq = cur_elem->seq;
373 }
374 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200375 spin_unlock(&fs_info->tree_mod_seq_lock);
376
377 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 * anything that's lower than the lowest existing (read: blocked)
379 * sequence number can be removed from the tree.
380 */
David Sterbab1a09f12018-03-05 15:43:41 +0100381 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200382 tm_root = &fs_info->tree_mod_log;
383 for (node = rb_first(tm_root); node; node = next) {
384 next = rb_next(node);
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800385 tm = rb_entry(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200386 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200387 continue;
388 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200389 kfree(tm);
390 }
David Sterbab1a09f12018-03-05 15:43:41 +0100391 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200392}
393
394/*
395 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530396 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200397 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530398 * The 'start address' is the logical address of the *new* root node
399 * for root replace operations, or the logical address of the affected
400 * block for all other operations.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200401 */
402static noinline int
403__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
404{
405 struct rb_root *tm_root;
406 struct rb_node **new;
407 struct rb_node *parent = NULL;
408 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409
David Sterba73e82fe2019-03-27 16:19:55 +0100410 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
411
Josef Bacikfcebe452014-05-13 17:30:47 -0700412 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200413
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200414 tm_root = &fs_info->tree_mod_log;
415 new = &tm_root->rb_node;
416 while (*new) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800417 cur = rb_entry(*new, struct tree_mod_elem, node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200418 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530419 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530421 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200422 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200423 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200424 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200425 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200426 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000427 else
428 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200429 }
430
431 rb_link_node(&tm->node, parent, new);
432 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000433 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200434}
435
Jan Schmidt097b8a72012-06-21 11:08:04 +0200436/*
437 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
438 * returns zero with the tree_mod_log_lock acquired. The caller must hold
439 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbab1a09f12018-03-05 15:43:41 +0100440 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200441 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200442static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
443 struct extent_buffer *eb) {
444 smp_mb();
445 if (list_empty(&(fs_info)->tree_mod_seq_list))
446 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200447 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200448 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000449
David Sterbab1a09f12018-03-05 15:43:41 +0100450 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000451 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbab1a09f12018-03-05 15:43:41 +0100452 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000453 return 1;
454 }
455
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200456 return 0;
457}
458
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000459/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
460static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
461 struct extent_buffer *eb)
462{
463 smp_mb();
464 if (list_empty(&(fs_info)->tree_mod_seq_list))
465 return 0;
466 if (eb && btrfs_header_level(eb) == 0)
467 return 0;
468
469 return 1;
470}
471
472static struct tree_mod_elem *
473alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
474 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200475{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200476 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200477
Josef Bacikc8cc6342013-07-01 16:18:19 -0400478 tm = kzalloc(sizeof(*tm), flags);
479 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000480 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200481
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530482 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200483 if (op != MOD_LOG_KEY_ADD) {
484 btrfs_node_key(eb, &tm->key, slot);
485 tm->blockptr = btrfs_node_blockptr(eb, slot);
486 }
487 tm->op = op;
488 tm->slot = slot;
489 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000490 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200491
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000492 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200493}
494
David Sterbae09c2ef2018-03-05 15:09:03 +0100495static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
496 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200497{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000498 struct tree_mod_elem *tm;
499 int ret;
500
David Sterbae09c2ef2018-03-05 15:09:03 +0100501 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200502 return 0;
503
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000504 tm = alloc_tree_mod_elem(eb, slot, op, flags);
505 if (!tm)
506 return -ENOMEM;
507
David Sterbae09c2ef2018-03-05 15:09:03 +0100508 if (tree_mod_dont_log(eb->fs_info, eb)) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000509 kfree(tm);
510 return 0;
511 }
512
David Sterbae09c2ef2018-03-05 15:09:03 +0100513 ret = __tree_mod_log_insert(eb->fs_info, tm);
David Sterbab1a09f12018-03-05 15:43:41 +0100514 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000515 if (ret)
516 kfree(tm);
517
518 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200519}
520
David Sterba6074d452018-03-05 15:03:52 +0100521static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
522 int dst_slot, int src_slot, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200523{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000524 struct tree_mod_elem *tm = NULL;
525 struct tree_mod_elem **tm_list = NULL;
526 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200527 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000528 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200529
David Sterba6074d452018-03-05 15:03:52 +0100530 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200531 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200532
David Sterba176ef8f2017-03-28 14:35:01 +0200533 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000534 if (!tm_list)
535 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200536
David Sterba176ef8f2017-03-28 14:35:01 +0200537 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000538 if (!tm) {
539 ret = -ENOMEM;
540 goto free_tms;
541 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200542
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530543 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200544 tm->slot = src_slot;
545 tm->move.dst_slot = dst_slot;
546 tm->move.nr_items = nr_items;
547 tm->op = MOD_LOG_MOVE_KEYS;
548
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000549 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
550 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
David Sterba176ef8f2017-03-28 14:35:01 +0200551 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000552 if (!tm_list[i]) {
553 ret = -ENOMEM;
554 goto free_tms;
555 }
556 }
557
David Sterba6074d452018-03-05 15:03:52 +0100558 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000559 goto free_tms;
560 locked = 1;
561
562 /*
563 * When we override something during the move, we log these removals.
564 * This can only happen when we move towards the beginning of the
565 * buffer, i.e. dst_slot < src_slot.
566 */
567 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
David Sterba6074d452018-03-05 15:03:52 +0100568 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000569 if (ret)
570 goto free_tms;
571 }
572
David Sterba6074d452018-03-05 15:03:52 +0100573 ret = __tree_mod_log_insert(eb->fs_info, tm);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000574 if (ret)
575 goto free_tms;
David Sterbab1a09f12018-03-05 15:43:41 +0100576 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000577 kfree(tm_list);
578
579 return 0;
580free_tms:
581 for (i = 0; i < nr_items; i++) {
582 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
David Sterba6074d452018-03-05 15:03:52 +0100583 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000584 kfree(tm_list[i]);
585 }
586 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100587 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000588 kfree(tm_list);
589 kfree(tm);
590
591 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200592}
593
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000594static inline int
595__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
596 struct tree_mod_elem **tm_list,
597 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200598{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000599 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200600 int ret;
601
Jan Schmidt097b8a72012-06-21 11:08:04 +0200602 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000603 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
604 if (ret) {
605 for (j = nritems - 1; j > i; j--)
606 rb_erase(&tm_list[j]->node,
607 &fs_info->tree_mod_log);
608 return ret;
609 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200610 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000611
612 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200613}
614
David Sterba95b757c2018-03-05 15:22:30 +0100615static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
616 struct extent_buffer *new_root, int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200617{
David Sterba95b757c2018-03-05 15:22:30 +0100618 struct btrfs_fs_info *fs_info = old_root->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000619 struct tree_mod_elem *tm = NULL;
620 struct tree_mod_elem **tm_list = NULL;
621 int nritems = 0;
622 int ret = 0;
623 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200624
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000625 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200626 return 0;
627
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000628 if (log_removal && btrfs_header_level(old_root) > 0) {
629 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100630 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
David Sterbabcc8e072017-03-28 14:35:42 +0200631 GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000632 if (!tm_list) {
633 ret = -ENOMEM;
634 goto free_tms;
635 }
636 for (i = 0; i < nritems; i++) {
637 tm_list[i] = alloc_tree_mod_elem(old_root, i,
David Sterbabcc8e072017-03-28 14:35:42 +0200638 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000639 if (!tm_list[i]) {
640 ret = -ENOMEM;
641 goto free_tms;
642 }
643 }
644 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000645
David Sterbabcc8e072017-03-28 14:35:42 +0200646 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000647 if (!tm) {
648 ret = -ENOMEM;
649 goto free_tms;
650 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200651
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530652 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200653 tm->old_root.logical = old_root->start;
654 tm->old_root.level = btrfs_header_level(old_root);
655 tm->generation = btrfs_header_generation(old_root);
656 tm->op = MOD_LOG_ROOT_REPLACE;
657
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000658 if (tree_mod_dont_log(fs_info, NULL))
659 goto free_tms;
660
661 if (tm_list)
662 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
663 if (!ret)
664 ret = __tree_mod_log_insert(fs_info, tm);
665
David Sterbab1a09f12018-03-05 15:43:41 +0100666 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000667 if (ret)
668 goto free_tms;
669 kfree(tm_list);
670
671 return ret;
672
673free_tms:
674 if (tm_list) {
675 for (i = 0; i < nritems; i++)
676 kfree(tm_list[i]);
677 kfree(tm_list);
678 }
679 kfree(tm);
680
681 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200682}
683
684static struct tree_mod_elem *
685__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
686 int smallest)
687{
688 struct rb_root *tm_root;
689 struct rb_node *node;
690 struct tree_mod_elem *cur = NULL;
691 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200692
David Sterbab1a09f12018-03-05 15:43:41 +0100693 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 tm_root = &fs_info->tree_mod_log;
695 node = tm_root->rb_node;
696 while (node) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800697 cur = rb_entry(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530698 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200699 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530700 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200701 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200702 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200703 node = node->rb_left;
704 } else if (!smallest) {
705 /* we want the node with the highest seq */
706 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200707 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708 found = cur;
709 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200710 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200711 /* we want the node with the smallest seq */
712 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200713 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200714 found = cur;
715 node = node->rb_right;
716 } else {
717 found = cur;
718 break;
719 }
720 }
David Sterbab1a09f12018-03-05 15:43:41 +0100721 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200722
723 return found;
724}
725
726/*
727 * this returns the element from the log with the smallest time sequence
728 * value that's in the log (the oldest log item). any element with a time
729 * sequence lower than min_seq will be ignored.
730 */
731static struct tree_mod_elem *
732tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
733 u64 min_seq)
734{
735 return __tree_mod_log_search(fs_info, start, min_seq, 1);
736}
737
738/*
739 * this returns the element from the log with the largest time sequence
740 * value that's in the log (the most recent log item). any element with
741 * a time sequence lower than min_seq will be ignored.
742 */
743static struct tree_mod_elem *
744tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
745{
746 return __tree_mod_log_search(fs_info, start, min_seq, 0);
747}
748
David Sterbaed874f02019-03-20 14:22:04 +0100749static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200750 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000751 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200752{
David Sterbaed874f02019-03-20 14:22:04 +0100753 struct btrfs_fs_info *fs_info = dst->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000754 int ret = 0;
755 struct tree_mod_elem **tm_list = NULL;
756 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200757 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000758 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200759
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000760 if (!tree_mod_need_log(fs_info, NULL))
761 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200762
Josef Bacikc8cc6342013-07-01 16:18:19 -0400763 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000764 return 0;
765
David Sterba31e818f2015-02-20 18:00:26 +0100766 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000767 GFP_NOFS);
768 if (!tm_list)
769 return -ENOMEM;
770
771 tm_list_add = tm_list;
772 tm_list_rem = tm_list + nr_items;
773 for (i = 0; i < nr_items; i++) {
774 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
775 MOD_LOG_KEY_REMOVE, GFP_NOFS);
776 if (!tm_list_rem[i]) {
777 ret = -ENOMEM;
778 goto free_tms;
779 }
780
781 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
782 MOD_LOG_KEY_ADD, GFP_NOFS);
783 if (!tm_list_add[i]) {
784 ret = -ENOMEM;
785 goto free_tms;
786 }
787 }
788
789 if (tree_mod_dont_log(fs_info, NULL))
790 goto free_tms;
791 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200793 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000794 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
795 if (ret)
796 goto free_tms;
797 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
798 if (ret)
799 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000801
David Sterbab1a09f12018-03-05 15:43:41 +0100802 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000803 kfree(tm_list);
804
805 return 0;
806
807free_tms:
808 for (i = 0; i < nr_items * 2; i++) {
809 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
810 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
811 kfree(tm_list[i]);
812 }
813 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100814 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000815 kfree(tm_list);
816
817 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200818}
819
David Sterbadb7279a2018-03-05 15:14:25 +0100820static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200821{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000822 struct tree_mod_elem **tm_list = NULL;
823 int nritems = 0;
824 int i;
825 int ret = 0;
826
827 if (btrfs_header_level(eb) == 0)
828 return 0;
829
David Sterbadb7279a2018-03-05 15:14:25 +0100830 if (!tree_mod_need_log(eb->fs_info, NULL))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000831 return 0;
832
833 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100834 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000835 if (!tm_list)
836 return -ENOMEM;
837
838 for (i = 0; i < nritems; i++) {
839 tm_list[i] = alloc_tree_mod_elem(eb, i,
840 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
841 if (!tm_list[i]) {
842 ret = -ENOMEM;
843 goto free_tms;
844 }
845 }
846
David Sterbadb7279a2018-03-05 15:14:25 +0100847 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000848 goto free_tms;
849
David Sterbadb7279a2018-03-05 15:14:25 +0100850 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
David Sterbab1a09f12018-03-05 15:43:41 +0100851 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000852 if (ret)
853 goto free_tms;
854 kfree(tm_list);
855
856 return 0;
857
858free_tms:
859 for (i = 0; i < nritems; i++)
860 kfree(tm_list[i]);
861 kfree(tm_list);
862
863 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200864}
865
Chris Masond352ac62008-09-29 15:18:18 -0400866/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400867 * check if the tree block can be shared by multiple trees
868 */
869int btrfs_block_can_be_shared(struct btrfs_root *root,
870 struct extent_buffer *buf)
871{
872 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400873 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 * are never shared. If a block was allocated after the last
875 * snapshot and the block was not allocated by tree relocation,
876 * we know the block is not shared.
877 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800878 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400879 buf != root->node && buf != root->commit_root &&
880 (btrfs_header_generation(buf) <=
881 btrfs_root_last_snapshot(&root->root_item) ||
882 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
883 return 1;
Nikolay Borisova79865c2018-06-21 09:45:00 +0300884
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885 return 0;
886}
887
888static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
889 struct btrfs_root *root,
890 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400891 struct extent_buffer *cow,
892 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400893{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400894 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400895 u64 refs;
896 u64 owner;
897 u64 flags;
898 u64 new_flags = 0;
899 int ret;
900
901 /*
902 * Backrefs update rules:
903 *
904 * Always use full backrefs for extent pointers in tree block
905 * allocated by tree relocation.
906 *
907 * If a shared tree block is no longer referenced by its owner
908 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
909 * use full backrefs for extent pointers in tree block.
910 *
911 * If a tree block is been relocating
912 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
913 * use full backrefs for extent pointers in tree block.
914 * The reason for this is some operations (such as drop tree)
915 * are only allowed for blocks use full backrefs.
916 */
917
918 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400919 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500920 btrfs_header_level(buf), 1,
921 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700922 if (ret)
923 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700924 if (refs == 0) {
925 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400926 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700927 return ret;
928 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 } else {
930 refs = 1;
931 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
932 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
933 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
934 else
935 flags = 0;
936 }
937
938 owner = btrfs_header_owner(buf);
939 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
940 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
941
942 if (refs > 1) {
943 if ((owner == root->root_key.objectid ||
944 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
945 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700946 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500947 if (ret)
948 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949
950 if (root->root_key.objectid ==
951 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700952 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500953 if (ret)
954 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700955 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500956 if (ret)
957 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958 }
959 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
960 } else {
961
962 if (root->root_key.objectid ==
963 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700964 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700966 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500967 if (ret)
968 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 }
970 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400971 int level = btrfs_header_level(buf);
972
David Sterbaf5c8daa2019-03-20 11:43:36 +0100973 ret = btrfs_set_disk_extent_flags(trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400974 buf->start,
975 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400976 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700977 if (ret)
978 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400979 }
980 } else {
981 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
982 if (root->root_key.objectid ==
983 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700984 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700986 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500987 if (ret)
988 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700989 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500990 if (ret)
991 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 }
David Sterba6a884d7d2019-03-20 14:30:02 +0100993 btrfs_clean_tree_block(buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400994 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400995 }
996 return 0;
997}
998
Filipe Mananaa6279472019-01-25 11:48:51 +0000999static struct extent_buffer *alloc_tree_block_no_bg_flush(
1000 struct btrfs_trans_handle *trans,
1001 struct btrfs_root *root,
1002 u64 parent_start,
1003 const struct btrfs_disk_key *disk_key,
1004 int level,
1005 u64 hint,
1006 u64 empty_size)
1007{
1008 struct btrfs_fs_info *fs_info = root->fs_info;
1009 struct extent_buffer *ret;
1010
1011 /*
1012 * If we are COWing a node/leaf from the extent, chunk, device or free
1013 * space trees, make sure that we do not finish block group creation of
1014 * pending block groups. We do this to avoid a deadlock.
1015 * COWing can result in allocation of a new chunk, and flushing pending
1016 * block groups (btrfs_create_pending_block_groups()) can be triggered
1017 * when finishing allocation of a new chunk. Creation of a pending block
1018 * group modifies the extent, chunk, device and free space trees,
1019 * therefore we could deadlock with ourselves since we are holding a
1020 * lock on an extent buffer that btrfs_create_pending_block_groups() may
1021 * try to COW later.
1022 * For similar reasons, we also need to delay flushing pending block
1023 * groups when splitting a leaf or node, from one of those trees, since
1024 * we are holding a write lock on it and its parent or when inserting a
1025 * new root node for one of those trees.
1026 */
1027 if (root == fs_info->extent_root ||
1028 root == fs_info->chunk_root ||
1029 root == fs_info->dev_root ||
1030 root == fs_info->free_space_root)
1031 trans->can_flush_pending_bgs = false;
1032
1033 ret = btrfs_alloc_tree_block(trans, root, parent_start,
1034 root->root_key.objectid, disk_key, level,
1035 hint, empty_size);
1036 trans->can_flush_pending_bgs = true;
1037
1038 return ret;
1039}
1040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001041/*
Chris Masond3977122009-01-05 21:25:51 -05001042 * does the dirty work in cow of a single block. The parent block (if
1043 * supplied) is updated to point to the new cow copy. The new buffer is marked
1044 * dirty and returned locked. If you modify the block it needs to be marked
1045 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001046 *
1047 * search_start -- an allocation hint for the new block
1048 *
Chris Masond3977122009-01-05 21:25:51 -05001049 * empty_size -- a hint that you plan on doing more cow. This is the size in
1050 * bytes the allocator should try to find free next to the block it returns.
1051 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001052 */
Chris Masond3977122009-01-05 21:25:51 -05001053static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001054 struct btrfs_root *root,
1055 struct extent_buffer *buf,
1056 struct extent_buffer *parent, int parent_slot,
1057 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001058 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001059{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001060 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001061 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001062 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001063 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001064 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001065 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001066 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001067
Chris Mason925baed2008-06-25 16:01:30 -04001068 if (*cow_ret == buf)
1069 unlock_orig = 1;
1070
Chris Masonb9447ef82009-03-09 11:45:38 -04001071 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001072
Miao Xie27cdeb72014-04-02 19:51:05 +08001073 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001074 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +08001075 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1076 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001077
Chris Mason7bb86312007-12-11 09:25:06 -05001078 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001079
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001080 if (level == 0)
1081 btrfs_item_key(buf, &disk_key, 0);
1082 else
1083 btrfs_node_key(buf, &disk_key, 0);
1084
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001085 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1086 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001087
Filipe Mananaa6279472019-01-25 11:48:51 +00001088 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1089 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001090 if (IS_ERR(cow))
1091 return PTR_ERR(cow);
1092
Chris Masonb4ce94d2009-02-04 09:25:08 -05001093 /* cow is set to blocking by btrfs_init_new_buffer */
1094
David Sterba58e80122016-11-08 18:30:31 +01001095 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001096 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001097 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001098 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1099 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1100 BTRFS_HEADER_FLAG_RELOC);
1101 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1102 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1103 else
1104 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001105
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001106 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05001107
Mark Fashehbe1a5562011-08-08 13:20:18 -07001108 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001109 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001110 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001111 return ret;
1112 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001113
Miao Xie27cdeb72014-04-02 19:51:05 +08001114 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001115 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001116 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001117 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001118 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001119 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001120 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001121
Chris Mason6702ed42007-08-07 16:15:09 -04001122 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001123 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001124 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1125 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1126 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001127
Chris Mason5f39d392007-10-15 16:14:19 -04001128 extent_buffer_get(cow);
David Sterbad9d19a02018-03-05 16:35:29 +01001129 ret = tree_mod_log_insert_root(root->node, cow, 1);
1130 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001131 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001132
Yan, Zhengf0486c62010-05-16 10:46:25 -04001133 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001134 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001135 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001136 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001137 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001138 WARN_ON(trans->transid != btrfs_header_generation(parent));
David Sterbae09c2ef2018-03-05 15:09:03 +01001139 tree_mod_log_insert_key(parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001140 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001141 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001142 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001143 btrfs_set_node_ptr_generation(parent, parent_slot,
1144 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001145 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001146 if (last_ref) {
David Sterbadb7279a2018-03-05 15:14:25 +01001147 ret = tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001149 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001150 return ret;
1151 }
1152 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001153 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001154 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001155 }
Chris Mason925baed2008-06-25 16:01:30 -04001156 if (unlock_orig)
1157 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001158 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001159 btrfs_mark_buffer_dirty(cow);
1160 *cow_ret = cow;
1161 return 0;
1162}
1163
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001164/*
1165 * returns the logical address of the oldest predecessor of the given root.
1166 * entries older than time_seq are ignored.
1167 */
David Sterbabcd24da2018-03-05 15:33:18 +01001168static struct tree_mod_elem *__tree_mod_log_oldest_root(
1169 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001170{
1171 struct tree_mod_elem *tm;
1172 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001173 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001174 int looped = 0;
1175
1176 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001177 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001178
1179 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301180 * the very last operation that's logged for a root is the
1181 * replacement operation (if it is replaced at all). this has
1182 * the logical address of the *new* root, making it the very
1183 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001184 */
1185 while (1) {
David Sterbabcd24da2018-03-05 15:33:18 +01001186 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001187 time_seq);
1188 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001189 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001190 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001191 * if there are no tree operation for the oldest root, we simply
1192 * return it. this should only happen if that (old) root is at
1193 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001194 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001195 if (!tm)
1196 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001197
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001198 /*
1199 * if there's an operation that's not a root replacement, we
1200 * found the oldest version of our root. normally, we'll find a
1201 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1202 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001203 if (tm->op != MOD_LOG_ROOT_REPLACE)
1204 break;
1205
1206 found = tm;
1207 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001208 looped = 1;
1209 }
1210
Jan Schmidta95236d2012-06-05 16:41:24 +02001211 /* if there's no old root to return, return what we found instead */
1212 if (!found)
1213 found = tm;
1214
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001215 return found;
1216}
1217
1218/*
1219 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001220 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001221 * time_seq).
1222 */
1223static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001224__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1225 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001226{
1227 u32 n;
1228 struct rb_node *next;
1229 struct tree_mod_elem *tm = first_tm;
1230 unsigned long o_dst;
1231 unsigned long o_src;
1232 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1233
1234 n = btrfs_header_nritems(eb);
David Sterbab1a09f12018-03-05 15:43:41 +01001235 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001236 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001237 /*
1238 * all the operations are recorded with the operator used for
1239 * the modification. as we're going backwards, we do the
1240 * opposite of each operation here.
1241 */
1242 switch (tm->op) {
1243 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1244 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001245 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001246 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001247 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001248 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);
Chris Mason4c3e6962012-12-18 15:43:18 -05001252 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001253 break;
1254 case MOD_LOG_KEY_REPLACE:
1255 BUG_ON(tm->slot >= n);
1256 btrfs_set_node_key(eb, &tm->key, tm->slot);
1257 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1258 btrfs_set_node_ptr_generation(eb, tm->slot,
1259 tm->generation);
1260 break;
1261 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001262 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001263 n--;
1264 break;
1265 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001266 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1267 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1268 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001269 tm->move.nr_items * p_size);
1270 break;
1271 case MOD_LOG_ROOT_REPLACE:
1272 /*
1273 * this operation is special. for roots, this must be
1274 * handled explicitly before rewinding.
1275 * for non-roots, this operation may exist if the node
1276 * was a root: root A -> child B; then A gets empty and
1277 * B is promoted to the new root. in the mod log, we'll
1278 * have a root-replace operation for B, a tree block
1279 * that is no root. we simply ignore that operation.
1280 */
1281 break;
1282 }
1283 next = rb_next(&tm->node);
1284 if (!next)
1285 break;
Geliang Tang6b4df8b2016-12-19 22:53:41 +08001286 tm = rb_entry(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301287 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001288 break;
1289 }
David Sterbab1a09f12018-03-05 15:43:41 +01001290 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001291 btrfs_set_header_nritems(eb, n);
1292}
1293
Jan Schmidt47fb0912013-04-13 13:19:55 +00001294/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001295 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001296 * is returned. If rewind operations happen, a fresh buffer is returned. The
1297 * returned buffer is always read-locked. If the returned buffer is not the
1298 * input buffer, the lock on the input buffer is released and the input buffer
1299 * is freed (its refcount is decremented).
1300 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001301static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001302tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1303 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001304{
1305 struct extent_buffer *eb_rewin;
1306 struct tree_mod_elem *tm;
1307
1308 if (!time_seq)
1309 return eb;
1310
1311 if (btrfs_header_level(eb) == 0)
1312 return eb;
1313
1314 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1315 if (!tm)
1316 return eb;
1317
Josef Bacik9ec72672013-08-07 16:57:23 -04001318 btrfs_set_path_blocking(path);
David Sterba300aa892018-04-04 02:00:17 +02001319 btrfs_set_lock_blocking_read(eb);
Josef Bacik9ec72672013-08-07 16:57:23 -04001320
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001321 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1322 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001323 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001324 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001325 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001326 free_extent_buffer(eb);
1327 return NULL;
1328 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001329 btrfs_set_header_bytenr(eb_rewin, eb->start);
1330 btrfs_set_header_backref_rev(eb_rewin,
1331 btrfs_header_backref_rev(eb));
1332 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001333 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001334 } else {
1335 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001336 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001337 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001338 free_extent_buffer(eb);
1339 return NULL;
1340 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001341 }
1342
Josef Bacik9ec72672013-08-07 16:57:23 -04001343 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001344 free_extent_buffer(eb);
1345
Jan Schmidt47fb0912013-04-13 13:19:55 +00001346 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001347 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001348 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001349 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001350
1351 return eb_rewin;
1352}
1353
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001354/*
1355 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1356 * value. If there are no changes, the current root->root_node is returned. If
1357 * anything changed in between, there's a fresh buffer allocated on which the
1358 * rewind operations are done. In any case, the returned buffer is read locked.
1359 * Returns NULL on error (with no locks held).
1360 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001361static inline struct extent_buffer *
1362get_old_root(struct btrfs_root *root, u64 time_seq)
1363{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001364 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001365 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001366 struct extent_buffer *eb = NULL;
1367 struct extent_buffer *eb_root;
Filipe Mananaefad8a82019-08-12 19:14:29 +01001368 u64 eb_root_owner = 0;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001369 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001370 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001371 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001372 u64 logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001373 int level;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001374
Jan Schmidt30b04632013-04-13 13:19:54 +00001375 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001376 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001377 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001378 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001379
Jan Schmidta95236d2012-06-05 16:41:24 +02001380 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1381 old_root = &tm->old_root;
1382 old_generation = tm->generation;
1383 logical = old_root->logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001384 level = old_root->level;
Jan Schmidta95236d2012-06-05 16:41:24 +02001385 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001386 logical = eb_root->start;
Qu Wenruo581c1762018-03-29 09:08:11 +08001387 level = btrfs_header_level(eb_root);
Jan Schmidta95236d2012-06-05 16:41:24 +02001388 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001390 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001391 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001392 btrfs_tree_read_unlock(eb_root);
1393 free_extent_buffer(eb_root);
Qu Wenruo581c1762018-03-29 09:08:11 +08001394 old = read_tree_block(fs_info, logical, 0, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08001395 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1396 if (!IS_ERR(old))
1397 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001398 btrfs_warn(fs_info,
1399 "failed to read tree block %llu from get_old_root",
1400 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001401 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001402 eb = btrfs_clone_extent_buffer(old);
1403 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001404 }
1405 } else if (old_root) {
Filipe Mananaefad8a82019-08-12 19:14:29 +01001406 eb_root_owner = btrfs_header_owner(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001407 btrfs_tree_read_unlock(eb_root);
1408 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001410 } else {
David Sterba300aa892018-04-04 02:00:17 +02001411 btrfs_set_lock_blocking_read(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001412 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001413 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001414 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001415 }
1416
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001417 if (!eb)
1418 return NULL;
1419 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001420 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001421 btrfs_set_header_bytenr(eb, eb->start);
1422 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Filipe Mananaefad8a82019-08-12 19:14:29 +01001423 btrfs_set_header_owner(eb, eb_root_owner);
Jan Schmidta95236d2012-06-05 16:41:24 +02001424 btrfs_set_header_level(eb, old_root->level);
1425 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001426 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001427 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001428 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001429 else
1430 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001432
1433 return eb;
1434}
1435
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001436int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1437{
1438 struct tree_mod_elem *tm;
1439 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001440 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001441
David Sterbabcd24da2018-03-05 15:33:18 +01001442 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001443 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1444 level = tm->old_root.level;
1445 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001446 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001447 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001448 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001449
1450 return level;
1451}
1452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001453static inline int should_cow_block(struct btrfs_trans_handle *trans,
1454 struct btrfs_root *root,
1455 struct extent_buffer *buf)
1456{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001457 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001458 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001459
David Sterbad1980132018-03-16 02:39:40 +01001460 /* Ensure we can see the FORCE_COW bit */
1461 smp_mb__before_atomic();
Liu Bof1ebcc72011-11-14 20:48:06 -05001462
1463 /*
1464 * We do not need to cow a block if
1465 * 1) this block is not created or changed in this transaction;
1466 * 2) this block does not belong to TREE_RELOC tree;
1467 * 3) the root is not forced COW.
1468 *
1469 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001470 * when we create snapshot during committing the transaction,
Andrea Gelmini52042d82018-11-28 12:05:13 +01001471 * after we've finished copying src root, we must COW the shared
Liu Bof1ebcc72011-11-14 20:48:06 -05001472 * block to ensure the metadata consistency.
1473 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 if (btrfs_header_generation(buf) == trans->transid &&
1475 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1476 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001477 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001478 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 return 0;
1480 return 1;
1481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001485 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001486 * once per transaction, as long as it hasn't been written yet
1487 */
Chris Masond3977122009-01-05 21:25:51 -05001488noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001489 struct btrfs_root *root, struct extent_buffer *buf,
1490 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001491 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001492{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001493 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001494 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001495 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001496
Josef Bacik83354f02018-11-30 11:52:13 -05001497 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1498 btrfs_err(fs_info,
1499 "COW'ing blocks on a fs root that's being dropped");
1500
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001501 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001502 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001503 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001504 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001505
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001506 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001507 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001508 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001509
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001510 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001511 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001512 *cow_ret = buf;
1513 return 0;
1514 }
Chris Masonc4876852009-02-04 09:24:25 -05001515
Byongho Leeee221842015-12-15 01:42:10 +09001516 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001517
1518 if (parent)
David Sterba8bead252018-04-04 02:03:48 +02001519 btrfs_set_lock_blocking_write(parent);
1520 btrfs_set_lock_blocking_write(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001521
Qu Wenruof616f5c2019-01-23 15:15:17 +08001522 /*
1523 * Before CoWing this block for later modification, check if it's
1524 * the subtree root and do the delayed subtree trace if needed.
1525 *
1526 * Also We don't care about the error, as it's handled internally.
1527 */
1528 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001529 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001530 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001531
1532 trace_btrfs_cow_block(root, buf, *cow_ret);
1533
Chris Masonf510cfe2007-10-15 16:14:48 -04001534 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001535}
1536
Chris Masond352ac62008-09-29 15:18:18 -04001537/*
1538 * helper function for defrag to decide if two blocks pointed to by a
1539 * node are actually close by
1540 */
Chris Mason6b800532007-10-15 16:17:34 -04001541static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001542{
Chris Mason6b800532007-10-15 16:17:34 -04001543 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001544 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001545 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001546 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001547 return 0;
1548}
1549
Chris Mason081e9572007-11-06 10:26:24 -05001550/*
1551 * compare two keys in a memcmp fashion
1552 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001553static int comp_keys(const struct btrfs_disk_key *disk,
1554 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001555{
1556 struct btrfs_key k1;
1557
1558 btrfs_disk_key_to_cpu(&k1, disk);
1559
Diego Calleja20736ab2009-07-24 11:06:52 -04001560 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001561}
1562
Josef Bacikf3465ca2008-11-12 14:19:50 -05001563/*
1564 * same as comp_keys only with two btrfs_key's
1565 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001566int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001567{
1568 if (k1->objectid > k2->objectid)
1569 return 1;
1570 if (k1->objectid < k2->objectid)
1571 return -1;
1572 if (k1->type > k2->type)
1573 return 1;
1574 if (k1->type < k2->type)
1575 return -1;
1576 if (k1->offset > k2->offset)
1577 return 1;
1578 if (k1->offset < k2->offset)
1579 return -1;
1580 return 0;
1581}
Chris Mason081e9572007-11-06 10:26:24 -05001582
Chris Masond352ac62008-09-29 15:18:18 -04001583/*
1584 * this is used by the defrag code to go through all the
1585 * leaves pointed to by a node and reallocate them so that
1586 * disk order is close to key order
1587 */
Chris Mason6702ed42007-08-07 16:15:09 -04001588int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001589 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001590 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001591 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001592{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001593 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001594 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001595 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001596 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001597 u64 search_start = *last_ret;
1598 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001599 u64 other;
1600 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001601 int end_slot;
1602 int i;
1603 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001604 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001605 int uptodate;
1606 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001607 int progress_passed = 0;
1608 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001609
Chris Mason5708b952007-10-25 15:43:18 -04001610 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001611
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001612 WARN_ON(trans->transaction != fs_info->running_transaction);
1613 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001614
Chris Mason6b800532007-10-15 16:17:34 -04001615 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001616 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001617 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001618
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001619 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001620 return 0;
1621
David Sterba8bead252018-04-04 02:03:48 +02001622 btrfs_set_lock_blocking_write(parent);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001623
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001624 for (i = start_slot; i <= end_slot; i++) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001625 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001626 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001627
Chris Mason081e9572007-11-06 10:26:24 -05001628 btrfs_node_key(parent, &disk_key, i);
1629 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1630 continue;
1631
1632 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001633 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001634 gen = btrfs_node_ptr_generation(parent, i);
Qu Wenruo581c1762018-03-29 09:08:11 +08001635 btrfs_node_key_to_cpu(parent, &first_key, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001636 if (last_block == 0)
1637 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001638
Chris Mason6702ed42007-08-07 16:15:09 -04001639 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001640 other = btrfs_node_blockptr(parent, i - 1);
1641 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001642 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001643 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001644 other = btrfs_node_blockptr(parent, i + 1);
1645 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001646 }
Chris Masone9d0b132007-08-10 14:06:19 -04001647 if (close) {
1648 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001649 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001650 }
Chris Mason6702ed42007-08-07 16:15:09 -04001651
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001652 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001653 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001654 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001655 else
1656 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001657 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001658 if (!cur) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001659 cur = read_tree_block(fs_info, blocknr, gen,
1660 parent_level - 1,
1661 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08001662 if (IS_ERR(cur)) {
1663 return PTR_ERR(cur);
1664 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001665 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001666 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001667 }
Chris Mason6b800532007-10-15 16:17:34 -04001668 } else if (!uptodate) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001669 err = btrfs_read_buffer(cur, gen,
1670 parent_level - 1,&first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001671 if (err) {
1672 free_extent_buffer(cur);
1673 return err;
1674 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001675 }
Chris Mason6702ed42007-08-07 16:15:09 -04001676 }
Chris Masone9d0b132007-08-10 14:06:19 -04001677 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001678 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001679
Chris Masone7a84562008-06-25 16:01:31 -04001680 btrfs_tree_lock(cur);
David Sterba8bead252018-04-04 02:03:48 +02001681 btrfs_set_lock_blocking_write(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001682 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001683 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001684 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001685 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001686 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001687 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001688 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001689 break;
Yan252c38f2007-08-29 09:11:44 -04001690 }
Chris Masone7a84562008-06-25 16:01:31 -04001691 search_start = cur->start;
1692 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001693 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001694 btrfs_tree_unlock(cur);
1695 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001696 }
1697 return err;
1698}
1699
Chris Mason74123bd2007-02-02 11:05:29 -05001700/*
Chris Mason5f39d392007-10-15 16:14:19 -04001701 * search for key in the extent_buffer. The items start at offset p,
1702 * and they are item_size apart. There are 'max' items in p.
1703 *
Chris Mason74123bd2007-02-02 11:05:29 -05001704 * the slot in the array is returned via slot, and it points to
1705 * the place where you would insert key if it is not found in
1706 * the array.
1707 *
1708 * slot may point to max if the key is bigger than all of the keys
1709 */
Chris Masone02119d2008-09-05 16:13:11 -04001710static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001711 unsigned long p, int item_size,
1712 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001713 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001714{
1715 int low = 0;
1716 int high = max;
1717 int mid;
1718 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001719 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001720 struct btrfs_disk_key unaligned;
1721 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001722 char *kaddr = NULL;
1723 unsigned long map_start = 0;
1724 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001725 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001726
Liu Bo5e24e9a2016-06-23 16:32:45 -07001727 if (low > high) {
1728 btrfs_err(eb->fs_info,
1729 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1730 __func__, low, high, eb->start,
1731 btrfs_header_owner(eb), btrfs_header_level(eb));
1732 return -EINVAL;
1733 }
1734
Chris Masond3977122009-01-05 21:25:51 -05001735 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001737 offset = p + mid * item_size;
1738
Chris Masona6591712011-07-19 12:04:14 -04001739 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001740 (offset + sizeof(struct btrfs_disk_key)) >
1741 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001742
1743 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001744 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001745 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001746
Chris Mason479965d2007-10-15 16:14:27 -04001747 if (!err) {
1748 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1749 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001750 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001751 read_extent_buffer(eb, &unaligned,
1752 offset, sizeof(unaligned));
1753 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001754 } else {
1755 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001756 }
1757
Chris Mason5f39d392007-10-15 16:14:19 -04001758 } else {
1759 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1760 map_start);
1761 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001762 ret = comp_keys(tmp, key);
1763
1764 if (ret < 0)
1765 low = mid + 1;
1766 else if (ret > 0)
1767 high = mid;
1768 else {
1769 *slot = mid;
1770 return 0;
1771 }
1772 }
1773 *slot = low;
1774 return 1;
1775}
1776
Chris Mason97571fd2007-02-24 13:39:08 -05001777/*
1778 * simple bin_search frontend that does the right thing for
1779 * leaves vs nodes
1780 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001781int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1782 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001783{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001784 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001785 return generic_bin_search(eb,
1786 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001787 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001788 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001789 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001790 else
Chris Mason5f39d392007-10-15 16:14:19 -04001791 return generic_bin_search(eb,
1792 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001793 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001794 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001795 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001796}
1797
Yan, Zhengf0486c62010-05-16 10:46:25 -04001798static void root_add_used(struct btrfs_root *root, u32 size)
1799{
1800 spin_lock(&root->accounting_lock);
1801 btrfs_set_root_used(&root->root_item,
1802 btrfs_root_used(&root->root_item) + size);
1803 spin_unlock(&root->accounting_lock);
1804}
1805
1806static void root_sub_used(struct btrfs_root *root, u32 size)
1807{
1808 spin_lock(&root->accounting_lock);
1809 btrfs_set_root_used(&root->root_item,
1810 btrfs_root_used(&root->root_item) - size);
1811 spin_unlock(&root->accounting_lock);
1812}
1813
Chris Masond352ac62008-09-29 15:18:18 -04001814/* given a node and slot number, this reads the blocks it points to. The
1815 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001816 */
David Sterba4b231ae2019-08-21 19:16:27 +02001817struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1818 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001819{
Chris Masonca7a79a2008-05-12 12:59:19 -04001820 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001821 struct extent_buffer *eb;
Qu Wenruo581c1762018-03-29 09:08:11 +08001822 struct btrfs_key first_key;
Josef Bacik416bc652013-04-23 14:17:42 -04001823
Liu Bofb770ae2016-07-05 12:10:14 -07001824 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1825 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001826
1827 BUG_ON(level == 0);
1828
Qu Wenruo581c1762018-03-29 09:08:11 +08001829 btrfs_node_key_to_cpu(parent, &first_key, slot);
David Sterbad0d20b02019-03-20 14:54:01 +01001830 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
Qu Wenruo581c1762018-03-29 09:08:11 +08001831 btrfs_node_ptr_generation(parent, slot),
1832 level - 1, &first_key);
Liu Bofb770ae2016-07-05 12:10:14 -07001833 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1834 free_extent_buffer(eb);
1835 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001836 }
1837
1838 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001839}
1840
Chris Masond352ac62008-09-29 15:18:18 -04001841/*
1842 * node level balancing, used to make sure nodes are in proper order for
1843 * item deletion. We balance from the top down, so we have to make sure
1844 * that a deletion won't leave an node completely empty later on.
1845 */
Chris Masone02119d2008-09-05 16:13:11 -04001846static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001847 struct btrfs_root *root,
1848 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001849{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001850 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001851 struct extent_buffer *right = NULL;
1852 struct extent_buffer *mid;
1853 struct extent_buffer *left = NULL;
1854 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001855 int ret = 0;
1856 int wret;
1857 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001858 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001859 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001860
Liu Bo98e6b1e2018-09-12 06:06:23 +08001861 ASSERT(level > 0);
Chris Masonbb803952007-03-01 12:04:21 -05001862
Chris Mason5f39d392007-10-15 16:14:19 -04001863 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001864
Chris Masonbd681512011-07-16 15:23:14 -04001865 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1866 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001867 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1868
Chris Mason1d4f8a02007-03-13 09:28:32 -04001869 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001870
Li Zefana05a9bb2011-09-06 16:55:34 +08001871 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001872 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001873 pslot = path->slots[level + 1];
1874 }
Chris Masonbb803952007-03-01 12:04:21 -05001875
Chris Mason40689472007-03-17 14:29:23 -04001876 /*
1877 * deal with the case where there is only one pointer in the root
1878 * by promoting the node below to a root
1879 */
Chris Mason5f39d392007-10-15 16:14:19 -04001880 if (!parent) {
1881 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001882
Chris Mason5f39d392007-10-15 16:14:19 -04001883 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001884 return 0;
1885
1886 /* promote the child to a root */
David Sterba4b231ae2019-08-21 19:16:27 +02001887 child = btrfs_read_node_slot(mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001888 if (IS_ERR(child)) {
1889 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001890 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001891 goto enospc;
1892 }
1893
Chris Mason925baed2008-06-25 16:01:30 -04001894 btrfs_tree_lock(child);
David Sterba8bead252018-04-04 02:03:48 +02001895 btrfs_set_lock_blocking_write(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001896 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001897 if (ret) {
1898 btrfs_tree_unlock(child);
1899 free_extent_buffer(child);
1900 goto enospc;
1901 }
Yan2f375ab2008-02-01 14:58:07 -05001902
David Sterbad9d19a02018-03-05 16:35:29 +01001903 ret = tree_mod_log_insert_root(root->node, child, 1);
1904 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001905 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001906
Chris Mason0b86a832008-03-24 15:01:56 -04001907 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001908 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001909
Chris Mason925baed2008-06-25 16:01:30 -04001910 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001911 path->nodes[level] = NULL;
David Sterba6a884d7d2019-03-20 14:30:02 +01001912 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001913 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001914 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001915 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001916
1917 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001918 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001919 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001920 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001921 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001922 }
Chris Mason5f39d392007-10-15 16:14:19 -04001923 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001924 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001925 return 0;
1926
David Sterba4b231ae2019-08-21 19:16:27 +02001927 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001928 if (IS_ERR(left))
1929 left = NULL;
1930
Chris Mason5f39d392007-10-15 16:14:19 -04001931 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001932 btrfs_tree_lock(left);
David Sterba8bead252018-04-04 02:03:48 +02001933 btrfs_set_lock_blocking_write(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001934 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001935 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001936 if (wret) {
1937 ret = wret;
1938 goto enospc;
1939 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001940 }
Liu Bofb770ae2016-07-05 12:10:14 -07001941
David Sterba4b231ae2019-08-21 19:16:27 +02001942 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001943 if (IS_ERR(right))
1944 right = NULL;
1945
Chris Mason5f39d392007-10-15 16:14:19 -04001946 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001947 btrfs_tree_lock(right);
David Sterba8bead252018-04-04 02:03:48 +02001948 btrfs_set_lock_blocking_write(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001950 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001951 if (wret) {
1952 ret = wret;
1953 goto enospc;
1954 }
1955 }
1956
1957 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001958 if (left) {
1959 orig_slot += btrfs_header_nritems(left);
David Sterbad30a6682019-03-20 14:16:45 +01001960 wret = push_node_left(trans, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001961 if (wret < 0)
1962 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001963 }
Chris Mason79f95c82007-03-01 15:16:26 -05001964
1965 /*
1966 * then try to empty the right most buffer into the middle
1967 */
Chris Mason5f39d392007-10-15 16:14:19 -04001968 if (right) {
David Sterbad30a6682019-03-20 14:16:45 +01001969 wret = push_node_left(trans, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001970 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001971 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001972 if (btrfs_header_nritems(right) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001973 btrfs_clean_tree_block(right);
Chris Mason925baed2008-06-25 16:01:30 -04001974 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001975 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001976 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001977 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001978 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001979 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001980 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001981 struct btrfs_disk_key right_key;
1982 btrfs_node_key(right, &right_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001983 ret = tree_mod_log_insert_key(parent, pslot + 1,
1984 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1985 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001986 btrfs_set_node_key(parent, &right_key, pslot + 1);
1987 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001988 }
1989 }
Chris Mason5f39d392007-10-15 16:14:19 -04001990 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001991 /*
1992 * we're not allowed to leave a node with one item in the
1993 * tree during a delete. A deletion from lower in the tree
1994 * could try to delete the only pointer in this node.
1995 * So, pull some keys from the left.
1996 * There has to be a left pointer at this point because
1997 * otherwise we would have pulled some pointers from the
1998 * right
1999 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002000 if (!left) {
2001 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002002 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07002003 goto enospc;
2004 }
David Sterba55d32ed2019-03-20 14:18:06 +01002005 wret = balance_node_right(trans, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002006 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002007 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002008 goto enospc;
2009 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002010 if (wret == 1) {
David Sterbad30a6682019-03-20 14:16:45 +01002011 wret = push_node_left(trans, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04002012 if (wret < 0)
2013 ret = wret;
2014 }
Chris Mason79f95c82007-03-01 15:16:26 -05002015 BUG_ON(wret == 1);
2016 }
Chris Mason5f39d392007-10-15 16:14:19 -04002017 if (btrfs_header_nritems(mid) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01002018 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002019 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002020 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002021 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002022 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002023 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002024 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002025 } else {
2026 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002027 struct btrfs_disk_key mid_key;
2028 btrfs_node_key(mid, &mid_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002029 ret = tree_mod_log_insert_key(parent, pslot,
2030 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2031 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002032 btrfs_set_node_key(parent, &mid_key, pslot);
2033 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002034 }
Chris Masonbb803952007-03-01 12:04:21 -05002035
Chris Mason79f95c82007-03-01 15:16:26 -05002036 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002037 if (left) {
2038 if (btrfs_header_nritems(left) > orig_slot) {
2039 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002040 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002041 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002042 path->slots[level + 1] -= 1;
2043 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002044 if (mid) {
2045 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002046 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002047 }
Chris Masonbb803952007-03-01 12:04:21 -05002048 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002049 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002050 path->slots[level] = orig_slot;
2051 }
2052 }
Chris Mason79f95c82007-03-01 15:16:26 -05002053 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002054 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002055 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002056 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002057enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002058 if (right) {
2059 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002060 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002061 }
2062 if (left) {
2063 if (path->nodes[level] != left)
2064 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002065 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002066 }
Chris Masonbb803952007-03-01 12:04:21 -05002067 return ret;
2068}
2069
Chris Masond352ac62008-09-29 15:18:18 -04002070/* Node balancing for insertion. Here we only split or push nodes around
2071 * when they are completely full. This is also done top down, so we
2072 * have to be pessimistic.
2073 */
Chris Masond3977122009-01-05 21:25:51 -05002074static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002075 struct btrfs_root *root,
2076 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002077{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002078 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002079 struct extent_buffer *right = NULL;
2080 struct extent_buffer *mid;
2081 struct extent_buffer *left = NULL;
2082 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002083 int ret = 0;
2084 int wret;
2085 int pslot;
2086 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002087
2088 if (level == 0)
2089 return 1;
2090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002092 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002093
Li Zefana05a9bb2011-09-06 16:55:34 +08002094 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002095 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002096 pslot = path->slots[level + 1];
2097 }
Chris Masone66f7092007-04-20 13:16:02 -04002098
Chris Mason5f39d392007-10-15 16:14:19 -04002099 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002100 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002101
David Sterba4b231ae2019-08-21 19:16:27 +02002102 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002103 if (IS_ERR(left))
2104 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002105
2106 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002107 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002108 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002109
2110 btrfs_tree_lock(left);
David Sterba8bead252018-04-04 02:03:48 +02002111 btrfs_set_lock_blocking_write(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002112
Chris Mason5f39d392007-10-15 16:14:19 -04002113 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002114 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002115 wret = 1;
2116 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002117 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002118 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002119 if (ret)
2120 wret = 1;
2121 else {
David Sterbad30a6682019-03-20 14:16:45 +01002122 wret = push_node_left(trans, left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002123 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002124 }
Chris Masone66f7092007-04-20 13:16:02 -04002125 if (wret < 0)
2126 ret = wret;
2127 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002128 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002129 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_node_key(mid, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002131 ret = tree_mod_log_insert_key(parent, pslot,
2132 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2133 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002134 btrfs_set_node_key(parent, &disk_key, pslot);
2135 btrfs_mark_buffer_dirty(parent);
2136 if (btrfs_header_nritems(left) > orig_slot) {
2137 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002138 path->slots[level + 1] -= 1;
2139 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002140 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002141 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002142 } else {
2143 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002144 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002145 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002146 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002147 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002148 }
Chris Masone66f7092007-04-20 13:16:02 -04002149 return 0;
2150 }
Chris Mason925baed2008-06-25 16:01:30 -04002151 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002152 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002153 }
David Sterba4b231ae2019-08-21 19:16:27 +02002154 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002155 if (IS_ERR(right))
2156 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002157
2158 /*
2159 * then try to empty the right most buffer into the middle
2160 */
Chris Mason5f39d392007-10-15 16:14:19 -04002161 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002162 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002163
Chris Mason925baed2008-06-25 16:01:30 -04002164 btrfs_tree_lock(right);
David Sterba8bead252018-04-04 02:03:48 +02002165 btrfs_set_lock_blocking_write(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002166
Chris Mason5f39d392007-10-15 16:14:19 -04002167 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002168 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002169 wret = 1;
2170 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002171 ret = btrfs_cow_block(trans, root, right,
2172 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002173 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002174 if (ret)
2175 wret = 1;
2176 else {
David Sterba55d32ed2019-03-20 14:18:06 +01002177 wret = balance_node_right(trans, right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002178 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002179 }
Chris Masone66f7092007-04-20 13:16:02 -04002180 if (wret < 0)
2181 ret = wret;
2182 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002183 struct btrfs_disk_key disk_key;
2184
2185 btrfs_node_key(right, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002186 ret = tree_mod_log_insert_key(parent, pslot + 1,
2187 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2188 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2190 btrfs_mark_buffer_dirty(parent);
2191
2192 if (btrfs_header_nritems(mid) <= orig_slot) {
2193 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002194 path->slots[level + 1] += 1;
2195 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002196 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002197 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002198 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002199 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002200 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002201 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002202 }
Chris Masone66f7092007-04-20 13:16:02 -04002203 return 0;
2204 }
Chris Mason925baed2008-06-25 16:01:30 -04002205 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002206 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002207 }
Chris Masone66f7092007-04-20 13:16:02 -04002208 return 1;
2209}
2210
Chris Mason74123bd2007-02-02 11:05:29 -05002211/*
Chris Masond352ac62008-09-29 15:18:18 -04002212 * readahead one full node of leaves, finding things that are close
2213 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002214 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002215static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002216 struct btrfs_path *path,
2217 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002218{
Chris Mason5f39d392007-10-15 16:14:19 -04002219 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002220 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002221 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002222 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002223 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002224 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002225 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002226 u32 nr;
2227 u32 blocksize;
2228 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002229
Chris Masona6b6e752007-10-15 16:22:39 -04002230 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002231 return;
2232
Chris Mason6702ed42007-08-07 16:15:09 -04002233 if (!path->nodes[level])
2234 return;
2235
Chris Mason5f39d392007-10-15 16:14:19 -04002236 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002237
Chris Mason3c69fae2007-08-07 15:52:22 -04002238 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002239 blocksize = fs_info->nodesize;
2240 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002241 if (eb) {
2242 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002243 return;
2244 }
2245
Chris Masona7175312009-01-22 09:23:10 -05002246 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002247
Chris Mason5f39d392007-10-15 16:14:19 -04002248 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002249 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002250
Chris Masond3977122009-01-05 21:25:51 -05002251 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002252 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002253 if (nr == 0)
2254 break;
2255 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002256 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002257 nr++;
2258 if (nr >= nritems)
2259 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002260 }
David Sterbae4058b52015-11-27 16:31:35 +01002261 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002262 btrfs_node_key(node, &disk_key, nr);
2263 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2264 break;
2265 }
Chris Mason6b800532007-10-15 16:17:34 -04002266 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002267 if ((search <= target && target - search <= 65536) ||
2268 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002269 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002270 nread += blocksize;
2271 }
2272 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002273 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002274 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002275 }
2276}
Chris Mason925baed2008-06-25 16:01:30 -04002277
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002278static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002279 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002280{
2281 int slot;
2282 int nritems;
2283 struct extent_buffer *parent;
2284 struct extent_buffer *eb;
2285 u64 gen;
2286 u64 block1 = 0;
2287 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002288
Chris Mason8c594ea2009-04-20 15:50:10 -04002289 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002290 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002291 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002292
2293 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002294 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002295
2296 if (slot > 0) {
2297 block1 = btrfs_node_blockptr(parent, slot - 1);
2298 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002299 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002300 /*
2301 * if we get -eagain from btrfs_buffer_uptodate, we
2302 * don't want to return eagain here. That will loop
2303 * forever
2304 */
2305 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002306 block1 = 0;
2307 free_extent_buffer(eb);
2308 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002309 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002310 block2 = btrfs_node_blockptr(parent, slot + 1);
2311 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002312 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002313 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002314 block2 = 0;
2315 free_extent_buffer(eb);
2316 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002317
Josef Bacik0b088512013-06-17 14:23:02 -04002318 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002319 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002320 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002321 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002322}
2323
2324
2325/*
Chris Masond3977122009-01-05 21:25:51 -05002326 * when we walk down the tree, it is usually safe to unlock the higher layers
2327 * in the tree. The exceptions are when our path goes through slot 0, because
2328 * operations on the tree might require changing key pointers higher up in the
2329 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002330 *
Chris Masond3977122009-01-05 21:25:51 -05002331 * callers might also have set path->keep_locks, which tells this code to keep
2332 * the lock if the path points to the last slot in the block. This is part of
2333 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002334 *
Chris Masond3977122009-01-05 21:25:51 -05002335 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2336 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002337 */
Chris Masone02119d2008-09-05 16:13:11 -04002338static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002339 int lowest_unlock, int min_write_lock_level,
2340 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002341{
2342 int i;
2343 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002344 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002345 struct extent_buffer *t;
2346
2347 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2348 if (!path->nodes[i])
2349 break;
2350 if (!path->locks[i])
2351 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002352 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002353 skip_level = i + 1;
2354 continue;
2355 }
Chris Mason051e1b92008-06-25 16:01:30 -04002356 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002357 u32 nritems;
2358 t = path->nodes[i];
2359 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002360 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002361 skip_level = i + 1;
2362 continue;
2363 }
2364 }
Chris Mason051e1b92008-06-25 16:01:30 -04002365 if (skip_level < i && i >= lowest_unlock)
2366 no_skips = 1;
2367
Chris Mason925baed2008-06-25 16:01:30 -04002368 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08002369 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04002370 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002371 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002372 if (write_lock_level &&
2373 i > min_write_lock_level &&
2374 i <= *write_lock_level) {
2375 *write_lock_level = i - 1;
2376 }
Chris Mason925baed2008-06-25 16:01:30 -04002377 }
2378 }
2379}
2380
Chris Mason3c69fae2007-08-07 15:52:22 -04002381/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002382 * This releases any locks held in the path starting at level and
2383 * going all the way up to the root.
2384 *
2385 * btrfs_search_slot will keep the lock held on higher nodes in a few
2386 * corner cases, such as COW of the block at slot zero in the node. This
2387 * ignores those rules, and it should only be called when there are no
2388 * more updates to be done higher up in the tree.
2389 */
2390noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2391{
2392 int i;
2393
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002394 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002395 return;
2396
2397 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2398 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002399 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002400 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002401 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002402 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002403 path->locks[i] = 0;
2404 }
2405}
2406
2407/*
Chris Masonc8c42862009-04-03 10:14:18 -04002408 * helper function for btrfs_search_slot. The goal is to find a block
2409 * in cache without setting the path to blocking. If we find the block
2410 * we return zero and the path is unchanged.
2411 *
2412 * If we can't find the block, we set the path blocking and do some
2413 * reada. -EAGAIN is returned and the search must be repeated.
2414 */
2415static int
Liu Bod07b8522017-01-30 12:23:42 -08002416read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2417 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002418 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002419{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002420 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002421 u64 blocknr;
2422 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002423 struct extent_buffer *b = *eb_ret;
2424 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08002425 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04002426 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08002427 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04002428
2429 blocknr = btrfs_node_blockptr(b, slot);
2430 gen = btrfs_node_ptr_generation(b, slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08002431 parent_level = btrfs_header_level(b);
2432 btrfs_node_key_to_cpu(b, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002433
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002434 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002435 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002436 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002437 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Qu Wenruo448de472019-03-12 17:10:40 +08002438 /*
2439 * Do extra check for first_key, eb can be stale due to
2440 * being cached, read from scrub, or have multiple
2441 * parents (shared tree blocks).
2442 */
David Sterbae064d5e2019-03-20 14:58:13 +01002443 if (btrfs_verify_level_key(tmp,
Qu Wenruo448de472019-03-12 17:10:40 +08002444 parent_level - 1, &first_key, gen)) {
2445 free_extent_buffer(tmp);
2446 return -EUCLEAN;
2447 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002448 *eb_ret = tmp;
2449 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002450 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002451
2452 /* the pages were up to date, but we failed
2453 * the generation number check. Do a full
2454 * read for the generation number that is correct.
2455 * We must do this without dropping locks so
2456 * we can trust our generation number
2457 */
2458 btrfs_set_path_blocking(p);
2459
2460 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08002461 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04002462 if (!ret) {
2463 *eb_ret = tmp;
2464 return 0;
2465 }
2466 free_extent_buffer(tmp);
2467 btrfs_release_path(p);
2468 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002469 }
2470
2471 /*
2472 * reduce lock contention at high levels
2473 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002474 * we read. Don't release the lock on the current
2475 * level because we need to walk this node to figure
2476 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002477 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002478 btrfs_unlock_up_safe(p, level + 1);
2479 btrfs_set_path_blocking(p);
2480
David Sterbae4058b52015-11-27 16:31:35 +01002481 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002482 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002483
Chris Mason76a05b32009-05-14 13:24:30 -04002484 ret = -EAGAIN;
Liu Bo02a33072018-05-16 01:37:36 +08002485 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
Qu Wenruo581c1762018-03-29 09:08:11 +08002486 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002487 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002488 /*
2489 * If the read above didn't mark this buffer up to date,
2490 * it will never end up being up to date. Set ret to EIO now
2491 * and give up so that our caller doesn't loop forever
2492 * on our EAGAINs.
2493 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08002494 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04002495 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002496 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002497 } else {
2498 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002499 }
Liu Bo02a33072018-05-16 01:37:36 +08002500
2501 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002502 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002503}
2504
2505/*
2506 * helper function for btrfs_search_slot. This does all of the checks
2507 * for node-level blocks and does any balancing required based on
2508 * the ins_len.
2509 *
2510 * If no extra work was required, zero is returned. If we had to
2511 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2512 * start over
2513 */
2514static int
2515setup_nodes_for_search(struct btrfs_trans_handle *trans,
2516 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002517 struct extent_buffer *b, int level, int ins_len,
2518 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002519{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002520 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002521 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002522
Chris Masonc8c42862009-04-03 10:14:18 -04002523 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002524 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002525 int sret;
2526
Chris Masonbd681512011-07-16 15:23:14 -04002527 if (*write_lock_level < level + 1) {
2528 *write_lock_level = level + 1;
2529 btrfs_release_path(p);
2530 goto again;
2531 }
2532
Chris Masonc8c42862009-04-03 10:14:18 -04002533 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002534 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002535 sret = split_node(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002536
2537 BUG_ON(sret > 0);
2538 if (sret) {
2539 ret = sret;
2540 goto done;
2541 }
2542 b = p->nodes[level];
2543 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002544 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002545 int sret;
2546
Chris Masonbd681512011-07-16 15:23:14 -04002547 if (*write_lock_level < level + 1) {
2548 *write_lock_level = level + 1;
2549 btrfs_release_path(p);
2550 goto again;
2551 }
2552
Chris Masonc8c42862009-04-03 10:14:18 -04002553 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002554 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002555 sret = balance_level(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002556
2557 if (sret) {
2558 ret = sret;
2559 goto done;
2560 }
2561 b = p->nodes[level];
2562 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002563 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002564 goto again;
2565 }
2566 BUG_ON(btrfs_header_nritems(b) == 1);
2567 }
2568 return 0;
2569
2570again:
2571 ret = -EAGAIN;
2572done:
2573 return ret;
2574}
2575
Omar Sandoval310712b2017-01-17 23:24:37 -08002576static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002577 int level, int *prev_cmp, int *slot)
2578{
2579 if (*prev_cmp != 0) {
Nikolay Borisova74b35e2017-12-08 16:27:43 +02002580 *prev_cmp = btrfs_bin_search(b, key, level, slot);
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002581 return *prev_cmp;
2582 }
2583
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002584 *slot = 0;
2585
2586 return 0;
2587}
2588
David Sterba381cf652015-01-02 18:45:16 +01002589int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002590 u64 iobjectid, u64 ioff, u8 key_type,
2591 struct btrfs_key *found_key)
2592{
2593 int ret;
2594 struct btrfs_key key;
2595 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002596
2597 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002598 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002599
2600 key.type = key_type;
2601 key.objectid = iobjectid;
2602 key.offset = ioff;
2603
2604 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002605 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002606 return ret;
2607
2608 eb = path->nodes[0];
2609 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2610 ret = btrfs_next_leaf(fs_root, path);
2611 if (ret)
2612 return ret;
2613 eb = path->nodes[0];
2614 }
2615
2616 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2617 if (found_key->type != key.type ||
2618 found_key->objectid != key.objectid)
2619 return 1;
2620
2621 return 0;
2622}
2623
Liu Bo1fc28d82018-05-18 11:00:21 +08002624static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2625 struct btrfs_path *p,
2626 int write_lock_level)
2627{
2628 struct btrfs_fs_info *fs_info = root->fs_info;
2629 struct extent_buffer *b;
2630 int root_lock;
2631 int level = 0;
2632
2633 /* We try very hard to do read locks on the root */
2634 root_lock = BTRFS_READ_LOCK;
2635
2636 if (p->search_commit_root) {
Filipe Mananabe6821f2018-12-11 10:19:45 +00002637 /*
2638 * The commit roots are read only so we always do read locks,
2639 * and we always must hold the commit_root_sem when doing
2640 * searches on them, the only exception is send where we don't
2641 * want to block transaction commits for a long time, so
2642 * we need to clone the commit root in order to avoid races
2643 * with transaction commits that create a snapshot of one of
2644 * the roots used by a send operation.
2645 */
2646 if (p->need_commit_sem) {
Liu Bo1fc28d82018-05-18 11:00:21 +08002647 down_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002648 b = btrfs_clone_extent_buffer(root->commit_root);
Liu Bo1fc28d82018-05-18 11:00:21 +08002649 up_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002650 if (!b)
2651 return ERR_PTR(-ENOMEM);
2652
2653 } else {
2654 b = root->commit_root;
2655 extent_buffer_get(b);
2656 }
2657 level = btrfs_header_level(b);
Liu Bof9ddfd02018-05-29 21:27:06 +08002658 /*
2659 * Ensure that all callers have set skip_locking when
2660 * p->search_commit_root = 1.
2661 */
2662 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08002663
2664 goto out;
2665 }
2666
2667 if (p->skip_locking) {
2668 b = btrfs_root_node(root);
2669 level = btrfs_header_level(b);
2670 goto out;
2671 }
2672
2673 /*
Liu Bo662c6532018-05-18 11:00:23 +08002674 * If the level is set to maximum, we can skip trying to get the read
2675 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08002676 */
Liu Bo662c6532018-05-18 11:00:23 +08002677 if (write_lock_level < BTRFS_MAX_LEVEL) {
2678 /*
2679 * We don't know the level of the root node until we actually
2680 * have it read locked
2681 */
2682 b = btrfs_read_lock_root_node(root);
2683 level = btrfs_header_level(b);
2684 if (level > write_lock_level)
2685 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08002686
Liu Bo662c6532018-05-18 11:00:23 +08002687 /* Whoops, must trade for write lock */
2688 btrfs_tree_read_unlock(b);
2689 free_extent_buffer(b);
2690 }
2691
Liu Bo1fc28d82018-05-18 11:00:21 +08002692 b = btrfs_lock_root_node(root);
2693 root_lock = BTRFS_WRITE_LOCK;
2694
2695 /* The level might have changed, check again */
2696 level = btrfs_header_level(b);
2697
2698out:
2699 p->nodes[level] = b;
2700 if (!p->skip_locking)
2701 p->locks[level] = root_lock;
2702 /*
2703 * Callers are responsible for dropping b's references.
2704 */
2705 return b;
2706}
2707
2708
Chris Masonc8c42862009-04-03 10:14:18 -04002709/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002710 * btrfs_search_slot - look for a key in a tree and perform necessary
2711 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002712 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002713 * @trans: Handle of transaction, used when modifying the tree
2714 * @p: Holds all btree nodes along the search path
2715 * @root: The root node of the tree
2716 * @key: The key we are looking for
2717 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2718 * deletions it's -1. 0 for plain searches
2719 * @cow: boolean should CoW operations be performed. Must always be 1
2720 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002721 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002722 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2723 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2724 *
2725 * If @key is found, 0 is returned and you can find the item in the leaf level
2726 * of the path (level 0)
2727 *
2728 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2729 * points to the slot where it should be inserted
2730 *
2731 * If an error is encountered while searching the tree a negative error number
2732 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002733 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002734int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2735 const struct btrfs_key *key, struct btrfs_path *p,
2736 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002737{
Chris Mason5f39d392007-10-15 16:14:19 -04002738 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002739 int slot;
2740 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002741 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002742 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002743 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002744 /* everything at write_lock_level or lower must be write locked */
2745 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002746 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002747 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002748 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002749
Chris Mason6702ed42007-08-07 16:15:09 -04002750 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002751 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002752 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002753 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002754
Chris Masonbd681512011-07-16 15:23:14 -04002755 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002756 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002757
Chris Masonbd681512011-07-16 15:23:14 -04002758 /* when we are removing items, we might have to go up to level
2759 * two as we update tree pointers Make sure we keep write
2760 * for those levels as well
2761 */
2762 write_lock_level = 2;
2763 } else if (ins_len > 0) {
2764 /*
2765 * for inserting items, make sure we have a write lock on
2766 * level 1 so we can update keys
2767 */
2768 write_lock_level = 1;
2769 }
2770
2771 if (!cow)
2772 write_lock_level = -1;
2773
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002774 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002775 write_lock_level = BTRFS_MAX_LEVEL;
2776
Chris Masonf7c79f32012-03-19 15:54:38 -04002777 min_write_lock_level = write_lock_level;
2778
Chris Masonbb803952007-03-01 12:04:21 -05002779again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002780 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08002781 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002782 if (IS_ERR(b)) {
2783 ret = PTR_ERR(b);
2784 goto done;
2785 }
Chris Mason925baed2008-06-25 16:01:30 -04002786
Chris Masoneb60cea2007-02-02 09:18:22 -05002787 while (b) {
Qu Wenruof624d972019-09-10 15:40:17 +08002788 int dec = 0;
2789
Chris Mason5f39d392007-10-15 16:14:19 -04002790 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002791
2792 /*
2793 * setup the path here so we can release it under lock
2794 * contention with the cow code
2795 */
Chris Mason02217ed2007-03-02 16:08:05 -05002796 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002797 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2798
Chris Masonc8c42862009-04-03 10:14:18 -04002799 /*
2800 * if we don't really need to cow this block
2801 * then we don't want to set the path blocking,
2802 * so we test it here
2803 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002804 if (!should_cow_block(trans, root, b)) {
2805 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002806 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002807 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002808
Chris Masonbd681512011-07-16 15:23:14 -04002809 /*
2810 * must have write locks on this node and the
2811 * parent
2812 */
Josef Bacik5124e002012-11-07 13:44:13 -05002813 if (level > write_lock_level ||
2814 (level + 1 > write_lock_level &&
2815 level + 1 < BTRFS_MAX_LEVEL &&
2816 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002817 write_lock_level = level + 1;
2818 btrfs_release_path(p);
2819 goto again;
2820 }
2821
Filipe Manana160f4082014-07-28 19:37:17 +01002822 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002823 if (last_level)
2824 err = btrfs_cow_block(trans, root, b, NULL, 0,
2825 &b);
2826 else
2827 err = btrfs_cow_block(trans, root, b,
2828 p->nodes[level + 1],
2829 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002830 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002831 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002832 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002833 }
Chris Mason02217ed2007-03-02 16:08:05 -05002834 }
Chris Mason65b51a02008-08-01 15:11:20 -04002835cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002836 p->nodes[level] = b;
Liu Bo52398342018-08-22 05:54:37 +08002837 /*
2838 * Leave path with blocking locks to avoid massive
2839 * lock context switch, this is made on purpose.
2840 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002841
2842 /*
2843 * we have a lock on b and as long as we aren't changing
2844 * the tree, there is no way to for the items in b to change.
2845 * It is safe to drop the lock on our parent before we
2846 * go through the expensive btree search on b.
2847 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002848 * If we're inserting or deleting (ins_len != 0), then we might
2849 * be changing slot zero, which may require changing the parent.
2850 * So, we can't drop the lock until after we know which slot
2851 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002852 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002853 if (!ins_len && !p->keep_locks) {
2854 int u = level + 1;
2855
2856 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2857 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2858 p->locks[u] = 0;
2859 }
2860 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002861
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002862 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002863 if (ret < 0)
2864 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002865
Qu Wenruof624d972019-09-10 15:40:17 +08002866 if (level == 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002867 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002868 if (ins_len > 0 &&
David Sterbae902baa2019-03-20 14:36:46 +01002869 btrfs_leaf_free_space(b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002870 if (write_lock_level < 1) {
2871 write_lock_level = 1;
2872 btrfs_release_path(p);
2873 goto again;
2874 }
2875
Chris Masonb4ce94d2009-02-04 09:25:08 -05002876 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002877 err = split_leaf(trans, root, key,
2878 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002879
Yan Zheng33c66f42009-07-22 09:59:00 -04002880 BUG_ON(err > 0);
2881 if (err) {
2882 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002883 goto done;
2884 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002885 }
Chris Mason459931e2008-12-10 09:10:46 -05002886 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002887 unlock_up(p, level, lowest_unlock,
Liu Bo4b6f8e92018-08-14 10:46:53 +08002888 min_write_lock_level, NULL);
Chris Mason65b51a02008-08-01 15:11:20 -04002889 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002890 }
Qu Wenruof624d972019-09-10 15:40:17 +08002891 if (ret && slot > 0) {
2892 dec = 1;
2893 slot--;
2894 }
2895 p->slots[level] = slot;
2896 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2897 &write_lock_level);
2898 if (err == -EAGAIN)
2899 goto again;
2900 if (err) {
2901 ret = err;
2902 goto done;
2903 }
2904 b = p->nodes[level];
2905 slot = p->slots[level];
2906
2907 /*
2908 * Slot 0 is special, if we change the key we have to update
2909 * the parent pointer which means we must have a write lock on
2910 * the parent
2911 */
2912 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2913 write_lock_level = level + 1;
2914 btrfs_release_path(p);
2915 goto again;
2916 }
2917
2918 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2919 &write_lock_level);
2920
2921 if (level == lowest_level) {
2922 if (dec)
2923 p->slots[level]++;
2924 goto done;
2925 }
2926
2927 err = read_block_for_search(root, p, &b, level, slot, key);
2928 if (err == -EAGAIN)
2929 goto again;
2930 if (err) {
2931 ret = err;
2932 goto done;
2933 }
2934
2935 if (!p->skip_locking) {
2936 level = btrfs_header_level(b);
2937 if (level <= write_lock_level) {
2938 if (!btrfs_try_tree_write_lock(b)) {
2939 btrfs_set_path_blocking(p);
2940 btrfs_tree_lock(b);
2941 }
2942 p->locks[level] = BTRFS_WRITE_LOCK;
2943 } else {
2944 if (!btrfs_tree_read_lock_atomic(b)) {
2945 btrfs_set_path_blocking(p);
2946 btrfs_tree_read_lock(b);
2947 }
2948 p->locks[level] = BTRFS_READ_LOCK;
2949 }
2950 p->nodes[level] = b;
2951 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002952 }
Chris Mason65b51a02008-08-01 15:11:20 -04002953 ret = 1;
2954done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002955 /*
2956 * we don't really know what they plan on doing with the path
2957 * from here on, so for now just mark it as blocking
2958 */
Chris Masonb9473432009-03-13 11:00:37 -04002959 if (!p->leave_spinning)
2960 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002961 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002962 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002963 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002964}
2965
Chris Mason74123bd2007-02-02 11:05:29 -05002966/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002967 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2968 * current state of the tree together with the operations recorded in the tree
2969 * modification log to search for the key in a previous version of this tree, as
2970 * denoted by the time_seq parameter.
2971 *
2972 * Naturally, there is no support for insert, delete or cow operations.
2973 *
2974 * The resulting path and return value will be set up as if we called
2975 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2976 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002977int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002978 struct btrfs_path *p, u64 time_seq)
2979{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002980 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002981 struct extent_buffer *b;
2982 int slot;
2983 int ret;
2984 int err;
2985 int level;
2986 int lowest_unlock = 1;
2987 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002988 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002989
2990 lowest_level = p->lowest_level;
2991 WARN_ON(p->nodes[0] != NULL);
2992
2993 if (p->search_commit_root) {
2994 BUG_ON(time_seq);
2995 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2996 }
2997
2998again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002999 b = get_old_root(root, time_seq);
Nikolay Borisov315bed42018-09-13 11:35:10 +03003000 if (!b) {
3001 ret = -EIO;
3002 goto done;
3003 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003004 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003005 p->locks[level] = BTRFS_READ_LOCK;
3006
3007 while (b) {
Qu Wenruoabe93392019-09-10 15:40:18 +08003008 int dec = 0;
3009
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003010 level = btrfs_header_level(b);
3011 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003012
3013 /*
3014 * we have a lock on b and as long as we aren't changing
3015 * the tree, there is no way to for the items in b to change.
3016 * It is safe to drop the lock on our parent before we
3017 * go through the expensive btree search on b.
3018 */
3019 btrfs_unlock_up_safe(p, level + 1);
3020
Josef Bacikd4b40872013-09-24 14:09:34 -04003021 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04003022 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04003023 * time.
3024 */
3025 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01003026 ret = key_search(b, key, level, &prev_cmp, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00003027 if (ret < 0)
3028 goto done;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003029
Qu Wenruoabe93392019-09-10 15:40:18 +08003030 if (level == 0) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003031 p->slots[level] = slot;
3032 unlock_up(p, level, lowest_unlock, 0, NULL);
3033 goto done;
3034 }
Qu Wenruoabe93392019-09-10 15:40:18 +08003035
3036 if (ret && slot > 0) {
3037 dec = 1;
3038 slot--;
3039 }
3040 p->slots[level] = slot;
3041 unlock_up(p, level, lowest_unlock, 0, NULL);
3042
3043 if (level == lowest_level) {
3044 if (dec)
3045 p->slots[level]++;
3046 goto done;
3047 }
3048
3049 err = read_block_for_search(root, p, &b, level, slot, key);
3050 if (err == -EAGAIN)
3051 goto again;
3052 if (err) {
3053 ret = err;
3054 goto done;
3055 }
3056
3057 level = btrfs_header_level(b);
3058 if (!btrfs_tree_read_lock_atomic(b)) {
3059 btrfs_set_path_blocking(p);
3060 btrfs_tree_read_lock(b);
3061 }
3062 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3063 if (!b) {
3064 ret = -ENOMEM;
3065 goto done;
3066 }
3067 p->locks[level] = BTRFS_READ_LOCK;
3068 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003069 }
3070 ret = 1;
3071done:
3072 if (!p->leave_spinning)
3073 btrfs_set_path_blocking(p);
3074 if (ret < 0)
3075 btrfs_release_path(p);
3076
3077 return ret;
3078}
3079
3080/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003081 * helper to use instead of search slot if no exact match is needed but
3082 * instead the next or previous item should be returned.
3083 * When find_higher is true, the next higher item is returned, the next lower
3084 * otherwise.
3085 * When return_any and find_higher are both true, and no higher item is found,
3086 * return the next lower instead.
3087 * When return_any is true and find_higher is false, and no lower item is found,
3088 * return the next higher instead.
3089 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3090 * < 0 on error
3091 */
3092int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003093 const struct btrfs_key *key,
3094 struct btrfs_path *p, int find_higher,
3095 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003096{
3097 int ret;
3098 struct extent_buffer *leaf;
3099
3100again:
3101 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3102 if (ret <= 0)
3103 return ret;
3104 /*
3105 * a return value of 1 means the path is at the position where the
3106 * item should be inserted. Normally this is the next bigger item,
3107 * but in case the previous item is the last in a leaf, path points
3108 * to the first free slot in the previous leaf, i.e. at an invalid
3109 * item.
3110 */
3111 leaf = p->nodes[0];
3112
3113 if (find_higher) {
3114 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3115 ret = btrfs_next_leaf(root, p);
3116 if (ret <= 0)
3117 return ret;
3118 if (!return_any)
3119 return 1;
3120 /*
3121 * no higher item found, return the next
3122 * lower instead
3123 */
3124 return_any = 0;
3125 find_higher = 0;
3126 btrfs_release_path(p);
3127 goto again;
3128 }
3129 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003130 if (p->slots[0] == 0) {
3131 ret = btrfs_prev_leaf(root, p);
3132 if (ret < 0)
3133 return ret;
3134 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003135 leaf = p->nodes[0];
3136 if (p->slots[0] == btrfs_header_nritems(leaf))
3137 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003138 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003139 }
Arne Jansene6793762011-09-13 11:18:10 +02003140 if (!return_any)
3141 return 1;
3142 /*
3143 * no lower item found, return the next
3144 * higher instead
3145 */
3146 return_any = 0;
3147 find_higher = 1;
3148 btrfs_release_path(p);
3149 goto again;
3150 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003151 --p->slots[0];
3152 }
3153 }
3154 return 0;
3155}
3156
3157/*
Chris Mason74123bd2007-02-02 11:05:29 -05003158 * adjust the pointers going up the tree, starting at level
3159 * making sure the right key of each node is points to 'key'.
3160 * This is used after shifting pointers to the left, so it stops
3161 * fixing up pointers when a given leaf/node is not in slot 0 of the
3162 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003163 *
Chris Mason74123bd2007-02-02 11:05:29 -05003164 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003165static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003166 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003167{
3168 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003169 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01003170 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003171
Chris Mason234b63a2007-03-13 10:46:10 -04003172 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003173 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003174
Chris Masoneb60cea2007-02-02 09:18:22 -05003175 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003176 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003177 t = path->nodes[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003178 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3179 GFP_ATOMIC);
3180 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003181 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003182 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003183 if (tslot != 0)
3184 break;
3185 }
3186}
3187
Chris Mason74123bd2007-02-02 11:05:29 -05003188/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003189 * update item key.
3190 *
3191 * This function isn't completely safe. It's the caller's responsibility
3192 * that the new key won't break the order
3193 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003194void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3195 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003196 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003197{
3198 struct btrfs_disk_key disk_key;
3199 struct extent_buffer *eb;
3200 int slot;
3201
3202 eb = path->nodes[0];
3203 slot = path->slots[0];
3204 if (slot > 0) {
3205 btrfs_item_key(eb, &disk_key, slot - 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003206 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3207 btrfs_crit(fs_info,
3208 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3209 slot, btrfs_disk_key_objectid(&disk_key),
3210 btrfs_disk_key_type(&disk_key),
3211 btrfs_disk_key_offset(&disk_key),
3212 new_key->objectid, new_key->type,
3213 new_key->offset);
3214 btrfs_print_leaf(eb);
3215 BUG();
3216 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003217 }
3218 if (slot < btrfs_header_nritems(eb) - 1) {
3219 btrfs_item_key(eb, &disk_key, slot + 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003220 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3221 btrfs_crit(fs_info,
3222 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3223 slot, btrfs_disk_key_objectid(&disk_key),
3224 btrfs_disk_key_type(&disk_key),
3225 btrfs_disk_key_offset(&disk_key),
3226 new_key->objectid, new_key->type,
3227 new_key->offset);
3228 btrfs_print_leaf(eb);
3229 BUG();
3230 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003231 }
3232
3233 btrfs_cpu_key_to_disk(&disk_key, new_key);
3234 btrfs_set_item_key(eb, &disk_key, slot);
3235 btrfs_mark_buffer_dirty(eb);
3236 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003237 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003238}
3239
3240/*
Chris Mason74123bd2007-02-02 11:05:29 -05003241 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003242 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003243 *
3244 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3245 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003246 */
Chris Mason98ed5172008-01-03 10:01:48 -05003247static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003248 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003249 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003250{
David Sterbad30a6682019-03-20 14:16:45 +01003251 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003252 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003253 int src_nritems;
3254 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003255 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003256
Chris Mason5f39d392007-10-15 16:14:19 -04003257 src_nritems = btrfs_header_nritems(src);
3258 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003259 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003260 WARN_ON(btrfs_header_generation(src) != trans->transid);
3261 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003262
Chris Masonbce4eae2008-04-24 14:42:46 -04003263 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003264 return 1;
3265
Chris Masond3977122009-01-05 21:25:51 -05003266 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003267 return 1;
3268
Chris Masonbce4eae2008-04-24 14:42:46 -04003269 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003270 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003271 if (push_items < src_nritems) {
3272 /* leave at least 8 pointers in the node if
3273 * we aren't going to empty it
3274 */
3275 if (src_nritems - push_items < 8) {
3276 if (push_items <= 8)
3277 return 1;
3278 push_items -= 8;
3279 }
3280 }
3281 } else
3282 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003283
David Sterbaed874f02019-03-20 14:22:04 +01003284 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003285 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003286 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003287 return ret;
3288 }
Chris Mason5f39d392007-10-15 16:14:19 -04003289 copy_extent_buffer(dst, src,
3290 btrfs_node_key_ptr_offset(dst_nritems),
3291 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003292 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003293
Chris Masonbb803952007-03-01 12:04:21 -05003294 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003295 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003296 * Don't call tree_mod_log_insert_move here, key removal was
3297 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003298 */
Chris Mason5f39d392007-10-15 16:14:19 -04003299 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3300 btrfs_node_key_ptr_offset(push_items),
3301 (src_nritems - push_items) *
3302 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003303 }
Chris Mason5f39d392007-10-15 16:14:19 -04003304 btrfs_set_header_nritems(src, src_nritems - push_items);
3305 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3306 btrfs_mark_buffer_dirty(src);
3307 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003308
Chris Masonbb803952007-03-01 12:04:21 -05003309 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003310}
3311
Chris Mason97571fd2007-02-24 13:39:08 -05003312/*
Chris Mason79f95c82007-03-01 15:16:26 -05003313 * try to push data from one node into the next node right in the
3314 * tree.
3315 *
3316 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3317 * error, and > 0 if there was no room in the right hand block.
3318 *
3319 * this will only push up to 1/2 the contents of the left node over
3320 */
Chris Mason5f39d392007-10-15 16:14:19 -04003321static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003322 struct extent_buffer *dst,
3323 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003324{
David Sterba55d32ed2019-03-20 14:18:06 +01003325 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason79f95c82007-03-01 15:16:26 -05003326 int push_items = 0;
3327 int max_push;
3328 int src_nritems;
3329 int dst_nritems;
3330 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003331
Chris Mason7bb86312007-12-11 09:25:06 -05003332 WARN_ON(btrfs_header_generation(src) != trans->transid);
3333 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3334
Chris Mason5f39d392007-10-15 16:14:19 -04003335 src_nritems = btrfs_header_nritems(src);
3336 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003337 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003338 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003339 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003340
Chris Masond3977122009-01-05 21:25:51 -05003341 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003342 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003343
3344 max_push = src_nritems / 2 + 1;
3345 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003346 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003347 return 1;
Yan252c38f2007-08-29 09:11:44 -04003348
Chris Mason79f95c82007-03-01 15:16:26 -05003349 if (max_push < push_items)
3350 push_items = max_push;
3351
David Sterbabf1d3422018-03-05 15:47:39 +01003352 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3353 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003354 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3355 btrfs_node_key_ptr_offset(0),
3356 (dst_nritems) *
3357 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003358
David Sterbaed874f02019-03-20 14:22:04 +01003359 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3360 push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003361 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003362 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003363 return ret;
3364 }
Chris Mason5f39d392007-10-15 16:14:19 -04003365 copy_extent_buffer(dst, src,
3366 btrfs_node_key_ptr_offset(0),
3367 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003368 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003369
Chris Mason5f39d392007-10-15 16:14:19 -04003370 btrfs_set_header_nritems(src, src_nritems - push_items);
3371 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003372
Chris Mason5f39d392007-10-15 16:14:19 -04003373 btrfs_mark_buffer_dirty(src);
3374 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003375
Chris Mason79f95c82007-03-01 15:16:26 -05003376 return ret;
3377}
3378
3379/*
Chris Mason97571fd2007-02-24 13:39:08 -05003380 * helper function to insert a new root level in the tree.
3381 * A new node is allocated, and a single item is inserted to
3382 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003383 *
3384 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003385 */
Chris Masond3977122009-01-05 21:25:51 -05003386static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003387 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003388 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003390 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003391 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003392 struct extent_buffer *lower;
3393 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003394 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003395 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01003396 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003397
3398 BUG_ON(path->nodes[level]);
3399 BUG_ON(path->nodes[level-1] != root->node);
3400
Chris Mason7bb86312007-12-11 09:25:06 -05003401 lower = path->nodes[level-1];
3402 if (level == 1)
3403 btrfs_item_key(lower, &lower_key, 0);
3404 else
3405 btrfs_node_key(lower, &lower_key, 0);
3406
Filipe Mananaa6279472019-01-25 11:48:51 +00003407 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3408 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003409 if (IS_ERR(c))
3410 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003411
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003412 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003413
Chris Mason5f39d392007-10-15 16:14:19 -04003414 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003415 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003416 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003417 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003418 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003419
3420 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003421
3422 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003423
Chris Mason925baed2008-06-25 16:01:30 -04003424 old = root->node;
David Sterbad9d19a02018-03-05 16:35:29 +01003425 ret = tree_mod_log_insert_root(root->node, c, 0);
3426 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003427 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003428
3429 /* the super has an extra ref to root->node */
3430 free_extent_buffer(old);
3431
Chris Mason0b86a832008-03-24 15:01:56 -04003432 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003433 extent_buffer_get(c);
3434 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303435 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003436 path->slots[level] = 0;
3437 return 0;
3438}
3439
Chris Mason74123bd2007-02-02 11:05:29 -05003440/*
3441 * worker function to insert a single pointer in a node.
3442 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003443 *
Chris Mason74123bd2007-02-02 11:05:29 -05003444 * slot and level indicate where you want the key to go, and
3445 * blocknr is the block the key points to.
3446 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003447static void insert_ptr(struct btrfs_trans_handle *trans,
David Sterba6ad3cf62019-03-20 14:32:45 +01003448 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003449 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003450 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003451{
Chris Mason5f39d392007-10-15 16:14:19 -04003452 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003453 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003454 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003455
3456 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003457 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003458 lower = path->nodes[level];
3459 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003460 BUG_ON(slot > nritems);
David Sterba6ad3cf62019-03-20 14:32:45 +01003461 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003462 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003463 if (level) {
3464 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003465 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003466 BUG_ON(ret < 0);
3467 }
Chris Mason5f39d392007-10-15 16:14:19 -04003468 memmove_extent_buffer(lower,
3469 btrfs_node_key_ptr_offset(slot + 1),
3470 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003471 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003472 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003473 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003474 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3475 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003476 BUG_ON(ret < 0);
3477 }
Chris Mason5f39d392007-10-15 16:14:19 -04003478 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003479 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003480 WARN_ON(trans->transid == 0);
3481 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_set_header_nritems(lower, nritems + 1);
3483 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003484}
3485
Chris Mason97571fd2007-02-24 13:39:08 -05003486/*
3487 * split the node at the specified level in path in two.
3488 * The path is corrected to point to the appropriate node after the split
3489 *
3490 * Before splitting this tries to make some room in the node by pushing
3491 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003492 *
3493 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003494 */
Chris Masone02119d2008-09-05 16:13:11 -04003495static noinline int split_node(struct btrfs_trans_handle *trans,
3496 struct btrfs_root *root,
3497 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003498{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003499 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003500 struct extent_buffer *c;
3501 struct extent_buffer *split;
3502 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003503 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003504 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003505 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003506
Chris Mason5f39d392007-10-15 16:14:19 -04003507 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003508 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003510 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003511 * trying to split the root, lets make a new one
3512 *
Liu Bofdd99c72013-05-22 12:06:51 +00003513 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003514 * insert_new_root, because that root buffer will be kept as a
3515 * normal node. We are going to log removal of half of the
3516 * elements below with tree_mod_log_eb_copy. We're holding a
3517 * tree lock on the buffer, which is why we cannot race with
3518 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003519 */
Liu Bofdd99c72013-05-22 12:06:51 +00003520 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003521 if (ret)
3522 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003523 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003524 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003525 c = path->nodes[level];
3526 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003527 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003528 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003529 if (ret < 0)
3530 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003531 }
Chris Masone66f7092007-04-20 13:16:02 -04003532
Chris Mason5f39d392007-10-15 16:14:19 -04003533 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003534 mid = (c_nritems + 1) / 2;
3535 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003536
Filipe Mananaa6279472019-01-25 11:48:51 +00003537 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3538 c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003539 if (IS_ERR(split))
3540 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003542 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03003543 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04003544
David Sterbaed874f02019-03-20 14:22:04 +01003545 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003546 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003547 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003548 return ret;
3549 }
Chris Mason5f39d392007-10-15 16:14:19 -04003550 copy_extent_buffer(split, c,
3551 btrfs_node_key_ptr_offset(0),
3552 btrfs_node_key_ptr_offset(mid),
3553 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3554 btrfs_set_header_nritems(split, c_nritems - mid);
3555 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003556 ret = 0;
3557
Chris Mason5f39d392007-10-15 16:14:19 -04003558 btrfs_mark_buffer_dirty(c);
3559 btrfs_mark_buffer_dirty(split);
3560
David Sterba6ad3cf62019-03-20 14:32:45 +01003561 insert_ptr(trans, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003562 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003563
Chris Mason5de08d72007-02-24 06:24:44 -05003564 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003565 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003566 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003567 free_extent_buffer(c);
3568 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003569 path->slots[level + 1] += 1;
3570 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003571 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003572 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003573 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003574 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003575}
3576
Chris Mason74123bd2007-02-02 11:05:29 -05003577/*
3578 * how many bytes are required to store the items in a leaf. start
3579 * and nr indicate which items in the leaf to check. This totals up the
3580 * space used both by the item structs and the item data
3581 */
Chris Mason5f39d392007-10-15 16:14:19 -04003582static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003583{
Josef Bacik41be1f32012-10-15 13:43:18 -04003584 struct btrfs_item *start_item;
3585 struct btrfs_item *end_item;
3586 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003587 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003588 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003589 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003590
3591 if (!nr)
3592 return 0;
David Sterbac82f8232019-08-09 17:48:21 +02003593 btrfs_init_map_token(&token, l);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003594 start_item = btrfs_item_nr(start);
3595 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003596 data_len = btrfs_token_item_offset(l, start_item, &token) +
3597 btrfs_token_item_size(l, start_item, &token);
3598 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003599 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003600 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003601 return data_len;
3602}
3603
Chris Mason74123bd2007-02-02 11:05:29 -05003604/*
Chris Masond4dbff92007-04-04 14:08:15 -04003605 * The space between the end of the leaf items and
3606 * the start of the leaf data. IOW, how much room
3607 * the leaf has left for both items and data
3608 */
David Sterbae902baa2019-03-20 14:36:46 +01003609noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003610{
David Sterbae902baa2019-03-20 14:36:46 +01003611 struct btrfs_fs_info *fs_info = leaf->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003612 int nritems = btrfs_header_nritems(leaf);
3613 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003614
3615 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003616 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003617 btrfs_crit(fs_info,
3618 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3619 ret,
3620 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3621 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003622 }
3623 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003624}
3625
Chris Mason99d8f832010-07-07 10:51:48 -04003626/*
3627 * min slot controls the lowest index we're willing to push to the
3628 * right. We'll push up to and including min_slot, but no lower
3629 */
David Sterbaf72f0012019-03-20 14:39:45 +01003630static noinline int __push_leaf_right(struct btrfs_path *path,
Chris Mason44871b12009-03-13 10:04:31 -04003631 int data_size, int empty,
3632 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003633 int free_space, u32 left_nritems,
3634 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003635{
David Sterbaf72f0012019-03-20 14:39:45 +01003636 struct btrfs_fs_info *fs_info = right->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003637 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003638 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003639 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003640 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003641 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003642 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003643 int push_space = 0;
3644 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003645 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003646 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003647 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003648 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003649 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003650
Chris Mason34a38212007-11-07 13:31:03 -05003651 if (empty)
3652 nr = 0;
3653 else
Chris Mason99d8f832010-07-07 10:51:48 -04003654 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003655
Zheng Yan31840ae2008-09-23 13:14:14 -04003656 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003657 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003658
Chris Mason44871b12009-03-13 10:04:31 -04003659 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003660 i = left_nritems - 1;
3661 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003662 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003663
Zheng Yan31840ae2008-09-23 13:14:14 -04003664 if (!empty && push_items > 0) {
3665 if (path->slots[0] > i)
3666 break;
3667 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003668 int space = btrfs_leaf_free_space(left);
3669
Zheng Yan31840ae2008-09-23 13:14:14 -04003670 if (space + push_space * 2 > free_space)
3671 break;
3672 }
3673 }
3674
Chris Mason00ec4c52007-02-24 12:47:20 -05003675 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003676 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003677
Chris Masondb945352007-10-15 16:15:53 -04003678 this_item_size = btrfs_item_size(left, item);
3679 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003680 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003681
Chris Mason00ec4c52007-02-24 12:47:20 -05003682 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003683 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003684 if (i == 0)
3685 break;
3686 i--;
Chris Masondb945352007-10-15 16:15:53 -04003687 }
Chris Mason5f39d392007-10-15 16:14:19 -04003688
Chris Mason925baed2008-06-25 16:01:30 -04003689 if (push_items == 0)
3690 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003691
Julia Lawall6c1500f2012-11-03 20:30:18 +00003692 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003693
Chris Mason00ec4c52007-02-24 12:47:20 -05003694 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003695 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003696
Chris Mason5f39d392007-10-15 16:14:19 -04003697 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
David Sterba8f881e82019-03-20 11:33:10 +01003698 push_space -= leaf_data_end(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003699
Chris Mason00ec4c52007-02-24 12:47:20 -05003700 /* make room in the right data area */
David Sterba8f881e82019-03-20 11:33:10 +01003701 data_end = leaf_data_end(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003702 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003703 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3704 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003705 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003706
Chris Mason00ec4c52007-02-24 12:47:20 -05003707 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003708 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003709 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
David Sterba8f881e82019-03-20 11:33:10 +01003710 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
Chris Masond6025572007-03-30 14:27:56 -04003711 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003712
3713 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3714 btrfs_item_nr_offset(0),
3715 right_nritems * sizeof(struct btrfs_item));
3716
Chris Mason00ec4c52007-02-24 12:47:20 -05003717 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003718 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3719 btrfs_item_nr_offset(left_nritems - push_items),
3720 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003721
3722 /* update the item pointers */
David Sterbac82f8232019-08-09 17:48:21 +02003723 btrfs_init_map_token(&token, right);
Chris Mason7518a232007-03-12 12:01:18 -04003724 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003725 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003726 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003727 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003728 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003729 push_space -= btrfs_token_item_size(right, item, &token);
3730 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003731 }
3732
Chris Mason7518a232007-03-12 12:01:18 -04003733 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003734 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003735
Chris Mason34a38212007-11-07 13:31:03 -05003736 if (left_nritems)
3737 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003738 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003739 btrfs_clean_tree_block(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003740
Chris Mason5f39d392007-10-15 16:14:19 -04003741 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003742
Chris Mason5f39d392007-10-15 16:14:19 -04003743 btrfs_item_key(right, &disk_key, 0);
3744 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003745 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003746
Chris Mason00ec4c52007-02-24 12:47:20 -05003747 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003748 if (path->slots[0] >= left_nritems) {
3749 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003750 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba6a884d7d2019-03-20 14:30:02 +01003751 btrfs_clean_tree_block(path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003752 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003753 free_extent_buffer(path->nodes[0]);
3754 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003755 path->slots[1] += 1;
3756 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003757 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003758 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003759 }
3760 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003761
3762out_unlock:
3763 btrfs_tree_unlock(right);
3764 free_extent_buffer(right);
3765 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003766}
Chris Mason925baed2008-06-25 16:01:30 -04003767
Chris Mason00ec4c52007-02-24 12:47:20 -05003768/*
Chris Mason44871b12009-03-13 10:04:31 -04003769 * push some data in the path leaf to the right, trying to free up at
3770 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3771 *
3772 * returns 1 if the push failed because the other node didn't have enough
3773 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003774 *
3775 * this will push starting from min_slot to the end of the leaf. It won't
3776 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003777 */
3778static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003779 *root, struct btrfs_path *path,
3780 int min_data_size, int data_size,
3781 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003782{
3783 struct extent_buffer *left = path->nodes[0];
3784 struct extent_buffer *right;
3785 struct extent_buffer *upper;
3786 int slot;
3787 int free_space;
3788 u32 left_nritems;
3789 int ret;
3790
3791 if (!path->nodes[1])
3792 return 1;
3793
3794 slot = path->slots[1];
3795 upper = path->nodes[1];
3796 if (slot >= btrfs_header_nritems(upper) - 1)
3797 return 1;
3798
3799 btrfs_assert_tree_locked(path->nodes[1]);
3800
David Sterba4b231ae2019-08-21 19:16:27 +02003801 right = btrfs_read_node_slot(upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003802 /*
3803 * slot + 1 is not valid or we fail to read the right node,
3804 * no big deal, just return.
3805 */
3806 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003807 return 1;
3808
Chris Mason44871b12009-03-13 10:04:31 -04003809 btrfs_tree_lock(right);
David Sterba8bead252018-04-04 02:03:48 +02003810 btrfs_set_lock_blocking_write(right);
Chris Mason44871b12009-03-13 10:04:31 -04003811
David Sterbae902baa2019-03-20 14:36:46 +01003812 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003813 if (free_space < data_size)
3814 goto out_unlock;
3815
3816 /* cow and double check */
3817 ret = btrfs_cow_block(trans, root, right, upper,
3818 slot + 1, &right);
3819 if (ret)
3820 goto out_unlock;
3821
David Sterbae902baa2019-03-20 14:36:46 +01003822 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003823 if (free_space < data_size)
3824 goto out_unlock;
3825
3826 left_nritems = btrfs_header_nritems(left);
3827 if (left_nritems == 0)
3828 goto out_unlock;
3829
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003830 if (path->slots[0] == left_nritems && !empty) {
3831 /* Key greater than all keys in the leaf, right neighbor has
3832 * enough room for it and we're not emptying our leaf to delete
3833 * it, therefore use right neighbor to insert the new item and
Andrea Gelmini52042d82018-11-28 12:05:13 +01003834 * no need to touch/dirty our left leaf. */
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003835 btrfs_tree_unlock(left);
3836 free_extent_buffer(left);
3837 path->nodes[0] = right;
3838 path->slots[0] = 0;
3839 path->slots[1]++;
3840 return 0;
3841 }
3842
David Sterbaf72f0012019-03-20 14:39:45 +01003843 return __push_leaf_right(path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003844 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003845out_unlock:
3846 btrfs_tree_unlock(right);
3847 free_extent_buffer(right);
3848 return 1;
3849}
3850
3851/*
Chris Mason74123bd2007-02-02 11:05:29 -05003852 * push some data in the path leaf to the left, trying to free up at
3853 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003854 *
3855 * max_slot can put a limit on how far into the leaf we'll push items. The
3856 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3857 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003858 */
David Sterba8087c192019-03-20 14:40:41 +01003859static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003860 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003861 int free_space, u32 right_nritems,
3862 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003863{
David Sterba8087c192019-03-20 14:40:41 +01003864 struct btrfs_fs_info *fs_info = left->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003865 struct btrfs_disk_key disk_key;
3866 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003867 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003868 int push_space = 0;
3869 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003870 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003871 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003872 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003873 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003874 u32 this_item_size;
3875 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003876 struct btrfs_map_token token;
3877
Chris Mason34a38212007-11-07 13:31:03 -05003878 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003879 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003880 else
Chris Mason99d8f832010-07-07 10:51:48 -04003881 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003882
3883 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003884 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003885
Zheng Yan31840ae2008-09-23 13:14:14 -04003886 if (!empty && push_items > 0) {
3887 if (path->slots[0] < i)
3888 break;
3889 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003890 int space = btrfs_leaf_free_space(right);
3891
Zheng Yan31840ae2008-09-23 13:14:14 -04003892 if (space + push_space * 2 > free_space)
3893 break;
3894 }
3895 }
3896
Chris Masonbe0e5c02007-01-26 15:51:26 -05003897 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003898 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003899
3900 this_item_size = btrfs_item_size(right, item);
3901 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003902 break;
Chris Masondb945352007-10-15 16:15:53 -04003903
Chris Masonbe0e5c02007-01-26 15:51:26 -05003904 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003905 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003906 }
Chris Masondb945352007-10-15 16:15:53 -04003907
Chris Masonbe0e5c02007-01-26 15:51:26 -05003908 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003909 ret = 1;
3910 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003911 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303912 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003913
Chris Masonbe0e5c02007-01-26 15:51:26 -05003914 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003915 copy_extent_buffer(left, right,
3916 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3917 btrfs_item_nr_offset(0),
3918 push_items * sizeof(struct btrfs_item));
3919
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003920 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003921 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003922
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003923 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003924 leaf_data_end(left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003925 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003926 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003927 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003928 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003929 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003930
David Sterbac82f8232019-08-09 17:48:21 +02003931 btrfs_init_map_token(&token, left);
Chris Masondb945352007-10-15 16:15:53 -04003932 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003933 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003934 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003935
Ross Kirkdd3cc162013-09-16 15:58:09 +01003936 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003937
Chris Masoncfed81a2012-03-03 07:40:03 -05003938 ioff = btrfs_token_item_offset(left, item, &token);
3939 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003940 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003941 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003942 }
Chris Mason5f39d392007-10-15 16:14:19 -04003943 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003944
3945 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003946 if (push_items > right_nritems)
3947 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003948 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003949
Chris Mason34a38212007-11-07 13:31:03 -05003950 if (push_items < right_nritems) {
3951 push_space = btrfs_item_offset_nr(right, push_items - 1) -
David Sterba8f881e82019-03-20 11:33:10 +01003952 leaf_data_end(right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003953 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003954 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003955 BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003956 leaf_data_end(right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003957
3958 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003959 btrfs_item_nr_offset(push_items),
3960 (btrfs_header_nritems(right) - push_items) *
3961 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003962 }
David Sterbac82f8232019-08-09 17:48:21 +02003963
3964 btrfs_init_map_token(&token, right);
Yaneef1c492007-11-26 10:58:13 -05003965 right_nritems -= push_items;
3966 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003967 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003968 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003969 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003970
Chris Masoncfed81a2012-03-03 07:40:03 -05003971 push_space = push_space - btrfs_token_item_size(right,
3972 item, &token);
3973 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003974 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003975
Chris Mason5f39d392007-10-15 16:14:19 -04003976 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003977 if (right_nritems)
3978 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003979 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003980 btrfs_clean_tree_block(right);
Chris Mason098f59c2007-05-11 11:33:21 -04003981
Chris Mason5f39d392007-10-15 16:14:19 -04003982 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003983 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003984
3985 /* then fixup the leaf pointer in the path */
3986 if (path->slots[0] < push_items) {
3987 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003988 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003989 free_extent_buffer(path->nodes[0]);
3990 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003991 path->slots[1] -= 1;
3992 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003993 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003994 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003995 path->slots[0] -= push_items;
3996 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003997 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003998 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003999out:
4000 btrfs_tree_unlock(left);
4001 free_extent_buffer(left);
4002 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004003}
4004
Chris Mason74123bd2007-02-02 11:05:29 -05004005/*
Chris Mason44871b12009-03-13 10:04:31 -04004006 * push some data in the path leaf to the left, trying to free up at
4007 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04004008 *
4009 * max_slot can put a limit on how far into the leaf we'll push items. The
4010 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
4011 * items
Chris Mason44871b12009-03-13 10:04:31 -04004012 */
4013static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04004014 *root, struct btrfs_path *path, int min_data_size,
4015 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04004016{
4017 struct extent_buffer *right = path->nodes[0];
4018 struct extent_buffer *left;
4019 int slot;
4020 int free_space;
4021 u32 right_nritems;
4022 int ret = 0;
4023
4024 slot = path->slots[1];
4025 if (slot == 0)
4026 return 1;
4027 if (!path->nodes[1])
4028 return 1;
4029
4030 right_nritems = btrfs_header_nritems(right);
4031 if (right_nritems == 0)
4032 return 1;
4033
4034 btrfs_assert_tree_locked(path->nodes[1]);
4035
David Sterba4b231ae2019-08-21 19:16:27 +02004036 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07004037 /*
4038 * slot - 1 is not valid or we fail to read the left node,
4039 * no big deal, just return.
4040 */
4041 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004042 return 1;
4043
Chris Mason44871b12009-03-13 10:04:31 -04004044 btrfs_tree_lock(left);
David Sterba8bead252018-04-04 02:03:48 +02004045 btrfs_set_lock_blocking_write(left);
Chris Mason44871b12009-03-13 10:04:31 -04004046
David Sterbae902baa2019-03-20 14:36:46 +01004047 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04004048 if (free_space < data_size) {
4049 ret = 1;
4050 goto out;
4051 }
4052
4053 /* cow and double check */
4054 ret = btrfs_cow_block(trans, root, left,
4055 path->nodes[1], slot - 1, &left);
4056 if (ret) {
4057 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004058 if (ret == -ENOSPC)
4059 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004060 goto out;
4061 }
4062
David Sterbae902baa2019-03-20 14:36:46 +01004063 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04004064 if (free_space < data_size) {
4065 ret = 1;
4066 goto out;
4067 }
4068
David Sterba8087c192019-03-20 14:40:41 +01004069 return __push_leaf_left(path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004070 empty, left, free_space, right_nritems,
4071 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004072out:
4073 btrfs_tree_unlock(left);
4074 free_extent_buffer(left);
4075 return ret;
4076}
4077
4078/*
Chris Mason74123bd2007-02-02 11:05:29 -05004079 * split the path's leaf in two, making sure there is at least data_size
4080 * available for the resulting leaf level of the path.
4081 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004082static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004083 struct btrfs_path *path,
4084 struct extent_buffer *l,
4085 struct extent_buffer *right,
4086 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004087{
David Sterba94f94ad2019-03-20 14:42:33 +01004088 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004089 int data_copy_size;
4090 int rt_data_off;
4091 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004092 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004093 struct btrfs_map_token token;
4094
Chris Mason5f39d392007-10-15 16:14:19 -04004095 nritems = nritems - mid;
4096 btrfs_set_header_nritems(right, nritems);
David Sterba8f881e82019-03-20 11:33:10 +01004097 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
Chris Mason5f39d392007-10-15 16:14:19 -04004098
4099 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4100 btrfs_item_nr_offset(mid),
4101 nritems * sizeof(struct btrfs_item));
4102
4103 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004104 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4105 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01004106 leaf_data_end(l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004107
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004108 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004109
David Sterbac82f8232019-08-09 17:48:21 +02004110 btrfs_init_map_token(&token, right);
Chris Mason5f39d392007-10-15 16:14:19 -04004111 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004112 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004113 u32 ioff;
4114
Chris Masoncfed81a2012-03-03 07:40:03 -05004115 ioff = btrfs_token_item_offset(right, item, &token);
4116 btrfs_set_token_item_offset(right, item,
4117 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004118 }
Chris Mason74123bd2007-02-02 11:05:29 -05004119
Chris Mason5f39d392007-10-15 16:14:19 -04004120 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004121 btrfs_item_key(right, &disk_key, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004122 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004123
4124 btrfs_mark_buffer_dirty(right);
4125 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004126 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004127
Chris Masonbe0e5c02007-01-26 15:51:26 -05004128 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004129 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004130 free_extent_buffer(path->nodes[0]);
4131 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004132 path->slots[0] -= mid;
4133 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004134 } else {
4135 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004136 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004137 }
Chris Mason5f39d392007-10-15 16:14:19 -04004138
Chris Masoneb60cea2007-02-02 09:18:22 -05004139 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004140}
4141
4142/*
Chris Mason99d8f832010-07-07 10:51:48 -04004143 * double splits happen when we need to insert a big item in the middle
4144 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4145 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4146 * A B C
4147 *
4148 * We avoid this by trying to push the items on either side of our target
4149 * into the adjacent leaves. If all goes well we can avoid the double split
4150 * completely.
4151 */
4152static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4153 struct btrfs_root *root,
4154 struct btrfs_path *path,
4155 int data_size)
4156{
4157 int ret;
4158 int progress = 0;
4159 int slot;
4160 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004161 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004162
4163 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004164 if (slot < btrfs_header_nritems(path->nodes[0]))
David Sterbae902baa2019-03-20 14:36:46 +01004165 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004166
4167 /*
4168 * try to push all the items after our slot into the
4169 * right leaf
4170 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004171 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004172 if (ret < 0)
4173 return ret;
4174
4175 if (ret == 0)
4176 progress++;
4177
4178 nritems = btrfs_header_nritems(path->nodes[0]);
4179 /*
4180 * our goal is to get our slot at the start or end of a leaf. If
4181 * we've done so we're done
4182 */
4183 if (path->slots[0] == 0 || path->slots[0] == nritems)
4184 return 0;
4185
David Sterbae902baa2019-03-20 14:36:46 +01004186 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004187 return 0;
4188
4189 /* try to push all the items before our slot into the next leaf */
4190 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004191 space_needed = data_size;
4192 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004193 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004194 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004195 if (ret < 0)
4196 return ret;
4197
4198 if (ret == 0)
4199 progress++;
4200
4201 if (progress)
4202 return 0;
4203 return 1;
4204}
4205
4206/*
Chris Mason44871b12009-03-13 10:04:31 -04004207 * split the path's leaf in two, making sure there is at least data_size
4208 * available for the resulting leaf level of the path.
4209 *
4210 * returns 0 if all went well and < 0 on failure.
4211 */
4212static noinline int split_leaf(struct btrfs_trans_handle *trans,
4213 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004214 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004215 struct btrfs_path *path, int data_size,
4216 int extend)
4217{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004218 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004219 struct extent_buffer *l;
4220 u32 nritems;
4221 int mid;
4222 int slot;
4223 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004224 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004225 int ret = 0;
4226 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004228 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004229 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004230
Yan, Zhenga5719522009-09-24 09:17:31 -04004231 l = path->nodes[0];
4232 slot = path->slots[0];
4233 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004234 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004235 return -EOVERFLOW;
4236
Chris Mason44871b12009-03-13 10:04:31 -04004237 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004238 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004239 int space_needed = data_size;
4240
4241 if (slot < btrfs_header_nritems(l))
David Sterbae902baa2019-03-20 14:36:46 +01004242 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004243
4244 wret = push_leaf_right(trans, root, path, space_needed,
4245 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004246 if (wret < 0)
4247 return wret;
4248 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004249 space_needed = data_size;
4250 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004251 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004252 wret = push_leaf_left(trans, root, path, space_needed,
4253 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004254 if (wret < 0)
4255 return wret;
4256 }
4257 l = path->nodes[0];
4258
4259 /* did the pushes work? */
David Sterbae902baa2019-03-20 14:36:46 +01004260 if (btrfs_leaf_free_space(l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004261 return 0;
4262 }
4263
4264 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004265 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004266 if (ret)
4267 return ret;
4268 }
4269again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004270 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004271 l = path->nodes[0];
4272 slot = path->slots[0];
4273 nritems = btrfs_header_nritems(l);
4274 mid = (nritems + 1) / 2;
4275
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004276 if (mid <= slot) {
4277 if (nritems == 1 ||
4278 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004279 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004280 if (slot >= nritems) {
4281 split = 0;
4282 } else {
4283 mid = slot;
4284 if (mid != nritems &&
4285 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004286 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004287 if (data_size && !tried_avoid_double)
4288 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004289 split = 2;
4290 }
4291 }
4292 }
4293 } else {
4294 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004295 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004296 if (!extend && data_size && slot == 0) {
4297 split = 0;
4298 } else if ((extend || !data_size) && slot == 0) {
4299 mid = 1;
4300 } else {
4301 mid = slot;
4302 if (mid != nritems &&
4303 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004304 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004305 if (data_size && !tried_avoid_double)
4306 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304307 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004308 }
4309 }
4310 }
4311 }
4312
4313 if (split == 0)
4314 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4315 else
4316 btrfs_item_key(l, &disk_key, mid);
4317
Filipe Mananaa6279472019-01-25 11:48:51 +00004318 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4319 l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004320 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004321 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004322
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004323 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004324
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325 if (split == 0) {
4326 if (mid <= slot) {
4327 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004328 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004329 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004330 btrfs_tree_unlock(path->nodes[0]);
4331 free_extent_buffer(path->nodes[0]);
4332 path->nodes[0] = right;
4333 path->slots[0] = 0;
4334 path->slots[1] += 1;
4335 } else {
4336 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004337 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004338 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004339 btrfs_tree_unlock(path->nodes[0]);
4340 free_extent_buffer(path->nodes[0]);
4341 path->nodes[0] = right;
4342 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004343 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004344 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004345 }
Liu Bo196e0242016-09-07 14:48:28 -07004346 /*
4347 * We create a new leaf 'right' for the required ins_len and
4348 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4349 * the content of ins_len to 'right'.
4350 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004351 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004352 }
4353
David Sterba94f94ad2019-03-20 14:42:33 +01004354 copy_for_split(trans, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004355
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004356 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004357 BUG_ON(num_doubles != 0);
4358 num_doubles++;
4359 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004360 }
Chris Mason44871b12009-03-13 10:04:31 -04004361
Jeff Mahoney143bede2012-03-01 14:56:26 +01004362 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004363
4364push_for_double:
4365 push_for_double_split(trans, root, path, data_size);
4366 tried_avoid_double = 1;
David Sterbae902baa2019-03-20 14:36:46 +01004367 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004368 return 0;
4369 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004370}
4371
Yan, Zhengad48fd752009-11-12 09:33:58 +00004372static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4373 struct btrfs_root *root,
4374 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004375{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004376 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004377 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004378 struct btrfs_file_extent_item *fi;
4379 u64 extent_len = 0;
4380 u32 item_size;
4381 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004382
4383 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004384 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4385
4386 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4387 key.type != BTRFS_EXTENT_CSUM_KEY);
4388
David Sterbae902baa2019-03-20 14:36:46 +01004389 if (btrfs_leaf_free_space(leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004390 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004391
4392 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004393 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4394 fi = btrfs_item_ptr(leaf, path->slots[0],
4395 struct btrfs_file_extent_item);
4396 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4397 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004398 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004399
Chris Mason459931e2008-12-10 09:10:46 -05004400 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004401 path->search_for_split = 1;
4402 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004403 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004404 if (ret > 0)
4405 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004406 if (ret < 0)
4407 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004408
Yan, Zhengad48fd752009-11-12 09:33:58 +00004409 ret = -EAGAIN;
4410 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004411 /* if our item isn't there, return now */
4412 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004413 goto err;
4414
Chris Mason109f6ae2010-04-02 09:20:18 -04004415 /* the leaf has changed, it now has room. return now */
David Sterbae902baa2019-03-20 14:36:46 +01004416 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004417 goto err;
4418
Yan, Zhengad48fd752009-11-12 09:33:58 +00004419 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4420 fi = btrfs_item_ptr(leaf, path->slots[0],
4421 struct btrfs_file_extent_item);
4422 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4423 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004424 }
4425
Chris Masonb9473432009-03-13 11:00:37 -04004426 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004427 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004428 if (ret)
4429 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004430
Yan, Zhengad48fd752009-11-12 09:33:58 +00004431 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004432 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004433 return 0;
4434err:
4435 path->keep_locks = 0;
4436 return ret;
4437}
4438
David Sterba25263cd2019-03-20 14:44:57 +01004439static noinline int split_item(struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004440 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004441 unsigned long split_offset)
4442{
4443 struct extent_buffer *leaf;
4444 struct btrfs_item *item;
4445 struct btrfs_item *new_item;
4446 int slot;
4447 char *buf;
4448 u32 nritems;
4449 u32 item_size;
4450 u32 orig_offset;
4451 struct btrfs_disk_key disk_key;
4452
Chris Masonb9473432009-03-13 11:00:37 -04004453 leaf = path->nodes[0];
David Sterbae902baa2019-03-20 14:36:46 +01004454 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004455
Chris Masonb4ce94d2009-02-04 09:25:08 -05004456 btrfs_set_path_blocking(path);
4457
Ross Kirkdd3cc162013-09-16 15:58:09 +01004458 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004459 orig_offset = btrfs_item_offset(leaf, item);
4460 item_size = btrfs_item_size(leaf, item);
4461
Chris Mason459931e2008-12-10 09:10:46 -05004462 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004463 if (!buf)
4464 return -ENOMEM;
4465
Chris Mason459931e2008-12-10 09:10:46 -05004466 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4467 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004468
Chris Mason459931e2008-12-10 09:10:46 -05004469 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004470 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004471 if (slot != nritems) {
4472 /* shift the items */
4473 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004474 btrfs_item_nr_offset(slot),
4475 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004476 }
4477
4478 btrfs_cpu_key_to_disk(&disk_key, new_key);
4479 btrfs_set_item_key(leaf, &disk_key, slot);
4480
Ross Kirkdd3cc162013-09-16 15:58:09 +01004481 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004482
4483 btrfs_set_item_offset(leaf, new_item, orig_offset);
4484 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4485
4486 btrfs_set_item_offset(leaf, item,
4487 orig_offset + item_size - split_offset);
4488 btrfs_set_item_size(leaf, item, split_offset);
4489
4490 btrfs_set_header_nritems(leaf, nritems + 1);
4491
4492 /* write the data for the start of the original item */
4493 write_extent_buffer(leaf, buf,
4494 btrfs_item_ptr_offset(leaf, path->slots[0]),
4495 split_offset);
4496
4497 /* write the data for the new item */
4498 write_extent_buffer(leaf, buf + split_offset,
4499 btrfs_item_ptr_offset(leaf, slot),
4500 item_size - split_offset);
4501 btrfs_mark_buffer_dirty(leaf);
4502
David Sterbae902baa2019-03-20 14:36:46 +01004503 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004504 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004505 return 0;
4506}
4507
4508/*
4509 * This function splits a single item into two items,
4510 * giving 'new_key' to the new item and splitting the
4511 * old one at split_offset (from the start of the item).
4512 *
4513 * The path may be released by this operation. After
4514 * the split, the path is pointing to the old item. The
4515 * new item is going to be in the same node as the old one.
4516 *
4517 * Note, the item being split must be smaller enough to live alone on
4518 * a tree block with room for one extra struct btrfs_item
4519 *
4520 * This allows us to split the item in place, keeping a lock on the
4521 * leaf the entire time.
4522 */
4523int btrfs_split_item(struct btrfs_trans_handle *trans,
4524 struct btrfs_root *root,
4525 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004526 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004527 unsigned long split_offset)
4528{
4529 int ret;
4530 ret = setup_leaf_for_split(trans, root, path,
4531 sizeof(struct btrfs_item));
4532 if (ret)
4533 return ret;
4534
David Sterba25263cd2019-03-20 14:44:57 +01004535 ret = split_item(path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004536 return ret;
4537}
4538
4539/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004540 * This function duplicate a item, giving 'new_key' to the new item.
4541 * It guarantees both items live in the same tree leaf and the new item
4542 * is contiguous with the original item.
4543 *
4544 * This allows us to split file extent in place, keeping a lock on the
4545 * leaf the entire time.
4546 */
4547int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4548 struct btrfs_root *root,
4549 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004550 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004551{
4552 struct extent_buffer *leaf;
4553 int ret;
4554 u32 item_size;
4555
4556 leaf = path->nodes[0];
4557 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4558 ret = setup_leaf_for_split(trans, root, path,
4559 item_size + sizeof(struct btrfs_item));
4560 if (ret)
4561 return ret;
4562
4563 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004564 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004565 item_size, item_size +
4566 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004567 leaf = path->nodes[0];
4568 memcpy_extent_buffer(leaf,
4569 btrfs_item_ptr_offset(leaf, path->slots[0]),
4570 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4571 item_size);
4572 return 0;
4573}
4574
4575/*
Chris Masond352ac62008-09-29 15:18:18 -04004576 * make the item pointed to by the path smaller. new_size indicates
4577 * how small to make it, and from_end tells us if we just chop bytes
4578 * off the end of the item or if we shift the item to chop bytes off
4579 * the front.
4580 */
David Sterba78ac4f92019-03-20 14:49:12 +01004581void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004582{
Chris Masonb18c6682007-04-17 13:26:50 -04004583 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004584 struct extent_buffer *leaf;
4585 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004586 u32 nritems;
4587 unsigned int data_end;
4588 unsigned int old_data_start;
4589 unsigned int old_size;
4590 unsigned int size_diff;
4591 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004592 struct btrfs_map_token token;
4593
Chris Mason5f39d392007-10-15 16:14:19 -04004594 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004595 slot = path->slots[0];
4596
4597 old_size = btrfs_item_size_nr(leaf, slot);
4598 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004599 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004600
Chris Mason5f39d392007-10-15 16:14:19 -04004601 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004602 data_end = leaf_data_end(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004603
Chris Mason5f39d392007-10-15 16:14:19 -04004604 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004605
Chris Masonb18c6682007-04-17 13:26:50 -04004606 size_diff = old_size - new_size;
4607
4608 BUG_ON(slot < 0);
4609 BUG_ON(slot >= nritems);
4610
4611 /*
4612 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4613 */
4614 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004615 btrfs_init_map_token(&token, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004616 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004617 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004618 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004619
Chris Masoncfed81a2012-03-03 07:40:03 -05004620 ioff = btrfs_token_item_offset(leaf, item, &token);
4621 btrfs_set_token_item_offset(leaf, item,
4622 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004623 }
Chris Masondb945352007-10-15 16:15:53 -04004624
Chris Masonb18c6682007-04-17 13:26:50 -04004625 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004626 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004627 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4628 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004629 data_end, old_data_start + new_size - data_end);
4630 } else {
4631 struct btrfs_disk_key disk_key;
4632 u64 offset;
4633
4634 btrfs_item_key(leaf, &disk_key, slot);
4635
4636 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4637 unsigned long ptr;
4638 struct btrfs_file_extent_item *fi;
4639
4640 fi = btrfs_item_ptr(leaf, slot,
4641 struct btrfs_file_extent_item);
4642 fi = (struct btrfs_file_extent_item *)(
4643 (unsigned long)fi - size_diff);
4644
4645 if (btrfs_file_extent_type(leaf, fi) ==
4646 BTRFS_FILE_EXTENT_INLINE) {
4647 ptr = btrfs_item_ptr_offset(leaf, slot);
4648 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004649 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004650 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004651 }
4652 }
4653
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004654 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4655 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004656 data_end, old_data_start - data_end);
4657
4658 offset = btrfs_disk_key_offset(&disk_key);
4659 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4660 btrfs_set_item_key(leaf, &disk_key, slot);
4661 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004662 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004663 }
Chris Mason5f39d392007-10-15 16:14:19 -04004664
Ross Kirkdd3cc162013-09-16 15:58:09 +01004665 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004666 btrfs_set_item_size(leaf, item, new_size);
4667 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004668
David Sterbae902baa2019-03-20 14:36:46 +01004669 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004670 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004671 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004672 }
Chris Masonb18c6682007-04-17 13:26:50 -04004673}
4674
Chris Masond352ac62008-09-29 15:18:18 -04004675/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004676 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004677 */
David Sterbac71dd882019-03-20 14:51:10 +01004678void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004679{
Chris Mason6567e832007-04-16 09:22:45 -04004680 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004681 struct extent_buffer *leaf;
4682 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004683 u32 nritems;
4684 unsigned int data_end;
4685 unsigned int old_data;
4686 unsigned int old_size;
4687 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004688 struct btrfs_map_token token;
4689
Chris Mason5f39d392007-10-15 16:14:19 -04004690 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004691
Chris Mason5f39d392007-10-15 16:14:19 -04004692 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004693 data_end = leaf_data_end(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004694
David Sterbae902baa2019-03-20 14:36:46 +01004695 if (btrfs_leaf_free_space(leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004696 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004697 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004698 }
Chris Mason6567e832007-04-16 09:22:45 -04004699 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004700 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004701
4702 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004703 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004704 btrfs_print_leaf(leaf);
David Sterbac71dd882019-03-20 14:51:10 +01004705 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004706 slot, nritems);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004707 BUG();
Chris Mason3326d1b2007-10-15 16:18:25 -04004708 }
Chris Mason6567e832007-04-16 09:22:45 -04004709
4710 /*
4711 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4712 */
4713 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004714 btrfs_init_map_token(&token, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004715 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004716 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004717 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004718
Chris Masoncfed81a2012-03-03 07:40:03 -05004719 ioff = btrfs_token_item_offset(leaf, item, &token);
4720 btrfs_set_token_item_offset(leaf, item,
4721 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004722 }
Chris Mason5f39d392007-10-15 16:14:19 -04004723
Chris Mason6567e832007-04-16 09:22:45 -04004724 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004725 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4726 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004727 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004728
Chris Mason6567e832007-04-16 09:22:45 -04004729 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004730 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004731 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004732 btrfs_set_item_size(leaf, item, old_size + data_size);
4733 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004734
David Sterbae902baa2019-03-20 14:36:46 +01004735 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004736 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004737 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004738 }
Chris Mason6567e832007-04-16 09:22:45 -04004739}
4740
Chris Mason74123bd2007-02-02 11:05:29 -05004741/*
Chris Mason44871b12009-03-13 10:04:31 -04004742 * this is a helper for btrfs_insert_empty_items, the main goal here is
4743 * to save stack depth by doing the bulk of the work in a function
4744 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004745 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004746void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004747 const struct btrfs_key *cpu_key, u32 *data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004748 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004749{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004750 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004751 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004752 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004753 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004754 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004755 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004756 struct extent_buffer *leaf;
4757 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004758 struct btrfs_map_token token;
4759
Filipe Manana24cdc842014-07-28 19:34:35 +01004760 if (path->slots[0] == 0) {
4761 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004762 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004763 }
4764 btrfs_unlock_up_safe(path, 1);
4765
Chris Mason5f39d392007-10-15 16:14:19 -04004766 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004767 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004768
Chris Mason5f39d392007-10-15 16:14:19 -04004769 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004770 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004771
David Sterbae902baa2019-03-20 14:36:46 +01004772 if (btrfs_leaf_free_space(leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004773 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004774 btrfs_crit(fs_info, "not enough freespace need %u have %d",
David Sterbae902baa2019-03-20 14:36:46 +01004775 total_size, btrfs_leaf_free_space(leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004776 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004777 }
Chris Mason5f39d392007-10-15 16:14:19 -04004778
David Sterbac82f8232019-08-09 17:48:21 +02004779 btrfs_init_map_token(&token, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004780 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004781 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782
Chris Mason5f39d392007-10-15 16:14:19 -04004783 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004784 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004785 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004786 slot, old_data, data_end);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004787 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004788 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004789 /*
4790 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4791 */
4792 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004793 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004794 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004795
Jeff Mahoney62e85572016-09-20 10:05:01 -04004796 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004797 ioff = btrfs_token_item_offset(leaf, item, &token);
4798 btrfs_set_token_item_offset(leaf, item,
4799 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004800 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004801 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004802 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004803 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004804 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004805
4806 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004807 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4808 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004809 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004810 data_end = old_data;
4811 }
Chris Mason5f39d392007-10-15 16:14:19 -04004812
Chris Mason62e27492007-03-15 12:56:47 -04004813 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004814 for (i = 0; i < nr; i++) {
4815 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4816 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004817 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004818 btrfs_set_token_item_offset(leaf, item,
4819 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004820 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004821 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004822 }
Chris Mason44871b12009-03-13 10:04:31 -04004823
Chris Mason9c583092008-01-29 15:15:18 -05004824 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004825 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004826
David Sterbae902baa2019-03-20 14:36:46 +01004827 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004828 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004829 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004830 }
Chris Mason44871b12009-03-13 10:04:31 -04004831}
4832
4833/*
4834 * Given a key and some data, insert items into the tree.
4835 * This does all the path init required, making room in the tree if needed.
4836 */
4837int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4838 struct btrfs_root *root,
4839 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004840 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004841 int nr)
4842{
Chris Mason44871b12009-03-13 10:04:31 -04004843 int ret = 0;
4844 int slot;
4845 int i;
4846 u32 total_size = 0;
4847 u32 total_data = 0;
4848
4849 for (i = 0; i < nr; i++)
4850 total_data += data_size[i];
4851
4852 total_size = total_data + (nr * sizeof(struct btrfs_item));
4853 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4854 if (ret == 0)
4855 return -EEXIST;
4856 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004857 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004858
Chris Mason44871b12009-03-13 10:04:31 -04004859 slot = path->slots[0];
4860 BUG_ON(slot < 0);
4861
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004862 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004863 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004864 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004865}
4866
4867/*
4868 * Given a key and some data, insert an item into the tree.
4869 * This does all the path init required, making room in the tree if needed.
4870 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004871int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4872 const struct btrfs_key *cpu_key, void *data,
4873 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004874{
4875 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004876 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004877 struct extent_buffer *leaf;
4878 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004879
Chris Mason2c90e5d2007-04-02 10:50:19 -04004880 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004881 if (!path)
4882 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004883 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004884 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004885 leaf = path->nodes[0];
4886 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4887 write_extent_buffer(leaf, data, ptr, data_size);
4888 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004889 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004890 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004891 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004892}
4893
Chris Mason74123bd2007-02-02 11:05:29 -05004894/*
Chris Mason5de08d72007-02-24 06:24:44 -05004895 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004896 *
Chris Masond352ac62008-09-29 15:18:18 -04004897 * the tree should have been previously balanced so the deletion does not
4898 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004899 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004900static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4901 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004902{
Chris Mason5f39d392007-10-15 16:14:19 -04004903 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004904 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004905 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004906
Chris Mason5f39d392007-10-15 16:14:19 -04004907 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004908 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004909 if (level) {
4910 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004911 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004912 BUG_ON(ret < 0);
4913 }
Chris Mason5f39d392007-10-15 16:14:19 -04004914 memmove_extent_buffer(parent,
4915 btrfs_node_key_ptr_offset(slot),
4916 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004917 sizeof(struct btrfs_key_ptr) *
4918 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004919 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004920 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4921 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004922 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004923 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004924
Chris Mason7518a232007-03-12 12:01:18 -04004925 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004926 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004927 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004928 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004929 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004930 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004931 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004932 struct btrfs_disk_key disk_key;
4933
4934 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004935 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004936 }
Chris Masond6025572007-03-30 14:27:56 -04004937 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004938}
4939
Chris Mason74123bd2007-02-02 11:05:29 -05004940/*
Chris Mason323ac952008-10-01 19:05:46 -04004941 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004942 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004943 *
4944 * This deletes the pointer in path->nodes[1] and frees the leaf
4945 * block extent. zero is returned if it all worked out, < 0 otherwise.
4946 *
4947 * The path must have already been setup for deleting the leaf, including
4948 * all the proper balancing. path->nodes[1] must be locked.
4949 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004950static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4951 struct btrfs_root *root,
4952 struct btrfs_path *path,
4953 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004954{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004955 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004956 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004957
Chris Mason4d081c42009-02-04 09:31:28 -05004958 /*
4959 * btrfs_free_extent is expensive, we want to make sure we
4960 * aren't holding any locks when we call it
4961 */
4962 btrfs_unlock_up_safe(path, 0);
4963
Yan, Zhengf0486c62010-05-16 10:46:25 -04004964 root_sub_used(root, leaf->len);
4965
Josef Bacik3083ee22012-03-09 16:01:49 -05004966 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004967 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004968 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004969}
4970/*
Chris Mason74123bd2007-02-02 11:05:29 -05004971 * delete the item at the leaf level in path. If that empties
4972 * the leaf, remove it from the tree
4973 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004974int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4975 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004977 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004978 struct extent_buffer *leaf;
4979 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004980 u32 last_off;
4981 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004982 int ret = 0;
4983 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004984 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004985 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004986
Chris Mason5f39d392007-10-15 16:14:19 -04004987 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004988 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4989
4990 for (i = 0; i < nr; i++)
4991 dsize += btrfs_item_size_nr(leaf, slot + i);
4992
Chris Mason5f39d392007-10-15 16:14:19 -04004993 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004994
Chris Mason85e21ba2008-01-29 15:11:36 -05004995 if (slot + nr != nritems) {
David Sterba8f881e82019-03-20 11:33:10 +01004996 int data_end = leaf_data_end(leaf);
David Sterbac82f8232019-08-09 17:48:21 +02004997 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04004998
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004999 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04005000 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03005001 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05005002 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04005003
David Sterbac82f8232019-08-09 17:48:21 +02005004 btrfs_init_map_token(&token, leaf);
Chris Mason85e21ba2008-01-29 15:11:36 -05005005 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04005006 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04005007
Ross Kirkdd3cc162013-09-16 15:58:09 +01005008 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05005009 ioff = btrfs_token_item_offset(leaf, item, &token);
5010 btrfs_set_token_item_offset(leaf, item,
5011 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04005012 }
Chris Masondb945352007-10-15 16:15:53 -04005013
Chris Mason5f39d392007-10-15 16:14:19 -04005014 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05005015 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04005016 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05005017 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05005018 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005019 btrfs_set_header_nritems(leaf, nritems - nr);
5020 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04005021
Chris Mason74123bd2007-02-02 11:05:29 -05005022 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04005023 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005024 if (leaf == root->node) {
5025 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005026 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005027 btrfs_set_path_blocking(path);
David Sterba6a884d7d2019-03-20 14:30:02 +01005028 btrfs_clean_tree_block(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005029 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005030 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005031 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005032 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005033 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005034 struct btrfs_disk_key disk_key;
5035
5036 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03005037 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005038 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005039
Chris Mason74123bd2007-02-02 11:05:29 -05005040 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005041 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005042 /* push_leaf_left fixes the path.
5043 * make sure the path still points to our leaf
5044 * for possible call to del_ptr below
5045 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005046 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005047 extent_buffer_get(leaf);
5048
Chris Masonb9473432009-03-13 11:00:37 -04005049 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005050 wret = push_leaf_left(trans, root, path, 1, 1,
5051 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005052 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005053 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005054
5055 if (path->nodes[0] == leaf &&
5056 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005057 wret = push_leaf_right(trans, root, path, 1,
5058 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005059 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005060 ret = wret;
5061 }
Chris Mason5f39d392007-10-15 16:14:19 -04005062
5063 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005064 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005065 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005066 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005067 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005068 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005069 /* if we're still in the path, make sure
5070 * we're dirty. Otherwise, one of the
5071 * push_leaf functions must have already
5072 * dirtied this buffer
5073 */
5074 if (path->nodes[0] == leaf)
5075 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005076 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005077 }
Chris Masond5719762007-03-23 10:01:08 -04005078 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005079 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005080 }
5081 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005082 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005083}
5084
Chris Mason97571fd2007-02-24 13:39:08 -05005085/*
Chris Mason925baed2008-06-25 16:01:30 -04005086 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005087 * returns 0 if it found something or 1 if there are no lesser leaves.
5088 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005089 *
5090 * This may release the path, and so you may lose any locks held at the
5091 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005092 */
Josef Bacik16e75492013-10-22 12:18:51 -04005093int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005094{
Chris Mason925baed2008-06-25 16:01:30 -04005095 struct btrfs_key key;
5096 struct btrfs_disk_key found_key;
5097 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005098
Chris Mason925baed2008-06-25 16:01:30 -04005099 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005100
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005101 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005102 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005103 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005104 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005105 key.offset = (u64)-1;
5106 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005107 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005108 key.type = (u8)-1;
5109 key.offset = (u64)-1;
5110 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005111 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005112 }
Chris Mason7bb86312007-12-11 09:25:06 -05005113
David Sterbab3b4aa72011-04-21 01:20:15 +02005114 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005115 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5116 if (ret < 0)
5117 return ret;
5118 btrfs_item_key(path->nodes[0], &found_key, 0);
5119 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005120 /*
5121 * We might have had an item with the previous key in the tree right
5122 * before we released our path. And after we released our path, that
5123 * item might have been pushed to the first slot (0) of the leaf we
5124 * were holding due to a tree balance. Alternatively, an item with the
5125 * previous key can exist as the only element of a leaf (big fat item).
5126 * Therefore account for these 2 cases, so that our callers (like
5127 * btrfs_previous_item) don't miss an existing item with a key matching
5128 * the previous key we computed above.
5129 */
5130 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005131 return 0;
5132 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005133}
5134
Chris Mason3f157a22008-06-25 16:01:31 -04005135/*
5136 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005137 * for nodes or leaves that are have a minimum transaction id.
5138 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005139 *
5140 * This does not cow, but it does stuff the starting key it finds back
5141 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5142 * key and get a writable path.
5143 *
Chris Mason3f157a22008-06-25 16:01:31 -04005144 * This honors path->lowest_level to prevent descent past a given level
5145 * of the tree.
5146 *
Chris Masond352ac62008-09-29 15:18:18 -04005147 * min_trans indicates the oldest transaction that you are interested
5148 * in walking through. Any nodes or leaves older than min_trans are
5149 * skipped over (without reading them).
5150 *
Chris Mason3f157a22008-06-25 16:01:31 -04005151 * returns zero if something useful was found, < 0 on error and 1 if there
5152 * was nothing in the tree that matched the search criteria.
5153 */
5154int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005155 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005156 u64 min_trans)
5157{
5158 struct extent_buffer *cur;
5159 struct btrfs_key found_key;
5160 int slot;
Yan96524802008-07-24 12:19:49 -04005161 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005162 u32 nritems;
5163 int level;
5164 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005165 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005166
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005167 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005168again:
Chris Masonbd681512011-07-16 15:23:14 -04005169 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005170 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005171 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005172 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005173 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005174
5175 if (btrfs_header_generation(cur) < min_trans) {
5176 ret = 1;
5177 goto out;
5178 }
Chris Masond3977122009-01-05 21:25:51 -05005179 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005180 nritems = btrfs_header_nritems(cur);
5181 level = btrfs_header_level(cur);
Nikolay Borisova74b35e2017-12-08 16:27:43 +02005182 sret = btrfs_bin_search(cur, min_key, level, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00005183 if (sret < 0) {
5184 ret = sret;
5185 goto out;
5186 }
Chris Mason3f157a22008-06-25 16:01:31 -04005187
Chris Mason323ac952008-10-01 19:05:46 -04005188 /* at the lowest level, we're done, setup the path and exit */
5189 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005190 if (slot >= nritems)
5191 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005192 ret = 0;
5193 path->slots[level] = slot;
5194 btrfs_item_key_to_cpu(cur, &found_key, slot);
5195 goto out;
5196 }
Yan96524802008-07-24 12:19:49 -04005197 if (sret && slot > 0)
5198 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005199 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005200 * check this node pointer against the min_trans parameters.
5201 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005202 */
Chris Masond3977122009-01-05 21:25:51 -05005203 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005204 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005205
Chris Mason3f157a22008-06-25 16:01:31 -04005206 gen = btrfs_node_ptr_generation(cur, slot);
5207 if (gen < min_trans) {
5208 slot++;
5209 continue;
5210 }
Eric Sandeende78b512013-01-31 18:21:12 +00005211 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005212 }
Chris Masone02119d2008-09-05 16:13:11 -04005213find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005214 /*
5215 * we didn't find a candidate key in this node, walk forward
5216 * and find another one
5217 */
5218 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005219 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005220 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005221 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005222 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005223 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005224 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005225 goto again;
5226 } else {
5227 goto out;
5228 }
5229 }
5230 /* save our key for returning back */
5231 btrfs_node_key_to_cpu(cur, &found_key, slot);
5232 path->slots[level] = slot;
5233 if (level == path->lowest_level) {
5234 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005235 goto out;
5236 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005237 btrfs_set_path_blocking(path);
David Sterba4b231ae2019-08-21 19:16:27 +02005238 cur = btrfs_read_node_slot(cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005239 if (IS_ERR(cur)) {
5240 ret = PTR_ERR(cur);
5241 goto out;
5242 }
Chris Mason3f157a22008-06-25 16:01:31 -04005243
Chris Masonbd681512011-07-16 15:23:14 -04005244 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005245
Chris Masonbd681512011-07-16 15:23:14 -04005246 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005247 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005248 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005249 }
5250out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005251 path->keep_locks = keep_locks;
5252 if (ret == 0) {
5253 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5254 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005255 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005256 }
Chris Mason3f157a22008-06-25 16:01:31 -04005257 return ret;
5258}
5259
5260/*
5261 * this is similar to btrfs_next_leaf, but does not try to preserve
5262 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005263 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005264 *
5265 * 0 is returned if another key is found, < 0 if there are any errors
5266 * and 1 is returned if there are no higher keys in the tree
5267 *
5268 * path->keep_locks should be set to 1 on the search made before
5269 * calling this function.
5270 */
Chris Masone7a84562008-06-25 16:01:31 -04005271int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005272 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005273{
Chris Masone7a84562008-06-25 16:01:31 -04005274 int slot;
5275 struct extent_buffer *c;
5276
Josef Bacik6a9fb462019-06-20 15:37:52 -04005277 WARN_ON(!path->keep_locks && !path->skip_locking);
Chris Masond3977122009-01-05 21:25:51 -05005278 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005279 if (!path->nodes[level])
5280 return 1;
5281
5282 slot = path->slots[level] + 1;
5283 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005284next:
Chris Masone7a84562008-06-25 16:01:31 -04005285 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005286 int ret;
5287 int orig_lowest;
5288 struct btrfs_key cur_key;
5289 if (level + 1 >= BTRFS_MAX_LEVEL ||
5290 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005291 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005292
Josef Bacik6a9fb462019-06-20 15:37:52 -04005293 if (path->locks[level + 1] || path->skip_locking) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005294 level++;
5295 continue;
5296 }
5297
5298 slot = btrfs_header_nritems(c) - 1;
5299 if (level == 0)
5300 btrfs_item_key_to_cpu(c, &cur_key, slot);
5301 else
5302 btrfs_node_key_to_cpu(c, &cur_key, slot);
5303
5304 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005305 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005306 path->lowest_level = level;
5307 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5308 0, 0);
5309 path->lowest_level = orig_lowest;
5310 if (ret < 0)
5311 return ret;
5312
5313 c = path->nodes[level];
5314 slot = path->slots[level];
5315 if (ret == 0)
5316 slot++;
5317 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005318 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005319
Chris Masone7a84562008-06-25 16:01:31 -04005320 if (level == 0)
5321 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005322 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005323 u64 gen = btrfs_node_ptr_generation(c, slot);
5324
Chris Mason3f157a22008-06-25 16:01:31 -04005325 if (gen < min_trans) {
5326 slot++;
5327 goto next;
5328 }
Chris Masone7a84562008-06-25 16:01:31 -04005329 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005330 }
Chris Masone7a84562008-06-25 16:01:31 -04005331 return 0;
5332 }
5333 return 1;
5334}
5335
Chris Mason7bb86312007-12-11 09:25:06 -05005336/*
Chris Mason925baed2008-06-25 16:01:30 -04005337 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005338 * returns 0 if it found something or 1 if there are no greater leaves.
5339 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005340 */
Chris Mason234b63a2007-03-13 10:46:10 -04005341int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005342{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005343 return btrfs_next_old_leaf(root, path, 0);
5344}
5345
5346int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5347 u64 time_seq)
5348{
Chris Masond97e63b2007-02-20 16:40:44 -05005349 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005350 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005351 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005352 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005353 struct btrfs_key key;
5354 u32 nritems;
5355 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005356 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005357 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005358
5359 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005360 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005361 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005362
Chris Mason8e73f272009-04-03 10:14:18 -04005363 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5364again:
5365 level = 1;
5366 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005367 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005368 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005369
Chris Masona2135012008-06-25 16:01:30 -04005370 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005371 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005372
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005373 if (time_seq)
5374 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5375 else
5376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005377 path->keep_locks = 0;
5378
5379 if (ret < 0)
5380 return ret;
5381
Chris Masona2135012008-06-25 16:01:30 -04005382 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005383 /*
5384 * by releasing the path above we dropped all our locks. A balance
5385 * could have added more items next to the key that used to be
5386 * at the very end of the block. So, check again here and
5387 * advance the path if there are now more items available.
5388 */
Chris Masona2135012008-06-25 16:01:30 -04005389 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005390 if (ret == 0)
5391 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005392 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005393 goto done;
5394 }
Liu Bo0b43e042014-06-09 11:04:49 +08005395 /*
5396 * So the above check misses one case:
5397 * - after releasing the path above, someone has removed the item that
5398 * used to be at the very end of the block, and balance between leafs
5399 * gets another one with bigger key.offset to replace it.
5400 *
5401 * This one should be returned as well, or we can get leaf corruption
5402 * later(esp. in __btrfs_drop_extents()).
5403 *
5404 * And a bit more explanation about this check,
5405 * with ret > 0, the key isn't found, the path points to the slot
5406 * where it should be inserted, so the path->slots[0] item must be the
5407 * bigger one.
5408 */
5409 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5410 ret = 0;
5411 goto done;
5412 }
Chris Masond97e63b2007-02-20 16:40:44 -05005413
Chris Masond3977122009-01-05 21:25:51 -05005414 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005415 if (!path->nodes[level]) {
5416 ret = 1;
5417 goto done;
5418 }
Chris Mason5f39d392007-10-15 16:14:19 -04005419
Chris Masond97e63b2007-02-20 16:40:44 -05005420 slot = path->slots[level] + 1;
5421 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005422 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005423 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005424 if (level == BTRFS_MAX_LEVEL) {
5425 ret = 1;
5426 goto done;
5427 }
Chris Masond97e63b2007-02-20 16:40:44 -05005428 continue;
5429 }
Chris Mason5f39d392007-10-15 16:14:19 -04005430
Chris Mason925baed2008-06-25 16:01:30 -04005431 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005432 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005433 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005434 }
Chris Mason5f39d392007-10-15 16:14:19 -04005435
Chris Mason8e73f272009-04-03 10:14:18 -04005436 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005437 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005438 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005439 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005440 if (ret == -EAGAIN)
5441 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005442
Chris Mason76a05b32009-05-14 13:24:30 -04005443 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005444 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005445 goto done;
5446 }
5447
Chris Mason5cd57b22008-06-25 16:01:30 -04005448 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005449 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005450 if (!ret && time_seq) {
5451 /*
5452 * If we don't get the lock, we may be racing
5453 * with push_leaf_left, holding that lock while
5454 * itself waiting for the leaf we've currently
5455 * locked. To solve this situation, we give up
5456 * on our lock and cycle.
5457 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005458 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005459 btrfs_release_path(path);
5460 cond_resched();
5461 goto again;
5462 }
Chris Mason8e73f272009-04-03 10:14:18 -04005463 if (!ret) {
5464 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005465 btrfs_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005466 }
Chris Mason31533fb2011-07-26 16:01:59 -04005467 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005468 }
Chris Masond97e63b2007-02-20 16:40:44 -05005469 break;
5470 }
5471 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005472 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005473 level--;
5474 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005475 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005476 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005477
Chris Mason5f39d392007-10-15 16:14:19 -04005478 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005479 path->nodes[level] = next;
5480 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005481 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005482 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005483 if (!level)
5484 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005485
Liu Bod07b8522017-01-30 12:23:42 -08005486 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005487 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005488 if (ret == -EAGAIN)
5489 goto again;
5490
Chris Mason76a05b32009-05-14 13:24:30 -04005491 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005492 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005493 goto done;
5494 }
5495
Chris Mason5cd57b22008-06-25 16:01:30 -04005496 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005497 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005498 if (!ret) {
5499 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005500 btrfs_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005501 }
Chris Mason31533fb2011-07-26 16:01:59 -04005502 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005503 }
Chris Masond97e63b2007-02-20 16:40:44 -05005504 }
Chris Mason8e73f272009-04-03 10:14:18 -04005505 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005506done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005507 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005508 path->leave_spinning = old_spinning;
5509 if (!old_spinning)
5510 btrfs_set_path_blocking(path);
5511
5512 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005513}
Chris Mason0b86a832008-03-24 15:01:56 -04005514
Chris Mason3f157a22008-06-25 16:01:31 -04005515/*
5516 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5517 * searching until it gets past min_objectid or finds an item of 'type'
5518 *
5519 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5520 */
Chris Mason0b86a832008-03-24 15:01:56 -04005521int btrfs_previous_item(struct btrfs_root *root,
5522 struct btrfs_path *path, u64 min_objectid,
5523 int type)
5524{
5525 struct btrfs_key found_key;
5526 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005527 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005528 int ret;
5529
Chris Masond3977122009-01-05 21:25:51 -05005530 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005531 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005532 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005533 ret = btrfs_prev_leaf(root, path);
5534 if (ret != 0)
5535 return ret;
5536 } else {
5537 path->slots[0]--;
5538 }
5539 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005540 nritems = btrfs_header_nritems(leaf);
5541 if (nritems == 0)
5542 return 1;
5543 if (path->slots[0] == nritems)
5544 path->slots[0]--;
5545
Chris Mason0b86a832008-03-24 15:01:56 -04005546 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005547 if (found_key.objectid < min_objectid)
5548 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005549 if (found_key.type == type)
5550 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005551 if (found_key.objectid == min_objectid &&
5552 found_key.type < type)
5553 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005554 }
5555 return 1;
5556}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005557
5558/*
5559 * search in extent tree to find a previous Metadata/Data extent item with
5560 * min objecitd.
5561 *
5562 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5563 */
5564int btrfs_previous_extent_item(struct btrfs_root *root,
5565 struct btrfs_path *path, u64 min_objectid)
5566{
5567 struct btrfs_key found_key;
5568 struct extent_buffer *leaf;
5569 u32 nritems;
5570 int ret;
5571
5572 while (1) {
5573 if (path->slots[0] == 0) {
5574 btrfs_set_path_blocking(path);
5575 ret = btrfs_prev_leaf(root, path);
5576 if (ret != 0)
5577 return ret;
5578 } else {
5579 path->slots[0]--;
5580 }
5581 leaf = path->nodes[0];
5582 nritems = btrfs_header_nritems(leaf);
5583 if (nritems == 0)
5584 return 1;
5585 if (path->slots[0] == nritems)
5586 path->slots[0]--;
5587
5588 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5589 if (found_key.objectid < min_objectid)
5590 break;
5591 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5592 found_key.type == BTRFS_METADATA_ITEM_KEY)
5593 return 0;
5594 if (found_key.objectid == min_objectid &&
5595 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5596 break;
5597 }
5598 return 1;
5599}