blob: d56730a6788537ac01cb6848442b80b101187c5f [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;
David Sterba59a0fcd2020-02-27 21:00:45 +010034 const char name[10];
35 const char driver[12];
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020036} btrfs_csums[] = {
37 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
Johannes Thumshirn3951e7f2019-10-07 11:11:01 +020038 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
Johannes Thumshirn3831bf02019-10-07 11:11:02 +020039 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
David Sterba352ae072019-10-07 11:11:02 +020040 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
41 .driver = "blake2b-256" },
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020042};
43
44int btrfs_super_csum_size(const struct btrfs_super_block *s)
45{
46 u16 t = btrfs_super_csum_type(s);
47 /*
48 * csum type is validated at mount time
49 */
50 return btrfs_csums[t].size;
51}
52
53const char *btrfs_super_csum_name(u16 csum_type)
54{
55 /* csum type is validated at mount time */
56 return btrfs_csums[csum_type].name;
57}
58
David Sterbab4e967b2019-10-08 18:41:33 +020059/*
60 * Return driver name if defined, otherwise the name that's also a valid driver
61 * name
62 */
63const char *btrfs_super_csum_driver(u16 csum_type)
64{
65 /* csum type is validated at mount time */
David Sterba59a0fcd2020-02-27 21:00:45 +010066 return btrfs_csums[csum_type].driver[0] ?
67 btrfs_csums[csum_type].driver :
David Sterbab4e967b2019-10-08 18:41:33 +020068 btrfs_csums[csum_type].name;
69}
70
David Sterba604997b2020-07-27 17:38:19 +020071size_t __attribute_const__ btrfs_get_num_csums(void)
David Sterbaf7cea562019-10-07 11:11:03 +020072{
73 return ARRAY_SIZE(btrfs_csums);
74}
75
Chris Mason2c90e5d2007-04-02 10:50:19 -040076struct btrfs_path *btrfs_alloc_path(void)
77{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090078 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040079}
80
Chris Masond352ac62008-09-29 15:18:18 -040081/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040082void btrfs_free_path(struct btrfs_path *p)
83{
Jesper Juhlff175d52010-12-25 21:22:30 +000084 if (!p)
85 return;
David Sterbab3b4aa72011-04-21 01:20:15 +020086 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040087 kmem_cache_free(btrfs_path_cachep, p);
88}
89
Chris Masond352ac62008-09-29 15:18:18 -040090/*
91 * path release drops references on the extent buffers in the path
92 * and it drops any locks held by this path
93 *
94 * It is safe to call this on paths that no locks or extent buffers held.
95 */
David Sterbab3b4aa72011-04-21 01:20:15 +020096noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050097{
98 int i;
Chris Masona2135012008-06-25 16:01:30 -040099
Chris Mason234b63a2007-03-13 10:46:10 -0400100 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400101 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500102 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400103 continue;
104 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400105 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400106 p->locks[i] = 0;
107 }
Chris Mason5f39d392007-10-15 16:14:19 -0400108 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400109 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500110 }
111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/*
114 * safely gets a reference on the root node of a tree. A lock
115 * is not taken, so a concurrent writer may put a different node
116 * at the root of the tree. See btrfs_lock_root_node for the
117 * looping required.
118 *
119 * The extent buffer returned by this has a reference taken, so
120 * it won't disappear. It may stop being the root of the tree
121 * at any time because there are no locks held.
122 */
Chris Mason925baed2008-06-25 16:01:30 -0400123struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
124{
125 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400126
Josef Bacik3083ee22012-03-09 16:01:49 -0500127 while (1) {
128 rcu_read_lock();
129 eb = rcu_dereference(root->node);
130
131 /*
132 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400133 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500134 * the inc_not_zero dance and if it doesn't work then
135 * synchronize_rcu and try again.
136 */
137 if (atomic_inc_not_zero(&eb->refs)) {
138 rcu_read_unlock();
139 break;
140 }
141 rcu_read_unlock();
142 synchronize_rcu();
143 }
Chris Mason925baed2008-06-25 16:01:30 -0400144 return eb;
145}
146
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800147/*
148 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
149 * just get put onto a simple dirty list. Transaction walks this list to make
150 * sure they get properly updated on disk.
Chris Masond352ac62008-09-29 15:18:18 -0400151 */
Chris Mason0b86a832008-03-24 15:01:56 -0400152static void add_root_to_dirty_list(struct btrfs_root *root)
153{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400154 struct btrfs_fs_info *fs_info = root->fs_info;
155
Josef Bacike7070be2014-12-16 08:54:43 -0800156 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
157 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
158 return;
159
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400160 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800161 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
162 /* Want the extent tree to be the last on the list */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900163 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
Josef Bacike7070be2014-12-16 08:54:43 -0800164 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400165 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800166 else
167 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400168 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400169 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400170 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400171}
172
Chris Masond352ac62008-09-29 15:18:18 -0400173/*
174 * used by snapshot creation to make a copy of a root for a tree with
175 * a given objectid. The buffer with the new root node is returned in
176 * cow_ret, and this func returns zero on success or a negative error code.
177 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500178int btrfs_copy_root(struct btrfs_trans_handle *trans,
179 struct btrfs_root *root,
180 struct extent_buffer *buf,
181 struct extent_buffer **cow_ret, u64 new_root_objectid)
182{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400183 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500184 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500185 int ret = 0;
186 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400187 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500188
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800189 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400190 trans->transid != fs_info->running_transaction->transid);
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800191 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Miao Xie27cdeb72014-04-02 19:51:05 +0800192 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500193
194 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400195 if (level == 0)
196 btrfs_item_key(buf, &disk_key, 0);
197 else
198 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400199
David Sterba4d75f8a2014-06-15 01:54:12 +0200200 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
Josef Bacikcf6f34a2020-08-20 11:46:07 -0400201 &disk_key, level, buf->start, 0,
202 BTRFS_NESTING_NEW_ROOT);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400203 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 return PTR_ERR(cow);
205
David Sterba58e80122016-11-08 18:30:31 +0100206 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500207 btrfs_set_header_bytenr(cow, cow->start);
208 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400209 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
210 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
211 BTRFS_HEADER_FLAG_RELOC);
212 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
213 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
214 else
215 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500216
Nikolay Borisovde37aa52018-10-30 16:43:24 +0200217 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -0500218
Chris Masonbe20aa92007-12-17 20:14:01 -0500219 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400220 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700221 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400222 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700223 ret = btrfs_inc_ref(trans, root, cow, 0);
Josef Bacik867ed322021-01-14 14:02:46 -0500224 if (ret) {
Filipe Manana72c99252021-02-04 14:35:44 +0000225 btrfs_tree_unlock(cow);
226 free_extent_buffer(cow);
Josef Bacik867ed322021-01-14 14:02:46 -0500227 btrfs_abort_transaction(trans, ret);
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 return ret;
Josef Bacik867ed322021-01-14 14:02:46 -0500229 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500230
231 btrfs_mark_buffer_dirty(cow);
232 *cow_ret = cow;
233 return 0;
234}
235
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200236enum mod_log_op {
237 MOD_LOG_KEY_REPLACE,
238 MOD_LOG_KEY_ADD,
239 MOD_LOG_KEY_REMOVE,
240 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
241 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
242 MOD_LOG_MOVE_KEYS,
243 MOD_LOG_ROOT_REPLACE,
244};
245
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200246struct tree_mod_root {
247 u64 logical;
248 u8 level;
249};
250
251struct tree_mod_elem {
252 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530253 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200254 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200255 enum mod_log_op op;
256
257 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
258 int slot;
259
260 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
261 u64 generation;
262
263 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
264 struct btrfs_disk_key key;
265 u64 blockptr;
266
267 /* this is used for op == MOD_LOG_MOVE_KEYS */
David Sterbab6dfa352018-03-05 15:31:18 +0100268 struct {
269 int dst_slot;
270 int nr_items;
271 } move;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200272
273 /* this is used for op == MOD_LOG_ROOT_REPLACE */
274 struct tree_mod_root old_root;
275};
276
Jan Schmidt097b8a72012-06-21 11:08:04 +0200277/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700278 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000279 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700280static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000281{
282 return atomic64_inc_return(&fs_info->tree_mod_seq);
283}
284
285/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200286 * This adds a new blocker to the tree mod log's blocker list if the @elem
287 * passed does not already have a sequence number set. So when a caller expects
288 * to record tree modifications, it should ensure to set elem->seq to zero
289 * before calling btrfs_get_tree_mod_seq.
290 * Returns a fresh, unused tree log modification sequence number, even if no new
291 * blocker was added.
292 */
293u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
294 struct seq_list *elem)
295{
David Sterbab1a09f12018-03-05 15:43:41 +0100296 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200297 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700298 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200299 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
300 }
David Sterbab1a09f12018-03-05 15:43:41 +0100301 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200302
Josef Bacikfcebe452014-05-13 17:30:47 -0700303 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200304}
305
306void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
307 struct seq_list *elem)
308{
309 struct rb_root *tm_root;
310 struct rb_node *node;
311 struct rb_node *next;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200312 struct tree_mod_elem *tm;
313 u64 min_seq = (u64)-1;
314 u64 seq_putting = elem->seq;
315
316 if (!seq_putting)
317 return;
318
Filipe Manana7227ff42020-01-22 12:23:20 +0000319 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200320 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200321 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200322
Filipe Manana42836cf2020-01-22 12:23:54 +0000323 if (!list_empty(&fs_info->tree_mod_seq_list)) {
324 struct seq_list *first;
325
326 first = list_first_entry(&fs_info->tree_mod_seq_list,
327 struct seq_list, list);
328 if (seq_putting > first->seq) {
329 /*
330 * Blocker with lower sequence number exists, we
331 * cannot remove anything from the log.
332 */
333 write_unlock(&fs_info->tree_mod_log_lock);
334 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200335 }
Filipe Manana42836cf2020-01-22 12:23:54 +0000336 min_seq = first->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200337 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338
339 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200340 * anything that's lower than the lowest existing (read: blocked)
341 * sequence number can be removed from the tree.
342 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200343 tm_root = &fs_info->tree_mod_log;
344 for (node = rb_first(tm_root); node; node = next) {
345 next = rb_next(node);
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800346 tm = rb_entry(node, struct tree_mod_elem, node);
Filipe Manana6609fee2019-12-06 12:27:39 +0000347 if (tm->seq >= min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200348 continue;
349 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200350 kfree(tm);
351 }
David Sterbab1a09f12018-03-05 15:43:41 +0100352 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200353}
354
355/*
356 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530357 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200358 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530359 * The 'start address' is the logical address of the *new* root node
360 * for root replace operations, or the logical address of the affected
361 * block for all other operations.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200362 */
363static noinline int
364__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
365{
366 struct rb_root *tm_root;
367 struct rb_node **new;
368 struct rb_node *parent = NULL;
369 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200370
David Sterba73e82fe2019-03-27 16:19:55 +0100371 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
372
Josef Bacikfcebe452014-05-13 17:30:47 -0700373 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200374
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200375 tm_root = &fs_info->tree_mod_log;
376 new = &tm_root->rb_node;
377 while (*new) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800378 cur = rb_entry(*new, struct tree_mod_elem, node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200379 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530380 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200381 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530382 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200383 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200384 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200385 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200386 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200387 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000388 else
389 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200390 }
391
392 rb_link_node(&tm->node, parent, new);
393 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000394 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200395}
396
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397/*
398 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
399 * returns zero with the tree_mod_log_lock acquired. The caller must hold
400 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbab1a09f12018-03-05 15:43:41 +0100401 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200402 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200403static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
404 struct extent_buffer *eb) {
405 smp_mb();
406 if (list_empty(&(fs_info)->tree_mod_seq_list))
407 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200408 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200409 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000410
David Sterbab1a09f12018-03-05 15:43:41 +0100411 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000412 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbab1a09f12018-03-05 15:43:41 +0100413 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000414 return 1;
415 }
416
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200417 return 0;
418}
419
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000420/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
421static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
422 struct extent_buffer *eb)
423{
424 smp_mb();
425 if (list_empty(&(fs_info)->tree_mod_seq_list))
426 return 0;
427 if (eb && btrfs_header_level(eb) == 0)
428 return 0;
429
430 return 1;
431}
432
433static struct tree_mod_elem *
434alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
435 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200437 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200438
Josef Bacikc8cc6342013-07-01 16:18:19 -0400439 tm = kzalloc(sizeof(*tm), flags);
440 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000441 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530443 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200444 if (op != MOD_LOG_KEY_ADD) {
445 btrfs_node_key(eb, &tm->key, slot);
446 tm->blockptr = btrfs_node_blockptr(eb, slot);
447 }
448 tm->op = op;
449 tm->slot = slot;
450 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000451 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200452
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000453 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200454}
455
David Sterbae09c2ef2018-03-05 15:09:03 +0100456static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
457 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200458{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000459 struct tree_mod_elem *tm;
460 int ret;
461
David Sterbae09c2ef2018-03-05 15:09:03 +0100462 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200463 return 0;
464
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000465 tm = alloc_tree_mod_elem(eb, slot, op, flags);
466 if (!tm)
467 return -ENOMEM;
468
David Sterbae09c2ef2018-03-05 15:09:03 +0100469 if (tree_mod_dont_log(eb->fs_info, eb)) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000470 kfree(tm);
471 return 0;
472 }
473
David Sterbae09c2ef2018-03-05 15:09:03 +0100474 ret = __tree_mod_log_insert(eb->fs_info, tm);
David Sterbab1a09f12018-03-05 15:43:41 +0100475 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000476 if (ret)
477 kfree(tm);
478
479 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200480}
481
David Sterba6074d452018-03-05 15:03:52 +0100482static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
483 int dst_slot, int src_slot, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200484{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000485 struct tree_mod_elem *tm = NULL;
486 struct tree_mod_elem **tm_list = NULL;
487 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200488 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000489 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200490
David Sterba6074d452018-03-05 15:03:52 +0100491 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200492 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200493
David Sterba176ef8f2017-03-28 14:35:01 +0200494 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000495 if (!tm_list)
496 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200497
David Sterba176ef8f2017-03-28 14:35:01 +0200498 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000499 if (!tm) {
500 ret = -ENOMEM;
501 goto free_tms;
502 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200503
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530504 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200505 tm->slot = src_slot;
506 tm->move.dst_slot = dst_slot;
507 tm->move.nr_items = nr_items;
508 tm->op = MOD_LOG_MOVE_KEYS;
509
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000510 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
511 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
David Sterba176ef8f2017-03-28 14:35:01 +0200512 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000513 if (!tm_list[i]) {
514 ret = -ENOMEM;
515 goto free_tms;
516 }
517 }
518
David Sterba6074d452018-03-05 15:03:52 +0100519 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000520 goto free_tms;
521 locked = 1;
522
523 /*
524 * When we override something during the move, we log these removals.
525 * This can only happen when we move towards the beginning of the
526 * buffer, i.e. dst_slot < src_slot.
527 */
528 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
David Sterba6074d452018-03-05 15:03:52 +0100529 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000530 if (ret)
531 goto free_tms;
532 }
533
David Sterba6074d452018-03-05 15:03:52 +0100534 ret = __tree_mod_log_insert(eb->fs_info, tm);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000535 if (ret)
536 goto free_tms;
David Sterbab1a09f12018-03-05 15:43:41 +0100537 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000538 kfree(tm_list);
539
540 return 0;
541free_tms:
542 for (i = 0; i < nr_items; i++) {
543 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
David Sterba6074d452018-03-05 15:03:52 +0100544 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000545 kfree(tm_list[i]);
546 }
547 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100548 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000549 kfree(tm_list);
550 kfree(tm);
551
552 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200553}
554
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000555static inline int
556__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
557 struct tree_mod_elem **tm_list,
558 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200559{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000560 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200561 int ret;
562
Jan Schmidt097b8a72012-06-21 11:08:04 +0200563 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000564 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
565 if (ret) {
566 for (j = nritems - 1; j > i; j--)
567 rb_erase(&tm_list[j]->node,
568 &fs_info->tree_mod_log);
569 return ret;
570 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200571 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000572
573 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200574}
575
David Sterba95b757c2018-03-05 15:22:30 +0100576static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
577 struct extent_buffer *new_root, int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200578{
David Sterba95b757c2018-03-05 15:22:30 +0100579 struct btrfs_fs_info *fs_info = old_root->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000580 struct tree_mod_elem *tm = NULL;
581 struct tree_mod_elem **tm_list = NULL;
582 int nritems = 0;
583 int ret = 0;
584 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200585
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000586 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200587 return 0;
588
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000589 if (log_removal && btrfs_header_level(old_root) > 0) {
590 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100591 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
David Sterbabcc8e072017-03-28 14:35:42 +0200592 GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000593 if (!tm_list) {
594 ret = -ENOMEM;
595 goto free_tms;
596 }
597 for (i = 0; i < nritems; i++) {
598 tm_list[i] = alloc_tree_mod_elem(old_root, i,
David Sterbabcc8e072017-03-28 14:35:42 +0200599 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000600 if (!tm_list[i]) {
601 ret = -ENOMEM;
602 goto free_tms;
603 }
604 }
605 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000606
David Sterbabcc8e072017-03-28 14:35:42 +0200607 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000608 if (!tm) {
609 ret = -ENOMEM;
610 goto free_tms;
611 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200612
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530613 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200614 tm->old_root.logical = old_root->start;
615 tm->old_root.level = btrfs_header_level(old_root);
616 tm->generation = btrfs_header_generation(old_root);
617 tm->op = MOD_LOG_ROOT_REPLACE;
618
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000619 if (tree_mod_dont_log(fs_info, NULL))
620 goto free_tms;
621
622 if (tm_list)
623 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
624 if (!ret)
625 ret = __tree_mod_log_insert(fs_info, tm);
626
David Sterbab1a09f12018-03-05 15:43:41 +0100627 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000628 if (ret)
629 goto free_tms;
630 kfree(tm_list);
631
632 return ret;
633
634free_tms:
635 if (tm_list) {
636 for (i = 0; i < nritems; i++)
637 kfree(tm_list[i]);
638 kfree(tm_list);
639 }
640 kfree(tm);
641
642 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200643}
644
645static struct tree_mod_elem *
646__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
647 int smallest)
648{
649 struct rb_root *tm_root;
650 struct rb_node *node;
651 struct tree_mod_elem *cur = NULL;
652 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200653
David Sterbab1a09f12018-03-05 15:43:41 +0100654 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200655 tm_root = &fs_info->tree_mod_log;
656 node = tm_root->rb_node;
657 while (node) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800658 cur = rb_entry(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530659 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200660 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530661 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200662 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200663 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200664 node = node->rb_left;
665 } else if (!smallest) {
666 /* we want the node with the highest seq */
667 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200668 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200669 found = cur;
670 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200671 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200672 /* we want the node with the smallest seq */
673 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200674 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200675 found = cur;
676 node = node->rb_right;
677 } else {
678 found = cur;
679 break;
680 }
681 }
David Sterbab1a09f12018-03-05 15:43:41 +0100682 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200683
684 return found;
685}
686
687/*
688 * this returns the element from the log with the smallest time sequence
689 * value that's in the log (the oldest log item). any element with a time
690 * sequence lower than min_seq will be ignored.
691 */
692static struct tree_mod_elem *
693tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
694 u64 min_seq)
695{
696 return __tree_mod_log_search(fs_info, start, min_seq, 1);
697}
698
699/*
700 * this returns the element from the log with the largest time sequence
701 * value that's in the log (the most recent log item). any element with
702 * a time sequence lower than min_seq will be ignored.
703 */
704static struct tree_mod_elem *
705tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
706{
707 return __tree_mod_log_search(fs_info, start, min_seq, 0);
708}
709
David Sterbaed874f02019-03-20 14:22:04 +0100710static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200711 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000712 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200713{
David Sterbaed874f02019-03-20 14:22:04 +0100714 struct btrfs_fs_info *fs_info = dst->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000715 int ret = 0;
716 struct tree_mod_elem **tm_list = NULL;
717 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200718 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000719 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200720
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000721 if (!tree_mod_need_log(fs_info, NULL))
722 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200723
Josef Bacikc8cc6342013-07-01 16:18:19 -0400724 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000725 return 0;
726
David Sterba31e818f2015-02-20 18:00:26 +0100727 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000728 GFP_NOFS);
729 if (!tm_list)
730 return -ENOMEM;
731
732 tm_list_add = tm_list;
733 tm_list_rem = tm_list + nr_items;
734 for (i = 0; i < nr_items; i++) {
735 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
736 MOD_LOG_KEY_REMOVE, GFP_NOFS);
737 if (!tm_list_rem[i]) {
738 ret = -ENOMEM;
739 goto free_tms;
740 }
741
742 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
743 MOD_LOG_KEY_ADD, GFP_NOFS);
744 if (!tm_list_add[i]) {
745 ret = -ENOMEM;
746 goto free_tms;
747 }
748 }
749
750 if (tree_mod_dont_log(fs_info, NULL))
751 goto free_tms;
752 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200753
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200754 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000755 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
756 if (ret)
757 goto free_tms;
758 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
759 if (ret)
760 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200761 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000762
David Sterbab1a09f12018-03-05 15:43:41 +0100763 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000764 kfree(tm_list);
765
766 return 0;
767
768free_tms:
769 for (i = 0; i < nr_items * 2; i++) {
770 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
771 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
772 kfree(tm_list[i]);
773 }
774 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100775 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000776 kfree(tm_list);
777
778 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200779}
780
David Sterbadb7279a2018-03-05 15:14:25 +0100781static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200782{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000783 struct tree_mod_elem **tm_list = NULL;
784 int nritems = 0;
785 int i;
786 int ret = 0;
787
788 if (btrfs_header_level(eb) == 0)
789 return 0;
790
David Sterbadb7279a2018-03-05 15:14:25 +0100791 if (!tree_mod_need_log(eb->fs_info, NULL))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000792 return 0;
793
794 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100795 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000796 if (!tm_list)
797 return -ENOMEM;
798
799 for (i = 0; i < nritems; i++) {
800 tm_list[i] = alloc_tree_mod_elem(eb, i,
801 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
802 if (!tm_list[i]) {
803 ret = -ENOMEM;
804 goto free_tms;
805 }
806 }
807
David Sterbadb7279a2018-03-05 15:14:25 +0100808 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000809 goto free_tms;
810
David Sterbadb7279a2018-03-05 15:14:25 +0100811 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
David Sterbab1a09f12018-03-05 15:43:41 +0100812 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000813 if (ret)
814 goto free_tms;
815 kfree(tm_list);
816
817 return 0;
818
819free_tms:
820 for (i = 0; i < nritems; i++)
821 kfree(tm_list[i]);
822 kfree(tm_list);
823
824 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200825}
826
Chris Masond352ac62008-09-29 15:18:18 -0400827/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400828 * check if the tree block can be shared by multiple trees
829 */
830int btrfs_block_can_be_shared(struct btrfs_root *root,
831 struct extent_buffer *buf)
832{
833 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800834 * Tree blocks not in shareable trees and tree roots are never shared.
835 * If a block was allocated after the last snapshot and the block was
836 * not allocated by tree relocation, we know the block is not shared.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400837 */
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800838 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400839 buf != root->node && buf != root->commit_root &&
840 (btrfs_header_generation(buf) <=
841 btrfs_root_last_snapshot(&root->root_item) ||
842 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
843 return 1;
Nikolay Borisova79865c2018-06-21 09:45:00 +0300844
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400845 return 0;
846}
847
848static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
849 struct btrfs_root *root,
850 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400851 struct extent_buffer *cow,
852 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400854 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400855 u64 refs;
856 u64 owner;
857 u64 flags;
858 u64 new_flags = 0;
859 int ret;
860
861 /*
862 * Backrefs update rules:
863 *
864 * Always use full backrefs for extent pointers in tree block
865 * allocated by tree relocation.
866 *
867 * If a shared tree block is no longer referenced by its owner
868 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
869 * use full backrefs for extent pointers in tree block.
870 *
871 * If a tree block is been relocating
872 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
873 * use full backrefs for extent pointers in tree block.
874 * The reason for this is some operations (such as drop tree)
875 * are only allowed for blocks use full backrefs.
876 */
877
878 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400879 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500880 btrfs_header_level(buf), 1,
881 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700882 if (ret)
883 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700884 if (refs == 0) {
885 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400886 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700887 return ret;
888 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889 } else {
890 refs = 1;
891 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
892 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
893 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
894 else
895 flags = 0;
896 }
897
898 owner = btrfs_header_owner(buf);
899 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
900 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
901
902 if (refs > 1) {
903 if ((owner == root->root_key.objectid ||
904 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
905 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700906 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500907 if (ret)
908 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909
910 if (root->root_key.objectid ==
911 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700912 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500913 if (ret)
914 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700915 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500916 if (ret)
917 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 }
919 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
920 } else {
921
922 if (root->root_key.objectid ==
923 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700924 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700926 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500927 if (ret)
928 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 }
930 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400931 int level = btrfs_header_level(buf);
932
David Sterba42c9d0b2019-03-20 11:54:13 +0100933 ret = btrfs_set_disk_extent_flags(trans, buf,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400934 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700935 if (ret)
936 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937 }
938 } else {
939 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
940 if (root->root_key.objectid ==
941 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700942 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700944 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500945 if (ret)
946 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700947 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500948 if (ret)
949 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400950 }
David Sterba6a884d7d2019-03-20 14:30:02 +0100951 btrfs_clean_tree_block(buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400952 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 }
954 return 0;
955}
956
Filipe Mananaa6279472019-01-25 11:48:51 +0000957static struct extent_buffer *alloc_tree_block_no_bg_flush(
958 struct btrfs_trans_handle *trans,
959 struct btrfs_root *root,
960 u64 parent_start,
961 const struct btrfs_disk_key *disk_key,
962 int level,
963 u64 hint,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400964 u64 empty_size,
965 enum btrfs_lock_nesting nest)
Filipe Mananaa6279472019-01-25 11:48:51 +0000966{
967 struct btrfs_fs_info *fs_info = root->fs_info;
968 struct extent_buffer *ret;
969
970 /*
971 * If we are COWing a node/leaf from the extent, chunk, device or free
972 * space trees, make sure that we do not finish block group creation of
973 * pending block groups. We do this to avoid a deadlock.
974 * COWing can result in allocation of a new chunk, and flushing pending
975 * block groups (btrfs_create_pending_block_groups()) can be triggered
976 * when finishing allocation of a new chunk. Creation of a pending block
977 * group modifies the extent, chunk, device and free space trees,
978 * therefore we could deadlock with ourselves since we are holding a
979 * lock on an extent buffer that btrfs_create_pending_block_groups() may
980 * try to COW later.
981 * For similar reasons, we also need to delay flushing pending block
982 * groups when splitting a leaf or node, from one of those trees, since
983 * we are holding a write lock on it and its parent or when inserting a
984 * new root node for one of those trees.
985 */
986 if (root == fs_info->extent_root ||
987 root == fs_info->chunk_root ||
988 root == fs_info->dev_root ||
989 root == fs_info->free_space_root)
990 trans->can_flush_pending_bgs = false;
991
992 ret = btrfs_alloc_tree_block(trans, root, parent_start,
993 root->root_key.objectid, disk_key, level,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400994 hint, empty_size, nest);
Filipe Mananaa6279472019-01-25 11:48:51 +0000995 trans->can_flush_pending_bgs = true;
996
997 return ret;
998}
999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001000/*
Chris Masond3977122009-01-05 21:25:51 -05001001 * does the dirty work in cow of a single block. The parent block (if
1002 * supplied) is updated to point to the new cow copy. The new buffer is marked
1003 * dirty and returned locked. If you modify the block it needs to be marked
1004 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001005 *
1006 * search_start -- an allocation hint for the new block
1007 *
Chris Masond3977122009-01-05 21:25:51 -05001008 * empty_size -- a hint that you plan on doing more cow. This is the size in
1009 * bytes the allocator should try to find free next to the block it returns.
1010 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001011 */
Chris Masond3977122009-01-05 21:25:51 -05001012static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001013 struct btrfs_root *root,
1014 struct extent_buffer *buf,
1015 struct extent_buffer *parent, int parent_slot,
1016 struct extent_buffer **cow_ret,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001017 u64 search_start, u64 empty_size,
1018 enum btrfs_lock_nesting nest)
Chris Mason6702ed42007-08-07 16:15:09 -04001019{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001020 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001022 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001023 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001024 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001025 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001026 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001027
Chris Mason925baed2008-06-25 16:01:30 -04001028 if (*cow_ret == buf)
1029 unlock_orig = 1;
1030
Chris Masonb9447ef82009-03-09 11:45:38 -04001031 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001032
Qu Wenruo92a7cc42020-05-15 14:01:40 +08001033 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001034 trans->transid != fs_info->running_transaction->transid);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08001035 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001036 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001037
Chris Mason7bb86312007-12-11 09:25:06 -05001038 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 if (level == 0)
1041 btrfs_item_key(buf, &disk_key, 0);
1042 else
1043 btrfs_node_key(buf, &disk_key, 0);
1044
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001045 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1046 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047
Filipe Mananaa6279472019-01-25 11:48:51 +00001048 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001049 level, search_start, empty_size, nest);
Chris Mason6702ed42007-08-07 16:15:09 -04001050 if (IS_ERR(cow))
1051 return PTR_ERR(cow);
1052
Chris Masonb4ce94d2009-02-04 09:25:08 -05001053 /* cow is set to blocking by btrfs_init_new_buffer */
1054
David Sterba58e80122016-11-08 18:30:31 +01001055 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001056 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001057 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1059 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1060 BTRFS_HEADER_FLAG_RELOC);
1061 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1062 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1063 else
1064 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001065
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001066 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05001067
Mark Fashehbe1a5562011-08-08 13:20:18 -07001068 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001069 if (ret) {
Josef Bacik572c83a2020-09-29 08:53:54 -04001070 btrfs_tree_unlock(cow);
1071 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -04001072 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001073 return ret;
1074 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001075
Qu Wenruo92a7cc42020-05-15 14:01:40 +08001076 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001077 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001078 if (ret) {
Josef Bacik572c83a2020-09-29 08:53:54 -04001079 btrfs_tree_unlock(cow);
1080 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -04001081 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001082 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001083 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001084 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001085
Chris Mason6702ed42007-08-07 16:15:09 -04001086 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001087 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001088 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1089 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1090 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001091
David Sterba67439da2019-10-08 13:28:47 +02001092 atomic_inc(&cow->refs);
David Sterbad9d19a02018-03-05 16:35:29 +01001093 ret = tree_mod_log_insert_root(root->node, cow, 1);
1094 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001095 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001096
Yan, Zhengf0486c62010-05-16 10:46:25 -04001097 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001098 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001099 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001100 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001101 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102 WARN_ON(trans->transid != btrfs_header_generation(parent));
David Sterbae09c2ef2018-03-05 15:09:03 +01001103 tree_mod_log_insert_key(parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001104 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001105 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001106 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001107 btrfs_set_node_ptr_generation(parent, parent_slot,
1108 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001109 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001110 if (last_ref) {
David Sterbadb7279a2018-03-05 15:14:25 +01001111 ret = tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001112 if (ret) {
Josef Bacik572c83a2020-09-29 08:53:54 -04001113 btrfs_tree_unlock(cow);
1114 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -04001115 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001116 return ret;
1117 }
1118 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001119 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001120 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001121 }
Chris Mason925baed2008-06-25 16:01:30 -04001122 if (unlock_orig)
1123 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001124 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001125 btrfs_mark_buffer_dirty(cow);
1126 *cow_ret = cow;
1127 return 0;
1128}
1129
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001130/*
1131 * returns the logical address of the oldest predecessor of the given root.
1132 * entries older than time_seq are ignored.
1133 */
David Sterbabcd24da2018-03-05 15:33:18 +01001134static struct tree_mod_elem *__tree_mod_log_oldest_root(
1135 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001136{
1137 struct tree_mod_elem *tm;
1138 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001139 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001140 int looped = 0;
1141
1142 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001143 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001144
1145 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301146 * the very last operation that's logged for a root is the
1147 * replacement operation (if it is replaced at all). this has
1148 * the logical address of the *new* root, making it the very
1149 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001150 */
1151 while (1) {
David Sterbabcd24da2018-03-05 15:33:18 +01001152 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001153 time_seq);
1154 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001155 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001156 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001157 * if there are no tree operation for the oldest root, we simply
1158 * return it. this should only happen if that (old) root is at
1159 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001160 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001161 if (!tm)
1162 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001163
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001164 /*
1165 * if there's an operation that's not a root replacement, we
1166 * found the oldest version of our root. normally, we'll find a
1167 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1168 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001169 if (tm->op != MOD_LOG_ROOT_REPLACE)
1170 break;
1171
1172 found = tm;
1173 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001174 looped = 1;
1175 }
1176
Jan Schmidta95236d2012-06-05 16:41:24 +02001177 /* if there's no old root to return, return what we found instead */
1178 if (!found)
1179 found = tm;
1180
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001181 return found;
1182}
1183
1184/*
1185 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001186 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001187 * time_seq).
1188 */
1189static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001190__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1191 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001192{
1193 u32 n;
1194 struct rb_node *next;
1195 struct tree_mod_elem *tm = first_tm;
1196 unsigned long o_dst;
1197 unsigned long o_src;
1198 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1199
1200 n = btrfs_header_nritems(eb);
David Sterbab1a09f12018-03-05 15:43:41 +01001201 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001202 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001203 /*
1204 * all the operations are recorded with the operator used for
1205 * the modification. as we're going backwards, we do the
1206 * opposite of each operation here.
1207 */
1208 switch (tm->op) {
1209 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1210 BUG_ON(tm->slot < n);
Marcos Paulo de Souzac730ae02020-06-16 15:54:29 -03001211 fallthrough;
Liu Bo95c80bb2012-10-19 09:50:52 +00001212 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001213 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001214 btrfs_set_node_key(eb, &tm->key, tm->slot);
1215 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1216 btrfs_set_node_ptr_generation(eb, tm->slot,
1217 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001218 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001219 break;
1220 case MOD_LOG_KEY_REPLACE:
1221 BUG_ON(tm->slot >= n);
1222 btrfs_set_node_key(eb, &tm->key, tm->slot);
1223 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1224 btrfs_set_node_ptr_generation(eb, tm->slot,
1225 tm->generation);
1226 break;
1227 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001228 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001229 n--;
1230 break;
1231 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001232 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1233 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1234 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001235 tm->move.nr_items * p_size);
1236 break;
1237 case MOD_LOG_ROOT_REPLACE:
1238 /*
1239 * this operation is special. for roots, this must be
1240 * handled explicitly before rewinding.
1241 * for non-roots, this operation may exist if the node
1242 * was a root: root A -> child B; then A gets empty and
1243 * B is promoted to the new root. in the mod log, we'll
1244 * have a root-replace operation for B, a tree block
1245 * that is no root. we simply ignore that operation.
1246 */
1247 break;
1248 }
1249 next = rb_next(&tm->node);
1250 if (!next)
1251 break;
Geliang Tang6b4df8b2016-12-19 22:53:41 +08001252 tm = rb_entry(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301253 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001254 break;
1255 }
David Sterbab1a09f12018-03-05 15:43:41 +01001256 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001257 btrfs_set_header_nritems(eb, n);
1258}
1259
Jan Schmidt47fb0912013-04-13 13:19:55 +00001260/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001261 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001262 * is returned. If rewind operations happen, a fresh buffer is returned. The
1263 * returned buffer is always read-locked. If the returned buffer is not the
1264 * input buffer, the lock on the input buffer is released and the input buffer
1265 * is freed (its refcount is decremented).
1266 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001267static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001268tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1269 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001270{
1271 struct extent_buffer *eb_rewin;
1272 struct tree_mod_elem *tm;
1273
1274 if (!time_seq)
1275 return eb;
1276
1277 if (btrfs_header_level(eb) == 0)
1278 return eb;
1279
1280 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1281 if (!tm)
1282 return eb;
1283
1284 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1285 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001286 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001287 if (!eb_rewin) {
Josef Bacikac5887c2020-08-20 11:46:10 -04001288 btrfs_tree_read_unlock(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001289 free_extent_buffer(eb);
1290 return NULL;
1291 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001292 btrfs_set_header_bytenr(eb_rewin, eb->start);
1293 btrfs_set_header_backref_rev(eb_rewin,
1294 btrfs_header_backref_rev(eb));
1295 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001296 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001297 } else {
1298 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001299 if (!eb_rewin) {
Josef Bacikac5887c2020-08-20 11:46:10 -04001300 btrfs_tree_read_unlock(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001301 free_extent_buffer(eb);
1302 return NULL;
1303 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001304 }
1305
Josef Bacikac5887c2020-08-20 11:46:10 -04001306 btrfs_tree_read_unlock(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307 free_extent_buffer(eb);
1308
Josef Bacikd3beaa22020-08-10 11:42:31 -04001309 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
1310 eb_rewin, btrfs_header_level(eb_rewin));
Jan Schmidt47fb0912013-04-13 13:19:55 +00001311 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001312 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001313 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001314 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001315
1316 return eb_rewin;
1317}
1318
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001319/*
1320 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1321 * value. If there are no changes, the current root->root_node is returned. If
1322 * anything changed in between, there's a fresh buffer allocated on which the
1323 * rewind operations are done. In any case, the returned buffer is read locked.
1324 * Returns NULL on error (with no locks held).
1325 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001326static inline struct extent_buffer *
1327get_old_root(struct btrfs_root *root, u64 time_seq)
1328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001329 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001330 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001331 struct extent_buffer *eb = NULL;
1332 struct extent_buffer *eb_root;
Filipe Mananaefad8a82019-08-12 19:14:29 +01001333 u64 eb_root_owner = 0;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001334 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001335 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001336 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001337 u64 logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001338 int level;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001339
Jan Schmidt30b04632013-04-13 13:19:54 +00001340 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001341 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001342 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001343 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001344
Jan Schmidta95236d2012-06-05 16:41:24 +02001345 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1346 old_root = &tm->old_root;
1347 old_generation = tm->generation;
1348 logical = old_root->logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001349 level = old_root->level;
Jan Schmidta95236d2012-06-05 16:41:24 +02001350 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001351 logical = eb_root->start;
Qu Wenruo581c1762018-03-29 09:08:11 +08001352 level = btrfs_header_level(eb_root);
Jan Schmidta95236d2012-06-05 16:41:24 +02001353 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001354
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001355 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001356 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001357 btrfs_tree_read_unlock(eb_root);
1358 free_extent_buffer(eb_root);
Josef Bacik1b7ec852020-11-05 10:45:18 -05001359 old = read_tree_block(fs_info, logical, root->root_key.objectid,
1360 0, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08001361 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1362 if (!IS_ERR(old))
1363 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001364 btrfs_warn(fs_info,
1365 "failed to read tree block %llu from get_old_root",
1366 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001367 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001368 eb = btrfs_clone_extent_buffer(old);
1369 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001370 }
1371 } else if (old_root) {
Filipe Mananaefad8a82019-08-12 19:14:29 +01001372 eb_root_owner = btrfs_header_owner(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001373 btrfs_tree_read_unlock(eb_root);
1374 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001375 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001376 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001377 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacikac5887c2020-08-20 11:46:10 -04001378 btrfs_tree_read_unlock(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001379 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001380 }
1381
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001382 if (!eb)
1383 return NULL;
Jan Schmidta95236d2012-06-05 16:41:24 +02001384 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001385 btrfs_set_header_bytenr(eb, eb->start);
1386 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Filipe Mananaefad8a82019-08-12 19:14:29 +01001387 btrfs_set_header_owner(eb, eb_root_owner);
Jan Schmidta95236d2012-06-05 16:41:24 +02001388 btrfs_set_header_level(eb, old_root->level);
1389 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001390 }
Josef Bacikd3beaa22020-08-10 11:42:31 -04001391 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1392 btrfs_header_level(eb));
1393 btrfs_tree_read_lock(eb);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001394 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001395 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001396 else
1397 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001398 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001399
1400 return eb;
1401}
1402
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001403int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1404{
1405 struct tree_mod_elem *tm;
1406 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001407 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001408
David Sterbabcd24da2018-03-05 15:33:18 +01001409 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001410 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1411 level = tm->old_root.level;
1412 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001413 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001414 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001415 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001416
1417 return level;
1418}
1419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001420static inline int should_cow_block(struct btrfs_trans_handle *trans,
1421 struct btrfs_root *root,
1422 struct extent_buffer *buf)
1423{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001424 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001425 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001426
David Sterbad1980132018-03-16 02:39:40 +01001427 /* Ensure we can see the FORCE_COW bit */
1428 smp_mb__before_atomic();
Liu Bof1ebcc72011-11-14 20:48:06 -05001429
1430 /*
1431 * We do not need to cow a block if
1432 * 1) this block is not created or changed in this transaction;
1433 * 2) this block does not belong to TREE_RELOC tree;
1434 * 3) the root is not forced COW.
1435 *
1436 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001437 * when we create snapshot during committing the transaction,
Andrea Gelmini52042d82018-11-28 12:05:13 +01001438 * after we've finished copying src root, we must COW the shared
Liu Bof1ebcc72011-11-14 20:48:06 -05001439 * block to ensure the metadata consistency.
1440 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 if (btrfs_header_generation(buf) == trans->transid &&
1442 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1443 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001444 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001445 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001446 return 0;
1447 return 1;
1448}
1449
Chris Masond352ac62008-09-29 15:18:18 -04001450/*
1451 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001452 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001453 * once per transaction, as long as it hasn't been written yet
1454 */
Chris Masond3977122009-01-05 21:25:51 -05001455noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001456 struct btrfs_root *root, struct extent_buffer *buf,
1457 struct extent_buffer *parent, int parent_slot,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001458 struct extent_buffer **cow_ret,
1459 enum btrfs_lock_nesting nest)
Chris Mason02217ed2007-03-02 16:08:05 -05001460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001461 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001462 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001463 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001464
Josef Bacik83354f02018-11-30 11:52:13 -05001465 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1466 btrfs_err(fs_info,
1467 "COW'ing blocks on a fs root that's being dropped");
1468
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001469 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001470 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001471 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001472 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001473
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001474 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001475 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001476 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001477
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001478 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001479 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001480 *cow_ret = buf;
1481 return 0;
1482 }
Chris Masonc4876852009-02-04 09:24:25 -05001483
Byongho Leeee221842015-12-15 01:42:10 +09001484 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001485
Qu Wenruof616f5c2019-01-23 15:15:17 +08001486 /*
1487 * Before CoWing this block for later modification, check if it's
1488 * the subtree root and do the delayed subtree trace if needed.
1489 *
1490 * Also We don't care about the error, as it's handled internally.
1491 */
1492 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001493 ret = __btrfs_cow_block(trans, root, buf, parent,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001494 parent_slot, cow_ret, search_start, 0, nest);
liubo1abe9b82011-03-24 11:18:59 +00001495
1496 trace_btrfs_cow_block(root, buf, *cow_ret);
1497
Chris Masonf510cfe2007-10-15 16:14:48 -04001498 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001499}
Josef Bacikf75e2b72020-12-16 11:18:43 -05001500ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
Chris Mason6702ed42007-08-07 16:15:09 -04001501
Chris Masond352ac62008-09-29 15:18:18 -04001502/*
1503 * helper function for defrag to decide if two blocks pointed to by a
1504 * node are actually close by
1505 */
Chris Mason6b800532007-10-15 16:17:34 -04001506static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001507{
Chris Mason6b800532007-10-15 16:17:34 -04001508 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001509 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001510 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001511 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001512 return 0;
1513}
1514
David Sterbace6ef5a2020-06-08 16:06:07 +02001515#ifdef __LITTLE_ENDIAN
1516
1517/*
1518 * Compare two keys, on little-endian the disk order is same as CPU order and
1519 * we can avoid the conversion.
1520 */
1521static int comp_keys(const struct btrfs_disk_key *disk_key,
1522 const struct btrfs_key *k2)
1523{
1524 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
1525
1526 return btrfs_comp_cpu_keys(k1, k2);
1527}
1528
1529#else
1530
Chris Mason081e9572007-11-06 10:26:24 -05001531/*
1532 * compare two keys in a memcmp fashion
1533 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001534static int comp_keys(const struct btrfs_disk_key *disk,
1535 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001536{
1537 struct btrfs_key k1;
1538
1539 btrfs_disk_key_to_cpu(&k1, disk);
1540
Diego Calleja20736ab2009-07-24 11:06:52 -04001541 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001542}
David Sterbace6ef5a2020-06-08 16:06:07 +02001543#endif
Chris Mason081e9572007-11-06 10:26:24 -05001544
Josef Bacikf3465ca2008-11-12 14:19:50 -05001545/*
1546 * same as comp_keys only with two btrfs_key's
1547 */
David Sterbae1f60a62019-10-01 19:57:39 +02001548int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001549{
1550 if (k1->objectid > k2->objectid)
1551 return 1;
1552 if (k1->objectid < k2->objectid)
1553 return -1;
1554 if (k1->type > k2->type)
1555 return 1;
1556 if (k1->type < k2->type)
1557 return -1;
1558 if (k1->offset > k2->offset)
1559 return 1;
1560 if (k1->offset < k2->offset)
1561 return -1;
1562 return 0;
1563}
Chris Mason081e9572007-11-06 10:26:24 -05001564
Chris Masond352ac62008-09-29 15:18:18 -04001565/*
1566 * this is used by the defrag code to go through all the
1567 * leaves pointed to by a node and reallocate them so that
1568 * disk order is close to key order
1569 */
Chris Mason6702ed42007-08-07 16:15:09 -04001570int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001571 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001572 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001573 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001574{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001575 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001576 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001577 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -04001578 u64 search_start = *last_ret;
1579 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001580 u64 other;
1581 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001582 int end_slot;
1583 int i;
1584 int err = 0;
Chris Mason6b800532007-10-15 16:17:34 -04001585 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001586 int progress_passed = 0;
1587 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001588
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001589 WARN_ON(trans->transaction != fs_info->running_transaction);
1590 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001591
Chris Mason6b800532007-10-15 16:17:34 -04001592 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001593 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001594 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001595
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001596 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001597 return 0;
1598
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001599 for (i = start_slot; i <= end_slot; i++) {
Chris Mason6702ed42007-08-07 16:15:09 -04001600 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001601
Chris Mason081e9572007-11-06 10:26:24 -05001602 btrfs_node_key(parent, &disk_key, i);
1603 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1604 continue;
1605
1606 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001607 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001608 if (last_block == 0)
1609 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001610
Chris Mason6702ed42007-08-07 16:15:09 -04001611 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001612 other = btrfs_node_blockptr(parent, i - 1);
1613 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001614 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001615 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001616 other = btrfs_node_blockptr(parent, i + 1);
1617 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001618 }
Chris Masone9d0b132007-08-10 14:06:19 -04001619 if (close) {
1620 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001621 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001622 }
Chris Mason6702ed42007-08-07 16:15:09 -04001623
Josef Bacik206983b2020-11-05 10:45:10 -05001624 cur = btrfs_read_node_slot(parent, i);
1625 if (IS_ERR(cur))
1626 return PTR_ERR(cur);
Chris Masone9d0b132007-08-10 14:06:19 -04001627 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001628 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001629
Chris Masone7a84562008-06-25 16:01:31 -04001630 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001631 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001632 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001633 min(16 * blocksize,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001634 (end_slot - i) * blocksize),
1635 BTRFS_NESTING_COW);
Yan252c38f2007-08-29 09:11:44 -04001636 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001637 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001638 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001639 break;
Yan252c38f2007-08-29 09:11:44 -04001640 }
Chris Masone7a84562008-06-25 16:01:31 -04001641 search_start = cur->start;
1642 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001643 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001644 btrfs_tree_unlock(cur);
1645 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001646 }
1647 return err;
1648}
1649
Chris Mason74123bd2007-02-02 11:05:29 -05001650/*
Chris Mason5f39d392007-10-15 16:14:19 -04001651 * search for key in the extent_buffer. The items start at offset p,
1652 * and they are item_size apart. There are 'max' items in p.
1653 *
Chris Mason74123bd2007-02-02 11:05:29 -05001654 * the slot in the array is returned via slot, and it points to
1655 * the place where you would insert key if it is not found in
1656 * the array.
1657 *
1658 * slot may point to max if the key is bigger than all of the keys
1659 */
Chris Masone02119d2008-09-05 16:13:11 -04001660static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001661 unsigned long p, int item_size,
1662 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001663 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001664{
1665 int low = 0;
1666 int high = max;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001667 int ret;
David Sterba5cd17f32020-04-29 23:23:37 +02001668 const int key_size = sizeof(struct btrfs_disk_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001669
Liu Bo5e24e9a2016-06-23 16:32:45 -07001670 if (low > high) {
1671 btrfs_err(eb->fs_info,
1672 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1673 __func__, low, high, eb->start,
1674 btrfs_header_owner(eb), btrfs_header_level(eb));
1675 return -EINVAL;
1676 }
1677
Chris Masond3977122009-01-05 21:25:51 -05001678 while (low < high) {
David Sterba5cd17f32020-04-29 23:23:37 +02001679 unsigned long oip;
1680 unsigned long offset;
1681 struct btrfs_disk_key *tmp;
1682 struct btrfs_disk_key unaligned;
1683 int mid;
1684
Chris Masonbe0e5c02007-01-26 15:51:26 -05001685 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001686 offset = p + mid * item_size;
David Sterba5cd17f32020-04-29 23:23:37 +02001687 oip = offset_in_page(offset);
Chris Mason5f39d392007-10-15 16:14:19 -04001688
David Sterba5cd17f32020-04-29 23:23:37 +02001689 if (oip + key_size <= PAGE_SIZE) {
Qu Wenruo884b07d2020-12-02 14:48:04 +08001690 const unsigned long idx = get_eb_page_index(offset);
David Sterba5cd17f32020-04-29 23:23:37 +02001691 char *kaddr = page_address(eb->pages[idx]);
Chris Mason934d3752008-12-08 16:43:10 -05001692
Qu Wenruo884b07d2020-12-02 14:48:04 +08001693 oip = get_eb_offset_in_page(eb, offset);
David Sterba5cd17f32020-04-29 23:23:37 +02001694 tmp = (struct btrfs_disk_key *)(kaddr + oip);
Chris Mason5f39d392007-10-15 16:14:19 -04001695 } else {
David Sterba5cd17f32020-04-29 23:23:37 +02001696 read_extent_buffer(eb, &unaligned, offset, key_size);
1697 tmp = &unaligned;
Chris Mason5f39d392007-10-15 16:14:19 -04001698 }
David Sterba5cd17f32020-04-29 23:23:37 +02001699
Chris Masonbe0e5c02007-01-26 15:51:26 -05001700 ret = comp_keys(tmp, key);
1701
1702 if (ret < 0)
1703 low = mid + 1;
1704 else if (ret > 0)
1705 high = mid;
1706 else {
1707 *slot = mid;
1708 return 0;
1709 }
1710 }
1711 *slot = low;
1712 return 1;
1713}
1714
Chris Mason97571fd2007-02-24 13:39:08 -05001715/*
1716 * simple bin_search frontend that does the right thing for
1717 * leaves vs nodes
1718 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001719int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
Qu Wenruoe3b83362020-04-17 15:08:21 +08001720 int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001721{
Qu Wenruoe3b83362020-04-17 15:08:21 +08001722 if (btrfs_header_level(eb) == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001723 return generic_bin_search(eb,
1724 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001725 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001726 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001727 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001728 else
Chris Mason5f39d392007-10-15 16:14:19 -04001729 return generic_bin_search(eb,
1730 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001731 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001732 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001733 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001734}
1735
Yan, Zhengf0486c62010-05-16 10:46:25 -04001736static void root_add_used(struct btrfs_root *root, u32 size)
1737{
1738 spin_lock(&root->accounting_lock);
1739 btrfs_set_root_used(&root->root_item,
1740 btrfs_root_used(&root->root_item) + size);
1741 spin_unlock(&root->accounting_lock);
1742}
1743
1744static void root_sub_used(struct btrfs_root *root, u32 size)
1745{
1746 spin_lock(&root->accounting_lock);
1747 btrfs_set_root_used(&root->root_item,
1748 btrfs_root_used(&root->root_item) - size);
1749 spin_unlock(&root->accounting_lock);
1750}
1751
Chris Masond352ac62008-09-29 15:18:18 -04001752/* given a node and slot number, this reads the blocks it points to. The
1753 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001754 */
David Sterba4b231ae2019-08-21 19:16:27 +02001755struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1756 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001757{
Chris Masonca7a79a2008-05-12 12:59:19 -04001758 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001759 struct extent_buffer *eb;
Qu Wenruo581c1762018-03-29 09:08:11 +08001760 struct btrfs_key first_key;
Josef Bacik416bc652013-04-23 14:17:42 -04001761
Liu Bofb770ae2016-07-05 12:10:14 -07001762 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1763 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001764
1765 BUG_ON(level == 0);
1766
Qu Wenruo581c1762018-03-29 09:08:11 +08001767 btrfs_node_key_to_cpu(parent, &first_key, slot);
David Sterbad0d20b02019-03-20 14:54:01 +01001768 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
Josef Bacik1b7ec852020-11-05 10:45:18 -05001769 btrfs_header_owner(parent),
Qu Wenruo581c1762018-03-29 09:08:11 +08001770 btrfs_node_ptr_generation(parent, slot),
1771 level - 1, &first_key);
Liu Bofb770ae2016-07-05 12:10:14 -07001772 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1773 free_extent_buffer(eb);
1774 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001775 }
1776
1777 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001778}
1779
Chris Masond352ac62008-09-29 15:18:18 -04001780/*
1781 * node level balancing, used to make sure nodes are in proper order for
1782 * item deletion. We balance from the top down, so we have to make sure
1783 * that a deletion won't leave an node completely empty later on.
1784 */
Chris Masone02119d2008-09-05 16:13:11 -04001785static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001786 struct btrfs_root *root,
1787 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001788{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001789 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001790 struct extent_buffer *right = NULL;
1791 struct extent_buffer *mid;
1792 struct extent_buffer *left = NULL;
1793 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001794 int ret = 0;
1795 int wret;
1796 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001797 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001798 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001799
Liu Bo98e6b1e2018-09-12 06:06:23 +08001800 ASSERT(level > 0);
Chris Masonbb803952007-03-01 12:04:21 -05001801
Chris Mason5f39d392007-10-15 16:14:19 -04001802 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001803
Josef Bacikac5887c2020-08-20 11:46:10 -04001804 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
Chris Mason7bb86312007-12-11 09:25:06 -05001805 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1806
Chris Mason1d4f8a02007-03-13 09:28:32 -04001807 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001808
Li Zefana05a9bb2011-09-06 16:55:34 +08001809 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001810 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001811 pslot = path->slots[level + 1];
1812 }
Chris Masonbb803952007-03-01 12:04:21 -05001813
Chris Mason40689472007-03-17 14:29:23 -04001814 /*
1815 * deal with the case where there is only one pointer in the root
1816 * by promoting the node below to a root
1817 */
Chris Mason5f39d392007-10-15 16:14:19 -04001818 if (!parent) {
1819 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001820
Chris Mason5f39d392007-10-15 16:14:19 -04001821 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001822 return 0;
1823
1824 /* promote the child to a root */
David Sterba4b231ae2019-08-21 19:16:27 +02001825 child = btrfs_read_node_slot(mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001826 if (IS_ERR(child)) {
1827 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001828 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001829 goto enospc;
1830 }
1831
Chris Mason925baed2008-06-25 16:01:30 -04001832 btrfs_tree_lock(child);
Josef Bacik9631e4c2020-08-20 11:46:03 -04001833 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
1834 BTRFS_NESTING_COW);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001835 if (ret) {
1836 btrfs_tree_unlock(child);
1837 free_extent_buffer(child);
1838 goto enospc;
1839 }
Yan2f375ab2008-02-01 14:58:07 -05001840
David Sterbad9d19a02018-03-05 16:35:29 +01001841 ret = tree_mod_log_insert_root(root->node, child, 1);
1842 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001843 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001844
Chris Mason0b86a832008-03-24 15:01:56 -04001845 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001846 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001847
Chris Mason925baed2008-06-25 16:01:30 -04001848 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001849 path->nodes[level] = NULL;
David Sterba6a884d7d2019-03-20 14:30:02 +01001850 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001851 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001852 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001853 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001854
1855 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001856 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001857 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001858 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001859 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001860 }
Chris Mason5f39d392007-10-15 16:14:19 -04001861 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001862 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001863 return 0;
1864
David Sterba4b231ae2019-08-21 19:16:27 +02001865 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001866 if (IS_ERR(left))
1867 left = NULL;
1868
Chris Mason5f39d392007-10-15 16:14:19 -04001869 if (left) {
Josef Bacikbf774672020-08-20 11:46:04 -04001870 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
Chris Mason5f39d392007-10-15 16:14:19 -04001871 wret = btrfs_cow_block(trans, root, left,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001872 parent, pslot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001873 BTRFS_NESTING_LEFT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04001874 if (wret) {
1875 ret = wret;
1876 goto enospc;
1877 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001878 }
Liu Bofb770ae2016-07-05 12:10:14 -07001879
David Sterba4b231ae2019-08-21 19:16:27 +02001880 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001881 if (IS_ERR(right))
1882 right = NULL;
1883
Chris Mason5f39d392007-10-15 16:14:19 -04001884 if (right) {
Josef Bacikbf774672020-08-20 11:46:04 -04001885 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
Chris Mason5f39d392007-10-15 16:14:19 -04001886 wret = btrfs_cow_block(trans, root, right,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001887 parent, pslot + 1, &right,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001888 BTRFS_NESTING_RIGHT_COW);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001889 if (wret) {
1890 ret = wret;
1891 goto enospc;
1892 }
1893 }
1894
1895 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (left) {
1897 orig_slot += btrfs_header_nritems(left);
David Sterbad30a6682019-03-20 14:16:45 +01001898 wret = push_node_left(trans, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001899 if (wret < 0)
1900 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001901 }
Chris Mason79f95c82007-03-01 15:16:26 -05001902
1903 /*
1904 * then try to empty the right most buffer into the middle
1905 */
Chris Mason5f39d392007-10-15 16:14:19 -04001906 if (right) {
David Sterbad30a6682019-03-20 14:16:45 +01001907 wret = push_node_left(trans, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001908 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001909 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001910 if (btrfs_header_nritems(right) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001911 btrfs_clean_tree_block(right);
Chris Mason925baed2008-06-25 16:01:30 -04001912 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001913 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001914 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001915 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001916 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001917 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001918 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001919 struct btrfs_disk_key right_key;
1920 btrfs_node_key(right, &right_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001921 ret = tree_mod_log_insert_key(parent, pslot + 1,
1922 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1923 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001924 btrfs_set_node_key(parent, &right_key, pslot + 1);
1925 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001926 }
1927 }
Chris Mason5f39d392007-10-15 16:14:19 -04001928 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001929 /*
1930 * we're not allowed to leave a node with one item in the
1931 * tree during a delete. A deletion from lower in the tree
1932 * could try to delete the only pointer in this node.
1933 * So, pull some keys from the left.
1934 * There has to be a left pointer at this point because
1935 * otherwise we would have pulled some pointers from the
1936 * right
1937 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001938 if (!left) {
1939 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001940 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001941 goto enospc;
1942 }
David Sterba55d32ed2019-03-20 14:18:06 +01001943 wret = balance_node_right(trans, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001944 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001945 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001946 goto enospc;
1947 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001948 if (wret == 1) {
David Sterbad30a6682019-03-20 14:16:45 +01001949 wret = push_node_left(trans, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04001950 if (wret < 0)
1951 ret = wret;
1952 }
Chris Mason79f95c82007-03-01 15:16:26 -05001953 BUG_ON(wret == 1);
1954 }
Chris Mason5f39d392007-10-15 16:14:19 -04001955 if (btrfs_header_nritems(mid) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001956 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001957 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001958 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001959 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001960 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001961 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001962 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001963 } else {
1964 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001965 struct btrfs_disk_key mid_key;
1966 btrfs_node_key(mid, &mid_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001967 ret = tree_mod_log_insert_key(parent, pslot,
1968 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1969 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 btrfs_set_node_key(parent, &mid_key, pslot);
1971 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001972 }
Chris Masonbb803952007-03-01 12:04:21 -05001973
Chris Mason79f95c82007-03-01 15:16:26 -05001974 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001975 if (left) {
1976 if (btrfs_header_nritems(left) > orig_slot) {
David Sterba67439da2019-10-08 13:28:47 +02001977 atomic_inc(&left->refs);
Chris Mason925baed2008-06-25 16:01:30 -04001978 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001979 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001980 path->slots[level + 1] -= 1;
1981 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001982 if (mid) {
1983 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001984 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001985 }
Chris Masonbb803952007-03-01 12:04:21 -05001986 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001987 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001988 path->slots[level] = orig_slot;
1989 }
1990 }
Chris Mason79f95c82007-03-01 15:16:26 -05001991 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001992 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001993 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001994 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001995enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001996 if (right) {
1997 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001998 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001999 }
2000 if (left) {
2001 if (path->nodes[level] != left)
2002 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002003 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002004 }
Chris Masonbb803952007-03-01 12:04:21 -05002005 return ret;
2006}
2007
Chris Masond352ac62008-09-29 15:18:18 -04002008/* Node balancing for insertion. Here we only split or push nodes around
2009 * when they are completely full. This is also done top down, so we
2010 * have to be pessimistic.
2011 */
Chris Masond3977122009-01-05 21:25:51 -05002012static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002013 struct btrfs_root *root,
2014 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002015{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002016 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002017 struct extent_buffer *right = NULL;
2018 struct extent_buffer *mid;
2019 struct extent_buffer *left = NULL;
2020 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002021 int ret = 0;
2022 int wret;
2023 int pslot;
2024 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002025
2026 if (level == 0)
2027 return 1;
2028
Chris Mason5f39d392007-10-15 16:14:19 -04002029 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002030 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002031
Li Zefana05a9bb2011-09-06 16:55:34 +08002032 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002033 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002034 pslot = path->slots[level + 1];
2035 }
Chris Masone66f7092007-04-20 13:16:02 -04002036
Chris Mason5f39d392007-10-15 16:14:19 -04002037 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002038 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002039
David Sterba4b231ae2019-08-21 19:16:27 +02002040 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002041 if (IS_ERR(left))
2042 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002043
2044 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002045 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002046 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002047
Josef Bacikbf774672020-08-20 11:46:04 -04002048 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002049
Chris Mason5f39d392007-10-15 16:14:19 -04002050 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002051 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002052 wret = 1;
2053 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002054 ret = btrfs_cow_block(trans, root, left, parent,
Josef Bacik9631e4c2020-08-20 11:46:03 -04002055 pslot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04002056 BTRFS_NESTING_LEFT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04002057 if (ret)
2058 wret = 1;
2059 else {
David Sterbad30a6682019-03-20 14:16:45 +01002060 wret = push_node_left(trans, left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002061 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002062 }
Chris Masone66f7092007-04-20 13:16:02 -04002063 if (wret < 0)
2064 ret = wret;
2065 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002066 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002067 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002068 btrfs_node_key(mid, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002069 ret = tree_mod_log_insert_key(parent, pslot,
2070 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2071 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002072 btrfs_set_node_key(parent, &disk_key, pslot);
2073 btrfs_mark_buffer_dirty(parent);
2074 if (btrfs_header_nritems(left) > orig_slot) {
2075 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002076 path->slots[level + 1] -= 1;
2077 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002078 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002079 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002080 } else {
2081 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002082 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002083 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002084 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002086 }
Chris Masone66f7092007-04-20 13:16:02 -04002087 return 0;
2088 }
Chris Mason925baed2008-06-25 16:01:30 -04002089 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002090 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002091 }
David Sterba4b231ae2019-08-21 19:16:27 +02002092 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002093 if (IS_ERR(right))
2094 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002095
2096 /*
2097 * then try to empty the right most buffer into the middle
2098 */
Chris Mason5f39d392007-10-15 16:14:19 -04002099 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002100 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002101
Josef Bacikbf774672020-08-20 11:46:04 -04002102 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002103
Chris Mason5f39d392007-10-15 16:14:19 -04002104 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002105 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002106 wret = 1;
2107 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002108 ret = btrfs_cow_block(trans, root, right,
2109 parent, pslot + 1,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04002110 &right, BTRFS_NESTING_RIGHT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04002111 if (ret)
2112 wret = 1;
2113 else {
David Sterba55d32ed2019-03-20 14:18:06 +01002114 wret = balance_node_right(trans, right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002115 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002116 }
Chris Masone66f7092007-04-20 13:16:02 -04002117 if (wret < 0)
2118 ret = wret;
2119 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002120 struct btrfs_disk_key disk_key;
2121
2122 btrfs_node_key(right, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002123 ret = tree_mod_log_insert_key(parent, pslot + 1,
2124 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2125 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002126 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2127 btrfs_mark_buffer_dirty(parent);
2128
2129 if (btrfs_header_nritems(mid) <= orig_slot) {
2130 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002131 path->slots[level + 1] += 1;
2132 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002133 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002134 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002135 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002136 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002137 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002138 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002139 }
Chris Masone66f7092007-04-20 13:16:02 -04002140 return 0;
2141 }
Chris Mason925baed2008-06-25 16:01:30 -04002142 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002144 }
Chris Masone66f7092007-04-20 13:16:02 -04002145 return 1;
2146}
2147
Chris Mason74123bd2007-02-02 11:05:29 -05002148/*
Chris Masond352ac62008-09-29 15:18:18 -04002149 * readahead one full node of leaves, finding things that are close
2150 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002151 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002152static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002153 struct btrfs_path *path,
2154 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002155{
Chris Mason5f39d392007-10-15 16:14:19 -04002156 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002157 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002158 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002159 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002160 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002161 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002162 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002163 u32 nr;
2164 u32 blocksize;
2165 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002166
Chris Masona6b6e752007-10-15 16:22:39 -04002167 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002168 return;
2169
Chris Mason6702ed42007-08-07 16:15:09 -04002170 if (!path->nodes[level])
2171 return;
2172
Chris Mason5f39d392007-10-15 16:14:19 -04002173 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002174
Chris Mason3c69fae2007-08-07 15:52:22 -04002175 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 blocksize = fs_info->nodesize;
2177 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002178 if (eb) {
2179 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002180 return;
2181 }
2182
Chris Masona7175312009-01-22 09:23:10 -05002183 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002184
Chris Mason5f39d392007-10-15 16:14:19 -04002185 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002186 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002187
Chris Masond3977122009-01-05 21:25:51 -05002188 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002189 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002190 if (nr == 0)
2191 break;
2192 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002193 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002194 nr++;
2195 if (nr >= nritems)
2196 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002197 }
David Sterbae4058b52015-11-27 16:31:35 +01002198 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002199 btrfs_node_key(node, &disk_key, nr);
2200 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2201 break;
2202 }
Chris Mason6b800532007-10-15 16:17:34 -04002203 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002204 if ((search <= target && target - search <= 65536) ||
2205 (search > target && search - target <= 65536)) {
Josef Bacikbfb484d2020-11-05 10:45:09 -05002206 btrfs_readahead_node_child(node, nr);
Chris Mason6b800532007-10-15 16:17:34 -04002207 nread += blocksize;
2208 }
2209 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002210 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002211 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002212 }
2213}
Chris Mason925baed2008-06-25 16:01:30 -04002214
Josef Bacikbfb484d2020-11-05 10:45:09 -05002215static noinline void reada_for_balance(struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002216{
Josef Bacikbfb484d2020-11-05 10:45:09 -05002217 struct extent_buffer *parent;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002218 int slot;
2219 int nritems;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002220
Chris Mason8c594ea2009-04-20 15:50:10 -04002221 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002222 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002223 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002224
2225 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002226 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002227
Josef Bacikbfb484d2020-11-05 10:45:09 -05002228 if (slot > 0)
2229 btrfs_readahead_node_child(parent, slot - 1);
2230 if (slot + 1 < nritems)
2231 btrfs_readahead_node_child(parent, slot + 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002232}
2233
2234
2235/*
Chris Masond3977122009-01-05 21:25:51 -05002236 * when we walk down the tree, it is usually safe to unlock the higher layers
2237 * in the tree. The exceptions are when our path goes through slot 0, because
2238 * operations on the tree might require changing key pointers higher up in the
2239 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002240 *
Chris Masond3977122009-01-05 21:25:51 -05002241 * callers might also have set path->keep_locks, which tells this code to keep
2242 * the lock if the path points to the last slot in the block. This is part of
2243 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002244 *
Chris Masond3977122009-01-05 21:25:51 -05002245 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2246 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002247 */
Chris Masone02119d2008-09-05 16:13:11 -04002248static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002249 int lowest_unlock, int min_write_lock_level,
2250 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002251{
2252 int i;
2253 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002254 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002255 struct extent_buffer *t;
2256
2257 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2258 if (!path->nodes[i])
2259 break;
2260 if (!path->locks[i])
2261 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002262 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002263 skip_level = i + 1;
2264 continue;
2265 }
Chris Mason051e1b92008-06-25 16:01:30 -04002266 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002267 u32 nritems;
2268 t = path->nodes[i];
2269 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002270 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002271 skip_level = i + 1;
2272 continue;
2273 }
2274 }
Chris Mason051e1b92008-06-25 16:01:30 -04002275 if (skip_level < i && i >= lowest_unlock)
2276 no_skips = 1;
2277
Chris Mason925baed2008-06-25 16:01:30 -04002278 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08002279 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04002280 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002281 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002282 if (write_lock_level &&
2283 i > min_write_lock_level &&
2284 i <= *write_lock_level) {
2285 *write_lock_level = i - 1;
2286 }
Chris Mason925baed2008-06-25 16:01:30 -04002287 }
2288 }
2289}
2290
Chris Mason3c69fae2007-08-07 15:52:22 -04002291/*
Chris Masonc8c42862009-04-03 10:14:18 -04002292 * helper function for btrfs_search_slot. The goal is to find a block
2293 * in cache without setting the path to blocking. If we find the block
2294 * we return zero and the path is unchanged.
2295 *
2296 * If we can't find the block, we set the path blocking and do some
2297 * reada. -EAGAIN is returned and the search must be repeated.
2298 */
2299static int
Liu Bod07b8522017-01-30 12:23:42 -08002300read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2301 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002302 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002303{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002304 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002305 u64 blocknr;
2306 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002307 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08002308 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04002309 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08002310 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04002311
Nikolay Borisov213ff4b2020-05-27 13:10:59 +03002312 blocknr = btrfs_node_blockptr(*eb_ret, slot);
2313 gen = btrfs_node_ptr_generation(*eb_ret, slot);
2314 parent_level = btrfs_header_level(*eb_ret);
2315 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002316
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002317 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002318 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002319 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002320 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Qu Wenruo448de472019-03-12 17:10:40 +08002321 /*
2322 * Do extra check for first_key, eb can be stale due to
2323 * being cached, read from scrub, or have multiple
2324 * parents (shared tree blocks).
2325 */
David Sterbae064d5e2019-03-20 14:58:13 +01002326 if (btrfs_verify_level_key(tmp,
Qu Wenruo448de472019-03-12 17:10:40 +08002327 parent_level - 1, &first_key, gen)) {
2328 free_extent_buffer(tmp);
2329 return -EUCLEAN;
2330 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002331 *eb_ret = tmp;
2332 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002333 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002334
Josef Bacikbdf7c002013-06-17 13:44:48 -04002335 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08002336 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04002337 if (!ret) {
2338 *eb_ret = tmp;
2339 return 0;
2340 }
2341 free_extent_buffer(tmp);
2342 btrfs_release_path(p);
2343 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002344 }
2345
2346 /*
2347 * reduce lock contention at high levels
2348 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002349 * we read. Don't release the lock on the current
2350 * level because we need to walk this node to figure
2351 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002352 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002353 btrfs_unlock_up_safe(p, level + 1);
Chris Mason8c594ea2009-04-20 15:50:10 -04002354
David Sterbae4058b52015-11-27 16:31:35 +01002355 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002356 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002357
Chris Mason76a05b32009-05-14 13:24:30 -04002358 ret = -EAGAIN;
Josef Bacik1b7ec852020-11-05 10:45:18 -05002359 tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
2360 gen, parent_level - 1, &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002361 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002362 /*
2363 * If the read above didn't mark this buffer up to date,
2364 * it will never end up being up to date. Set ret to EIO now
2365 * and give up so that our caller doesn't loop forever
2366 * on our EAGAINs.
2367 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08002368 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04002369 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002370 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002371 } else {
2372 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002373 }
Liu Bo02a33072018-05-16 01:37:36 +08002374
2375 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002376 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002377}
2378
2379/*
2380 * helper function for btrfs_search_slot. This does all of the checks
2381 * for node-level blocks and does any balancing required based on
2382 * the ins_len.
2383 *
2384 * If no extra work was required, zero is returned. If we had to
2385 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2386 * start over
2387 */
2388static int
2389setup_nodes_for_search(struct btrfs_trans_handle *trans,
2390 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002391 struct extent_buffer *b, int level, int ins_len,
2392 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002393{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov95b982d2020-11-13 09:29:40 +02002395 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396
Chris Masonc8c42862009-04-03 10:14:18 -04002397 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002398 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002399
Chris Masonbd681512011-07-16 15:23:14 -04002400 if (*write_lock_level < level + 1) {
2401 *write_lock_level = level + 1;
2402 btrfs_release_path(p);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02002403 return -EAGAIN;
Chris Masonbd681512011-07-16 15:23:14 -04002404 }
2405
Josef Bacikbfb484d2020-11-05 10:45:09 -05002406 reada_for_balance(p, level);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02002407 ret = split_node(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002408
Chris Masonc8c42862009-04-03 10:14:18 -04002409 b = p->nodes[level];
2410 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002411 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002412
Chris Masonbd681512011-07-16 15:23:14 -04002413 if (*write_lock_level < level + 1) {
2414 *write_lock_level = level + 1;
2415 btrfs_release_path(p);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02002416 return -EAGAIN;
Chris Masonbd681512011-07-16 15:23:14 -04002417 }
2418
Josef Bacikbfb484d2020-11-05 10:45:09 -05002419 reada_for_balance(p, level);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02002420 ret = balance_level(trans, root, p, level);
2421 if (ret)
2422 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002423
Chris Masonc8c42862009-04-03 10:14:18 -04002424 b = p->nodes[level];
2425 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002426 btrfs_release_path(p);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02002427 return -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04002428 }
2429 BUG_ON(btrfs_header_nritems(b) == 1);
2430 }
Chris Masonc8c42862009-04-03 10:14:18 -04002431 return ret;
2432}
2433
David Sterba381cf652015-01-02 18:45:16 +01002434int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002435 u64 iobjectid, u64 ioff, u8 key_type,
2436 struct btrfs_key *found_key)
2437{
2438 int ret;
2439 struct btrfs_key key;
2440 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002441
2442 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002443 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002444
2445 key.type = key_type;
2446 key.objectid = iobjectid;
2447 key.offset = ioff;
2448
2449 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002450 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002451 return ret;
2452
2453 eb = path->nodes[0];
2454 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2455 ret = btrfs_next_leaf(fs_root, path);
2456 if (ret)
2457 return ret;
2458 eb = path->nodes[0];
2459 }
2460
2461 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2462 if (found_key->type != key.type ||
2463 found_key->objectid != key.objectid)
2464 return 1;
2465
2466 return 0;
2467}
2468
Liu Bo1fc28d82018-05-18 11:00:21 +08002469static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2470 struct btrfs_path *p,
2471 int write_lock_level)
2472{
2473 struct btrfs_fs_info *fs_info = root->fs_info;
2474 struct extent_buffer *b;
2475 int root_lock;
2476 int level = 0;
2477
2478 /* We try very hard to do read locks on the root */
2479 root_lock = BTRFS_READ_LOCK;
2480
2481 if (p->search_commit_root) {
Filipe Mananabe6821f2018-12-11 10:19:45 +00002482 /*
2483 * The commit roots are read only so we always do read locks,
2484 * and we always must hold the commit_root_sem when doing
2485 * searches on them, the only exception is send where we don't
2486 * want to block transaction commits for a long time, so
2487 * we need to clone the commit root in order to avoid races
2488 * with transaction commits that create a snapshot of one of
2489 * the roots used by a send operation.
2490 */
2491 if (p->need_commit_sem) {
Liu Bo1fc28d82018-05-18 11:00:21 +08002492 down_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002493 b = btrfs_clone_extent_buffer(root->commit_root);
Liu Bo1fc28d82018-05-18 11:00:21 +08002494 up_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002495 if (!b)
2496 return ERR_PTR(-ENOMEM);
2497
2498 } else {
2499 b = root->commit_root;
David Sterba67439da2019-10-08 13:28:47 +02002500 atomic_inc(&b->refs);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002501 }
2502 level = btrfs_header_level(b);
Liu Bof9ddfd02018-05-29 21:27:06 +08002503 /*
2504 * Ensure that all callers have set skip_locking when
2505 * p->search_commit_root = 1.
2506 */
2507 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08002508
2509 goto out;
2510 }
2511
2512 if (p->skip_locking) {
2513 b = btrfs_root_node(root);
2514 level = btrfs_header_level(b);
2515 goto out;
2516 }
2517
2518 /*
Liu Bo662c6532018-05-18 11:00:23 +08002519 * If the level is set to maximum, we can skip trying to get the read
2520 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08002521 */
Liu Bo662c6532018-05-18 11:00:23 +08002522 if (write_lock_level < BTRFS_MAX_LEVEL) {
2523 /*
2524 * We don't know the level of the root node until we actually
2525 * have it read locked
2526 */
Josef Bacik1bb96592020-11-06 16:27:33 -05002527 b = btrfs_read_lock_root_node(root);
Liu Bo662c6532018-05-18 11:00:23 +08002528 level = btrfs_header_level(b);
2529 if (level > write_lock_level)
2530 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08002531
Liu Bo662c6532018-05-18 11:00:23 +08002532 /* Whoops, must trade for write lock */
2533 btrfs_tree_read_unlock(b);
2534 free_extent_buffer(b);
2535 }
2536
Liu Bo1fc28d82018-05-18 11:00:21 +08002537 b = btrfs_lock_root_node(root);
2538 root_lock = BTRFS_WRITE_LOCK;
2539
2540 /* The level might have changed, check again */
2541 level = btrfs_header_level(b);
2542
2543out:
2544 p->nodes[level] = b;
2545 if (!p->skip_locking)
2546 p->locks[level] = root_lock;
2547 /*
2548 * Callers are responsible for dropping b's references.
2549 */
2550 return b;
2551}
2552
2553
Chris Masonc8c42862009-04-03 10:14:18 -04002554/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002555 * btrfs_search_slot - look for a key in a tree and perform necessary
2556 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002557 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002558 * @trans: Handle of transaction, used when modifying the tree
2559 * @p: Holds all btree nodes along the search path
2560 * @root: The root node of the tree
2561 * @key: The key we are looking for
ethanwu9a664972020-12-01 17:25:12 +08002562 * @ins_len: Indicates purpose of search:
2563 * >0 for inserts it's size of item inserted (*)
2564 * <0 for deletions
2565 * 0 for plain searches, not modifying the tree
2566 *
2567 * (*) If size of item inserted doesn't include
2568 * sizeof(struct btrfs_item), then p->search_for_extension must
2569 * be set.
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002570 * @cow: boolean should CoW operations be performed. Must always be 1
2571 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002572 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002573 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2574 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2575 *
2576 * If @key is found, 0 is returned and you can find the item in the leaf level
2577 * of the path (level 0)
2578 *
2579 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2580 * points to the slot where it should be inserted
2581 *
2582 * If an error is encountered while searching the tree a negative error number
2583 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002584 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002585int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2586 const struct btrfs_key *key, struct btrfs_path *p,
2587 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002588{
Chris Mason5f39d392007-10-15 16:14:19 -04002589 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002590 int slot;
2591 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002592 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002594 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002595 /* everything at write_lock_level or lower must be write locked */
2596 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002597 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002598 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002599 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002600
Chris Mason6702ed42007-08-07 16:15:09 -04002601 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002602 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002603 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002604 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002605
Chris Masonbd681512011-07-16 15:23:14 -04002606 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002607 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002608
Chris Masonbd681512011-07-16 15:23:14 -04002609 /* when we are removing items, we might have to go up to level
2610 * two as we update tree pointers Make sure we keep write
2611 * for those levels as well
2612 */
2613 write_lock_level = 2;
2614 } else if (ins_len > 0) {
2615 /*
2616 * for inserting items, make sure we have a write lock on
2617 * level 1 so we can update keys
2618 */
2619 write_lock_level = 1;
2620 }
2621
2622 if (!cow)
2623 write_lock_level = -1;
2624
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002625 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002626 write_lock_level = BTRFS_MAX_LEVEL;
2627
Chris Masonf7c79f32012-03-19 15:54:38 -04002628 min_write_lock_level = write_lock_level;
2629
Chris Masonbb803952007-03-01 12:04:21 -05002630again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002631 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08002632 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002633 if (IS_ERR(b)) {
2634 ret = PTR_ERR(b);
2635 goto done;
2636 }
Chris Mason925baed2008-06-25 16:01:30 -04002637
Chris Masoneb60cea2007-02-02 09:18:22 -05002638 while (b) {
Qu Wenruof624d972019-09-10 15:40:17 +08002639 int dec = 0;
2640
Chris Mason5f39d392007-10-15 16:14:19 -04002641 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002642
Chris Mason02217ed2007-03-02 16:08:05 -05002643 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002644 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2645
Chris Masonc8c42862009-04-03 10:14:18 -04002646 /*
2647 * if we don't really need to cow this block
2648 * then we don't want to set the path blocking,
2649 * so we test it here
2650 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002651 if (!should_cow_block(trans, root, b)) {
2652 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002653 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002654 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002655
Chris Masonbd681512011-07-16 15:23:14 -04002656 /*
2657 * must have write locks on this node and the
2658 * parent
2659 */
Josef Bacik5124e002012-11-07 13:44:13 -05002660 if (level > write_lock_level ||
2661 (level + 1 > write_lock_level &&
2662 level + 1 < BTRFS_MAX_LEVEL &&
2663 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002664 write_lock_level = level + 1;
2665 btrfs_release_path(p);
2666 goto again;
2667 }
2668
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002669 if (last_level)
2670 err = btrfs_cow_block(trans, root, b, NULL, 0,
Josef Bacik9631e4c2020-08-20 11:46:03 -04002671 &b,
2672 BTRFS_NESTING_COW);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002673 else
2674 err = btrfs_cow_block(trans, root, b,
2675 p->nodes[level + 1],
Josef Bacik9631e4c2020-08-20 11:46:03 -04002676 p->slots[level + 1], &b,
2677 BTRFS_NESTING_COW);
Yan Zheng33c66f42009-07-22 09:59:00 -04002678 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002679 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002680 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002681 }
Chris Mason02217ed2007-03-02 16:08:05 -05002682 }
Chris Mason65b51a02008-08-01 15:11:20 -04002683cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002684 p->nodes[level] = b;
Liu Bo52398342018-08-22 05:54:37 +08002685 /*
2686 * Leave path with blocking locks to avoid massive
2687 * lock context switch, this is made on purpose.
2688 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002689
2690 /*
2691 * we have a lock on b and as long as we aren't changing
2692 * the tree, there is no way to for the items in b to change.
2693 * It is safe to drop the lock on our parent before we
2694 * go through the expensive btree search on b.
2695 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002696 * If we're inserting or deleting (ins_len != 0), then we might
2697 * be changing slot zero, which may require changing the parent.
2698 * So, we can't drop the lock until after we know which slot
2699 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002700 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002701 if (!ins_len && !p->keep_locks) {
2702 int u = level + 1;
2703
2704 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2705 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2706 p->locks[u] = 0;
2707 }
2708 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002709
Nikolay Borisov995e9a12020-05-27 13:10:53 +03002710 /*
2711 * If btrfs_bin_search returns an exact match (prev_cmp == 0)
2712 * we can safely assume the target key will always be in slot 0
2713 * on lower levels due to the invariants BTRFS' btree provides,
2714 * namely that a btrfs_key_ptr entry always points to the
2715 * lowest key in the child node, thus we can skip searching
2716 * lower levels
2717 */
2718 if (prev_cmp == 0) {
2719 slot = 0;
2720 ret = 0;
2721 } else {
2722 ret = btrfs_bin_search(b, key, &slot);
2723 prev_cmp = ret;
2724 if (ret < 0)
2725 goto done;
2726 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002727
Qu Wenruof624d972019-09-10 15:40:17 +08002728 if (level == 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002729 p->slots[level] = slot;
ethanwu9a664972020-12-01 17:25:12 +08002730 /*
2731 * Item key already exists. In this case, if we are
2732 * allowed to insert the item (for example, in dir_item
2733 * case, item key collision is allowed), it will be
2734 * merged with the original item. Only the item size
2735 * grows, no new btrfs item will be added. If
2736 * search_for_extension is not set, ins_len already
2737 * accounts the size btrfs_item, deduct it here so leaf
2738 * space check will be correct.
2739 */
2740 if (ret == 0 && ins_len > 0 && !p->search_for_extension) {
2741 ASSERT(ins_len >= sizeof(struct btrfs_item));
2742 ins_len -= sizeof(struct btrfs_item);
2743 }
Yan Zheng87b29b22008-12-17 10:21:48 -05002744 if (ins_len > 0 &&
David Sterbae902baa2019-03-20 14:36:46 +01002745 btrfs_leaf_free_space(b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002746 if (write_lock_level < 1) {
2747 write_lock_level = 1;
2748 btrfs_release_path(p);
2749 goto again;
2750 }
2751
Yan Zheng33c66f42009-07-22 09:59:00 -04002752 err = split_leaf(trans, root, key,
2753 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002754
Yan Zheng33c66f42009-07-22 09:59:00 -04002755 BUG_ON(err > 0);
2756 if (err) {
2757 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002758 goto done;
2759 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002760 }
Chris Mason459931e2008-12-10 09:10:46 -05002761 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002762 unlock_up(p, level, lowest_unlock,
Liu Bo4b6f8e92018-08-14 10:46:53 +08002763 min_write_lock_level, NULL);
Chris Mason65b51a02008-08-01 15:11:20 -04002764 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002765 }
Qu Wenruof624d972019-09-10 15:40:17 +08002766 if (ret && slot > 0) {
2767 dec = 1;
2768 slot--;
2769 }
2770 p->slots[level] = slot;
2771 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2772 &write_lock_level);
2773 if (err == -EAGAIN)
2774 goto again;
2775 if (err) {
2776 ret = err;
2777 goto done;
2778 }
2779 b = p->nodes[level];
2780 slot = p->slots[level];
2781
2782 /*
2783 * Slot 0 is special, if we change the key we have to update
2784 * the parent pointer which means we must have a write lock on
2785 * the parent
2786 */
2787 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2788 write_lock_level = level + 1;
2789 btrfs_release_path(p);
2790 goto again;
2791 }
2792
2793 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2794 &write_lock_level);
2795
2796 if (level == lowest_level) {
2797 if (dec)
2798 p->slots[level]++;
2799 goto done;
2800 }
2801
2802 err = read_block_for_search(root, p, &b, level, slot, key);
2803 if (err == -EAGAIN)
2804 goto again;
2805 if (err) {
2806 ret = err;
2807 goto done;
2808 }
2809
2810 if (!p->skip_locking) {
2811 level = btrfs_header_level(b);
2812 if (level <= write_lock_level) {
Josef Bacikac5887c2020-08-20 11:46:10 -04002813 btrfs_tree_lock(b);
Qu Wenruof624d972019-09-10 15:40:17 +08002814 p->locks[level] = BTRFS_WRITE_LOCK;
2815 } else {
Josef Bacikfe596ca2020-11-06 16:27:34 -05002816 btrfs_tree_read_lock(b);
Qu Wenruof624d972019-09-10 15:40:17 +08002817 p->locks[level] = BTRFS_READ_LOCK;
2818 }
2819 p->nodes[level] = b;
2820 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002821 }
Chris Mason65b51a02008-08-01 15:11:20 -04002822 ret = 1;
2823done:
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002824 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002825 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002826 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002827}
Josef Bacikf75e2b72020-12-16 11:18:43 -05002828ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002829
Chris Mason74123bd2007-02-02 11:05:29 -05002830/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002831 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2832 * current state of the tree together with the operations recorded in the tree
2833 * modification log to search for the key in a previous version of this tree, as
2834 * denoted by the time_seq parameter.
2835 *
2836 * Naturally, there is no support for insert, delete or cow operations.
2837 *
2838 * The resulting path and return value will be set up as if we called
2839 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2840 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002841int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002842 struct btrfs_path *p, u64 time_seq)
2843{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002844 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002845 struct extent_buffer *b;
2846 int slot;
2847 int ret;
2848 int err;
2849 int level;
2850 int lowest_unlock = 1;
2851 u8 lowest_level = 0;
2852
2853 lowest_level = p->lowest_level;
2854 WARN_ON(p->nodes[0] != NULL);
2855
2856 if (p->search_commit_root) {
2857 BUG_ON(time_seq);
2858 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2859 }
2860
2861again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002862 b = get_old_root(root, time_seq);
Nikolay Borisov315bed42018-09-13 11:35:10 +03002863 if (!b) {
2864 ret = -EIO;
2865 goto done;
2866 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002867 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002868 p->locks[level] = BTRFS_READ_LOCK;
2869
2870 while (b) {
Qu Wenruoabe93392019-09-10 15:40:18 +08002871 int dec = 0;
2872
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002873 level = btrfs_header_level(b);
2874 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002875
2876 /*
2877 * we have a lock on b and as long as we aren't changing
2878 * the tree, there is no way to for the items in b to change.
2879 * It is safe to drop the lock on our parent before we
2880 * go through the expensive btree search on b.
2881 */
2882 btrfs_unlock_up_safe(p, level + 1);
2883
Nikolay Borisov995e9a12020-05-27 13:10:53 +03002884 ret = btrfs_bin_search(b, key, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00002885 if (ret < 0)
2886 goto done;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002887
Qu Wenruoabe93392019-09-10 15:40:18 +08002888 if (level == 0) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002889 p->slots[level] = slot;
2890 unlock_up(p, level, lowest_unlock, 0, NULL);
2891 goto done;
2892 }
Qu Wenruoabe93392019-09-10 15:40:18 +08002893
2894 if (ret && slot > 0) {
2895 dec = 1;
2896 slot--;
2897 }
2898 p->slots[level] = slot;
2899 unlock_up(p, level, lowest_unlock, 0, NULL);
2900
2901 if (level == lowest_level) {
2902 if (dec)
2903 p->slots[level]++;
2904 goto done;
2905 }
2906
2907 err = read_block_for_search(root, p, &b, level, slot, key);
2908 if (err == -EAGAIN)
2909 goto again;
2910 if (err) {
2911 ret = err;
2912 goto done;
2913 }
2914
2915 level = btrfs_header_level(b);
Josef Bacikac5887c2020-08-20 11:46:10 -04002916 btrfs_tree_read_lock(b);
Qu Wenruoabe93392019-09-10 15:40:18 +08002917 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
2918 if (!b) {
2919 ret = -ENOMEM;
2920 goto done;
2921 }
2922 p->locks[level] = BTRFS_READ_LOCK;
2923 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002924 }
2925 ret = 1;
2926done:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002927 if (ret < 0)
2928 btrfs_release_path(p);
2929
2930 return ret;
2931}
2932
2933/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002934 * helper to use instead of search slot if no exact match is needed but
2935 * instead the next or previous item should be returned.
2936 * When find_higher is true, the next higher item is returned, the next lower
2937 * otherwise.
2938 * When return_any and find_higher are both true, and no higher item is found,
2939 * return the next lower instead.
2940 * When return_any is true and find_higher is false, and no lower item is found,
2941 * return the next higher instead.
2942 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2943 * < 0 on error
2944 */
2945int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08002946 const struct btrfs_key *key,
2947 struct btrfs_path *p, int find_higher,
2948 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002949{
2950 int ret;
2951 struct extent_buffer *leaf;
2952
2953again:
2954 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2955 if (ret <= 0)
2956 return ret;
2957 /*
2958 * a return value of 1 means the path is at the position where the
2959 * item should be inserted. Normally this is the next bigger item,
2960 * but in case the previous item is the last in a leaf, path points
2961 * to the first free slot in the previous leaf, i.e. at an invalid
2962 * item.
2963 */
2964 leaf = p->nodes[0];
2965
2966 if (find_higher) {
2967 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2968 ret = btrfs_next_leaf(root, p);
2969 if (ret <= 0)
2970 return ret;
2971 if (!return_any)
2972 return 1;
2973 /*
2974 * no higher item found, return the next
2975 * lower instead
2976 */
2977 return_any = 0;
2978 find_higher = 0;
2979 btrfs_release_path(p);
2980 goto again;
2981 }
2982 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002983 if (p->slots[0] == 0) {
2984 ret = btrfs_prev_leaf(root, p);
2985 if (ret < 0)
2986 return ret;
2987 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00002988 leaf = p->nodes[0];
2989 if (p->slots[0] == btrfs_header_nritems(leaf))
2990 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02002991 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002992 }
Arne Jansene6793762011-09-13 11:18:10 +02002993 if (!return_any)
2994 return 1;
2995 /*
2996 * no lower item found, return the next
2997 * higher instead
2998 */
2999 return_any = 0;
3000 find_higher = 1;
3001 btrfs_release_path(p);
3002 goto again;
3003 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003004 --p->slots[0];
3005 }
3006 }
3007 return 0;
3008}
3009
3010/*
Chris Mason74123bd2007-02-02 11:05:29 -05003011 * adjust the pointers going up the tree, starting at level
3012 * making sure the right key of each node is points to 'key'.
3013 * This is used after shifting pointers to the left, so it stops
3014 * fixing up pointers when a given leaf/node is not in slot 0 of the
3015 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003016 *
Chris Mason74123bd2007-02-02 11:05:29 -05003017 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003018static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003019 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003020{
3021 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003022 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01003023 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003024
Chris Mason234b63a2007-03-13 10:46:10 -04003025 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003026 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003027
Chris Masoneb60cea2007-02-02 09:18:22 -05003028 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003029 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003030 t = path->nodes[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003031 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3032 GFP_ATOMIC);
3033 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003034 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003035 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003036 if (tslot != 0)
3037 break;
3038 }
3039}
3040
Chris Mason74123bd2007-02-02 11:05:29 -05003041/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003042 * update item key.
3043 *
3044 * This function isn't completely safe. It's the caller's responsibility
3045 * that the new key won't break the order
3046 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003047void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3048 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003049 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003050{
3051 struct btrfs_disk_key disk_key;
3052 struct extent_buffer *eb;
3053 int slot;
3054
3055 eb = path->nodes[0];
3056 slot = path->slots[0];
3057 if (slot > 0) {
3058 btrfs_item_key(eb, &disk_key, slot - 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003059 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3060 btrfs_crit(fs_info,
3061 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3062 slot, btrfs_disk_key_objectid(&disk_key),
3063 btrfs_disk_key_type(&disk_key),
3064 btrfs_disk_key_offset(&disk_key),
3065 new_key->objectid, new_key->type,
3066 new_key->offset);
3067 btrfs_print_leaf(eb);
3068 BUG();
3069 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003070 }
3071 if (slot < btrfs_header_nritems(eb) - 1) {
3072 btrfs_item_key(eb, &disk_key, slot + 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003073 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3074 btrfs_crit(fs_info,
3075 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3076 slot, btrfs_disk_key_objectid(&disk_key),
3077 btrfs_disk_key_type(&disk_key),
3078 btrfs_disk_key_offset(&disk_key),
3079 new_key->objectid, new_key->type,
3080 new_key->offset);
3081 btrfs_print_leaf(eb);
3082 BUG();
3083 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003084 }
3085
3086 btrfs_cpu_key_to_disk(&disk_key, new_key);
3087 btrfs_set_item_key(eb, &disk_key, slot);
3088 btrfs_mark_buffer_dirty(eb);
3089 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003090 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003091}
3092
3093/*
Qu Wenruod16c7022020-08-19 14:35:50 +08003094 * Check key order of two sibling extent buffers.
3095 *
3096 * Return true if something is wrong.
3097 * Return false if everything is fine.
3098 *
3099 * Tree-checker only works inside one tree block, thus the following
3100 * corruption can not be detected by tree-checker:
3101 *
3102 * Leaf @left | Leaf @right
3103 * --------------------------------------------------------------
3104 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
3105 *
3106 * Key f6 in leaf @left itself is valid, but not valid when the next
3107 * key in leaf @right is 7.
3108 * This can only be checked at tree block merge time.
3109 * And since tree checker has ensured all key order in each tree block
3110 * is correct, we only need to bother the last key of @left and the first
3111 * key of @right.
3112 */
3113static bool check_sibling_keys(struct extent_buffer *left,
3114 struct extent_buffer *right)
3115{
3116 struct btrfs_key left_last;
3117 struct btrfs_key right_first;
3118 int level = btrfs_header_level(left);
3119 int nr_left = btrfs_header_nritems(left);
3120 int nr_right = btrfs_header_nritems(right);
3121
3122 /* No key to check in one of the tree blocks */
3123 if (!nr_left || !nr_right)
3124 return false;
3125
3126 if (level) {
3127 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
3128 btrfs_node_key_to_cpu(right, &right_first, 0);
3129 } else {
3130 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
3131 btrfs_item_key_to_cpu(right, &right_first, 0);
3132 }
3133
3134 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
3135 btrfs_crit(left->fs_info,
3136"bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
3137 left_last.objectid, left_last.type,
3138 left_last.offset, right_first.objectid,
3139 right_first.type, right_first.offset);
3140 return true;
3141 }
3142 return false;
3143}
3144
3145/*
Chris Mason74123bd2007-02-02 11:05:29 -05003146 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003147 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003148 *
3149 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3150 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003151 */
Chris Mason98ed5172008-01-03 10:01:48 -05003152static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003153 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003154 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003155{
David Sterbad30a6682019-03-20 14:16:45 +01003156 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003157 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003158 int src_nritems;
3159 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003160 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003161
Chris Mason5f39d392007-10-15 16:14:19 -04003162 src_nritems = btrfs_header_nritems(src);
3163 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003164 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003165 WARN_ON(btrfs_header_generation(src) != trans->transid);
3166 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003167
Chris Masonbce4eae2008-04-24 14:42:46 -04003168 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003169 return 1;
3170
Chris Masond3977122009-01-05 21:25:51 -05003171 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003172 return 1;
3173
Chris Masonbce4eae2008-04-24 14:42:46 -04003174 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003175 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003176 if (push_items < src_nritems) {
3177 /* leave at least 8 pointers in the node if
3178 * we aren't going to empty it
3179 */
3180 if (src_nritems - push_items < 8) {
3181 if (push_items <= 8)
3182 return 1;
3183 push_items -= 8;
3184 }
3185 }
3186 } else
3187 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003188
Qu Wenruod16c7022020-08-19 14:35:50 +08003189 /* dst is the left eb, src is the middle eb */
3190 if (check_sibling_keys(dst, src)) {
3191 ret = -EUCLEAN;
3192 btrfs_abort_transaction(trans, ret);
3193 return ret;
3194 }
David Sterbaed874f02019-03-20 14:22:04 +01003195 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003196 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003197 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003198 return ret;
3199 }
Chris Mason5f39d392007-10-15 16:14:19 -04003200 copy_extent_buffer(dst, src,
3201 btrfs_node_key_ptr_offset(dst_nritems),
3202 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003203 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003204
Chris Masonbb803952007-03-01 12:04:21 -05003205 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003206 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003207 * Don't call tree_mod_log_insert_move here, key removal was
3208 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003209 */
Chris Mason5f39d392007-10-15 16:14:19 -04003210 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3211 btrfs_node_key_ptr_offset(push_items),
3212 (src_nritems - push_items) *
3213 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003214 }
Chris Mason5f39d392007-10-15 16:14:19 -04003215 btrfs_set_header_nritems(src, src_nritems - push_items);
3216 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3217 btrfs_mark_buffer_dirty(src);
3218 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003219
Chris Masonbb803952007-03-01 12:04:21 -05003220 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003221}
3222
Chris Mason97571fd2007-02-24 13:39:08 -05003223/*
Chris Mason79f95c82007-03-01 15:16:26 -05003224 * try to push data from one node into the next node right in the
3225 * tree.
3226 *
3227 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3228 * error, and > 0 if there was no room in the right hand block.
3229 *
3230 * this will only push up to 1/2 the contents of the left node over
3231 */
Chris Mason5f39d392007-10-15 16:14:19 -04003232static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003233 struct extent_buffer *dst,
3234 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003235{
David Sterba55d32ed2019-03-20 14:18:06 +01003236 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason79f95c82007-03-01 15:16:26 -05003237 int push_items = 0;
3238 int max_push;
3239 int src_nritems;
3240 int dst_nritems;
3241 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003242
Chris Mason7bb86312007-12-11 09:25:06 -05003243 WARN_ON(btrfs_header_generation(src) != trans->transid);
3244 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3245
Chris Mason5f39d392007-10-15 16:14:19 -04003246 src_nritems = btrfs_header_nritems(src);
3247 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003248 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003249 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003250 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003251
Chris Masond3977122009-01-05 21:25:51 -05003252 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003253 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003254
3255 max_push = src_nritems / 2 + 1;
3256 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003257 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003258 return 1;
Yan252c38f2007-08-29 09:11:44 -04003259
Chris Mason79f95c82007-03-01 15:16:26 -05003260 if (max_push < push_items)
3261 push_items = max_push;
3262
Qu Wenruod16c7022020-08-19 14:35:50 +08003263 /* dst is the right eb, src is the middle eb */
3264 if (check_sibling_keys(src, dst)) {
3265 ret = -EUCLEAN;
3266 btrfs_abort_transaction(trans, ret);
3267 return ret;
3268 }
David Sterbabf1d3422018-03-05 15:47:39 +01003269 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3270 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003271 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3272 btrfs_node_key_ptr_offset(0),
3273 (dst_nritems) *
3274 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003275
David Sterbaed874f02019-03-20 14:22:04 +01003276 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3277 push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003278 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003279 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003280 return ret;
3281 }
Chris Mason5f39d392007-10-15 16:14:19 -04003282 copy_extent_buffer(dst, src,
3283 btrfs_node_key_ptr_offset(0),
3284 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003285 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003286
Chris Mason5f39d392007-10-15 16:14:19 -04003287 btrfs_set_header_nritems(src, src_nritems - push_items);
3288 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003289
Chris Mason5f39d392007-10-15 16:14:19 -04003290 btrfs_mark_buffer_dirty(src);
3291 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003292
Chris Mason79f95c82007-03-01 15:16:26 -05003293 return ret;
3294}
3295
3296/*
Chris Mason97571fd2007-02-24 13:39:08 -05003297 * helper function to insert a new root level in the tree.
3298 * A new node is allocated, and a single item is inserted to
3299 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003300 *
3301 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003302 */
Chris Masond3977122009-01-05 21:25:51 -05003303static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003304 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003305 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003306{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003308 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003309 struct extent_buffer *lower;
3310 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003311 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003312 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01003313 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003314
3315 BUG_ON(path->nodes[level]);
3316 BUG_ON(path->nodes[level-1] != root->node);
3317
Chris Mason7bb86312007-12-11 09:25:06 -05003318 lower = path->nodes[level-1];
3319 if (level == 1)
3320 btrfs_item_key(lower, &lower_key, 0);
3321 else
3322 btrfs_node_key(lower, &lower_key, 0);
3323
Filipe Mananaa6279472019-01-25 11:48:51 +00003324 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
Josef Bacik9631e4c2020-08-20 11:46:03 -04003325 root->node->start, 0,
Josef Bacikcf6f34a2020-08-20 11:46:07 -04003326 BTRFS_NESTING_NEW_ROOT);
Chris Mason5f39d392007-10-15 16:14:19 -04003327 if (IS_ERR(c))
3328 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003329
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003330 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003331
Chris Mason5f39d392007-10-15 16:14:19 -04003332 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003333 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003334 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003335 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003336 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003337
3338 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003339
3340 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003341
Chris Mason925baed2008-06-25 16:01:30 -04003342 old = root->node;
David Sterbad9d19a02018-03-05 16:35:29 +01003343 ret = tree_mod_log_insert_root(root->node, c, 0);
3344 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003345 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003346
3347 /* the super has an extra ref to root->node */
3348 free_extent_buffer(old);
3349
Chris Mason0b86a832008-03-24 15:01:56 -04003350 add_root_to_dirty_list(root);
David Sterba67439da2019-10-08 13:28:47 +02003351 atomic_inc(&c->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04003352 path->nodes[level] = c;
Josef Bacikac5887c2020-08-20 11:46:10 -04003353 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003354 path->slots[level] = 0;
3355 return 0;
3356}
3357
Chris Mason74123bd2007-02-02 11:05:29 -05003358/*
3359 * worker function to insert a single pointer in a node.
3360 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003361 *
Chris Mason74123bd2007-02-02 11:05:29 -05003362 * slot and level indicate where you want the key to go, and
3363 * blocknr is the block the key points to.
3364 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003365static void insert_ptr(struct btrfs_trans_handle *trans,
David Sterba6ad3cf62019-03-20 14:32:45 +01003366 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003367 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003368 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003369{
Chris Mason5f39d392007-10-15 16:14:19 -04003370 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003371 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003372 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003373
3374 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003375 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003376 lower = path->nodes[level];
3377 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003378 BUG_ON(slot > nritems);
David Sterba6ad3cf62019-03-20 14:32:45 +01003379 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003380 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003381 if (level) {
3382 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003383 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003384 BUG_ON(ret < 0);
3385 }
Chris Mason5f39d392007-10-15 16:14:19 -04003386 memmove_extent_buffer(lower,
3387 btrfs_node_key_ptr_offset(slot + 1),
3388 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003389 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003390 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003391 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003392 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3393 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003394 BUG_ON(ret < 0);
3395 }
Chris Mason5f39d392007-10-15 16:14:19 -04003396 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003397 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003398 WARN_ON(trans->transid == 0);
3399 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003400 btrfs_set_header_nritems(lower, nritems + 1);
3401 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003402}
3403
Chris Mason97571fd2007-02-24 13:39:08 -05003404/*
3405 * split the node at the specified level in path in two.
3406 * The path is corrected to point to the appropriate node after the split
3407 *
3408 * Before splitting this tries to make some room in the node by pushing
3409 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003410 *
3411 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003412 */
Chris Masone02119d2008-09-05 16:13:11 -04003413static noinline int split_node(struct btrfs_trans_handle *trans,
3414 struct btrfs_root *root,
3415 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003416{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003417 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003418 struct extent_buffer *c;
3419 struct extent_buffer *split;
3420 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003421 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003422 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003423 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003424
Chris Mason5f39d392007-10-15 16:14:19 -04003425 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003426 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003427 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003428 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003429 * trying to split the root, lets make a new one
3430 *
Liu Bofdd99c72013-05-22 12:06:51 +00003431 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003432 * insert_new_root, because that root buffer will be kept as a
3433 * normal node. We are going to log removal of half of the
3434 * elements below with tree_mod_log_eb_copy. We're holding a
3435 * tree lock on the buffer, which is why we cannot race with
3436 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003437 */
Liu Bofdd99c72013-05-22 12:06:51 +00003438 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003439 if (ret)
3440 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003441 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003442 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003443 c = path->nodes[level];
3444 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003445 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003446 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003447 if (ret < 0)
3448 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003449 }
Chris Masone66f7092007-04-20 13:16:02 -04003450
Chris Mason5f39d392007-10-15 16:14:19 -04003451 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003452 mid = (c_nritems + 1) / 2;
3453 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003454
Filipe Mananaa6279472019-01-25 11:48:51 +00003455 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
Josef Bacik4dff97e2020-08-20 11:46:06 -04003456 c->start, 0, BTRFS_NESTING_SPLIT);
Chris Mason5f39d392007-10-15 16:14:19 -04003457 if (IS_ERR(split))
3458 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003459
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03003461 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04003462
David Sterbaed874f02019-03-20 14:22:04 +01003463 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003464 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003465 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003466 return ret;
3467 }
Chris Mason5f39d392007-10-15 16:14:19 -04003468 copy_extent_buffer(split, c,
3469 btrfs_node_key_ptr_offset(0),
3470 btrfs_node_key_ptr_offset(mid),
3471 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3472 btrfs_set_header_nritems(split, c_nritems - mid);
3473 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003474
Chris Mason5f39d392007-10-15 16:14:19 -04003475 btrfs_mark_buffer_dirty(c);
3476 btrfs_mark_buffer_dirty(split);
3477
David Sterba6ad3cf62019-03-20 14:32:45 +01003478 insert_ptr(trans, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003479 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003480
Chris Mason5de08d72007-02-24 06:24:44 -05003481 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003482 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003483 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003484 free_extent_buffer(c);
3485 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003486 path->slots[level + 1] += 1;
3487 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003488 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003489 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003490 }
Nikolay Borisovd5286a922020-11-12 13:24:02 +02003491 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003492}
3493
Chris Mason74123bd2007-02-02 11:05:29 -05003494/*
3495 * how many bytes are required to store the items in a leaf. start
3496 * and nr indicate which items in the leaf to check. This totals up the
3497 * space used both by the item structs and the item data
3498 */
Chris Mason5f39d392007-10-15 16:14:19 -04003499static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003500{
Josef Bacik41be1f32012-10-15 13:43:18 -04003501 struct btrfs_item *start_item;
3502 struct btrfs_item *end_item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003503 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003504 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003505 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003506
3507 if (!nr)
3508 return 0;
Ross Kirkdd3cc162013-09-16 15:58:09 +01003509 start_item = btrfs_item_nr(start);
3510 end_item = btrfs_item_nr(end);
David Sterbaa31356b2020-04-29 22:56:01 +02003511 data_len = btrfs_item_offset(l, start_item) +
3512 btrfs_item_size(l, start_item);
3513 data_len = data_len - btrfs_item_offset(l, end_item);
Chris Mason0783fcf2007-03-12 20:12:07 -04003514 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003515 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003516 return data_len;
3517}
3518
Chris Mason74123bd2007-02-02 11:05:29 -05003519/*
Chris Masond4dbff92007-04-04 14:08:15 -04003520 * The space between the end of the leaf items and
3521 * the start of the leaf data. IOW, how much room
3522 * the leaf has left for both items and data
3523 */
David Sterbae902baa2019-03-20 14:36:46 +01003524noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003525{
David Sterbae902baa2019-03-20 14:36:46 +01003526 struct btrfs_fs_info *fs_info = leaf->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003527 int nritems = btrfs_header_nritems(leaf);
3528 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003529
3530 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003531 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003532 btrfs_crit(fs_info,
3533 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3534 ret,
3535 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3536 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003537 }
3538 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003539}
3540
Chris Mason99d8f832010-07-07 10:51:48 -04003541/*
3542 * min slot controls the lowest index we're willing to push to the
3543 * right. We'll push up to and including min_slot, but no lower
3544 */
David Sterbaf72f0012019-03-20 14:39:45 +01003545static noinline int __push_leaf_right(struct btrfs_path *path,
Chris Mason44871b12009-03-13 10:04:31 -04003546 int data_size, int empty,
3547 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003548 int free_space, u32 left_nritems,
3549 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003550{
David Sterbaf72f0012019-03-20 14:39:45 +01003551 struct btrfs_fs_info *fs_info = right->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003552 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003553 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003554 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003555 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003556 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003557 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003558 int push_space = 0;
3559 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003560 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003561 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003562 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003563 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003564 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003565
Chris Mason34a38212007-11-07 13:31:03 -05003566 if (empty)
3567 nr = 0;
3568 else
Chris Mason99d8f832010-07-07 10:51:48 -04003569 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003570
Zheng Yan31840ae2008-09-23 13:14:14 -04003571 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003572 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003573
Chris Mason44871b12009-03-13 10:04:31 -04003574 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003575 i = left_nritems - 1;
3576 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003577 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003578
Zheng Yan31840ae2008-09-23 13:14:14 -04003579 if (!empty && push_items > 0) {
3580 if (path->slots[0] > i)
3581 break;
3582 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003583 int space = btrfs_leaf_free_space(left);
3584
Zheng Yan31840ae2008-09-23 13:14:14 -04003585 if (space + push_space * 2 > free_space)
3586 break;
3587 }
3588 }
3589
Chris Mason00ec4c52007-02-24 12:47:20 -05003590 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003591 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003592
Chris Masondb945352007-10-15 16:15:53 -04003593 this_item_size = btrfs_item_size(left, item);
3594 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003595 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003596
Chris Mason00ec4c52007-02-24 12:47:20 -05003597 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003598 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003599 if (i == 0)
3600 break;
3601 i--;
Chris Masondb945352007-10-15 16:15:53 -04003602 }
Chris Mason5f39d392007-10-15 16:14:19 -04003603
Chris Mason925baed2008-06-25 16:01:30 -04003604 if (push_items == 0)
3605 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003606
Julia Lawall6c1500f2012-11-03 20:30:18 +00003607 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003608
Chris Mason00ec4c52007-02-24 12:47:20 -05003609 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003610 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003611
Chris Mason5f39d392007-10-15 16:14:19 -04003612 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
David Sterba8f881e82019-03-20 11:33:10 +01003613 push_space -= leaf_data_end(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003614
Chris Mason00ec4c52007-02-24 12:47:20 -05003615 /* make room in the right data area */
David Sterba8f881e82019-03-20 11:33:10 +01003616 data_end = leaf_data_end(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003617 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003618 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3619 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003620 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003621
Chris Mason00ec4c52007-02-24 12:47:20 -05003622 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003623 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003624 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
David Sterba8f881e82019-03-20 11:33:10 +01003625 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
Chris Masond6025572007-03-30 14:27:56 -04003626 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
3628 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3629 btrfs_item_nr_offset(0),
3630 right_nritems * sizeof(struct btrfs_item));
3631
Chris Mason00ec4c52007-02-24 12:47:20 -05003632 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003633 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3634 btrfs_item_nr_offset(left_nritems - push_items),
3635 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003636
3637 /* update the item pointers */
David Sterbac82f8232019-08-09 17:48:21 +02003638 btrfs_init_map_token(&token, right);
Chris Mason7518a232007-03-12 12:01:18 -04003639 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003640 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003641 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003642 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003643 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02003644 push_space -= btrfs_token_item_size(&token, item);
3645 btrfs_set_token_item_offset(&token, item, push_space);
Chris Masondb945352007-10-15 16:15:53 -04003646 }
3647
Chris Mason7518a232007-03-12 12:01:18 -04003648 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003649 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003650
Chris Mason34a38212007-11-07 13:31:03 -05003651 if (left_nritems)
3652 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003653 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003654 btrfs_clean_tree_block(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003655
Chris Mason5f39d392007-10-15 16:14:19 -04003656 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003657
Chris Mason5f39d392007-10-15 16:14:19 -04003658 btrfs_item_key(right, &disk_key, 0);
3659 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003660 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003661
Chris Mason00ec4c52007-02-24 12:47:20 -05003662 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003663 if (path->slots[0] >= left_nritems) {
3664 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003665 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba6a884d7d2019-03-20 14:30:02 +01003666 btrfs_clean_tree_block(path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003667 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003668 free_extent_buffer(path->nodes[0]);
3669 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003670 path->slots[1] += 1;
3671 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003672 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003673 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003674 }
3675 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003676
3677out_unlock:
3678 btrfs_tree_unlock(right);
3679 free_extent_buffer(right);
3680 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003681}
Chris Mason925baed2008-06-25 16:01:30 -04003682
Chris Mason00ec4c52007-02-24 12:47:20 -05003683/*
Chris Mason44871b12009-03-13 10:04:31 -04003684 * push some data in the path leaf to the right, trying to free up at
3685 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3686 *
3687 * returns 1 if the push failed because the other node didn't have enough
3688 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003689 *
3690 * this will push starting from min_slot to the end of the leaf. It won't
3691 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003692 */
3693static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003694 *root, struct btrfs_path *path,
3695 int min_data_size, int data_size,
3696 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003697{
3698 struct extent_buffer *left = path->nodes[0];
3699 struct extent_buffer *right;
3700 struct extent_buffer *upper;
3701 int slot;
3702 int free_space;
3703 u32 left_nritems;
3704 int ret;
3705
3706 if (!path->nodes[1])
3707 return 1;
3708
3709 slot = path->slots[1];
3710 upper = path->nodes[1];
3711 if (slot >= btrfs_header_nritems(upper) - 1)
3712 return 1;
3713
3714 btrfs_assert_tree_locked(path->nodes[1]);
3715
David Sterba4b231ae2019-08-21 19:16:27 +02003716 right = btrfs_read_node_slot(upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003717 /*
3718 * slot + 1 is not valid or we fail to read the right node,
3719 * no big deal, just return.
3720 */
3721 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003722 return 1;
3723
Josef Bacikbf774672020-08-20 11:46:04 -04003724 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
Chris Mason44871b12009-03-13 10:04:31 -04003725
David Sterbae902baa2019-03-20 14:36:46 +01003726 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003727 if (free_space < data_size)
3728 goto out_unlock;
3729
3730 /* cow and double check */
3731 ret = btrfs_cow_block(trans, root, right, upper,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04003732 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
Chris Mason44871b12009-03-13 10:04:31 -04003733 if (ret)
3734 goto out_unlock;
3735
David Sterbae902baa2019-03-20 14:36:46 +01003736 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003737 if (free_space < data_size)
3738 goto out_unlock;
3739
3740 left_nritems = btrfs_header_nritems(left);
3741 if (left_nritems == 0)
3742 goto out_unlock;
3743
Qu Wenruod16c7022020-08-19 14:35:50 +08003744 if (check_sibling_keys(left, right)) {
3745 ret = -EUCLEAN;
3746 btrfs_tree_unlock(right);
3747 free_extent_buffer(right);
3748 return ret;
3749 }
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003750 if (path->slots[0] == left_nritems && !empty) {
3751 /* Key greater than all keys in the leaf, right neighbor has
3752 * enough room for it and we're not emptying our leaf to delete
3753 * it, therefore use right neighbor to insert the new item and
Andrea Gelmini52042d82018-11-28 12:05:13 +01003754 * no need to touch/dirty our left leaf. */
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003755 btrfs_tree_unlock(left);
3756 free_extent_buffer(left);
3757 path->nodes[0] = right;
3758 path->slots[0] = 0;
3759 path->slots[1]++;
3760 return 0;
3761 }
3762
David Sterbaf72f0012019-03-20 14:39:45 +01003763 return __push_leaf_right(path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003764 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003765out_unlock:
3766 btrfs_tree_unlock(right);
3767 free_extent_buffer(right);
3768 return 1;
3769}
3770
3771/*
Chris Mason74123bd2007-02-02 11:05:29 -05003772 * push some data in the path leaf to the left, trying to free up at
3773 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003774 *
3775 * max_slot can put a limit on how far into the leaf we'll push items. The
3776 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3777 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003778 */
David Sterba8087c192019-03-20 14:40:41 +01003779static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003780 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003781 int free_space, u32 right_nritems,
3782 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003783{
David Sterba8087c192019-03-20 14:40:41 +01003784 struct btrfs_fs_info *fs_info = left->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003785 struct btrfs_disk_key disk_key;
3786 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003787 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003788 int push_space = 0;
3789 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003790 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003791 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003792 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003793 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003794 u32 this_item_size;
3795 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003796 struct btrfs_map_token token;
3797
Chris Mason34a38212007-11-07 13:31:03 -05003798 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003799 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003800 else
Chris Mason99d8f832010-07-07 10:51:48 -04003801 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003802
3803 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003804 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003805
Zheng Yan31840ae2008-09-23 13:14:14 -04003806 if (!empty && push_items > 0) {
3807 if (path->slots[0] < i)
3808 break;
3809 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003810 int space = btrfs_leaf_free_space(right);
3811
Zheng Yan31840ae2008-09-23 13:14:14 -04003812 if (space + push_space * 2 > free_space)
3813 break;
3814 }
3815 }
3816
Chris Masonbe0e5c02007-01-26 15:51:26 -05003817 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003818 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003819
3820 this_item_size = btrfs_item_size(right, item);
3821 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003822 break;
Chris Masondb945352007-10-15 16:15:53 -04003823
Chris Masonbe0e5c02007-01-26 15:51:26 -05003824 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003825 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003826 }
Chris Masondb945352007-10-15 16:15:53 -04003827
Chris Masonbe0e5c02007-01-26 15:51:26 -05003828 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003829 ret = 1;
3830 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003831 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303832 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003833
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003835 copy_extent_buffer(left, right,
3836 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3837 btrfs_item_nr_offset(0),
3838 push_items * sizeof(struct btrfs_item));
3839
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003840 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003841 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003842
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003843 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003844 leaf_data_end(left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003845 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003846 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003847 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003848 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003849 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003850
David Sterbac82f8232019-08-09 17:48:21 +02003851 btrfs_init_map_token(&token, left);
Chris Masondb945352007-10-15 16:15:53 -04003852 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003853 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003854 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003855
Ross Kirkdd3cc162013-09-16 15:58:09 +01003856 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003857
David Sterbacc4c13d2020-04-29 02:15:56 +02003858 ioff = btrfs_token_item_offset(&token, item);
3859 btrfs_set_token_item_offset(&token, item,
3860 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003861 }
Chris Mason5f39d392007-10-15 16:14:19 -04003862 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003863
3864 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003865 if (push_items > right_nritems)
3866 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003867 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003868
Chris Mason34a38212007-11-07 13:31:03 -05003869 if (push_items < right_nritems) {
3870 push_space = btrfs_item_offset_nr(right, push_items - 1) -
David Sterba8f881e82019-03-20 11:33:10 +01003871 leaf_data_end(right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003872 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003873 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003874 BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003875 leaf_data_end(right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003876
3877 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003878 btrfs_item_nr_offset(push_items),
3879 (btrfs_header_nritems(right) - push_items) *
3880 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003881 }
David Sterbac82f8232019-08-09 17:48:21 +02003882
3883 btrfs_init_map_token(&token, right);
Yaneef1c492007-11-26 10:58:13 -05003884 right_nritems -= push_items;
3885 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003886 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003887 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003888 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003889
David Sterbacc4c13d2020-04-29 02:15:56 +02003890 push_space = push_space - btrfs_token_item_size(&token, item);
3891 btrfs_set_token_item_offset(&token, item, push_space);
Chris Masondb945352007-10-15 16:15:53 -04003892 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003893
Chris Mason5f39d392007-10-15 16:14:19 -04003894 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003895 if (right_nritems)
3896 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003897 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003898 btrfs_clean_tree_block(right);
Chris Mason098f59c2007-05-11 11:33:21 -04003899
Chris Mason5f39d392007-10-15 16:14:19 -04003900 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003901 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003902
3903 /* then fixup the leaf pointer in the path */
3904 if (path->slots[0] < push_items) {
3905 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003906 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 free_extent_buffer(path->nodes[0]);
3908 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003909 path->slots[1] -= 1;
3910 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003911 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003912 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003913 path->slots[0] -= push_items;
3914 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003915 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003916 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003917out:
3918 btrfs_tree_unlock(left);
3919 free_extent_buffer(left);
3920 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003921}
3922
Chris Mason74123bd2007-02-02 11:05:29 -05003923/*
Chris Mason44871b12009-03-13 10:04:31 -04003924 * push some data in the path leaf to the left, trying to free up at
3925 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003926 *
3927 * max_slot can put a limit on how far into the leaf we'll push items. The
3928 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3929 * items
Chris Mason44871b12009-03-13 10:04:31 -04003930 */
3931static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003932 *root, struct btrfs_path *path, int min_data_size,
3933 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003934{
3935 struct extent_buffer *right = path->nodes[0];
3936 struct extent_buffer *left;
3937 int slot;
3938 int free_space;
3939 u32 right_nritems;
3940 int ret = 0;
3941
3942 slot = path->slots[1];
3943 if (slot == 0)
3944 return 1;
3945 if (!path->nodes[1])
3946 return 1;
3947
3948 right_nritems = btrfs_header_nritems(right);
3949 if (right_nritems == 0)
3950 return 1;
3951
3952 btrfs_assert_tree_locked(path->nodes[1]);
3953
David Sterba4b231ae2019-08-21 19:16:27 +02003954 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003955 /*
3956 * slot - 1 is not valid or we fail to read the left node,
3957 * no big deal, just return.
3958 */
3959 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003960 return 1;
3961
Josef Bacikbf774672020-08-20 11:46:04 -04003962 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
Chris Mason44871b12009-03-13 10:04:31 -04003963
David Sterbae902baa2019-03-20 14:36:46 +01003964 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04003965 if (free_space < data_size) {
3966 ret = 1;
3967 goto out;
3968 }
3969
3970 /* cow and double check */
3971 ret = btrfs_cow_block(trans, root, left,
Josef Bacik9631e4c2020-08-20 11:46:03 -04003972 path->nodes[1], slot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04003973 BTRFS_NESTING_LEFT_COW);
Chris Mason44871b12009-03-13 10:04:31 -04003974 if (ret) {
3975 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003976 if (ret == -ENOSPC)
3977 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003978 goto out;
3979 }
3980
David Sterbae902baa2019-03-20 14:36:46 +01003981 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04003982 if (free_space < data_size) {
3983 ret = 1;
3984 goto out;
3985 }
3986
Qu Wenruod16c7022020-08-19 14:35:50 +08003987 if (check_sibling_keys(left, right)) {
3988 ret = -EUCLEAN;
3989 goto out;
3990 }
David Sterba8087c192019-03-20 14:40:41 +01003991 return __push_leaf_left(path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04003992 empty, left, free_space, right_nritems,
3993 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003994out:
3995 btrfs_tree_unlock(left);
3996 free_extent_buffer(left);
3997 return ret;
3998}
3999
4000/*
Chris Mason74123bd2007-02-02 11:05:29 -05004001 * split the path's leaf in two, making sure there is at least data_size
4002 * available for the resulting leaf level of the path.
4003 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004004static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004005 struct btrfs_path *path,
4006 struct extent_buffer *l,
4007 struct extent_buffer *right,
4008 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004009{
David Sterba94f94ad2019-03-20 14:42:33 +01004010 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004011 int data_copy_size;
4012 int rt_data_off;
4013 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004014 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004015 struct btrfs_map_token token;
4016
Chris Mason5f39d392007-10-15 16:14:19 -04004017 nritems = nritems - mid;
4018 btrfs_set_header_nritems(right, nritems);
David Sterba8f881e82019-03-20 11:33:10 +01004019 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
Chris Mason5f39d392007-10-15 16:14:19 -04004020
4021 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4022 btrfs_item_nr_offset(mid),
4023 nritems * sizeof(struct btrfs_item));
4024
4025 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004026 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4027 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01004028 leaf_data_end(l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004029
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004030 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004031
David Sterbac82f8232019-08-09 17:48:21 +02004032 btrfs_init_map_token(&token, right);
Chris Mason5f39d392007-10-15 16:14:19 -04004033 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004034 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004035 u32 ioff;
4036
David Sterbacc4c13d2020-04-29 02:15:56 +02004037 ioff = btrfs_token_item_offset(&token, item);
4038 btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04004039 }
Chris Mason74123bd2007-02-02 11:05:29 -05004040
Chris Mason5f39d392007-10-15 16:14:19 -04004041 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004042 btrfs_item_key(right, &disk_key, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004043 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004044
4045 btrfs_mark_buffer_dirty(right);
4046 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004047 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004048
Chris Masonbe0e5c02007-01-26 15:51:26 -05004049 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004050 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004051 free_extent_buffer(path->nodes[0]);
4052 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004053 path->slots[0] -= mid;
4054 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004055 } else {
4056 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004057 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004058 }
Chris Mason5f39d392007-10-15 16:14:19 -04004059
Chris Masoneb60cea2007-02-02 09:18:22 -05004060 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004061}
4062
4063/*
Chris Mason99d8f832010-07-07 10:51:48 -04004064 * double splits happen when we need to insert a big item in the middle
4065 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4066 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4067 * A B C
4068 *
4069 * We avoid this by trying to push the items on either side of our target
4070 * into the adjacent leaves. If all goes well we can avoid the double split
4071 * completely.
4072 */
4073static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4074 struct btrfs_root *root,
4075 struct btrfs_path *path,
4076 int data_size)
4077{
4078 int ret;
4079 int progress = 0;
4080 int slot;
4081 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004082 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004083
4084 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004085 if (slot < btrfs_header_nritems(path->nodes[0]))
David Sterbae902baa2019-03-20 14:36:46 +01004086 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004087
4088 /*
4089 * try to push all the items after our slot into the
4090 * right leaf
4091 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004092 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004093 if (ret < 0)
4094 return ret;
4095
4096 if (ret == 0)
4097 progress++;
4098
4099 nritems = btrfs_header_nritems(path->nodes[0]);
4100 /*
4101 * our goal is to get our slot at the start or end of a leaf. If
4102 * we've done so we're done
4103 */
4104 if (path->slots[0] == 0 || path->slots[0] == nritems)
4105 return 0;
4106
David Sterbae902baa2019-03-20 14:36:46 +01004107 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004108 return 0;
4109
4110 /* try to push all the items before our slot into the next leaf */
4111 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004112 space_needed = data_size;
4113 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004114 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004115 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004116 if (ret < 0)
4117 return ret;
4118
4119 if (ret == 0)
4120 progress++;
4121
4122 if (progress)
4123 return 0;
4124 return 1;
4125}
4126
4127/*
Chris Mason44871b12009-03-13 10:04:31 -04004128 * split the path's leaf in two, making sure there is at least data_size
4129 * available for the resulting leaf level of the path.
4130 *
4131 * returns 0 if all went well and < 0 on failure.
4132 */
4133static noinline int split_leaf(struct btrfs_trans_handle *trans,
4134 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004135 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004136 struct btrfs_path *path, int data_size,
4137 int extend)
4138{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004139 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004140 struct extent_buffer *l;
4141 u32 nritems;
4142 int mid;
4143 int slot;
4144 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004145 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004146 int ret = 0;
4147 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004148 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004149 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004150 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004151
Yan, Zhenga5719522009-09-24 09:17:31 -04004152 l = path->nodes[0];
4153 slot = path->slots[0];
4154 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004155 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004156 return -EOVERFLOW;
4157
Chris Mason44871b12009-03-13 10:04:31 -04004158 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004159 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004160 int space_needed = data_size;
4161
4162 if (slot < btrfs_header_nritems(l))
David Sterbae902baa2019-03-20 14:36:46 +01004163 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004164
4165 wret = push_leaf_right(trans, root, path, space_needed,
4166 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004167 if (wret < 0)
4168 return wret;
4169 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004170 space_needed = data_size;
4171 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004172 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004173 wret = push_leaf_left(trans, root, path, space_needed,
4174 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004175 if (wret < 0)
4176 return wret;
4177 }
4178 l = path->nodes[0];
4179
4180 /* did the pushes work? */
David Sterbae902baa2019-03-20 14:36:46 +01004181 if (btrfs_leaf_free_space(l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004182 return 0;
4183 }
4184
4185 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004186 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004187 if (ret)
4188 return ret;
4189 }
4190again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004191 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004192 l = path->nodes[0];
4193 slot = path->slots[0];
4194 nritems = btrfs_header_nritems(l);
4195 mid = (nritems + 1) / 2;
4196
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004197 if (mid <= slot) {
4198 if (nritems == 1 ||
4199 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004200 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004201 if (slot >= nritems) {
4202 split = 0;
4203 } else {
4204 mid = slot;
4205 if (mid != nritems &&
4206 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004207 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004208 if (data_size && !tried_avoid_double)
4209 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004210 split = 2;
4211 }
4212 }
4213 }
4214 } else {
4215 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004216 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004217 if (!extend && data_size && slot == 0) {
4218 split = 0;
4219 } else if ((extend || !data_size) && slot == 0) {
4220 mid = 1;
4221 } else {
4222 mid = slot;
4223 if (mid != nritems &&
4224 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004226 if (data_size && !tried_avoid_double)
4227 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304228 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004229 }
4230 }
4231 }
4232 }
4233
4234 if (split == 0)
4235 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4236 else
4237 btrfs_item_key(l, &disk_key, mid);
4238
Josef Bacikca9d4732020-08-20 11:46:08 -04004239 /*
4240 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
4241 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
4242 * subclasses, which is 8 at the time of this patch, and we've maxed it
4243 * out. In the future we could add a
4244 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
4245 * use BTRFS_NESTING_NEW_ROOT.
4246 */
Filipe Mananaa6279472019-01-25 11:48:51 +00004247 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
Josef Bacikca9d4732020-08-20 11:46:08 -04004248 l->start, 0, num_doubles ?
4249 BTRFS_NESTING_NEW_ROOT :
4250 BTRFS_NESTING_SPLIT);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004251 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004252 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004253
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004254 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004255
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004256 if (split == 0) {
4257 if (mid <= slot) {
4258 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004259 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004260 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004261 btrfs_tree_unlock(path->nodes[0]);
4262 free_extent_buffer(path->nodes[0]);
4263 path->nodes[0] = right;
4264 path->slots[0] = 0;
4265 path->slots[1] += 1;
4266 } else {
4267 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004268 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004269 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004270 btrfs_tree_unlock(path->nodes[0]);
4271 free_extent_buffer(path->nodes[0]);
4272 path->nodes[0] = right;
4273 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004274 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004275 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004276 }
Liu Bo196e0242016-09-07 14:48:28 -07004277 /*
4278 * We create a new leaf 'right' for the required ins_len and
4279 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4280 * the content of ins_len to 'right'.
4281 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004282 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004283 }
4284
David Sterba94f94ad2019-03-20 14:42:33 +01004285 copy_for_split(trans, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004286
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004287 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004288 BUG_ON(num_doubles != 0);
4289 num_doubles++;
4290 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004291 }
Chris Mason44871b12009-03-13 10:04:31 -04004292
Jeff Mahoney143bede2012-03-01 14:56:26 +01004293 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004294
4295push_for_double:
4296 push_for_double_split(trans, root, path, data_size);
4297 tried_avoid_double = 1;
David Sterbae902baa2019-03-20 14:36:46 +01004298 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004299 return 0;
4300 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004301}
4302
Yan, Zhengad48fd752009-11-12 09:33:58 +00004303static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4304 struct btrfs_root *root,
4305 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004306{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004307 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004308 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004309 struct btrfs_file_extent_item *fi;
4310 u64 extent_len = 0;
4311 u32 item_size;
4312 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004313
4314 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004315 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4316
4317 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4318 key.type != BTRFS_EXTENT_CSUM_KEY);
4319
David Sterbae902baa2019-03-20 14:36:46 +01004320 if (btrfs_leaf_free_space(leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004321 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004322
4323 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004324 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4325 fi = btrfs_item_ptr(leaf, path->slots[0],
4326 struct btrfs_file_extent_item);
4327 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4328 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004329 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004330
Chris Mason459931e2008-12-10 09:10:46 -05004331 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004332 path->search_for_split = 1;
4333 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004334 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004335 if (ret > 0)
4336 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004337 if (ret < 0)
4338 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004339
Yan, Zhengad48fd752009-11-12 09:33:58 +00004340 ret = -EAGAIN;
4341 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004342 /* if our item isn't there, return now */
4343 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004344 goto err;
4345
Chris Mason109f6ae2010-04-02 09:20:18 -04004346 /* the leaf has changed, it now has room. return now */
David Sterbae902baa2019-03-20 14:36:46 +01004347 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004348 goto err;
4349
Yan, Zhengad48fd752009-11-12 09:33:58 +00004350 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4351 fi = btrfs_item_ptr(leaf, path->slots[0],
4352 struct btrfs_file_extent_item);
4353 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4354 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004355 }
4356
Yan, Zhengad48fd752009-11-12 09:33:58 +00004357 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004358 if (ret)
4359 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004360
Yan, Zhengad48fd752009-11-12 09:33:58 +00004361 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004362 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004363 return 0;
4364err:
4365 path->keep_locks = 0;
4366 return ret;
4367}
4368
David Sterba25263cd2019-03-20 14:44:57 +01004369static noinline int split_item(struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004370 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004371 unsigned long split_offset)
4372{
4373 struct extent_buffer *leaf;
4374 struct btrfs_item *item;
4375 struct btrfs_item *new_item;
4376 int slot;
4377 char *buf;
4378 u32 nritems;
4379 u32 item_size;
4380 u32 orig_offset;
4381 struct btrfs_disk_key disk_key;
4382
Chris Masonb9473432009-03-13 11:00:37 -04004383 leaf = path->nodes[0];
David Sterbae902baa2019-03-20 14:36:46 +01004384 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004385
Ross Kirkdd3cc162013-09-16 15:58:09 +01004386 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004387 orig_offset = btrfs_item_offset(leaf, item);
4388 item_size = btrfs_item_size(leaf, item);
4389
Chris Mason459931e2008-12-10 09:10:46 -05004390 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004391 if (!buf)
4392 return -ENOMEM;
4393
Chris Mason459931e2008-12-10 09:10:46 -05004394 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4395 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004396
Chris Mason459931e2008-12-10 09:10:46 -05004397 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004398 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004399 if (slot != nritems) {
4400 /* shift the items */
4401 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004402 btrfs_item_nr_offset(slot),
4403 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004404 }
4405
4406 btrfs_cpu_key_to_disk(&disk_key, new_key);
4407 btrfs_set_item_key(leaf, &disk_key, slot);
4408
Ross Kirkdd3cc162013-09-16 15:58:09 +01004409 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004410
4411 btrfs_set_item_offset(leaf, new_item, orig_offset);
4412 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4413
4414 btrfs_set_item_offset(leaf, item,
4415 orig_offset + item_size - split_offset);
4416 btrfs_set_item_size(leaf, item, split_offset);
4417
4418 btrfs_set_header_nritems(leaf, nritems + 1);
4419
4420 /* write the data for the start of the original item */
4421 write_extent_buffer(leaf, buf,
4422 btrfs_item_ptr_offset(leaf, path->slots[0]),
4423 split_offset);
4424
4425 /* write the data for the new item */
4426 write_extent_buffer(leaf, buf + split_offset,
4427 btrfs_item_ptr_offset(leaf, slot),
4428 item_size - split_offset);
4429 btrfs_mark_buffer_dirty(leaf);
4430
David Sterbae902baa2019-03-20 14:36:46 +01004431 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004432 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004433 return 0;
4434}
4435
4436/*
4437 * This function splits a single item into two items,
4438 * giving 'new_key' to the new item and splitting the
4439 * old one at split_offset (from the start of the item).
4440 *
4441 * The path may be released by this operation. After
4442 * the split, the path is pointing to the old item. The
4443 * new item is going to be in the same node as the old one.
4444 *
4445 * Note, the item being split must be smaller enough to live alone on
4446 * a tree block with room for one extra struct btrfs_item
4447 *
4448 * This allows us to split the item in place, keeping a lock on the
4449 * leaf the entire time.
4450 */
4451int btrfs_split_item(struct btrfs_trans_handle *trans,
4452 struct btrfs_root *root,
4453 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004454 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004455 unsigned long split_offset)
4456{
4457 int ret;
4458 ret = setup_leaf_for_split(trans, root, path,
4459 sizeof(struct btrfs_item));
4460 if (ret)
4461 return ret;
4462
David Sterba25263cd2019-03-20 14:44:57 +01004463 ret = split_item(path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004464 return ret;
4465}
4466
4467/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004468 * This function duplicate a item, giving 'new_key' to the new item.
4469 * It guarantees both items live in the same tree leaf and the new item
4470 * is contiguous with the original item.
4471 *
4472 * This allows us to split file extent in place, keeping a lock on the
4473 * leaf the entire time.
4474 */
4475int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4476 struct btrfs_root *root,
4477 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004478 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004479{
4480 struct extent_buffer *leaf;
4481 int ret;
4482 u32 item_size;
4483
4484 leaf = path->nodes[0];
4485 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4486 ret = setup_leaf_for_split(trans, root, path,
4487 item_size + sizeof(struct btrfs_item));
4488 if (ret)
4489 return ret;
4490
4491 path->slots[0]++;
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004492 setup_items_for_insert(root, path, new_key, &item_size, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004493 leaf = path->nodes[0];
4494 memcpy_extent_buffer(leaf,
4495 btrfs_item_ptr_offset(leaf, path->slots[0]),
4496 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4497 item_size);
4498 return 0;
4499}
4500
4501/*
Chris Masond352ac62008-09-29 15:18:18 -04004502 * make the item pointed to by the path smaller. new_size indicates
4503 * how small to make it, and from_end tells us if we just chop bytes
4504 * off the end of the item or if we shift the item to chop bytes off
4505 * the front.
4506 */
David Sterba78ac4f92019-03-20 14:49:12 +01004507void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004508{
Chris Masonb18c6682007-04-17 13:26:50 -04004509 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004510 struct extent_buffer *leaf;
4511 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004512 u32 nritems;
4513 unsigned int data_end;
4514 unsigned int old_data_start;
4515 unsigned int old_size;
4516 unsigned int size_diff;
4517 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004518 struct btrfs_map_token token;
4519
Chris Mason5f39d392007-10-15 16:14:19 -04004520 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004521 slot = path->slots[0];
4522
4523 old_size = btrfs_item_size_nr(leaf, slot);
4524 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004525 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004526
Chris Mason5f39d392007-10-15 16:14:19 -04004527 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004528 data_end = leaf_data_end(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004529
Chris Mason5f39d392007-10-15 16:14:19 -04004530 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004531
Chris Masonb18c6682007-04-17 13:26:50 -04004532 size_diff = old_size - new_size;
4533
4534 BUG_ON(slot < 0);
4535 BUG_ON(slot >= nritems);
4536
4537 /*
4538 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4539 */
4540 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004541 btrfs_init_map_token(&token, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004542 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004543 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004544 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004545
David Sterbacc4c13d2020-04-29 02:15:56 +02004546 ioff = btrfs_token_item_offset(&token, item);
4547 btrfs_set_token_item_offset(&token, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04004548 }
Chris Masondb945352007-10-15 16:15:53 -04004549
Chris Masonb18c6682007-04-17 13:26:50 -04004550 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004551 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004552 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4553 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004554 data_end, old_data_start + new_size - data_end);
4555 } else {
4556 struct btrfs_disk_key disk_key;
4557 u64 offset;
4558
4559 btrfs_item_key(leaf, &disk_key, slot);
4560
4561 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4562 unsigned long ptr;
4563 struct btrfs_file_extent_item *fi;
4564
4565 fi = btrfs_item_ptr(leaf, slot,
4566 struct btrfs_file_extent_item);
4567 fi = (struct btrfs_file_extent_item *)(
4568 (unsigned long)fi - size_diff);
4569
4570 if (btrfs_file_extent_type(leaf, fi) ==
4571 BTRFS_FILE_EXTENT_INLINE) {
4572 ptr = btrfs_item_ptr_offset(leaf, slot);
4573 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004574 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004575 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004576 }
4577 }
4578
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004579 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4580 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004581 data_end, old_data_start - data_end);
4582
4583 offset = btrfs_disk_key_offset(&disk_key);
4584 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4585 btrfs_set_item_key(leaf, &disk_key, slot);
4586 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004587 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004588 }
Chris Mason5f39d392007-10-15 16:14:19 -04004589
Ross Kirkdd3cc162013-09-16 15:58:09 +01004590 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004591 btrfs_set_item_size(leaf, item, new_size);
4592 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004593
David Sterbae902baa2019-03-20 14:36:46 +01004594 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004595 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004596 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004597 }
Chris Masonb18c6682007-04-17 13:26:50 -04004598}
4599
Chris Masond352ac62008-09-29 15:18:18 -04004600/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004601 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004602 */
David Sterbac71dd882019-03-20 14:51:10 +01004603void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004604{
Chris Mason6567e832007-04-16 09:22:45 -04004605 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004606 struct extent_buffer *leaf;
4607 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004608 u32 nritems;
4609 unsigned int data_end;
4610 unsigned int old_data;
4611 unsigned int old_size;
4612 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004613 struct btrfs_map_token token;
4614
Chris Mason5f39d392007-10-15 16:14:19 -04004615 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004616
Chris Mason5f39d392007-10-15 16:14:19 -04004617 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004618 data_end = leaf_data_end(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004619
David Sterbae902baa2019-03-20 14:36:46 +01004620 if (btrfs_leaf_free_space(leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004621 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004622 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004623 }
Chris Mason6567e832007-04-16 09:22:45 -04004624 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004625 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004626
4627 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004628 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004629 btrfs_print_leaf(leaf);
David Sterbac71dd882019-03-20 14:51:10 +01004630 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004631 slot, nritems);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004632 BUG();
Chris Mason3326d1b2007-10-15 16:18:25 -04004633 }
Chris Mason6567e832007-04-16 09:22:45 -04004634
4635 /*
4636 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4637 */
4638 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004639 btrfs_init_map_token(&token, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004640 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004641 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004642 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004643
David Sterbacc4c13d2020-04-29 02:15:56 +02004644 ioff = btrfs_token_item_offset(&token, item);
4645 btrfs_set_token_item_offset(&token, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04004646 }
Chris Mason5f39d392007-10-15 16:14:19 -04004647
Chris Mason6567e832007-04-16 09:22:45 -04004648 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004649 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4650 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004651 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004652
Chris Mason6567e832007-04-16 09:22:45 -04004653 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004654 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004655 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004656 btrfs_set_item_size(leaf, item, old_size + data_size);
4657 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004658
David Sterbae902baa2019-03-20 14:36:46 +01004659 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004660 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004661 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004662 }
Chris Mason6567e832007-04-16 09:22:45 -04004663}
4664
Nikolay Borisovda9ffb22020-09-01 17:40:00 +03004665/**
4666 * setup_items_for_insert - Helper called before inserting one or more items
4667 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
4668 * in a function that doesn't call btrfs_search_slot
4669 *
4670 * @root: root we are inserting items to
4671 * @path: points to the leaf/slot where we are going to insert new items
4672 * @cpu_key: array of keys for items to be inserted
4673 * @data_size: size of the body of each item we are going to insert
4674 * @nr: size of @cpu_key/@data_size arrays
Chris Mason74123bd2007-02-02 11:05:29 -05004675 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004676void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004677 const struct btrfs_key *cpu_key, u32 *data_size,
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004678 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004679{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004680 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004681 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004682 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004683 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004684 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004685 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004686 struct extent_buffer *leaf;
4687 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004688 struct btrfs_map_token token;
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004689 u32 total_size;
4690 u32 total_data = 0;
4691
4692 for (i = 0; i < nr; i++)
4693 total_data += data_size[i];
4694 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Masoncfed81a2012-03-03 07:40:03 -05004695
Filipe Manana24cdc842014-07-28 19:34:35 +01004696 if (path->slots[0] == 0) {
4697 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004698 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004699 }
4700 btrfs_unlock_up_safe(path, 1);
4701
Chris Mason5f39d392007-10-15 16:14:19 -04004702 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004703 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004704
Chris Mason5f39d392007-10-15 16:14:19 -04004705 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004706 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004707
David Sterbae902baa2019-03-20 14:36:46 +01004708 if (btrfs_leaf_free_space(leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004709 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004710 btrfs_crit(fs_info, "not enough freespace need %u have %d",
David Sterbae902baa2019-03-20 14:36:46 +01004711 total_size, btrfs_leaf_free_space(leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004712 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004713 }
Chris Mason5f39d392007-10-15 16:14:19 -04004714
David Sterbac82f8232019-08-09 17:48:21 +02004715 btrfs_init_map_token(&token, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004716 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004717 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004718
Chris Mason5f39d392007-10-15 16:14:19 -04004719 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004720 btrfs_print_leaf(leaf);
Nikolay Borisov7269ddd2020-09-01 17:40:01 +03004721 btrfs_crit(fs_info,
4722 "item at slot %d with data offset %u beyond data end of leaf %u",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004723 slot, old_data, data_end);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004724 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004725 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004726 /*
4727 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4728 */
4729 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004730 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004731 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004732
Jeff Mahoney62e85572016-09-20 10:05:01 -04004733 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02004734 ioff = btrfs_token_item_offset(&token, item);
4735 btrfs_set_token_item_offset(&token, item,
4736 ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04004737 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004738 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004739 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004740 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004741 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004742
4743 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004744 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4745 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004746 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004747 data_end = old_data;
4748 }
Chris Mason5f39d392007-10-15 16:14:19 -04004749
Chris Mason62e27492007-03-15 12:56:47 -04004750 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004751 for (i = 0; i < nr; i++) {
4752 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4753 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004754 item = btrfs_item_nr(slot + i);
Chris Mason9c583092008-01-29 15:15:18 -05004755 data_end -= data_size[i];
Nikolay Borisovfc0716c2020-09-01 17:39:57 +03004756 btrfs_set_token_item_offset(&token, item, data_end);
David Sterbacc4c13d2020-04-29 02:15:56 +02004757 btrfs_set_token_item_size(&token, item, data_size[i]);
Chris Mason9c583092008-01-29 15:15:18 -05004758 }
Chris Mason44871b12009-03-13 10:04:31 -04004759
Chris Mason9c583092008-01-29 15:15:18 -05004760 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004761 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004762
David Sterbae902baa2019-03-20 14:36:46 +01004763 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004764 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004765 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004766 }
Chris Mason44871b12009-03-13 10:04:31 -04004767}
4768
4769/*
4770 * Given a key and some data, insert items into the tree.
4771 * This does all the path init required, making room in the tree if needed.
4772 */
4773int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4774 struct btrfs_root *root,
4775 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004776 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004777 int nr)
4778{
Chris Mason44871b12009-03-13 10:04:31 -04004779 int ret = 0;
4780 int slot;
4781 int i;
4782 u32 total_size = 0;
4783 u32 total_data = 0;
4784
4785 for (i = 0; i < nr; i++)
4786 total_data += data_size[i];
4787
4788 total_size = total_data + (nr * sizeof(struct btrfs_item));
4789 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4790 if (ret == 0)
4791 return -EEXIST;
4792 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004793 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004794
Chris Mason44871b12009-03-13 10:04:31 -04004795 slot = path->slots[0];
4796 BUG_ON(slot < 0);
4797
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004798 setup_items_for_insert(root, path, cpu_key, data_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004799 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004800}
4801
4802/*
4803 * Given a key and some data, insert an item into the tree.
4804 * This does all the path init required, making room in the tree if needed.
4805 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004806int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4807 const struct btrfs_key *cpu_key, void *data,
4808 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004809{
4810 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004811 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004812 struct extent_buffer *leaf;
4813 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004814
Chris Mason2c90e5d2007-04-02 10:50:19 -04004815 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004816 if (!path)
4817 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004818 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004819 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004820 leaf = path->nodes[0];
4821 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4822 write_extent_buffer(leaf, data, ptr, data_size);
4823 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004824 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004825 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004826 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004827}
4828
Chris Mason74123bd2007-02-02 11:05:29 -05004829/*
Chris Mason5de08d72007-02-24 06:24:44 -05004830 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004831 *
Chris Masond352ac62008-09-29 15:18:18 -04004832 * the tree should have been previously balanced so the deletion does not
4833 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004834 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004835static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4836 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004837{
Chris Mason5f39d392007-10-15 16:14:19 -04004838 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004839 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004840 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004841
Chris Mason5f39d392007-10-15 16:14:19 -04004842 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004843 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004844 if (level) {
4845 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004846 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004847 BUG_ON(ret < 0);
4848 }
Chris Mason5f39d392007-10-15 16:14:19 -04004849 memmove_extent_buffer(parent,
4850 btrfs_node_key_ptr_offset(slot),
4851 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004852 sizeof(struct btrfs_key_ptr) *
4853 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004854 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004855 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4856 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004857 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004858 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004859
Chris Mason7518a232007-03-12 12:01:18 -04004860 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004861 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004862 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004863 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004864 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004865 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004866 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004867 struct btrfs_disk_key disk_key;
4868
4869 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004870 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004871 }
Chris Masond6025572007-03-30 14:27:56 -04004872 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004873}
4874
Chris Mason74123bd2007-02-02 11:05:29 -05004875/*
Chris Mason323ac952008-10-01 19:05:46 -04004876 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004877 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004878 *
4879 * This deletes the pointer in path->nodes[1] and frees the leaf
4880 * block extent. zero is returned if it all worked out, < 0 otherwise.
4881 *
4882 * The path must have already been setup for deleting the leaf, including
4883 * all the proper balancing. path->nodes[1] must be locked.
4884 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004885static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4886 struct btrfs_root *root,
4887 struct btrfs_path *path,
4888 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004889{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004890 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004891 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004892
Chris Mason4d081c42009-02-04 09:31:28 -05004893 /*
4894 * btrfs_free_extent is expensive, we want to make sure we
4895 * aren't holding any locks when we call it
4896 */
4897 btrfs_unlock_up_safe(path, 0);
4898
Yan, Zhengf0486c62010-05-16 10:46:25 -04004899 root_sub_used(root, leaf->len);
4900
David Sterba67439da2019-10-08 13:28:47 +02004901 atomic_inc(&leaf->refs);
Jan Schmidt5581a512012-05-16 17:04:52 +02004902 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004903 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004904}
4905/*
Chris Mason74123bd2007-02-02 11:05:29 -05004906 * delete the item at the leaf level in path. If that empties
4907 * the leaf, remove it from the tree
4908 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004909int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4910 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004911{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004912 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004913 struct extent_buffer *leaf;
4914 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004915 u32 last_off;
4916 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004917 int ret = 0;
4918 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004919 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004920 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004921
Chris Mason5f39d392007-10-15 16:14:19 -04004922 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004923 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4924
4925 for (i = 0; i < nr; i++)
4926 dsize += btrfs_item_size_nr(leaf, slot + i);
4927
Chris Mason5f39d392007-10-15 16:14:19 -04004928 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004929
Chris Mason85e21ba2008-01-29 15:11:36 -05004930 if (slot + nr != nritems) {
David Sterba8f881e82019-03-20 11:33:10 +01004931 int data_end = leaf_data_end(leaf);
David Sterbac82f8232019-08-09 17:48:21 +02004932 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04004933
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004934 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004935 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004936 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004937 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004938
David Sterbac82f8232019-08-09 17:48:21 +02004939 btrfs_init_map_token(&token, leaf);
Chris Mason85e21ba2008-01-29 15:11:36 -05004940 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004941 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004942
Ross Kirkdd3cc162013-09-16 15:58:09 +01004943 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02004944 ioff = btrfs_token_item_offset(&token, item);
4945 btrfs_set_token_item_offset(&token, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04004946 }
Chris Masondb945352007-10-15 16:15:53 -04004947
Chris Mason5f39d392007-10-15 16:14:19 -04004948 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004949 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004950 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004951 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004952 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004953 btrfs_set_header_nritems(leaf, nritems - nr);
4954 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004955
Chris Mason74123bd2007-02-02 11:05:29 -05004956 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004957 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004958 if (leaf == root->node) {
4959 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004960 } else {
David Sterba6a884d7d2019-03-20 14:30:02 +01004961 btrfs_clean_tree_block(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004962 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004963 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004964 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004965 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004966 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004967 struct btrfs_disk_key disk_key;
4968
4969 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004970 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004971 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004972
Chris Mason74123bd2007-02-02 11:05:29 -05004973 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004974 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004975 /* push_leaf_left fixes the path.
4976 * make sure the path still points to our leaf
4977 * for possible call to del_ptr below
4978 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004979 slot = path->slots[1];
David Sterba67439da2019-10-08 13:28:47 +02004980 atomic_inc(&leaf->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04004981
Chris Mason99d8f832010-07-07 10:51:48 -04004982 wret = push_leaf_left(trans, root, path, 1, 1,
4983 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004984 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004985 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004986
4987 if (path->nodes[0] == leaf &&
4988 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004989 wret = push_leaf_right(trans, root, path, 1,
4990 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004991 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004992 ret = wret;
4993 }
Chris Mason5f39d392007-10-15 16:14:19 -04004994
4995 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004996 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004997 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004998 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004999 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005000 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005001 /* if we're still in the path, make sure
5002 * we're dirty. Otherwise, one of the
5003 * push_leaf functions must have already
5004 * dirtied this buffer
5005 */
5006 if (path->nodes[0] == leaf)
5007 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005008 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005009 }
Chris Masond5719762007-03-23 10:01:08 -04005010 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005011 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005012 }
5013 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005014 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005015}
5016
Chris Mason97571fd2007-02-24 13:39:08 -05005017/*
Chris Mason925baed2008-06-25 16:01:30 -04005018 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005019 * returns 0 if it found something or 1 if there are no lesser leaves.
5020 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005021 *
5022 * This may release the path, and so you may lose any locks held at the
5023 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005024 */
Josef Bacik16e75492013-10-22 12:18:51 -04005025int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005026{
Chris Mason925baed2008-06-25 16:01:30 -04005027 struct btrfs_key key;
5028 struct btrfs_disk_key found_key;
5029 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005030
Chris Mason925baed2008-06-25 16:01:30 -04005031 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005032
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005033 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005034 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005035 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005036 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005037 key.offset = (u64)-1;
5038 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005039 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005040 key.type = (u8)-1;
5041 key.offset = (u64)-1;
5042 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005043 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005044 }
Chris Mason7bb86312007-12-11 09:25:06 -05005045
David Sterbab3b4aa72011-04-21 01:20:15 +02005046 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005047 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5048 if (ret < 0)
5049 return ret;
5050 btrfs_item_key(path->nodes[0], &found_key, 0);
5051 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005052 /*
5053 * We might have had an item with the previous key in the tree right
5054 * before we released our path. And after we released our path, that
5055 * item might have been pushed to the first slot (0) of the leaf we
5056 * were holding due to a tree balance. Alternatively, an item with the
5057 * previous key can exist as the only element of a leaf (big fat item).
5058 * Therefore account for these 2 cases, so that our callers (like
5059 * btrfs_previous_item) don't miss an existing item with a key matching
5060 * the previous key we computed above.
5061 */
5062 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005063 return 0;
5064 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005065}
5066
Chris Mason3f157a22008-06-25 16:01:31 -04005067/*
5068 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005069 * for nodes or leaves that are have a minimum transaction id.
5070 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005071 *
5072 * This does not cow, but it does stuff the starting key it finds back
5073 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5074 * key and get a writable path.
5075 *
Chris Mason3f157a22008-06-25 16:01:31 -04005076 * This honors path->lowest_level to prevent descent past a given level
5077 * of the tree.
5078 *
Chris Masond352ac62008-09-29 15:18:18 -04005079 * min_trans indicates the oldest transaction that you are interested
5080 * in walking through. Any nodes or leaves older than min_trans are
5081 * skipped over (without reading them).
5082 *
Chris Mason3f157a22008-06-25 16:01:31 -04005083 * returns zero if something useful was found, < 0 on error and 1 if there
5084 * was nothing in the tree that matched the search criteria.
5085 */
5086int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005087 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005088 u64 min_trans)
5089{
5090 struct extent_buffer *cur;
5091 struct btrfs_key found_key;
5092 int slot;
Yan96524802008-07-24 12:19:49 -04005093 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005094 u32 nritems;
5095 int level;
5096 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005097 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005098
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005099 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005100again:
Chris Masonbd681512011-07-16 15:23:14 -04005101 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005102 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005103 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005104 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005105 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005106
5107 if (btrfs_header_generation(cur) < min_trans) {
5108 ret = 1;
5109 goto out;
5110 }
Chris Masond3977122009-01-05 21:25:51 -05005111 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005112 nritems = btrfs_header_nritems(cur);
5113 level = btrfs_header_level(cur);
Qu Wenruoe3b83362020-04-17 15:08:21 +08005114 sret = btrfs_bin_search(cur, min_key, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00005115 if (sret < 0) {
5116 ret = sret;
5117 goto out;
5118 }
Chris Mason3f157a22008-06-25 16:01:31 -04005119
Chris Mason323ac952008-10-01 19:05:46 -04005120 /* at the lowest level, we're done, setup the path and exit */
5121 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005122 if (slot >= nritems)
5123 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005124 ret = 0;
5125 path->slots[level] = slot;
5126 btrfs_item_key_to_cpu(cur, &found_key, slot);
5127 goto out;
5128 }
Yan96524802008-07-24 12:19:49 -04005129 if (sret && slot > 0)
5130 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005131 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005132 * check this node pointer against the min_trans parameters.
Randy Dunlap260db432020-08-04 19:48:34 -07005133 * If it is too old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005134 */
Chris Masond3977122009-01-05 21:25:51 -05005135 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005136 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005137
Chris Mason3f157a22008-06-25 16:01:31 -04005138 gen = btrfs_node_ptr_generation(cur, slot);
5139 if (gen < min_trans) {
5140 slot++;
5141 continue;
5142 }
Eric Sandeende78b512013-01-31 18:21:12 +00005143 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005144 }
Chris Masone02119d2008-09-05 16:13:11 -04005145find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005146 /*
5147 * we didn't find a candidate key in this node, walk forward
5148 * and find another one
5149 */
5150 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005151 path->slots[level] = slot;
5152 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005153 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005154 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005155 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005156 goto again;
5157 } else {
5158 goto out;
5159 }
5160 }
5161 /* save our key for returning back */
5162 btrfs_node_key_to_cpu(cur, &found_key, slot);
5163 path->slots[level] = slot;
5164 if (level == path->lowest_level) {
5165 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005166 goto out;
5167 }
David Sterba4b231ae2019-08-21 19:16:27 +02005168 cur = btrfs_read_node_slot(cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005169 if (IS_ERR(cur)) {
5170 ret = PTR_ERR(cur);
5171 goto out;
5172 }
Chris Mason3f157a22008-06-25 16:01:31 -04005173
Chris Masonbd681512011-07-16 15:23:14 -04005174 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005175
Chris Masonbd681512011-07-16 15:23:14 -04005176 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005177 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005178 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005179 }
5180out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005181 path->keep_locks = keep_locks;
5182 if (ret == 0) {
5183 btrfs_unlock_up_safe(path, path->lowest_level + 1);
Chris Mason3f157a22008-06-25 16:01:31 -04005184 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005185 }
Chris Mason3f157a22008-06-25 16:01:31 -04005186 return ret;
5187}
5188
5189/*
5190 * this is similar to btrfs_next_leaf, but does not try to preserve
5191 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005192 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005193 *
5194 * 0 is returned if another key is found, < 0 if there are any errors
5195 * and 1 is returned if there are no higher keys in the tree
5196 *
5197 * path->keep_locks should be set to 1 on the search made before
5198 * calling this function.
5199 */
Chris Masone7a84562008-06-25 16:01:31 -04005200int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005201 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005202{
Chris Masone7a84562008-06-25 16:01:31 -04005203 int slot;
5204 struct extent_buffer *c;
5205
Josef Bacik6a9fb462019-06-20 15:37:52 -04005206 WARN_ON(!path->keep_locks && !path->skip_locking);
Chris Masond3977122009-01-05 21:25:51 -05005207 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005208 if (!path->nodes[level])
5209 return 1;
5210
5211 slot = path->slots[level] + 1;
5212 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005213next:
Chris Masone7a84562008-06-25 16:01:31 -04005214 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005215 int ret;
5216 int orig_lowest;
5217 struct btrfs_key cur_key;
5218 if (level + 1 >= BTRFS_MAX_LEVEL ||
5219 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005220 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005221
Josef Bacik6a9fb462019-06-20 15:37:52 -04005222 if (path->locks[level + 1] || path->skip_locking) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005223 level++;
5224 continue;
5225 }
5226
5227 slot = btrfs_header_nritems(c) - 1;
5228 if (level == 0)
5229 btrfs_item_key_to_cpu(c, &cur_key, slot);
5230 else
5231 btrfs_node_key_to_cpu(c, &cur_key, slot);
5232
5233 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005234 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005235 path->lowest_level = level;
5236 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5237 0, 0);
5238 path->lowest_level = orig_lowest;
5239 if (ret < 0)
5240 return ret;
5241
5242 c = path->nodes[level];
5243 slot = path->slots[level];
5244 if (ret == 0)
5245 slot++;
5246 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005247 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005248
Chris Masone7a84562008-06-25 16:01:31 -04005249 if (level == 0)
5250 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005251 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005252 u64 gen = btrfs_node_ptr_generation(c, slot);
5253
Chris Mason3f157a22008-06-25 16:01:31 -04005254 if (gen < min_trans) {
5255 slot++;
5256 goto next;
5257 }
Chris Masone7a84562008-06-25 16:01:31 -04005258 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005259 }
Chris Masone7a84562008-06-25 16:01:31 -04005260 return 0;
5261 }
5262 return 1;
5263}
5264
Chris Mason7bb86312007-12-11 09:25:06 -05005265/*
Chris Mason925baed2008-06-25 16:01:30 -04005266 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005267 * returns 0 if it found something or 1 if there are no greater leaves.
5268 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005269 */
Chris Mason234b63a2007-03-13 10:46:10 -04005270int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005271{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005272 return btrfs_next_old_leaf(root, path, 0);
5273}
5274
5275int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5276 u64 time_seq)
5277{
Chris Masond97e63b2007-02-20 16:40:44 -05005278 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005279 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005280 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005281 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005282 struct btrfs_key key;
5283 u32 nritems;
5284 int ret;
Josef Bacik0e463182020-11-06 16:27:30 -05005285 int i;
Chris Mason925baed2008-06-25 16:01:30 -04005286
5287 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005288 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005289 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005290
Chris Mason8e73f272009-04-03 10:14:18 -04005291 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5292again:
5293 level = 1;
5294 next = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +02005295 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005296
Chris Masona2135012008-06-25 16:01:30 -04005297 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005298
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005299 if (time_seq)
5300 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5301 else
5302 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005303 path->keep_locks = 0;
5304
5305 if (ret < 0)
5306 return ret;
5307
Chris Masona2135012008-06-25 16:01:30 -04005308 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005309 /*
5310 * by releasing the path above we dropped all our locks. A balance
5311 * could have added more items next to the key that used to be
5312 * at the very end of the block. So, check again here and
5313 * advance the path if there are now more items available.
5314 */
Chris Masona2135012008-06-25 16:01:30 -04005315 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005316 if (ret == 0)
5317 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005318 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005319 goto done;
5320 }
Liu Bo0b43e042014-06-09 11:04:49 +08005321 /*
5322 * So the above check misses one case:
5323 * - after releasing the path above, someone has removed the item that
5324 * used to be at the very end of the block, and balance between leafs
5325 * gets another one with bigger key.offset to replace it.
5326 *
5327 * This one should be returned as well, or we can get leaf corruption
5328 * later(esp. in __btrfs_drop_extents()).
5329 *
5330 * And a bit more explanation about this check,
5331 * with ret > 0, the key isn't found, the path points to the slot
5332 * where it should be inserted, so the path->slots[0] item must be the
5333 * bigger one.
5334 */
5335 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5336 ret = 0;
5337 goto done;
5338 }
Chris Masond97e63b2007-02-20 16:40:44 -05005339
Chris Masond3977122009-01-05 21:25:51 -05005340 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005341 if (!path->nodes[level]) {
5342 ret = 1;
5343 goto done;
5344 }
Chris Mason5f39d392007-10-15 16:14:19 -04005345
Chris Masond97e63b2007-02-20 16:40:44 -05005346 slot = path->slots[level] + 1;
5347 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005348 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005349 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005350 if (level == BTRFS_MAX_LEVEL) {
5351 ret = 1;
5352 goto done;
5353 }
Chris Masond97e63b2007-02-20 16:40:44 -05005354 continue;
5355 }
Chris Mason5f39d392007-10-15 16:14:19 -04005356
Josef Bacik0e463182020-11-06 16:27:30 -05005357
5358 /*
5359 * Our current level is where we're going to start from, and to
5360 * make sure lockdep doesn't complain we need to drop our locks
5361 * and nodes from 0 to our current level.
5362 */
5363 for (i = 0; i < level; i++) {
5364 if (path->locks[level]) {
5365 btrfs_tree_read_unlock(path->nodes[i]);
5366 path->locks[i] = 0;
5367 }
5368 free_extent_buffer(path->nodes[i]);
5369 path->nodes[i] = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04005370 }
Chris Mason5f39d392007-10-15 16:14:19 -04005371
Chris Mason8e73f272009-04-03 10:14:18 -04005372 next = c;
Liu Bod07b8522017-01-30 12:23:42 -08005373 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005374 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005375 if (ret == -EAGAIN)
5376 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005377
Chris Mason76a05b32009-05-14 13:24:30 -04005378 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005379 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005380 goto done;
5381 }
5382
Chris Mason5cd57b22008-06-25 16:01:30 -04005383 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005384 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005385 if (!ret && time_seq) {
5386 /*
5387 * If we don't get the lock, we may be racing
5388 * with push_leaf_left, holding that lock while
5389 * itself waiting for the leaf we've currently
5390 * locked. To solve this situation, we give up
5391 * on our lock and cycle.
5392 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005393 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005394 btrfs_release_path(path);
5395 cond_resched();
5396 goto again;
5397 }
Josef Bacik0e463182020-11-06 16:27:30 -05005398 if (!ret)
5399 btrfs_tree_read_lock(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04005400 }
Chris Masond97e63b2007-02-20 16:40:44 -05005401 break;
5402 }
5403 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005404 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005405 level--;
Chris Masond97e63b2007-02-20 16:40:44 -05005406 path->nodes[level] = next;
5407 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005408 if (!path->skip_locking)
Josef Bacikffeb03c2020-11-06 16:27:29 -05005409 path->locks[level] = BTRFS_READ_LOCK;
Chris Masond97e63b2007-02-20 16:40:44 -05005410 if (!level)
5411 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005412
Liu Bod07b8522017-01-30 12:23:42 -08005413 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005414 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005415 if (ret == -EAGAIN)
5416 goto again;
5417
Chris Mason76a05b32009-05-14 13:24:30 -04005418 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005419 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005420 goto done;
5421 }
5422
Josef Bacikffeb03c2020-11-06 16:27:29 -05005423 if (!path->skip_locking)
Josef Bacik0e463182020-11-06 16:27:30 -05005424 btrfs_tree_read_lock(next);
Chris Masond97e63b2007-02-20 16:40:44 -05005425 }
Chris Mason8e73f272009-04-03 10:14:18 -04005426 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005427done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005428 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005429
5430 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005431}
Chris Mason0b86a832008-03-24 15:01:56 -04005432
Chris Mason3f157a22008-06-25 16:01:31 -04005433/*
5434 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5435 * searching until it gets past min_objectid or finds an item of 'type'
5436 *
5437 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5438 */
Chris Mason0b86a832008-03-24 15:01:56 -04005439int btrfs_previous_item(struct btrfs_root *root,
5440 struct btrfs_path *path, u64 min_objectid,
5441 int type)
5442{
5443 struct btrfs_key found_key;
5444 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005445 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005446 int ret;
5447
Chris Masond3977122009-01-05 21:25:51 -05005448 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005449 if (path->slots[0] == 0) {
5450 ret = btrfs_prev_leaf(root, path);
5451 if (ret != 0)
5452 return ret;
5453 } else {
5454 path->slots[0]--;
5455 }
5456 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005457 nritems = btrfs_header_nritems(leaf);
5458 if (nritems == 0)
5459 return 1;
5460 if (path->slots[0] == nritems)
5461 path->slots[0]--;
5462
Chris Mason0b86a832008-03-24 15:01:56 -04005463 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005464 if (found_key.objectid < min_objectid)
5465 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005466 if (found_key.type == type)
5467 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005468 if (found_key.objectid == min_objectid &&
5469 found_key.type < type)
5470 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005471 }
5472 return 1;
5473}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005474
5475/*
5476 * search in extent tree to find a previous Metadata/Data extent item with
5477 * min objecitd.
5478 *
5479 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5480 */
5481int btrfs_previous_extent_item(struct btrfs_root *root,
5482 struct btrfs_path *path, u64 min_objectid)
5483{
5484 struct btrfs_key found_key;
5485 struct extent_buffer *leaf;
5486 u32 nritems;
5487 int ret;
5488
5489 while (1) {
5490 if (path->slots[0] == 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08005491 ret = btrfs_prev_leaf(root, path);
5492 if (ret != 0)
5493 return ret;
5494 } else {
5495 path->slots[0]--;
5496 }
5497 leaf = path->nodes[0];
5498 nritems = btrfs_header_nritems(leaf);
5499 if (nritems == 0)
5500 return 1;
5501 if (path->slots[0] == nritems)
5502 path->slots[0]--;
5503
5504 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5505 if (found_key.objectid < min_objectid)
5506 break;
5507 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5508 found_key.type == BTRFS_METADATA_ITEM_KEY)
5509 return 0;
5510 if (found_key.objectid == min_objectid &&
5511 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5512 break;
5513 }
5514 return 1;
5515}