blob: a484fb72a01f0b620627331522e805a28b7101c0 [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"
Filipe Mananaf3a84cc2021-03-11 14:31:07 +000017#include "tree-mod-log.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050018
Chris Masone089f052007-03-16 16:20:31 -040019static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
20 *root, struct btrfs_path *path, int level);
Omar Sandoval310712b2017-01-17 23:24:37 -080021static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
22 const struct btrfs_key *ins_key, struct btrfs_path *path,
23 int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040024static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040025 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040026 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040027static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -040028 struct extent_buffer *dst_buf,
29 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000030static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
31 int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050032
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020033static const struct btrfs_csums {
34 u16 size;
David Sterba59a0fcd2020-02-27 21:00:45 +010035 const char name[10];
36 const char driver[12];
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020037} btrfs_csums[] = {
38 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
Johannes Thumshirn3951e7f2019-10-07 11:11:01 +020039 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
Johannes Thumshirn3831bf02019-10-07 11:11:02 +020040 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
David Sterba352ae072019-10-07 11:11:02 +020041 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
42 .driver = "blake2b-256" },
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020043};
44
45int btrfs_super_csum_size(const struct btrfs_super_block *s)
46{
47 u16 t = btrfs_super_csum_type(s);
48 /*
49 * csum type is validated at mount time
50 */
51 return btrfs_csums[t].size;
52}
53
54const char *btrfs_super_csum_name(u16 csum_type)
55{
56 /* csum type is validated at mount time */
57 return btrfs_csums[csum_type].name;
58}
59
David Sterbab4e967b2019-10-08 18:41:33 +020060/*
61 * Return driver name if defined, otherwise the name that's also a valid driver
62 * name
63 */
64const char *btrfs_super_csum_driver(u16 csum_type)
65{
66 /* csum type is validated at mount time */
David Sterba59a0fcd2020-02-27 21:00:45 +010067 return btrfs_csums[csum_type].driver[0] ?
68 btrfs_csums[csum_type].driver :
David Sterbab4e967b2019-10-08 18:41:33 +020069 btrfs_csums[csum_type].name;
70}
71
David Sterba604997b2020-07-27 17:38:19 +020072size_t __attribute_const__ btrfs_get_num_csums(void)
David Sterbaf7cea562019-10-07 11:11:03 +020073{
74 return ARRAY_SIZE(btrfs_csums);
75}
76
Chris Mason2c90e5d2007-04-02 10:50:19 -040077struct btrfs_path *btrfs_alloc_path(void)
78{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090079 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040080}
81
Chris Masond352ac62008-09-29 15:18:18 -040082/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040083void btrfs_free_path(struct btrfs_path *p)
84{
Jesper Juhlff175d52010-12-25 21:22:30 +000085 if (!p)
86 return;
David Sterbab3b4aa72011-04-21 01:20:15 +020087 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040088 kmem_cache_free(btrfs_path_cachep, p);
89}
90
Chris Masond352ac62008-09-29 15:18:18 -040091/*
92 * path release drops references on the extent buffers in the path
93 * and it drops any locks held by this path
94 *
95 * It is safe to call this on paths that no locks or extent buffers held.
96 */
David Sterbab3b4aa72011-04-21 01:20:15 +020097noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050098{
99 int i;
Chris Masona2135012008-06-25 16:01:30 -0400100
Chris Mason234b63a2007-03-13 10:46:10 -0400101 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400102 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500103 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400104 continue;
105 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400106 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400107 p->locks[i] = 0;
108 }
Chris Mason5f39d392007-10-15 16:14:19 -0400109 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400110 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500111 }
112}
113
Chris Masond352ac62008-09-29 15:18:18 -0400114/*
115 * safely gets a reference on the root node of a tree. A lock
116 * is not taken, so a concurrent writer may put a different node
117 * at the root of the tree. See btrfs_lock_root_node for the
118 * looping required.
119 *
120 * The extent buffer returned by this has a reference taken, so
121 * it won't disappear. It may stop being the root of the tree
122 * at any time because there are no locks held.
123 */
Chris Mason925baed2008-06-25 16:01:30 -0400124struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
125{
126 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400127
Josef Bacik3083ee22012-03-09 16:01:49 -0500128 while (1) {
129 rcu_read_lock();
130 eb = rcu_dereference(root->node);
131
132 /*
133 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400134 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500135 * the inc_not_zero dance and if it doesn't work then
136 * synchronize_rcu and try again.
137 */
138 if (atomic_inc_not_zero(&eb->refs)) {
139 rcu_read_unlock();
140 break;
141 }
142 rcu_read_unlock();
143 synchronize_rcu();
144 }
Chris Mason925baed2008-06-25 16:01:30 -0400145 return eb;
146}
147
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800148/*
149 * Cowonly root (not-shareable trees, everything not subvolume or reloc roots),
150 * just get put onto a simple dirty list. Transaction walks this list to make
151 * sure they get properly updated on disk.
Chris Masond352ac62008-09-29 15:18:18 -0400152 */
Chris Mason0b86a832008-03-24 15:01:56 -0400153static void add_root_to_dirty_list(struct btrfs_root *root)
154{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400155 struct btrfs_fs_info *fs_info = root->fs_info;
156
Josef Bacike7070be2014-12-16 08:54:43 -0800157 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
158 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
159 return;
160
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400161 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800162 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
163 /* Want the extent tree to be the last on the list */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900164 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
Josef Bacike7070be2014-12-16 08:54:43 -0800165 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400166 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800167 else
168 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400169 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400170 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400171 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400172}
173
Chris Masond352ac62008-09-29 15:18:18 -0400174/*
175 * used by snapshot creation to make a copy of a root for a tree with
176 * a given objectid. The buffer with the new root node is returned in
177 * cow_ret, and this func returns zero on success or a negative error code.
178 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500179int btrfs_copy_root(struct btrfs_trans_handle *trans,
180 struct btrfs_root *root,
181 struct extent_buffer *buf,
182 struct extent_buffer **cow_ret, u64 new_root_objectid)
183{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400184 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500185 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500186 int ret = 0;
187 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400188 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500189
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800190 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400191 trans->transid != fs_info->running_transaction->transid);
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800192 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Miao Xie27cdeb72014-04-02 19:51:05 +0800193 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500194
195 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400196 if (level == 0)
197 btrfs_item_key(buf, &disk_key, 0);
198 else
199 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400200
David Sterba4d75f8a2014-06-15 01:54:12 +0200201 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
Josef Bacikcf6f34a2020-08-20 11:46:07 -0400202 &disk_key, level, buf->start, 0,
203 BTRFS_NESTING_NEW_ROOT);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400204 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500205 return PTR_ERR(cow);
206
David Sterba58e80122016-11-08 18:30:31 +0100207 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500208 btrfs_set_header_bytenr(cow, cow->start);
209 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400210 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
211 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
212 BTRFS_HEADER_FLAG_RELOC);
213 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
214 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
215 else
216 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500217
Nikolay Borisovde37aa52018-10-30 16:43:24 +0200218 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -0500219
Chris Masonbe20aa92007-12-17 20:14:01 -0500220 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400221 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700222 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400223 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700224 ret = btrfs_inc_ref(trans, root, cow, 0);
Josef Bacik867ed322021-01-14 14:02:46 -0500225 if (ret) {
Filipe Manana72c99252021-02-04 14:35:44 +0000226 btrfs_tree_unlock(cow);
227 free_extent_buffer(cow);
Josef Bacik867ed322021-01-14 14:02:46 -0500228 btrfs_abort_transaction(trans, ret);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229 return ret;
Josef Bacik867ed322021-01-14 14:02:46 -0500230 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500231
232 btrfs_mark_buffer_dirty(cow);
233 *cow_ret = cow;
234 return 0;
235}
236
Chris Masond352ac62008-09-29 15:18:18 -0400237/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400238 * check if the tree block can be shared by multiple trees
239 */
240int btrfs_block_can_be_shared(struct btrfs_root *root,
241 struct extent_buffer *buf)
242{
243 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800244 * Tree blocks not in shareable trees and tree roots are never shared.
245 * If a block was allocated after the last snapshot and the block was
246 * not allocated by tree relocation, we know the block is not shared.
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400247 */
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800248 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400249 buf != root->node && buf != root->commit_root &&
250 (btrfs_header_generation(buf) <=
251 btrfs_root_last_snapshot(&root->root_item) ||
252 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
253 return 1;
Nikolay Borisova79865c2018-06-21 09:45:00 +0300254
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 return 0;
256}
257
258static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
259 struct btrfs_root *root,
260 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400261 struct extent_buffer *cow,
262 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400264 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 u64 refs;
266 u64 owner;
267 u64 flags;
268 u64 new_flags = 0;
269 int ret;
270
271 /*
272 * Backrefs update rules:
273 *
274 * Always use full backrefs for extent pointers in tree block
275 * allocated by tree relocation.
276 *
277 * If a shared tree block is no longer referenced by its owner
278 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
279 * use full backrefs for extent pointers in tree block.
280 *
281 * If a tree block is been relocating
282 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
283 * use full backrefs for extent pointers in tree block.
284 * The reason for this is some operations (such as drop tree)
285 * are only allowed for blocks use full backrefs.
286 */
287
288 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400289 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500290 btrfs_header_level(buf), 1,
291 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700292 if (ret)
293 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700294 if (refs == 0) {
295 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400296 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700297 return ret;
298 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400299 } else {
300 refs = 1;
301 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
302 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
303 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
304 else
305 flags = 0;
306 }
307
308 owner = btrfs_header_owner(buf);
309 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
310 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
311
312 if (refs > 1) {
313 if ((owner == root->root_key.objectid ||
314 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
315 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700316 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500317 if (ret)
318 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400319
320 if (root->root_key.objectid ==
321 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700322 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500323 if (ret)
324 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700325 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500326 if (ret)
327 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400328 }
329 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
330 } else {
331
332 if (root->root_key.objectid ==
333 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700334 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400335 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700336 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500337 if (ret)
338 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400339 }
340 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400341 int level = btrfs_header_level(buf);
342
David Sterba42c9d0b2019-03-20 11:54:13 +0100343 ret = btrfs_set_disk_extent_flags(trans, buf,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400344 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700345 if (ret)
346 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400347 }
348 } else {
349 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
350 if (root->root_key.objectid ==
351 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700352 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400353 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700354 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500355 if (ret)
356 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700357 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500358 if (ret)
359 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400360 }
David Sterba6a884d7d2019-03-20 14:30:02 +0100361 btrfs_clean_tree_block(buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400362 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400363 }
364 return 0;
365}
366
Filipe Mananaa6279472019-01-25 11:48:51 +0000367static struct extent_buffer *alloc_tree_block_no_bg_flush(
368 struct btrfs_trans_handle *trans,
369 struct btrfs_root *root,
370 u64 parent_start,
371 const struct btrfs_disk_key *disk_key,
372 int level,
373 u64 hint,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400374 u64 empty_size,
375 enum btrfs_lock_nesting nest)
Filipe Mananaa6279472019-01-25 11:48:51 +0000376{
377 struct btrfs_fs_info *fs_info = root->fs_info;
378 struct extent_buffer *ret;
379
380 /*
381 * If we are COWing a node/leaf from the extent, chunk, device or free
382 * space trees, make sure that we do not finish block group creation of
383 * pending block groups. We do this to avoid a deadlock.
384 * COWing can result in allocation of a new chunk, and flushing pending
385 * block groups (btrfs_create_pending_block_groups()) can be triggered
386 * when finishing allocation of a new chunk. Creation of a pending block
387 * group modifies the extent, chunk, device and free space trees,
388 * therefore we could deadlock with ourselves since we are holding a
389 * lock on an extent buffer that btrfs_create_pending_block_groups() may
390 * try to COW later.
391 * For similar reasons, we also need to delay flushing pending block
392 * groups when splitting a leaf or node, from one of those trees, since
393 * we are holding a write lock on it and its parent or when inserting a
394 * new root node for one of those trees.
395 */
396 if (root == fs_info->extent_root ||
397 root == fs_info->chunk_root ||
398 root == fs_info->dev_root ||
399 root == fs_info->free_space_root)
400 trans->can_flush_pending_bgs = false;
401
402 ret = btrfs_alloc_tree_block(trans, root, parent_start,
403 root->root_key.objectid, disk_key, level,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400404 hint, empty_size, nest);
Filipe Mananaa6279472019-01-25 11:48:51 +0000405 trans->can_flush_pending_bgs = true;
406
407 return ret;
408}
409
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400410/*
Chris Masond3977122009-01-05 21:25:51 -0500411 * does the dirty work in cow of a single block. The parent block (if
412 * supplied) is updated to point to the new cow copy. The new buffer is marked
413 * dirty and returned locked. If you modify the block it needs to be marked
414 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400415 *
416 * search_start -- an allocation hint for the new block
417 *
Chris Masond3977122009-01-05 21:25:51 -0500418 * empty_size -- a hint that you plan on doing more cow. This is the size in
419 * bytes the allocator should try to find free next to the block it returns.
420 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400421 */
Chris Masond3977122009-01-05 21:25:51 -0500422static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400423 struct btrfs_root *root,
424 struct extent_buffer *buf,
425 struct extent_buffer *parent, int parent_slot,
426 struct extent_buffer **cow_ret,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400427 u64 search_start, u64 empty_size,
428 enum btrfs_lock_nesting nest)
Chris Mason6702ed42007-08-07 16:15:09 -0400429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400430 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400431 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400432 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700433 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400434 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400435 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -0500436 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400437
Chris Mason925baed2008-06-25 16:01:30 -0400438 if (*cow_ret == buf)
439 unlock_orig = 1;
440
Chris Masonb9447ef82009-03-09 11:45:38 -0400441 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400442
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800443 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400444 trans->transid != fs_info->running_transaction->transid);
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800445 WARN_ON(test_bit(BTRFS_ROOT_SHAREABLE, &root->state) &&
Miao Xie27cdeb72014-04-02 19:51:05 +0800446 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400447
Chris Mason7bb86312007-12-11 09:25:06 -0500448 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400449
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400450 if (level == 0)
451 btrfs_item_key(buf, &disk_key, 0);
452 else
453 btrfs_node_key(buf, &disk_key, 0);
454
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -0500455 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
456 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400457
Filipe Mananaa6279472019-01-25 11:48:51 +0000458 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400459 level, search_start, empty_size, nest);
Chris Mason6702ed42007-08-07 16:15:09 -0400460 if (IS_ERR(cow))
461 return PTR_ERR(cow);
462
Chris Masonb4ce94d2009-02-04 09:25:08 -0500463 /* cow is set to blocking by btrfs_init_new_buffer */
464
David Sterba58e80122016-11-08 18:30:31 +0100465 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -0400466 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400467 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400468 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
469 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
470 BTRFS_HEADER_FLAG_RELOC);
471 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
472 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
473 else
474 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400475
Nikolay Borisovde37aa52018-10-30 16:43:24 +0200476 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -0500477
Mark Fashehbe1a5562011-08-08 13:20:18 -0700478 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700479 if (ret) {
Josef Bacik572c83a2020-09-29 08:53:54 -0400480 btrfs_tree_unlock(cow);
481 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -0400482 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700483 return ret;
484 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400485
Qu Wenruo92a7cc42020-05-15 14:01:40 +0800486 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -0400487 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +0800488 if (ret) {
Josef Bacik572c83a2020-09-29 08:53:54 -0400489 btrfs_tree_unlock(cow);
490 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -0400491 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -0400492 return ret;
Zhaolei93314e32015-08-06 21:56:58 +0800493 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -0400494 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400495
Chris Mason6702ed42007-08-07 16:15:09 -0400496 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400497 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400498 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
499 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
500 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -0400501
David Sterba67439da2019-10-08 13:28:47 +0200502 atomic_inc(&cow->refs);
Filipe Manana406808a2021-03-11 14:31:08 +0000503 ret = btrfs_tree_mod_log_insert_root(root->node, cow, true);
David Sterbad9d19a02018-03-05 16:35:29 +0100504 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -0400505 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400506
Yan, Zhengf0486c62010-05-16 10:46:25 -0400507 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200508 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400509 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400510 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400511 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400512 WARN_ON(trans->transid != btrfs_header_generation(parent));
Filipe Mananaf3a84cc2021-03-11 14:31:07 +0000513 btrfs_tree_mod_log_insert_key(parent, parent_slot,
514 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -0400515 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400516 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500517 btrfs_set_node_ptr_generation(parent, parent_slot,
518 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400519 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000520 if (last_ref) {
Filipe Mananaf3a84cc2021-03-11 14:31:07 +0000521 ret = btrfs_tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000522 if (ret) {
Josef Bacik572c83a2020-09-29 08:53:54 -0400523 btrfs_tree_unlock(cow);
524 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -0400525 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000526 return ret;
527 }
528 }
Yan, Zhengf0486c62010-05-16 10:46:25 -0400529 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200530 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400531 }
Chris Mason925baed2008-06-25 16:01:30 -0400532 if (unlock_orig)
533 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500534 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400535 btrfs_mark_buffer_dirty(cow);
536 *cow_ret = cow;
537 return 0;
538}
539
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400540static inline int should_cow_block(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root,
542 struct extent_buffer *buf)
543{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -0400544 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -0400545 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +0200546
David Sterbad1980132018-03-16 02:39:40 +0100547 /* Ensure we can see the FORCE_COW bit */
548 smp_mb__before_atomic();
Liu Bof1ebcc72011-11-14 20:48:06 -0500549
550 /*
551 * We do not need to cow a block if
552 * 1) this block is not created or changed in this transaction;
553 * 2) this block does not belong to TREE_RELOC tree;
554 * 3) the root is not forced COW.
555 *
556 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -0400557 * when we create snapshot during committing the transaction,
Andrea Gelmini52042d82018-11-28 12:05:13 +0100558 * after we've finished copying src root, we must COW the shared
Liu Bof1ebcc72011-11-14 20:48:06 -0500559 * block to ensure the metadata consistency.
560 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400561 if (btrfs_header_generation(buf) == trans->transid &&
562 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
563 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500564 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +0800565 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400566 return 0;
567 return 1;
568}
569
Chris Masond352ac62008-09-29 15:18:18 -0400570/*
571 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -0400572 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -0400573 * once per transaction, as long as it hasn't been written yet
574 */
Chris Masond3977122009-01-05 21:25:51 -0500575noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400576 struct btrfs_root *root, struct extent_buffer *buf,
577 struct extent_buffer *parent, int parent_slot,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400578 struct extent_buffer **cow_ret,
579 enum btrfs_lock_nesting nest)
Chris Mason02217ed2007-03-02 16:08:05 -0500580{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400581 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -0400582 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400583 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500584
Josef Bacik83354f02018-11-30 11:52:13 -0500585 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
586 btrfs_err(fs_info,
587 "COW'ing blocks on a fs root that's being dropped");
588
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400589 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000590 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200591 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400592 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000593
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400594 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000595 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400596 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -0500597
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -0400599 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -0500600 *cow_ret = buf;
601 return 0;
602 }
Chris Masonc4876852009-02-04 09:24:25 -0500603
Byongho Leeee221842015-12-15 01:42:10 +0900604 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500605
Qu Wenruof616f5c2019-01-23 15:15:17 +0800606 /*
607 * Before CoWing this block for later modification, check if it's
608 * the subtree root and do the delayed subtree trace if needed.
609 *
610 * Also We don't care about the error, as it's handled internally.
611 */
612 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
Chris Masonf510cfe2007-10-15 16:14:48 -0400613 ret = __btrfs_cow_block(trans, root, buf, parent,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400614 parent_slot, cow_ret, search_start, 0, nest);
liubo1abe9b82011-03-24 11:18:59 +0000615
616 trace_btrfs_cow_block(root, buf, *cow_ret);
617
Chris Masonf510cfe2007-10-15 16:14:48 -0400618 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -0400619}
Josef Bacikf75e2b72020-12-16 11:18:43 -0500620ALLOW_ERROR_INJECTION(btrfs_cow_block, ERRNO);
Chris Mason6702ed42007-08-07 16:15:09 -0400621
Chris Masond352ac62008-09-29 15:18:18 -0400622/*
623 * helper function for defrag to decide if two blocks pointed to by a
624 * node are actually close by
625 */
Chris Mason6b800532007-10-15 16:17:34 -0400626static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -0400627{
Chris Mason6b800532007-10-15 16:17:34 -0400628 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400629 return 1;
Chris Mason6b800532007-10-15 16:17:34 -0400630 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -0400631 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -0500632 return 0;
633}
634
David Sterbace6ef5a2020-06-08 16:06:07 +0200635#ifdef __LITTLE_ENDIAN
636
637/*
638 * Compare two keys, on little-endian the disk order is same as CPU order and
639 * we can avoid the conversion.
640 */
641static int comp_keys(const struct btrfs_disk_key *disk_key,
642 const struct btrfs_key *k2)
643{
644 const struct btrfs_key *k1 = (const struct btrfs_key *)disk_key;
645
646 return btrfs_comp_cpu_keys(k1, k2);
647}
648
649#else
650
Chris Mason081e9572007-11-06 10:26:24 -0500651/*
652 * compare two keys in a memcmp fashion
653 */
Omar Sandoval310712b2017-01-17 23:24:37 -0800654static int comp_keys(const struct btrfs_disk_key *disk,
655 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -0500656{
657 struct btrfs_key k1;
658
659 btrfs_disk_key_to_cpu(&k1, disk);
660
Diego Calleja20736ab2009-07-24 11:06:52 -0400661 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -0500662}
David Sterbace6ef5a2020-06-08 16:06:07 +0200663#endif
Chris Mason081e9572007-11-06 10:26:24 -0500664
Josef Bacikf3465ca2008-11-12 14:19:50 -0500665/*
666 * same as comp_keys only with two btrfs_key's
667 */
David Sterbae1f60a62019-10-01 19:57:39 +0200668int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -0500669{
670 if (k1->objectid > k2->objectid)
671 return 1;
672 if (k1->objectid < k2->objectid)
673 return -1;
674 if (k1->type > k2->type)
675 return 1;
676 if (k1->type < k2->type)
677 return -1;
678 if (k1->offset > k2->offset)
679 return 1;
680 if (k1->offset < k2->offset)
681 return -1;
682 return 0;
683}
Chris Mason081e9572007-11-06 10:26:24 -0500684
Chris Masond352ac62008-09-29 15:18:18 -0400685/*
686 * this is used by the defrag code to go through all the
687 * leaves pointed to by a node and reallocate them so that
688 * disk order is close to key order
689 */
Chris Mason6702ed42007-08-07 16:15:09 -0400690int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400691 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +0000692 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -0400693 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -0400694{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400695 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -0400696 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -0400697 u64 blocknr;
Chris Masone9d0b132007-08-10 14:06:19 -0400698 u64 search_start = *last_ret;
699 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -0400700 u64 other;
701 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -0400702 int end_slot;
703 int i;
704 int err = 0;
Chris Mason6b800532007-10-15 16:17:34 -0400705 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -0500706 int progress_passed = 0;
707 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -0400708
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400709 WARN_ON(trans->transaction != fs_info->running_transaction);
710 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -0400711
Chris Mason6b800532007-10-15 16:17:34 -0400712 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400713 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +0000714 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -0400715
Filipe Manana5dfe2be2015-02-23 19:48:52 +0000716 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -0400717 return 0;
718
Filipe Manana5dfe2be2015-02-23 19:48:52 +0000719 for (i = start_slot; i <= end_slot; i++) {
Chris Mason6702ed42007-08-07 16:15:09 -0400720 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -0400721
Chris Mason081e9572007-11-06 10:26:24 -0500722 btrfs_node_key(parent, &disk_key, i);
723 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
724 continue;
725
726 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -0400727 blocknr = btrfs_node_blockptr(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -0400728 if (last_block == 0)
729 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -0400730
Chris Mason6702ed42007-08-07 16:15:09 -0400731 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -0400732 other = btrfs_node_blockptr(parent, i - 1);
733 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400734 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +0000735 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -0400736 other = btrfs_node_blockptr(parent, i + 1);
737 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -0400738 }
Chris Masone9d0b132007-08-10 14:06:19 -0400739 if (close) {
740 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -0400741 continue;
Chris Masone9d0b132007-08-10 14:06:19 -0400742 }
Chris Mason6702ed42007-08-07 16:15:09 -0400743
Josef Bacik206983b2020-11-05 10:45:10 -0500744 cur = btrfs_read_node_slot(parent, i);
745 if (IS_ERR(cur))
746 return PTR_ERR(cur);
Chris Masone9d0b132007-08-10 14:06:19 -0400747 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -0400748 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -0400749
Chris Masone7a84562008-06-25 16:01:31 -0400750 btrfs_tree_lock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400751 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -0400752 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -0400753 min(16 * blocksize,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400754 (end_slot - i) * blocksize),
755 BTRFS_NESTING_COW);
Yan252c38f2007-08-29 09:11:44 -0400756 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -0400757 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -0400758 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400759 break;
Yan252c38f2007-08-29 09:11:44 -0400760 }
Chris Masone7a84562008-06-25 16:01:31 -0400761 search_start = cur->start;
762 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -0400763 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -0400764 btrfs_tree_unlock(cur);
765 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -0400766 }
767 return err;
768}
769
Chris Mason74123bd2007-02-02 11:05:29 -0500770/*
Chris Mason5f39d392007-10-15 16:14:19 -0400771 * search for key in the extent_buffer. The items start at offset p,
772 * and they are item_size apart. There are 'max' items in p.
773 *
Chris Mason74123bd2007-02-02 11:05:29 -0500774 * the slot in the array is returned via slot, and it points to
775 * the place where you would insert key if it is not found in
776 * the array.
777 *
778 * slot may point to max if the key is bigger than all of the keys
779 */
Chris Masone02119d2008-09-05 16:13:11 -0400780static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -0800781 unsigned long p, int item_size,
782 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -0400783 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500784{
785 int low = 0;
786 int high = max;
Chris Masonbe0e5c02007-01-26 15:51:26 -0500787 int ret;
David Sterba5cd17f32020-04-29 23:23:37 +0200788 const int key_size = sizeof(struct btrfs_disk_key);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500789
Liu Bo5e24e9a2016-06-23 16:32:45 -0700790 if (low > high) {
791 btrfs_err(eb->fs_info,
792 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
793 __func__, low, high, eb->start,
794 btrfs_header_owner(eb), btrfs_header_level(eb));
795 return -EINVAL;
796 }
797
Chris Masond3977122009-01-05 21:25:51 -0500798 while (low < high) {
David Sterba5cd17f32020-04-29 23:23:37 +0200799 unsigned long oip;
800 unsigned long offset;
801 struct btrfs_disk_key *tmp;
802 struct btrfs_disk_key unaligned;
803 int mid;
804
Chris Masonbe0e5c02007-01-26 15:51:26 -0500805 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -0400806 offset = p + mid * item_size;
David Sterba5cd17f32020-04-29 23:23:37 +0200807 oip = offset_in_page(offset);
Chris Mason5f39d392007-10-15 16:14:19 -0400808
David Sterba5cd17f32020-04-29 23:23:37 +0200809 if (oip + key_size <= PAGE_SIZE) {
Qu Wenruo884b07d2020-12-02 14:48:04 +0800810 const unsigned long idx = get_eb_page_index(offset);
David Sterba5cd17f32020-04-29 23:23:37 +0200811 char *kaddr = page_address(eb->pages[idx]);
Chris Mason934d3752008-12-08 16:43:10 -0500812
Qu Wenruo884b07d2020-12-02 14:48:04 +0800813 oip = get_eb_offset_in_page(eb, offset);
David Sterba5cd17f32020-04-29 23:23:37 +0200814 tmp = (struct btrfs_disk_key *)(kaddr + oip);
Chris Mason5f39d392007-10-15 16:14:19 -0400815 } else {
David Sterba5cd17f32020-04-29 23:23:37 +0200816 read_extent_buffer(eb, &unaligned, offset, key_size);
817 tmp = &unaligned;
Chris Mason5f39d392007-10-15 16:14:19 -0400818 }
David Sterba5cd17f32020-04-29 23:23:37 +0200819
Chris Masonbe0e5c02007-01-26 15:51:26 -0500820 ret = comp_keys(tmp, key);
821
822 if (ret < 0)
823 low = mid + 1;
824 else if (ret > 0)
825 high = mid;
826 else {
827 *slot = mid;
828 return 0;
829 }
830 }
831 *slot = low;
832 return 1;
833}
834
Chris Mason97571fd2007-02-24 13:39:08 -0500835/*
836 * simple bin_search frontend that does the right thing for
837 * leaves vs nodes
838 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +0200839int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
Qu Wenruoe3b83362020-04-17 15:08:21 +0800840 int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -0500841{
Qu Wenruoe3b83362020-04-17 15:08:21 +0800842 if (btrfs_header_level(eb) == 0)
Chris Mason5f39d392007-10-15 16:14:19 -0400843 return generic_bin_search(eb,
844 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -0400845 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -0400846 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400847 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +0800848 else
Chris Mason5f39d392007-10-15 16:14:19 -0400849 return generic_bin_search(eb,
850 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -0400851 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -0400852 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -0400853 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -0500854}
855
Yan, Zhengf0486c62010-05-16 10:46:25 -0400856static void root_add_used(struct btrfs_root *root, u32 size)
857{
858 spin_lock(&root->accounting_lock);
859 btrfs_set_root_used(&root->root_item,
860 btrfs_root_used(&root->root_item) + size);
861 spin_unlock(&root->accounting_lock);
862}
863
864static void root_sub_used(struct btrfs_root *root, u32 size)
865{
866 spin_lock(&root->accounting_lock);
867 btrfs_set_root_used(&root->root_item,
868 btrfs_root_used(&root->root_item) - size);
869 spin_unlock(&root->accounting_lock);
870}
871
Chris Masond352ac62008-09-29 15:18:18 -0400872/* given a node and slot number, this reads the blocks it points to. The
873 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -0400874 */
David Sterba4b231ae2019-08-21 19:16:27 +0200875struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
876 int slot)
Chris Masonbb803952007-03-01 12:04:21 -0500877{
Chris Masonca7a79a2008-05-12 12:59:19 -0400878 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -0400879 struct extent_buffer *eb;
Qu Wenruo581c1762018-03-29 09:08:11 +0800880 struct btrfs_key first_key;
Josef Bacik416bc652013-04-23 14:17:42 -0400881
Liu Bofb770ae2016-07-05 12:10:14 -0700882 if (slot < 0 || slot >= btrfs_header_nritems(parent))
883 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -0400884
885 BUG_ON(level == 0);
886
Qu Wenruo581c1762018-03-29 09:08:11 +0800887 btrfs_node_key_to_cpu(parent, &first_key, slot);
David Sterbad0d20b02019-03-20 14:54:01 +0100888 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
Josef Bacik1b7ec852020-11-05 10:45:18 -0500889 btrfs_header_owner(parent),
Qu Wenruo581c1762018-03-29 09:08:11 +0800890 btrfs_node_ptr_generation(parent, slot),
891 level - 1, &first_key);
Liu Bofb770ae2016-07-05 12:10:14 -0700892 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
893 free_extent_buffer(eb);
894 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -0400895 }
896
897 return eb;
Chris Masonbb803952007-03-01 12:04:21 -0500898}
899
Chris Masond352ac62008-09-29 15:18:18 -0400900/*
901 * node level balancing, used to make sure nodes are in proper order for
902 * item deletion. We balance from the top down, so we have to make sure
903 * that a deletion won't leave an node completely empty later on.
904 */
Chris Masone02119d2008-09-05 16:13:11 -0400905static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -0500906 struct btrfs_root *root,
907 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -0500908{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400909 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -0400910 struct extent_buffer *right = NULL;
911 struct extent_buffer *mid;
912 struct extent_buffer *left = NULL;
913 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -0500914 int ret = 0;
915 int wret;
916 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -0500917 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -0500918 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -0500919
Liu Bo98e6b1e2018-09-12 06:06:23 +0800920 ASSERT(level > 0);
Chris Masonbb803952007-03-01 12:04:21 -0500921
Chris Mason5f39d392007-10-15 16:14:19 -0400922 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -0500923
Josef Bacikac5887c2020-08-20 11:46:10 -0400924 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK);
Chris Mason7bb86312007-12-11 09:25:06 -0500925 WARN_ON(btrfs_header_generation(mid) != trans->transid);
926
Chris Mason1d4f8a02007-03-13 09:28:32 -0400927 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -0500928
Li Zefana05a9bb2011-09-06 16:55:34 +0800929 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400930 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +0800931 pslot = path->slots[level + 1];
932 }
Chris Masonbb803952007-03-01 12:04:21 -0500933
Chris Mason40689472007-03-17 14:29:23 -0400934 /*
935 * deal with the case where there is only one pointer in the root
936 * by promoting the node below to a root
937 */
Chris Mason5f39d392007-10-15 16:14:19 -0400938 if (!parent) {
939 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -0500940
Chris Mason5f39d392007-10-15 16:14:19 -0400941 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -0500942 return 0;
943
944 /* promote the child to a root */
David Sterba4b231ae2019-08-21 19:16:27 +0200945 child = btrfs_read_node_slot(mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -0700946 if (IS_ERR(child)) {
947 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400948 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -0700949 goto enospc;
950 }
951
Chris Mason925baed2008-06-25 16:01:30 -0400952 btrfs_tree_lock(child);
Josef Bacik9631e4c2020-08-20 11:46:03 -0400953 ret = btrfs_cow_block(trans, root, child, mid, 0, &child,
954 BTRFS_NESTING_COW);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400955 if (ret) {
956 btrfs_tree_unlock(child);
957 free_extent_buffer(child);
958 goto enospc;
959 }
Yan2f375ab2008-02-01 14:58:07 -0500960
Filipe Manana406808a2021-03-11 14:31:08 +0000961 ret = btrfs_tree_mod_log_insert_root(root->node, child, true);
David Sterbad9d19a02018-03-05 16:35:29 +0100962 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -0400963 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -0400964
Chris Mason0b86a832008-03-24 15:01:56 -0400965 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -0400966 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500967
Chris Mason925baed2008-06-25 16:01:30 -0400968 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -0500969 path->nodes[level] = NULL;
David Sterba6a884d7d2019-03-20 14:30:02 +0100970 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -0400971 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -0500972 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -0400973 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400974
975 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +0200976 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -0500977 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -0500978 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400979 return 0;
Chris Masonbb803952007-03-01 12:04:21 -0500980 }
Chris Mason5f39d392007-10-15 16:14:19 -0400981 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400982 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -0500983 return 0;
984
David Sterba4b231ae2019-08-21 19:16:27 +0200985 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -0700986 if (IS_ERR(left))
987 left = NULL;
988
Chris Mason5f39d392007-10-15 16:14:19 -0400989 if (left) {
Josef Bacikbf774672020-08-20 11:46:04 -0400990 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
Chris Mason5f39d392007-10-15 16:14:19 -0400991 wret = btrfs_cow_block(trans, root, left,
Josef Bacik9631e4c2020-08-20 11:46:03 -0400992 parent, pslot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -0400993 BTRFS_NESTING_LEFT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -0400994 if (wret) {
995 ret = wret;
996 goto enospc;
997 }
Chris Mason2cc58cf2007-08-27 16:49:44 -0400998 }
Liu Bofb770ae2016-07-05 12:10:14 -0700999
David Sterba4b231ae2019-08-21 19:16:27 +02001000 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001001 if (IS_ERR(right))
1002 right = NULL;
1003
Chris Mason5f39d392007-10-15 16:14:19 -04001004 if (right) {
Josef Bacikbf774672020-08-20 11:46:04 -04001005 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
Chris Mason5f39d392007-10-15 16:14:19 -04001006 wret = btrfs_cow_block(trans, root, right,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001007 parent, pslot + 1, &right,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001008 BTRFS_NESTING_RIGHT_COW);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001009 if (wret) {
1010 ret = wret;
1011 goto enospc;
1012 }
1013 }
1014
1015 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001016 if (left) {
1017 orig_slot += btrfs_header_nritems(left);
David Sterbad30a6682019-03-20 14:16:45 +01001018 wret = push_node_left(trans, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001019 if (wret < 0)
1020 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001021 }
Chris Mason79f95c82007-03-01 15:16:26 -05001022
1023 /*
1024 * then try to empty the right most buffer into the middle
1025 */
Chris Mason5f39d392007-10-15 16:14:19 -04001026 if (right) {
David Sterbad30a6682019-03-20 14:16:45 +01001027 wret = push_node_left(trans, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001028 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001029 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001030 if (btrfs_header_nritems(right) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001031 btrfs_clean_tree_block(right);
Chris Mason925baed2008-06-25 16:01:30 -04001032 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001033 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001034 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001035 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001036 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001037 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001038 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001039 struct btrfs_disk_key right_key;
1040 btrfs_node_key(right, &right_key, 0);
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00001041 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1042 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
David Sterba0e82bcf2018-03-05 16:16:54 +01001043 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001044 btrfs_set_node_key(parent, &right_key, pslot + 1);
1045 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001046 }
1047 }
Chris Mason5f39d392007-10-15 16:14:19 -04001048 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001049 /*
1050 * we're not allowed to leave a node with one item in the
1051 * tree during a delete. A deletion from lower in the tree
1052 * could try to delete the only pointer in this node.
1053 * So, pull some keys from the left.
1054 * There has to be a left pointer at this point because
1055 * otherwise we would have pulled some pointers from the
1056 * right
1057 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001058 if (!left) {
1059 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001060 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001061 goto enospc;
1062 }
David Sterba55d32ed2019-03-20 14:18:06 +01001063 wret = balance_node_right(trans, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001064 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001065 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001066 goto enospc;
1067 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001068 if (wret == 1) {
David Sterbad30a6682019-03-20 14:16:45 +01001069 wret = push_node_left(trans, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04001070 if (wret < 0)
1071 ret = wret;
1072 }
Chris Mason79f95c82007-03-01 15:16:26 -05001073 BUG_ON(wret == 1);
1074 }
Chris Mason5f39d392007-10-15 16:14:19 -04001075 if (btrfs_header_nritems(mid) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001076 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001077 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001078 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001079 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001080 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001081 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001082 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001083 } else {
1084 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001085 struct btrfs_disk_key mid_key;
1086 btrfs_node_key(mid, &mid_key, 0);
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00001087 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1088 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
David Sterba0e82bcf2018-03-05 16:16:54 +01001089 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001090 btrfs_set_node_key(parent, &mid_key, pslot);
1091 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001092 }
Chris Masonbb803952007-03-01 12:04:21 -05001093
Chris Mason79f95c82007-03-01 15:16:26 -05001094 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001095 if (left) {
1096 if (btrfs_header_nritems(left) > orig_slot) {
David Sterba67439da2019-10-08 13:28:47 +02001097 atomic_inc(&left->refs);
Chris Mason925baed2008-06-25 16:01:30 -04001098 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001099 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001100 path->slots[level + 1] -= 1;
1101 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001102 if (mid) {
1103 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001104 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001105 }
Chris Masonbb803952007-03-01 12:04:21 -05001106 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001107 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001108 path->slots[level] = orig_slot;
1109 }
1110 }
Chris Mason79f95c82007-03-01 15:16:26 -05001111 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001112 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001113 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001114 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001115enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001116 if (right) {
1117 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001118 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001119 }
1120 if (left) {
1121 if (path->nodes[level] != left)
1122 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001123 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001124 }
Chris Masonbb803952007-03-01 12:04:21 -05001125 return ret;
1126}
1127
Chris Masond352ac62008-09-29 15:18:18 -04001128/* Node balancing for insertion. Here we only split or push nodes around
1129 * when they are completely full. This is also done top down, so we
1130 * have to be pessimistic.
1131 */
Chris Masond3977122009-01-05 21:25:51 -05001132static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001133 struct btrfs_root *root,
1134 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001135{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001136 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001137 struct extent_buffer *right = NULL;
1138 struct extent_buffer *mid;
1139 struct extent_buffer *left = NULL;
1140 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001141 int ret = 0;
1142 int wret;
1143 int pslot;
1144 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001145
1146 if (level == 0)
1147 return 1;
1148
Chris Mason5f39d392007-10-15 16:14:19 -04001149 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001150 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001151
Li Zefana05a9bb2011-09-06 16:55:34 +08001152 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001153 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001154 pslot = path->slots[level + 1];
1155 }
Chris Masone66f7092007-04-20 13:16:02 -04001156
Chris Mason5f39d392007-10-15 16:14:19 -04001157 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001158 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001159
David Sterba4b231ae2019-08-21 19:16:27 +02001160 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001161 if (IS_ERR(left))
1162 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001163
1164 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001165 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001166 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001167
Josef Bacikbf774672020-08-20 11:46:04 -04001168 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001169
Chris Mason5f39d392007-10-15 16:14:19 -04001170 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001171 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001172 wret = 1;
1173 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001174 ret = btrfs_cow_block(trans, root, left, parent,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001175 pslot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001176 BTRFS_NESTING_LEFT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04001177 if (ret)
1178 wret = 1;
1179 else {
David Sterbad30a6682019-03-20 14:16:45 +01001180 wret = push_node_left(trans, left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001181 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001182 }
Chris Masone66f7092007-04-20 13:16:02 -04001183 if (wret < 0)
1184 ret = wret;
1185 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001187 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001188 btrfs_node_key(mid, &disk_key, 0);
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00001189 ret = btrfs_tree_mod_log_insert_key(parent, pslot,
1190 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
David Sterba0e82bcf2018-03-05 16:16:54 +01001191 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001192 btrfs_set_node_key(parent, &disk_key, pslot);
1193 btrfs_mark_buffer_dirty(parent);
1194 if (btrfs_header_nritems(left) > orig_slot) {
1195 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001196 path->slots[level + 1] -= 1;
1197 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001198 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001199 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001200 } else {
1201 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001202 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001203 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001204 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001205 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001206 }
Chris Masone66f7092007-04-20 13:16:02 -04001207 return 0;
1208 }
Chris Mason925baed2008-06-25 16:01:30 -04001209 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001210 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001211 }
David Sterba4b231ae2019-08-21 19:16:27 +02001212 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001213 if (IS_ERR(right))
1214 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001215
1216 /*
1217 * then try to empty the right most buffer into the middle
1218 */
Chris Mason5f39d392007-10-15 16:14:19 -04001219 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001220 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001221
Josef Bacikbf774672020-08-20 11:46:04 -04001222 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001223
Chris Mason5f39d392007-10-15 16:14:19 -04001224 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001225 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001226 wret = 1;
1227 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001228 ret = btrfs_cow_block(trans, root, right,
1229 parent, pslot + 1,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04001230 &right, BTRFS_NESTING_RIGHT_COW);
Chris Mason54aa1f42007-06-22 14:16:25 -04001231 if (ret)
1232 wret = 1;
1233 else {
David Sterba55d32ed2019-03-20 14:18:06 +01001234 wret = balance_node_right(trans, right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001235 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001236 }
Chris Masone66f7092007-04-20 13:16:02 -04001237 if (wret < 0)
1238 ret = wret;
1239 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001240 struct btrfs_disk_key disk_key;
1241
1242 btrfs_node_key(right, &disk_key, 0);
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00001243 ret = btrfs_tree_mod_log_insert_key(parent, pslot + 1,
1244 BTRFS_MOD_LOG_KEY_REPLACE, GFP_NOFS);
David Sterba0e82bcf2018-03-05 16:16:54 +01001245 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001246 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1247 btrfs_mark_buffer_dirty(parent);
1248
1249 if (btrfs_header_nritems(mid) <= orig_slot) {
1250 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001251 path->slots[level + 1] += 1;
1252 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001253 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001254 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001255 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001256 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001257 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001258 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001259 }
Chris Masone66f7092007-04-20 13:16:02 -04001260 return 0;
1261 }
Chris Mason925baed2008-06-25 16:01:30 -04001262 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001263 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001264 }
Chris Masone66f7092007-04-20 13:16:02 -04001265 return 1;
1266}
1267
Chris Mason74123bd2007-02-02 11:05:29 -05001268/*
Chris Masond352ac62008-09-29 15:18:18 -04001269 * readahead one full node of leaves, finding things that are close
1270 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001271 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001272static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04001273 struct btrfs_path *path,
1274 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001275{
Chris Mason5f39d392007-10-15 16:14:19 -04001276 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001277 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001278 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001279 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001280 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001281 u64 nread = 0;
Filipe Mananaace75062021-03-31 11:56:21 +01001282 u64 nread_max;
Chris Mason5f39d392007-10-15 16:14:19 -04001283 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001284 u32 nr;
1285 u32 blocksize;
1286 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001287
Filipe Mananaace75062021-03-31 11:56:21 +01001288 if (level != 1 && path->reada != READA_FORWARD_ALWAYS)
Chris Mason3c69fae2007-08-07 15:52:22 -04001289 return;
1290
Chris Mason6702ed42007-08-07 16:15:09 -04001291 if (!path->nodes[level])
1292 return;
1293
Chris Mason5f39d392007-10-15 16:14:19 -04001294 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001295
Filipe Mananaace75062021-03-31 11:56:21 +01001296 /*
1297 * Since the time between visiting leaves is much shorter than the time
1298 * between visiting nodes, limit read ahead of nodes to 1, to avoid too
1299 * much IO at once (possibly random).
1300 */
1301 if (path->reada == READA_FORWARD_ALWAYS) {
1302 if (level > 1)
1303 nread_max = node->fs_info->nodesize;
1304 else
1305 nread_max = SZ_128K;
1306 } else {
1307 nread_max = SZ_64K;
1308 }
1309
Chris Mason3c69fae2007-08-07 15:52:22 -04001310 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001311 blocksize = fs_info->nodesize;
1312 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04001313 if (eb) {
1314 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001315 return;
1316 }
1317
Chris Masona7175312009-01-22 09:23:10 -05001318 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001319
Chris Mason5f39d392007-10-15 16:14:19 -04001320 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001321 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001322
Chris Masond3977122009-01-05 21:25:51 -05001323 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01001324 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04001325 if (nr == 0)
1326 break;
1327 nr--;
Filipe Mananaace75062021-03-31 11:56:21 +01001328 } else if (path->reada == READA_FORWARD ||
1329 path->reada == READA_FORWARD_ALWAYS) {
Chris Mason6b800532007-10-15 16:17:34 -04001330 nr++;
1331 if (nr >= nritems)
1332 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001333 }
David Sterbae4058b52015-11-27 16:31:35 +01001334 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05001335 btrfs_node_key(node, &disk_key, nr);
1336 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1337 break;
1338 }
Chris Mason6b800532007-10-15 16:17:34 -04001339 search = btrfs_node_blockptr(node, nr);
Filipe Mananaace75062021-03-31 11:56:21 +01001340 if (path->reada == READA_FORWARD_ALWAYS ||
1341 (search <= target && target - search <= 65536) ||
Chris Masona7175312009-01-22 09:23:10 -05001342 (search > target && search - target <= 65536)) {
Josef Bacikbfb484d2020-11-05 10:45:09 -05001343 btrfs_readahead_node_child(node, nr);
Chris Mason6b800532007-10-15 16:17:34 -04001344 nread += blocksize;
1345 }
1346 nscan++;
Filipe Mananaace75062021-03-31 11:56:21 +01001347 if (nread > nread_max || nscan > 32)
Chris Mason6b800532007-10-15 16:17:34 -04001348 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001349 }
1350}
Chris Mason925baed2008-06-25 16:01:30 -04001351
Josef Bacikbfb484d2020-11-05 10:45:09 -05001352static noinline void reada_for_balance(struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001353{
Josef Bacikbfb484d2020-11-05 10:45:09 -05001354 struct extent_buffer *parent;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001355 int slot;
1356 int nritems;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001357
Chris Mason8c594ea2009-04-20 15:50:10 -04001358 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001359 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04001360 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001361
1362 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001363 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001364
Josef Bacikbfb484d2020-11-05 10:45:09 -05001365 if (slot > 0)
1366 btrfs_readahead_node_child(parent, slot - 1);
1367 if (slot + 1 < nritems)
1368 btrfs_readahead_node_child(parent, slot + 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001369}
1370
1371
1372/*
Chris Masond3977122009-01-05 21:25:51 -05001373 * when we walk down the tree, it is usually safe to unlock the higher layers
1374 * in the tree. The exceptions are when our path goes through slot 0, because
1375 * operations on the tree might require changing key pointers higher up in the
1376 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001377 *
Chris Masond3977122009-01-05 21:25:51 -05001378 * callers might also have set path->keep_locks, which tells this code to keep
1379 * the lock if the path points to the last slot in the block. This is part of
1380 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001381 *
Chris Masond3977122009-01-05 21:25:51 -05001382 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1383 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001384 */
Chris Masone02119d2008-09-05 16:13:11 -04001385static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001386 int lowest_unlock, int min_write_lock_level,
1387 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001388{
1389 int i;
1390 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001391 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001392 struct extent_buffer *t;
1393
1394 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1395 if (!path->nodes[i])
1396 break;
1397 if (!path->locks[i])
1398 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001399 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001400 skip_level = i + 1;
1401 continue;
1402 }
Chris Mason051e1b92008-06-25 16:01:30 -04001403 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001404 u32 nritems;
1405 t = path->nodes[i];
1406 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001407 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001408 skip_level = i + 1;
1409 continue;
1410 }
1411 }
Chris Mason051e1b92008-06-25 16:01:30 -04001412 if (skip_level < i && i >= lowest_unlock)
1413 no_skips = 1;
1414
Chris Mason925baed2008-06-25 16:01:30 -04001415 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08001416 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04001417 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001418 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001419 if (write_lock_level &&
1420 i > min_write_lock_level &&
1421 i <= *write_lock_level) {
1422 *write_lock_level = i - 1;
1423 }
Chris Mason925baed2008-06-25 16:01:30 -04001424 }
1425 }
1426}
1427
Chris Mason3c69fae2007-08-07 15:52:22 -04001428/*
Chris Masonc8c42862009-04-03 10:14:18 -04001429 * helper function for btrfs_search_slot. The goal is to find a block
1430 * in cache without setting the path to blocking. If we find the block
1431 * we return zero and the path is unchanged.
1432 *
1433 * If we can't find the block, we set the path blocking and do some
1434 * reada. -EAGAIN is returned and the search must be repeated.
1435 */
1436static int
Liu Bod07b8522017-01-30 12:23:42 -08001437read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
1438 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01001439 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04001440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001441 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04001442 u64 blocknr;
1443 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04001444 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08001445 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04001446 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08001447 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04001448
Nikolay Borisov213ff4b2020-05-27 13:10:59 +03001449 blocknr = btrfs_node_blockptr(*eb_ret, slot);
1450 gen = btrfs_node_ptr_generation(*eb_ret, slot);
1451 parent_level = btrfs_header_level(*eb_ret);
1452 btrfs_node_key_to_cpu(*eb_ret, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04001453
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001454 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04001455 if (tmp) {
Filipe Mananaace75062021-03-31 11:56:21 +01001456 if (p->reada == READA_FORWARD_ALWAYS)
1457 reada_for_search(fs_info, p, level, slot, key->objectid);
1458
Chris Masonb9fab912012-05-06 07:23:47 -04001459 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04001460 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Qu Wenruo448de472019-03-12 17:10:40 +08001461 /*
1462 * Do extra check for first_key, eb can be stale due to
1463 * being cached, read from scrub, or have multiple
1464 * parents (shared tree blocks).
1465 */
David Sterbae064d5e2019-03-20 14:58:13 +01001466 if (btrfs_verify_level_key(tmp,
Qu Wenruo448de472019-03-12 17:10:40 +08001467 parent_level - 1, &first_key, gen)) {
1468 free_extent_buffer(tmp);
1469 return -EUCLEAN;
1470 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04001471 *eb_ret = tmp;
1472 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04001473 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04001474
Josef Bacikbdf7c002013-06-17 13:44:48 -04001475 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08001476 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04001477 if (!ret) {
1478 *eb_ret = tmp;
1479 return 0;
1480 }
1481 free_extent_buffer(tmp);
1482 btrfs_release_path(p);
1483 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001484 }
1485
1486 /*
1487 * reduce lock contention at high levels
1488 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001489 * we read. Don't release the lock on the current
1490 * level because we need to walk this node to figure
1491 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001492 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001493 btrfs_unlock_up_safe(p, level + 1);
Chris Mason8c594ea2009-04-20 15:50:10 -04001494
David Sterbae4058b52015-11-27 16:31:35 +01001495 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001496 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04001497
Chris Mason76a05b32009-05-14 13:24:30 -04001498 ret = -EAGAIN;
Josef Bacik1b7ec852020-11-05 10:45:18 -05001499 tmp = read_tree_block(fs_info, blocknr, root->root_key.objectid,
1500 gen, parent_level - 1, &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08001501 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04001502 /*
1503 * If the read above didn't mark this buffer up to date,
1504 * it will never end up being up to date. Set ret to EIO now
1505 * and give up so that our caller doesn't loop forever
1506 * on our EAGAINs.
1507 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08001508 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04001509 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001510 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07001511 } else {
1512 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001513 }
Liu Bo02a33072018-05-16 01:37:36 +08001514
1515 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001516 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001517}
1518
1519/*
1520 * helper function for btrfs_search_slot. This does all of the checks
1521 * for node-level blocks and does any balancing required based on
1522 * the ins_len.
1523 *
1524 * If no extra work was required, zero is returned. If we had to
1525 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1526 * start over
1527 */
1528static int
1529setup_nodes_for_search(struct btrfs_trans_handle *trans,
1530 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001531 struct extent_buffer *b, int level, int ins_len,
1532 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001533{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001534 struct btrfs_fs_info *fs_info = root->fs_info;
Nikolay Borisov95b982d2020-11-13 09:29:40 +02001535 int ret = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001536
Chris Masonc8c42862009-04-03 10:14:18 -04001537 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001538 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04001539
Chris Masonbd681512011-07-16 15:23:14 -04001540 if (*write_lock_level < level + 1) {
1541 *write_lock_level = level + 1;
1542 btrfs_release_path(p);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02001543 return -EAGAIN;
Chris Masonbd681512011-07-16 15:23:14 -04001544 }
1545
Josef Bacikbfb484d2020-11-05 10:45:09 -05001546 reada_for_balance(p, level);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02001547 ret = split_node(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04001548
Chris Masonc8c42862009-04-03 10:14:18 -04001549 b = p->nodes[level];
1550 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001551 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04001552
Chris Masonbd681512011-07-16 15:23:14 -04001553 if (*write_lock_level < level + 1) {
1554 *write_lock_level = level + 1;
1555 btrfs_release_path(p);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02001556 return -EAGAIN;
Chris Masonbd681512011-07-16 15:23:14 -04001557 }
1558
Josef Bacikbfb484d2020-11-05 10:45:09 -05001559 reada_for_balance(p, level);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02001560 ret = balance_level(trans, root, p, level);
1561 if (ret)
1562 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001563
Chris Masonc8c42862009-04-03 10:14:18 -04001564 b = p->nodes[level];
1565 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001566 btrfs_release_path(p);
Nikolay Borisov95b982d2020-11-13 09:29:40 +02001567 return -EAGAIN;
Chris Masonc8c42862009-04-03 10:14:18 -04001568 }
1569 BUG_ON(btrfs_header_nritems(b) == 1);
1570 }
Chris Masonc8c42862009-04-03 10:14:18 -04001571 return ret;
1572}
1573
David Sterba381cf652015-01-02 18:45:16 +01001574int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08001575 u64 iobjectid, u64 ioff, u8 key_type,
1576 struct btrfs_key *found_key)
1577{
1578 int ret;
1579 struct btrfs_key key;
1580 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01001581
1582 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01001583 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08001584
1585 key.type = key_type;
1586 key.objectid = iobjectid;
1587 key.offset = ioff;
1588
1589 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01001590 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08001591 return ret;
1592
1593 eb = path->nodes[0];
1594 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
1595 ret = btrfs_next_leaf(fs_root, path);
1596 if (ret)
1597 return ret;
1598 eb = path->nodes[0];
1599 }
1600
1601 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
1602 if (found_key->type != key.type ||
1603 found_key->objectid != key.objectid)
1604 return 1;
1605
1606 return 0;
1607}
1608
Liu Bo1fc28d82018-05-18 11:00:21 +08001609static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
1610 struct btrfs_path *p,
1611 int write_lock_level)
1612{
1613 struct btrfs_fs_info *fs_info = root->fs_info;
1614 struct extent_buffer *b;
1615 int root_lock;
1616 int level = 0;
1617
1618 /* We try very hard to do read locks on the root */
1619 root_lock = BTRFS_READ_LOCK;
1620
1621 if (p->search_commit_root) {
Filipe Mananabe6821f2018-12-11 10:19:45 +00001622 /*
1623 * The commit roots are read only so we always do read locks,
1624 * and we always must hold the commit_root_sem when doing
1625 * searches on them, the only exception is send where we don't
1626 * want to block transaction commits for a long time, so
1627 * we need to clone the commit root in order to avoid races
1628 * with transaction commits that create a snapshot of one of
1629 * the roots used by a send operation.
1630 */
1631 if (p->need_commit_sem) {
Liu Bo1fc28d82018-05-18 11:00:21 +08001632 down_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00001633 b = btrfs_clone_extent_buffer(root->commit_root);
Liu Bo1fc28d82018-05-18 11:00:21 +08001634 up_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00001635 if (!b)
1636 return ERR_PTR(-ENOMEM);
1637
1638 } else {
1639 b = root->commit_root;
David Sterba67439da2019-10-08 13:28:47 +02001640 atomic_inc(&b->refs);
Filipe Mananabe6821f2018-12-11 10:19:45 +00001641 }
1642 level = btrfs_header_level(b);
Liu Bof9ddfd02018-05-29 21:27:06 +08001643 /*
1644 * Ensure that all callers have set skip_locking when
1645 * p->search_commit_root = 1.
1646 */
1647 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08001648
1649 goto out;
1650 }
1651
1652 if (p->skip_locking) {
1653 b = btrfs_root_node(root);
1654 level = btrfs_header_level(b);
1655 goto out;
1656 }
1657
1658 /*
Liu Bo662c6532018-05-18 11:00:23 +08001659 * If the level is set to maximum, we can skip trying to get the read
1660 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08001661 */
Liu Bo662c6532018-05-18 11:00:23 +08001662 if (write_lock_level < BTRFS_MAX_LEVEL) {
1663 /*
1664 * We don't know the level of the root node until we actually
1665 * have it read locked
1666 */
Josef Bacik1bb96592020-11-06 16:27:33 -05001667 b = btrfs_read_lock_root_node(root);
Liu Bo662c6532018-05-18 11:00:23 +08001668 level = btrfs_header_level(b);
1669 if (level > write_lock_level)
1670 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08001671
Liu Bo662c6532018-05-18 11:00:23 +08001672 /* Whoops, must trade for write lock */
1673 btrfs_tree_read_unlock(b);
1674 free_extent_buffer(b);
1675 }
1676
Liu Bo1fc28d82018-05-18 11:00:21 +08001677 b = btrfs_lock_root_node(root);
1678 root_lock = BTRFS_WRITE_LOCK;
1679
1680 /* The level might have changed, check again */
1681 level = btrfs_header_level(b);
1682
1683out:
1684 p->nodes[level] = b;
1685 if (!p->skip_locking)
1686 p->locks[level] = root_lock;
1687 /*
1688 * Callers are responsible for dropping b's references.
1689 */
1690 return b;
1691}
1692
1693
Chris Masonc8c42862009-04-03 10:14:18 -04001694/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02001695 * btrfs_search_slot - look for a key in a tree and perform necessary
1696 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05001697 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02001698 * @trans: Handle of transaction, used when modifying the tree
1699 * @p: Holds all btree nodes along the search path
1700 * @root: The root node of the tree
1701 * @key: The key we are looking for
ethanwu9a664972020-12-01 17:25:12 +08001702 * @ins_len: Indicates purpose of search:
1703 * >0 for inserts it's size of item inserted (*)
1704 * <0 for deletions
1705 * 0 for plain searches, not modifying the tree
1706 *
1707 * (*) If size of item inserted doesn't include
1708 * sizeof(struct btrfs_item), then p->search_for_extension must
1709 * be set.
Nikolay Borisov4271ece2017-12-13 09:38:14 +02001710 * @cow: boolean should CoW operations be performed. Must always be 1
1711 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05001712 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02001713 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
1714 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
1715 *
1716 * If @key is found, 0 is returned and you can find the item in the leaf level
1717 * of the path (level 0)
1718 *
1719 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
1720 * points to the slot where it should be inserted
1721 *
1722 * If an error is encountered while searching the tree a negative error number
1723 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05001724 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001725int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1726 const struct btrfs_key *key, struct btrfs_path *p,
1727 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001728{
Chris Mason5f39d392007-10-15 16:14:19 -04001729 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001730 int slot;
1731 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04001732 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001733 int level;
Chris Mason925baed2008-06-25 16:01:30 -04001734 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04001735 /* everything at write_lock_level or lower must be write locked */
1736 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04001737 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001738 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01001739 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04001740
Chris Mason6702ed42007-08-07 16:15:09 -04001741 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04001742 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04001743 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00001744 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04001745
Chris Masonbd681512011-07-16 15:23:14 -04001746 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001747 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04001748
Chris Masonbd681512011-07-16 15:23:14 -04001749 /* when we are removing items, we might have to go up to level
1750 * two as we update tree pointers Make sure we keep write
1751 * for those levels as well
1752 */
1753 write_lock_level = 2;
1754 } else if (ins_len > 0) {
1755 /*
1756 * for inserting items, make sure we have a write lock on
1757 * level 1 so we can update keys
1758 */
1759 write_lock_level = 1;
1760 }
1761
1762 if (!cow)
1763 write_lock_level = -1;
1764
Josef Bacik09a2a8f92013-04-05 16:51:15 -04001765 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04001766 write_lock_level = BTRFS_MAX_LEVEL;
1767
Chris Masonf7c79f32012-03-19 15:54:38 -04001768 min_write_lock_level = write_lock_level;
1769
Chris Masonbb803952007-03-01 12:04:21 -05001770again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01001771 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08001772 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Filipe Mananabe6821f2018-12-11 10:19:45 +00001773 if (IS_ERR(b)) {
1774 ret = PTR_ERR(b);
1775 goto done;
1776 }
Chris Mason925baed2008-06-25 16:01:30 -04001777
Chris Masoneb60cea2007-02-02 09:18:22 -05001778 while (b) {
Qu Wenruof624d972019-09-10 15:40:17 +08001779 int dec = 0;
1780
Chris Mason5f39d392007-10-15 16:14:19 -04001781 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04001782
Chris Mason02217ed2007-03-02 16:08:05 -05001783 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02001784 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
1785
Chris Masonc8c42862009-04-03 10:14:18 -04001786 /*
1787 * if we don't really need to cow this block
1788 * then we don't want to set the path blocking,
1789 * so we test it here
1790 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04001791 if (!should_cow_block(trans, root, b)) {
1792 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04001793 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04001794 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001795
Chris Masonbd681512011-07-16 15:23:14 -04001796 /*
1797 * must have write locks on this node and the
1798 * parent
1799 */
Josef Bacik5124e002012-11-07 13:44:13 -05001800 if (level > write_lock_level ||
1801 (level + 1 > write_lock_level &&
1802 level + 1 < BTRFS_MAX_LEVEL &&
1803 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04001804 write_lock_level = level + 1;
1805 btrfs_release_path(p);
1806 goto again;
1807 }
1808
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02001809 if (last_level)
1810 err = btrfs_cow_block(trans, root, b, NULL, 0,
Josef Bacik9631e4c2020-08-20 11:46:03 -04001811 &b,
1812 BTRFS_NESTING_COW);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02001813 else
1814 err = btrfs_cow_block(trans, root, b,
1815 p->nodes[level + 1],
Josef Bacik9631e4c2020-08-20 11:46:03 -04001816 p->slots[level + 1], &b,
1817 BTRFS_NESTING_COW);
Yan Zheng33c66f42009-07-22 09:59:00 -04001818 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04001819 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001820 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04001821 }
Chris Mason02217ed2007-03-02 16:08:05 -05001822 }
Chris Mason65b51a02008-08-01 15:11:20 -04001823cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05001824 p->nodes[level] = b;
Liu Bo52398342018-08-22 05:54:37 +08001825 /*
1826 * Leave path with blocking locks to avoid massive
1827 * lock context switch, this is made on purpose.
1828 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001829
1830 /*
1831 * we have a lock on b and as long as we aren't changing
1832 * the tree, there is no way to for the items in b to change.
1833 * It is safe to drop the lock on our parent before we
1834 * go through the expensive btree search on b.
1835 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00001836 * If we're inserting or deleting (ins_len != 0), then we might
1837 * be changing slot zero, which may require changing the parent.
1838 * So, we can't drop the lock until after we know which slot
1839 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05001840 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00001841 if (!ins_len && !p->keep_locks) {
1842 int u = level + 1;
1843
1844 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
1845 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
1846 p->locks[u] = 0;
1847 }
1848 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001849
Nikolay Borisov995e9a12020-05-27 13:10:53 +03001850 /*
1851 * If btrfs_bin_search returns an exact match (prev_cmp == 0)
1852 * we can safely assume the target key will always be in slot 0
1853 * on lower levels due to the invariants BTRFS' btree provides,
1854 * namely that a btrfs_key_ptr entry always points to the
1855 * lowest key in the child node, thus we can skip searching
1856 * lower levels
1857 */
1858 if (prev_cmp == 0) {
1859 slot = 0;
1860 ret = 0;
1861 } else {
1862 ret = btrfs_bin_search(b, key, &slot);
1863 prev_cmp = ret;
1864 if (ret < 0)
1865 goto done;
1866 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05001867
Qu Wenruof624d972019-09-10 15:40:17 +08001868 if (level == 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001869 p->slots[level] = slot;
ethanwu9a664972020-12-01 17:25:12 +08001870 /*
1871 * Item key already exists. In this case, if we are
1872 * allowed to insert the item (for example, in dir_item
1873 * case, item key collision is allowed), it will be
1874 * merged with the original item. Only the item size
1875 * grows, no new btrfs item will be added. If
1876 * search_for_extension is not set, ins_len already
1877 * accounts the size btrfs_item, deduct it here so leaf
1878 * space check will be correct.
1879 */
1880 if (ret == 0 && ins_len > 0 && !p->search_for_extension) {
1881 ASSERT(ins_len >= sizeof(struct btrfs_item));
1882 ins_len -= sizeof(struct btrfs_item);
1883 }
Yan Zheng87b29b22008-12-17 10:21:48 -05001884 if (ins_len > 0 &&
David Sterbae902baa2019-03-20 14:36:46 +01001885 btrfs_leaf_free_space(b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04001886 if (write_lock_level < 1) {
1887 write_lock_level = 1;
1888 btrfs_release_path(p);
1889 goto again;
1890 }
1891
Yan Zheng33c66f42009-07-22 09:59:00 -04001892 err = split_leaf(trans, root, key,
1893 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001894
Yan Zheng33c66f42009-07-22 09:59:00 -04001895 BUG_ON(err > 0);
1896 if (err) {
1897 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04001898 goto done;
1899 }
Chris Mason5c680ed2007-02-22 11:39:13 -05001900 }
Chris Mason459931e2008-12-10 09:10:46 -05001901 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04001902 unlock_up(p, level, lowest_unlock,
Liu Bo4b6f8e92018-08-14 10:46:53 +08001903 min_write_lock_level, NULL);
Chris Mason65b51a02008-08-01 15:11:20 -04001904 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001905 }
Qu Wenruof624d972019-09-10 15:40:17 +08001906 if (ret && slot > 0) {
1907 dec = 1;
1908 slot--;
1909 }
1910 p->slots[level] = slot;
1911 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
1912 &write_lock_level);
1913 if (err == -EAGAIN)
1914 goto again;
1915 if (err) {
1916 ret = err;
1917 goto done;
1918 }
1919 b = p->nodes[level];
1920 slot = p->slots[level];
1921
1922 /*
1923 * Slot 0 is special, if we change the key we have to update
1924 * the parent pointer which means we must have a write lock on
1925 * the parent
1926 */
1927 if (slot == 0 && ins_len && write_lock_level < level + 1) {
1928 write_lock_level = level + 1;
1929 btrfs_release_path(p);
1930 goto again;
1931 }
1932
1933 unlock_up(p, level, lowest_unlock, min_write_lock_level,
1934 &write_lock_level);
1935
1936 if (level == lowest_level) {
1937 if (dec)
1938 p->slots[level]++;
1939 goto done;
1940 }
1941
1942 err = read_block_for_search(root, p, &b, level, slot, key);
1943 if (err == -EAGAIN)
1944 goto again;
1945 if (err) {
1946 ret = err;
1947 goto done;
1948 }
1949
1950 if (!p->skip_locking) {
1951 level = btrfs_header_level(b);
1952 if (level <= write_lock_level) {
Josef Bacikac5887c2020-08-20 11:46:10 -04001953 btrfs_tree_lock(b);
Qu Wenruof624d972019-09-10 15:40:17 +08001954 p->locks[level] = BTRFS_WRITE_LOCK;
1955 } else {
Josef Bacikfe596ca2020-11-06 16:27:34 -05001956 btrfs_tree_read_lock(b);
Qu Wenruof624d972019-09-10 15:40:17 +08001957 p->locks[level] = BTRFS_READ_LOCK;
1958 }
1959 p->nodes[level] = b;
1960 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001961 }
Chris Mason65b51a02008-08-01 15:11:20 -04001962 ret = 1;
1963done:
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00001964 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02001965 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04001966 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001967}
Josef Bacikf75e2b72020-12-16 11:18:43 -05001968ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001969
Chris Mason74123bd2007-02-02 11:05:29 -05001970/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001971 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
1972 * current state of the tree together with the operations recorded in the tree
1973 * modification log to search for the key in a previous version of this tree, as
1974 * denoted by the time_seq parameter.
1975 *
1976 * Naturally, there is no support for insert, delete or cow operations.
1977 *
1978 * The resulting path and return value will be set up as if we called
1979 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
1980 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001981int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001982 struct btrfs_path *p, u64 time_seq)
1983{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001984 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001985 struct extent_buffer *b;
1986 int slot;
1987 int ret;
1988 int err;
1989 int level;
1990 int lowest_unlock = 1;
1991 u8 lowest_level = 0;
1992
1993 lowest_level = p->lowest_level;
1994 WARN_ON(p->nodes[0] != NULL);
1995
1996 if (p->search_commit_root) {
1997 BUG_ON(time_seq);
1998 return btrfs_search_slot(NULL, root, key, p, 0, 0);
1999 }
2000
2001again:
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002002 b = btrfs_get_old_root(root, time_seq);
Nikolay Borisov315bed42018-09-13 11:35:10 +03002003 if (!b) {
2004 ret = -EIO;
2005 goto done;
2006 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002007 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002008 p->locks[level] = BTRFS_READ_LOCK;
2009
2010 while (b) {
Qu Wenruoabe93392019-09-10 15:40:18 +08002011 int dec = 0;
2012
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002013 level = btrfs_header_level(b);
2014 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002015
2016 /*
2017 * we have a lock on b and as long as we aren't changing
2018 * the tree, there is no way to for the items in b to change.
2019 * It is safe to drop the lock on our parent before we
2020 * go through the expensive btree search on b.
2021 */
2022 btrfs_unlock_up_safe(p, level + 1);
2023
Nikolay Borisov995e9a12020-05-27 13:10:53 +03002024 ret = btrfs_bin_search(b, key, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00002025 if (ret < 0)
2026 goto done;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002027
Qu Wenruoabe93392019-09-10 15:40:18 +08002028 if (level == 0) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002029 p->slots[level] = slot;
2030 unlock_up(p, level, lowest_unlock, 0, NULL);
2031 goto done;
2032 }
Qu Wenruoabe93392019-09-10 15:40:18 +08002033
2034 if (ret && slot > 0) {
2035 dec = 1;
2036 slot--;
2037 }
2038 p->slots[level] = slot;
2039 unlock_up(p, level, lowest_unlock, 0, NULL);
2040
2041 if (level == lowest_level) {
2042 if (dec)
2043 p->slots[level]++;
2044 goto done;
2045 }
2046
2047 err = read_block_for_search(root, p, &b, level, slot, key);
2048 if (err == -EAGAIN)
2049 goto again;
2050 if (err) {
2051 ret = err;
2052 goto done;
2053 }
2054
2055 level = btrfs_header_level(b);
Josef Bacikac5887c2020-08-20 11:46:10 -04002056 btrfs_tree_read_lock(b);
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002057 b = btrfs_tree_mod_log_rewind(fs_info, p, b, time_seq);
Qu Wenruoabe93392019-09-10 15:40:18 +08002058 if (!b) {
2059 ret = -ENOMEM;
2060 goto done;
2061 }
2062 p->locks[level] = BTRFS_READ_LOCK;
2063 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002064 }
2065 ret = 1;
2066done:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002067 if (ret < 0)
2068 btrfs_release_path(p);
2069
2070 return ret;
2071}
2072
2073/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002074 * helper to use instead of search slot if no exact match is needed but
2075 * instead the next or previous item should be returned.
2076 * When find_higher is true, the next higher item is returned, the next lower
2077 * otherwise.
2078 * When return_any and find_higher are both true, and no higher item is found,
2079 * return the next lower instead.
2080 * When return_any is true and find_higher is false, and no lower item is found,
2081 * return the next higher instead.
2082 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2083 * < 0 on error
2084 */
2085int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08002086 const struct btrfs_key *key,
2087 struct btrfs_path *p, int find_higher,
2088 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002089{
2090 int ret;
2091 struct extent_buffer *leaf;
2092
2093again:
2094 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2095 if (ret <= 0)
2096 return ret;
2097 /*
2098 * a return value of 1 means the path is at the position where the
2099 * item should be inserted. Normally this is the next bigger item,
2100 * but in case the previous item is the last in a leaf, path points
2101 * to the first free slot in the previous leaf, i.e. at an invalid
2102 * item.
2103 */
2104 leaf = p->nodes[0];
2105
2106 if (find_higher) {
2107 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2108 ret = btrfs_next_leaf(root, p);
2109 if (ret <= 0)
2110 return ret;
2111 if (!return_any)
2112 return 1;
2113 /*
2114 * no higher item found, return the next
2115 * lower instead
2116 */
2117 return_any = 0;
2118 find_higher = 0;
2119 btrfs_release_path(p);
2120 goto again;
2121 }
2122 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002123 if (p->slots[0] == 0) {
2124 ret = btrfs_prev_leaf(root, p);
2125 if (ret < 0)
2126 return ret;
2127 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00002128 leaf = p->nodes[0];
2129 if (p->slots[0] == btrfs_header_nritems(leaf))
2130 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02002131 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002132 }
Arne Jansene6793762011-09-13 11:18:10 +02002133 if (!return_any)
2134 return 1;
2135 /*
2136 * no lower item found, return the next
2137 * higher instead
2138 */
2139 return_any = 0;
2140 find_higher = 1;
2141 btrfs_release_path(p);
2142 goto again;
2143 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002144 --p->slots[0];
2145 }
2146 }
2147 return 0;
2148}
2149
2150/*
Chris Mason74123bd2007-02-02 11:05:29 -05002151 * adjust the pointers going up the tree, starting at level
2152 * making sure the right key of each node is points to 'key'.
2153 * This is used after shifting pointers to the left, so it stops
2154 * fixing up pointers when a given leaf/node is not in slot 0 of the
2155 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002156 *
Chris Mason74123bd2007-02-02 11:05:29 -05002157 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03002158static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002159 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002160{
2161 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002162 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01002163 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002164
Chris Mason234b63a2007-03-13 10:46:10 -04002165 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002166 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01002167
Chris Masoneb60cea2007-02-02 09:18:22 -05002168 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002169 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002170 t = path->nodes[i];
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002171 ret = btrfs_tree_mod_log_insert_key(t, tslot,
2172 BTRFS_MOD_LOG_KEY_REPLACE, GFP_ATOMIC);
David Sterba0e82bcf2018-03-05 16:16:54 +01002173 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002174 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002175 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002176 if (tslot != 0)
2177 break;
2178 }
2179}
2180
Chris Mason74123bd2007-02-02 11:05:29 -05002181/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002182 * update item key.
2183 *
2184 * This function isn't completely safe. It's the caller's responsibility
2185 * that the new key won't break the order
2186 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09002187void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
2188 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08002189 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002190{
2191 struct btrfs_disk_key disk_key;
2192 struct extent_buffer *eb;
2193 int slot;
2194
2195 eb = path->nodes[0];
2196 slot = path->slots[0];
2197 if (slot > 0) {
2198 btrfs_item_key(eb, &disk_key, slot - 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08002199 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
2200 btrfs_crit(fs_info,
2201 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2202 slot, btrfs_disk_key_objectid(&disk_key),
2203 btrfs_disk_key_type(&disk_key),
2204 btrfs_disk_key_offset(&disk_key),
2205 new_key->objectid, new_key->type,
2206 new_key->offset);
2207 btrfs_print_leaf(eb);
2208 BUG();
2209 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002210 }
2211 if (slot < btrfs_header_nritems(eb) - 1) {
2212 btrfs_item_key(eb, &disk_key, slot + 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08002213 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
2214 btrfs_crit(fs_info,
2215 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
2216 slot, btrfs_disk_key_objectid(&disk_key),
2217 btrfs_disk_key_type(&disk_key),
2218 btrfs_disk_key_offset(&disk_key),
2219 new_key->objectid, new_key->type,
2220 new_key->offset);
2221 btrfs_print_leaf(eb);
2222 BUG();
2223 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002224 }
2225
2226 btrfs_cpu_key_to_disk(&disk_key, new_key);
2227 btrfs_set_item_key(eb, &disk_key, slot);
2228 btrfs_mark_buffer_dirty(eb);
2229 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03002230 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002231}
2232
2233/*
Qu Wenruod16c7022020-08-19 14:35:50 +08002234 * Check key order of two sibling extent buffers.
2235 *
2236 * Return true if something is wrong.
2237 * Return false if everything is fine.
2238 *
2239 * Tree-checker only works inside one tree block, thus the following
2240 * corruption can not be detected by tree-checker:
2241 *
2242 * Leaf @left | Leaf @right
2243 * --------------------------------------------------------------
2244 * | 1 | 2 | 3 | 4 | 5 | f6 | | 7 | 8 |
2245 *
2246 * Key f6 in leaf @left itself is valid, but not valid when the next
2247 * key in leaf @right is 7.
2248 * This can only be checked at tree block merge time.
2249 * And since tree checker has ensured all key order in each tree block
2250 * is correct, we only need to bother the last key of @left and the first
2251 * key of @right.
2252 */
2253static bool check_sibling_keys(struct extent_buffer *left,
2254 struct extent_buffer *right)
2255{
2256 struct btrfs_key left_last;
2257 struct btrfs_key right_first;
2258 int level = btrfs_header_level(left);
2259 int nr_left = btrfs_header_nritems(left);
2260 int nr_right = btrfs_header_nritems(right);
2261
2262 /* No key to check in one of the tree blocks */
2263 if (!nr_left || !nr_right)
2264 return false;
2265
2266 if (level) {
2267 btrfs_node_key_to_cpu(left, &left_last, nr_left - 1);
2268 btrfs_node_key_to_cpu(right, &right_first, 0);
2269 } else {
2270 btrfs_item_key_to_cpu(left, &left_last, nr_left - 1);
2271 btrfs_item_key_to_cpu(right, &right_first, 0);
2272 }
2273
2274 if (btrfs_comp_cpu_keys(&left_last, &right_first) >= 0) {
2275 btrfs_crit(left->fs_info,
2276"bad key order, sibling blocks, left last (%llu %u %llu) right first (%llu %u %llu)",
2277 left_last.objectid, left_last.type,
2278 left_last.offset, right_first.objectid,
2279 right_first.type, right_first.offset);
2280 return true;
2281 }
2282 return false;
2283}
2284
2285/*
Chris Mason74123bd2007-02-02 11:05:29 -05002286 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002287 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002288 *
2289 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2290 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002291 */
Chris Mason98ed5172008-01-03 10:01:48 -05002292static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002293 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002294 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002295{
David Sterbad30a6682019-03-20 14:16:45 +01002296 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002297 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002298 int src_nritems;
2299 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002300 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002301
Chris Mason5f39d392007-10-15 16:14:19 -04002302 src_nritems = btrfs_header_nritems(src);
2303 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002304 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002305 WARN_ON(btrfs_header_generation(src) != trans->transid);
2306 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002307
Chris Masonbce4eae2008-04-24 14:42:46 -04002308 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002309 return 1;
2310
Chris Masond3977122009-01-05 21:25:51 -05002311 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002312 return 1;
2313
Chris Masonbce4eae2008-04-24 14:42:46 -04002314 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002315 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002316 if (push_items < src_nritems) {
2317 /* leave at least 8 pointers in the node if
2318 * we aren't going to empty it
2319 */
2320 if (src_nritems - push_items < 8) {
2321 if (push_items <= 8)
2322 return 1;
2323 push_items -= 8;
2324 }
2325 }
2326 } else
2327 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002328
Qu Wenruod16c7022020-08-19 14:35:50 +08002329 /* dst is the left eb, src is the middle eb */
2330 if (check_sibling_keys(dst, src)) {
2331 ret = -EUCLEAN;
2332 btrfs_abort_transaction(trans, ret);
2333 return ret;
2334 }
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002335 ret = btrfs_tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00002336 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002337 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00002338 return ret;
2339 }
Chris Mason5f39d392007-10-15 16:14:19 -04002340 copy_extent_buffer(dst, src,
2341 btrfs_node_key_ptr_offset(dst_nritems),
2342 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002343 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002344
Chris Masonbb803952007-03-01 12:04:21 -05002345 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02002346 /*
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002347 * Don't call btrfs_tree_mod_log_insert_move() here, key removal
2348 * was already fully logged by btrfs_tree_mod_log_eb_copy() above.
Jan Schmidt57911b82012-10-19 09:22:03 +02002349 */
Chris Mason5f39d392007-10-15 16:14:19 -04002350 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2351 btrfs_node_key_ptr_offset(push_items),
2352 (src_nritems - push_items) *
2353 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002354 }
Chris Mason5f39d392007-10-15 16:14:19 -04002355 btrfs_set_header_nritems(src, src_nritems - push_items);
2356 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2357 btrfs_mark_buffer_dirty(src);
2358 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002359
Chris Masonbb803952007-03-01 12:04:21 -05002360 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002361}
2362
Chris Mason97571fd2007-02-24 13:39:08 -05002363/*
Chris Mason79f95c82007-03-01 15:16:26 -05002364 * try to push data from one node into the next node right in the
2365 * tree.
2366 *
2367 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2368 * error, and > 0 if there was no room in the right hand block.
2369 *
2370 * this will only push up to 1/2 the contents of the left node over
2371 */
Chris Mason5f39d392007-10-15 16:14:19 -04002372static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002373 struct extent_buffer *dst,
2374 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002375{
David Sterba55d32ed2019-03-20 14:18:06 +01002376 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason79f95c82007-03-01 15:16:26 -05002377 int push_items = 0;
2378 int max_push;
2379 int src_nritems;
2380 int dst_nritems;
2381 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002382
Chris Mason7bb86312007-12-11 09:25:06 -05002383 WARN_ON(btrfs_header_generation(src) != trans->transid);
2384 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2385
Chris Mason5f39d392007-10-15 16:14:19 -04002386 src_nritems = btrfs_header_nritems(src);
2387 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002388 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002389 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002390 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002391
Chris Masond3977122009-01-05 21:25:51 -05002392 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002393 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002394
2395 max_push = src_nritems / 2 + 1;
2396 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002397 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002398 return 1;
Yan252c38f2007-08-29 09:11:44 -04002399
Chris Mason79f95c82007-03-01 15:16:26 -05002400 if (max_push < push_items)
2401 push_items = max_push;
2402
Qu Wenruod16c7022020-08-19 14:35:50 +08002403 /* dst is the right eb, src is the middle eb */
2404 if (check_sibling_keys(src, dst)) {
2405 ret = -EUCLEAN;
2406 btrfs_abort_transaction(trans, ret);
2407 return ret;
2408 }
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002409 ret = btrfs_tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
David Sterbabf1d3422018-03-05 15:47:39 +01002410 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002411 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2412 btrfs_node_key_ptr_offset(0),
2413 (dst_nritems) *
2414 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002415
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002416 ret = btrfs_tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
2417 push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00002418 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002419 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00002420 return ret;
2421 }
Chris Mason5f39d392007-10-15 16:14:19 -04002422 copy_extent_buffer(dst, src,
2423 btrfs_node_key_ptr_offset(0),
2424 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002425 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002426
Chris Mason5f39d392007-10-15 16:14:19 -04002427 btrfs_set_header_nritems(src, src_nritems - push_items);
2428 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002429
Chris Mason5f39d392007-10-15 16:14:19 -04002430 btrfs_mark_buffer_dirty(src);
2431 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002432
Chris Mason79f95c82007-03-01 15:16:26 -05002433 return ret;
2434}
2435
2436/*
Chris Mason97571fd2007-02-24 13:39:08 -05002437 * helper function to insert a new root level in the tree.
2438 * A new node is allocated, and a single item is inserted to
2439 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002440 *
2441 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002442 */
Chris Masond3977122009-01-05 21:25:51 -05002443static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002444 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00002445 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002447 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05002448 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002449 struct extent_buffer *lower;
2450 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002451 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002452 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01002453 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002454
2455 BUG_ON(path->nodes[level]);
2456 BUG_ON(path->nodes[level-1] != root->node);
2457
Chris Mason7bb86312007-12-11 09:25:06 -05002458 lower = path->nodes[level-1];
2459 if (level == 1)
2460 btrfs_item_key(lower, &lower_key, 0);
2461 else
2462 btrfs_node_key(lower, &lower_key, 0);
2463
Filipe Mananaa6279472019-01-25 11:48:51 +00002464 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
Josef Bacik9631e4c2020-08-20 11:46:03 -04002465 root->node->start, 0,
Josef Bacikcf6f34a2020-08-20 11:46:07 -04002466 BTRFS_NESTING_NEW_ROOT);
Chris Mason5f39d392007-10-15 16:14:19 -04002467 if (IS_ERR(c))
2468 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002469
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002471
Chris Mason5f39d392007-10-15 16:14:19 -04002472 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002473 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002474 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002475 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002476 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002477
2478 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002479
2480 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002481
Chris Mason925baed2008-06-25 16:01:30 -04002482 old = root->node;
Filipe Manana406808a2021-03-11 14:31:08 +00002483 ret = btrfs_tree_mod_log_insert_root(root->node, c, false);
David Sterbad9d19a02018-03-05 16:35:29 +01002484 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04002485 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002486
2487 /* the super has an extra ref to root->node */
2488 free_extent_buffer(old);
2489
Chris Mason0b86a832008-03-24 15:01:56 -04002490 add_root_to_dirty_list(root);
David Sterba67439da2019-10-08 13:28:47 +02002491 atomic_inc(&c->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04002492 path->nodes[level] = c;
Josef Bacikac5887c2020-08-20 11:46:10 -04002493 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002494 path->slots[level] = 0;
2495 return 0;
2496}
2497
Chris Mason74123bd2007-02-02 11:05:29 -05002498/*
2499 * worker function to insert a single pointer in a node.
2500 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002501 *
Chris Mason74123bd2007-02-02 11:05:29 -05002502 * slot and level indicate where you want the key to go, and
2503 * blocknr is the block the key points to.
2504 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002505static void insert_ptr(struct btrfs_trans_handle *trans,
David Sterba6ad3cf62019-03-20 14:32:45 +01002506 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002507 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02002508 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002509{
Chris Mason5f39d392007-10-15 16:14:19 -04002510 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002511 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002512 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002513
2514 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002515 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002516 lower = path->nodes[level];
2517 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002518 BUG_ON(slot > nritems);
David Sterba6ad3cf62019-03-20 14:32:45 +01002519 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05002520 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01002521 if (level) {
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002522 ret = btrfs_tree_mod_log_insert_move(lower, slot + 1,
2523 slot, nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01002524 BUG_ON(ret < 0);
2525 }
Chris Mason5f39d392007-10-15 16:14:19 -04002526 memmove_extent_buffer(lower,
2527 btrfs_node_key_ptr_offset(slot + 1),
2528 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002529 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002530 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02002531 if (level) {
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002532 ret = btrfs_tree_mod_log_insert_key(lower, slot,
2533 BTRFS_MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002534 BUG_ON(ret < 0);
2535 }
Chris Mason5f39d392007-10-15 16:14:19 -04002536 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002537 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002538 WARN_ON(trans->transid == 0);
2539 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002540 btrfs_set_header_nritems(lower, nritems + 1);
2541 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002542}
2543
Chris Mason97571fd2007-02-24 13:39:08 -05002544/*
2545 * split the node at the specified level in path in two.
2546 * The path is corrected to point to the appropriate node after the split
2547 *
2548 * Before splitting this tries to make some room in the node by pushing
2549 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002550 *
2551 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002552 */
Chris Masone02119d2008-09-05 16:13:11 -04002553static noinline int split_node(struct btrfs_trans_handle *trans,
2554 struct btrfs_root *root,
2555 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002556{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002557 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002558 struct extent_buffer *c;
2559 struct extent_buffer *split;
2560 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002561 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002562 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002563 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564
Chris Mason5f39d392007-10-15 16:14:19 -04002565 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002566 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002567 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00002568 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00002569 * trying to split the root, lets make a new one
2570 *
Liu Bofdd99c72013-05-22 12:06:51 +00002571 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00002572 * insert_new_root, because that root buffer will be kept as a
2573 * normal node. We are going to log removal of half of the
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002574 * elements below with btrfs_tree_mod_log_eb_copy(). We're
2575 * holding a tree lock on the buffer, which is why we cannot
2576 * race with other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00002577 */
Liu Bofdd99c72013-05-22 12:06:51 +00002578 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002579 if (ret)
2580 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002581 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002582 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002583 c = path->nodes[level];
2584 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002585 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002586 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002587 if (ret < 0)
2588 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589 }
Chris Masone66f7092007-04-20 13:16:02 -04002590
Chris Mason5f39d392007-10-15 16:14:19 -04002591 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002592 mid = (c_nritems + 1) / 2;
2593 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002594
Filipe Mananaa6279472019-01-25 11:48:51 +00002595 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
Josef Bacik4dff97e2020-08-20 11:46:06 -04002596 c->start, 0, BTRFS_NESTING_SPLIT);
Chris Mason5f39d392007-10-15 16:14:19 -04002597 if (IS_ERR(split))
2598 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002599
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002600 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03002601 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04002602
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00002603 ret = btrfs_tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00002604 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002605 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00002606 return ret;
2607 }
Chris Mason5f39d392007-10-15 16:14:19 -04002608 copy_extent_buffer(split, c,
2609 btrfs_node_key_ptr_offset(0),
2610 btrfs_node_key_ptr_offset(mid),
2611 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2612 btrfs_set_header_nritems(split, c_nritems - mid);
2613 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002614
Chris Mason5f39d392007-10-15 16:14:19 -04002615 btrfs_mark_buffer_dirty(c);
2616 btrfs_mark_buffer_dirty(split);
2617
David Sterba6ad3cf62019-03-20 14:32:45 +01002618 insert_ptr(trans, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02002619 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002620
Chris Mason5de08d72007-02-24 06:24:44 -05002621 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002622 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002623 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002624 free_extent_buffer(c);
2625 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002626 path->slots[level + 1] += 1;
2627 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002628 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002629 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002630 }
Nikolay Borisovd5286a922020-11-12 13:24:02 +02002631 return 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002632}
2633
Chris Mason74123bd2007-02-02 11:05:29 -05002634/*
2635 * how many bytes are required to store the items in a leaf. start
2636 * and nr indicate which items in the leaf to check. This totals up the
2637 * space used both by the item structs and the item data
2638 */
Chris Mason5f39d392007-10-15 16:14:19 -04002639static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002640{
Josef Bacik41be1f32012-10-15 13:43:18 -04002641 struct btrfs_item *start_item;
2642 struct btrfs_item *end_item;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002643 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002644 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002645 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002646
2647 if (!nr)
2648 return 0;
Ross Kirkdd3cc162013-09-16 15:58:09 +01002649 start_item = btrfs_item_nr(start);
2650 end_item = btrfs_item_nr(end);
David Sterbaa31356b2020-04-29 22:56:01 +02002651 data_len = btrfs_item_offset(l, start_item) +
2652 btrfs_item_size(l, start_item);
2653 data_len = data_len - btrfs_item_offset(l, end_item);
Chris Mason0783fcf2007-03-12 20:12:07 -04002654 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002655 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002656 return data_len;
2657}
2658
Chris Mason74123bd2007-02-02 11:05:29 -05002659/*
Chris Masond4dbff92007-04-04 14:08:15 -04002660 * The space between the end of the leaf items and
2661 * the start of the leaf data. IOW, how much room
2662 * the leaf has left for both items and data
2663 */
David Sterbae902baa2019-03-20 14:36:46 +01002664noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002665{
David Sterbae902baa2019-03-20 14:36:46 +01002666 struct btrfs_fs_info *fs_info = leaf->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002667 int nritems = btrfs_header_nritems(leaf);
2668 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002669
2670 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002672 btrfs_crit(fs_info,
2673 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
2674 ret,
2675 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
2676 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 }
2678 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002679}
2680
Chris Mason99d8f832010-07-07 10:51:48 -04002681/*
2682 * min slot controls the lowest index we're willing to push to the
2683 * right. We'll push up to and including min_slot, but no lower
2684 */
David Sterbaf72f0012019-03-20 14:39:45 +01002685static noinline int __push_leaf_right(struct btrfs_path *path,
Chris Mason44871b12009-03-13 10:04:31 -04002686 int data_size, int empty,
2687 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002688 int free_space, u32 left_nritems,
2689 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002690{
David Sterbaf72f0012019-03-20 14:39:45 +01002691 struct btrfs_fs_info *fs_info = right->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002692 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002693 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002694 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002695 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002696 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002697 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002698 int push_space = 0;
2699 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002700 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002701 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002702 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002703 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002704 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002705
Chris Mason34a38212007-11-07 13:31:03 -05002706 if (empty)
2707 nr = 0;
2708 else
Chris Mason99d8f832010-07-07 10:51:48 -04002709 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002710
Zheng Yan31840ae2008-09-23 13:14:14 -04002711 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002712 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002713
Chris Mason44871b12009-03-13 10:04:31 -04002714 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002715 i = left_nritems - 1;
2716 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01002717 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04002718
Zheng Yan31840ae2008-09-23 13:14:14 -04002719 if (!empty && push_items > 0) {
2720 if (path->slots[0] > i)
2721 break;
2722 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01002723 int space = btrfs_leaf_free_space(left);
2724
Zheng Yan31840ae2008-09-23 13:14:14 -04002725 if (space + push_space * 2 > free_space)
2726 break;
2727 }
2728 }
2729
Chris Mason00ec4c52007-02-24 12:47:20 -05002730 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002731 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002732
Chris Masondb945352007-10-15 16:15:53 -04002733 this_item_size = btrfs_item_size(left, item);
2734 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002735 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002736
Chris Mason00ec4c52007-02-24 12:47:20 -05002737 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002738 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002739 if (i == 0)
2740 break;
2741 i--;
Chris Masondb945352007-10-15 16:15:53 -04002742 }
Chris Mason5f39d392007-10-15 16:14:19 -04002743
Chris Mason925baed2008-06-25 16:01:30 -04002744 if (push_items == 0)
2745 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002746
Julia Lawall6c1500f2012-11-03 20:30:18 +00002747 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002748
Chris Mason00ec4c52007-02-24 12:47:20 -05002749 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002750 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002751
Chris Mason5f39d392007-10-15 16:14:19 -04002752 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
David Sterba8f881e82019-03-20 11:33:10 +01002753 push_space -= leaf_data_end(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002754
Chris Mason00ec4c52007-02-24 12:47:20 -05002755 /* make room in the right data area */
David Sterba8f881e82019-03-20 11:33:10 +01002756 data_end = leaf_data_end(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002757 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03002758 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
2759 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002760 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04002761
Chris Mason00ec4c52007-02-24 12:47:20 -05002762 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03002763 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002764 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
David Sterba8f881e82019-03-20 11:33:10 +01002765 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
Chris Masond6025572007-03-30 14:27:56 -04002766 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002767
2768 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2769 btrfs_item_nr_offset(0),
2770 right_nritems * sizeof(struct btrfs_item));
2771
Chris Mason00ec4c52007-02-24 12:47:20 -05002772 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002773 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2774 btrfs_item_nr_offset(left_nritems - push_items),
2775 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002776
2777 /* update the item pointers */
David Sterbac82f8232019-08-09 17:48:21 +02002778 btrfs_init_map_token(&token, right);
Chris Mason7518a232007-03-12 12:01:18 -04002779 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002780 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002781 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04002782 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01002783 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02002784 push_space -= btrfs_token_item_size(&token, item);
2785 btrfs_set_token_item_offset(&token, item, push_space);
Chris Masondb945352007-10-15 16:15:53 -04002786 }
2787
Chris Mason7518a232007-03-12 12:01:18 -04002788 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002789 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002790
Chris Mason34a38212007-11-07 13:31:03 -05002791 if (left_nritems)
2792 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002793 else
David Sterba6a884d7d2019-03-20 14:30:02 +01002794 btrfs_clean_tree_block(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002795
Chris Mason5f39d392007-10-15 16:14:19 -04002796 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002797
Chris Mason5f39d392007-10-15 16:14:19 -04002798 btrfs_item_key(right, &disk_key, 0);
2799 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002800 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002801
Chris Mason00ec4c52007-02-24 12:47:20 -05002802 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002803 if (path->slots[0] >= left_nritems) {
2804 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002805 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba6a884d7d2019-03-20 14:30:02 +01002806 btrfs_clean_tree_block(path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04002807 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002808 free_extent_buffer(path->nodes[0]);
2809 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002810 path->slots[1] += 1;
2811 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002812 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002813 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002814 }
2815 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002816
2817out_unlock:
2818 btrfs_tree_unlock(right);
2819 free_extent_buffer(right);
2820 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002821}
Chris Mason925baed2008-06-25 16:01:30 -04002822
Chris Mason00ec4c52007-02-24 12:47:20 -05002823/*
Chris Mason44871b12009-03-13 10:04:31 -04002824 * push some data in the path leaf to the right, trying to free up at
2825 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2826 *
2827 * returns 1 if the push failed because the other node didn't have enough
2828 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002829 *
2830 * this will push starting from min_slot to the end of the leaf. It won't
2831 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002832 */
2833static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002834 *root, struct btrfs_path *path,
2835 int min_data_size, int data_size,
2836 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002837{
2838 struct extent_buffer *left = path->nodes[0];
2839 struct extent_buffer *right;
2840 struct extent_buffer *upper;
2841 int slot;
2842 int free_space;
2843 u32 left_nritems;
2844 int ret;
2845
2846 if (!path->nodes[1])
2847 return 1;
2848
2849 slot = path->slots[1];
2850 upper = path->nodes[1];
2851 if (slot >= btrfs_header_nritems(upper) - 1)
2852 return 1;
2853
2854 btrfs_assert_tree_locked(path->nodes[1]);
2855
David Sterba4b231ae2019-08-21 19:16:27 +02002856 right = btrfs_read_node_slot(upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002857 /*
2858 * slot + 1 is not valid or we fail to read the right node,
2859 * no big deal, just return.
2860 */
2861 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002862 return 1;
2863
Josef Bacikbf774672020-08-20 11:46:04 -04002864 __btrfs_tree_lock(right, BTRFS_NESTING_RIGHT);
Chris Mason44871b12009-03-13 10:04:31 -04002865
David Sterbae902baa2019-03-20 14:36:46 +01002866 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04002867 if (free_space < data_size)
2868 goto out_unlock;
2869
2870 /* cow and double check */
2871 ret = btrfs_cow_block(trans, root, right, upper,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04002872 slot + 1, &right, BTRFS_NESTING_RIGHT_COW);
Chris Mason44871b12009-03-13 10:04:31 -04002873 if (ret)
2874 goto out_unlock;
2875
David Sterbae902baa2019-03-20 14:36:46 +01002876 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04002877 if (free_space < data_size)
2878 goto out_unlock;
2879
2880 left_nritems = btrfs_header_nritems(left);
2881 if (left_nritems == 0)
2882 goto out_unlock;
2883
Qu Wenruod16c7022020-08-19 14:35:50 +08002884 if (check_sibling_keys(left, right)) {
2885 ret = -EUCLEAN;
2886 btrfs_tree_unlock(right);
2887 free_extent_buffer(right);
2888 return ret;
2889 }
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00002890 if (path->slots[0] == left_nritems && !empty) {
2891 /* Key greater than all keys in the leaf, right neighbor has
2892 * enough room for it and we're not emptying our leaf to delete
2893 * it, therefore use right neighbor to insert the new item and
Andrea Gelmini52042d82018-11-28 12:05:13 +01002894 * no need to touch/dirty our left leaf. */
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00002895 btrfs_tree_unlock(left);
2896 free_extent_buffer(left);
2897 path->nodes[0] = right;
2898 path->slots[0] = 0;
2899 path->slots[1]++;
2900 return 0;
2901 }
2902
David Sterbaf72f0012019-03-20 14:39:45 +01002903 return __push_leaf_right(path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04002904 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002905out_unlock:
2906 btrfs_tree_unlock(right);
2907 free_extent_buffer(right);
2908 return 1;
2909}
2910
2911/*
Chris Mason74123bd2007-02-02 11:05:29 -05002912 * push some data in the path leaf to the left, trying to free up at
2913 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002914 *
2915 * max_slot can put a limit on how far into the leaf we'll push items. The
2916 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2917 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002918 */
David Sterba8087c192019-03-20 14:40:41 +01002919static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
Chris Mason44871b12009-03-13 10:04:31 -04002920 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002921 int free_space, u32 right_nritems,
2922 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002923{
David Sterba8087c192019-03-20 14:40:41 +01002924 struct btrfs_fs_info *fs_info = left->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002925 struct btrfs_disk_key disk_key;
2926 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002927 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002928 int push_space = 0;
2929 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002930 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002931 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002932 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002933 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002934 u32 this_item_size;
2935 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002936 struct btrfs_map_token token;
2937
Chris Mason34a38212007-11-07 13:31:03 -05002938 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002939 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002940 else
Chris Mason99d8f832010-07-07 10:51:48 -04002941 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002942
2943 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01002944 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04002945
Zheng Yan31840ae2008-09-23 13:14:14 -04002946 if (!empty && push_items > 0) {
2947 if (path->slots[0] < i)
2948 break;
2949 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01002950 int space = btrfs_leaf_free_space(right);
2951
Zheng Yan31840ae2008-09-23 13:14:14 -04002952 if (space + push_space * 2 > free_space)
2953 break;
2954 }
2955 }
2956
Chris Masonbe0e5c02007-01-26 15:51:26 -05002957 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002958 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002959
2960 this_item_size = btrfs_item_size(right, item);
2961 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002962 break;
Chris Masondb945352007-10-15 16:15:53 -04002963
Chris Masonbe0e5c02007-01-26 15:51:26 -05002964 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002965 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002966 }
Chris Masondb945352007-10-15 16:15:53 -04002967
Chris Masonbe0e5c02007-01-26 15:51:26 -05002968 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002969 ret = 1;
2970 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002971 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05302972 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04002973
Chris Masonbe0e5c02007-01-26 15:51:26 -05002974 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04002975 copy_extent_buffer(left, right,
2976 btrfs_item_nr_offset(btrfs_header_nritems(left)),
2977 btrfs_item_nr_offset(0),
2978 push_items * sizeof(struct btrfs_item));
2979
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002980 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05002981 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002982
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03002983 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01002984 leaf_data_end(left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03002985 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04002986 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04002987 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002988 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05002989 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05002990
David Sterbac82f8232019-08-09 17:48:21 +02002991 btrfs_init_map_token(&token, left);
Chris Masondb945352007-10-15 16:15:53 -04002992 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04002993 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002994 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04002995
Ross Kirkdd3cc162013-09-16 15:58:09 +01002996 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04002997
David Sterbacc4c13d2020-04-29 02:15:56 +02002998 ioff = btrfs_token_item_offset(&token, item);
2999 btrfs_set_token_item_offset(&token, item,
3000 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003001 }
Chris Mason5f39d392007-10-15 16:14:19 -04003002 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003003
3004 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003005 if (push_items > right_nritems)
3006 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003007 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003008
Chris Mason34a38212007-11-07 13:31:03 -05003009 if (push_items < right_nritems) {
3010 push_space = btrfs_item_offset_nr(right, push_items - 1) -
David Sterba8f881e82019-03-20 11:33:10 +01003011 leaf_data_end(right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003012 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003013 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003014 BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003015 leaf_data_end(right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003016
3017 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003018 btrfs_item_nr_offset(push_items),
3019 (btrfs_header_nritems(right) - push_items) *
3020 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003021 }
David Sterbac82f8232019-08-09 17:48:21 +02003022
3023 btrfs_init_map_token(&token, right);
Yaneef1c492007-11-26 10:58:13 -05003024 right_nritems -= push_items;
3025 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003026 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003027 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003028 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003029
David Sterbacc4c13d2020-04-29 02:15:56 +02003030 push_space = push_space - btrfs_token_item_size(&token, item);
3031 btrfs_set_token_item_offset(&token, item, push_space);
Chris Masondb945352007-10-15 16:15:53 -04003032 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003033
Chris Mason5f39d392007-10-15 16:14:19 -04003034 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003035 if (right_nritems)
3036 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003037 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003038 btrfs_clean_tree_block(right);
Chris Mason098f59c2007-05-11 11:33:21 -04003039
Chris Mason5f39d392007-10-15 16:14:19 -04003040 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003041 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003042
3043 /* then fixup the leaf pointer in the path */
3044 if (path->slots[0] < push_items) {
3045 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003046 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003047 free_extent_buffer(path->nodes[0]);
3048 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003049 path->slots[1] -= 1;
3050 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003051 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003052 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003053 path->slots[0] -= push_items;
3054 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003055 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003056 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003057out:
3058 btrfs_tree_unlock(left);
3059 free_extent_buffer(left);
3060 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003061}
3062
Chris Mason74123bd2007-02-02 11:05:29 -05003063/*
Chris Mason44871b12009-03-13 10:04:31 -04003064 * push some data in the path leaf to the left, trying to free up at
3065 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003066 *
3067 * max_slot can put a limit on how far into the leaf we'll push items. The
3068 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3069 * items
Chris Mason44871b12009-03-13 10:04:31 -04003070 */
3071static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003072 *root, struct btrfs_path *path, int min_data_size,
3073 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003074{
3075 struct extent_buffer *right = path->nodes[0];
3076 struct extent_buffer *left;
3077 int slot;
3078 int free_space;
3079 u32 right_nritems;
3080 int ret = 0;
3081
3082 slot = path->slots[1];
3083 if (slot == 0)
3084 return 1;
3085 if (!path->nodes[1])
3086 return 1;
3087
3088 right_nritems = btrfs_header_nritems(right);
3089 if (right_nritems == 0)
3090 return 1;
3091
3092 btrfs_assert_tree_locked(path->nodes[1]);
3093
David Sterba4b231ae2019-08-21 19:16:27 +02003094 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003095 /*
3096 * slot - 1 is not valid or we fail to read the left node,
3097 * no big deal, just return.
3098 */
3099 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003100 return 1;
3101
Josef Bacikbf774672020-08-20 11:46:04 -04003102 __btrfs_tree_lock(left, BTRFS_NESTING_LEFT);
Chris Mason44871b12009-03-13 10:04:31 -04003103
David Sterbae902baa2019-03-20 14:36:46 +01003104 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04003105 if (free_space < data_size) {
3106 ret = 1;
3107 goto out;
3108 }
3109
3110 /* cow and double check */
3111 ret = btrfs_cow_block(trans, root, left,
Josef Bacik9631e4c2020-08-20 11:46:03 -04003112 path->nodes[1], slot - 1, &left,
Josef Bacikbf59a5a2020-08-20 11:46:05 -04003113 BTRFS_NESTING_LEFT_COW);
Chris Mason44871b12009-03-13 10:04:31 -04003114 if (ret) {
3115 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003116 if (ret == -ENOSPC)
3117 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003118 goto out;
3119 }
3120
David Sterbae902baa2019-03-20 14:36:46 +01003121 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04003122 if (free_space < data_size) {
3123 ret = 1;
3124 goto out;
3125 }
3126
Qu Wenruod16c7022020-08-19 14:35:50 +08003127 if (check_sibling_keys(left, right)) {
3128 ret = -EUCLEAN;
3129 goto out;
3130 }
David Sterba8087c192019-03-20 14:40:41 +01003131 return __push_leaf_left(path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04003132 empty, left, free_space, right_nritems,
3133 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003134out:
3135 btrfs_tree_unlock(left);
3136 free_extent_buffer(left);
3137 return ret;
3138}
3139
3140/*
Chris Mason74123bd2007-02-02 11:05:29 -05003141 * split the path's leaf in two, making sure there is at least data_size
3142 * available for the resulting leaf level of the path.
3143 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003144static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003145 struct btrfs_path *path,
3146 struct extent_buffer *l,
3147 struct extent_buffer *right,
3148 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003149{
David Sterba94f94ad2019-03-20 14:42:33 +01003150 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003151 int data_copy_size;
3152 int rt_data_off;
3153 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003154 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003155 struct btrfs_map_token token;
3156
Chris Mason5f39d392007-10-15 16:14:19 -04003157 nritems = nritems - mid;
3158 btrfs_set_header_nritems(right, nritems);
David Sterba8f881e82019-03-20 11:33:10 +01003159 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
Chris Mason5f39d392007-10-15 16:14:19 -04003160
3161 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3162 btrfs_item_nr_offset(mid),
3163 nritems * sizeof(struct btrfs_item));
3164
3165 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003166 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
3167 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003168 leaf_data_end(l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003169
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003170 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003171
David Sterbac82f8232019-08-09 17:48:21 +02003172 btrfs_init_map_token(&token, right);
Chris Mason5f39d392007-10-15 16:14:19 -04003173 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003174 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003175 u32 ioff;
3176
David Sterbacc4c13d2020-04-29 02:15:56 +02003177 ioff = btrfs_token_item_offset(&token, item);
3178 btrfs_set_token_item_offset(&token, item, ioff + rt_data_off);
Chris Mason0783fcf2007-03-12 20:12:07 -04003179 }
Chris Mason74123bd2007-02-02 11:05:29 -05003180
Chris Mason5f39d392007-10-15 16:14:19 -04003181 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003182 btrfs_item_key(right, &disk_key, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01003183 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003184
3185 btrfs_mark_buffer_dirty(right);
3186 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003187 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003188
Chris Masonbe0e5c02007-01-26 15:51:26 -05003189 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003190 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003191 free_extent_buffer(path->nodes[0]);
3192 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003193 path->slots[0] -= mid;
3194 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003195 } else {
3196 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003197 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003198 }
Chris Mason5f39d392007-10-15 16:14:19 -04003199
Chris Masoneb60cea2007-02-02 09:18:22 -05003200 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003201}
3202
3203/*
Chris Mason99d8f832010-07-07 10:51:48 -04003204 * double splits happen when we need to insert a big item in the middle
3205 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3206 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3207 * A B C
3208 *
3209 * We avoid this by trying to push the items on either side of our target
3210 * into the adjacent leaves. If all goes well we can avoid the double split
3211 * completely.
3212 */
3213static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3214 struct btrfs_root *root,
3215 struct btrfs_path *path,
3216 int data_size)
3217{
3218 int ret;
3219 int progress = 0;
3220 int slot;
3221 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003222 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04003223
3224 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003225 if (slot < btrfs_header_nritems(path->nodes[0]))
David Sterbae902baa2019-03-20 14:36:46 +01003226 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04003227
3228 /*
3229 * try to push all the items after our slot into the
3230 * right leaf
3231 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003232 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04003233 if (ret < 0)
3234 return ret;
3235
3236 if (ret == 0)
3237 progress++;
3238
3239 nritems = btrfs_header_nritems(path->nodes[0]);
3240 /*
3241 * our goal is to get our slot at the start or end of a leaf. If
3242 * we've done so we're done
3243 */
3244 if (path->slots[0] == 0 || path->slots[0] == nritems)
3245 return 0;
3246
David Sterbae902baa2019-03-20 14:36:46 +01003247 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04003248 return 0;
3249
3250 /* try to push all the items before our slot into the next leaf */
3251 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00003252 space_needed = data_size;
3253 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01003254 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003255 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04003256 if (ret < 0)
3257 return ret;
3258
3259 if (ret == 0)
3260 progress++;
3261
3262 if (progress)
3263 return 0;
3264 return 1;
3265}
3266
3267/*
Chris Mason44871b12009-03-13 10:04:31 -04003268 * split the path's leaf in two, making sure there is at least data_size
3269 * available for the resulting leaf level of the path.
3270 *
3271 * returns 0 if all went well and < 0 on failure.
3272 */
3273static noinline int split_leaf(struct btrfs_trans_handle *trans,
3274 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003275 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04003276 struct btrfs_path *path, int data_size,
3277 int extend)
3278{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003279 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003280 struct extent_buffer *l;
3281 u32 nritems;
3282 int mid;
3283 int slot;
3284 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003285 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003286 int ret = 0;
3287 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003288 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003289 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003290 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003291
Yan, Zhenga5719522009-09-24 09:17:31 -04003292 l = path->nodes[0];
3293 slot = path->slots[0];
3294 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003295 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04003296 return -EOVERFLOW;
3297
Chris Mason44871b12009-03-13 10:04:31 -04003298 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003299 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003300 int space_needed = data_size;
3301
3302 if (slot < btrfs_header_nritems(l))
David Sterbae902baa2019-03-20 14:36:46 +01003303 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003304
3305 wret = push_leaf_right(trans, root, path, space_needed,
3306 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003307 if (wret < 0)
3308 return wret;
3309 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00003310 space_needed = data_size;
3311 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01003312 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003313 wret = push_leaf_left(trans, root, path, space_needed,
3314 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003315 if (wret < 0)
3316 return wret;
3317 }
3318 l = path->nodes[0];
3319
3320 /* did the pushes work? */
David Sterbae902baa2019-03-20 14:36:46 +01003321 if (btrfs_leaf_free_space(l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04003322 return 0;
3323 }
3324
3325 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003326 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003327 if (ret)
3328 return ret;
3329 }
3330again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003331 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003332 l = path->nodes[0];
3333 slot = path->slots[0];
3334 nritems = btrfs_header_nritems(l);
3335 mid = (nritems + 1) / 2;
3336
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003337 if (mid <= slot) {
3338 if (nritems == 1 ||
3339 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003340 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003341 if (slot >= nritems) {
3342 split = 0;
3343 } else {
3344 mid = slot;
3345 if (mid != nritems &&
3346 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003347 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003348 if (data_size && !tried_avoid_double)
3349 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003350 split = 2;
3351 }
3352 }
3353 }
3354 } else {
3355 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003356 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003357 if (!extend && data_size && slot == 0) {
3358 split = 0;
3359 } else if ((extend || !data_size) && slot == 0) {
3360 mid = 1;
3361 } else {
3362 mid = slot;
3363 if (mid != nritems &&
3364 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003365 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003366 if (data_size && !tried_avoid_double)
3367 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05303368 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003369 }
3370 }
3371 }
3372 }
3373
3374 if (split == 0)
3375 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3376 else
3377 btrfs_item_key(l, &disk_key, mid);
3378
Josef Bacikca9d4732020-08-20 11:46:08 -04003379 /*
3380 * We have to about BTRFS_NESTING_NEW_ROOT here if we've done a double
3381 * split, because we're only allowed to have MAX_LOCKDEP_SUBCLASSES
3382 * subclasses, which is 8 at the time of this patch, and we've maxed it
3383 * out. In the future we could add a
3384 * BTRFS_NESTING_SPLIT_THE_SPLITTENING if we need to, but for now just
3385 * use BTRFS_NESTING_NEW_ROOT.
3386 */
Filipe Mananaa6279472019-01-25 11:48:51 +00003387 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
Josef Bacikca9d4732020-08-20 11:46:08 -04003388 l->start, 0, num_doubles ?
3389 BTRFS_NESTING_NEW_ROOT :
3390 BTRFS_NESTING_SPLIT);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003391 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003392 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003394 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04003395
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003396 if (split == 0) {
3397 if (mid <= slot) {
3398 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01003399 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003400 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003401 btrfs_tree_unlock(path->nodes[0]);
3402 free_extent_buffer(path->nodes[0]);
3403 path->nodes[0] = right;
3404 path->slots[0] = 0;
3405 path->slots[1] += 1;
3406 } else {
3407 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01003408 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003409 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003410 btrfs_tree_unlock(path->nodes[0]);
3411 free_extent_buffer(path->nodes[0]);
3412 path->nodes[0] = right;
3413 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003414 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003415 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003416 }
Liu Bo196e0242016-09-07 14:48:28 -07003417 /*
3418 * We create a new leaf 'right' for the required ins_len and
3419 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
3420 * the content of ins_len to 'right'.
3421 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003422 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003423 }
3424
David Sterba94f94ad2019-03-20 14:42:33 +01003425 copy_for_split(trans, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003426
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003427 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003428 BUG_ON(num_doubles != 0);
3429 num_doubles++;
3430 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003431 }
Chris Mason44871b12009-03-13 10:04:31 -04003432
Jeff Mahoney143bede2012-03-01 14:56:26 +01003433 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003434
3435push_for_double:
3436 push_for_double_split(trans, root, path, data_size);
3437 tried_avoid_double = 1;
David Sterbae902baa2019-03-20 14:36:46 +01003438 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04003439 return 0;
3440 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003441}
3442
Yan, Zhengad48fd752009-11-12 09:33:58 +00003443static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3444 struct btrfs_root *root,
3445 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003446{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003447 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003448 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003449 struct btrfs_file_extent_item *fi;
3450 u64 extent_len = 0;
3451 u32 item_size;
3452 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003453
3454 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003455 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3456
3457 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3458 key.type != BTRFS_EXTENT_CSUM_KEY);
3459
David Sterbae902baa2019-03-20 14:36:46 +01003460 if (btrfs_leaf_free_space(leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00003461 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003462
3463 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003464 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3465 fi = btrfs_item_ptr(leaf, path->slots[0],
3466 struct btrfs_file_extent_item);
3467 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3468 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003469 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003470
Chris Mason459931e2008-12-10 09:10:46 -05003471 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003472 path->search_for_split = 1;
3473 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003474 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00003475 if (ret > 0)
3476 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003477 if (ret < 0)
3478 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003479
Yan, Zhengad48fd752009-11-12 09:33:58 +00003480 ret = -EAGAIN;
3481 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00003482 /* if our item isn't there, return now */
3483 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00003484 goto err;
3485
Chris Mason109f6ae2010-04-02 09:20:18 -04003486 /* the leaf has changed, it now has room. return now */
David Sterbae902baa2019-03-20 14:36:46 +01003487 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04003488 goto err;
3489
Yan, Zhengad48fd752009-11-12 09:33:58 +00003490 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3491 fi = btrfs_item_ptr(leaf, path->slots[0],
3492 struct btrfs_file_extent_item);
3493 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3494 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003495 }
3496
Yan, Zhengad48fd752009-11-12 09:33:58 +00003497 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003498 if (ret)
3499 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003500
Yan, Zhengad48fd752009-11-12 09:33:58 +00003501 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003502 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003503 return 0;
3504err:
3505 path->keep_locks = 0;
3506 return ret;
3507}
3508
David Sterba25263cd2019-03-20 14:44:57 +01003509static noinline int split_item(struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003510 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00003511 unsigned long split_offset)
3512{
3513 struct extent_buffer *leaf;
3514 struct btrfs_item *item;
3515 struct btrfs_item *new_item;
3516 int slot;
3517 char *buf;
3518 u32 nritems;
3519 u32 item_size;
3520 u32 orig_offset;
3521 struct btrfs_disk_key disk_key;
3522
Chris Masonb9473432009-03-13 11:00:37 -04003523 leaf = path->nodes[0];
David Sterbae902baa2019-03-20 14:36:46 +01003524 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04003525
Ross Kirkdd3cc162013-09-16 15:58:09 +01003526 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05003527 orig_offset = btrfs_item_offset(leaf, item);
3528 item_size = btrfs_item_size(leaf, item);
3529
Chris Mason459931e2008-12-10 09:10:46 -05003530 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003531 if (!buf)
3532 return -ENOMEM;
3533
Chris Mason459931e2008-12-10 09:10:46 -05003534 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3535 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003536
Chris Mason459931e2008-12-10 09:10:46 -05003537 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003538 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003539 if (slot != nritems) {
3540 /* shift the items */
3541 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003542 btrfs_item_nr_offset(slot),
3543 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003544 }
3545
3546 btrfs_cpu_key_to_disk(&disk_key, new_key);
3547 btrfs_set_item_key(leaf, &disk_key, slot);
3548
Ross Kirkdd3cc162013-09-16 15:58:09 +01003549 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05003550
3551 btrfs_set_item_offset(leaf, new_item, orig_offset);
3552 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3553
3554 btrfs_set_item_offset(leaf, item,
3555 orig_offset + item_size - split_offset);
3556 btrfs_set_item_size(leaf, item, split_offset);
3557
3558 btrfs_set_header_nritems(leaf, nritems + 1);
3559
3560 /* write the data for the start of the original item */
3561 write_extent_buffer(leaf, buf,
3562 btrfs_item_ptr_offset(leaf, path->slots[0]),
3563 split_offset);
3564
3565 /* write the data for the new item */
3566 write_extent_buffer(leaf, buf + split_offset,
3567 btrfs_item_ptr_offset(leaf, slot),
3568 item_size - split_offset);
3569 btrfs_mark_buffer_dirty(leaf);
3570
David Sterbae902baa2019-03-20 14:36:46 +01003571 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003572 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003573 return 0;
3574}
3575
3576/*
3577 * This function splits a single item into two items,
3578 * giving 'new_key' to the new item and splitting the
3579 * old one at split_offset (from the start of the item).
3580 *
3581 * The path may be released by this operation. After
3582 * the split, the path is pointing to the old item. The
3583 * new item is going to be in the same node as the old one.
3584 *
3585 * Note, the item being split must be smaller enough to live alone on
3586 * a tree block with room for one extra struct btrfs_item
3587 *
3588 * This allows us to split the item in place, keeping a lock on the
3589 * leaf the entire time.
3590 */
3591int btrfs_split_item(struct btrfs_trans_handle *trans,
3592 struct btrfs_root *root,
3593 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003594 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00003595 unsigned long split_offset)
3596{
3597 int ret;
3598 ret = setup_leaf_for_split(trans, root, path,
3599 sizeof(struct btrfs_item));
3600 if (ret)
3601 return ret;
3602
David Sterba25263cd2019-03-20 14:44:57 +01003603 ret = split_item(path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003604 return ret;
3605}
3606
3607/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003608 * This function duplicate a item, giving 'new_key' to the new item.
3609 * It guarantees both items live in the same tree leaf and the new item
3610 * is contiguous with the original item.
3611 *
3612 * This allows us to split file extent in place, keeping a lock on the
3613 * leaf the entire time.
3614 */
3615int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3616 struct btrfs_root *root,
3617 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003618 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00003619{
3620 struct extent_buffer *leaf;
3621 int ret;
3622 u32 item_size;
3623
3624 leaf = path->nodes[0];
3625 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3626 ret = setup_leaf_for_split(trans, root, path,
3627 item_size + sizeof(struct btrfs_item));
3628 if (ret)
3629 return ret;
3630
3631 path->slots[0]++;
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03003632 setup_items_for_insert(root, path, new_key, &item_size, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003633 leaf = path->nodes[0];
3634 memcpy_extent_buffer(leaf,
3635 btrfs_item_ptr_offset(leaf, path->slots[0]),
3636 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3637 item_size);
3638 return 0;
3639}
3640
3641/*
Chris Masond352ac62008-09-29 15:18:18 -04003642 * make the item pointed to by the path smaller. new_size indicates
3643 * how small to make it, and from_end tells us if we just chop bytes
3644 * off the end of the item or if we shift the item to chop bytes off
3645 * the front.
3646 */
David Sterba78ac4f92019-03-20 14:49:12 +01003647void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003648{
Chris Masonb18c6682007-04-17 13:26:50 -04003649 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003650 struct extent_buffer *leaf;
3651 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003652 u32 nritems;
3653 unsigned int data_end;
3654 unsigned int old_data_start;
3655 unsigned int old_size;
3656 unsigned int size_diff;
3657 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003658 struct btrfs_map_token token;
3659
Chris Mason5f39d392007-10-15 16:14:19 -04003660 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003661 slot = path->slots[0];
3662
3663 old_size = btrfs_item_size_nr(leaf, slot);
3664 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003665 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003666
Chris Mason5f39d392007-10-15 16:14:19 -04003667 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01003668 data_end = leaf_data_end(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003669
Chris Mason5f39d392007-10-15 16:14:19 -04003670 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003671
Chris Masonb18c6682007-04-17 13:26:50 -04003672 size_diff = old_size - new_size;
3673
3674 BUG_ON(slot < 0);
3675 BUG_ON(slot >= nritems);
3676
3677 /*
3678 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3679 */
3680 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02003681 btrfs_init_map_token(&token, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003682 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003683 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01003684 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003685
David Sterbacc4c13d2020-04-29 02:15:56 +02003686 ioff = btrfs_token_item_offset(&token, item);
3687 btrfs_set_token_item_offset(&token, item, ioff + size_diff);
Chris Masonb18c6682007-04-17 13:26:50 -04003688 }
Chris Masondb945352007-10-15 16:15:53 -04003689
Chris Masonb18c6682007-04-17 13:26:50 -04003690 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003691 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003692 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3693 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04003694 data_end, old_data_start + new_size - data_end);
3695 } else {
3696 struct btrfs_disk_key disk_key;
3697 u64 offset;
3698
3699 btrfs_item_key(leaf, &disk_key, slot);
3700
3701 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3702 unsigned long ptr;
3703 struct btrfs_file_extent_item *fi;
3704
3705 fi = btrfs_item_ptr(leaf, slot,
3706 struct btrfs_file_extent_item);
3707 fi = (struct btrfs_file_extent_item *)(
3708 (unsigned long)fi - size_diff);
3709
3710 if (btrfs_file_extent_type(leaf, fi) ==
3711 BTRFS_FILE_EXTENT_INLINE) {
3712 ptr = btrfs_item_ptr_offset(leaf, slot);
3713 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003714 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02003715 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04003716 }
3717 }
3718
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003719 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3720 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04003721 data_end, old_data_start - data_end);
3722
3723 offset = btrfs_disk_key_offset(&disk_key);
3724 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3725 btrfs_set_item_key(leaf, &disk_key, slot);
3726 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003727 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04003728 }
Chris Mason5f39d392007-10-15 16:14:19 -04003729
Ross Kirkdd3cc162013-09-16 15:58:09 +01003730 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003731 btrfs_set_item_size(leaf, item, new_size);
3732 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003733
David Sterbae902baa2019-03-20 14:36:46 +01003734 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02003735 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003736 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003737 }
Chris Masonb18c6682007-04-17 13:26:50 -04003738}
3739
Chris Masond352ac62008-09-29 15:18:18 -04003740/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00003741 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04003742 */
David Sterbac71dd882019-03-20 14:51:10 +01003743void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003744{
Chris Mason6567e832007-04-16 09:22:45 -04003745 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003746 struct extent_buffer *leaf;
3747 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003748 u32 nritems;
3749 unsigned int data_end;
3750 unsigned int old_data;
3751 unsigned int old_size;
3752 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003753 struct btrfs_map_token token;
3754
Chris Mason5f39d392007-10-15 16:14:19 -04003755 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003756
Chris Mason5f39d392007-10-15 16:14:19 -04003757 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01003758 data_end = leaf_data_end(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003759
David Sterbae902baa2019-03-20 14:36:46 +01003760 if (btrfs_leaf_free_space(leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02003761 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003762 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003763 }
Chris Mason6567e832007-04-16 09:22:45 -04003764 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003765 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003766
3767 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003768 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02003769 btrfs_print_leaf(leaf);
David Sterbac71dd882019-03-20 14:51:10 +01003770 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003771 slot, nritems);
Arnd Bergmann290342f2019-03-25 14:02:25 +01003772 BUG();
Chris Mason3326d1b2007-10-15 16:18:25 -04003773 }
Chris Mason6567e832007-04-16 09:22:45 -04003774
3775 /*
3776 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3777 */
3778 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02003779 btrfs_init_map_token(&token, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003780 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003781 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01003782 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003783
David Sterbacc4c13d2020-04-29 02:15:56 +02003784 ioff = btrfs_token_item_offset(&token, item);
3785 btrfs_set_token_item_offset(&token, item, ioff - data_size);
Chris Mason6567e832007-04-16 09:22:45 -04003786 }
Chris Mason5f39d392007-10-15 16:14:19 -04003787
Chris Mason6567e832007-04-16 09:22:45 -04003788 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003789 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3790 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04003791 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003792
Chris Mason6567e832007-04-16 09:22:45 -04003793 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003794 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003795 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003796 btrfs_set_item_size(leaf, item, old_size + data_size);
3797 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003798
David Sterbae902baa2019-03-20 14:36:46 +01003799 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02003800 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003801 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003802 }
Chris Mason6567e832007-04-16 09:22:45 -04003803}
3804
Nikolay Borisovda9ffb22020-09-01 17:40:00 +03003805/**
3806 * setup_items_for_insert - Helper called before inserting one or more items
3807 * to a leaf. Main purpose is to save stack depth by doing the bulk of the work
3808 * in a function that doesn't call btrfs_search_slot
3809 *
3810 * @root: root we are inserting items to
3811 * @path: points to the leaf/slot where we are going to insert new items
3812 * @cpu_key: array of keys for items to be inserted
3813 * @data_size: size of the body of each item we are going to insert
3814 * @nr: size of @cpu_key/@data_size arrays
Chris Mason74123bd2007-02-02 11:05:29 -05003815 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003816void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003817 const struct btrfs_key *cpu_key, u32 *data_size,
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03003818 int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003819{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003820 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003821 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05003822 int i;
Chris Mason7518a232007-03-12 12:01:18 -04003823 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003824 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04003825 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003826 struct extent_buffer *leaf;
3827 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05003828 struct btrfs_map_token token;
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03003829 u32 total_size;
3830 u32 total_data = 0;
3831
3832 for (i = 0; i < nr; i++)
3833 total_data += data_size[i];
3834 total_size = total_data + (nr * sizeof(struct btrfs_item));
Chris Masoncfed81a2012-03-03 07:40:03 -05003835
Filipe Manana24cdc842014-07-28 19:34:35 +01003836 if (path->slots[0] == 0) {
3837 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003838 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01003839 }
3840 btrfs_unlock_up_safe(path, 1);
3841
Chris Mason5f39d392007-10-15 16:14:19 -04003842 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003843 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05003844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01003846 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05003847
David Sterbae902baa2019-03-20 14:36:46 +01003848 if (btrfs_leaf_free_space(leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02003849 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003850 btrfs_crit(fs_info, "not enough freespace need %u have %d",
David Sterbae902baa2019-03-20 14:36:46 +01003851 total_size, btrfs_leaf_free_space(leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003852 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04003853 }
Chris Mason5f39d392007-10-15 16:14:19 -04003854
David Sterbac82f8232019-08-09 17:48:21 +02003855 btrfs_init_map_token(&token, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003856 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04003857 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003858
Chris Mason5f39d392007-10-15 16:14:19 -04003859 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02003860 btrfs_print_leaf(leaf);
Nikolay Borisov7269ddd2020-09-01 17:40:01 +03003861 btrfs_crit(fs_info,
3862 "item at slot %d with data offset %u beyond data end of leaf %u",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04003863 slot, old_data, data_end);
Arnd Bergmann290342f2019-03-25 14:02:25 +01003864 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003865 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003866 /*
3867 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3868 */
3869 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04003870 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003871 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003872
Jeff Mahoney62e85572016-09-20 10:05:01 -04003873 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02003874 ioff = btrfs_token_item_offset(&token, item);
3875 btrfs_set_token_item_offset(&token, item,
3876 ioff - total_data);
Chris Mason0783fcf2007-03-12 20:12:07 -04003877 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05003878 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05003879 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04003880 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003881 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05003882
3883 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003884 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
3885 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04003886 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003887 data_end = old_data;
3888 }
Chris Mason5f39d392007-10-15 16:14:19 -04003889
Chris Mason62e27492007-03-15 12:56:47 -04003890 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05003891 for (i = 0; i < nr; i++) {
3892 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3893 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003894 item = btrfs_item_nr(slot + i);
Chris Mason9c583092008-01-29 15:15:18 -05003895 data_end -= data_size[i];
Nikolay Borisovfc0716c2020-09-01 17:39:57 +03003896 btrfs_set_token_item_offset(&token, item, data_end);
David Sterbacc4c13d2020-04-29 02:15:56 +02003897 btrfs_set_token_item_size(&token, item, data_size[i]);
Chris Mason9c583092008-01-29 15:15:18 -05003898 }
Chris Mason44871b12009-03-13 10:04:31 -04003899
Chris Mason9c583092008-01-29 15:15:18 -05003900 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04003901 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003902
David Sterbae902baa2019-03-20 14:36:46 +01003903 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02003904 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003905 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003906 }
Chris Mason44871b12009-03-13 10:04:31 -04003907}
3908
3909/*
3910 * Given a key and some data, insert items into the tree.
3911 * This does all the path init required, making room in the tree if needed.
3912 */
3913int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
3914 struct btrfs_root *root,
3915 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003916 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003917 int nr)
3918{
Chris Mason44871b12009-03-13 10:04:31 -04003919 int ret = 0;
3920 int slot;
3921 int i;
3922 u32 total_size = 0;
3923 u32 total_data = 0;
3924
3925 for (i = 0; i < nr; i++)
3926 total_data += data_size[i];
3927
3928 total_size = total_data + (nr * sizeof(struct btrfs_item));
3929 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3930 if (ret == 0)
3931 return -EEXIST;
3932 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003933 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003934
Chris Mason44871b12009-03-13 10:04:31 -04003935 slot = path->slots[0];
3936 BUG_ON(slot < 0);
3937
Nikolay Borisovfc0d82e2020-09-01 17:39:59 +03003938 setup_items_for_insert(root, path, cpu_key, data_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003939 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04003940}
3941
3942/*
3943 * Given a key and some data, insert an item into the tree.
3944 * This does all the path init required, making room in the tree if needed.
3945 */
Omar Sandoval310712b2017-01-17 23:24:37 -08003946int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3947 const struct btrfs_key *cpu_key, void *data,
3948 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04003949{
3950 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003951 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003952 struct extent_buffer *leaf;
3953 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04003954
Chris Mason2c90e5d2007-04-02 10:50:19 -04003955 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00003956 if (!path)
3957 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04003958 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04003959 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04003960 leaf = path->nodes[0];
3961 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3962 write_extent_buffer(leaf, data, ptr, data_size);
3963 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04003964 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04003965 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003966 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003967}
3968
Chris Mason74123bd2007-02-02 11:05:29 -05003969/*
Chris Mason5de08d72007-02-24 06:24:44 -05003970 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05003971 *
Chris Masond352ac62008-09-29 15:18:18 -04003972 * the tree should have been previously balanced so the deletion does not
3973 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05003974 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00003975static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
3976 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003977{
Chris Mason5f39d392007-10-15 16:14:19 -04003978 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04003979 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003980 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003981
Chris Mason5f39d392007-10-15 16:14:19 -04003982 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05003983 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01003984 if (level) {
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00003985 ret = btrfs_tree_mod_log_insert_move(parent, slot,
3986 slot + 1, nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01003987 BUG_ON(ret < 0);
3988 }
Chris Mason5f39d392007-10-15 16:14:19 -04003989 memmove_extent_buffer(parent,
3990 btrfs_node_key_ptr_offset(slot),
3991 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04003992 sizeof(struct btrfs_key_ptr) *
3993 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05003994 } else if (level) {
Filipe Mananaf3a84cc2021-03-11 14:31:07 +00003995 ret = btrfs_tree_mod_log_insert_key(parent, slot,
3996 BTRFS_MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05003997 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05003998 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003999
Chris Mason7518a232007-03-12 12:01:18 -04004000 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004001 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004002 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004003 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004004 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004005 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004006 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004007 struct btrfs_disk_key disk_key;
4008
4009 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004010 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004011 }
Chris Masond6025572007-03-30 14:27:56 -04004012 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004013}
4014
Chris Mason74123bd2007-02-02 11:05:29 -05004015/*
Chris Mason323ac952008-10-01 19:05:46 -04004016 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004017 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004018 *
4019 * This deletes the pointer in path->nodes[1] and frees the leaf
4020 * block extent. zero is returned if it all worked out, < 0 otherwise.
4021 *
4022 * The path must have already been setup for deleting the leaf, including
4023 * all the proper balancing. path->nodes[1] must be locked.
4024 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004025static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4026 struct btrfs_root *root,
4027 struct btrfs_path *path,
4028 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004029{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004030 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004031 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004032
Chris Mason4d081c42009-02-04 09:31:28 -05004033 /*
4034 * btrfs_free_extent is expensive, we want to make sure we
4035 * aren't holding any locks when we call it
4036 */
4037 btrfs_unlock_up_safe(path, 0);
4038
Yan, Zhengf0486c62010-05-16 10:46:25 -04004039 root_sub_used(root, leaf->len);
4040
David Sterba67439da2019-10-08 13:28:47 +02004041 atomic_inc(&leaf->refs);
Jan Schmidt5581a512012-05-16 17:04:52 +02004042 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004043 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004044}
4045/*
Chris Mason74123bd2007-02-02 11:05:29 -05004046 * delete the item at the leaf level in path. If that empties
4047 * the leaf, remove it from the tree
4048 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004049int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4050 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004052 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004053 struct extent_buffer *leaf;
4054 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004055 u32 last_off;
4056 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004057 int ret = 0;
4058 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004059 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004060 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004061
Chris Mason5f39d392007-10-15 16:14:19 -04004062 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004063 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4064
4065 for (i = 0; i < nr; i++)
4066 dsize += btrfs_item_size_nr(leaf, slot + i);
4067
Chris Mason5f39d392007-10-15 16:14:19 -04004068 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004069
Chris Mason85e21ba2008-01-29 15:11:36 -05004070 if (slot + nr != nritems) {
David Sterba8f881e82019-03-20 11:33:10 +01004071 int data_end = leaf_data_end(leaf);
David Sterbac82f8232019-08-09 17:48:21 +02004072 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04004073
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004074 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004075 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004076 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004077 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004078
David Sterbac82f8232019-08-09 17:48:21 +02004079 btrfs_init_map_token(&token, leaf);
Chris Mason85e21ba2008-01-29 15:11:36 -05004080 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004081 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004082
Ross Kirkdd3cc162013-09-16 15:58:09 +01004083 item = btrfs_item_nr(i);
David Sterbacc4c13d2020-04-29 02:15:56 +02004084 ioff = btrfs_token_item_offset(&token, item);
4085 btrfs_set_token_item_offset(&token, item, ioff + dsize);
Chris Mason0783fcf2007-03-12 20:12:07 -04004086 }
Chris Masondb945352007-10-15 16:15:53 -04004087
Chris Mason5f39d392007-10-15 16:14:19 -04004088 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004089 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004090 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004091 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004092 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004093 btrfs_set_header_nritems(leaf, nritems - nr);
4094 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004095
Chris Mason74123bd2007-02-02 11:05:29 -05004096 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004097 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004098 if (leaf == root->node) {
4099 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004100 } else {
David Sterba6a884d7d2019-03-20 14:30:02 +01004101 btrfs_clean_tree_block(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004102 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004103 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004104 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004105 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004106 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004107 struct btrfs_disk_key disk_key;
4108
4109 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004110 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004111 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004112
Chris Mason74123bd2007-02-02 11:05:29 -05004113 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004114 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004115 /* push_leaf_left fixes the path.
4116 * make sure the path still points to our leaf
4117 * for possible call to del_ptr below
4118 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004119 slot = path->slots[1];
David Sterba67439da2019-10-08 13:28:47 +02004120 atomic_inc(&leaf->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04004121
Chris Mason99d8f832010-07-07 10:51:48 -04004122 wret = push_leaf_left(trans, root, path, 1, 1,
4123 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004124 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004125 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004126
4127 if (path->nodes[0] == leaf &&
4128 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004129 wret = push_leaf_right(trans, root, path, 1,
4130 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004131 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004132 ret = wret;
4133 }
Chris Mason5f39d392007-10-15 16:14:19 -04004134
4135 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004136 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004137 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004138 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004139 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004140 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004141 /* if we're still in the path, make sure
4142 * we're dirty. Otherwise, one of the
4143 * push_leaf functions must have already
4144 * dirtied this buffer
4145 */
4146 if (path->nodes[0] == leaf)
4147 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004148 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004149 }
Chris Masond5719762007-03-23 10:01:08 -04004150 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004151 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004152 }
4153 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004154 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004155}
4156
Chris Mason97571fd2007-02-24 13:39:08 -05004157/*
Chris Mason925baed2008-06-25 16:01:30 -04004158 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004159 * returns 0 if it found something or 1 if there are no lesser leaves.
4160 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004161 *
4162 * This may release the path, and so you may lose any locks held at the
4163 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004164 */
Josef Bacik16e75492013-10-22 12:18:51 -04004165int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05004166{
Chris Mason925baed2008-06-25 16:01:30 -04004167 struct btrfs_key key;
4168 struct btrfs_disk_key found_key;
4169 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004170
Chris Mason925baed2008-06-25 16:01:30 -04004171 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004172
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004173 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004174 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004175 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004176 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004177 key.offset = (u64)-1;
4178 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004179 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004180 key.type = (u8)-1;
4181 key.offset = (u64)-1;
4182 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004183 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004184 }
Chris Mason7bb86312007-12-11 09:25:06 -05004185
David Sterbab3b4aa72011-04-21 01:20:15 +02004186 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4188 if (ret < 0)
4189 return ret;
4190 btrfs_item_key(path->nodes[0], &found_key, 0);
4191 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01004192 /*
4193 * We might have had an item with the previous key in the tree right
4194 * before we released our path. And after we released our path, that
4195 * item might have been pushed to the first slot (0) of the leaf we
4196 * were holding due to a tree balance. Alternatively, an item with the
4197 * previous key can exist as the only element of a leaf (big fat item).
4198 * Therefore account for these 2 cases, so that our callers (like
4199 * btrfs_previous_item) don't miss an existing item with a key matching
4200 * the previous key we computed above.
4201 */
4202 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04004203 return 0;
4204 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004205}
4206
Chris Mason3f157a22008-06-25 16:01:31 -04004207/*
4208 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004209 * for nodes or leaves that are have a minimum transaction id.
4210 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004211 *
4212 * This does not cow, but it does stuff the starting key it finds back
4213 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4214 * key and get a writable path.
4215 *
Chris Mason3f157a22008-06-25 16:01:31 -04004216 * This honors path->lowest_level to prevent descent past a given level
4217 * of the tree.
4218 *
Chris Masond352ac62008-09-29 15:18:18 -04004219 * min_trans indicates the oldest transaction that you are interested
4220 * in walking through. Any nodes or leaves older than min_trans are
4221 * skipped over (without reading them).
4222 *
Chris Mason3f157a22008-06-25 16:01:31 -04004223 * returns zero if something useful was found, < 0 on error and 1 if there
4224 * was nothing in the tree that matched the search criteria.
4225 */
4226int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004227 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004228 u64 min_trans)
4229{
4230 struct extent_buffer *cur;
4231 struct btrfs_key found_key;
4232 int slot;
Yan96524802008-07-24 12:19:49 -04004233 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004234 u32 nritems;
4235 int level;
4236 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01004237 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04004238
Filipe Mananaf98de9b2014-08-04 19:37:21 +01004239 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04004240again:
Chris Masonbd681512011-07-16 15:23:14 -04004241 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004242 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004243 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004244 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004245 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004246
4247 if (btrfs_header_generation(cur) < min_trans) {
4248 ret = 1;
4249 goto out;
4250 }
Chris Masond3977122009-01-05 21:25:51 -05004251 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004252 nritems = btrfs_header_nritems(cur);
4253 level = btrfs_header_level(cur);
Qu Wenruoe3b83362020-04-17 15:08:21 +08004254 sret = btrfs_bin_search(cur, min_key, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00004255 if (sret < 0) {
4256 ret = sret;
4257 goto out;
4258 }
Chris Mason3f157a22008-06-25 16:01:31 -04004259
Chris Mason323ac952008-10-01 19:05:46 -04004260 /* at the lowest level, we're done, setup the path and exit */
4261 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004262 if (slot >= nritems)
4263 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004264 ret = 0;
4265 path->slots[level] = slot;
4266 btrfs_item_key_to_cpu(cur, &found_key, slot);
4267 goto out;
4268 }
Yan96524802008-07-24 12:19:49 -04004269 if (sret && slot > 0)
4270 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004271 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004272 * check this node pointer against the min_trans parameters.
Randy Dunlap260db432020-08-04 19:48:34 -07004273 * If it is too old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004274 */
Chris Masond3977122009-01-05 21:25:51 -05004275 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004276 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004277
Chris Mason3f157a22008-06-25 16:01:31 -04004278 gen = btrfs_node_ptr_generation(cur, slot);
4279 if (gen < min_trans) {
4280 slot++;
4281 continue;
4282 }
Eric Sandeende78b512013-01-31 18:21:12 +00004283 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004284 }
Chris Masone02119d2008-09-05 16:13:11 -04004285find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004286 /*
4287 * we didn't find a candidate key in this node, walk forward
4288 * and find another one
4289 */
4290 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004291 path->slots[level] = slot;
4292 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004293 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004294 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004295 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004296 goto again;
4297 } else {
4298 goto out;
4299 }
4300 }
4301 /* save our key for returning back */
4302 btrfs_node_key_to_cpu(cur, &found_key, slot);
4303 path->slots[level] = slot;
4304 if (level == path->lowest_level) {
4305 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04004306 goto out;
4307 }
David Sterba4b231ae2019-08-21 19:16:27 +02004308 cur = btrfs_read_node_slot(cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07004309 if (IS_ERR(cur)) {
4310 ret = PTR_ERR(cur);
4311 goto out;
4312 }
Chris Mason3f157a22008-06-25 16:01:31 -04004313
Chris Masonbd681512011-07-16 15:23:14 -04004314 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004315
Chris Masonbd681512011-07-16 15:23:14 -04004316 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004317 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004318 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004319 }
4320out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01004321 path->keep_locks = keep_locks;
4322 if (ret == 0) {
4323 btrfs_unlock_up_safe(path, path->lowest_level + 1);
Chris Mason3f157a22008-06-25 16:01:31 -04004324 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01004325 }
Chris Mason3f157a22008-06-25 16:01:31 -04004326 return ret;
4327}
4328
4329/*
4330 * this is similar to btrfs_next_leaf, but does not try to preserve
4331 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00004332 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04004333 *
4334 * 0 is returned if another key is found, < 0 if there are any errors
4335 * and 1 is returned if there are no higher keys in the tree
4336 *
4337 * path->keep_locks should be set to 1 on the search made before
4338 * calling this function.
4339 */
Chris Masone7a84562008-06-25 16:01:31 -04004340int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00004341 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004342{
Chris Masone7a84562008-06-25 16:01:31 -04004343 int slot;
4344 struct extent_buffer *c;
4345
Josef Bacik6a9fb462019-06-20 15:37:52 -04004346 WARN_ON(!path->keep_locks && !path->skip_locking);
Chris Masond3977122009-01-05 21:25:51 -05004347 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004348 if (!path->nodes[level])
4349 return 1;
4350
4351 slot = path->slots[level] + 1;
4352 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004353next:
Chris Masone7a84562008-06-25 16:01:31 -04004354 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004355 int ret;
4356 int orig_lowest;
4357 struct btrfs_key cur_key;
4358 if (level + 1 >= BTRFS_MAX_LEVEL ||
4359 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004360 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004361
Josef Bacik6a9fb462019-06-20 15:37:52 -04004362 if (path->locks[level + 1] || path->skip_locking) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004363 level++;
4364 continue;
4365 }
4366
4367 slot = btrfs_header_nritems(c) - 1;
4368 if (level == 0)
4369 btrfs_item_key_to_cpu(c, &cur_key, slot);
4370 else
4371 btrfs_node_key_to_cpu(c, &cur_key, slot);
4372
4373 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004374 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004375 path->lowest_level = level;
4376 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4377 0, 0);
4378 path->lowest_level = orig_lowest;
4379 if (ret < 0)
4380 return ret;
4381
4382 c = path->nodes[level];
4383 slot = path->slots[level];
4384 if (ret == 0)
4385 slot++;
4386 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004387 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004388
Chris Masone7a84562008-06-25 16:01:31 -04004389 if (level == 0)
4390 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004391 else {
Chris Mason3f157a22008-06-25 16:01:31 -04004392 u64 gen = btrfs_node_ptr_generation(c, slot);
4393
Chris Mason3f157a22008-06-25 16:01:31 -04004394 if (gen < min_trans) {
4395 slot++;
4396 goto next;
4397 }
Chris Masone7a84562008-06-25 16:01:31 -04004398 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004399 }
Chris Masone7a84562008-06-25 16:01:31 -04004400 return 0;
4401 }
4402 return 1;
4403}
4404
Chris Mason7bb86312007-12-11 09:25:06 -05004405/*
Chris Mason925baed2008-06-25 16:01:30 -04004406 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004407 * returns 0 if it found something or 1 if there are no greater leaves.
4408 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004409 */
Chris Mason234b63a2007-03-13 10:46:10 -04004410int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004411{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02004412 return btrfs_next_old_leaf(root, path, 0);
4413}
4414
4415int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
4416 u64 time_seq)
4417{
Chris Masond97e63b2007-02-20 16:40:44 -05004418 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004419 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004420 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004421 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004422 struct btrfs_key key;
4423 u32 nritems;
4424 int ret;
Josef Bacik0e463182020-11-06 16:27:30 -05004425 int i;
Chris Mason925baed2008-06-25 16:01:30 -04004426
4427 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004428 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004429 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004430
Chris Mason8e73f272009-04-03 10:14:18 -04004431 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4432again:
4433 level = 1;
4434 next = NULL;
David Sterbab3b4aa72011-04-21 01:20:15 +02004435 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004436
Chris Masona2135012008-06-25 16:01:30 -04004437 path->keep_locks = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004438
Jan Schmidt3d7806e2012-06-11 08:29:29 +02004439 if (time_seq)
4440 ret = btrfs_search_old_slot(root, &key, path, time_seq);
4441 else
4442 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04004443 path->keep_locks = 0;
4444
4445 if (ret < 0)
4446 return ret;
4447
Chris Masona2135012008-06-25 16:01:30 -04004448 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004449 /*
4450 * by releasing the path above we dropped all our locks. A balance
4451 * could have added more items next to the key that used to be
4452 * at the very end of the block. So, check again here and
4453 * advance the path if there are now more items available.
4454 */
Chris Masona2135012008-06-25 16:01:30 -04004455 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004456 if (ret == 0)
4457 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004458 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004459 goto done;
4460 }
Liu Bo0b43e042014-06-09 11:04:49 +08004461 /*
4462 * So the above check misses one case:
4463 * - after releasing the path above, someone has removed the item that
4464 * used to be at the very end of the block, and balance between leafs
4465 * gets another one with bigger key.offset to replace it.
4466 *
4467 * This one should be returned as well, or we can get leaf corruption
4468 * later(esp. in __btrfs_drop_extents()).
4469 *
4470 * And a bit more explanation about this check,
4471 * with ret > 0, the key isn't found, the path points to the slot
4472 * where it should be inserted, so the path->slots[0] item must be the
4473 * bigger one.
4474 */
4475 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
4476 ret = 0;
4477 goto done;
4478 }
Chris Masond97e63b2007-02-20 16:40:44 -05004479
Chris Masond3977122009-01-05 21:25:51 -05004480 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004481 if (!path->nodes[level]) {
4482 ret = 1;
4483 goto done;
4484 }
Chris Mason5f39d392007-10-15 16:14:19 -04004485
Chris Masond97e63b2007-02-20 16:40:44 -05004486 slot = path->slots[level] + 1;
4487 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004488 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004489 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004490 if (level == BTRFS_MAX_LEVEL) {
4491 ret = 1;
4492 goto done;
4493 }
Chris Masond97e63b2007-02-20 16:40:44 -05004494 continue;
4495 }
Chris Mason5f39d392007-10-15 16:14:19 -04004496
Josef Bacik0e463182020-11-06 16:27:30 -05004497
4498 /*
4499 * Our current level is where we're going to start from, and to
4500 * make sure lockdep doesn't complain we need to drop our locks
4501 * and nodes from 0 to our current level.
4502 */
4503 for (i = 0; i < level; i++) {
4504 if (path->locks[level]) {
4505 btrfs_tree_read_unlock(path->nodes[i]);
4506 path->locks[i] = 0;
4507 }
4508 free_extent_buffer(path->nodes[i]);
4509 path->nodes[i] = NULL;
Chris Mason925baed2008-06-25 16:01:30 -04004510 }
Chris Mason5f39d392007-10-15 16:14:19 -04004511
Chris Mason8e73f272009-04-03 10:14:18 -04004512 next = c;
Liu Bod07b8522017-01-30 12:23:42 -08004513 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01004514 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04004515 if (ret == -EAGAIN)
4516 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004517
Chris Mason76a05b32009-05-14 13:24:30 -04004518 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004519 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004520 goto done;
4521 }
4522
Chris Mason5cd57b22008-06-25 16:01:30 -04004523 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004524 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02004525 if (!ret && time_seq) {
4526 /*
4527 * If we don't get the lock, we may be racing
4528 * with push_leaf_left, holding that lock while
4529 * itself waiting for the leaf we've currently
4530 * locked. To solve this situation, we give up
4531 * on our lock and cycle.
4532 */
Jan Schmidtcf538832012-07-04 15:42:48 +02004533 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02004534 btrfs_release_path(path);
4535 cond_resched();
4536 goto again;
4537 }
Josef Bacik0e463182020-11-06 16:27:30 -05004538 if (!ret)
4539 btrfs_tree_read_lock(next);
Chris Mason5cd57b22008-06-25 16:01:30 -04004540 }
Chris Masond97e63b2007-02-20 16:40:44 -05004541 break;
4542 }
4543 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004544 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004545 level--;
Chris Masond97e63b2007-02-20 16:40:44 -05004546 path->nodes[level] = next;
4547 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004548 if (!path->skip_locking)
Josef Bacikffeb03c2020-11-06 16:27:29 -05004549 path->locks[level] = BTRFS_READ_LOCK;
Chris Masond97e63b2007-02-20 16:40:44 -05004550 if (!level)
4551 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004552
Liu Bod07b8522017-01-30 12:23:42 -08004553 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01004554 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04004555 if (ret == -EAGAIN)
4556 goto again;
4557
Chris Mason76a05b32009-05-14 13:24:30 -04004558 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004559 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004560 goto done;
4561 }
4562
Josef Bacikffeb03c2020-11-06 16:27:29 -05004563 if (!path->skip_locking)
Josef Bacik0e463182020-11-06 16:27:30 -05004564 btrfs_tree_read_lock(next);
Chris Masond97e63b2007-02-20 16:40:44 -05004565 }
Chris Mason8e73f272009-04-03 10:14:18 -04004566 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004567done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004568 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004569
4570 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004571}
Chris Mason0b86a832008-03-24 15:01:56 -04004572
Chris Mason3f157a22008-06-25 16:01:31 -04004573/*
4574 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4575 * searching until it gets past min_objectid or finds an item of 'type'
4576 *
4577 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4578 */
Chris Mason0b86a832008-03-24 15:01:56 -04004579int btrfs_previous_item(struct btrfs_root *root,
4580 struct btrfs_path *path, u64 min_objectid,
4581 int type)
4582{
4583 struct btrfs_key found_key;
4584 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004585 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004586 int ret;
4587
Chris Masond3977122009-01-05 21:25:51 -05004588 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004589 if (path->slots[0] == 0) {
4590 ret = btrfs_prev_leaf(root, path);
4591 if (ret != 0)
4592 return ret;
4593 } else {
4594 path->slots[0]--;
4595 }
4596 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004597 nritems = btrfs_header_nritems(leaf);
4598 if (nritems == 0)
4599 return 1;
4600 if (path->slots[0] == nritems)
4601 path->slots[0]--;
4602
Chris Mason0b86a832008-03-24 15:01:56 -04004603 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004604 if (found_key.objectid < min_objectid)
4605 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004606 if (found_key.type == type)
4607 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004608 if (found_key.objectid == min_objectid &&
4609 found_key.type < type)
4610 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004611 }
4612 return 1;
4613}
Wang Shilongade2e0b2014-01-12 21:38:33 +08004614
4615/*
4616 * search in extent tree to find a previous Metadata/Data extent item with
4617 * min objecitd.
4618 *
4619 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4620 */
4621int btrfs_previous_extent_item(struct btrfs_root *root,
4622 struct btrfs_path *path, u64 min_objectid)
4623{
4624 struct btrfs_key found_key;
4625 struct extent_buffer *leaf;
4626 u32 nritems;
4627 int ret;
4628
4629 while (1) {
4630 if (path->slots[0] == 0) {
Wang Shilongade2e0b2014-01-12 21:38:33 +08004631 ret = btrfs_prev_leaf(root, path);
4632 if (ret != 0)
4633 return ret;
4634 } else {
4635 path->slots[0]--;
4636 }
4637 leaf = path->nodes[0];
4638 nritems = btrfs_header_nritems(leaf);
4639 if (nritems == 0)
4640 return 1;
4641 if (path->slots[0] == nritems)
4642 path->slots[0]--;
4643
4644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4645 if (found_key.objectid < min_objectid)
4646 break;
4647 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
4648 found_key.type == BTRFS_METADATA_ITEM_KEY)
4649 return 0;
4650 if (found_key.objectid == min_objectid &&
4651 found_key.type < BTRFS_EXTENT_ITEM_KEY)
4652 break;
4653 }
4654 return 1;
4655}