blob: 6178fadf80a1e6c11d2e8406f0bcce95cb4d9f98 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
Chris Masond352ac62008-09-29 15:18:18 -04003 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Masona6b6e752007-10-15 16:22:39 -04006#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +02008#include <linux/rbtree.h>
David Sterbaadf02122017-05-31 19:44:31 +02009#include <linux/mm.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050010#include "ctree.h"
11#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040012#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040013#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040014#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050015
Chris Masone089f052007-03-16 16:20:31 -040016static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
17 *root, struct btrfs_path *path, int level);
Omar Sandoval310712b2017-01-17 23:24:37 -080018static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
19 const struct btrfs_key *ins_key, struct btrfs_path *path,
20 int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040021static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040022 struct btrfs_fs_info *fs_info,
23 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040024 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040025static int balance_node_right(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040026 struct btrfs_fs_info *fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -040027 struct extent_buffer *dst_buf,
28 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000029static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
30 int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050031
Chris Mason2c90e5d2007-04-02 10:50:19 -040032struct btrfs_path *btrfs_alloc_path(void)
33{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090034 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040035}
36
Chris Masonb4ce94d2009-02-04 09:25:08 -050037/*
38 * set all locked nodes in the path to blocking locks. This should
39 * be done before scheduling
40 */
41noinline void btrfs_set_path_blocking(struct btrfs_path *p)
42{
43 int i;
44 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040045 if (!p->nodes[i] || !p->locks[i])
46 continue;
47 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
48 if (p->locks[i] == BTRFS_READ_LOCK)
49 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
50 else if (p->locks[i] == BTRFS_WRITE_LOCK)
51 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050052 }
53}
54
55/*
56 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050057 *
58 * held is used to keep lockdep happy, when lockdep is enabled
59 * we set held to a blocking lock before we go around and
60 * retake all the spinlocks in the path. You can safely use NULL
61 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050062 */
Chris Mason4008c042009-02-12 14:09:45 -050063noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040064 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050065{
66 int i;
Chris Mason4008c042009-02-12 14:09:45 -050067
Chris Masonbd681512011-07-16 15:23:14 -040068 if (held) {
69 btrfs_set_lock_blocking_rw(held, held_rw);
70 if (held_rw == BTRFS_WRITE_LOCK)
71 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
72 else if (held_rw == BTRFS_READ_LOCK)
73 held_rw = BTRFS_READ_LOCK_BLOCKING;
74 }
Chris Mason4008c042009-02-12 14:09:45 -050075 btrfs_set_path_blocking(p);
Chris Mason4008c042009-02-12 14:09:45 -050076
77 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040078 if (p->nodes[i] && p->locks[i]) {
79 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
80 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
81 p->locks[i] = BTRFS_WRITE_LOCK;
82 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
83 p->locks[i] = BTRFS_READ_LOCK;
84 }
Chris Masonb4ce94d2009-02-04 09:25:08 -050085 }
Chris Mason4008c042009-02-12 14:09:45 -050086
Chris Mason4008c042009-02-12 14:09:45 -050087 if (held)
Chris Masonbd681512011-07-16 15:23:14 -040088 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Masonb4ce94d2009-02-04 09:25:08 -050089}
90
Chris Masond352ac62008-09-29 15:18:18 -040091/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -040092void btrfs_free_path(struct btrfs_path *p)
93{
Jesper Juhlff175d52010-12-25 21:22:30 +000094 if (!p)
95 return;
David Sterbab3b4aa72011-04-21 01:20:15 +020096 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -040097 kmem_cache_free(btrfs_path_cachep, p);
98}
99
Chris Masond352ac62008-09-29 15:18:18 -0400100/*
101 * path release drops references on the extent buffers in the path
102 * and it drops any locks held by this path
103 *
104 * It is safe to call this on paths that no locks or extent buffers held.
105 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200106noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500107{
108 int i;
Chris Masona2135012008-06-25 16:01:30 -0400109
Chris Mason234b63a2007-03-13 10:46:10 -0400110 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400111 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500112 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400113 continue;
114 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400115 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400116 p->locks[i] = 0;
117 }
Chris Mason5f39d392007-10-15 16:14:19 -0400118 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400119 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500120 }
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
124 * safely gets a reference on the root node of a tree. A lock
125 * is not taken, so a concurrent writer may put a different node
126 * at the root of the tree. See btrfs_lock_root_node for the
127 * looping required.
128 *
129 * The extent buffer returned by this has a reference taken, so
130 * it won't disappear. It may stop being the root of the tree
131 * at any time because there are no locks held.
132 */
Chris Mason925baed2008-06-25 16:01:30 -0400133struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
134{
135 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400136
Josef Bacik3083ee22012-03-09 16:01:49 -0500137 while (1) {
138 rcu_read_lock();
139 eb = rcu_dereference(root->node);
140
141 /*
142 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400143 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500144 * the inc_not_zero dance and if it doesn't work then
145 * synchronize_rcu and try again.
146 */
147 if (atomic_inc_not_zero(&eb->refs)) {
148 rcu_read_unlock();
149 break;
150 }
151 rcu_read_unlock();
152 synchronize_rcu();
153 }
Chris Mason925baed2008-06-25 16:01:30 -0400154 return eb;
155}
156
Chris Masond352ac62008-09-29 15:18:18 -0400157/* loop around taking references on and locking the root node of the
158 * tree until you end up with a lock on the root. A locked buffer
159 * is returned, with a reference held.
160 */
Chris Mason925baed2008-06-25 16:01:30 -0400161struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
162{
163 struct extent_buffer *eb;
164
Chris Masond3977122009-01-05 21:25:51 -0500165 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400166 eb = btrfs_root_node(root);
167 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400168 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400169 break;
Chris Mason925baed2008-06-25 16:01:30 -0400170 btrfs_tree_unlock(eb);
171 free_extent_buffer(eb);
172 }
173 return eb;
174}
175
Chris Masonbd681512011-07-16 15:23:14 -0400176/* loop around taking references on and locking the root node of the
177 * tree until you end up with a lock on the root. A locked buffer
178 * is returned, with a reference held.
179 */
Josef Bacik84f7d8e2017-09-29 15:43:49 -0400180struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400181{
182 struct extent_buffer *eb;
183
184 while (1) {
185 eb = btrfs_root_node(root);
186 btrfs_tree_read_lock(eb);
187 if (eb == root->node)
188 break;
189 btrfs_tree_read_unlock(eb);
190 free_extent_buffer(eb);
191 }
192 return eb;
193}
194
Chris Masond352ac62008-09-29 15:18:18 -0400195/* cowonly root (everything not a reference counted cow subvolume), just get
196 * put onto a simple dirty list. transaction.c walks this to make sure they
197 * get properly updated on disk.
198 */
Chris Mason0b86a832008-03-24 15:01:56 -0400199static void add_root_to_dirty_list(struct btrfs_root *root)
200{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400201 struct btrfs_fs_info *fs_info = root->fs_info;
202
Josef Bacike7070be2014-12-16 08:54:43 -0800203 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
204 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
205 return;
206
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400207 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800208 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
209 /* Want the extent tree to be the last on the list */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +0900210 if (root->root_key.objectid == BTRFS_EXTENT_TREE_OBJECTID)
Josef Bacike7070be2014-12-16 08:54:43 -0800211 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400212 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800213 else
214 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400215 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400216 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400217 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400218}
219
Chris Masond352ac62008-09-29 15:18:18 -0400220/*
221 * used by snapshot creation to make a copy of a root for a tree with
222 * a given objectid. The buffer with the new root node is returned in
223 * cow_ret, and this func returns zero on success or a negative error code.
224 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500225int btrfs_copy_root(struct btrfs_trans_handle *trans,
226 struct btrfs_root *root,
227 struct extent_buffer *buf,
228 struct extent_buffer **cow_ret, u64 new_root_objectid)
229{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400230 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500232 int ret = 0;
233 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400234 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500235
Miao Xie27cdeb72014-04-02 19:51:05 +0800236 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400237 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +0800238 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
239 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500240
241 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400242 if (level == 0)
243 btrfs_item_key(buf, &disk_key, 0);
244 else
245 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400246
David Sterba4d75f8a2014-06-15 01:54:12 +0200247 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
248 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400249 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500250 return PTR_ERR(cow);
251
David Sterba58e80122016-11-08 18:30:31 +0100252 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500253 btrfs_set_header_bytenr(cow, cow->start);
254 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
256 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
257 BTRFS_HEADER_FLAG_RELOC);
258 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
259 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
260 else
261 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500262
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400263 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -0500264
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700267 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400268 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700269 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500270
Chris Masonbe20aa92007-12-17 20:14:01 -0500271 if (ret)
272 return ret;
273
274 btrfs_mark_buffer_dirty(cow);
275 *cow_ret = cow;
276 return 0;
277}
278
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200279enum mod_log_op {
280 MOD_LOG_KEY_REPLACE,
281 MOD_LOG_KEY_ADD,
282 MOD_LOG_KEY_REMOVE,
283 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
284 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
285 MOD_LOG_MOVE_KEYS,
286 MOD_LOG_ROOT_REPLACE,
287};
288
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200289struct tree_mod_root {
290 u64 logical;
291 u8 level;
292};
293
294struct tree_mod_elem {
295 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530296 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200297 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200298 enum mod_log_op op;
299
300 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
301 int slot;
302
303 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
304 u64 generation;
305
306 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
307 struct btrfs_disk_key key;
308 u64 blockptr;
309
310 /* this is used for op == MOD_LOG_MOVE_KEYS */
David Sterbab6dfa352018-03-05 15:31:18 +0100311 struct {
312 int dst_slot;
313 int nr_items;
314 } move;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200315
316 /* this is used for op == MOD_LOG_ROOT_REPLACE */
317 struct tree_mod_root old_root;
318};
319
Jan Schmidt097b8a72012-06-21 11:08:04 +0200320/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700321 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000322 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700323static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000324{
325 return atomic64_inc_return(&fs_info->tree_mod_seq);
326}
327
328/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200329 * This adds a new blocker to the tree mod log's blocker list if the @elem
330 * passed does not already have a sequence number set. So when a caller expects
331 * to record tree modifications, it should ensure to set elem->seq to zero
332 * before calling btrfs_get_tree_mod_seq.
333 * Returns a fresh, unused tree log modification sequence number, even if no new
334 * blocker was added.
335 */
336u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
337 struct seq_list *elem)
338{
David Sterbab1a09f12018-03-05 15:43:41 +0100339 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200340 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200341 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700342 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
344 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200345 spin_unlock(&fs_info->tree_mod_seq_lock);
David Sterbab1a09f12018-03-05 15:43:41 +0100346 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200347
Josef Bacikfcebe452014-05-13 17:30:47 -0700348 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200349}
350
351void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 struct rb_root *tm_root;
355 struct rb_node *node;
356 struct rb_node *next;
357 struct seq_list *cur_elem;
358 struct tree_mod_elem *tm;
359 u64 min_seq = (u64)-1;
360 u64 seq_putting = elem->seq;
361
362 if (!seq_putting)
363 return;
364
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200365 spin_lock(&fs_info->tree_mod_seq_lock);
366 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200367 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200368
369 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200370 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200371 if (seq_putting > cur_elem->seq) {
372 /*
373 * blocker with lower sequence number exists, we
374 * cannot remove anything from the log
375 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200376 spin_unlock(&fs_info->tree_mod_seq_lock);
377 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 }
379 min_seq = cur_elem->seq;
380 }
381 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200382 spin_unlock(&fs_info->tree_mod_seq_lock);
383
384 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200385 * anything that's lower than the lowest existing (read: blocked)
386 * sequence number can be removed from the tree.
387 */
David Sterbab1a09f12018-03-05 15:43:41 +0100388 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200389 tm_root = &fs_info->tree_mod_log;
390 for (node = rb_first(tm_root); node; node = next) {
391 next = rb_next(node);
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800392 tm = rb_entry(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200393 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200394 continue;
395 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200396 kfree(tm);
397 }
David Sterbab1a09f12018-03-05 15:43:41 +0100398 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200399}
400
401/*
402 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530403 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200404 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530405 * The 'start address' is the logical address of the *new* root node
406 * for root replace operations, or the logical address of the affected
407 * block for all other operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000408 *
David Sterbab1a09f12018-03-05 15:43:41 +0100409 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 */
411static noinline int
412__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
413{
414 struct rb_root *tm_root;
415 struct rb_node **new;
416 struct rb_node *parent = NULL;
417 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200418
Josef Bacikfcebe452014-05-13 17:30:47 -0700419 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200421 tm_root = &fs_info->tree_mod_log;
422 new = &tm_root->rb_node;
423 while (*new) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800424 cur = rb_entry(*new, struct tree_mod_elem, node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200425 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530426 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200427 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530428 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200429 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200430 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200431 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200432 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200433 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000434 else
435 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436 }
437
438 rb_link_node(&tm->node, parent, new);
439 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000440 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200441}
442
Jan Schmidt097b8a72012-06-21 11:08:04 +0200443/*
444 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
445 * returns zero with the tree_mod_log_lock acquired. The caller must hold
446 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbab1a09f12018-03-05 15:43:41 +0100447 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200448 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200449static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
450 struct extent_buffer *eb) {
451 smp_mb();
452 if (list_empty(&(fs_info)->tree_mod_seq_list))
453 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200454 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200455 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000456
David Sterbab1a09f12018-03-05 15:43:41 +0100457 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000458 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbab1a09f12018-03-05 15:43:41 +0100459 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000460 return 1;
461 }
462
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200463 return 0;
464}
465
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000466/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
467static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
468 struct extent_buffer *eb)
469{
470 smp_mb();
471 if (list_empty(&(fs_info)->tree_mod_seq_list))
472 return 0;
473 if (eb && btrfs_header_level(eb) == 0)
474 return 0;
475
476 return 1;
477}
478
479static struct tree_mod_elem *
480alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
481 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200482{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200483 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200484
Josef Bacikc8cc6342013-07-01 16:18:19 -0400485 tm = kzalloc(sizeof(*tm), flags);
486 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000487 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200488
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530489 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200490 if (op != MOD_LOG_KEY_ADD) {
491 btrfs_node_key(eb, &tm->key, slot);
492 tm->blockptr = btrfs_node_blockptr(eb, slot);
493 }
494 tm->op = op;
495 tm->slot = slot;
496 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000497 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200498
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000499 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200500}
501
David Sterbae09c2ef2018-03-05 15:09:03 +0100502static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
503 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200504{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000505 struct tree_mod_elem *tm;
506 int ret;
507
David Sterbae09c2ef2018-03-05 15:09:03 +0100508 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200509 return 0;
510
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000511 tm = alloc_tree_mod_elem(eb, slot, op, flags);
512 if (!tm)
513 return -ENOMEM;
514
David Sterbae09c2ef2018-03-05 15:09:03 +0100515 if (tree_mod_dont_log(eb->fs_info, eb)) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000516 kfree(tm);
517 return 0;
518 }
519
David Sterbae09c2ef2018-03-05 15:09:03 +0100520 ret = __tree_mod_log_insert(eb->fs_info, tm);
David Sterbab1a09f12018-03-05 15:43:41 +0100521 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000522 if (ret)
523 kfree(tm);
524
525 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200526}
527
David Sterba6074d452018-03-05 15:03:52 +0100528static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
529 int dst_slot, int src_slot, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200530{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000531 struct tree_mod_elem *tm = NULL;
532 struct tree_mod_elem **tm_list = NULL;
533 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200534 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000535 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200536
David Sterba6074d452018-03-05 15:03:52 +0100537 if (!tree_mod_need_log(eb->fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200538 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200539
David Sterba176ef8f2017-03-28 14:35:01 +0200540 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000541 if (!tm_list)
542 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200543
David Sterba176ef8f2017-03-28 14:35:01 +0200544 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000545 if (!tm) {
546 ret = -ENOMEM;
547 goto free_tms;
548 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200549
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530550 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200551 tm->slot = src_slot;
552 tm->move.dst_slot = dst_slot;
553 tm->move.nr_items = nr_items;
554 tm->op = MOD_LOG_MOVE_KEYS;
555
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000556 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
557 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
David Sterba176ef8f2017-03-28 14:35:01 +0200558 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000559 if (!tm_list[i]) {
560 ret = -ENOMEM;
561 goto free_tms;
562 }
563 }
564
David Sterba6074d452018-03-05 15:03:52 +0100565 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000566 goto free_tms;
567 locked = 1;
568
569 /*
570 * When we override something during the move, we log these removals.
571 * This can only happen when we move towards the beginning of the
572 * buffer, i.e. dst_slot < src_slot.
573 */
574 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
David Sterba6074d452018-03-05 15:03:52 +0100575 ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000576 if (ret)
577 goto free_tms;
578 }
579
David Sterba6074d452018-03-05 15:03:52 +0100580 ret = __tree_mod_log_insert(eb->fs_info, tm);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000581 if (ret)
582 goto free_tms;
David Sterbab1a09f12018-03-05 15:43:41 +0100583 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000584 kfree(tm_list);
585
586 return 0;
587free_tms:
588 for (i = 0; i < nr_items; i++) {
589 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
David Sterba6074d452018-03-05 15:03:52 +0100590 rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000591 kfree(tm_list[i]);
592 }
593 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100594 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000595 kfree(tm_list);
596 kfree(tm);
597
598 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200599}
600
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000601static inline int
602__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
603 struct tree_mod_elem **tm_list,
604 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200605{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000606 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200607 int ret;
608
Jan Schmidt097b8a72012-06-21 11:08:04 +0200609 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000610 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
611 if (ret) {
612 for (j = nritems - 1; j > i; j--)
613 rb_erase(&tm_list[j]->node,
614 &fs_info->tree_mod_log);
615 return ret;
616 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200617 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000618
619 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200620}
621
David Sterba95b757c2018-03-05 15:22:30 +0100622static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
623 struct extent_buffer *new_root, int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200624{
David Sterba95b757c2018-03-05 15:22:30 +0100625 struct btrfs_fs_info *fs_info = old_root->fs_info;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000626 struct tree_mod_elem *tm = NULL;
627 struct tree_mod_elem **tm_list = NULL;
628 int nritems = 0;
629 int ret = 0;
630 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200631
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000632 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200633 return 0;
634
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000635 if (log_removal && btrfs_header_level(old_root) > 0) {
636 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100637 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
David Sterbabcc8e072017-03-28 14:35:42 +0200638 GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000639 if (!tm_list) {
640 ret = -ENOMEM;
641 goto free_tms;
642 }
643 for (i = 0; i < nritems; i++) {
644 tm_list[i] = alloc_tree_mod_elem(old_root, i,
David Sterbabcc8e072017-03-28 14:35:42 +0200645 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000646 if (!tm_list[i]) {
647 ret = -ENOMEM;
648 goto free_tms;
649 }
650 }
651 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000652
David Sterbabcc8e072017-03-28 14:35:42 +0200653 tm = kzalloc(sizeof(*tm), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000654 if (!tm) {
655 ret = -ENOMEM;
656 goto free_tms;
657 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200658
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530659 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200660 tm->old_root.logical = old_root->start;
661 tm->old_root.level = btrfs_header_level(old_root);
662 tm->generation = btrfs_header_generation(old_root);
663 tm->op = MOD_LOG_ROOT_REPLACE;
664
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000665 if (tree_mod_dont_log(fs_info, NULL))
666 goto free_tms;
667
668 if (tm_list)
669 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
670 if (!ret)
671 ret = __tree_mod_log_insert(fs_info, tm);
672
David Sterbab1a09f12018-03-05 15:43:41 +0100673 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000674 if (ret)
675 goto free_tms;
676 kfree(tm_list);
677
678 return ret;
679
680free_tms:
681 if (tm_list) {
682 for (i = 0; i < nritems; i++)
683 kfree(tm_list[i]);
684 kfree(tm_list);
685 }
686 kfree(tm);
687
688 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200689}
690
691static struct tree_mod_elem *
692__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
693 int smallest)
694{
695 struct rb_root *tm_root;
696 struct rb_node *node;
697 struct tree_mod_elem *cur = NULL;
698 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200699
David Sterbab1a09f12018-03-05 15:43:41 +0100700 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200701 tm_root = &fs_info->tree_mod_log;
702 node = tm_root->rb_node;
703 while (node) {
Geliang Tang6b4df8b2016-12-19 22:53:41 +0800704 cur = rb_entry(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530705 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200706 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530707 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200709 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200710 node = node->rb_left;
711 } else if (!smallest) {
712 /* we want the node with the highest seq */
713 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200714 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200715 found = cur;
716 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200717 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200718 /* we want the node with the smallest seq */
719 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200720 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200721 found = cur;
722 node = node->rb_right;
723 } else {
724 found = cur;
725 break;
726 }
727 }
David Sterbab1a09f12018-03-05 15:43:41 +0100728 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200729
730 return found;
731}
732
733/*
734 * this returns the element from the log with the smallest time sequence
735 * value that's in the log (the oldest log item). any element with a time
736 * sequence lower than min_seq will be ignored.
737 */
738static struct tree_mod_elem *
739tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
740 u64 min_seq)
741{
742 return __tree_mod_log_search(fs_info, start, min_seq, 1);
743}
744
745/*
746 * this returns the element from the log with the largest time sequence
747 * value that's in the log (the most recent log item). any element with
748 * a time sequence lower than min_seq will be ignored.
749 */
750static struct tree_mod_elem *
751tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
752{
753 return __tree_mod_log_search(fs_info, start, min_seq, 0);
754}
755
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000756static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200757tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
758 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000759 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200760{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000761 int ret = 0;
762 struct tree_mod_elem **tm_list = NULL;
763 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200764 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000765 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200766
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000767 if (!tree_mod_need_log(fs_info, NULL))
768 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200769
Josef Bacikc8cc6342013-07-01 16:18:19 -0400770 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000771 return 0;
772
David Sterba31e818f2015-02-20 18:00:26 +0100773 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000774 GFP_NOFS);
775 if (!tm_list)
776 return -ENOMEM;
777
778 tm_list_add = tm_list;
779 tm_list_rem = tm_list + nr_items;
780 for (i = 0; i < nr_items; i++) {
781 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
782 MOD_LOG_KEY_REMOVE, GFP_NOFS);
783 if (!tm_list_rem[i]) {
784 ret = -ENOMEM;
785 goto free_tms;
786 }
787
788 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
789 MOD_LOG_KEY_ADD, GFP_NOFS);
790 if (!tm_list_add[i]) {
791 ret = -ENOMEM;
792 goto free_tms;
793 }
794 }
795
796 if (tree_mod_dont_log(fs_info, NULL))
797 goto free_tms;
798 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200799
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000801 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
802 if (ret)
803 goto free_tms;
804 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
805 if (ret)
806 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200807 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000808
David Sterbab1a09f12018-03-05 15:43:41 +0100809 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000810 kfree(tm_list);
811
812 return 0;
813
814free_tms:
815 for (i = 0; i < nr_items * 2; i++) {
816 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
817 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
818 kfree(tm_list[i]);
819 }
820 if (locked)
David Sterbab1a09f12018-03-05 15:43:41 +0100821 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000822 kfree(tm_list);
823
824 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200825}
826
David Sterbadb7279a2018-03-05 15:14:25 +0100827static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200828{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000829 struct tree_mod_elem **tm_list = NULL;
830 int nritems = 0;
831 int i;
832 int ret = 0;
833
834 if (btrfs_header_level(eb) == 0)
835 return 0;
836
David Sterbadb7279a2018-03-05 15:14:25 +0100837 if (!tree_mod_need_log(eb->fs_info, NULL))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000838 return 0;
839
840 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100841 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000842 if (!tm_list)
843 return -ENOMEM;
844
845 for (i = 0; i < nritems; i++) {
846 tm_list[i] = alloc_tree_mod_elem(eb, i,
847 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
848 if (!tm_list[i]) {
849 ret = -ENOMEM;
850 goto free_tms;
851 }
852 }
853
David Sterbadb7279a2018-03-05 15:14:25 +0100854 if (tree_mod_dont_log(eb->fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000855 goto free_tms;
856
David Sterbadb7279a2018-03-05 15:14:25 +0100857 ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
David Sterbab1a09f12018-03-05 15:43:41 +0100858 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000859 if (ret)
860 goto free_tms;
861 kfree(tm_list);
862
863 return 0;
864
865free_tms:
866 for (i = 0; i < nritems; i++)
867 kfree(tm_list[i]);
868 kfree(tm_list);
869
870 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200871}
872
Chris Masond352ac62008-09-29 15:18:18 -0400873/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400874 * check if the tree block can be shared by multiple trees
875 */
876int btrfs_block_can_be_shared(struct btrfs_root *root,
877 struct extent_buffer *buf)
878{
879 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400880 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 * are never shared. If a block was allocated after the last
882 * snapshot and the block was not allocated by tree relocation,
883 * we know the block is not shared.
884 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800885 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 buf != root->node && buf != root->commit_root &&
887 (btrfs_header_generation(buf) <=
888 btrfs_root_last_snapshot(&root->root_item) ||
889 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
890 return 1;
Nikolay Borisova79865c2018-06-21 09:45:00 +0300891
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892 return 0;
893}
894
895static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400898 struct extent_buffer *cow,
899 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400901 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 u64 refs;
903 u64 owner;
904 u64 flags;
905 u64 new_flags = 0;
906 int ret;
907
908 /*
909 * Backrefs update rules:
910 *
911 * Always use full backrefs for extent pointers in tree block
912 * allocated by tree relocation.
913 *
914 * If a shared tree block is no longer referenced by its owner
915 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
916 * use full backrefs for extent pointers in tree block.
917 *
918 * If a tree block is been relocating
919 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
920 * use full backrefs for extent pointers in tree block.
921 * The reason for this is some operations (such as drop tree)
922 * are only allowed for blocks use full backrefs.
923 */
924
925 if (btrfs_block_can_be_shared(root, buf)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400926 ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500927 btrfs_header_level(buf), 1,
928 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700929 if (ret)
930 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700931 if (refs == 0) {
932 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400933 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700934 return ret;
935 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400936 } else {
937 refs = 1;
938 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
939 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
940 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
941 else
942 flags = 0;
943 }
944
945 owner = btrfs_header_owner(buf);
946 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
947 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
948
949 if (refs > 1) {
950 if ((owner == root->root_key.objectid ||
951 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
952 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700953 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500954 if (ret)
955 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956
957 if (root->root_key.objectid ==
958 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -0700959 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500960 if (ret)
961 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700962 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500963 if (ret)
964 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400965 }
966 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
967 } else {
968
969 if (root->root_key.objectid ==
970 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700971 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400972 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700973 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500974 if (ret)
975 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976 }
977 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400978 int level = btrfs_header_level(buf);
979
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400980 ret = btrfs_set_disk_extent_flags(trans, fs_info,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 buf->start,
982 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400983 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700984 if (ret)
985 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 }
987 } else {
988 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
989 if (root->root_key.objectid ==
990 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700991 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400992 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700993 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500994 if (ret)
995 return ret;
Josef Bacike339a6b2014-07-02 10:54:25 -0700996 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney692826b2017-11-21 13:58:49 -0500997 if (ret)
998 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 }
David Sterba7c302b42017-02-10 18:47:57 +01001000 clean_tree_block(fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001001 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001002 }
1003 return 0;
1004}
1005
1006/*
Chris Masond3977122009-01-05 21:25:51 -05001007 * does the dirty work in cow of a single block. The parent block (if
1008 * supplied) is updated to point to the new cow copy. The new buffer is marked
1009 * dirty and returned locked. If you modify the block it needs to be marked
1010 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001011 *
1012 * search_start -- an allocation hint for the new block
1013 *
Chris Masond3977122009-01-05 21:25:51 -05001014 * empty_size -- a hint that you plan on doing more cow. This is the size in
1015 * bytes the allocator should try to find free next to the block it returns.
1016 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001017 */
Chris Masond3977122009-01-05 21:25:51 -05001018static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001019 struct btrfs_root *root,
1020 struct extent_buffer *buf,
1021 struct extent_buffer *parent, int parent_slot,
1022 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001023 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001024{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001025 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001026 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001027 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001028 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001029 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001030 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001031 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001032
Chris Mason925baed2008-06-25 16:01:30 -04001033 if (*cow_ret == buf)
1034 unlock_orig = 1;
1035
Chris Masonb9447ef82009-03-09 11:45:38 -04001036 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001037
Miao Xie27cdeb72014-04-02 19:51:05 +08001038 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001039 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +08001040 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1041 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001042
Chris Mason7bb86312007-12-11 09:25:06 -05001043 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001045 if (level == 0)
1046 btrfs_item_key(buf, &disk_key, 0);
1047 else
1048 btrfs_node_key(buf, &disk_key, 0);
1049
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001050 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1051 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001052
David Sterba4d75f8a2014-06-15 01:54:12 +02001053 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1054 root->root_key.objectid, &disk_key, level,
1055 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001056 if (IS_ERR(cow))
1057 return PTR_ERR(cow);
1058
Chris Masonb4ce94d2009-02-04 09:25:08 -05001059 /* cow is set to blocking by btrfs_init_new_buffer */
1060
David Sterba58e80122016-11-08 18:30:31 +01001061 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001062 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001063 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1065 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1066 BTRFS_HEADER_FLAG_RELOC);
1067 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1068 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1069 else
1070 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001071
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001072 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05001073
Mark Fashehbe1a5562011-08-08 13:20:18 -07001074 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001075 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001076 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001077 return ret;
1078 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001079
Miao Xie27cdeb72014-04-02 19:51:05 +08001080 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001081 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001082 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001083 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001084 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001085 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001086 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001087
Chris Mason6702ed42007-08-07 16:15:09 -04001088 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001089 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001090 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1091 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1092 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001093
Chris Mason5f39d392007-10-15 16:14:19 -04001094 extent_buffer_get(cow);
David Sterbad9d19a02018-03-05 16:35:29 +01001095 ret = tree_mod_log_insert_root(root->node, cow, 1);
1096 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001097 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001098
Yan, Zhengf0486c62010-05-16 10:46:25 -04001099 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001100 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001101 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001102 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001103 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001104 WARN_ON(trans->transid != btrfs_header_generation(parent));
David Sterbae09c2ef2018-03-05 15:09:03 +01001105 tree_mod_log_insert_key(parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001106 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001107 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001108 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001109 btrfs_set_node_ptr_generation(parent, parent_slot,
1110 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001111 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001112 if (last_ref) {
David Sterbadb7279a2018-03-05 15:14:25 +01001113 ret = tree_mod_log_free_eb(buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001114 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001115 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001116 return ret;
1117 }
1118 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001119 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001120 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001121 }
Chris Mason925baed2008-06-25 16:01:30 -04001122 if (unlock_orig)
1123 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001124 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001125 btrfs_mark_buffer_dirty(cow);
1126 *cow_ret = cow;
1127 return 0;
1128}
1129
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001130/*
1131 * returns the logical address of the oldest predecessor of the given root.
1132 * entries older than time_seq are ignored.
1133 */
David Sterbabcd24da2018-03-05 15:33:18 +01001134static struct tree_mod_elem *__tree_mod_log_oldest_root(
1135 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001136{
1137 struct tree_mod_elem *tm;
1138 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001139 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001140 int looped = 0;
1141
1142 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001143 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001144
1145 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301146 * the very last operation that's logged for a root is the
1147 * replacement operation (if it is replaced at all). this has
1148 * the logical address of the *new* root, making it the very
1149 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001150 */
1151 while (1) {
David Sterbabcd24da2018-03-05 15:33:18 +01001152 tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001153 time_seq);
1154 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001155 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001156 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001157 * if there are no tree operation for the oldest root, we simply
1158 * return it. this should only happen if that (old) root is at
1159 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001160 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001161 if (!tm)
1162 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001163
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001164 /*
1165 * if there's an operation that's not a root replacement, we
1166 * found the oldest version of our root. normally, we'll find a
1167 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1168 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001169 if (tm->op != MOD_LOG_ROOT_REPLACE)
1170 break;
1171
1172 found = tm;
1173 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001174 looped = 1;
1175 }
1176
Jan Schmidta95236d2012-06-05 16:41:24 +02001177 /* if there's no old root to return, return what we found instead */
1178 if (!found)
1179 found = tm;
1180
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001181 return found;
1182}
1183
1184/*
1185 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001186 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001187 * time_seq).
1188 */
1189static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001190__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1191 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001192{
1193 u32 n;
1194 struct rb_node *next;
1195 struct tree_mod_elem *tm = first_tm;
1196 unsigned long o_dst;
1197 unsigned long o_src;
1198 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1199
1200 n = btrfs_header_nritems(eb);
David Sterbab1a09f12018-03-05 15:43:41 +01001201 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001202 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001203 /*
1204 * all the operations are recorded with the operator used for
1205 * the modification. as we're going backwards, we do the
1206 * opposite of each operation here.
1207 */
1208 switch (tm->op) {
1209 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1210 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001211 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001212 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001213 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001214 btrfs_set_node_key(eb, &tm->key, tm->slot);
1215 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1216 btrfs_set_node_ptr_generation(eb, tm->slot,
1217 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001218 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001219 break;
1220 case MOD_LOG_KEY_REPLACE:
1221 BUG_ON(tm->slot >= n);
1222 btrfs_set_node_key(eb, &tm->key, tm->slot);
1223 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1224 btrfs_set_node_ptr_generation(eb, tm->slot,
1225 tm->generation);
1226 break;
1227 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001228 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001229 n--;
1230 break;
1231 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001232 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1233 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1234 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001235 tm->move.nr_items * p_size);
1236 break;
1237 case MOD_LOG_ROOT_REPLACE:
1238 /*
1239 * this operation is special. for roots, this must be
1240 * handled explicitly before rewinding.
1241 * for non-roots, this operation may exist if the node
1242 * was a root: root A -> child B; then A gets empty and
1243 * B is promoted to the new root. in the mod log, we'll
1244 * have a root-replace operation for B, a tree block
1245 * that is no root. we simply ignore that operation.
1246 */
1247 break;
1248 }
1249 next = rb_next(&tm->node);
1250 if (!next)
1251 break;
Geliang Tang6b4df8b2016-12-19 22:53:41 +08001252 tm = rb_entry(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301253 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001254 break;
1255 }
David Sterbab1a09f12018-03-05 15:43:41 +01001256 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001257 btrfs_set_header_nritems(eb, n);
1258}
1259
Jan Schmidt47fb0912013-04-13 13:19:55 +00001260/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001261 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001262 * is returned. If rewind operations happen, a fresh buffer is returned. The
1263 * returned buffer is always read-locked. If the returned buffer is not the
1264 * input buffer, the lock on the input buffer is released and the input buffer
1265 * is freed (its refcount is decremented).
1266 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001267static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001268tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1269 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001270{
1271 struct extent_buffer *eb_rewin;
1272 struct tree_mod_elem *tm;
1273
1274 if (!time_seq)
1275 return eb;
1276
1277 if (btrfs_header_level(eb) == 0)
1278 return eb;
1279
1280 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1281 if (!tm)
1282 return eb;
1283
Josef Bacik9ec72672013-08-07 16:57:23 -04001284 btrfs_set_path_blocking(path);
1285 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1286
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001287 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1288 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001289 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001290 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001291 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001292 free_extent_buffer(eb);
1293 return NULL;
1294 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001295 btrfs_set_header_bytenr(eb_rewin, eb->start);
1296 btrfs_set_header_backref_rev(eb_rewin,
1297 btrfs_header_backref_rev(eb));
1298 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001299 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001300 } else {
1301 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001302 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001303 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001304 free_extent_buffer(eb);
1305 return NULL;
1306 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307 }
1308
Josef Bacik9ec72672013-08-07 16:57:23 -04001309 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1310 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001311 free_extent_buffer(eb);
1312
Jan Schmidt47fb0912013-04-13 13:19:55 +00001313 extent_buffer_get(eb_rewin);
1314 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001315 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001316 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001317 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001318
1319 return eb_rewin;
1320}
1321
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001322/*
1323 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1324 * value. If there are no changes, the current root->root_node is returned. If
1325 * anything changed in between, there's a fresh buffer allocated on which the
1326 * rewind operations are done. In any case, the returned buffer is read locked.
1327 * Returns NULL on error (with no locks held).
1328 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001329static inline struct extent_buffer *
1330get_old_root(struct btrfs_root *root, u64 time_seq)
1331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001332 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001333 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001334 struct extent_buffer *eb = NULL;
1335 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001336 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001337 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001338 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001339 u64 logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001340 int level;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001341
Jan Schmidt30b04632013-04-13 13:19:54 +00001342 eb_root = btrfs_read_lock_root_node(root);
David Sterbabcd24da2018-03-05 15:33:18 +01001343 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001344 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001345 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001346
Jan Schmidta95236d2012-06-05 16:41:24 +02001347 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1348 old_root = &tm->old_root;
1349 old_generation = tm->generation;
1350 logical = old_root->logical;
Qu Wenruo581c1762018-03-29 09:08:11 +08001351 level = old_root->level;
Jan Schmidta95236d2012-06-05 16:41:24 +02001352 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001353 logical = eb_root->start;
Qu Wenruo581c1762018-03-29 09:08:11 +08001354 level = btrfs_header_level(eb_root);
Jan Schmidta95236d2012-06-05 16:41:24 +02001355 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001356
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001357 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001358 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001359 btrfs_tree_read_unlock(eb_root);
1360 free_extent_buffer(eb_root);
Qu Wenruo581c1762018-03-29 09:08:11 +08001361 old = read_tree_block(fs_info, logical, 0, level, NULL);
Liu Bo64c043d2015-05-25 17:30:15 +08001362 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1363 if (!IS_ERR(old))
1364 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001365 btrfs_warn(fs_info,
1366 "failed to read tree block %llu from get_old_root",
1367 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001368 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001369 eb = btrfs_clone_extent_buffer(old);
1370 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001371 }
1372 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001373 btrfs_tree_read_unlock(eb_root);
1374 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001375 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001376 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001377 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001378 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001379 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001380 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001381 }
1382
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001383 if (!eb)
1384 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001385 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001386 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001387 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001388 btrfs_set_header_bytenr(eb, eb->start);
1389 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001390 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001391 btrfs_set_header_level(eb, old_root->level);
1392 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001393 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001394 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001395 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001396 else
1397 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001398 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001399
1400 return eb;
1401}
1402
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001403int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1404{
1405 struct tree_mod_elem *tm;
1406 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001407 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001408
David Sterbabcd24da2018-03-05 15:33:18 +01001409 tm = __tree_mod_log_oldest_root(eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001410 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1411 level = tm->old_root.level;
1412 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001413 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001414 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001415 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001416
1417 return level;
1418}
1419
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001420static inline int should_cow_block(struct btrfs_trans_handle *trans,
1421 struct btrfs_root *root,
1422 struct extent_buffer *buf)
1423{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001424 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001425 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001426
David Sterbad1980132018-03-16 02:39:40 +01001427 /* Ensure we can see the FORCE_COW bit */
1428 smp_mb__before_atomic();
Liu Bof1ebcc72011-11-14 20:48:06 -05001429
1430 /*
1431 * We do not need to cow a block if
1432 * 1) this block is not created or changed in this transaction;
1433 * 2) this block does not belong to TREE_RELOC tree;
1434 * 3) the root is not forced COW.
1435 *
1436 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001437 * when we create snapshot during committing the transaction,
Liu Bof1ebcc72011-11-14 20:48:06 -05001438 * after we've finished coping src root, we must COW the shared
1439 * block to ensure the metadata consistency.
1440 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001441 if (btrfs_header_generation(buf) == trans->transid &&
1442 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1443 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001444 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001445 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001446 return 0;
1447 return 1;
1448}
1449
Chris Masond352ac62008-09-29 15:18:18 -04001450/*
1451 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001452 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001453 * once per transaction, as long as it hasn't been written yet
1454 */
Chris Masond3977122009-01-05 21:25:51 -05001455noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001456 struct btrfs_root *root, struct extent_buffer *buf,
1457 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001458 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001460 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001461 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001462 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001463
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001464 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001465 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001466 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001467 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001468
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001469 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001470 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001471 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001472
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001473 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001474 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001475 *cow_ret = buf;
1476 return 0;
1477 }
Chris Masonc4876852009-02-04 09:24:25 -05001478
Byongho Leeee221842015-12-15 01:42:10 +09001479 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001480
1481 if (parent)
1482 btrfs_set_lock_blocking(parent);
1483 btrfs_set_lock_blocking(buf);
1484
Chris Masonf510cfe2007-10-15 16:14:48 -04001485 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001486 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001487
1488 trace_btrfs_cow_block(root, buf, *cow_ret);
1489
Chris Masonf510cfe2007-10-15 16:14:48 -04001490 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001491}
1492
Chris Masond352ac62008-09-29 15:18:18 -04001493/*
1494 * helper function for defrag to decide if two blocks pointed to by a
1495 * node are actually close by
1496 */
Chris Mason6b800532007-10-15 16:17:34 -04001497static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001498{
Chris Mason6b800532007-10-15 16:17:34 -04001499 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001500 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001501 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001502 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001503 return 0;
1504}
1505
Chris Mason081e9572007-11-06 10:26:24 -05001506/*
1507 * compare two keys in a memcmp fashion
1508 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001509static int comp_keys(const struct btrfs_disk_key *disk,
1510 const struct btrfs_key *k2)
Chris Mason081e9572007-11-06 10:26:24 -05001511{
1512 struct btrfs_key k1;
1513
1514 btrfs_disk_key_to_cpu(&k1, disk);
1515
Diego Calleja20736ab2009-07-24 11:06:52 -04001516 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001517}
1518
Josef Bacikf3465ca2008-11-12 14:19:50 -05001519/*
1520 * same as comp_keys only with two btrfs_key's
1521 */
Omar Sandoval310712b2017-01-17 23:24:37 -08001522int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001523{
1524 if (k1->objectid > k2->objectid)
1525 return 1;
1526 if (k1->objectid < k2->objectid)
1527 return -1;
1528 if (k1->type > k2->type)
1529 return 1;
1530 if (k1->type < k2->type)
1531 return -1;
1532 if (k1->offset > k2->offset)
1533 return 1;
1534 if (k1->offset < k2->offset)
1535 return -1;
1536 return 0;
1537}
Chris Mason081e9572007-11-06 10:26:24 -05001538
Chris Masond352ac62008-09-29 15:18:18 -04001539/*
1540 * this is used by the defrag code to go through all the
1541 * leaves pointed to by a node and reallocate them so that
1542 * disk order is close to key order
1543 */
Chris Mason6702ed42007-08-07 16:15:09 -04001544int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001545 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001546 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001547 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001548{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001549 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001550 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001551 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001552 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001553 u64 search_start = *last_ret;
1554 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001555 u64 other;
1556 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001557 int end_slot;
1558 int i;
1559 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001560 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001561 int uptodate;
1562 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001563 int progress_passed = 0;
1564 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001565
Chris Mason5708b952007-10-25 15:43:18 -04001566 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001567
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001568 WARN_ON(trans->transaction != fs_info->running_transaction);
1569 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001570
Chris Mason6b800532007-10-15 16:17:34 -04001571 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001572 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001573 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001574
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001575 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001576 return 0;
1577
Chris Masonb4ce94d2009-02-04 09:25:08 -05001578 btrfs_set_lock_blocking(parent);
1579
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001580 for (i = start_slot; i <= end_slot; i++) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001581 struct btrfs_key first_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001582 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001583
Chris Mason081e9572007-11-06 10:26:24 -05001584 btrfs_node_key(parent, &disk_key, i);
1585 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1586 continue;
1587
1588 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001589 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001590 gen = btrfs_node_ptr_generation(parent, i);
Qu Wenruo581c1762018-03-29 09:08:11 +08001591 btrfs_node_key_to_cpu(parent, &first_key, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001592 if (last_block == 0)
1593 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001594
Chris Mason6702ed42007-08-07 16:15:09 -04001595 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001596 other = btrfs_node_blockptr(parent, i - 1);
1597 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001598 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001599 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001600 other = btrfs_node_blockptr(parent, i + 1);
1601 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001602 }
Chris Masone9d0b132007-08-10 14:06:19 -04001603 if (close) {
1604 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001605 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001606 }
Chris Mason6702ed42007-08-07 16:15:09 -04001607
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001608 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001609 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001610 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001611 else
1612 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001613 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001614 if (!cur) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001615 cur = read_tree_block(fs_info, blocknr, gen,
1616 parent_level - 1,
1617 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08001618 if (IS_ERR(cur)) {
1619 return PTR_ERR(cur);
1620 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001621 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001622 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001623 }
Chris Mason6b800532007-10-15 16:17:34 -04001624 } else if (!uptodate) {
Qu Wenruo581c1762018-03-29 09:08:11 +08001625 err = btrfs_read_buffer(cur, gen,
1626 parent_level - 1,&first_key);
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001627 if (err) {
1628 free_extent_buffer(cur);
1629 return err;
1630 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001631 }
Chris Mason6702ed42007-08-07 16:15:09 -04001632 }
Chris Masone9d0b132007-08-10 14:06:19 -04001633 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001634 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001635
Chris Masone7a84562008-06-25 16:01:31 -04001636 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001637 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001638 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001639 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001640 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001641 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001642 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001643 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001644 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001645 break;
Yan252c38f2007-08-29 09:11:44 -04001646 }
Chris Masone7a84562008-06-25 16:01:31 -04001647 search_start = cur->start;
1648 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001649 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001650 btrfs_tree_unlock(cur);
1651 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001652 }
1653 return err;
1654}
1655
Chris Mason74123bd2007-02-02 11:05:29 -05001656/*
Chris Mason5f39d392007-10-15 16:14:19 -04001657 * search for key in the extent_buffer. The items start at offset p,
1658 * and they are item_size apart. There are 'max' items in p.
1659 *
Chris Mason74123bd2007-02-02 11:05:29 -05001660 * the slot in the array is returned via slot, and it points to
1661 * the place where you would insert key if it is not found in
1662 * the array.
1663 *
1664 * slot may point to max if the key is bigger than all of the keys
1665 */
Chris Masone02119d2008-09-05 16:13:11 -04001666static noinline int generic_bin_search(struct extent_buffer *eb,
Omar Sandoval310712b2017-01-17 23:24:37 -08001667 unsigned long p, int item_size,
1668 const struct btrfs_key *key,
Chris Masone02119d2008-09-05 16:13:11 -04001669 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001670{
1671 int low = 0;
1672 int high = max;
1673 int mid;
1674 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001675 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001676 struct btrfs_disk_key unaligned;
1677 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001678 char *kaddr = NULL;
1679 unsigned long map_start = 0;
1680 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001681 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001682
Liu Bo5e24e9a2016-06-23 16:32:45 -07001683 if (low > high) {
1684 btrfs_err(eb->fs_info,
1685 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1686 __func__, low, high, eb->start,
1687 btrfs_header_owner(eb), btrfs_header_level(eb));
1688 return -EINVAL;
1689 }
1690
Chris Masond3977122009-01-05 21:25:51 -05001691 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001692 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001693 offset = p + mid * item_size;
1694
Chris Masona6591712011-07-19 12:04:14 -04001695 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001696 (offset + sizeof(struct btrfs_disk_key)) >
1697 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001698
1699 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001700 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001701 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001702
Chris Mason479965d2007-10-15 16:14:27 -04001703 if (!err) {
1704 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1705 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001706 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001707 read_extent_buffer(eb, &unaligned,
1708 offset, sizeof(unaligned));
1709 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001710 } else {
1711 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001712 }
1713
Chris Mason5f39d392007-10-15 16:14:19 -04001714 } else {
1715 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1716 map_start);
1717 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001718 ret = comp_keys(tmp, key);
1719
1720 if (ret < 0)
1721 low = mid + 1;
1722 else if (ret > 0)
1723 high = mid;
1724 else {
1725 *slot = mid;
1726 return 0;
1727 }
1728 }
1729 *slot = low;
1730 return 1;
1731}
1732
Chris Mason97571fd2007-02-24 13:39:08 -05001733/*
1734 * simple bin_search frontend that does the right thing for
1735 * leaves vs nodes
1736 */
Nikolay Borisova74b35e2017-12-08 16:27:43 +02001737int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
1738 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001739{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001740 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001741 return generic_bin_search(eb,
1742 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001743 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001744 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001745 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001746 else
Chris Mason5f39d392007-10-15 16:14:19 -04001747 return generic_bin_search(eb,
1748 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001749 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001750 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001751 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001752}
1753
Yan, Zhengf0486c62010-05-16 10:46:25 -04001754static void root_add_used(struct btrfs_root *root, u32 size)
1755{
1756 spin_lock(&root->accounting_lock);
1757 btrfs_set_root_used(&root->root_item,
1758 btrfs_root_used(&root->root_item) + size);
1759 spin_unlock(&root->accounting_lock);
1760}
1761
1762static void root_sub_used(struct btrfs_root *root, u32 size)
1763{
1764 spin_lock(&root->accounting_lock);
1765 btrfs_set_root_used(&root->root_item,
1766 btrfs_root_used(&root->root_item) - size);
1767 spin_unlock(&root->accounting_lock);
1768}
1769
Chris Masond352ac62008-09-29 15:18:18 -04001770/* given a node and slot number, this reads the blocks it points to. The
1771 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001772 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001773static noinline struct extent_buffer *
1774read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
1775 int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001776{
Chris Masonca7a79a2008-05-12 12:59:19 -04001777 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001778 struct extent_buffer *eb;
Qu Wenruo581c1762018-03-29 09:08:11 +08001779 struct btrfs_key first_key;
Josef Bacik416bc652013-04-23 14:17:42 -04001780
Liu Bofb770ae2016-07-05 12:10:14 -07001781 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1782 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001783
1784 BUG_ON(level == 0);
1785
Qu Wenruo581c1762018-03-29 09:08:11 +08001786 btrfs_node_key_to_cpu(parent, &first_key, slot);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001787 eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
Qu Wenruo581c1762018-03-29 09:08:11 +08001788 btrfs_node_ptr_generation(parent, slot),
1789 level - 1, &first_key);
Liu Bofb770ae2016-07-05 12:10:14 -07001790 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1791 free_extent_buffer(eb);
1792 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001793 }
1794
1795 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001796}
1797
Chris Masond352ac62008-09-29 15:18:18 -04001798/*
1799 * node level balancing, used to make sure nodes are in proper order for
1800 * item deletion. We balance from the top down, so we have to make sure
1801 * that a deletion won't leave an node completely empty later on.
1802 */
Chris Masone02119d2008-09-05 16:13:11 -04001803static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001804 struct btrfs_root *root,
1805 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001806{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001807 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001808 struct extent_buffer *right = NULL;
1809 struct extent_buffer *mid;
1810 struct extent_buffer *left = NULL;
1811 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001812 int ret = 0;
1813 int wret;
1814 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001815 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001816 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001817
Liu Bo98e6b1e2018-09-12 06:06:23 +08001818 ASSERT(level > 0);
Chris Masonbb803952007-03-01 12:04:21 -05001819
Chris Mason5f39d392007-10-15 16:14:19 -04001820 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001821
Chris Masonbd681512011-07-16 15:23:14 -04001822 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1823 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001824 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1825
Chris Mason1d4f8a02007-03-13 09:28:32 -04001826 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001827
Li Zefana05a9bb2011-09-06 16:55:34 +08001828 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001829 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001830 pslot = path->slots[level + 1];
1831 }
Chris Masonbb803952007-03-01 12:04:21 -05001832
Chris Mason40689472007-03-17 14:29:23 -04001833 /*
1834 * deal with the case where there is only one pointer in the root
1835 * by promoting the node below to a root
1836 */
Chris Mason5f39d392007-10-15 16:14:19 -04001837 if (!parent) {
1838 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001839
Chris Mason5f39d392007-10-15 16:14:19 -04001840 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001841 return 0;
1842
1843 /* promote the child to a root */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001844 child = read_node_slot(fs_info, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001845 if (IS_ERR(child)) {
1846 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001847 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001848 goto enospc;
1849 }
1850
Chris Mason925baed2008-06-25 16:01:30 -04001851 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001852 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001853 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001854 if (ret) {
1855 btrfs_tree_unlock(child);
1856 free_extent_buffer(child);
1857 goto enospc;
1858 }
Yan2f375ab2008-02-01 14:58:07 -05001859
David Sterbad9d19a02018-03-05 16:35:29 +01001860 ret = tree_mod_log_insert_root(root->node, child, 1);
1861 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001862 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001863
Chris Mason0b86a832008-03-24 15:01:56 -04001864 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001865 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001866
Chris Mason925baed2008-06-25 16:01:30 -04001867 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001868 path->nodes[level] = NULL;
David Sterba7c302b42017-02-10 18:47:57 +01001869 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001870 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001871 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001872 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001873
1874 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001875 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001876 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001877 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001878 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001879 }
Chris Mason5f39d392007-10-15 16:14:19 -04001880 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001881 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001882 return 0;
1883
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001884 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001885 if (IS_ERR(left))
1886 left = NULL;
1887
Chris Mason5f39d392007-10-15 16:14:19 -04001888 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001889 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001890 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001891 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001892 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001893 if (wret) {
1894 ret = wret;
1895 goto enospc;
1896 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001897 }
Liu Bofb770ae2016-07-05 12:10:14 -07001898
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001899 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001900 if (IS_ERR(right))
1901 right = NULL;
1902
Chris Mason5f39d392007-10-15 16:14:19 -04001903 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001904 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001905 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001906 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001907 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001908 if (wret) {
1909 ret = wret;
1910 goto enospc;
1911 }
1912 }
1913
1914 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001915 if (left) {
1916 orig_slot += btrfs_header_nritems(left);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001917 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001918 if (wret < 0)
1919 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001920 }
Chris Mason79f95c82007-03-01 15:16:26 -05001921
1922 /*
1923 * then try to empty the right most buffer into the middle
1924 */
Chris Mason5f39d392007-10-15 16:14:19 -04001925 if (right) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001926 wret = push_node_left(trans, fs_info, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001927 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001928 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001929 if (btrfs_header_nritems(right) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001930 clean_tree_block(fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04001931 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001932 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001933 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001934 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001935 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001936 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001937 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001938 struct btrfs_disk_key right_key;
1939 btrfs_node_key(right, &right_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001940 ret = tree_mod_log_insert_key(parent, pslot + 1,
1941 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1942 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001943 btrfs_set_node_key(parent, &right_key, pslot + 1);
1944 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001945 }
1946 }
Chris Mason5f39d392007-10-15 16:14:19 -04001947 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001948 /*
1949 * we're not allowed to leave a node with one item in the
1950 * tree during a delete. A deletion from lower in the tree
1951 * could try to delete the only pointer in this node.
1952 * So, pull some keys from the left.
1953 * There has to be a left pointer at this point because
1954 * otherwise we would have pulled some pointers from the
1955 * right
1956 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001957 if (!left) {
1958 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001959 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001960 goto enospc;
1961 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001962 wret = balance_node_right(trans, fs_info, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001963 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001964 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001965 goto enospc;
1966 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001967 if (wret == 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001968 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04001969 if (wret < 0)
1970 ret = wret;
1971 }
Chris Mason79f95c82007-03-01 15:16:26 -05001972 BUG_ON(wret == 1);
1973 }
Chris Mason5f39d392007-10-15 16:14:19 -04001974 if (btrfs_header_nritems(mid) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001975 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001976 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001977 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001978 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001979 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001980 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001981 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001982 } else {
1983 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001984 struct btrfs_disk_key mid_key;
1985 btrfs_node_key(mid, &mid_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001986 ret = tree_mod_log_insert_key(parent, pslot,
1987 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1988 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001989 btrfs_set_node_key(parent, &mid_key, pslot);
1990 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001991 }
Chris Masonbb803952007-03-01 12:04:21 -05001992
Chris Mason79f95c82007-03-01 15:16:26 -05001993 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001994 if (left) {
1995 if (btrfs_header_nritems(left) > orig_slot) {
1996 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001997 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001998 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001999 path->slots[level + 1] -= 1;
2000 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002001 if (mid) {
2002 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002003 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002004 }
Chris Masonbb803952007-03-01 12:04:21 -05002005 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002006 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002007 path->slots[level] = orig_slot;
2008 }
2009 }
Chris Mason79f95c82007-03-01 15:16:26 -05002010 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002011 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002012 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002013 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002014enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002015 if (right) {
2016 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002017 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002018 }
2019 if (left) {
2020 if (path->nodes[level] != left)
2021 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002022 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002023 }
Chris Masonbb803952007-03-01 12:04:21 -05002024 return ret;
2025}
2026
Chris Masond352ac62008-09-29 15:18:18 -04002027/* Node balancing for insertion. Here we only split or push nodes around
2028 * when they are completely full. This is also done top down, so we
2029 * have to be pessimistic.
2030 */
Chris Masond3977122009-01-05 21:25:51 -05002031static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002032 struct btrfs_root *root,
2033 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002034{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002035 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002036 struct extent_buffer *right = NULL;
2037 struct extent_buffer *mid;
2038 struct extent_buffer *left = NULL;
2039 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002040 int ret = 0;
2041 int wret;
2042 int pslot;
2043 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002044
2045 if (level == 0)
2046 return 1;
2047
Chris Mason5f39d392007-10-15 16:14:19 -04002048 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002049 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002050
Li Zefana05a9bb2011-09-06 16:55:34 +08002051 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002052 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002053 pslot = path->slots[level + 1];
2054 }
Chris Masone66f7092007-04-20 13:16:02 -04002055
Chris Mason5f39d392007-10-15 16:14:19 -04002056 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002057 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002058
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002059 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002060 if (IS_ERR(left))
2061 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002062
2063 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002064 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002065 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002066
2067 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002068 btrfs_set_lock_blocking(left);
2069
Chris Mason5f39d392007-10-15 16:14:19 -04002070 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002071 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002072 wret = 1;
2073 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002074 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002075 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002076 if (ret)
2077 wret = 1;
2078 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002079 wret = push_node_left(trans, fs_info,
Chris Mason971a1f62008-04-24 10:54:32 -04002080 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002081 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002082 }
Chris Masone66f7092007-04-20 13:16:02 -04002083 if (wret < 0)
2084 ret = wret;
2085 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002086 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002087 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002088 btrfs_node_key(mid, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002089 ret = tree_mod_log_insert_key(parent, pslot,
2090 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2091 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002092 btrfs_set_node_key(parent, &disk_key, pslot);
2093 btrfs_mark_buffer_dirty(parent);
2094 if (btrfs_header_nritems(left) > orig_slot) {
2095 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002096 path->slots[level + 1] -= 1;
2097 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002098 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002099 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002100 } else {
2101 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002102 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002103 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002104 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002105 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002106 }
Chris Masone66f7092007-04-20 13:16:02 -04002107 return 0;
2108 }
Chris Mason925baed2008-06-25 16:01:30 -04002109 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002110 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002111 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002112 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002113 if (IS_ERR(right))
2114 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002115
2116 /*
2117 * then try to empty the right most buffer into the middle
2118 */
Chris Mason5f39d392007-10-15 16:14:19 -04002119 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002120 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002121
Chris Mason925baed2008-06-25 16:01:30 -04002122 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002123 btrfs_set_lock_blocking(right);
2124
Chris Mason5f39d392007-10-15 16:14:19 -04002125 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002126 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002127 wret = 1;
2128 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002129 ret = btrfs_cow_block(trans, root, right,
2130 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002131 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002132 if (ret)
2133 wret = 1;
2134 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002135 wret = balance_node_right(trans, fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04002136 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002137 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002138 }
Chris Masone66f7092007-04-20 13:16:02 -04002139 if (wret < 0)
2140 ret = wret;
2141 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002142 struct btrfs_disk_key disk_key;
2143
2144 btrfs_node_key(right, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002145 ret = tree_mod_log_insert_key(parent, pslot + 1,
2146 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2147 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002148 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2149 btrfs_mark_buffer_dirty(parent);
2150
2151 if (btrfs_header_nritems(mid) <= orig_slot) {
2152 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002153 path->slots[level + 1] += 1;
2154 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002155 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002156 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002157 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002158 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002159 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002160 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002161 }
Chris Masone66f7092007-04-20 13:16:02 -04002162 return 0;
2163 }
Chris Mason925baed2008-06-25 16:01:30 -04002164 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002165 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002166 }
Chris Masone66f7092007-04-20 13:16:02 -04002167 return 1;
2168}
2169
Chris Mason74123bd2007-02-02 11:05:29 -05002170/*
Chris Masond352ac62008-09-29 15:18:18 -04002171 * readahead one full node of leaves, finding things that are close
2172 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002173 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002174static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002175 struct btrfs_path *path,
2176 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002177{
Chris Mason5f39d392007-10-15 16:14:19 -04002178 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002179 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002180 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002181 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002182 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002183 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002184 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002185 u32 nr;
2186 u32 blocksize;
2187 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002188
Chris Masona6b6e752007-10-15 16:22:39 -04002189 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002190 return;
2191
Chris Mason6702ed42007-08-07 16:15:09 -04002192 if (!path->nodes[level])
2193 return;
2194
Chris Mason5f39d392007-10-15 16:14:19 -04002195 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002196
Chris Mason3c69fae2007-08-07 15:52:22 -04002197 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002198 blocksize = fs_info->nodesize;
2199 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002200 if (eb) {
2201 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002202 return;
2203 }
2204
Chris Masona7175312009-01-22 09:23:10 -05002205 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002206
Chris Mason5f39d392007-10-15 16:14:19 -04002207 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002208 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002209
Chris Masond3977122009-01-05 21:25:51 -05002210 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002211 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002212 if (nr == 0)
2213 break;
2214 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002215 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002216 nr++;
2217 if (nr >= nritems)
2218 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002219 }
David Sterbae4058b52015-11-27 16:31:35 +01002220 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002221 btrfs_node_key(node, &disk_key, nr);
2222 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2223 break;
2224 }
Chris Mason6b800532007-10-15 16:17:34 -04002225 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002226 if ((search <= target && target - search <= 65536) ||
2227 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002228 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002229 nread += blocksize;
2230 }
2231 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002232 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002233 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002234 }
2235}
Chris Mason925baed2008-06-25 16:01:30 -04002236
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002237static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002238 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002239{
2240 int slot;
2241 int nritems;
2242 struct extent_buffer *parent;
2243 struct extent_buffer *eb;
2244 u64 gen;
2245 u64 block1 = 0;
2246 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002247
Chris Mason8c594ea2009-04-20 15:50:10 -04002248 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002249 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002250 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002251
2252 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002253 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002254
2255 if (slot > 0) {
2256 block1 = btrfs_node_blockptr(parent, slot - 1);
2257 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002258 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002259 /*
2260 * if we get -eagain from btrfs_buffer_uptodate, we
2261 * don't want to return eagain here. That will loop
2262 * forever
2263 */
2264 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002265 block1 = 0;
2266 free_extent_buffer(eb);
2267 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002268 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002269 block2 = btrfs_node_blockptr(parent, slot + 1);
2270 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002271 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002272 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002273 block2 = 0;
2274 free_extent_buffer(eb);
2275 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002276
Josef Bacik0b088512013-06-17 14:23:02 -04002277 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002278 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002279 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002280 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002281}
2282
2283
2284/*
Chris Masond3977122009-01-05 21:25:51 -05002285 * when we walk down the tree, it is usually safe to unlock the higher layers
2286 * in the tree. The exceptions are when our path goes through slot 0, because
2287 * operations on the tree might require changing key pointers higher up in the
2288 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002289 *
Chris Masond3977122009-01-05 21:25:51 -05002290 * callers might also have set path->keep_locks, which tells this code to keep
2291 * the lock if the path points to the last slot in the block. This is part of
2292 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002293 *
Chris Masond3977122009-01-05 21:25:51 -05002294 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2295 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002296 */
Chris Masone02119d2008-09-05 16:13:11 -04002297static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002298 int lowest_unlock, int min_write_lock_level,
2299 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002300{
2301 int i;
2302 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002303 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002304 struct extent_buffer *t;
2305
2306 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2307 if (!path->nodes[i])
2308 break;
2309 if (!path->locks[i])
2310 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002311 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002312 skip_level = i + 1;
2313 continue;
2314 }
Chris Mason051e1b92008-06-25 16:01:30 -04002315 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002316 u32 nritems;
2317 t = path->nodes[i];
2318 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002319 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002320 skip_level = i + 1;
2321 continue;
2322 }
2323 }
Chris Mason051e1b92008-06-25 16:01:30 -04002324 if (skip_level < i && i >= lowest_unlock)
2325 no_skips = 1;
2326
Chris Mason925baed2008-06-25 16:01:30 -04002327 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08002328 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04002329 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002330 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002331 if (write_lock_level &&
2332 i > min_write_lock_level &&
2333 i <= *write_lock_level) {
2334 *write_lock_level = i - 1;
2335 }
Chris Mason925baed2008-06-25 16:01:30 -04002336 }
2337 }
2338}
2339
Chris Mason3c69fae2007-08-07 15:52:22 -04002340/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002341 * This releases any locks held in the path starting at level and
2342 * going all the way up to the root.
2343 *
2344 * btrfs_search_slot will keep the lock held on higher nodes in a few
2345 * corner cases, such as COW of the block at slot zero in the node. This
2346 * ignores those rules, and it should only be called when there are no
2347 * more updates to be done higher up in the tree.
2348 */
2349noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2350{
2351 int i;
2352
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002353 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002354 return;
2355
2356 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2357 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002358 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002359 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002360 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002361 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002362 path->locks[i] = 0;
2363 }
2364}
2365
2366/*
Chris Masonc8c42862009-04-03 10:14:18 -04002367 * helper function for btrfs_search_slot. The goal is to find a block
2368 * in cache without setting the path to blocking. If we find the block
2369 * we return zero and the path is unchanged.
2370 *
2371 * If we can't find the block, we set the path blocking and do some
2372 * reada. -EAGAIN is returned and the search must be repeated.
2373 */
2374static int
Liu Bod07b8522017-01-30 12:23:42 -08002375read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2376 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002377 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002378{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002379 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002380 u64 blocknr;
2381 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002382 struct extent_buffer *b = *eb_ret;
2383 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08002384 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04002385 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08002386 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04002387
2388 blocknr = btrfs_node_blockptr(b, slot);
2389 gen = btrfs_node_ptr_generation(b, slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08002390 parent_level = btrfs_header_level(b);
2391 btrfs_node_key_to_cpu(b, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002392
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002393 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002394 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002395 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002396 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2397 *eb_ret = tmp;
2398 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002399 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002400
2401 /* the pages were up to date, but we failed
2402 * the generation number check. Do a full
2403 * read for the generation number that is correct.
2404 * We must do this without dropping locks so
2405 * we can trust our generation number
2406 */
2407 btrfs_set_path_blocking(p);
2408
2409 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08002410 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04002411 if (!ret) {
2412 *eb_ret = tmp;
2413 return 0;
2414 }
2415 free_extent_buffer(tmp);
2416 btrfs_release_path(p);
2417 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002418 }
2419
2420 /*
2421 * reduce lock contention at high levels
2422 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002423 * we read. Don't release the lock on the current
2424 * level because we need to walk this node to figure
2425 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002426 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002427 btrfs_unlock_up_safe(p, level + 1);
2428 btrfs_set_path_blocking(p);
2429
David Sterbae4058b52015-11-27 16:31:35 +01002430 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002431 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002432
Chris Mason76a05b32009-05-14 13:24:30 -04002433 ret = -EAGAIN;
Liu Bo02a33072018-05-16 01:37:36 +08002434 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
Qu Wenruo581c1762018-03-29 09:08:11 +08002435 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002436 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002437 /*
2438 * If the read above didn't mark this buffer up to date,
2439 * it will never end up being up to date. Set ret to EIO now
2440 * and give up so that our caller doesn't loop forever
2441 * on our EAGAINs.
2442 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08002443 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04002444 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002445 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002446 } else {
2447 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002448 }
Liu Bo02a33072018-05-16 01:37:36 +08002449
2450 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002451 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002452}
2453
2454/*
2455 * helper function for btrfs_search_slot. This does all of the checks
2456 * for node-level blocks and does any balancing required based on
2457 * the ins_len.
2458 *
2459 * If no extra work was required, zero is returned. If we had to
2460 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2461 * start over
2462 */
2463static int
2464setup_nodes_for_search(struct btrfs_trans_handle *trans,
2465 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002466 struct extent_buffer *b, int level, int ins_len,
2467 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002469 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002470 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002471
Chris Masonc8c42862009-04-03 10:14:18 -04002472 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002473 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002474 int sret;
2475
Chris Masonbd681512011-07-16 15:23:14 -04002476 if (*write_lock_level < level + 1) {
2477 *write_lock_level = level + 1;
2478 btrfs_release_path(p);
2479 goto again;
2480 }
2481
Chris Masonc8c42862009-04-03 10:14:18 -04002482 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002483 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002484 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002485 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002486
2487 BUG_ON(sret > 0);
2488 if (sret) {
2489 ret = sret;
2490 goto done;
2491 }
2492 b = p->nodes[level];
2493 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002494 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002495 int sret;
2496
Chris Masonbd681512011-07-16 15:23:14 -04002497 if (*write_lock_level < level + 1) {
2498 *write_lock_level = level + 1;
2499 btrfs_release_path(p);
2500 goto again;
2501 }
2502
Chris Masonc8c42862009-04-03 10:14:18 -04002503 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002504 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002505 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002506 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002507
2508 if (sret) {
2509 ret = sret;
2510 goto done;
2511 }
2512 b = p->nodes[level];
2513 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002514 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002515 goto again;
2516 }
2517 BUG_ON(btrfs_header_nritems(b) == 1);
2518 }
2519 return 0;
2520
2521again:
2522 ret = -EAGAIN;
2523done:
2524 return ret;
2525}
2526
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002527static void key_search_validate(struct extent_buffer *b,
Omar Sandoval310712b2017-01-17 23:24:37 -08002528 const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002529 int level)
2530{
2531#ifdef CONFIG_BTRFS_ASSERT
2532 struct btrfs_disk_key disk_key;
2533
2534 btrfs_cpu_key_to_disk(&disk_key, key);
2535
2536 if (level == 0)
2537 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2538 offsetof(struct btrfs_leaf, items[0].key),
2539 sizeof(disk_key)));
2540 else
2541 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2542 offsetof(struct btrfs_node, ptrs[0].key),
2543 sizeof(disk_key)));
2544#endif
2545}
2546
Omar Sandoval310712b2017-01-17 23:24:37 -08002547static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002548 int level, int *prev_cmp, int *slot)
2549{
2550 if (*prev_cmp != 0) {
Nikolay Borisova74b35e2017-12-08 16:27:43 +02002551 *prev_cmp = btrfs_bin_search(b, key, level, slot);
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002552 return *prev_cmp;
2553 }
2554
2555 key_search_validate(b, key, level);
2556 *slot = 0;
2557
2558 return 0;
2559}
2560
David Sterba381cf652015-01-02 18:45:16 +01002561int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002562 u64 iobjectid, u64 ioff, u8 key_type,
2563 struct btrfs_key *found_key)
2564{
2565 int ret;
2566 struct btrfs_key key;
2567 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002568
2569 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002570 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002571
2572 key.type = key_type;
2573 key.objectid = iobjectid;
2574 key.offset = ioff;
2575
2576 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002577 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002578 return ret;
2579
2580 eb = path->nodes[0];
2581 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2582 ret = btrfs_next_leaf(fs_root, path);
2583 if (ret)
2584 return ret;
2585 eb = path->nodes[0];
2586 }
2587
2588 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2589 if (found_key->type != key.type ||
2590 found_key->objectid != key.objectid)
2591 return 1;
2592
2593 return 0;
2594}
2595
Liu Bo1fc28d82018-05-18 11:00:21 +08002596static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2597 struct btrfs_path *p,
2598 int write_lock_level)
2599{
2600 struct btrfs_fs_info *fs_info = root->fs_info;
2601 struct extent_buffer *b;
2602 int root_lock;
2603 int level = 0;
2604
2605 /* We try very hard to do read locks on the root */
2606 root_lock = BTRFS_READ_LOCK;
2607
2608 if (p->search_commit_root) {
2609 /* The commit roots are read only so we always do read locks */
2610 if (p->need_commit_sem)
2611 down_read(&fs_info->commit_root_sem);
2612 b = root->commit_root;
2613 extent_buffer_get(b);
2614 level = btrfs_header_level(b);
2615 if (p->need_commit_sem)
2616 up_read(&fs_info->commit_root_sem);
Liu Bof9ddfd02018-05-29 21:27:06 +08002617 /*
2618 * Ensure that all callers have set skip_locking when
2619 * p->search_commit_root = 1.
2620 */
2621 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08002622
2623 goto out;
2624 }
2625
2626 if (p->skip_locking) {
2627 b = btrfs_root_node(root);
2628 level = btrfs_header_level(b);
2629 goto out;
2630 }
2631
2632 /*
Liu Bo662c6532018-05-18 11:00:23 +08002633 * If the level is set to maximum, we can skip trying to get the read
2634 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08002635 */
Liu Bo662c6532018-05-18 11:00:23 +08002636 if (write_lock_level < BTRFS_MAX_LEVEL) {
2637 /*
2638 * We don't know the level of the root node until we actually
2639 * have it read locked
2640 */
2641 b = btrfs_read_lock_root_node(root);
2642 level = btrfs_header_level(b);
2643 if (level > write_lock_level)
2644 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08002645
Liu Bo662c6532018-05-18 11:00:23 +08002646 /* Whoops, must trade for write lock */
2647 btrfs_tree_read_unlock(b);
2648 free_extent_buffer(b);
2649 }
2650
Liu Bo1fc28d82018-05-18 11:00:21 +08002651 b = btrfs_lock_root_node(root);
2652 root_lock = BTRFS_WRITE_LOCK;
2653
2654 /* The level might have changed, check again */
2655 level = btrfs_header_level(b);
2656
2657out:
2658 p->nodes[level] = b;
2659 if (!p->skip_locking)
2660 p->locks[level] = root_lock;
2661 /*
2662 * Callers are responsible for dropping b's references.
2663 */
2664 return b;
2665}
2666
2667
Chris Masonc8c42862009-04-03 10:14:18 -04002668/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002669 * btrfs_search_slot - look for a key in a tree and perform necessary
2670 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002671 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002672 * @trans: Handle of transaction, used when modifying the tree
2673 * @p: Holds all btree nodes along the search path
2674 * @root: The root node of the tree
2675 * @key: The key we are looking for
2676 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2677 * deletions it's -1. 0 for plain searches
2678 * @cow: boolean should CoW operations be performed. Must always be 1
2679 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002680 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002681 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2682 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2683 *
2684 * If @key is found, 0 is returned and you can find the item in the leaf level
2685 * of the path (level 0)
2686 *
2687 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2688 * points to the slot where it should be inserted
2689 *
2690 * If an error is encountered while searching the tree a negative error number
2691 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002692 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002693int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2694 const struct btrfs_key *key, struct btrfs_path *p,
2695 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002696{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002697 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002698 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699 int slot;
2700 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002701 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002702 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002703 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002704 /* everything at write_lock_level or lower must be write locked */
2705 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002706 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002707 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002708 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002709
Chris Mason6702ed42007-08-07 16:15:09 -04002710 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002711 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002712 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002713 BUG_ON(!cow && ins_len);
Josef Bacik251792012008-10-29 14:49:05 -04002714
Chris Masonbd681512011-07-16 15:23:14 -04002715 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002716 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002717
Chris Masonbd681512011-07-16 15:23:14 -04002718 /* when we are removing items, we might have to go up to level
2719 * two as we update tree pointers Make sure we keep write
2720 * for those levels as well
2721 */
2722 write_lock_level = 2;
2723 } else if (ins_len > 0) {
2724 /*
2725 * for inserting items, make sure we have a write lock on
2726 * level 1 so we can update keys
2727 */
2728 write_lock_level = 1;
2729 }
2730
2731 if (!cow)
2732 write_lock_level = -1;
2733
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002734 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002735 write_lock_level = BTRFS_MAX_LEVEL;
2736
Chris Masonf7c79f32012-03-19 15:54:38 -04002737 min_write_lock_level = write_lock_level;
2738
Chris Masonbb803952007-03-01 12:04:21 -05002739again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002740 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08002741 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Chris Mason925baed2008-06-25 16:01:30 -04002742
Chris Masoneb60cea2007-02-02 09:18:22 -05002743 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002744 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002745
2746 /*
2747 * setup the path here so we can release it under lock
2748 * contention with the cow code
2749 */
Chris Mason02217ed2007-03-02 16:08:05 -05002750 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002751 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2752
Chris Masonc8c42862009-04-03 10:14:18 -04002753 /*
2754 * if we don't really need to cow this block
2755 * then we don't want to set the path blocking,
2756 * so we test it here
2757 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002758 if (!should_cow_block(trans, root, b)) {
2759 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002760 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002761 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002762
Chris Masonbd681512011-07-16 15:23:14 -04002763 /*
2764 * must have write locks on this node and the
2765 * parent
2766 */
Josef Bacik5124e002012-11-07 13:44:13 -05002767 if (level > write_lock_level ||
2768 (level + 1 > write_lock_level &&
2769 level + 1 < BTRFS_MAX_LEVEL &&
2770 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002771 write_lock_level = level + 1;
2772 btrfs_release_path(p);
2773 goto again;
2774 }
2775
Filipe Manana160f4082014-07-28 19:37:17 +01002776 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002777 if (last_level)
2778 err = btrfs_cow_block(trans, root, b, NULL, 0,
2779 &b);
2780 else
2781 err = btrfs_cow_block(trans, root, b,
2782 p->nodes[level + 1],
2783 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002784 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002785 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002786 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002787 }
Chris Mason02217ed2007-03-02 16:08:05 -05002788 }
Chris Mason65b51a02008-08-01 15:11:20 -04002789cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002790 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002791 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002792
2793 /*
2794 * we have a lock on b and as long as we aren't changing
2795 * the tree, there is no way to for the items in b to change.
2796 * It is safe to drop the lock on our parent before we
2797 * go through the expensive btree search on b.
2798 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002799 * If we're inserting or deleting (ins_len != 0), then we might
2800 * be changing slot zero, which may require changing the parent.
2801 * So, we can't drop the lock until after we know which slot
2802 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002803 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002804 if (!ins_len && !p->keep_locks) {
2805 int u = level + 1;
2806
2807 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2808 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2809 p->locks[u] = 0;
2810 }
2811 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002812
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002813 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002814 if (ret < 0)
2815 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002816
Chris Mason5f39d392007-10-15 16:14:19 -04002817 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002818 int dec = 0;
2819 if (ret && slot > 0) {
2820 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002821 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002822 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002823 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002824 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002825 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002826 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002827 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002828 if (err) {
2829 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002830 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002831 }
Chris Masonc8c42862009-04-03 10:14:18 -04002832 b = p->nodes[level];
2833 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002834
Chris Masonbd681512011-07-16 15:23:14 -04002835 /*
2836 * slot 0 is special, if we change the key
2837 * we have to update the parent pointer
2838 * which means we must have a write lock
2839 * on the parent
2840 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002841 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002842 write_lock_level < level + 1) {
2843 write_lock_level = level + 1;
2844 btrfs_release_path(p);
2845 goto again;
2846 }
2847
Chris Masonf7c79f32012-03-19 15:54:38 -04002848 unlock_up(p, level, lowest_unlock,
2849 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002850
Chris Mason925baed2008-06-25 16:01:30 -04002851 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002852 if (dec)
2853 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002854 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002855 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002856
Liu Bod07b8522017-01-30 12:23:42 -08002857 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01002858 slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002859 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002860 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002861 if (err) {
2862 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002863 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002864 }
Chris Mason76a05b32009-05-14 13:24:30 -04002865
Chris Masonb4ce94d2009-02-04 09:25:08 -05002866 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002867 level = btrfs_header_level(b);
2868 if (level <= write_lock_level) {
2869 err = btrfs_try_tree_write_lock(b);
2870 if (!err) {
2871 btrfs_set_path_blocking(p);
2872 btrfs_tree_lock(b);
2873 btrfs_clear_path_blocking(p, b,
2874 BTRFS_WRITE_LOCK);
2875 }
2876 p->locks[level] = BTRFS_WRITE_LOCK;
2877 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002878 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002879 if (!err) {
2880 btrfs_set_path_blocking(p);
2881 btrfs_tree_read_lock(b);
2882 btrfs_clear_path_blocking(p, b,
2883 BTRFS_READ_LOCK);
2884 }
2885 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002886 }
Chris Masonbd681512011-07-16 15:23:14 -04002887 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002888 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002889 } else {
2890 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002891 if (ins_len > 0 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002892 btrfs_leaf_free_space(fs_info, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002893 if (write_lock_level < 1) {
2894 write_lock_level = 1;
2895 btrfs_release_path(p);
2896 goto again;
2897 }
2898
Chris Masonb4ce94d2009-02-04 09:25:08 -05002899 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002900 err = split_leaf(trans, root, key,
2901 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002902 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002903
Yan Zheng33c66f42009-07-22 09:59:00 -04002904 BUG_ON(err > 0);
2905 if (err) {
2906 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002907 goto done;
2908 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002909 }
Chris Mason459931e2008-12-10 09:10:46 -05002910 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002911 unlock_up(p, level, lowest_unlock,
Liu Bo4b6f8e92018-08-14 10:46:53 +08002912 min_write_lock_level, NULL);
Chris Mason65b51a02008-08-01 15:11:20 -04002913 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002914 }
2915 }
Chris Mason65b51a02008-08-01 15:11:20 -04002916 ret = 1;
2917done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002918 /*
2919 * we don't really know what they plan on doing with the path
2920 * from here on, so for now just mark it as blocking
2921 */
Chris Masonb9473432009-03-13 11:00:37 -04002922 if (!p->leave_spinning)
2923 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002924 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002925 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002926 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002927}
2928
Chris Mason74123bd2007-02-02 11:05:29 -05002929/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002930 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2931 * current state of the tree together with the operations recorded in the tree
2932 * modification log to search for the key in a previous version of this tree, as
2933 * denoted by the time_seq parameter.
2934 *
2935 * Naturally, there is no support for insert, delete or cow operations.
2936 *
2937 * The resulting path and return value will be set up as if we called
2938 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2939 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002940int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002941 struct btrfs_path *p, u64 time_seq)
2942{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002943 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002944 struct extent_buffer *b;
2945 int slot;
2946 int ret;
2947 int err;
2948 int level;
2949 int lowest_unlock = 1;
2950 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002951 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002952
2953 lowest_level = p->lowest_level;
2954 WARN_ON(p->nodes[0] != NULL);
2955
2956 if (p->search_commit_root) {
2957 BUG_ON(time_seq);
2958 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2959 }
2960
2961again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002962 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002963 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002964 p->locks[level] = BTRFS_READ_LOCK;
2965
2966 while (b) {
2967 level = btrfs_header_level(b);
2968 p->nodes[level] = b;
2969 btrfs_clear_path_blocking(p, NULL, 0);
2970
2971 /*
2972 * we have a lock on b and as long as we aren't changing
2973 * the tree, there is no way to for the items in b to change.
2974 * It is safe to drop the lock on our parent before we
2975 * go through the expensive btree search on b.
2976 */
2977 btrfs_unlock_up_safe(p, level + 1);
2978
Josef Bacikd4b40872013-09-24 14:09:34 -04002979 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002980 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002981 * time.
2982 */
2983 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002984 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002985
2986 if (level != 0) {
2987 int dec = 0;
2988 if (ret && slot > 0) {
2989 dec = 1;
2990 slot -= 1;
2991 }
2992 p->slots[level] = slot;
2993 unlock_up(p, level, lowest_unlock, 0, NULL);
2994
2995 if (level == lowest_level) {
2996 if (dec)
2997 p->slots[level]++;
2998 goto done;
2999 }
3000
Liu Bod07b8522017-01-30 12:23:42 -08003001 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01003002 slot, key);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003003 if (err == -EAGAIN)
3004 goto again;
3005 if (err) {
3006 ret = err;
3007 goto done;
3008 }
3009
3010 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003011 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003012 if (!err) {
3013 btrfs_set_path_blocking(p);
3014 btrfs_tree_read_lock(b);
3015 btrfs_clear_path_blocking(p, b,
3016 BTRFS_READ_LOCK);
3017 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003018 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003019 if (!b) {
3020 ret = -ENOMEM;
3021 goto done;
3022 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003023 p->locks[level] = BTRFS_READ_LOCK;
3024 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003025 } else {
3026 p->slots[level] = slot;
3027 unlock_up(p, level, lowest_unlock, 0, NULL);
3028 goto done;
3029 }
3030 }
3031 ret = 1;
3032done:
3033 if (!p->leave_spinning)
3034 btrfs_set_path_blocking(p);
3035 if (ret < 0)
3036 btrfs_release_path(p);
3037
3038 return ret;
3039}
3040
3041/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003042 * helper to use instead of search slot if no exact match is needed but
3043 * instead the next or previous item should be returned.
3044 * When find_higher is true, the next higher item is returned, the next lower
3045 * otherwise.
3046 * When return_any and find_higher are both true, and no higher item is found,
3047 * return the next lower instead.
3048 * When return_any is true and find_higher is false, and no lower item is found,
3049 * return the next higher instead.
3050 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3051 * < 0 on error
3052 */
3053int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003054 const struct btrfs_key *key,
3055 struct btrfs_path *p, int find_higher,
3056 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003057{
3058 int ret;
3059 struct extent_buffer *leaf;
3060
3061again:
3062 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3063 if (ret <= 0)
3064 return ret;
3065 /*
3066 * a return value of 1 means the path is at the position where the
3067 * item should be inserted. Normally this is the next bigger item,
3068 * but in case the previous item is the last in a leaf, path points
3069 * to the first free slot in the previous leaf, i.e. at an invalid
3070 * item.
3071 */
3072 leaf = p->nodes[0];
3073
3074 if (find_higher) {
3075 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3076 ret = btrfs_next_leaf(root, p);
3077 if (ret <= 0)
3078 return ret;
3079 if (!return_any)
3080 return 1;
3081 /*
3082 * no higher item found, return the next
3083 * lower instead
3084 */
3085 return_any = 0;
3086 find_higher = 0;
3087 btrfs_release_path(p);
3088 goto again;
3089 }
3090 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003091 if (p->slots[0] == 0) {
3092 ret = btrfs_prev_leaf(root, p);
3093 if (ret < 0)
3094 return ret;
3095 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003096 leaf = p->nodes[0];
3097 if (p->slots[0] == btrfs_header_nritems(leaf))
3098 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003099 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003100 }
Arne Jansene6793762011-09-13 11:18:10 +02003101 if (!return_any)
3102 return 1;
3103 /*
3104 * no lower item found, return the next
3105 * higher instead
3106 */
3107 return_any = 0;
3108 find_higher = 1;
3109 btrfs_release_path(p);
3110 goto again;
3111 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003112 --p->slots[0];
3113 }
3114 }
3115 return 0;
3116}
3117
3118/*
Chris Mason74123bd2007-02-02 11:05:29 -05003119 * adjust the pointers going up the tree, starting at level
3120 * making sure the right key of each node is points to 'key'.
3121 * This is used after shifting pointers to the left, so it stops
3122 * fixing up pointers when a given leaf/node is not in slot 0 of the
3123 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003124 *
Chris Mason74123bd2007-02-02 11:05:29 -05003125 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003126static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003127 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003128{
3129 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003130 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01003131 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003132
Chris Mason234b63a2007-03-13 10:46:10 -04003133 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003134 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003135
Chris Masoneb60cea2007-02-02 09:18:22 -05003136 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003137 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003138 t = path->nodes[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003139 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3140 GFP_ATOMIC);
3141 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003142 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003143 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003144 if (tslot != 0)
3145 break;
3146 }
3147}
3148
Chris Mason74123bd2007-02-02 11:05:29 -05003149/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003150 * update item key.
3151 *
3152 * This function isn't completely safe. It's the caller's responsibility
3153 * that the new key won't break the order
3154 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003155void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3156 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003157 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003158{
3159 struct btrfs_disk_key disk_key;
3160 struct extent_buffer *eb;
3161 int slot;
3162
3163 eb = path->nodes[0];
3164 slot = path->slots[0];
3165 if (slot > 0) {
3166 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003167 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003168 }
3169 if (slot < btrfs_header_nritems(eb) - 1) {
3170 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003171 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003172 }
3173
3174 btrfs_cpu_key_to_disk(&disk_key, new_key);
3175 btrfs_set_item_key(eb, &disk_key, slot);
3176 btrfs_mark_buffer_dirty(eb);
3177 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003178 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003179}
3180
3181/*
Chris Mason74123bd2007-02-02 11:05:29 -05003182 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003183 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003184 *
3185 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3186 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003187 */
Chris Mason98ed5172008-01-03 10:01:48 -05003188static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003189 struct btrfs_fs_info *fs_info,
3190 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003191 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003192{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003193 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003194 int src_nritems;
3195 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003196 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003197
Chris Mason5f39d392007-10-15 16:14:19 -04003198 src_nritems = btrfs_header_nritems(src);
3199 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003200 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003201 WARN_ON(btrfs_header_generation(src) != trans->transid);
3202 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003203
Chris Masonbce4eae2008-04-24 14:42:46 -04003204 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003205 return 1;
3206
Chris Masond3977122009-01-05 21:25:51 -05003207 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003208 return 1;
3209
Chris Masonbce4eae2008-04-24 14:42:46 -04003210 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003211 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003212 if (push_items < src_nritems) {
3213 /* leave at least 8 pointers in the node if
3214 * we aren't going to empty it
3215 */
3216 if (src_nritems - push_items < 8) {
3217 if (push_items <= 8)
3218 return 1;
3219 push_items -= 8;
3220 }
3221 }
3222 } else
3223 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003224
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003225 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003226 push_items);
3227 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003228 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003229 return ret;
3230 }
Chris Mason5f39d392007-10-15 16:14:19 -04003231 copy_extent_buffer(dst, src,
3232 btrfs_node_key_ptr_offset(dst_nritems),
3233 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003234 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003235
Chris Masonbb803952007-03-01 12:04:21 -05003236 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003237 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003238 * Don't call tree_mod_log_insert_move here, key removal was
3239 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003240 */
Chris Mason5f39d392007-10-15 16:14:19 -04003241 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3242 btrfs_node_key_ptr_offset(push_items),
3243 (src_nritems - push_items) *
3244 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003245 }
Chris Mason5f39d392007-10-15 16:14:19 -04003246 btrfs_set_header_nritems(src, src_nritems - push_items);
3247 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3248 btrfs_mark_buffer_dirty(src);
3249 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003250
Chris Masonbb803952007-03-01 12:04:21 -05003251 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003252}
3253
Chris Mason97571fd2007-02-24 13:39:08 -05003254/*
Chris Mason79f95c82007-03-01 15:16:26 -05003255 * try to push data from one node into the next node right in the
3256 * tree.
3257 *
3258 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3259 * error, and > 0 if there was no room in the right hand block.
3260 *
3261 * this will only push up to 1/2 the contents of the left node over
3262 */
Chris Mason5f39d392007-10-15 16:14:19 -04003263static int balance_node_right(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003264 struct btrfs_fs_info *fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04003265 struct extent_buffer *dst,
3266 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003267{
Chris Mason79f95c82007-03-01 15:16:26 -05003268 int push_items = 0;
3269 int max_push;
3270 int src_nritems;
3271 int dst_nritems;
3272 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003273
Chris Mason7bb86312007-12-11 09:25:06 -05003274 WARN_ON(btrfs_header_generation(src) != trans->transid);
3275 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3276
Chris Mason5f39d392007-10-15 16:14:19 -04003277 src_nritems = btrfs_header_nritems(src);
3278 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003279 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003280 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003281 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003282
Chris Masond3977122009-01-05 21:25:51 -05003283 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003284 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003285
3286 max_push = src_nritems / 2 + 1;
3287 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003288 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003289 return 1;
Yan252c38f2007-08-29 09:11:44 -04003290
Chris Mason79f95c82007-03-01 15:16:26 -05003291 if (max_push < push_items)
3292 push_items = max_push;
3293
David Sterbabf1d3422018-03-05 15:47:39 +01003294 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3295 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003296 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3297 btrfs_node_key_ptr_offset(0),
3298 (dst_nritems) *
3299 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003300
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003301 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003302 src_nritems - push_items, push_items);
3303 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003304 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003305 return ret;
3306 }
Chris Mason5f39d392007-10-15 16:14:19 -04003307 copy_extent_buffer(dst, src,
3308 btrfs_node_key_ptr_offset(0),
3309 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003310 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003311
Chris Mason5f39d392007-10-15 16:14:19 -04003312 btrfs_set_header_nritems(src, src_nritems - push_items);
3313 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003314
Chris Mason5f39d392007-10-15 16:14:19 -04003315 btrfs_mark_buffer_dirty(src);
3316 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003317
Chris Mason79f95c82007-03-01 15:16:26 -05003318 return ret;
3319}
3320
3321/*
Chris Mason97571fd2007-02-24 13:39:08 -05003322 * helper function to insert a new root level in the tree.
3323 * A new node is allocated, and a single item is inserted to
3324 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003325 *
3326 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003327 */
Chris Masond3977122009-01-05 21:25:51 -05003328static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003329 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003330 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003331{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003332 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003333 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003334 struct extent_buffer *lower;
3335 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003336 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003337 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01003338 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003339
3340 BUG_ON(path->nodes[level]);
3341 BUG_ON(path->nodes[level-1] != root->node);
3342
Chris Mason7bb86312007-12-11 09:25:06 -05003343 lower = path->nodes[level-1];
3344 if (level == 1)
3345 btrfs_item_key(lower, &lower_key, 0);
3346 else
3347 btrfs_node_key(lower, &lower_key, 0);
3348
David Sterba4d75f8a2014-06-15 01:54:12 +02003349 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3350 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003351 if (IS_ERR(c))
3352 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003353
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003354 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003355
Chris Mason5f39d392007-10-15 16:14:19 -04003356 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003357 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003358 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003359 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003360 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003361
3362 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003363
3364 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003365
Chris Mason925baed2008-06-25 16:01:30 -04003366 old = root->node;
David Sterbad9d19a02018-03-05 16:35:29 +01003367 ret = tree_mod_log_insert_root(root->node, c, 0);
3368 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003369 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003370
3371 /* the super has an extra ref to root->node */
3372 free_extent_buffer(old);
3373
Chris Mason0b86a832008-03-24 15:01:56 -04003374 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003375 extent_buffer_get(c);
3376 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303377 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003378 path->slots[level] = 0;
3379 return 0;
3380}
3381
Chris Mason74123bd2007-02-02 11:05:29 -05003382/*
3383 * worker function to insert a single pointer in a node.
3384 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003385 *
Chris Mason74123bd2007-02-02 11:05:29 -05003386 * slot and level indicate where you want the key to go, and
3387 * blocknr is the block the key points to.
3388 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003389static void insert_ptr(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003390 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003391 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003392 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003393{
Chris Mason5f39d392007-10-15 16:14:19 -04003394 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003395 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003396 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003397
3398 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003399 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003400 lower = path->nodes[level];
3401 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003402 BUG_ON(slot > nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003403 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003404 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003405 if (level) {
3406 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003407 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003408 BUG_ON(ret < 0);
3409 }
Chris Mason5f39d392007-10-15 16:14:19 -04003410 memmove_extent_buffer(lower,
3411 btrfs_node_key_ptr_offset(slot + 1),
3412 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003413 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003414 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003415 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003416 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3417 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003418 BUG_ON(ret < 0);
3419 }
Chris Mason5f39d392007-10-15 16:14:19 -04003420 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003421 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003422 WARN_ON(trans->transid == 0);
3423 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003424 btrfs_set_header_nritems(lower, nritems + 1);
3425 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003426}
3427
Chris Mason97571fd2007-02-24 13:39:08 -05003428/*
3429 * split the node at the specified level in path in two.
3430 * The path is corrected to point to the appropriate node after the split
3431 *
3432 * Before splitting this tries to make some room in the node by pushing
3433 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003434 *
3435 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003436 */
Chris Masone02119d2008-09-05 16:13:11 -04003437static noinline int split_node(struct btrfs_trans_handle *trans,
3438 struct btrfs_root *root,
3439 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003441 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003442 struct extent_buffer *c;
3443 struct extent_buffer *split;
3444 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003445 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003446 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003447 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003448
Chris Mason5f39d392007-10-15 16:14:19 -04003449 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003450 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003451 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003452 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003453 * trying to split the root, lets make a new one
3454 *
Liu Bofdd99c72013-05-22 12:06:51 +00003455 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003456 * insert_new_root, because that root buffer will be kept as a
3457 * normal node. We are going to log removal of half of the
3458 * elements below with tree_mod_log_eb_copy. We're holding a
3459 * tree lock on the buffer, which is why we cannot race with
3460 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003461 */
Liu Bofdd99c72013-05-22 12:06:51 +00003462 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003463 if (ret)
3464 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003465 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003466 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003467 c = path->nodes[level];
3468 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003469 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003470 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003471 if (ret < 0)
3472 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003473 }
Chris Masone66f7092007-04-20 13:16:02 -04003474
Chris Mason5f39d392007-10-15 16:14:19 -04003475 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003476 mid = (c_nritems + 1) / 2;
3477 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003478
David Sterba4d75f8a2014-06-15 01:54:12 +02003479 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3480 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003481 if (IS_ERR(split))
3482 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003483
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003484 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03003485 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04003486
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003487 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003488 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003489 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003490 return ret;
3491 }
Chris Mason5f39d392007-10-15 16:14:19 -04003492 copy_extent_buffer(split, c,
3493 btrfs_node_key_ptr_offset(0),
3494 btrfs_node_key_ptr_offset(mid),
3495 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3496 btrfs_set_header_nritems(split, c_nritems - mid);
3497 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003498 ret = 0;
3499
Chris Mason5f39d392007-10-15 16:14:19 -04003500 btrfs_mark_buffer_dirty(c);
3501 btrfs_mark_buffer_dirty(split);
3502
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003503 insert_ptr(trans, fs_info, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003504 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003505
Chris Mason5de08d72007-02-24 06:24:44 -05003506 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003507 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003508 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003509 free_extent_buffer(c);
3510 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003511 path->slots[level + 1] += 1;
3512 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003513 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003514 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003515 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003516 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003517}
3518
Chris Mason74123bd2007-02-02 11:05:29 -05003519/*
3520 * how many bytes are required to store the items in a leaf. start
3521 * and nr indicate which items in the leaf to check. This totals up the
3522 * space used both by the item structs and the item data
3523 */
Chris Mason5f39d392007-10-15 16:14:19 -04003524static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003525{
Josef Bacik41be1f32012-10-15 13:43:18 -04003526 struct btrfs_item *start_item;
3527 struct btrfs_item *end_item;
3528 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003529 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003530 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003531 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003532
3533 if (!nr)
3534 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003535 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003536 start_item = btrfs_item_nr(start);
3537 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003538 data_len = btrfs_token_item_offset(l, start_item, &token) +
3539 btrfs_token_item_size(l, start_item, &token);
3540 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003541 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003542 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003543 return data_len;
3544}
3545
Chris Mason74123bd2007-02-02 11:05:29 -05003546/*
Chris Masond4dbff92007-04-04 14:08:15 -04003547 * The space between the end of the leaf items and
3548 * the start of the leaf data. IOW, how much room
3549 * the leaf has left for both items and data
3550 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003551noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04003552 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003553{
Chris Mason5f39d392007-10-15 16:14:19 -04003554 int nritems = btrfs_header_nritems(leaf);
3555 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556
3557 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003558 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559 btrfs_crit(fs_info,
3560 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3561 ret,
3562 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3563 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003564 }
3565 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003566}
3567
Chris Mason99d8f832010-07-07 10:51:48 -04003568/*
3569 * min slot controls the lowest index we're willing to push to the
3570 * right. We'll push up to and including min_slot, but no lower
3571 */
David Sterba1e47eef2017-02-10 19:13:06 +01003572static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003573 struct btrfs_path *path,
3574 int data_size, int empty,
3575 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003576 int free_space, u32 left_nritems,
3577 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003578{
Chris Mason5f39d392007-10-15 16:14:19 -04003579 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003580 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003581 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003582 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003583 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003584 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003585 int push_space = 0;
3586 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003587 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003588 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003589 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003590 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003591 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003592
Chris Masoncfed81a2012-03-03 07:40:03 -05003593 btrfs_init_map_token(&token);
3594
Chris Mason34a38212007-11-07 13:31:03 -05003595 if (empty)
3596 nr = 0;
3597 else
Chris Mason99d8f832010-07-07 10:51:48 -04003598 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003599
Zheng Yan31840ae2008-09-23 13:14:14 -04003600 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003601 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003602
Chris Mason44871b12009-03-13 10:04:31 -04003603 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003604 i = left_nritems - 1;
3605 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003606 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003607
Zheng Yan31840ae2008-09-23 13:14:14 -04003608 if (!empty && push_items > 0) {
3609 if (path->slots[0] > i)
3610 break;
3611 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003612 int space = btrfs_leaf_free_space(fs_info, left);
Zheng Yan31840ae2008-09-23 13:14:14 -04003613 if (space + push_space * 2 > free_space)
3614 break;
3615 }
3616 }
3617
Chris Mason00ec4c52007-02-24 12:47:20 -05003618 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003619 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003620
Chris Masondb945352007-10-15 16:15:53 -04003621 this_item_size = btrfs_item_size(left, item);
3622 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003623 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003624
Chris Mason00ec4c52007-02-24 12:47:20 -05003625 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003626 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003627 if (i == 0)
3628 break;
3629 i--;
Chris Masondb945352007-10-15 16:15:53 -04003630 }
Chris Mason5f39d392007-10-15 16:14:19 -04003631
Chris Mason925baed2008-06-25 16:01:30 -04003632 if (push_items == 0)
3633 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Julia Lawall6c1500f2012-11-03 20:30:18 +00003635 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Chris Mason00ec4c52007-02-24 12:47:20 -05003637 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003638 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003639
Chris Mason5f39d392007-10-15 16:14:19 -04003640 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003641 push_space -= leaf_data_end(fs_info, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003642
Chris Mason00ec4c52007-02-24 12:47:20 -05003643 /* make room in the right data area */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003644 data_end = leaf_data_end(fs_info, right);
Chris Mason5f39d392007-10-15 16:14:19 -04003645 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003646 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3647 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003648 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003649
Chris Mason00ec4c52007-02-24 12:47:20 -05003650 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003651 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003652 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003653 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
Chris Masond6025572007-03-30 14:27:56 -04003654 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003655
3656 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3657 btrfs_item_nr_offset(0),
3658 right_nritems * sizeof(struct btrfs_item));
3659
Chris Mason00ec4c52007-02-24 12:47:20 -05003660 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003661 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3662 btrfs_item_nr_offset(left_nritems - push_items),
3663 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003664
3665 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003666 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003667 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003668 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003669 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003670 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003671 push_space -= btrfs_token_item_size(right, item, &token);
3672 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003673 }
3674
Chris Mason7518a232007-03-12 12:01:18 -04003675 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003676 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003677
Chris Mason34a38212007-11-07 13:31:03 -05003678 if (left_nritems)
3679 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003680 else
David Sterba7c302b42017-02-10 18:47:57 +01003681 clean_tree_block(fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003682
Chris Mason5f39d392007-10-15 16:14:19 -04003683 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003684
Chris Mason5f39d392007-10-15 16:14:19 -04003685 btrfs_item_key(right, &disk_key, 0);
3686 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003687 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003688
Chris Mason00ec4c52007-02-24 12:47:20 -05003689 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003690 if (path->slots[0] >= left_nritems) {
3691 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003692 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba7c302b42017-02-10 18:47:57 +01003693 clean_tree_block(fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003694 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003695 free_extent_buffer(path->nodes[0]);
3696 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003697 path->slots[1] += 1;
3698 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003699 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003700 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003701 }
3702 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003703
3704out_unlock:
3705 btrfs_tree_unlock(right);
3706 free_extent_buffer(right);
3707 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003708}
Chris Mason925baed2008-06-25 16:01:30 -04003709
Chris Mason00ec4c52007-02-24 12:47:20 -05003710/*
Chris Mason44871b12009-03-13 10:04:31 -04003711 * push some data in the path leaf to the right, trying to free up at
3712 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3713 *
3714 * returns 1 if the push failed because the other node didn't have enough
3715 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003716 *
3717 * this will push starting from min_slot to the end of the leaf. It won't
3718 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003719 */
3720static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003721 *root, struct btrfs_path *path,
3722 int min_data_size, int data_size,
3723 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003724{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003725 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003726 struct extent_buffer *left = path->nodes[0];
3727 struct extent_buffer *right;
3728 struct extent_buffer *upper;
3729 int slot;
3730 int free_space;
3731 u32 left_nritems;
3732 int ret;
3733
3734 if (!path->nodes[1])
3735 return 1;
3736
3737 slot = path->slots[1];
3738 upper = path->nodes[1];
3739 if (slot >= btrfs_header_nritems(upper) - 1)
3740 return 1;
3741
3742 btrfs_assert_tree_locked(path->nodes[1]);
3743
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003744 right = read_node_slot(fs_info, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003745 /*
3746 * slot + 1 is not valid or we fail to read the right node,
3747 * no big deal, just return.
3748 */
3749 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003750 return 1;
3751
Chris Mason44871b12009-03-13 10:04:31 -04003752 btrfs_tree_lock(right);
3753 btrfs_set_lock_blocking(right);
3754
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003755 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003756 if (free_space < data_size)
3757 goto out_unlock;
3758
3759 /* cow and double check */
3760 ret = btrfs_cow_block(trans, root, right, upper,
3761 slot + 1, &right);
3762 if (ret)
3763 goto out_unlock;
3764
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003765 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003766 if (free_space < data_size)
3767 goto out_unlock;
3768
3769 left_nritems = btrfs_header_nritems(left);
3770 if (left_nritems == 0)
3771 goto out_unlock;
3772
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003773 if (path->slots[0] == left_nritems && !empty) {
3774 /* Key greater than all keys in the leaf, right neighbor has
3775 * enough room for it and we're not emptying our leaf to delete
3776 * it, therefore use right neighbor to insert the new item and
3777 * no need to touch/dirty our left leaft. */
3778 btrfs_tree_unlock(left);
3779 free_extent_buffer(left);
3780 path->nodes[0] = right;
3781 path->slots[0] = 0;
3782 path->slots[1]++;
3783 return 0;
3784 }
3785
David Sterba1e47eef2017-02-10 19:13:06 +01003786 return __push_leaf_right(fs_info, path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003787 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003788out_unlock:
3789 btrfs_tree_unlock(right);
3790 free_extent_buffer(right);
3791 return 1;
3792}
3793
3794/*
Chris Mason74123bd2007-02-02 11:05:29 -05003795 * push some data in the path leaf to the left, trying to free up at
3796 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003797 *
3798 * max_slot can put a limit on how far into the leaf we'll push items. The
3799 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3800 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003801 */
David Sterba66cb7dd2017-02-10 19:14:36 +01003802static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003803 struct btrfs_path *path, int data_size,
3804 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003805 int free_space, u32 right_nritems,
3806 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003807{
Chris Mason5f39d392007-10-15 16:14:19 -04003808 struct btrfs_disk_key disk_key;
3809 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003810 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003811 int push_space = 0;
3812 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003813 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003814 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003815 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003816 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003817 u32 this_item_size;
3818 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003819 struct btrfs_map_token token;
3820
3821 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003822
Chris Mason34a38212007-11-07 13:31:03 -05003823 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003824 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003825 else
Chris Mason99d8f832010-07-07 10:51:48 -04003826 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003827
3828 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003829 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003830
Zheng Yan31840ae2008-09-23 13:14:14 -04003831 if (!empty && push_items > 0) {
3832 if (path->slots[0] < i)
3833 break;
3834 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003835 int space = btrfs_leaf_free_space(fs_info, right);
Zheng Yan31840ae2008-09-23 13:14:14 -04003836 if (space + push_space * 2 > free_space)
3837 break;
3838 }
3839 }
3840
Chris Masonbe0e5c02007-01-26 15:51:26 -05003841 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003842 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003843
3844 this_item_size = btrfs_item_size(right, item);
3845 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003846 break;
Chris Masondb945352007-10-15 16:15:53 -04003847
Chris Masonbe0e5c02007-01-26 15:51:26 -05003848 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003849 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003850 }
Chris Masondb945352007-10-15 16:15:53 -04003851
Chris Masonbe0e5c02007-01-26 15:51:26 -05003852 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003853 ret = 1;
3854 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303856 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003857
Chris Masonbe0e5c02007-01-26 15:51:26 -05003858 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003859 copy_extent_buffer(left, right,
3860 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3861 btrfs_item_nr_offset(0),
3862 push_items * sizeof(struct btrfs_item));
3863
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003864 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003865 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003866
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003867 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003868 leaf_data_end(fs_info, left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003869 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003870 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003871 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003872 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003873 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003874
Chris Masondb945352007-10-15 16:15:53 -04003875 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003876 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003877 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003878
Ross Kirkdd3cc162013-09-16 15:58:09 +01003879 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003880
Chris Masoncfed81a2012-03-03 07:40:03 -05003881 ioff = btrfs_token_item_offset(left, item, &token);
3882 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003883 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003884 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003885 }
Chris Mason5f39d392007-10-15 16:14:19 -04003886 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003887
3888 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003889 if (push_items > right_nritems)
3890 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003891 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003892
Chris Mason34a38212007-11-07 13:31:03 -05003893 if (push_items < right_nritems) {
3894 push_space = btrfs_item_offset_nr(right, push_items - 1) -
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003895 leaf_data_end(fs_info, right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003896 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003897 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003898 BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003899 leaf_data_end(fs_info, right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003900
3901 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003902 btrfs_item_nr_offset(push_items),
3903 (btrfs_header_nritems(right) - push_items) *
3904 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003905 }
Yaneef1c492007-11-26 10:58:13 -05003906 right_nritems -= push_items;
3907 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003908 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003909 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003910 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003911
Chris Masoncfed81a2012-03-03 07:40:03 -05003912 push_space = push_space - btrfs_token_item_size(right,
3913 item, &token);
3914 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003915 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003916
Chris Mason5f39d392007-10-15 16:14:19 -04003917 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003918 if (right_nritems)
3919 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003920 else
David Sterba7c302b42017-02-10 18:47:57 +01003921 clean_tree_block(fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003922
Chris Mason5f39d392007-10-15 16:14:19 -04003923 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003924 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003925
3926 /* then fixup the leaf pointer in the path */
3927 if (path->slots[0] < push_items) {
3928 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003929 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003930 free_extent_buffer(path->nodes[0]);
3931 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003932 path->slots[1] -= 1;
3933 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003934 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003935 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003936 path->slots[0] -= push_items;
3937 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003938 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003939 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003940out:
3941 btrfs_tree_unlock(left);
3942 free_extent_buffer(left);
3943 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003944}
3945
Chris Mason74123bd2007-02-02 11:05:29 -05003946/*
Chris Mason44871b12009-03-13 10:04:31 -04003947 * push some data in the path leaf to the left, trying to free up at
3948 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003949 *
3950 * max_slot can put a limit on how far into the leaf we'll push items. The
3951 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3952 * items
Chris Mason44871b12009-03-13 10:04:31 -04003953 */
3954static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003955 *root, struct btrfs_path *path, int min_data_size,
3956 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003957{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003958 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003959 struct extent_buffer *right = path->nodes[0];
3960 struct extent_buffer *left;
3961 int slot;
3962 int free_space;
3963 u32 right_nritems;
3964 int ret = 0;
3965
3966 slot = path->slots[1];
3967 if (slot == 0)
3968 return 1;
3969 if (!path->nodes[1])
3970 return 1;
3971
3972 right_nritems = btrfs_header_nritems(right);
3973 if (right_nritems == 0)
3974 return 1;
3975
3976 btrfs_assert_tree_locked(path->nodes[1]);
3977
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003978 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003979 /*
3980 * slot - 1 is not valid or we fail to read the left node,
3981 * no big deal, just return.
3982 */
3983 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003984 return 1;
3985
Chris Mason44871b12009-03-13 10:04:31 -04003986 btrfs_tree_lock(left);
3987 btrfs_set_lock_blocking(left);
3988
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003989 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04003990 if (free_space < data_size) {
3991 ret = 1;
3992 goto out;
3993 }
3994
3995 /* cow and double check */
3996 ret = btrfs_cow_block(trans, root, left,
3997 path->nodes[1], slot - 1, &left);
3998 if (ret) {
3999 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004000 if (ret == -ENOSPC)
4001 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004002 goto out;
4003 }
4004
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004005 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04004006 if (free_space < data_size) {
4007 ret = 1;
4008 goto out;
4009 }
4010
David Sterba66cb7dd2017-02-10 19:14:36 +01004011 return __push_leaf_left(fs_info, path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004012 empty, left, free_space, right_nritems,
4013 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004014out:
4015 btrfs_tree_unlock(left);
4016 free_extent_buffer(left);
4017 return ret;
4018}
4019
4020/*
Chris Mason74123bd2007-02-02 11:05:29 -05004021 * split the path's leaf in two, making sure there is at least data_size
4022 * available for the resulting leaf level of the path.
4023 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004024static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004025 struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004026 struct btrfs_path *path,
4027 struct extent_buffer *l,
4028 struct extent_buffer *right,
4029 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004030{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004031 int data_copy_size;
4032 int rt_data_off;
4033 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004034 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004035 struct btrfs_map_token token;
4036
4037 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004038
Chris Mason5f39d392007-10-15 16:14:19 -04004039 nritems = nritems - mid;
4040 btrfs_set_header_nritems(right, nritems);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004041 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
Chris Mason5f39d392007-10-15 16:14:19 -04004042
4043 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4044 btrfs_item_nr_offset(mid),
4045 nritems * sizeof(struct btrfs_item));
4046
4047 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004048 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4049 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004050 leaf_data_end(fs_info, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004051
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004052 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004053
4054 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004055 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004056 u32 ioff;
4057
Chris Masoncfed81a2012-03-03 07:40:03 -05004058 ioff = btrfs_token_item_offset(right, item, &token);
4059 btrfs_set_token_item_offset(right, item,
4060 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004061 }
Chris Mason74123bd2007-02-02 11:05:29 -05004062
Chris Mason5f39d392007-10-15 16:14:19 -04004063 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004064 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004065 insert_ptr(trans, fs_info, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004066 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004067
4068 btrfs_mark_buffer_dirty(right);
4069 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004070 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004071
Chris Masonbe0e5c02007-01-26 15:51:26 -05004072 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004073 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004074 free_extent_buffer(path->nodes[0]);
4075 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004076 path->slots[0] -= mid;
4077 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004078 } else {
4079 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004080 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004081 }
Chris Mason5f39d392007-10-15 16:14:19 -04004082
Chris Masoneb60cea2007-02-02 09:18:22 -05004083 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004084}
4085
4086/*
Chris Mason99d8f832010-07-07 10:51:48 -04004087 * double splits happen when we need to insert a big item in the middle
4088 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4089 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4090 * A B C
4091 *
4092 * We avoid this by trying to push the items on either side of our target
4093 * into the adjacent leaves. If all goes well we can avoid the double split
4094 * completely.
4095 */
4096static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4097 struct btrfs_root *root,
4098 struct btrfs_path *path,
4099 int data_size)
4100{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004101 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason99d8f832010-07-07 10:51:48 -04004102 int ret;
4103 int progress = 0;
4104 int slot;
4105 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004106 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004107
4108 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004109 if (slot < btrfs_header_nritems(path->nodes[0]))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004110 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004111
4112 /*
4113 * try to push all the items after our slot into the
4114 * right leaf
4115 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004116 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004117 if (ret < 0)
4118 return ret;
4119
4120 if (ret == 0)
4121 progress++;
4122
4123 nritems = btrfs_header_nritems(path->nodes[0]);
4124 /*
4125 * our goal is to get our slot at the start or end of a leaf. If
4126 * we've done so we're done
4127 */
4128 if (path->slots[0] == 0 || path->slots[0] == nritems)
4129 return 0;
4130
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004131 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004132 return 0;
4133
4134 /* try to push all the items before our slot into the next leaf */
4135 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004136 space_needed = data_size;
4137 if (slot > 0)
4138 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004139 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004140 if (ret < 0)
4141 return ret;
4142
4143 if (ret == 0)
4144 progress++;
4145
4146 if (progress)
4147 return 0;
4148 return 1;
4149}
4150
4151/*
Chris Mason44871b12009-03-13 10:04:31 -04004152 * split the path's leaf in two, making sure there is at least data_size
4153 * available for the resulting leaf level of the path.
4154 *
4155 * returns 0 if all went well and < 0 on failure.
4156 */
4157static noinline int split_leaf(struct btrfs_trans_handle *trans,
4158 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004159 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004160 struct btrfs_path *path, int data_size,
4161 int extend)
4162{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004163 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004164 struct extent_buffer *l;
4165 u32 nritems;
4166 int mid;
4167 int slot;
4168 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004169 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004170 int ret = 0;
4171 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004172 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004173 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004174 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004175
Yan, Zhenga5719522009-09-24 09:17:31 -04004176 l = path->nodes[0];
4177 slot = path->slots[0];
4178 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004179 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004180 return -EOVERFLOW;
4181
Chris Mason44871b12009-03-13 10:04:31 -04004182 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004183 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004184 int space_needed = data_size;
4185
4186 if (slot < btrfs_header_nritems(l))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004187 space_needed -= btrfs_leaf_free_space(fs_info, l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004188
4189 wret = push_leaf_right(trans, root, path, space_needed,
4190 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004191 if (wret < 0)
4192 return wret;
4193 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004194 space_needed = data_size;
4195 if (slot > 0)
4196 space_needed -= btrfs_leaf_free_space(fs_info,
4197 l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004198 wret = push_leaf_left(trans, root, path, space_needed,
4199 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004200 if (wret < 0)
4201 return wret;
4202 }
4203 l = path->nodes[0];
4204
4205 /* did the pushes work? */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004206 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004207 return 0;
4208 }
4209
4210 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004211 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004212 if (ret)
4213 return ret;
4214 }
4215again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004216 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004217 l = path->nodes[0];
4218 slot = path->slots[0];
4219 nritems = btrfs_header_nritems(l);
4220 mid = (nritems + 1) / 2;
4221
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004222 if (mid <= slot) {
4223 if (nritems == 1 ||
4224 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004225 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004226 if (slot >= nritems) {
4227 split = 0;
4228 } else {
4229 mid = slot;
4230 if (mid != nritems &&
4231 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004232 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004233 if (data_size && !tried_avoid_double)
4234 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004235 split = 2;
4236 }
4237 }
4238 }
4239 } else {
4240 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004241 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004242 if (!extend && data_size && slot == 0) {
4243 split = 0;
4244 } else if ((extend || !data_size) && slot == 0) {
4245 mid = 1;
4246 } else {
4247 mid = slot;
4248 if (mid != nritems &&
4249 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004250 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004251 if (data_size && !tried_avoid_double)
4252 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304253 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004254 }
4255 }
4256 }
4257 }
4258
4259 if (split == 0)
4260 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4261 else
4262 btrfs_item_key(l, &disk_key, mid);
4263
David Sterba4d75f8a2014-06-15 01:54:12 +02004264 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4265 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004267 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004268
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004269 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004270
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004271 if (split == 0) {
4272 if (mid <= slot) {
4273 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004274 insert_ptr(trans, fs_info, path, &disk_key,
4275 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004276 btrfs_tree_unlock(path->nodes[0]);
4277 free_extent_buffer(path->nodes[0]);
4278 path->nodes[0] = right;
4279 path->slots[0] = 0;
4280 path->slots[1] += 1;
4281 } else {
4282 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004283 insert_ptr(trans, fs_info, path, &disk_key,
4284 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004285 btrfs_tree_unlock(path->nodes[0]);
4286 free_extent_buffer(path->nodes[0]);
4287 path->nodes[0] = right;
4288 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004289 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004290 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004291 }
Liu Bo196e0242016-09-07 14:48:28 -07004292 /*
4293 * We create a new leaf 'right' for the required ins_len and
4294 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4295 * the content of ins_len to 'right'.
4296 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004298 }
4299
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004300 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004301
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004302 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004303 BUG_ON(num_doubles != 0);
4304 num_doubles++;
4305 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004306 }
Chris Mason44871b12009-03-13 10:04:31 -04004307
Jeff Mahoney143bede2012-03-01 14:56:26 +01004308 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004309
4310push_for_double:
4311 push_for_double_split(trans, root, path, data_size);
4312 tried_avoid_double = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004313 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004314 return 0;
4315 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004316}
4317
Yan, Zhengad48fd752009-11-12 09:33:58 +00004318static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4319 struct btrfs_root *root,
4320 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004321{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004322 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004323 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004324 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004325 struct btrfs_file_extent_item *fi;
4326 u64 extent_len = 0;
4327 u32 item_size;
4328 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004329
4330 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004331 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4332
4333 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4334 key.type != BTRFS_EXTENT_CSUM_KEY);
4335
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004336 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004337 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004338
4339 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004340 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4341 fi = btrfs_item_ptr(leaf, path->slots[0],
4342 struct btrfs_file_extent_item);
4343 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4344 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004345 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004346
Chris Mason459931e2008-12-10 09:10:46 -05004347 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004348 path->search_for_split = 1;
4349 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004350 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004351 if (ret > 0)
4352 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004353 if (ret < 0)
4354 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004355
Yan, Zhengad48fd752009-11-12 09:33:58 +00004356 ret = -EAGAIN;
4357 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004358 /* if our item isn't there, return now */
4359 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004360 goto err;
4361
Chris Mason109f6ae2010-04-02 09:20:18 -04004362 /* the leaf has changed, it now has room. return now */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004363 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004364 goto err;
4365
Yan, Zhengad48fd752009-11-12 09:33:58 +00004366 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4367 fi = btrfs_item_ptr(leaf, path->slots[0],
4368 struct btrfs_file_extent_item);
4369 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4370 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004371 }
4372
Chris Masonb9473432009-03-13 11:00:37 -04004373 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004374 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004375 if (ret)
4376 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004377
Yan, Zhengad48fd752009-11-12 09:33:58 +00004378 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004379 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004380 return 0;
4381err:
4382 path->keep_locks = 0;
4383 return ret;
4384}
4385
David Sterba4961e292017-02-10 18:49:53 +01004386static noinline int split_item(struct btrfs_fs_info *fs_info,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004387 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004388 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004389 unsigned long split_offset)
4390{
4391 struct extent_buffer *leaf;
4392 struct btrfs_item *item;
4393 struct btrfs_item *new_item;
4394 int slot;
4395 char *buf;
4396 u32 nritems;
4397 u32 item_size;
4398 u32 orig_offset;
4399 struct btrfs_disk_key disk_key;
4400
Chris Masonb9473432009-03-13 11:00:37 -04004401 leaf = path->nodes[0];
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004402 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004403
Chris Masonb4ce94d2009-02-04 09:25:08 -05004404 btrfs_set_path_blocking(path);
4405
Ross Kirkdd3cc162013-09-16 15:58:09 +01004406 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004407 orig_offset = btrfs_item_offset(leaf, item);
4408 item_size = btrfs_item_size(leaf, item);
4409
Chris Mason459931e2008-12-10 09:10:46 -05004410 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004411 if (!buf)
4412 return -ENOMEM;
4413
Chris Mason459931e2008-12-10 09:10:46 -05004414 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4415 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004416
Chris Mason459931e2008-12-10 09:10:46 -05004417 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004418 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004419 if (slot != nritems) {
4420 /* shift the items */
4421 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004422 btrfs_item_nr_offset(slot),
4423 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004424 }
4425
4426 btrfs_cpu_key_to_disk(&disk_key, new_key);
4427 btrfs_set_item_key(leaf, &disk_key, slot);
4428
Ross Kirkdd3cc162013-09-16 15:58:09 +01004429 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004430
4431 btrfs_set_item_offset(leaf, new_item, orig_offset);
4432 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4433
4434 btrfs_set_item_offset(leaf, item,
4435 orig_offset + item_size - split_offset);
4436 btrfs_set_item_size(leaf, item, split_offset);
4437
4438 btrfs_set_header_nritems(leaf, nritems + 1);
4439
4440 /* write the data for the start of the original item */
4441 write_extent_buffer(leaf, buf,
4442 btrfs_item_ptr_offset(leaf, path->slots[0]),
4443 split_offset);
4444
4445 /* write the data for the new item */
4446 write_extent_buffer(leaf, buf + split_offset,
4447 btrfs_item_ptr_offset(leaf, slot),
4448 item_size - split_offset);
4449 btrfs_mark_buffer_dirty(leaf);
4450
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004451 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004452 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004453 return 0;
4454}
4455
4456/*
4457 * This function splits a single item into two items,
4458 * giving 'new_key' to the new item and splitting the
4459 * old one at split_offset (from the start of the item).
4460 *
4461 * The path may be released by this operation. After
4462 * the split, the path is pointing to the old item. The
4463 * new item is going to be in the same node as the old one.
4464 *
4465 * Note, the item being split must be smaller enough to live alone on
4466 * a tree block with room for one extra struct btrfs_item
4467 *
4468 * This allows us to split the item in place, keeping a lock on the
4469 * leaf the entire time.
4470 */
4471int btrfs_split_item(struct btrfs_trans_handle *trans,
4472 struct btrfs_root *root,
4473 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004474 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004475 unsigned long split_offset)
4476{
4477 int ret;
4478 ret = setup_leaf_for_split(trans, root, path,
4479 sizeof(struct btrfs_item));
4480 if (ret)
4481 return ret;
4482
David Sterba4961e292017-02-10 18:49:53 +01004483 ret = split_item(root->fs_info, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004484 return ret;
4485}
4486
4487/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004488 * This function duplicate a item, giving 'new_key' to the new item.
4489 * It guarantees both items live in the same tree leaf and the new item
4490 * is contiguous with the original item.
4491 *
4492 * This allows us to split file extent in place, keeping a lock on the
4493 * leaf the entire time.
4494 */
4495int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4496 struct btrfs_root *root,
4497 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004498 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004499{
4500 struct extent_buffer *leaf;
4501 int ret;
4502 u32 item_size;
4503
4504 leaf = path->nodes[0];
4505 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4506 ret = setup_leaf_for_split(trans, root, path,
4507 item_size + sizeof(struct btrfs_item));
4508 if (ret)
4509 return ret;
4510
4511 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004512 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004513 item_size, item_size +
4514 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004515 leaf = path->nodes[0];
4516 memcpy_extent_buffer(leaf,
4517 btrfs_item_ptr_offset(leaf, path->slots[0]),
4518 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4519 item_size);
4520 return 0;
4521}
4522
4523/*
Chris Masond352ac62008-09-29 15:18:18 -04004524 * make the item pointed to by the path smaller. new_size indicates
4525 * how small to make it, and from_end tells us if we just chop bytes
4526 * off the end of the item or if we shift the item to chop bytes off
4527 * the front.
4528 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004529void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4530 struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004531{
Chris Masonb18c6682007-04-17 13:26:50 -04004532 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004533 struct extent_buffer *leaf;
4534 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004535 u32 nritems;
4536 unsigned int data_end;
4537 unsigned int old_data_start;
4538 unsigned int old_size;
4539 unsigned int size_diff;
4540 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004541 struct btrfs_map_token token;
4542
4543 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004544
Chris Mason5f39d392007-10-15 16:14:19 -04004545 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004546 slot = path->slots[0];
4547
4548 old_size = btrfs_item_size_nr(leaf, slot);
4549 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004550 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004551
Chris Mason5f39d392007-10-15 16:14:19 -04004552 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004553 data_end = leaf_data_end(fs_info, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004554
Chris Mason5f39d392007-10-15 16:14:19 -04004555 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004556
Chris Masonb18c6682007-04-17 13:26:50 -04004557 size_diff = old_size - new_size;
4558
4559 BUG_ON(slot < 0);
4560 BUG_ON(slot >= nritems);
4561
4562 /*
4563 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4564 */
4565 /* first correct the data pointers */
4566 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004567 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004568 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004569
Chris Masoncfed81a2012-03-03 07:40:03 -05004570 ioff = btrfs_token_item_offset(leaf, item, &token);
4571 btrfs_set_token_item_offset(leaf, item,
4572 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004573 }
Chris Masondb945352007-10-15 16:15:53 -04004574
Chris Masonb18c6682007-04-17 13:26:50 -04004575 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004576 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004577 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4578 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004579 data_end, old_data_start + new_size - data_end);
4580 } else {
4581 struct btrfs_disk_key disk_key;
4582 u64 offset;
4583
4584 btrfs_item_key(leaf, &disk_key, slot);
4585
4586 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4587 unsigned long ptr;
4588 struct btrfs_file_extent_item *fi;
4589
4590 fi = btrfs_item_ptr(leaf, slot,
4591 struct btrfs_file_extent_item);
4592 fi = (struct btrfs_file_extent_item *)(
4593 (unsigned long)fi - size_diff);
4594
4595 if (btrfs_file_extent_type(leaf, fi) ==
4596 BTRFS_FILE_EXTENT_INLINE) {
4597 ptr = btrfs_item_ptr_offset(leaf, slot);
4598 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004599 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004600 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004601 }
4602 }
4603
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004604 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4605 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004606 data_end, old_data_start - data_end);
4607
4608 offset = btrfs_disk_key_offset(&disk_key);
4609 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4610 btrfs_set_item_key(leaf, &disk_key, slot);
4611 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004612 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004613 }
Chris Mason5f39d392007-10-15 16:14:19 -04004614
Ross Kirkdd3cc162013-09-16 15:58:09 +01004615 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004616 btrfs_set_item_size(leaf, item, new_size);
4617 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004618
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004619 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004620 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004621 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004622 }
Chris Masonb18c6682007-04-17 13:26:50 -04004623}
4624
Chris Masond352ac62008-09-29 15:18:18 -04004625/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004626 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004627 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004628void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004629 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004630{
Chris Mason6567e832007-04-16 09:22:45 -04004631 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004632 struct extent_buffer *leaf;
4633 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004634 u32 nritems;
4635 unsigned int data_end;
4636 unsigned int old_data;
4637 unsigned int old_size;
4638 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004639 struct btrfs_map_token token;
4640
4641 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004642
Chris Mason5f39d392007-10-15 16:14:19 -04004643 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004644
Chris Mason5f39d392007-10-15 16:14:19 -04004645 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004646 data_end = leaf_data_end(fs_info, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004647
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004648 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004649 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004650 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004651 }
Chris Mason6567e832007-04-16 09:22:45 -04004652 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004653 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004654
4655 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004656 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004657 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004658 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4659 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004660 BUG_ON(1);
4661 }
Chris Mason6567e832007-04-16 09:22:45 -04004662
4663 /*
4664 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4665 */
4666 /* first correct the data pointers */
4667 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004668 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004669 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004670
Chris Masoncfed81a2012-03-03 07:40:03 -05004671 ioff = btrfs_token_item_offset(leaf, item, &token);
4672 btrfs_set_token_item_offset(leaf, item,
4673 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004674 }
Chris Mason5f39d392007-10-15 16:14:19 -04004675
Chris Mason6567e832007-04-16 09:22:45 -04004676 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004677 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4678 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004679 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004680
Chris Mason6567e832007-04-16 09:22:45 -04004681 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004682 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004683 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004684 btrfs_set_item_size(leaf, item, old_size + data_size);
4685 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004686
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004687 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004688 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004689 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004690 }
Chris Mason6567e832007-04-16 09:22:45 -04004691}
4692
Chris Mason74123bd2007-02-02 11:05:29 -05004693/*
Chris Mason44871b12009-03-13 10:04:31 -04004694 * this is a helper for btrfs_insert_empty_items, the main goal here is
4695 * to save stack depth by doing the bulk of the work in a function
4696 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004697 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004698void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004699 const struct btrfs_key *cpu_key, u32 *data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004700 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004701{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004702 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004703 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004704 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004705 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004706 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004707 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004708 struct extent_buffer *leaf;
4709 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004710 struct btrfs_map_token token;
4711
Filipe Manana24cdc842014-07-28 19:34:35 +01004712 if (path->slots[0] == 0) {
4713 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004714 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004715 }
4716 btrfs_unlock_up_safe(path, 1);
4717
Chris Masoncfed81a2012-03-03 07:40:03 -05004718 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004719
Chris Mason5f39d392007-10-15 16:14:19 -04004720 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004721 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004722
Chris Mason5f39d392007-10-15 16:14:19 -04004723 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004724 data_end = leaf_data_end(fs_info, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004725
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004726 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004727 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004728 btrfs_crit(fs_info, "not enough freespace need %u have %d",
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004729 total_size, btrfs_leaf_free_space(fs_info, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004730 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004731 }
Chris Mason5f39d392007-10-15 16:14:19 -04004732
Chris Masonbe0e5c02007-01-26 15:51:26 -05004733 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004734 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004735
Chris Mason5f39d392007-10-15 16:14:19 -04004736 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004737 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004738 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004739 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004740 BUG_ON(1);
4741 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004742 /*
4743 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4744 */
4745 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004746 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004747 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004748
Jeff Mahoney62e85572016-09-20 10:05:01 -04004749 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004750 ioff = btrfs_token_item_offset(leaf, item, &token);
4751 btrfs_set_token_item_offset(leaf, item,
4752 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004753 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004754 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004755 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004756 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004757 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004758
4759 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004760 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4761 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004762 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004763 data_end = old_data;
4764 }
Chris Mason5f39d392007-10-15 16:14:19 -04004765
Chris Mason62e27492007-03-15 12:56:47 -04004766 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004767 for (i = 0; i < nr; i++) {
4768 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4769 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004770 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004771 btrfs_set_token_item_offset(leaf, item,
4772 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004773 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004774 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004775 }
Chris Mason44871b12009-03-13 10:04:31 -04004776
Chris Mason9c583092008-01-29 15:15:18 -05004777 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004778 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004779
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004780 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004781 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004783 }
Chris Mason44871b12009-03-13 10:04:31 -04004784}
4785
4786/*
4787 * Given a key and some data, insert items into the tree.
4788 * This does all the path init required, making room in the tree if needed.
4789 */
4790int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4791 struct btrfs_root *root,
4792 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004793 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004794 int nr)
4795{
Chris Mason44871b12009-03-13 10:04:31 -04004796 int ret = 0;
4797 int slot;
4798 int i;
4799 u32 total_size = 0;
4800 u32 total_data = 0;
4801
4802 for (i = 0; i < nr; i++)
4803 total_data += data_size[i];
4804
4805 total_size = total_data + (nr * sizeof(struct btrfs_item));
4806 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4807 if (ret == 0)
4808 return -EEXIST;
4809 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004810 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004811
Chris Mason44871b12009-03-13 10:04:31 -04004812 slot = path->slots[0];
4813 BUG_ON(slot < 0);
4814
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004815 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004816 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004817 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004818}
4819
4820/*
4821 * Given a key and some data, insert an item into the tree.
4822 * This does all the path init required, making room in the tree if needed.
4823 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004824int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4825 const struct btrfs_key *cpu_key, void *data,
4826 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004827{
4828 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004829 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004830 struct extent_buffer *leaf;
4831 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004832
Chris Mason2c90e5d2007-04-02 10:50:19 -04004833 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004834 if (!path)
4835 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004836 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004837 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004838 leaf = path->nodes[0];
4839 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4840 write_extent_buffer(leaf, data, ptr, data_size);
4841 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004842 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004843 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004844 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004845}
4846
Chris Mason74123bd2007-02-02 11:05:29 -05004847/*
Chris Mason5de08d72007-02-24 06:24:44 -05004848 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004849 *
Chris Masond352ac62008-09-29 15:18:18 -04004850 * the tree should have been previously balanced so the deletion does not
4851 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004852 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004853static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4854 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004855{
Chris Mason5f39d392007-10-15 16:14:19 -04004856 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004857 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004858 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004859
Chris Mason5f39d392007-10-15 16:14:19 -04004860 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004861 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004862 if (level) {
4863 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004864 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004865 BUG_ON(ret < 0);
4866 }
Chris Mason5f39d392007-10-15 16:14:19 -04004867 memmove_extent_buffer(parent,
4868 btrfs_node_key_ptr_offset(slot),
4869 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004870 sizeof(struct btrfs_key_ptr) *
4871 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004872 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004873 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4874 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004875 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004876 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004877
Chris Mason7518a232007-03-12 12:01:18 -04004878 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004879 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004880 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004881 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004882 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004883 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004884 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004885 struct btrfs_disk_key disk_key;
4886
4887 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004888 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004889 }
Chris Masond6025572007-03-30 14:27:56 -04004890 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004891}
4892
Chris Mason74123bd2007-02-02 11:05:29 -05004893/*
Chris Mason323ac952008-10-01 19:05:46 -04004894 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004895 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004896 *
4897 * This deletes the pointer in path->nodes[1] and frees the leaf
4898 * block extent. zero is returned if it all worked out, < 0 otherwise.
4899 *
4900 * The path must have already been setup for deleting the leaf, including
4901 * all the proper balancing. path->nodes[1] must be locked.
4902 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004903static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4904 struct btrfs_root *root,
4905 struct btrfs_path *path,
4906 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004907{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004908 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004909 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004910
Chris Mason4d081c42009-02-04 09:31:28 -05004911 /*
4912 * btrfs_free_extent is expensive, we want to make sure we
4913 * aren't holding any locks when we call it
4914 */
4915 btrfs_unlock_up_safe(path, 0);
4916
Yan, Zhengf0486c62010-05-16 10:46:25 -04004917 root_sub_used(root, leaf->len);
4918
Josef Bacik3083ee22012-03-09 16:01:49 -05004919 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004920 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004921 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004922}
4923/*
Chris Mason74123bd2007-02-02 11:05:29 -05004924 * delete the item at the leaf level in path. If that empties
4925 * the leaf, remove it from the tree
4926 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004927int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4928 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004929{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004930 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004931 struct extent_buffer *leaf;
4932 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004933 u32 last_off;
4934 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004935 int ret = 0;
4936 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004937 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004938 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004939 struct btrfs_map_token token;
4940
4941 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004942
Chris Mason5f39d392007-10-15 16:14:19 -04004943 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004944 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4945
4946 for (i = 0; i < nr; i++)
4947 dsize += btrfs_item_size_nr(leaf, slot + i);
4948
Chris Mason5f39d392007-10-15 16:14:19 -04004949 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004950
Chris Mason85e21ba2008-01-29 15:11:36 -05004951 if (slot + nr != nritems) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004952 int data_end = leaf_data_end(fs_info, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004953
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004954 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004955 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004956 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004957 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004958
Chris Mason85e21ba2008-01-29 15:11:36 -05004959 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004960 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004961
Ross Kirkdd3cc162013-09-16 15:58:09 +01004962 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004963 ioff = btrfs_token_item_offset(leaf, item, &token);
4964 btrfs_set_token_item_offset(leaf, item,
4965 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004966 }
Chris Masondb945352007-10-15 16:15:53 -04004967
Chris Mason5f39d392007-10-15 16:14:19 -04004968 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004969 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004970 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004971 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004972 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004973 btrfs_set_header_nritems(leaf, nritems - nr);
4974 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004975
Chris Mason74123bd2007-02-02 11:05:29 -05004976 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004977 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004978 if (leaf == root->node) {
4979 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004980 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004981 btrfs_set_path_blocking(path);
David Sterba7c302b42017-02-10 18:47:57 +01004982 clean_tree_block(fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004983 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004984 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004985 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004986 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004987 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004988 struct btrfs_disk_key disk_key;
4989
4990 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004991 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004992 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004993
Chris Mason74123bd2007-02-02 11:05:29 -05004994 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004995 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004996 /* push_leaf_left fixes the path.
4997 * make sure the path still points to our leaf
4998 * for possible call to del_ptr below
4999 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005000 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005001 extent_buffer_get(leaf);
5002
Chris Masonb9473432009-03-13 11:00:37 -04005003 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005004 wret = push_leaf_left(trans, root, path, 1, 1,
5005 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005006 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005007 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005008
5009 if (path->nodes[0] == leaf &&
5010 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005011 wret = push_leaf_right(trans, root, path, 1,
5012 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005013 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005014 ret = wret;
5015 }
Chris Mason5f39d392007-10-15 16:14:19 -04005016
5017 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005018 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005019 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005020 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005021 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005022 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005023 /* if we're still in the path, make sure
5024 * we're dirty. Otherwise, one of the
5025 * push_leaf functions must have already
5026 * dirtied this buffer
5027 */
5028 if (path->nodes[0] == leaf)
5029 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005030 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005031 }
Chris Masond5719762007-03-23 10:01:08 -04005032 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005033 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005034 }
5035 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005036 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005037}
5038
Chris Mason97571fd2007-02-24 13:39:08 -05005039/*
Chris Mason925baed2008-06-25 16:01:30 -04005040 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005041 * returns 0 if it found something or 1 if there are no lesser leaves.
5042 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005043 *
5044 * This may release the path, and so you may lose any locks held at the
5045 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005046 */
Josef Bacik16e75492013-10-22 12:18:51 -04005047int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005048{
Chris Mason925baed2008-06-25 16:01:30 -04005049 struct btrfs_key key;
5050 struct btrfs_disk_key found_key;
5051 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005052
Chris Mason925baed2008-06-25 16:01:30 -04005053 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005054
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005055 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005056 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005057 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005058 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005059 key.offset = (u64)-1;
5060 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005061 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005062 key.type = (u8)-1;
5063 key.offset = (u64)-1;
5064 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005065 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005066 }
Chris Mason7bb86312007-12-11 09:25:06 -05005067
David Sterbab3b4aa72011-04-21 01:20:15 +02005068 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005069 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5070 if (ret < 0)
5071 return ret;
5072 btrfs_item_key(path->nodes[0], &found_key, 0);
5073 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005074 /*
5075 * We might have had an item with the previous key in the tree right
5076 * before we released our path. And after we released our path, that
5077 * item might have been pushed to the first slot (0) of the leaf we
5078 * were holding due to a tree balance. Alternatively, an item with the
5079 * previous key can exist as the only element of a leaf (big fat item).
5080 * Therefore account for these 2 cases, so that our callers (like
5081 * btrfs_previous_item) don't miss an existing item with a key matching
5082 * the previous key we computed above.
5083 */
5084 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005085 return 0;
5086 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005087}
5088
Chris Mason3f157a22008-06-25 16:01:31 -04005089/*
5090 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005091 * for nodes or leaves that are have a minimum transaction id.
5092 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005093 *
5094 * This does not cow, but it does stuff the starting key it finds back
5095 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5096 * key and get a writable path.
5097 *
Chris Mason3f157a22008-06-25 16:01:31 -04005098 * This honors path->lowest_level to prevent descent past a given level
5099 * of the tree.
5100 *
Chris Masond352ac62008-09-29 15:18:18 -04005101 * min_trans indicates the oldest transaction that you are interested
5102 * in walking through. Any nodes or leaves older than min_trans are
5103 * skipped over (without reading them).
5104 *
Chris Mason3f157a22008-06-25 16:01:31 -04005105 * returns zero if something useful was found, < 0 on error and 1 if there
5106 * was nothing in the tree that matched the search criteria.
5107 */
5108int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005109 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005110 u64 min_trans)
5111{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005112 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason3f157a22008-06-25 16:01:31 -04005113 struct extent_buffer *cur;
5114 struct btrfs_key found_key;
5115 int slot;
Yan96524802008-07-24 12:19:49 -04005116 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005117 u32 nritems;
5118 int level;
5119 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005120 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005121
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005122 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005123again:
Chris Masonbd681512011-07-16 15:23:14 -04005124 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005125 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005126 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005127 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005128 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005129
5130 if (btrfs_header_generation(cur) < min_trans) {
5131 ret = 1;
5132 goto out;
5133 }
Chris Masond3977122009-01-05 21:25:51 -05005134 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005135 nritems = btrfs_header_nritems(cur);
5136 level = btrfs_header_level(cur);
Nikolay Borisova74b35e2017-12-08 16:27:43 +02005137 sret = btrfs_bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005138
Chris Mason323ac952008-10-01 19:05:46 -04005139 /* at the lowest level, we're done, setup the path and exit */
5140 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005141 if (slot >= nritems)
5142 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005143 ret = 0;
5144 path->slots[level] = slot;
5145 btrfs_item_key_to_cpu(cur, &found_key, slot);
5146 goto out;
5147 }
Yan96524802008-07-24 12:19:49 -04005148 if (sret && slot > 0)
5149 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005150 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005151 * check this node pointer against the min_trans parameters.
5152 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005153 */
Chris Masond3977122009-01-05 21:25:51 -05005154 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005155 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005156
Chris Mason3f157a22008-06-25 16:01:31 -04005157 gen = btrfs_node_ptr_generation(cur, slot);
5158 if (gen < min_trans) {
5159 slot++;
5160 continue;
5161 }
Eric Sandeende78b512013-01-31 18:21:12 +00005162 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005163 }
Chris Masone02119d2008-09-05 16:13:11 -04005164find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005165 /*
5166 * we didn't find a candidate key in this node, walk forward
5167 * and find another one
5168 */
5169 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005170 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005171 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005172 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005173 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005174 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005175 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005176 goto again;
5177 } else {
5178 goto out;
5179 }
5180 }
5181 /* save our key for returning back */
5182 btrfs_node_key_to_cpu(cur, &found_key, slot);
5183 path->slots[level] = slot;
5184 if (level == path->lowest_level) {
5185 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005186 goto out;
5187 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005188 btrfs_set_path_blocking(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005189 cur = read_node_slot(fs_info, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005190 if (IS_ERR(cur)) {
5191 ret = PTR_ERR(cur);
5192 goto out;
5193 }
Chris Mason3f157a22008-06-25 16:01:31 -04005194
Chris Masonbd681512011-07-16 15:23:14 -04005195 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005196
Chris Masonbd681512011-07-16 15:23:14 -04005197 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005198 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005199 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005200 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005201 }
5202out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005203 path->keep_locks = keep_locks;
5204 if (ret == 0) {
5205 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5206 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005207 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005208 }
Chris Mason3f157a22008-06-25 16:01:31 -04005209 return ret;
5210}
5211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005212static int tree_move_down(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005213 struct btrfs_path *path,
David Sterbaab6a43e2017-02-10 19:24:53 +01005214 int *level)
Alexander Block70698302012-06-05 21:07:48 +02005215{
Liu Bofb770ae2016-07-05 12:10:14 -07005216 struct extent_buffer *eb;
5217
Chris Mason74dd17f2012-08-07 16:25:13 -04005218 BUG_ON(*level == 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005219 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
Liu Bofb770ae2016-07-05 12:10:14 -07005220 if (IS_ERR(eb))
5221 return PTR_ERR(eb);
5222
5223 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005224 path->slots[*level - 1] = 0;
5225 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005226 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005227}
5228
David Sterbaf1e30262017-02-10 19:25:51 +01005229static int tree_move_next_or_upnext(struct btrfs_path *path,
Alexander Block70698302012-06-05 21:07:48 +02005230 int *level, int root_level)
5231{
5232 int ret = 0;
5233 int nritems;
5234 nritems = btrfs_header_nritems(path->nodes[*level]);
5235
5236 path->slots[*level]++;
5237
Chris Mason74dd17f2012-08-07 16:25:13 -04005238 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005239 if (*level == root_level)
5240 return -1;
5241
5242 /* move upnext */
5243 path->slots[*level] = 0;
5244 free_extent_buffer(path->nodes[*level]);
5245 path->nodes[*level] = NULL;
5246 (*level)++;
5247 path->slots[*level]++;
5248
5249 nritems = btrfs_header_nritems(path->nodes[*level]);
5250 ret = 1;
5251 }
5252 return ret;
5253}
5254
5255/*
5256 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5257 * or down.
5258 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005259static int tree_advance(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005260 struct btrfs_path *path,
5261 int *level, int root_level,
5262 int allow_down,
5263 struct btrfs_key *key)
5264{
5265 int ret;
5266
5267 if (*level == 0 || !allow_down) {
David Sterbaf1e30262017-02-10 19:25:51 +01005268 ret = tree_move_next_or_upnext(path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005269 } else {
David Sterbaab6a43e2017-02-10 19:24:53 +01005270 ret = tree_move_down(fs_info, path, level);
Alexander Block70698302012-06-05 21:07:48 +02005271 }
5272 if (ret >= 0) {
5273 if (*level == 0)
5274 btrfs_item_key_to_cpu(path->nodes[*level], key,
5275 path->slots[*level]);
5276 else
5277 btrfs_node_key_to_cpu(path->nodes[*level], key,
5278 path->slots[*level]);
5279 }
5280 return ret;
5281}
5282
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005283static int tree_compare_item(struct btrfs_path *left_path,
Alexander Block70698302012-06-05 21:07:48 +02005284 struct btrfs_path *right_path,
5285 char *tmp_buf)
5286{
5287 int cmp;
5288 int len1, len2;
5289 unsigned long off1, off2;
5290
5291 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5292 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5293 if (len1 != len2)
5294 return 1;
5295
5296 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5297 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5298 right_path->slots[0]);
5299
5300 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5301
5302 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5303 if (cmp)
5304 return 1;
5305 return 0;
5306}
5307
5308#define ADVANCE 1
5309#define ADVANCE_ONLY_NEXT -1
5310
5311/*
5312 * This function compares two trees and calls the provided callback for
5313 * every changed/new/deleted item it finds.
5314 * If shared tree blocks are encountered, whole subtrees are skipped, making
5315 * the compare pretty fast on snapshotted subvolumes.
5316 *
5317 * This currently works on commit roots only. As commit roots are read only,
5318 * we don't do any locking. The commit roots are protected with transactions.
5319 * Transactions are ended and rejoined when a commit is tried in between.
5320 *
5321 * This function checks for modifications done to the trees while comparing.
5322 * If it detects a change, it aborts immediately.
5323 */
5324int btrfs_compare_trees(struct btrfs_root *left_root,
5325 struct btrfs_root *right_root,
5326 btrfs_changed_cb_t changed_cb, void *ctx)
5327{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005328 struct btrfs_fs_info *fs_info = left_root->fs_info;
Alexander Block70698302012-06-05 21:07:48 +02005329 int ret;
5330 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005331 struct btrfs_path *left_path = NULL;
5332 struct btrfs_path *right_path = NULL;
5333 struct btrfs_key left_key;
5334 struct btrfs_key right_key;
5335 char *tmp_buf = NULL;
5336 int left_root_level;
5337 int right_root_level;
5338 int left_level;
5339 int right_level;
5340 int left_end_reached;
5341 int right_end_reached;
5342 int advance_left;
5343 int advance_right;
5344 u64 left_blockptr;
5345 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005346 u64 left_gen;
5347 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005348
5349 left_path = btrfs_alloc_path();
5350 if (!left_path) {
5351 ret = -ENOMEM;
5352 goto out;
5353 }
5354 right_path = btrfs_alloc_path();
5355 if (!right_path) {
5356 ret = -ENOMEM;
5357 goto out;
5358 }
5359
Michal Hocko752ade62017-05-08 15:57:27 -07005360 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
Alexander Block70698302012-06-05 21:07:48 +02005361 if (!tmp_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07005362 ret = -ENOMEM;
5363 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005364 }
5365
5366 left_path->search_commit_root = 1;
5367 left_path->skip_locking = 1;
5368 right_path->search_commit_root = 1;
5369 right_path->skip_locking = 1;
5370
Alexander Block70698302012-06-05 21:07:48 +02005371 /*
5372 * Strategy: Go to the first items of both trees. Then do
5373 *
5374 * If both trees are at level 0
5375 * Compare keys of current items
5376 * If left < right treat left item as new, advance left tree
5377 * and repeat
5378 * If left > right treat right item as deleted, advance right tree
5379 * and repeat
5380 * If left == right do deep compare of items, treat as changed if
5381 * needed, advance both trees and repeat
5382 * If both trees are at the same level but not at level 0
5383 * Compare keys of current nodes/leafs
5384 * If left < right advance left tree and repeat
5385 * If left > right advance right tree and repeat
5386 * If left == right compare blockptrs of the next nodes/leafs
5387 * If they match advance both trees but stay at the same level
5388 * and repeat
5389 * If they don't match advance both trees while allowing to go
5390 * deeper and repeat
5391 * If tree levels are different
5392 * Advance the tree that needs it and repeat
5393 *
5394 * Advancing a tree means:
5395 * If we are at level 0, try to go to the next slot. If that's not
5396 * possible, go one level up and repeat. Stop when we found a level
5397 * where we could go to the next slot. We may at this point be on a
5398 * node or a leaf.
5399 *
5400 * If we are not at level 0 and not on shared tree blocks, go one
5401 * level deeper.
5402 *
5403 * If we are not at level 0 and on shared tree blocks, go one slot to
5404 * the right if possible or go up and right.
5405 */
5406
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005407 down_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005408 left_level = btrfs_header_level(left_root->commit_root);
5409 left_root_level = left_level;
Robbie Ko6f2f0b32018-05-14 10:51:34 +08005410 left_path->nodes[left_level] =
5411 btrfs_clone_extent_buffer(left_root->commit_root);
5412 if (!left_path->nodes[left_level]) {
5413 up_read(&fs_info->commit_root_sem);
5414 ret = -ENOMEM;
5415 goto out;
5416 }
Alexander Block70698302012-06-05 21:07:48 +02005417 extent_buffer_get(left_path->nodes[left_level]);
5418
5419 right_level = btrfs_header_level(right_root->commit_root);
5420 right_root_level = right_level;
Robbie Ko6f2f0b32018-05-14 10:51:34 +08005421 right_path->nodes[right_level] =
5422 btrfs_clone_extent_buffer(right_root->commit_root);
5423 if (!right_path->nodes[right_level]) {
5424 up_read(&fs_info->commit_root_sem);
5425 ret = -ENOMEM;
5426 goto out;
5427 }
Alexander Block70698302012-06-05 21:07:48 +02005428 extent_buffer_get(right_path->nodes[right_level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005429 up_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005430
5431 if (left_level == 0)
5432 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5433 &left_key, left_path->slots[left_level]);
5434 else
5435 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5436 &left_key, left_path->slots[left_level]);
5437 if (right_level == 0)
5438 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5439 &right_key, right_path->slots[right_level]);
5440 else
5441 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5442 &right_key, right_path->slots[right_level]);
5443
5444 left_end_reached = right_end_reached = 0;
5445 advance_left = advance_right = 0;
5446
5447 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005448 if (advance_left && !left_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005449 ret = tree_advance(fs_info, left_path, &left_level,
Alexander Block70698302012-06-05 21:07:48 +02005450 left_root_level,
5451 advance_left != ADVANCE_ONLY_NEXT,
5452 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005453 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005454 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005455 else if (ret < 0)
5456 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005457 advance_left = 0;
5458 }
5459 if (advance_right && !right_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005460 ret = tree_advance(fs_info, right_path, &right_level,
Alexander Block70698302012-06-05 21:07:48 +02005461 right_root_level,
5462 advance_right != ADVANCE_ONLY_NEXT,
5463 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005464 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005465 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005466 else if (ret < 0)
5467 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005468 advance_right = 0;
5469 }
5470
5471 if (left_end_reached && right_end_reached) {
5472 ret = 0;
5473 goto out;
5474 } else if (left_end_reached) {
5475 if (right_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005476 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005477 &right_key,
5478 BTRFS_COMPARE_TREE_DELETED,
5479 ctx);
5480 if (ret < 0)
5481 goto out;
5482 }
5483 advance_right = ADVANCE;
5484 continue;
5485 } else if (right_end_reached) {
5486 if (left_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005487 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005488 &left_key,
5489 BTRFS_COMPARE_TREE_NEW,
5490 ctx);
5491 if (ret < 0)
5492 goto out;
5493 }
5494 advance_left = ADVANCE;
5495 continue;
5496 }
5497
5498 if (left_level == 0 && right_level == 0) {
5499 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5500 if (cmp < 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005501 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005502 &left_key,
5503 BTRFS_COMPARE_TREE_NEW,
5504 ctx);
5505 if (ret < 0)
5506 goto out;
5507 advance_left = ADVANCE;
5508 } else if (cmp > 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005509 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005510 &right_key,
5511 BTRFS_COMPARE_TREE_DELETED,
5512 ctx);
5513 if (ret < 0)
5514 goto out;
5515 advance_right = ADVANCE;
5516 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005517 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005518
Chris Mason74dd17f2012-08-07 16:25:13 -04005519 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005520 ret = tree_compare_item(left_path, right_path,
5521 tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005522 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005523 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005524 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005525 result = BTRFS_COMPARE_TREE_SAME;
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005526 ret = changed_cb(left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005527 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005528 if (ret < 0)
5529 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005530 advance_left = ADVANCE;
5531 advance_right = ADVANCE;
5532 }
5533 } else if (left_level == right_level) {
5534 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5535 if (cmp < 0) {
5536 advance_left = ADVANCE;
5537 } else if (cmp > 0) {
5538 advance_right = ADVANCE;
5539 } else {
5540 left_blockptr = btrfs_node_blockptr(
5541 left_path->nodes[left_level],
5542 left_path->slots[left_level]);
5543 right_blockptr = btrfs_node_blockptr(
5544 right_path->nodes[right_level],
5545 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005546 left_gen = btrfs_node_ptr_generation(
5547 left_path->nodes[left_level],
5548 left_path->slots[left_level]);
5549 right_gen = btrfs_node_ptr_generation(
5550 right_path->nodes[right_level],
5551 right_path->slots[right_level]);
5552 if (left_blockptr == right_blockptr &&
5553 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005554 /*
5555 * As we're on a shared block, don't
5556 * allow to go deeper.
5557 */
5558 advance_left = ADVANCE_ONLY_NEXT;
5559 advance_right = ADVANCE_ONLY_NEXT;
5560 } else {
5561 advance_left = ADVANCE;
5562 advance_right = ADVANCE;
5563 }
5564 }
5565 } else if (left_level < right_level) {
5566 advance_right = ADVANCE;
5567 } else {
5568 advance_left = ADVANCE;
5569 }
5570 }
5571
5572out:
5573 btrfs_free_path(left_path);
5574 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005575 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005576 return ret;
5577}
5578
Chris Mason3f157a22008-06-25 16:01:31 -04005579/*
5580 * this is similar to btrfs_next_leaf, but does not try to preserve
5581 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005582 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005583 *
5584 * 0 is returned if another key is found, < 0 if there are any errors
5585 * and 1 is returned if there are no higher keys in the tree
5586 *
5587 * path->keep_locks should be set to 1 on the search made before
5588 * calling this function.
5589 */
Chris Masone7a84562008-06-25 16:01:31 -04005590int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005591 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005592{
Chris Masone7a84562008-06-25 16:01:31 -04005593 int slot;
5594 struct extent_buffer *c;
5595
Chris Mason934d3752008-12-08 16:43:10 -05005596 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005597 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005598 if (!path->nodes[level])
5599 return 1;
5600
5601 slot = path->slots[level] + 1;
5602 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005603next:
Chris Masone7a84562008-06-25 16:01:31 -04005604 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005605 int ret;
5606 int orig_lowest;
5607 struct btrfs_key cur_key;
5608 if (level + 1 >= BTRFS_MAX_LEVEL ||
5609 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005610 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005611
5612 if (path->locks[level + 1]) {
5613 level++;
5614 continue;
5615 }
5616
5617 slot = btrfs_header_nritems(c) - 1;
5618 if (level == 0)
5619 btrfs_item_key_to_cpu(c, &cur_key, slot);
5620 else
5621 btrfs_node_key_to_cpu(c, &cur_key, slot);
5622
5623 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005624 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005625 path->lowest_level = level;
5626 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5627 0, 0);
5628 path->lowest_level = orig_lowest;
5629 if (ret < 0)
5630 return ret;
5631
5632 c = path->nodes[level];
5633 slot = path->slots[level];
5634 if (ret == 0)
5635 slot++;
5636 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005637 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005638
Chris Masone7a84562008-06-25 16:01:31 -04005639 if (level == 0)
5640 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005641 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005642 u64 gen = btrfs_node_ptr_generation(c, slot);
5643
Chris Mason3f157a22008-06-25 16:01:31 -04005644 if (gen < min_trans) {
5645 slot++;
5646 goto next;
5647 }
Chris Masone7a84562008-06-25 16:01:31 -04005648 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005649 }
Chris Masone7a84562008-06-25 16:01:31 -04005650 return 0;
5651 }
5652 return 1;
5653}
5654
Chris Mason7bb86312007-12-11 09:25:06 -05005655/*
Chris Mason925baed2008-06-25 16:01:30 -04005656 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005657 * returns 0 if it found something or 1 if there are no greater leaves.
5658 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005659 */
Chris Mason234b63a2007-03-13 10:46:10 -04005660int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005661{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005662 return btrfs_next_old_leaf(root, path, 0);
5663}
5664
5665int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5666 u64 time_seq)
5667{
Chris Masond97e63b2007-02-20 16:40:44 -05005668 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005669 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005670 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005671 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005672 struct btrfs_key key;
5673 u32 nritems;
5674 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005675 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005676 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005677
5678 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005679 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005680 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005681
Chris Mason8e73f272009-04-03 10:14:18 -04005682 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5683again:
5684 level = 1;
5685 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005686 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005687 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005688
Chris Masona2135012008-06-25 16:01:30 -04005689 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005690 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005691
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005692 if (time_seq)
5693 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5694 else
5695 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005696 path->keep_locks = 0;
5697
5698 if (ret < 0)
5699 return ret;
5700
Chris Masona2135012008-06-25 16:01:30 -04005701 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005702 /*
5703 * by releasing the path above we dropped all our locks. A balance
5704 * could have added more items next to the key that used to be
5705 * at the very end of the block. So, check again here and
5706 * advance the path if there are now more items available.
5707 */
Chris Masona2135012008-06-25 16:01:30 -04005708 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005709 if (ret == 0)
5710 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005711 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005712 goto done;
5713 }
Liu Bo0b43e042014-06-09 11:04:49 +08005714 /*
5715 * So the above check misses one case:
5716 * - after releasing the path above, someone has removed the item that
5717 * used to be at the very end of the block, and balance between leafs
5718 * gets another one with bigger key.offset to replace it.
5719 *
5720 * This one should be returned as well, or we can get leaf corruption
5721 * later(esp. in __btrfs_drop_extents()).
5722 *
5723 * And a bit more explanation about this check,
5724 * with ret > 0, the key isn't found, the path points to the slot
5725 * where it should be inserted, so the path->slots[0] item must be the
5726 * bigger one.
5727 */
5728 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5729 ret = 0;
5730 goto done;
5731 }
Chris Masond97e63b2007-02-20 16:40:44 -05005732
Chris Masond3977122009-01-05 21:25:51 -05005733 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005734 if (!path->nodes[level]) {
5735 ret = 1;
5736 goto done;
5737 }
Chris Mason5f39d392007-10-15 16:14:19 -04005738
Chris Masond97e63b2007-02-20 16:40:44 -05005739 slot = path->slots[level] + 1;
5740 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005741 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005742 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005743 if (level == BTRFS_MAX_LEVEL) {
5744 ret = 1;
5745 goto done;
5746 }
Chris Masond97e63b2007-02-20 16:40:44 -05005747 continue;
5748 }
Chris Mason5f39d392007-10-15 16:14:19 -04005749
Chris Mason925baed2008-06-25 16:01:30 -04005750 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005751 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005752 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005753 }
Chris Mason5f39d392007-10-15 16:14:19 -04005754
Chris Mason8e73f272009-04-03 10:14:18 -04005755 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005756 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005757 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005758 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005759 if (ret == -EAGAIN)
5760 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005761
Chris Mason76a05b32009-05-14 13:24:30 -04005762 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005763 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005764 goto done;
5765 }
5766
Chris Mason5cd57b22008-06-25 16:01:30 -04005767 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005768 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005769 if (!ret && time_seq) {
5770 /*
5771 * If we don't get the lock, we may be racing
5772 * with push_leaf_left, holding that lock while
5773 * itself waiting for the leaf we've currently
5774 * locked. To solve this situation, we give up
5775 * on our lock and cycle.
5776 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005777 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005778 btrfs_release_path(path);
5779 cond_resched();
5780 goto again;
5781 }
Chris Mason8e73f272009-04-03 10:14:18 -04005782 if (!ret) {
5783 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005784 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005785 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005786 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005787 }
Chris Mason31533fb2011-07-26 16:01:59 -04005788 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005789 }
Chris Masond97e63b2007-02-20 16:40:44 -05005790 break;
5791 }
5792 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005793 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005794 level--;
5795 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005796 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005797 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005798
Chris Mason5f39d392007-10-15 16:14:19 -04005799 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005800 path->nodes[level] = next;
5801 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005802 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005803 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005804 if (!level)
5805 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005806
Liu Bod07b8522017-01-30 12:23:42 -08005807 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005808 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005809 if (ret == -EAGAIN)
5810 goto again;
5811
Chris Mason76a05b32009-05-14 13:24:30 -04005812 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005813 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005814 goto done;
5815 }
5816
Chris Mason5cd57b22008-06-25 16:01:30 -04005817 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005818 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005819 if (!ret) {
5820 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005821 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005822 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005823 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005824 }
Chris Mason31533fb2011-07-26 16:01:59 -04005825 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005826 }
Chris Masond97e63b2007-02-20 16:40:44 -05005827 }
Chris Mason8e73f272009-04-03 10:14:18 -04005828 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005829done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005830 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005831 path->leave_spinning = old_spinning;
5832 if (!old_spinning)
5833 btrfs_set_path_blocking(path);
5834
5835 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005836}
Chris Mason0b86a832008-03-24 15:01:56 -04005837
Chris Mason3f157a22008-06-25 16:01:31 -04005838/*
5839 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5840 * searching until it gets past min_objectid or finds an item of 'type'
5841 *
5842 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5843 */
Chris Mason0b86a832008-03-24 15:01:56 -04005844int btrfs_previous_item(struct btrfs_root *root,
5845 struct btrfs_path *path, u64 min_objectid,
5846 int type)
5847{
5848 struct btrfs_key found_key;
5849 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005850 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005851 int ret;
5852
Chris Masond3977122009-01-05 21:25:51 -05005853 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005854 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005855 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005856 ret = btrfs_prev_leaf(root, path);
5857 if (ret != 0)
5858 return ret;
5859 } else {
5860 path->slots[0]--;
5861 }
5862 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005863 nritems = btrfs_header_nritems(leaf);
5864 if (nritems == 0)
5865 return 1;
5866 if (path->slots[0] == nritems)
5867 path->slots[0]--;
5868
Chris Mason0b86a832008-03-24 15:01:56 -04005869 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005870 if (found_key.objectid < min_objectid)
5871 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005872 if (found_key.type == type)
5873 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005874 if (found_key.objectid == min_objectid &&
5875 found_key.type < type)
5876 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005877 }
5878 return 1;
5879}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005880
5881/*
5882 * search in extent tree to find a previous Metadata/Data extent item with
5883 * min objecitd.
5884 *
5885 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5886 */
5887int btrfs_previous_extent_item(struct btrfs_root *root,
5888 struct btrfs_path *path, u64 min_objectid)
5889{
5890 struct btrfs_key found_key;
5891 struct extent_buffer *leaf;
5892 u32 nritems;
5893 int ret;
5894
5895 while (1) {
5896 if (path->slots[0] == 0) {
5897 btrfs_set_path_blocking(path);
5898 ret = btrfs_prev_leaf(root, path);
5899 if (ret != 0)
5900 return ret;
5901 } else {
5902 path->slots[0]--;
5903 }
5904 leaf = path->nodes[0];
5905 nritems = btrfs_header_nritems(leaf);
5906 if (nritems == 0)
5907 return 1;
5908 if (path->slots[0] == nritems)
5909 path->slots[0]--;
5910
5911 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5912 if (found_key.objectid < min_objectid)
5913 break;
5914 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5915 found_key.type == BTRFS_METADATA_ITEM_KEY)
5916 return 0;
5917 if (found_key.objectid == min_objectid &&
5918 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5919 break;
5920 }
5921 return 1;
5922}