blob: 24658b5a578708f8b649a634e0503545e3930881 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
Chris Masond352ac62008-09-29 15:18:18 -04003 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Masona6b6e752007-10-15 16:22:39 -04006#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +02008#include <linux/rbtree.h>
David Sterbaadf02122017-05-31 19:44:31 +02009#include <linux/mm.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050010#include "ctree.h"
11#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040012#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040013#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040014#include "locking.h"
Nikolay Borisovde37aa52018-10-30 16:43:24 +020015#include "volumes.h"
Qu Wenruof616f5c2019-01-23 15:15:17 +080016#include "qgroup.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050017
Chris Masone089f052007-03-16 16:20:31 -040018static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
19 *root, struct btrfs_path *path, int level);
Omar Sandoval310712b2017-01-17 23:24:37 -080020static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
21 const struct btrfs_key *ins_key, struct btrfs_path *path,
22 int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040023static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040024 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040025 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040026static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -040027 struct extent_buffer *dst_buf,
28 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000029static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
30 int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050031
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020032static const struct btrfs_csums {
33 u16 size;
34 const char *name;
David Sterbab4e967b2019-10-08 18:41:33 +020035 const char *driver;
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020036} btrfs_csums[] = {
37 [BTRFS_CSUM_TYPE_CRC32] = { .size = 4, .name = "crc32c" },
Johannes Thumshirn3951e7f2019-10-07 11:11:01 +020038 [BTRFS_CSUM_TYPE_XXHASH] = { .size = 8, .name = "xxhash64" },
Johannes Thumshirn3831bf02019-10-07 11:11:02 +020039 [BTRFS_CSUM_TYPE_SHA256] = { .size = 32, .name = "sha256" },
David Sterba352ae072019-10-07 11:11:02 +020040 [BTRFS_CSUM_TYPE_BLAKE2] = { .size = 32, .name = "blake2b",
41 .driver = "blake2b-256" },
Johannes Thumshirnaf024ed2019-08-30 13:36:09 +020042};
43
44int btrfs_super_csum_size(const struct btrfs_super_block *s)
45{
46 u16 t = btrfs_super_csum_type(s);
47 /*
48 * csum type is validated at mount time
49 */
50 return btrfs_csums[t].size;
51}
52
53const char *btrfs_super_csum_name(u16 csum_type)
54{
55 /* csum type is validated at mount time */
56 return btrfs_csums[csum_type].name;
57}
58
David Sterbab4e967b2019-10-08 18:41:33 +020059/*
60 * Return driver name if defined, otherwise the name that's also a valid driver
61 * name
62 */
63const char *btrfs_super_csum_driver(u16 csum_type)
64{
65 /* csum type is validated at mount time */
66 return btrfs_csums[csum_type].driver ?:
67 btrfs_csums[csum_type].name;
68}
69
David Sterbaf7cea562019-10-07 11:11:03 +020070size_t __const btrfs_get_num_csums(void)
71{
72 return ARRAY_SIZE(btrfs_csums);
73}
74
Chris Mason2c90e5d2007-04-02 10:50:19 -040075struct btrfs_path *btrfs_alloc_path(void)
76{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090077 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040078}
79
Chris Masond352ac62008-09-29 15:18:18 -040080/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040081void btrfs_free_path(struct btrfs_path *p)
82{
Jesper Juhlff175d52010-12-25 21:22:30 +000083 if (!p)
84 return;
David Sterbab3b4aa72011-04-21 01:20:15 +020085 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040086 kmem_cache_free(btrfs_path_cachep, p);
87}
88
Chris Masond352ac62008-09-29 15:18:18 -040089/*
90 * path release drops references on the extent buffers in the path
91 * and it drops any locks held by this path
92 *
93 * It is safe to call this on paths that no locks or extent buffers held.
94 */
David Sterbab3b4aa72011-04-21 01:20:15 +020095noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -050096{
97 int i;
Chris Masona2135012008-06-25 16:01:30 -040098
Chris Mason234b63a2007-03-13 10:46:10 -040099 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400100 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500101 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400102 continue;
103 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400104 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400105 p->locks[i] = 0;
106 }
Chris Mason5f39d392007-10-15 16:14:19 -0400107 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400108 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500109 }
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
113 * safely gets a reference on the root node of a tree. A lock
114 * is not taken, so a concurrent writer may put a different node
115 * at the root of the tree. See btrfs_lock_root_node for the
116 * looping required.
117 *
118 * The extent buffer returned by this has a reference taken, so
119 * it won't disappear. It may stop being the root of the tree
120 * at any time because there are no locks held.
121 */
Chris Mason925baed2008-06-25 16:01:30 -0400122struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
123{
124 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400125
Josef Bacik3083ee22012-03-09 16:01:49 -0500126 while (1) {
127 rcu_read_lock();
128 eb = rcu_dereference(root->node);
129
130 /*
131 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400132 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500133 * the inc_not_zero dance and if it doesn't work then
134 * synchronize_rcu and try again.
135 */
136 if (atomic_inc_not_zero(&eb->refs)) {
137 rcu_read_unlock();
138 break;
139 }
140 rcu_read_unlock();
141 synchronize_rcu();
142 }
Chris Mason925baed2008-06-25 16:01:30 -0400143 return eb;
144}
145
Chris Masond352ac62008-09-29 15:18:18 -0400146/* loop around taking references on and locking the root node of the
147 * tree until you end up with a lock on the root. A locked buffer
148 * is returned, with a reference held.
149 */
Chris Mason925baed2008-06-25 16:01:30 -0400150struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
151{
152 struct extent_buffer *eb;
153
Chris Masond3977122009-01-05 21:25:51 -0500154 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400155 eb = btrfs_root_node(root);
156 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400157 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400158 break;
Chris Mason925baed2008-06-25 16:01:30 -0400159 btrfs_tree_unlock(eb);
160 free_extent_buffer(eb);
161 }
162 return eb;
163}
164
Chris Masonbd681512011-07-16 15:23:14 -0400165/* loop around taking references on and locking the root node of the
166 * tree until you end up with a lock on the root. A locked buffer
167 * is returned, with a reference held.
168 */
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400169struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400170{
171 struct extent_buffer *eb;
172
173 while (1) {
174 eb = btrfs_root_node(root);
175 btrfs_tree_read_lock(eb);
176 if (eb == root->node)
177 break;
178 btrfs_tree_read_unlock(eb);
179 free_extent_buffer(eb);
180 }
181 return eb;
182}
183
Chris Masond352ac62008-09-29 15:18:18 -0400184/* cowonly root (everything not a reference counted cow subvolume), just get
185 * put onto a simple dirty list. transaction.c walks this to make sure they
186 * get properly updated on disk.
187 */
Chris Mason0b86a832008-03-24 15:01:56 -0400188static void add_root_to_dirty_list(struct btrfs_root *root)
189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400190 struct btrfs_fs_info *fs_info = root->fs_info;
191
Josef Bacike7070be2014-12-16 08:54:43 -0800192 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
193 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
194 return;
195
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400196 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800197 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
198 /* Want the extent tree to be the last on the list */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900199 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
Josef Bacike7070be2014-12-16 08:54:43 -0800200 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400201 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800202 else
203 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400204 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400205 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400206 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400207}
208
Chris Masond352ac62008-09-29 15:18:18 -0400209/*
210 * used by snapshot creation to make a copy of a root for a tree with
211 * a given objectid. The buffer with the new root node is returned in
212 * cow_ret, and this func returns zero on success or a negative error code.
213 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500214int btrfs_copy_root(struct btrfs_trans_handle *trans,
215 struct btrfs_root *root,
216 struct extent_buffer *buf,
217 struct extent_buffer **cow_ret, u64 new_root_objectid)
218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400219 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500220 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 int ret = 0;
222 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400223 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500224
Miao Xie27cdeb72014-04-02 19:51:05 +0800225 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400226 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +0800227 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
228 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229
230 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400231 if (level == 0)
232 btrfs_item_key(buf, &disk_key, 0);
233 else
234 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400235
David Sterba4d75f8a2014-06-15 01:54:12 +0200236 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
237 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400238 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 return PTR_ERR(cow);
240
David Sterba58e80122016-11-08 18:30:31 +0100241 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500242 btrfs_set_header_bytenr(cow, cow->start);
243 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400244 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
245 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
246 BTRFS_HEADER_FLAG_RELOC);
247 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
248 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
249 else
250 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500251
Nikolay Borisovde37aa52018-10-30 16:43:24 +0200252 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -0500253
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700256 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400257 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700258 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500259
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 if (ret)
261 return ret;
262
263 btrfs_mark_buffer_dirty(cow);
264 *cow_ret = cow;
265 return 0;
266}
267
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200268enum mod_log_op {
269 MOD_LOG_KEY_REPLACE,
270 MOD_LOG_KEY_ADD,
271 MOD_LOG_KEY_REMOVE,
272 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
273 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
274 MOD_LOG_MOVE_KEYS,
275 MOD_LOG_ROOT_REPLACE,
276};
277
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200278struct tree_mod_root {
279 u64 logical;
280 u8 level;
281};
282
283struct tree_mod_elem {
284 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530285 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200286 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200287 enum mod_log_op op;
288
289 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
290 int slot;
291
292 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
293 u64 generation;
294
295 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
296 struct btrfs_disk_key key;
297 u64 blockptr;
298
299 /* this is used for op == MOD_LOG_MOVE_KEYS */
David Sterbab6dfa352018-03-05 15:31:18 +0100300 struct {
301 int dst_slot;
302 int nr_items;
303 } move;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200304
305 /* this is used for op == MOD_LOG_ROOT_REPLACE */
306 struct tree_mod_root old_root;
307};
308
Jan Schmidt097b8a72012-06-21 11:08:04 +0200309/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700310 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000311 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700312static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000313{
314 return atomic64_inc_return(&fs_info->tree_mod_seq);
315}
316
317/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200318 * This adds a new blocker to the tree mod log's blocker list if the @elem
319 * passed does not already have a sequence number set. So when a caller expects
320 * to record tree modifications, it should ensure to set elem->seq to zero
321 * before calling btrfs_get_tree_mod_seq.
322 * Returns a fresh, unused tree log modification sequence number, even if no new
323 * blocker was added.
324 */
325u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
326 struct seq_list *elem)
327{
David Sterbab1a09f12018-03-05 15:43:41 +0100328 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200329 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200330 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700331 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200332 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
333 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200334 spin_unlock(&fs_info->tree_mod_seq_lock);
David Sterbab1a09f12018-03-05 15:43:41 +0100335 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200336
Josef Bacikfcebe452014-05-13 17:30:47 -0700337 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200338}
339
340void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
341 struct seq_list *elem)
342{
343 struct rb_root *tm_root;
344 struct rb_node *node;
345 struct rb_node *next;
346 struct seq_list *cur_elem;
347 struct tree_mod_elem *tm;
348 u64 min_seq = (u64)-1;
349 u64 seq_putting = elem->seq;
350
351 if (!seq_putting)
352 return;
353
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200354 spin_lock(&fs_info->tree_mod_seq_lock);
355 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200356 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200357
358 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200359 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200360 if (seq_putting > cur_elem->seq) {
361 /*
362 * blocker with lower sequence number exists, we
363 * cannot remove anything from the log
364 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200365 spin_unlock(&fs_info->tree_mod_seq_lock);
366 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200367 }
368 min_seq = cur_elem->seq;
369 }
370 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200371 spin_unlock(&fs_info->tree_mod_seq_lock);
372
373 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200374 * anything that's lower than the lowest existing (read: blocked)
375 * sequence number can be removed from the tree.
376 */
David Sterbab1a09f12018-03-05 15:43:41 +0100377 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 tm_root = &fs_info->tree_mod_log;
379 for (node = rb_first(tm_root); node; node = next) {
380 next = rb_next(node);
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800381 tm = rb_entry(node, struct tree_mod_elem, node);
Filipe Manana6609fee2019-12-06 12:27:39 +0000382 if (tm->seq >= min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200383 continue;
384 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200385 kfree(tm);
386 }
David Sterbab1a09f12018-03-05 15:43:41 +0100387 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200388}
389
390/*
391 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530392 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200393 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530394 * The 'start address' is the logical address of the *new* root node
395 * for root replace operations, or the logical address of the affected
396 * block for all other operations.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200397 */
398static noinline int
399__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
400{
401 struct rb_root *tm_root;
402 struct rb_node **new;
403 struct rb_node *parent = NULL;
404 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200405
David Sterba73e82fe2019-03-27 16:19:55 +0100406 lockdep_assert_held_write(&fs_info->tree_mod_log_lock);
407
Josef Bacikfcebe452014-05-13 17:30:47 -0700408 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 tm_root = &fs_info->tree_mod_log;
411 new = &tm_root->rb_node;
412 while (*new) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800413 cur = rb_entry(*new, struct tree_mod_elem, node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200414 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530415 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530417 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200418 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200419 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200421 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200422 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000423 else
424 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200425 }
426
427 rb_link_node(&tm->node, parent, new);
428 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000429 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200430}
431
Jan Schmidt097b8a72012-06-21 11:08:04 +0200432/*
433 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
434 * returns zero with the tree_mod_log_lock acquired. The caller must hold
435 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbab1a09f12018-03-05 15:43:41 +0100436 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200437 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200438static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
439 struct extent_buffer *eb) {
440 smp_mb();
441 if (list_empty(&(fs_info)->tree_mod_seq_list))
442 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200443 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200444 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000445
David Sterbab1a09f12018-03-05 15:43:41 +0100446 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000447 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbab1a09f12018-03-05 15:43:41 +0100448 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000449 return 1;
450 }
451
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200452 return 0;
453}
454
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000455/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
456static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
457 struct extent_buffer *eb)
458{
459 smp_mb();
460 if (list_empty(&(fs_info)->tree_mod_seq_list))
461 return 0;
462 if (eb && btrfs_header_level(eb) == 0)
463 return 0;
464
465 return 1;
466}
467
468static struct tree_mod_elem *
469alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
470 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200471{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200472 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200473
Josef Bacikc8cc6342013-07-01 16:18:19 -0400474 tm = kzalloc(sizeof(*tm), flags);
475 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000476 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200477
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530478 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200479 if (op != MOD_LOG_KEY_ADD) {
480 btrfs_node_key(eb, &tm->key, slot);
481 tm->blockptr = btrfs_node_blockptr(eb, slot);
482 }
483 tm->op = op;
484 tm->slot = slot;
485 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000486 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200487
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000488 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200489}
490
David Sterbae09c2ef2018-03-05 15:09:03 +0100491static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
492 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200493{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000494 struct tree_mod_elem *tm;
495 int ret;
496
David Sterbae09c2ef2018-03-05 15:09:03 +0100497 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200498 return 0;
499
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000500 tm = alloc_tree_mod_elem(eb, slot, op, flags);
501 if (!tm)
502 return -ENOMEM;
503
David Sterbae09c2ef2018-03-05 15:09:03 +0100504 if (tree_mod_dont_log(eb->fs_info, eb)) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000505 kfree(tm);
506 return 0;
507 }
508
David Sterbae09c2ef2018-03-05 15:09:03 +0100509 ret = __tree_mod_log_insert(eb->fs_info, tm);
David Sterbab1a09f12018-03-05 15:43:41 +0100510 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000511 if (ret)
512 kfree(tm);
513
514 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200515}
516
David Sterba6074d452018-03-05 15:03:52 +0100517static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
518 int dst_slot, int src_slot, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200519{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000520 struct tree_mod_elem *tm = NULL;
521 struct tree_mod_elem **tm_list = NULL;
522 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200523 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000524 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200525
David Sterba6074d452018-03-05 15:03:52 +0100526 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200527 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200528
David Sterba176ef8f2017-03-28 14:35:01 +0200529 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000530 if (!tm_list)
531 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200532
David Sterba176ef8f2017-03-28 14:35:01 +0200533 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000534 if (!tm) {
535 ret = -ENOMEM;
536 goto free_tms;
537 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200538
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530539 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200540 tm->slot = src_slot;
541 tm->move.dst_slot = dst_slot;
542 tm->move.nr_items = nr_items;
543 tm->op = MOD_LOG_MOVE_KEYS;
544
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000545 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
546 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
David Sterba176ef8f2017-03-28 14:35:01 +0200547 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000548 if (!tm_list[i]) {
549 ret = -ENOMEM;
550 goto free_tms;
551 }
552 }
553
David Sterba6074d452018-03-05 15:03:52 +0100554 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000555 goto free_tms;
556 locked = 1;
557
558 /*
559 * When we override something during the move, we log these removals.
560 * This can only happen when we move towards the beginning of the
561 * buffer, i.e. dst_slot < src_slot.
562 */
563 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
David Sterba6074d452018-03-05 15:03:52 +0100564 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000565 if (ret)
566 goto free_tms;
567 }
568
David Sterba6074d452018-03-05 15:03:52 +0100569 ret = __tree_mod_log_insert(eb->fs_info, tm);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000570 if (ret)
571 goto free_tms;
David Sterbab1a09f12018-03-05 15:43:41 +0100572 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000573 kfree(tm_list);
574
575 return 0;
576free_tms:
577 for (i = 0; i < nr_items; i++) {
578 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
David Sterba6074d452018-03-05 15:03:52 +0100579 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000580 kfree(tm_list[i]);
581 }
582 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100583 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000584 kfree(tm_list);
585 kfree(tm);
586
587 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200588}
589
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000590static inline int
591__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
592 struct tree_mod_elem **tm_list,
593 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200594{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000595 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200596 int ret;
597
Jan Schmidt097b8a72012-06-21 11:08:04 +0200598 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000599 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
600 if (ret) {
601 for (j = nritems - 1; j > i; j--)
602 rb_erase(&tm_list[j]->node,
603 &fs_info->tree_mod_log);
604 return ret;
605 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200606 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000607
608 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200609}
610
David Sterba95b757c2018-03-05 15:22:30 +0100611static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
612 struct extent_buffer *new_root, int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200613{
David Sterba95b757c2018-03-05 15:22:30 +0100614 struct btrfs_fs_info *fs_info = old_root->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000615 struct tree_mod_elem *tm = NULL;
616 struct tree_mod_elem **tm_list = NULL;
617 int nritems = 0;
618 int ret = 0;
619 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200620
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000621 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200622 return 0;
623
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000624 if (log_removal && btrfs_header_level(old_root) > 0) {
625 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100626 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
David Sterbabcc8e072017-03-28 14:35:42 +0200627 GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000628 if (!tm_list) {
629 ret = -ENOMEM;
630 goto free_tms;
631 }
632 for (i = 0; i < nritems; i++) {
633 tm_list[i] = alloc_tree_mod_elem(old_root, i,
David Sterbabcc8e072017-03-28 14:35:42 +0200634 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000635 if (!tm_list[i]) {
636 ret = -ENOMEM;
637 goto free_tms;
638 }
639 }
640 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000641
David Sterbabcc8e072017-03-28 14:35:42 +0200642 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000643 if (!tm) {
644 ret = -ENOMEM;
645 goto free_tms;
646 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200647
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530648 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200649 tm->old_root.logical = old_root->start;
650 tm->old_root.level = btrfs_header_level(old_root);
651 tm->generation = btrfs_header_generation(old_root);
652 tm->op = MOD_LOG_ROOT_REPLACE;
653
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000654 if (tree_mod_dont_log(fs_info, NULL))
655 goto free_tms;
656
657 if (tm_list)
658 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
659 if (!ret)
660 ret = __tree_mod_log_insert(fs_info, tm);
661
David Sterbab1a09f12018-03-05 15:43:41 +0100662 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000663 if (ret)
664 goto free_tms;
665 kfree(tm_list);
666
667 return ret;
668
669free_tms:
670 if (tm_list) {
671 for (i = 0; i < nritems; i++)
672 kfree(tm_list[i]);
673 kfree(tm_list);
674 }
675 kfree(tm);
676
677 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200678}
679
680static struct tree_mod_elem *
681__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
682 int smallest)
683{
684 struct rb_root *tm_root;
685 struct rb_node *node;
686 struct tree_mod_elem *cur = NULL;
687 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200688
David Sterbab1a09f12018-03-05 15:43:41 +0100689 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200690 tm_root = &fs_info->tree_mod_log;
691 node = tm_root->rb_node;
692 while (node) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800693 cur = rb_entry(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530694 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200695 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530696 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200698 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200699 node = node->rb_left;
700 } else if (!smallest) {
701 /* we want the node with the highest seq */
702 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200703 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200704 found = cur;
705 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200706 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200707 /* we want the node with the smallest seq */
708 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200709 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200710 found = cur;
711 node = node->rb_right;
712 } else {
713 found = cur;
714 break;
715 }
716 }
David Sterbab1a09f12018-03-05 15:43:41 +0100717 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200718
719 return found;
720}
721
722/*
723 * this returns the element from the log with the smallest time sequence
724 * value that's in the log (the oldest log item). any element with a time
725 * sequence lower than min_seq will be ignored.
726 */
727static struct tree_mod_elem *
728tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
729 u64 min_seq)
730{
731 return __tree_mod_log_search(fs_info, start, min_seq, 1);
732}
733
734/*
735 * this returns the element from the log with the largest time sequence
736 * value that's in the log (the most recent log item). any element with
737 * a time sequence lower than min_seq will be ignored.
738 */
739static struct tree_mod_elem *
740tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
741{
742 return __tree_mod_log_search(fs_info, start, min_seq, 0);
743}
744
David Sterbaed874f02019-03-20 14:22:04 +0100745static noinline int tree_mod_log_eb_copy(struct extent_buffer *dst,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200746 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000747 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200748{
David Sterbaed874f02019-03-20 14:22:04 +0100749 struct btrfs_fs_info *fs_info = dst->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000750 int ret = 0;
751 struct tree_mod_elem **tm_list = NULL;
752 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200753 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000754 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200755
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000756 if (!tree_mod_need_log(fs_info, NULL))
757 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200758
Josef Bacikc8cc6342013-07-01 16:18:19 -0400759 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000760 return 0;
761
David Sterba31e818f2015-02-20 18:00:26 +0100762 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000763 GFP_NOFS);
764 if (!tm_list)
765 return -ENOMEM;
766
767 tm_list_add = tm_list;
768 tm_list_rem = tm_list + nr_items;
769 for (i = 0; i < nr_items; i++) {
770 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
771 MOD_LOG_KEY_REMOVE, GFP_NOFS);
772 if (!tm_list_rem[i]) {
773 ret = -ENOMEM;
774 goto free_tms;
775 }
776
777 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
778 MOD_LOG_KEY_ADD, GFP_NOFS);
779 if (!tm_list_add[i]) {
780 ret = -ENOMEM;
781 goto free_tms;
782 }
783 }
784
785 if (tree_mod_dont_log(fs_info, NULL))
786 goto free_tms;
787 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200788
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000790 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
791 if (ret)
792 goto free_tms;
793 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
794 if (ret)
795 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200796 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000797
David Sterbab1a09f12018-03-05 15:43:41 +0100798 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000799 kfree(tm_list);
800
801 return 0;
802
803free_tms:
804 for (i = 0; i < nr_items * 2; i++) {
805 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
806 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
807 kfree(tm_list[i]);
808 }
809 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100810 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000811 kfree(tm_list);
812
813 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200814}
815
David Sterbadb7279a2018-03-05 15:14:25 +0100816static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200817{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000818 struct tree_mod_elem **tm_list = NULL;
819 int nritems = 0;
820 int i;
821 int ret = 0;
822
823 if (btrfs_header_level(eb) == 0)
824 return 0;
825
David Sterbadb7279a2018-03-05 15:14:25 +0100826 if (!tree_mod_need_log(eb->fs_info, NULL))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000827 return 0;
828
829 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100830 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000831 if (!tm_list)
832 return -ENOMEM;
833
834 for (i = 0; i < nritems; i++) {
835 tm_list[i] = alloc_tree_mod_elem(eb, i,
836 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
837 if (!tm_list[i]) {
838 ret = -ENOMEM;
839 goto free_tms;
840 }
841 }
842
David Sterbadb7279a2018-03-05 15:14:25 +0100843 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000844 goto free_tms;
845
David Sterbadb7279a2018-03-05 15:14:25 +0100846 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
David Sterbab1a09f12018-03-05 15:43:41 +0100847 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000848 if (ret)
849 goto free_tms;
850 kfree(tm_list);
851
852 return 0;
853
854free_tms:
855 for (i = 0; i < nritems; i++)
856 kfree(tm_list[i]);
857 kfree(tm_list);
858
859 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200860}
861
Chris Masond352ac62008-09-29 15:18:18 -0400862/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 * check if the tree block can be shared by multiple trees
864 */
865int btrfs_block_can_be_shared(struct btrfs_root *root,
866 struct extent_buffer *buf)
867{
868 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400869 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400870 * are never shared. If a block was allocated after the last
871 * snapshot and the block was not allocated by tree relocation,
872 * we know the block is not shared.
873 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800874 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400875 buf != root->node && buf != root->commit_root &&
876 (btrfs_header_generation(buf) <=
877 btrfs_root_last_snapshot(&root->root_item) ||
878 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
879 return 1;
Nikolay Borisova79865c2018-06-21 09:45:00 +0300880
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 return 0;
882}
883
884static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
885 struct btrfs_root *root,
886 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400887 struct extent_buffer *cow,
888 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400889{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400890 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891 u64 refs;
892 u64 owner;
893 u64 flags;
894 u64 new_flags = 0;
895 int ret;
896
897 /*
898 * Backrefs update rules:
899 *
900 * Always use full backrefs for extent pointers in tree block
901 * allocated by tree relocation.
902 *
903 * If a shared tree block is no longer referenced by its owner
904 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
905 * use full backrefs for extent pointers in tree block.
906 *
907 * If a tree block is been relocating
908 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
909 * use full backrefs for extent pointers in tree block.
910 * The reason for this is some operations (such as drop tree)
911 * are only allowed for blocks use full backrefs.
912 */
913
914 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400915 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500916 btrfs_header_level(buf), 1,
917 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700918 if (ret)
919 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700920 if (refs == 0) {
921 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400922 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700923 return ret;
924 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 } else {
926 refs = 1;
927 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
928 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
929 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
930 else
931 flags = 0;
932 }
933
934 owner = btrfs_header_owner(buf);
935 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
936 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
937
938 if (refs > 1) {
939 if ((owner == root->root_key.objectid ||
940 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
941 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700942 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500943 if (ret)
944 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945
946 if (root->root_key.objectid ==
947 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700948 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500949 if (ret)
950 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700951 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500952 if (ret)
953 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 }
955 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
956 } else {
957
958 if (root->root_key.objectid ==
959 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700960 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700962 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500963 if (ret)
964 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 }
966 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400967 int level = btrfs_header_level(buf);
968
David Sterbaf5c8daa2019-03-20 11:43:36 +0100969 ret = btrfs_set_disk_extent_flags(trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400970 buf->start,
971 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400972 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700973 if (ret)
974 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 }
976 } else {
977 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
978 if (root->root_key.objectid ==
979 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700980 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700982 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500983 if (ret)
984 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700985 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500986 if (ret)
987 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400988 }
David Sterba6a884d7d2019-03-20 14:30:02 +0100989 btrfs_clean_tree_block(buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400990 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 }
992 return 0;
993}
994
Filipe Mananaa6279472019-01-25 11:48:51 +0000995static struct extent_buffer *alloc_tree_block_no_bg_flush(
996 struct btrfs_trans_handle *trans,
997 struct btrfs_root *root,
998 u64 parent_start,
999 const struct btrfs_disk_key *disk_key,
1000 int level,
1001 u64 hint,
1002 u64 empty_size)
1003{
1004 struct btrfs_fs_info *fs_info = root->fs_info;
1005 struct extent_buffer *ret;
1006
1007 /*
1008 * If we are COWing a node/leaf from the extent, chunk, device or free
1009 * space trees, make sure that we do not finish block group creation of
1010 * pending block groups. We do this to avoid a deadlock.
1011 * COWing can result in allocation of a new chunk, and flushing pending
1012 * block groups (btrfs_create_pending_block_groups()) can be triggered
1013 * when finishing allocation of a new chunk. Creation of a pending block
1014 * group modifies the extent, chunk, device and free space trees,
1015 * therefore we could deadlock with ourselves since we are holding a
1016 * lock on an extent buffer that btrfs_create_pending_block_groups() may
1017 * try to COW later.
1018 * For similar reasons, we also need to delay flushing pending block
1019 * groups when splitting a leaf or node, from one of those trees, since
1020 * we are holding a write lock on it and its parent or when inserting a
1021 * new root node for one of those trees.
1022 */
1023 if (root == fs_info->extent_root ||
1024 root == fs_info->chunk_root ||
1025 root == fs_info->dev_root ||
1026 root == fs_info->free_space_root)
1027 trans->can_flush_pending_bgs = false;
1028
1029 ret = btrfs_alloc_tree_block(trans, root, parent_start,
1030 root->root_key.objectid, disk_key, level,
1031 hint, empty_size);
1032 trans->can_flush_pending_bgs = true;
1033
1034 return ret;
1035}
1036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037/*
Chris Masond3977122009-01-05 21:25:51 -05001038 * does the dirty work in cow of a single block. The parent block (if
1039 * supplied) is updated to point to the new cow copy. The new buffer is marked
1040 * dirty and returned locked. If you modify the block it needs to be marked
1041 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001042 *
1043 * search_start -- an allocation hint for the new block
1044 *
Chris Masond3977122009-01-05 21:25:51 -05001045 * empty_size -- a hint that you plan on doing more cow. This is the size in
1046 * bytes the allocator should try to find free next to the block it returns.
1047 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001048 */
Chris Masond3977122009-01-05 21:25:51 -05001049static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001050 struct btrfs_root *root,
1051 struct extent_buffer *buf,
1052 struct extent_buffer *parent, int parent_slot,
1053 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001054 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001055{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001056 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001057 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001058 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001059 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001060 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001061 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001062 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001063
Chris Mason925baed2008-06-25 16:01:30 -04001064 if (*cow_ret == buf)
1065 unlock_orig = 1;
1066
Chris Masonb9447ef82009-03-09 11:45:38 -04001067 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001068
Miao Xie27cdeb72014-04-02 19:51:05 +08001069 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001070 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +08001071 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1072 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001073
Chris Mason7bb86312007-12-11 09:25:06 -05001074 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001075
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001076 if (level == 0)
1077 btrfs_item_key(buf, &disk_key, 0);
1078 else
1079 btrfs_node_key(buf, &disk_key, 0);
1080
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001081 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1082 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001083
Filipe Mananaa6279472019-01-25 11:48:51 +00001084 cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
1085 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001086 if (IS_ERR(cow))
1087 return PTR_ERR(cow);
1088
Chris Masonb4ce94d2009-02-04 09:25:08 -05001089 /* cow is set to blocking by btrfs_init_new_buffer */
1090
David Sterba58e80122016-11-08 18:30:31 +01001091 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001092 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001093 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001094 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1095 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1096 BTRFS_HEADER_FLAG_RELOC);
1097 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1098 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1099 else
1100 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001101
Nikolay Borisovde37aa52018-10-30 16:43:24 +02001102 write_extent_buffer_fsid(cow, fs_info->fs_devices->metadata_uuid);
Yan Zheng2b820322008-11-17 21:11:30 -05001103
Mark Fashehbe1a5562011-08-08 13:20:18 -07001104 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001105 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001106 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001107 return ret;
1108 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001109
Miao Xie27cdeb72014-04-02 19:51:05 +08001110 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001111 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001112 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001113 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001114 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001115 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001116 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001117
Chris Mason6702ed42007-08-07 16:15:09 -04001118 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001119 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001120 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1121 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1122 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001123
David Sterba67439da2019-10-08 13:28:47 +02001124 atomic_inc(&cow->refs);
David Sterbad9d19a02018-03-05 16:35:29 +01001125 ret = tree_mod_log_insert_root(root->node, cow, 1);
1126 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001127 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001128
Yan, Zhengf0486c62010-05-16 10:46:25 -04001129 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001130 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001131 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001132 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001133 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001134 WARN_ON(trans->transid != btrfs_header_generation(parent));
David Sterbae09c2ef2018-03-05 15:09:03 +01001135 tree_mod_log_insert_key(parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001136 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001137 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001138 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001139 btrfs_set_node_ptr_generation(parent, parent_slot,
1140 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001141 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001142 if (last_ref) {
David Sterbadb7279a2018-03-05 15:14:25 +01001143 ret = tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001144 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001145 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001146 return ret;
1147 }
1148 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001149 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001150 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001151 }
Chris Mason925baed2008-06-25 16:01:30 -04001152 if (unlock_orig)
1153 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001154 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001155 btrfs_mark_buffer_dirty(cow);
1156 *cow_ret = cow;
1157 return 0;
1158}
1159
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001160/*
1161 * returns the logical address of the oldest predecessor of the given root.
1162 * entries older than time_seq are ignored.
1163 */
David Sterbabcd24da2018-03-05 15:33:18 +01001164static struct tree_mod_elem *__tree_mod_log_oldest_root(
1165 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001166{
1167 struct tree_mod_elem *tm;
1168 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001169 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001170 int looped = 0;
1171
1172 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001173 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001174
1175 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301176 * the very last operation that's logged for a root is the
1177 * replacement operation (if it is replaced at all). this has
1178 * the logical address of the *new* root, making it the very
1179 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001180 */
1181 while (1) {
David Sterbabcd24da2018-03-05 15:33:18 +01001182 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001183 time_seq);
1184 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001185 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001186 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001187 * if there are no tree operation for the oldest root, we simply
1188 * return it. this should only happen if that (old) root is at
1189 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001190 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001191 if (!tm)
1192 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001193
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001194 /*
1195 * if there's an operation that's not a root replacement, we
1196 * found the oldest version of our root. normally, we'll find a
1197 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1198 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001199 if (tm->op != MOD_LOG_ROOT_REPLACE)
1200 break;
1201
1202 found = tm;
1203 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001204 looped = 1;
1205 }
1206
Jan Schmidta95236d2012-06-05 16:41:24 +02001207 /* if there's no old root to return, return what we found instead */
1208 if (!found)
1209 found = tm;
1210
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001211 return found;
1212}
1213
1214/*
1215 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001216 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001217 * time_seq).
1218 */
1219static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001220__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1221 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001222{
1223 u32 n;
1224 struct rb_node *next;
1225 struct tree_mod_elem *tm = first_tm;
1226 unsigned long o_dst;
1227 unsigned long o_src;
1228 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1229
1230 n = btrfs_header_nritems(eb);
David Sterbab1a09f12018-03-05 15:43:41 +01001231 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001232 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001233 /*
1234 * all the operations are recorded with the operator used for
1235 * the modification. as we're going backwards, we do the
1236 * opposite of each operation here.
1237 */
1238 switch (tm->op) {
1239 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1240 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001241 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001242 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001243 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001244 btrfs_set_node_key(eb, &tm->key, tm->slot);
1245 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1246 btrfs_set_node_ptr_generation(eb, tm->slot,
1247 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001248 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001249 break;
1250 case MOD_LOG_KEY_REPLACE:
1251 BUG_ON(tm->slot >= n);
1252 btrfs_set_node_key(eb, &tm->key, tm->slot);
1253 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1254 btrfs_set_node_ptr_generation(eb, tm->slot,
1255 tm->generation);
1256 break;
1257 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001258 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001259 n--;
1260 break;
1261 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001262 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1263 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1264 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001265 tm->move.nr_items * p_size);
1266 break;
1267 case MOD_LOG_ROOT_REPLACE:
1268 /*
1269 * this operation is special. for roots, this must be
1270 * handled explicitly before rewinding.
1271 * for non-roots, this operation may exist if the node
1272 * was a root: root A -> child B; then A gets empty and
1273 * B is promoted to the new root. in the mod log, we'll
1274 * have a root-replace operation for B, a tree block
1275 * that is no root. we simply ignore that operation.
1276 */
1277 break;
1278 }
1279 next = rb_next(&tm->node);
1280 if (!next)
1281 break;
Geliang Tang6b4df8b2016-12-19 22:53:41 +08001282 tm = rb_entry(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301283 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001284 break;
1285 }
David Sterbab1a09f12018-03-05 15:43:41 +01001286 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001287 btrfs_set_header_nritems(eb, n);
1288}
1289
Jan Schmidt47fb0912013-04-13 13:19:55 +00001290/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001291 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001292 * is returned. If rewind operations happen, a fresh buffer is returned. The
1293 * returned buffer is always read-locked. If the returned buffer is not the
1294 * input buffer, the lock on the input buffer is released and the input buffer
1295 * is freed (its refcount is decremented).
1296 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001297static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001298tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1299 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001300{
1301 struct extent_buffer *eb_rewin;
1302 struct tree_mod_elem *tm;
1303
1304 if (!time_seq)
1305 return eb;
1306
1307 if (btrfs_header_level(eb) == 0)
1308 return eb;
1309
1310 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1311 if (!tm)
1312 return eb;
1313
Josef Bacik9ec72672013-08-07 16:57:23 -04001314 btrfs_set_path_blocking(path);
David Sterba300aa892018-04-04 02:00:17 +02001315 btrfs_set_lock_blocking_read(eb);
Josef Bacik9ec72672013-08-07 16:57:23 -04001316
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001317 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1318 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001319 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001320 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001321 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001322 free_extent_buffer(eb);
1323 return NULL;
1324 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001325 btrfs_set_header_bytenr(eb_rewin, eb->start);
1326 btrfs_set_header_backref_rev(eb_rewin,
1327 btrfs_header_backref_rev(eb));
1328 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001329 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001330 } else {
1331 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001332 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001333 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001334 free_extent_buffer(eb);
1335 return NULL;
1336 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001337 }
1338
Josef Bacik9ec72672013-08-07 16:57:23 -04001339 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001340 free_extent_buffer(eb);
1341
Jan Schmidt47fb0912013-04-13 13:19:55 +00001342 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001343 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001344 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001345 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001346
1347 return eb_rewin;
1348}
1349
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001350/*
1351 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1352 * value. If there are no changes, the current root->root_node is returned. If
1353 * anything changed in between, there's a fresh buffer allocated on which the
1354 * rewind operations are done. In any case, the returned buffer is read locked.
1355 * Returns NULL on error (with no locks held).
1356 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001357static inline struct extent_buffer *
1358get_old_root(struct btrfs_root *root, u64 time_seq)
1359{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001360 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001361 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001362 struct extent_buffer *eb = NULL;
1363 struct extent_buffer *eb_root;
Filipe Mananaefad8a82019-08-12 19:14:29 +01001364 u64 eb_root_owner = 0;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001365 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001366 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001367 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001368 u64 logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001369 int level;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001370
Jan Schmidt30b04632013-04-13 13:19:54 +00001371 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001372 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001373 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001374 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001375
Jan Schmidta95236d2012-06-05 16:41:24 +02001376 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1377 old_root = &tm->old_root;
1378 old_generation = tm->generation;
1379 logical = old_root->logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001380 level = old_root->level;
Jan Schmidta95236d2012-06-05 16:41:24 +02001381 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001382 logical = eb_root->start;
Qu Wenruo581c1762018-03-29 09:08:11 +08001383 level = btrfs_header_level(eb_root);
Jan Schmidta95236d2012-06-05 16:41:24 +02001384 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001385
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001386 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001387 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001388 btrfs_tree_read_unlock(eb_root);
1389 free_extent_buffer(eb_root);
Qu Wenruo581c1762018-03-29 09:08:11 +08001390 old = read_tree_block(fs_info, logical, 0, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08001391 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1392 if (!IS_ERR(old))
1393 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001394 btrfs_warn(fs_info,
1395 "failed to read tree block %llu from get_old_root",
1396 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001397 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001398 eb = btrfs_clone_extent_buffer(old);
1399 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001400 }
1401 } else if (old_root) {
Filipe Mananaefad8a82019-08-12 19:14:29 +01001402 eb_root_owner = btrfs_header_owner(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001403 btrfs_tree_read_unlock(eb_root);
1404 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001405 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001406 } else {
David Sterba300aa892018-04-04 02:00:17 +02001407 btrfs_set_lock_blocking_read(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001408 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001409 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001410 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001411 }
1412
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001413 if (!eb)
1414 return NULL;
1415 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001416 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001417 btrfs_set_header_bytenr(eb, eb->start);
1418 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Filipe Mananaefad8a82019-08-12 19:14:29 +01001419 btrfs_set_header_owner(eb, eb_root_owner);
Jan Schmidta95236d2012-06-05 16:41:24 +02001420 btrfs_set_header_level(eb, old_root->level);
1421 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001422 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001423 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001424 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001425 else
1426 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001427 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001428
1429 return eb;
1430}
1431
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001432int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1433{
1434 struct tree_mod_elem *tm;
1435 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001436 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001437
David Sterbabcd24da2018-03-05 15:33:18 +01001438 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001439 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1440 level = tm->old_root.level;
1441 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001442 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001443 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001444 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001445
1446 return level;
1447}
1448
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001449static inline int should_cow_block(struct btrfs_trans_handle *trans,
1450 struct btrfs_root *root,
1451 struct extent_buffer *buf)
1452{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001453 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001454 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001455
David Sterbad1980132018-03-16 02:39:40 +01001456 /* Ensure we can see the FORCE_COW bit */
1457 smp_mb__before_atomic();
Liu Bof1ebcc72011-11-14 20:48:06 -05001458
1459 /*
1460 * We do not need to cow a block if
1461 * 1) this block is not created or changed in this transaction;
1462 * 2) this block does not belong to TREE_RELOC tree;
1463 * 3) the root is not forced COW.
1464 *
1465 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001466 * when we create snapshot during committing the transaction,
Andrea Gelmini52042d82018-11-28 12:05:13 +01001467 * after we've finished copying src root, we must COW the shared
Liu Bof1ebcc72011-11-14 20:48:06 -05001468 * block to ensure the metadata consistency.
1469 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001470 if (btrfs_header_generation(buf) == trans->transid &&
1471 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1472 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001473 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001474 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001475 return 0;
1476 return 1;
1477}
1478
Chris Masond352ac62008-09-29 15:18:18 -04001479/*
1480 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001481 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001482 * once per transaction, as long as it hasn't been written yet
1483 */
Chris Masond3977122009-01-05 21:25:51 -05001484noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001485 struct btrfs_root *root, struct extent_buffer *buf,
1486 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001487 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001488{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001489 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001490 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001491 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001492
Josef Bacik83354f02018-11-30 11:52:13 -05001493 if (test_bit(BTRFS_ROOT_DELETING, &root->state))
1494 btrfs_err(fs_info,
1495 "COW'ing blocks on a fs root that's being dropped");
1496
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001498 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001499 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001500 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001501
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001503 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001504 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001507 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001508 *cow_ret = buf;
1509 return 0;
1510 }
Chris Masonc4876852009-02-04 09:24:25 -05001511
Byongho Leeee221842015-12-15 01:42:10 +09001512 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001513
1514 if (parent)
David Sterba8bead252018-04-04 02:03:48 +02001515 btrfs_set_lock_blocking_write(parent);
1516 btrfs_set_lock_blocking_write(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001517
Qu Wenruof616f5c2019-01-23 15:15:17 +08001518 /*
1519 * Before CoWing this block for later modification, check if it's
1520 * the subtree root and do the delayed subtree trace if needed.
1521 *
1522 * Also We don't care about the error, as it's handled internally.
1523 */
1524 btrfs_qgroup_trace_subtree_after_cow(trans, root, buf);
Chris Masonf510cfe2007-10-15 16:14:48 -04001525 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001526 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001527
1528 trace_btrfs_cow_block(root, buf, *cow_ret);
1529
Chris Masonf510cfe2007-10-15 16:14:48 -04001530 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001531}
1532
Chris Masond352ac62008-09-29 15:18:18 -04001533/*
1534 * helper function for defrag to decide if two blocks pointed to by a
1535 * node are actually close by
1536 */
Chris Mason6b800532007-10-15 16:17:34 -04001537static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001538{
Chris Mason6b800532007-10-15 16:17:34 -04001539 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001540 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001541 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001542 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001543 return 0;
1544}
1545
Chris Mason081e9572007-11-06 10:26:24 -05001546/*
1547 * compare two keys in a memcmp fashion
1548 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001549static int comp_keys(const struct btrfs_disk_key *disk,
1550 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001551{
1552 struct btrfs_key k1;
1553
1554 btrfs_disk_key_to_cpu(&k1, disk);
1555
Diego Calleja20736ab2009-07-24 11:06:52 -04001556 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001557}
1558
Josef Bacikf3465ca2008-11-12 14:19:50 -05001559/*
1560 * same as comp_keys only with two btrfs_key's
1561 */
David Sterbae1f60a62019-10-01 19:57:39 +02001562int __pure btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001563{
1564 if (k1->objectid > k2->objectid)
1565 return 1;
1566 if (k1->objectid < k2->objectid)
1567 return -1;
1568 if (k1->type > k2->type)
1569 return 1;
1570 if (k1->type < k2->type)
1571 return -1;
1572 if (k1->offset > k2->offset)
1573 return 1;
1574 if (k1->offset < k2->offset)
1575 return -1;
1576 return 0;
1577}
Chris Mason081e9572007-11-06 10:26:24 -05001578
Chris Masond352ac62008-09-29 15:18:18 -04001579/*
1580 * this is used by the defrag code to go through all the
1581 * leaves pointed to by a node and reallocate them so that
1582 * disk order is close to key order
1583 */
Chris Mason6702ed42007-08-07 16:15:09 -04001584int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001585 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001586 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001587 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001588{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001589 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001590 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001591 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001592 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001593 u64 search_start = *last_ret;
1594 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001595 u64 other;
1596 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001597 int end_slot;
1598 int i;
1599 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001600 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001601 int uptodate;
1602 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001603 int progress_passed = 0;
1604 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001605
Chris Mason5708b952007-10-25 15:43:18 -04001606 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001607
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 WARN_ON(trans->transaction != fs_info->running_transaction);
1609 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001610
Chris Mason6b800532007-10-15 16:17:34 -04001611 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001612 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001613 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001614
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001615 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001616 return 0;
1617
David Sterba8bead252018-04-04 02:03:48 +02001618 btrfs_set_lock_blocking_write(parent);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001619
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001620 for (i = start_slot; i <= end_slot; i++) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001621 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001622 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001623
Chris Mason081e9572007-11-06 10:26:24 -05001624 btrfs_node_key(parent, &disk_key, i);
1625 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1626 continue;
1627
1628 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001629 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001630 gen = btrfs_node_ptr_generation(parent, i);
Qu Wenruo581c1762018-03-29 09:08:11 +08001631 btrfs_node_key_to_cpu(parent, &first_key, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001632 if (last_block == 0)
1633 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001634
Chris Mason6702ed42007-08-07 16:15:09 -04001635 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001636 other = btrfs_node_blockptr(parent, i - 1);
1637 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001638 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001639 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001640 other = btrfs_node_blockptr(parent, i + 1);
1641 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001642 }
Chris Masone9d0b132007-08-10 14:06:19 -04001643 if (close) {
1644 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001645 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001646 }
Chris Mason6702ed42007-08-07 16:15:09 -04001647
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001648 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001649 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001650 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001651 else
1652 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001653 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001654 if (!cur) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001655 cur = read_tree_block(fs_info, blocknr, gen,
1656 parent_level - 1,
1657 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08001658 if (IS_ERR(cur)) {
1659 return PTR_ERR(cur);
1660 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001661 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001662 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001663 }
Chris Mason6b800532007-10-15 16:17:34 -04001664 } else if (!uptodate) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001665 err = btrfs_read_buffer(cur, gen,
1666 parent_level - 1,&first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001667 if (err) {
1668 free_extent_buffer(cur);
1669 return err;
1670 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001671 }
Chris Mason6702ed42007-08-07 16:15:09 -04001672 }
Chris Masone9d0b132007-08-10 14:06:19 -04001673 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001674 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001675
Chris Masone7a84562008-06-25 16:01:31 -04001676 btrfs_tree_lock(cur);
David Sterba8bead252018-04-04 02:03:48 +02001677 btrfs_set_lock_blocking_write(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001678 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001679 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001680 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001681 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001682 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001683 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001684 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001685 break;
Yan252c38f2007-08-29 09:11:44 -04001686 }
Chris Masone7a84562008-06-25 16:01:31 -04001687 search_start = cur->start;
1688 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001689 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001690 btrfs_tree_unlock(cur);
1691 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001692 }
1693 return err;
1694}
1695
Chris Mason74123bd2007-02-02 11:05:29 -05001696/*
Chris Mason5f39d392007-10-15 16:14:19 -04001697 * search for key in the extent_buffer. The items start at offset p,
1698 * and they are item_size apart. There are 'max' items in p.
1699 *
Chris Mason74123bd2007-02-02 11:05:29 -05001700 * the slot in the array is returned via slot, and it points to
1701 * the place where you would insert key if it is not found in
1702 * the array.
1703 *
1704 * slot may point to max if the key is bigger than all of the keys
1705 */
Chris Masone02119d2008-09-05 16:13:11 -04001706static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001707 unsigned long p, int item_size,
1708 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001709 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001710{
1711 int low = 0;
1712 int high = max;
1713 int mid;
1714 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001715 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001716 struct btrfs_disk_key unaligned;
1717 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001718 char *kaddr = NULL;
1719 unsigned long map_start = 0;
1720 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001721 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001722
Liu Bo5e24e9a2016-06-23 16:32:45 -07001723 if (low > high) {
1724 btrfs_err(eb->fs_info,
1725 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1726 __func__, low, high, eb->start,
1727 btrfs_header_owner(eb), btrfs_header_level(eb));
1728 return -EINVAL;
1729 }
1730
Chris Masond3977122009-01-05 21:25:51 -05001731 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001732 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001733 offset = p + mid * item_size;
1734
Chris Masona6591712011-07-19 12:04:14 -04001735 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001736 (offset + sizeof(struct btrfs_disk_key)) >
1737 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001738
1739 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001740 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001741 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001742
Chris Mason479965d2007-10-15 16:14:27 -04001743 if (!err) {
1744 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1745 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001746 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001747 read_extent_buffer(eb, &unaligned,
1748 offset, sizeof(unaligned));
1749 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001750 } else {
1751 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001752 }
1753
Chris Mason5f39d392007-10-15 16:14:19 -04001754 } else {
1755 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1756 map_start);
1757 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001758 ret = comp_keys(tmp, key);
1759
1760 if (ret < 0)
1761 low = mid + 1;
1762 else if (ret > 0)
1763 high = mid;
1764 else {
1765 *slot = mid;
1766 return 0;
1767 }
1768 }
1769 *slot = low;
1770 return 1;
1771}
1772
Chris Mason97571fd2007-02-24 13:39:08 -05001773/*
1774 * simple bin_search frontend that does the right thing for
1775 * leaves vs nodes
1776 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001777int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1778 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001779{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001780 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001781 return generic_bin_search(eb,
1782 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001783 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001784 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001785 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001786 else
Chris Mason5f39d392007-10-15 16:14:19 -04001787 return generic_bin_search(eb,
1788 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001789 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001790 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001791 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001792}
1793
Yan, Zhengf0486c62010-05-16 10:46:25 -04001794static void root_add_used(struct btrfs_root *root, u32 size)
1795{
1796 spin_lock(&root->accounting_lock);
1797 btrfs_set_root_used(&root->root_item,
1798 btrfs_root_used(&root->root_item) + size);
1799 spin_unlock(&root->accounting_lock);
1800}
1801
1802static void root_sub_used(struct btrfs_root *root, u32 size)
1803{
1804 spin_lock(&root->accounting_lock);
1805 btrfs_set_root_used(&root->root_item,
1806 btrfs_root_used(&root->root_item) - size);
1807 spin_unlock(&root->accounting_lock);
1808}
1809
Chris Masond352ac62008-09-29 15:18:18 -04001810/* given a node and slot number, this reads the blocks it points to. The
1811 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001812 */
David Sterba4b231ae2019-08-21 19:16:27 +02001813struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
1814 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001815{
Chris Masonca7a79a2008-05-12 12:59:19 -04001816 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001817 struct extent_buffer *eb;
Qu Wenruo581c1762018-03-29 09:08:11 +08001818 struct btrfs_key first_key;
Josef Bacik416bc652013-04-23 14:17:42 -04001819
Liu Bofb770ae2016-07-05 12:10:14 -07001820 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1821 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001822
1823 BUG_ON(level == 0);
1824
Qu Wenruo581c1762018-03-29 09:08:11 +08001825 btrfs_node_key_to_cpu(parent, &first_key, slot);
David Sterbad0d20b02019-03-20 14:54:01 +01001826 eb = read_tree_block(parent->fs_info, btrfs_node_blockptr(parent, slot),
Qu Wenruo581c1762018-03-29 09:08:11 +08001827 btrfs_node_ptr_generation(parent, slot),
1828 level - 1, &first_key);
Liu Bofb770ae2016-07-05 12:10:14 -07001829 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1830 free_extent_buffer(eb);
1831 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001832 }
1833
1834 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001835}
1836
Chris Masond352ac62008-09-29 15:18:18 -04001837/*
1838 * node level balancing, used to make sure nodes are in proper order for
1839 * item deletion. We balance from the top down, so we have to make sure
1840 * that a deletion won't leave an node completely empty later on.
1841 */
Chris Masone02119d2008-09-05 16:13:11 -04001842static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001843 struct btrfs_root *root,
1844 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001845{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001846 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001847 struct extent_buffer *right = NULL;
1848 struct extent_buffer *mid;
1849 struct extent_buffer *left = NULL;
1850 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001851 int ret = 0;
1852 int wret;
1853 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001854 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001855 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001856
Liu Bo98e6b1e2018-09-12 06:06:23 +08001857 ASSERT(level > 0);
Chris Masonbb803952007-03-01 12:04:21 -05001858
Chris Mason5f39d392007-10-15 16:14:19 -04001859 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001860
Chris Masonbd681512011-07-16 15:23:14 -04001861 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1862 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001863 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1864
Chris Mason1d4f8a02007-03-13 09:28:32 -04001865 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001866
Li Zefana05a9bb2011-09-06 16:55:34 +08001867 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001868 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001869 pslot = path->slots[level + 1];
1870 }
Chris Masonbb803952007-03-01 12:04:21 -05001871
Chris Mason40689472007-03-17 14:29:23 -04001872 /*
1873 * deal with the case where there is only one pointer in the root
1874 * by promoting the node below to a root
1875 */
Chris Mason5f39d392007-10-15 16:14:19 -04001876 if (!parent) {
1877 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001878
Chris Mason5f39d392007-10-15 16:14:19 -04001879 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001880 return 0;
1881
1882 /* promote the child to a root */
David Sterba4b231ae2019-08-21 19:16:27 +02001883 child = btrfs_read_node_slot(mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001884 if (IS_ERR(child)) {
1885 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001886 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001887 goto enospc;
1888 }
1889
Chris Mason925baed2008-06-25 16:01:30 -04001890 btrfs_tree_lock(child);
David Sterba8bead252018-04-04 02:03:48 +02001891 btrfs_set_lock_blocking_write(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001892 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001893 if (ret) {
1894 btrfs_tree_unlock(child);
1895 free_extent_buffer(child);
1896 goto enospc;
1897 }
Yan2f375ab2008-02-01 14:58:07 -05001898
David Sterbad9d19a02018-03-05 16:35:29 +01001899 ret = tree_mod_log_insert_root(root->node, child, 1);
1900 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001901 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001902
Chris Mason0b86a832008-03-24 15:01:56 -04001903 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001904 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001905
Chris Mason925baed2008-06-25 16:01:30 -04001906 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001907 path->nodes[level] = NULL;
David Sterba6a884d7d2019-03-20 14:30:02 +01001908 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001909 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001910 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001911 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001912
1913 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001914 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001915 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001916 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001917 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001918 }
Chris Mason5f39d392007-10-15 16:14:19 -04001919 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001920 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001921 return 0;
1922
David Sterba4b231ae2019-08-21 19:16:27 +02001923 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001924 if (IS_ERR(left))
1925 left = NULL;
1926
Chris Mason5f39d392007-10-15 16:14:19 -04001927 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001928 btrfs_tree_lock(left);
David Sterba8bead252018-04-04 02:03:48 +02001929 btrfs_set_lock_blocking_write(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001930 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001931 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001932 if (wret) {
1933 ret = wret;
1934 goto enospc;
1935 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001936 }
Liu Bofb770ae2016-07-05 12:10:14 -07001937
David Sterba4b231ae2019-08-21 19:16:27 +02001938 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001939 if (IS_ERR(right))
1940 right = NULL;
1941
Chris Mason5f39d392007-10-15 16:14:19 -04001942 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001943 btrfs_tree_lock(right);
David Sterba8bead252018-04-04 02:03:48 +02001944 btrfs_set_lock_blocking_write(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001945 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001946 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001947 if (wret) {
1948 ret = wret;
1949 goto enospc;
1950 }
1951 }
1952
1953 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001954 if (left) {
1955 orig_slot += btrfs_header_nritems(left);
David Sterbad30a6682019-03-20 14:16:45 +01001956 wret = push_node_left(trans, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001957 if (wret < 0)
1958 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001959 }
Chris Mason79f95c82007-03-01 15:16:26 -05001960
1961 /*
1962 * then try to empty the right most buffer into the middle
1963 */
Chris Mason5f39d392007-10-15 16:14:19 -04001964 if (right) {
David Sterbad30a6682019-03-20 14:16:45 +01001965 wret = push_node_left(trans, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001966 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001967 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001968 if (btrfs_header_nritems(right) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01001969 btrfs_clean_tree_block(right);
Chris Mason925baed2008-06-25 16:01:30 -04001970 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001971 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001972 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001973 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001974 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001975 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001976 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001977 struct btrfs_disk_key right_key;
1978 btrfs_node_key(right, &right_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001979 ret = tree_mod_log_insert_key(parent, pslot + 1,
1980 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1981 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001982 btrfs_set_node_key(parent, &right_key, pslot + 1);
1983 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001984 }
1985 }
Chris Mason5f39d392007-10-15 16:14:19 -04001986 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001987 /*
1988 * we're not allowed to leave a node with one item in the
1989 * tree during a delete. A deletion from lower in the tree
1990 * could try to delete the only pointer in this node.
1991 * So, pull some keys from the left.
1992 * There has to be a left pointer at this point because
1993 * otherwise we would have pulled some pointers from the
1994 * right
1995 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001996 if (!left) {
1997 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001999 goto enospc;
2000 }
David Sterba55d32ed2019-03-20 14:18:06 +01002001 wret = balance_node_right(trans, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002002 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002003 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002004 goto enospc;
2005 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002006 if (wret == 1) {
David Sterbad30a6682019-03-20 14:16:45 +01002007 wret = push_node_left(trans, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04002008 if (wret < 0)
2009 ret = wret;
2010 }
Chris Mason79f95c82007-03-01 15:16:26 -05002011 BUG_ON(wret == 1);
2012 }
Chris Mason5f39d392007-10-15 16:14:19 -04002013 if (btrfs_header_nritems(mid) == 0) {
David Sterba6a884d7d2019-03-20 14:30:02 +01002014 btrfs_clean_tree_block(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002015 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002016 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002017 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002018 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002019 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002020 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002021 } else {
2022 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002023 struct btrfs_disk_key mid_key;
2024 btrfs_node_key(mid, &mid_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002025 ret = tree_mod_log_insert_key(parent, pslot,
2026 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2027 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002028 btrfs_set_node_key(parent, &mid_key, pslot);
2029 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002030 }
Chris Masonbb803952007-03-01 12:04:21 -05002031
Chris Mason79f95c82007-03-01 15:16:26 -05002032 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002033 if (left) {
2034 if (btrfs_header_nritems(left) > orig_slot) {
David Sterba67439da2019-10-08 13:28:47 +02002035 atomic_inc(&left->refs);
Chris Mason925baed2008-06-25 16:01:30 -04002036 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002037 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002038 path->slots[level + 1] -= 1;
2039 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002040 if (mid) {
2041 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002042 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002043 }
Chris Masonbb803952007-03-01 12:04:21 -05002044 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002045 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002046 path->slots[level] = orig_slot;
2047 }
2048 }
Chris Mason79f95c82007-03-01 15:16:26 -05002049 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002050 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002052 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002053enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002054 if (right) {
2055 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002056 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002057 }
2058 if (left) {
2059 if (path->nodes[level] != left)
2060 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002062 }
Chris Masonbb803952007-03-01 12:04:21 -05002063 return ret;
2064}
2065
Chris Masond352ac62008-09-29 15:18:18 -04002066/* Node balancing for insertion. Here we only split or push nodes around
2067 * when they are completely full. This is also done top down, so we
2068 * have to be pessimistic.
2069 */
Chris Masond3977122009-01-05 21:25:51 -05002070static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002071 struct btrfs_root *root,
2072 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002074 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002075 struct extent_buffer *right = NULL;
2076 struct extent_buffer *mid;
2077 struct extent_buffer *left = NULL;
2078 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002079 int ret = 0;
2080 int wret;
2081 int pslot;
2082 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002083
2084 if (level == 0)
2085 return 1;
2086
Chris Mason5f39d392007-10-15 16:14:19 -04002087 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002088 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002089
Li Zefana05a9bb2011-09-06 16:55:34 +08002090 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002091 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002092 pslot = path->slots[level + 1];
2093 }
Chris Masone66f7092007-04-20 13:16:02 -04002094
Chris Mason5f39d392007-10-15 16:14:19 -04002095 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002096 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002097
David Sterba4b231ae2019-08-21 19:16:27 +02002098 left = btrfs_read_node_slot(parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002099 if (IS_ERR(left))
2100 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002101
2102 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002103 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002104 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002105
2106 btrfs_tree_lock(left);
David Sterba8bead252018-04-04 02:03:48 +02002107 btrfs_set_lock_blocking_write(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002110 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002111 wret = 1;
2112 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002113 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002114 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002115 if (ret)
2116 wret = 1;
2117 else {
David Sterbad30a6682019-03-20 14:16:45 +01002118 wret = push_node_left(trans, left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002119 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002120 }
Chris Masone66f7092007-04-20 13:16:02 -04002121 if (wret < 0)
2122 ret = wret;
2123 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002124 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002125 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002126 btrfs_node_key(mid, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002127 ret = tree_mod_log_insert_key(parent, pslot,
2128 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2129 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_set_node_key(parent, &disk_key, pslot);
2131 btrfs_mark_buffer_dirty(parent);
2132 if (btrfs_header_nritems(left) > orig_slot) {
2133 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002134 path->slots[level + 1] -= 1;
2135 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002136 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002138 } else {
2139 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002141 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002142 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002144 }
Chris Masone66f7092007-04-20 13:16:02 -04002145 return 0;
2146 }
Chris Mason925baed2008-06-25 16:01:30 -04002147 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002148 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002149 }
David Sterba4b231ae2019-08-21 19:16:27 +02002150 right = btrfs_read_node_slot(parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002151 if (IS_ERR(right))
2152 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002153
2154 /*
2155 * then try to empty the right most buffer into the middle
2156 */
Chris Mason5f39d392007-10-15 16:14:19 -04002157 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002158 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002159
Chris Mason925baed2008-06-25 16:01:30 -04002160 btrfs_tree_lock(right);
David Sterba8bead252018-04-04 02:03:48 +02002161 btrfs_set_lock_blocking_write(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002162
Chris Mason5f39d392007-10-15 16:14:19 -04002163 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002164 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002165 wret = 1;
2166 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002167 ret = btrfs_cow_block(trans, root, right,
2168 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002169 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002170 if (ret)
2171 wret = 1;
2172 else {
David Sterba55d32ed2019-03-20 14:18:06 +01002173 wret = balance_node_right(trans, right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002174 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002175 }
Chris Masone66f7092007-04-20 13:16:02 -04002176 if (wret < 0)
2177 ret = wret;
2178 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002179 struct btrfs_disk_key disk_key;
2180
2181 btrfs_node_key(right, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002182 ret = tree_mod_log_insert_key(parent, pslot + 1,
2183 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2184 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2186 btrfs_mark_buffer_dirty(parent);
2187
2188 if (btrfs_header_nritems(mid) <= orig_slot) {
2189 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002190 path->slots[level + 1] += 1;
2191 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002192 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002193 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002194 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002195 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002196 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002197 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002198 }
Chris Masone66f7092007-04-20 13:16:02 -04002199 return 0;
2200 }
Chris Mason925baed2008-06-25 16:01:30 -04002201 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002202 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002203 }
Chris Masone66f7092007-04-20 13:16:02 -04002204 return 1;
2205}
2206
Chris Mason74123bd2007-02-02 11:05:29 -05002207/*
Chris Masond352ac62008-09-29 15:18:18 -04002208 * readahead one full node of leaves, finding things that are close
2209 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002210 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002211static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002212 struct btrfs_path *path,
2213 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002214{
Chris Mason5f39d392007-10-15 16:14:19 -04002215 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002216 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002217 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002218 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002219 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002220 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002221 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002222 u32 nr;
2223 u32 blocksize;
2224 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002225
Chris Masona6b6e752007-10-15 16:22:39 -04002226 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002227 return;
2228
Chris Mason6702ed42007-08-07 16:15:09 -04002229 if (!path->nodes[level])
2230 return;
2231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002233
Chris Mason3c69fae2007-08-07 15:52:22 -04002234 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002235 blocksize = fs_info->nodesize;
2236 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 if (eb) {
2238 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002239 return;
2240 }
2241
Chris Masona7175312009-01-22 09:23:10 -05002242 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002243
Chris Mason5f39d392007-10-15 16:14:19 -04002244 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002245 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002246
Chris Masond3977122009-01-05 21:25:51 -05002247 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002248 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002249 if (nr == 0)
2250 break;
2251 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002252 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002253 nr++;
2254 if (nr >= nritems)
2255 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002256 }
David Sterbae4058b52015-11-27 16:31:35 +01002257 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002258 btrfs_node_key(node, &disk_key, nr);
2259 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2260 break;
2261 }
Chris Mason6b800532007-10-15 16:17:34 -04002262 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002263 if ((search <= target && target - search <= 65536) ||
2264 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002265 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002266 nread += blocksize;
2267 }
2268 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002269 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002270 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002271 }
2272}
Chris Mason925baed2008-06-25 16:01:30 -04002273
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002274static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002275 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002276{
2277 int slot;
2278 int nritems;
2279 struct extent_buffer *parent;
2280 struct extent_buffer *eb;
2281 u64 gen;
2282 u64 block1 = 0;
2283 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002284
Chris Mason8c594ea2009-04-20 15:50:10 -04002285 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002286 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002287 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002288
2289 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002290 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002291
2292 if (slot > 0) {
2293 block1 = btrfs_node_blockptr(parent, slot - 1);
2294 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002295 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002296 /*
2297 * if we get -eagain from btrfs_buffer_uptodate, we
2298 * don't want to return eagain here. That will loop
2299 * forever
2300 */
2301 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002302 block1 = 0;
2303 free_extent_buffer(eb);
2304 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002305 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002306 block2 = btrfs_node_blockptr(parent, slot + 1);
2307 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002308 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002309 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002310 block2 = 0;
2311 free_extent_buffer(eb);
2312 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002313
Josef Bacik0b088512013-06-17 14:23:02 -04002314 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002315 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002316 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002317 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002318}
2319
2320
2321/*
Chris Masond3977122009-01-05 21:25:51 -05002322 * when we walk down the tree, it is usually safe to unlock the higher layers
2323 * in the tree. The exceptions are when our path goes through slot 0, because
2324 * operations on the tree might require changing key pointers higher up in the
2325 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002326 *
Chris Masond3977122009-01-05 21:25:51 -05002327 * callers might also have set path->keep_locks, which tells this code to keep
2328 * the lock if the path points to the last slot in the block. This is part of
2329 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002330 *
Chris Masond3977122009-01-05 21:25:51 -05002331 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2332 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002333 */
Chris Masone02119d2008-09-05 16:13:11 -04002334static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002335 int lowest_unlock, int min_write_lock_level,
2336 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002337{
2338 int i;
2339 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002340 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002341 struct extent_buffer *t;
2342
2343 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2344 if (!path->nodes[i])
2345 break;
2346 if (!path->locks[i])
2347 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002348 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002349 skip_level = i + 1;
2350 continue;
2351 }
Chris Mason051e1b92008-06-25 16:01:30 -04002352 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002353 u32 nritems;
2354 t = path->nodes[i];
2355 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002356 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002357 skip_level = i + 1;
2358 continue;
2359 }
2360 }
Chris Mason051e1b92008-06-25 16:01:30 -04002361 if (skip_level < i && i >= lowest_unlock)
2362 no_skips = 1;
2363
Chris Mason925baed2008-06-25 16:01:30 -04002364 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08002365 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04002366 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002367 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002368 if (write_lock_level &&
2369 i > min_write_lock_level &&
2370 i <= *write_lock_level) {
2371 *write_lock_level = i - 1;
2372 }
Chris Mason925baed2008-06-25 16:01:30 -04002373 }
2374 }
2375}
2376
Chris Mason3c69fae2007-08-07 15:52:22 -04002377/*
Chris Masonc8c42862009-04-03 10:14:18 -04002378 * helper function for btrfs_search_slot. The goal is to find a block
2379 * in cache without setting the path to blocking. If we find the block
2380 * we return zero and the path is unchanged.
2381 *
2382 * If we can't find the block, we set the path blocking and do some
2383 * reada. -EAGAIN is returned and the search must be repeated.
2384 */
2385static int
Liu Bod07b8522017-01-30 12:23:42 -08002386read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2387 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002388 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002390 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002391 u64 blocknr;
2392 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002393 struct extent_buffer *b = *eb_ret;
2394 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08002395 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04002396 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08002397 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04002398
2399 blocknr = btrfs_node_blockptr(b, slot);
2400 gen = btrfs_node_ptr_generation(b, slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08002401 parent_level = btrfs_header_level(b);
2402 btrfs_node_key_to_cpu(b, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002403
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002404 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002405 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002406 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002407 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Qu Wenruo448de472019-03-12 17:10:40 +08002408 /*
2409 * Do extra check for first_key, eb can be stale due to
2410 * being cached, read from scrub, or have multiple
2411 * parents (shared tree blocks).
2412 */
David Sterbae064d5e2019-03-20 14:58:13 +01002413 if (btrfs_verify_level_key(tmp,
Qu Wenruo448de472019-03-12 17:10:40 +08002414 parent_level - 1, &first_key, gen)) {
2415 free_extent_buffer(tmp);
2416 return -EUCLEAN;
2417 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002418 *eb_ret = tmp;
2419 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002420 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002421
2422 /* the pages were up to date, but we failed
2423 * the generation number check. Do a full
2424 * read for the generation number that is correct.
2425 * We must do this without dropping locks so
2426 * we can trust our generation number
2427 */
2428 btrfs_set_path_blocking(p);
2429
2430 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08002431 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04002432 if (!ret) {
2433 *eb_ret = tmp;
2434 return 0;
2435 }
2436 free_extent_buffer(tmp);
2437 btrfs_release_path(p);
2438 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002439 }
2440
2441 /*
2442 * reduce lock contention at high levels
2443 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002444 * we read. Don't release the lock on the current
2445 * level because we need to walk this node to figure
2446 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002447 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002448 btrfs_unlock_up_safe(p, level + 1);
2449 btrfs_set_path_blocking(p);
2450
David Sterbae4058b52015-11-27 16:31:35 +01002451 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002452 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002453
Chris Mason76a05b32009-05-14 13:24:30 -04002454 ret = -EAGAIN;
Liu Bo02a33072018-05-16 01:37:36 +08002455 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
Qu Wenruo581c1762018-03-29 09:08:11 +08002456 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002457 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002458 /*
2459 * If the read above didn't mark this buffer up to date,
2460 * it will never end up being up to date. Set ret to EIO now
2461 * and give up so that our caller doesn't loop forever
2462 * on our EAGAINs.
2463 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08002464 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04002465 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002466 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002467 } else {
2468 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002469 }
Liu Bo02a33072018-05-16 01:37:36 +08002470
2471 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002472 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002473}
2474
2475/*
2476 * helper function for btrfs_search_slot. This does all of the checks
2477 * for node-level blocks and does any balancing required based on
2478 * the ins_len.
2479 *
2480 * If no extra work was required, zero is returned. If we had to
2481 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2482 * start over
2483 */
2484static int
2485setup_nodes_for_search(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002487 struct extent_buffer *b, int level, int ins_len,
2488 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002489{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002490 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002491 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002492
Chris Masonc8c42862009-04-03 10:14:18 -04002493 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002495 int sret;
2496
Chris Masonbd681512011-07-16 15:23:14 -04002497 if (*write_lock_level < level + 1) {
2498 *write_lock_level = level + 1;
2499 btrfs_release_path(p);
2500 goto again;
2501 }
2502
Chris Masonc8c42862009-04-03 10:14:18 -04002503 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002504 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002505 sret = split_node(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002506
2507 BUG_ON(sret > 0);
2508 if (sret) {
2509 ret = sret;
2510 goto done;
2511 }
2512 b = p->nodes[level];
2513 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002514 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002515 int sret;
2516
Chris Masonbd681512011-07-16 15:23:14 -04002517 if (*write_lock_level < level + 1) {
2518 *write_lock_level = level + 1;
2519 btrfs_release_path(p);
2520 goto again;
2521 }
2522
Chris Masonc8c42862009-04-03 10:14:18 -04002523 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002524 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002525 sret = balance_level(trans, root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002526
2527 if (sret) {
2528 ret = sret;
2529 goto done;
2530 }
2531 b = p->nodes[level];
2532 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002533 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002534 goto again;
2535 }
2536 BUG_ON(btrfs_header_nritems(b) == 1);
2537 }
2538 return 0;
2539
2540again:
2541 ret = -EAGAIN;
2542done:
2543 return ret;
2544}
2545
Omar Sandoval310712b2017-01-17 23:24:37 -08002546static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002547 int level, int *prev_cmp, int *slot)
2548{
2549 if (*prev_cmp != 0) {
Nikolay Borisova74b35e2017-12-08 16:27:43 +02002550 *prev_cmp = btrfs_bin_search(b, key, level, slot);
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002551 return *prev_cmp;
2552 }
2553
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002554 *slot = 0;
2555
2556 return 0;
2557}
2558
David Sterba381cf652015-01-02 18:45:16 +01002559int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002560 u64 iobjectid, u64 ioff, u8 key_type,
2561 struct btrfs_key *found_key)
2562{
2563 int ret;
2564 struct btrfs_key key;
2565 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002566
2567 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002568 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002569
2570 key.type = key_type;
2571 key.objectid = iobjectid;
2572 key.offset = ioff;
2573
2574 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002575 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002576 return ret;
2577
2578 eb = path->nodes[0];
2579 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2580 ret = btrfs_next_leaf(fs_root, path);
2581 if (ret)
2582 return ret;
2583 eb = path->nodes[0];
2584 }
2585
2586 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2587 if (found_key->type != key.type ||
2588 found_key->objectid != key.objectid)
2589 return 1;
2590
2591 return 0;
2592}
2593
Liu Bo1fc28d82018-05-18 11:00:21 +08002594static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2595 struct btrfs_path *p,
2596 int write_lock_level)
2597{
2598 struct btrfs_fs_info *fs_info = root->fs_info;
2599 struct extent_buffer *b;
2600 int root_lock;
2601 int level = 0;
2602
2603 /* We try very hard to do read locks on the root */
2604 root_lock = BTRFS_READ_LOCK;
2605
2606 if (p->search_commit_root) {
Filipe Mananabe6821f2018-12-11 10:19:45 +00002607 /*
2608 * The commit roots are read only so we always do read locks,
2609 * and we always must hold the commit_root_sem when doing
2610 * searches on them, the only exception is send where we don't
2611 * want to block transaction commits for a long time, so
2612 * we need to clone the commit root in order to avoid races
2613 * with transaction commits that create a snapshot of one of
2614 * the roots used by a send operation.
2615 */
2616 if (p->need_commit_sem) {
Liu Bo1fc28d82018-05-18 11:00:21 +08002617 down_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002618 b = btrfs_clone_extent_buffer(root->commit_root);
Liu Bo1fc28d82018-05-18 11:00:21 +08002619 up_read(&fs_info->commit_root_sem);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002620 if (!b)
2621 return ERR_PTR(-ENOMEM);
2622
2623 } else {
2624 b = root->commit_root;
David Sterba67439da2019-10-08 13:28:47 +02002625 atomic_inc(&b->refs);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002626 }
2627 level = btrfs_header_level(b);
Liu Bof9ddfd02018-05-29 21:27:06 +08002628 /*
2629 * Ensure that all callers have set skip_locking when
2630 * p->search_commit_root = 1.
2631 */
2632 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08002633
2634 goto out;
2635 }
2636
2637 if (p->skip_locking) {
2638 b = btrfs_root_node(root);
2639 level = btrfs_header_level(b);
2640 goto out;
2641 }
2642
2643 /*
Liu Bo662c6532018-05-18 11:00:23 +08002644 * If the level is set to maximum, we can skip trying to get the read
2645 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08002646 */
Liu Bo662c6532018-05-18 11:00:23 +08002647 if (write_lock_level < BTRFS_MAX_LEVEL) {
2648 /*
2649 * We don't know the level of the root node until we actually
2650 * have it read locked
2651 */
2652 b = btrfs_read_lock_root_node(root);
2653 level = btrfs_header_level(b);
2654 if (level > write_lock_level)
2655 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08002656
Liu Bo662c6532018-05-18 11:00:23 +08002657 /* Whoops, must trade for write lock */
2658 btrfs_tree_read_unlock(b);
2659 free_extent_buffer(b);
2660 }
2661
Liu Bo1fc28d82018-05-18 11:00:21 +08002662 b = btrfs_lock_root_node(root);
2663 root_lock = BTRFS_WRITE_LOCK;
2664
2665 /* The level might have changed, check again */
2666 level = btrfs_header_level(b);
2667
2668out:
2669 p->nodes[level] = b;
2670 if (!p->skip_locking)
2671 p->locks[level] = root_lock;
2672 /*
2673 * Callers are responsible for dropping b's references.
2674 */
2675 return b;
2676}
2677
2678
Chris Masonc8c42862009-04-03 10:14:18 -04002679/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002680 * btrfs_search_slot - look for a key in a tree and perform necessary
2681 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002682 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002683 * @trans: Handle of transaction, used when modifying the tree
2684 * @p: Holds all btree nodes along the search path
2685 * @root: The root node of the tree
2686 * @key: The key we are looking for
2687 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2688 * deletions it's -1. 0 for plain searches
2689 * @cow: boolean should CoW operations be performed. Must always be 1
2690 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002691 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002692 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2693 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2694 *
2695 * If @key is found, 0 is returned and you can find the item in the leaf level
2696 * of the path (level 0)
2697 *
2698 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2699 * points to the slot where it should be inserted
2700 *
2701 * If an error is encountered while searching the tree a negative error number
2702 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002703 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002704int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2705 const struct btrfs_key *key, struct btrfs_path *p,
2706 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002707{
Chris Mason5f39d392007-10-15 16:14:19 -04002708 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002709 int slot;
2710 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002711 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002712 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002713 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002714 /* everything at write_lock_level or lower must be write locked */
2715 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002716 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002717 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002718 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002719
Chris Mason6702ed42007-08-07 16:15:09 -04002720 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002721 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002722 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002723 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002724
Chris Masonbd681512011-07-16 15:23:14 -04002725 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002726 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002727
Chris Masonbd681512011-07-16 15:23:14 -04002728 /* when we are removing items, we might have to go up to level
2729 * two as we update tree pointers Make sure we keep write
2730 * for those levels as well
2731 */
2732 write_lock_level = 2;
2733 } else if (ins_len > 0) {
2734 /*
2735 * for inserting items, make sure we have a write lock on
2736 * level 1 so we can update keys
2737 */
2738 write_lock_level = 1;
2739 }
2740
2741 if (!cow)
2742 write_lock_level = -1;
2743
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002744 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002745 write_lock_level = BTRFS_MAX_LEVEL;
2746
Chris Masonf7c79f32012-03-19 15:54:38 -04002747 min_write_lock_level = write_lock_level;
2748
Chris Masonbb803952007-03-01 12:04:21 -05002749again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002750 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08002751 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Filipe Mananabe6821f2018-12-11 10:19:45 +00002752 if (IS_ERR(b)) {
2753 ret = PTR_ERR(b);
2754 goto done;
2755 }
Chris Mason925baed2008-06-25 16:01:30 -04002756
Chris Masoneb60cea2007-02-02 09:18:22 -05002757 while (b) {
Qu Wenruof624d972019-09-10 15:40:17 +08002758 int dec = 0;
2759
Chris Mason5f39d392007-10-15 16:14:19 -04002760 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002761
Chris Mason02217ed2007-03-02 16:08:05 -05002762 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002763 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2764
Chris Masonc8c42862009-04-03 10:14:18 -04002765 /*
2766 * if we don't really need to cow this block
2767 * then we don't want to set the path blocking,
2768 * so we test it here
2769 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002770 if (!should_cow_block(trans, root, b)) {
2771 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002772 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002773 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002774
Chris Masonbd681512011-07-16 15:23:14 -04002775 /*
2776 * must have write locks on this node and the
2777 * parent
2778 */
Josef Bacik5124e002012-11-07 13:44:13 -05002779 if (level > write_lock_level ||
2780 (level + 1 > write_lock_level &&
2781 level + 1 < BTRFS_MAX_LEVEL &&
2782 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002783 write_lock_level = level + 1;
2784 btrfs_release_path(p);
2785 goto again;
2786 }
2787
Filipe Manana160f4082014-07-28 19:37:17 +01002788 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002789 if (last_level)
2790 err = btrfs_cow_block(trans, root, b, NULL, 0,
2791 &b);
2792 else
2793 err = btrfs_cow_block(trans, root, b,
2794 p->nodes[level + 1],
2795 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002796 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002797 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002798 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002799 }
Chris Mason02217ed2007-03-02 16:08:05 -05002800 }
Chris Mason65b51a02008-08-01 15:11:20 -04002801cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002802 p->nodes[level] = b;
Liu Bo52398342018-08-22 05:54:37 +08002803 /*
2804 * Leave path with blocking locks to avoid massive
2805 * lock context switch, this is made on purpose.
2806 */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002807
2808 /*
2809 * we have a lock on b and as long as we aren't changing
2810 * the tree, there is no way to for the items in b to change.
2811 * It is safe to drop the lock on our parent before we
2812 * go through the expensive btree search on b.
2813 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002814 * If we're inserting or deleting (ins_len != 0), then we might
2815 * be changing slot zero, which may require changing the parent.
2816 * So, we can't drop the lock until after we know which slot
2817 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002818 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002819 if (!ins_len && !p->keep_locks) {
2820 int u = level + 1;
2821
2822 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2823 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2824 p->locks[u] = 0;
2825 }
2826 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002827
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002828 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002829 if (ret < 0)
2830 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002831
Qu Wenruof624d972019-09-10 15:40:17 +08002832 if (level == 0) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002833 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002834 if (ins_len > 0 &&
David Sterbae902baa2019-03-20 14:36:46 +01002835 btrfs_leaf_free_space(b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002836 if (write_lock_level < 1) {
2837 write_lock_level = 1;
2838 btrfs_release_path(p);
2839 goto again;
2840 }
2841
Chris Masonb4ce94d2009-02-04 09:25:08 -05002842 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002843 err = split_leaf(trans, root, key,
2844 p, ins_len, ret == 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002845
Yan Zheng33c66f42009-07-22 09:59:00 -04002846 BUG_ON(err > 0);
2847 if (err) {
2848 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002849 goto done;
2850 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002851 }
Chris Mason459931e2008-12-10 09:10:46 -05002852 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002853 unlock_up(p, level, lowest_unlock,
Liu Bo4b6f8e92018-08-14 10:46:53 +08002854 min_write_lock_level, NULL);
Chris Mason65b51a02008-08-01 15:11:20 -04002855 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002856 }
Qu Wenruof624d972019-09-10 15:40:17 +08002857 if (ret && slot > 0) {
2858 dec = 1;
2859 slot--;
2860 }
2861 p->slots[level] = slot;
2862 err = setup_nodes_for_search(trans, root, p, b, level, ins_len,
2863 &write_lock_level);
2864 if (err == -EAGAIN)
2865 goto again;
2866 if (err) {
2867 ret = err;
2868 goto done;
2869 }
2870 b = p->nodes[level];
2871 slot = p->slots[level];
2872
2873 /*
2874 * Slot 0 is special, if we change the key we have to update
2875 * the parent pointer which means we must have a write lock on
2876 * the parent
2877 */
2878 if (slot == 0 && ins_len && write_lock_level < level + 1) {
2879 write_lock_level = level + 1;
2880 btrfs_release_path(p);
2881 goto again;
2882 }
2883
2884 unlock_up(p, level, lowest_unlock, min_write_lock_level,
2885 &write_lock_level);
2886
2887 if (level == lowest_level) {
2888 if (dec)
2889 p->slots[level]++;
2890 goto done;
2891 }
2892
2893 err = read_block_for_search(root, p, &b, level, slot, key);
2894 if (err == -EAGAIN)
2895 goto again;
2896 if (err) {
2897 ret = err;
2898 goto done;
2899 }
2900
2901 if (!p->skip_locking) {
2902 level = btrfs_header_level(b);
2903 if (level <= write_lock_level) {
2904 if (!btrfs_try_tree_write_lock(b)) {
2905 btrfs_set_path_blocking(p);
2906 btrfs_tree_lock(b);
2907 }
2908 p->locks[level] = BTRFS_WRITE_LOCK;
2909 } else {
2910 if (!btrfs_tree_read_lock_atomic(b)) {
2911 btrfs_set_path_blocking(p);
2912 btrfs_tree_read_lock(b);
2913 }
2914 p->locks[level] = BTRFS_READ_LOCK;
2915 }
2916 p->nodes[level] = b;
2917 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002918 }
Chris Mason65b51a02008-08-01 15:11:20 -04002919 ret = 1;
2920done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002921 /*
2922 * we don't really know what they plan on doing with the path
2923 * from here on, so for now just mark it as blocking
2924 */
Chris Masonb9473432009-03-13 11:00:37 -04002925 if (!p->leave_spinning)
2926 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002927 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002928 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002929 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002930}
2931
Chris Mason74123bd2007-02-02 11:05:29 -05002932/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002933 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2934 * current state of the tree together with the operations recorded in the tree
2935 * modification log to search for the key in a previous version of this tree, as
2936 * denoted by the time_seq parameter.
2937 *
2938 * Naturally, there is no support for insert, delete or cow operations.
2939 *
2940 * The resulting path and return value will be set up as if we called
2941 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2942 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002943int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002944 struct btrfs_path *p, u64 time_seq)
2945{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002946 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002947 struct extent_buffer *b;
2948 int slot;
2949 int ret;
2950 int err;
2951 int level;
2952 int lowest_unlock = 1;
2953 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002954 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002955
2956 lowest_level = p->lowest_level;
2957 WARN_ON(p->nodes[0] != NULL);
2958
2959 if (p->search_commit_root) {
2960 BUG_ON(time_seq);
2961 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2962 }
2963
2964again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002965 b = get_old_root(root, time_seq);
Nikolay Borisov315bed42018-09-13 11:35:10 +03002966 if (!b) {
2967 ret = -EIO;
2968 goto done;
2969 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002970 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002971 p->locks[level] = BTRFS_READ_LOCK;
2972
2973 while (b) {
Qu Wenruoabe93392019-09-10 15:40:18 +08002974 int dec = 0;
2975
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002976 level = btrfs_header_level(b);
2977 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002978
2979 /*
2980 * we have a lock on b and as long as we aren't changing
2981 * the tree, there is no way to for the items in b to change.
2982 * It is safe to drop the lock on our parent before we
2983 * go through the expensive btree search on b.
2984 */
2985 btrfs_unlock_up_safe(p, level + 1);
2986
Josef Bacikd4b40872013-09-24 14:09:34 -04002987 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002988 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002989 * time.
2990 */
2991 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002992 ret = key_search(b, key, level, &prev_cmp, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00002993 if (ret < 0)
2994 goto done;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002995
Qu Wenruoabe93392019-09-10 15:40:18 +08002996 if (level == 0) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002997 p->slots[level] = slot;
2998 unlock_up(p, level, lowest_unlock, 0, NULL);
2999 goto done;
3000 }
Qu Wenruoabe93392019-09-10 15:40:18 +08003001
3002 if (ret && slot > 0) {
3003 dec = 1;
3004 slot--;
3005 }
3006 p->slots[level] = slot;
3007 unlock_up(p, level, lowest_unlock, 0, NULL);
3008
3009 if (level == lowest_level) {
3010 if (dec)
3011 p->slots[level]++;
3012 goto done;
3013 }
3014
3015 err = read_block_for_search(root, p, &b, level, slot, key);
3016 if (err == -EAGAIN)
3017 goto again;
3018 if (err) {
3019 ret = err;
3020 goto done;
3021 }
3022
3023 level = btrfs_header_level(b);
3024 if (!btrfs_tree_read_lock_atomic(b)) {
3025 btrfs_set_path_blocking(p);
3026 btrfs_tree_read_lock(b);
3027 }
3028 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
3029 if (!b) {
3030 ret = -ENOMEM;
3031 goto done;
3032 }
3033 p->locks[level] = BTRFS_READ_LOCK;
3034 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003035 }
3036 ret = 1;
3037done:
3038 if (!p->leave_spinning)
3039 btrfs_set_path_blocking(p);
3040 if (ret < 0)
3041 btrfs_release_path(p);
3042
3043 return ret;
3044}
3045
3046/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003047 * helper to use instead of search slot if no exact match is needed but
3048 * instead the next or previous item should be returned.
3049 * When find_higher is true, the next higher item is returned, the next lower
3050 * otherwise.
3051 * When return_any and find_higher are both true, and no higher item is found,
3052 * return the next lower instead.
3053 * When return_any is true and find_higher is false, and no lower item is found,
3054 * return the next higher instead.
3055 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3056 * < 0 on error
3057 */
3058int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003059 const struct btrfs_key *key,
3060 struct btrfs_path *p, int find_higher,
3061 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003062{
3063 int ret;
3064 struct extent_buffer *leaf;
3065
3066again:
3067 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3068 if (ret <= 0)
3069 return ret;
3070 /*
3071 * a return value of 1 means the path is at the position where the
3072 * item should be inserted. Normally this is the next bigger item,
3073 * but in case the previous item is the last in a leaf, path points
3074 * to the first free slot in the previous leaf, i.e. at an invalid
3075 * item.
3076 */
3077 leaf = p->nodes[0];
3078
3079 if (find_higher) {
3080 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3081 ret = btrfs_next_leaf(root, p);
3082 if (ret <= 0)
3083 return ret;
3084 if (!return_any)
3085 return 1;
3086 /*
3087 * no higher item found, return the next
3088 * lower instead
3089 */
3090 return_any = 0;
3091 find_higher = 0;
3092 btrfs_release_path(p);
3093 goto again;
3094 }
3095 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003096 if (p->slots[0] == 0) {
3097 ret = btrfs_prev_leaf(root, p);
3098 if (ret < 0)
3099 return ret;
3100 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003101 leaf = p->nodes[0];
3102 if (p->slots[0] == btrfs_header_nritems(leaf))
3103 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003104 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003105 }
Arne Jansene6793762011-09-13 11:18:10 +02003106 if (!return_any)
3107 return 1;
3108 /*
3109 * no lower item found, return the next
3110 * higher instead
3111 */
3112 return_any = 0;
3113 find_higher = 1;
3114 btrfs_release_path(p);
3115 goto again;
3116 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003117 --p->slots[0];
3118 }
3119 }
3120 return 0;
3121}
3122
3123/*
Chris Mason74123bd2007-02-02 11:05:29 -05003124 * adjust the pointers going up the tree, starting at level
3125 * making sure the right key of each node is points to 'key'.
3126 * This is used after shifting pointers to the left, so it stops
3127 * fixing up pointers when a given leaf/node is not in slot 0 of the
3128 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003129 *
Chris Mason74123bd2007-02-02 11:05:29 -05003130 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003131static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003132 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003133{
3134 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003135 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01003136 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003137
Chris Mason234b63a2007-03-13 10:46:10 -04003138 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003139 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003140
Chris Masoneb60cea2007-02-02 09:18:22 -05003141 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003142 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003143 t = path->nodes[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003144 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3145 GFP_ATOMIC);
3146 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003147 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003148 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003149 if (tslot != 0)
3150 break;
3151 }
3152}
3153
Chris Mason74123bd2007-02-02 11:05:29 -05003154/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003155 * update item key.
3156 *
3157 * This function isn't completely safe. It's the caller's responsibility
3158 * that the new key won't break the order
3159 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003160void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3161 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003162 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003163{
3164 struct btrfs_disk_key disk_key;
3165 struct extent_buffer *eb;
3166 int slot;
3167
3168 eb = path->nodes[0];
3169 slot = path->slots[0];
3170 if (slot > 0) {
3171 btrfs_item_key(eb, &disk_key, slot - 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003172 if (unlikely(comp_keys(&disk_key, new_key) >= 0)) {
3173 btrfs_crit(fs_info,
3174 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3175 slot, btrfs_disk_key_objectid(&disk_key),
3176 btrfs_disk_key_type(&disk_key),
3177 btrfs_disk_key_offset(&disk_key),
3178 new_key->objectid, new_key->type,
3179 new_key->offset);
3180 btrfs_print_leaf(eb);
3181 BUG();
3182 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003183 }
3184 if (slot < btrfs_header_nritems(eb) - 1) {
3185 btrfs_item_key(eb, &disk_key, slot + 1);
Qu Wenruo7c15d412019-04-25 08:55:53 +08003186 if (unlikely(comp_keys(&disk_key, new_key) <= 0)) {
3187 btrfs_crit(fs_info,
3188 "slot %u key (%llu %u %llu) new key (%llu %u %llu)",
3189 slot, btrfs_disk_key_objectid(&disk_key),
3190 btrfs_disk_key_type(&disk_key),
3191 btrfs_disk_key_offset(&disk_key),
3192 new_key->objectid, new_key->type,
3193 new_key->offset);
3194 btrfs_print_leaf(eb);
3195 BUG();
3196 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003197 }
3198
3199 btrfs_cpu_key_to_disk(&disk_key, new_key);
3200 btrfs_set_item_key(eb, &disk_key, slot);
3201 btrfs_mark_buffer_dirty(eb);
3202 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003203 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003204}
3205
3206/*
Chris Mason74123bd2007-02-02 11:05:29 -05003207 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003208 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003209 *
3210 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3211 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003212 */
Chris Mason98ed5172008-01-03 10:01:48 -05003213static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003214 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003215 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003216{
David Sterbad30a6682019-03-20 14:16:45 +01003217 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003218 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003219 int src_nritems;
3220 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003221 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003222
Chris Mason5f39d392007-10-15 16:14:19 -04003223 src_nritems = btrfs_header_nritems(src);
3224 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003225 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003226 WARN_ON(btrfs_header_generation(src) != trans->transid);
3227 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003228
Chris Masonbce4eae2008-04-24 14:42:46 -04003229 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003230 return 1;
3231
Chris Masond3977122009-01-05 21:25:51 -05003232 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003233 return 1;
3234
Chris Masonbce4eae2008-04-24 14:42:46 -04003235 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003236 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003237 if (push_items < src_nritems) {
3238 /* leave at least 8 pointers in the node if
3239 * we aren't going to empty it
3240 */
3241 if (src_nritems - push_items < 8) {
3242 if (push_items <= 8)
3243 return 1;
3244 push_items -= 8;
3245 }
3246 }
3247 } else
3248 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003249
David Sterbaed874f02019-03-20 14:22:04 +01003250 ret = tree_mod_log_eb_copy(dst, src, dst_nritems, 0, push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003251 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003252 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003253 return ret;
3254 }
Chris Mason5f39d392007-10-15 16:14:19 -04003255 copy_extent_buffer(dst, src,
3256 btrfs_node_key_ptr_offset(dst_nritems),
3257 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003258 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003259
Chris Masonbb803952007-03-01 12:04:21 -05003260 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003261 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003262 * Don't call tree_mod_log_insert_move here, key removal was
3263 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003264 */
Chris Mason5f39d392007-10-15 16:14:19 -04003265 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3266 btrfs_node_key_ptr_offset(push_items),
3267 (src_nritems - push_items) *
3268 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003269 }
Chris Mason5f39d392007-10-15 16:14:19 -04003270 btrfs_set_header_nritems(src, src_nritems - push_items);
3271 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3272 btrfs_mark_buffer_dirty(src);
3273 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003274
Chris Masonbb803952007-03-01 12:04:21 -05003275 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003276}
3277
Chris Mason97571fd2007-02-24 13:39:08 -05003278/*
Chris Mason79f95c82007-03-01 15:16:26 -05003279 * try to push data from one node into the next node right in the
3280 * tree.
3281 *
3282 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3283 * error, and > 0 if there was no room in the right hand block.
3284 *
3285 * this will only push up to 1/2 the contents of the left node over
3286 */
Chris Mason5f39d392007-10-15 16:14:19 -04003287static int balance_node_right(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003288 struct extent_buffer *dst,
3289 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003290{
David Sterba55d32ed2019-03-20 14:18:06 +01003291 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Mason79f95c82007-03-01 15:16:26 -05003292 int push_items = 0;
3293 int max_push;
3294 int src_nritems;
3295 int dst_nritems;
3296 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003297
Chris Mason7bb86312007-12-11 09:25:06 -05003298 WARN_ON(btrfs_header_generation(src) != trans->transid);
3299 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3300
Chris Mason5f39d392007-10-15 16:14:19 -04003301 src_nritems = btrfs_header_nritems(src);
3302 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003303 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003304 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003305 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003306
Chris Masond3977122009-01-05 21:25:51 -05003307 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003308 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003309
3310 max_push = src_nritems / 2 + 1;
3311 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003312 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003313 return 1;
Yan252c38f2007-08-29 09:11:44 -04003314
Chris Mason79f95c82007-03-01 15:16:26 -05003315 if (max_push < push_items)
3316 push_items = max_push;
3317
David Sterbabf1d3422018-03-05 15:47:39 +01003318 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3319 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003320 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3321 btrfs_node_key_ptr_offset(0),
3322 (dst_nritems) *
3323 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003324
David Sterbaed874f02019-03-20 14:22:04 +01003325 ret = tree_mod_log_eb_copy(dst, src, 0, src_nritems - push_items,
3326 push_items);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003327 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003328 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003329 return ret;
3330 }
Chris Mason5f39d392007-10-15 16:14:19 -04003331 copy_extent_buffer(dst, src,
3332 btrfs_node_key_ptr_offset(0),
3333 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003334 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003335
Chris Mason5f39d392007-10-15 16:14:19 -04003336 btrfs_set_header_nritems(src, src_nritems - push_items);
3337 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003338
Chris Mason5f39d392007-10-15 16:14:19 -04003339 btrfs_mark_buffer_dirty(src);
3340 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003341
Chris Mason79f95c82007-03-01 15:16:26 -05003342 return ret;
3343}
3344
3345/*
Chris Mason97571fd2007-02-24 13:39:08 -05003346 * helper function to insert a new root level in the tree.
3347 * A new node is allocated, and a single item is inserted to
3348 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003349 *
3350 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003351 */
Chris Masond3977122009-01-05 21:25:51 -05003352static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003353 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003354 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003355{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003356 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003357 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003358 struct extent_buffer *lower;
3359 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003360 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003361 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01003362 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003363
3364 BUG_ON(path->nodes[level]);
3365 BUG_ON(path->nodes[level-1] != root->node);
3366
Chris Mason7bb86312007-12-11 09:25:06 -05003367 lower = path->nodes[level-1];
3368 if (level == 1)
3369 btrfs_item_key(lower, &lower_key, 0);
3370 else
3371 btrfs_node_key(lower, &lower_key, 0);
3372
Filipe Mananaa6279472019-01-25 11:48:51 +00003373 c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
3374 root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003375 if (IS_ERR(c))
3376 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003377
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003379
Chris Mason5f39d392007-10-15 16:14:19 -04003380 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003381 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003382 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003383 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003384 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003385
3386 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003387
3388 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003389
Chris Mason925baed2008-06-25 16:01:30 -04003390 old = root->node;
David Sterbad9d19a02018-03-05 16:35:29 +01003391 ret = tree_mod_log_insert_root(root->node, c, 0);
3392 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003393 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003394
3395 /* the super has an extra ref to root->node */
3396 free_extent_buffer(old);
3397
Chris Mason0b86a832008-03-24 15:01:56 -04003398 add_root_to_dirty_list(root);
David Sterba67439da2019-10-08 13:28:47 +02003399 atomic_inc(&c->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04003400 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303401 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003402 path->slots[level] = 0;
3403 return 0;
3404}
3405
Chris Mason74123bd2007-02-02 11:05:29 -05003406/*
3407 * worker function to insert a single pointer in a node.
3408 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003409 *
Chris Mason74123bd2007-02-02 11:05:29 -05003410 * slot and level indicate where you want the key to go, and
3411 * blocknr is the block the key points to.
3412 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003413static void insert_ptr(struct btrfs_trans_handle *trans,
David Sterba6ad3cf62019-03-20 14:32:45 +01003414 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003415 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003416 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003417{
Chris Mason5f39d392007-10-15 16:14:19 -04003418 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003419 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003420 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003421
3422 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003423 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003424 lower = path->nodes[level];
3425 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003426 BUG_ON(slot > nritems);
David Sterba6ad3cf62019-03-20 14:32:45 +01003427 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(trans->fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003428 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003429 if (level) {
3430 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003431 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003432 BUG_ON(ret < 0);
3433 }
Chris Mason5f39d392007-10-15 16:14:19 -04003434 memmove_extent_buffer(lower,
3435 btrfs_node_key_ptr_offset(slot + 1),
3436 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003437 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003438 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003439 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003440 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3441 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003442 BUG_ON(ret < 0);
3443 }
Chris Mason5f39d392007-10-15 16:14:19 -04003444 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003445 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003446 WARN_ON(trans->transid == 0);
3447 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003448 btrfs_set_header_nritems(lower, nritems + 1);
3449 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003450}
3451
Chris Mason97571fd2007-02-24 13:39:08 -05003452/*
3453 * split the node at the specified level in path in two.
3454 * The path is corrected to point to the appropriate node after the split
3455 *
3456 * Before splitting this tries to make some room in the node by pushing
3457 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003458 *
3459 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003460 */
Chris Masone02119d2008-09-05 16:13:11 -04003461static noinline int split_node(struct btrfs_trans_handle *trans,
3462 struct btrfs_root *root,
3463 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003464{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003465 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003466 struct extent_buffer *c;
3467 struct extent_buffer *split;
3468 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003469 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003470 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003471 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003472
Chris Mason5f39d392007-10-15 16:14:19 -04003473 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003474 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003475 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003476 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003477 * trying to split the root, lets make a new one
3478 *
Liu Bofdd99c72013-05-22 12:06:51 +00003479 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003480 * insert_new_root, because that root buffer will be kept as a
3481 * normal node. We are going to log removal of half of the
3482 * elements below with tree_mod_log_eb_copy. We're holding a
3483 * tree lock on the buffer, which is why we cannot race with
3484 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003485 */
Liu Bofdd99c72013-05-22 12:06:51 +00003486 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003487 if (ret)
3488 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003489 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003490 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003491 c = path->nodes[level];
3492 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003493 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003494 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003495 if (ret < 0)
3496 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003497 }
Chris Masone66f7092007-04-20 13:16:02 -04003498
Chris Mason5f39d392007-10-15 16:14:19 -04003499 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003500 mid = (c_nritems + 1) / 2;
3501 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003502
Filipe Mananaa6279472019-01-25 11:48:51 +00003503 split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
3504 c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003505 if (IS_ERR(split))
3506 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003507
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003508 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03003509 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04003510
David Sterbaed874f02019-03-20 14:22:04 +01003511 ret = tree_mod_log_eb_copy(split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003512 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003513 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003514 return ret;
3515 }
Chris Mason5f39d392007-10-15 16:14:19 -04003516 copy_extent_buffer(split, c,
3517 btrfs_node_key_ptr_offset(0),
3518 btrfs_node_key_ptr_offset(mid),
3519 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3520 btrfs_set_header_nritems(split, c_nritems - mid);
3521 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003522 ret = 0;
3523
Chris Mason5f39d392007-10-15 16:14:19 -04003524 btrfs_mark_buffer_dirty(c);
3525 btrfs_mark_buffer_dirty(split);
3526
David Sterba6ad3cf62019-03-20 14:32:45 +01003527 insert_ptr(trans, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003528 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003529
Chris Mason5de08d72007-02-24 06:24:44 -05003530 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003531 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003532 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003533 free_extent_buffer(c);
3534 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003535 path->slots[level + 1] += 1;
3536 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003537 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003538 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003539 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003540 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003541}
3542
Chris Mason74123bd2007-02-02 11:05:29 -05003543/*
3544 * how many bytes are required to store the items in a leaf. start
3545 * and nr indicate which items in the leaf to check. This totals up the
3546 * space used both by the item structs and the item data
3547 */
Chris Mason5f39d392007-10-15 16:14:19 -04003548static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003549{
Josef Bacik41be1f32012-10-15 13:43:18 -04003550 struct btrfs_item *start_item;
3551 struct btrfs_item *end_item;
3552 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003553 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003554 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003555 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003556
3557 if (!nr)
3558 return 0;
David Sterbac82f8232019-08-09 17:48:21 +02003559 btrfs_init_map_token(&token, l);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003560 start_item = btrfs_item_nr(start);
3561 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003562 data_len = btrfs_token_item_offset(l, start_item, &token) +
3563 btrfs_token_item_size(l, start_item, &token);
3564 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003565 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003566 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567 return data_len;
3568}
3569
Chris Mason74123bd2007-02-02 11:05:29 -05003570/*
Chris Masond4dbff92007-04-04 14:08:15 -04003571 * The space between the end of the leaf items and
3572 * the start of the leaf data. IOW, how much room
3573 * the leaf has left for both items and data
3574 */
David Sterbae902baa2019-03-20 14:36:46 +01003575noinline int btrfs_leaf_free_space(struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003576{
David Sterbae902baa2019-03-20 14:36:46 +01003577 struct btrfs_fs_info *fs_info = leaf->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003578 int nritems = btrfs_header_nritems(leaf);
3579 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003580
3581 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003582 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003583 btrfs_crit(fs_info,
3584 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3585 ret,
3586 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3587 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003588 }
3589 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003590}
3591
Chris Mason99d8f832010-07-07 10:51:48 -04003592/*
3593 * min slot controls the lowest index we're willing to push to the
3594 * right. We'll push up to and including min_slot, but no lower
3595 */
David Sterbaf72f0012019-03-20 14:39:45 +01003596static noinline int __push_leaf_right(struct btrfs_path *path,
Chris Mason44871b12009-03-13 10:04:31 -04003597 int data_size, int empty,
3598 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003599 int free_space, u32 left_nritems,
3600 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003601{
David Sterbaf72f0012019-03-20 14:39:45 +01003602 struct btrfs_fs_info *fs_info = right->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003603 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003604 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003605 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003606 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003607 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003608 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003609 int push_space = 0;
3610 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003611 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003612 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003613 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003614 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003615 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003616
Chris Mason34a38212007-11-07 13:31:03 -05003617 if (empty)
3618 nr = 0;
3619 else
Chris Mason99d8f832010-07-07 10:51:48 -04003620 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003621
Zheng Yan31840ae2008-09-23 13:14:14 -04003622 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003623 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003624
Chris Mason44871b12009-03-13 10:04:31 -04003625 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003626 i = left_nritems - 1;
3627 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003628 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003629
Zheng Yan31840ae2008-09-23 13:14:14 -04003630 if (!empty && push_items > 0) {
3631 if (path->slots[0] > i)
3632 break;
3633 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003634 int space = btrfs_leaf_free_space(left);
3635
Zheng Yan31840ae2008-09-23 13:14:14 -04003636 if (space + push_space * 2 > free_space)
3637 break;
3638 }
3639 }
3640
Chris Mason00ec4c52007-02-24 12:47:20 -05003641 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003642 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003643
Chris Masondb945352007-10-15 16:15:53 -04003644 this_item_size = btrfs_item_size(left, item);
3645 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003646 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003647
Chris Mason00ec4c52007-02-24 12:47:20 -05003648 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003649 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003650 if (i == 0)
3651 break;
3652 i--;
Chris Masondb945352007-10-15 16:15:53 -04003653 }
Chris Mason5f39d392007-10-15 16:14:19 -04003654
Chris Mason925baed2008-06-25 16:01:30 -04003655 if (push_items == 0)
3656 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003657
Julia Lawall6c1500f2012-11-03 20:30:18 +00003658 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003659
Chris Mason00ec4c52007-02-24 12:47:20 -05003660 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003661 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003662
Chris Mason5f39d392007-10-15 16:14:19 -04003663 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
David Sterba8f881e82019-03-20 11:33:10 +01003664 push_space -= leaf_data_end(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003665
Chris Mason00ec4c52007-02-24 12:47:20 -05003666 /* make room in the right data area */
David Sterba8f881e82019-03-20 11:33:10 +01003667 data_end = leaf_data_end(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003668 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003669 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3670 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003671 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003672
Chris Mason00ec4c52007-02-24 12:47:20 -05003673 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003674 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003675 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
David Sterba8f881e82019-03-20 11:33:10 +01003676 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(left),
Chris Masond6025572007-03-30 14:27:56 -04003677 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003678
3679 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3680 btrfs_item_nr_offset(0),
3681 right_nritems * sizeof(struct btrfs_item));
3682
Chris Mason00ec4c52007-02-24 12:47:20 -05003683 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003684 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3685 btrfs_item_nr_offset(left_nritems - push_items),
3686 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003687
3688 /* update the item pointers */
David Sterbac82f8232019-08-09 17:48:21 +02003689 btrfs_init_map_token(&token, right);
Chris Mason7518a232007-03-12 12:01:18 -04003690 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003691 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003692 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003693 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003694 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003695 push_space -= btrfs_token_item_size(right, item, &token);
3696 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003697 }
3698
Chris Mason7518a232007-03-12 12:01:18 -04003699 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003700 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003701
Chris Mason34a38212007-11-07 13:31:03 -05003702 if (left_nritems)
3703 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003704 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003705 btrfs_clean_tree_block(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003706
Chris Mason5f39d392007-10-15 16:14:19 -04003707 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003708
Chris Mason5f39d392007-10-15 16:14:19 -04003709 btrfs_item_key(right, &disk_key, 0);
3710 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003711 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003712
Chris Mason00ec4c52007-02-24 12:47:20 -05003713 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003714 if (path->slots[0] >= left_nritems) {
3715 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003716 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba6a884d7d2019-03-20 14:30:02 +01003717 btrfs_clean_tree_block(path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003718 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003719 free_extent_buffer(path->nodes[0]);
3720 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003721 path->slots[1] += 1;
3722 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003723 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003724 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003725 }
3726 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003727
3728out_unlock:
3729 btrfs_tree_unlock(right);
3730 free_extent_buffer(right);
3731 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003732}
Chris Mason925baed2008-06-25 16:01:30 -04003733
Chris Mason00ec4c52007-02-24 12:47:20 -05003734/*
Chris Mason44871b12009-03-13 10:04:31 -04003735 * push some data in the path leaf to the right, trying to free up at
3736 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3737 *
3738 * returns 1 if the push failed because the other node didn't have enough
3739 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003740 *
3741 * this will push starting from min_slot to the end of the leaf. It won't
3742 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003743 */
3744static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003745 *root, struct btrfs_path *path,
3746 int min_data_size, int data_size,
3747 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003748{
3749 struct extent_buffer *left = path->nodes[0];
3750 struct extent_buffer *right;
3751 struct extent_buffer *upper;
3752 int slot;
3753 int free_space;
3754 u32 left_nritems;
3755 int ret;
3756
3757 if (!path->nodes[1])
3758 return 1;
3759
3760 slot = path->slots[1];
3761 upper = path->nodes[1];
3762 if (slot >= btrfs_header_nritems(upper) - 1)
3763 return 1;
3764
3765 btrfs_assert_tree_locked(path->nodes[1]);
3766
David Sterba4b231ae2019-08-21 19:16:27 +02003767 right = btrfs_read_node_slot(upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003768 /*
3769 * slot + 1 is not valid or we fail to read the right node,
3770 * no big deal, just return.
3771 */
3772 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003773 return 1;
3774
Chris Mason44871b12009-03-13 10:04:31 -04003775 btrfs_tree_lock(right);
David Sterba8bead252018-04-04 02:03:48 +02003776 btrfs_set_lock_blocking_write(right);
Chris Mason44871b12009-03-13 10:04:31 -04003777
David Sterbae902baa2019-03-20 14:36:46 +01003778 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003779 if (free_space < data_size)
3780 goto out_unlock;
3781
3782 /* cow and double check */
3783 ret = btrfs_cow_block(trans, root, right, upper,
3784 slot + 1, &right);
3785 if (ret)
3786 goto out_unlock;
3787
David Sterbae902baa2019-03-20 14:36:46 +01003788 free_space = btrfs_leaf_free_space(right);
Chris Mason44871b12009-03-13 10:04:31 -04003789 if (free_space < data_size)
3790 goto out_unlock;
3791
3792 left_nritems = btrfs_header_nritems(left);
3793 if (left_nritems == 0)
3794 goto out_unlock;
3795
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003796 if (path->slots[0] == left_nritems && !empty) {
3797 /* Key greater than all keys in the leaf, right neighbor has
3798 * enough room for it and we're not emptying our leaf to delete
3799 * it, therefore use right neighbor to insert the new item and
Andrea Gelmini52042d82018-11-28 12:05:13 +01003800 * no need to touch/dirty our left leaf. */
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003801 btrfs_tree_unlock(left);
3802 free_extent_buffer(left);
3803 path->nodes[0] = right;
3804 path->slots[0] = 0;
3805 path->slots[1]++;
3806 return 0;
3807 }
3808
David Sterbaf72f0012019-03-20 14:39:45 +01003809 return __push_leaf_right(path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003810 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003811out_unlock:
3812 btrfs_tree_unlock(right);
3813 free_extent_buffer(right);
3814 return 1;
3815}
3816
3817/*
Chris Mason74123bd2007-02-02 11:05:29 -05003818 * push some data in the path leaf to the left, trying to free up at
3819 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003820 *
3821 * max_slot can put a limit on how far into the leaf we'll push items. The
3822 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3823 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003824 */
David Sterba8087c192019-03-20 14:40:41 +01003825static noinline int __push_leaf_left(struct btrfs_path *path, int data_size,
Chris Mason44871b12009-03-13 10:04:31 -04003826 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003827 int free_space, u32 right_nritems,
3828 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003829{
David Sterba8087c192019-03-20 14:40:41 +01003830 struct btrfs_fs_info *fs_info = left->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003831 struct btrfs_disk_key disk_key;
3832 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003833 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003834 int push_space = 0;
3835 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003836 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003837 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003838 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003839 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003840 u32 this_item_size;
3841 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003842 struct btrfs_map_token token;
3843
Chris Mason34a38212007-11-07 13:31:03 -05003844 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003845 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003846 else
Chris Mason99d8f832010-07-07 10:51:48 -04003847 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003848
3849 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003850 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003851
Zheng Yan31840ae2008-09-23 13:14:14 -04003852 if (!empty && push_items > 0) {
3853 if (path->slots[0] < i)
3854 break;
3855 if (path->slots[0] == i) {
David Sterbae902baa2019-03-20 14:36:46 +01003856 int space = btrfs_leaf_free_space(right);
3857
Zheng Yan31840ae2008-09-23 13:14:14 -04003858 if (space + push_space * 2 > free_space)
3859 break;
3860 }
3861 }
3862
Chris Masonbe0e5c02007-01-26 15:51:26 -05003863 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003864 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003865
3866 this_item_size = btrfs_item_size(right, item);
3867 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003868 break;
Chris Masondb945352007-10-15 16:15:53 -04003869
Chris Masonbe0e5c02007-01-26 15:51:26 -05003870 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003871 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003872 }
Chris Masondb945352007-10-15 16:15:53 -04003873
Chris Masonbe0e5c02007-01-26 15:51:26 -05003874 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003875 ret = 1;
3876 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003877 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303878 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003879
Chris Masonbe0e5c02007-01-26 15:51:26 -05003880 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003881 copy_extent_buffer(left, right,
3882 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3883 btrfs_item_nr_offset(0),
3884 push_items * sizeof(struct btrfs_item));
3885
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003886 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003887 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003888
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003889 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003890 leaf_data_end(left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003891 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003892 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003893 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003894 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003895 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003896
David Sterbac82f8232019-08-09 17:48:21 +02003897 btrfs_init_map_token(&token, left);
Chris Masondb945352007-10-15 16:15:53 -04003898 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003899 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003900 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003901
Ross Kirkdd3cc162013-09-16 15:58:09 +01003902 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003903
Chris Masoncfed81a2012-03-03 07:40:03 -05003904 ioff = btrfs_token_item_offset(left, item, &token);
3905 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003906 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003907 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003908 }
Chris Mason5f39d392007-10-15 16:14:19 -04003909 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003910
3911 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003912 if (push_items > right_nritems)
3913 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003914 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003915
Chris Mason34a38212007-11-07 13:31:03 -05003916 if (push_items < right_nritems) {
3917 push_space = btrfs_item_offset_nr(right, push_items - 1) -
David Sterba8f881e82019-03-20 11:33:10 +01003918 leaf_data_end(right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003919 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003920 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003921 BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01003922 leaf_data_end(right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003923
3924 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003925 btrfs_item_nr_offset(push_items),
3926 (btrfs_header_nritems(right) - push_items) *
3927 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003928 }
David Sterbac82f8232019-08-09 17:48:21 +02003929
3930 btrfs_init_map_token(&token, right);
Yaneef1c492007-11-26 10:58:13 -05003931 right_nritems -= push_items;
3932 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003933 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003934 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003935 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003936
Chris Masoncfed81a2012-03-03 07:40:03 -05003937 push_space = push_space - btrfs_token_item_size(right,
3938 item, &token);
3939 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003940 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003941
Chris Mason5f39d392007-10-15 16:14:19 -04003942 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003943 if (right_nritems)
3944 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003945 else
David Sterba6a884d7d2019-03-20 14:30:02 +01003946 btrfs_clean_tree_block(right);
Chris Mason098f59c2007-05-11 11:33:21 -04003947
Chris Mason5f39d392007-10-15 16:14:19 -04003948 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003949 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003950
3951 /* then fixup the leaf pointer in the path */
3952 if (path->slots[0] < push_items) {
3953 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003954 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003955 free_extent_buffer(path->nodes[0]);
3956 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003957 path->slots[1] -= 1;
3958 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003959 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003960 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003961 path->slots[0] -= push_items;
3962 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003963 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003964 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003965out:
3966 btrfs_tree_unlock(left);
3967 free_extent_buffer(left);
3968 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003969}
3970
Chris Mason74123bd2007-02-02 11:05:29 -05003971/*
Chris Mason44871b12009-03-13 10:04:31 -04003972 * push some data in the path leaf to the left, trying to free up at
3973 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003974 *
3975 * max_slot can put a limit on how far into the leaf we'll push items. The
3976 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3977 * items
Chris Mason44871b12009-03-13 10:04:31 -04003978 */
3979static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003980 *root, struct btrfs_path *path, int min_data_size,
3981 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003982{
3983 struct extent_buffer *right = path->nodes[0];
3984 struct extent_buffer *left;
3985 int slot;
3986 int free_space;
3987 u32 right_nritems;
3988 int ret = 0;
3989
3990 slot = path->slots[1];
3991 if (slot == 0)
3992 return 1;
3993 if (!path->nodes[1])
3994 return 1;
3995
3996 right_nritems = btrfs_header_nritems(right);
3997 if (right_nritems == 0)
3998 return 1;
3999
4000 btrfs_assert_tree_locked(path->nodes[1]);
4001
David Sterba4b231ae2019-08-21 19:16:27 +02004002 left = btrfs_read_node_slot(path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07004003 /*
4004 * slot - 1 is not valid or we fail to read the left node,
4005 * no big deal, just return.
4006 */
4007 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004008 return 1;
4009
Chris Mason44871b12009-03-13 10:04:31 -04004010 btrfs_tree_lock(left);
David Sterba8bead252018-04-04 02:03:48 +02004011 btrfs_set_lock_blocking_write(left);
Chris Mason44871b12009-03-13 10:04:31 -04004012
David Sterbae902baa2019-03-20 14:36:46 +01004013 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04004014 if (free_space < data_size) {
4015 ret = 1;
4016 goto out;
4017 }
4018
4019 /* cow and double check */
4020 ret = btrfs_cow_block(trans, root, left,
4021 path->nodes[1], slot - 1, &left);
4022 if (ret) {
4023 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004024 if (ret == -ENOSPC)
4025 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004026 goto out;
4027 }
4028
David Sterbae902baa2019-03-20 14:36:46 +01004029 free_space = btrfs_leaf_free_space(left);
Chris Mason44871b12009-03-13 10:04:31 -04004030 if (free_space < data_size) {
4031 ret = 1;
4032 goto out;
4033 }
4034
David Sterba8087c192019-03-20 14:40:41 +01004035 return __push_leaf_left(path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004036 empty, left, free_space, right_nritems,
4037 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004038out:
4039 btrfs_tree_unlock(left);
4040 free_extent_buffer(left);
4041 return ret;
4042}
4043
4044/*
Chris Mason74123bd2007-02-02 11:05:29 -05004045 * split the path's leaf in two, making sure there is at least data_size
4046 * available for the resulting leaf level of the path.
4047 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004048static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004049 struct btrfs_path *path,
4050 struct extent_buffer *l,
4051 struct extent_buffer *right,
4052 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004053{
David Sterba94f94ad2019-03-20 14:42:33 +01004054 struct btrfs_fs_info *fs_info = trans->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004055 int data_copy_size;
4056 int rt_data_off;
4057 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004058 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004059 struct btrfs_map_token token;
4060
Chris Mason5f39d392007-10-15 16:14:19 -04004061 nritems = nritems - mid;
4062 btrfs_set_header_nritems(right, nritems);
David Sterba8f881e82019-03-20 11:33:10 +01004063 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(l);
Chris Mason5f39d392007-10-15 16:14:19 -04004064
4065 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4066 btrfs_item_nr_offset(mid),
4067 nritems * sizeof(struct btrfs_item));
4068
4069 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004070 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4071 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
David Sterba8f881e82019-03-20 11:33:10 +01004072 leaf_data_end(l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004073
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004074 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004075
David Sterbac82f8232019-08-09 17:48:21 +02004076 btrfs_init_map_token(&token, right);
Chris Mason5f39d392007-10-15 16:14:19 -04004077 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004078 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004079 u32 ioff;
4080
Chris Masoncfed81a2012-03-03 07:40:03 -05004081 ioff = btrfs_token_item_offset(right, item, &token);
4082 btrfs_set_token_item_offset(right, item,
4083 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004084 }
Chris Mason74123bd2007-02-02 11:05:29 -05004085
Chris Mason5f39d392007-10-15 16:14:19 -04004086 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004087 btrfs_item_key(right, &disk_key, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004088 insert_ptr(trans, path, &disk_key, right->start, path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004089
4090 btrfs_mark_buffer_dirty(right);
4091 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004092 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004093
Chris Masonbe0e5c02007-01-26 15:51:26 -05004094 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004095 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004096 free_extent_buffer(path->nodes[0]);
4097 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004098 path->slots[0] -= mid;
4099 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004100 } else {
4101 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004102 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004103 }
Chris Mason5f39d392007-10-15 16:14:19 -04004104
Chris Masoneb60cea2007-02-02 09:18:22 -05004105 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004106}
4107
4108/*
Chris Mason99d8f832010-07-07 10:51:48 -04004109 * double splits happen when we need to insert a big item in the middle
4110 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4111 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4112 * A B C
4113 *
4114 * We avoid this by trying to push the items on either side of our target
4115 * into the adjacent leaves. If all goes well we can avoid the double split
4116 * completely.
4117 */
4118static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4119 struct btrfs_root *root,
4120 struct btrfs_path *path,
4121 int data_size)
4122{
4123 int ret;
4124 int progress = 0;
4125 int slot;
4126 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004127 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004128
4129 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004130 if (slot < btrfs_header_nritems(path->nodes[0]))
David Sterbae902baa2019-03-20 14:36:46 +01004131 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004132
4133 /*
4134 * try to push all the items after our slot into the
4135 * right leaf
4136 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004137 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004138 if (ret < 0)
4139 return ret;
4140
4141 if (ret == 0)
4142 progress++;
4143
4144 nritems = btrfs_header_nritems(path->nodes[0]);
4145 /*
4146 * our goal is to get our slot at the start or end of a leaf. If
4147 * we've done so we're done
4148 */
4149 if (path->slots[0] == 0 || path->slots[0] == nritems)
4150 return 0;
4151
David Sterbae902baa2019-03-20 14:36:46 +01004152 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004153 return 0;
4154
4155 /* try to push all the items before our slot into the next leaf */
4156 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004157 space_needed = data_size;
4158 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004159 space_needed -= btrfs_leaf_free_space(path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004160 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004161 if (ret < 0)
4162 return ret;
4163
4164 if (ret == 0)
4165 progress++;
4166
4167 if (progress)
4168 return 0;
4169 return 1;
4170}
4171
4172/*
Chris Mason44871b12009-03-13 10:04:31 -04004173 * split the path's leaf in two, making sure there is at least data_size
4174 * available for the resulting leaf level of the path.
4175 *
4176 * returns 0 if all went well and < 0 on failure.
4177 */
4178static noinline int split_leaf(struct btrfs_trans_handle *trans,
4179 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004180 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004181 struct btrfs_path *path, int data_size,
4182 int extend)
4183{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004184 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004185 struct extent_buffer *l;
4186 u32 nritems;
4187 int mid;
4188 int slot;
4189 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004190 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004191 int ret = 0;
4192 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004193 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004194 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004195 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004196
Yan, Zhenga5719522009-09-24 09:17:31 -04004197 l = path->nodes[0];
4198 slot = path->slots[0];
4199 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004200 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004201 return -EOVERFLOW;
4202
Chris Mason44871b12009-03-13 10:04:31 -04004203 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004204 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004205 int space_needed = data_size;
4206
4207 if (slot < btrfs_header_nritems(l))
David Sterbae902baa2019-03-20 14:36:46 +01004208 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004209
4210 wret = push_leaf_right(trans, root, path, space_needed,
4211 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004212 if (wret < 0)
4213 return wret;
4214 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004215 space_needed = data_size;
4216 if (slot > 0)
David Sterbae902baa2019-03-20 14:36:46 +01004217 space_needed -= btrfs_leaf_free_space(l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004218 wret = push_leaf_left(trans, root, path, space_needed,
4219 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004220 if (wret < 0)
4221 return wret;
4222 }
4223 l = path->nodes[0];
4224
4225 /* did the pushes work? */
David Sterbae902baa2019-03-20 14:36:46 +01004226 if (btrfs_leaf_free_space(l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004227 return 0;
4228 }
4229
4230 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004231 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004232 if (ret)
4233 return ret;
4234 }
4235again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004236 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004237 l = path->nodes[0];
4238 slot = path->slots[0];
4239 nritems = btrfs_header_nritems(l);
4240 mid = (nritems + 1) / 2;
4241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004242 if (mid <= slot) {
4243 if (nritems == 1 ||
4244 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004245 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004246 if (slot >= nritems) {
4247 split = 0;
4248 } else {
4249 mid = slot;
4250 if (mid != nritems &&
4251 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004252 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004253 if (data_size && !tried_avoid_double)
4254 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004255 split = 2;
4256 }
4257 }
4258 }
4259 } else {
4260 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004261 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004262 if (!extend && data_size && slot == 0) {
4263 split = 0;
4264 } else if ((extend || !data_size) && slot == 0) {
4265 mid = 1;
4266 } else {
4267 mid = slot;
4268 if (mid != nritems &&
4269 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004270 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004271 if (data_size && !tried_avoid_double)
4272 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304273 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004274 }
4275 }
4276 }
4277 }
4278
4279 if (split == 0)
4280 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4281 else
4282 btrfs_item_key(l, &disk_key, mid);
4283
Filipe Mananaa6279472019-01-25 11:48:51 +00004284 right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
4285 l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004286 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004287 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004288
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004289 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004290
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004291 if (split == 0) {
4292 if (mid <= slot) {
4293 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004294 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004295 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004296 btrfs_tree_unlock(path->nodes[0]);
4297 free_extent_buffer(path->nodes[0]);
4298 path->nodes[0] = right;
4299 path->slots[0] = 0;
4300 path->slots[1] += 1;
4301 } else {
4302 btrfs_set_header_nritems(right, 0);
David Sterba6ad3cf62019-03-20 14:32:45 +01004303 insert_ptr(trans, path, &disk_key,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004304 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004305 btrfs_tree_unlock(path->nodes[0]);
4306 free_extent_buffer(path->nodes[0]);
4307 path->nodes[0] = right;
4308 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004309 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004310 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004311 }
Liu Bo196e0242016-09-07 14:48:28 -07004312 /*
4313 * We create a new leaf 'right' for the required ins_len and
4314 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4315 * the content of ins_len to 'right'.
4316 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004317 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004318 }
4319
David Sterba94f94ad2019-03-20 14:42:33 +01004320 copy_for_split(trans, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004321
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004322 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004323 BUG_ON(num_doubles != 0);
4324 num_doubles++;
4325 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004326 }
Chris Mason44871b12009-03-13 10:04:31 -04004327
Jeff Mahoney143bede2012-03-01 14:56:26 +01004328 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004329
4330push_for_double:
4331 push_for_double_split(trans, root, path, data_size);
4332 tried_avoid_double = 1;
David Sterbae902baa2019-03-20 14:36:46 +01004333 if (btrfs_leaf_free_space(path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004334 return 0;
4335 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004336}
4337
Yan, Zhengad48fd752009-11-12 09:33:58 +00004338static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4339 struct btrfs_root *root,
4340 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004341{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004342 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004343 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004344 struct btrfs_file_extent_item *fi;
4345 u64 extent_len = 0;
4346 u32 item_size;
4347 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004348
4349 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004350 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4351
4352 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4353 key.type != BTRFS_EXTENT_CSUM_KEY);
4354
David Sterbae902baa2019-03-20 14:36:46 +01004355 if (btrfs_leaf_free_space(leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004356 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004357
4358 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004359 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4360 fi = btrfs_item_ptr(leaf, path->slots[0],
4361 struct btrfs_file_extent_item);
4362 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4363 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004364 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004365
Chris Mason459931e2008-12-10 09:10:46 -05004366 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004367 path->search_for_split = 1;
4368 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004369 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004370 if (ret > 0)
4371 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004372 if (ret < 0)
4373 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004374
Yan, Zhengad48fd752009-11-12 09:33:58 +00004375 ret = -EAGAIN;
4376 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004377 /* if our item isn't there, return now */
4378 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004379 goto err;
4380
Chris Mason109f6ae2010-04-02 09:20:18 -04004381 /* the leaf has changed, it now has room. return now */
David Sterbae902baa2019-03-20 14:36:46 +01004382 if (btrfs_leaf_free_space(path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004383 goto err;
4384
Yan, Zhengad48fd752009-11-12 09:33:58 +00004385 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4386 fi = btrfs_item_ptr(leaf, path->slots[0],
4387 struct btrfs_file_extent_item);
4388 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4389 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004390 }
4391
Chris Masonb9473432009-03-13 11:00:37 -04004392 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004393 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004394 if (ret)
4395 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004396
Yan, Zhengad48fd752009-11-12 09:33:58 +00004397 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004398 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004399 return 0;
4400err:
4401 path->keep_locks = 0;
4402 return ret;
4403}
4404
David Sterba25263cd2019-03-20 14:44:57 +01004405static noinline int split_item(struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004406 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004407 unsigned long split_offset)
4408{
4409 struct extent_buffer *leaf;
4410 struct btrfs_item *item;
4411 struct btrfs_item *new_item;
4412 int slot;
4413 char *buf;
4414 u32 nritems;
4415 u32 item_size;
4416 u32 orig_offset;
4417 struct btrfs_disk_key disk_key;
4418
Chris Masonb9473432009-03-13 11:00:37 -04004419 leaf = path->nodes[0];
David Sterbae902baa2019-03-20 14:36:46 +01004420 BUG_ON(btrfs_leaf_free_space(leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004421
Chris Masonb4ce94d2009-02-04 09:25:08 -05004422 btrfs_set_path_blocking(path);
4423
Ross Kirkdd3cc162013-09-16 15:58:09 +01004424 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004425 orig_offset = btrfs_item_offset(leaf, item);
4426 item_size = btrfs_item_size(leaf, item);
4427
Chris Mason459931e2008-12-10 09:10:46 -05004428 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004429 if (!buf)
4430 return -ENOMEM;
4431
Chris Mason459931e2008-12-10 09:10:46 -05004432 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4433 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004434
Chris Mason459931e2008-12-10 09:10:46 -05004435 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004436 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004437 if (slot != nritems) {
4438 /* shift the items */
4439 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004440 btrfs_item_nr_offset(slot),
4441 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004442 }
4443
4444 btrfs_cpu_key_to_disk(&disk_key, new_key);
4445 btrfs_set_item_key(leaf, &disk_key, slot);
4446
Ross Kirkdd3cc162013-09-16 15:58:09 +01004447 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004448
4449 btrfs_set_item_offset(leaf, new_item, orig_offset);
4450 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4451
4452 btrfs_set_item_offset(leaf, item,
4453 orig_offset + item_size - split_offset);
4454 btrfs_set_item_size(leaf, item, split_offset);
4455
4456 btrfs_set_header_nritems(leaf, nritems + 1);
4457
4458 /* write the data for the start of the original item */
4459 write_extent_buffer(leaf, buf,
4460 btrfs_item_ptr_offset(leaf, path->slots[0]),
4461 split_offset);
4462
4463 /* write the data for the new item */
4464 write_extent_buffer(leaf, buf + split_offset,
4465 btrfs_item_ptr_offset(leaf, slot),
4466 item_size - split_offset);
4467 btrfs_mark_buffer_dirty(leaf);
4468
David Sterbae902baa2019-03-20 14:36:46 +01004469 BUG_ON(btrfs_leaf_free_space(leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004470 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004471 return 0;
4472}
4473
4474/*
4475 * This function splits a single item into two items,
4476 * giving 'new_key' to the new item and splitting the
4477 * old one at split_offset (from the start of the item).
4478 *
4479 * The path may be released by this operation. After
4480 * the split, the path is pointing to the old item. The
4481 * new item is going to be in the same node as the old one.
4482 *
4483 * Note, the item being split must be smaller enough to live alone on
4484 * a tree block with room for one extra struct btrfs_item
4485 *
4486 * This allows us to split the item in place, keeping a lock on the
4487 * leaf the entire time.
4488 */
4489int btrfs_split_item(struct btrfs_trans_handle *trans,
4490 struct btrfs_root *root,
4491 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004492 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004493 unsigned long split_offset)
4494{
4495 int ret;
4496 ret = setup_leaf_for_split(trans, root, path,
4497 sizeof(struct btrfs_item));
4498 if (ret)
4499 return ret;
4500
David Sterba25263cd2019-03-20 14:44:57 +01004501 ret = split_item(path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004502 return ret;
4503}
4504
4505/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004506 * This function duplicate a item, giving 'new_key' to the new item.
4507 * It guarantees both items live in the same tree leaf and the new item
4508 * is contiguous with the original item.
4509 *
4510 * This allows us to split file extent in place, keeping a lock on the
4511 * leaf the entire time.
4512 */
4513int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4514 struct btrfs_root *root,
4515 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004516 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004517{
4518 struct extent_buffer *leaf;
4519 int ret;
4520 u32 item_size;
4521
4522 leaf = path->nodes[0];
4523 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4524 ret = setup_leaf_for_split(trans, root, path,
4525 item_size + sizeof(struct btrfs_item));
4526 if (ret)
4527 return ret;
4528
4529 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004530 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004531 item_size, item_size +
4532 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004533 leaf = path->nodes[0];
4534 memcpy_extent_buffer(leaf,
4535 btrfs_item_ptr_offset(leaf, path->slots[0]),
4536 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4537 item_size);
4538 return 0;
4539}
4540
4541/*
Chris Masond352ac62008-09-29 15:18:18 -04004542 * make the item pointed to by the path smaller. new_size indicates
4543 * how small to make it, and from_end tells us if we just chop bytes
4544 * off the end of the item or if we shift the item to chop bytes off
4545 * the front.
4546 */
David Sterba78ac4f92019-03-20 14:49:12 +01004547void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004548{
Chris Masonb18c6682007-04-17 13:26:50 -04004549 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004550 struct extent_buffer *leaf;
4551 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004552 u32 nritems;
4553 unsigned int data_end;
4554 unsigned int old_data_start;
4555 unsigned int old_size;
4556 unsigned int size_diff;
4557 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004558 struct btrfs_map_token token;
4559
Chris Mason5f39d392007-10-15 16:14:19 -04004560 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004561 slot = path->slots[0];
4562
4563 old_size = btrfs_item_size_nr(leaf, slot);
4564 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004565 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004566
Chris Mason5f39d392007-10-15 16:14:19 -04004567 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004568 data_end = leaf_data_end(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004569
Chris Mason5f39d392007-10-15 16:14:19 -04004570 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004571
Chris Masonb18c6682007-04-17 13:26:50 -04004572 size_diff = old_size - new_size;
4573
4574 BUG_ON(slot < 0);
4575 BUG_ON(slot >= nritems);
4576
4577 /*
4578 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4579 */
4580 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004581 btrfs_init_map_token(&token, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004582 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004583 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004584 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004585
Chris Masoncfed81a2012-03-03 07:40:03 -05004586 ioff = btrfs_token_item_offset(leaf, item, &token);
4587 btrfs_set_token_item_offset(leaf, item,
4588 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004589 }
Chris Masondb945352007-10-15 16:15:53 -04004590
Chris Masonb18c6682007-04-17 13:26:50 -04004591 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004592 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004593 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4594 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004595 data_end, old_data_start + new_size - data_end);
4596 } else {
4597 struct btrfs_disk_key disk_key;
4598 u64 offset;
4599
4600 btrfs_item_key(leaf, &disk_key, slot);
4601
4602 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4603 unsigned long ptr;
4604 struct btrfs_file_extent_item *fi;
4605
4606 fi = btrfs_item_ptr(leaf, slot,
4607 struct btrfs_file_extent_item);
4608 fi = (struct btrfs_file_extent_item *)(
4609 (unsigned long)fi - size_diff);
4610
4611 if (btrfs_file_extent_type(leaf, fi) ==
4612 BTRFS_FILE_EXTENT_INLINE) {
4613 ptr = btrfs_item_ptr_offset(leaf, slot);
4614 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004615 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004616 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004617 }
4618 }
4619
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004620 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4621 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004622 data_end, old_data_start - data_end);
4623
4624 offset = btrfs_disk_key_offset(&disk_key);
4625 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4626 btrfs_set_item_key(leaf, &disk_key, slot);
4627 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004628 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004629 }
Chris Mason5f39d392007-10-15 16:14:19 -04004630
Ross Kirkdd3cc162013-09-16 15:58:09 +01004631 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004632 btrfs_set_item_size(leaf, item, new_size);
4633 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004634
David Sterbae902baa2019-03-20 14:36:46 +01004635 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004636 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004637 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004638 }
Chris Masonb18c6682007-04-17 13:26:50 -04004639}
4640
Chris Masond352ac62008-09-29 15:18:18 -04004641/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004642 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004643 */
David Sterbac71dd882019-03-20 14:51:10 +01004644void btrfs_extend_item(struct btrfs_path *path, u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004645{
Chris Mason6567e832007-04-16 09:22:45 -04004646 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004647 struct extent_buffer *leaf;
4648 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004649 u32 nritems;
4650 unsigned int data_end;
4651 unsigned int old_data;
4652 unsigned int old_size;
4653 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004654 struct btrfs_map_token token;
4655
Chris Mason5f39d392007-10-15 16:14:19 -04004656 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004657
Chris Mason5f39d392007-10-15 16:14:19 -04004658 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004659 data_end = leaf_data_end(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004660
David Sterbae902baa2019-03-20 14:36:46 +01004661 if (btrfs_leaf_free_space(leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004662 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004663 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004664 }
Chris Mason6567e832007-04-16 09:22:45 -04004665 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004666 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004667
4668 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004669 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004670 btrfs_print_leaf(leaf);
David Sterbac71dd882019-03-20 14:51:10 +01004671 btrfs_crit(leaf->fs_info, "slot %d too large, nritems %d",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004672 slot, nritems);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004673 BUG();
Chris Mason3326d1b2007-10-15 16:18:25 -04004674 }
Chris Mason6567e832007-04-16 09:22:45 -04004675
4676 /*
4677 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4678 */
4679 /* first correct the data pointers */
David Sterbac82f8232019-08-09 17:48:21 +02004680 btrfs_init_map_token(&token, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004681 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004682 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004683 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004684
Chris Masoncfed81a2012-03-03 07:40:03 -05004685 ioff = btrfs_token_item_offset(leaf, item, &token);
4686 btrfs_set_token_item_offset(leaf, item,
4687 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004688 }
Chris Mason5f39d392007-10-15 16:14:19 -04004689
Chris Mason6567e832007-04-16 09:22:45 -04004690 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004691 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4692 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004693 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004694
Chris Mason6567e832007-04-16 09:22:45 -04004695 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004696 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004697 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004698 btrfs_set_item_size(leaf, item, old_size + data_size);
4699 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004700
David Sterbae902baa2019-03-20 14:36:46 +01004701 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004702 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004703 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004704 }
Chris Mason6567e832007-04-16 09:22:45 -04004705}
4706
Chris Mason74123bd2007-02-02 11:05:29 -05004707/*
Chris Mason44871b12009-03-13 10:04:31 -04004708 * this is a helper for btrfs_insert_empty_items, the main goal here is
4709 * to save stack depth by doing the bulk of the work in a function
4710 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004711 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004712void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004713 const struct btrfs_key *cpu_key, u32 *data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004714 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004716 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004717 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004718 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004719 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004720 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004721 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004722 struct extent_buffer *leaf;
4723 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004724 struct btrfs_map_token token;
4725
Filipe Manana24cdc842014-07-28 19:34:35 +01004726 if (path->slots[0] == 0) {
4727 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004728 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004729 }
4730 btrfs_unlock_up_safe(path, 1);
4731
Chris Mason5f39d392007-10-15 16:14:19 -04004732 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004733 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004734
Chris Mason5f39d392007-10-15 16:14:19 -04004735 nritems = btrfs_header_nritems(leaf);
David Sterba8f881e82019-03-20 11:33:10 +01004736 data_end = leaf_data_end(leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004737
David Sterbae902baa2019-03-20 14:36:46 +01004738 if (btrfs_leaf_free_space(leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004739 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004740 btrfs_crit(fs_info, "not enough freespace need %u have %d",
David Sterbae902baa2019-03-20 14:36:46 +01004741 total_size, btrfs_leaf_free_space(leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004742 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004743 }
Chris Mason5f39d392007-10-15 16:14:19 -04004744
David Sterbac82f8232019-08-09 17:48:21 +02004745 btrfs_init_map_token(&token, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004746 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004747 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004748
Chris Mason5f39d392007-10-15 16:14:19 -04004749 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004750 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004751 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004752 slot, old_data, data_end);
Arnd Bergmann290342f2019-03-25 14:02:25 +01004753 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004754 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004755 /*
4756 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4757 */
4758 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004759 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004760 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004761
Jeff Mahoney62e85572016-09-20 10:05:01 -04004762 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004763 ioff = btrfs_token_item_offset(leaf, item, &token);
4764 btrfs_set_token_item_offset(leaf, item,
4765 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004766 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004767 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004768 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004769 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004770 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004771
4772 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004773 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4774 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004775 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004776 data_end = old_data;
4777 }
Chris Mason5f39d392007-10-15 16:14:19 -04004778
Chris Mason62e27492007-03-15 12:56:47 -04004779 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004780 for (i = 0; i < nr; i++) {
4781 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4782 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004783 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004784 btrfs_set_token_item_offset(leaf, item,
4785 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004786 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004787 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004788 }
Chris Mason44871b12009-03-13 10:04:31 -04004789
Chris Mason9c583092008-01-29 15:15:18 -05004790 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004791 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004792
David Sterbae902baa2019-03-20 14:36:46 +01004793 if (btrfs_leaf_free_space(leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004794 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004795 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004796 }
Chris Mason44871b12009-03-13 10:04:31 -04004797}
4798
4799/*
4800 * Given a key and some data, insert items into the tree.
4801 * This does all the path init required, making room in the tree if needed.
4802 */
4803int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4804 struct btrfs_root *root,
4805 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004806 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004807 int nr)
4808{
Chris Mason44871b12009-03-13 10:04:31 -04004809 int ret = 0;
4810 int slot;
4811 int i;
4812 u32 total_size = 0;
4813 u32 total_data = 0;
4814
4815 for (i = 0; i < nr; i++)
4816 total_data += data_size[i];
4817
4818 total_size = total_data + (nr * sizeof(struct btrfs_item));
4819 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4820 if (ret == 0)
4821 return -EEXIST;
4822 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004823 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004824
Chris Mason44871b12009-03-13 10:04:31 -04004825 slot = path->slots[0];
4826 BUG_ON(slot < 0);
4827
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004828 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004829 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004830 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004831}
4832
4833/*
4834 * Given a key and some data, insert an item into the tree.
4835 * This does all the path init required, making room in the tree if needed.
4836 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004837int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4838 const struct btrfs_key *cpu_key, void *data,
4839 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004840{
4841 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004842 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004843 struct extent_buffer *leaf;
4844 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004845
Chris Mason2c90e5d2007-04-02 10:50:19 -04004846 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004847 if (!path)
4848 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004849 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004850 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004851 leaf = path->nodes[0];
4852 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4853 write_extent_buffer(leaf, data, ptr, data_size);
4854 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004855 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004856 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004857 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004858}
4859
Chris Mason74123bd2007-02-02 11:05:29 -05004860/*
Chris Mason5de08d72007-02-24 06:24:44 -05004861 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004862 *
Chris Masond352ac62008-09-29 15:18:18 -04004863 * the tree should have been previously balanced so the deletion does not
4864 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004865 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004866static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4867 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004868{
Chris Mason5f39d392007-10-15 16:14:19 -04004869 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004870 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004871 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004872
Chris Mason5f39d392007-10-15 16:14:19 -04004873 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004874 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004875 if (level) {
4876 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004877 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004878 BUG_ON(ret < 0);
4879 }
Chris Mason5f39d392007-10-15 16:14:19 -04004880 memmove_extent_buffer(parent,
4881 btrfs_node_key_ptr_offset(slot),
4882 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004883 sizeof(struct btrfs_key_ptr) *
4884 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004885 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004886 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4887 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004888 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004889 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004890
Chris Mason7518a232007-03-12 12:01:18 -04004891 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004892 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004893 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004894 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004895 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004896 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004897 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004898 struct btrfs_disk_key disk_key;
4899
4900 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004901 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004902 }
Chris Masond6025572007-03-30 14:27:56 -04004903 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004904}
4905
Chris Mason74123bd2007-02-02 11:05:29 -05004906/*
Chris Mason323ac952008-10-01 19:05:46 -04004907 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004908 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004909 *
4910 * This deletes the pointer in path->nodes[1] and frees the leaf
4911 * block extent. zero is returned if it all worked out, < 0 otherwise.
4912 *
4913 * The path must have already been setup for deleting the leaf, including
4914 * all the proper balancing. path->nodes[1] must be locked.
4915 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004916static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4917 struct btrfs_root *root,
4918 struct btrfs_path *path,
4919 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004920{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004921 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004922 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004923
Chris Mason4d081c42009-02-04 09:31:28 -05004924 /*
4925 * btrfs_free_extent is expensive, we want to make sure we
4926 * aren't holding any locks when we call it
4927 */
4928 btrfs_unlock_up_safe(path, 0);
4929
Yan, Zhengf0486c62010-05-16 10:46:25 -04004930 root_sub_used(root, leaf->len);
4931
David Sterba67439da2019-10-08 13:28:47 +02004932 atomic_inc(&leaf->refs);
Jan Schmidt5581a512012-05-16 17:04:52 +02004933 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004934 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004935}
4936/*
Chris Mason74123bd2007-02-02 11:05:29 -05004937 * delete the item at the leaf level in path. If that empties
4938 * the leaf, remove it from the tree
4939 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004940int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4941 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004942{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004943 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004944 struct extent_buffer *leaf;
4945 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004946 u32 last_off;
4947 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004948 int ret = 0;
4949 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004950 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004951 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004952
Chris Mason5f39d392007-10-15 16:14:19 -04004953 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004954 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4955
4956 for (i = 0; i < nr; i++)
4957 dsize += btrfs_item_size_nr(leaf, slot + i);
4958
Chris Mason5f39d392007-10-15 16:14:19 -04004959 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004960
Chris Mason85e21ba2008-01-29 15:11:36 -05004961 if (slot + nr != nritems) {
David Sterba8f881e82019-03-20 11:33:10 +01004962 int data_end = leaf_data_end(leaf);
David Sterbac82f8232019-08-09 17:48:21 +02004963 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04004964
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004965 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004966 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004967 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004968 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004969
David Sterbac82f8232019-08-09 17:48:21 +02004970 btrfs_init_map_token(&token, leaf);
Chris Mason85e21ba2008-01-29 15:11:36 -05004971 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004972 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004973
Ross Kirkdd3cc162013-09-16 15:58:09 +01004974 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004975 ioff = btrfs_token_item_offset(leaf, item, &token);
4976 btrfs_set_token_item_offset(leaf, item,
4977 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004978 }
Chris Masondb945352007-10-15 16:15:53 -04004979
Chris Mason5f39d392007-10-15 16:14:19 -04004980 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004981 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004982 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004983 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004984 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004985 btrfs_set_header_nritems(leaf, nritems - nr);
4986 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004987
Chris Mason74123bd2007-02-02 11:05:29 -05004988 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004989 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004990 if (leaf == root->node) {
4991 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004992 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004993 btrfs_set_path_blocking(path);
David Sterba6a884d7d2019-03-20 14:30:02 +01004994 btrfs_clean_tree_block(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004995 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004996 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004997 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004998 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004999 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005000 struct btrfs_disk_key disk_key;
5001
5002 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03005003 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005004 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005005
Chris Mason74123bd2007-02-02 11:05:29 -05005006 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005007 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005008 /* push_leaf_left fixes the path.
5009 * make sure the path still points to our leaf
5010 * for possible call to del_ptr below
5011 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005012 slot = path->slots[1];
David Sterba67439da2019-10-08 13:28:47 +02005013 atomic_inc(&leaf->refs);
Chris Mason5f39d392007-10-15 16:14:19 -04005014
Chris Masonb9473432009-03-13 11:00:37 -04005015 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005016 wret = push_leaf_left(trans, root, path, 1, 1,
5017 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005018 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005019 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005020
5021 if (path->nodes[0] == leaf &&
5022 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005023 wret = push_leaf_right(trans, root, path, 1,
5024 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005025 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005026 ret = wret;
5027 }
Chris Mason5f39d392007-10-15 16:14:19 -04005028
5029 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005030 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005031 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005032 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005033 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005034 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005035 /* if we're still in the path, make sure
5036 * we're dirty. Otherwise, one of the
5037 * push_leaf functions must have already
5038 * dirtied this buffer
5039 */
5040 if (path->nodes[0] == leaf)
5041 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005042 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005043 }
Chris Masond5719762007-03-23 10:01:08 -04005044 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005045 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005046 }
5047 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005048 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005049}
5050
Chris Mason97571fd2007-02-24 13:39:08 -05005051/*
Chris Mason925baed2008-06-25 16:01:30 -04005052 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005053 * returns 0 if it found something or 1 if there are no lesser leaves.
5054 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005055 *
5056 * This may release the path, and so you may lose any locks held at the
5057 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005058 */
Josef Bacik16e75492013-10-22 12:18:51 -04005059int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005060{
Chris Mason925baed2008-06-25 16:01:30 -04005061 struct btrfs_key key;
5062 struct btrfs_disk_key found_key;
5063 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005064
Chris Mason925baed2008-06-25 16:01:30 -04005065 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005066
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005067 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005068 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005069 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005070 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005071 key.offset = (u64)-1;
5072 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005073 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005074 key.type = (u8)-1;
5075 key.offset = (u64)-1;
5076 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005077 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005078 }
Chris Mason7bb86312007-12-11 09:25:06 -05005079
David Sterbab3b4aa72011-04-21 01:20:15 +02005080 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5082 if (ret < 0)
5083 return ret;
5084 btrfs_item_key(path->nodes[0], &found_key, 0);
5085 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005086 /*
5087 * We might have had an item with the previous key in the tree right
5088 * before we released our path. And after we released our path, that
5089 * item might have been pushed to the first slot (0) of the leaf we
5090 * were holding due to a tree balance. Alternatively, an item with the
5091 * previous key can exist as the only element of a leaf (big fat item).
5092 * Therefore account for these 2 cases, so that our callers (like
5093 * btrfs_previous_item) don't miss an existing item with a key matching
5094 * the previous key we computed above.
5095 */
5096 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005097 return 0;
5098 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005099}
5100
Chris Mason3f157a22008-06-25 16:01:31 -04005101/*
5102 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005103 * for nodes or leaves that are have a minimum transaction id.
5104 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005105 *
5106 * This does not cow, but it does stuff the starting key it finds back
5107 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5108 * key and get a writable path.
5109 *
Chris Mason3f157a22008-06-25 16:01:31 -04005110 * This honors path->lowest_level to prevent descent past a given level
5111 * of the tree.
5112 *
Chris Masond352ac62008-09-29 15:18:18 -04005113 * min_trans indicates the oldest transaction that you are interested
5114 * in walking through. Any nodes or leaves older than min_trans are
5115 * skipped over (without reading them).
5116 *
Chris Mason3f157a22008-06-25 16:01:31 -04005117 * returns zero if something useful was found, < 0 on error and 1 if there
5118 * was nothing in the tree that matched the search criteria.
5119 */
5120int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005121 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005122 u64 min_trans)
5123{
5124 struct extent_buffer *cur;
5125 struct btrfs_key found_key;
5126 int slot;
Yan96524802008-07-24 12:19:49 -04005127 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005128 u32 nritems;
5129 int level;
5130 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005131 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005132
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005133 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005134again:
Chris Masonbd681512011-07-16 15:23:14 -04005135 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005136 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005137 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005138 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005139 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005140
5141 if (btrfs_header_generation(cur) < min_trans) {
5142 ret = 1;
5143 goto out;
5144 }
Chris Masond3977122009-01-05 21:25:51 -05005145 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005146 nritems = btrfs_header_nritems(cur);
5147 level = btrfs_header_level(cur);
Nikolay Borisova74b35e2017-12-08 16:27:43 +02005148 sret = btrfs_bin_search(cur, min_key, level, &slot);
Filipe Mananacbca7d52019-02-18 16:57:26 +00005149 if (sret < 0) {
5150 ret = sret;
5151 goto out;
5152 }
Chris Mason3f157a22008-06-25 16:01:31 -04005153
Chris Mason323ac952008-10-01 19:05:46 -04005154 /* at the lowest level, we're done, setup the path and exit */
5155 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005156 if (slot >= nritems)
5157 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005158 ret = 0;
5159 path->slots[level] = slot;
5160 btrfs_item_key_to_cpu(cur, &found_key, slot);
5161 goto out;
5162 }
Yan96524802008-07-24 12:19:49 -04005163 if (sret && slot > 0)
5164 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005165 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005166 * check this node pointer against the min_trans parameters.
5167 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005168 */
Chris Masond3977122009-01-05 21:25:51 -05005169 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005170 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005171
Chris Mason3f157a22008-06-25 16:01:31 -04005172 gen = btrfs_node_ptr_generation(cur, slot);
5173 if (gen < min_trans) {
5174 slot++;
5175 continue;
5176 }
Eric Sandeende78b512013-01-31 18:21:12 +00005177 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005178 }
Chris Masone02119d2008-09-05 16:13:11 -04005179find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005180 /*
5181 * we didn't find a candidate key in this node, walk forward
5182 * and find another one
5183 */
5184 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005185 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005186 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005187 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005188 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005189 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005190 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005191 goto again;
5192 } else {
5193 goto out;
5194 }
5195 }
5196 /* save our key for returning back */
5197 btrfs_node_key_to_cpu(cur, &found_key, slot);
5198 path->slots[level] = slot;
5199 if (level == path->lowest_level) {
5200 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005201 goto out;
5202 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005203 btrfs_set_path_blocking(path);
David Sterba4b231ae2019-08-21 19:16:27 +02005204 cur = btrfs_read_node_slot(cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005205 if (IS_ERR(cur)) {
5206 ret = PTR_ERR(cur);
5207 goto out;
5208 }
Chris Mason3f157a22008-06-25 16:01:31 -04005209
Chris Masonbd681512011-07-16 15:23:14 -04005210 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005211
Chris Masonbd681512011-07-16 15:23:14 -04005212 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005213 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005214 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005215 }
5216out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005217 path->keep_locks = keep_locks;
5218 if (ret == 0) {
5219 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5220 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005221 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005222 }
Chris Mason3f157a22008-06-25 16:01:31 -04005223 return ret;
5224}
5225
5226/*
5227 * this is similar to btrfs_next_leaf, but does not try to preserve
5228 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005229 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005230 *
5231 * 0 is returned if another key is found, < 0 if there are any errors
5232 * and 1 is returned if there are no higher keys in the tree
5233 *
5234 * path->keep_locks should be set to 1 on the search made before
5235 * calling this function.
5236 */
Chris Masone7a84562008-06-25 16:01:31 -04005237int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005238 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005239{
Chris Masone7a84562008-06-25 16:01:31 -04005240 int slot;
5241 struct extent_buffer *c;
5242
Josef Bacik6a9fb462019-06-20 15:37:52 -04005243 WARN_ON(!path->keep_locks && !path->skip_locking);
Chris Masond3977122009-01-05 21:25:51 -05005244 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005245 if (!path->nodes[level])
5246 return 1;
5247
5248 slot = path->slots[level] + 1;
5249 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005250next:
Chris Masone7a84562008-06-25 16:01:31 -04005251 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005252 int ret;
5253 int orig_lowest;
5254 struct btrfs_key cur_key;
5255 if (level + 1 >= BTRFS_MAX_LEVEL ||
5256 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005257 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005258
Josef Bacik6a9fb462019-06-20 15:37:52 -04005259 if (path->locks[level + 1] || path->skip_locking) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005260 level++;
5261 continue;
5262 }
5263
5264 slot = btrfs_header_nritems(c) - 1;
5265 if (level == 0)
5266 btrfs_item_key_to_cpu(c, &cur_key, slot);
5267 else
5268 btrfs_node_key_to_cpu(c, &cur_key, slot);
5269
5270 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005271 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005272 path->lowest_level = level;
5273 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5274 0, 0);
5275 path->lowest_level = orig_lowest;
5276 if (ret < 0)
5277 return ret;
5278
5279 c = path->nodes[level];
5280 slot = path->slots[level];
5281 if (ret == 0)
5282 slot++;
5283 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005284 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005285
Chris Masone7a84562008-06-25 16:01:31 -04005286 if (level == 0)
5287 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005288 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005289 u64 gen = btrfs_node_ptr_generation(c, slot);
5290
Chris Mason3f157a22008-06-25 16:01:31 -04005291 if (gen < min_trans) {
5292 slot++;
5293 goto next;
5294 }
Chris Masone7a84562008-06-25 16:01:31 -04005295 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005296 }
Chris Masone7a84562008-06-25 16:01:31 -04005297 return 0;
5298 }
5299 return 1;
5300}
5301
Chris Mason7bb86312007-12-11 09:25:06 -05005302/*
Chris Mason925baed2008-06-25 16:01:30 -04005303 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005304 * returns 0 if it found something or 1 if there are no greater leaves.
5305 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005306 */
Chris Mason234b63a2007-03-13 10:46:10 -04005307int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005308{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005309 return btrfs_next_old_leaf(root, path, 0);
5310}
5311
5312int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5313 u64 time_seq)
5314{
Chris Masond97e63b2007-02-20 16:40:44 -05005315 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005316 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005317 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005318 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005319 struct btrfs_key key;
5320 u32 nritems;
5321 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005322 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005323 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005324
5325 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005326 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005327 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005328
Chris Mason8e73f272009-04-03 10:14:18 -04005329 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5330again:
5331 level = 1;
5332 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005333 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005334 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005335
Chris Masona2135012008-06-25 16:01:30 -04005336 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005337 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005338
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005339 if (time_seq)
5340 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5341 else
5342 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005343 path->keep_locks = 0;
5344
5345 if (ret < 0)
5346 return ret;
5347
Chris Masona2135012008-06-25 16:01:30 -04005348 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005349 /*
5350 * by releasing the path above we dropped all our locks. A balance
5351 * could have added more items next to the key that used to be
5352 * at the very end of the block. So, check again here and
5353 * advance the path if there are now more items available.
5354 */
Chris Masona2135012008-06-25 16:01:30 -04005355 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005356 if (ret == 0)
5357 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005358 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005359 goto done;
5360 }
Liu Bo0b43e042014-06-09 11:04:49 +08005361 /*
5362 * So the above check misses one case:
5363 * - after releasing the path above, someone has removed the item that
5364 * used to be at the very end of the block, and balance between leafs
5365 * gets another one with bigger key.offset to replace it.
5366 *
5367 * This one should be returned as well, or we can get leaf corruption
5368 * later(esp. in __btrfs_drop_extents()).
5369 *
5370 * And a bit more explanation about this check,
5371 * with ret > 0, the key isn't found, the path points to the slot
5372 * where it should be inserted, so the path->slots[0] item must be the
5373 * bigger one.
5374 */
5375 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5376 ret = 0;
5377 goto done;
5378 }
Chris Masond97e63b2007-02-20 16:40:44 -05005379
Chris Masond3977122009-01-05 21:25:51 -05005380 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005381 if (!path->nodes[level]) {
5382 ret = 1;
5383 goto done;
5384 }
Chris Mason5f39d392007-10-15 16:14:19 -04005385
Chris Masond97e63b2007-02-20 16:40:44 -05005386 slot = path->slots[level] + 1;
5387 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005388 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005389 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005390 if (level == BTRFS_MAX_LEVEL) {
5391 ret = 1;
5392 goto done;
5393 }
Chris Masond97e63b2007-02-20 16:40:44 -05005394 continue;
5395 }
Chris Mason5f39d392007-10-15 16:14:19 -04005396
Chris Mason925baed2008-06-25 16:01:30 -04005397 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005398 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005399 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005400 }
Chris Mason5f39d392007-10-15 16:14:19 -04005401
Chris Mason8e73f272009-04-03 10:14:18 -04005402 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005403 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005404 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005405 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005406 if (ret == -EAGAIN)
5407 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005408
Chris Mason76a05b32009-05-14 13:24:30 -04005409 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005410 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005411 goto done;
5412 }
5413
Chris Mason5cd57b22008-06-25 16:01:30 -04005414 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005415 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005416 if (!ret && time_seq) {
5417 /*
5418 * If we don't get the lock, we may be racing
5419 * with push_leaf_left, holding that lock while
5420 * itself waiting for the leaf we've currently
5421 * locked. To solve this situation, we give up
5422 * on our lock and cycle.
5423 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005424 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005425 btrfs_release_path(path);
5426 cond_resched();
5427 goto again;
5428 }
Chris Mason8e73f272009-04-03 10:14:18 -04005429 if (!ret) {
5430 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005431 btrfs_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005432 }
Chris Mason31533fb2011-07-26 16:01:59 -04005433 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005434 }
Chris Masond97e63b2007-02-20 16:40:44 -05005435 break;
5436 }
5437 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005438 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005439 level--;
5440 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005441 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005442 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005443
Chris Mason5f39d392007-10-15 16:14:19 -04005444 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005445 path->nodes[level] = next;
5446 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005447 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005448 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005449 if (!level)
5450 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005451
Liu Bod07b8522017-01-30 12:23:42 -08005452 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005453 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005454 if (ret == -EAGAIN)
5455 goto again;
5456
Chris Mason76a05b32009-05-14 13:24:30 -04005457 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005458 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005459 goto done;
5460 }
5461
Chris Mason5cd57b22008-06-25 16:01:30 -04005462 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005463 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005464 if (!ret) {
5465 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005466 btrfs_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005467 }
Chris Mason31533fb2011-07-26 16:01:59 -04005468 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005469 }
Chris Masond97e63b2007-02-20 16:40:44 -05005470 }
Chris Mason8e73f272009-04-03 10:14:18 -04005471 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005472done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005473 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005474 path->leave_spinning = old_spinning;
5475 if (!old_spinning)
5476 btrfs_set_path_blocking(path);
5477
5478 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005479}
Chris Mason0b86a832008-03-24 15:01:56 -04005480
Chris Mason3f157a22008-06-25 16:01:31 -04005481/*
5482 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5483 * searching until it gets past min_objectid or finds an item of 'type'
5484 *
5485 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5486 */
Chris Mason0b86a832008-03-24 15:01:56 -04005487int btrfs_previous_item(struct btrfs_root *root,
5488 struct btrfs_path *path, u64 min_objectid,
5489 int type)
5490{
5491 struct btrfs_key found_key;
5492 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005493 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005494 int ret;
5495
Chris Masond3977122009-01-05 21:25:51 -05005496 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005497 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005498 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005499 ret = btrfs_prev_leaf(root, path);
5500 if (ret != 0)
5501 return ret;
5502 } else {
5503 path->slots[0]--;
5504 }
5505 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005506 nritems = btrfs_header_nritems(leaf);
5507 if (nritems == 0)
5508 return 1;
5509 if (path->slots[0] == nritems)
5510 path->slots[0]--;
5511
Chris Mason0b86a832008-03-24 15:01:56 -04005512 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005513 if (found_key.objectid < min_objectid)
5514 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005515 if (found_key.type == type)
5516 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005517 if (found_key.objectid == min_objectid &&
5518 found_key.type < type)
5519 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005520 }
5521 return 1;
5522}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005523
5524/*
5525 * search in extent tree to find a previous Metadata/Data extent item with
5526 * min objecitd.
5527 *
5528 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5529 */
5530int btrfs_previous_extent_item(struct btrfs_root *root,
5531 struct btrfs_path *path, u64 min_objectid)
5532{
5533 struct btrfs_key found_key;
5534 struct extent_buffer *leaf;
5535 u32 nritems;
5536 int ret;
5537
5538 while (1) {
5539 if (path->slots[0] == 0) {
5540 btrfs_set_path_blocking(path);
5541 ret = btrfs_prev_leaf(root, path);
5542 if (ret != 0)
5543 return ret;
5544 } else {
5545 path->slots[0]--;
5546 }
5547 leaf = path->nodes[0];
5548 nritems = btrfs_header_nritems(leaf);
5549 if (nritems == 0)
5550 return 1;
5551 if (path->slots[0] == nritems)
5552 path->slots[0]--;
5553
5554 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5555 if (found_key.objectid < min_objectid)
5556 break;
5557 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5558 found_key.type == BTRFS_METADATA_ITEM_KEY)
5559 return 0;
5560 if (found_key.objectid == min_objectid &&
5561 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5562 break;
5563 }
5564 return 1;
5565}