blob: 1e6a3281befe37444750ab87344f7356c1274a38 [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 Sterba3ac6de12018-03-05 15:00:37 +0100840static noinline void tree_mod_log_set_node_key(struct extent_buffer *eb,
841 int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200842{
843 int ret;
844
David Sterbae09c2ef2018-03-05 15:09:03 +0100845 ret = tree_mod_log_insert_key(eb, slot, MOD_LOG_KEY_REPLACE,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400846 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200847 BUG_ON(ret < 0);
848}
849
David Sterbadb7279a2018-03-05 15:14:25 +0100850static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200851{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000852 struct tree_mod_elem **tm_list = NULL;
853 int nritems = 0;
854 int i;
855 int ret = 0;
856
857 if (btrfs_header_level(eb) == 0)
858 return 0;
859
David Sterbadb7279a2018-03-05 15:14:25 +0100860 if (!tree_mod_need_log(eb->fs_info, NULL))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000861 return 0;
862
863 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100864 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000865 if (!tm_list)
866 return -ENOMEM;
867
868 for (i = 0; i < nritems; i++) {
869 tm_list[i] = alloc_tree_mod_elem(eb, i,
870 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
871 if (!tm_list[i]) {
872 ret = -ENOMEM;
873 goto free_tms;
874 }
875 }
876
David Sterbadb7279a2018-03-05 15:14:25 +0100877 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000878 goto free_tms;
879
David Sterbadb7279a2018-03-05 15:14:25 +0100880 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
David Sterbab1a09f12018-03-05 15:43:41 +0100881 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000882 if (ret)
883 goto free_tms;
884 kfree(tm_list);
885
886 return 0;
887
888free_tms:
889 for (i = 0; i < nritems; i++)
890 kfree(tm_list[i]);
891 kfree(tm_list);
892
893 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200894}
895
Jan Schmidt097b8a72012-06-21 11:08:04 +0200896static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200897tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000898 struct extent_buffer *new_root_node,
899 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200900{
901 int ret;
David Sterba95b757c2018-03-05 15:22:30 +0100902 ret = tree_mod_log_insert_root(root->node, new_root_node, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200903 BUG_ON(ret < 0);
904}
905
Chris Masond352ac62008-09-29 15:18:18 -0400906/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 * check if the tree block can be shared by multiple trees
908 */
909int btrfs_block_can_be_shared(struct btrfs_root *root,
910 struct extent_buffer *buf)
911{
912 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400913 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 * are never shared. If a block was allocated after the last
915 * snapshot and the block was not allocated by tree relocation,
916 * we know the block is not shared.
917 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800918 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400919 buf != root->node && buf != root->commit_root &&
920 (btrfs_header_generation(buf) <=
921 btrfs_root_last_snapshot(&root->root_item) ||
922 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
923 return 1;
924#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800925 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
927 return 1;
928#endif
929 return 0;
930}
931
932static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400935 struct extent_buffer *cow,
936 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400938 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400939 u64 refs;
940 u64 owner;
941 u64 flags;
942 u64 new_flags = 0;
943 int ret;
944
945 /*
946 * Backrefs update rules:
947 *
948 * Always use full backrefs for extent pointers in tree block
949 * allocated by tree relocation.
950 *
951 * If a shared tree block is no longer referenced by its owner
952 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
953 * use full backrefs for extent pointers in tree block.
954 *
955 * If a tree block is been relocating
956 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
957 * use full backrefs for extent pointers in tree block.
958 * The reason for this is some operations (such as drop tree)
959 * are only allowed for blocks use full backrefs.
960 */
961
962 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400963 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500964 btrfs_header_level(buf), 1,
965 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700966 if (ret)
967 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700968 if (refs == 0) {
969 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400970 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700971 return ret;
972 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 } else {
974 refs = 1;
975 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
976 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
977 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
978 else
979 flags = 0;
980 }
981
982 owner = btrfs_header_owner(buf);
983 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
984 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
985
986 if (refs > 1) {
987 if ((owner == root->root_key.objectid ||
988 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
989 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700990 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500991 if (ret)
992 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400993
994 if (root->root_key.objectid ==
995 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700996 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500997 if (ret)
998 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700999 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001000 if (ret)
1001 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 }
1003 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1004 } else {
1005
1006 if (root->root_key.objectid ==
1007 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001008 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001009 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001010 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001011 if (ret)
1012 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 }
1014 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001015 int level = btrfs_header_level(buf);
1016
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001017 ret = btrfs_set_disk_extent_flags(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 buf->start,
1019 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001020 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001021 if (ret)
1022 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001023 }
1024 } else {
1025 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1026 if (root->root_key.objectid ==
1027 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001028 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001029 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001030 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001031 if (ret)
1032 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -07001033 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -05001034 if (ret)
1035 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036 }
David Sterba7c302b42017-02-10 18:47:57 +01001037 clean_tree_block(fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001038 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001039 }
1040 return 0;
1041}
1042
1043/*
Chris Masond3977122009-01-05 21:25:51 -05001044 * does the dirty work in cow of a single block. The parent block (if
1045 * supplied) is updated to point to the new cow copy. The new buffer is marked
1046 * dirty and returned locked. If you modify the block it needs to be marked
1047 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001048 *
1049 * search_start -- an allocation hint for the new block
1050 *
Chris Masond3977122009-01-05 21:25:51 -05001051 * empty_size -- a hint that you plan on doing more cow. This is the size in
1052 * bytes the allocator should try to find free next to the block it returns.
1053 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001054 */
Chris Masond3977122009-01-05 21:25:51 -05001055static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001056 struct btrfs_root *root,
1057 struct extent_buffer *buf,
1058 struct extent_buffer *parent, int parent_slot,
1059 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001060 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001061{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001062 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001063 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001064 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001065 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001066 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001067 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001068 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001069
Chris Mason925baed2008-06-25 16:01:30 -04001070 if (*cow_ret == buf)
1071 unlock_orig = 1;
1072
Chris Masonb9447ef2009-03-09 11:45:38 -04001073 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001074
Miao Xie27cdeb72014-04-02 19:51:05 +08001075 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001076 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +08001077 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1078 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001079
Chris Mason7bb86312007-12-11 09:25:06 -05001080 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 if (level == 0)
1083 btrfs_item_key(buf, &disk_key, 0);
1084 else
1085 btrfs_node_key(buf, &disk_key, 0);
1086
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001087 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1088 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001089
David Sterba4d75f8a2014-06-15 01:54:12 +02001090 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1091 root->root_key.objectid, &disk_key, level,
1092 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001093 if (IS_ERR(cow))
1094 return PTR_ERR(cow);
1095
Chris Masonb4ce94d2009-02-04 09:25:08 -05001096 /* cow is set to blocking by btrfs_init_new_buffer */
1097
David Sterba58e80122016-11-08 18:30:31 +01001098 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001099 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001100 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001101 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1102 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1103 BTRFS_HEADER_FLAG_RELOC);
1104 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1105 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1106 else
1107 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001108
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001109 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05001110
Mark Fashehbe1a5562011-08-08 13:20:18 -07001111 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001112 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001113 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001114 return ret;
1115 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001116
Miao Xie27cdeb72014-04-02 19:51:05 +08001117 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001118 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001119 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001120 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001121 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001122 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001123 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001124
Chris Mason6702ed42007-08-07 16:15:09 -04001125 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001126 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001127 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1128 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1129 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001130
Chris Mason5f39d392007-10-15 16:14:19 -04001131 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001132 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001133 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001134
Yan, Zhengf0486c62010-05-16 10:46:25 -04001135 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001136 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001137 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001138 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001139 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140 WARN_ON(trans->transid != btrfs_header_generation(parent));
David Sterbae09c2ef2018-03-05 15:09:03 +01001141 tree_mod_log_insert_key(parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001142 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001143 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001144 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001145 btrfs_set_node_ptr_generation(parent, parent_slot,
1146 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001147 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001148 if (last_ref) {
David Sterbadb7279a2018-03-05 15:14:25 +01001149 ret = tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001150 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001151 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001152 return ret;
1153 }
1154 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001155 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001156 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001157 }
Chris Mason925baed2008-06-25 16:01:30 -04001158 if (unlock_orig)
1159 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001160 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001161 btrfs_mark_buffer_dirty(cow);
1162 *cow_ret = cow;
1163 return 0;
1164}
1165
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001166/*
1167 * returns the logical address of the oldest predecessor of the given root.
1168 * entries older than time_seq are ignored.
1169 */
David Sterbabcd24da2018-03-05 15:33:18 +01001170static struct tree_mod_elem *__tree_mod_log_oldest_root(
1171 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001172{
1173 struct tree_mod_elem *tm;
1174 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001175 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001176 int looped = 0;
1177
1178 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001179 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001180
1181 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301182 * the very last operation that's logged for a root is the
1183 * replacement operation (if it is replaced at all). this has
1184 * the logical address of the *new* root, making it the very
1185 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001186 */
1187 while (1) {
David Sterbabcd24da2018-03-05 15:33:18 +01001188 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001189 time_seq);
1190 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001191 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001192 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001193 * if there are no tree operation for the oldest root, we simply
1194 * return it. this should only happen if that (old) root is at
1195 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001196 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001197 if (!tm)
1198 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001199
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001200 /*
1201 * if there's an operation that's not a root replacement, we
1202 * found the oldest version of our root. normally, we'll find a
1203 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1204 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001205 if (tm->op != MOD_LOG_ROOT_REPLACE)
1206 break;
1207
1208 found = tm;
1209 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001210 looped = 1;
1211 }
1212
Jan Schmidta95236d2012-06-05 16:41:24 +02001213 /* if there's no old root to return, return what we found instead */
1214 if (!found)
1215 found = tm;
1216
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001217 return found;
1218}
1219
1220/*
1221 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001222 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001223 * time_seq).
1224 */
1225static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001226__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1227 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001228{
1229 u32 n;
1230 struct rb_node *next;
1231 struct tree_mod_elem *tm = first_tm;
1232 unsigned long o_dst;
1233 unsigned long o_src;
1234 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1235
1236 n = btrfs_header_nritems(eb);
David Sterbab1a09f12018-03-05 15:43:41 +01001237 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001238 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239 /*
1240 * all the operations are recorded with the operator used for
1241 * the modification. as we're going backwards, we do the
1242 * opposite of each operation here.
1243 */
1244 switch (tm->op) {
1245 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1246 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001247 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001248 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001249 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001250 btrfs_set_node_key(eb, &tm->key, tm->slot);
1251 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1252 btrfs_set_node_ptr_generation(eb, tm->slot,
1253 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001254 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001255 break;
1256 case MOD_LOG_KEY_REPLACE:
1257 BUG_ON(tm->slot >= n);
1258 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);
1262 break;
1263 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001264 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001265 n--;
1266 break;
1267 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001268 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1269 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1270 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001271 tm->move.nr_items * p_size);
1272 break;
1273 case MOD_LOG_ROOT_REPLACE:
1274 /*
1275 * this operation is special. for roots, this must be
1276 * handled explicitly before rewinding.
1277 * for non-roots, this operation may exist if the node
1278 * was a root: root A -> child B; then A gets empty and
1279 * B is promoted to the new root. in the mod log, we'll
1280 * have a root-replace operation for B, a tree block
1281 * that is no root. we simply ignore that operation.
1282 */
1283 break;
1284 }
1285 next = rb_next(&tm->node);
1286 if (!next)
1287 break;
Geliang Tang6b4df8b2016-12-19 22:53:41 +08001288 tm = rb_entry(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301289 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001290 break;
1291 }
David Sterbab1a09f12018-03-05 15:43:41 +01001292 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001293 btrfs_set_header_nritems(eb, n);
1294}
1295
Jan Schmidt47fb0912013-04-13 13:19:55 +00001296/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001297 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001298 * is returned. If rewind operations happen, a fresh buffer is returned. The
1299 * returned buffer is always read-locked. If the returned buffer is not the
1300 * input buffer, the lock on the input buffer is released and the input buffer
1301 * is freed (its refcount is decremented).
1302 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001303static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001304tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1305 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001306{
1307 struct extent_buffer *eb_rewin;
1308 struct tree_mod_elem *tm;
1309
1310 if (!time_seq)
1311 return eb;
1312
1313 if (btrfs_header_level(eb) == 0)
1314 return eb;
1315
1316 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1317 if (!tm)
1318 return eb;
1319
Josef Bacik9ec72672013-08-07 16:57:23 -04001320 btrfs_set_path_blocking(path);
1321 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1322
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001323 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1324 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001325 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001326 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001327 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001328 free_extent_buffer(eb);
1329 return NULL;
1330 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001331 btrfs_set_header_bytenr(eb_rewin, eb->start);
1332 btrfs_set_header_backref_rev(eb_rewin,
1333 btrfs_header_backref_rev(eb));
1334 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001335 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001336 } else {
1337 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001338 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001339 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001340 free_extent_buffer(eb);
1341 return NULL;
1342 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001343 }
1344
Josef Bacik9ec72672013-08-07 16:57:23 -04001345 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1346 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001347 free_extent_buffer(eb);
1348
Jan Schmidt47fb0912013-04-13 13:19:55 +00001349 extent_buffer_get(eb_rewin);
1350 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001351 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001352 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001353 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001354
1355 return eb_rewin;
1356}
1357
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001358/*
1359 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1360 * value. If there are no changes, the current root->root_node is returned. If
1361 * anything changed in between, there's a fresh buffer allocated on which the
1362 * rewind operations are done. In any case, the returned buffer is read locked.
1363 * Returns NULL on error (with no locks held).
1364 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001365static inline struct extent_buffer *
1366get_old_root(struct btrfs_root *root, u64 time_seq)
1367{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001368 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001369 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001370 struct extent_buffer *eb = NULL;
1371 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001372 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001373 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001374 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001375 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001376
Jan Schmidt30b04632013-04-13 13:19:54 +00001377 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001378 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001379 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001380 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001381
Jan Schmidta95236d2012-06-05 16:41:24 +02001382 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1383 old_root = &tm->old_root;
1384 old_generation = tm->generation;
1385 logical = old_root->logical;
1386 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001387 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001388 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001390 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001391 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001392 btrfs_tree_read_unlock(eb_root);
1393 free_extent_buffer(eb_root);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001394 old = read_tree_block(fs_info, logical, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001395 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1396 if (!IS_ERR(old))
1397 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001398 btrfs_warn(fs_info,
1399 "failed to read tree block %llu from get_old_root",
1400 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001401 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001402 eb = btrfs_clone_extent_buffer(old);
1403 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001404 }
1405 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001406 btrfs_tree_read_unlock(eb_root);
1407 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001408 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001409 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001410 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001411 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001412 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001413 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001414 }
1415
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001416 if (!eb)
1417 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001418 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001419 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001420 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001421 btrfs_set_header_bytenr(eb, eb->start);
1422 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001423 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001424 btrfs_set_header_level(eb, old_root->level);
1425 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001426 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001427 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001428 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001429 else
1430 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001431 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001432
1433 return eb;
1434}
1435
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001436int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1437{
1438 struct tree_mod_elem *tm;
1439 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001440 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001441
David Sterbabcd24da2018-03-05 15:33:18 +01001442 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001443 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1444 level = tm->old_root.level;
1445 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001446 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001447 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001448 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001449
1450 return level;
1451}
1452
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001453static inline int should_cow_block(struct btrfs_trans_handle *trans,
1454 struct btrfs_root *root,
1455 struct extent_buffer *buf)
1456{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001457 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001458 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001459
Liu Bof1ebcc72011-11-14 20:48:06 -05001460 /* ensure we can see the force_cow */
1461 smp_rmb();
1462
1463 /*
1464 * We do not need to cow a block if
1465 * 1) this block is not created or changed in this transaction;
1466 * 2) this block does not belong to TREE_RELOC tree;
1467 * 3) the root is not forced COW.
1468 *
1469 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001470 * when we create snapshot during committing the transaction,
Liu Bof1ebcc72011-11-14 20:48:06 -05001471 * after we've finished coping src root, we must COW the shared
1472 * block to ensure the metadata consistency.
1473 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001474 if (btrfs_header_generation(buf) == trans->transid &&
1475 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1476 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001477 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001478 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001479 return 0;
1480 return 1;
1481}
1482
Chris Masond352ac62008-09-29 15:18:18 -04001483/*
1484 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001485 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001486 * once per transaction, as long as it hasn't been written yet
1487 */
Chris Masond3977122009-01-05 21:25:51 -05001488noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001489 struct btrfs_root *root, struct extent_buffer *buf,
1490 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001491 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001492{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001493 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001494 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001495 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001496
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001497 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001498 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001499 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001500 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001501
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001502 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001503 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001504 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001505
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001506 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001507 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001508 *cow_ret = buf;
1509 return 0;
1510 }
Chris Masonc4876852009-02-04 09:24:25 -05001511
Byongho Leeee221842015-12-15 01:42:10 +09001512 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001513
1514 if (parent)
1515 btrfs_set_lock_blocking(parent);
1516 btrfs_set_lock_blocking(buf);
1517
Chris Masonf510cfe2007-10-15 16:14:48 -04001518 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001519 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001520
1521 trace_btrfs_cow_block(root, buf, *cow_ret);
1522
Chris Masonf510cfe2007-10-15 16:14:48 -04001523 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001524}
1525
Chris Masond352ac62008-09-29 15:18:18 -04001526/*
1527 * helper function for defrag to decide if two blocks pointed to by a
1528 * node are actually close by
1529 */
Chris Mason6b800532007-10-15 16:17:34 -04001530static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001531{
Chris Mason6b800532007-10-15 16:17:34 -04001532 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001533 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001534 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001535 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001536 return 0;
1537}
1538
Chris Mason081e9572007-11-06 10:26:24 -05001539/*
1540 * compare two keys in a memcmp fashion
1541 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001542static int comp_keys(const struct btrfs_disk_key *disk,
1543 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001544{
1545 struct btrfs_key k1;
1546
1547 btrfs_disk_key_to_cpu(&k1, disk);
1548
Diego Calleja20736ab2009-07-24 11:06:52 -04001549 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001550}
1551
Josef Bacikf3465ca2008-11-12 14:19:50 -05001552/*
1553 * same as comp_keys only with two btrfs_key's
1554 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001555int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001556{
1557 if (k1->objectid > k2->objectid)
1558 return 1;
1559 if (k1->objectid < k2->objectid)
1560 return -1;
1561 if (k1->type > k2->type)
1562 return 1;
1563 if (k1->type < k2->type)
1564 return -1;
1565 if (k1->offset > k2->offset)
1566 return 1;
1567 if (k1->offset < k2->offset)
1568 return -1;
1569 return 0;
1570}
Chris Mason081e9572007-11-06 10:26:24 -05001571
Chris Masond352ac62008-09-29 15:18:18 -04001572/*
1573 * this is used by the defrag code to go through all the
1574 * leaves pointed to by a node and reallocate them so that
1575 * disk order is close to key order
1576 */
Chris Mason6702ed42007-08-07 16:15:09 -04001577int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001578 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001579 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001580 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001582 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001583 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001584 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001585 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001586 u64 search_start = *last_ret;
1587 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001588 u64 other;
1589 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001590 int end_slot;
1591 int i;
1592 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001593 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001594 int uptodate;
1595 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001596 int progress_passed = 0;
1597 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001598
Chris Mason5708b952007-10-25 15:43:18 -04001599 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001600
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001601 WARN_ON(trans->transaction != fs_info->running_transaction);
1602 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001603
Chris Mason6b800532007-10-15 16:17:34 -04001604 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001605 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001606 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001607
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001608 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001609 return 0;
1610
Chris Masonb4ce94d2009-02-04 09:25:08 -05001611 btrfs_set_lock_blocking(parent);
1612
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001613 for (i = start_slot; i <= end_slot; i++) {
Chris Mason6702ed42007-08-07 16:15:09 -04001614 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001615
Chris Mason081e9572007-11-06 10:26:24 -05001616 btrfs_node_key(parent, &disk_key, i);
1617 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1618 continue;
1619
1620 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001621 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001622 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001623 if (last_block == 0)
1624 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001625
Chris Mason6702ed42007-08-07 16:15:09 -04001626 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001627 other = btrfs_node_blockptr(parent, i - 1);
1628 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001629 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001630 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001631 other = btrfs_node_blockptr(parent, i + 1);
1632 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001633 }
Chris Masone9d0b132007-08-10 14:06:19 -04001634 if (close) {
1635 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001636 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001637 }
Chris Mason6702ed42007-08-07 16:15:09 -04001638
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001639 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001640 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001641 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001642 else
1643 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001644 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001645 if (!cur) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001646 cur = read_tree_block(fs_info, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001647 if (IS_ERR(cur)) {
1648 return PTR_ERR(cur);
1649 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001650 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001651 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001652 }
Chris Mason6b800532007-10-15 16:17:34 -04001653 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001654 err = btrfs_read_buffer(cur, gen);
1655 if (err) {
1656 free_extent_buffer(cur);
1657 return err;
1658 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001659 }
Chris Mason6702ed42007-08-07 16:15:09 -04001660 }
Chris Masone9d0b132007-08-10 14:06:19 -04001661 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001662 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001663
Chris Masone7a84562008-06-25 16:01:31 -04001664 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001665 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001666 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001667 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001668 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001669 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001670 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001671 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001672 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001673 break;
Yan252c38f2007-08-29 09:11:44 -04001674 }
Chris Masone7a84562008-06-25 16:01:31 -04001675 search_start = cur->start;
1676 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001677 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001678 btrfs_tree_unlock(cur);
1679 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001680 }
1681 return err;
1682}
1683
Chris Mason74123bd2007-02-02 11:05:29 -05001684/*
Chris Mason5f39d392007-10-15 16:14:19 -04001685 * search for key in the extent_buffer. The items start at offset p,
1686 * and they are item_size apart. There are 'max' items in p.
1687 *
Chris Mason74123bd2007-02-02 11:05:29 -05001688 * the slot in the array is returned via slot, and it points to
1689 * the place where you would insert key if it is not found in
1690 * the array.
1691 *
1692 * slot may point to max if the key is bigger than all of the keys
1693 */
Chris Masone02119d2008-09-05 16:13:11 -04001694static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001695 unsigned long p, int item_size,
1696 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001697 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001698{
1699 int low = 0;
1700 int high = max;
1701 int mid;
1702 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001703 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001704 struct btrfs_disk_key unaligned;
1705 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001706 char *kaddr = NULL;
1707 unsigned long map_start = 0;
1708 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001709 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001710
Liu Bo5e24e9a2016-06-23 16:32:45 -07001711 if (low > high) {
1712 btrfs_err(eb->fs_info,
1713 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1714 __func__, low, high, eb->start,
1715 btrfs_header_owner(eb), btrfs_header_level(eb));
1716 return -EINVAL;
1717 }
1718
Chris Masond3977122009-01-05 21:25:51 -05001719 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001720 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001721 offset = p + mid * item_size;
1722
Chris Masona6591712011-07-19 12:04:14 -04001723 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001724 (offset + sizeof(struct btrfs_disk_key)) >
1725 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001726
1727 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001728 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001729 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001730
Chris Mason479965d2007-10-15 16:14:27 -04001731 if (!err) {
1732 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1733 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001734 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001735 read_extent_buffer(eb, &unaligned,
1736 offset, sizeof(unaligned));
1737 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001738 } else {
1739 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001740 }
1741
Chris Mason5f39d392007-10-15 16:14:19 -04001742 } else {
1743 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1744 map_start);
1745 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746 ret = comp_keys(tmp, key);
1747
1748 if (ret < 0)
1749 low = mid + 1;
1750 else if (ret > 0)
1751 high = mid;
1752 else {
1753 *slot = mid;
1754 return 0;
1755 }
1756 }
1757 *slot = low;
1758 return 1;
1759}
1760
Chris Mason97571fd2007-02-24 13:39:08 -05001761/*
1762 * simple bin_search frontend that does the right thing for
1763 * leaves vs nodes
1764 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001765int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1766 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001767{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001768 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001769 return generic_bin_search(eb,
1770 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001771 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001772 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001773 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001774 else
Chris Mason5f39d392007-10-15 16:14:19 -04001775 return generic_bin_search(eb,
1776 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001777 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001778 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001779 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001780}
1781
Yan, Zhengf0486c62010-05-16 10:46:25 -04001782static void root_add_used(struct btrfs_root *root, u32 size)
1783{
1784 spin_lock(&root->accounting_lock);
1785 btrfs_set_root_used(&root->root_item,
1786 btrfs_root_used(&root->root_item) + size);
1787 spin_unlock(&root->accounting_lock);
1788}
1789
1790static void root_sub_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
Chris Masond352ac62008-09-29 15:18:18 -04001798/* given a node and slot number, this reads the blocks it points to. The
1799 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001800 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001801static noinline struct extent_buffer *
1802read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1803 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001804{
Chris Masonca7a79a2008-05-12 12:59:19 -04001805 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001806 struct extent_buffer *eb;
1807
Liu Bofb770ae2016-07-05 12:10:14 -07001808 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1809 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001810
1811 BUG_ON(level == 0);
1812
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001813 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001814 btrfs_node_ptr_generation(parent, slot));
Liu Bofb770ae2016-07-05 12:10:14 -07001815 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1816 free_extent_buffer(eb);
1817 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001818 }
1819
1820 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001821}
1822
Chris Masond352ac62008-09-29 15:18:18 -04001823/*
1824 * node level balancing, used to make sure nodes are in proper order for
1825 * item deletion. We balance from the top down, so we have to make sure
1826 * that a deletion won't leave an node completely empty later on.
1827 */
Chris Masone02119d2008-09-05 16:13:11 -04001828static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001829 struct btrfs_root *root,
1830 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001831{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001832 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001833 struct extent_buffer *right = NULL;
1834 struct extent_buffer *mid;
1835 struct extent_buffer *left = NULL;
1836 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001837 int ret = 0;
1838 int wret;
1839 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001840 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001841 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001842
1843 if (level == 0)
1844 return 0;
1845
Chris Mason5f39d392007-10-15 16:14:19 -04001846 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001847
Chris Masonbd681512011-07-16 15:23:14 -04001848 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1849 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001850 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1851
Chris Mason1d4f8a02007-03-13 09:28:32 -04001852 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001853
Li Zefana05a9bb2011-09-06 16:55:34 +08001854 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001855 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001856 pslot = path->slots[level + 1];
1857 }
Chris Masonbb803952007-03-01 12:04:21 -05001858
Chris Mason40689472007-03-17 14:29:23 -04001859 /*
1860 * deal with the case where there is only one pointer in the root
1861 * by promoting the node below to a root
1862 */
Chris Mason5f39d392007-10-15 16:14:19 -04001863 if (!parent) {
1864 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001865
Chris Mason5f39d392007-10-15 16:14:19 -04001866 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001867 return 0;
1868
1869 /* promote the child to a root */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001870 child = read_node_slot(fs_info, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001871 if (IS_ERR(child)) {
1872 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001874 goto enospc;
1875 }
1876
Chris Mason925baed2008-06-25 16:01:30 -04001877 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001878 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001879 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001880 if (ret) {
1881 btrfs_tree_unlock(child);
1882 free_extent_buffer(child);
1883 goto enospc;
1884 }
Yan2f375ab2008-02-01 14:58:07 -05001885
Jan Schmidt90f8d622013-04-13 13:19:53 +00001886 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001887 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001888
Chris Mason0b86a832008-03-24 15:01:56 -04001889 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001890 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001891
Chris Mason925baed2008-06-25 16:01:30 -04001892 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001893 path->nodes[level] = NULL;
David Sterba7c302b42017-02-10 18:47:57 +01001894 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001895 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001896 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001897 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001898
1899 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001900 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001901 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001902 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001903 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001904 }
Chris Mason5f39d392007-10-15 16:14:19 -04001905 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001906 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001907 return 0;
1908
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001909 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001910 if (IS_ERR(left))
1911 left = NULL;
1912
Chris Mason5f39d392007-10-15 16:14:19 -04001913 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001914 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001915 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001916 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001917 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001918 if (wret) {
1919 ret = wret;
1920 goto enospc;
1921 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001922 }
Liu Bofb770ae2016-07-05 12:10:14 -07001923
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001924 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001925 if (IS_ERR(right))
1926 right = NULL;
1927
Chris Mason5f39d392007-10-15 16:14:19 -04001928 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001929 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001930 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001931 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001932 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001933 if (wret) {
1934 ret = wret;
1935 goto enospc;
1936 }
1937 }
1938
1939 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001940 if (left) {
1941 orig_slot += btrfs_header_nritems(left);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001942 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001943 if (wret < 0)
1944 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001945 }
Chris Mason79f95c82007-03-01 15:16:26 -05001946
1947 /*
1948 * then try to empty the right most buffer into the middle
1949 */
Chris Mason5f39d392007-10-15 16:14:19 -04001950 if (right) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001951 wret = push_node_left(trans, fs_info, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001952 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001953 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001954 if (btrfs_header_nritems(right) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001955 clean_tree_block(fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04001956 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001957 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001958 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001959 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001960 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001961 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001962 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001963 struct btrfs_disk_key right_key;
1964 btrfs_node_key(right, &right_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01001965 tree_mod_log_set_node_key(parent, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001966 btrfs_set_node_key(parent, &right_key, pslot + 1);
1967 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001968 }
1969 }
Chris Mason5f39d392007-10-15 16:14:19 -04001970 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001971 /*
1972 * we're not allowed to leave a node with one item in the
1973 * tree during a delete. A deletion from lower in the tree
1974 * could try to delete the only pointer in this node.
1975 * So, pull some keys from the left.
1976 * There has to be a left pointer at this point because
1977 * otherwise we would have pulled some pointers from the
1978 * right
1979 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001980 if (!left) {
1981 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001982 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001983 goto enospc;
1984 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001985 wret = balance_node_right(trans, fs_info, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001986 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001987 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001988 goto enospc;
1989 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001990 if (wret == 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001991 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04001992 if (wret < 0)
1993 ret = wret;
1994 }
Chris Mason79f95c82007-03-01 15:16:26 -05001995 BUG_ON(wret == 1);
1996 }
Chris Mason5f39d392007-10-15 16:14:19 -04001997 if (btrfs_header_nritems(mid) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001998 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001999 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002000 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002001 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002002 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002003 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002004 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002005 } else {
2006 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002007 struct btrfs_disk_key mid_key;
2008 btrfs_node_key(mid, &mid_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01002009 tree_mod_log_set_node_key(parent, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002010 btrfs_set_node_key(parent, &mid_key, pslot);
2011 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002012 }
Chris Masonbb803952007-03-01 12:04:21 -05002013
Chris Mason79f95c82007-03-01 15:16:26 -05002014 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002015 if (left) {
2016 if (btrfs_header_nritems(left) > orig_slot) {
2017 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002018 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002019 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002020 path->slots[level + 1] -= 1;
2021 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002022 if (mid) {
2023 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002024 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002025 }
Chris Masonbb803952007-03-01 12:04:21 -05002026 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002027 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002028 path->slots[level] = orig_slot;
2029 }
2030 }
Chris Mason79f95c82007-03-01 15:16:26 -05002031 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002032 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002033 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002034 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002035enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002036 if (right) {
2037 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002038 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002039 }
2040 if (left) {
2041 if (path->nodes[level] != left)
2042 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002043 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002044 }
Chris Masonbb803952007-03-01 12:04:21 -05002045 return ret;
2046}
2047
Chris Masond352ac62008-09-29 15:18:18 -04002048/* Node balancing for insertion. Here we only split or push nodes around
2049 * when they are completely full. This is also done top down, so we
2050 * have to be pessimistic.
2051 */
Chris Masond3977122009-01-05 21:25:51 -05002052static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002053 struct btrfs_root *root,
2054 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002055{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002056 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002057 struct extent_buffer *right = NULL;
2058 struct extent_buffer *mid;
2059 struct extent_buffer *left = NULL;
2060 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002061 int ret = 0;
2062 int wret;
2063 int pslot;
2064 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002065
2066 if (level == 0)
2067 return 1;
2068
Chris Mason5f39d392007-10-15 16:14:19 -04002069 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002070 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002071
Li Zefana05a9bb2011-09-06 16:55:34 +08002072 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002073 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002074 pslot = path->slots[level + 1];
2075 }
Chris Masone66f7092007-04-20 13:16:02 -04002076
Chris Mason5f39d392007-10-15 16:14:19 -04002077 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002078 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002079
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002080 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002081 if (IS_ERR(left))
2082 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002083
2084 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002085 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002086 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002087
2088 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002089 btrfs_set_lock_blocking(left);
2090
Chris Mason5f39d392007-10-15 16:14:19 -04002091 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002092 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002093 wret = 1;
2094 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002095 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002096 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002097 if (ret)
2098 wret = 1;
2099 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002100 wret = push_node_left(trans, fs_info,
Chris Mason971a1f62008-04-24 10:54:32 -04002101 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002102 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002103 }
Chris Masone66f7092007-04-20 13:16:02 -04002104 if (wret < 0)
2105 ret = wret;
2106 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002107 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002108 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002109 btrfs_node_key(mid, &disk_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01002110 tree_mod_log_set_node_key(parent, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 btrfs_set_node_key(parent, &disk_key, pslot);
2112 btrfs_mark_buffer_dirty(parent);
2113 if (btrfs_header_nritems(left) > orig_slot) {
2114 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002115 path->slots[level + 1] -= 1;
2116 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002117 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002118 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002119 } else {
2120 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002121 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002122 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002123 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002124 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002125 }
Chris Masone66f7092007-04-20 13:16:02 -04002126 return 0;
2127 }
Chris Mason925baed2008-06-25 16:01:30 -04002128 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002129 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002130 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002131 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002132 if (IS_ERR(right))
2133 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002134
2135 /*
2136 * then try to empty the right most buffer into the middle
2137 */
Chris Mason5f39d392007-10-15 16:14:19 -04002138 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002139 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002140
Chris Mason925baed2008-06-25 16:01:30 -04002141 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002142 btrfs_set_lock_blocking(right);
2143
Chris Mason5f39d392007-10-15 16:14:19 -04002144 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002145 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002146 wret = 1;
2147 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002148 ret = btrfs_cow_block(trans, root, right,
2149 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002150 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002151 if (ret)
2152 wret = 1;
2153 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002154 wret = balance_node_right(trans, fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04002155 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002156 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002157 }
Chris Masone66f7092007-04-20 13:16:02 -04002158 if (wret < 0)
2159 ret = wret;
2160 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002161 struct btrfs_disk_key disk_key;
2162
2163 btrfs_node_key(right, &disk_key, 0);
David Sterba3ac6de12018-03-05 15:00:37 +01002164 tree_mod_log_set_node_key(parent, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2166 btrfs_mark_buffer_dirty(parent);
2167
2168 if (btrfs_header_nritems(mid) <= orig_slot) {
2169 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002170 path->slots[level + 1] += 1;
2171 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002172 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002173 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002174 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002175 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002176 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002177 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002178 }
Chris Masone66f7092007-04-20 13:16:02 -04002179 return 0;
2180 }
Chris Mason925baed2008-06-25 16:01:30 -04002181 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002182 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002183 }
Chris Masone66f7092007-04-20 13:16:02 -04002184 return 1;
2185}
2186
Chris Mason74123bd2007-02-02 11:05:29 -05002187/*
Chris Masond352ac62008-09-29 15:18:18 -04002188 * readahead one full node of leaves, finding things that are close
2189 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002190 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002191static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002192 struct btrfs_path *path,
2193 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002194{
Chris Mason5f39d392007-10-15 16:14:19 -04002195 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002196 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002197 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002198 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002199 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002200 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002201 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002202 u32 nr;
2203 u32 blocksize;
2204 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002205
Chris Masona6b6e752007-10-15 16:22:39 -04002206 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002207 return;
2208
Chris Mason6702ed42007-08-07 16:15:09 -04002209 if (!path->nodes[level])
2210 return;
2211
Chris Mason5f39d392007-10-15 16:14:19 -04002212 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002213
Chris Mason3c69fae2007-08-07 15:52:22 -04002214 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002215 blocksize = fs_info->nodesize;
2216 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002217 if (eb) {
2218 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002219 return;
2220 }
2221
Chris Masona7175312009-01-22 09:23:10 -05002222 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002223
Chris Mason5f39d392007-10-15 16:14:19 -04002224 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002225 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002226
Chris Masond3977122009-01-05 21:25:51 -05002227 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002228 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002229 if (nr == 0)
2230 break;
2231 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002232 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002233 nr++;
2234 if (nr >= nritems)
2235 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002236 }
David Sterbae4058b52015-11-27 16:31:35 +01002237 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002238 btrfs_node_key(node, &disk_key, nr);
2239 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2240 break;
2241 }
Chris Mason6b800532007-10-15 16:17:34 -04002242 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002243 if ((search <= target && target - search <= 65536) ||
2244 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002245 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002246 nread += blocksize;
2247 }
2248 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002249 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002250 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002251 }
2252}
Chris Mason925baed2008-06-25 16:01:30 -04002253
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002254static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002255 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002256{
2257 int slot;
2258 int nritems;
2259 struct extent_buffer *parent;
2260 struct extent_buffer *eb;
2261 u64 gen;
2262 u64 block1 = 0;
2263 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002264
Chris Mason8c594ea2009-04-20 15:50:10 -04002265 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002266 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002267 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002268
2269 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002270 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002271
2272 if (slot > 0) {
2273 block1 = btrfs_node_blockptr(parent, slot - 1);
2274 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002275 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002276 /*
2277 * if we get -eagain from btrfs_buffer_uptodate, we
2278 * don't want to return eagain here. That will loop
2279 * forever
2280 */
2281 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002282 block1 = 0;
2283 free_extent_buffer(eb);
2284 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002285 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002286 block2 = btrfs_node_blockptr(parent, slot + 1);
2287 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002288 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002289 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002290 block2 = 0;
2291 free_extent_buffer(eb);
2292 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002293
Josef Bacik0b088512013-06-17 14:23:02 -04002294 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002295 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002296 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002297 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002298}
2299
2300
2301/*
Chris Masond3977122009-01-05 21:25:51 -05002302 * when we walk down the tree, it is usually safe to unlock the higher layers
2303 * in the tree. The exceptions are when our path goes through slot 0, because
2304 * operations on the tree might require changing key pointers higher up in the
2305 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002306 *
Chris Masond3977122009-01-05 21:25:51 -05002307 * callers might also have set path->keep_locks, which tells this code to keep
2308 * the lock if the path points to the last slot in the block. This is part of
2309 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002310 *
Chris Masond3977122009-01-05 21:25:51 -05002311 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2312 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002313 */
Chris Masone02119d2008-09-05 16:13:11 -04002314static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002315 int lowest_unlock, int min_write_lock_level,
2316 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002317{
2318 int i;
2319 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002320 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002321 struct extent_buffer *t;
2322
2323 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2324 if (!path->nodes[i])
2325 break;
2326 if (!path->locks[i])
2327 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002328 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002329 skip_level = i + 1;
2330 continue;
2331 }
Chris Mason051e1b92008-06-25 16:01:30 -04002332 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002333 u32 nritems;
2334 t = path->nodes[i];
2335 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002336 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002337 skip_level = i + 1;
2338 continue;
2339 }
2340 }
Chris Mason051e1b92008-06-25 16:01:30 -04002341 if (skip_level < i && i >= lowest_unlock)
2342 no_skips = 1;
2343
Chris Mason925baed2008-06-25 16:01:30 -04002344 t = path->nodes[i];
2345 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002346 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002347 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002348 if (write_lock_level &&
2349 i > min_write_lock_level &&
2350 i <= *write_lock_level) {
2351 *write_lock_level = i - 1;
2352 }
Chris Mason925baed2008-06-25 16:01:30 -04002353 }
2354 }
2355}
2356
Chris Mason3c69fae2007-08-07 15:52:22 -04002357/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002358 * This releases any locks held in the path starting at level and
2359 * going all the way up to the root.
2360 *
2361 * btrfs_search_slot will keep the lock held on higher nodes in a few
2362 * corner cases, such as COW of the block at slot zero in the node. This
2363 * ignores those rules, and it should only be called when there are no
2364 * more updates to be done higher up in the tree.
2365 */
2366noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2367{
2368 int i;
2369
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002370 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002371 return;
2372
2373 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2374 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002375 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002376 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002377 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002378 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002379 path->locks[i] = 0;
2380 }
2381}
2382
2383/*
Chris Masonc8c42862009-04-03 10:14:18 -04002384 * helper function for btrfs_search_slot. The goal is to find a block
2385 * in cache without setting the path to blocking. If we find the block
2386 * we return zero and the path is unchanged.
2387 *
2388 * If we can't find the block, we set the path blocking and do some
2389 * reada. -EAGAIN is returned and the search must be repeated.
2390 */
2391static int
Liu Bod07b8522017-01-30 12:23:42 -08002392read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2393 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002394 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002395{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002396 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002397 u64 blocknr;
2398 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002399 struct extent_buffer *b = *eb_ret;
2400 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002401 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002402
2403 blocknr = btrfs_node_blockptr(b, slot);
2404 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002405
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002406 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002407 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002408 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002409 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2410 *eb_ret = tmp;
2411 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002412 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002413
2414 /* the pages were up to date, but we failed
2415 * the generation number check. Do a full
2416 * read for the generation number that is correct.
2417 * We must do this without dropping locks so
2418 * we can trust our generation number
2419 */
2420 btrfs_set_path_blocking(p);
2421
2422 /* now we're allowed to do a blocking uptodate check */
2423 ret = btrfs_read_buffer(tmp, gen);
2424 if (!ret) {
2425 *eb_ret = tmp;
2426 return 0;
2427 }
2428 free_extent_buffer(tmp);
2429 btrfs_release_path(p);
2430 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002431 }
2432
2433 /*
2434 * reduce lock contention at high levels
2435 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002436 * we read. Don't release the lock on the current
2437 * level because we need to walk this node to figure
2438 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002439 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002440 btrfs_unlock_up_safe(p, level + 1);
2441 btrfs_set_path_blocking(p);
2442
Chris Masoncb449212010-10-24 11:01:27 -04002443 free_extent_buffer(tmp);
David Sterbae4058b52015-11-27 16:31:35 +01002444 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002445 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002446
David Sterbab3b4aa72011-04-21 01:20:15 +02002447 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002448
2449 ret = -EAGAIN;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002450 tmp = read_tree_block(fs_info, blocknr, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08002451 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002452 /*
2453 * If the read above didn't mark this buffer up to date,
2454 * it will never end up being up to date. Set ret to EIO now
2455 * and give up so that our caller doesn't loop forever
2456 * on our EAGAINs.
2457 */
Chris Masonb9fab912012-05-06 07:23:47 -04002458 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002459 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002460 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002461 } else {
2462 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002463 }
2464 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002465}
2466
2467/*
2468 * helper function for btrfs_search_slot. This does all of the checks
2469 * for node-level blocks and does any balancing required based on
2470 * the ins_len.
2471 *
2472 * If no extra work was required, zero is returned. If we had to
2473 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2474 * start over
2475 */
2476static int
2477setup_nodes_for_search(struct btrfs_trans_handle *trans,
2478 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002479 struct extent_buffer *b, int level, int ins_len,
2480 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002481{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002482 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002483 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002484
Chris Masonc8c42862009-04-03 10:14:18 -04002485 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002487 int sret;
2488
Chris Masonbd681512011-07-16 15:23:14 -04002489 if (*write_lock_level < level + 1) {
2490 *write_lock_level = level + 1;
2491 btrfs_release_path(p);
2492 goto again;
2493 }
2494
Chris Masonc8c42862009-04-03 10:14:18 -04002495 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002496 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002497 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002498 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002499
2500 BUG_ON(sret > 0);
2501 if (sret) {
2502 ret = sret;
2503 goto done;
2504 }
2505 b = p->nodes[level];
2506 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002507 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002508 int sret;
2509
Chris Masonbd681512011-07-16 15:23:14 -04002510 if (*write_lock_level < level + 1) {
2511 *write_lock_level = level + 1;
2512 btrfs_release_path(p);
2513 goto again;
2514 }
2515
Chris Masonc8c42862009-04-03 10:14:18 -04002516 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002517 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002518 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002519 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002520
2521 if (sret) {
2522 ret = sret;
2523 goto done;
2524 }
2525 b = p->nodes[level];
2526 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002527 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002528 goto again;
2529 }
2530 BUG_ON(btrfs_header_nritems(b) == 1);
2531 }
2532 return 0;
2533
2534again:
2535 ret = -EAGAIN;
2536done:
2537 return ret;
2538}
2539
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002540static void key_search_validate(struct extent_buffer *b,
Omar Sandoval310712b2017-01-17 23:24:37 -08002541 const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002542 int level)
2543{
2544#ifdef CONFIG_BTRFS_ASSERT
2545 struct btrfs_disk_key disk_key;
2546
2547 btrfs_cpu_key_to_disk(&disk_key, key);
2548
2549 if (level == 0)
2550 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2551 offsetof(struct btrfs_leaf, items[0].key),
2552 sizeof(disk_key)));
2553 else
2554 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2555 offsetof(struct btrfs_node, ptrs[0].key),
2556 sizeof(disk_key)));
2557#endif
2558}
2559
Omar Sandoval310712b2017-01-17 23:24:37 -08002560static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002561 int level, int *prev_cmp, int *slot)
2562{
2563 if (*prev_cmp != 0) {
Nikolay Borisova74b35e2017-12-08 16:27:43 +02002564 *prev_cmp = btrfs_bin_search(b, key, level, slot);
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002565 return *prev_cmp;
2566 }
2567
2568 key_search_validate(b, key, level);
2569 *slot = 0;
2570
2571 return 0;
2572}
2573
David Sterba381cf652015-01-02 18:45:16 +01002574int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002575 u64 iobjectid, u64 ioff, u8 key_type,
2576 struct btrfs_key *found_key)
2577{
2578 int ret;
2579 struct btrfs_key key;
2580 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002581
2582 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002583 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002584
2585 key.type = key_type;
2586 key.objectid = iobjectid;
2587 key.offset = ioff;
2588
2589 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002590 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002591 return ret;
2592
2593 eb = path->nodes[0];
2594 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2595 ret = btrfs_next_leaf(fs_root, path);
2596 if (ret)
2597 return ret;
2598 eb = path->nodes[0];
2599 }
2600
2601 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2602 if (found_key->type != key.type ||
2603 found_key->objectid != key.objectid)
2604 return 1;
2605
2606 return 0;
2607}
2608
Chris Masonc8c42862009-04-03 10:14:18 -04002609/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002610 * btrfs_search_slot - look for a key in a tree and perform necessary
2611 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002612 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002613 * @trans: Handle of transaction, used when modifying the tree
2614 * @p: Holds all btree nodes along the search path
2615 * @root: The root node of the tree
2616 * @key: The key we are looking for
2617 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2618 * deletions it's -1. 0 for plain searches
2619 * @cow: boolean should CoW operations be performed. Must always be 1
2620 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002621 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002622 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2623 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2624 *
2625 * If @key is found, 0 is returned and you can find the item in the leaf level
2626 * of the path (level 0)
2627 *
2628 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2629 * points to the slot where it should be inserted
2630 *
2631 * If an error is encountered while searching the tree a negative error number
2632 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002633 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002634int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2635 const struct btrfs_key *key, struct btrfs_path *p,
2636 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002637{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002638 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002639 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002640 int slot;
2641 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002642 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002643 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002644 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002645 int root_lock;
2646 /* everything at write_lock_level or lower must be write locked */
2647 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002648 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002649 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002650 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002651
Chris Mason6702ed42007-08-07 16:15:09 -04002652 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002653 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002654 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002655 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002656
Chris Masonbd681512011-07-16 15:23:14 -04002657 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002658 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002659
Chris Masonbd681512011-07-16 15:23:14 -04002660 /* when we are removing items, we might have to go up to level
2661 * two as we update tree pointers Make sure we keep write
2662 * for those levels as well
2663 */
2664 write_lock_level = 2;
2665 } else if (ins_len > 0) {
2666 /*
2667 * for inserting items, make sure we have a write lock on
2668 * level 1 so we can update keys
2669 */
2670 write_lock_level = 1;
2671 }
2672
2673 if (!cow)
2674 write_lock_level = -1;
2675
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002676 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002677 write_lock_level = BTRFS_MAX_LEVEL;
2678
Chris Masonf7c79f32012-03-19 15:54:38 -04002679 min_write_lock_level = write_lock_level;
2680
Chris Masonbb803952007-03-01 12:04:21 -05002681again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002682 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002683 /*
2684 * we try very hard to do read locks on the root
2685 */
2686 root_lock = BTRFS_READ_LOCK;
2687 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002688 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002689 /*
2690 * the commit roots are read only
2691 * so we always do read locks
2692 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002693 if (p->need_commit_sem)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002694 down_read(&fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002695 b = root->commit_root;
2696 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002697 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002698 if (p->need_commit_sem)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002699 up_read(&fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002700 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002701 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002702 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002703 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002704 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002705 level = btrfs_header_level(b);
2706 } else {
2707 /* we don't know the level of the root node
2708 * until we actually have it read locked
2709 */
2710 b = btrfs_read_lock_root_node(root);
2711 level = btrfs_header_level(b);
2712 if (level <= write_lock_level) {
2713 /* whoops, must trade for write lock */
2714 btrfs_tree_read_unlock(b);
2715 free_extent_buffer(b);
2716 b = btrfs_lock_root_node(root);
2717 root_lock = BTRFS_WRITE_LOCK;
2718
2719 /* the level might have changed, check again */
2720 level = btrfs_header_level(b);
2721 }
2722 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002723 }
Chris Masonbd681512011-07-16 15:23:14 -04002724 p->nodes[level] = b;
2725 if (!p->skip_locking)
2726 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002727
Chris Masoneb60cea2007-02-02 09:18:22 -05002728 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002729 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002730
2731 /*
2732 * setup the path here so we can release it under lock
2733 * contention with the cow code
2734 */
Chris Mason02217ed2007-03-02 16:08:05 -05002735 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002736 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2737
Chris Masonc8c42862009-04-03 10:14:18 -04002738 /*
2739 * if we don't really need to cow this block
2740 * then we don't want to set the path blocking,
2741 * so we test it here
2742 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002743 if (!should_cow_block(trans, root, b)) {
2744 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002745 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002746 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002747
Chris Masonbd681512011-07-16 15:23:14 -04002748 /*
2749 * must have write locks on this node and the
2750 * parent
2751 */
Josef Bacik5124e002012-11-07 13:44:13 -05002752 if (level > write_lock_level ||
2753 (level + 1 > write_lock_level &&
2754 level + 1 < BTRFS_MAX_LEVEL &&
2755 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002756 write_lock_level = level + 1;
2757 btrfs_release_path(p);
2758 goto again;
2759 }
2760
Filipe Manana160f4082014-07-28 19:37:17 +01002761 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002762 if (last_level)
2763 err = btrfs_cow_block(trans, root, b, NULL, 0,
2764 &b);
2765 else
2766 err = btrfs_cow_block(trans, root, b,
2767 p->nodes[level + 1],
2768 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002769 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002770 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002771 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002772 }
Chris Mason02217ed2007-03-02 16:08:05 -05002773 }
Chris Mason65b51a02008-08-01 15:11:20 -04002774cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002775 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002776 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002777
2778 /*
2779 * we have a lock on b and as long as we aren't changing
2780 * the tree, there is no way to for the items in b to change.
2781 * It is safe to drop the lock on our parent before we
2782 * go through the expensive btree search on b.
2783 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002784 * If we're inserting or deleting (ins_len != 0), then we might
2785 * be changing slot zero, which may require changing the parent.
2786 * So, we can't drop the lock until after we know which slot
2787 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002788 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002789 if (!ins_len && !p->keep_locks) {
2790 int u = level + 1;
2791
2792 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2793 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2794 p->locks[u] = 0;
2795 }
2796 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002797
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002798 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002799 if (ret < 0)
2800 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002801
Chris Mason5f39d392007-10-15 16:14:19 -04002802 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002803 int dec = 0;
2804 if (ret && slot > 0) {
2805 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002806 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002807 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002808 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002809 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002810 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002811 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002812 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002813 if (err) {
2814 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002815 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002816 }
Chris Masonc8c42862009-04-03 10:14:18 -04002817 b = p->nodes[level];
2818 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002819
Chris Masonbd681512011-07-16 15:23:14 -04002820 /*
2821 * slot 0 is special, if we change the key
2822 * we have to update the parent pointer
2823 * which means we must have a write lock
2824 * on the parent
2825 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002826 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002827 write_lock_level < level + 1) {
2828 write_lock_level = level + 1;
2829 btrfs_release_path(p);
2830 goto again;
2831 }
2832
Chris Masonf7c79f32012-03-19 15:54:38 -04002833 unlock_up(p, level, lowest_unlock,
2834 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002835
Chris Mason925baed2008-06-25 16:01:30 -04002836 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002837 if (dec)
2838 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002839 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002840 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002841
Liu Bod07b8522017-01-30 12:23:42 -08002842 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01002843 slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002844 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002845 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002846 if (err) {
2847 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002848 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002849 }
Chris Mason76a05b32009-05-14 13:24:30 -04002850
Chris Masonb4ce94d2009-02-04 09:25:08 -05002851 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002852 level = btrfs_header_level(b);
2853 if (level <= write_lock_level) {
2854 err = btrfs_try_tree_write_lock(b);
2855 if (!err) {
2856 btrfs_set_path_blocking(p);
2857 btrfs_tree_lock(b);
2858 btrfs_clear_path_blocking(p, b,
2859 BTRFS_WRITE_LOCK);
2860 }
2861 p->locks[level] = BTRFS_WRITE_LOCK;
2862 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002863 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002864 if (!err) {
2865 btrfs_set_path_blocking(p);
2866 btrfs_tree_read_lock(b);
2867 btrfs_clear_path_blocking(p, b,
2868 BTRFS_READ_LOCK);
2869 }
2870 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002871 }
Chris Masonbd681512011-07-16 15:23:14 -04002872 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002873 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002874 } else {
2875 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002876 if (ins_len > 0 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002877 btrfs_leaf_free_space(fs_info, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002878 if (write_lock_level < 1) {
2879 write_lock_level = 1;
2880 btrfs_release_path(p);
2881 goto again;
2882 }
2883
Chris Masonb4ce94d2009-02-04 09:25:08 -05002884 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002885 err = split_leaf(trans, root, key,
2886 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002887 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002888
Yan Zheng33c66f42009-07-22 09:59:00 -04002889 BUG_ON(err > 0);
2890 if (err) {
2891 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002892 goto done;
2893 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002894 }
Chris Mason459931e2008-12-10 09:10:46 -05002895 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002896 unlock_up(p, level, lowest_unlock,
2897 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002898 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002899 }
2900 }
Chris Mason65b51a02008-08-01 15:11:20 -04002901 ret = 1;
2902done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002903 /*
2904 * we don't really know what they plan on doing with the path
2905 * from here on, so for now just mark it as blocking
2906 */
Chris Masonb9473432009-03-13 11:00:37 -04002907 if (!p->leave_spinning)
2908 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002909 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002910 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002911 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002912}
2913
Chris Mason74123bd2007-02-02 11:05:29 -05002914/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002915 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2916 * current state of the tree together with the operations recorded in the tree
2917 * modification log to search for the key in a previous version of this tree, as
2918 * denoted by the time_seq parameter.
2919 *
2920 * Naturally, there is no support for insert, delete or cow operations.
2921 *
2922 * The resulting path and return value will be set up as if we called
2923 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2924 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002925int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002926 struct btrfs_path *p, u64 time_seq)
2927{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002929 struct extent_buffer *b;
2930 int slot;
2931 int ret;
2932 int err;
2933 int level;
2934 int lowest_unlock = 1;
2935 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002936 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002937
2938 lowest_level = p->lowest_level;
2939 WARN_ON(p->nodes[0] != NULL);
2940
2941 if (p->search_commit_root) {
2942 BUG_ON(time_seq);
2943 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2944 }
2945
2946again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002947 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002948 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002949 p->locks[level] = BTRFS_READ_LOCK;
2950
2951 while (b) {
2952 level = btrfs_header_level(b);
2953 p->nodes[level] = b;
2954 btrfs_clear_path_blocking(p, NULL, 0);
2955
2956 /*
2957 * we have a lock on b and as long as we aren't changing
2958 * the tree, there is no way to for the items in b to change.
2959 * It is safe to drop the lock on our parent before we
2960 * go through the expensive btree search on b.
2961 */
2962 btrfs_unlock_up_safe(p, level + 1);
2963
Josef Bacikd4b40872013-09-24 14:09:34 -04002964 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002965 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002966 * time.
2967 */
2968 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002969 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002970
2971 if (level != 0) {
2972 int dec = 0;
2973 if (ret && slot > 0) {
2974 dec = 1;
2975 slot -= 1;
2976 }
2977 p->slots[level] = slot;
2978 unlock_up(p, level, lowest_unlock, 0, NULL);
2979
2980 if (level == lowest_level) {
2981 if (dec)
2982 p->slots[level]++;
2983 goto done;
2984 }
2985
Liu Bod07b8522017-01-30 12:23:42 -08002986 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01002987 slot, key);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002988 if (err == -EAGAIN)
2989 goto again;
2990 if (err) {
2991 ret = err;
2992 goto done;
2993 }
2994
2995 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08002996 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002997 if (!err) {
2998 btrfs_set_path_blocking(p);
2999 btrfs_tree_read_lock(b);
3000 btrfs_clear_path_blocking(p, b,
3001 BTRFS_READ_LOCK);
3002 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003003 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003004 if (!b) {
3005 ret = -ENOMEM;
3006 goto done;
3007 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003008 p->locks[level] = BTRFS_READ_LOCK;
3009 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003010 } else {
3011 p->slots[level] = slot;
3012 unlock_up(p, level, lowest_unlock, 0, NULL);
3013 goto done;
3014 }
3015 }
3016 ret = 1;
3017done:
3018 if (!p->leave_spinning)
3019 btrfs_set_path_blocking(p);
3020 if (ret < 0)
3021 btrfs_release_path(p);
3022
3023 return ret;
3024}
3025
3026/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003027 * helper to use instead of search slot if no exact match is needed but
3028 * instead the next or previous item should be returned.
3029 * When find_higher is true, the next higher item is returned, the next lower
3030 * otherwise.
3031 * When return_any and find_higher are both true, and no higher item is found,
3032 * return the next lower instead.
3033 * When return_any is true and find_higher is false, and no lower item is found,
3034 * return the next higher instead.
3035 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3036 * < 0 on error
3037 */
3038int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003039 const struct btrfs_key *key,
3040 struct btrfs_path *p, int find_higher,
3041 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003042{
3043 int ret;
3044 struct extent_buffer *leaf;
3045
3046again:
3047 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3048 if (ret <= 0)
3049 return ret;
3050 /*
3051 * a return value of 1 means the path is at the position where the
3052 * item should be inserted. Normally this is the next bigger item,
3053 * but in case the previous item is the last in a leaf, path points
3054 * to the first free slot in the previous leaf, i.e. at an invalid
3055 * item.
3056 */
3057 leaf = p->nodes[0];
3058
3059 if (find_higher) {
3060 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3061 ret = btrfs_next_leaf(root, p);
3062 if (ret <= 0)
3063 return ret;
3064 if (!return_any)
3065 return 1;
3066 /*
3067 * no higher item found, return the next
3068 * lower instead
3069 */
3070 return_any = 0;
3071 find_higher = 0;
3072 btrfs_release_path(p);
3073 goto again;
3074 }
3075 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003076 if (p->slots[0] == 0) {
3077 ret = btrfs_prev_leaf(root, p);
3078 if (ret < 0)
3079 return ret;
3080 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003081 leaf = p->nodes[0];
3082 if (p->slots[0] == btrfs_header_nritems(leaf))
3083 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003084 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003085 }
Arne Jansene6793762011-09-13 11:18:10 +02003086 if (!return_any)
3087 return 1;
3088 /*
3089 * no lower item found, return the next
3090 * higher instead
3091 */
3092 return_any = 0;
3093 find_higher = 1;
3094 btrfs_release_path(p);
3095 goto again;
3096 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003097 --p->slots[0];
3098 }
3099 }
3100 return 0;
3101}
3102
3103/*
Chris Mason74123bd2007-02-02 11:05:29 -05003104 * adjust the pointers going up the tree, starting at level
3105 * making sure the right key of each node is points to 'key'.
3106 * This is used after shifting pointers to the left, so it stops
3107 * fixing up pointers when a given leaf/node is not in slot 0 of the
3108 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003109 *
Chris Mason74123bd2007-02-02 11:05:29 -05003110 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003111static void fixup_low_keys(struct btrfs_fs_info *fs_info,
3112 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003113 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003114{
3115 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003116 struct extent_buffer *t;
3117
Chris Mason234b63a2007-03-13 10:46:10 -04003118 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003119 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003120 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003121 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003122 t = path->nodes[i];
David Sterba3ac6de12018-03-05 15:00:37 +01003123 tree_mod_log_set_node_key(t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003124 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003125 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003126 if (tslot != 0)
3127 break;
3128 }
3129}
3130
Chris Mason74123bd2007-02-02 11:05:29 -05003131/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003132 * update item key.
3133 *
3134 * This function isn't completely safe. It's the caller's responsibility
3135 * that the new key won't break the order
3136 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003137void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3138 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003139 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003140{
3141 struct btrfs_disk_key disk_key;
3142 struct extent_buffer *eb;
3143 int slot;
3144
3145 eb = path->nodes[0];
3146 slot = path->slots[0];
3147 if (slot > 0) {
3148 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003149 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003150 }
3151 if (slot < btrfs_header_nritems(eb) - 1) {
3152 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003153 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003154 }
3155
3156 btrfs_cpu_key_to_disk(&disk_key, new_key);
3157 btrfs_set_item_key(eb, &disk_key, slot);
3158 btrfs_mark_buffer_dirty(eb);
3159 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003160 fixup_low_keys(fs_info, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003161}
3162
3163/*
Chris Mason74123bd2007-02-02 11:05:29 -05003164 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003165 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003166 *
3167 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3168 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003169 */
Chris Mason98ed5172008-01-03 10:01:48 -05003170static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003171 struct btrfs_fs_info *fs_info,
3172 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003173 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003174{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003175 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003176 int src_nritems;
3177 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003178 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003179
Chris Mason5f39d392007-10-15 16:14:19 -04003180 src_nritems = btrfs_header_nritems(src);
3181 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003182 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003183 WARN_ON(btrfs_header_generation(src) != trans->transid);
3184 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003185
Chris Masonbce4eae2008-04-24 14:42:46 -04003186 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003187 return 1;
3188
Chris Masond3977122009-01-05 21:25:51 -05003189 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003190 return 1;
3191
Chris Masonbce4eae2008-04-24 14:42:46 -04003192 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003193 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003194 if (push_items < src_nritems) {
3195 /* leave at least 8 pointers in the node if
3196 * we aren't going to empty it
3197 */
3198 if (src_nritems - push_items < 8) {
3199 if (push_items <= 8)
3200 return 1;
3201 push_items -= 8;
3202 }
3203 }
3204 } else
3205 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003206
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003207 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003208 push_items);
3209 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003210 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003211 return ret;
3212 }
Chris Mason5f39d392007-10-15 16:14:19 -04003213 copy_extent_buffer(dst, src,
3214 btrfs_node_key_ptr_offset(dst_nritems),
3215 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003216 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003217
Chris Masonbb803952007-03-01 12:04:21 -05003218 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003219 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003220 * Don't call tree_mod_log_insert_move here, key removal was
3221 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003222 */
Chris Mason5f39d392007-10-15 16:14:19 -04003223 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3224 btrfs_node_key_ptr_offset(push_items),
3225 (src_nritems - push_items) *
3226 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003227 }
Chris Mason5f39d392007-10-15 16:14:19 -04003228 btrfs_set_header_nritems(src, src_nritems - push_items);
3229 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3230 btrfs_mark_buffer_dirty(src);
3231 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003232
Chris Masonbb803952007-03-01 12:04:21 -05003233 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003234}
3235
Chris Mason97571fd2007-02-24 13:39:08 -05003236/*
Chris Mason79f95c82007-03-01 15:16:26 -05003237 * try to push data from one node into the next node right in the
3238 * tree.
3239 *
3240 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3241 * error, and > 0 if there was no room in the right hand block.
3242 *
3243 * this will only push up to 1/2 the contents of the left node over
3244 */
Chris Mason5f39d392007-10-15 16:14:19 -04003245static int balance_node_right(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003246 struct btrfs_fs_info *fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04003247 struct extent_buffer *dst,
3248 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003249{
Chris Mason79f95c82007-03-01 15:16:26 -05003250 int push_items = 0;
3251 int max_push;
3252 int src_nritems;
3253 int dst_nritems;
3254 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003255
Chris Mason7bb86312007-12-11 09:25:06 -05003256 WARN_ON(btrfs_header_generation(src) != trans->transid);
3257 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3258
Chris Mason5f39d392007-10-15 16:14:19 -04003259 src_nritems = btrfs_header_nritems(src);
3260 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003261 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003262 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003263 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003264
Chris Masond3977122009-01-05 21:25:51 -05003265 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003266 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003267
3268 max_push = src_nritems / 2 + 1;
3269 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003270 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003271 return 1;
Yan252c38f2007-08-29 09:11:44 -04003272
Chris Mason79f95c82007-03-01 15:16:26 -05003273 if (max_push < push_items)
3274 push_items = max_push;
3275
David Sterbabf1d3422018-03-05 15:47:39 +01003276 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3277 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003278 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3279 btrfs_node_key_ptr_offset(0),
3280 (dst_nritems) *
3281 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003282
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003283 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003284 src_nritems - push_items, push_items);
3285 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003286 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003287 return ret;
3288 }
Chris Mason5f39d392007-10-15 16:14:19 -04003289 copy_extent_buffer(dst, src,
3290 btrfs_node_key_ptr_offset(0),
3291 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003292 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003293
Chris Mason5f39d392007-10-15 16:14:19 -04003294 btrfs_set_header_nritems(src, src_nritems - push_items);
3295 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003296
Chris Mason5f39d392007-10-15 16:14:19 -04003297 btrfs_mark_buffer_dirty(src);
3298 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003299
Chris Mason79f95c82007-03-01 15:16:26 -05003300 return ret;
3301}
3302
3303/*
Chris Mason97571fd2007-02-24 13:39:08 -05003304 * helper function to insert a new root level in the tree.
3305 * A new node is allocated, and a single item is inserted to
3306 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003307 *
3308 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003309 */
Chris Masond3977122009-01-05 21:25:51 -05003310static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003311 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003312 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003313{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003314 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003315 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003316 struct extent_buffer *lower;
3317 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003318 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003319 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003320
3321 BUG_ON(path->nodes[level]);
3322 BUG_ON(path->nodes[level-1] != root->node);
3323
Chris Mason7bb86312007-12-11 09:25:06 -05003324 lower = path->nodes[level-1];
3325 if (level == 1)
3326 btrfs_item_key(lower, &lower_key, 0);
3327 else
3328 btrfs_node_key(lower, &lower_key, 0);
3329
David Sterba4d75f8a2014-06-15 01:54:12 +02003330 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3331 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003332 if (IS_ERR(c))
3333 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003334
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003335 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003336
David Sterbab159fa22016-11-08 18:09:03 +01003337 memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003338 btrfs_set_header_nritems(c, 1);
3339 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003340 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003341 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003342 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003343 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003344
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003345 write_extent_buffer_fsid(c, fs_info->fsid);
3346 write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
Chris Masone17cade2008-04-15 15:41:47 -04003347
Chris Mason5f39d392007-10-15 16:14:19 -04003348 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003349 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003350 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003351 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003352
3353 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003354
3355 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003356
Chris Mason925baed2008-06-25 16:01:30 -04003357 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003358 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003359 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003360
3361 /* the super has an extra ref to root->node */
3362 free_extent_buffer(old);
3363
Chris Mason0b86a832008-03-24 15:01:56 -04003364 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003365 extent_buffer_get(c);
3366 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303367 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003368 path->slots[level] = 0;
3369 return 0;
3370}
3371
Chris Mason74123bd2007-02-02 11:05:29 -05003372/*
3373 * worker function to insert a single pointer in a node.
3374 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003375 *
Chris Mason74123bd2007-02-02 11:05:29 -05003376 * slot and level indicate where you want the key to go, and
3377 * blocknr is the block the key points to.
3378 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003379static void insert_ptr(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003380 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003381 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003382 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003383{
Chris Mason5f39d392007-10-15 16:14:19 -04003384 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003385 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003386 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003387
3388 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003389 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003390 lower = path->nodes[level];
3391 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003392 BUG_ON(slot > nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003393 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003394 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003395 if (level) {
3396 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003397 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003398 BUG_ON(ret < 0);
3399 }
Chris Mason5f39d392007-10-15 16:14:19 -04003400 memmove_extent_buffer(lower,
3401 btrfs_node_key_ptr_offset(slot + 1),
3402 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003403 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003404 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003405 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003406 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3407 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003408 BUG_ON(ret < 0);
3409 }
Chris Mason5f39d392007-10-15 16:14:19 -04003410 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003411 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003412 WARN_ON(trans->transid == 0);
3413 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003414 btrfs_set_header_nritems(lower, nritems + 1);
3415 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003416}
3417
Chris Mason97571fd2007-02-24 13:39:08 -05003418/*
3419 * split the node at the specified level in path in two.
3420 * The path is corrected to point to the appropriate node after the split
3421 *
3422 * Before splitting this tries to make some room in the node by pushing
3423 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003424 *
3425 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003426 */
Chris Masone02119d2008-09-05 16:13:11 -04003427static noinline int split_node(struct btrfs_trans_handle *trans,
3428 struct btrfs_root *root,
3429 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003431 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003432 struct extent_buffer *c;
3433 struct extent_buffer *split;
3434 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003435 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003436 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003437 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003438
Chris Mason5f39d392007-10-15 16:14:19 -04003439 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003440 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003441 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003442 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003443 * trying to split the root, lets make a new one
3444 *
Liu Bofdd99c72013-05-22 12:06:51 +00003445 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003446 * insert_new_root, because that root buffer will be kept as a
3447 * normal node. We are going to log removal of half of the
3448 * elements below with tree_mod_log_eb_copy. We're holding a
3449 * tree lock on the buffer, which is why we cannot race with
3450 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003451 */
Liu Bofdd99c72013-05-22 12:06:51 +00003452 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003453 if (ret)
3454 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003455 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003456 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003457 c = path->nodes[level];
3458 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003459 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003460 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003461 if (ret < 0)
3462 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003463 }
Chris Masone66f7092007-04-20 13:16:02 -04003464
Chris Mason5f39d392007-10-15 16:14:19 -04003465 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003466 mid = (c_nritems + 1) / 2;
3467 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003468
David Sterba4d75f8a2014-06-15 01:54:12 +02003469 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3470 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003471 if (IS_ERR(split))
3472 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003473
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003474 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003475
David Sterbab159fa22016-11-08 18:09:03 +01003476 memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003477 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003478 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003479 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003480 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003481 btrfs_set_header_owner(split, root->root_key.objectid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003482 write_extent_buffer_fsid(split, fs_info->fsid);
3483 write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
Chris Mason5f39d392007-10-15 16:14:19 -04003484
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003485 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003486 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003487 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003488 return ret;
3489 }
Chris Mason5f39d392007-10-15 16:14:19 -04003490 copy_extent_buffer(split, c,
3491 btrfs_node_key_ptr_offset(0),
3492 btrfs_node_key_ptr_offset(mid),
3493 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3494 btrfs_set_header_nritems(split, c_nritems - mid);
3495 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003496 ret = 0;
3497
Chris Mason5f39d392007-10-15 16:14:19 -04003498 btrfs_mark_buffer_dirty(c);
3499 btrfs_mark_buffer_dirty(split);
3500
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003501 insert_ptr(trans, fs_info, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003502 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003503
Chris Mason5de08d72007-02-24 06:24:44 -05003504 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003505 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003506 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003507 free_extent_buffer(c);
3508 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003509 path->slots[level + 1] += 1;
3510 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003511 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003512 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003513 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003514 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003515}
3516
Chris Mason74123bd2007-02-02 11:05:29 -05003517/*
3518 * how many bytes are required to store the items in a leaf. start
3519 * and nr indicate which items in the leaf to check. This totals up the
3520 * space used both by the item structs and the item data
3521 */
Chris Mason5f39d392007-10-15 16:14:19 -04003522static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003523{
Josef Bacik41be1f32012-10-15 13:43:18 -04003524 struct btrfs_item *start_item;
3525 struct btrfs_item *end_item;
3526 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003527 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003528 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003529 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003530
3531 if (!nr)
3532 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003533 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003534 start_item = btrfs_item_nr(start);
3535 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003536 data_len = btrfs_token_item_offset(l, start_item, &token) +
3537 btrfs_token_item_size(l, start_item, &token);
3538 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003539 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003540 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003541 return data_len;
3542}
3543
Chris Mason74123bd2007-02-02 11:05:29 -05003544/*
Chris Masond4dbff92007-04-04 14:08:15 -04003545 * The space between the end of the leaf items and
3546 * the start of the leaf data. IOW, how much room
3547 * the leaf has left for both items and data
3548 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003549noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04003550 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003551{
Chris Mason5f39d392007-10-15 16:14:19 -04003552 int nritems = btrfs_header_nritems(leaf);
3553 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003554
3555 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003556 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003557 btrfs_crit(fs_info,
3558 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3559 ret,
3560 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3561 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003562 }
3563 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003564}
3565
Chris Mason99d8f832010-07-07 10:51:48 -04003566/*
3567 * min slot controls the lowest index we're willing to push to the
3568 * right. We'll push up to and including min_slot, but no lower
3569 */
David Sterba1e47eef2017-02-10 19:13:06 +01003570static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003571 struct btrfs_path *path,
3572 int data_size, int empty,
3573 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003574 int free_space, u32 left_nritems,
3575 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003576{
Chris Mason5f39d392007-10-15 16:14:19 -04003577 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003578 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003579 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003581 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003582 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003583 int push_space = 0;
3584 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003585 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003586 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003587 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003588 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003589 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003590
Chris Masoncfed81a2012-03-03 07:40:03 -05003591 btrfs_init_map_token(&token);
3592
Chris Mason34a38212007-11-07 13:31:03 -05003593 if (empty)
3594 nr = 0;
3595 else
Chris Mason99d8f832010-07-07 10:51:48 -04003596 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003597
Zheng Yan31840ae2008-09-23 13:14:14 -04003598 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003599 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003600
Chris Mason44871b12009-03-13 10:04:31 -04003601 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003602 i = left_nritems - 1;
3603 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003604 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003605
Zheng Yan31840ae2008-09-23 13:14:14 -04003606 if (!empty && push_items > 0) {
3607 if (path->slots[0] > i)
3608 break;
3609 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003610 int space = btrfs_leaf_free_space(fs_info, left);
Zheng Yan31840ae2008-09-23 13:14:14 -04003611 if (space + push_space * 2 > free_space)
3612 break;
3613 }
3614 }
3615
Chris Mason00ec4c52007-02-24 12:47:20 -05003616 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003617 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003618
Chris Masondb945352007-10-15 16:15:53 -04003619 this_item_size = btrfs_item_size(left, item);
3620 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003621 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003622
Chris Mason00ec4c52007-02-24 12:47:20 -05003623 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003624 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003625 if (i == 0)
3626 break;
3627 i--;
Chris Masondb945352007-10-15 16:15:53 -04003628 }
Chris Mason5f39d392007-10-15 16:14:19 -04003629
Chris Mason925baed2008-06-25 16:01:30 -04003630 if (push_items == 0)
3631 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003632
Julia Lawall6c1500f2012-11-03 20:30:18 +00003633 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Chris Mason00ec4c52007-02-24 12:47:20 -05003635 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003636 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003637
Chris Mason5f39d392007-10-15 16:14:19 -04003638 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003639 push_space -= leaf_data_end(fs_info, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003640
Chris Mason00ec4c52007-02-24 12:47:20 -05003641 /* make room in the right data area */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003642 data_end = leaf_data_end(fs_info, right);
Chris Mason5f39d392007-10-15 16:14:19 -04003643 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003644 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3645 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003646 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003647
Chris Mason00ec4c52007-02-24 12:47:20 -05003648 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003649 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003650 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003651 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
Chris Masond6025572007-03-30 14:27:56 -04003652 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003653
3654 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3655 btrfs_item_nr_offset(0),
3656 right_nritems * sizeof(struct btrfs_item));
3657
Chris Mason00ec4c52007-02-24 12:47:20 -05003658 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003659 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3660 btrfs_item_nr_offset(left_nritems - push_items),
3661 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003662
3663 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003664 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003665 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003666 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003667 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003668 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003669 push_space -= btrfs_token_item_size(right, item, &token);
3670 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003671 }
3672
Chris Mason7518a232007-03-12 12:01:18 -04003673 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003674 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003675
Chris Mason34a38212007-11-07 13:31:03 -05003676 if (left_nritems)
3677 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003678 else
David Sterba7c302b42017-02-10 18:47:57 +01003679 clean_tree_block(fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003680
Chris Mason5f39d392007-10-15 16:14:19 -04003681 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003682
Chris Mason5f39d392007-10-15 16:14:19 -04003683 btrfs_item_key(right, &disk_key, 0);
3684 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003685 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003686
Chris Mason00ec4c52007-02-24 12:47:20 -05003687 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003688 if (path->slots[0] >= left_nritems) {
3689 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003690 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba7c302b42017-02-10 18:47:57 +01003691 clean_tree_block(fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003692 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003693 free_extent_buffer(path->nodes[0]);
3694 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003695 path->slots[1] += 1;
3696 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003697 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003698 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003699 }
3700 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003701
3702out_unlock:
3703 btrfs_tree_unlock(right);
3704 free_extent_buffer(right);
3705 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003706}
Chris Mason925baed2008-06-25 16:01:30 -04003707
Chris Mason00ec4c52007-02-24 12:47:20 -05003708/*
Chris Mason44871b12009-03-13 10:04:31 -04003709 * push some data in the path leaf to the right, trying to free up at
3710 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3711 *
3712 * returns 1 if the push failed because the other node didn't have enough
3713 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003714 *
3715 * this will push starting from min_slot to the end of the leaf. It won't
3716 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003717 */
3718static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003719 *root, struct btrfs_path *path,
3720 int min_data_size, int data_size,
3721 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003722{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003723 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003724 struct extent_buffer *left = path->nodes[0];
3725 struct extent_buffer *right;
3726 struct extent_buffer *upper;
3727 int slot;
3728 int free_space;
3729 u32 left_nritems;
3730 int ret;
3731
3732 if (!path->nodes[1])
3733 return 1;
3734
3735 slot = path->slots[1];
3736 upper = path->nodes[1];
3737 if (slot >= btrfs_header_nritems(upper) - 1)
3738 return 1;
3739
3740 btrfs_assert_tree_locked(path->nodes[1]);
3741
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003742 right = read_node_slot(fs_info, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003743 /*
3744 * slot + 1 is not valid or we fail to read the right node,
3745 * no big deal, just return.
3746 */
3747 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003748 return 1;
3749
Chris Mason44871b12009-03-13 10:04:31 -04003750 btrfs_tree_lock(right);
3751 btrfs_set_lock_blocking(right);
3752
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003753 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003754 if (free_space < data_size)
3755 goto out_unlock;
3756
3757 /* cow and double check */
3758 ret = btrfs_cow_block(trans, root, right, upper,
3759 slot + 1, &right);
3760 if (ret)
3761 goto out_unlock;
3762
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003763 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003764 if (free_space < data_size)
3765 goto out_unlock;
3766
3767 left_nritems = btrfs_header_nritems(left);
3768 if (left_nritems == 0)
3769 goto out_unlock;
3770
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003771 if (path->slots[0] == left_nritems && !empty) {
3772 /* Key greater than all keys in the leaf, right neighbor has
3773 * enough room for it and we're not emptying our leaf to delete
3774 * it, therefore use right neighbor to insert the new item and
3775 * no need to touch/dirty our left leaft. */
3776 btrfs_tree_unlock(left);
3777 free_extent_buffer(left);
3778 path->nodes[0] = right;
3779 path->slots[0] = 0;
3780 path->slots[1]++;
3781 return 0;
3782 }
3783
David Sterba1e47eef2017-02-10 19:13:06 +01003784 return __push_leaf_right(fs_info, path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003785 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003786out_unlock:
3787 btrfs_tree_unlock(right);
3788 free_extent_buffer(right);
3789 return 1;
3790}
3791
3792/*
Chris Mason74123bd2007-02-02 11:05:29 -05003793 * push some data in the path leaf to the left, trying to free up at
3794 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003795 *
3796 * max_slot can put a limit on how far into the leaf we'll push items. The
3797 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3798 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003799 */
David Sterba66cb7dd2017-02-10 19:14:36 +01003800static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003801 struct btrfs_path *path, int data_size,
3802 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003803 int free_space, u32 right_nritems,
3804 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003805{
Chris Mason5f39d392007-10-15 16:14:19 -04003806 struct btrfs_disk_key disk_key;
3807 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003808 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003809 int push_space = 0;
3810 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003811 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003812 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003813 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003814 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003815 u32 this_item_size;
3816 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003817 struct btrfs_map_token token;
3818
3819 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003820
Chris Mason34a38212007-11-07 13:31:03 -05003821 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003822 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003823 else
Chris Mason99d8f832010-07-07 10:51:48 -04003824 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003825
3826 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003827 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003828
Zheng Yan31840ae2008-09-23 13:14:14 -04003829 if (!empty && push_items > 0) {
3830 if (path->slots[0] < i)
3831 break;
3832 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003833 int space = btrfs_leaf_free_space(fs_info, right);
Zheng Yan31840ae2008-09-23 13:14:14 -04003834 if (space + push_space * 2 > free_space)
3835 break;
3836 }
3837 }
3838
Chris Masonbe0e5c02007-01-26 15:51:26 -05003839 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003840 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003841
3842 this_item_size = btrfs_item_size(right, item);
3843 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003844 break;
Chris Masondb945352007-10-15 16:15:53 -04003845
Chris Masonbe0e5c02007-01-26 15:51:26 -05003846 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003847 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003848 }
Chris Masondb945352007-10-15 16:15:53 -04003849
Chris Masonbe0e5c02007-01-26 15:51:26 -05003850 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003851 ret = 1;
3852 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303854 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003855
Chris Masonbe0e5c02007-01-26 15:51:26 -05003856 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003857 copy_extent_buffer(left, right,
3858 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3859 btrfs_item_nr_offset(0),
3860 push_items * sizeof(struct btrfs_item));
3861
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003862 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003863 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003864
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003865 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003866 leaf_data_end(fs_info, left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003867 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003868 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003869 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003870 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003871 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003872
Chris Masondb945352007-10-15 16:15:53 -04003873 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003874 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003875 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003876
Ross Kirkdd3cc162013-09-16 15:58:09 +01003877 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003878
Chris Masoncfed81a2012-03-03 07:40:03 -05003879 ioff = btrfs_token_item_offset(left, item, &token);
3880 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003881 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003882 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883 }
Chris Mason5f39d392007-10-15 16:14:19 -04003884 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885
3886 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003887 if (push_items > right_nritems)
3888 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003889 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003890
Chris Mason34a38212007-11-07 13:31:03 -05003891 if (push_items < right_nritems) {
3892 push_space = btrfs_item_offset_nr(right, push_items - 1) -
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003893 leaf_data_end(fs_info, right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003894 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003895 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003896 BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003897 leaf_data_end(fs_info, right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003898
3899 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003900 btrfs_item_nr_offset(push_items),
3901 (btrfs_header_nritems(right) - push_items) *
3902 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003903 }
Yaneef1c492007-11-26 10:58:13 -05003904 right_nritems -= push_items;
3905 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003906 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003908 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003909
Chris Masoncfed81a2012-03-03 07:40:03 -05003910 push_space = push_space - btrfs_token_item_size(right,
3911 item, &token);
3912 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003913 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003914
Chris Mason5f39d392007-10-15 16:14:19 -04003915 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003916 if (right_nritems)
3917 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003918 else
David Sterba7c302b42017-02-10 18:47:57 +01003919 clean_tree_block(fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003920
Chris Mason5f39d392007-10-15 16:14:19 -04003921 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003922 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003923
3924 /* then fixup the leaf pointer in the path */
3925 if (path->slots[0] < push_items) {
3926 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003927 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003928 free_extent_buffer(path->nodes[0]);
3929 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003930 path->slots[1] -= 1;
3931 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003932 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003933 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003934 path->slots[0] -= push_items;
3935 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003936 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003937 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003938out:
3939 btrfs_tree_unlock(left);
3940 free_extent_buffer(left);
3941 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003942}
3943
Chris Mason74123bd2007-02-02 11:05:29 -05003944/*
Chris Mason44871b12009-03-13 10:04:31 -04003945 * push some data in the path leaf to the left, trying to free up at
3946 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003947 *
3948 * max_slot can put a limit on how far into the leaf we'll push items. The
3949 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3950 * items
Chris Mason44871b12009-03-13 10:04:31 -04003951 */
3952static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003953 *root, struct btrfs_path *path, int min_data_size,
3954 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003955{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003956 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003957 struct extent_buffer *right = path->nodes[0];
3958 struct extent_buffer *left;
3959 int slot;
3960 int free_space;
3961 u32 right_nritems;
3962 int ret = 0;
3963
3964 slot = path->slots[1];
3965 if (slot == 0)
3966 return 1;
3967 if (!path->nodes[1])
3968 return 1;
3969
3970 right_nritems = btrfs_header_nritems(right);
3971 if (right_nritems == 0)
3972 return 1;
3973
3974 btrfs_assert_tree_locked(path->nodes[1]);
3975
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003976 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003977 /*
3978 * slot - 1 is not valid or we fail to read the left node,
3979 * no big deal, just return.
3980 */
3981 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003982 return 1;
3983
Chris Mason44871b12009-03-13 10:04:31 -04003984 btrfs_tree_lock(left);
3985 btrfs_set_lock_blocking(left);
3986
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003987 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04003988 if (free_space < data_size) {
3989 ret = 1;
3990 goto out;
3991 }
3992
3993 /* cow and double check */
3994 ret = btrfs_cow_block(trans, root, left,
3995 path->nodes[1], slot - 1, &left);
3996 if (ret) {
3997 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003998 if (ret == -ENOSPC)
3999 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004000 goto out;
4001 }
4002
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004003 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04004004 if (free_space < data_size) {
4005 ret = 1;
4006 goto out;
4007 }
4008
David Sterba66cb7dd2017-02-10 19:14:36 +01004009 return __push_leaf_left(fs_info, path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004010 empty, left, free_space, right_nritems,
4011 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004012out:
4013 btrfs_tree_unlock(left);
4014 free_extent_buffer(left);
4015 return ret;
4016}
4017
4018/*
Chris Mason74123bd2007-02-02 11:05:29 -05004019 * split the path's leaf in two, making sure there is at least data_size
4020 * available for the resulting leaf level of the path.
4021 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004022static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004023 struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004024 struct btrfs_path *path,
4025 struct extent_buffer *l,
4026 struct extent_buffer *right,
4027 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004028{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004029 int data_copy_size;
4030 int rt_data_off;
4031 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004032 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004033 struct btrfs_map_token token;
4034
4035 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004036
Chris Mason5f39d392007-10-15 16:14:19 -04004037 nritems = nritems - mid;
4038 btrfs_set_header_nritems(right, nritems);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004039 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
Chris Mason5f39d392007-10-15 16:14:19 -04004040
4041 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4042 btrfs_item_nr_offset(mid),
4043 nritems * sizeof(struct btrfs_item));
4044
4045 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004046 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4047 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004048 leaf_data_end(fs_info, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004049
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004050 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004051
4052 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004053 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004054 u32 ioff;
4055
Chris Masoncfed81a2012-03-03 07:40:03 -05004056 ioff = btrfs_token_item_offset(right, item, &token);
4057 btrfs_set_token_item_offset(right, item,
4058 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004059 }
Chris Mason74123bd2007-02-02 11:05:29 -05004060
Chris Mason5f39d392007-10-15 16:14:19 -04004061 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004062 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004063 insert_ptr(trans, fs_info, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004064 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004065
4066 btrfs_mark_buffer_dirty(right);
4067 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004068 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004069
Chris Masonbe0e5c02007-01-26 15:51:26 -05004070 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004071 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004072 free_extent_buffer(path->nodes[0]);
4073 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004074 path->slots[0] -= mid;
4075 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004076 } else {
4077 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004078 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004079 }
Chris Mason5f39d392007-10-15 16:14:19 -04004080
Chris Masoneb60cea2007-02-02 09:18:22 -05004081 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004082}
4083
4084/*
Chris Mason99d8f832010-07-07 10:51:48 -04004085 * double splits happen when we need to insert a big item in the middle
4086 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4087 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4088 * A B C
4089 *
4090 * We avoid this by trying to push the items on either side of our target
4091 * into the adjacent leaves. If all goes well we can avoid the double split
4092 * completely.
4093 */
4094static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *root,
4096 struct btrfs_path *path,
4097 int data_size)
4098{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004099 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason99d8f832010-07-07 10:51:48 -04004100 int ret;
4101 int progress = 0;
4102 int slot;
4103 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004104 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004105
4106 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004107 if (slot < btrfs_header_nritems(path->nodes[0]))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004108 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004109
4110 /*
4111 * try to push all the items after our slot into the
4112 * right leaf
4113 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004114 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004115 if (ret < 0)
4116 return ret;
4117
4118 if (ret == 0)
4119 progress++;
4120
4121 nritems = btrfs_header_nritems(path->nodes[0]);
4122 /*
4123 * our goal is to get our slot at the start or end of a leaf. If
4124 * we've done so we're done
4125 */
4126 if (path->slots[0] == 0 || path->slots[0] == nritems)
4127 return 0;
4128
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004129 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004130 return 0;
4131
4132 /* try to push all the items before our slot into the next leaf */
4133 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004134 space_needed = data_size;
4135 if (slot > 0)
4136 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004137 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004138 if (ret < 0)
4139 return ret;
4140
4141 if (ret == 0)
4142 progress++;
4143
4144 if (progress)
4145 return 0;
4146 return 1;
4147}
4148
4149/*
Chris Mason44871b12009-03-13 10:04:31 -04004150 * split the path's leaf in two, making sure there is at least data_size
4151 * available for the resulting leaf level of the path.
4152 *
4153 * returns 0 if all went well and < 0 on failure.
4154 */
4155static noinline int split_leaf(struct btrfs_trans_handle *trans,
4156 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004157 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004158 struct btrfs_path *path, int data_size,
4159 int extend)
4160{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004161 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004162 struct extent_buffer *l;
4163 u32 nritems;
4164 int mid;
4165 int slot;
4166 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004167 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004168 int ret = 0;
4169 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004170 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004171 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004172 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004173
Yan, Zhenga5719522009-09-24 09:17:31 -04004174 l = path->nodes[0];
4175 slot = path->slots[0];
4176 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004177 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004178 return -EOVERFLOW;
4179
Chris Mason44871b12009-03-13 10:04:31 -04004180 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004181 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004182 int space_needed = data_size;
4183
4184 if (slot < btrfs_header_nritems(l))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004185 space_needed -= btrfs_leaf_free_space(fs_info, l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004186
4187 wret = push_leaf_right(trans, root, path, space_needed,
4188 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004189 if (wret < 0)
4190 return wret;
4191 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004192 space_needed = data_size;
4193 if (slot > 0)
4194 space_needed -= btrfs_leaf_free_space(fs_info,
4195 l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004196 wret = push_leaf_left(trans, root, path, space_needed,
4197 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004198 if (wret < 0)
4199 return wret;
4200 }
4201 l = path->nodes[0];
4202
4203 /* did the pushes work? */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004204 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004205 return 0;
4206 }
4207
4208 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004209 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004210 if (ret)
4211 return ret;
4212 }
4213again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004214 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004215 l = path->nodes[0];
4216 slot = path->slots[0];
4217 nritems = btrfs_header_nritems(l);
4218 mid = (nritems + 1) / 2;
4219
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004220 if (mid <= slot) {
4221 if (nritems == 1 ||
4222 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004223 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004224 if (slot >= nritems) {
4225 split = 0;
4226 } else {
4227 mid = slot;
4228 if (mid != nritems &&
4229 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004230 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004231 if (data_size && !tried_avoid_double)
4232 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004233 split = 2;
4234 }
4235 }
4236 }
4237 } else {
4238 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004239 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004240 if (!extend && data_size && slot == 0) {
4241 split = 0;
4242 } else if ((extend || !data_size) && slot == 0) {
4243 mid = 1;
4244 } else {
4245 mid = slot;
4246 if (mid != nritems &&
4247 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004248 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004249 if (data_size && !tried_avoid_double)
4250 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304251 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004252 }
4253 }
4254 }
4255 }
4256
4257 if (split == 0)
4258 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4259 else
4260 btrfs_item_key(l, &disk_key, mid);
4261
David Sterba4d75f8a2014-06-15 01:54:12 +02004262 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4263 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004264 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004265 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004267 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004268
David Sterbab159fa22016-11-08 18:09:03 +01004269 memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
Chris Mason44871b12009-03-13 10:04:31 -04004270 btrfs_set_header_bytenr(right, right->start);
4271 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004272 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004273 btrfs_set_header_owner(right, root->root_key.objectid);
4274 btrfs_set_header_level(right, 0);
David Sterbad24ee972016-11-09 17:44:25 +01004275 write_extent_buffer_fsid(right, fs_info->fsid);
4276 write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
Chris Mason44871b12009-03-13 10:04:31 -04004277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004278 if (split == 0) {
4279 if (mid <= slot) {
4280 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004281 insert_ptr(trans, fs_info, path, &disk_key,
4282 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004283 btrfs_tree_unlock(path->nodes[0]);
4284 free_extent_buffer(path->nodes[0]);
4285 path->nodes[0] = right;
4286 path->slots[0] = 0;
4287 path->slots[1] += 1;
4288 } else {
4289 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004290 insert_ptr(trans, fs_info, path, &disk_key,
4291 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004292 btrfs_tree_unlock(path->nodes[0]);
4293 free_extent_buffer(path->nodes[0]);
4294 path->nodes[0] = right;
4295 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004296 if (path->slots[1] == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004297 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004298 }
Liu Bo196e0242016-09-07 14:48:28 -07004299 /*
4300 * We create a new leaf 'right' for the required ins_len and
4301 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4302 * the content of ins_len to 'right'.
4303 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004304 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004305 }
4306
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004307 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004308
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004309 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004310 BUG_ON(num_doubles != 0);
4311 num_doubles++;
4312 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004313 }
Chris Mason44871b12009-03-13 10:04:31 -04004314
Jeff Mahoney143bede2012-03-01 14:56:26 +01004315 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004316
4317push_for_double:
4318 push_for_double_split(trans, root, path, data_size);
4319 tried_avoid_double = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004320 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004321 return 0;
4322 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004323}
4324
Yan, Zhengad48fd752009-11-12 09:33:58 +00004325static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4326 struct btrfs_root *root,
4327 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004328{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004329 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004330 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004331 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004332 struct btrfs_file_extent_item *fi;
4333 u64 extent_len = 0;
4334 u32 item_size;
4335 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004336
4337 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004338 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4339
4340 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4341 key.type != BTRFS_EXTENT_CSUM_KEY);
4342
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004343 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004344 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004345
4346 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004347 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4348 fi = btrfs_item_ptr(leaf, path->slots[0],
4349 struct btrfs_file_extent_item);
4350 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4351 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004352 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004353
Chris Mason459931e2008-12-10 09:10:46 -05004354 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004355 path->search_for_split = 1;
4356 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004357 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004358 if (ret > 0)
4359 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004360 if (ret < 0)
4361 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004362
Yan, Zhengad48fd752009-11-12 09:33:58 +00004363 ret = -EAGAIN;
4364 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004365 /* if our item isn't there, return now */
4366 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004367 goto err;
4368
Chris Mason109f6ae2010-04-02 09:20:18 -04004369 /* the leaf has changed, it now has room. return now */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004370 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004371 goto err;
4372
Yan, Zhengad48fd752009-11-12 09:33:58 +00004373 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4374 fi = btrfs_item_ptr(leaf, path->slots[0],
4375 struct btrfs_file_extent_item);
4376 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4377 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004378 }
4379
Chris Masonb9473432009-03-13 11:00:37 -04004380 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004381 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004382 if (ret)
4383 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004384
Yan, Zhengad48fd752009-11-12 09:33:58 +00004385 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004386 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004387 return 0;
4388err:
4389 path->keep_locks = 0;
4390 return ret;
4391}
4392
David Sterba4961e292017-02-10 18:49:53 +01004393static noinline int split_item(struct btrfs_fs_info *fs_info,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004394 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004395 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004396 unsigned long split_offset)
4397{
4398 struct extent_buffer *leaf;
4399 struct btrfs_item *item;
4400 struct btrfs_item *new_item;
4401 int slot;
4402 char *buf;
4403 u32 nritems;
4404 u32 item_size;
4405 u32 orig_offset;
4406 struct btrfs_disk_key disk_key;
4407
Chris Masonb9473432009-03-13 11:00:37 -04004408 leaf = path->nodes[0];
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004409 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004410
Chris Masonb4ce94d2009-02-04 09:25:08 -05004411 btrfs_set_path_blocking(path);
4412
Ross Kirkdd3cc162013-09-16 15:58:09 +01004413 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004414 orig_offset = btrfs_item_offset(leaf, item);
4415 item_size = btrfs_item_size(leaf, item);
4416
Chris Mason459931e2008-12-10 09:10:46 -05004417 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004418 if (!buf)
4419 return -ENOMEM;
4420
Chris Mason459931e2008-12-10 09:10:46 -05004421 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4422 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004423
Chris Mason459931e2008-12-10 09:10:46 -05004424 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004425 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004426 if (slot != nritems) {
4427 /* shift the items */
4428 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004429 btrfs_item_nr_offset(slot),
4430 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004431 }
4432
4433 btrfs_cpu_key_to_disk(&disk_key, new_key);
4434 btrfs_set_item_key(leaf, &disk_key, slot);
4435
Ross Kirkdd3cc162013-09-16 15:58:09 +01004436 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004437
4438 btrfs_set_item_offset(leaf, new_item, orig_offset);
4439 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4440
4441 btrfs_set_item_offset(leaf, item,
4442 orig_offset + item_size - split_offset);
4443 btrfs_set_item_size(leaf, item, split_offset);
4444
4445 btrfs_set_header_nritems(leaf, nritems + 1);
4446
4447 /* write the data for the start of the original item */
4448 write_extent_buffer(leaf, buf,
4449 btrfs_item_ptr_offset(leaf, path->slots[0]),
4450 split_offset);
4451
4452 /* write the data for the new item */
4453 write_extent_buffer(leaf, buf + split_offset,
4454 btrfs_item_ptr_offset(leaf, slot),
4455 item_size - split_offset);
4456 btrfs_mark_buffer_dirty(leaf);
4457
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004458 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004459 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004460 return 0;
4461}
4462
4463/*
4464 * This function splits a single item into two items,
4465 * giving 'new_key' to the new item and splitting the
4466 * old one at split_offset (from the start of the item).
4467 *
4468 * The path may be released by this operation. After
4469 * the split, the path is pointing to the old item. The
4470 * new item is going to be in the same node as the old one.
4471 *
4472 * Note, the item being split must be smaller enough to live alone on
4473 * a tree block with room for one extra struct btrfs_item
4474 *
4475 * This allows us to split the item in place, keeping a lock on the
4476 * leaf the entire time.
4477 */
4478int btrfs_split_item(struct btrfs_trans_handle *trans,
4479 struct btrfs_root *root,
4480 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004481 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004482 unsigned long split_offset)
4483{
4484 int ret;
4485 ret = setup_leaf_for_split(trans, root, path,
4486 sizeof(struct btrfs_item));
4487 if (ret)
4488 return ret;
4489
David Sterba4961e292017-02-10 18:49:53 +01004490 ret = split_item(root->fs_info, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004491 return ret;
4492}
4493
4494/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004495 * This function duplicate a item, giving 'new_key' to the new item.
4496 * It guarantees both items live in the same tree leaf and the new item
4497 * is contiguous with the original item.
4498 *
4499 * This allows us to split file extent in place, keeping a lock on the
4500 * leaf the entire time.
4501 */
4502int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4503 struct btrfs_root *root,
4504 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004505 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004506{
4507 struct extent_buffer *leaf;
4508 int ret;
4509 u32 item_size;
4510
4511 leaf = path->nodes[0];
4512 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4513 ret = setup_leaf_for_split(trans, root, path,
4514 item_size + sizeof(struct btrfs_item));
4515 if (ret)
4516 return ret;
4517
4518 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004519 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004520 item_size, item_size +
4521 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004522 leaf = path->nodes[0];
4523 memcpy_extent_buffer(leaf,
4524 btrfs_item_ptr_offset(leaf, path->slots[0]),
4525 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4526 item_size);
4527 return 0;
4528}
4529
4530/*
Chris Masond352ac62008-09-29 15:18:18 -04004531 * make the item pointed to by the path smaller. new_size indicates
4532 * how small to make it, and from_end tells us if we just chop bytes
4533 * off the end of the item or if we shift the item to chop bytes off
4534 * the front.
4535 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004536void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4537 struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004538{
Chris Masonb18c6682007-04-17 13:26:50 -04004539 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004540 struct extent_buffer *leaf;
4541 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004542 u32 nritems;
4543 unsigned int data_end;
4544 unsigned int old_data_start;
4545 unsigned int old_size;
4546 unsigned int size_diff;
4547 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004548 struct btrfs_map_token token;
4549
4550 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004551
Chris Mason5f39d392007-10-15 16:14:19 -04004552 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004553 slot = path->slots[0];
4554
4555 old_size = btrfs_item_size_nr(leaf, slot);
4556 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004557 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004558
Chris Mason5f39d392007-10-15 16:14:19 -04004559 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004560 data_end = leaf_data_end(fs_info, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004561
Chris Mason5f39d392007-10-15 16:14:19 -04004562 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004563
Chris Masonb18c6682007-04-17 13:26:50 -04004564 size_diff = old_size - new_size;
4565
4566 BUG_ON(slot < 0);
4567 BUG_ON(slot >= nritems);
4568
4569 /*
4570 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4571 */
4572 /* first correct the data pointers */
4573 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004574 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004575 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004576
Chris Masoncfed81a2012-03-03 07:40:03 -05004577 ioff = btrfs_token_item_offset(leaf, item, &token);
4578 btrfs_set_token_item_offset(leaf, item,
4579 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004580 }
Chris Masondb945352007-10-15 16:15:53 -04004581
Chris Masonb18c6682007-04-17 13:26:50 -04004582 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004583 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004584 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4585 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004586 data_end, old_data_start + new_size - data_end);
4587 } else {
4588 struct btrfs_disk_key disk_key;
4589 u64 offset;
4590
4591 btrfs_item_key(leaf, &disk_key, slot);
4592
4593 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4594 unsigned long ptr;
4595 struct btrfs_file_extent_item *fi;
4596
4597 fi = btrfs_item_ptr(leaf, slot,
4598 struct btrfs_file_extent_item);
4599 fi = (struct btrfs_file_extent_item *)(
4600 (unsigned long)fi - size_diff);
4601
4602 if (btrfs_file_extent_type(leaf, fi) ==
4603 BTRFS_FILE_EXTENT_INLINE) {
4604 ptr = btrfs_item_ptr_offset(leaf, slot);
4605 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004606 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004607 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004608 }
4609 }
4610
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004611 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4612 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004613 data_end, old_data_start - data_end);
4614
4615 offset = btrfs_disk_key_offset(&disk_key);
4616 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4617 btrfs_set_item_key(leaf, &disk_key, slot);
4618 if (slot == 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004619 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004620 }
Chris Mason5f39d392007-10-15 16:14:19 -04004621
Ross Kirkdd3cc162013-09-16 15:58:09 +01004622 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004623 btrfs_set_item_size(leaf, item, new_size);
4624 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004625
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004626 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004627 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004628 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004629 }
Chris Masonb18c6682007-04-17 13:26:50 -04004630}
4631
Chris Masond352ac62008-09-29 15:18:18 -04004632/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004633 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004634 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004635void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004636 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004637{
Chris Mason6567e832007-04-16 09:22:45 -04004638 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004639 struct extent_buffer *leaf;
4640 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004641 u32 nritems;
4642 unsigned int data_end;
4643 unsigned int old_data;
4644 unsigned int old_size;
4645 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004646 struct btrfs_map_token token;
4647
4648 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004649
Chris Mason5f39d392007-10-15 16:14:19 -04004650 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004651
Chris Mason5f39d392007-10-15 16:14:19 -04004652 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004653 data_end = leaf_data_end(fs_info, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004654
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004655 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004656 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004657 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004658 }
Chris Mason6567e832007-04-16 09:22:45 -04004659 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004660 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004661
4662 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004663 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004664 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004665 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4666 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004667 BUG_ON(1);
4668 }
Chris Mason6567e832007-04-16 09:22:45 -04004669
4670 /*
4671 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4672 */
4673 /* first correct the data pointers */
4674 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004675 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004676 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004677
Chris Masoncfed81a2012-03-03 07:40:03 -05004678 ioff = btrfs_token_item_offset(leaf, item, &token);
4679 btrfs_set_token_item_offset(leaf, item,
4680 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004681 }
Chris Mason5f39d392007-10-15 16:14:19 -04004682
Chris Mason6567e832007-04-16 09:22:45 -04004683 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004684 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4685 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004686 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004687
Chris Mason6567e832007-04-16 09:22:45 -04004688 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004689 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004690 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004691 btrfs_set_item_size(leaf, item, old_size + data_size);
4692 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004693
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004694 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004695 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004696 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004697 }
Chris Mason6567e832007-04-16 09:22:45 -04004698}
4699
Chris Mason74123bd2007-02-02 11:05:29 -05004700/*
Chris Mason44871b12009-03-13 10:04:31 -04004701 * this is a helper for btrfs_insert_empty_items, the main goal here is
4702 * to save stack depth by doing the bulk of the work in a function
4703 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004704 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004705void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004706 const struct btrfs_key *cpu_key, u32 *data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004707 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004709 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004710 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004711 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004712 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004713 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004714 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004715 struct extent_buffer *leaf;
4716 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004717 struct btrfs_map_token token;
4718
Filipe Manana24cdc842014-07-28 19:34:35 +01004719 if (path->slots[0] == 0) {
4720 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004721 fixup_low_keys(fs_info, path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004722 }
4723 btrfs_unlock_up_safe(path, 1);
4724
Chris Masoncfed81a2012-03-03 07:40:03 -05004725 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004726
Chris Mason5f39d392007-10-15 16:14:19 -04004727 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004728 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004729
Chris Mason5f39d392007-10-15 16:14:19 -04004730 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004731 data_end = leaf_data_end(fs_info, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004732
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004733 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004734 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004735 btrfs_crit(fs_info, "not enough freespace need %u have %d",
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004736 total_size, btrfs_leaf_free_space(fs_info, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004737 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004738 }
Chris Mason5f39d392007-10-15 16:14:19 -04004739
Chris Masonbe0e5c02007-01-26 15:51:26 -05004740 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004741 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004742
Chris Mason5f39d392007-10-15 16:14:19 -04004743 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004744 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004745 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004746 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004747 BUG_ON(1);
4748 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004749 /*
4750 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4751 */
4752 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004753 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004754 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004755
Jeff Mahoney62e85572016-09-20 10:05:01 -04004756 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004757 ioff = btrfs_token_item_offset(leaf, item, &token);
4758 btrfs_set_token_item_offset(leaf, item,
4759 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004760 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004761 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004762 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004763 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004764 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004765
4766 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004767 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4768 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004769 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004770 data_end = old_data;
4771 }
Chris Mason5f39d392007-10-15 16:14:19 -04004772
Chris Mason62e27492007-03-15 12:56:47 -04004773 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004774 for (i = 0; i < nr; i++) {
4775 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4776 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004777 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004778 btrfs_set_token_item_offset(leaf, item,
4779 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004780 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004781 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004782 }
Chris Mason44871b12009-03-13 10:04:31 -04004783
Chris Mason9c583092008-01-29 15:15:18 -05004784 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004785 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004786
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004787 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004788 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004789 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004790 }
Chris Mason44871b12009-03-13 10:04:31 -04004791}
4792
4793/*
4794 * Given a key and some data, insert items into the tree.
4795 * This does all the path init required, making room in the tree if needed.
4796 */
4797int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4798 struct btrfs_root *root,
4799 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004800 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004801 int nr)
4802{
Chris Mason44871b12009-03-13 10:04:31 -04004803 int ret = 0;
4804 int slot;
4805 int i;
4806 u32 total_size = 0;
4807 u32 total_data = 0;
4808
4809 for (i = 0; i < nr; i++)
4810 total_data += data_size[i];
4811
4812 total_size = total_data + (nr * sizeof(struct btrfs_item));
4813 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4814 if (ret == 0)
4815 return -EEXIST;
4816 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004817 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004818
Chris Mason44871b12009-03-13 10:04:31 -04004819 slot = path->slots[0];
4820 BUG_ON(slot < 0);
4821
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004822 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004823 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004824 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004825}
4826
4827/*
4828 * Given a key and some data, insert an item into the tree.
4829 * This does all the path init required, making room in the tree if needed.
4830 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004831int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4832 const struct btrfs_key *cpu_key, void *data,
4833 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004834{
4835 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004836 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004837 struct extent_buffer *leaf;
4838 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004839
Chris Mason2c90e5d2007-04-02 10:50:19 -04004840 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004841 if (!path)
4842 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004843 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004844 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004845 leaf = path->nodes[0];
4846 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4847 write_extent_buffer(leaf, data, ptr, data_size);
4848 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004849 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004850 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004851 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004852}
4853
Chris Mason74123bd2007-02-02 11:05:29 -05004854/*
Chris Mason5de08d72007-02-24 06:24:44 -05004855 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004856 *
Chris Masond352ac62008-09-29 15:18:18 -04004857 * the tree should have been previously balanced so the deletion does not
4858 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004859 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004860static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4861 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004863 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004864 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004865 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004866 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004867
Chris Mason5f39d392007-10-15 16:14:19 -04004868 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004869 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004870 if (level) {
4871 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004872 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004873 BUG_ON(ret < 0);
4874 }
Chris Mason5f39d392007-10-15 16:14:19 -04004875 memmove_extent_buffer(parent,
4876 btrfs_node_key_ptr_offset(slot),
4877 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004878 sizeof(struct btrfs_key_ptr) *
4879 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004880 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004881 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4882 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004883 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004884 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004885
Chris Mason7518a232007-03-12 12:01:18 -04004886 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004887 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004888 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004889 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004890 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004891 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004892 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004893 struct btrfs_disk_key disk_key;
4894
4895 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004896 fixup_low_keys(fs_info, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004897 }
Chris Masond6025572007-03-30 14:27:56 -04004898 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004899}
4900
Chris Mason74123bd2007-02-02 11:05:29 -05004901/*
Chris Mason323ac952008-10-01 19:05:46 -04004902 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004903 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004904 *
4905 * This deletes the pointer in path->nodes[1] and frees the leaf
4906 * block extent. zero is returned if it all worked out, < 0 otherwise.
4907 *
4908 * The path must have already been setup for deleting the leaf, including
4909 * all the proper balancing. path->nodes[1] must be locked.
4910 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004911static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4912 struct btrfs_root *root,
4913 struct btrfs_path *path,
4914 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004915{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004916 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004917 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004918
Chris Mason4d081c42009-02-04 09:31:28 -05004919 /*
4920 * btrfs_free_extent is expensive, we want to make sure we
4921 * aren't holding any locks when we call it
4922 */
4923 btrfs_unlock_up_safe(path, 0);
4924
Yan, Zhengf0486c62010-05-16 10:46:25 -04004925 root_sub_used(root, leaf->len);
4926
Josef Bacik3083ee22012-03-09 16:01:49 -05004927 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004928 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004929 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004930}
4931/*
Chris Mason74123bd2007-02-02 11:05:29 -05004932 * delete the item at the leaf level in path. If that empties
4933 * the leaf, remove it from the tree
4934 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004935int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4936 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004937{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004938 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004939 struct extent_buffer *leaf;
4940 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004941 u32 last_off;
4942 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004943 int ret = 0;
4944 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004945 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004946 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004947 struct btrfs_map_token token;
4948
4949 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004950
Chris Mason5f39d392007-10-15 16:14:19 -04004951 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004952 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4953
4954 for (i = 0; i < nr; i++)
4955 dsize += btrfs_item_size_nr(leaf, slot + i);
4956
Chris Mason5f39d392007-10-15 16:14:19 -04004957 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004958
Chris Mason85e21ba2008-01-29 15:11:36 -05004959 if (slot + nr != nritems) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004960 int data_end = leaf_data_end(fs_info, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004961
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004962 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004963 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004964 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004965 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004966
Chris Mason85e21ba2008-01-29 15:11:36 -05004967 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004968 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004969
Ross Kirkdd3cc162013-09-16 15:58:09 +01004970 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004971 ioff = btrfs_token_item_offset(leaf, item, &token);
4972 btrfs_set_token_item_offset(leaf, item,
4973 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004974 }
Chris Masondb945352007-10-15 16:15:53 -04004975
Chris Mason5f39d392007-10-15 16:14:19 -04004976 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004977 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004978 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004979 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004980 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004981 btrfs_set_header_nritems(leaf, nritems - nr);
4982 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004983
Chris Mason74123bd2007-02-02 11:05:29 -05004984 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004985 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004986 if (leaf == root->node) {
4987 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004988 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004989 btrfs_set_path_blocking(path);
David Sterba7c302b42017-02-10 18:47:57 +01004990 clean_tree_block(fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004991 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004992 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004993 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004994 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004995 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004996 struct btrfs_disk_key disk_key;
4997
4998 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004999 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005000 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005001
Chris Mason74123bd2007-02-02 11:05:29 -05005002 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005003 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005004 /* push_leaf_left fixes the path.
5005 * make sure the path still points to our leaf
5006 * for possible call to del_ptr below
5007 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005008 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005009 extent_buffer_get(leaf);
5010
Chris Masonb9473432009-03-13 11:00:37 -04005011 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005012 wret = push_leaf_left(trans, root, path, 1, 1,
5013 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005014 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005015 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005016
5017 if (path->nodes[0] == leaf &&
5018 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005019 wret = push_leaf_right(trans, root, path, 1,
5020 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005021 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005022 ret = wret;
5023 }
Chris Mason5f39d392007-10-15 16:14:19 -04005024
5025 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005026 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005027 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005028 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005029 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005030 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005031 /* if we're still in the path, make sure
5032 * we're dirty. Otherwise, one of the
5033 * push_leaf functions must have already
5034 * dirtied this buffer
5035 */
5036 if (path->nodes[0] == leaf)
5037 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005038 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005039 }
Chris Masond5719762007-03-23 10:01:08 -04005040 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005041 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005042 }
5043 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005044 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005045}
5046
Chris Mason97571fd2007-02-24 13:39:08 -05005047/*
Chris Mason925baed2008-06-25 16:01:30 -04005048 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005049 * returns 0 if it found something or 1 if there are no lesser leaves.
5050 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005051 *
5052 * This may release the path, and so you may lose any locks held at the
5053 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005054 */
Josef Bacik16e75492013-10-22 12:18:51 -04005055int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005056{
Chris Mason925baed2008-06-25 16:01:30 -04005057 struct btrfs_key key;
5058 struct btrfs_disk_key found_key;
5059 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005060
Chris Mason925baed2008-06-25 16:01:30 -04005061 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005062
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005063 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005064 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005065 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005066 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005067 key.offset = (u64)-1;
5068 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005069 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005070 key.type = (u8)-1;
5071 key.offset = (u64)-1;
5072 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005073 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005074 }
Chris Mason7bb86312007-12-11 09:25:06 -05005075
David Sterbab3b4aa72011-04-21 01:20:15 +02005076 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005077 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5078 if (ret < 0)
5079 return ret;
5080 btrfs_item_key(path->nodes[0], &found_key, 0);
5081 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005082 /*
5083 * We might have had an item with the previous key in the tree right
5084 * before we released our path. And after we released our path, that
5085 * item might have been pushed to the first slot (0) of the leaf we
5086 * were holding due to a tree balance. Alternatively, an item with the
5087 * previous key can exist as the only element of a leaf (big fat item).
5088 * Therefore account for these 2 cases, so that our callers (like
5089 * btrfs_previous_item) don't miss an existing item with a key matching
5090 * the previous key we computed above.
5091 */
5092 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005093 return 0;
5094 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005095}
5096
Chris Mason3f157a22008-06-25 16:01:31 -04005097/*
5098 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005099 * for nodes or leaves that are have a minimum transaction id.
5100 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005101 *
5102 * This does not cow, but it does stuff the starting key it finds back
5103 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5104 * key and get a writable path.
5105 *
Chris Mason3f157a22008-06-25 16:01:31 -04005106 * This honors path->lowest_level to prevent descent past a given level
5107 * of the tree.
5108 *
Chris Masond352ac62008-09-29 15:18:18 -04005109 * min_trans indicates the oldest transaction that you are interested
5110 * in walking through. Any nodes or leaves older than min_trans are
5111 * skipped over (without reading them).
5112 *
Chris Mason3f157a22008-06-25 16:01:31 -04005113 * returns zero if something useful was found, < 0 on error and 1 if there
5114 * was nothing in the tree that matched the search criteria.
5115 */
5116int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005117 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005118 u64 min_trans)
5119{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005120 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason3f157a22008-06-25 16:01:31 -04005121 struct extent_buffer *cur;
5122 struct btrfs_key found_key;
5123 int slot;
Yan96524802008-07-24 12:19:49 -04005124 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005125 u32 nritems;
5126 int level;
5127 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005128 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005129
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005130 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005131again:
Chris Masonbd681512011-07-16 15:23:14 -04005132 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005133 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005134 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005135 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005136 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005137
5138 if (btrfs_header_generation(cur) < min_trans) {
5139 ret = 1;
5140 goto out;
5141 }
Chris Masond3977122009-01-05 21:25:51 -05005142 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005143 nritems = btrfs_header_nritems(cur);
5144 level = btrfs_header_level(cur);
Nikolay Borisova74b35e2017-12-08 16:27:43 +02005145 sret = btrfs_bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005146
Chris Mason323ac952008-10-01 19:05:46 -04005147 /* at the lowest level, we're done, setup the path and exit */
5148 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005149 if (slot >= nritems)
5150 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005151 ret = 0;
5152 path->slots[level] = slot;
5153 btrfs_item_key_to_cpu(cur, &found_key, slot);
5154 goto out;
5155 }
Yan96524802008-07-24 12:19:49 -04005156 if (sret && slot > 0)
5157 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005158 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005159 * check this node pointer against the min_trans parameters.
5160 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005161 */
Chris Masond3977122009-01-05 21:25:51 -05005162 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005163 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005164
Chris Mason3f157a22008-06-25 16:01:31 -04005165 gen = btrfs_node_ptr_generation(cur, slot);
5166 if (gen < min_trans) {
5167 slot++;
5168 continue;
5169 }
Eric Sandeende78b512013-01-31 18:21:12 +00005170 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005171 }
Chris Masone02119d2008-09-05 16:13:11 -04005172find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005173 /*
5174 * we didn't find a candidate key in this node, walk forward
5175 * and find another one
5176 */
5177 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005178 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005179 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005180 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005181 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005182 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005183 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005184 goto again;
5185 } else {
5186 goto out;
5187 }
5188 }
5189 /* save our key for returning back */
5190 btrfs_node_key_to_cpu(cur, &found_key, slot);
5191 path->slots[level] = slot;
5192 if (level == path->lowest_level) {
5193 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005194 goto out;
5195 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005196 btrfs_set_path_blocking(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005197 cur = read_node_slot(fs_info, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005198 if (IS_ERR(cur)) {
5199 ret = PTR_ERR(cur);
5200 goto out;
5201 }
Chris Mason3f157a22008-06-25 16:01:31 -04005202
Chris Masonbd681512011-07-16 15:23:14 -04005203 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005204
Chris Masonbd681512011-07-16 15:23:14 -04005205 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005206 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005207 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005208 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005209 }
5210out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005211 path->keep_locks = keep_locks;
5212 if (ret == 0) {
5213 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5214 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005215 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005216 }
Chris Mason3f157a22008-06-25 16:01:31 -04005217 return ret;
5218}
5219
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005220static int tree_move_down(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005221 struct btrfs_path *path,
David Sterbaab6a43e2017-02-10 19:24:53 +01005222 int *level)
Alexander Block70698302012-06-05 21:07:48 +02005223{
Liu Bofb770ae2016-07-05 12:10:14 -07005224 struct extent_buffer *eb;
5225
Chris Mason74dd17f2012-08-07 16:25:13 -04005226 BUG_ON(*level == 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005227 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
Liu Bofb770ae2016-07-05 12:10:14 -07005228 if (IS_ERR(eb))
5229 return PTR_ERR(eb);
5230
5231 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005232 path->slots[*level - 1] = 0;
5233 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005234 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005235}
5236
David Sterbaf1e30262017-02-10 19:25:51 +01005237static int tree_move_next_or_upnext(struct btrfs_path *path,
Alexander Block70698302012-06-05 21:07:48 +02005238 int *level, int root_level)
5239{
5240 int ret = 0;
5241 int nritems;
5242 nritems = btrfs_header_nritems(path->nodes[*level]);
5243
5244 path->slots[*level]++;
5245
Chris Mason74dd17f2012-08-07 16:25:13 -04005246 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005247 if (*level == root_level)
5248 return -1;
5249
5250 /* move upnext */
5251 path->slots[*level] = 0;
5252 free_extent_buffer(path->nodes[*level]);
5253 path->nodes[*level] = NULL;
5254 (*level)++;
5255 path->slots[*level]++;
5256
5257 nritems = btrfs_header_nritems(path->nodes[*level]);
5258 ret = 1;
5259 }
5260 return ret;
5261}
5262
5263/*
5264 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5265 * or down.
5266 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005267static int tree_advance(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005268 struct btrfs_path *path,
5269 int *level, int root_level,
5270 int allow_down,
5271 struct btrfs_key *key)
5272{
5273 int ret;
5274
5275 if (*level == 0 || !allow_down) {
David Sterbaf1e30262017-02-10 19:25:51 +01005276 ret = tree_move_next_or_upnext(path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005277 } else {
David Sterbaab6a43e2017-02-10 19:24:53 +01005278 ret = tree_move_down(fs_info, path, level);
Alexander Block70698302012-06-05 21:07:48 +02005279 }
5280 if (ret >= 0) {
5281 if (*level == 0)
5282 btrfs_item_key_to_cpu(path->nodes[*level], key,
5283 path->slots[*level]);
5284 else
5285 btrfs_node_key_to_cpu(path->nodes[*level], key,
5286 path->slots[*level]);
5287 }
5288 return ret;
5289}
5290
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005291static int tree_compare_item(struct btrfs_path *left_path,
Alexander Block70698302012-06-05 21:07:48 +02005292 struct btrfs_path *right_path,
5293 char *tmp_buf)
5294{
5295 int cmp;
5296 int len1, len2;
5297 unsigned long off1, off2;
5298
5299 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5300 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5301 if (len1 != len2)
5302 return 1;
5303
5304 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5305 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5306 right_path->slots[0]);
5307
5308 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5309
5310 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5311 if (cmp)
5312 return 1;
5313 return 0;
5314}
5315
5316#define ADVANCE 1
5317#define ADVANCE_ONLY_NEXT -1
5318
5319/*
5320 * This function compares two trees and calls the provided callback for
5321 * every changed/new/deleted item it finds.
5322 * If shared tree blocks are encountered, whole subtrees are skipped, making
5323 * the compare pretty fast on snapshotted subvolumes.
5324 *
5325 * This currently works on commit roots only. As commit roots are read only,
5326 * we don't do any locking. The commit roots are protected with transactions.
5327 * Transactions are ended and rejoined when a commit is tried in between.
5328 *
5329 * This function checks for modifications done to the trees while comparing.
5330 * If it detects a change, it aborts immediately.
5331 */
5332int btrfs_compare_trees(struct btrfs_root *left_root,
5333 struct btrfs_root *right_root,
5334 btrfs_changed_cb_t changed_cb, void *ctx)
5335{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005336 struct btrfs_fs_info *fs_info = left_root->fs_info;
Alexander Block70698302012-06-05 21:07:48 +02005337 int ret;
5338 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005339 struct btrfs_path *left_path = NULL;
5340 struct btrfs_path *right_path = NULL;
5341 struct btrfs_key left_key;
5342 struct btrfs_key right_key;
5343 char *tmp_buf = NULL;
5344 int left_root_level;
5345 int right_root_level;
5346 int left_level;
5347 int right_level;
5348 int left_end_reached;
5349 int right_end_reached;
5350 int advance_left;
5351 int advance_right;
5352 u64 left_blockptr;
5353 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005354 u64 left_gen;
5355 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005356
5357 left_path = btrfs_alloc_path();
5358 if (!left_path) {
5359 ret = -ENOMEM;
5360 goto out;
5361 }
5362 right_path = btrfs_alloc_path();
5363 if (!right_path) {
5364 ret = -ENOMEM;
5365 goto out;
5366 }
5367
Michal Hocko752ade62017-05-08 15:57:27 -07005368 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
Alexander Block70698302012-06-05 21:07:48 +02005369 if (!tmp_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07005370 ret = -ENOMEM;
5371 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005372 }
5373
5374 left_path->search_commit_root = 1;
5375 left_path->skip_locking = 1;
5376 right_path->search_commit_root = 1;
5377 right_path->skip_locking = 1;
5378
Alexander Block70698302012-06-05 21:07:48 +02005379 /*
5380 * Strategy: Go to the first items of both trees. Then do
5381 *
5382 * If both trees are at level 0
5383 * Compare keys of current items
5384 * If left < right treat left item as new, advance left tree
5385 * and repeat
5386 * If left > right treat right item as deleted, advance right tree
5387 * and repeat
5388 * If left == right do deep compare of items, treat as changed if
5389 * needed, advance both trees and repeat
5390 * If both trees are at the same level but not at level 0
5391 * Compare keys of current nodes/leafs
5392 * If left < right advance left tree and repeat
5393 * If left > right advance right tree and repeat
5394 * If left == right compare blockptrs of the next nodes/leafs
5395 * If they match advance both trees but stay at the same level
5396 * and repeat
5397 * If they don't match advance both trees while allowing to go
5398 * deeper and repeat
5399 * If tree levels are different
5400 * Advance the tree that needs it and repeat
5401 *
5402 * Advancing a tree means:
5403 * If we are at level 0, try to go to the next slot. If that's not
5404 * possible, go one level up and repeat. Stop when we found a level
5405 * where we could go to the next slot. We may at this point be on a
5406 * node or a leaf.
5407 *
5408 * If we are not at level 0 and not on shared tree blocks, go one
5409 * level deeper.
5410 *
5411 * If we are not at level 0 and on shared tree blocks, go one slot to
5412 * the right if possible or go up and right.
5413 */
5414
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005415 down_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005416 left_level = btrfs_header_level(left_root->commit_root);
5417 left_root_level = left_level;
5418 left_path->nodes[left_level] = left_root->commit_root;
5419 extent_buffer_get(left_path->nodes[left_level]);
5420
5421 right_level = btrfs_header_level(right_root->commit_root);
5422 right_root_level = right_level;
5423 right_path->nodes[right_level] = right_root->commit_root;
5424 extent_buffer_get(right_path->nodes[right_level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005425 up_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005426
5427 if (left_level == 0)
5428 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5429 &left_key, left_path->slots[left_level]);
5430 else
5431 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5432 &left_key, left_path->slots[left_level]);
5433 if (right_level == 0)
5434 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5435 &right_key, right_path->slots[right_level]);
5436 else
5437 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5438 &right_key, right_path->slots[right_level]);
5439
5440 left_end_reached = right_end_reached = 0;
5441 advance_left = advance_right = 0;
5442
5443 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005444 if (advance_left && !left_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005445 ret = tree_advance(fs_info, left_path, &left_level,
Alexander Block70698302012-06-05 21:07:48 +02005446 left_root_level,
5447 advance_left != ADVANCE_ONLY_NEXT,
5448 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005449 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005450 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005451 else if (ret < 0)
5452 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005453 advance_left = 0;
5454 }
5455 if (advance_right && !right_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005456 ret = tree_advance(fs_info, right_path, &right_level,
Alexander Block70698302012-06-05 21:07:48 +02005457 right_root_level,
5458 advance_right != ADVANCE_ONLY_NEXT,
5459 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005460 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005461 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005462 else if (ret < 0)
5463 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005464 advance_right = 0;
5465 }
5466
5467 if (left_end_reached && right_end_reached) {
5468 ret = 0;
5469 goto out;
5470 } else if (left_end_reached) {
5471 if (right_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005472 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005473 &right_key,
5474 BTRFS_COMPARE_TREE_DELETED,
5475 ctx);
5476 if (ret < 0)
5477 goto out;
5478 }
5479 advance_right = ADVANCE;
5480 continue;
5481 } else if (right_end_reached) {
5482 if (left_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005483 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005484 &left_key,
5485 BTRFS_COMPARE_TREE_NEW,
5486 ctx);
5487 if (ret < 0)
5488 goto out;
5489 }
5490 advance_left = ADVANCE;
5491 continue;
5492 }
5493
5494 if (left_level == 0 && right_level == 0) {
5495 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5496 if (cmp < 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005497 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005498 &left_key,
5499 BTRFS_COMPARE_TREE_NEW,
5500 ctx);
5501 if (ret < 0)
5502 goto out;
5503 advance_left = ADVANCE;
5504 } else if (cmp > 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005505 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005506 &right_key,
5507 BTRFS_COMPARE_TREE_DELETED,
5508 ctx);
5509 if (ret < 0)
5510 goto out;
5511 advance_right = ADVANCE;
5512 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005513 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005514
Chris Mason74dd17f2012-08-07 16:25:13 -04005515 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005516 ret = tree_compare_item(left_path, right_path,
5517 tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005518 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005519 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005520 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005521 result = BTRFS_COMPARE_TREE_SAME;
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005522 ret = changed_cb(left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005523 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005524 if (ret < 0)
5525 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005526 advance_left = ADVANCE;
5527 advance_right = ADVANCE;
5528 }
5529 } else if (left_level == right_level) {
5530 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5531 if (cmp < 0) {
5532 advance_left = ADVANCE;
5533 } else if (cmp > 0) {
5534 advance_right = ADVANCE;
5535 } else {
5536 left_blockptr = btrfs_node_blockptr(
5537 left_path->nodes[left_level],
5538 left_path->slots[left_level]);
5539 right_blockptr = btrfs_node_blockptr(
5540 right_path->nodes[right_level],
5541 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005542 left_gen = btrfs_node_ptr_generation(
5543 left_path->nodes[left_level],
5544 left_path->slots[left_level]);
5545 right_gen = btrfs_node_ptr_generation(
5546 right_path->nodes[right_level],
5547 right_path->slots[right_level]);
5548 if (left_blockptr == right_blockptr &&
5549 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005550 /*
5551 * As we're on a shared block, don't
5552 * allow to go deeper.
5553 */
5554 advance_left = ADVANCE_ONLY_NEXT;
5555 advance_right = ADVANCE_ONLY_NEXT;
5556 } else {
5557 advance_left = ADVANCE;
5558 advance_right = ADVANCE;
5559 }
5560 }
5561 } else if (left_level < right_level) {
5562 advance_right = ADVANCE;
5563 } else {
5564 advance_left = ADVANCE;
5565 }
5566 }
5567
5568out:
5569 btrfs_free_path(left_path);
5570 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005571 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005572 return ret;
5573}
5574
Chris Mason3f157a22008-06-25 16:01:31 -04005575/*
5576 * this is similar to btrfs_next_leaf, but does not try to preserve
5577 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005578 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005579 *
5580 * 0 is returned if another key is found, < 0 if there are any errors
5581 * and 1 is returned if there are no higher keys in the tree
5582 *
5583 * path->keep_locks should be set to 1 on the search made before
5584 * calling this function.
5585 */
Chris Masone7a84562008-06-25 16:01:31 -04005586int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005587 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005588{
Chris Masone7a84562008-06-25 16:01:31 -04005589 int slot;
5590 struct extent_buffer *c;
5591
Chris Mason934d3752008-12-08 16:43:10 -05005592 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005593 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005594 if (!path->nodes[level])
5595 return 1;
5596
5597 slot = path->slots[level] + 1;
5598 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005599next:
Chris Masone7a84562008-06-25 16:01:31 -04005600 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005601 int ret;
5602 int orig_lowest;
5603 struct btrfs_key cur_key;
5604 if (level + 1 >= BTRFS_MAX_LEVEL ||
5605 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005606 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005607
5608 if (path->locks[level + 1]) {
5609 level++;
5610 continue;
5611 }
5612
5613 slot = btrfs_header_nritems(c) - 1;
5614 if (level == 0)
5615 btrfs_item_key_to_cpu(c, &cur_key, slot);
5616 else
5617 btrfs_node_key_to_cpu(c, &cur_key, slot);
5618
5619 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005620 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005621 path->lowest_level = level;
5622 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5623 0, 0);
5624 path->lowest_level = orig_lowest;
5625 if (ret < 0)
5626 return ret;
5627
5628 c = path->nodes[level];
5629 slot = path->slots[level];
5630 if (ret == 0)
5631 slot++;
5632 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005633 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005634
Chris Masone7a84562008-06-25 16:01:31 -04005635 if (level == 0)
5636 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005637 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005638 u64 gen = btrfs_node_ptr_generation(c, slot);
5639
Chris Mason3f157a22008-06-25 16:01:31 -04005640 if (gen < min_trans) {
5641 slot++;
5642 goto next;
5643 }
Chris Masone7a84562008-06-25 16:01:31 -04005644 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005645 }
Chris Masone7a84562008-06-25 16:01:31 -04005646 return 0;
5647 }
5648 return 1;
5649}
5650
Chris Mason7bb86312007-12-11 09:25:06 -05005651/*
Chris Mason925baed2008-06-25 16:01:30 -04005652 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005653 * returns 0 if it found something or 1 if there are no greater leaves.
5654 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005655 */
Chris Mason234b63a2007-03-13 10:46:10 -04005656int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005657{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005658 return btrfs_next_old_leaf(root, path, 0);
5659}
5660
5661int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5662 u64 time_seq)
5663{
Chris Masond97e63b2007-02-20 16:40:44 -05005664 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005665 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005666 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005667 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005668 struct btrfs_key key;
5669 u32 nritems;
5670 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005671 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005672 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005673
5674 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005675 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005676 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005677
Chris Mason8e73f272009-04-03 10:14:18 -04005678 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5679again:
5680 level = 1;
5681 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005682 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005683 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005684
Chris Masona2135012008-06-25 16:01:30 -04005685 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005686 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005687
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005688 if (time_seq)
5689 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5690 else
5691 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005692 path->keep_locks = 0;
5693
5694 if (ret < 0)
5695 return ret;
5696
Chris Masona2135012008-06-25 16:01:30 -04005697 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005698 /*
5699 * by releasing the path above we dropped all our locks. A balance
5700 * could have added more items next to the key that used to be
5701 * at the very end of the block. So, check again here and
5702 * advance the path if there are now more items available.
5703 */
Chris Masona2135012008-06-25 16:01:30 -04005704 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005705 if (ret == 0)
5706 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005707 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005708 goto done;
5709 }
Liu Bo0b43e042014-06-09 11:04:49 +08005710 /*
5711 * So the above check misses one case:
5712 * - after releasing the path above, someone has removed the item that
5713 * used to be at the very end of the block, and balance between leafs
5714 * gets another one with bigger key.offset to replace it.
5715 *
5716 * This one should be returned as well, or we can get leaf corruption
5717 * later(esp. in __btrfs_drop_extents()).
5718 *
5719 * And a bit more explanation about this check,
5720 * with ret > 0, the key isn't found, the path points to the slot
5721 * where it should be inserted, so the path->slots[0] item must be the
5722 * bigger one.
5723 */
5724 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5725 ret = 0;
5726 goto done;
5727 }
Chris Masond97e63b2007-02-20 16:40:44 -05005728
Chris Masond3977122009-01-05 21:25:51 -05005729 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005730 if (!path->nodes[level]) {
5731 ret = 1;
5732 goto done;
5733 }
Chris Mason5f39d392007-10-15 16:14:19 -04005734
Chris Masond97e63b2007-02-20 16:40:44 -05005735 slot = path->slots[level] + 1;
5736 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005737 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005738 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005739 if (level == BTRFS_MAX_LEVEL) {
5740 ret = 1;
5741 goto done;
5742 }
Chris Masond97e63b2007-02-20 16:40:44 -05005743 continue;
5744 }
Chris Mason5f39d392007-10-15 16:14:19 -04005745
Chris Mason925baed2008-06-25 16:01:30 -04005746 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005747 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005748 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005749 }
Chris Mason5f39d392007-10-15 16:14:19 -04005750
Chris Mason8e73f272009-04-03 10:14:18 -04005751 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005752 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005753 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005754 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005755 if (ret == -EAGAIN)
5756 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Chris Mason76a05b32009-05-14 13:24:30 -04005758 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005759 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005760 goto done;
5761 }
5762
Chris Mason5cd57b22008-06-25 16:01:30 -04005763 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005764 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005765 if (!ret && time_seq) {
5766 /*
5767 * If we don't get the lock, we may be racing
5768 * with push_leaf_left, holding that lock while
5769 * itself waiting for the leaf we've currently
5770 * locked. To solve this situation, we give up
5771 * on our lock and cycle.
5772 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005773 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005774 btrfs_release_path(path);
5775 cond_resched();
5776 goto again;
5777 }
Chris Mason8e73f272009-04-03 10:14:18 -04005778 if (!ret) {
5779 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005780 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005781 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005782 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005783 }
Chris Mason31533fb2011-07-26 16:01:59 -04005784 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005785 }
Chris Masond97e63b2007-02-20 16:40:44 -05005786 break;
5787 }
5788 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005789 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005790 level--;
5791 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005792 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005793 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005794
Chris Mason5f39d392007-10-15 16:14:19 -04005795 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005796 path->nodes[level] = next;
5797 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005798 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005799 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005800 if (!level)
5801 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005802
Liu Bod07b8522017-01-30 12:23:42 -08005803 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005804 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005805 if (ret == -EAGAIN)
5806 goto again;
5807
Chris Mason76a05b32009-05-14 13:24:30 -04005808 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005809 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005810 goto done;
5811 }
5812
Chris Mason5cd57b22008-06-25 16:01:30 -04005813 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005814 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005815 if (!ret) {
5816 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005817 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005818 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005819 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005820 }
Chris Mason31533fb2011-07-26 16:01:59 -04005821 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005822 }
Chris Masond97e63b2007-02-20 16:40:44 -05005823 }
Chris Mason8e73f272009-04-03 10:14:18 -04005824 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005825done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005826 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005827 path->leave_spinning = old_spinning;
5828 if (!old_spinning)
5829 btrfs_set_path_blocking(path);
5830
5831 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005832}
Chris Mason0b86a832008-03-24 15:01:56 -04005833
Chris Mason3f157a22008-06-25 16:01:31 -04005834/*
5835 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5836 * searching until it gets past min_objectid or finds an item of 'type'
5837 *
5838 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5839 */
Chris Mason0b86a832008-03-24 15:01:56 -04005840int btrfs_previous_item(struct btrfs_root *root,
5841 struct btrfs_path *path, u64 min_objectid,
5842 int type)
5843{
5844 struct btrfs_key found_key;
5845 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005846 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005847 int ret;
5848
Chris Masond3977122009-01-05 21:25:51 -05005849 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005850 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005851 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005852 ret = btrfs_prev_leaf(root, path);
5853 if (ret != 0)
5854 return ret;
5855 } else {
5856 path->slots[0]--;
5857 }
5858 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005859 nritems = btrfs_header_nritems(leaf);
5860 if (nritems == 0)
5861 return 1;
5862 if (path->slots[0] == nritems)
5863 path->slots[0]--;
5864
Chris Mason0b86a832008-03-24 15:01:56 -04005865 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005866 if (found_key.objectid < min_objectid)
5867 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005868 if (found_key.type == type)
5869 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005870 if (found_key.objectid == min_objectid &&
5871 found_key.type < type)
5872 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005873 }
5874 return 1;
5875}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005876
5877/*
5878 * search in extent tree to find a previous Metadata/Data extent item with
5879 * min objecitd.
5880 *
5881 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5882 */
5883int btrfs_previous_extent_item(struct btrfs_root *root,
5884 struct btrfs_path *path, u64 min_objectid)
5885{
5886 struct btrfs_key found_key;
5887 struct extent_buffer *leaf;
5888 u32 nritems;
5889 int ret;
5890
5891 while (1) {
5892 if (path->slots[0] == 0) {
5893 btrfs_set_path_blocking(path);
5894 ret = btrfs_prev_leaf(root, path);
5895 if (ret != 0)
5896 return ret;
5897 } else {
5898 path->slots[0]--;
5899 }
5900 leaf = path->nodes[0];
5901 nritems = btrfs_header_nritems(leaf);
5902 if (nritems == 0)
5903 return 1;
5904 if (path->slots[0] == nritems)
5905 path->slots[0]--;
5906
5907 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5908 if (found_key.objectid < min_objectid)
5909 break;
5910 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5911 found_key.type == BTRFS_METADATA_ITEM_KEY)
5912 return 0;
5913 if (found_key.objectid == min_objectid &&
5914 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5915 break;
5916 }
5917 return 1;
5918}