blob: 519cf145f9bd176596bef40a03a17a6b3e81b07e [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 Bacik4d3edf72021-01-14 14:02:46 -0500224 if (ret) {
Filipe Mananade3ea5b2021-02-04 14:35:44 +0000225 btrfs_tree_unlock(cow);
226 free_extent_buffer(cow);
Josef Bacik4d3edf72021-01-14 14:02:46 -0500227 btrfs_abort_transaction(trans, ret);
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 return ret;
Josef Bacik4d3edf72021-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
Josef Bacik9ec72672013-08-07 16:57:23 -04001284 btrfs_set_path_blocking(path);
David Sterba300aa892018-04-04 02:00:17 +02001285 btrfs_set_lock_blocking_read(eb);
Josef Bacik9ec72672013-08-07 16:57:23 -04001286
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001287 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1288 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001289 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001290 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001291 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001292 free_extent_buffer(eb);
1293 return NULL;
1294 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001295 btrfs_set_header_bytenr(eb_rewin, eb->start);
1296 btrfs_set_header_backref_rev(eb_rewin,
1297 btrfs_header_backref_rev(eb));
1298 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001299 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001300 } else {
1301 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001302 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001303 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001304 free_extent_buffer(eb);
1305 return NULL;
1306 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307 }
1308
Josef Bacik9ec72672013-08-07 16:57:23 -04001309 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001310 free_extent_buffer(eb);
1311
Josef Bacikd3beaa22020-08-10 11:42:31 -04001312 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
1313 eb_rewin, btrfs_header_level(eb_rewin));
Jan Schmidt47fb0912013-04-13 13:19:55 +00001314 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001315 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001316 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001317 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001318
1319 return eb_rewin;
1320}
1321
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001322/*
1323 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1324 * value. If there are no changes, the current root->root_node is returned. If
1325 * anything changed in between, there's a fresh buffer allocated on which the
1326 * rewind operations are done. In any case, the returned buffer is read locked.
1327 * Returns NULL on error (with no locks held).
1328 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001329static inline struct extent_buffer *
1330get_old_root(struct btrfs_root *root, u64 time_seq)
1331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001332 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001333 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001334 struct extent_buffer *eb = NULL;
1335 struct extent_buffer *eb_root;
Filipe Mananaefad8a82019-08-12 19:14:29 +01001336 u64 eb_root_owner = 0;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001337 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001338 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001339 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001340 u64 logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001341 int level;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001342
Jan Schmidt30b04632013-04-13 13:19:54 +00001343 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001344 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001345 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001346 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001347
Jan Schmidta95236d2012-06-05 16:41:24 +02001348 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1349 old_root = &tm->old_root;
1350 old_generation = tm->generation;
1351 logical = old_root->logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001352 level = old_root->level;
Jan Schmidta95236d2012-06-05 16:41:24 +02001353 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001354 logical = eb_root->start;
Qu Wenruo581c1762018-03-29 09:08:11 +08001355 level = btrfs_header_level(eb_root);
Jan Schmidta95236d2012-06-05 16:41:24 +02001356 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001357
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001358 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001359 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001360 btrfs_tree_read_unlock(eb_root);
1361 free_extent_buffer(eb_root);
Qu Wenruo581c1762018-03-29 09:08:11 +08001362 old = read_tree_block(fs_info, logical, 0, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08001363 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1364 if (!IS_ERR(old))
1365 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001366 btrfs_warn(fs_info,
1367 "failed to read tree block %llu from get_old_root",
1368 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001369 } else {
Filipe Manana1d852d62021-04-20 10:55:44 +01001370 struct tree_mod_elem *tm2;
1371
Filipe Manana38ffe9e2021-03-11 14:31:05 +00001372 btrfs_tree_read_lock(old);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001373 eb = btrfs_clone_extent_buffer(old);
Filipe Manana1d852d62021-04-20 10:55:44 +01001374 /*
1375 * After the lookup for the most recent tree mod operation
1376 * above and before we locked and cloned the extent buffer
1377 * 'old', a new tree mod log operation may have been added.
1378 * So lookup for a more recent one to make sure the number
1379 * of mod log operations we replay is consistent with the
1380 * number of items we have in the cloned extent buffer,
1381 * otherwise we can hit a BUG_ON when rewinding the extent
1382 * buffer.
1383 */
1384 tm2 = tree_mod_log_search(fs_info, logical, time_seq);
Filipe Manana38ffe9e2021-03-11 14:31:05 +00001385 btrfs_tree_read_unlock(old);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001386 free_extent_buffer(old);
Filipe Manana1d852d62021-04-20 10:55:44 +01001387 ASSERT(tm2);
1388 ASSERT(tm2 == tm || tm2->seq > tm->seq);
1389 if (!tm2 || tm2->seq < tm->seq) {
1390 free_extent_buffer(eb);
1391 return NULL;
1392 }
1393 tm = tm2;
Jan Schmidt834328a2012-10-23 11:27:33 +02001394 }
1395 } else if (old_root) {
Filipe Mananaefad8a82019-08-12 19:14:29 +01001396 eb_root_owner = btrfs_header_owner(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001397 btrfs_tree_read_unlock(eb_root);
1398 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001399 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001400 } else {
David Sterba300aa892018-04-04 02:00:17 +02001401 btrfs_set_lock_blocking_read(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001402 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001403 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001404 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001405 }
1406
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001407 if (!eb)
1408 return NULL;
Jan Schmidta95236d2012-06-05 16:41:24 +02001409 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001410 btrfs_set_header_bytenr(eb, eb->start);
1411 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Filipe Mananaefad8a82019-08-12 19:14:29 +01001412 btrfs_set_header_owner(eb, eb_root_owner);
Jan Schmidta95236d2012-06-05 16:41:24 +02001413 btrfs_set_header_level(eb, old_root->level);
1414 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001415 }
Josef Bacikd3beaa22020-08-10 11:42:31 -04001416 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1417 btrfs_header_level(eb));
1418 btrfs_tree_read_lock(eb);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001419 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001420 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001421 else
1422 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001423 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001424
1425 return eb;
1426}
1427
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001428int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1429{
1430 struct tree_mod_elem *tm;
1431 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001432 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001433
David Sterbabcd24da2018-03-05 15:33:18 +01001434 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001435 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1436 level = tm->old_root.level;
1437 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001438 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001439 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001440 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001441
1442 return level;
1443}
1444
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001445static inline int should_cow_block(struct btrfs_trans_handle *trans,
1446 struct btrfs_root *root,
1447 struct extent_buffer *buf)
1448{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001449 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001450 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001451
David Sterbad1980132018-03-16 02:39:40 +01001452 /* Ensure we can see the FORCE_COW bit */
1453 smp_mb__before_atomic();
Liu Bof1ebcc72011-11-14 20:48:06 -05001454
1455 /*
1456 * We do not need to cow a block if
1457 * 1) this block is not created or changed in this transaction;
1458 * 2) this block does not belong to TREE_RELOC tree;
1459 * 3) the root is not forced COW.
1460 *
1461 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001462 * when we create snapshot during committing the transaction,
Andrea Gelmini52042d82018-11-28 12:05:13 +01001463 * after we've finished copying src root, we must COW the shared
Liu Bof1ebcc72011-11-14 20:48:06 -05001464 * block to ensure the metadata consistency.
1465 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001466 if (btrfs_header_generation(buf) == trans->transid &&
1467 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1468 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001469 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001470 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001471 return 0;
1472 return 1;
1473}
1474
Chris Masond352ac62008-09-29 15:18:18 -04001475/*
1476 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001477 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001478 * once per transaction, as long as it hasn't been written yet
1479 */
Chris Masond3977122009-01-05 21:25:51 -05001480noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001481 struct btrfs_root *root, struct extent_buffer *buf,
1482 struct extent_buffer *parent, int parent_slot,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001483 struct extent_buffer **cow_ret,
1484 enum btrfs_lock_nesting nest)
Chris Mason02217ed2007-03-02 16:08:05 -05001485{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001486 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001487 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001488 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001489
Josef Bacik83354f02018-11-30 11:52:13 -05001490 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1491 btrfs_err(fs_info,
1492 "COW'ing blocks on a fs root that's being dropped");
1493
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001494 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001495 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001496 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001498
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001499 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001500 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001501 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001502
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001503 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001504 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001505 *cow_ret = buf;
1506 return 0;
1507 }
Chris Masonc4876852009-02-04 09:24:25 -05001508
Byongho Leeee221842015-12-15 01:42:10 +09001509 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001510
1511 if (parent)
David Sterba8bead252018-04-04 02:03:48 +02001512 btrfs_set_lock_blocking_write(parent);
1513 btrfs_set_lock_blocking_write(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001514
Qu Wenruof616f5c2019-01-23 15:15:17 +08001515 /*
1516 * Before CoWing this block for later modification, check if it's
1517 * the subtree root and do the delayed subtree trace if needed.
1518 *
1519 * Also We don't care about the error, as it's handled internally.
1520 */
1521 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001522 ret = __btrfs_cow_block(trans, root, buf, parent,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001523 parent_slot, cow_ret, search_start, 0, nest);
liubo1abe9b82011-03-24 11:18:59 +00001524
1525 trace_btrfs_cow_block(root, buf, *cow_ret);
1526
Chris Masonf510cfe2007-10-15 16:14:48 -04001527 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001528}
1529
Chris Masond352ac62008-09-29 15:18:18 -04001530/*
1531 * helper function for defrag to decide if two blocks pointed to by a
1532 * node are actually close by
1533 */
Chris Mason6b800532007-10-15 16:17:34 -04001534static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001535{
Chris Mason6b800532007-10-15 16:17:34 -04001536 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001537 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001538 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001539 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001540 return 0;
1541}
1542
David Sterbace6ef5a2020-06-08 16:06:07 +02001543#ifdef __LITTLE_ENDIAN
1544
1545/*
1546 * Compare two keys, on little-endian the disk order is same as CPU order and
1547 * we can avoid the conversion.
1548 */
1549static int comp_keys(const struct btrfs_disk_key *disk_key,
1550 const struct btrfs_key *k2)
1551{
1552 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
1553
1554 return btrfs_comp_cpu_keys(k1, k2);
1555}
1556
1557#else
1558
Chris Mason081e9572007-11-06 10:26:24 -05001559/*
1560 * compare two keys in a memcmp fashion
1561 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001562static int comp_keys(const struct btrfs_disk_key *disk,
1563 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001564{
1565 struct btrfs_key k1;
1566
1567 btrfs_disk_key_to_cpu(&k1, disk);
1568
Diego Calleja20736ab2009-07-24 11:06:52 -04001569 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001570}
David Sterbace6ef5a2020-06-08 16:06:07 +02001571#endif
Chris Mason081e9572007-11-06 10:26:24 -05001572
Josef Bacikf3465ca2008-11-12 14:19:50 -05001573/*
1574 * same as comp_keys only with two btrfs_key's
1575 */
David Sterbae1f60a62019-10-01 19:57:39 +02001576int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001577{
1578 if (k1->objectid > k2->objectid)
1579 return 1;
1580 if (k1->objectid < k2->objectid)
1581 return -1;
1582 if (k1->type > k2->type)
1583 return 1;
1584 if (k1->type < k2->type)
1585 return -1;
1586 if (k1->offset > k2->offset)
1587 return 1;
1588 if (k1->offset < k2->offset)
1589 return -1;
1590 return 0;
1591}
Chris Mason081e9572007-11-06 10:26:24 -05001592
Chris Masond352ac62008-09-29 15:18:18 -04001593/*
1594 * this is used by the defrag code to go through all the
1595 * leaves pointed to by a node and reallocate them so that
1596 * disk order is close to key order
1597 */
Chris Mason6702ed42007-08-07 16:15:09 -04001598int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001599 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001600 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001601 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001602{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001603 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001604 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001605 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001606 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001607 u64 search_start = *last_ret;
1608 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001609 u64 other;
1610 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001611 int end_slot;
1612 int i;
1613 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001614 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001615 int uptodate;
1616 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001617 int progress_passed = 0;
1618 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001619
Chris Mason5708b952007-10-25 15:43:18 -04001620 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001621
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001622 WARN_ON(trans->transaction != fs_info->running_transaction);
1623 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001624
Chris Mason6b800532007-10-15 16:17:34 -04001625 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001626 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001627 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001628
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001629 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001630 return 0;
1631
David Sterba8bead252018-04-04 02:03:48 +02001632 btrfs_set_lock_blocking_write(parent);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001633
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001634 for (i = start_slot; i <= end_slot; i++) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001635 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001636 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001637
Chris Mason081e9572007-11-06 10:26:24 -05001638 btrfs_node_key(parent, &disk_key, i);
1639 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1640 continue;
1641
1642 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001643 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001644 gen = btrfs_node_ptr_generation(parent, i);
Qu Wenruo581c1762018-03-29 09:08:11 +08001645 btrfs_node_key_to_cpu(parent, &first_key, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001646 if (last_block == 0)
1647 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001648
Chris Mason6702ed42007-08-07 16:15:09 -04001649 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001650 other = btrfs_node_blockptr(parent, i - 1);
1651 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001652 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001653 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001654 other = btrfs_node_blockptr(parent, i + 1);
1655 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001656 }
Chris Masone9d0b132007-08-10 14:06:19 -04001657 if (close) {
1658 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001659 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001660 }
Chris Mason6702ed42007-08-07 16:15:09 -04001661
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001662 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001663 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001664 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001665 else
1666 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001667 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001668 if (!cur) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001669 cur = read_tree_block(fs_info, blocknr, gen,
1670 parent_level - 1,
1671 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08001672 if (IS_ERR(cur)) {
1673 return PTR_ERR(cur);
1674 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001675 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001676 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001677 }
Chris Mason6b800532007-10-15 16:17:34 -04001678 } else if (!uptodate) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001679 err = btrfs_read_buffer(cur, gen,
1680 parent_level - 1,&first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001681 if (err) {
1682 free_extent_buffer(cur);
1683 return err;
1684 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001685 }
Chris Mason6702ed42007-08-07 16:15:09 -04001686 }
Chris Masone9d0b132007-08-10 14:06:19 -04001687 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001688 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001689
Chris Masone7a84562008-06-25 16:01:31 -04001690 btrfs_tree_lock(cur);
David Sterba8bead252018-04-04 02:03:48 +02001691 btrfs_set_lock_blocking_write(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001692 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001693 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001694 min(16 * blocksize,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001695 (end_slot - i) * blocksize),
1696 BTRFS_NESTING_COW);
Yan252c38f2007-08-29 09:11:44 -04001697 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001698 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001699 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001700 break;
Yan252c38f2007-08-29 09:11:44 -04001701 }
Chris Masone7a84562008-06-25 16:01:31 -04001702 search_start = cur->start;
1703 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001704 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001705 btrfs_tree_unlock(cur);
1706 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001707 }
1708 return err;
1709}
1710
Chris Mason74123bd2007-02-02 11:05:29 -05001711/*
Chris Mason5f39d392007-10-15 16:14:19 -04001712 * search for key in the extent_buffer. The items start at offset p,
1713 * and they are item_size apart. There are 'max' items in p.
1714 *
Chris Mason74123bd2007-02-02 11:05:29 -05001715 * the slot in the array is returned via slot, and it points to
1716 * the place where you would insert key if it is not found in
1717 * the array.
1718 *
1719 * slot may point to max if the key is bigger than all of the keys
1720 */
Chris Masone02119d2008-09-05 16:13:11 -04001721static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001722 unsigned long p, int item_size,
1723 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001724 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001725{
1726 int low = 0;
1727 int high = max;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001728 int ret;
David Sterba5cd17f32020-04-29 23:23:37 +02001729 const int key_size = sizeof(struct btrfs_disk_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001730
Liu Bo5e24e9a2016-06-23 16:32:45 -07001731 if (low > high) {
1732 btrfs_err(eb->fs_info,
1733 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1734 __func__, low, high, eb->start,
1735 btrfs_header_owner(eb), btrfs_header_level(eb));
1736 return -EINVAL;
1737 }
1738
Chris Masond3977122009-01-05 21:25:51 -05001739 while (low < high) {
David Sterba5cd17f32020-04-29 23:23:37 +02001740 unsigned long oip;
1741 unsigned long offset;
1742 struct btrfs_disk_key *tmp;
1743 struct btrfs_disk_key unaligned;
1744 int mid;
1745
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001747 offset = p + mid * item_size;
David Sterba5cd17f32020-04-29 23:23:37 +02001748 oip = offset_in_page(offset);
Chris Mason5f39d392007-10-15 16:14:19 -04001749
David Sterba5cd17f32020-04-29 23:23:37 +02001750 if (oip + key_size <= PAGE_SIZE) {
1751 const unsigned long idx = offset >> PAGE_SHIFT;
1752 char *kaddr = page_address(eb->pages[idx]);
Chris Mason934d3752008-12-08 16:43:10 -05001753
David Sterba5cd17f32020-04-29 23:23:37 +02001754 tmp = (struct btrfs_disk_key *)(kaddr + oip);
Chris Mason5f39d392007-10-15 16:14:19 -04001755 } else {
David Sterba5cd17f32020-04-29 23:23:37 +02001756 read_extent_buffer(eb, &unaligned, offset, key_size);
1757 tmp = &unaligned;
Chris Mason5f39d392007-10-15 16:14:19 -04001758 }
David Sterba5cd17f32020-04-29 23:23:37 +02001759
Chris Masonbe0e5c02007-01-26 15:51:26 -05001760 ret = comp_keys(tmp, key);
1761
1762 if (ret < 0)
1763 low = mid + 1;
1764 else if (ret > 0)
1765 high = mid;
1766 else {
1767 *slot = mid;
1768 return 0;
1769 }
1770 }
1771 *slot = low;
1772 return 1;
1773}
1774
Chris Mason97571fd2007-02-24 13:39:08 -05001775/*
1776 * simple bin_search frontend that does the right thing for
1777 * leaves vs nodes
1778 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001779int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
Qu Wenruoe3b83362020-04-17 15:08:21 +08001780 int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001781{
Qu Wenruoe3b83362020-04-17 15:08:21 +08001782 if (btrfs_header_level(eb) == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001783 return generic_bin_search(eb,
1784 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001785 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001786 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001787 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001788 else
Chris Mason5f39d392007-10-15 16:14:19 -04001789 return generic_bin_search(eb,
1790 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001791 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001792 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001793 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001794}
1795
Yan, Zhengf0486c62010-05-16 10:46:25 -04001796static void root_add_used(struct btrfs_root *root, u32 size)
1797{
1798 spin_lock(&root->accounting_lock);
1799 btrfs_set_root_used(&root->root_item,
1800 btrfs_root_used(&root->root_item) + size);
1801 spin_unlock(&root->accounting_lock);
1802}
1803
1804static void root_sub_used(struct btrfs_root *root, u32 size)
1805{
1806 spin_lock(&root->accounting_lock);
1807 btrfs_set_root_used(&root->root_item,
1808 btrfs_root_used(&root->root_item) - size);
1809 spin_unlock(&root->accounting_lock);
1810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/* given a node and slot number, this reads the blocks it points to. The
1813 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001814 */
David Sterba4b231ae2019-08-21 19:16:27 +02001815struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1816 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001817{
Chris Masonca7a79a2008-05-12 12:59:19 -04001818 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001819 struct extent_buffer *eb;
Qu Wenruo581c1762018-03-29 09:08:11 +08001820 struct btrfs_key first_key;
Josef Bacik416bc652013-04-23 14:17:42 -04001821
Liu Bofb770ae2016-07-05 12:10:14 -07001822 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1823 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001824
1825 BUG_ON(level == 0);
1826
Qu Wenruo581c1762018-03-29 09:08:11 +08001827 btrfs_node_key_to_cpu(parent, &first_key, slot);
David Sterbad0d20b02019-03-20 14:54:01 +01001828 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
Qu Wenruo581c1762018-03-29 09:08:11 +08001829 btrfs_node_ptr_generation(parent, slot),
1830 level - 1, &first_key);
Liu Bofb770ae2016-07-05 12:10:14 -07001831 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1832 free_extent_buffer(eb);
1833 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001834 }
1835
1836 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001837}
1838
Chris Masond352ac62008-09-29 15:18:18 -04001839/*
1840 * node level balancing, used to make sure nodes are in proper order for
1841 * item deletion. We balance from the top down, so we have to make sure
1842 * that a deletion won't leave an node completely empty later on.
1843 */
Chris Masone02119d2008-09-05 16:13:11 -04001844static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001845 struct btrfs_root *root,
1846 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001847{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001849 struct extent_buffer *right = NULL;
1850 struct extent_buffer *mid;
1851 struct extent_buffer *left = NULL;
1852 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001853 int ret = 0;
1854 int wret;
1855 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001856 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001857 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001858
Liu Bo98e6b1e2018-09-12 06:06:23 +08001859 ASSERT(level > 0);
Chris Masonbb803952007-03-01 12:04:21 -05001860
Chris Mason5f39d392007-10-15 16:14:19 -04001861 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001862
Chris Masonbd681512011-07-16 15:23:14 -04001863 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1864 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001865 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1866
Chris Mason1d4f8a02007-03-13 09:28:32 -04001867 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001868
Li Zefana05a9bb2011-09-06 16:55:34 +08001869 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001870 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001871 pslot = path->slots[level + 1];
1872 }
Chris Masonbb803952007-03-01 12:04:21 -05001873
Chris Mason40689472007-03-17 14:29:23 -04001874 /*
1875 * deal with the case where there is only one pointer in the root
1876 * by promoting the node below to a root
1877 */
Chris Mason5f39d392007-10-15 16:14:19 -04001878 if (!parent) {
1879 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001880
Chris Mason5f39d392007-10-15 16:14:19 -04001881 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001882 return 0;
1883
1884 /* promote the child to a root */
David Sterba4b231ae2019-08-21 19:16:27 +02001885 child = btrfs_read_node_slot(mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001886 if (IS_ERR(child)) {
1887 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001889 goto enospc;
1890 }
1891
Chris Mason925baed2008-06-25 16:01:30 -04001892 btrfs_tree_lock(child);
David Sterba8bead252018-04-04 02:03:48 +02001893 btrfs_set_lock_blocking_write(child);
Josef Bacik9631e4c2020-08-20 11:46:03 -04001894 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
1895 BTRFS_NESTING_COW);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001896 if (ret) {
1897 btrfs_tree_unlock(child);
1898 free_extent_buffer(child);
1899 goto enospc;
1900 }
Yan2f375ab2008-02-01 14:58:07 -05001901
David Sterbad9d19a02018-03-05 16:35:29 +01001902 ret = tree_mod_log_insert_root(root->node, child, 1);
1903 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001904 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001905
Chris Mason0b86a832008-03-24 15:01:56 -04001906 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001907 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001908
Chris Mason925baed2008-06-25 16:01:30 -04001909 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001910 path->nodes[level] = NULL;
David Sterba6a884d7d2019-03-20 14:30:02 +01001911 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001912 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001913 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001914 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001915
1916 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001917 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001918 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001919 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001920 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001921 }
Chris Mason5f39d392007-10-15 16:14:19 -04001922 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001923 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001924 return 0;
1925
David Sterba4b231ae2019-08-21 19:16:27 +02001926 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001927 if (IS_ERR(left))
1928 left = NULL;
1929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 if (left) {
Josef Bacikbf774672020-08-20 11:46:04 -04001931 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
David Sterba8bead252018-04-04 02:03:48 +02001932 btrfs_set_lock_blocking_write(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001933 wret = btrfs_cow_block(trans, root, left,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001934 parent, pslot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001935 BTRFS_NESTING_LEFT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04001936 if (wret) {
1937 ret = wret;
1938 goto enospc;
1939 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001940 }
Liu Bofb770ae2016-07-05 12:10:14 -07001941
David Sterba4b231ae2019-08-21 19:16:27 +02001942 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001943 if (IS_ERR(right))
1944 right = NULL;
1945
Chris Mason5f39d392007-10-15 16:14:19 -04001946 if (right) {
Josef Bacikbf774672020-08-20 11:46:04 -04001947 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
David Sterba8bead252018-04-04 02:03:48 +02001948 btrfs_set_lock_blocking_write(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001949 wret = btrfs_cow_block(trans, root, right,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001950 parent, pslot + 1, &right,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001951 BTRFS_NESTING_RIGHT_COW);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001952 if (wret) {
1953 ret = wret;
1954 goto enospc;
1955 }
1956 }
1957
1958 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001959 if (left) {
1960 orig_slot += btrfs_header_nritems(left);
David Sterbad30a6682019-03-20 14:16:45 +01001961 wret = push_node_left(trans, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001962 if (wret < 0)
1963 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001964 }
Chris Mason79f95c82007-03-01 15:16:26 -05001965
1966 /*
1967 * then try to empty the right most buffer into the middle
1968 */
Chris Mason5f39d392007-10-15 16:14:19 -04001969 if (right) {
David Sterbad30a6682019-03-20 14:16:45 +01001970 wret = push_node_left(trans, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001971 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001972 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001973 if (btrfs_header_nritems(right) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001974 btrfs_clean_tree_block(right);
Chris Mason925baed2008-06-25 16:01:30 -04001975 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001976 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001977 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001978 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001979 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001980 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001981 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001982 struct btrfs_disk_key right_key;
1983 btrfs_node_key(right, &right_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001984 ret = tree_mod_log_insert_key(parent, pslot + 1,
1985 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1986 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001987 btrfs_set_node_key(parent, &right_key, pslot + 1);
1988 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001989 }
1990 }
Chris Mason5f39d392007-10-15 16:14:19 -04001991 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001992 /*
1993 * we're not allowed to leave a node with one item in the
1994 * tree during a delete. A deletion from lower in the tree
1995 * could try to delete the only pointer in this node.
1996 * So, pull some keys from the left.
1997 * There has to be a left pointer at this point because
1998 * otherwise we would have pulled some pointers from the
1999 * right
2000 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002001 if (!left) {
2002 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002003 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07002004 goto enospc;
2005 }
David Sterba55d32ed2019-03-20 14:18:06 +01002006 wret = balance_node_right(trans, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002007 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002008 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002009 goto enospc;
2010 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002011 if (wret == 1) {
David Sterbad30a6682019-03-20 14:16:45 +01002012 wret = push_node_left(trans, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04002013 if (wret < 0)
2014 ret = wret;
2015 }
Chris Mason79f95c82007-03-01 15:16:26 -05002016 BUG_ON(wret == 1);
2017 }
Chris Mason5f39d392007-10-15 16:14:19 -04002018 if (btrfs_header_nritems(mid) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01002019 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002020 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002021 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002022 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002023 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002024 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002025 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002026 } else {
2027 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002028 struct btrfs_disk_key mid_key;
2029 btrfs_node_key(mid, &mid_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002030 ret = tree_mod_log_insert_key(parent, pslot,
2031 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2032 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002033 btrfs_set_node_key(parent, &mid_key, pslot);
2034 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002035 }
Chris Masonbb803952007-03-01 12:04:21 -05002036
Chris Mason79f95c82007-03-01 15:16:26 -05002037 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002038 if (left) {
2039 if (btrfs_header_nritems(left) > orig_slot) {
David Sterba67439da2019-10-08 13:28:47 +02002040 atomic_inc(&left->refs);
Chris Mason925baed2008-06-25 16:01:30 -04002041 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002042 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002043 path->slots[level + 1] -= 1;
2044 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002045 if (mid) {
2046 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002047 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002048 }
Chris Masonbb803952007-03-01 12:04:21 -05002049 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002050 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002051 path->slots[level] = orig_slot;
2052 }
2053 }
Chris Mason79f95c82007-03-01 15:16:26 -05002054 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002055 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002056 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002057 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002058enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002059 if (right) {
2060 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002062 }
2063 if (left) {
2064 if (path->nodes[level] != left)
2065 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002067 }
Chris Masonbb803952007-03-01 12:04:21 -05002068 return ret;
2069}
2070
Chris Masond352ac62008-09-29 15:18:18 -04002071/* Node balancing for insertion. Here we only split or push nodes around
2072 * when they are completely full. This is also done top down, so we
2073 * have to be pessimistic.
2074 */
Chris Masond3977122009-01-05 21:25:51 -05002075static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002076 struct btrfs_root *root,
2077 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002078{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002079 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002080 struct extent_buffer *right = NULL;
2081 struct extent_buffer *mid;
2082 struct extent_buffer *left = NULL;
2083 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002084 int ret = 0;
2085 int wret;
2086 int pslot;
2087 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002088
2089 if (level == 0)
2090 return 1;
2091
Chris Mason5f39d392007-10-15 16:14:19 -04002092 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002093 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002094
Li Zefana05a9bb2011-09-06 16:55:34 +08002095 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002096 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002097 pslot = path->slots[level + 1];
2098 }
Chris Masone66f7092007-04-20 13:16:02 -04002099
Chris Mason5f39d392007-10-15 16:14:19 -04002100 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002101 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002102
David Sterba4b231ae2019-08-21 19:16:27 +02002103 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002104 if (IS_ERR(left))
2105 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002106
2107 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002108 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002109 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002110
Josef Bacikbf774672020-08-20 11:46:04 -04002111 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
David Sterba8bead252018-04-04 02:03:48 +02002112 btrfs_set_lock_blocking_write(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002113
Chris Mason5f39d392007-10-15 16:14:19 -04002114 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002115 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002116 wret = 1;
2117 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002118 ret = btrfs_cow_block(trans, root, left, parent,
Josef Bacik9631e4c2020-08-20 11:46:03 -04002119 pslot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04002120 BTRFS_NESTING_LEFT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04002121 if (ret)
2122 wret = 1;
2123 else {
David Sterbad30a6682019-03-20 14:16:45 +01002124 wret = push_node_left(trans, left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002125 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002126 }
Chris Masone66f7092007-04-20 13:16:02 -04002127 if (wret < 0)
2128 ret = wret;
2129 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002130 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002131 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002132 btrfs_node_key(mid, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002133 ret = tree_mod_log_insert_key(parent, pslot,
2134 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2135 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002136 btrfs_set_node_key(parent, &disk_key, pslot);
2137 btrfs_mark_buffer_dirty(parent);
2138 if (btrfs_header_nritems(left) > orig_slot) {
2139 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002140 path->slots[level + 1] -= 1;
2141 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002142 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002144 } else {
2145 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002146 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002147 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002148 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002150 }
Chris Masone66f7092007-04-20 13:16:02 -04002151 return 0;
2152 }
Chris Mason925baed2008-06-25 16:01:30 -04002153 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002154 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002155 }
David Sterba4b231ae2019-08-21 19:16:27 +02002156 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002157 if (IS_ERR(right))
2158 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002159
2160 /*
2161 * then try to empty the right most buffer into the middle
2162 */
Chris Mason5f39d392007-10-15 16:14:19 -04002163 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002164 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002165
Josef Bacikbf774672020-08-20 11:46:04 -04002166 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
David Sterba8bead252018-04-04 02:03:48 +02002167 btrfs_set_lock_blocking_write(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002168
Chris Mason5f39d392007-10-15 16:14:19 -04002169 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002170 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002171 wret = 1;
2172 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002173 ret = btrfs_cow_block(trans, root, right,
2174 parent, pslot + 1,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04002175 &right, BTRFS_NESTING_RIGHT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04002176 if (ret)
2177 wret = 1;
2178 else {
David Sterba55d32ed2019-03-20 14:18:06 +01002179 wret = balance_node_right(trans, right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002180 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002181 }
Chris Masone66f7092007-04-20 13:16:02 -04002182 if (wret < 0)
2183 ret = wret;
2184 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002185 struct btrfs_disk_key disk_key;
2186
2187 btrfs_node_key(right, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002188 ret = tree_mod_log_insert_key(parent, pslot + 1,
2189 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2190 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002191 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2192 btrfs_mark_buffer_dirty(parent);
2193
2194 if (btrfs_header_nritems(mid) <= orig_slot) {
2195 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002196 path->slots[level + 1] += 1;
2197 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002198 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002199 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002200 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002201 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002202 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002203 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002204 }
Chris Masone66f7092007-04-20 13:16:02 -04002205 return 0;
2206 }
Chris Mason925baed2008-06-25 16:01:30 -04002207 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002208 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002209 }
Chris Masone66f7092007-04-20 13:16:02 -04002210 return 1;
2211}
2212
Chris Mason74123bd2007-02-02 11:05:29 -05002213/*
Chris Masond352ac62008-09-29 15:18:18 -04002214 * readahead one full node of leaves, finding things that are close
2215 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002216 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002217static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002218 struct btrfs_path *path,
2219 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002220{
Chris Mason5f39d392007-10-15 16:14:19 -04002221 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002222 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002223 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002224 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002225 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002226 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002227 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002228 u32 nr;
2229 u32 blocksize;
2230 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002231
Chris Masona6b6e752007-10-15 16:22:39 -04002232 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002233 return;
2234
Chris Mason6702ed42007-08-07 16:15:09 -04002235 if (!path->nodes[level])
2236 return;
2237
Chris Mason5f39d392007-10-15 16:14:19 -04002238 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002239
Chris Mason3c69fae2007-08-07 15:52:22 -04002240 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002241 blocksize = fs_info->nodesize;
2242 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002243 if (eb) {
2244 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002245 return;
2246 }
2247
Chris Masona7175312009-01-22 09:23:10 -05002248 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002249
Chris Mason5f39d392007-10-15 16:14:19 -04002250 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002251 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002252
Chris Masond3977122009-01-05 21:25:51 -05002253 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002254 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002255 if (nr == 0)
2256 break;
2257 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002258 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002259 nr++;
2260 if (nr >= nritems)
2261 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002262 }
David Sterbae4058b52015-11-27 16:31:35 +01002263 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002264 btrfs_node_key(node, &disk_key, nr);
2265 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2266 break;
2267 }
Chris Mason6b800532007-10-15 16:17:34 -04002268 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002269 if ((search <= target && target - search <= 65536) ||
2270 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002271 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002272 nread += blocksize;
2273 }
2274 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002275 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002276 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002277 }
2278}
Chris Mason925baed2008-06-25 16:01:30 -04002279
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002280static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002281 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002282{
2283 int slot;
2284 int nritems;
2285 struct extent_buffer *parent;
2286 struct extent_buffer *eb;
2287 u64 gen;
2288 u64 block1 = 0;
2289 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002290
Chris Mason8c594ea2009-04-20 15:50:10 -04002291 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002292 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002293 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002294
2295 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002296 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002297
2298 if (slot > 0) {
2299 block1 = btrfs_node_blockptr(parent, slot - 1);
2300 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002301 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002302 /*
2303 * if we get -eagain from btrfs_buffer_uptodate, we
2304 * don't want to return eagain here. That will loop
2305 * forever
2306 */
2307 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002308 block1 = 0;
2309 free_extent_buffer(eb);
2310 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002311 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002312 block2 = btrfs_node_blockptr(parent, slot + 1);
2313 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002314 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002315 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002316 block2 = 0;
2317 free_extent_buffer(eb);
2318 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002319
Josef Bacik0b088512013-06-17 14:23:02 -04002320 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002321 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002322 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002323 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002324}
2325
2326
2327/*
Chris Masond3977122009-01-05 21:25:51 -05002328 * when we walk down the tree, it is usually safe to unlock the higher layers
2329 * in the tree. The exceptions are when our path goes through slot 0, because
2330 * operations on the tree might require changing key pointers higher up in the
2331 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002332 *
Chris Masond3977122009-01-05 21:25:51 -05002333 * callers might also have set path->keep_locks, which tells this code to keep
2334 * the lock if the path points to the last slot in the block. This is part of
2335 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002336 *
Chris Masond3977122009-01-05 21:25:51 -05002337 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2338 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002339 */
Chris Masone02119d2008-09-05 16:13:11 -04002340static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002341 int lowest_unlock, int min_write_lock_level,
2342 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002343{
2344 int i;
2345 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002346 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002347 struct extent_buffer *t;
2348
2349 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2350 if (!path->nodes[i])
2351 break;
2352 if (!path->locks[i])
2353 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002354 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002355 skip_level = i + 1;
2356 continue;
2357 }
Chris Mason051e1b92008-06-25 16:01:30 -04002358 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002359 u32 nritems;
2360 t = path->nodes[i];
2361 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002362 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002363 skip_level = i + 1;
2364 continue;
2365 }
2366 }
Chris Mason051e1b92008-06-25 16:01:30 -04002367 if (skip_level < i && i >= lowest_unlock)
2368 no_skips = 1;
2369
Chris Mason925baed2008-06-25 16:01:30 -04002370 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08002371 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04002372 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002373 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002374 if (write_lock_level &&
2375 i > min_write_lock_level &&
2376 i <= *write_lock_level) {
2377 *write_lock_level = i - 1;
2378 }
Chris Mason925baed2008-06-25 16:01:30 -04002379 }
2380 }
2381}
2382
Chris Mason3c69fae2007-08-07 15:52:22 -04002383/*
Chris Masonc8c42862009-04-03 10:14:18 -04002384 * helper function for btrfs_search_slot. The goal is to find a block
2385 * in cache without setting the path to blocking. If we find the block
2386 * we return zero and the path is unchanged.
2387 *
2388 * If we can't find the block, we set the path blocking and do some
2389 * reada. -EAGAIN is returned and the search must be repeated.
2390 */
2391static int
Liu Bod07b8522017-01-30 12:23:42 -08002392read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2393 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002394 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002397 u64 blocknr;
2398 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002399 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08002400 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04002401 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08002402 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04002403
Nikolay Borisov213ff4b2020-05-27 13:10:59 +03002404 blocknr = btrfs_node_blockptr(*eb_ret, slot);
2405 gen = btrfs_node_ptr_generation(*eb_ret, slot);
2406 parent_level = btrfs_header_level(*eb_ret);
2407 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002408
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002409 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002410 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002411 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002412 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Qu Wenruo448de472019-03-12 17:10:40 +08002413 /*
2414 * Do extra check for first_key, eb can be stale due to
2415 * being cached, read from scrub, or have multiple
2416 * parents (shared tree blocks).
2417 */
David Sterbae064d5e2019-03-20 14:58:13 +01002418 if (btrfs_verify_level_key(tmp,
Qu Wenruo448de472019-03-12 17:10:40 +08002419 parent_level - 1, &first_key, gen)) {
2420 free_extent_buffer(tmp);
2421 return -EUCLEAN;
2422 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002423 *eb_ret = tmp;
2424 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002425 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002426
2427 /* the pages were up to date, but we failed
2428 * the generation number check. Do a full
2429 * read for the generation number that is correct.
2430 * We must do this without dropping locks so
2431 * we can trust our generation number
2432 */
2433 btrfs_set_path_blocking(p);
2434
2435 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08002436 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04002437 if (!ret) {
2438 *eb_ret = tmp;
2439 return 0;
2440 }
2441 free_extent_buffer(tmp);
2442 btrfs_release_path(p);
2443 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002444 }
2445
2446 /*
2447 * reduce lock contention at high levels
2448 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002449 * we read. Don't release the lock on the current
2450 * level because we need to walk this node to figure
2451 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002452 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002453 btrfs_unlock_up_safe(p, level + 1);
2454 btrfs_set_path_blocking(p);
2455
David Sterbae4058b52015-11-27 16:31:35 +01002456 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002457 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002458
Chris Mason76a05b32009-05-14 13:24:30 -04002459 ret = -EAGAIN;
Liu Bo02a33072018-05-16 01:37:36 +08002460 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
Qu Wenruo581c1762018-03-29 09:08:11 +08002461 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002462 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002463 /*
2464 * If the read above didn't mark this buffer up to date,
2465 * it will never end up being up to date. Set ret to EIO now
2466 * and give up so that our caller doesn't loop forever
2467 * on our EAGAINs.
2468 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08002469 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04002470 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002471 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002472 } else {
2473 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002474 }
Liu Bo02a33072018-05-16 01:37:36 +08002475
2476 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002477 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002478}
2479
2480/*
2481 * helper function for btrfs_search_slot. This does all of the checks
2482 * for node-level blocks and does any balancing required based on
2483 * the ins_len.
2484 *
2485 * If no extra work was required, zero is returned. If we had to
2486 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2487 * start over
2488 */
2489static int
2490setup_nodes_for_search(struct btrfs_trans_handle *trans,
2491 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002492 struct extent_buffer *b, int level, int ins_len,
2493 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002494{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002495 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002496 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002497
Chris Masonc8c42862009-04-03 10:14:18 -04002498 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002499 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002500 int sret;
2501
Chris Masonbd681512011-07-16 15:23:14 -04002502 if (*write_lock_level < level + 1) {
2503 *write_lock_level = level + 1;
2504 btrfs_release_path(p);
2505 goto again;
2506 }
2507
Chris Masonc8c42862009-04-03 10:14:18 -04002508 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002509 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002510 sret = split_node(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002511
2512 BUG_ON(sret > 0);
2513 if (sret) {
2514 ret = sret;
2515 goto done;
2516 }
2517 b = p->nodes[level];
2518 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002519 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002520 int sret;
2521
Chris Masonbd681512011-07-16 15:23:14 -04002522 if (*write_lock_level < level + 1) {
2523 *write_lock_level = level + 1;
2524 btrfs_release_path(p);
2525 goto again;
2526 }
2527
Chris Masonc8c42862009-04-03 10:14:18 -04002528 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002529 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002530 sret = balance_level(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002531
2532 if (sret) {
2533 ret = sret;
2534 goto done;
2535 }
2536 b = p->nodes[level];
2537 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002538 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002539 goto again;
2540 }
2541 BUG_ON(btrfs_header_nritems(b) == 1);
2542 }
2543 return 0;
2544
2545again:
2546 ret = -EAGAIN;
2547done:
2548 return ret;
2549}
2550
David Sterba381cf652015-01-02 18:45:16 +01002551int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002552 u64 iobjectid, u64 ioff, u8 key_type,
2553 struct btrfs_key *found_key)
2554{
2555 int ret;
2556 struct btrfs_key key;
2557 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002558
2559 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002560 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002561
2562 key.type = key_type;
2563 key.objectid = iobjectid;
2564 key.offset = ioff;
2565
2566 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002567 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002568 return ret;
2569
2570 eb = path->nodes[0];
2571 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2572 ret = btrfs_next_leaf(fs_root, path);
2573 if (ret)
2574 return ret;
2575 eb = path->nodes[0];
2576 }
2577
2578 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2579 if (found_key->type != key.type ||
2580 found_key->objectid != key.objectid)
2581 return 1;
2582
2583 return 0;
2584}
2585
Liu Bo1fc28d82018-05-18 11:00:21 +08002586static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2587 struct btrfs_path *p,
2588 int write_lock_level)
2589{
2590 struct btrfs_fs_info *fs_info = root->fs_info;
2591 struct extent_buffer *b;
2592 int root_lock;
2593 int level = 0;
2594
2595 /* We try very hard to do read locks on the root */
2596 root_lock = BTRFS_READ_LOCK;
2597
2598 if (p->search_commit_root) {
Filipe Mananabe6821f2018-12-11 10:19:45 +00002599 /*
2600 * The commit roots are read only so we always do read locks,
2601 * and we always must hold the commit_root_sem when doing
2602 * searches on them, the only exception is send where we don't
2603 * want to block transaction commits for a long time, so
2604 * we need to clone the commit root in order to avoid races
2605 * with transaction commits that create a snapshot of one of
2606 * the roots used by a send operation.
2607 */
2608 if (p->need_commit_sem) {
Liu Bo1fc28d82018-05-18 11:00:21 +08002609 down_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002610 b = btrfs_clone_extent_buffer(root->commit_root);
Liu Bo1fc28d82018-05-18 11:00:21 +08002611 up_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002612 if (!b)
2613 return ERR_PTR(-ENOMEM);
2614
2615 } else {
2616 b = root->commit_root;
David Sterba67439da2019-10-08 13:28:47 +02002617 atomic_inc(&b->refs);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002618 }
2619 level = btrfs_header_level(b);
Liu Bof9ddfd02018-05-29 21:27:06 +08002620 /*
2621 * Ensure that all callers have set skip_locking when
2622 * p->search_commit_root = 1.
2623 */
2624 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08002625
2626 goto out;
2627 }
2628
2629 if (p->skip_locking) {
2630 b = btrfs_root_node(root);
2631 level = btrfs_header_level(b);
2632 goto out;
2633 }
2634
2635 /*
Liu Bo662c6532018-05-18 11:00:23 +08002636 * If the level is set to maximum, we can skip trying to get the read
2637 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08002638 */
Liu Bo662c6532018-05-18 11:00:23 +08002639 if (write_lock_level < BTRFS_MAX_LEVEL) {
2640 /*
2641 * We don't know the level of the root node until we actually
2642 * have it read locked
2643 */
Josef Bacik51899412020-08-20 11:46:01 -04002644 b = __btrfs_read_lock_root_node(root, p->recurse);
Liu Bo662c6532018-05-18 11:00:23 +08002645 level = btrfs_header_level(b);
2646 if (level > write_lock_level)
2647 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08002648
Liu Bo662c6532018-05-18 11:00:23 +08002649 /* Whoops, must trade for write lock */
2650 btrfs_tree_read_unlock(b);
2651 free_extent_buffer(b);
2652 }
2653
Liu Bo1fc28d82018-05-18 11:00:21 +08002654 b = btrfs_lock_root_node(root);
2655 root_lock = BTRFS_WRITE_LOCK;
2656
2657 /* The level might have changed, check again */
2658 level = btrfs_header_level(b);
2659
2660out:
2661 p->nodes[level] = b;
2662 if (!p->skip_locking)
2663 p->locks[level] = root_lock;
2664 /*
2665 * Callers are responsible for dropping b's references.
2666 */
2667 return b;
2668}
2669
2670
Chris Masonc8c42862009-04-03 10:14:18 -04002671/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002672 * btrfs_search_slot - look for a key in a tree and perform necessary
2673 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002674 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002675 * @trans: Handle of transaction, used when modifying the tree
2676 * @p: Holds all btree nodes along the search path
2677 * @root: The root node of the tree
2678 * @key: The key we are looking for
2679 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2680 * deletions it's -1. 0 for plain searches
2681 * @cow: boolean should CoW operations be performed. Must always be 1
2682 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002683 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002684 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2685 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2686 *
2687 * If @key is found, 0 is returned and you can find the item in the leaf level
2688 * of the path (level 0)
2689 *
2690 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2691 * points to the slot where it should be inserted
2692 *
2693 * If an error is encountered while searching the tree a negative error number
2694 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002695 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002696int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2697 const struct btrfs_key *key, struct btrfs_path *p,
2698 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699{
Chris Mason5f39d392007-10-15 16:14:19 -04002700 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002701 int slot;
2702 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002703 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002705 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002706 /* everything at write_lock_level or lower must be write locked */
2707 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002708 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002709 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002710 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002711
Chris Mason6702ed42007-08-07 16:15:09 -04002712 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002713 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002714 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002715 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002716
Chris Masonbd681512011-07-16 15:23:14 -04002717 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002718 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002719
Chris Masonbd681512011-07-16 15:23:14 -04002720 /* when we are removing items, we might have to go up to level
2721 * two as we update tree pointers Make sure we keep write
2722 * for those levels as well
2723 */
2724 write_lock_level = 2;
2725 } else if (ins_len > 0) {
2726 /*
2727 * for inserting items, make sure we have a write lock on
2728 * level 1 so we can update keys
2729 */
2730 write_lock_level = 1;
2731 }
2732
2733 if (!cow)
2734 write_lock_level = -1;
2735
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002736 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002737 write_lock_level = BTRFS_MAX_LEVEL;
2738
Chris Masonf7c79f32012-03-19 15:54:38 -04002739 min_write_lock_level = write_lock_level;
2740
Chris Masonbb803952007-03-01 12:04:21 -05002741again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002742 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08002743 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002744 if (IS_ERR(b)) {
2745 ret = PTR_ERR(b);
2746 goto done;
2747 }
Chris Mason925baed2008-06-25 16:01:30 -04002748
Chris Masoneb60cea2007-02-02 09:18:22 -05002749 while (b) {
Qu Wenruof624d972019-09-10 15:40:17 +08002750 int dec = 0;
2751
Chris Mason5f39d392007-10-15 16:14:19 -04002752 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002753
Chris Mason02217ed2007-03-02 16:08:05 -05002754 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002755 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2756
Chris Masonc8c42862009-04-03 10:14:18 -04002757 /*
2758 * if we don't really need to cow this block
2759 * then we don't want to set the path blocking,
2760 * so we test it here
2761 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002762 if (!should_cow_block(trans, root, b)) {
2763 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002764 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002765 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002766
Chris Masonbd681512011-07-16 15:23:14 -04002767 /*
2768 * must have write locks on this node and the
2769 * parent
2770 */
Josef Bacik5124e002012-11-07 13:44:13 -05002771 if (level > write_lock_level ||
2772 (level + 1 > write_lock_level &&
2773 level + 1 < BTRFS_MAX_LEVEL &&
2774 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002775 write_lock_level = level + 1;
2776 btrfs_release_path(p);
2777 goto again;
2778 }
2779
Filipe Manana160f4082014-07-28 19:37:17 +01002780 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002781 if (last_level)
2782 err = btrfs_cow_block(trans, root, b, NULL, 0,
Josef Bacik9631e4c2020-08-20 11:46:03 -04002783 &b,
2784 BTRFS_NESTING_COW);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002785 else
2786 err = btrfs_cow_block(trans, root, b,
2787 p->nodes[level + 1],
Josef Bacik9631e4c2020-08-20 11:46:03 -04002788 p->slots[level + 1], &b,
2789 BTRFS_NESTING_COW);
Yan Zheng33c66f42009-07-22 09:59:00 -04002790 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002791 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002792 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002793 }
Chris Mason02217ed2007-03-02 16:08:05 -05002794 }
Chris Mason65b51a02008-08-01 15:11:20 -04002795cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002796 p->nodes[level] = b;
Liu Bo52398342018-08-22 05:54:37 +08002797 /*
2798 * Leave path with blocking locks to avoid massive
2799 * lock context switch, this is made on purpose.
2800 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002801
2802 /*
2803 * we have a lock on b and as long as we aren't changing
2804 * the tree, there is no way to for the items in b to change.
2805 * It is safe to drop the lock on our parent before we
2806 * go through the expensive btree search on b.
2807 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002808 * If we're inserting or deleting (ins_len != 0), then we might
2809 * be changing slot zero, which may require changing the parent.
2810 * So, we can't drop the lock until after we know which slot
2811 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002812 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002813 if (!ins_len && !p->keep_locks) {
2814 int u = level + 1;
2815
2816 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2817 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2818 p->locks[u] = 0;
2819 }
2820 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002821
Nikolay Borisov995e9a12020-05-27 13:10:53 +03002822 /*
2823 * If btrfs_bin_search returns an exact match (prev_cmp == 0)
2824 * we can safely assume the target key will always be in slot 0
2825 * on lower levels due to the invariants BTRFS' btree provides,
2826 * namely that a btrfs_key_ptr entry always points to the
2827 * lowest key in the child node, thus we can skip searching
2828 * lower levels
2829 */
2830 if (prev_cmp == 0) {
2831 slot = 0;
2832 ret = 0;
2833 } else {
2834 ret = btrfs_bin_search(b, key, &slot);
2835 prev_cmp = ret;
2836 if (ret < 0)
2837 goto done;
2838 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002839
Qu Wenruof624d972019-09-10 15:40:17 +08002840 if (level == 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002841 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002842 if (ins_len > 0 &&
David Sterbae902baa2019-03-20 14:36:46 +01002843 btrfs_leaf_free_space(b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002844 if (write_lock_level < 1) {
2845 write_lock_level = 1;
2846 btrfs_release_path(p);
2847 goto again;
2848 }
2849
Chris Masonb4ce94d2009-02-04 09:25:08 -05002850 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002851 err = split_leaf(trans, root, key,
2852 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002853
Yan Zheng33c66f42009-07-22 09:59:00 -04002854 BUG_ON(err > 0);
2855 if (err) {
2856 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002857 goto done;
2858 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002859 }
Chris Mason459931e2008-12-10 09:10:46 -05002860 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002861 unlock_up(p, level, lowest_unlock,
Liu Bo4b6f8e92018-08-14 10:46:53 +08002862 min_write_lock_level, NULL);
Chris Mason65b51a02008-08-01 15:11:20 -04002863 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002864 }
Qu Wenruof624d972019-09-10 15:40:17 +08002865 if (ret && slot > 0) {
2866 dec = 1;
2867 slot--;
2868 }
2869 p->slots[level] = slot;
2870 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2871 &write_lock_level);
2872 if (err == -EAGAIN)
2873 goto again;
2874 if (err) {
2875 ret = err;
2876 goto done;
2877 }
2878 b = p->nodes[level];
2879 slot = p->slots[level];
2880
2881 /*
2882 * Slot 0 is special, if we change the key we have to update
2883 * the parent pointer which means we must have a write lock on
2884 * the parent
2885 */
2886 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2887 write_lock_level = level + 1;
2888 btrfs_release_path(p);
2889 goto again;
2890 }
2891
2892 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2893 &write_lock_level);
2894
2895 if (level == lowest_level) {
2896 if (dec)
2897 p->slots[level]++;
2898 goto done;
2899 }
2900
2901 err = read_block_for_search(root, p, &b, level, slot, key);
2902 if (err == -EAGAIN)
2903 goto again;
2904 if (err) {
2905 ret = err;
2906 goto done;
2907 }
2908
2909 if (!p->skip_locking) {
2910 level = btrfs_header_level(b);
2911 if (level <= write_lock_level) {
2912 if (!btrfs_try_tree_write_lock(b)) {
2913 btrfs_set_path_blocking(p);
2914 btrfs_tree_lock(b);
2915 }
2916 p->locks[level] = BTRFS_WRITE_LOCK;
2917 } else {
2918 if (!btrfs_tree_read_lock_atomic(b)) {
2919 btrfs_set_path_blocking(p);
Josef Bacikfd7ba1c2020-08-20 11:46:02 -04002920 __btrfs_tree_read_lock(b, BTRFS_NESTING_NORMAL,
2921 p->recurse);
Qu Wenruof624d972019-09-10 15:40:17 +08002922 }
2923 p->locks[level] = BTRFS_READ_LOCK;
2924 }
2925 p->nodes[level] = b;
2926 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002927 }
Chris Mason65b51a02008-08-01 15:11:20 -04002928 ret = 1;
2929done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002930 /*
2931 * we don't really know what they plan on doing with the path
2932 * from here on, so for now just mark it as blocking
2933 */
Chris Masonb9473432009-03-13 11:00:37 -04002934 if (!p->leave_spinning)
2935 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002936 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002937 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002938 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002939}
2940
Chris Mason74123bd2007-02-02 11:05:29 -05002941/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002942 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2943 * current state of the tree together with the operations recorded in the tree
2944 * modification log to search for the key in a previous version of this tree, as
2945 * denoted by the time_seq parameter.
2946 *
2947 * Naturally, there is no support for insert, delete or cow operations.
2948 *
2949 * The resulting path and return value will be set up as if we called
2950 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2951 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002952int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002953 struct btrfs_path *p, u64 time_seq)
2954{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002955 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002956 struct extent_buffer *b;
2957 int slot;
2958 int ret;
2959 int err;
2960 int level;
2961 int lowest_unlock = 1;
2962 u8 lowest_level = 0;
2963
2964 lowest_level = p->lowest_level;
2965 WARN_ON(p->nodes[0] != NULL);
2966
2967 if (p->search_commit_root) {
2968 BUG_ON(time_seq);
2969 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2970 }
2971
2972again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002973 b = get_old_root(root, time_seq);
Nikolay Borisov315bed42018-09-13 11:35:10 +03002974 if (!b) {
2975 ret = -EIO;
2976 goto done;
2977 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002978 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002979 p->locks[level] = BTRFS_READ_LOCK;
2980
2981 while (b) {
Qu Wenruoabe93392019-09-10 15:40:18 +08002982 int dec = 0;
2983
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002984 level = btrfs_header_level(b);
2985 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002986
2987 /*
2988 * we have a lock on b and as long as we aren't changing
2989 * the tree, there is no way to for the items in b to change.
2990 * It is safe to drop the lock on our parent before we
2991 * go through the expensive btree search on b.
2992 */
2993 btrfs_unlock_up_safe(p, level + 1);
2994
Nikolay Borisov995e9a12020-05-27 13:10:53 +03002995 ret = btrfs_bin_search(b, key, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00002996 if (ret < 0)
2997 goto done;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002998
Qu Wenruoabe93392019-09-10 15:40:18 +08002999 if (level == 0) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003000 p->slots[level] = slot;
3001 unlock_up(p, level, lowest_unlock, 0, NULL);
3002 goto done;
3003 }
Qu Wenruoabe93392019-09-10 15:40:18 +08003004
3005 if (ret && slot > 0) {
3006 dec = 1;
3007 slot--;
3008 }
3009 p->slots[level] = slot;
3010 unlock_up(p, level, lowest_unlock, 0, NULL);
3011
3012 if (level == lowest_level) {
3013 if (dec)
3014 p->slots[level]++;
3015 goto done;
3016 }
3017
3018 err = read_block_for_search(root, p, &b, level, slot, key);
3019 if (err == -EAGAIN)
3020 goto again;
3021 if (err) {
3022 ret = err;
3023 goto done;
3024 }
3025
3026 level = btrfs_header_level(b);
3027 if (!btrfs_tree_read_lock_atomic(b)) {
3028 btrfs_set_path_blocking(p);
3029 btrfs_tree_read_lock(b);
3030 }
3031 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3032 if (!b) {
3033 ret = -ENOMEM;
3034 goto done;
3035 }
3036 p->locks[level] = BTRFS_READ_LOCK;
3037 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003038 }
3039 ret = 1;
3040done:
3041 if (!p->leave_spinning)
3042 btrfs_set_path_blocking(p);
3043 if (ret < 0)
3044 btrfs_release_path(p);
3045
3046 return ret;
3047}
3048
3049/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003050 * helper to use instead of search slot if no exact match is needed but
3051 * instead the next or previous item should be returned.
3052 * When find_higher is true, the next higher item is returned, the next lower
3053 * otherwise.
3054 * When return_any and find_higher are both true, and no higher item is found,
3055 * return the next lower instead.
3056 * When return_any is true and find_higher is false, and no lower item is found,
3057 * return the next higher instead.
3058 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3059 * < 0 on error
3060 */
3061int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003062 const struct btrfs_key *key,
3063 struct btrfs_path *p, int find_higher,
3064 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003065{
3066 int ret;
3067 struct extent_buffer *leaf;
3068
3069again:
3070 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3071 if (ret <= 0)
3072 return ret;
3073 /*
3074 * a return value of 1 means the path is at the position where the
3075 * item should be inserted. Normally this is the next bigger item,
3076 * but in case the previous item is the last in a leaf, path points
3077 * to the first free slot in the previous leaf, i.e. at an invalid
3078 * item.
3079 */
3080 leaf = p->nodes[0];
3081
3082 if (find_higher) {
3083 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3084 ret = btrfs_next_leaf(root, p);
3085 if (ret <= 0)
3086 return ret;
3087 if (!return_any)
3088 return 1;
3089 /*
3090 * no higher item found, return the next
3091 * lower instead
3092 */
3093 return_any = 0;
3094 find_higher = 0;
3095 btrfs_release_path(p);
3096 goto again;
3097 }
3098 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003099 if (p->slots[0] == 0) {
3100 ret = btrfs_prev_leaf(root, p);
3101 if (ret < 0)
3102 return ret;
3103 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003104 leaf = p->nodes[0];
3105 if (p->slots[0] == btrfs_header_nritems(leaf))
3106 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003107 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003108 }
Arne Jansene6793762011-09-13 11:18:10 +02003109 if (!return_any)
3110 return 1;
3111 /*
3112 * no lower item found, return the next
3113 * higher instead
3114 */
3115 return_any = 0;
3116 find_higher = 1;
3117 btrfs_release_path(p);
3118 goto again;
3119 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003120 --p->slots[0];
3121 }
3122 }
3123 return 0;
3124}
3125
3126/*
Chris Mason74123bd2007-02-02 11:05:29 -05003127 * adjust the pointers going up the tree, starting at level
3128 * making sure the right key of each node is points to 'key'.
3129 * This is used after shifting pointers to the left, so it stops
3130 * fixing up pointers when a given leaf/node is not in slot 0 of the
3131 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003132 *
Chris Mason74123bd2007-02-02 11:05:29 -05003133 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003134static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003135 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003136{
3137 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003138 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01003139 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003140
Chris Mason234b63a2007-03-13 10:46:10 -04003141 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003142 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003143
Chris Masoneb60cea2007-02-02 09:18:22 -05003144 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003145 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003146 t = path->nodes[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003147 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3148 GFP_ATOMIC);
3149 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003150 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003151 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003152 if (tslot != 0)
3153 break;
3154 }
3155}
3156
Chris Mason74123bd2007-02-02 11:05:29 -05003157/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003158 * update item key.
3159 *
3160 * This function isn't completely safe. It's the caller's responsibility
3161 * that the new key won't break the order
3162 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003163void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3164 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003165 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003166{
3167 struct btrfs_disk_key disk_key;
3168 struct extent_buffer *eb;
3169 int slot;
3170
3171 eb = path->nodes[0];
3172 slot = path->slots[0];
3173 if (slot > 0) {
3174 btrfs_item_key(eb, &disk_key, slot - 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003175 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3176 btrfs_crit(fs_info,
3177 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3178 slot, btrfs_disk_key_objectid(&disk_key),
3179 btrfs_disk_key_type(&disk_key),
3180 btrfs_disk_key_offset(&disk_key),
3181 new_key->objectid, new_key->type,
3182 new_key->offset);
3183 btrfs_print_leaf(eb);
3184 BUG();
3185 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003186 }
3187 if (slot < btrfs_header_nritems(eb) - 1) {
3188 btrfs_item_key(eb, &disk_key, slot + 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003189 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3190 btrfs_crit(fs_info,
3191 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3192 slot, btrfs_disk_key_objectid(&disk_key),
3193 btrfs_disk_key_type(&disk_key),
3194 btrfs_disk_key_offset(&disk_key),
3195 new_key->objectid, new_key->type,
3196 new_key->offset);
3197 btrfs_print_leaf(eb);
3198 BUG();
3199 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003200 }
3201
3202 btrfs_cpu_key_to_disk(&disk_key, new_key);
3203 btrfs_set_item_key(eb, &disk_key, slot);
3204 btrfs_mark_buffer_dirty(eb);
3205 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003206 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003207}
3208
3209/*
Qu Wenruod16c7022020-08-19 14:35:50 +08003210 * Check key order of two sibling extent buffers.
3211 *
3212 * Return true if something is wrong.
3213 * Return false if everything is fine.
3214 *
3215 * Tree-checker only works inside one tree block, thus the following
3216 * corruption can not be detected by tree-checker:
3217 *
3218 * Leaf @left | Leaf @right
3219 * --------------------------------------------------------------
3220 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
3221 *
3222 * Key f6 in leaf @left itself is valid, but not valid when the next
3223 * key in leaf @right is 7.
3224 * This can only be checked at tree block merge time.
3225 * And since tree checker has ensured all key order in each tree block
3226 * is correct, we only need to bother the last key of @left and the first
3227 * key of @right.
3228 */
3229static bool check_sibling_keys(struct extent_buffer *left,
3230 struct extent_buffer *right)
3231{
3232 struct btrfs_key left_last;
3233 struct btrfs_key right_first;
3234 int level = btrfs_header_level(left);
3235 int nr_left = btrfs_header_nritems(left);
3236 int nr_right = btrfs_header_nritems(right);
3237
3238 /* No key to check in one of the tree blocks */
3239 if (!nr_left || !nr_right)
3240 return false;
3241
3242 if (level) {
3243 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
3244 btrfs_node_key_to_cpu(right, &right_first, 0);
3245 } else {
3246 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
3247 btrfs_item_key_to_cpu(right, &right_first, 0);
3248 }
3249
3250 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
3251 btrfs_crit(left->fs_info,
3252"bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
3253 left_last.objectid, left_last.type,
3254 left_last.offset, right_first.objectid,
3255 right_first.type, right_first.offset);
3256 return true;
3257 }
3258 return false;
3259}
3260
3261/*
Chris Mason74123bd2007-02-02 11:05:29 -05003262 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003263 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003264 *
3265 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3266 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003267 */
Chris Mason98ed5172008-01-03 10:01:48 -05003268static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003269 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003270 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003271{
David Sterbad30a6682019-03-20 14:16:45 +01003272 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003273 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003274 int src_nritems;
3275 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003276 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003277
Chris Mason5f39d392007-10-15 16:14:19 -04003278 src_nritems = btrfs_header_nritems(src);
3279 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003280 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003281 WARN_ON(btrfs_header_generation(src) != trans->transid);
3282 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003283
Chris Masonbce4eae2008-04-24 14:42:46 -04003284 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003285 return 1;
3286
Chris Masond3977122009-01-05 21:25:51 -05003287 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003288 return 1;
3289
Chris Masonbce4eae2008-04-24 14:42:46 -04003290 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003291 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003292 if (push_items < src_nritems) {
3293 /* leave at least 8 pointers in the node if
3294 * we aren't going to empty it
3295 */
3296 if (src_nritems - push_items < 8) {
3297 if (push_items <= 8)
3298 return 1;
3299 push_items -= 8;
3300 }
3301 }
3302 } else
3303 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003304
Qu Wenruod16c7022020-08-19 14:35:50 +08003305 /* dst is the left eb, src is the middle eb */
3306 if (check_sibling_keys(dst, src)) {
3307 ret = -EUCLEAN;
3308 btrfs_abort_transaction(trans, ret);
3309 return ret;
3310 }
David Sterbaed874f02019-03-20 14:22:04 +01003311 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003312 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003313 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003314 return ret;
3315 }
Chris Mason5f39d392007-10-15 16:14:19 -04003316 copy_extent_buffer(dst, src,
3317 btrfs_node_key_ptr_offset(dst_nritems),
3318 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003319 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003320
Chris Masonbb803952007-03-01 12:04:21 -05003321 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003322 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003323 * Don't call tree_mod_log_insert_move here, key removal was
3324 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003325 */
Chris Mason5f39d392007-10-15 16:14:19 -04003326 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3327 btrfs_node_key_ptr_offset(push_items),
3328 (src_nritems - push_items) *
3329 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003330 }
Chris Mason5f39d392007-10-15 16:14:19 -04003331 btrfs_set_header_nritems(src, src_nritems - push_items);
3332 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3333 btrfs_mark_buffer_dirty(src);
3334 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003335
Chris Masonbb803952007-03-01 12:04:21 -05003336 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003337}
3338
Chris Mason97571fd2007-02-24 13:39:08 -05003339/*
Chris Mason79f95c82007-03-01 15:16:26 -05003340 * try to push data from one node into the next node right in the
3341 * tree.
3342 *
3343 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3344 * error, and > 0 if there was no room in the right hand block.
3345 *
3346 * this will only push up to 1/2 the contents of the left node over
3347 */
Chris Mason5f39d392007-10-15 16:14:19 -04003348static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct extent_buffer *dst,
3350 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003351{
David Sterba55d32ed2019-03-20 14:18:06 +01003352 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason79f95c82007-03-01 15:16:26 -05003353 int push_items = 0;
3354 int max_push;
3355 int src_nritems;
3356 int dst_nritems;
3357 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003358
Chris Mason7bb86312007-12-11 09:25:06 -05003359 WARN_ON(btrfs_header_generation(src) != trans->transid);
3360 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3361
Chris Mason5f39d392007-10-15 16:14:19 -04003362 src_nritems = btrfs_header_nritems(src);
3363 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003364 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003365 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003366 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003367
Chris Masond3977122009-01-05 21:25:51 -05003368 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003369 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003370
3371 max_push = src_nritems / 2 + 1;
3372 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003373 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003374 return 1;
Yan252c38f2007-08-29 09:11:44 -04003375
Chris Mason79f95c82007-03-01 15:16:26 -05003376 if (max_push < push_items)
3377 push_items = max_push;
3378
Qu Wenruod16c7022020-08-19 14:35:50 +08003379 /* dst is the right eb, src is the middle eb */
3380 if (check_sibling_keys(src, dst)) {
3381 ret = -EUCLEAN;
3382 btrfs_abort_transaction(trans, ret);
3383 return ret;
3384 }
David Sterbabf1d3422018-03-05 15:47:39 +01003385 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3386 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003387 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3388 btrfs_node_key_ptr_offset(0),
3389 (dst_nritems) *
3390 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003391
David Sterbaed874f02019-03-20 14:22:04 +01003392 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3393 push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003394 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003395 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003396 return ret;
3397 }
Chris Mason5f39d392007-10-15 16:14:19 -04003398 copy_extent_buffer(dst, src,
3399 btrfs_node_key_ptr_offset(0),
3400 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003401 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003402
Chris Mason5f39d392007-10-15 16:14:19 -04003403 btrfs_set_header_nritems(src, src_nritems - push_items);
3404 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003405
Chris Mason5f39d392007-10-15 16:14:19 -04003406 btrfs_mark_buffer_dirty(src);
3407 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003408
Chris Mason79f95c82007-03-01 15:16:26 -05003409 return ret;
3410}
3411
3412/*
Chris Mason97571fd2007-02-24 13:39:08 -05003413 * helper function to insert a new root level in the tree.
3414 * A new node is allocated, and a single item is inserted to
3415 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003416 *
3417 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003418 */
Chris Masond3977122009-01-05 21:25:51 -05003419static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003420 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003421 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003422{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003423 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003424 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003425 struct extent_buffer *lower;
3426 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003427 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003428 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01003429 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003430
3431 BUG_ON(path->nodes[level]);
3432 BUG_ON(path->nodes[level-1] != root->node);
3433
Chris Mason7bb86312007-12-11 09:25:06 -05003434 lower = path->nodes[level-1];
3435 if (level == 1)
3436 btrfs_item_key(lower, &lower_key, 0);
3437 else
3438 btrfs_node_key(lower, &lower_key, 0);
3439
Filipe Mananaa6279472019-01-25 11:48:51 +00003440 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
Josef Bacik9631e4c2020-08-20 11:46:03 -04003441 root->node->start, 0,
Josef Bacikcf6f34a2020-08-20 11:46:07 -04003442 BTRFS_NESTING_NEW_ROOT);
Chris Mason5f39d392007-10-15 16:14:19 -04003443 if (IS_ERR(c))
3444 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003445
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003446 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003447
Chris Mason5f39d392007-10-15 16:14:19 -04003448 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003449 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003450 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003451 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003452 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003453
3454 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003455
3456 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003457
Chris Mason925baed2008-06-25 16:01:30 -04003458 old = root->node;
David Sterbad9d19a02018-03-05 16:35:29 +01003459 ret = tree_mod_log_insert_root(root->node, c, 0);
3460 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003461 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003462
3463 /* the super has an extra ref to root->node */
3464 free_extent_buffer(old);
3465
Chris Mason0b86a832008-03-24 15:01:56 -04003466 add_root_to_dirty_list(root);
David Sterba67439da2019-10-08 13:28:47 +02003467 atomic_inc(&c->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04003468 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303469 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003470 path->slots[level] = 0;
3471 return 0;
3472}
3473
Chris Mason74123bd2007-02-02 11:05:29 -05003474/*
3475 * worker function to insert a single pointer in a node.
3476 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003477 *
Chris Mason74123bd2007-02-02 11:05:29 -05003478 * slot and level indicate where you want the key to go, and
3479 * blocknr is the block the key points to.
3480 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003481static void insert_ptr(struct btrfs_trans_handle *trans,
David Sterba6ad3cf62019-03-20 14:32:45 +01003482 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003483 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003484 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003485{
Chris Mason5f39d392007-10-15 16:14:19 -04003486 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003487 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003488 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003489
3490 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003491 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003492 lower = path->nodes[level];
3493 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003494 BUG_ON(slot > nritems);
David Sterba6ad3cf62019-03-20 14:32:45 +01003495 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003496 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003497 if (level) {
3498 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003499 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003500 BUG_ON(ret < 0);
3501 }
Chris Mason5f39d392007-10-15 16:14:19 -04003502 memmove_extent_buffer(lower,
3503 btrfs_node_key_ptr_offset(slot + 1),
3504 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003505 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003506 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003507 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003508 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3509 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003510 BUG_ON(ret < 0);
3511 }
Chris Mason5f39d392007-10-15 16:14:19 -04003512 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003513 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003514 WARN_ON(trans->transid == 0);
3515 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003516 btrfs_set_header_nritems(lower, nritems + 1);
3517 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003518}
3519
Chris Mason97571fd2007-02-24 13:39:08 -05003520/*
3521 * split the node at the specified level in path in two.
3522 * The path is corrected to point to the appropriate node after the split
3523 *
3524 * Before splitting this tries to make some room in the node by pushing
3525 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003526 *
3527 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003528 */
Chris Masone02119d2008-09-05 16:13:11 -04003529static noinline int split_node(struct btrfs_trans_handle *trans,
3530 struct btrfs_root *root,
3531 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003532{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003533 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003534 struct extent_buffer *c;
3535 struct extent_buffer *split;
3536 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003537 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003538 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003539 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003540
Chris Mason5f39d392007-10-15 16:14:19 -04003541 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003542 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003543 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003544 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003545 * trying to split the root, lets make a new one
3546 *
Liu Bofdd99c72013-05-22 12:06:51 +00003547 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003548 * insert_new_root, because that root buffer will be kept as a
3549 * normal node. We are going to log removal of half of the
3550 * elements below with tree_mod_log_eb_copy. We're holding a
3551 * tree lock on the buffer, which is why we cannot race with
3552 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003553 */
Liu Bofdd99c72013-05-22 12:06:51 +00003554 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003555 if (ret)
3556 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003557 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003558 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003559 c = path->nodes[level];
3560 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003561 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003562 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003563 if (ret < 0)
3564 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003565 }
Chris Masone66f7092007-04-20 13:16:02 -04003566
Chris Mason5f39d392007-10-15 16:14:19 -04003567 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003568 mid = (c_nritems + 1) / 2;
3569 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003570
Filipe Mananaa6279472019-01-25 11:48:51 +00003571 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
Josef Bacik4dff97e2020-08-20 11:46:06 -04003572 c->start, 0, BTRFS_NESTING_SPLIT);
Chris Mason5f39d392007-10-15 16:14:19 -04003573 if (IS_ERR(split))
3574 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003575
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003576 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03003577 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04003578
David Sterbaed874f02019-03-20 14:22:04 +01003579 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003580 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003581 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003582 return ret;
3583 }
Chris Mason5f39d392007-10-15 16:14:19 -04003584 copy_extent_buffer(split, c,
3585 btrfs_node_key_ptr_offset(0),
3586 btrfs_node_key_ptr_offset(mid),
3587 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3588 btrfs_set_header_nritems(split, c_nritems - mid);
3589 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003590 ret = 0;
3591
Chris Mason5f39d392007-10-15 16:14:19 -04003592 btrfs_mark_buffer_dirty(c);
3593 btrfs_mark_buffer_dirty(split);
3594
David Sterba6ad3cf62019-03-20 14:32:45 +01003595 insert_ptr(trans, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003596 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003597
Chris Mason5de08d72007-02-24 06:24:44 -05003598 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003599 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003600 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003601 free_extent_buffer(c);
3602 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003603 path->slots[level + 1] += 1;
3604 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003605 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003606 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003607 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003608 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003609}
3610
Chris Mason74123bd2007-02-02 11:05:29 -05003611/*
3612 * how many bytes are required to store the items in a leaf. start
3613 * and nr indicate which items in the leaf to check. This totals up the
3614 * space used both by the item structs and the item data
3615 */
Chris Mason5f39d392007-10-15 16:14:19 -04003616static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003617{
Josef Bacik41be1f32012-10-15 13:43:18 -04003618 struct btrfs_item *start_item;
3619 struct btrfs_item *end_item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003620 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003621 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003622 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003623
3624 if (!nr)
3625 return 0;
Ross Kirkdd3cc162013-09-16 15:58:09 +01003626 start_item = btrfs_item_nr(start);
3627 end_item = btrfs_item_nr(end);
David Sterbaa31356b2020-04-29 22:56:01 +02003628 data_len = btrfs_item_offset(l, start_item) +
3629 btrfs_item_size(l, start_item);
3630 data_len = data_len - btrfs_item_offset(l, end_item);
Chris Mason0783fcf2007-03-12 20:12:07 -04003631 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003632 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003633 return data_len;
3634}
3635
Chris Mason74123bd2007-02-02 11:05:29 -05003636/*
Chris Masond4dbff92007-04-04 14:08:15 -04003637 * The space between the end of the leaf items and
3638 * the start of the leaf data. IOW, how much room
3639 * the leaf has left for both items and data
3640 */
David Sterbae902baa2019-03-20 14:36:46 +01003641noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003642{
David Sterbae902baa2019-03-20 14:36:46 +01003643 struct btrfs_fs_info *fs_info = leaf->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003644 int nritems = btrfs_header_nritems(leaf);
3645 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003646
3647 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003648 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003649 btrfs_crit(fs_info,
3650 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3651 ret,
3652 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3653 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003654 }
3655 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003656}
3657
Chris Mason99d8f832010-07-07 10:51:48 -04003658/*
3659 * min slot controls the lowest index we're willing to push to the
3660 * right. We'll push up to and including min_slot, but no lower
3661 */
David Sterbaf72f0012019-03-20 14:39:45 +01003662static noinline int __push_leaf_right(struct btrfs_path *path,
Chris Mason44871b12009-03-13 10:04:31 -04003663 int data_size, int empty,
3664 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003665 int free_space, u32 left_nritems,
3666 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003667{
David Sterbaf72f0012019-03-20 14:39:45 +01003668 struct btrfs_fs_info *fs_info = right->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003669 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003670 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003671 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003672 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003673 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003674 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003675 int push_space = 0;
3676 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003677 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003678 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003679 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003680 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003681 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003682
Chris Mason34a38212007-11-07 13:31:03 -05003683 if (empty)
3684 nr = 0;
3685 else
Chris Mason99d8f832010-07-07 10:51:48 -04003686 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003687
Zheng Yan31840ae2008-09-23 13:14:14 -04003688 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003689 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003690
Chris Mason44871b12009-03-13 10:04:31 -04003691 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003692 i = left_nritems - 1;
3693 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003694 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003695
Zheng Yan31840ae2008-09-23 13:14:14 -04003696 if (!empty && push_items > 0) {
3697 if (path->slots[0] > i)
3698 break;
3699 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003700 int space = btrfs_leaf_free_space(left);
3701
Zheng Yan31840ae2008-09-23 13:14:14 -04003702 if (space + push_space * 2 > free_space)
3703 break;
3704 }
3705 }
3706
Chris Mason00ec4c52007-02-24 12:47:20 -05003707 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003708 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003709
Chris Masondb945352007-10-15 16:15:53 -04003710 this_item_size = btrfs_item_size(left, item);
3711 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003712 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003713
Chris Mason00ec4c52007-02-24 12:47:20 -05003714 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003715 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003716 if (i == 0)
3717 break;
3718 i--;
Chris Masondb945352007-10-15 16:15:53 -04003719 }
Chris Mason5f39d392007-10-15 16:14:19 -04003720
Chris Mason925baed2008-06-25 16:01:30 -04003721 if (push_items == 0)
3722 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003723
Julia Lawall6c1500f2012-11-03 20:30:18 +00003724 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003725
Chris Mason00ec4c52007-02-24 12:47:20 -05003726 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003727 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003728
Chris Mason5f39d392007-10-15 16:14:19 -04003729 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
David Sterba8f881e82019-03-20 11:33:10 +01003730 push_space -= leaf_data_end(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003731
Chris Mason00ec4c52007-02-24 12:47:20 -05003732 /* make room in the right data area */
David Sterba8f881e82019-03-20 11:33:10 +01003733 data_end = leaf_data_end(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003734 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003735 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3736 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003737 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003738
Chris Mason00ec4c52007-02-24 12:47:20 -05003739 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003740 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003741 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
David Sterba8f881e82019-03-20 11:33:10 +01003742 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
Chris Masond6025572007-03-30 14:27:56 -04003743 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003744
3745 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3746 btrfs_item_nr_offset(0),
3747 right_nritems * sizeof(struct btrfs_item));
3748
Chris Mason00ec4c52007-02-24 12:47:20 -05003749 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003750 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3751 btrfs_item_nr_offset(left_nritems - push_items),
3752 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003753
3754 /* update the item pointers */
David Sterbac82f8232019-08-09 17:48:21 +02003755 btrfs_init_map_token(&token, right);
Chris Mason7518a232007-03-12 12:01:18 -04003756 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003757 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003758 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003759 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003760 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02003761 push_space -= btrfs_token_item_size(&token, item);
3762 btrfs_set_token_item_offset(&token, item, push_space);
Chris Masondb945352007-10-15 16:15:53 -04003763 }
3764
Chris Mason7518a232007-03-12 12:01:18 -04003765 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003766 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003767
Chris Mason34a38212007-11-07 13:31:03 -05003768 if (left_nritems)
3769 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003770 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003771 btrfs_clean_tree_block(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003772
Chris Mason5f39d392007-10-15 16:14:19 -04003773 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003774
Chris Mason5f39d392007-10-15 16:14:19 -04003775 btrfs_item_key(right, &disk_key, 0);
3776 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003777 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003778
Chris Mason00ec4c52007-02-24 12:47:20 -05003779 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003780 if (path->slots[0] >= left_nritems) {
3781 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003782 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba6a884d7d2019-03-20 14:30:02 +01003783 btrfs_clean_tree_block(path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003784 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003785 free_extent_buffer(path->nodes[0]);
3786 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003787 path->slots[1] += 1;
3788 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003789 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003790 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003791 }
3792 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003793
3794out_unlock:
3795 btrfs_tree_unlock(right);
3796 free_extent_buffer(right);
3797 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003798}
Chris Mason925baed2008-06-25 16:01:30 -04003799
Chris Mason00ec4c52007-02-24 12:47:20 -05003800/*
Chris Mason44871b12009-03-13 10:04:31 -04003801 * push some data in the path leaf to the right, trying to free up at
3802 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3803 *
3804 * returns 1 if the push failed because the other node didn't have enough
3805 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003806 *
3807 * this will push starting from min_slot to the end of the leaf. It won't
3808 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003809 */
3810static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003811 *root, struct btrfs_path *path,
3812 int min_data_size, int data_size,
3813 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003814{
3815 struct extent_buffer *left = path->nodes[0];
3816 struct extent_buffer *right;
3817 struct extent_buffer *upper;
3818 int slot;
3819 int free_space;
3820 u32 left_nritems;
3821 int ret;
3822
3823 if (!path->nodes[1])
3824 return 1;
3825
3826 slot = path->slots[1];
3827 upper = path->nodes[1];
3828 if (slot >= btrfs_header_nritems(upper) - 1)
3829 return 1;
3830
3831 btrfs_assert_tree_locked(path->nodes[1]);
3832
David Sterba4b231ae2019-08-21 19:16:27 +02003833 right = btrfs_read_node_slot(upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003834 /*
3835 * slot + 1 is not valid or we fail to read the right node,
3836 * no big deal, just return.
3837 */
3838 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003839 return 1;
3840
Josef Bacikbf774672020-08-20 11:46:04 -04003841 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
David Sterba8bead252018-04-04 02:03:48 +02003842 btrfs_set_lock_blocking_write(right);
Chris Mason44871b12009-03-13 10:04:31 -04003843
David Sterbae902baa2019-03-20 14:36:46 +01003844 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003845 if (free_space < data_size)
3846 goto out_unlock;
3847
3848 /* cow and double check */
3849 ret = btrfs_cow_block(trans, root, right, upper,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04003850 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
Chris Mason44871b12009-03-13 10:04:31 -04003851 if (ret)
3852 goto out_unlock;
3853
David Sterbae902baa2019-03-20 14:36:46 +01003854 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003855 if (free_space < data_size)
3856 goto out_unlock;
3857
3858 left_nritems = btrfs_header_nritems(left);
3859 if (left_nritems == 0)
3860 goto out_unlock;
3861
Qu Wenruod16c7022020-08-19 14:35:50 +08003862 if (check_sibling_keys(left, right)) {
3863 ret = -EUCLEAN;
3864 btrfs_tree_unlock(right);
3865 free_extent_buffer(right);
3866 return ret;
3867 }
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003868 if (path->slots[0] == left_nritems && !empty) {
3869 /* Key greater than all keys in the leaf, right neighbor has
3870 * enough room for it and we're not emptying our leaf to delete
3871 * it, therefore use right neighbor to insert the new item and
Andrea Gelmini52042d82018-11-28 12:05:13 +01003872 * no need to touch/dirty our left leaf. */
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003873 btrfs_tree_unlock(left);
3874 free_extent_buffer(left);
3875 path->nodes[0] = right;
3876 path->slots[0] = 0;
3877 path->slots[1]++;
3878 return 0;
3879 }
3880
David Sterbaf72f0012019-03-20 14:39:45 +01003881 return __push_leaf_right(path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003882 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003883out_unlock:
3884 btrfs_tree_unlock(right);
3885 free_extent_buffer(right);
3886 return 1;
3887}
3888
3889/*
Chris Mason74123bd2007-02-02 11:05:29 -05003890 * push some data in the path leaf to the left, trying to free up at
3891 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003892 *
3893 * max_slot can put a limit on how far into the leaf we'll push items. The
3894 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3895 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003896 */
David Sterba8087c192019-03-20 14:40:41 +01003897static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003898 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003899 int free_space, u32 right_nritems,
3900 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003901{
David Sterba8087c192019-03-20 14:40:41 +01003902 struct btrfs_fs_info *fs_info = left->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003903 struct btrfs_disk_key disk_key;
3904 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003905 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003906 int push_space = 0;
3907 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003908 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003909 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003910 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003911 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003912 u32 this_item_size;
3913 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003914 struct btrfs_map_token token;
3915
Chris Mason34a38212007-11-07 13:31:03 -05003916 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003917 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003918 else
Chris Mason99d8f832010-07-07 10:51:48 -04003919 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003920
3921 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003922 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003923
Zheng Yan31840ae2008-09-23 13:14:14 -04003924 if (!empty && push_items > 0) {
3925 if (path->slots[0] < i)
3926 break;
3927 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003928 int space = btrfs_leaf_free_space(right);
3929
Zheng Yan31840ae2008-09-23 13:14:14 -04003930 if (space + push_space * 2 > free_space)
3931 break;
3932 }
3933 }
3934
Chris Masonbe0e5c02007-01-26 15:51:26 -05003935 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003936 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003937
3938 this_item_size = btrfs_item_size(right, item);
3939 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003940 break;
Chris Masondb945352007-10-15 16:15:53 -04003941
Chris Masonbe0e5c02007-01-26 15:51:26 -05003942 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003943 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003944 }
Chris Masondb945352007-10-15 16:15:53 -04003945
Chris Masonbe0e5c02007-01-26 15:51:26 -05003946 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003947 ret = 1;
3948 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003949 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303950 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003951
Chris Masonbe0e5c02007-01-26 15:51:26 -05003952 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003953 copy_extent_buffer(left, right,
3954 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3955 btrfs_item_nr_offset(0),
3956 push_items * sizeof(struct btrfs_item));
3957
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003958 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003959 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003960
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003961 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003962 leaf_data_end(left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003963 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003964 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003965 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003966 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003967 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003968
David Sterbac82f8232019-08-09 17:48:21 +02003969 btrfs_init_map_token(&token, left);
Chris Masondb945352007-10-15 16:15:53 -04003970 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003971 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003972 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003973
Ross Kirkdd3cc162013-09-16 15:58:09 +01003974 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003975
David Sterbacc4c13d2020-04-29 02:15:56 +02003976 ioff = btrfs_token_item_offset(&token, item);
3977 btrfs_set_token_item_offset(&token, item,
3978 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003979 }
Chris Mason5f39d392007-10-15 16:14:19 -04003980 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003981
3982 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003983 if (push_items > right_nritems)
3984 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003985 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003986
Chris Mason34a38212007-11-07 13:31:03 -05003987 if (push_items < right_nritems) {
3988 push_space = btrfs_item_offset_nr(right, push_items - 1) -
David Sterba8f881e82019-03-20 11:33:10 +01003989 leaf_data_end(right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003990 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003991 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003992 BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003993 leaf_data_end(right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003994
3995 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003996 btrfs_item_nr_offset(push_items),
3997 (btrfs_header_nritems(right) - push_items) *
3998 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003999 }
David Sterbac82f8232019-08-09 17:48:21 +02004000
4001 btrfs_init_map_token(&token, right);
Yaneef1c492007-11-26 10:58:13 -05004002 right_nritems -= push_items;
4003 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004004 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04004005 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004006 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004007
David Sterbacc4c13d2020-04-29 02:15:56 +02004008 push_space = push_space - btrfs_token_item_size(&token, item);
4009 btrfs_set_token_item_offset(&token, item, push_space);
Chris Masondb945352007-10-15 16:15:53 -04004010 }
Chris Masoneb60cea2007-02-02 09:18:22 -05004011
Chris Mason5f39d392007-10-15 16:14:19 -04004012 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05004013 if (right_nritems)
4014 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004015 else
David Sterba6a884d7d2019-03-20 14:30:02 +01004016 btrfs_clean_tree_block(right);
Chris Mason098f59c2007-05-11 11:33:21 -04004017
Chris Mason5f39d392007-10-15 16:14:19 -04004018 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004019 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004020
4021 /* then fixup the leaf pointer in the path */
4022 if (path->slots[0] < push_items) {
4023 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04004024 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004025 free_extent_buffer(path->nodes[0]);
4026 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004027 path->slots[1] -= 1;
4028 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004029 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04004030 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004031 path->slots[0] -= push_items;
4032 }
Chris Masoneb60cea2007-02-02 09:18:22 -05004033 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004034 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04004035out:
4036 btrfs_tree_unlock(left);
4037 free_extent_buffer(left);
4038 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004039}
4040
Chris Mason74123bd2007-02-02 11:05:29 -05004041/*
Chris Mason44871b12009-03-13 10:04:31 -04004042 * push some data in the path leaf to the left, trying to free up at
4043 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04004044 *
4045 * max_slot can put a limit on how far into the leaf we'll push items. The
4046 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
4047 * items
Chris Mason44871b12009-03-13 10:04:31 -04004048 */
4049static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04004050 *root, struct btrfs_path *path, int min_data_size,
4051 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04004052{
4053 struct extent_buffer *right = path->nodes[0];
4054 struct extent_buffer *left;
4055 int slot;
4056 int free_space;
4057 u32 right_nritems;
4058 int ret = 0;
4059
4060 slot = path->slots[1];
4061 if (slot == 0)
4062 return 1;
4063 if (!path->nodes[1])
4064 return 1;
4065
4066 right_nritems = btrfs_header_nritems(right);
4067 if (right_nritems == 0)
4068 return 1;
4069
4070 btrfs_assert_tree_locked(path->nodes[1]);
4071
David Sterba4b231ae2019-08-21 19:16:27 +02004072 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07004073 /*
4074 * slot - 1 is not valid or we fail to read the left node,
4075 * no big deal, just return.
4076 */
4077 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004078 return 1;
4079
Josef Bacikbf774672020-08-20 11:46:04 -04004080 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
David Sterba8bead252018-04-04 02:03:48 +02004081 btrfs_set_lock_blocking_write(left);
Chris Mason44871b12009-03-13 10:04:31 -04004082
David Sterbae902baa2019-03-20 14:36:46 +01004083 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04004084 if (free_space < data_size) {
4085 ret = 1;
4086 goto out;
4087 }
4088
4089 /* cow and double check */
4090 ret = btrfs_cow_block(trans, root, left,
Josef Bacik9631e4c2020-08-20 11:46:03 -04004091 path->nodes[1], slot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04004092 BTRFS_NESTING_LEFT_COW);
Chris Mason44871b12009-03-13 10:04:31 -04004093 if (ret) {
4094 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004095 if (ret == -ENOSPC)
4096 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004097 goto out;
4098 }
4099
David Sterbae902baa2019-03-20 14:36:46 +01004100 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04004101 if (free_space < data_size) {
4102 ret = 1;
4103 goto out;
4104 }
4105
Qu Wenruod16c7022020-08-19 14:35:50 +08004106 if (check_sibling_keys(left, right)) {
4107 ret = -EUCLEAN;
4108 goto out;
4109 }
David Sterba8087c192019-03-20 14:40:41 +01004110 return __push_leaf_left(path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004111 empty, left, free_space, right_nritems,
4112 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004113out:
4114 btrfs_tree_unlock(left);
4115 free_extent_buffer(left);
4116 return ret;
4117}
4118
4119/*
Chris Mason74123bd2007-02-02 11:05:29 -05004120 * split the path's leaf in two, making sure there is at least data_size
4121 * available for the resulting leaf level of the path.
4122 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004123static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004124 struct btrfs_path *path,
4125 struct extent_buffer *l,
4126 struct extent_buffer *right,
4127 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004128{
David Sterba94f94ad2019-03-20 14:42:33 +01004129 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004130 int data_copy_size;
4131 int rt_data_off;
4132 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004133 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004134 struct btrfs_map_token token;
4135
Chris Mason5f39d392007-10-15 16:14:19 -04004136 nritems = nritems - mid;
4137 btrfs_set_header_nritems(right, nritems);
David Sterba8f881e82019-03-20 11:33:10 +01004138 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
Chris Mason5f39d392007-10-15 16:14:19 -04004139
4140 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4141 btrfs_item_nr_offset(mid),
4142 nritems * sizeof(struct btrfs_item));
4143
4144 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004145 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4146 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01004147 leaf_data_end(l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004148
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004149 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004150
David Sterbac82f8232019-08-09 17:48:21 +02004151 btrfs_init_map_token(&token, right);
Chris Mason5f39d392007-10-15 16:14:19 -04004152 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004153 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004154 u32 ioff;
4155
David Sterbacc4c13d2020-04-29 02:15:56 +02004156 ioff = btrfs_token_item_offset(&token, item);
4157 btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04004158 }
Chris Mason74123bd2007-02-02 11:05:29 -05004159
Chris Mason5f39d392007-10-15 16:14:19 -04004160 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004161 btrfs_item_key(right, &disk_key, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004162 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004163
4164 btrfs_mark_buffer_dirty(right);
4165 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004166 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004167
Chris Masonbe0e5c02007-01-26 15:51:26 -05004168 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004169 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004170 free_extent_buffer(path->nodes[0]);
4171 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004172 path->slots[0] -= mid;
4173 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004174 } else {
4175 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004176 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004177 }
Chris Mason5f39d392007-10-15 16:14:19 -04004178
Chris Masoneb60cea2007-02-02 09:18:22 -05004179 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004180}
4181
4182/*
Chris Mason99d8f832010-07-07 10:51:48 -04004183 * double splits happen when we need to insert a big item in the middle
4184 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4185 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4186 * A B C
4187 *
4188 * We avoid this by trying to push the items on either side of our target
4189 * into the adjacent leaves. If all goes well we can avoid the double split
4190 * completely.
4191 */
4192static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4193 struct btrfs_root *root,
4194 struct btrfs_path *path,
4195 int data_size)
4196{
4197 int ret;
4198 int progress = 0;
4199 int slot;
4200 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004201 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004202
4203 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004204 if (slot < btrfs_header_nritems(path->nodes[0]))
David Sterbae902baa2019-03-20 14:36:46 +01004205 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004206
4207 /*
4208 * try to push all the items after our slot into the
4209 * right leaf
4210 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004211 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004212 if (ret < 0)
4213 return ret;
4214
4215 if (ret == 0)
4216 progress++;
4217
4218 nritems = btrfs_header_nritems(path->nodes[0]);
4219 /*
4220 * our goal is to get our slot at the start or end of a leaf. If
4221 * we've done so we're done
4222 */
4223 if (path->slots[0] == 0 || path->slots[0] == nritems)
4224 return 0;
4225
David Sterbae902baa2019-03-20 14:36:46 +01004226 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004227 return 0;
4228
4229 /* try to push all the items before our slot into the next leaf */
4230 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004231 space_needed = data_size;
4232 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004233 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004234 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004235 if (ret < 0)
4236 return ret;
4237
4238 if (ret == 0)
4239 progress++;
4240
4241 if (progress)
4242 return 0;
4243 return 1;
4244}
4245
4246/*
Chris Mason44871b12009-03-13 10:04:31 -04004247 * split the path's leaf in two, making sure there is at least data_size
4248 * available for the resulting leaf level of the path.
4249 *
4250 * returns 0 if all went well and < 0 on failure.
4251 */
4252static noinline int split_leaf(struct btrfs_trans_handle *trans,
4253 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004254 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004255 struct btrfs_path *path, int data_size,
4256 int extend)
4257{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004258 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004259 struct extent_buffer *l;
4260 u32 nritems;
4261 int mid;
4262 int slot;
4263 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004264 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004265 int ret = 0;
4266 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004267 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004268 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004269 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004270
Yan, Zhenga5719522009-09-24 09:17:31 -04004271 l = path->nodes[0];
4272 slot = path->slots[0];
4273 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004274 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004275 return -EOVERFLOW;
4276
Chris Mason44871b12009-03-13 10:04:31 -04004277 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004278 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004279 int space_needed = data_size;
4280
4281 if (slot < btrfs_header_nritems(l))
David Sterbae902baa2019-03-20 14:36:46 +01004282 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004283
4284 wret = push_leaf_right(trans, root, path, space_needed,
4285 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004286 if (wret < 0)
4287 return wret;
4288 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004289 space_needed = data_size;
4290 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004291 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004292 wret = push_leaf_left(trans, root, path, space_needed,
4293 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004294 if (wret < 0)
4295 return wret;
4296 }
4297 l = path->nodes[0];
4298
4299 /* did the pushes work? */
David Sterbae902baa2019-03-20 14:36:46 +01004300 if (btrfs_leaf_free_space(l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004301 return 0;
4302 }
4303
4304 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004305 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004306 if (ret)
4307 return ret;
4308 }
4309again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004310 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004311 l = path->nodes[0];
4312 slot = path->slots[0];
4313 nritems = btrfs_header_nritems(l);
4314 mid = (nritems + 1) / 2;
4315
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004316 if (mid <= slot) {
4317 if (nritems == 1 ||
4318 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004319 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004320 if (slot >= nritems) {
4321 split = 0;
4322 } else {
4323 mid = slot;
4324 if (mid != nritems &&
4325 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004326 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004327 if (data_size && !tried_avoid_double)
4328 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004329 split = 2;
4330 }
4331 }
4332 }
4333 } else {
4334 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004335 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004336 if (!extend && data_size && slot == 0) {
4337 split = 0;
4338 } else if ((extend || !data_size) && slot == 0) {
4339 mid = 1;
4340 } else {
4341 mid = slot;
4342 if (mid != nritems &&
4343 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004344 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004345 if (data_size && !tried_avoid_double)
4346 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304347 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004348 }
4349 }
4350 }
4351 }
4352
4353 if (split == 0)
4354 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4355 else
4356 btrfs_item_key(l, &disk_key, mid);
4357
Josef Bacikca9d4732020-08-20 11:46:08 -04004358 /*
4359 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
4360 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
4361 * subclasses, which is 8 at the time of this patch, and we've maxed it
4362 * out. In the future we could add a
4363 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
4364 * use BTRFS_NESTING_NEW_ROOT.
4365 */
Filipe Mananaa6279472019-01-25 11:48:51 +00004366 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
Josef Bacikca9d4732020-08-20 11:46:08 -04004367 l->start, 0, num_doubles ?
4368 BTRFS_NESTING_NEW_ROOT :
4369 BTRFS_NESTING_SPLIT);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004370 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004371 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004372
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004373 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004374
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004375 if (split == 0) {
4376 if (mid <= slot) {
4377 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004378 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004379 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004380 btrfs_tree_unlock(path->nodes[0]);
4381 free_extent_buffer(path->nodes[0]);
4382 path->nodes[0] = right;
4383 path->slots[0] = 0;
4384 path->slots[1] += 1;
4385 } else {
4386 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004387 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004388 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004389 btrfs_tree_unlock(path->nodes[0]);
4390 free_extent_buffer(path->nodes[0]);
4391 path->nodes[0] = right;
4392 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004393 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004394 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004395 }
Liu Bo196e0242016-09-07 14:48:28 -07004396 /*
4397 * We create a new leaf 'right' for the required ins_len and
4398 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4399 * the content of ins_len to 'right'.
4400 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004401 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004402 }
4403
David Sterba94f94ad2019-03-20 14:42:33 +01004404 copy_for_split(trans, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004405
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004406 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004407 BUG_ON(num_doubles != 0);
4408 num_doubles++;
4409 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004410 }
Chris Mason44871b12009-03-13 10:04:31 -04004411
Jeff Mahoney143bede2012-03-01 14:56:26 +01004412 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004413
4414push_for_double:
4415 push_for_double_split(trans, root, path, data_size);
4416 tried_avoid_double = 1;
David Sterbae902baa2019-03-20 14:36:46 +01004417 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004418 return 0;
4419 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004420}
4421
Yan, Zhengad48fd752009-11-12 09:33:58 +00004422static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4423 struct btrfs_root *root,
4424 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004425{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004426 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004427 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004428 struct btrfs_file_extent_item *fi;
4429 u64 extent_len = 0;
4430 u32 item_size;
4431 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004432
4433 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004434 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4435
4436 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4437 key.type != BTRFS_EXTENT_CSUM_KEY);
4438
David Sterbae902baa2019-03-20 14:36:46 +01004439 if (btrfs_leaf_free_space(leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004440 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004441
4442 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004443 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4444 fi = btrfs_item_ptr(leaf, path->slots[0],
4445 struct btrfs_file_extent_item);
4446 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4447 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004448 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004449
Chris Mason459931e2008-12-10 09:10:46 -05004450 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004451 path->search_for_split = 1;
4452 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004453 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004454 if (ret > 0)
4455 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004456 if (ret < 0)
4457 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004458
Yan, Zhengad48fd752009-11-12 09:33:58 +00004459 ret = -EAGAIN;
4460 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004461 /* if our item isn't there, return now */
4462 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004463 goto err;
4464
Chris Mason109f6ae2010-04-02 09:20:18 -04004465 /* the leaf has changed, it now has room. return now */
David Sterbae902baa2019-03-20 14:36:46 +01004466 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004467 goto err;
4468
Yan, Zhengad48fd752009-11-12 09:33:58 +00004469 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4470 fi = btrfs_item_ptr(leaf, path->slots[0],
4471 struct btrfs_file_extent_item);
4472 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4473 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004474 }
4475
Chris Masonb9473432009-03-13 11:00:37 -04004476 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004477 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004478 if (ret)
4479 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004480
Yan, Zhengad48fd752009-11-12 09:33:58 +00004481 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004482 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004483 return 0;
4484err:
4485 path->keep_locks = 0;
4486 return ret;
4487}
4488
David Sterba25263cd2019-03-20 14:44:57 +01004489static noinline int split_item(struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004490 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004491 unsigned long split_offset)
4492{
4493 struct extent_buffer *leaf;
4494 struct btrfs_item *item;
4495 struct btrfs_item *new_item;
4496 int slot;
4497 char *buf;
4498 u32 nritems;
4499 u32 item_size;
4500 u32 orig_offset;
4501 struct btrfs_disk_key disk_key;
4502
Chris Masonb9473432009-03-13 11:00:37 -04004503 leaf = path->nodes[0];
David Sterbae902baa2019-03-20 14:36:46 +01004504 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004505
Chris Masonb4ce94d2009-02-04 09:25:08 -05004506 btrfs_set_path_blocking(path);
4507
Ross Kirkdd3cc162013-09-16 15:58:09 +01004508 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004509 orig_offset = btrfs_item_offset(leaf, item);
4510 item_size = btrfs_item_size(leaf, item);
4511
Chris Mason459931e2008-12-10 09:10:46 -05004512 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004513 if (!buf)
4514 return -ENOMEM;
4515
Chris Mason459931e2008-12-10 09:10:46 -05004516 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4517 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004518
Chris Mason459931e2008-12-10 09:10:46 -05004519 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004520 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004521 if (slot != nritems) {
4522 /* shift the items */
4523 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004524 btrfs_item_nr_offset(slot),
4525 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004526 }
4527
4528 btrfs_cpu_key_to_disk(&disk_key, new_key);
4529 btrfs_set_item_key(leaf, &disk_key, slot);
4530
Ross Kirkdd3cc162013-09-16 15:58:09 +01004531 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004532
4533 btrfs_set_item_offset(leaf, new_item, orig_offset);
4534 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4535
4536 btrfs_set_item_offset(leaf, item,
4537 orig_offset + item_size - split_offset);
4538 btrfs_set_item_size(leaf, item, split_offset);
4539
4540 btrfs_set_header_nritems(leaf, nritems + 1);
4541
4542 /* write the data for the start of the original item */
4543 write_extent_buffer(leaf, buf,
4544 btrfs_item_ptr_offset(leaf, path->slots[0]),
4545 split_offset);
4546
4547 /* write the data for the new item */
4548 write_extent_buffer(leaf, buf + split_offset,
4549 btrfs_item_ptr_offset(leaf, slot),
4550 item_size - split_offset);
4551 btrfs_mark_buffer_dirty(leaf);
4552
David Sterbae902baa2019-03-20 14:36:46 +01004553 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004554 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004555 return 0;
4556}
4557
4558/*
4559 * This function splits a single item into two items,
4560 * giving 'new_key' to the new item and splitting the
4561 * old one at split_offset (from the start of the item).
4562 *
4563 * The path may be released by this operation. After
4564 * the split, the path is pointing to the old item. The
4565 * new item is going to be in the same node as the old one.
4566 *
4567 * Note, the item being split must be smaller enough to live alone on
4568 * a tree block with room for one extra struct btrfs_item
4569 *
4570 * This allows us to split the item in place, keeping a lock on the
4571 * leaf the entire time.
4572 */
4573int btrfs_split_item(struct btrfs_trans_handle *trans,
4574 struct btrfs_root *root,
4575 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004576 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004577 unsigned long split_offset)
4578{
4579 int ret;
4580 ret = setup_leaf_for_split(trans, root, path,
4581 sizeof(struct btrfs_item));
4582 if (ret)
4583 return ret;
4584
David Sterba25263cd2019-03-20 14:44:57 +01004585 ret = split_item(path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004586 return ret;
4587}
4588
4589/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004590 * This function duplicate a item, giving 'new_key' to the new item.
4591 * It guarantees both items live in the same tree leaf and the new item
4592 * is contiguous with the original item.
4593 *
4594 * This allows us to split file extent in place, keeping a lock on the
4595 * leaf the entire time.
4596 */
4597int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4598 struct btrfs_root *root,
4599 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004600 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004601{
4602 struct extent_buffer *leaf;
4603 int ret;
4604 u32 item_size;
4605
4606 leaf = path->nodes[0];
4607 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4608 ret = setup_leaf_for_split(trans, root, path,
4609 item_size + sizeof(struct btrfs_item));
4610 if (ret)
4611 return ret;
4612
4613 path->slots[0]++;
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004614 setup_items_for_insert(root, path, new_key, &item_size, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004615 leaf = path->nodes[0];
4616 memcpy_extent_buffer(leaf,
4617 btrfs_item_ptr_offset(leaf, path->slots[0]),
4618 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4619 item_size);
4620 return 0;
4621}
4622
4623/*
Chris Masond352ac62008-09-29 15:18:18 -04004624 * make the item pointed to by the path smaller. new_size indicates
4625 * how small to make it, and from_end tells us if we just chop bytes
4626 * off the end of the item or if we shift the item to chop bytes off
4627 * the front.
4628 */
David Sterba78ac4f92019-03-20 14:49:12 +01004629void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004630{
Chris Masonb18c6682007-04-17 13:26:50 -04004631 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004632 struct extent_buffer *leaf;
4633 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004634 u32 nritems;
4635 unsigned int data_end;
4636 unsigned int old_data_start;
4637 unsigned int old_size;
4638 unsigned int size_diff;
4639 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004640 struct btrfs_map_token token;
4641
Chris Mason5f39d392007-10-15 16:14:19 -04004642 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004643 slot = path->slots[0];
4644
4645 old_size = btrfs_item_size_nr(leaf, slot);
4646 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004647 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004648
Chris Mason5f39d392007-10-15 16:14:19 -04004649 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004650 data_end = leaf_data_end(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004651
Chris Mason5f39d392007-10-15 16:14:19 -04004652 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004653
Chris Masonb18c6682007-04-17 13:26:50 -04004654 size_diff = old_size - new_size;
4655
4656 BUG_ON(slot < 0);
4657 BUG_ON(slot >= nritems);
4658
4659 /*
4660 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4661 */
4662 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004663 btrfs_init_map_token(&token, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004664 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004665 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004666 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004667
David Sterbacc4c13d2020-04-29 02:15:56 +02004668 ioff = btrfs_token_item_offset(&token, item);
4669 btrfs_set_token_item_offset(&token, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04004670 }
Chris Masondb945352007-10-15 16:15:53 -04004671
Chris Masonb18c6682007-04-17 13:26:50 -04004672 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004673 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004674 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4675 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004676 data_end, old_data_start + new_size - data_end);
4677 } else {
4678 struct btrfs_disk_key disk_key;
4679 u64 offset;
4680
4681 btrfs_item_key(leaf, &disk_key, slot);
4682
4683 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4684 unsigned long ptr;
4685 struct btrfs_file_extent_item *fi;
4686
4687 fi = btrfs_item_ptr(leaf, slot,
4688 struct btrfs_file_extent_item);
4689 fi = (struct btrfs_file_extent_item *)(
4690 (unsigned long)fi - size_diff);
4691
4692 if (btrfs_file_extent_type(leaf, fi) ==
4693 BTRFS_FILE_EXTENT_INLINE) {
4694 ptr = btrfs_item_ptr_offset(leaf, slot);
4695 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004696 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004697 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004698 }
4699 }
4700
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004701 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4702 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004703 data_end, old_data_start - data_end);
4704
4705 offset = btrfs_disk_key_offset(&disk_key);
4706 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4707 btrfs_set_item_key(leaf, &disk_key, slot);
4708 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004709 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004710 }
Chris Mason5f39d392007-10-15 16:14:19 -04004711
Ross Kirkdd3cc162013-09-16 15:58:09 +01004712 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004713 btrfs_set_item_size(leaf, item, new_size);
4714 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004715
David Sterbae902baa2019-03-20 14:36:46 +01004716 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004717 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004718 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004719 }
Chris Masonb18c6682007-04-17 13:26:50 -04004720}
4721
Chris Masond352ac62008-09-29 15:18:18 -04004722/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004723 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004724 */
David Sterbac71dd882019-03-20 14:51:10 +01004725void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004726{
Chris Mason6567e832007-04-16 09:22:45 -04004727 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004728 struct extent_buffer *leaf;
4729 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004730 u32 nritems;
4731 unsigned int data_end;
4732 unsigned int old_data;
4733 unsigned int old_size;
4734 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004735 struct btrfs_map_token token;
4736
Chris Mason5f39d392007-10-15 16:14:19 -04004737 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004738
Chris Mason5f39d392007-10-15 16:14:19 -04004739 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004740 data_end = leaf_data_end(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004741
David Sterbae902baa2019-03-20 14:36:46 +01004742 if (btrfs_leaf_free_space(leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004743 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004744 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004745 }
Chris Mason6567e832007-04-16 09:22:45 -04004746 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004747 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004748
4749 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004750 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004751 btrfs_print_leaf(leaf);
David Sterbac71dd882019-03-20 14:51:10 +01004752 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004753 slot, nritems);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004754 BUG();
Chris Mason3326d1b2007-10-15 16:18:25 -04004755 }
Chris Mason6567e832007-04-16 09:22:45 -04004756
4757 /*
4758 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4759 */
4760 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004761 btrfs_init_map_token(&token, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004762 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004763 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004764 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004765
David Sterbacc4c13d2020-04-29 02:15:56 +02004766 ioff = btrfs_token_item_offset(&token, item);
4767 btrfs_set_token_item_offset(&token, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04004768 }
Chris Mason5f39d392007-10-15 16:14:19 -04004769
Chris Mason6567e832007-04-16 09:22:45 -04004770 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004771 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4772 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004773 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004774
Chris Mason6567e832007-04-16 09:22:45 -04004775 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004776 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004777 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004778 btrfs_set_item_size(leaf, item, old_size + data_size);
4779 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004780
David Sterbae902baa2019-03-20 14:36:46 +01004781 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004782 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004783 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004784 }
Chris Mason6567e832007-04-16 09:22:45 -04004785}
4786
Nikolay Borisovda9ffb22020-09-01 17:40:00 +03004787/**
4788 * setup_items_for_insert - Helper called before inserting one or more items
4789 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
4790 * in a function that doesn't call btrfs_search_slot
4791 *
4792 * @root: root we are inserting items to
4793 * @path: points to the leaf/slot where we are going to insert new items
4794 * @cpu_key: array of keys for items to be inserted
4795 * @data_size: size of the body of each item we are going to insert
4796 * @nr: size of @cpu_key/@data_size arrays
Chris Mason74123bd2007-02-02 11:05:29 -05004797 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004798void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004799 const struct btrfs_key *cpu_key, u32 *data_size,
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004800 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004801{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004802 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004803 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004804 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004805 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004806 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004807 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004808 struct extent_buffer *leaf;
4809 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004810 struct btrfs_map_token token;
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004811 u32 total_size;
4812 u32 total_data = 0;
4813
4814 for (i = 0; i < nr; i++)
4815 total_data += data_size[i];
4816 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Masoncfed81a2012-03-03 07:40:03 -05004817
Filipe Manana24cdc842014-07-28 19:34:35 +01004818 if (path->slots[0] == 0) {
4819 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004820 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004821 }
4822 btrfs_unlock_up_safe(path, 1);
4823
Chris Mason5f39d392007-10-15 16:14:19 -04004824 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004825 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004826
Chris Mason5f39d392007-10-15 16:14:19 -04004827 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004828 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004829
David Sterbae902baa2019-03-20 14:36:46 +01004830 if (btrfs_leaf_free_space(leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004831 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004832 btrfs_crit(fs_info, "not enough freespace need %u have %d",
David Sterbae902baa2019-03-20 14:36:46 +01004833 total_size, btrfs_leaf_free_space(leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004834 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004835 }
Chris Mason5f39d392007-10-15 16:14:19 -04004836
David Sterbac82f8232019-08-09 17:48:21 +02004837 btrfs_init_map_token(&token, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004838 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004839 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004840
Chris Mason5f39d392007-10-15 16:14:19 -04004841 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004842 btrfs_print_leaf(leaf);
Nikolay Borisov7269ddd2020-09-01 17:40:01 +03004843 btrfs_crit(fs_info,
4844 "item at slot %d with data offset %u beyond data end of leaf %u",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004845 slot, old_data, data_end);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004846 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004847 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004848 /*
4849 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4850 */
4851 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004852 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004853 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004854
Jeff Mahoney62e85572016-09-20 10:05:01 -04004855 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02004856 ioff = btrfs_token_item_offset(&token, item);
4857 btrfs_set_token_item_offset(&token, item,
4858 ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04004859 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004860 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004861 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004862 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004863 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004864
4865 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004866 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4867 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004868 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004869 data_end = old_data;
4870 }
Chris Mason5f39d392007-10-15 16:14:19 -04004871
Chris Mason62e27492007-03-15 12:56:47 -04004872 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004873 for (i = 0; i < nr; i++) {
4874 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4875 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004876 item = btrfs_item_nr(slot + i);
Chris Mason9c583092008-01-29 15:15:18 -05004877 data_end -= data_size[i];
Nikolay Borisovfc0716c2020-09-01 17:39:57 +03004878 btrfs_set_token_item_offset(&token, item, data_end);
David Sterbacc4c13d2020-04-29 02:15:56 +02004879 btrfs_set_token_item_size(&token, item, data_size[i]);
Chris Mason9c583092008-01-29 15:15:18 -05004880 }
Chris Mason44871b12009-03-13 10:04:31 -04004881
Chris Mason9c583092008-01-29 15:15:18 -05004882 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004883 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004884
David Sterbae902baa2019-03-20 14:36:46 +01004885 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004886 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004887 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004888 }
Chris Mason44871b12009-03-13 10:04:31 -04004889}
4890
4891/*
4892 * Given a key and some data, insert items into the tree.
4893 * This does all the path init required, making room in the tree if needed.
4894 */
4895int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4896 struct btrfs_root *root,
4897 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004898 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004899 int nr)
4900{
Chris Mason44871b12009-03-13 10:04:31 -04004901 int ret = 0;
4902 int slot;
4903 int i;
4904 u32 total_size = 0;
4905 u32 total_data = 0;
4906
4907 for (i = 0; i < nr; i++)
4908 total_data += data_size[i];
4909
4910 total_size = total_data + (nr * sizeof(struct btrfs_item));
4911 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4912 if (ret == 0)
4913 return -EEXIST;
4914 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004915 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004916
Chris Mason44871b12009-03-13 10:04:31 -04004917 slot = path->slots[0];
4918 BUG_ON(slot < 0);
4919
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03004920 setup_items_for_insert(root, path, cpu_key, data_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004921 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004922}
4923
4924/*
4925 * Given a key and some data, insert an item into the tree.
4926 * This does all the path init required, making room in the tree if needed.
4927 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004928int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4929 const struct btrfs_key *cpu_key, void *data,
4930 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004931{
4932 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004933 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004934 struct extent_buffer *leaf;
4935 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004936
Chris Mason2c90e5d2007-04-02 10:50:19 -04004937 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004938 if (!path)
4939 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004940 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004941 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004942 leaf = path->nodes[0];
4943 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4944 write_extent_buffer(leaf, data, ptr, data_size);
4945 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004946 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004947 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004948 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004949}
4950
Chris Mason74123bd2007-02-02 11:05:29 -05004951/*
Chris Mason5de08d72007-02-24 06:24:44 -05004952 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004953 *
Chris Masond352ac62008-09-29 15:18:18 -04004954 * the tree should have been previously balanced so the deletion does not
4955 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004956 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004957static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4958 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004959{
Chris Mason5f39d392007-10-15 16:14:19 -04004960 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004961 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004962 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004963
Chris Mason5f39d392007-10-15 16:14:19 -04004964 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004965 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004966 if (level) {
4967 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004968 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004969 BUG_ON(ret < 0);
4970 }
Chris Mason5f39d392007-10-15 16:14:19 -04004971 memmove_extent_buffer(parent,
4972 btrfs_node_key_ptr_offset(slot),
4973 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004974 sizeof(struct btrfs_key_ptr) *
4975 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004976 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004977 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4978 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004979 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004980 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004981
Chris Mason7518a232007-03-12 12:01:18 -04004982 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004983 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004984 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004985 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004986 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004987 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004988 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004989 struct btrfs_disk_key disk_key;
4990
4991 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004992 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004993 }
Chris Masond6025572007-03-30 14:27:56 -04004994 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004995}
4996
Chris Mason74123bd2007-02-02 11:05:29 -05004997/*
Chris Mason323ac952008-10-01 19:05:46 -04004998 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004999 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04005000 *
5001 * This deletes the pointer in path->nodes[1] and frees the leaf
5002 * block extent. zero is returned if it all worked out, < 0 otherwise.
5003 *
5004 * The path must have already been setup for deleting the leaf, including
5005 * all the proper balancing. path->nodes[1] must be locked.
5006 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01005007static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
5008 struct btrfs_root *root,
5009 struct btrfs_path *path,
5010 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04005011{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005012 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00005013 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04005014
Chris Mason4d081c42009-02-04 09:31:28 -05005015 /*
5016 * btrfs_free_extent is expensive, we want to make sure we
5017 * aren't holding any locks when we call it
5018 */
5019 btrfs_unlock_up_safe(path, 0);
5020
Yan, Zhengf0486c62010-05-16 10:46:25 -04005021 root_sub_used(root, leaf->len);
5022
David Sterba67439da2019-10-08 13:28:47 +02005023 atomic_inc(&leaf->refs);
Jan Schmidt5581a512012-05-16 17:04:52 +02005024 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05005025 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04005026}
5027/*
Chris Mason74123bd2007-02-02 11:05:29 -05005028 * delete the item at the leaf level in path. If that empties
5029 * the leaf, remove it from the tree
5030 */
Chris Mason85e21ba2008-01-29 15:11:36 -05005031int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5032 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05005033{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005034 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04005035 struct extent_buffer *leaf;
5036 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00005037 u32 last_off;
5038 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05005039 int ret = 0;
5040 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05005041 int i;
Chris Mason7518a232007-03-12 12:01:18 -04005042 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005043
Chris Mason5f39d392007-10-15 16:14:19 -04005044 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05005045 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
5046
5047 for (i = 0; i < nr; i++)
5048 dsize += btrfs_item_size_nr(leaf, slot + i);
5049
Chris Mason5f39d392007-10-15 16:14:19 -04005050 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005051
Chris Mason85e21ba2008-01-29 15:11:36 -05005052 if (slot + nr != nritems) {
David Sterba8f881e82019-03-20 11:33:10 +01005053 int data_end = leaf_data_end(leaf);
David Sterbac82f8232019-08-09 17:48:21 +02005054 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04005055
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03005056 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04005057 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03005058 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05005059 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04005060
David Sterbac82f8232019-08-09 17:48:21 +02005061 btrfs_init_map_token(&token, leaf);
Chris Mason85e21ba2008-01-29 15:11:36 -05005062 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04005063 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04005064
Ross Kirkdd3cc162013-09-16 15:58:09 +01005065 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02005066 ioff = btrfs_token_item_offset(&token, item);
5067 btrfs_set_token_item_offset(&token, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04005068 }
Chris Masondb945352007-10-15 16:15:53 -04005069
Chris Mason5f39d392007-10-15 16:14:19 -04005070 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05005071 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04005072 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05005073 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05005074 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005075 btrfs_set_header_nritems(leaf, nritems - nr);
5076 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04005077
Chris Mason74123bd2007-02-02 11:05:29 -05005078 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04005079 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005080 if (leaf == root->node) {
5081 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005082 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005083 btrfs_set_path_blocking(path);
David Sterba6a884d7d2019-03-20 14:30:02 +01005084 btrfs_clean_tree_block(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005085 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005086 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005087 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005088 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005089 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005090 struct btrfs_disk_key disk_key;
5091
5092 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03005093 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005094 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005095
Chris Mason74123bd2007-02-02 11:05:29 -05005096 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005097 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005098 /* push_leaf_left fixes the path.
5099 * make sure the path still points to our leaf
5100 * for possible call to del_ptr below
5101 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005102 slot = path->slots[1];
David Sterba67439da2019-10-08 13:28:47 +02005103 atomic_inc(&leaf->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04005104
Chris Masonb9473432009-03-13 11:00:37 -04005105 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005106 wret = push_leaf_left(trans, root, path, 1, 1,
5107 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005108 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005109 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005110
5111 if (path->nodes[0] == leaf &&
5112 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005113 wret = push_leaf_right(trans, root, path, 1,
5114 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005115 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005116 ret = wret;
5117 }
Chris Mason5f39d392007-10-15 16:14:19 -04005118
5119 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005120 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005121 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005122 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005123 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005124 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005125 /* if we're still in the path, make sure
5126 * we're dirty. Otherwise, one of the
5127 * push_leaf functions must have already
5128 * dirtied this buffer
5129 */
5130 if (path->nodes[0] == leaf)
5131 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005132 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005133 }
Chris Masond5719762007-03-23 10:01:08 -04005134 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005135 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005136 }
5137 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005138 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005139}
5140
Chris Mason97571fd2007-02-24 13:39:08 -05005141/*
Chris Mason925baed2008-06-25 16:01:30 -04005142 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005143 * returns 0 if it found something or 1 if there are no lesser leaves.
5144 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005145 *
5146 * This may release the path, and so you may lose any locks held at the
5147 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005148 */
Josef Bacik16e75492013-10-22 12:18:51 -04005149int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005150{
Chris Mason925baed2008-06-25 16:01:30 -04005151 struct btrfs_key key;
5152 struct btrfs_disk_key found_key;
5153 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005154
Chris Mason925baed2008-06-25 16:01:30 -04005155 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005156
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005157 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005158 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005159 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005160 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005161 key.offset = (u64)-1;
5162 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005163 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005164 key.type = (u8)-1;
5165 key.offset = (u64)-1;
5166 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005167 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005168 }
Chris Mason7bb86312007-12-11 09:25:06 -05005169
David Sterbab3b4aa72011-04-21 01:20:15 +02005170 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005171 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5172 if (ret < 0)
5173 return ret;
5174 btrfs_item_key(path->nodes[0], &found_key, 0);
5175 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005176 /*
5177 * We might have had an item with the previous key in the tree right
5178 * before we released our path. And after we released our path, that
5179 * item might have been pushed to the first slot (0) of the leaf we
5180 * were holding due to a tree balance. Alternatively, an item with the
5181 * previous key can exist as the only element of a leaf (big fat item).
5182 * Therefore account for these 2 cases, so that our callers (like
5183 * btrfs_previous_item) don't miss an existing item with a key matching
5184 * the previous key we computed above.
5185 */
5186 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005187 return 0;
5188 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005189}
5190
Chris Mason3f157a22008-06-25 16:01:31 -04005191/*
5192 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005193 * for nodes or leaves that are have a minimum transaction id.
5194 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005195 *
5196 * This does not cow, but it does stuff the starting key it finds back
5197 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5198 * key and get a writable path.
5199 *
Chris Mason3f157a22008-06-25 16:01:31 -04005200 * This honors path->lowest_level to prevent descent past a given level
5201 * of the tree.
5202 *
Chris Masond352ac62008-09-29 15:18:18 -04005203 * min_trans indicates the oldest transaction that you are interested
5204 * in walking through. Any nodes or leaves older than min_trans are
5205 * skipped over (without reading them).
5206 *
Chris Mason3f157a22008-06-25 16:01:31 -04005207 * returns zero if something useful was found, < 0 on error and 1 if there
5208 * was nothing in the tree that matched the search criteria.
5209 */
5210int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005211 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005212 u64 min_trans)
5213{
5214 struct extent_buffer *cur;
5215 struct btrfs_key found_key;
5216 int slot;
Yan96524802008-07-24 12:19:49 -04005217 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005218 u32 nritems;
5219 int level;
5220 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005221 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005222
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005223 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005224again:
Chris Masonbd681512011-07-16 15:23:14 -04005225 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005226 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005227 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005228 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005229 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005230
5231 if (btrfs_header_generation(cur) < min_trans) {
5232 ret = 1;
5233 goto out;
5234 }
Chris Masond3977122009-01-05 21:25:51 -05005235 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005236 nritems = btrfs_header_nritems(cur);
5237 level = btrfs_header_level(cur);
Qu Wenruoe3b83362020-04-17 15:08:21 +08005238 sret = btrfs_bin_search(cur, min_key, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00005239 if (sret < 0) {
5240 ret = sret;
5241 goto out;
5242 }
Chris Mason3f157a22008-06-25 16:01:31 -04005243
Chris Mason323ac952008-10-01 19:05:46 -04005244 /* at the lowest level, we're done, setup the path and exit */
5245 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005246 if (slot >= nritems)
5247 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005248 ret = 0;
5249 path->slots[level] = slot;
5250 btrfs_item_key_to_cpu(cur, &found_key, slot);
5251 goto out;
5252 }
Yan96524802008-07-24 12:19:49 -04005253 if (sret && slot > 0)
5254 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005255 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005256 * check this node pointer against the min_trans parameters.
Randy Dunlap260db432020-08-04 19:48:34 -07005257 * If it is too old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005258 */
Chris Masond3977122009-01-05 21:25:51 -05005259 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005260 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005261
Chris Mason3f157a22008-06-25 16:01:31 -04005262 gen = btrfs_node_ptr_generation(cur, slot);
5263 if (gen < min_trans) {
5264 slot++;
5265 continue;
5266 }
Eric Sandeende78b512013-01-31 18:21:12 +00005267 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005268 }
Chris Masone02119d2008-09-05 16:13:11 -04005269find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005270 /*
5271 * we didn't find a candidate key in this node, walk forward
5272 * and find another one
5273 */
5274 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005275 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005276 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005277 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005278 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005279 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005280 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005281 goto again;
5282 } else {
5283 goto out;
5284 }
5285 }
5286 /* save our key for returning back */
5287 btrfs_node_key_to_cpu(cur, &found_key, slot);
5288 path->slots[level] = slot;
5289 if (level == path->lowest_level) {
5290 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005291 goto out;
5292 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005293 btrfs_set_path_blocking(path);
David Sterba4b231ae2019-08-21 19:16:27 +02005294 cur = btrfs_read_node_slot(cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005295 if (IS_ERR(cur)) {
5296 ret = PTR_ERR(cur);
5297 goto out;
5298 }
Chris Mason3f157a22008-06-25 16:01:31 -04005299
Chris Masonbd681512011-07-16 15:23:14 -04005300 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005301
Chris Masonbd681512011-07-16 15:23:14 -04005302 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005303 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005304 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005305 }
5306out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005307 path->keep_locks = keep_locks;
5308 if (ret == 0) {
5309 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5310 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005311 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005312 }
Chris Mason3f157a22008-06-25 16:01:31 -04005313 return ret;
5314}
5315
5316/*
5317 * this is similar to btrfs_next_leaf, but does not try to preserve
5318 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005319 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005320 *
5321 * 0 is returned if another key is found, < 0 if there are any errors
5322 * and 1 is returned if there are no higher keys in the tree
5323 *
5324 * path->keep_locks should be set to 1 on the search made before
5325 * calling this function.
5326 */
Chris Masone7a84562008-06-25 16:01:31 -04005327int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005328 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005329{
Chris Masone7a84562008-06-25 16:01:31 -04005330 int slot;
5331 struct extent_buffer *c;
5332
Josef Bacik6a9fb462019-06-20 15:37:52 -04005333 WARN_ON(!path->keep_locks && !path->skip_locking);
Chris Masond3977122009-01-05 21:25:51 -05005334 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005335 if (!path->nodes[level])
5336 return 1;
5337
5338 slot = path->slots[level] + 1;
5339 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005340next:
Chris Masone7a84562008-06-25 16:01:31 -04005341 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005342 int ret;
5343 int orig_lowest;
5344 struct btrfs_key cur_key;
5345 if (level + 1 >= BTRFS_MAX_LEVEL ||
5346 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005347 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005348
Josef Bacik6a9fb462019-06-20 15:37:52 -04005349 if (path->locks[level + 1] || path->skip_locking) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005350 level++;
5351 continue;
5352 }
5353
5354 slot = btrfs_header_nritems(c) - 1;
5355 if (level == 0)
5356 btrfs_item_key_to_cpu(c, &cur_key, slot);
5357 else
5358 btrfs_node_key_to_cpu(c, &cur_key, slot);
5359
5360 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005361 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005362 path->lowest_level = level;
5363 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5364 0, 0);
5365 path->lowest_level = orig_lowest;
5366 if (ret < 0)
5367 return ret;
5368
5369 c = path->nodes[level];
5370 slot = path->slots[level];
5371 if (ret == 0)
5372 slot++;
5373 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005374 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005375
Chris Masone7a84562008-06-25 16:01:31 -04005376 if (level == 0)
5377 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005378 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005379 u64 gen = btrfs_node_ptr_generation(c, slot);
5380
Chris Mason3f157a22008-06-25 16:01:31 -04005381 if (gen < min_trans) {
5382 slot++;
5383 goto next;
5384 }
Chris Masone7a84562008-06-25 16:01:31 -04005385 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005386 }
Chris Masone7a84562008-06-25 16:01:31 -04005387 return 0;
5388 }
5389 return 1;
5390}
5391
Chris Mason7bb86312007-12-11 09:25:06 -05005392/*
Chris Mason925baed2008-06-25 16:01:30 -04005393 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005394 * returns 0 if it found something or 1 if there are no greater leaves.
5395 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005396 */
Chris Mason234b63a2007-03-13 10:46:10 -04005397int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005398{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005399 return btrfs_next_old_leaf(root, path, 0);
5400}
5401
5402int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5403 u64 time_seq)
5404{
Chris Masond97e63b2007-02-20 16:40:44 -05005405 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005406 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005407 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005408 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005409 struct btrfs_key key;
5410 u32 nritems;
5411 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005412 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005413 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005414
5415 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005416 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005417 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005418
Chris Mason8e73f272009-04-03 10:14:18 -04005419 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5420again:
5421 level = 1;
5422 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005423 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005424 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005425
Chris Masona2135012008-06-25 16:01:30 -04005426 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005427 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005428
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005429 if (time_seq)
5430 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5431 else
5432 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005433 path->keep_locks = 0;
5434
5435 if (ret < 0)
5436 return ret;
5437
Chris Masona2135012008-06-25 16:01:30 -04005438 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005439 /*
5440 * by releasing the path above we dropped all our locks. A balance
5441 * could have added more items next to the key that used to be
5442 * at the very end of the block. So, check again here and
5443 * advance the path if there are now more items available.
5444 */
Chris Masona2135012008-06-25 16:01:30 -04005445 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005446 if (ret == 0)
5447 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005448 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005449 goto done;
5450 }
Liu Bo0b43e042014-06-09 11:04:49 +08005451 /*
5452 * So the above check misses one case:
5453 * - after releasing the path above, someone has removed the item that
5454 * used to be at the very end of the block, and balance between leafs
5455 * gets another one with bigger key.offset to replace it.
5456 *
5457 * This one should be returned as well, or we can get leaf corruption
5458 * later(esp. in __btrfs_drop_extents()).
5459 *
5460 * And a bit more explanation about this check,
5461 * with ret > 0, the key isn't found, the path points to the slot
5462 * where it should be inserted, so the path->slots[0] item must be the
5463 * bigger one.
5464 */
5465 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5466 ret = 0;
5467 goto done;
5468 }
Chris Masond97e63b2007-02-20 16:40:44 -05005469
Chris Masond3977122009-01-05 21:25:51 -05005470 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005471 if (!path->nodes[level]) {
5472 ret = 1;
5473 goto done;
5474 }
Chris Mason5f39d392007-10-15 16:14:19 -04005475
Chris Masond97e63b2007-02-20 16:40:44 -05005476 slot = path->slots[level] + 1;
5477 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005478 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005479 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005480 if (level == BTRFS_MAX_LEVEL) {
5481 ret = 1;
5482 goto done;
5483 }
Chris Masond97e63b2007-02-20 16:40:44 -05005484 continue;
5485 }
Chris Mason5f39d392007-10-15 16:14:19 -04005486
Chris Mason925baed2008-06-25 16:01:30 -04005487 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005488 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005489 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005490 }
Chris Mason5f39d392007-10-15 16:14:19 -04005491
Chris Mason8e73f272009-04-03 10:14:18 -04005492 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005493 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005494 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005495 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005496 if (ret == -EAGAIN)
5497 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005498
Chris Mason76a05b32009-05-14 13:24:30 -04005499 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005500 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005501 goto done;
5502 }
5503
Chris Mason5cd57b22008-06-25 16:01:30 -04005504 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005505 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005506 if (!ret && time_seq) {
5507 /*
5508 * If we don't get the lock, we may be racing
5509 * with push_leaf_left, holding that lock while
5510 * itself waiting for the leaf we've currently
5511 * locked. To solve this situation, we give up
5512 * on our lock and cycle.
5513 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005514 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005515 btrfs_release_path(path);
5516 cond_resched();
5517 goto again;
5518 }
Chris Mason8e73f272009-04-03 10:14:18 -04005519 if (!ret) {
5520 btrfs_set_path_blocking(path);
Josef Bacikfd7ba1c2020-08-20 11:46:02 -04005521 __btrfs_tree_read_lock(next,
Josef Bacikbf774672020-08-20 11:46:04 -04005522 BTRFS_NESTING_RIGHT,
Josef Bacikfd7ba1c2020-08-20 11:46:02 -04005523 path->recurse);
Chris Mason8e73f272009-04-03 10:14:18 -04005524 }
Chris Mason31533fb2011-07-26 16:01:59 -04005525 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005526 }
Chris Masond97e63b2007-02-20 16:40:44 -05005527 break;
5528 }
5529 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005530 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005531 level--;
5532 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005533 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005534 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005535
Chris Mason5f39d392007-10-15 16:14:19 -04005536 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005537 path->nodes[level] = next;
5538 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005539 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005540 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005541 if (!level)
5542 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005543
Liu Bod07b8522017-01-30 12:23:42 -08005544 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005545 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005546 if (ret == -EAGAIN)
5547 goto again;
5548
Chris Mason76a05b32009-05-14 13:24:30 -04005549 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005550 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005551 goto done;
5552 }
5553
Chris Mason5cd57b22008-06-25 16:01:30 -04005554 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005555 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005556 if (!ret) {
5557 btrfs_set_path_blocking(path);
Josef Bacikfd7ba1c2020-08-20 11:46:02 -04005558 __btrfs_tree_read_lock(next,
Josef Bacikbf774672020-08-20 11:46:04 -04005559 BTRFS_NESTING_RIGHT,
Josef Bacikfd7ba1c2020-08-20 11:46:02 -04005560 path->recurse);
Chris Mason8e73f272009-04-03 10:14:18 -04005561 }
Chris Mason31533fb2011-07-26 16:01:59 -04005562 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005563 }
Chris Masond97e63b2007-02-20 16:40:44 -05005564 }
Chris Mason8e73f272009-04-03 10:14:18 -04005565 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005566done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005567 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005568 path->leave_spinning = old_spinning;
5569 if (!old_spinning)
5570 btrfs_set_path_blocking(path);
5571
5572 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005573}
Chris Mason0b86a832008-03-24 15:01:56 -04005574
Chris Mason3f157a22008-06-25 16:01:31 -04005575/*
5576 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5577 * searching until it gets past min_objectid or finds an item of 'type'
5578 *
5579 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5580 */
Chris Mason0b86a832008-03-24 15:01:56 -04005581int btrfs_previous_item(struct btrfs_root *root,
5582 struct btrfs_path *path, u64 min_objectid,
5583 int type)
5584{
5585 struct btrfs_key found_key;
5586 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005587 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005588 int ret;
5589
Chris Masond3977122009-01-05 21:25:51 -05005590 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005591 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005592 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005593 ret = btrfs_prev_leaf(root, path);
5594 if (ret != 0)
5595 return ret;
5596 } else {
5597 path->slots[0]--;
5598 }
5599 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005600 nritems = btrfs_header_nritems(leaf);
5601 if (nritems == 0)
5602 return 1;
5603 if (path->slots[0] == nritems)
5604 path->slots[0]--;
5605
Chris Mason0b86a832008-03-24 15:01:56 -04005606 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005607 if (found_key.objectid < min_objectid)
5608 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005609 if (found_key.type == type)
5610 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005611 if (found_key.objectid == min_objectid &&
5612 found_key.type < type)
5613 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005614 }
5615 return 1;
5616}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005617
5618/*
5619 * search in extent tree to find a previous Metadata/Data extent item with
5620 * min objecitd.
5621 *
5622 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5623 */
5624int btrfs_previous_extent_item(struct btrfs_root *root,
5625 struct btrfs_path *path, u64 min_objectid)
5626{
5627 struct btrfs_key found_key;
5628 struct extent_buffer *leaf;
5629 u32 nritems;
5630 int ret;
5631
5632 while (1) {
5633 if (path->slots[0] == 0) {
5634 btrfs_set_path_blocking(path);
5635 ret = btrfs_prev_leaf(root, path);
5636 if (ret != 0)
5637 return ret;
5638 } else {
5639 path->slots[0]--;
5640 }
5641 leaf = path->nodes[0];
5642 nritems = btrfs_header_nritems(leaf);
5643 if (nritems == 0)
5644 return 1;
5645 if (path->slots[0] == nritems)
5646 path->slots[0]--;
5647
5648 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5649 if (found_key.objectid < min_objectid)
5650 break;
5651 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5652 found_key.type == BTRFS_METADATA_ITEM_KEY)
5653 return 0;
5654 if (found_key.objectid == min_objectid &&
5655 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5656 break;
5657 }
5658 return 1;
5659}