blob: a1c987a26827b30922dd3a36fccf92e8baf212c4 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
David Sterbaadf02122017-05-31 19:44:31 +020022#include <linux/mm.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050023#include "ctree.h"
24#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040025#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040026#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050028
Chris Masone089f052007-03-16 16:20:31 -040029static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_path *path, int level);
Omar Sandoval310712b2017-01-17 23:24:37 -080031static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
32 const struct btrfs_key *ins_key, struct btrfs_path *path,
33 int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040035 struct btrfs_fs_info *fs_info,
36 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040037 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040038static int balance_node_right(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040039 struct btrfs_fs_info *fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -040040 struct extent_buffer *dst_buf,
41 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000042static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
43 int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050044
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090047 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040048}
49
Chris Masonb4ce94d2009-02-04 09:25:08 -050050/*
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
53 */
54noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55{
56 int i;
57 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040058 if (!p->nodes[i] || !p->locks[i])
59 continue;
60 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
61 if (p->locks[i] == BTRFS_READ_LOCK)
62 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
63 else if (p->locks[i] == BTRFS_WRITE_LOCK)
64 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050065 }
66}
67
68/*
69 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050070 *
71 * held is used to keep lockdep happy, when lockdep is enabled
72 * we set held to a blocking lock before we go around and
73 * retake all the spinlocks in the path. You can safely use NULL
74 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050075 */
Chris Mason4008c042009-02-12 14:09:45 -050076noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040077 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050078{
79 int i;
Chris Mason4008c042009-02-12 14:09:45 -050080
Chris Masonbd681512011-07-16 15:23:14 -040081 if (held) {
82 btrfs_set_lock_blocking_rw(held, held_rw);
83 if (held_rw == BTRFS_WRITE_LOCK)
84 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
85 else if (held_rw == BTRFS_READ_LOCK)
86 held_rw = BTRFS_READ_LOCK_BLOCKING;
87 }
Chris Mason4008c042009-02-12 14:09:45 -050088 btrfs_set_path_blocking(p);
Chris Mason4008c042009-02-12 14:09:45 -050089
90 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040091 if (p->nodes[i] && p->locks[i]) {
92 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
93 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
94 p->locks[i] = BTRFS_WRITE_LOCK;
95 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
96 p->locks[i] = BTRFS_READ_LOCK;
97 }
Chris Masonb4ce94d2009-02-04 09:25:08 -050098 }
Chris Mason4008c042009-02-12 14:09:45 -050099
Chris Mason4008c042009-02-12 14:09:45 -0500100 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400101 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500102}
103
Chris Masond352ac62008-09-29 15:18:18 -0400104/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400105void btrfs_free_path(struct btrfs_path *p)
106{
Jesper Juhlff175d52010-12-25 21:22:30 +0000107 if (!p)
108 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200109 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400110 kmem_cache_free(btrfs_path_cachep, p);
111}
112
Chris Masond352ac62008-09-29 15:18:18 -0400113/*
114 * path release drops references on the extent buffers in the path
115 * and it drops any locks held by this path
116 *
117 * It is safe to call this on paths that no locks or extent buffers held.
118 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200119noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500120{
121 int i;
Chris Masona2135012008-06-25 16:01:30 -0400122
Chris Mason234b63a2007-03-13 10:46:10 -0400123 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400124 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500125 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400126 continue;
127 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400128 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400129 p->locks[i] = 0;
130 }
Chris Mason5f39d392007-10-15 16:14:19 -0400131 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400132 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500133 }
134}
135
Chris Masond352ac62008-09-29 15:18:18 -0400136/*
137 * safely gets a reference on the root node of a tree. A lock
138 * is not taken, so a concurrent writer may put a different node
139 * at the root of the tree. See btrfs_lock_root_node for the
140 * looping required.
141 *
142 * The extent buffer returned by this has a reference taken, so
143 * it won't disappear. It may stop being the root of the tree
144 * at any time because there are no locks held.
145 */
Chris Mason925baed2008-06-25 16:01:30 -0400146struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
147{
148 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400149
Josef Bacik3083ee22012-03-09 16:01:49 -0500150 while (1) {
151 rcu_read_lock();
152 eb = rcu_dereference(root->node);
153
154 /*
155 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400156 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500157 * the inc_not_zero dance and if it doesn't work then
158 * synchronize_rcu and try again.
159 */
160 if (atomic_inc_not_zero(&eb->refs)) {
161 rcu_read_unlock();
162 break;
163 }
164 rcu_read_unlock();
165 synchronize_rcu();
166 }
Chris Mason925baed2008-06-25 16:01:30 -0400167 return eb;
168}
169
Chris Masond352ac62008-09-29 15:18:18 -0400170/* loop around taking references on and locking the root node of the
171 * tree until you end up with a lock on the root. A locked buffer
172 * is returned, with a reference held.
173 */
Chris Mason925baed2008-06-25 16:01:30 -0400174struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
175{
176 struct extent_buffer *eb;
177
Chris Masond3977122009-01-05 21:25:51 -0500178 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400179 eb = btrfs_root_node(root);
180 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400181 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400182 break;
Chris Mason925baed2008-06-25 16:01:30 -0400183 btrfs_tree_unlock(eb);
184 free_extent_buffer(eb);
185 }
186 return eb;
187}
188
Chris Masonbd681512011-07-16 15:23:14 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400193struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400194{
195 struct extent_buffer *eb;
196
197 while (1) {
198 eb = btrfs_root_node(root);
199 btrfs_tree_read_lock(eb);
200 if (eb == root->node)
201 break;
202 btrfs_tree_read_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masond352ac62008-09-29 15:18:18 -0400208/* cowonly root (everything not a reference counted cow subvolume), just get
209 * put onto a simple dirty list. transaction.c walks this to make sure they
210 * get properly updated on disk.
211 */
Chris Mason0b86a832008-03-24 15:01:56 -0400212static void add_root_to_dirty_list(struct btrfs_root *root)
213{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400214 struct btrfs_fs_info *fs_info = root->fs_info;
215
Josef Bacike7070be2014-12-16 08:54:43 -0800216 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
217 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
218 return;
219
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400220 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800221 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
222 /* Want the extent tree to be the last on the list */
223 if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
224 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400225 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800226 else
227 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400228 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400229 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400231}
232
Chris Masond352ac62008-09-29 15:18:18 -0400233/*
234 * used by snapshot creation to make a copy of a root for a tree with
235 * a given objectid. The buffer with the new root node is returned in
236 * cow_ret, and this func returns zero on success or a negative error code.
237 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500238int btrfs_copy_root(struct btrfs_trans_handle *trans,
239 struct btrfs_root *root,
240 struct extent_buffer *buf,
241 struct extent_buffer **cow_ret, u64 new_root_objectid)
242{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400243 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 int ret = 0;
246 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400247 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500248
Miao Xie27cdeb72014-04-02 19:51:05 +0800249 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400250 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +0800251 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
252 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500253
254 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 if (level == 0)
256 btrfs_item_key(buf, &disk_key, 0);
257 else
258 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400259
David Sterba4d75f8a2014-06-15 01:54:12 +0200260 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
261 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400262 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 return PTR_ERR(cow);
264
David Sterba58e80122016-11-08 18:30:31 +0100265 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500266 btrfs_set_header_bytenr(cow, cow->start);
267 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400268 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
269 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
270 BTRFS_HEADER_FLAG_RELOC);
271 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
272 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
273 else
274 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500275
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400276 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -0500277
Chris Masonbe20aa92007-12-17 20:14:01 -0500278 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400279 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700280 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400281 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700282 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500283
Chris Masonbe20aa92007-12-17 20:14:01 -0500284 if (ret)
285 return ret;
286
287 btrfs_mark_buffer_dirty(cow);
288 *cow_ret = cow;
289 return 0;
290}
291
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200292enum mod_log_op {
293 MOD_LOG_KEY_REPLACE,
294 MOD_LOG_KEY_ADD,
295 MOD_LOG_KEY_REMOVE,
296 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
297 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
298 MOD_LOG_MOVE_KEYS,
299 MOD_LOG_ROOT_REPLACE,
300};
301
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200302struct tree_mod_root {
303 u64 logical;
304 u8 level;
305};
306
307struct tree_mod_elem {
308 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530309 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200310 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200311 enum mod_log_op op;
312
313 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
314 int slot;
315
316 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
317 u64 generation;
318
319 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
320 struct btrfs_disk_key key;
321 u64 blockptr;
322
323 /* this is used for op == MOD_LOG_MOVE_KEYS */
David Sterbab6dfa352018-03-05 15:31:18 +0100324 struct {
325 int dst_slot;
326 int nr_items;
327 } move;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200328
329 /* this is used for op == MOD_LOG_ROOT_REPLACE */
330 struct tree_mod_root old_root;
331};
332
Jan Schmidt097b8a72012-06-21 11:08:04 +0200333/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700334 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000335 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700336static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000337{
338 return atomic64_inc_return(&fs_info->tree_mod_seq);
339}
340
341/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200342 * This adds a new blocker to the tree mod log's blocker list if the @elem
343 * passed does not already have a sequence number set. So when a caller expects
344 * to record tree modifications, it should ensure to set elem->seq to zero
345 * before calling btrfs_get_tree_mod_seq.
346 * Returns a fresh, unused tree log modification sequence number, even if no new
347 * blocker was added.
348 */
349u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
350 struct seq_list *elem)
351{
David Sterbab1a09f12018-03-05 15:43:41 +0100352 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200353 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200354 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700355 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200356 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
357 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200358 spin_unlock(&fs_info->tree_mod_seq_lock);
David Sterbab1a09f12018-03-05 15:43:41 +0100359 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200360
Josef Bacikfcebe452014-05-13 17:30:47 -0700361 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200362}
363
364void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
365 struct seq_list *elem)
366{
367 struct rb_root *tm_root;
368 struct rb_node *node;
369 struct rb_node *next;
370 struct seq_list *cur_elem;
371 struct tree_mod_elem *tm;
372 u64 min_seq = (u64)-1;
373 u64 seq_putting = elem->seq;
374
375 if (!seq_putting)
376 return;
377
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 spin_lock(&fs_info->tree_mod_seq_lock);
379 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200380 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200381
382 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200383 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200384 if (seq_putting > cur_elem->seq) {
385 /*
386 * blocker with lower sequence number exists, we
387 * cannot remove anything from the log
388 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200389 spin_unlock(&fs_info->tree_mod_seq_lock);
390 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200391 }
392 min_seq = cur_elem->seq;
393 }
394 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200395 spin_unlock(&fs_info->tree_mod_seq_lock);
396
397 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200398 * anything that's lower than the lowest existing (read: blocked)
399 * sequence number can be removed from the tree.
400 */
David Sterbab1a09f12018-03-05 15:43:41 +0100401 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200402 tm_root = &fs_info->tree_mod_log;
403 for (node = rb_first(tm_root); node; node = next) {
404 next = rb_next(node);
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800405 tm = rb_entry(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200406 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200407 continue;
408 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409 kfree(tm);
410 }
David Sterbab1a09f12018-03-05 15:43:41 +0100411 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200412}
413
414/*
415 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530416 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200417 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530418 * The 'start address' is the logical address of the *new* root node
419 * for root replace operations, or the logical address of the affected
420 * block for all other operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000421 *
David Sterbab1a09f12018-03-05 15:43:41 +0100422 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200423 */
424static noinline int
425__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
426{
427 struct rb_root *tm_root;
428 struct rb_node **new;
429 struct rb_node *parent = NULL;
430 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200431
Josef Bacikfcebe452014-05-13 17:30:47 -0700432 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200433
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200434 tm_root = &fs_info->tree_mod_log;
435 new = &tm_root->rb_node;
436 while (*new) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800437 cur = rb_entry(*new, struct tree_mod_elem, node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200438 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530439 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200440 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530441 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200443 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200444 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200445 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200446 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000447 else
448 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200449 }
450
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000453 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200454}
455
Jan Schmidt097b8a72012-06-21 11:08:04 +0200456/*
457 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
458 * returns zero with the tree_mod_log_lock acquired. The caller must hold
459 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbab1a09f12018-03-05 15:43:41 +0100460 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200461 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200462static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
463 struct extent_buffer *eb) {
464 smp_mb();
465 if (list_empty(&(fs_info)->tree_mod_seq_list))
466 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200467 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200468 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000469
David Sterbab1a09f12018-03-05 15:43:41 +0100470 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000471 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbab1a09f12018-03-05 15:43:41 +0100472 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000473 return 1;
474 }
475
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200476 return 0;
477}
478
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000479/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
480static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
481 struct extent_buffer *eb)
482{
483 smp_mb();
484 if (list_empty(&(fs_info)->tree_mod_seq_list))
485 return 0;
486 if (eb && btrfs_header_level(eb) == 0)
487 return 0;
488
489 return 1;
490}
491
492static struct tree_mod_elem *
493alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
494 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200495{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200496 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200497
Josef Bacikc8cc6342013-07-01 16:18:19 -0400498 tm = kzalloc(sizeof(*tm), flags);
499 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000500 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200501
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530502 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200503 if (op != MOD_LOG_KEY_ADD) {
504 btrfs_node_key(eb, &tm->key, slot);
505 tm->blockptr = btrfs_node_blockptr(eb, slot);
506 }
507 tm->op = op;
508 tm->slot = slot;
509 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000510 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200511
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000512 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200513}
514
David Sterbae09c2ef2018-03-05 15:09:03 +0100515static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
516 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200517{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000518 struct tree_mod_elem *tm;
519 int ret;
520
David Sterbae09c2ef2018-03-05 15:09:03 +0100521 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200522 return 0;
523
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000524 tm = alloc_tree_mod_elem(eb, slot, op, flags);
525 if (!tm)
526 return -ENOMEM;
527
David Sterbae09c2ef2018-03-05 15:09:03 +0100528 if (tree_mod_dont_log(eb->fs_info, eb)) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000529 kfree(tm);
530 return 0;
531 }
532
David Sterbae09c2ef2018-03-05 15:09:03 +0100533 ret = __tree_mod_log_insert(eb->fs_info, tm);
David Sterbab1a09f12018-03-05 15:43:41 +0100534 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000535 if (ret)
536 kfree(tm);
537
538 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200539}
540
David Sterba6074d452018-03-05 15:03:52 +0100541static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
542 int dst_slot, int src_slot, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200543{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000544 struct tree_mod_elem *tm = NULL;
545 struct tree_mod_elem **tm_list = NULL;
546 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200547 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000548 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200549
David Sterba6074d452018-03-05 15:03:52 +0100550 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200551 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200552
David Sterba176ef8f2017-03-28 14:35:01 +0200553 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000554 if (!tm_list)
555 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556
David Sterba176ef8f2017-03-28 14:35:01 +0200557 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000558 if (!tm) {
559 ret = -ENOMEM;
560 goto free_tms;
561 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200562
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530563 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200564 tm->slot = src_slot;
565 tm->move.dst_slot = dst_slot;
566 tm->move.nr_items = nr_items;
567 tm->op = MOD_LOG_MOVE_KEYS;
568
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000569 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
570 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
David Sterba176ef8f2017-03-28 14:35:01 +0200571 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000572 if (!tm_list[i]) {
573 ret = -ENOMEM;
574 goto free_tms;
575 }
576 }
577
David Sterba6074d452018-03-05 15:03:52 +0100578 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000579 goto free_tms;
580 locked = 1;
581
582 /*
583 * When we override something during the move, we log these removals.
584 * This can only happen when we move towards the beginning of the
585 * buffer, i.e. dst_slot < src_slot.
586 */
587 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
David Sterba6074d452018-03-05 15:03:52 +0100588 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000589 if (ret)
590 goto free_tms;
591 }
592
David Sterba6074d452018-03-05 15:03:52 +0100593 ret = __tree_mod_log_insert(eb->fs_info, tm);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000594 if (ret)
595 goto free_tms;
David Sterbab1a09f12018-03-05 15:43:41 +0100596 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000597 kfree(tm_list);
598
599 return 0;
600free_tms:
601 for (i = 0; i < nr_items; i++) {
602 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
David Sterba6074d452018-03-05 15:03:52 +0100603 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000604 kfree(tm_list[i]);
605 }
606 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100607 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000608 kfree(tm_list);
609 kfree(tm);
610
611 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200612}
613
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000614static inline int
615__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
616 struct tree_mod_elem **tm_list,
617 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200618{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000619 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200620 int ret;
621
Jan Schmidt097b8a72012-06-21 11:08:04 +0200622 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000623 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
624 if (ret) {
625 for (j = nritems - 1; j > i; j--)
626 rb_erase(&tm_list[j]->node,
627 &fs_info->tree_mod_log);
628 return ret;
629 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200630 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000631
632 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200633}
634
David Sterba95b757c2018-03-05 15:22:30 +0100635static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
636 struct extent_buffer *new_root, int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200637{
David Sterba95b757c2018-03-05 15:22:30 +0100638 struct btrfs_fs_info *fs_info = old_root->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000639 struct tree_mod_elem *tm = NULL;
640 struct tree_mod_elem **tm_list = NULL;
641 int nritems = 0;
642 int ret = 0;
643 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200644
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000645 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200646 return 0;
647
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000648 if (log_removal && btrfs_header_level(old_root) > 0) {
649 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100650 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
David Sterbabcc8e072017-03-28 14:35:42 +0200651 GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000652 if (!tm_list) {
653 ret = -ENOMEM;
654 goto free_tms;
655 }
656 for (i = 0; i < nritems; i++) {
657 tm_list[i] = alloc_tree_mod_elem(old_root, i,
David Sterbabcc8e072017-03-28 14:35:42 +0200658 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000659 if (!tm_list[i]) {
660 ret = -ENOMEM;
661 goto free_tms;
662 }
663 }
664 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000665
David Sterbabcc8e072017-03-28 14:35:42 +0200666 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000667 if (!tm) {
668 ret = -ENOMEM;
669 goto free_tms;
670 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200671
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530672 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200673 tm->old_root.logical = old_root->start;
674 tm->old_root.level = btrfs_header_level(old_root);
675 tm->generation = btrfs_header_generation(old_root);
676 tm->op = MOD_LOG_ROOT_REPLACE;
677
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000678 if (tree_mod_dont_log(fs_info, NULL))
679 goto free_tms;
680
681 if (tm_list)
682 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
683 if (!ret)
684 ret = __tree_mod_log_insert(fs_info, tm);
685
David Sterbab1a09f12018-03-05 15:43:41 +0100686 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000687 if (ret)
688 goto free_tms;
689 kfree(tm_list);
690
691 return ret;
692
693free_tms:
694 if (tm_list) {
695 for (i = 0; i < nritems; i++)
696 kfree(tm_list[i]);
697 kfree(tm_list);
698 }
699 kfree(tm);
700
701 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200702}
703
704static struct tree_mod_elem *
705__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
706 int smallest)
707{
708 struct rb_root *tm_root;
709 struct rb_node *node;
710 struct tree_mod_elem *cur = NULL;
711 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200712
David Sterbab1a09f12018-03-05 15:43:41 +0100713 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200714 tm_root = &fs_info->tree_mod_log;
715 node = tm_root->rb_node;
716 while (node) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800717 cur = rb_entry(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530718 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200719 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530720 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200721 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200722 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200723 node = node->rb_left;
724 } else if (!smallest) {
725 /* we want the node with the highest seq */
726 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200727 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200728 found = cur;
729 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200730 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200731 /* we want the node with the smallest seq */
732 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200733 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200734 found = cur;
735 node = node->rb_right;
736 } else {
737 found = cur;
738 break;
739 }
740 }
David Sterbab1a09f12018-03-05 15:43:41 +0100741 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200742
743 return found;
744}
745
746/*
747 * this returns the element from the log with the smallest time sequence
748 * value that's in the log (the oldest log item). any element with a time
749 * sequence lower than min_seq will be ignored.
750 */
751static struct tree_mod_elem *
752tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
753 u64 min_seq)
754{
755 return __tree_mod_log_search(fs_info, start, min_seq, 1);
756}
757
758/*
759 * this returns the element from the log with the largest time sequence
760 * value that's in the log (the most recent log item). any element with
761 * a time sequence lower than min_seq will be ignored.
762 */
763static struct tree_mod_elem *
764tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
765{
766 return __tree_mod_log_search(fs_info, start, min_seq, 0);
767}
768
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000769static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200770tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
771 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000772 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200773{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000774 int ret = 0;
775 struct tree_mod_elem **tm_list = NULL;
776 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200777 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000778 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200779
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000780 if (!tree_mod_need_log(fs_info, NULL))
781 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200782
Josef Bacikc8cc6342013-07-01 16:18:19 -0400783 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000784 return 0;
785
David Sterba31e818f2015-02-20 18:00:26 +0100786 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000787 GFP_NOFS);
788 if (!tm_list)
789 return -ENOMEM;
790
791 tm_list_add = tm_list;
792 tm_list_rem = tm_list + nr_items;
793 for (i = 0; i < nr_items; i++) {
794 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
795 MOD_LOG_KEY_REMOVE, GFP_NOFS);
796 if (!tm_list_rem[i]) {
797 ret = -ENOMEM;
798 goto free_tms;
799 }
800
801 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
802 MOD_LOG_KEY_ADD, GFP_NOFS);
803 if (!tm_list_add[i]) {
804 ret = -ENOMEM;
805 goto free_tms;
806 }
807 }
808
809 if (tree_mod_dont_log(fs_info, NULL))
810 goto free_tms;
811 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200812
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200813 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000814 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
815 if (ret)
816 goto free_tms;
817 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
818 if (ret)
819 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200820 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000821
David Sterbab1a09f12018-03-05 15:43:41 +0100822 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000823 kfree(tm_list);
824
825 return 0;
826
827free_tms:
828 for (i = 0; i < nr_items * 2; i++) {
829 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
830 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
831 kfree(tm_list[i]);
832 }
833 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100834 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000835 kfree(tm_list);
836
837 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200838}
839
David Sterbaa446a972018-03-05 15:26:29 +0100840static inline void tree_mod_log_eb_move(struct extent_buffer *dst,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200841 int dst_offset, int src_offset, int nr_items)
842{
843 int ret;
David Sterba6074d452018-03-05 15:03:52 +0100844 ret = tree_mod_log_insert_move(dst, dst_offset, src_offset, nr_items);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200845 BUG_ON(ret < 0);
846}
847
David Sterba3ac6de12018-03-05 15:00:37 +0100848static noinline void tree_mod_log_set_node_key(struct extent_buffer *eb,
849 int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200850{
851 int ret;
852
David Sterbae09c2ef2018-03-05 15:09:03 +0100853 ret = tree_mod_log_insert_key(eb, slot, MOD_LOG_KEY_REPLACE,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400854 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200855 BUG_ON(ret < 0);
856}
857
David Sterbadb7279a2018-03-05 15:14:25 +0100858static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200859{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000860 struct tree_mod_elem **tm_list = NULL;
861 int nritems = 0;
862 int i;
863 int ret = 0;
864
865 if (btrfs_header_level(eb) == 0)
866 return 0;
867
David Sterbadb7279a2018-03-05 15:14:25 +0100868 if (!tree_mod_need_log(eb->fs_info, NULL))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000869 return 0;
870
871 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100872 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000873 if (!tm_list)
874 return -ENOMEM;
875
876 for (i = 0; i < nritems; i++) {
877 tm_list[i] = alloc_tree_mod_elem(eb, i,
878 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
879 if (!tm_list[i]) {
880 ret = -ENOMEM;
881 goto free_tms;
882 }
883 }
884
David Sterbadb7279a2018-03-05 15:14:25 +0100885 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000886 goto free_tms;
887
David Sterbadb7279a2018-03-05 15:14:25 +0100888 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
David Sterbab1a09f12018-03-05 15:43:41 +0100889 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000890 if (ret)
891 goto free_tms;
892 kfree(tm_list);
893
894 return 0;
895
896free_tms:
897 for (i = 0; i < nritems; i++)
898 kfree(tm_list[i]);
899 kfree(tm_list);
900
901 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200902}
903
Jan Schmidt097b8a72012-06-21 11:08:04 +0200904static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200905tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000906 struct extent_buffer *new_root_node,
907 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200908{
909 int ret;
David Sterba95b757c2018-03-05 15:22:30 +0100910 ret = tree_mod_log_insert_root(root->node, new_root_node, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200911 BUG_ON(ret < 0);
912}
913
Chris Masond352ac62008-09-29 15:18:18 -0400914/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400915 * check if the tree block can be shared by multiple trees
916 */
917int btrfs_block_can_be_shared(struct btrfs_root *root,
918 struct extent_buffer *buf)
919{
920 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400921 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400922 * are never shared. If a block was allocated after the last
923 * snapshot and the block was not allocated by tree relocation,
924 * we know the block is not shared.
925 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800926 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400927 buf != root->node && buf != root->commit_root &&
928 (btrfs_header_generation(buf) <=
929 btrfs_root_last_snapshot(&root->root_item) ||
930 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
931 return 1;
932#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800933 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400934 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
935 return 1;
936#endif
937 return 0;
938}
939
940static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
941 struct btrfs_root *root,
942 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400943 struct extent_buffer *cow,
944 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400946 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400947 u64 refs;
948 u64 owner;
949 u64 flags;
950 u64 new_flags = 0;
951 int ret;
952
953 /*
954 * Backrefs update rules:
955 *
956 * Always use full backrefs for extent pointers in tree block
957 * allocated by tree relocation.
958 *
959 * If a shared tree block is no longer referenced by its owner
960 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
961 * use full backrefs for extent pointers in tree block.
962 *
963 * If a tree block is been relocating
964 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
965 * use full backrefs for extent pointers in tree block.
966 * The reason for this is some operations (such as drop tree)
967 * are only allowed for blocks use full backrefs.
968 */
969
970 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400971 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500972 btrfs_header_level(buf), 1,
973 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700974 if (ret)
975 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700976 if (refs == 0) {
977 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400978 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700979 return ret;
980 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 } else {
982 refs = 1;
983 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
984 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
985 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
986 else
987 flags = 0;
988 }
989
990 owner = btrfs_header_owner(buf);
991 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
992 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
993
994 if (refs > 1) {
995 if ((owner == root->root_key.objectid ||
996 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
997 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700998 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500999 if (ret)
1000 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001
1002 if (root->root_key.objectid ==
1003 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001004 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001005 if (ret)
1006 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -07001007 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001008 if (ret)
1009 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 }
1011 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1012 } else {
1013
1014 if (root->root_key.objectid ==
1015 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001016 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001018 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001019 if (ret)
1020 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001021 }
1022 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001023 int level = btrfs_header_level(buf);
1024
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001025 ret = btrfs_set_disk_extent_flags(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026 buf->start,
1027 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001028 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001029 if (ret)
1030 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 }
1032 } else {
1033 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1034 if (root->root_key.objectid ==
1035 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001036 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001038 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001039 if (ret)
1040 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -07001041 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001042 if (ret)
1043 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 }
David Sterba7c302b42017-02-10 18:47:57 +01001045 clean_tree_block(fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001046 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001047 }
1048 return 0;
1049}
1050
1051/*
Chris Masond3977122009-01-05 21:25:51 -05001052 * does the dirty work in cow of a single block. The parent block (if
1053 * supplied) is updated to point to the new cow copy. The new buffer is marked
1054 * dirty and returned locked. If you modify the block it needs to be marked
1055 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001056 *
1057 * search_start -- an allocation hint for the new block
1058 *
Chris Masond3977122009-01-05 21:25:51 -05001059 * empty_size -- a hint that you plan on doing more cow. This is the size in
1060 * bytes the allocator should try to find free next to the block it returns.
1061 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001062 */
Chris Masond3977122009-01-05 21:25:51 -05001063static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001064 struct btrfs_root *root,
1065 struct extent_buffer *buf,
1066 struct extent_buffer *parent, int parent_slot,
1067 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001068 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001069{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001070 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001071 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001072 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001073 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001074 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001075 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001076 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001077
Chris Mason925baed2008-06-25 16:01:30 -04001078 if (*cow_ret == buf)
1079 unlock_orig = 1;
1080
Chris Masonb9447ef2009-03-09 11:45:38 -04001081 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001082
Miao Xie27cdeb72014-04-02 19:51:05 +08001083 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001084 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +08001085 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1086 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001087
Chris Mason7bb86312007-12-11 09:25:06 -05001088 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001089
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 if (level == 0)
1091 btrfs_item_key(buf, &disk_key, 0);
1092 else
1093 btrfs_node_key(buf, &disk_key, 0);
1094
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001095 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1096 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001097
David Sterba4d75f8a2014-06-15 01:54:12 +02001098 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1099 root->root_key.objectid, &disk_key, level,
1100 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001101 if (IS_ERR(cow))
1102 return PTR_ERR(cow);
1103
Chris Masonb4ce94d2009-02-04 09:25:08 -05001104 /* cow is set to blocking by btrfs_init_new_buffer */
1105
David Sterba58e80122016-11-08 18:30:31 +01001106 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001107 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001108 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001109 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1110 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1111 BTRFS_HEADER_FLAG_RELOC);
1112 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1113 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1114 else
1115 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001116
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001117 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05001118
Mark Fashehbe1a5562011-08-08 13:20:18 -07001119 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001120 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001121 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001122 return ret;
1123 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001124
Miao Xie27cdeb72014-04-02 19:51:05 +08001125 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001126 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001127 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001128 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001129 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001130 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001131 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001132
Chris Mason6702ed42007-08-07 16:15:09 -04001133 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001134 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001135 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1136 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1137 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001138
Chris Mason5f39d392007-10-15 16:14:19 -04001139 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001140 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001141 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001142
Yan, Zhengf0486c62010-05-16 10:46:25 -04001143 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001144 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001145 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001146 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001147 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001148 WARN_ON(trans->transid != btrfs_header_generation(parent));
David Sterbae09c2ef2018-03-05 15:09:03 +01001149 tree_mod_log_insert_key(parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001150 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001151 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001152 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001153 btrfs_set_node_ptr_generation(parent, parent_slot,
1154 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001155 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001156 if (last_ref) {
David Sterbadb7279a2018-03-05 15:14:25 +01001157 ret = tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001158 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001159 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001160 return ret;
1161 }
1162 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001163 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001164 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001165 }
Chris Mason925baed2008-06-25 16:01:30 -04001166 if (unlock_orig)
1167 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001168 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001169 btrfs_mark_buffer_dirty(cow);
1170 *cow_ret = cow;
1171 return 0;
1172}
1173
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001174/*
1175 * returns the logical address of the oldest predecessor of the given root.
1176 * entries older than time_seq are ignored.
1177 */
David Sterbabcd24da2018-03-05 15:33:18 +01001178static struct tree_mod_elem *__tree_mod_log_oldest_root(
1179 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001180{
1181 struct tree_mod_elem *tm;
1182 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001183 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001184 int looped = 0;
1185
1186 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001187 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001188
1189 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301190 * the very last operation that's logged for a root is the
1191 * replacement operation (if it is replaced at all). this has
1192 * the logical address of the *new* root, making it the very
1193 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001194 */
1195 while (1) {
David Sterbabcd24da2018-03-05 15:33:18 +01001196 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001197 time_seq);
1198 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001199 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001200 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001201 * if there are no tree operation for the oldest root, we simply
1202 * return it. this should only happen if that (old) root is at
1203 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001204 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001205 if (!tm)
1206 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001207
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001208 /*
1209 * if there's an operation that's not a root replacement, we
1210 * found the oldest version of our root. normally, we'll find a
1211 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1212 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001213 if (tm->op != MOD_LOG_ROOT_REPLACE)
1214 break;
1215
1216 found = tm;
1217 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001218 looped = 1;
1219 }
1220
Jan Schmidta95236d2012-06-05 16:41:24 +02001221 /* if there's no old root to return, return what we found instead */
1222 if (!found)
1223 found = tm;
1224
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001225 return found;
1226}
1227
1228/*
1229 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001230 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001231 * time_seq).
1232 */
1233static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001234__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1235 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001236{
1237 u32 n;
1238 struct rb_node *next;
1239 struct tree_mod_elem *tm = first_tm;
1240 unsigned long o_dst;
1241 unsigned long o_src;
1242 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1243
1244 n = btrfs_header_nritems(eb);
David Sterbab1a09f12018-03-05 15:43:41 +01001245 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001246 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001247 /*
1248 * all the operations are recorded with the operator used for
1249 * the modification. as we're going backwards, we do the
1250 * opposite of each operation here.
1251 */
1252 switch (tm->op) {
1253 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1254 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001255 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001256 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001257 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001258 btrfs_set_node_key(eb, &tm->key, tm->slot);
1259 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1260 btrfs_set_node_ptr_generation(eb, tm->slot,
1261 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001262 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001263 break;
1264 case MOD_LOG_KEY_REPLACE:
1265 BUG_ON(tm->slot >= n);
1266 btrfs_set_node_key(eb, &tm->key, tm->slot);
1267 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1268 btrfs_set_node_ptr_generation(eb, tm->slot,
1269 tm->generation);
1270 break;
1271 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001272 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001273 n--;
1274 break;
1275 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001276 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1277 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1278 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001279 tm->move.nr_items * p_size);
1280 break;
1281 case MOD_LOG_ROOT_REPLACE:
1282 /*
1283 * this operation is special. for roots, this must be
1284 * handled explicitly before rewinding.
1285 * for non-roots, this operation may exist if the node
1286 * was a root: root A -> child B; then A gets empty and
1287 * B is promoted to the new root. in the mod log, we'll
1288 * have a root-replace operation for B, a tree block
1289 * that is no root. we simply ignore that operation.
1290 */
1291 break;
1292 }
1293 next = rb_next(&tm->node);
1294 if (!next)
1295 break;
Geliang Tang6b4df8b2016-12-19 22:53:41 +08001296 tm = rb_entry(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301297 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001298 break;
1299 }
David Sterbab1a09f12018-03-05 15:43:41 +01001300 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001301 btrfs_set_header_nritems(eb, n);
1302}
1303
Jan Schmidt47fb0912013-04-13 13:19:55 +00001304/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001305 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001306 * is returned. If rewind operations happen, a fresh buffer is returned. The
1307 * returned buffer is always read-locked. If the returned buffer is not the
1308 * input buffer, the lock on the input buffer is released and the input buffer
1309 * is freed (its refcount is decremented).
1310 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001311static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001312tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1313 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001314{
1315 struct extent_buffer *eb_rewin;
1316 struct tree_mod_elem *tm;
1317
1318 if (!time_seq)
1319 return eb;
1320
1321 if (btrfs_header_level(eb) == 0)
1322 return eb;
1323
1324 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1325 if (!tm)
1326 return eb;
1327
Josef Bacik9ec72672013-08-07 16:57:23 -04001328 btrfs_set_path_blocking(path);
1329 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1330
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001331 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1332 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001333 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001334 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001335 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001336 free_extent_buffer(eb);
1337 return NULL;
1338 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001339 btrfs_set_header_bytenr(eb_rewin, eb->start);
1340 btrfs_set_header_backref_rev(eb_rewin,
1341 btrfs_header_backref_rev(eb));
1342 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001343 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001344 } else {
1345 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001346 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001347 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001348 free_extent_buffer(eb);
1349 return NULL;
1350 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001351 }
1352
Josef Bacik9ec72672013-08-07 16:57:23 -04001353 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1354 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001355 free_extent_buffer(eb);
1356
Jan Schmidt47fb0912013-04-13 13:19:55 +00001357 extent_buffer_get(eb_rewin);
1358 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001359 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001360 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001361 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001362
1363 return eb_rewin;
1364}
1365
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001366/*
1367 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1368 * value. If there are no changes, the current root->root_node is returned. If
1369 * anything changed in between, there's a fresh buffer allocated on which the
1370 * rewind operations are done. In any case, the returned buffer is read locked.
1371 * Returns NULL on error (with no locks held).
1372 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001373static inline struct extent_buffer *
1374get_old_root(struct btrfs_root *root, u64 time_seq)
1375{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001376 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001377 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001378 struct extent_buffer *eb = NULL;
1379 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001380 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001381 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001382 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001383 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001384
Jan Schmidt30b04632013-04-13 13:19:54 +00001385 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001386 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001387 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001388 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389
Jan Schmidta95236d2012-06-05 16:41:24 +02001390 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1391 old_root = &tm->old_root;
1392 old_generation = tm->generation;
1393 logical = old_root->logical;
1394 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001395 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001396 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001397
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001398 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001399 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001400 btrfs_tree_read_unlock(eb_root);
1401 free_extent_buffer(eb_root);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001402 old = read_tree_block(fs_info, logical, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001403 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1404 if (!IS_ERR(old))
1405 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001406 btrfs_warn(fs_info,
1407 "failed to read tree block %llu from get_old_root",
1408 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001409 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001410 eb = btrfs_clone_extent_buffer(old);
1411 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001412 }
1413 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001414 btrfs_tree_read_unlock(eb_root);
1415 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001416 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001417 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001418 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001419 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001420 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001421 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001422 }
1423
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001424 if (!eb)
1425 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001426 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001427 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001428 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001429 btrfs_set_header_bytenr(eb, eb->start);
1430 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001431 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001432 btrfs_set_header_level(eb, old_root->level);
1433 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001434 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001435 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001436 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001437 else
1438 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001439 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001440
1441 return eb;
1442}
1443
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001444int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1445{
1446 struct tree_mod_elem *tm;
1447 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001448 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001449
David Sterbabcd24da2018-03-05 15:33:18 +01001450 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001451 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1452 level = tm->old_root.level;
1453 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001454 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001455 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001456 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001457
1458 return level;
1459}
1460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001461static inline int should_cow_block(struct btrfs_trans_handle *trans,
1462 struct btrfs_root *root,
1463 struct extent_buffer *buf)
1464{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001465 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001466 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001467
Liu Bof1ebcc72011-11-14 20:48:06 -05001468 /* ensure we can see the force_cow */
1469 smp_rmb();
1470
1471 /*
1472 * We do not need to cow a block if
1473 * 1) this block is not created or changed in this transaction;
1474 * 2) this block does not belong to TREE_RELOC tree;
1475 * 3) the root is not forced COW.
1476 *
1477 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001478 * when we create snapshot during committing the transaction,
Liu Bof1ebcc72011-11-14 20:48:06 -05001479 * after we've finished coping src root, we must COW the shared
1480 * block to ensure the metadata consistency.
1481 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001482 if (btrfs_header_generation(buf) == trans->transid &&
1483 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1484 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001485 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001486 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001487 return 0;
1488 return 1;
1489}
1490
Chris Masond352ac62008-09-29 15:18:18 -04001491/*
1492 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001493 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001494 * once per transaction, as long as it hasn't been written yet
1495 */
Chris Masond3977122009-01-05 21:25:51 -05001496noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001497 struct btrfs_root *root, struct extent_buffer *buf,
1498 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001499 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001500{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001501 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001502 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001503 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001504
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001505 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001506 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001507 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001508 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001509
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001510 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001511 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001512 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001513
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001515 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001516 *cow_ret = buf;
1517 return 0;
1518 }
Chris Masonc4876852009-02-04 09:24:25 -05001519
Byongho Leeee221842015-12-15 01:42:10 +09001520 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001521
1522 if (parent)
1523 btrfs_set_lock_blocking(parent);
1524 btrfs_set_lock_blocking(buf);
1525
Chris Masonf510cfe2007-10-15 16:14:48 -04001526 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001527 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001528
1529 trace_btrfs_cow_block(root, buf, *cow_ret);
1530
Chris Masonf510cfe2007-10-15 16:14:48 -04001531 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001532}
1533
Chris Masond352ac62008-09-29 15:18:18 -04001534/*
1535 * helper function for defrag to decide if two blocks pointed to by a
1536 * node are actually close by
1537 */
Chris Mason6b800532007-10-15 16:17:34 -04001538static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001539{
Chris Mason6b800532007-10-15 16:17:34 -04001540 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001541 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001542 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001543 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001544 return 0;
1545}
1546
Chris Mason081e9572007-11-06 10:26:24 -05001547/*
1548 * compare two keys in a memcmp fashion
1549 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001550static int comp_keys(const struct btrfs_disk_key *disk,
1551 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001552{
1553 struct btrfs_key k1;
1554
1555 btrfs_disk_key_to_cpu(&k1, disk);
1556
Diego Calleja20736ab2009-07-24 11:06:52 -04001557 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001558}
1559
Josef Bacikf3465ca2008-11-12 14:19:50 -05001560/*
1561 * same as comp_keys only with two btrfs_key's
1562 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001563int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001564{
1565 if (k1->objectid > k2->objectid)
1566 return 1;
1567 if (k1->objectid < k2->objectid)
1568 return -1;
1569 if (k1->type > k2->type)
1570 return 1;
1571 if (k1->type < k2->type)
1572 return -1;
1573 if (k1->offset > k2->offset)
1574 return 1;
1575 if (k1->offset < k2->offset)
1576 return -1;
1577 return 0;
1578}
Chris Mason081e9572007-11-06 10:26:24 -05001579
Chris Masond352ac62008-09-29 15:18:18 -04001580/*
1581 * this is used by the defrag code to go through all the
1582 * leaves pointed to by a node and reallocate them so that
1583 * disk order is close to key order
1584 */
Chris Mason6702ed42007-08-07 16:15:09 -04001585int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001586 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001587 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001588 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001589{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001590 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001591 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001592 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001593 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001594 u64 search_start = *last_ret;
1595 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001596 u64 other;
1597 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001598 int end_slot;
1599 int i;
1600 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001601 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001602 int uptodate;
1603 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001604 int progress_passed = 0;
1605 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001606
Chris Mason5708b952007-10-25 15:43:18 -04001607 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001608
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 WARN_ON(trans->transaction != fs_info->running_transaction);
1610 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001611
Chris Mason6b800532007-10-15 16:17:34 -04001612 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001613 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001614 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001615
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001616 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001617 return 0;
1618
Chris Masonb4ce94d2009-02-04 09:25:08 -05001619 btrfs_set_lock_blocking(parent);
1620
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001621 for (i = start_slot; i <= end_slot; i++) {
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);
Chris Masone9d0b132007-08-10 14:06:19 -04001631 if (last_block == 0)
1632 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001633
Chris Mason6702ed42007-08-07 16:15:09 -04001634 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001635 other = btrfs_node_blockptr(parent, i - 1);
1636 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001637 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001638 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001639 other = btrfs_node_blockptr(parent, i + 1);
1640 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001641 }
Chris Masone9d0b132007-08-10 14:06:19 -04001642 if (close) {
1643 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001644 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001645 }
Chris Mason6702ed42007-08-07 16:15:09 -04001646
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001647 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001648 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001649 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001650 else
1651 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001652 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001653 if (!cur) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001654 cur = read_tree_block(fs_info, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001655 if (IS_ERR(cur)) {
1656 return PTR_ERR(cur);
1657 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001658 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001659 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001660 }
Chris Mason6b800532007-10-15 16:17:34 -04001661 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001662 err = btrfs_read_buffer(cur, gen);
1663 if (err) {
1664 free_extent_buffer(cur);
1665 return err;
1666 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001667 }
Chris Mason6702ed42007-08-07 16:15:09 -04001668 }
Chris Masone9d0b132007-08-10 14:06:19 -04001669 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001670 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001671
Chris Masone7a84562008-06-25 16:01:31 -04001672 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001673 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001674 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001675 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001676 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001677 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001678 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001679 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001680 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001681 break;
Yan252c38f2007-08-29 09:11:44 -04001682 }
Chris Masone7a84562008-06-25 16:01:31 -04001683 search_start = cur->start;
1684 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001685 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001686 btrfs_tree_unlock(cur);
1687 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001688 }
1689 return err;
1690}
1691
Chris Mason74123bd2007-02-02 11:05:29 -05001692/*
Chris Mason5f39d392007-10-15 16:14:19 -04001693 * search for key in the extent_buffer. The items start at offset p,
1694 * and they are item_size apart. There are 'max' items in p.
1695 *
Chris Mason74123bd2007-02-02 11:05:29 -05001696 * the slot in the array is returned via slot, and it points to
1697 * the place where you would insert key if it is not found in
1698 * the array.
1699 *
1700 * slot may point to max if the key is bigger than all of the keys
1701 */
Chris Masone02119d2008-09-05 16:13:11 -04001702static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001703 unsigned long p, int item_size,
1704 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001705 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001706{
1707 int low = 0;
1708 int high = max;
1709 int mid;
1710 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001711 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001712 struct btrfs_disk_key unaligned;
1713 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001714 char *kaddr = NULL;
1715 unsigned long map_start = 0;
1716 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001717 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001718
Liu Bo5e24e9a2016-06-23 16:32:45 -07001719 if (low > high) {
1720 btrfs_err(eb->fs_info,
1721 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1722 __func__, low, high, eb->start,
1723 btrfs_header_owner(eb), btrfs_header_level(eb));
1724 return -EINVAL;
1725 }
1726
Chris Masond3977122009-01-05 21:25:51 -05001727 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001728 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001729 offset = p + mid * item_size;
1730
Chris Masona6591712011-07-19 12:04:14 -04001731 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001732 (offset + sizeof(struct btrfs_disk_key)) >
1733 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001734
1735 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001736 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001737 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001738
Chris Mason479965d2007-10-15 16:14:27 -04001739 if (!err) {
1740 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1741 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001742 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001743 read_extent_buffer(eb, &unaligned,
1744 offset, sizeof(unaligned));
1745 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001746 } else {
1747 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001748 }
1749
Chris Mason5f39d392007-10-15 16:14:19 -04001750 } else {
1751 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1752 map_start);
1753 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001754 ret = comp_keys(tmp, key);
1755
1756 if (ret < 0)
1757 low = mid + 1;
1758 else if (ret > 0)
1759 high = mid;
1760 else {
1761 *slot = mid;
1762 return 0;
1763 }
1764 }
1765 *slot = low;
1766 return 1;
1767}
1768
Chris Mason97571fd2007-02-24 13:39:08 -05001769/*
1770 * simple bin_search frontend that does the right thing for
1771 * leaves vs nodes
1772 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001773int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1774 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001775{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001776 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001777 return generic_bin_search(eb,
1778 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001779 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001780 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001781 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001782 else
Chris Mason5f39d392007-10-15 16:14:19 -04001783 return generic_bin_search(eb,
1784 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001785 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001786 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001787 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001788}
1789
Yan, Zhengf0486c62010-05-16 10:46:25 -04001790static void root_add_used(struct btrfs_root *root, u32 size)
1791{
1792 spin_lock(&root->accounting_lock);
1793 btrfs_set_root_used(&root->root_item,
1794 btrfs_root_used(&root->root_item) + size);
1795 spin_unlock(&root->accounting_lock);
1796}
1797
1798static void root_sub_used(struct btrfs_root *root, u32 size)
1799{
1800 spin_lock(&root->accounting_lock);
1801 btrfs_set_root_used(&root->root_item,
1802 btrfs_root_used(&root->root_item) - size);
1803 spin_unlock(&root->accounting_lock);
1804}
1805
Chris Masond352ac62008-09-29 15:18:18 -04001806/* given a node and slot number, this reads the blocks it points to. The
1807 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001808 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001809static noinline struct extent_buffer *
1810read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1811 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001812{
Chris Masonca7a79a2008-05-12 12:59:19 -04001813 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001814 struct extent_buffer *eb;
1815
Liu Bofb770ae2016-07-05 12:10:14 -07001816 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1817 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001818
1819 BUG_ON(level == 0);
1820
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001821 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001822 btrfs_node_ptr_generation(parent, slot));
Liu Bofb770ae2016-07-05 12:10:14 -07001823 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1824 free_extent_buffer(eb);
1825 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001826 }
1827
1828 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001829}
1830
Chris Masond352ac62008-09-29 15:18:18 -04001831/*
1832 * node level balancing, used to make sure nodes are in proper order for
1833 * item deletion. We balance from the top down, so we have to make sure
1834 * that a deletion won't leave an node completely empty later on.
1835 */
Chris Masone02119d2008-09-05 16:13:11 -04001836static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001837 struct btrfs_root *root,
1838 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001839{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001840 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001841 struct extent_buffer *right = NULL;
1842 struct extent_buffer *mid;
1843 struct extent_buffer *left = NULL;
1844 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001845 int ret = 0;
1846 int wret;
1847 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001848 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001849 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001850
1851 if (level == 0)
1852 return 0;
1853
Chris Mason5f39d392007-10-15 16:14:19 -04001854 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001855
Chris Masonbd681512011-07-16 15:23:14 -04001856 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1857 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001858 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1859
Chris Mason1d4f8a02007-03-13 09:28:32 -04001860 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001861
Li Zefana05a9bb2011-09-06 16:55:34 +08001862 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001863 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001864 pslot = path->slots[level + 1];
1865 }
Chris Masonbb803952007-03-01 12:04:21 -05001866
Chris Mason40689472007-03-17 14:29:23 -04001867 /*
1868 * deal with the case where there is only one pointer in the root
1869 * by promoting the node below to a root
1870 */
Chris Mason5f39d392007-10-15 16:14:19 -04001871 if (!parent) {
1872 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001873
Chris Mason5f39d392007-10-15 16:14:19 -04001874 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001875 return 0;
1876
1877 /* promote the child to a root */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001878 child = read_node_slot(fs_info, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001879 if (IS_ERR(child)) {
1880 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001882 goto enospc;
1883 }
1884
Chris Mason925baed2008-06-25 16:01:30 -04001885 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001886 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001887 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001888 if (ret) {
1889 btrfs_tree_unlock(child);
1890 free_extent_buffer(child);
1891 goto enospc;
1892 }
Yan2f375ab2008-02-01 14:58:07 -05001893
Jan Schmidt90f8d622013-04-13 13:19:53 +00001894 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001895 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001896
Chris Mason0b86a832008-03-24 15:01:56 -04001897 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001898 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001899
Chris Mason925baed2008-06-25 16:01:30 -04001900 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001901 path->nodes[level] = NULL;
David Sterba7c302b42017-02-10 18:47:57 +01001902 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001903 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001904 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001905 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001906
1907 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001908 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001909 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001910 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001911 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001912 }
Chris Mason5f39d392007-10-15 16:14:19 -04001913 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001914 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001915 return 0;
1916
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001917 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001918 if (IS_ERR(left))
1919 left = NULL;
1920
Chris Mason5f39d392007-10-15 16:14:19 -04001921 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001922 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001923 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001924 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001925 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001926 if (wret) {
1927 ret = wret;
1928 goto enospc;
1929 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001930 }
Liu Bofb770ae2016-07-05 12:10:14 -07001931
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001932 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001933 if (IS_ERR(right))
1934 right = NULL;
1935
Chris Mason5f39d392007-10-15 16:14:19 -04001936 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001937 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001938 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001939 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001940 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001941 if (wret) {
1942 ret = wret;
1943 goto enospc;
1944 }
1945 }
1946
1947 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001948 if (left) {
1949 orig_slot += btrfs_header_nritems(left);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001950 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001951 if (wret < 0)
1952 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001953 }
Chris Mason79f95c82007-03-01 15:16:26 -05001954
1955 /*
1956 * then try to empty the right most buffer into the middle
1957 */
Chris Mason5f39d392007-10-15 16:14:19 -04001958 if (right) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001959 wret = push_node_left(trans, fs_info, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001960 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001961 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001962 if (btrfs_header_nritems(right) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001963 clean_tree_block(fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04001964 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001965 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001966 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001967 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001968 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001969 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001970 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001971 struct btrfs_disk_key right_key;
1972 btrfs_node_key(right, &right_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01001973 tree_mod_log_set_node_key(parent, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001974 btrfs_set_node_key(parent, &right_key, pslot + 1);
1975 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001976 }
1977 }
Chris Mason5f39d392007-10-15 16:14:19 -04001978 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001979 /*
1980 * we're not allowed to leave a node with one item in the
1981 * tree during a delete. A deletion from lower in the tree
1982 * could try to delete the only pointer in this node.
1983 * So, pull some keys from the left.
1984 * There has to be a left pointer at this point because
1985 * otherwise we would have pulled some pointers from the
1986 * right
1987 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001988 if (!left) {
1989 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001990 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001991 goto enospc;
1992 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001993 wret = balance_node_right(trans, fs_info, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001994 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001995 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001996 goto enospc;
1997 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001998 if (wret == 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001999 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04002000 if (wret < 0)
2001 ret = wret;
2002 }
Chris Mason79f95c82007-03-01 15:16:26 -05002003 BUG_ON(wret == 1);
2004 }
Chris Mason5f39d392007-10-15 16:14:19 -04002005 if (btrfs_header_nritems(mid) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01002006 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002007 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002008 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002009 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002010 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002011 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002012 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002013 } else {
2014 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002015 struct btrfs_disk_key mid_key;
2016 btrfs_node_key(mid, &mid_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01002017 tree_mod_log_set_node_key(parent, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002018 btrfs_set_node_key(parent, &mid_key, pslot);
2019 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002020 }
Chris Masonbb803952007-03-01 12:04:21 -05002021
Chris Mason79f95c82007-03-01 15:16:26 -05002022 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002023 if (left) {
2024 if (btrfs_header_nritems(left) > orig_slot) {
2025 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002026 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002027 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002028 path->slots[level + 1] -= 1;
2029 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002030 if (mid) {
2031 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002032 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002033 }
Chris Masonbb803952007-03-01 12:04:21 -05002034 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002035 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002036 path->slots[level] = orig_slot;
2037 }
2038 }
Chris Mason79f95c82007-03-01 15:16:26 -05002039 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002040 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002041 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002042 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002043enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002044 if (right) {
2045 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002046 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002047 }
2048 if (left) {
2049 if (path->nodes[level] != left)
2050 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002052 }
Chris Masonbb803952007-03-01 12:04:21 -05002053 return ret;
2054}
2055
Chris Masond352ac62008-09-29 15:18:18 -04002056/* Node balancing for insertion. Here we only split or push nodes around
2057 * when they are completely full. This is also done top down, so we
2058 * have to be pessimistic.
2059 */
Chris Masond3977122009-01-05 21:25:51 -05002060static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002061 struct btrfs_root *root,
2062 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002063{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002064 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002065 struct extent_buffer *right = NULL;
2066 struct extent_buffer *mid;
2067 struct extent_buffer *left = NULL;
2068 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002069 int ret = 0;
2070 int wret;
2071 int pslot;
2072 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002073
2074 if (level == 0)
2075 return 1;
2076
Chris Mason5f39d392007-10-15 16:14:19 -04002077 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002078 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002079
Li Zefana05a9bb2011-09-06 16:55:34 +08002080 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002081 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002082 pslot = path->slots[level + 1];
2083 }
Chris Masone66f7092007-04-20 13:16:02 -04002084
Chris Mason5f39d392007-10-15 16:14:19 -04002085 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002086 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002087
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002088 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002089 if (IS_ERR(left))
2090 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002091
2092 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002093 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002094 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002095
2096 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002097 btrfs_set_lock_blocking(left);
2098
Chris Mason5f39d392007-10-15 16:14:19 -04002099 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002100 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002101 wret = 1;
2102 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002103 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002104 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002105 if (ret)
2106 wret = 1;
2107 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002108 wret = push_node_left(trans, fs_info,
Chris Mason971a1f62008-04-24 10:54:32 -04002109 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002110 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002111 }
Chris Masone66f7092007-04-20 13:16:02 -04002112 if (wret < 0)
2113 ret = wret;
2114 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002115 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002116 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002117 btrfs_node_key(mid, &disk_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01002118 tree_mod_log_set_node_key(parent, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002119 btrfs_set_node_key(parent, &disk_key, pslot);
2120 btrfs_mark_buffer_dirty(parent);
2121 if (btrfs_header_nritems(left) > orig_slot) {
2122 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002123 path->slots[level + 1] -= 1;
2124 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002125 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002126 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002127 } else {
2128 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002129 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002130 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002131 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002132 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002133 }
Chris Masone66f7092007-04-20 13:16:02 -04002134 return 0;
2135 }
Chris Mason925baed2008-06-25 16:01:30 -04002136 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002138 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002139 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002140 if (IS_ERR(right))
2141 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002142
2143 /*
2144 * then try to empty the right most buffer into the middle
2145 */
Chris Mason5f39d392007-10-15 16:14:19 -04002146 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002147 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002148
Chris Mason925baed2008-06-25 16:01:30 -04002149 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002150 btrfs_set_lock_blocking(right);
2151
Chris Mason5f39d392007-10-15 16:14:19 -04002152 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002153 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002154 wret = 1;
2155 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002156 ret = btrfs_cow_block(trans, root, right,
2157 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002158 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002159 if (ret)
2160 wret = 1;
2161 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002162 wret = balance_node_right(trans, fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04002163 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002164 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002165 }
Chris Masone66f7092007-04-20 13:16:02 -04002166 if (wret < 0)
2167 ret = wret;
2168 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002169 struct btrfs_disk_key disk_key;
2170
2171 btrfs_node_key(right, &disk_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01002172 tree_mod_log_set_node_key(parent, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002173 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2174 btrfs_mark_buffer_dirty(parent);
2175
2176 if (btrfs_header_nritems(mid) <= orig_slot) {
2177 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002178 path->slots[level + 1] += 1;
2179 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002180 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002181 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002183 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002184 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002186 }
Chris Masone66f7092007-04-20 13:16:02 -04002187 return 0;
2188 }
Chris Mason925baed2008-06-25 16:01:30 -04002189 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002190 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002191 }
Chris Masone66f7092007-04-20 13:16:02 -04002192 return 1;
2193}
2194
Chris Mason74123bd2007-02-02 11:05:29 -05002195/*
Chris Masond352ac62008-09-29 15:18:18 -04002196 * readahead one full node of leaves, finding things that are close
2197 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002198 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002199static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002200 struct btrfs_path *path,
2201 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002202{
Chris Mason5f39d392007-10-15 16:14:19 -04002203 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002204 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002205 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002206 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002207 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002208 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002209 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002210 u32 nr;
2211 u32 blocksize;
2212 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002213
Chris Masona6b6e752007-10-15 16:22:39 -04002214 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002215 return;
2216
Chris Mason6702ed42007-08-07 16:15:09 -04002217 if (!path->nodes[level])
2218 return;
2219
Chris Mason5f39d392007-10-15 16:14:19 -04002220 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002221
Chris Mason3c69fae2007-08-07 15:52:22 -04002222 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002223 blocksize = fs_info->nodesize;
2224 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002225 if (eb) {
2226 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002227 return;
2228 }
2229
Chris Masona7175312009-01-22 09:23:10 -05002230 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002233 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002234
Chris Masond3977122009-01-05 21:25:51 -05002235 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002236 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002237 if (nr == 0)
2238 break;
2239 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002240 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002241 nr++;
2242 if (nr >= nritems)
2243 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002244 }
David Sterbae4058b52015-11-27 16:31:35 +01002245 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002246 btrfs_node_key(node, &disk_key, nr);
2247 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2248 break;
2249 }
Chris Mason6b800532007-10-15 16:17:34 -04002250 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002251 if ((search <= target && target - search <= 65536) ||
2252 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002253 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002254 nread += blocksize;
2255 }
2256 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002257 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002258 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002259 }
2260}
Chris Mason925baed2008-06-25 16:01:30 -04002261
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002262static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002263 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002264{
2265 int slot;
2266 int nritems;
2267 struct extent_buffer *parent;
2268 struct extent_buffer *eb;
2269 u64 gen;
2270 u64 block1 = 0;
2271 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002272
Chris Mason8c594ea2009-04-20 15:50:10 -04002273 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002274 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002275 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002276
2277 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002278 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002279
2280 if (slot > 0) {
2281 block1 = btrfs_node_blockptr(parent, slot - 1);
2282 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002283 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002284 /*
2285 * if we get -eagain from btrfs_buffer_uptodate, we
2286 * don't want to return eagain here. That will loop
2287 * forever
2288 */
2289 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002290 block1 = 0;
2291 free_extent_buffer(eb);
2292 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002293 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002294 block2 = btrfs_node_blockptr(parent, slot + 1);
2295 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002296 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002297 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002298 block2 = 0;
2299 free_extent_buffer(eb);
2300 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002301
Josef Bacik0b088512013-06-17 14:23:02 -04002302 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002303 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002304 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002305 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002306}
2307
2308
2309/*
Chris Masond3977122009-01-05 21:25:51 -05002310 * when we walk down the tree, it is usually safe to unlock the higher layers
2311 * in the tree. The exceptions are when our path goes through slot 0, because
2312 * operations on the tree might require changing key pointers higher up in the
2313 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002314 *
Chris Masond3977122009-01-05 21:25:51 -05002315 * callers might also have set path->keep_locks, which tells this code to keep
2316 * the lock if the path points to the last slot in the block. This is part of
2317 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002318 *
Chris Masond3977122009-01-05 21:25:51 -05002319 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2320 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002321 */
Chris Masone02119d2008-09-05 16:13:11 -04002322static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002323 int lowest_unlock, int min_write_lock_level,
2324 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002325{
2326 int i;
2327 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002328 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002329 struct extent_buffer *t;
2330
2331 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2332 if (!path->nodes[i])
2333 break;
2334 if (!path->locks[i])
2335 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002336 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002337 skip_level = i + 1;
2338 continue;
2339 }
Chris Mason051e1b92008-06-25 16:01:30 -04002340 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002341 u32 nritems;
2342 t = path->nodes[i];
2343 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002344 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002345 skip_level = i + 1;
2346 continue;
2347 }
2348 }
Chris Mason051e1b92008-06-25 16:01:30 -04002349 if (skip_level < i && i >= lowest_unlock)
2350 no_skips = 1;
2351
Chris Mason925baed2008-06-25 16:01:30 -04002352 t = path->nodes[i];
2353 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002354 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002355 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002356 if (write_lock_level &&
2357 i > min_write_lock_level &&
2358 i <= *write_lock_level) {
2359 *write_lock_level = i - 1;
2360 }
Chris Mason925baed2008-06-25 16:01:30 -04002361 }
2362 }
2363}
2364
Chris Mason3c69fae2007-08-07 15:52:22 -04002365/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002366 * This releases any locks held in the path starting at level and
2367 * going all the way up to the root.
2368 *
2369 * btrfs_search_slot will keep the lock held on higher nodes in a few
2370 * corner cases, such as COW of the block at slot zero in the node. This
2371 * ignores those rules, and it should only be called when there are no
2372 * more updates to be done higher up in the tree.
2373 */
2374noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2375{
2376 int i;
2377
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002378 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002379 return;
2380
2381 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2382 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002383 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002384 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002385 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002386 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002387 path->locks[i] = 0;
2388 }
2389}
2390
2391/*
Chris Masonc8c42862009-04-03 10:14:18 -04002392 * helper function for btrfs_search_slot. The goal is to find a block
2393 * in cache without setting the path to blocking. If we find the block
2394 * we return zero and the path is unchanged.
2395 *
2396 * If we can't find the block, we set the path blocking and do some
2397 * reada. -EAGAIN is returned and the search must be repeated.
2398 */
2399static int
Liu Bod07b8522017-01-30 12:23:42 -08002400read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2401 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002402 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002403{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002404 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002405 u64 blocknr;
2406 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002407 struct extent_buffer *b = *eb_ret;
2408 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002409 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002410
2411 blocknr = btrfs_node_blockptr(b, slot);
2412 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002413
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002414 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002415 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002416 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002417 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2418 *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 */
2431 ret = btrfs_read_buffer(tmp, gen);
2432 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
Chris Masoncb449212010-10-24 11:01:27 -04002451 free_extent_buffer(tmp);
David Sterbae4058b52015-11-27 16:31:35 +01002452 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002453 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002454
David Sterbab3b4aa72011-04-21 01:20:15 +02002455 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002456
2457 ret = -EAGAIN;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002458 tmp = read_tree_block(fs_info, blocknr, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08002459 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002460 /*
2461 * If the read above didn't mark this buffer up to date,
2462 * it will never end up being up to date. Set ret to EIO now
2463 * and give up so that our caller doesn't loop forever
2464 * on our EAGAINs.
2465 */
Chris Masonb9fab912012-05-06 07:23:47 -04002466 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002467 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002468 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002469 } else {
2470 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002471 }
2472 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 Masonbd681512011-07-16 15:23:14 -04002506 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002507
2508 BUG_ON(sret > 0);
2509 if (sret) {
2510 ret = sret;
2511 goto done;
2512 }
2513 b = p->nodes[level];
2514 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002515 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002516 int sret;
2517
Chris Masonbd681512011-07-16 15:23:14 -04002518 if (*write_lock_level < level + 1) {
2519 *write_lock_level = level + 1;
2520 btrfs_release_path(p);
2521 goto again;
2522 }
2523
Chris Masonc8c42862009-04-03 10:14:18 -04002524 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002525 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002526 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002527 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002528
2529 if (sret) {
2530 ret = sret;
2531 goto done;
2532 }
2533 b = p->nodes[level];
2534 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002535 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002536 goto again;
2537 }
2538 BUG_ON(btrfs_header_nritems(b) == 1);
2539 }
2540 return 0;
2541
2542again:
2543 ret = -EAGAIN;
2544done:
2545 return ret;
2546}
2547
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002548static void key_search_validate(struct extent_buffer *b,
Omar Sandoval310712b2017-01-17 23:24:37 -08002549 const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002550 int level)
2551{
2552#ifdef CONFIG_BTRFS_ASSERT
2553 struct btrfs_disk_key disk_key;
2554
2555 btrfs_cpu_key_to_disk(&disk_key, key);
2556
2557 if (level == 0)
2558 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2559 offsetof(struct btrfs_leaf, items[0].key),
2560 sizeof(disk_key)));
2561 else
2562 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2563 offsetof(struct btrfs_node, ptrs[0].key),
2564 sizeof(disk_key)));
2565#endif
2566}
2567
Omar Sandoval310712b2017-01-17 23:24:37 -08002568static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002569 int level, int *prev_cmp, int *slot)
2570{
2571 if (*prev_cmp != 0) {
Nikolay Borisova74b35e2017-12-08 16:27:43 +02002572 *prev_cmp = btrfs_bin_search(b, key, level, slot);
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002573 return *prev_cmp;
2574 }
2575
2576 key_search_validate(b, key, level);
2577 *slot = 0;
2578
2579 return 0;
2580}
2581
David Sterba381cf652015-01-02 18:45:16 +01002582int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002583 u64 iobjectid, u64 ioff, u8 key_type,
2584 struct btrfs_key *found_key)
2585{
2586 int ret;
2587 struct btrfs_key key;
2588 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002589
2590 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002591 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002592
2593 key.type = key_type;
2594 key.objectid = iobjectid;
2595 key.offset = ioff;
2596
2597 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002598 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002599 return ret;
2600
2601 eb = path->nodes[0];
2602 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2603 ret = btrfs_next_leaf(fs_root, path);
2604 if (ret)
2605 return ret;
2606 eb = path->nodes[0];
2607 }
2608
2609 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2610 if (found_key->type != key.type ||
2611 found_key->objectid != key.objectid)
2612 return 1;
2613
2614 return 0;
2615}
2616
Chris Masonc8c42862009-04-03 10:14:18 -04002617/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002618 * btrfs_search_slot - look for a key in a tree and perform necessary
2619 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002620 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002621 * @trans: Handle of transaction, used when modifying the tree
2622 * @p: Holds all btree nodes along the search path
2623 * @root: The root node of the tree
2624 * @key: The key we are looking for
2625 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2626 * deletions it's -1. 0 for plain searches
2627 * @cow: boolean should CoW operations be performed. Must always be 1
2628 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002629 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002630 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2631 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2632 *
2633 * If @key is found, 0 is returned and you can find the item in the leaf level
2634 * of the path (level 0)
2635 *
2636 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2637 * points to the slot where it should be inserted
2638 *
2639 * If an error is encountered while searching the tree a negative error number
2640 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002641 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002642int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2643 const struct btrfs_key *key, struct btrfs_path *p,
2644 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002646 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002647 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648 int slot;
2649 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002650 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002651 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002652 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002653 int root_lock;
2654 /* everything at write_lock_level or lower must be write locked */
2655 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002656 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002657 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002658 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002659
Chris Mason6702ed42007-08-07 16:15:09 -04002660 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002661 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002662 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002663 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002664
Chris Masonbd681512011-07-16 15:23:14 -04002665 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002666 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002667
Chris Masonbd681512011-07-16 15:23:14 -04002668 /* when we are removing items, we might have to go up to level
2669 * two as we update tree pointers Make sure we keep write
2670 * for those levels as well
2671 */
2672 write_lock_level = 2;
2673 } else if (ins_len > 0) {
2674 /*
2675 * for inserting items, make sure we have a write lock on
2676 * level 1 so we can update keys
2677 */
2678 write_lock_level = 1;
2679 }
2680
2681 if (!cow)
2682 write_lock_level = -1;
2683
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002684 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002685 write_lock_level = BTRFS_MAX_LEVEL;
2686
Chris Masonf7c79f32012-03-19 15:54:38 -04002687 min_write_lock_level = write_lock_level;
2688
Chris Masonbb803952007-03-01 12:04:21 -05002689again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002690 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002691 /*
2692 * we try very hard to do read locks on the root
2693 */
2694 root_lock = BTRFS_READ_LOCK;
2695 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002696 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002697 /*
2698 * the commit roots are read only
2699 * so we always do read locks
2700 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002701 if (p->need_commit_sem)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002702 down_read(&fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703 b = root->commit_root;
2704 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002705 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002706 if (p->need_commit_sem)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002707 up_read(&fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002708 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002709 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002710 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002711 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002712 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002713 level = btrfs_header_level(b);
2714 } else {
2715 /* we don't know the level of the root node
2716 * until we actually have it read locked
2717 */
2718 b = btrfs_read_lock_root_node(root);
2719 level = btrfs_header_level(b);
2720 if (level <= write_lock_level) {
2721 /* whoops, must trade for write lock */
2722 btrfs_tree_read_unlock(b);
2723 free_extent_buffer(b);
2724 b = btrfs_lock_root_node(root);
2725 root_lock = BTRFS_WRITE_LOCK;
2726
2727 /* the level might have changed, check again */
2728 level = btrfs_header_level(b);
2729 }
2730 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002731 }
Chris Masonbd681512011-07-16 15:23:14 -04002732 p->nodes[level] = b;
2733 if (!p->skip_locking)
2734 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002735
Chris Masoneb60cea2007-02-02 09:18:22 -05002736 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002737 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002738
2739 /*
2740 * setup the path here so we can release it under lock
2741 * contention with the cow code
2742 */
Chris Mason02217ed2007-03-02 16:08:05 -05002743 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002744 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2745
Chris Masonc8c42862009-04-03 10:14:18 -04002746 /*
2747 * if we don't really need to cow this block
2748 * then we don't want to set the path blocking,
2749 * so we test it here
2750 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002751 if (!should_cow_block(trans, root, b)) {
2752 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002753 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002754 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002755
Chris Masonbd681512011-07-16 15:23:14 -04002756 /*
2757 * must have write locks on this node and the
2758 * parent
2759 */
Josef Bacik5124e002012-11-07 13:44:13 -05002760 if (level > write_lock_level ||
2761 (level + 1 > write_lock_level &&
2762 level + 1 < BTRFS_MAX_LEVEL &&
2763 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002764 write_lock_level = level + 1;
2765 btrfs_release_path(p);
2766 goto again;
2767 }
2768
Filipe Manana160f4082014-07-28 19:37:17 +01002769 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002770 if (last_level)
2771 err = btrfs_cow_block(trans, root, b, NULL, 0,
2772 &b);
2773 else
2774 err = btrfs_cow_block(trans, root, b,
2775 p->nodes[level + 1],
2776 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002777 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002778 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002779 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002780 }
Chris Mason02217ed2007-03-02 16:08:05 -05002781 }
Chris Mason65b51a02008-08-01 15:11:20 -04002782cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002783 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002784 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002785
2786 /*
2787 * we have a lock on b and as long as we aren't changing
2788 * the tree, there is no way to for the items in b to change.
2789 * It is safe to drop the lock on our parent before we
2790 * go through the expensive btree search on b.
2791 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002792 * If we're inserting or deleting (ins_len != 0), then we might
2793 * be changing slot zero, which may require changing the parent.
2794 * So, we can't drop the lock until after we know which slot
2795 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002796 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002797 if (!ins_len && !p->keep_locks) {
2798 int u = level + 1;
2799
2800 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2801 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2802 p->locks[u] = 0;
2803 }
2804 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002805
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002806 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002807 if (ret < 0)
2808 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002809
Chris Mason5f39d392007-10-15 16:14:19 -04002810 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002811 int dec = 0;
2812 if (ret && slot > 0) {
2813 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002814 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002815 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002816 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002817 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002818 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002819 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002820 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002821 if (err) {
2822 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002823 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002824 }
Chris Masonc8c42862009-04-03 10:14:18 -04002825 b = p->nodes[level];
2826 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002827
Chris Masonbd681512011-07-16 15:23:14 -04002828 /*
2829 * slot 0 is special, if we change the key
2830 * we have to update the parent pointer
2831 * which means we must have a write lock
2832 * on the parent
2833 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002834 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002835 write_lock_level < level + 1) {
2836 write_lock_level = level + 1;
2837 btrfs_release_path(p);
2838 goto again;
2839 }
2840
Chris Masonf7c79f32012-03-19 15:54:38 -04002841 unlock_up(p, level, lowest_unlock,
2842 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002843
Chris Mason925baed2008-06-25 16:01:30 -04002844 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002845 if (dec)
2846 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002847 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002848 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002849
Liu Bod07b8522017-01-30 12:23:42 -08002850 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01002851 slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002852 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002853 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002854 if (err) {
2855 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002856 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002857 }
Chris Mason76a05b32009-05-14 13:24:30 -04002858
Chris Masonb4ce94d2009-02-04 09:25:08 -05002859 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002860 level = btrfs_header_level(b);
2861 if (level <= write_lock_level) {
2862 err = btrfs_try_tree_write_lock(b);
2863 if (!err) {
2864 btrfs_set_path_blocking(p);
2865 btrfs_tree_lock(b);
2866 btrfs_clear_path_blocking(p, b,
2867 BTRFS_WRITE_LOCK);
2868 }
2869 p->locks[level] = BTRFS_WRITE_LOCK;
2870 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002871 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002872 if (!err) {
2873 btrfs_set_path_blocking(p);
2874 btrfs_tree_read_lock(b);
2875 btrfs_clear_path_blocking(p, b,
2876 BTRFS_READ_LOCK);
2877 }
2878 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002879 }
Chris Masonbd681512011-07-16 15:23:14 -04002880 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002881 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002882 } else {
2883 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002884 if (ins_len > 0 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002885 btrfs_leaf_free_space(fs_info, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002886 if (write_lock_level < 1) {
2887 write_lock_level = 1;
2888 btrfs_release_path(p);
2889 goto again;
2890 }
2891
Chris Masonb4ce94d2009-02-04 09:25:08 -05002892 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002893 err = split_leaf(trans, root, key,
2894 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002895 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002896
Yan Zheng33c66f42009-07-22 09:59:00 -04002897 BUG_ON(err > 0);
2898 if (err) {
2899 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002900 goto done;
2901 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002902 }
Chris Mason459931e2008-12-10 09:10:46 -05002903 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002904 unlock_up(p, level, lowest_unlock,
2905 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002906 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002907 }
2908 }
Chris Mason65b51a02008-08-01 15:11:20 -04002909 ret = 1;
2910done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002911 /*
2912 * we don't really know what they plan on doing with the path
2913 * from here on, so for now just mark it as blocking
2914 */
Chris Masonb9473432009-03-13 11:00:37 -04002915 if (!p->leave_spinning)
2916 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002917 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002918 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002919 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002920}
2921
Chris Mason74123bd2007-02-02 11:05:29 -05002922/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002923 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2924 * current state of the tree together with the operations recorded in the tree
2925 * modification log to search for the key in a previous version of this tree, as
2926 * denoted by the time_seq parameter.
2927 *
2928 * Naturally, there is no support for insert, delete or cow operations.
2929 *
2930 * The resulting path and return value will be set up as if we called
2931 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2932 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002933int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002934 struct btrfs_path *p, u64 time_seq)
2935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002936 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002937 struct extent_buffer *b;
2938 int slot;
2939 int ret;
2940 int err;
2941 int level;
2942 int lowest_unlock = 1;
2943 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002944 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002945
2946 lowest_level = p->lowest_level;
2947 WARN_ON(p->nodes[0] != NULL);
2948
2949 if (p->search_commit_root) {
2950 BUG_ON(time_seq);
2951 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2952 }
2953
2954again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002955 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002956 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002957 p->locks[level] = BTRFS_READ_LOCK;
2958
2959 while (b) {
2960 level = btrfs_header_level(b);
2961 p->nodes[level] = b;
2962 btrfs_clear_path_blocking(p, NULL, 0);
2963
2964 /*
2965 * we have a lock on b and as long as we aren't changing
2966 * the tree, there is no way to for the items in b to change.
2967 * It is safe to drop the lock on our parent before we
2968 * go through the expensive btree search on b.
2969 */
2970 btrfs_unlock_up_safe(p, level + 1);
2971
Josef Bacikd4b40872013-09-24 14:09:34 -04002972 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002973 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002974 * time.
2975 */
2976 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002977 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002978
2979 if (level != 0) {
2980 int dec = 0;
2981 if (ret && slot > 0) {
2982 dec = 1;
2983 slot -= 1;
2984 }
2985 p->slots[level] = slot;
2986 unlock_up(p, level, lowest_unlock, 0, NULL);
2987
2988 if (level == lowest_level) {
2989 if (dec)
2990 p->slots[level]++;
2991 goto done;
2992 }
2993
Liu Bod07b8522017-01-30 12:23:42 -08002994 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01002995 slot, key);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002996 if (err == -EAGAIN)
2997 goto again;
2998 if (err) {
2999 ret = err;
3000 goto done;
3001 }
3002
3003 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003004 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003005 if (!err) {
3006 btrfs_set_path_blocking(p);
3007 btrfs_tree_read_lock(b);
3008 btrfs_clear_path_blocking(p, b,
3009 BTRFS_READ_LOCK);
3010 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003011 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003012 if (!b) {
3013 ret = -ENOMEM;
3014 goto done;
3015 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003016 p->locks[level] = BTRFS_READ_LOCK;
3017 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003018 } else {
3019 p->slots[level] = slot;
3020 unlock_up(p, level, lowest_unlock, 0, NULL);
3021 goto done;
3022 }
3023 }
3024 ret = 1;
3025done:
3026 if (!p->leave_spinning)
3027 btrfs_set_path_blocking(p);
3028 if (ret < 0)
3029 btrfs_release_path(p);
3030
3031 return ret;
3032}
3033
3034/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003035 * helper to use instead of search slot if no exact match is needed but
3036 * instead the next or previous item should be returned.
3037 * When find_higher is true, the next higher item is returned, the next lower
3038 * otherwise.
3039 * When return_any and find_higher are both true, and no higher item is found,
3040 * return the next lower instead.
3041 * When return_any is true and find_higher is false, and no lower item is found,
3042 * return the next higher instead.
3043 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3044 * < 0 on error
3045 */
3046int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003047 const struct btrfs_key *key,
3048 struct btrfs_path *p, int find_higher,
3049 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003050{
3051 int ret;
3052 struct extent_buffer *leaf;
3053
3054again:
3055 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3056 if (ret <= 0)
3057 return ret;
3058 /*
3059 * a return value of 1 means the path is at the position where the
3060 * item should be inserted. Normally this is the next bigger item,
3061 * but in case the previous item is the last in a leaf, path points
3062 * to the first free slot in the previous leaf, i.e. at an invalid
3063 * item.
3064 */
3065 leaf = p->nodes[0];
3066
3067 if (find_higher) {
3068 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3069 ret = btrfs_next_leaf(root, p);
3070 if (ret <= 0)
3071 return ret;
3072 if (!return_any)
3073 return 1;
3074 /*
3075 * no higher item found, return the next
3076 * lower instead
3077 */
3078 return_any = 0;
3079 find_higher = 0;
3080 btrfs_release_path(p);
3081 goto again;
3082 }
3083 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003084 if (p->slots[0] == 0) {
3085 ret = btrfs_prev_leaf(root, p);
3086 if (ret < 0)
3087 return ret;
3088 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003089 leaf = p->nodes[0];
3090 if (p->slots[0] == btrfs_header_nritems(leaf))
3091 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003092 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003093 }
Arne Jansene6793762011-09-13 11:18:10 +02003094 if (!return_any)
3095 return 1;
3096 /*
3097 * no lower item found, return the next
3098 * higher instead
3099 */
3100 return_any = 0;
3101 find_higher = 1;
3102 btrfs_release_path(p);
3103 goto again;
3104 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003105 --p->slots[0];
3106 }
3107 }
3108 return 0;
3109}
3110
3111/*
Chris Mason74123bd2007-02-02 11:05:29 -05003112 * adjust the pointers going up the tree, starting at level
3113 * making sure the right key of each node is points to 'key'.
3114 * This is used after shifting pointers to the left, so it stops
3115 * fixing up pointers when a given leaf/node is not in slot 0 of the
3116 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003117 *
Chris Mason74123bd2007-02-02 11:05:29 -05003118 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003119static void fixup_low_keys(struct btrfs_fs_info *fs_info,
3120 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003121 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003122{
3123 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003124 struct extent_buffer *t;
3125
Chris Mason234b63a2007-03-13 10:46:10 -04003126 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003127 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003128 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003129 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003130 t = path->nodes[i];
David Sterba3ac6de12018-03-05 15:00:37 +01003131 tree_mod_log_set_node_key(t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003132 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003133 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003134 if (tslot != 0)
3135 break;
3136 }
3137}
3138
Chris Mason74123bd2007-02-02 11:05:29 -05003139/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003140 * update item key.
3141 *
3142 * This function isn't completely safe. It's the caller's responsibility
3143 * that the new key won't break the order
3144 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003145void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3146 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003147 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003148{
3149 struct btrfs_disk_key disk_key;
3150 struct extent_buffer *eb;
3151 int slot;
3152
3153 eb = path->nodes[0];
3154 slot = path->slots[0];
3155 if (slot > 0) {
3156 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003157 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003158 }
3159 if (slot < btrfs_header_nritems(eb) - 1) {
3160 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003161 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003162 }
3163
3164 btrfs_cpu_key_to_disk(&disk_key, new_key);
3165 btrfs_set_item_key(eb, &disk_key, slot);
3166 btrfs_mark_buffer_dirty(eb);
3167 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003168 fixup_low_keys(fs_info, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003169}
3170
3171/*
Chris Mason74123bd2007-02-02 11:05:29 -05003172 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003173 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003174 *
3175 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3176 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003177 */
Chris Mason98ed5172008-01-03 10:01:48 -05003178static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003179 struct btrfs_fs_info *fs_info,
3180 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003181 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003182{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003183 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003184 int src_nritems;
3185 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003186 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003187
Chris Mason5f39d392007-10-15 16:14:19 -04003188 src_nritems = btrfs_header_nritems(src);
3189 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003190 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003191 WARN_ON(btrfs_header_generation(src) != trans->transid);
3192 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003193
Chris Masonbce4eae2008-04-24 14:42:46 -04003194 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003195 return 1;
3196
Chris Masond3977122009-01-05 21:25:51 -05003197 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003198 return 1;
3199
Chris Masonbce4eae2008-04-24 14:42:46 -04003200 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003201 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003202 if (push_items < src_nritems) {
3203 /* leave at least 8 pointers in the node if
3204 * we aren't going to empty it
3205 */
3206 if (src_nritems - push_items < 8) {
3207 if (push_items <= 8)
3208 return 1;
3209 push_items -= 8;
3210 }
3211 }
3212 } else
3213 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003214
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003215 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003216 push_items);
3217 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003218 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003219 return ret;
3220 }
Chris Mason5f39d392007-10-15 16:14:19 -04003221 copy_extent_buffer(dst, src,
3222 btrfs_node_key_ptr_offset(dst_nritems),
3223 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003224 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003225
Chris Masonbb803952007-03-01 12:04:21 -05003226 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003227 /*
3228 * don't call tree_mod_log_eb_move here, key removal was already
3229 * fully logged by tree_mod_log_eb_copy above.
3230 */
Chris Mason5f39d392007-10-15 16:14:19 -04003231 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3232 btrfs_node_key_ptr_offset(push_items),
3233 (src_nritems - push_items) *
3234 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003235 }
Chris Mason5f39d392007-10-15 16:14:19 -04003236 btrfs_set_header_nritems(src, src_nritems - push_items);
3237 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3238 btrfs_mark_buffer_dirty(src);
3239 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003240
Chris Masonbb803952007-03-01 12:04:21 -05003241 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003242}
3243
Chris Mason97571fd2007-02-24 13:39:08 -05003244/*
Chris Mason79f95c82007-03-01 15:16:26 -05003245 * try to push data from one node into the next node right in the
3246 * tree.
3247 *
3248 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3249 * error, and > 0 if there was no room in the right hand block.
3250 *
3251 * this will only push up to 1/2 the contents of the left node over
3252 */
Chris Mason5f39d392007-10-15 16:14:19 -04003253static int balance_node_right(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003254 struct btrfs_fs_info *fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04003255 struct extent_buffer *dst,
3256 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003257{
Chris Mason79f95c82007-03-01 15:16:26 -05003258 int push_items = 0;
3259 int max_push;
3260 int src_nritems;
3261 int dst_nritems;
3262 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003263
Chris Mason7bb86312007-12-11 09:25:06 -05003264 WARN_ON(btrfs_header_generation(src) != trans->transid);
3265 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3266
Chris Mason5f39d392007-10-15 16:14:19 -04003267 src_nritems = btrfs_header_nritems(src);
3268 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003269 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003270 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003271 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003272
Chris Masond3977122009-01-05 21:25:51 -05003273 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003274 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003275
3276 max_push = src_nritems / 2 + 1;
3277 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003278 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003279 return 1;
Yan252c38f2007-08-29 09:11:44 -04003280
Chris Mason79f95c82007-03-01 15:16:26 -05003281 if (max_push < push_items)
3282 push_items = max_push;
3283
David Sterbaa446a972018-03-05 15:26:29 +01003284 tree_mod_log_eb_move(dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003285 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3286 btrfs_node_key_ptr_offset(0),
3287 (dst_nritems) *
3288 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003289
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003290 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003291 src_nritems - push_items, push_items);
3292 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003293 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003294 return ret;
3295 }
Chris Mason5f39d392007-10-15 16:14:19 -04003296 copy_extent_buffer(dst, src,
3297 btrfs_node_key_ptr_offset(0),
3298 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003299 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003300
Chris Mason5f39d392007-10-15 16:14:19 -04003301 btrfs_set_header_nritems(src, src_nritems - push_items);
3302 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003303
Chris Mason5f39d392007-10-15 16:14:19 -04003304 btrfs_mark_buffer_dirty(src);
3305 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003306
Chris Mason79f95c82007-03-01 15:16:26 -05003307 return ret;
3308}
3309
3310/*
Chris Mason97571fd2007-02-24 13:39:08 -05003311 * helper function to insert a new root level in the tree.
3312 * A new node is allocated, and a single item is inserted to
3313 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003314 *
3315 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003316 */
Chris Masond3977122009-01-05 21:25:51 -05003317static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003318 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003319 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003320{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003321 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003322 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003323 struct extent_buffer *lower;
3324 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003325 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003326 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003327
3328 BUG_ON(path->nodes[level]);
3329 BUG_ON(path->nodes[level-1] != root->node);
3330
Chris Mason7bb86312007-12-11 09:25:06 -05003331 lower = path->nodes[level-1];
3332 if (level == 1)
3333 btrfs_item_key(lower, &lower_key, 0);
3334 else
3335 btrfs_node_key(lower, &lower_key, 0);
3336
David Sterba4d75f8a2014-06-15 01:54:12 +02003337 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3338 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003339 if (IS_ERR(c))
3340 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003341
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003342 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003343
David Sterbab159fa22016-11-08 18:09:03 +01003344 memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003345 btrfs_set_header_nritems(c, 1);
3346 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003347 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003348 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003349 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003350 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003351
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003352 write_extent_buffer_fsid(c, fs_info->fsid);
3353 write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
Chris Masone17cade2008-04-15 15:41:47 -04003354
Chris Mason5f39d392007-10-15 16:14:19 -04003355 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003356 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003357 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003358 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003359
3360 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003361
3362 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003363
Chris Mason925baed2008-06-25 16:01:30 -04003364 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003365 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003366 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003367
3368 /* the super has an extra ref to root->node */
3369 free_extent_buffer(old);
3370
Chris Mason0b86a832008-03-24 15:01:56 -04003371 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003372 extent_buffer_get(c);
3373 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303374 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003375 path->slots[level] = 0;
3376 return 0;
3377}
3378
Chris Mason74123bd2007-02-02 11:05:29 -05003379/*
3380 * worker function to insert a single pointer in a node.
3381 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003382 *
Chris Mason74123bd2007-02-02 11:05:29 -05003383 * slot and level indicate where you want the key to go, and
3384 * blocknr is the block the key points to.
3385 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003386static void insert_ptr(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003387 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003388 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003389 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003390{
Chris Mason5f39d392007-10-15 16:14:19 -04003391 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003392 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003393 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003394
3395 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003396 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003397 lower = path->nodes[level];
3398 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003399 BUG_ON(slot > nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003400 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003401 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003402 if (level)
David Sterbaa446a972018-03-05 15:26:29 +01003403 tree_mod_log_eb_move(lower, slot + 1, slot,
3404 nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003405 memmove_extent_buffer(lower,
3406 btrfs_node_key_ptr_offset(slot + 1),
3407 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003408 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003409 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003410 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003411 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3412 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003413 BUG_ON(ret < 0);
3414 }
Chris Mason5f39d392007-10-15 16:14:19 -04003415 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003416 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003417 WARN_ON(trans->transid == 0);
3418 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003419 btrfs_set_header_nritems(lower, nritems + 1);
3420 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003421}
3422
Chris Mason97571fd2007-02-24 13:39:08 -05003423/*
3424 * split the node at the specified level in path in two.
3425 * The path is corrected to point to the appropriate node after the split
3426 *
3427 * Before splitting this tries to make some room in the node by pushing
3428 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003429 *
3430 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003431 */
Chris Masone02119d2008-09-05 16:13:11 -04003432static noinline int split_node(struct btrfs_trans_handle *trans,
3433 struct btrfs_root *root,
3434 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003435{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003436 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003437 struct extent_buffer *c;
3438 struct extent_buffer *split;
3439 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003440 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003441 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003442 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003443
Chris Mason5f39d392007-10-15 16:14:19 -04003444 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003445 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003446 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003447 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003448 * trying to split the root, lets make a new one
3449 *
Liu Bofdd99c72013-05-22 12:06:51 +00003450 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003451 * insert_new_root, because that root buffer will be kept as a
3452 * normal node. We are going to log removal of half of the
3453 * elements below with tree_mod_log_eb_copy. We're holding a
3454 * tree lock on the buffer, which is why we cannot race with
3455 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003456 */
Liu Bofdd99c72013-05-22 12:06:51 +00003457 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003458 if (ret)
3459 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003460 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003461 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003462 c = path->nodes[level];
3463 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003464 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003465 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003466 if (ret < 0)
3467 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003468 }
Chris Masone66f7092007-04-20 13:16:02 -04003469
Chris Mason5f39d392007-10-15 16:14:19 -04003470 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003471 mid = (c_nritems + 1) / 2;
3472 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003473
David Sterba4d75f8a2014-06-15 01:54:12 +02003474 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3475 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003476 if (IS_ERR(split))
3477 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003478
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003479 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003480
David Sterbab159fa22016-11-08 18:09:03 +01003481 memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003483 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003484 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003485 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003486 btrfs_set_header_owner(split, root->root_key.objectid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003487 write_extent_buffer_fsid(split, fs_info->fsid);
3488 write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
Chris Mason5f39d392007-10-15 16:14:19 -04003489
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003490 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003491 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003492 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003493 return ret;
3494 }
Chris Mason5f39d392007-10-15 16:14:19 -04003495 copy_extent_buffer(split, c,
3496 btrfs_node_key_ptr_offset(0),
3497 btrfs_node_key_ptr_offset(mid),
3498 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3499 btrfs_set_header_nritems(split, c_nritems - mid);
3500 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003501 ret = 0;
3502
Chris Mason5f39d392007-10-15 16:14:19 -04003503 btrfs_mark_buffer_dirty(c);
3504 btrfs_mark_buffer_dirty(split);
3505
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003506 insert_ptr(trans, fs_info, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003507 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003508
Chris Mason5de08d72007-02-24 06:24:44 -05003509 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003510 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003511 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003512 free_extent_buffer(c);
3513 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003514 path->slots[level + 1] += 1;
3515 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003516 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003517 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003518 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003519 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003520}
3521
Chris Mason74123bd2007-02-02 11:05:29 -05003522/*
3523 * how many bytes are required to store the items in a leaf. start
3524 * and nr indicate which items in the leaf to check. This totals up the
3525 * space used both by the item structs and the item data
3526 */
Chris Mason5f39d392007-10-15 16:14:19 -04003527static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003528{
Josef Bacik41be1f32012-10-15 13:43:18 -04003529 struct btrfs_item *start_item;
3530 struct btrfs_item *end_item;
3531 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003532 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003533 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003534 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003535
3536 if (!nr)
3537 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003538 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003539 start_item = btrfs_item_nr(start);
3540 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003541 data_len = btrfs_token_item_offset(l, start_item, &token) +
3542 btrfs_token_item_size(l, start_item, &token);
3543 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003544 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003545 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003546 return data_len;
3547}
3548
Chris Mason74123bd2007-02-02 11:05:29 -05003549/*
Chris Masond4dbff92007-04-04 14:08:15 -04003550 * The space between the end of the leaf items and
3551 * the start of the leaf data. IOW, how much room
3552 * the leaf has left for both items and data
3553 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003554noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04003555 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003556{
Chris Mason5f39d392007-10-15 16:14:19 -04003557 int nritems = btrfs_header_nritems(leaf);
3558 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559
3560 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003561 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003562 btrfs_crit(fs_info,
3563 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3564 ret,
3565 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3566 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003567 }
3568 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003569}
3570
Chris Mason99d8f832010-07-07 10:51:48 -04003571/*
3572 * min slot controls the lowest index we're willing to push to the
3573 * right. We'll push up to and including min_slot, but no lower
3574 */
David Sterba1e47eef2017-02-10 19:13:06 +01003575static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003576 struct btrfs_path *path,
3577 int data_size, int empty,
3578 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003579 int free_space, u32 left_nritems,
3580 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003581{
Chris Mason5f39d392007-10-15 16:14:19 -04003582 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003583 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003584 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003585 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003586 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003587 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003588 int push_space = 0;
3589 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003590 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003591 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003592 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003593 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003594 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003595
Chris Masoncfed81a2012-03-03 07:40:03 -05003596 btrfs_init_map_token(&token);
3597
Chris Mason34a38212007-11-07 13:31:03 -05003598 if (empty)
3599 nr = 0;
3600 else
Chris Mason99d8f832010-07-07 10:51:48 -04003601 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003602
Zheng Yan31840ae2008-09-23 13:14:14 -04003603 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003604 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003605
Chris Mason44871b12009-03-13 10:04:31 -04003606 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003607 i = left_nritems - 1;
3608 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003609 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003610
Zheng Yan31840ae2008-09-23 13:14:14 -04003611 if (!empty && push_items > 0) {
3612 if (path->slots[0] > i)
3613 break;
3614 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003615 int space = btrfs_leaf_free_space(fs_info, left);
Zheng Yan31840ae2008-09-23 13:14:14 -04003616 if (space + push_space * 2 > free_space)
3617 break;
3618 }
3619 }
3620
Chris Mason00ec4c52007-02-24 12:47:20 -05003621 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003622 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003623
Chris Masondb945352007-10-15 16:15:53 -04003624 this_item_size = btrfs_item_size(left, item);
3625 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003626 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003627
Chris Mason00ec4c52007-02-24 12:47:20 -05003628 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003629 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003630 if (i == 0)
3631 break;
3632 i--;
Chris Masondb945352007-10-15 16:15:53 -04003633 }
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Chris Mason925baed2008-06-25 16:01:30 -04003635 if (push_items == 0)
3636 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003637
Julia Lawall6c1500f2012-11-03 20:30:18 +00003638 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003639
Chris Mason00ec4c52007-02-24 12:47:20 -05003640 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003641 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003642
Chris Mason5f39d392007-10-15 16:14:19 -04003643 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003644 push_space -= leaf_data_end(fs_info, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003645
Chris Mason00ec4c52007-02-24 12:47:20 -05003646 /* make room in the right data area */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003647 data_end = leaf_data_end(fs_info, right);
Chris Mason5f39d392007-10-15 16:14:19 -04003648 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003649 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3650 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003651 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003652
Chris Mason00ec4c52007-02-24 12:47:20 -05003653 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003654 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003655 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003656 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
Chris Masond6025572007-03-30 14:27:56 -04003657 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003658
3659 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3660 btrfs_item_nr_offset(0),
3661 right_nritems * sizeof(struct btrfs_item));
3662
Chris Mason00ec4c52007-02-24 12:47:20 -05003663 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003664 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3665 btrfs_item_nr_offset(left_nritems - push_items),
3666 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003667
3668 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003669 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003670 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003671 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003672 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003673 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003674 push_space -= btrfs_token_item_size(right, item, &token);
3675 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003676 }
3677
Chris Mason7518a232007-03-12 12:01:18 -04003678 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003679 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003680
Chris Mason34a38212007-11-07 13:31:03 -05003681 if (left_nritems)
3682 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003683 else
David Sterba7c302b42017-02-10 18:47:57 +01003684 clean_tree_block(fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003685
Chris Mason5f39d392007-10-15 16:14:19 -04003686 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003687
Chris Mason5f39d392007-10-15 16:14:19 -04003688 btrfs_item_key(right, &disk_key, 0);
3689 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003690 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003691
Chris Mason00ec4c52007-02-24 12:47:20 -05003692 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003693 if (path->slots[0] >= left_nritems) {
3694 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003695 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba7c302b42017-02-10 18:47:57 +01003696 clean_tree_block(fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003697 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003698 free_extent_buffer(path->nodes[0]);
3699 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003700 path->slots[1] += 1;
3701 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003702 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003703 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003704 }
3705 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003706
3707out_unlock:
3708 btrfs_tree_unlock(right);
3709 free_extent_buffer(right);
3710 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003711}
Chris Mason925baed2008-06-25 16:01:30 -04003712
Chris Mason00ec4c52007-02-24 12:47:20 -05003713/*
Chris Mason44871b12009-03-13 10:04:31 -04003714 * push some data in the path leaf to the right, trying to free up at
3715 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3716 *
3717 * returns 1 if the push failed because the other node didn't have enough
3718 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003719 *
3720 * this will push starting from min_slot to the end of the leaf. It won't
3721 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003722 */
3723static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003724 *root, struct btrfs_path *path,
3725 int min_data_size, int data_size,
3726 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003727{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003728 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003729 struct extent_buffer *left = path->nodes[0];
3730 struct extent_buffer *right;
3731 struct extent_buffer *upper;
3732 int slot;
3733 int free_space;
3734 u32 left_nritems;
3735 int ret;
3736
3737 if (!path->nodes[1])
3738 return 1;
3739
3740 slot = path->slots[1];
3741 upper = path->nodes[1];
3742 if (slot >= btrfs_header_nritems(upper) - 1)
3743 return 1;
3744
3745 btrfs_assert_tree_locked(path->nodes[1]);
3746
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003747 right = read_node_slot(fs_info, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003748 /*
3749 * slot + 1 is not valid or we fail to read the right node,
3750 * no big deal, just return.
3751 */
3752 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003753 return 1;
3754
Chris Mason44871b12009-03-13 10:04:31 -04003755 btrfs_tree_lock(right);
3756 btrfs_set_lock_blocking(right);
3757
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003758 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003759 if (free_space < data_size)
3760 goto out_unlock;
3761
3762 /* cow and double check */
3763 ret = btrfs_cow_block(trans, root, right, upper,
3764 slot + 1, &right);
3765 if (ret)
3766 goto out_unlock;
3767
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003768 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003769 if (free_space < data_size)
3770 goto out_unlock;
3771
3772 left_nritems = btrfs_header_nritems(left);
3773 if (left_nritems == 0)
3774 goto out_unlock;
3775
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003776 if (path->slots[0] == left_nritems && !empty) {
3777 /* Key greater than all keys in the leaf, right neighbor has
3778 * enough room for it and we're not emptying our leaf to delete
3779 * it, therefore use right neighbor to insert the new item and
3780 * no need to touch/dirty our left leaft. */
3781 btrfs_tree_unlock(left);
3782 free_extent_buffer(left);
3783 path->nodes[0] = right;
3784 path->slots[0] = 0;
3785 path->slots[1]++;
3786 return 0;
3787 }
3788
David Sterba1e47eef2017-02-10 19:13:06 +01003789 return __push_leaf_right(fs_info, path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003790 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003791out_unlock:
3792 btrfs_tree_unlock(right);
3793 free_extent_buffer(right);
3794 return 1;
3795}
3796
3797/*
Chris Mason74123bd2007-02-02 11:05:29 -05003798 * push some data in the path leaf to the left, trying to free up at
3799 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003800 *
3801 * max_slot can put a limit on how far into the leaf we'll push items. The
3802 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3803 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003804 */
David Sterba66cb7dd2017-02-10 19:14:36 +01003805static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003806 struct btrfs_path *path, int data_size,
3807 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003808 int free_space, u32 right_nritems,
3809 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003810{
Chris Mason5f39d392007-10-15 16:14:19 -04003811 struct btrfs_disk_key disk_key;
3812 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003813 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814 int push_space = 0;
3815 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003816 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003817 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003818 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003819 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003820 u32 this_item_size;
3821 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003822 struct btrfs_map_token token;
3823
3824 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003825
Chris Mason34a38212007-11-07 13:31:03 -05003826 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003827 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003828 else
Chris Mason99d8f832010-07-07 10:51:48 -04003829 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003830
3831 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003832 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003833
Zheng Yan31840ae2008-09-23 13:14:14 -04003834 if (!empty && push_items > 0) {
3835 if (path->slots[0] < i)
3836 break;
3837 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003838 int space = btrfs_leaf_free_space(fs_info, right);
Zheng Yan31840ae2008-09-23 13:14:14 -04003839 if (space + push_space * 2 > free_space)
3840 break;
3841 }
3842 }
3843
Chris Masonbe0e5c02007-01-26 15:51:26 -05003844 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003845 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003846
3847 this_item_size = btrfs_item_size(right, item);
3848 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003849 break;
Chris Masondb945352007-10-15 16:15:53 -04003850
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003852 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853 }
Chris Masondb945352007-10-15 16:15:53 -04003854
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003856 ret = 1;
3857 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003858 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303859 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003860
Chris Masonbe0e5c02007-01-26 15:51:26 -05003861 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003862 copy_extent_buffer(left, right,
3863 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3864 btrfs_item_nr_offset(0),
3865 push_items * sizeof(struct btrfs_item));
3866
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003867 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003868 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003869
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003870 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003871 leaf_data_end(fs_info, left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003872 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003873 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003874 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003875 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003876 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003877
Chris Masondb945352007-10-15 16:15:53 -04003878 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003879 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003880 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003881
Ross Kirkdd3cc162013-09-16 15:58:09 +01003882 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003883
Chris Masoncfed81a2012-03-03 07:40:03 -05003884 ioff = btrfs_token_item_offset(left, item, &token);
3885 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003886 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003887 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003888 }
Chris Mason5f39d392007-10-15 16:14:19 -04003889 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003890
3891 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003892 if (push_items > right_nritems)
3893 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003894 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003895
Chris Mason34a38212007-11-07 13:31:03 -05003896 if (push_items < right_nritems) {
3897 push_space = btrfs_item_offset_nr(right, push_items - 1) -
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003898 leaf_data_end(fs_info, right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003899 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003900 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003901 BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003902 leaf_data_end(fs_info, right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003903
3904 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003905 btrfs_item_nr_offset(push_items),
3906 (btrfs_header_nritems(right) - push_items) *
3907 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003908 }
Yaneef1c492007-11-26 10:58:13 -05003909 right_nritems -= push_items;
3910 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003911 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003912 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003913 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003914
Chris Masoncfed81a2012-03-03 07:40:03 -05003915 push_space = push_space - btrfs_token_item_size(right,
3916 item, &token);
3917 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003918 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003919
Chris Mason5f39d392007-10-15 16:14:19 -04003920 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003921 if (right_nritems)
3922 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003923 else
David Sterba7c302b42017-02-10 18:47:57 +01003924 clean_tree_block(fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003925
Chris Mason5f39d392007-10-15 16:14:19 -04003926 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003927 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003928
3929 /* then fixup the leaf pointer in the path */
3930 if (path->slots[0] < push_items) {
3931 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003932 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003933 free_extent_buffer(path->nodes[0]);
3934 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003935 path->slots[1] -= 1;
3936 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003937 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003938 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003939 path->slots[0] -= push_items;
3940 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003941 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003942 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003943out:
3944 btrfs_tree_unlock(left);
3945 free_extent_buffer(left);
3946 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003947}
3948
Chris Mason74123bd2007-02-02 11:05:29 -05003949/*
Chris Mason44871b12009-03-13 10:04:31 -04003950 * push some data in the path leaf to the left, trying to free up at
3951 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003952 *
3953 * max_slot can put a limit on how far into the leaf we'll push items. The
3954 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3955 * items
Chris Mason44871b12009-03-13 10:04:31 -04003956 */
3957static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003958 *root, struct btrfs_path *path, int min_data_size,
3959 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003960{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003961 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003962 struct extent_buffer *right = path->nodes[0];
3963 struct extent_buffer *left;
3964 int slot;
3965 int free_space;
3966 u32 right_nritems;
3967 int ret = 0;
3968
3969 slot = path->slots[1];
3970 if (slot == 0)
3971 return 1;
3972 if (!path->nodes[1])
3973 return 1;
3974
3975 right_nritems = btrfs_header_nritems(right);
3976 if (right_nritems == 0)
3977 return 1;
3978
3979 btrfs_assert_tree_locked(path->nodes[1]);
3980
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003981 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003982 /*
3983 * slot - 1 is not valid or we fail to read the left node,
3984 * no big deal, just return.
3985 */
3986 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003987 return 1;
3988
Chris Mason44871b12009-03-13 10:04:31 -04003989 btrfs_tree_lock(left);
3990 btrfs_set_lock_blocking(left);
3991
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003992 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04003993 if (free_space < data_size) {
3994 ret = 1;
3995 goto out;
3996 }
3997
3998 /* cow and double check */
3999 ret = btrfs_cow_block(trans, root, left,
4000 path->nodes[1], slot - 1, &left);
4001 if (ret) {
4002 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004003 if (ret == -ENOSPC)
4004 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004005 goto out;
4006 }
4007
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004008 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04004009 if (free_space < data_size) {
4010 ret = 1;
4011 goto out;
4012 }
4013
David Sterba66cb7dd2017-02-10 19:14:36 +01004014 return __push_leaf_left(fs_info, path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004015 empty, left, free_space, right_nritems,
4016 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004017out:
4018 btrfs_tree_unlock(left);
4019 free_extent_buffer(left);
4020 return ret;
4021}
4022
4023/*
Chris Mason74123bd2007-02-02 11:05:29 -05004024 * split the path's leaf in two, making sure there is at least data_size
4025 * available for the resulting leaf level of the path.
4026 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004027static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004028 struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004029 struct btrfs_path *path,
4030 struct extent_buffer *l,
4031 struct extent_buffer *right,
4032 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004033{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004034 int data_copy_size;
4035 int rt_data_off;
4036 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004037 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004038 struct btrfs_map_token token;
4039
4040 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004041
Chris Mason5f39d392007-10-15 16:14:19 -04004042 nritems = nritems - mid;
4043 btrfs_set_header_nritems(right, nritems);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004044 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
Chris Mason5f39d392007-10-15 16:14:19 -04004045
4046 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4047 btrfs_item_nr_offset(mid),
4048 nritems * sizeof(struct btrfs_item));
4049
4050 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004051 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4052 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004053 leaf_data_end(fs_info, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004055 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004056
4057 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004058 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004059 u32 ioff;
4060
Chris Masoncfed81a2012-03-03 07:40:03 -05004061 ioff = btrfs_token_item_offset(right, item, &token);
4062 btrfs_set_token_item_offset(right, item,
4063 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004064 }
Chris Mason74123bd2007-02-02 11:05:29 -05004065
Chris Mason5f39d392007-10-15 16:14:19 -04004066 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004067 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004068 insert_ptr(trans, fs_info, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004069 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004070
4071 btrfs_mark_buffer_dirty(right);
4072 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004073 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004074
Chris Masonbe0e5c02007-01-26 15:51:26 -05004075 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004076 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004077 free_extent_buffer(path->nodes[0]);
4078 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004079 path->slots[0] -= mid;
4080 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004081 } else {
4082 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004083 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004084 }
Chris Mason5f39d392007-10-15 16:14:19 -04004085
Chris Masoneb60cea2007-02-02 09:18:22 -05004086 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004087}
4088
4089/*
Chris Mason99d8f832010-07-07 10:51:48 -04004090 * double splits happen when we need to insert a big item in the middle
4091 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4092 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4093 * A B C
4094 *
4095 * We avoid this by trying to push the items on either side of our target
4096 * into the adjacent leaves. If all goes well we can avoid the double split
4097 * completely.
4098 */
4099static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4100 struct btrfs_root *root,
4101 struct btrfs_path *path,
4102 int data_size)
4103{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004104 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason99d8f832010-07-07 10:51:48 -04004105 int ret;
4106 int progress = 0;
4107 int slot;
4108 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004109 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004110
4111 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004112 if (slot < btrfs_header_nritems(path->nodes[0]))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004113 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004114
4115 /*
4116 * try to push all the items after our slot into the
4117 * right leaf
4118 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004119 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004120 if (ret < 0)
4121 return ret;
4122
4123 if (ret == 0)
4124 progress++;
4125
4126 nritems = btrfs_header_nritems(path->nodes[0]);
4127 /*
4128 * our goal is to get our slot at the start or end of a leaf. If
4129 * we've done so we're done
4130 */
4131 if (path->slots[0] == 0 || path->slots[0] == nritems)
4132 return 0;
4133
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004134 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004135 return 0;
4136
4137 /* try to push all the items before our slot into the next leaf */
4138 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004139 space_needed = data_size;
4140 if (slot > 0)
4141 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004142 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004143 if (ret < 0)
4144 return ret;
4145
4146 if (ret == 0)
4147 progress++;
4148
4149 if (progress)
4150 return 0;
4151 return 1;
4152}
4153
4154/*
Chris Mason44871b12009-03-13 10:04:31 -04004155 * split the path's leaf in two, making sure there is at least data_size
4156 * available for the resulting leaf level of the path.
4157 *
4158 * returns 0 if all went well and < 0 on failure.
4159 */
4160static noinline int split_leaf(struct btrfs_trans_handle *trans,
4161 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004162 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004163 struct btrfs_path *path, int data_size,
4164 int extend)
4165{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004166 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004167 struct extent_buffer *l;
4168 u32 nritems;
4169 int mid;
4170 int slot;
4171 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004172 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004173 int ret = 0;
4174 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004175 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004176 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004177 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004178
Yan, Zhenga5719522009-09-24 09:17:31 -04004179 l = path->nodes[0];
4180 slot = path->slots[0];
4181 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004182 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004183 return -EOVERFLOW;
4184
Chris Mason44871b12009-03-13 10:04:31 -04004185 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004186 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004187 int space_needed = data_size;
4188
4189 if (slot < btrfs_header_nritems(l))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004190 space_needed -= btrfs_leaf_free_space(fs_info, l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004191
4192 wret = push_leaf_right(trans, root, path, space_needed,
4193 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004194 if (wret < 0)
4195 return wret;
4196 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004197 space_needed = data_size;
4198 if (slot > 0)
4199 space_needed -= btrfs_leaf_free_space(fs_info,
4200 l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004201 wret = push_leaf_left(trans, root, path, space_needed,
4202 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004203 if (wret < 0)
4204 return wret;
4205 }
4206 l = path->nodes[0];
4207
4208 /* did the pushes work? */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004209 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004210 return 0;
4211 }
4212
4213 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004214 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004215 if (ret)
4216 return ret;
4217 }
4218again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004219 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004220 l = path->nodes[0];
4221 slot = path->slots[0];
4222 nritems = btrfs_header_nritems(l);
4223 mid = (nritems + 1) / 2;
4224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004225 if (mid <= slot) {
4226 if (nritems == 1 ||
4227 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004228 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004229 if (slot >= nritems) {
4230 split = 0;
4231 } else {
4232 mid = slot;
4233 if (mid != nritems &&
4234 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004235 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004236 if (data_size && !tried_avoid_double)
4237 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004238 split = 2;
4239 }
4240 }
4241 }
4242 } else {
4243 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004244 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004245 if (!extend && data_size && slot == 0) {
4246 split = 0;
4247 } else if ((extend || !data_size) && slot == 0) {
4248 mid = 1;
4249 } else {
4250 mid = slot;
4251 if (mid != nritems &&
4252 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004253 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004254 if (data_size && !tried_avoid_double)
4255 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304256 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004257 }
4258 }
4259 }
4260 }
4261
4262 if (split == 0)
4263 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4264 else
4265 btrfs_item_key(l, &disk_key, mid);
4266
David Sterba4d75f8a2014-06-15 01:54:12 +02004267 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4268 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004269 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004270 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004271
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004272 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004273
David Sterbab159fa22016-11-08 18:09:03 +01004274 memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
Chris Mason44871b12009-03-13 10:04:31 -04004275 btrfs_set_header_bytenr(right, right->start);
4276 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004277 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004278 btrfs_set_header_owner(right, root->root_key.objectid);
4279 btrfs_set_header_level(right, 0);
David Sterbad24ee972016-11-09 17:44:25 +01004280 write_extent_buffer_fsid(right, fs_info->fsid);
4281 write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
Chris Mason44871b12009-03-13 10:04:31 -04004282
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004283 if (split == 0) {
4284 if (mid <= slot) {
4285 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004286 insert_ptr(trans, fs_info, path, &disk_key,
4287 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004288 btrfs_tree_unlock(path->nodes[0]);
4289 free_extent_buffer(path->nodes[0]);
4290 path->nodes[0] = right;
4291 path->slots[0] = 0;
4292 path->slots[1] += 1;
4293 } else {
4294 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004295 insert_ptr(trans, fs_info, path, &disk_key,
4296 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 btrfs_tree_unlock(path->nodes[0]);
4298 free_extent_buffer(path->nodes[0]);
4299 path->nodes[0] = right;
4300 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004301 if (path->slots[1] == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004302 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004303 }
Liu Bo196e0242016-09-07 14:48:28 -07004304 /*
4305 * We create a new leaf 'right' for the required ins_len and
4306 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4307 * the content of ins_len to 'right'.
4308 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004309 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004310 }
4311
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004312 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004313
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004314 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004315 BUG_ON(num_doubles != 0);
4316 num_doubles++;
4317 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004318 }
Chris Mason44871b12009-03-13 10:04:31 -04004319
Jeff Mahoney143bede2012-03-01 14:56:26 +01004320 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004321
4322push_for_double:
4323 push_for_double_split(trans, root, path, data_size);
4324 tried_avoid_double = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004325 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004326 return 0;
4327 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004328}
4329
Yan, Zhengad48fd752009-11-12 09:33:58 +00004330static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4331 struct btrfs_root *root,
4332 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004333{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004334 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004335 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004336 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004337 struct btrfs_file_extent_item *fi;
4338 u64 extent_len = 0;
4339 u32 item_size;
4340 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004341
4342 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004343 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4344
4345 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4346 key.type != BTRFS_EXTENT_CSUM_KEY);
4347
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004348 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004349 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004350
4351 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004352 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4353 fi = btrfs_item_ptr(leaf, path->slots[0],
4354 struct btrfs_file_extent_item);
4355 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4356 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004357 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004358
Chris Mason459931e2008-12-10 09:10:46 -05004359 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004360 path->search_for_split = 1;
4361 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004362 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004363 if (ret > 0)
4364 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004365 if (ret < 0)
4366 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004367
Yan, Zhengad48fd752009-11-12 09:33:58 +00004368 ret = -EAGAIN;
4369 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004370 /* if our item isn't there, return now */
4371 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004372 goto err;
4373
Chris Mason109f6ae2010-04-02 09:20:18 -04004374 /* the leaf has changed, it now has room. return now */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004375 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004376 goto err;
4377
Yan, Zhengad48fd752009-11-12 09:33:58 +00004378 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4379 fi = btrfs_item_ptr(leaf, path->slots[0],
4380 struct btrfs_file_extent_item);
4381 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4382 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004383 }
4384
Chris Masonb9473432009-03-13 11:00:37 -04004385 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004386 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004387 if (ret)
4388 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004389
Yan, Zhengad48fd752009-11-12 09:33:58 +00004390 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004391 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004392 return 0;
4393err:
4394 path->keep_locks = 0;
4395 return ret;
4396}
4397
David Sterba4961e292017-02-10 18:49:53 +01004398static noinline int split_item(struct btrfs_fs_info *fs_info,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004399 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004400 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004401 unsigned long split_offset)
4402{
4403 struct extent_buffer *leaf;
4404 struct btrfs_item *item;
4405 struct btrfs_item *new_item;
4406 int slot;
4407 char *buf;
4408 u32 nritems;
4409 u32 item_size;
4410 u32 orig_offset;
4411 struct btrfs_disk_key disk_key;
4412
Chris Masonb9473432009-03-13 11:00:37 -04004413 leaf = path->nodes[0];
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004414 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004415
Chris Masonb4ce94d2009-02-04 09:25:08 -05004416 btrfs_set_path_blocking(path);
4417
Ross Kirkdd3cc162013-09-16 15:58:09 +01004418 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004419 orig_offset = btrfs_item_offset(leaf, item);
4420 item_size = btrfs_item_size(leaf, item);
4421
Chris Mason459931e2008-12-10 09:10:46 -05004422 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004423 if (!buf)
4424 return -ENOMEM;
4425
Chris Mason459931e2008-12-10 09:10:46 -05004426 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4427 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004428
Chris Mason459931e2008-12-10 09:10:46 -05004429 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004430 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004431 if (slot != nritems) {
4432 /* shift the items */
4433 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004434 btrfs_item_nr_offset(slot),
4435 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004436 }
4437
4438 btrfs_cpu_key_to_disk(&disk_key, new_key);
4439 btrfs_set_item_key(leaf, &disk_key, slot);
4440
Ross Kirkdd3cc162013-09-16 15:58:09 +01004441 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004442
4443 btrfs_set_item_offset(leaf, new_item, orig_offset);
4444 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4445
4446 btrfs_set_item_offset(leaf, item,
4447 orig_offset + item_size - split_offset);
4448 btrfs_set_item_size(leaf, item, split_offset);
4449
4450 btrfs_set_header_nritems(leaf, nritems + 1);
4451
4452 /* write the data for the start of the original item */
4453 write_extent_buffer(leaf, buf,
4454 btrfs_item_ptr_offset(leaf, path->slots[0]),
4455 split_offset);
4456
4457 /* write the data for the new item */
4458 write_extent_buffer(leaf, buf + split_offset,
4459 btrfs_item_ptr_offset(leaf, slot),
4460 item_size - split_offset);
4461 btrfs_mark_buffer_dirty(leaf);
4462
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004463 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004464 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004465 return 0;
4466}
4467
4468/*
4469 * This function splits a single item into two items,
4470 * giving 'new_key' to the new item and splitting the
4471 * old one at split_offset (from the start of the item).
4472 *
4473 * The path may be released by this operation. After
4474 * the split, the path is pointing to the old item. The
4475 * new item is going to be in the same node as the old one.
4476 *
4477 * Note, the item being split must be smaller enough to live alone on
4478 * a tree block with room for one extra struct btrfs_item
4479 *
4480 * This allows us to split the item in place, keeping a lock on the
4481 * leaf the entire time.
4482 */
4483int btrfs_split_item(struct btrfs_trans_handle *trans,
4484 struct btrfs_root *root,
4485 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004486 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004487 unsigned long split_offset)
4488{
4489 int ret;
4490 ret = setup_leaf_for_split(trans, root, path,
4491 sizeof(struct btrfs_item));
4492 if (ret)
4493 return ret;
4494
David Sterba4961e292017-02-10 18:49:53 +01004495 ret = split_item(root->fs_info, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004496 return ret;
4497}
4498
4499/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004500 * This function duplicate a item, giving 'new_key' to the new item.
4501 * It guarantees both items live in the same tree leaf and the new item
4502 * is contiguous with the original item.
4503 *
4504 * This allows us to split file extent in place, keeping a lock on the
4505 * leaf the entire time.
4506 */
4507int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4508 struct btrfs_root *root,
4509 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004510 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004511{
4512 struct extent_buffer *leaf;
4513 int ret;
4514 u32 item_size;
4515
4516 leaf = path->nodes[0];
4517 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4518 ret = setup_leaf_for_split(trans, root, path,
4519 item_size + sizeof(struct btrfs_item));
4520 if (ret)
4521 return ret;
4522
4523 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004524 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004525 item_size, item_size +
4526 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004527 leaf = path->nodes[0];
4528 memcpy_extent_buffer(leaf,
4529 btrfs_item_ptr_offset(leaf, path->slots[0]),
4530 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4531 item_size);
4532 return 0;
4533}
4534
4535/*
Chris Masond352ac62008-09-29 15:18:18 -04004536 * make the item pointed to by the path smaller. new_size indicates
4537 * how small to make it, and from_end tells us if we just chop bytes
4538 * off the end of the item or if we shift the item to chop bytes off
4539 * the front.
4540 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004541void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4542 struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004543{
Chris Masonb18c6682007-04-17 13:26:50 -04004544 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004545 struct extent_buffer *leaf;
4546 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004547 u32 nritems;
4548 unsigned int data_end;
4549 unsigned int old_data_start;
4550 unsigned int old_size;
4551 unsigned int size_diff;
4552 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004553 struct btrfs_map_token token;
4554
4555 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004556
Chris Mason5f39d392007-10-15 16:14:19 -04004557 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004558 slot = path->slots[0];
4559
4560 old_size = btrfs_item_size_nr(leaf, slot);
4561 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004562 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004563
Chris Mason5f39d392007-10-15 16:14:19 -04004564 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004565 data_end = leaf_data_end(fs_info, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004566
Chris Mason5f39d392007-10-15 16:14:19 -04004567 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004568
Chris Masonb18c6682007-04-17 13:26:50 -04004569 size_diff = old_size - new_size;
4570
4571 BUG_ON(slot < 0);
4572 BUG_ON(slot >= nritems);
4573
4574 /*
4575 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4576 */
4577 /* first correct the data pointers */
4578 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004579 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004580 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004581
Chris Masoncfed81a2012-03-03 07:40:03 -05004582 ioff = btrfs_token_item_offset(leaf, item, &token);
4583 btrfs_set_token_item_offset(leaf, item,
4584 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004585 }
Chris Masondb945352007-10-15 16:15:53 -04004586
Chris Masonb18c6682007-04-17 13:26:50 -04004587 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004588 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004589 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4590 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004591 data_end, old_data_start + new_size - data_end);
4592 } else {
4593 struct btrfs_disk_key disk_key;
4594 u64 offset;
4595
4596 btrfs_item_key(leaf, &disk_key, slot);
4597
4598 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4599 unsigned long ptr;
4600 struct btrfs_file_extent_item *fi;
4601
4602 fi = btrfs_item_ptr(leaf, slot,
4603 struct btrfs_file_extent_item);
4604 fi = (struct btrfs_file_extent_item *)(
4605 (unsigned long)fi - size_diff);
4606
4607 if (btrfs_file_extent_type(leaf, fi) ==
4608 BTRFS_FILE_EXTENT_INLINE) {
4609 ptr = btrfs_item_ptr_offset(leaf, slot);
4610 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004611 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004612 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004613 }
4614 }
4615
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004616 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4617 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004618 data_end, old_data_start - data_end);
4619
4620 offset = btrfs_disk_key_offset(&disk_key);
4621 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4622 btrfs_set_item_key(leaf, &disk_key, slot);
4623 if (slot == 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004624 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004625 }
Chris Mason5f39d392007-10-15 16:14:19 -04004626
Ross Kirkdd3cc162013-09-16 15:58:09 +01004627 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004628 btrfs_set_item_size(leaf, item, new_size);
4629 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004630
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004631 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004632 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004633 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004634 }
Chris Masonb18c6682007-04-17 13:26:50 -04004635}
4636
Chris Masond352ac62008-09-29 15:18:18 -04004637/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004638 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004639 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004640void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004641 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004642{
Chris Mason6567e832007-04-16 09:22:45 -04004643 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004644 struct extent_buffer *leaf;
4645 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004646 u32 nritems;
4647 unsigned int data_end;
4648 unsigned int old_data;
4649 unsigned int old_size;
4650 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004651 struct btrfs_map_token token;
4652
4653 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004654
Chris Mason5f39d392007-10-15 16:14:19 -04004655 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004656
Chris Mason5f39d392007-10-15 16:14:19 -04004657 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004658 data_end = leaf_data_end(fs_info, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004659
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004660 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004661 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004662 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004663 }
Chris Mason6567e832007-04-16 09:22:45 -04004664 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004665 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004666
4667 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004668 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004669 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004670 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4671 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004672 BUG_ON(1);
4673 }
Chris Mason6567e832007-04-16 09:22:45 -04004674
4675 /*
4676 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4677 */
4678 /* first correct the data pointers */
4679 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004680 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004681 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004682
Chris Masoncfed81a2012-03-03 07:40:03 -05004683 ioff = btrfs_token_item_offset(leaf, item, &token);
4684 btrfs_set_token_item_offset(leaf, item,
4685 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004686 }
Chris Mason5f39d392007-10-15 16:14:19 -04004687
Chris Mason6567e832007-04-16 09:22:45 -04004688 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004689 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4690 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004691 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004692
Chris Mason6567e832007-04-16 09:22:45 -04004693 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004694 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004695 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004696 btrfs_set_item_size(leaf, item, old_size + data_size);
4697 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004698
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004699 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004700 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004701 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004702 }
Chris Mason6567e832007-04-16 09:22:45 -04004703}
4704
Chris Mason74123bd2007-02-02 11:05:29 -05004705/*
Chris Mason44871b12009-03-13 10:04:31 -04004706 * this is a helper for btrfs_insert_empty_items, the main goal here is
4707 * to save stack depth by doing the bulk of the work in a function
4708 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004709 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004710void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004711 const struct btrfs_key *cpu_key, u32 *data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004712 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004713{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004714 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004715 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004716 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004717 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004718 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004719 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004720 struct extent_buffer *leaf;
4721 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004722 struct btrfs_map_token token;
4723
Filipe Manana24cdc842014-07-28 19:34:35 +01004724 if (path->slots[0] == 0) {
4725 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004726 fixup_low_keys(fs_info, path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004727 }
4728 btrfs_unlock_up_safe(path, 1);
4729
Chris Masoncfed81a2012-03-03 07:40:03 -05004730 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004731
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);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004736 data_end = leaf_data_end(fs_info, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004737
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004738 if (btrfs_leaf_free_space(fs_info, 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",
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004741 total_size, btrfs_leaf_free_space(fs_info, 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
Chris Masonbe0e5c02007-01-26 15:51:26 -05004745 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004746 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004747
Chris Mason5f39d392007-10-15 16:14:19 -04004748 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004749 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004750 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004751 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004752 BUG_ON(1);
4753 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004754 /*
4755 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4756 */
4757 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004758 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004759 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004760
Jeff Mahoney62e85572016-09-20 10:05:01 -04004761 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004762 ioff = btrfs_token_item_offset(leaf, item, &token);
4763 btrfs_set_token_item_offset(leaf, item,
4764 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004765 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004766 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004767 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004768 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004769 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004770
4771 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004772 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4773 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004774 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004775 data_end = old_data;
4776 }
Chris Mason5f39d392007-10-15 16:14:19 -04004777
Chris Mason62e27492007-03-15 12:56:47 -04004778 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004779 for (i = 0; i < nr; i++) {
4780 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4781 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004782 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004783 btrfs_set_token_item_offset(leaf, item,
4784 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004785 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004786 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004787 }
Chris Mason44871b12009-03-13 10:04:31 -04004788
Chris Mason9c583092008-01-29 15:15:18 -05004789 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004790 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004791
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004792 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004793 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004794 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004795 }
Chris Mason44871b12009-03-13 10:04:31 -04004796}
4797
4798/*
4799 * Given a key and some data, insert items into the tree.
4800 * This does all the path init required, making room in the tree if needed.
4801 */
4802int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4803 struct btrfs_root *root,
4804 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004805 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004806 int nr)
4807{
Chris Mason44871b12009-03-13 10:04:31 -04004808 int ret = 0;
4809 int slot;
4810 int i;
4811 u32 total_size = 0;
4812 u32 total_data = 0;
4813
4814 for (i = 0; i < nr; i++)
4815 total_data += data_size[i];
4816
4817 total_size = total_data + (nr * sizeof(struct btrfs_item));
4818 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4819 if (ret == 0)
4820 return -EEXIST;
4821 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004822 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004823
Chris Mason44871b12009-03-13 10:04:31 -04004824 slot = path->slots[0];
4825 BUG_ON(slot < 0);
4826
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004827 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004828 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004829 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004830}
4831
4832/*
4833 * Given a key and some data, insert an item into the tree.
4834 * This does all the path init required, making room in the tree if needed.
4835 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004836int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4837 const struct btrfs_key *cpu_key, void *data,
4838 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004839{
4840 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004841 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004842 struct extent_buffer *leaf;
4843 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004844
Chris Mason2c90e5d2007-04-02 10:50:19 -04004845 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004846 if (!path)
4847 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004848 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004849 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004850 leaf = path->nodes[0];
4851 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4852 write_extent_buffer(leaf, data, ptr, data_size);
4853 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004854 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004855 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004856 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004857}
4858
Chris Mason74123bd2007-02-02 11:05:29 -05004859/*
Chris Mason5de08d72007-02-24 06:24:44 -05004860 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004861 *
Chris Masond352ac62008-09-29 15:18:18 -04004862 * the tree should have been previously balanced so the deletion does not
4863 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004864 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004865static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4866 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004868 struct btrfs_fs_info *fs_info = root->fs_info;
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) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004875 if (level)
David Sterbaa446a972018-03-05 15:26:29 +01004876 tree_mod_log_eb_move(parent, slot, slot + 1,
4877 nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004878 memmove_extent_buffer(parent,
4879 btrfs_node_key_ptr_offset(slot),
4880 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004881 sizeof(struct btrfs_key_ptr) *
4882 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004883 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004884 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4885 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004886 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004887 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004888
Chris Mason7518a232007-03-12 12:01:18 -04004889 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004890 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004891 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004892 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004893 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004894 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004895 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004896 struct btrfs_disk_key disk_key;
4897
4898 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004899 fixup_low_keys(fs_info, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004900 }
Chris Masond6025572007-03-30 14:27:56 -04004901 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004902}
4903
Chris Mason74123bd2007-02-02 11:05:29 -05004904/*
Chris Mason323ac952008-10-01 19:05:46 -04004905 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004906 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004907 *
4908 * This deletes the pointer in path->nodes[1] and frees the leaf
4909 * block extent. zero is returned if it all worked out, < 0 otherwise.
4910 *
4911 * The path must have already been setup for deleting the leaf, including
4912 * all the proper balancing. path->nodes[1] must be locked.
4913 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004914static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4915 struct btrfs_root *root,
4916 struct btrfs_path *path,
4917 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004918{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004919 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004920 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004921
Chris Mason4d081c42009-02-04 09:31:28 -05004922 /*
4923 * btrfs_free_extent is expensive, we want to make sure we
4924 * aren't holding any locks when we call it
4925 */
4926 btrfs_unlock_up_safe(path, 0);
4927
Yan, Zhengf0486c62010-05-16 10:46:25 -04004928 root_sub_used(root, leaf->len);
4929
Josef Bacik3083ee22012-03-09 16:01:49 -05004930 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004931 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004932 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004933}
4934/*
Chris Mason74123bd2007-02-02 11:05:29 -05004935 * delete the item at the leaf level in path. If that empties
4936 * the leaf, remove it from the tree
4937 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004938int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4939 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004940{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004941 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004942 struct extent_buffer *leaf;
4943 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004944 u32 last_off;
4945 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004946 int ret = 0;
4947 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004948 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004949 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004950 struct btrfs_map_token token;
4951
4952 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004953
Chris Mason5f39d392007-10-15 16:14:19 -04004954 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004955 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4956
4957 for (i = 0; i < nr; i++)
4958 dsize += btrfs_item_size_nr(leaf, slot + i);
4959
Chris Mason5f39d392007-10-15 16:14:19 -04004960 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004961
Chris Mason85e21ba2008-01-29 15:11:36 -05004962 if (slot + nr != nritems) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004963 int data_end = leaf_data_end(fs_info, leaf);
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
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004971 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004972
Ross Kirkdd3cc162013-09-16 15:58:09 +01004973 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004974 ioff = btrfs_token_item_offset(leaf, item, &token);
4975 btrfs_set_token_item_offset(leaf, item,
4976 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004977 }
Chris Masondb945352007-10-15 16:15:53 -04004978
Chris Mason5f39d392007-10-15 16:14:19 -04004979 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004980 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004981 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004982 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004983 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004984 btrfs_set_header_nritems(leaf, nritems - nr);
4985 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004986
Chris Mason74123bd2007-02-02 11:05:29 -05004987 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004988 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004989 if (leaf == root->node) {
4990 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004991 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004992 btrfs_set_path_blocking(path);
David Sterba7c302b42017-02-10 18:47:57 +01004993 clean_tree_block(fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004994 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004995 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004996 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004997 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004998 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004999 struct btrfs_disk_key disk_key;
5000
5001 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005002 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005003 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005004
Chris Mason74123bd2007-02-02 11:05:29 -05005005 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005006 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005007 /* push_leaf_left fixes the path.
5008 * make sure the path still points to our leaf
5009 * for possible call to del_ptr below
5010 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005011 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005012 extent_buffer_get(leaf);
5013
Chris Masonb9473432009-03-13 11:00:37 -04005014 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005015 wret = push_leaf_left(trans, root, path, 1, 1,
5016 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005017 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005018 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005019
5020 if (path->nodes[0] == leaf &&
5021 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005022 wret = push_leaf_right(trans, root, path, 1,
5023 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005024 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005025 ret = wret;
5026 }
Chris Mason5f39d392007-10-15 16:14:19 -04005027
5028 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005029 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005030 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005031 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005032 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005033 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005034 /* if we're still in the path, make sure
5035 * we're dirty. Otherwise, one of the
5036 * push_leaf functions must have already
5037 * dirtied this buffer
5038 */
5039 if (path->nodes[0] == leaf)
5040 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005041 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005042 }
Chris Masond5719762007-03-23 10:01:08 -04005043 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005044 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005045 }
5046 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005047 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005048}
5049
Chris Mason97571fd2007-02-24 13:39:08 -05005050/*
Chris Mason925baed2008-06-25 16:01:30 -04005051 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005052 * returns 0 if it found something or 1 if there are no lesser leaves.
5053 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005054 *
5055 * This may release the path, and so you may lose any locks held at the
5056 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005057 */
Josef Bacik16e75492013-10-22 12:18:51 -04005058int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005059{
Chris Mason925baed2008-06-25 16:01:30 -04005060 struct btrfs_key key;
5061 struct btrfs_disk_key found_key;
5062 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005063
Chris Mason925baed2008-06-25 16:01:30 -04005064 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005065
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005066 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005067 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005068 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005069 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005070 key.offset = (u64)-1;
5071 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005072 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005073 key.type = (u8)-1;
5074 key.offset = (u64)-1;
5075 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005076 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005077 }
Chris Mason7bb86312007-12-11 09:25:06 -05005078
David Sterbab3b4aa72011-04-21 01:20:15 +02005079 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005080 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5081 if (ret < 0)
5082 return ret;
5083 btrfs_item_key(path->nodes[0], &found_key, 0);
5084 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005085 /*
5086 * We might have had an item with the previous key in the tree right
5087 * before we released our path. And after we released our path, that
5088 * item might have been pushed to the first slot (0) of the leaf we
5089 * were holding due to a tree balance. Alternatively, an item with the
5090 * previous key can exist as the only element of a leaf (big fat item).
5091 * Therefore account for these 2 cases, so that our callers (like
5092 * btrfs_previous_item) don't miss an existing item with a key matching
5093 * the previous key we computed above.
5094 */
5095 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005096 return 0;
5097 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005098}
5099
Chris Mason3f157a22008-06-25 16:01:31 -04005100/*
5101 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005102 * for nodes or leaves that are have a minimum transaction id.
5103 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005104 *
5105 * This does not cow, but it does stuff the starting key it finds back
5106 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5107 * key and get a writable path.
5108 *
Chris Mason3f157a22008-06-25 16:01:31 -04005109 * This honors path->lowest_level to prevent descent past a given level
5110 * of the tree.
5111 *
Chris Masond352ac62008-09-29 15:18:18 -04005112 * min_trans indicates the oldest transaction that you are interested
5113 * in walking through. Any nodes or leaves older than min_trans are
5114 * skipped over (without reading them).
5115 *
Chris Mason3f157a22008-06-25 16:01:31 -04005116 * returns zero if something useful was found, < 0 on error and 1 if there
5117 * was nothing in the tree that matched the search criteria.
5118 */
5119int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005120 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005121 u64 min_trans)
5122{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005123 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason3f157a22008-06-25 16:01:31 -04005124 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);
Chris Mason3f157a22008-06-25 16:01:31 -04005149
Chris Mason323ac952008-10-01 19:05:46 -04005150 /* at the lowest level, we're done, setup the path and exit */
5151 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005152 if (slot >= nritems)
5153 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005154 ret = 0;
5155 path->slots[level] = slot;
5156 btrfs_item_key_to_cpu(cur, &found_key, slot);
5157 goto out;
5158 }
Yan96524802008-07-24 12:19:49 -04005159 if (sret && slot > 0)
5160 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005161 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005162 * check this node pointer against the min_trans parameters.
5163 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005164 */
Chris Masond3977122009-01-05 21:25:51 -05005165 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005166 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005167
Chris Mason3f157a22008-06-25 16:01:31 -04005168 gen = btrfs_node_ptr_generation(cur, slot);
5169 if (gen < min_trans) {
5170 slot++;
5171 continue;
5172 }
Eric Sandeende78b512013-01-31 18:21:12 +00005173 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005174 }
Chris Masone02119d2008-09-05 16:13:11 -04005175find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005176 /*
5177 * we didn't find a candidate key in this node, walk forward
5178 * and find another one
5179 */
5180 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005181 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005182 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005183 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005184 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005185 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005186 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005187 goto again;
5188 } else {
5189 goto out;
5190 }
5191 }
5192 /* save our key for returning back */
5193 btrfs_node_key_to_cpu(cur, &found_key, slot);
5194 path->slots[level] = slot;
5195 if (level == path->lowest_level) {
5196 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005197 goto out;
5198 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005199 btrfs_set_path_blocking(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005200 cur = read_node_slot(fs_info, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005201 if (IS_ERR(cur)) {
5202 ret = PTR_ERR(cur);
5203 goto out;
5204 }
Chris Mason3f157a22008-06-25 16:01:31 -04005205
Chris Masonbd681512011-07-16 15:23:14 -04005206 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005207
Chris Masonbd681512011-07-16 15:23:14 -04005208 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005209 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005210 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005211 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005212 }
5213out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005214 path->keep_locks = keep_locks;
5215 if (ret == 0) {
5216 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5217 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005218 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005219 }
Chris Mason3f157a22008-06-25 16:01:31 -04005220 return ret;
5221}
5222
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005223static int tree_move_down(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005224 struct btrfs_path *path,
David Sterbaab6a43e2017-02-10 19:24:53 +01005225 int *level)
Alexander Block70698302012-06-05 21:07:48 +02005226{
Liu Bofb770ae2016-07-05 12:10:14 -07005227 struct extent_buffer *eb;
5228
Chris Mason74dd17f2012-08-07 16:25:13 -04005229 BUG_ON(*level == 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005230 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
Liu Bofb770ae2016-07-05 12:10:14 -07005231 if (IS_ERR(eb))
5232 return PTR_ERR(eb);
5233
5234 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005235 path->slots[*level - 1] = 0;
5236 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005237 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005238}
5239
David Sterbaf1e30262017-02-10 19:25:51 +01005240static int tree_move_next_or_upnext(struct btrfs_path *path,
Alexander Block70698302012-06-05 21:07:48 +02005241 int *level, int root_level)
5242{
5243 int ret = 0;
5244 int nritems;
5245 nritems = btrfs_header_nritems(path->nodes[*level]);
5246
5247 path->slots[*level]++;
5248
Chris Mason74dd17f2012-08-07 16:25:13 -04005249 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005250 if (*level == root_level)
5251 return -1;
5252
5253 /* move upnext */
5254 path->slots[*level] = 0;
5255 free_extent_buffer(path->nodes[*level]);
5256 path->nodes[*level] = NULL;
5257 (*level)++;
5258 path->slots[*level]++;
5259
5260 nritems = btrfs_header_nritems(path->nodes[*level]);
5261 ret = 1;
5262 }
5263 return ret;
5264}
5265
5266/*
5267 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5268 * or down.
5269 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005270static int tree_advance(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005271 struct btrfs_path *path,
5272 int *level, int root_level,
5273 int allow_down,
5274 struct btrfs_key *key)
5275{
5276 int ret;
5277
5278 if (*level == 0 || !allow_down) {
David Sterbaf1e30262017-02-10 19:25:51 +01005279 ret = tree_move_next_or_upnext(path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005280 } else {
David Sterbaab6a43e2017-02-10 19:24:53 +01005281 ret = tree_move_down(fs_info, path, level);
Alexander Block70698302012-06-05 21:07:48 +02005282 }
5283 if (ret >= 0) {
5284 if (*level == 0)
5285 btrfs_item_key_to_cpu(path->nodes[*level], key,
5286 path->slots[*level]);
5287 else
5288 btrfs_node_key_to_cpu(path->nodes[*level], key,
5289 path->slots[*level]);
5290 }
5291 return ret;
5292}
5293
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005294static int tree_compare_item(struct btrfs_path *left_path,
Alexander Block70698302012-06-05 21:07:48 +02005295 struct btrfs_path *right_path,
5296 char *tmp_buf)
5297{
5298 int cmp;
5299 int len1, len2;
5300 unsigned long off1, off2;
5301
5302 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5303 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5304 if (len1 != len2)
5305 return 1;
5306
5307 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5308 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5309 right_path->slots[0]);
5310
5311 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5312
5313 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5314 if (cmp)
5315 return 1;
5316 return 0;
5317}
5318
5319#define ADVANCE 1
5320#define ADVANCE_ONLY_NEXT -1
5321
5322/*
5323 * This function compares two trees and calls the provided callback for
5324 * every changed/new/deleted item it finds.
5325 * If shared tree blocks are encountered, whole subtrees are skipped, making
5326 * the compare pretty fast on snapshotted subvolumes.
5327 *
5328 * This currently works on commit roots only. As commit roots are read only,
5329 * we don't do any locking. The commit roots are protected with transactions.
5330 * Transactions are ended and rejoined when a commit is tried in between.
5331 *
5332 * This function checks for modifications done to the trees while comparing.
5333 * If it detects a change, it aborts immediately.
5334 */
5335int btrfs_compare_trees(struct btrfs_root *left_root,
5336 struct btrfs_root *right_root,
5337 btrfs_changed_cb_t changed_cb, void *ctx)
5338{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005339 struct btrfs_fs_info *fs_info = left_root->fs_info;
Alexander Block70698302012-06-05 21:07:48 +02005340 int ret;
5341 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005342 struct btrfs_path *left_path = NULL;
5343 struct btrfs_path *right_path = NULL;
5344 struct btrfs_key left_key;
5345 struct btrfs_key right_key;
5346 char *tmp_buf = NULL;
5347 int left_root_level;
5348 int right_root_level;
5349 int left_level;
5350 int right_level;
5351 int left_end_reached;
5352 int right_end_reached;
5353 int advance_left;
5354 int advance_right;
5355 u64 left_blockptr;
5356 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005357 u64 left_gen;
5358 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005359
5360 left_path = btrfs_alloc_path();
5361 if (!left_path) {
5362 ret = -ENOMEM;
5363 goto out;
5364 }
5365 right_path = btrfs_alloc_path();
5366 if (!right_path) {
5367 ret = -ENOMEM;
5368 goto out;
5369 }
5370
Michal Hocko752ade62017-05-08 15:57:27 -07005371 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
Alexander Block70698302012-06-05 21:07:48 +02005372 if (!tmp_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07005373 ret = -ENOMEM;
5374 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005375 }
5376
5377 left_path->search_commit_root = 1;
5378 left_path->skip_locking = 1;
5379 right_path->search_commit_root = 1;
5380 right_path->skip_locking = 1;
5381
Alexander Block70698302012-06-05 21:07:48 +02005382 /*
5383 * Strategy: Go to the first items of both trees. Then do
5384 *
5385 * If both trees are at level 0
5386 * Compare keys of current items
5387 * If left < right treat left item as new, advance left tree
5388 * and repeat
5389 * If left > right treat right item as deleted, advance right tree
5390 * and repeat
5391 * If left == right do deep compare of items, treat as changed if
5392 * needed, advance both trees and repeat
5393 * If both trees are at the same level but not at level 0
5394 * Compare keys of current nodes/leafs
5395 * If left < right advance left tree and repeat
5396 * If left > right advance right tree and repeat
5397 * If left == right compare blockptrs of the next nodes/leafs
5398 * If they match advance both trees but stay at the same level
5399 * and repeat
5400 * If they don't match advance both trees while allowing to go
5401 * deeper and repeat
5402 * If tree levels are different
5403 * Advance the tree that needs it and repeat
5404 *
5405 * Advancing a tree means:
5406 * If we are at level 0, try to go to the next slot. If that's not
5407 * possible, go one level up and repeat. Stop when we found a level
5408 * where we could go to the next slot. We may at this point be on a
5409 * node or a leaf.
5410 *
5411 * If we are not at level 0 and not on shared tree blocks, go one
5412 * level deeper.
5413 *
5414 * If we are not at level 0 and on shared tree blocks, go one slot to
5415 * the right if possible or go up and right.
5416 */
5417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005418 down_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005419 left_level = btrfs_header_level(left_root->commit_root);
5420 left_root_level = left_level;
5421 left_path->nodes[left_level] = left_root->commit_root;
5422 extent_buffer_get(left_path->nodes[left_level]);
5423
5424 right_level = btrfs_header_level(right_root->commit_root);
5425 right_root_level = right_level;
5426 right_path->nodes[right_level] = right_root->commit_root;
5427 extent_buffer_get(right_path->nodes[right_level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005428 up_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005429
5430 if (left_level == 0)
5431 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5432 &left_key, left_path->slots[left_level]);
5433 else
5434 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5435 &left_key, left_path->slots[left_level]);
5436 if (right_level == 0)
5437 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5438 &right_key, right_path->slots[right_level]);
5439 else
5440 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5441 &right_key, right_path->slots[right_level]);
5442
5443 left_end_reached = right_end_reached = 0;
5444 advance_left = advance_right = 0;
5445
5446 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005447 if (advance_left && !left_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005448 ret = tree_advance(fs_info, left_path, &left_level,
Alexander Block70698302012-06-05 21:07:48 +02005449 left_root_level,
5450 advance_left != ADVANCE_ONLY_NEXT,
5451 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005452 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005453 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005454 else if (ret < 0)
5455 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005456 advance_left = 0;
5457 }
5458 if (advance_right && !right_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005459 ret = tree_advance(fs_info, right_path, &right_level,
Alexander Block70698302012-06-05 21:07:48 +02005460 right_root_level,
5461 advance_right != ADVANCE_ONLY_NEXT,
5462 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005463 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005464 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005465 else if (ret < 0)
5466 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005467 advance_right = 0;
5468 }
5469
5470 if (left_end_reached && right_end_reached) {
5471 ret = 0;
5472 goto out;
5473 } else if (left_end_reached) {
5474 if (right_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005475 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005476 &right_key,
5477 BTRFS_COMPARE_TREE_DELETED,
5478 ctx);
5479 if (ret < 0)
5480 goto out;
5481 }
5482 advance_right = ADVANCE;
5483 continue;
5484 } else if (right_end_reached) {
5485 if (left_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005486 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005487 &left_key,
5488 BTRFS_COMPARE_TREE_NEW,
5489 ctx);
5490 if (ret < 0)
5491 goto out;
5492 }
5493 advance_left = ADVANCE;
5494 continue;
5495 }
5496
5497 if (left_level == 0 && right_level == 0) {
5498 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5499 if (cmp < 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005500 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005501 &left_key,
5502 BTRFS_COMPARE_TREE_NEW,
5503 ctx);
5504 if (ret < 0)
5505 goto out;
5506 advance_left = ADVANCE;
5507 } else if (cmp > 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005508 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005509 &right_key,
5510 BTRFS_COMPARE_TREE_DELETED,
5511 ctx);
5512 if (ret < 0)
5513 goto out;
5514 advance_right = ADVANCE;
5515 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005516 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005517
Chris Mason74dd17f2012-08-07 16:25:13 -04005518 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005519 ret = tree_compare_item(left_path, right_path,
5520 tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005521 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005522 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005523 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005524 result = BTRFS_COMPARE_TREE_SAME;
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005525 ret = changed_cb(left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005526 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005527 if (ret < 0)
5528 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005529 advance_left = ADVANCE;
5530 advance_right = ADVANCE;
5531 }
5532 } else if (left_level == right_level) {
5533 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5534 if (cmp < 0) {
5535 advance_left = ADVANCE;
5536 } else if (cmp > 0) {
5537 advance_right = ADVANCE;
5538 } else {
5539 left_blockptr = btrfs_node_blockptr(
5540 left_path->nodes[left_level],
5541 left_path->slots[left_level]);
5542 right_blockptr = btrfs_node_blockptr(
5543 right_path->nodes[right_level],
5544 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005545 left_gen = btrfs_node_ptr_generation(
5546 left_path->nodes[left_level],
5547 left_path->slots[left_level]);
5548 right_gen = btrfs_node_ptr_generation(
5549 right_path->nodes[right_level],
5550 right_path->slots[right_level]);
5551 if (left_blockptr == right_blockptr &&
5552 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005553 /*
5554 * As we're on a shared block, don't
5555 * allow to go deeper.
5556 */
5557 advance_left = ADVANCE_ONLY_NEXT;
5558 advance_right = ADVANCE_ONLY_NEXT;
5559 } else {
5560 advance_left = ADVANCE;
5561 advance_right = ADVANCE;
5562 }
5563 }
5564 } else if (left_level < right_level) {
5565 advance_right = ADVANCE;
5566 } else {
5567 advance_left = ADVANCE;
5568 }
5569 }
5570
5571out:
5572 btrfs_free_path(left_path);
5573 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005574 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005575 return ret;
5576}
5577
Chris Mason3f157a22008-06-25 16:01:31 -04005578/*
5579 * this is similar to btrfs_next_leaf, but does not try to preserve
5580 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005581 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005582 *
5583 * 0 is returned if another key is found, < 0 if there are any errors
5584 * and 1 is returned if there are no higher keys in the tree
5585 *
5586 * path->keep_locks should be set to 1 on the search made before
5587 * calling this function.
5588 */
Chris Masone7a84562008-06-25 16:01:31 -04005589int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005590 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005591{
Chris Masone7a84562008-06-25 16:01:31 -04005592 int slot;
5593 struct extent_buffer *c;
5594
Chris Mason934d3752008-12-08 16:43:10 -05005595 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005596 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005597 if (!path->nodes[level])
5598 return 1;
5599
5600 slot = path->slots[level] + 1;
5601 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005602next:
Chris Masone7a84562008-06-25 16:01:31 -04005603 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005604 int ret;
5605 int orig_lowest;
5606 struct btrfs_key cur_key;
5607 if (level + 1 >= BTRFS_MAX_LEVEL ||
5608 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005609 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005610
5611 if (path->locks[level + 1]) {
5612 level++;
5613 continue;
5614 }
5615
5616 slot = btrfs_header_nritems(c) - 1;
5617 if (level == 0)
5618 btrfs_item_key_to_cpu(c, &cur_key, slot);
5619 else
5620 btrfs_node_key_to_cpu(c, &cur_key, slot);
5621
5622 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005623 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005624 path->lowest_level = level;
5625 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5626 0, 0);
5627 path->lowest_level = orig_lowest;
5628 if (ret < 0)
5629 return ret;
5630
5631 c = path->nodes[level];
5632 slot = path->slots[level];
5633 if (ret == 0)
5634 slot++;
5635 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005636 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005637
Chris Masone7a84562008-06-25 16:01:31 -04005638 if (level == 0)
5639 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005640 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005641 u64 gen = btrfs_node_ptr_generation(c, slot);
5642
Chris Mason3f157a22008-06-25 16:01:31 -04005643 if (gen < min_trans) {
5644 slot++;
5645 goto next;
5646 }
Chris Masone7a84562008-06-25 16:01:31 -04005647 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005648 }
Chris Masone7a84562008-06-25 16:01:31 -04005649 return 0;
5650 }
5651 return 1;
5652}
5653
Chris Mason7bb86312007-12-11 09:25:06 -05005654/*
Chris Mason925baed2008-06-25 16:01:30 -04005655 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005656 * returns 0 if it found something or 1 if there are no greater leaves.
5657 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005658 */
Chris Mason234b63a2007-03-13 10:46:10 -04005659int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005660{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005661 return btrfs_next_old_leaf(root, path, 0);
5662}
5663
5664int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5665 u64 time_seq)
5666{
Chris Masond97e63b2007-02-20 16:40:44 -05005667 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005668 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005669 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005670 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005671 struct btrfs_key key;
5672 u32 nritems;
5673 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005674 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005675 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005676
5677 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005678 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005679 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005680
Chris Mason8e73f272009-04-03 10:14:18 -04005681 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5682again:
5683 level = 1;
5684 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005685 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005686 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005687
Chris Masona2135012008-06-25 16:01:30 -04005688 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005689 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005690
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005691 if (time_seq)
5692 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5693 else
5694 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005695 path->keep_locks = 0;
5696
5697 if (ret < 0)
5698 return ret;
5699
Chris Masona2135012008-06-25 16:01:30 -04005700 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005701 /*
5702 * by releasing the path above we dropped all our locks. A balance
5703 * could have added more items next to the key that used to be
5704 * at the very end of the block. So, check again here and
5705 * advance the path if there are now more items available.
5706 */
Chris Masona2135012008-06-25 16:01:30 -04005707 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005708 if (ret == 0)
5709 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005710 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005711 goto done;
5712 }
Liu Bo0b43e042014-06-09 11:04:49 +08005713 /*
5714 * So the above check misses one case:
5715 * - after releasing the path above, someone has removed the item that
5716 * used to be at the very end of the block, and balance between leafs
5717 * gets another one with bigger key.offset to replace it.
5718 *
5719 * This one should be returned as well, or we can get leaf corruption
5720 * later(esp. in __btrfs_drop_extents()).
5721 *
5722 * And a bit more explanation about this check,
5723 * with ret > 0, the key isn't found, the path points to the slot
5724 * where it should be inserted, so the path->slots[0] item must be the
5725 * bigger one.
5726 */
5727 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5728 ret = 0;
5729 goto done;
5730 }
Chris Masond97e63b2007-02-20 16:40:44 -05005731
Chris Masond3977122009-01-05 21:25:51 -05005732 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005733 if (!path->nodes[level]) {
5734 ret = 1;
5735 goto done;
5736 }
Chris Mason5f39d392007-10-15 16:14:19 -04005737
Chris Masond97e63b2007-02-20 16:40:44 -05005738 slot = path->slots[level] + 1;
5739 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005740 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005741 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005742 if (level == BTRFS_MAX_LEVEL) {
5743 ret = 1;
5744 goto done;
5745 }
Chris Masond97e63b2007-02-20 16:40:44 -05005746 continue;
5747 }
Chris Mason5f39d392007-10-15 16:14:19 -04005748
Chris Mason925baed2008-06-25 16:01:30 -04005749 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005750 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005751 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005752 }
Chris Mason5f39d392007-10-15 16:14:19 -04005753
Chris Mason8e73f272009-04-03 10:14:18 -04005754 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005755 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005756 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005757 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005758 if (ret == -EAGAIN)
5759 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005760
Chris Mason76a05b32009-05-14 13:24:30 -04005761 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005762 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005763 goto done;
5764 }
5765
Chris Mason5cd57b22008-06-25 16:01:30 -04005766 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005767 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005768 if (!ret && time_seq) {
5769 /*
5770 * If we don't get the lock, we may be racing
5771 * with push_leaf_left, holding that lock while
5772 * itself waiting for the leaf we've currently
5773 * locked. To solve this situation, we give up
5774 * on our lock and cycle.
5775 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005776 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005777 btrfs_release_path(path);
5778 cond_resched();
5779 goto again;
5780 }
Chris Mason8e73f272009-04-03 10:14:18 -04005781 if (!ret) {
5782 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005783 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005784 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005785 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005786 }
Chris Mason31533fb2011-07-26 16:01:59 -04005787 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005788 }
Chris Masond97e63b2007-02-20 16:40:44 -05005789 break;
5790 }
5791 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005792 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005793 level--;
5794 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005795 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005796 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005797
Chris Mason5f39d392007-10-15 16:14:19 -04005798 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005799 path->nodes[level] = next;
5800 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005801 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005802 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005803 if (!level)
5804 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005805
Liu Bod07b8522017-01-30 12:23:42 -08005806 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005807 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005808 if (ret == -EAGAIN)
5809 goto again;
5810
Chris Mason76a05b32009-05-14 13:24:30 -04005811 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005812 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005813 goto done;
5814 }
5815
Chris Mason5cd57b22008-06-25 16:01:30 -04005816 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005817 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005818 if (!ret) {
5819 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005820 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005821 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005822 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005823 }
Chris Mason31533fb2011-07-26 16:01:59 -04005824 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005825 }
Chris Masond97e63b2007-02-20 16:40:44 -05005826 }
Chris Mason8e73f272009-04-03 10:14:18 -04005827 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005828done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005829 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005830 path->leave_spinning = old_spinning;
5831 if (!old_spinning)
5832 btrfs_set_path_blocking(path);
5833
5834 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005835}
Chris Mason0b86a832008-03-24 15:01:56 -04005836
Chris Mason3f157a22008-06-25 16:01:31 -04005837/*
5838 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5839 * searching until it gets past min_objectid or finds an item of 'type'
5840 *
5841 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5842 */
Chris Mason0b86a832008-03-24 15:01:56 -04005843int btrfs_previous_item(struct btrfs_root *root,
5844 struct btrfs_path *path, u64 min_objectid,
5845 int type)
5846{
5847 struct btrfs_key found_key;
5848 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005849 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005850 int ret;
5851
Chris Masond3977122009-01-05 21:25:51 -05005852 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005853 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005854 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005855 ret = btrfs_prev_leaf(root, path);
5856 if (ret != 0)
5857 return ret;
5858 } else {
5859 path->slots[0]--;
5860 }
5861 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005862 nritems = btrfs_header_nritems(leaf);
5863 if (nritems == 0)
5864 return 1;
5865 if (path->slots[0] == nritems)
5866 path->slots[0]--;
5867
Chris Mason0b86a832008-03-24 15:01:56 -04005868 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005869 if (found_key.objectid < min_objectid)
5870 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005871 if (found_key.type == type)
5872 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005873 if (found_key.objectid == min_objectid &&
5874 found_key.type < type)
5875 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005876 }
5877 return 1;
5878}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005879
5880/*
5881 * search in extent tree to find a previous Metadata/Data extent item with
5882 * min objecitd.
5883 *
5884 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5885 */
5886int btrfs_previous_extent_item(struct btrfs_root *root,
5887 struct btrfs_path *path, u64 min_objectid)
5888{
5889 struct btrfs_key found_key;
5890 struct extent_buffer *leaf;
5891 u32 nritems;
5892 int ret;
5893
5894 while (1) {
5895 if (path->slots[0] == 0) {
5896 btrfs_set_path_blocking(path);
5897 ret = btrfs_prev_leaf(root, path);
5898 if (ret != 0)
5899 return ret;
5900 } else {
5901 path->slots[0]--;
5902 }
5903 leaf = path->nodes[0];
5904 nritems = btrfs_header_nritems(leaf);
5905 if (nritems == 0)
5906 return 1;
5907 if (path->slots[0] == nritems)
5908 path->slots[0]--;
5909
5910 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5911 if (found_key.objectid < min_objectid)
5912 break;
5913 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5914 found_key.type == BTRFS_METADATA_ITEM_KEY)
5915 return 0;
5916 if (found_key.objectid == min_objectid &&
5917 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5918 break;
5919 }
5920 return 1;
5921}