blob: 1f71695cb0a80288e4a3256bcd9167dc03efb15a [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
1818 if (level == 0)
1819 return 0;
1820
Chris Mason5f39d392007-10-15 16:14:19 -04001821 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001822
Chris Masonbd681512011-07-16 15:23:14 -04001823 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1824 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001825 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1826
Chris Mason1d4f8a02007-03-13 09:28:32 -04001827 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001828
Li Zefana05a9bb2011-09-06 16:55:34 +08001829 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001830 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001831 pslot = path->slots[level + 1];
1832 }
Chris Masonbb803952007-03-01 12:04:21 -05001833
Chris Mason40689472007-03-17 14:29:23 -04001834 /*
1835 * deal with the case where there is only one pointer in the root
1836 * by promoting the node below to a root
1837 */
Chris Mason5f39d392007-10-15 16:14:19 -04001838 if (!parent) {
1839 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001840
Chris Mason5f39d392007-10-15 16:14:19 -04001841 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001842 return 0;
1843
1844 /* promote the child to a root */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001845 child = read_node_slot(fs_info, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001846 if (IS_ERR(child)) {
1847 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001848 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001849 goto enospc;
1850 }
1851
Chris Mason925baed2008-06-25 16:01:30 -04001852 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001853 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001854 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001855 if (ret) {
1856 btrfs_tree_unlock(child);
1857 free_extent_buffer(child);
1858 goto enospc;
1859 }
Yan2f375ab2008-02-01 14:58:07 -05001860
David Sterbad9d19a02018-03-05 16:35:29 +01001861 ret = tree_mod_log_insert_root(root->node, child, 1);
1862 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04001863 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001864
Chris Mason0b86a832008-03-24 15:01:56 -04001865 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001866 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001867
Chris Mason925baed2008-06-25 16:01:30 -04001868 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001869 path->nodes[level] = NULL;
David Sterba7c302b42017-02-10 18:47:57 +01001870 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001871 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001872 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001873 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001874
1875 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001876 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001877 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001878 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001879 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001880 }
Chris Mason5f39d392007-10-15 16:14:19 -04001881 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001882 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001883 return 0;
1884
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001885 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001886 if (IS_ERR(left))
1887 left = NULL;
1888
Chris Mason5f39d392007-10-15 16:14:19 -04001889 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001890 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001891 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001892 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001893 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001894 if (wret) {
1895 ret = wret;
1896 goto enospc;
1897 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001898 }
Liu Bofb770ae2016-07-05 12:10:14 -07001899
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001900 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001901 if (IS_ERR(right))
1902 right = NULL;
1903
Chris Mason5f39d392007-10-15 16:14:19 -04001904 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001905 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001906 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001907 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001908 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001909 if (wret) {
1910 ret = wret;
1911 goto enospc;
1912 }
1913 }
1914
1915 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001916 if (left) {
1917 orig_slot += btrfs_header_nritems(left);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001918 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001919 if (wret < 0)
1920 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001921 }
Chris Mason79f95c82007-03-01 15:16:26 -05001922
1923 /*
1924 * then try to empty the right most buffer into the middle
1925 */
Chris Mason5f39d392007-10-15 16:14:19 -04001926 if (right) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001927 wret = push_node_left(trans, fs_info, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001928 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001929 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001930 if (btrfs_header_nritems(right) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001931 clean_tree_block(fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04001932 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001933 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001934 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001935 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001936 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001937 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001938 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001939 struct btrfs_disk_key right_key;
1940 btrfs_node_key(right, &right_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001941 ret = tree_mod_log_insert_key(parent, pslot + 1,
1942 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1943 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001944 btrfs_set_node_key(parent, &right_key, pslot + 1);
1945 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001946 }
1947 }
Chris Mason5f39d392007-10-15 16:14:19 -04001948 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001949 /*
1950 * we're not allowed to leave a node with one item in the
1951 * tree during a delete. A deletion from lower in the tree
1952 * could try to delete the only pointer in this node.
1953 * So, pull some keys from the left.
1954 * There has to be a left pointer at this point because
1955 * otherwise we would have pulled some pointers from the
1956 * right
1957 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001958 if (!left) {
1959 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001960 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001961 goto enospc;
1962 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001963 wret = balance_node_right(trans, fs_info, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001964 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001965 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001966 goto enospc;
1967 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001968 if (wret == 1) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001969 wret = push_node_left(trans, fs_info, left, mid, 1);
Chris Masonbce4eae2008-04-24 14:42:46 -04001970 if (wret < 0)
1971 ret = wret;
1972 }
Chris Mason79f95c82007-03-01 15:16:26 -05001973 BUG_ON(wret == 1);
1974 }
Chris Mason5f39d392007-10-15 16:14:19 -04001975 if (btrfs_header_nritems(mid) == 0) {
David Sterba7c302b42017-02-10 18:47:57 +01001976 clean_tree_block(fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001977 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001978 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001979 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001980 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001981 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001982 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001983 } else {
1984 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001985 struct btrfs_disk_key mid_key;
1986 btrfs_node_key(mid, &mid_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01001987 ret = tree_mod_log_insert_key(parent, pslot,
1988 MOD_LOG_KEY_REPLACE, GFP_NOFS);
1989 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001990 btrfs_set_node_key(parent, &mid_key, pslot);
1991 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001992 }
Chris Masonbb803952007-03-01 12:04:21 -05001993
Chris Mason79f95c82007-03-01 15:16:26 -05001994 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001995 if (left) {
1996 if (btrfs_header_nritems(left) > orig_slot) {
1997 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001998 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001999 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002000 path->slots[level + 1] -= 1;
2001 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002002 if (mid) {
2003 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002004 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002005 }
Chris Masonbb803952007-03-01 12:04:21 -05002006 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002007 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002008 path->slots[level] = orig_slot;
2009 }
2010 }
Chris Mason79f95c82007-03-01 15:16:26 -05002011 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002012 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002013 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002014 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002015enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002016 if (right) {
2017 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002018 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002019 }
2020 if (left) {
2021 if (path->nodes[level] != left)
2022 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002023 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002024 }
Chris Masonbb803952007-03-01 12:04:21 -05002025 return ret;
2026}
2027
Chris Masond352ac62008-09-29 15:18:18 -04002028/* Node balancing for insertion. Here we only split or push nodes around
2029 * when they are completely full. This is also done top down, so we
2030 * have to be pessimistic.
2031 */
Chris Masond3977122009-01-05 21:25:51 -05002032static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002033 struct btrfs_root *root,
2034 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002035{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002036 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002037 struct extent_buffer *right = NULL;
2038 struct extent_buffer *mid;
2039 struct extent_buffer *left = NULL;
2040 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002041 int ret = 0;
2042 int wret;
2043 int pslot;
2044 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002045
2046 if (level == 0)
2047 return 1;
2048
Chris Mason5f39d392007-10-15 16:14:19 -04002049 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002050 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002051
Li Zefana05a9bb2011-09-06 16:55:34 +08002052 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002053 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002054 pslot = path->slots[level + 1];
2055 }
Chris Masone66f7092007-04-20 13:16:02 -04002056
Chris Mason5f39d392007-10-15 16:14:19 -04002057 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002058 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002059
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002060 left = read_node_slot(fs_info, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002061 if (IS_ERR(left))
2062 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002063
2064 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002065 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002066 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002067
2068 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002069 btrfs_set_lock_blocking(left);
2070
Chris Mason5f39d392007-10-15 16:14:19 -04002071 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002072 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002073 wret = 1;
2074 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002075 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002076 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002077 if (ret)
2078 wret = 1;
2079 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002080 wret = push_node_left(trans, fs_info,
Chris Mason971a1f62008-04-24 10:54:32 -04002081 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002082 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002083 }
Chris Masone66f7092007-04-20 13:16:02 -04002084 if (wret < 0)
2085 ret = wret;
2086 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002087 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002088 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_node_key(mid, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002090 ret = tree_mod_log_insert_key(parent, pslot,
2091 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2092 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002093 btrfs_set_node_key(parent, &disk_key, pslot);
2094 btrfs_mark_buffer_dirty(parent);
2095 if (btrfs_header_nritems(left) > orig_slot) {
2096 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002097 path->slots[level + 1] -= 1;
2098 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002099 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002100 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002101 } else {
2102 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002103 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002104 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002105 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002107 }
Chris Masone66f7092007-04-20 13:16:02 -04002108 return 0;
2109 }
Chris Mason925baed2008-06-25 16:01:30 -04002110 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002111 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002112 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002113 right = read_node_slot(fs_info, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002114 if (IS_ERR(right))
2115 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002116
2117 /*
2118 * then try to empty the right most buffer into the middle
2119 */
Chris Mason5f39d392007-10-15 16:14:19 -04002120 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002121 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002122
Chris Mason925baed2008-06-25 16:01:30 -04002123 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002124 btrfs_set_lock_blocking(right);
2125
Chris Mason5f39d392007-10-15 16:14:19 -04002126 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002127 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002128 wret = 1;
2129 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002130 ret = btrfs_cow_block(trans, root, right,
2131 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002132 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002133 if (ret)
2134 wret = 1;
2135 else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002136 wret = balance_node_right(trans, fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04002137 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002138 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002139 }
Chris Masone66f7092007-04-20 13:16:02 -04002140 if (wret < 0)
2141 ret = wret;
2142 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002143 struct btrfs_disk_key disk_key;
2144
2145 btrfs_node_key(right, &disk_key, 0);
David Sterba0e82bcf2018-03-05 16:16:54 +01002146 ret = tree_mod_log_insert_key(parent, pslot + 1,
2147 MOD_LOG_KEY_REPLACE, GFP_NOFS);
2148 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002149 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2150 btrfs_mark_buffer_dirty(parent);
2151
2152 if (btrfs_header_nritems(mid) <= orig_slot) {
2153 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002154 path->slots[level + 1] += 1;
2155 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002156 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002157 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002158 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002159 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002160 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002162 }
Chris Masone66f7092007-04-20 13:16:02 -04002163 return 0;
2164 }
Chris Mason925baed2008-06-25 16:01:30 -04002165 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002166 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002167 }
Chris Masone66f7092007-04-20 13:16:02 -04002168 return 1;
2169}
2170
Chris Mason74123bd2007-02-02 11:05:29 -05002171/*
Chris Masond352ac62008-09-29 15:18:18 -04002172 * readahead one full node of leaves, finding things that are close
2173 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002174 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002175static void reada_for_search(struct btrfs_fs_info *fs_info,
Chris Masonc8c42862009-04-03 10:14:18 -04002176 struct btrfs_path *path,
2177 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002178{
Chris Mason5f39d392007-10-15 16:14:19 -04002179 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002180 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002181 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002182 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002183 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002184 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002185 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002186 u32 nr;
2187 u32 blocksize;
2188 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002189
Chris Masona6b6e752007-10-15 16:22:39 -04002190 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002191 return;
2192
Chris Mason6702ed42007-08-07 16:15:09 -04002193 if (!path->nodes[level])
2194 return;
2195
Chris Mason5f39d392007-10-15 16:14:19 -04002196 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002197
Chris Mason3c69fae2007-08-07 15:52:22 -04002198 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002199 blocksize = fs_info->nodesize;
2200 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002201 if (eb) {
2202 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002203 return;
2204 }
2205
Chris Masona7175312009-01-22 09:23:10 -05002206 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002207
Chris Mason5f39d392007-10-15 16:14:19 -04002208 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002209 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002210
Chris Masond3977122009-01-05 21:25:51 -05002211 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002212 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002213 if (nr == 0)
2214 break;
2215 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002216 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002217 nr++;
2218 if (nr >= nritems)
2219 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002220 }
David Sterbae4058b52015-11-27 16:31:35 +01002221 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002222 btrfs_node_key(node, &disk_key, nr);
2223 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2224 break;
2225 }
Chris Mason6b800532007-10-15 16:17:34 -04002226 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002227 if ((search <= target && target - search <= 65536) ||
2228 (search > target && search - target <= 65536)) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002229 readahead_tree_block(fs_info, search);
Chris Mason6b800532007-10-15 16:17:34 -04002230 nread += blocksize;
2231 }
2232 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002233 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002234 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002235 }
2236}
Chris Mason925baed2008-06-25 16:01:30 -04002237
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002238static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
Josef Bacik0b088512013-06-17 14:23:02 -04002239 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002240{
2241 int slot;
2242 int nritems;
2243 struct extent_buffer *parent;
2244 struct extent_buffer *eb;
2245 u64 gen;
2246 u64 block1 = 0;
2247 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002248
Chris Mason8c594ea2009-04-20 15:50:10 -04002249 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002250 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002251 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002252
2253 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002254 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002255
2256 if (slot > 0) {
2257 block1 = btrfs_node_blockptr(parent, slot - 1);
2258 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002259 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002260 /*
2261 * if we get -eagain from btrfs_buffer_uptodate, we
2262 * don't want to return eagain here. That will loop
2263 * forever
2264 */
2265 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002266 block1 = 0;
2267 free_extent_buffer(eb);
2268 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002269 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002270 block2 = btrfs_node_blockptr(parent, slot + 1);
2271 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002272 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002273 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002274 block2 = 0;
2275 free_extent_buffer(eb);
2276 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002277
Josef Bacik0b088512013-06-17 14:23:02 -04002278 if (block1)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002279 readahead_tree_block(fs_info, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002280 if (block2)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002281 readahead_tree_block(fs_info, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002282}
2283
2284
2285/*
Chris Masond3977122009-01-05 21:25:51 -05002286 * when we walk down the tree, it is usually safe to unlock the higher layers
2287 * in the tree. The exceptions are when our path goes through slot 0, because
2288 * operations on the tree might require changing key pointers higher up in the
2289 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002290 *
Chris Masond3977122009-01-05 21:25:51 -05002291 * callers might also have set path->keep_locks, which tells this code to keep
2292 * the lock if the path points to the last slot in the block. This is part of
2293 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002294 *
Chris Masond3977122009-01-05 21:25:51 -05002295 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2296 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002297 */
Chris Masone02119d2008-09-05 16:13:11 -04002298static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002299 int lowest_unlock, int min_write_lock_level,
2300 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002301{
2302 int i;
2303 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002304 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002305 struct extent_buffer *t;
2306
2307 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2308 if (!path->nodes[i])
2309 break;
2310 if (!path->locks[i])
2311 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002312 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002313 skip_level = i + 1;
2314 continue;
2315 }
Chris Mason051e1b92008-06-25 16:01:30 -04002316 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002317 u32 nritems;
2318 t = path->nodes[i];
2319 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002320 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002321 skip_level = i + 1;
2322 continue;
2323 }
2324 }
Chris Mason051e1b92008-06-25 16:01:30 -04002325 if (skip_level < i && i >= lowest_unlock)
2326 no_skips = 1;
2327
Chris Mason925baed2008-06-25 16:01:30 -04002328 t = path->nodes[i];
Liu Bod80bb3f2018-05-18 11:00:24 +08002329 if (i >= lowest_unlock && i > skip_level) {
Chris Masonbd681512011-07-16 15:23:14 -04002330 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002331 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002332 if (write_lock_level &&
2333 i > min_write_lock_level &&
2334 i <= *write_lock_level) {
2335 *write_lock_level = i - 1;
2336 }
Chris Mason925baed2008-06-25 16:01:30 -04002337 }
2338 }
2339}
2340
Chris Mason3c69fae2007-08-07 15:52:22 -04002341/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002342 * This releases any locks held in the path starting at level and
2343 * going all the way up to the root.
2344 *
2345 * btrfs_search_slot will keep the lock held on higher nodes in a few
2346 * corner cases, such as COW of the block at slot zero in the node. This
2347 * ignores those rules, and it should only be called when there are no
2348 * more updates to be done higher up in the tree.
2349 */
2350noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2351{
2352 int i;
2353
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002354 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002355 return;
2356
2357 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2358 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002359 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002360 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002361 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002362 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002363 path->locks[i] = 0;
2364 }
2365}
2366
2367/*
Chris Masonc8c42862009-04-03 10:14:18 -04002368 * helper function for btrfs_search_slot. The goal is to find a block
2369 * in cache without setting the path to blocking. If we find the block
2370 * we return zero and the path is unchanged.
2371 *
2372 * If we can't find the block, we set the path blocking and do some
2373 * reada. -EAGAIN is returned and the search must be repeated.
2374 */
2375static int
Liu Bod07b8522017-01-30 12:23:42 -08002376read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
2377 struct extent_buffer **eb_ret, int level, int slot,
David Sterbacda79c52017-02-10 18:44:32 +01002378 const struct btrfs_key *key)
Chris Masonc8c42862009-04-03 10:14:18 -04002379{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002380 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002381 u64 blocknr;
2382 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002383 struct extent_buffer *b = *eb_ret;
2384 struct extent_buffer *tmp;
Qu Wenruo581c1762018-03-29 09:08:11 +08002385 struct btrfs_key first_key;
Chris Mason76a05b32009-05-14 13:24:30 -04002386 int ret;
Qu Wenruo581c1762018-03-29 09:08:11 +08002387 int parent_level;
Chris Masonc8c42862009-04-03 10:14:18 -04002388
2389 blocknr = btrfs_node_blockptr(b, slot);
2390 gen = btrfs_node_ptr_generation(b, slot);
Qu Wenruo581c1762018-03-29 09:08:11 +08002391 parent_level = btrfs_header_level(b);
2392 btrfs_node_key_to_cpu(b, &first_key, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002393
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002394 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002395 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002396 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002397 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2398 *eb_ret = tmp;
2399 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002400 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002401
2402 /* the pages were up to date, but we failed
2403 * the generation number check. Do a full
2404 * read for the generation number that is correct.
2405 * We must do this without dropping locks so
2406 * we can trust our generation number
2407 */
2408 btrfs_set_path_blocking(p);
2409
2410 /* now we're allowed to do a blocking uptodate check */
Qu Wenruo581c1762018-03-29 09:08:11 +08002411 ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
Josef Bacikbdf7c002013-06-17 13:44:48 -04002412 if (!ret) {
2413 *eb_ret = tmp;
2414 return 0;
2415 }
2416 free_extent_buffer(tmp);
2417 btrfs_release_path(p);
2418 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002419 }
2420
2421 /*
2422 * reduce lock contention at high levels
2423 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002424 * we read. Don't release the lock on the current
2425 * level because we need to walk this node to figure
2426 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002427 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002428 btrfs_unlock_up_safe(p, level + 1);
2429 btrfs_set_path_blocking(p);
2430
David Sterbae4058b52015-11-27 16:31:35 +01002431 if (p->reada != READA_NONE)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002432 reada_for_search(fs_info, p, level, slot, key->objectid);
Chris Masonc8c42862009-04-03 10:14:18 -04002433
Chris Mason76a05b32009-05-14 13:24:30 -04002434 ret = -EAGAIN;
Liu Bo02a33072018-05-16 01:37:36 +08002435 tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
Qu Wenruo581c1762018-03-29 09:08:11 +08002436 &first_key);
Liu Bo64c043d2015-05-25 17:30:15 +08002437 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002438 /*
2439 * If the read above didn't mark this buffer up to date,
2440 * it will never end up being up to date. Set ret to EIO now
2441 * and give up so that our caller doesn't loop forever
2442 * on our EAGAINs.
2443 */
Liu Boe6a1d6f2018-05-18 11:00:20 +08002444 if (!extent_buffer_uptodate(tmp))
Chris Mason76a05b32009-05-14 13:24:30 -04002445 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002446 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002447 } else {
2448 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002449 }
Liu Bo02a33072018-05-16 01:37:36 +08002450
2451 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002452 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002453}
2454
2455/*
2456 * helper function for btrfs_search_slot. This does all of the checks
2457 * for node-level blocks and does any balancing required based on
2458 * the ins_len.
2459 *
2460 * If no extra work was required, zero is returned. If we had to
2461 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2462 * start over
2463 */
2464static int
2465setup_nodes_for_search(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002467 struct extent_buffer *b, int level, int ins_len,
2468 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002470 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002471 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002472
Chris Masonc8c42862009-04-03 10:14:18 -04002473 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002474 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002475 int sret;
2476
Chris Masonbd681512011-07-16 15:23:14 -04002477 if (*write_lock_level < level + 1) {
2478 *write_lock_level = level + 1;
2479 btrfs_release_path(p);
2480 goto again;
2481 }
2482
Chris Masonc8c42862009-04-03 10:14:18 -04002483 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002484 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002485 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002486 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002487
2488 BUG_ON(sret > 0);
2489 if (sret) {
2490 ret = sret;
2491 goto done;
2492 }
2493 b = p->nodes[level];
2494 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002495 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002496 int sret;
2497
Chris Masonbd681512011-07-16 15:23:14 -04002498 if (*write_lock_level < level + 1) {
2499 *write_lock_level = level + 1;
2500 btrfs_release_path(p);
2501 goto again;
2502 }
2503
Chris Masonc8c42862009-04-03 10:14:18 -04002504 btrfs_set_path_blocking(p);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002505 reada_for_balance(fs_info, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002506 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002507 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002508
2509 if (sret) {
2510 ret = sret;
2511 goto done;
2512 }
2513 b = p->nodes[level];
2514 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002515 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002516 goto again;
2517 }
2518 BUG_ON(btrfs_header_nritems(b) == 1);
2519 }
2520 return 0;
2521
2522again:
2523 ret = -EAGAIN;
2524done:
2525 return ret;
2526}
2527
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002528static void key_search_validate(struct extent_buffer *b,
Omar Sandoval310712b2017-01-17 23:24:37 -08002529 const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002530 int level)
2531{
2532#ifdef CONFIG_BTRFS_ASSERT
2533 struct btrfs_disk_key disk_key;
2534
2535 btrfs_cpu_key_to_disk(&disk_key, key);
2536
2537 if (level == 0)
2538 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2539 offsetof(struct btrfs_leaf, items[0].key),
2540 sizeof(disk_key)));
2541 else
2542 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2543 offsetof(struct btrfs_node, ptrs[0].key),
2544 sizeof(disk_key)));
2545#endif
2546}
2547
Omar Sandoval310712b2017-01-17 23:24:37 -08002548static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002549 int level, int *prev_cmp, int *slot)
2550{
2551 if (*prev_cmp != 0) {
Nikolay Borisova74b35e2017-12-08 16:27:43 +02002552 *prev_cmp = btrfs_bin_search(b, key, level, slot);
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002553 return *prev_cmp;
2554 }
2555
2556 key_search_validate(b, key, level);
2557 *slot = 0;
2558
2559 return 0;
2560}
2561
David Sterba381cf652015-01-02 18:45:16 +01002562int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002563 u64 iobjectid, u64 ioff, u8 key_type,
2564 struct btrfs_key *found_key)
2565{
2566 int ret;
2567 struct btrfs_key key;
2568 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002569
2570 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002571 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002572
2573 key.type = key_type;
2574 key.objectid = iobjectid;
2575 key.offset = ioff;
2576
2577 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002578 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002579 return ret;
2580
2581 eb = path->nodes[0];
2582 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2583 ret = btrfs_next_leaf(fs_root, path);
2584 if (ret)
2585 return ret;
2586 eb = path->nodes[0];
2587 }
2588
2589 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2590 if (found_key->type != key.type ||
2591 found_key->objectid != key.objectid)
2592 return 1;
2593
2594 return 0;
2595}
2596
Liu Bo1fc28d82018-05-18 11:00:21 +08002597static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
2598 struct btrfs_path *p,
2599 int write_lock_level)
2600{
2601 struct btrfs_fs_info *fs_info = root->fs_info;
2602 struct extent_buffer *b;
2603 int root_lock;
2604 int level = 0;
2605
2606 /* We try very hard to do read locks on the root */
2607 root_lock = BTRFS_READ_LOCK;
2608
2609 if (p->search_commit_root) {
2610 /* The commit roots are read only so we always do read locks */
2611 if (p->need_commit_sem)
2612 down_read(&fs_info->commit_root_sem);
2613 b = root->commit_root;
2614 extent_buffer_get(b);
2615 level = btrfs_header_level(b);
2616 if (p->need_commit_sem)
2617 up_read(&fs_info->commit_root_sem);
Liu Bof9ddfd02018-05-29 21:27:06 +08002618 /*
2619 * Ensure that all callers have set skip_locking when
2620 * p->search_commit_root = 1.
2621 */
2622 ASSERT(p->skip_locking == 1);
Liu Bo1fc28d82018-05-18 11:00:21 +08002623
2624 goto out;
2625 }
2626
2627 if (p->skip_locking) {
2628 b = btrfs_root_node(root);
2629 level = btrfs_header_level(b);
2630 goto out;
2631 }
2632
2633 /*
Liu Bo662c6532018-05-18 11:00:23 +08002634 * If the level is set to maximum, we can skip trying to get the read
2635 * lock.
Liu Bo1fc28d82018-05-18 11:00:21 +08002636 */
Liu Bo662c6532018-05-18 11:00:23 +08002637 if (write_lock_level < BTRFS_MAX_LEVEL) {
2638 /*
2639 * We don't know the level of the root node until we actually
2640 * have it read locked
2641 */
2642 b = btrfs_read_lock_root_node(root);
2643 level = btrfs_header_level(b);
2644 if (level > write_lock_level)
2645 goto out;
Liu Bo1fc28d82018-05-18 11:00:21 +08002646
Liu Bo662c6532018-05-18 11:00:23 +08002647 /* Whoops, must trade for write lock */
2648 btrfs_tree_read_unlock(b);
2649 free_extent_buffer(b);
2650 }
2651
Liu Bo1fc28d82018-05-18 11:00:21 +08002652 b = btrfs_lock_root_node(root);
2653 root_lock = BTRFS_WRITE_LOCK;
2654
2655 /* The level might have changed, check again */
2656 level = btrfs_header_level(b);
2657
2658out:
2659 p->nodes[level] = b;
2660 if (!p->skip_locking)
2661 p->locks[level] = root_lock;
2662 /*
2663 * Callers are responsible for dropping b's references.
2664 */
2665 return b;
2666}
2667
2668
Chris Masonc8c42862009-04-03 10:14:18 -04002669/*
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002670 * btrfs_search_slot - look for a key in a tree and perform necessary
2671 * modifications to preserve tree invariants.
Chris Mason74123bd2007-02-02 11:05:29 -05002672 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002673 * @trans: Handle of transaction, used when modifying the tree
2674 * @p: Holds all btree nodes along the search path
2675 * @root: The root node of the tree
2676 * @key: The key we are looking for
2677 * @ins_len: Indicates purpose of search, for inserts it is 1, for
2678 * deletions it's -1. 0 for plain searches
2679 * @cow: boolean should CoW operations be performed. Must always be 1
2680 * when modifying the tree.
Chris Mason97571fd2007-02-24 13:39:08 -05002681 *
Nikolay Borisov4271ece2017-12-13 09:38:14 +02002682 * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
2683 * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
2684 *
2685 * If @key is found, 0 is returned and you can find the item in the leaf level
2686 * of the path (level 0)
2687 *
2688 * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
2689 * points to the slot where it should be inserted
2690 *
2691 * If an error is encountered while searching the tree a negative error number
2692 * is returned
Chris Mason74123bd2007-02-02 11:05:29 -05002693 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002694int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2695 const struct btrfs_key *key, struct btrfs_path *p,
2696 int ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002697{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002698 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002699 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002700 int slot;
2701 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002702 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002703 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002704 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002705 /* everything at write_lock_level or lower must be write locked */
2706 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002707 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002708 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002709 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002710
Chris Mason6702ed42007-08-07 16:15:09 -04002711 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002712 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002713 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002714 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002715
Chris Masonbd681512011-07-16 15:23:14 -04002716 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002717 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002718
Chris Masonbd681512011-07-16 15:23:14 -04002719 /* when we are removing items, we might have to go up to level
2720 * two as we update tree pointers Make sure we keep write
2721 * for those levels as well
2722 */
2723 write_lock_level = 2;
2724 } else if (ins_len > 0) {
2725 /*
2726 * for inserting items, make sure we have a write lock on
2727 * level 1 so we can update keys
2728 */
2729 write_lock_level = 1;
2730 }
2731
2732 if (!cow)
2733 write_lock_level = -1;
2734
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002735 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002736 write_lock_level = BTRFS_MAX_LEVEL;
2737
Chris Masonf7c79f32012-03-19 15:54:38 -04002738 min_write_lock_level = write_lock_level;
2739
Chris Masonbb803952007-03-01 12:04:21 -05002740again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002741 prev_cmp = -1;
Liu Bo1fc28d82018-05-18 11:00:21 +08002742 b = btrfs_search_slot_get_root(root, p, write_lock_level);
Chris Mason925baed2008-06-25 16:01:30 -04002743
Chris Masoneb60cea2007-02-02 09:18:22 -05002744 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002745 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002746
2747 /*
2748 * setup the path here so we can release it under lock
2749 * contention with the cow code
2750 */
Chris Mason02217ed2007-03-02 16:08:05 -05002751 if (cow) {
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002752 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2753
Chris Masonc8c42862009-04-03 10:14:18 -04002754 /*
2755 * if we don't really need to cow this block
2756 * then we don't want to set the path blocking,
2757 * so we test it here
2758 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002759 if (!should_cow_block(trans, root, b)) {
2760 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002761 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002762 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002763
Chris Masonbd681512011-07-16 15:23:14 -04002764 /*
2765 * must have write locks on this node and the
2766 * parent
2767 */
Josef Bacik5124e002012-11-07 13:44:13 -05002768 if (level > write_lock_level ||
2769 (level + 1 > write_lock_level &&
2770 level + 1 < BTRFS_MAX_LEVEL &&
2771 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002772 write_lock_level = level + 1;
2773 btrfs_release_path(p);
2774 goto again;
2775 }
2776
Filipe Manana160f4082014-07-28 19:37:17 +01002777 btrfs_set_path_blocking(p);
Nikolay Borisov9ea2c7c2017-12-12 11:14:49 +02002778 if (last_level)
2779 err = btrfs_cow_block(trans, root, b, NULL, 0,
2780 &b);
2781 else
2782 err = btrfs_cow_block(trans, root, b,
2783 p->nodes[level + 1],
2784 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002785 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002786 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002787 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002788 }
Chris Mason02217ed2007-03-02 16:08:05 -05002789 }
Chris Mason65b51a02008-08-01 15:11:20 -04002790cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002791 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002792 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002793
2794 /*
2795 * we have a lock on b and as long as we aren't changing
2796 * the tree, there is no way to for the items in b to change.
2797 * It is safe to drop the lock on our parent before we
2798 * go through the expensive btree search on b.
2799 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002800 * If we're inserting or deleting (ins_len != 0), then we might
2801 * be changing slot zero, which may require changing the parent.
2802 * So, we can't drop the lock until after we know which slot
2803 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002804 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002805 if (!ins_len && !p->keep_locks) {
2806 int u = level + 1;
2807
2808 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2809 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2810 p->locks[u] = 0;
2811 }
2812 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002813
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002814 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002815 if (ret < 0)
2816 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002817
Chris Mason5f39d392007-10-15 16:14:19 -04002818 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002819 int dec = 0;
2820 if (ret && slot > 0) {
2821 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002822 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002823 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002824 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002825 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002826 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002827 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002828 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002829 if (err) {
2830 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002831 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002832 }
Chris Masonc8c42862009-04-03 10:14:18 -04002833 b = p->nodes[level];
2834 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002835
Chris Masonbd681512011-07-16 15:23:14 -04002836 /*
2837 * slot 0 is special, if we change the key
2838 * we have to update the parent pointer
2839 * which means we must have a write lock
2840 * on the parent
2841 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002842 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002843 write_lock_level < level + 1) {
2844 write_lock_level = level + 1;
2845 btrfs_release_path(p);
2846 goto again;
2847 }
2848
Chris Masonf7c79f32012-03-19 15:54:38 -04002849 unlock_up(p, level, lowest_unlock,
2850 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002851
Chris Mason925baed2008-06-25 16:01:30 -04002852 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002853 if (dec)
2854 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002855 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002856 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002857
Liu Bod07b8522017-01-30 12:23:42 -08002858 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01002859 slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002860 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002861 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002862 if (err) {
2863 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002864 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002865 }
Chris Mason76a05b32009-05-14 13:24:30 -04002866
Chris Masonb4ce94d2009-02-04 09:25:08 -05002867 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002868 level = btrfs_header_level(b);
2869 if (level <= write_lock_level) {
2870 err = btrfs_try_tree_write_lock(b);
2871 if (!err) {
2872 btrfs_set_path_blocking(p);
2873 btrfs_tree_lock(b);
2874 btrfs_clear_path_blocking(p, b,
2875 BTRFS_WRITE_LOCK);
2876 }
2877 p->locks[level] = BTRFS_WRITE_LOCK;
2878 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002879 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002880 if (!err) {
2881 btrfs_set_path_blocking(p);
2882 btrfs_tree_read_lock(b);
2883 btrfs_clear_path_blocking(p, b,
2884 BTRFS_READ_LOCK);
2885 }
2886 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002887 }
Chris Masonbd681512011-07-16 15:23:14 -04002888 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002889 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002890 } else {
2891 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002892 if (ins_len > 0 &&
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002893 btrfs_leaf_free_space(fs_info, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002894 if (write_lock_level < 1) {
2895 write_lock_level = 1;
2896 btrfs_release_path(p);
2897 goto again;
2898 }
2899
Chris Masonb4ce94d2009-02-04 09:25:08 -05002900 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002901 err = split_leaf(trans, root, key,
2902 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002903 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002904
Yan Zheng33c66f42009-07-22 09:59:00 -04002905 BUG_ON(err > 0);
2906 if (err) {
2907 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002908 goto done;
2909 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002910 }
Chris Mason459931e2008-12-10 09:10:46 -05002911 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002912 unlock_up(p, level, lowest_unlock,
2913 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002914 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002915 }
2916 }
Chris Mason65b51a02008-08-01 15:11:20 -04002917 ret = 1;
2918done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002919 /*
2920 * we don't really know what they plan on doing with the path
2921 * from here on, so for now just mark it as blocking
2922 */
Chris Masonb9473432009-03-13 11:00:37 -04002923 if (!p->leave_spinning)
2924 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002925 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002926 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002927 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002928}
2929
Chris Mason74123bd2007-02-02 11:05:29 -05002930/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002931 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2932 * current state of the tree together with the operations recorded in the tree
2933 * modification log to search for the key in a previous version of this tree, as
2934 * denoted by the time_seq parameter.
2935 *
2936 * Naturally, there is no support for insert, delete or cow operations.
2937 *
2938 * The resulting path and return value will be set up as if we called
2939 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2940 */
Omar Sandoval310712b2017-01-17 23:24:37 -08002941int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002942 struct btrfs_path *p, u64 time_seq)
2943{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002944 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002945 struct extent_buffer *b;
2946 int slot;
2947 int ret;
2948 int err;
2949 int level;
2950 int lowest_unlock = 1;
2951 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002952 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002953
2954 lowest_level = p->lowest_level;
2955 WARN_ON(p->nodes[0] != NULL);
2956
2957 if (p->search_commit_root) {
2958 BUG_ON(time_seq);
2959 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2960 }
2961
2962again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002963 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002964 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002965 p->locks[level] = BTRFS_READ_LOCK;
2966
2967 while (b) {
2968 level = btrfs_header_level(b);
2969 p->nodes[level] = b;
2970 btrfs_clear_path_blocking(p, NULL, 0);
2971
2972 /*
2973 * we have a lock on b and as long as we aren't changing
2974 * the tree, there is no way to for the items in b to change.
2975 * It is safe to drop the lock on our parent before we
2976 * go through the expensive btree search on b.
2977 */
2978 btrfs_unlock_up_safe(p, level + 1);
2979
Josef Bacikd4b40872013-09-24 14:09:34 -04002980 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002981 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002982 * time.
2983 */
2984 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002985 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002986
2987 if (level != 0) {
2988 int dec = 0;
2989 if (ret && slot > 0) {
2990 dec = 1;
2991 slot -= 1;
2992 }
2993 p->slots[level] = slot;
2994 unlock_up(p, level, lowest_unlock, 0, NULL);
2995
2996 if (level == lowest_level) {
2997 if (dec)
2998 p->slots[level]++;
2999 goto done;
3000 }
3001
Liu Bod07b8522017-01-30 12:23:42 -08003002 err = read_block_for_search(root, p, &b, level,
David Sterbacda79c52017-02-10 18:44:32 +01003003 slot, key);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003004 if (err == -EAGAIN)
3005 goto again;
3006 if (err) {
3007 ret = err;
3008 goto done;
3009 }
3010
3011 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003012 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003013 if (!err) {
3014 btrfs_set_path_blocking(p);
3015 btrfs_tree_read_lock(b);
3016 btrfs_clear_path_blocking(p, b,
3017 BTRFS_READ_LOCK);
3018 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003019 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003020 if (!b) {
3021 ret = -ENOMEM;
3022 goto done;
3023 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003024 p->locks[level] = BTRFS_READ_LOCK;
3025 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003026 } else {
3027 p->slots[level] = slot;
3028 unlock_up(p, level, lowest_unlock, 0, NULL);
3029 goto done;
3030 }
3031 }
3032 ret = 1;
3033done:
3034 if (!p->leave_spinning)
3035 btrfs_set_path_blocking(p);
3036 if (ret < 0)
3037 btrfs_release_path(p);
3038
3039 return ret;
3040}
3041
3042/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003043 * helper to use instead of search slot if no exact match is needed but
3044 * instead the next or previous item should be returned.
3045 * When find_higher is true, the next higher item is returned, the next lower
3046 * otherwise.
3047 * When return_any and find_higher are both true, and no higher item is found,
3048 * return the next lower instead.
3049 * When return_any is true and find_higher is false, and no lower item is found,
3050 * return the next higher instead.
3051 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3052 * < 0 on error
3053 */
3054int btrfs_search_slot_for_read(struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08003055 const struct btrfs_key *key,
3056 struct btrfs_path *p, int find_higher,
3057 int return_any)
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003058{
3059 int ret;
3060 struct extent_buffer *leaf;
3061
3062again:
3063 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3064 if (ret <= 0)
3065 return ret;
3066 /*
3067 * a return value of 1 means the path is at the position where the
3068 * item should be inserted. Normally this is the next bigger item,
3069 * but in case the previous item is the last in a leaf, path points
3070 * to the first free slot in the previous leaf, i.e. at an invalid
3071 * item.
3072 */
3073 leaf = p->nodes[0];
3074
3075 if (find_higher) {
3076 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3077 ret = btrfs_next_leaf(root, p);
3078 if (ret <= 0)
3079 return ret;
3080 if (!return_any)
3081 return 1;
3082 /*
3083 * no higher item found, return the next
3084 * lower instead
3085 */
3086 return_any = 0;
3087 find_higher = 0;
3088 btrfs_release_path(p);
3089 goto again;
3090 }
3091 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003092 if (p->slots[0] == 0) {
3093 ret = btrfs_prev_leaf(root, p);
3094 if (ret < 0)
3095 return ret;
3096 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003097 leaf = p->nodes[0];
3098 if (p->slots[0] == btrfs_header_nritems(leaf))
3099 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003100 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003101 }
Arne Jansene6793762011-09-13 11:18:10 +02003102 if (!return_any)
3103 return 1;
3104 /*
3105 * no lower item found, return the next
3106 * higher instead
3107 */
3108 return_any = 0;
3109 find_higher = 1;
3110 btrfs_release_path(p);
3111 goto again;
3112 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003113 --p->slots[0];
3114 }
3115 }
3116 return 0;
3117}
3118
3119/*
Chris Mason74123bd2007-02-02 11:05:29 -05003120 * adjust the pointers going up the tree, starting at level
3121 * making sure the right key of each node is points to 'key'.
3122 * This is used after shifting pointers to the left, so it stops
3123 * fixing up pointers when a given leaf/node is not in slot 0 of the
3124 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003125 *
Chris Mason74123bd2007-02-02 11:05:29 -05003126 */
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003127static void fixup_low_keys(struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003128 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003129{
3130 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003131 struct extent_buffer *t;
David Sterba0e82bcf2018-03-05 16:16:54 +01003132 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003133
Chris Mason234b63a2007-03-13 10:46:10 -04003134 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003135 int tslot = path->slots[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003136
Chris Masoneb60cea2007-02-02 09:18:22 -05003137 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003138 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003139 t = path->nodes[i];
David Sterba0e82bcf2018-03-05 16:16:54 +01003140 ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
3141 GFP_ATOMIC);
3142 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003143 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003144 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003145 if (tslot != 0)
3146 break;
3147 }
3148}
3149
Chris Mason74123bd2007-02-02 11:05:29 -05003150/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003151 * update item key.
3152 *
3153 * This function isn't completely safe. It's the caller's responsibility
3154 * that the new key won't break the order
3155 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003156void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3157 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08003158 const struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003159{
3160 struct btrfs_disk_key disk_key;
3161 struct extent_buffer *eb;
3162 int slot;
3163
3164 eb = path->nodes[0];
3165 slot = path->slots[0];
3166 if (slot > 0) {
3167 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003168 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003169 }
3170 if (slot < btrfs_header_nritems(eb) - 1) {
3171 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003172 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003173 }
3174
3175 btrfs_cpu_key_to_disk(&disk_key, new_key);
3176 btrfs_set_item_key(eb, &disk_key, slot);
3177 btrfs_mark_buffer_dirty(eb);
3178 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003179 fixup_low_keys(path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003180}
3181
3182/*
Chris Mason74123bd2007-02-02 11:05:29 -05003183 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003184 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003185 *
3186 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3187 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003188 */
Chris Mason98ed5172008-01-03 10:01:48 -05003189static int push_node_left(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003190 struct btrfs_fs_info *fs_info,
3191 struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003192 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003193{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003194 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003195 int src_nritems;
3196 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003197 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003198
Chris Mason5f39d392007-10-15 16:14:19 -04003199 src_nritems = btrfs_header_nritems(src);
3200 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003201 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003202 WARN_ON(btrfs_header_generation(src) != trans->transid);
3203 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003204
Chris Masonbce4eae2008-04-24 14:42:46 -04003205 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003206 return 1;
3207
Chris Masond3977122009-01-05 21:25:51 -05003208 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003209 return 1;
3210
Chris Masonbce4eae2008-04-24 14:42:46 -04003211 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003212 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003213 if (push_items < src_nritems) {
3214 /* leave at least 8 pointers in the node if
3215 * we aren't going to empty it
3216 */
3217 if (src_nritems - push_items < 8) {
3218 if (push_items <= 8)
3219 return 1;
3220 push_items -= 8;
3221 }
3222 }
3223 } else
3224 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003225
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003226 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003227 push_items);
3228 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003229 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003230 return ret;
3231 }
Chris Mason5f39d392007-10-15 16:14:19 -04003232 copy_extent_buffer(dst, src,
3233 btrfs_node_key_ptr_offset(dst_nritems),
3234 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003235 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003236
Chris Masonbb803952007-03-01 12:04:21 -05003237 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003238 /*
David Sterbabf1d3422018-03-05 15:47:39 +01003239 * Don't call tree_mod_log_insert_move here, key removal was
3240 * already fully logged by tree_mod_log_eb_copy above.
Jan Schmidt57911b82012-10-19 09:22:03 +02003241 */
Chris Mason5f39d392007-10-15 16:14:19 -04003242 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3243 btrfs_node_key_ptr_offset(push_items),
3244 (src_nritems - push_items) *
3245 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003246 }
Chris Mason5f39d392007-10-15 16:14:19 -04003247 btrfs_set_header_nritems(src, src_nritems - push_items);
3248 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3249 btrfs_mark_buffer_dirty(src);
3250 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003251
Chris Masonbb803952007-03-01 12:04:21 -05003252 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003253}
3254
Chris Mason97571fd2007-02-24 13:39:08 -05003255/*
Chris Mason79f95c82007-03-01 15:16:26 -05003256 * try to push data from one node into the next node right in the
3257 * tree.
3258 *
3259 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3260 * error, and > 0 if there was no room in the right hand block.
3261 *
3262 * this will only push up to 1/2 the contents of the left node over
3263 */
Chris Mason5f39d392007-10-15 16:14:19 -04003264static int balance_node_right(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003265 struct btrfs_fs_info *fs_info,
Chris Mason5f39d392007-10-15 16:14:19 -04003266 struct extent_buffer *dst,
3267 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003268{
Chris Mason79f95c82007-03-01 15:16:26 -05003269 int push_items = 0;
3270 int max_push;
3271 int src_nritems;
3272 int dst_nritems;
3273 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003274
Chris Mason7bb86312007-12-11 09:25:06 -05003275 WARN_ON(btrfs_header_generation(src) != trans->transid);
3276 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3277
Chris Mason5f39d392007-10-15 16:14:19 -04003278 src_nritems = btrfs_header_nritems(src);
3279 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003280 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003281 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003282 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003283
Chris Masond3977122009-01-05 21:25:51 -05003284 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003285 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003286
3287 max_push = src_nritems / 2 + 1;
3288 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003289 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003290 return 1;
Yan252c38f2007-08-29 09:11:44 -04003291
Chris Mason79f95c82007-03-01 15:16:26 -05003292 if (max_push < push_items)
3293 push_items = max_push;
3294
David Sterbabf1d3422018-03-05 15:47:39 +01003295 ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
3296 BUG_ON(ret < 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003297 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3298 btrfs_node_key_ptr_offset(0),
3299 (dst_nritems) *
3300 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003301
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003302 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003303 src_nritems - push_items, push_items);
3304 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003305 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003306 return ret;
3307 }
Chris Mason5f39d392007-10-15 16:14:19 -04003308 copy_extent_buffer(dst, src,
3309 btrfs_node_key_ptr_offset(0),
3310 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003311 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003312
Chris Mason5f39d392007-10-15 16:14:19 -04003313 btrfs_set_header_nritems(src, src_nritems - push_items);
3314 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003315
Chris Mason5f39d392007-10-15 16:14:19 -04003316 btrfs_mark_buffer_dirty(src);
3317 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003318
Chris Mason79f95c82007-03-01 15:16:26 -05003319 return ret;
3320}
3321
3322/*
Chris Mason97571fd2007-02-24 13:39:08 -05003323 * helper function to insert a new root level in the tree.
3324 * A new node is allocated, and a single item is inserted to
3325 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003326 *
3327 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003328 */
Chris Masond3977122009-01-05 21:25:51 -05003329static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003330 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003331 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003332{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003333 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003334 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003335 struct extent_buffer *lower;
3336 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003337 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003338 struct btrfs_disk_key lower_key;
David Sterbad9d19a02018-03-05 16:35:29 +01003339 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003340
3341 BUG_ON(path->nodes[level]);
3342 BUG_ON(path->nodes[level-1] != root->node);
3343
Chris Mason7bb86312007-12-11 09:25:06 -05003344 lower = path->nodes[level-1];
3345 if (level == 1)
3346 btrfs_item_key(lower, &lower_key, 0);
3347 else
3348 btrfs_node_key(lower, &lower_key, 0);
3349
David Sterba4d75f8a2014-06-15 01:54:12 +02003350 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3351 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003352 if (IS_ERR(c))
3353 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003354
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003355 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003356
Chris Mason5f39d392007-10-15 16:14:19 -04003357 btrfs_set_header_nritems(c, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003358 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003359 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003360 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003361 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003362
3363 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003364
3365 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003366
Chris Mason925baed2008-06-25 16:01:30 -04003367 old = root->node;
David Sterbad9d19a02018-03-05 16:35:29 +01003368 ret = tree_mod_log_insert_root(root->node, c, 0);
3369 BUG_ON(ret < 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003370 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003371
3372 /* the super has an extra ref to root->node */
3373 free_extent_buffer(old);
3374
Chris Mason0b86a832008-03-24 15:01:56 -04003375 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003376 extent_buffer_get(c);
3377 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303378 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003379 path->slots[level] = 0;
3380 return 0;
3381}
3382
Chris Mason74123bd2007-02-02 11:05:29 -05003383/*
3384 * worker function to insert a single pointer in a node.
3385 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003386 *
Chris Mason74123bd2007-02-02 11:05:29 -05003387 * slot and level indicate where you want the key to go, and
3388 * blocknr is the block the key points to.
3389 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003390static void insert_ptr(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003391 struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003392 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003393 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003394{
Chris Mason5f39d392007-10-15 16:14:19 -04003395 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003396 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003397 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003398
3399 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003400 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003401 lower = path->nodes[level];
3402 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003403 BUG_ON(slot > nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003404 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003405 if (slot != nritems) {
David Sterbabf1d3422018-03-05 15:47:39 +01003406 if (level) {
3407 ret = tree_mod_log_insert_move(lower, slot + 1, slot,
David Sterbaa446a972018-03-05 15:26:29 +01003408 nritems - slot);
David Sterbabf1d3422018-03-05 15:47:39 +01003409 BUG_ON(ret < 0);
3410 }
Chris Mason5f39d392007-10-15 16:14:19 -04003411 memmove_extent_buffer(lower,
3412 btrfs_node_key_ptr_offset(slot + 1),
3413 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003414 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003415 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003416 if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01003417 ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
3418 GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003419 BUG_ON(ret < 0);
3420 }
Chris Mason5f39d392007-10-15 16:14:19 -04003421 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003422 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003423 WARN_ON(trans->transid == 0);
3424 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003425 btrfs_set_header_nritems(lower, nritems + 1);
3426 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003427}
3428
Chris Mason97571fd2007-02-24 13:39:08 -05003429/*
3430 * split the node at the specified level in path in two.
3431 * The path is corrected to point to the appropriate node after the split
3432 *
3433 * Before splitting this tries to make some room in the node by pushing
3434 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003435 *
3436 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003437 */
Chris Masone02119d2008-09-05 16:13:11 -04003438static noinline int split_node(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root,
3440 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003443 struct extent_buffer *c;
3444 struct extent_buffer *split;
3445 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003446 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003447 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003448 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003449
Chris Mason5f39d392007-10-15 16:14:19 -04003450 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003451 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003452 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003453 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003454 * trying to split the root, lets make a new one
3455 *
Liu Bofdd99c72013-05-22 12:06:51 +00003456 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003457 * insert_new_root, because that root buffer will be kept as a
3458 * normal node. We are going to log removal of half of the
3459 * elements below with tree_mod_log_eb_copy. We're holding a
3460 * tree lock on the buffer, which is why we cannot race with
3461 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003462 */
Liu Bofdd99c72013-05-22 12:06:51 +00003463 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003464 if (ret)
3465 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003466 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003467 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003468 c = path->nodes[level];
3469 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003470 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003471 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003472 if (ret < 0)
3473 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003474 }
Chris Masone66f7092007-04-20 13:16:02 -04003475
Chris Mason5f39d392007-10-15 16:14:19 -04003476 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003477 mid = (c_nritems + 1) / 2;
3478 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003479
David Sterba4d75f8a2014-06-15 01:54:12 +02003480 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3481 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003482 if (IS_ERR(split))
3483 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003484
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003485 root_add_used(root, fs_info->nodesize);
Nikolay Borisovbc877d22018-06-18 14:13:19 +03003486 ASSERT(btrfs_header_level(c) == level);
Chris Mason5f39d392007-10-15 16:14:19 -04003487
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003489 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003490 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003491 return ret;
3492 }
Chris Mason5f39d392007-10-15 16:14:19 -04003493 copy_extent_buffer(split, c,
3494 btrfs_node_key_ptr_offset(0),
3495 btrfs_node_key_ptr_offset(mid),
3496 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3497 btrfs_set_header_nritems(split, c_nritems - mid);
3498 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003499 ret = 0;
3500
Chris Mason5f39d392007-10-15 16:14:19 -04003501 btrfs_mark_buffer_dirty(c);
3502 btrfs_mark_buffer_dirty(split);
3503
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003504 insert_ptr(trans, fs_info, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003505 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003506
Chris Mason5de08d72007-02-24 06:24:44 -05003507 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003508 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003509 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003510 free_extent_buffer(c);
3511 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003512 path->slots[level + 1] += 1;
3513 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003514 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003515 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003516 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003517 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003518}
3519
Chris Mason74123bd2007-02-02 11:05:29 -05003520/*
3521 * how many bytes are required to store the items in a leaf. start
3522 * and nr indicate which items in the leaf to check. This totals up the
3523 * space used both by the item structs and the item data
3524 */
Chris Mason5f39d392007-10-15 16:14:19 -04003525static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003526{
Josef Bacik41be1f32012-10-15 13:43:18 -04003527 struct btrfs_item *start_item;
3528 struct btrfs_item *end_item;
3529 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003530 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003531 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003532 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003533
3534 if (!nr)
3535 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003536 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003537 start_item = btrfs_item_nr(start);
3538 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003539 data_len = btrfs_token_item_offset(l, start_item, &token) +
3540 btrfs_token_item_size(l, start_item, &token);
3541 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003542 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003543 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544 return data_len;
3545}
3546
Chris Mason74123bd2007-02-02 11:05:29 -05003547/*
Chris Masond4dbff92007-04-04 14:08:15 -04003548 * The space between the end of the leaf items and
3549 * the start of the leaf data. IOW, how much room
3550 * the leaf has left for both items and data
3551 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003552noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
Chris Masone02119d2008-09-05 16:13:11 -04003553 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003554{
Chris Mason5f39d392007-10-15 16:14:19 -04003555 int nritems = btrfs_header_nritems(leaf);
3556 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003557
3558 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003559 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003560 btrfs_crit(fs_info,
3561 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3562 ret,
3563 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3564 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003565 }
3566 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003567}
3568
Chris Mason99d8f832010-07-07 10:51:48 -04003569/*
3570 * min slot controls the lowest index we're willing to push to the
3571 * right. We'll push up to and including min_slot, but no lower
3572 */
David Sterba1e47eef2017-02-10 19:13:06 +01003573static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003574 struct btrfs_path *path,
3575 int data_size, int empty,
3576 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003577 int free_space, u32 left_nritems,
3578 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003579{
Chris Mason5f39d392007-10-15 16:14:19 -04003580 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003581 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003582 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003583 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003584 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003585 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003586 int push_space = 0;
3587 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003588 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003589 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003590 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003591 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003592 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003593
Chris Masoncfed81a2012-03-03 07:40:03 -05003594 btrfs_init_map_token(&token);
3595
Chris Mason34a38212007-11-07 13:31:03 -05003596 if (empty)
3597 nr = 0;
3598 else
Chris Mason99d8f832010-07-07 10:51:48 -04003599 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003600
Zheng Yan31840ae2008-09-23 13:14:14 -04003601 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003602 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003603
Chris Mason44871b12009-03-13 10:04:31 -04003604 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003605 i = left_nritems - 1;
3606 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003607 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003608
Zheng Yan31840ae2008-09-23 13:14:14 -04003609 if (!empty && push_items > 0) {
3610 if (path->slots[0] > i)
3611 break;
3612 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003613 int space = btrfs_leaf_free_space(fs_info, left);
Zheng Yan31840ae2008-09-23 13:14:14 -04003614 if (space + push_space * 2 > free_space)
3615 break;
3616 }
3617 }
3618
Chris Mason00ec4c52007-02-24 12:47:20 -05003619 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003620 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003621
Chris Masondb945352007-10-15 16:15:53 -04003622 this_item_size = btrfs_item_size(left, item);
3623 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003624 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003625
Chris Mason00ec4c52007-02-24 12:47:20 -05003626 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003627 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003628 if (i == 0)
3629 break;
3630 i--;
Chris Masondb945352007-10-15 16:15:53 -04003631 }
Chris Mason5f39d392007-10-15 16:14:19 -04003632
Chris Mason925baed2008-06-25 16:01:30 -04003633 if (push_items == 0)
3634 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003635
Julia Lawall6c1500f2012-11-03 20:30:18 +00003636 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003637
Chris Mason00ec4c52007-02-24 12:47:20 -05003638 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003639 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003640
Chris Mason5f39d392007-10-15 16:14:19 -04003641 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003642 push_space -= leaf_data_end(fs_info, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003643
Chris Mason00ec4c52007-02-24 12:47:20 -05003644 /* make room in the right data area */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003645 data_end = leaf_data_end(fs_info, right);
Chris Mason5f39d392007-10-15 16:14:19 -04003646 memmove_extent_buffer(right,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003647 BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
3648 BTRFS_LEAF_DATA_OFFSET + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003649 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003650
Chris Mason00ec4c52007-02-24 12:47:20 -05003651 /* copy from the left data area */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003652 copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003653 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003654 BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
Chris Masond6025572007-03-30 14:27:56 -04003655 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003656
3657 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3658 btrfs_item_nr_offset(0),
3659 right_nritems * sizeof(struct btrfs_item));
3660
Chris Mason00ec4c52007-02-24 12:47:20 -05003661 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003662 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3663 btrfs_item_nr_offset(left_nritems - push_items),
3664 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003665
3666 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003667 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003668 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003669 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003670 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003671 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003672 push_space -= btrfs_token_item_size(right, item, &token);
3673 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003674 }
3675
Chris Mason7518a232007-03-12 12:01:18 -04003676 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003677 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003678
Chris Mason34a38212007-11-07 13:31:03 -05003679 if (left_nritems)
3680 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003681 else
David Sterba7c302b42017-02-10 18:47:57 +01003682 clean_tree_block(fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003683
Chris Mason5f39d392007-10-15 16:14:19 -04003684 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003685
Chris Mason5f39d392007-10-15 16:14:19 -04003686 btrfs_item_key(right, &disk_key, 0);
3687 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003688 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003689
Chris Mason00ec4c52007-02-24 12:47:20 -05003690 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003691 if (path->slots[0] >= left_nritems) {
3692 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003693 if (btrfs_header_nritems(path->nodes[0]) == 0)
David Sterba7c302b42017-02-10 18:47:57 +01003694 clean_tree_block(fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003695 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003696 free_extent_buffer(path->nodes[0]);
3697 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003698 path->slots[1] += 1;
3699 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003700 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003701 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003702 }
3703 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003704
3705out_unlock:
3706 btrfs_tree_unlock(right);
3707 free_extent_buffer(right);
3708 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003709}
Chris Mason925baed2008-06-25 16:01:30 -04003710
Chris Mason00ec4c52007-02-24 12:47:20 -05003711/*
Chris Mason44871b12009-03-13 10:04:31 -04003712 * push some data in the path leaf to the right, trying to free up at
3713 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3714 *
3715 * returns 1 if the push failed because the other node didn't have enough
3716 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003717 *
3718 * this will push starting from min_slot to the end of the leaf. It won't
3719 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003720 */
3721static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003722 *root, struct btrfs_path *path,
3723 int min_data_size, int data_size,
3724 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003725{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003726 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003727 struct extent_buffer *left = path->nodes[0];
3728 struct extent_buffer *right;
3729 struct extent_buffer *upper;
3730 int slot;
3731 int free_space;
3732 u32 left_nritems;
3733 int ret;
3734
3735 if (!path->nodes[1])
3736 return 1;
3737
3738 slot = path->slots[1];
3739 upper = path->nodes[1];
3740 if (slot >= btrfs_header_nritems(upper) - 1)
3741 return 1;
3742
3743 btrfs_assert_tree_locked(path->nodes[1]);
3744
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003745 right = read_node_slot(fs_info, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003746 /*
3747 * slot + 1 is not valid or we fail to read the right node,
3748 * no big deal, just return.
3749 */
3750 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003751 return 1;
3752
Chris Mason44871b12009-03-13 10:04:31 -04003753 btrfs_tree_lock(right);
3754 btrfs_set_lock_blocking(right);
3755
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003756 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003757 if (free_space < data_size)
3758 goto out_unlock;
3759
3760 /* cow and double check */
3761 ret = btrfs_cow_block(trans, root, right, upper,
3762 slot + 1, &right);
3763 if (ret)
3764 goto out_unlock;
3765
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003766 free_space = btrfs_leaf_free_space(fs_info, right);
Chris Mason44871b12009-03-13 10:04:31 -04003767 if (free_space < data_size)
3768 goto out_unlock;
3769
3770 left_nritems = btrfs_header_nritems(left);
3771 if (left_nritems == 0)
3772 goto out_unlock;
3773
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003774 if (path->slots[0] == left_nritems && !empty) {
3775 /* Key greater than all keys in the leaf, right neighbor has
3776 * enough room for it and we're not emptying our leaf to delete
3777 * it, therefore use right neighbor to insert the new item and
3778 * no need to touch/dirty our left leaft. */
3779 btrfs_tree_unlock(left);
3780 free_extent_buffer(left);
3781 path->nodes[0] = right;
3782 path->slots[0] = 0;
3783 path->slots[1]++;
3784 return 0;
3785 }
3786
David Sterba1e47eef2017-02-10 19:13:06 +01003787 return __push_leaf_right(fs_info, path, min_data_size, empty,
Chris Mason99d8f832010-07-07 10:51:48 -04003788 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003789out_unlock:
3790 btrfs_tree_unlock(right);
3791 free_extent_buffer(right);
3792 return 1;
3793}
3794
3795/*
Chris Mason74123bd2007-02-02 11:05:29 -05003796 * push some data in the path leaf to the left, trying to free up at
3797 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003798 *
3799 * max_slot can put a limit on how far into the leaf we'll push items. The
3800 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3801 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003802 */
David Sterba66cb7dd2017-02-10 19:14:36 +01003803static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
Chris Mason44871b12009-03-13 10:04:31 -04003804 struct btrfs_path *path, int data_size,
3805 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003806 int free_space, u32 right_nritems,
3807 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003808{
Chris Mason5f39d392007-10-15 16:14:19 -04003809 struct btrfs_disk_key disk_key;
3810 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003811 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003812 int push_space = 0;
3813 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003814 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003815 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003816 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003817 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003818 u32 this_item_size;
3819 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003820 struct btrfs_map_token token;
3821
3822 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003823
Chris Mason34a38212007-11-07 13:31:03 -05003824 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003825 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003826 else
Chris Mason99d8f832010-07-07 10:51:48 -04003827 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003828
3829 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003830 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003831
Zheng Yan31840ae2008-09-23 13:14:14 -04003832 if (!empty && push_items > 0) {
3833 if (path->slots[0] < i)
3834 break;
3835 if (path->slots[0] == i) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003836 int space = btrfs_leaf_free_space(fs_info, right);
Zheng Yan31840ae2008-09-23 13:14:14 -04003837 if (space + push_space * 2 > free_space)
3838 break;
3839 }
3840 }
3841
Chris Masonbe0e5c02007-01-26 15:51:26 -05003842 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003843 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003844
3845 this_item_size = btrfs_item_size(right, item);
3846 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003847 break;
Chris Masondb945352007-10-15 16:15:53 -04003848
Chris Masonbe0e5c02007-01-26 15:51:26 -05003849 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003850 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851 }
Chris Masondb945352007-10-15 16:15:53 -04003852
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003854 ret = 1;
3855 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003856 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303857 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003858
Chris Masonbe0e5c02007-01-26 15:51:26 -05003859 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003860 copy_extent_buffer(left, right,
3861 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3862 btrfs_item_nr_offset(0),
3863 push_items * sizeof(struct btrfs_item));
3864
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003865 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003866 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003867
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003868 copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003869 leaf_data_end(fs_info, left) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003870 BTRFS_LEAF_DATA_OFFSET +
Chris Mason5f39d392007-10-15 16:14:19 -04003871 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003872 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003873 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003874 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003875
Chris Masondb945352007-10-15 16:15:53 -04003876 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003877 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003878 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003879
Ross Kirkdd3cc162013-09-16 15:58:09 +01003880 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003881
Chris Masoncfed81a2012-03-03 07:40:03 -05003882 ioff = btrfs_token_item_offset(left, item, &token);
3883 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003884 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003885 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003886 }
Chris Mason5f39d392007-10-15 16:14:19 -04003887 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003888
3889 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003890 if (push_items > right_nritems)
3891 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003892 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003893
Chris Mason34a38212007-11-07 13:31:03 -05003894 if (push_items < right_nritems) {
3895 push_space = btrfs_item_offset_nr(right, push_items - 1) -
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003896 leaf_data_end(fs_info, right);
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003897 memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003898 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03003899 BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003900 leaf_data_end(fs_info, right), push_space);
Chris Mason34a38212007-11-07 13:31:03 -05003901
3902 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003903 btrfs_item_nr_offset(push_items),
3904 (btrfs_header_nritems(right) - push_items) *
3905 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003906 }
Yaneef1c492007-11-26 10:58:13 -05003907 right_nritems -= push_items;
3908 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003909 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003910 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003911 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003912
Chris Masoncfed81a2012-03-03 07:40:03 -05003913 push_space = push_space - btrfs_token_item_size(right,
3914 item, &token);
3915 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003916 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003917
Chris Mason5f39d392007-10-15 16:14:19 -04003918 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003919 if (right_nritems)
3920 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003921 else
David Sterba7c302b42017-02-10 18:47:57 +01003922 clean_tree_block(fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003923
Chris Mason5f39d392007-10-15 16:14:19 -04003924 btrfs_item_key(right, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03003925 fixup_low_keys(path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003926
3927 /* then fixup the leaf pointer in the path */
3928 if (path->slots[0] < push_items) {
3929 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003930 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003931 free_extent_buffer(path->nodes[0]);
3932 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003933 path->slots[1] -= 1;
3934 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003935 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003936 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003937 path->slots[0] -= push_items;
3938 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003939 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003940 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003941out:
3942 btrfs_tree_unlock(left);
3943 free_extent_buffer(left);
3944 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003945}
3946
Chris Mason74123bd2007-02-02 11:05:29 -05003947/*
Chris Mason44871b12009-03-13 10:04:31 -04003948 * push some data in the path leaf to the left, trying to free up at
3949 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003950 *
3951 * max_slot can put a limit on how far into the leaf we'll push items. The
3952 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3953 * items
Chris Mason44871b12009-03-13 10:04:31 -04003954 */
3955static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003956 *root, struct btrfs_path *path, int min_data_size,
3957 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003958{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003959 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04003960 struct extent_buffer *right = path->nodes[0];
3961 struct extent_buffer *left;
3962 int slot;
3963 int free_space;
3964 u32 right_nritems;
3965 int ret = 0;
3966
3967 slot = path->slots[1];
3968 if (slot == 0)
3969 return 1;
3970 if (!path->nodes[1])
3971 return 1;
3972
3973 right_nritems = btrfs_header_nritems(right);
3974 if (right_nritems == 0)
3975 return 1;
3976
3977 btrfs_assert_tree_locked(path->nodes[1]);
3978
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003979 left = read_node_slot(fs_info, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003980 /*
3981 * slot - 1 is not valid or we fail to read the left node,
3982 * no big deal, just return.
3983 */
3984 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003985 return 1;
3986
Chris Mason44871b12009-03-13 10:04:31 -04003987 btrfs_tree_lock(left);
3988 btrfs_set_lock_blocking(left);
3989
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003990 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04003991 if (free_space < data_size) {
3992 ret = 1;
3993 goto out;
3994 }
3995
3996 /* cow and double check */
3997 ret = btrfs_cow_block(trans, root, left,
3998 path->nodes[1], slot - 1, &left);
3999 if (ret) {
4000 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004001 if (ret == -ENOSPC)
4002 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004003 goto out;
4004 }
4005
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004006 free_space = btrfs_leaf_free_space(fs_info, left);
Chris Mason44871b12009-03-13 10:04:31 -04004007 if (free_space < data_size) {
4008 ret = 1;
4009 goto out;
4010 }
4011
David Sterba66cb7dd2017-02-10 19:14:36 +01004012 return __push_leaf_left(fs_info, path, min_data_size,
Chris Mason99d8f832010-07-07 10:51:48 -04004013 empty, left, free_space, right_nritems,
4014 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004015out:
4016 btrfs_tree_unlock(left);
4017 free_extent_buffer(left);
4018 return ret;
4019}
4020
4021/*
Chris Mason74123bd2007-02-02 11:05:29 -05004022 * split the path's leaf in two, making sure there is at least data_size
4023 * available for the resulting leaf level of the path.
4024 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004025static noinline void copy_for_split(struct btrfs_trans_handle *trans,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004026 struct btrfs_fs_info *fs_info,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004027 struct btrfs_path *path,
4028 struct extent_buffer *l,
4029 struct extent_buffer *right,
4030 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004031{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004032 int data_copy_size;
4033 int rt_data_off;
4034 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004035 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004036 struct btrfs_map_token token;
4037
4038 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004039
Chris Mason5f39d392007-10-15 16:14:19 -04004040 nritems = nritems - mid;
4041 btrfs_set_header_nritems(right, nritems);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004042 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
Chris Mason5f39d392007-10-15 16:14:19 -04004043
4044 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4045 btrfs_item_nr_offset(mid),
4046 nritems * sizeof(struct btrfs_item));
4047
4048 copy_extent_buffer(right, l,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004049 BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
4050 data_copy_size, BTRFS_LEAF_DATA_OFFSET +
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004051 leaf_data_end(fs_info, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004052
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004053 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004054
4055 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004056 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004057 u32 ioff;
4058
Chris Masoncfed81a2012-03-03 07:40:03 -05004059 ioff = btrfs_token_item_offset(right, item, &token);
4060 btrfs_set_token_item_offset(right, item,
4061 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004062 }
Chris Mason74123bd2007-02-02 11:05:29 -05004063
Chris Mason5f39d392007-10-15 16:14:19 -04004064 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004065 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004066 insert_ptr(trans, fs_info, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004067 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004068
4069 btrfs_mark_buffer_dirty(right);
4070 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004071 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004072
Chris Masonbe0e5c02007-01-26 15:51:26 -05004073 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004074 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004075 free_extent_buffer(path->nodes[0]);
4076 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004077 path->slots[0] -= mid;
4078 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004079 } else {
4080 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004081 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004082 }
Chris Mason5f39d392007-10-15 16:14:19 -04004083
Chris Masoneb60cea2007-02-02 09:18:22 -05004084 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004085}
4086
4087/*
Chris Mason99d8f832010-07-07 10:51:48 -04004088 * double splits happen when we need to insert a big item in the middle
4089 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4090 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4091 * A B C
4092 *
4093 * We avoid this by trying to push the items on either side of our target
4094 * into the adjacent leaves. If all goes well we can avoid the double split
4095 * completely.
4096 */
4097static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4098 struct btrfs_root *root,
4099 struct btrfs_path *path,
4100 int data_size)
4101{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004102 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason99d8f832010-07-07 10:51:48 -04004103 int ret;
4104 int progress = 0;
4105 int slot;
4106 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004107 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004108
4109 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004110 if (slot < btrfs_header_nritems(path->nodes[0]))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004111 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004112
4113 /*
4114 * try to push all the items after our slot into the
4115 * right leaf
4116 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004117 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004118 if (ret < 0)
4119 return ret;
4120
4121 if (ret == 0)
4122 progress++;
4123
4124 nritems = btrfs_header_nritems(path->nodes[0]);
4125 /*
4126 * our goal is to get our slot at the start or end of a leaf. If
4127 * we've done so we're done
4128 */
4129 if (path->slots[0] == 0 || path->slots[0] == nritems)
4130 return 0;
4131
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004132 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004133 return 0;
4134
4135 /* try to push all the items before our slot into the next leaf */
4136 slot = path->slots[0];
Filipe Manana263d3992017-02-17 18:43:57 +00004137 space_needed = data_size;
4138 if (slot > 0)
4139 space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004140 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004141 if (ret < 0)
4142 return ret;
4143
4144 if (ret == 0)
4145 progress++;
4146
4147 if (progress)
4148 return 0;
4149 return 1;
4150}
4151
4152/*
Chris Mason44871b12009-03-13 10:04:31 -04004153 * split the path's leaf in two, making sure there is at least data_size
4154 * available for the resulting leaf level of the path.
4155 *
4156 * returns 0 if all went well and < 0 on failure.
4157 */
4158static noinline int split_leaf(struct btrfs_trans_handle *trans,
4159 struct btrfs_root *root,
Omar Sandoval310712b2017-01-17 23:24:37 -08004160 const struct btrfs_key *ins_key,
Chris Mason44871b12009-03-13 10:04:31 -04004161 struct btrfs_path *path, int data_size,
4162 int extend)
4163{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004164 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004165 struct extent_buffer *l;
4166 u32 nritems;
4167 int mid;
4168 int slot;
4169 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004170 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004171 int ret = 0;
4172 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004173 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004174 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004175 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004176
Yan, Zhenga5719522009-09-24 09:17:31 -04004177 l = path->nodes[0];
4178 slot = path->slots[0];
4179 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004180 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004181 return -EOVERFLOW;
4182
Chris Mason44871b12009-03-13 10:04:31 -04004183 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004184 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004185 int space_needed = data_size;
4186
4187 if (slot < btrfs_header_nritems(l))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004188 space_needed -= btrfs_leaf_free_space(fs_info, l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004189
4190 wret = push_leaf_right(trans, root, path, space_needed,
4191 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004192 if (wret < 0)
4193 return wret;
4194 if (wret) {
Filipe Manana263d3992017-02-17 18:43:57 +00004195 space_needed = data_size;
4196 if (slot > 0)
4197 space_needed -= btrfs_leaf_free_space(fs_info,
4198 l);
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004199 wret = push_leaf_left(trans, root, path, space_needed,
4200 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004201 if (wret < 0)
4202 return wret;
4203 }
4204 l = path->nodes[0];
4205
4206 /* did the pushes work? */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004207 if (btrfs_leaf_free_space(fs_info, l) >= data_size)
Chris Mason44871b12009-03-13 10:04:31 -04004208 return 0;
4209 }
4210
4211 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004212 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004213 if (ret)
4214 return ret;
4215 }
4216again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004217 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004218 l = path->nodes[0];
4219 slot = path->slots[0];
4220 nritems = btrfs_header_nritems(l);
4221 mid = (nritems + 1) / 2;
4222
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004223 if (mid <= slot) {
4224 if (nritems == 1 ||
4225 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004226 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 if (slot >= nritems) {
4228 split = 0;
4229 } else {
4230 mid = slot;
4231 if (mid != nritems &&
4232 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004233 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004234 if (data_size && !tried_avoid_double)
4235 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004236 split = 2;
4237 }
4238 }
4239 }
4240 } else {
4241 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004242 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004243 if (!extend && data_size && slot == 0) {
4244 split = 0;
4245 } else if ((extend || !data_size) && slot == 0) {
4246 mid = 1;
4247 } else {
4248 mid = slot;
4249 if (mid != nritems &&
4250 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004251 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004252 if (data_size && !tried_avoid_double)
4253 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304254 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004255 }
4256 }
4257 }
4258 }
4259
4260 if (split == 0)
4261 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4262 else
4263 btrfs_item_key(l, &disk_key, mid);
4264
David Sterba4d75f8a2014-06-15 01:54:12 +02004265 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4266 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004267 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004268 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004269
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004270 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004271
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004272 if (split == 0) {
4273 if (mid <= slot) {
4274 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004275 insert_ptr(trans, fs_info, path, &disk_key,
4276 right->start, path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004277 btrfs_tree_unlock(path->nodes[0]);
4278 free_extent_buffer(path->nodes[0]);
4279 path->nodes[0] = right;
4280 path->slots[0] = 0;
4281 path->slots[1] += 1;
4282 } else {
4283 btrfs_set_header_nritems(right, 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004284 insert_ptr(trans, fs_info, path, &disk_key,
4285 right->start, path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004286 btrfs_tree_unlock(path->nodes[0]);
4287 free_extent_buffer(path->nodes[0]);
4288 path->nodes[0] = right;
4289 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004290 if (path->slots[1] == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004291 fixup_low_keys(path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004292 }
Liu Bo196e0242016-09-07 14:48:28 -07004293 /*
4294 * We create a new leaf 'right' for the required ins_len and
4295 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4296 * the content of ins_len to 'right'.
4297 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004298 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004299 }
4300
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004301 copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004302
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004304 BUG_ON(num_doubles != 0);
4305 num_doubles++;
4306 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004307 }
Chris Mason44871b12009-03-13 10:04:31 -04004308
Jeff Mahoney143bede2012-03-01 14:56:26 +01004309 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004310
4311push_for_double:
4312 push_for_double_split(trans, root, path, data_size);
4313 tried_avoid_double = 1;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004314 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
Chris Mason99d8f832010-07-07 10:51:48 -04004315 return 0;
4316 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004317}
4318
Yan, Zhengad48fd752009-11-12 09:33:58 +00004319static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4320 struct btrfs_root *root,
4321 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004322{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004323 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004324 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004325 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004326 struct btrfs_file_extent_item *fi;
4327 u64 extent_len = 0;
4328 u32 item_size;
4329 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004330
4331 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004332 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4333
4334 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4335 key.type != BTRFS_EXTENT_CSUM_KEY);
4336
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004337 if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004338 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004339
4340 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004341 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4342 fi = btrfs_item_ptr(leaf, path->slots[0],
4343 struct btrfs_file_extent_item);
4344 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4345 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004346 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004347
Chris Mason459931e2008-12-10 09:10:46 -05004348 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004349 path->search_for_split = 1;
4350 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004351 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004352 if (ret > 0)
4353 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004354 if (ret < 0)
4355 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004356
Yan, Zhengad48fd752009-11-12 09:33:58 +00004357 ret = -EAGAIN;
4358 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004359 /* if our item isn't there, return now */
4360 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004361 goto err;
4362
Chris Mason109f6ae2010-04-02 09:20:18 -04004363 /* the leaf has changed, it now has room. return now */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004364 if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
Chris Mason109f6ae2010-04-02 09:20:18 -04004365 goto err;
4366
Yan, Zhengad48fd752009-11-12 09:33:58 +00004367 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4368 fi = btrfs_item_ptr(leaf, path->slots[0],
4369 struct btrfs_file_extent_item);
4370 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4371 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004372 }
4373
Chris Masonb9473432009-03-13 11:00:37 -04004374 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004375 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004376 if (ret)
4377 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004378
Yan, Zhengad48fd752009-11-12 09:33:58 +00004379 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004380 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004381 return 0;
4382err:
4383 path->keep_locks = 0;
4384 return ret;
4385}
4386
David Sterba4961e292017-02-10 18:49:53 +01004387static noinline int split_item(struct btrfs_fs_info *fs_info,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004388 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004389 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004390 unsigned long split_offset)
4391{
4392 struct extent_buffer *leaf;
4393 struct btrfs_item *item;
4394 struct btrfs_item *new_item;
4395 int slot;
4396 char *buf;
4397 u32 nritems;
4398 u32 item_size;
4399 u32 orig_offset;
4400 struct btrfs_disk_key disk_key;
4401
Chris Masonb9473432009-03-13 11:00:37 -04004402 leaf = path->nodes[0];
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004403 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
Chris Masonb9473432009-03-13 11:00:37 -04004404
Chris Masonb4ce94d2009-02-04 09:25:08 -05004405 btrfs_set_path_blocking(path);
4406
Ross Kirkdd3cc162013-09-16 15:58:09 +01004407 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004408 orig_offset = btrfs_item_offset(leaf, item);
4409 item_size = btrfs_item_size(leaf, item);
4410
Chris Mason459931e2008-12-10 09:10:46 -05004411 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004412 if (!buf)
4413 return -ENOMEM;
4414
Chris Mason459931e2008-12-10 09:10:46 -05004415 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4416 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004417
Chris Mason459931e2008-12-10 09:10:46 -05004418 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004419 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004420 if (slot != nritems) {
4421 /* shift the items */
4422 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004423 btrfs_item_nr_offset(slot),
4424 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004425 }
4426
4427 btrfs_cpu_key_to_disk(&disk_key, new_key);
4428 btrfs_set_item_key(leaf, &disk_key, slot);
4429
Ross Kirkdd3cc162013-09-16 15:58:09 +01004430 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004431
4432 btrfs_set_item_offset(leaf, new_item, orig_offset);
4433 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4434
4435 btrfs_set_item_offset(leaf, item,
4436 orig_offset + item_size - split_offset);
4437 btrfs_set_item_size(leaf, item, split_offset);
4438
4439 btrfs_set_header_nritems(leaf, nritems + 1);
4440
4441 /* write the data for the start of the original item */
4442 write_extent_buffer(leaf, buf,
4443 btrfs_item_ptr_offset(leaf, path->slots[0]),
4444 split_offset);
4445
4446 /* write the data for the new item */
4447 write_extent_buffer(leaf, buf + split_offset,
4448 btrfs_item_ptr_offset(leaf, slot),
4449 item_size - split_offset);
4450 btrfs_mark_buffer_dirty(leaf);
4451
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004452 BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004453 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004454 return 0;
4455}
4456
4457/*
4458 * This function splits a single item into two items,
4459 * giving 'new_key' to the new item and splitting the
4460 * old one at split_offset (from the start of the item).
4461 *
4462 * The path may be released by this operation. After
4463 * the split, the path is pointing to the old item. The
4464 * new item is going to be in the same node as the old one.
4465 *
4466 * Note, the item being split must be smaller enough to live alone on
4467 * a tree block with room for one extra struct btrfs_item
4468 *
4469 * This allows us to split the item in place, keeping a lock on the
4470 * leaf the entire time.
4471 */
4472int btrfs_split_item(struct btrfs_trans_handle *trans,
4473 struct btrfs_root *root,
4474 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004475 const struct btrfs_key *new_key,
Yan, Zhengad48fd752009-11-12 09:33:58 +00004476 unsigned long split_offset)
4477{
4478 int ret;
4479 ret = setup_leaf_for_split(trans, root, path,
4480 sizeof(struct btrfs_item));
4481 if (ret)
4482 return ret;
4483
David Sterba4961e292017-02-10 18:49:53 +01004484 ret = split_item(root->fs_info, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004485 return ret;
4486}
4487
4488/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004489 * This function duplicate a item, giving 'new_key' to the new item.
4490 * It guarantees both items live in the same tree leaf and the new item
4491 * is contiguous with the original item.
4492 *
4493 * This allows us to split file extent in place, keeping a lock on the
4494 * leaf the entire time.
4495 */
4496int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4497 struct btrfs_root *root,
4498 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004499 const struct btrfs_key *new_key)
Yan, Zhengad48fd752009-11-12 09:33:58 +00004500{
4501 struct extent_buffer *leaf;
4502 int ret;
4503 u32 item_size;
4504
4505 leaf = path->nodes[0];
4506 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4507 ret = setup_leaf_for_split(trans, root, path,
4508 item_size + sizeof(struct btrfs_item));
4509 if (ret)
4510 return ret;
4511
4512 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004513 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004514 item_size, item_size +
4515 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004516 leaf = path->nodes[0];
4517 memcpy_extent_buffer(leaf,
4518 btrfs_item_ptr_offset(leaf, path->slots[0]),
4519 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4520 item_size);
4521 return 0;
4522}
4523
4524/*
Chris Masond352ac62008-09-29 15:18:18 -04004525 * make the item pointed to by the path smaller. new_size indicates
4526 * how small to make it, and from_end tells us if we just chop bytes
4527 * off the end of the item or if we shift the item to chop bytes off
4528 * the front.
4529 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004530void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
4531 struct btrfs_path *path, u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004532{
Chris Masonb18c6682007-04-17 13:26:50 -04004533 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004534 struct extent_buffer *leaf;
4535 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004536 u32 nritems;
4537 unsigned int data_end;
4538 unsigned int old_data_start;
4539 unsigned int old_size;
4540 unsigned int size_diff;
4541 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004542 struct btrfs_map_token token;
4543
4544 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004545
Chris Mason5f39d392007-10-15 16:14:19 -04004546 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004547 slot = path->slots[0];
4548
4549 old_size = btrfs_item_size_nr(leaf, slot);
4550 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004551 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004552
Chris Mason5f39d392007-10-15 16:14:19 -04004553 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004554 data_end = leaf_data_end(fs_info, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004555
Chris Mason5f39d392007-10-15 16:14:19 -04004556 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004557
Chris Masonb18c6682007-04-17 13:26:50 -04004558 size_diff = old_size - new_size;
4559
4560 BUG_ON(slot < 0);
4561 BUG_ON(slot >= nritems);
4562
4563 /*
4564 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4565 */
4566 /* first correct the data pointers */
4567 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004568 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004569 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004570
Chris Masoncfed81a2012-03-03 07:40:03 -05004571 ioff = btrfs_token_item_offset(leaf, item, &token);
4572 btrfs_set_token_item_offset(leaf, item,
4573 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004574 }
Chris Masondb945352007-10-15 16:15:53 -04004575
Chris Masonb18c6682007-04-17 13:26:50 -04004576 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004577 if (from_end) {
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004578 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4579 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004580 data_end, old_data_start + new_size - data_end);
4581 } else {
4582 struct btrfs_disk_key disk_key;
4583 u64 offset;
4584
4585 btrfs_item_key(leaf, &disk_key, slot);
4586
4587 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4588 unsigned long ptr;
4589 struct btrfs_file_extent_item *fi;
4590
4591 fi = btrfs_item_ptr(leaf, slot,
4592 struct btrfs_file_extent_item);
4593 fi = (struct btrfs_file_extent_item *)(
4594 (unsigned long)fi - size_diff);
4595
4596 if (btrfs_file_extent_type(leaf, fi) ==
4597 BTRFS_FILE_EXTENT_INLINE) {
4598 ptr = btrfs_item_ptr_offset(leaf, slot);
4599 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004600 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004601 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004602 }
4603 }
4604
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004605 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4606 data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
Chris Mason179e29e2007-11-01 11:28:41 -04004607 data_end, old_data_start - data_end);
4608
4609 offset = btrfs_disk_key_offset(&disk_key);
4610 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4611 btrfs_set_item_key(leaf, &disk_key, slot);
4612 if (slot == 0)
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004613 fixup_low_keys(path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004614 }
Chris Mason5f39d392007-10-15 16:14:19 -04004615
Ross Kirkdd3cc162013-09-16 15:58:09 +01004616 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004617 btrfs_set_item_size(leaf, item, new_size);
4618 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004619
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004620 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004621 btrfs_print_leaf(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004622 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004623 }
Chris Masonb18c6682007-04-17 13:26:50 -04004624}
4625
Chris Masond352ac62008-09-29 15:18:18 -04004626/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004627 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004628 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004629void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004630 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004631{
Chris Mason6567e832007-04-16 09:22:45 -04004632 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004633 struct extent_buffer *leaf;
4634 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004635 u32 nritems;
4636 unsigned int data_end;
4637 unsigned int old_data;
4638 unsigned int old_size;
4639 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004640 struct btrfs_map_token token;
4641
4642 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004643
Chris Mason5f39d392007-10-15 16:14:19 -04004644 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004645
Chris Mason5f39d392007-10-15 16:14:19 -04004646 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004647 data_end = leaf_data_end(fs_info, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004648
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004649 if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004650 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004651 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004652 }
Chris Mason6567e832007-04-16 09:22:45 -04004653 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004654 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004655
4656 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004657 if (slot >= nritems) {
David Sterbaa4f78752017-06-29 18:37:49 +02004658 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004659 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4660 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004661 BUG_ON(1);
4662 }
Chris Mason6567e832007-04-16 09:22:45 -04004663
4664 /*
4665 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4666 */
4667 /* first correct the data pointers */
4668 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004669 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004670 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004671
Chris Masoncfed81a2012-03-03 07:40:03 -05004672 ioff = btrfs_token_item_offset(leaf, item, &token);
4673 btrfs_set_token_item_offset(leaf, item,
4674 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004675 }
Chris Mason5f39d392007-10-15 16:14:19 -04004676
Chris Mason6567e832007-04-16 09:22:45 -04004677 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004678 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4679 data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
Chris Mason6567e832007-04-16 09:22:45 -04004680 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004681
Chris Mason6567e832007-04-16 09:22:45 -04004682 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004683 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004684 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004685 btrfs_set_item_size(leaf, item, old_size + data_size);
4686 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004687
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004688 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004689 btrfs_print_leaf(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004690 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004691 }
Chris Mason6567e832007-04-16 09:22:45 -04004692}
4693
Chris Mason74123bd2007-02-02 11:05:29 -05004694/*
Chris Mason44871b12009-03-13 10:04:31 -04004695 * this is a helper for btrfs_insert_empty_items, the main goal here is
4696 * to save stack depth by doing the bulk of the work in a function
4697 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004698 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004699void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004700 const struct btrfs_key *cpu_key, u32 *data_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004701 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004702{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004703 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004704 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004705 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004706 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004707 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004708 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004709 struct extent_buffer *leaf;
4710 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004711 struct btrfs_map_token token;
4712
Filipe Manana24cdc842014-07-28 19:34:35 +01004713 if (path->slots[0] == 0) {
4714 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004715 fixup_low_keys(path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004716 }
4717 btrfs_unlock_up_safe(path, 1);
4718
Chris Masoncfed81a2012-03-03 07:40:03 -05004719 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004720
Chris Mason5f39d392007-10-15 16:14:19 -04004721 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004722 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004723
Chris Mason5f39d392007-10-15 16:14:19 -04004724 nritems = btrfs_header_nritems(leaf);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004725 data_end = leaf_data_end(fs_info, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004726
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004727 if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
David Sterbaa4f78752017-06-29 18:37:49 +02004728 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004729 btrfs_crit(fs_info, "not enough freespace need %u have %d",
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004730 total_size, btrfs_leaf_free_space(fs_info, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004731 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004732 }
Chris Mason5f39d392007-10-15 16:14:19 -04004733
Chris Masonbe0e5c02007-01-26 15:51:26 -05004734 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004735 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004736
Chris Mason5f39d392007-10-15 16:14:19 -04004737 if (old_data < data_end) {
David Sterbaa4f78752017-06-29 18:37:49 +02004738 btrfs_print_leaf(leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004739 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004740 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004741 BUG_ON(1);
4742 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004743 /*
4744 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4745 */
4746 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004747 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004748 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004749
Jeff Mahoney62e85572016-09-20 10:05:01 -04004750 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004751 ioff = btrfs_token_item_offset(leaf, item, &token);
4752 btrfs_set_token_item_offset(leaf, item,
4753 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004754 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004755 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004756 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004757 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004758 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004759
4760 /* shift the data */
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004761 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
4762 data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004763 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004764 data_end = old_data;
4765 }
Chris Mason5f39d392007-10-15 16:14:19 -04004766
Chris Mason62e27492007-03-15 12:56:47 -04004767 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004768 for (i = 0; i < nr; i++) {
4769 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4770 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004771 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004772 btrfs_set_token_item_offset(leaf, item,
4773 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004774 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004775 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004776 }
Chris Mason44871b12009-03-13 10:04:31 -04004777
Chris Mason9c583092008-01-29 15:15:18 -05004778 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004779 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004780
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004781 if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
David Sterbaa4f78752017-06-29 18:37:49 +02004782 btrfs_print_leaf(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004783 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004784 }
Chris Mason44871b12009-03-13 10:04:31 -04004785}
4786
4787/*
4788 * Given a key and some data, insert items into the tree.
4789 * This does all the path init required, making room in the tree if needed.
4790 */
4791int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4792 struct btrfs_root *root,
4793 struct btrfs_path *path,
Omar Sandoval310712b2017-01-17 23:24:37 -08004794 const struct btrfs_key *cpu_key, u32 *data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004795 int nr)
4796{
Chris Mason44871b12009-03-13 10:04:31 -04004797 int ret = 0;
4798 int slot;
4799 int i;
4800 u32 total_size = 0;
4801 u32 total_data = 0;
4802
4803 for (i = 0; i < nr; i++)
4804 total_data += data_size[i];
4805
4806 total_size = total_data + (nr * sizeof(struct btrfs_item));
4807 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4808 if (ret == 0)
4809 return -EEXIST;
4810 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004811 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004812
Chris Mason44871b12009-03-13 10:04:31 -04004813 slot = path->slots[0];
4814 BUG_ON(slot < 0);
4815
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004816 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004817 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004818 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004819}
4820
4821/*
4822 * Given a key and some data, insert an item into the tree.
4823 * This does all the path init required, making room in the tree if needed.
4824 */
Omar Sandoval310712b2017-01-17 23:24:37 -08004825int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4826 const struct btrfs_key *cpu_key, void *data,
4827 u32 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004828{
4829 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004830 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004831 struct extent_buffer *leaf;
4832 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004833
Chris Mason2c90e5d2007-04-02 10:50:19 -04004834 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004835 if (!path)
4836 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004837 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004838 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004839 leaf = path->nodes[0];
4840 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4841 write_extent_buffer(leaf, data, ptr, data_size);
4842 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004843 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004844 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004845 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004846}
4847
Chris Mason74123bd2007-02-02 11:05:29 -05004848/*
Chris Mason5de08d72007-02-24 06:24:44 -05004849 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004850 *
Chris Masond352ac62008-09-29 15:18:18 -04004851 * the tree should have been previously balanced so the deletion does not
4852 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004853 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004854static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4855 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004856{
Chris Mason5f39d392007-10-15 16:14:19 -04004857 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004858 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004859 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004860
Chris Mason5f39d392007-10-15 16:14:19 -04004861 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004862 if (slot != nritems - 1) {
David Sterbabf1d3422018-03-05 15:47:39 +01004863 if (level) {
4864 ret = tree_mod_log_insert_move(parent, slot, slot + 1,
David Sterbaa446a972018-03-05 15:26:29 +01004865 nritems - slot - 1);
David Sterbabf1d3422018-03-05 15:47:39 +01004866 BUG_ON(ret < 0);
4867 }
Chris Mason5f39d392007-10-15 16:14:19 -04004868 memmove_extent_buffer(parent,
4869 btrfs_node_key_ptr_offset(slot),
4870 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004871 sizeof(struct btrfs_key_ptr) *
4872 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004873 } else if (level) {
David Sterbae09c2ef2018-03-05 15:09:03 +01004874 ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
4875 GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004876 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004877 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004878
Chris Mason7518a232007-03-12 12:01:18 -04004879 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004880 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004881 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004882 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004883 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004884 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004885 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004886 struct btrfs_disk_key disk_key;
4887
4888 btrfs_node_key(parent, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004889 fixup_low_keys(path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004890 }
Chris Masond6025572007-03-30 14:27:56 -04004891 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004892}
4893
Chris Mason74123bd2007-02-02 11:05:29 -05004894/*
Chris Mason323ac952008-10-01 19:05:46 -04004895 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004896 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004897 *
4898 * This deletes the pointer in path->nodes[1] and frees the leaf
4899 * block extent. zero is returned if it all worked out, < 0 otherwise.
4900 *
4901 * The path must have already been setup for deleting the leaf, including
4902 * all the proper balancing. path->nodes[1] must be locked.
4903 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004904static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4905 struct btrfs_root *root,
4906 struct btrfs_path *path,
4907 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004908{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004909 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004910 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004911
Chris Mason4d081c42009-02-04 09:31:28 -05004912 /*
4913 * btrfs_free_extent is expensive, we want to make sure we
4914 * aren't holding any locks when we call it
4915 */
4916 btrfs_unlock_up_safe(path, 0);
4917
Yan, Zhengf0486c62010-05-16 10:46:25 -04004918 root_sub_used(root, leaf->len);
4919
Josef Bacik3083ee22012-03-09 16:01:49 -05004920 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004921 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004922 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004923}
4924/*
Chris Mason74123bd2007-02-02 11:05:29 -05004925 * delete the item at the leaf level in path. If that empties
4926 * the leaf, remove it from the tree
4927 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004928int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4929 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004930{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004931 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004932 struct extent_buffer *leaf;
4933 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004934 u32 last_off;
4935 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004936 int ret = 0;
4937 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004938 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004939 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004940 struct btrfs_map_token token;
4941
4942 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004943
Chris Mason5f39d392007-10-15 16:14:19 -04004944 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004945 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4946
4947 for (i = 0; i < nr; i++)
4948 dsize += btrfs_item_size_nr(leaf, slot + i);
4949
Chris Mason5f39d392007-10-15 16:14:19 -04004950 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004951
Chris Mason85e21ba2008-01-29 15:11:36 -05004952 if (slot + nr != nritems) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004953 int data_end = leaf_data_end(fs_info, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004954
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004955 memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
Chris Masond6025572007-03-30 14:27:56 -04004956 data_end + dsize,
Nikolay Borisov3d9ec8c2017-05-29 09:43:43 +03004957 BTRFS_LEAF_DATA_OFFSET + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004958 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004959
Chris Mason85e21ba2008-01-29 15:11:36 -05004960 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004961 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004962
Ross Kirkdd3cc162013-09-16 15:58:09 +01004963 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004964 ioff = btrfs_token_item_offset(leaf, item, &token);
4965 btrfs_set_token_item_offset(leaf, item,
4966 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004967 }
Chris Masondb945352007-10-15 16:15:53 -04004968
Chris Mason5f39d392007-10-15 16:14:19 -04004969 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004971 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004972 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004973 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004974 btrfs_set_header_nritems(leaf, nritems - nr);
4975 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004976
Chris Mason74123bd2007-02-02 11:05:29 -05004977 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004978 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004979 if (leaf == root->node) {
4980 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004981 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004982 btrfs_set_path_blocking(path);
David Sterba7c302b42017-02-10 18:47:57 +01004983 clean_tree_block(fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004984 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004985 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004986 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004987 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004988 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004989 struct btrfs_disk_key disk_key;
4990
4991 btrfs_item_key(leaf, &disk_key, 0);
Nikolay Borisovb167fa92018-06-20 15:48:47 +03004992 fixup_low_keys(path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004993 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004994
Chris Mason74123bd2007-02-02 11:05:29 -05004995 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004996 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004997 /* push_leaf_left fixes the path.
4998 * make sure the path still points to our leaf
4999 * for possible call to del_ptr below
5000 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005001 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005002 extent_buffer_get(leaf);
5003
Chris Masonb9473432009-03-13 11:00:37 -04005004 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005005 wret = push_leaf_left(trans, root, path, 1, 1,
5006 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005007 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005008 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005009
5010 if (path->nodes[0] == leaf &&
5011 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005012 wret = push_leaf_right(trans, root, path, 1,
5013 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005014 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005015 ret = wret;
5016 }
Chris Mason5f39d392007-10-15 16:14:19 -04005017
5018 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005019 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005020 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005021 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005022 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005023 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005024 /* if we're still in the path, make sure
5025 * we're dirty. Otherwise, one of the
5026 * push_leaf functions must have already
5027 * dirtied this buffer
5028 */
5029 if (path->nodes[0] == leaf)
5030 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005031 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005032 }
Chris Masond5719762007-03-23 10:01:08 -04005033 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005034 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005035 }
5036 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005037 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005038}
5039
Chris Mason97571fd2007-02-24 13:39:08 -05005040/*
Chris Mason925baed2008-06-25 16:01:30 -04005041 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005042 * returns 0 if it found something or 1 if there are no lesser leaves.
5043 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005044 *
5045 * This may release the path, and so you may lose any locks held at the
5046 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005047 */
Josef Bacik16e75492013-10-22 12:18:51 -04005048int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005049{
Chris Mason925baed2008-06-25 16:01:30 -04005050 struct btrfs_key key;
5051 struct btrfs_disk_key found_key;
5052 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005053
Chris Mason925baed2008-06-25 16:01:30 -04005054 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005055
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005056 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005057 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005058 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005059 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005060 key.offset = (u64)-1;
5061 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005062 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005063 key.type = (u8)-1;
5064 key.offset = (u64)-1;
5065 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005066 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005067 }
Chris Mason7bb86312007-12-11 09:25:06 -05005068
David Sterbab3b4aa72011-04-21 01:20:15 +02005069 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005070 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5071 if (ret < 0)
5072 return ret;
5073 btrfs_item_key(path->nodes[0], &found_key, 0);
5074 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005075 /*
5076 * We might have had an item with the previous key in the tree right
5077 * before we released our path. And after we released our path, that
5078 * item might have been pushed to the first slot (0) of the leaf we
5079 * were holding due to a tree balance. Alternatively, an item with the
5080 * previous key can exist as the only element of a leaf (big fat item).
5081 * Therefore account for these 2 cases, so that our callers (like
5082 * btrfs_previous_item) don't miss an existing item with a key matching
5083 * the previous key we computed above.
5084 */
5085 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005086 return 0;
5087 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005088}
5089
Chris Mason3f157a22008-06-25 16:01:31 -04005090/*
5091 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005092 * for nodes or leaves that are have a minimum transaction id.
5093 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005094 *
5095 * This does not cow, but it does stuff the starting key it finds back
5096 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5097 * key and get a writable path.
5098 *
Chris Mason3f157a22008-06-25 16:01:31 -04005099 * This honors path->lowest_level to prevent descent past a given level
5100 * of the tree.
5101 *
Chris Masond352ac62008-09-29 15:18:18 -04005102 * min_trans indicates the oldest transaction that you are interested
5103 * in walking through. Any nodes or leaves older than min_trans are
5104 * skipped over (without reading them).
5105 *
Chris Mason3f157a22008-06-25 16:01:31 -04005106 * returns zero if something useful was found, < 0 on error and 1 if there
5107 * was nothing in the tree that matched the search criteria.
5108 */
5109int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005110 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005111 u64 min_trans)
5112{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005113 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason3f157a22008-06-25 16:01:31 -04005114 struct extent_buffer *cur;
5115 struct btrfs_key found_key;
5116 int slot;
Yan96524802008-07-24 12:19:49 -04005117 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005118 u32 nritems;
5119 int level;
5120 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005121 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005122
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005123 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005124again:
Chris Masonbd681512011-07-16 15:23:14 -04005125 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005126 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005127 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005128 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005129 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005130
5131 if (btrfs_header_generation(cur) < min_trans) {
5132 ret = 1;
5133 goto out;
5134 }
Chris Masond3977122009-01-05 21:25:51 -05005135 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005136 nritems = btrfs_header_nritems(cur);
5137 level = btrfs_header_level(cur);
Nikolay Borisova74b35e2017-12-08 16:27:43 +02005138 sret = btrfs_bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005139
Chris Mason323ac952008-10-01 19:05:46 -04005140 /* at the lowest level, we're done, setup the path and exit */
5141 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005142 if (slot >= nritems)
5143 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005144 ret = 0;
5145 path->slots[level] = slot;
5146 btrfs_item_key_to_cpu(cur, &found_key, slot);
5147 goto out;
5148 }
Yan96524802008-07-24 12:19:49 -04005149 if (sret && slot > 0)
5150 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005151 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005152 * check this node pointer against the min_trans parameters.
5153 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005154 */
Chris Masond3977122009-01-05 21:25:51 -05005155 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005156 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005157
Chris Mason3f157a22008-06-25 16:01:31 -04005158 gen = btrfs_node_ptr_generation(cur, slot);
5159 if (gen < min_trans) {
5160 slot++;
5161 continue;
5162 }
Eric Sandeende78b512013-01-31 18:21:12 +00005163 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005164 }
Chris Masone02119d2008-09-05 16:13:11 -04005165find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005166 /*
5167 * we didn't find a candidate key in this node, walk forward
5168 * and find another one
5169 */
5170 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005171 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005172 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005173 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005174 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005175 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005176 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005177 goto again;
5178 } else {
5179 goto out;
5180 }
5181 }
5182 /* save our key for returning back */
5183 btrfs_node_key_to_cpu(cur, &found_key, slot);
5184 path->slots[level] = slot;
5185 if (level == path->lowest_level) {
5186 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005187 goto out;
5188 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005189 btrfs_set_path_blocking(path);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005190 cur = read_node_slot(fs_info, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005191 if (IS_ERR(cur)) {
5192 ret = PTR_ERR(cur);
5193 goto out;
5194 }
Chris Mason3f157a22008-06-25 16:01:31 -04005195
Chris Masonbd681512011-07-16 15:23:14 -04005196 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005197
Chris Masonbd681512011-07-16 15:23:14 -04005198 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005199 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005200 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005201 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005202 }
5203out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005204 path->keep_locks = keep_locks;
5205 if (ret == 0) {
5206 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5207 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005208 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005209 }
Chris Mason3f157a22008-06-25 16:01:31 -04005210 return ret;
5211}
5212
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005213static int tree_move_down(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005214 struct btrfs_path *path,
David Sterbaab6a43e2017-02-10 19:24:53 +01005215 int *level)
Alexander Block70698302012-06-05 21:07:48 +02005216{
Liu Bofb770ae2016-07-05 12:10:14 -07005217 struct extent_buffer *eb;
5218
Chris Mason74dd17f2012-08-07 16:25:13 -04005219 BUG_ON(*level == 0);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005220 eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
Liu Bofb770ae2016-07-05 12:10:14 -07005221 if (IS_ERR(eb))
5222 return PTR_ERR(eb);
5223
5224 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005225 path->slots[*level - 1] = 0;
5226 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005227 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005228}
5229
David Sterbaf1e30262017-02-10 19:25:51 +01005230static int tree_move_next_or_upnext(struct btrfs_path *path,
Alexander Block70698302012-06-05 21:07:48 +02005231 int *level, int root_level)
5232{
5233 int ret = 0;
5234 int nritems;
5235 nritems = btrfs_header_nritems(path->nodes[*level]);
5236
5237 path->slots[*level]++;
5238
Chris Mason74dd17f2012-08-07 16:25:13 -04005239 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005240 if (*level == root_level)
5241 return -1;
5242
5243 /* move upnext */
5244 path->slots[*level] = 0;
5245 free_extent_buffer(path->nodes[*level]);
5246 path->nodes[*level] = NULL;
5247 (*level)++;
5248 path->slots[*level]++;
5249
5250 nritems = btrfs_header_nritems(path->nodes[*level]);
5251 ret = 1;
5252 }
5253 return ret;
5254}
5255
5256/*
5257 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5258 * or down.
5259 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005260static int tree_advance(struct btrfs_fs_info *fs_info,
Alexander Block70698302012-06-05 21:07:48 +02005261 struct btrfs_path *path,
5262 int *level, int root_level,
5263 int allow_down,
5264 struct btrfs_key *key)
5265{
5266 int ret;
5267
5268 if (*level == 0 || !allow_down) {
David Sterbaf1e30262017-02-10 19:25:51 +01005269 ret = tree_move_next_or_upnext(path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005270 } else {
David Sterbaab6a43e2017-02-10 19:24:53 +01005271 ret = tree_move_down(fs_info, path, level);
Alexander Block70698302012-06-05 21:07:48 +02005272 }
5273 if (ret >= 0) {
5274 if (*level == 0)
5275 btrfs_item_key_to_cpu(path->nodes[*level], key,
5276 path->slots[*level]);
5277 else
5278 btrfs_node_key_to_cpu(path->nodes[*level], key,
5279 path->slots[*level]);
5280 }
5281 return ret;
5282}
5283
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005284static int tree_compare_item(struct btrfs_path *left_path,
Alexander Block70698302012-06-05 21:07:48 +02005285 struct btrfs_path *right_path,
5286 char *tmp_buf)
5287{
5288 int cmp;
5289 int len1, len2;
5290 unsigned long off1, off2;
5291
5292 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5293 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5294 if (len1 != len2)
5295 return 1;
5296
5297 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5298 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5299 right_path->slots[0]);
5300
5301 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5302
5303 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5304 if (cmp)
5305 return 1;
5306 return 0;
5307}
5308
5309#define ADVANCE 1
5310#define ADVANCE_ONLY_NEXT -1
5311
5312/*
5313 * This function compares two trees and calls the provided callback for
5314 * every changed/new/deleted item it finds.
5315 * If shared tree blocks are encountered, whole subtrees are skipped, making
5316 * the compare pretty fast on snapshotted subvolumes.
5317 *
5318 * This currently works on commit roots only. As commit roots are read only,
5319 * we don't do any locking. The commit roots are protected with transactions.
5320 * Transactions are ended and rejoined when a commit is tried in between.
5321 *
5322 * This function checks for modifications done to the trees while comparing.
5323 * If it detects a change, it aborts immediately.
5324 */
5325int btrfs_compare_trees(struct btrfs_root *left_root,
5326 struct btrfs_root *right_root,
5327 btrfs_changed_cb_t changed_cb, void *ctx)
5328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005329 struct btrfs_fs_info *fs_info = left_root->fs_info;
Alexander Block70698302012-06-05 21:07:48 +02005330 int ret;
5331 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005332 struct btrfs_path *left_path = NULL;
5333 struct btrfs_path *right_path = NULL;
5334 struct btrfs_key left_key;
5335 struct btrfs_key right_key;
5336 char *tmp_buf = NULL;
5337 int left_root_level;
5338 int right_root_level;
5339 int left_level;
5340 int right_level;
5341 int left_end_reached;
5342 int right_end_reached;
5343 int advance_left;
5344 int advance_right;
5345 u64 left_blockptr;
5346 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005347 u64 left_gen;
5348 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005349
5350 left_path = btrfs_alloc_path();
5351 if (!left_path) {
5352 ret = -ENOMEM;
5353 goto out;
5354 }
5355 right_path = btrfs_alloc_path();
5356 if (!right_path) {
5357 ret = -ENOMEM;
5358 goto out;
5359 }
5360
Michal Hocko752ade62017-05-08 15:57:27 -07005361 tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
Alexander Block70698302012-06-05 21:07:48 +02005362 if (!tmp_buf) {
Michal Hocko752ade62017-05-08 15:57:27 -07005363 ret = -ENOMEM;
5364 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005365 }
5366
5367 left_path->search_commit_root = 1;
5368 left_path->skip_locking = 1;
5369 right_path->search_commit_root = 1;
5370 right_path->skip_locking = 1;
5371
Alexander Block70698302012-06-05 21:07:48 +02005372 /*
5373 * Strategy: Go to the first items of both trees. Then do
5374 *
5375 * If both trees are at level 0
5376 * Compare keys of current items
5377 * If left < right treat left item as new, advance left tree
5378 * and repeat
5379 * If left > right treat right item as deleted, advance right tree
5380 * and repeat
5381 * If left == right do deep compare of items, treat as changed if
5382 * needed, advance both trees and repeat
5383 * If both trees are at the same level but not at level 0
5384 * Compare keys of current nodes/leafs
5385 * If left < right advance left tree and repeat
5386 * If left > right advance right tree and repeat
5387 * If left == right compare blockptrs of the next nodes/leafs
5388 * If they match advance both trees but stay at the same level
5389 * and repeat
5390 * If they don't match advance both trees while allowing to go
5391 * deeper and repeat
5392 * If tree levels are different
5393 * Advance the tree that needs it and repeat
5394 *
5395 * Advancing a tree means:
5396 * If we are at level 0, try to go to the next slot. If that's not
5397 * possible, go one level up and repeat. Stop when we found a level
5398 * where we could go to the next slot. We may at this point be on a
5399 * node or a leaf.
5400 *
5401 * If we are not at level 0 and not on shared tree blocks, go one
5402 * level deeper.
5403 *
5404 * If we are not at level 0 and on shared tree blocks, go one slot to
5405 * the right if possible or go up and right.
5406 */
5407
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005408 down_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005409 left_level = btrfs_header_level(left_root->commit_root);
5410 left_root_level = left_level;
Robbie Ko6f2f0b32018-05-14 10:51:34 +08005411 left_path->nodes[left_level] =
5412 btrfs_clone_extent_buffer(left_root->commit_root);
5413 if (!left_path->nodes[left_level]) {
5414 up_read(&fs_info->commit_root_sem);
5415 ret = -ENOMEM;
5416 goto out;
5417 }
Alexander Block70698302012-06-05 21:07:48 +02005418 extent_buffer_get(left_path->nodes[left_level]);
5419
5420 right_level = btrfs_header_level(right_root->commit_root);
5421 right_root_level = right_level;
Robbie Ko6f2f0b32018-05-14 10:51:34 +08005422 right_path->nodes[right_level] =
5423 btrfs_clone_extent_buffer(right_root->commit_root);
5424 if (!right_path->nodes[right_level]) {
5425 up_read(&fs_info->commit_root_sem);
5426 ret = -ENOMEM;
5427 goto out;
5428 }
Alexander Block70698302012-06-05 21:07:48 +02005429 extent_buffer_get(right_path->nodes[right_level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005430 up_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005431
5432 if (left_level == 0)
5433 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5434 &left_key, left_path->slots[left_level]);
5435 else
5436 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5437 &left_key, left_path->slots[left_level]);
5438 if (right_level == 0)
5439 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5440 &right_key, right_path->slots[right_level]);
5441 else
5442 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5443 &right_key, right_path->slots[right_level]);
5444
5445 left_end_reached = right_end_reached = 0;
5446 advance_left = advance_right = 0;
5447
5448 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005449 if (advance_left && !left_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005450 ret = tree_advance(fs_info, left_path, &left_level,
Alexander Block70698302012-06-05 21:07:48 +02005451 left_root_level,
5452 advance_left != ADVANCE_ONLY_NEXT,
5453 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005454 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005455 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005456 else if (ret < 0)
5457 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005458 advance_left = 0;
5459 }
5460 if (advance_right && !right_end_reached) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005461 ret = tree_advance(fs_info, right_path, &right_level,
Alexander Block70698302012-06-05 21:07:48 +02005462 right_root_level,
5463 advance_right != ADVANCE_ONLY_NEXT,
5464 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005465 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005466 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005467 else if (ret < 0)
5468 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005469 advance_right = 0;
5470 }
5471
5472 if (left_end_reached && right_end_reached) {
5473 ret = 0;
5474 goto out;
5475 } else if (left_end_reached) {
5476 if (right_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005477 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005478 &right_key,
5479 BTRFS_COMPARE_TREE_DELETED,
5480 ctx);
5481 if (ret < 0)
5482 goto out;
5483 }
5484 advance_right = ADVANCE;
5485 continue;
5486 } else if (right_end_reached) {
5487 if (left_level == 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005488 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005489 &left_key,
5490 BTRFS_COMPARE_TREE_NEW,
5491 ctx);
5492 if (ret < 0)
5493 goto out;
5494 }
5495 advance_left = ADVANCE;
5496 continue;
5497 }
5498
5499 if (left_level == 0 && right_level == 0) {
5500 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5501 if (cmp < 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005502 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005503 &left_key,
5504 BTRFS_COMPARE_TREE_NEW,
5505 ctx);
5506 if (ret < 0)
5507 goto out;
5508 advance_left = ADVANCE;
5509 } else if (cmp > 0) {
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005510 ret = changed_cb(left_path, right_path,
Alexander Block70698302012-06-05 21:07:48 +02005511 &right_key,
5512 BTRFS_COMPARE_TREE_DELETED,
5513 ctx);
5514 if (ret < 0)
5515 goto out;
5516 advance_right = ADVANCE;
5517 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005518 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005519
Chris Mason74dd17f2012-08-07 16:25:13 -04005520 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005521 ret = tree_compare_item(left_path, right_path,
5522 tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005523 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005524 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005525 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005526 result = BTRFS_COMPARE_TREE_SAME;
Nikolay Borisovee8c4942017-08-21 12:43:45 +03005527 ret = changed_cb(left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005528 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005529 if (ret < 0)
5530 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005531 advance_left = ADVANCE;
5532 advance_right = ADVANCE;
5533 }
5534 } else if (left_level == right_level) {
5535 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5536 if (cmp < 0) {
5537 advance_left = ADVANCE;
5538 } else if (cmp > 0) {
5539 advance_right = ADVANCE;
5540 } else {
5541 left_blockptr = btrfs_node_blockptr(
5542 left_path->nodes[left_level],
5543 left_path->slots[left_level]);
5544 right_blockptr = btrfs_node_blockptr(
5545 right_path->nodes[right_level],
5546 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005547 left_gen = btrfs_node_ptr_generation(
5548 left_path->nodes[left_level],
5549 left_path->slots[left_level]);
5550 right_gen = btrfs_node_ptr_generation(
5551 right_path->nodes[right_level],
5552 right_path->slots[right_level]);
5553 if (left_blockptr == right_blockptr &&
5554 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005555 /*
5556 * As we're on a shared block, don't
5557 * allow to go deeper.
5558 */
5559 advance_left = ADVANCE_ONLY_NEXT;
5560 advance_right = ADVANCE_ONLY_NEXT;
5561 } else {
5562 advance_left = ADVANCE;
5563 advance_right = ADVANCE;
5564 }
5565 }
5566 } else if (left_level < right_level) {
5567 advance_right = ADVANCE;
5568 } else {
5569 advance_left = ADVANCE;
5570 }
5571 }
5572
5573out:
5574 btrfs_free_path(left_path);
5575 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005576 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005577 return ret;
5578}
5579
Chris Mason3f157a22008-06-25 16:01:31 -04005580/*
5581 * this is similar to btrfs_next_leaf, but does not try to preserve
5582 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005583 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005584 *
5585 * 0 is returned if another key is found, < 0 if there are any errors
5586 * and 1 is returned if there are no higher keys in the tree
5587 *
5588 * path->keep_locks should be set to 1 on the search made before
5589 * calling this function.
5590 */
Chris Masone7a84562008-06-25 16:01:31 -04005591int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005592 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005593{
Chris Masone7a84562008-06-25 16:01:31 -04005594 int slot;
5595 struct extent_buffer *c;
5596
Chris Mason934d3752008-12-08 16:43:10 -05005597 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005598 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005599 if (!path->nodes[level])
5600 return 1;
5601
5602 slot = path->slots[level] + 1;
5603 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005604next:
Chris Masone7a84562008-06-25 16:01:31 -04005605 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005606 int ret;
5607 int orig_lowest;
5608 struct btrfs_key cur_key;
5609 if (level + 1 >= BTRFS_MAX_LEVEL ||
5610 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005611 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005612
5613 if (path->locks[level + 1]) {
5614 level++;
5615 continue;
5616 }
5617
5618 slot = btrfs_header_nritems(c) - 1;
5619 if (level == 0)
5620 btrfs_item_key_to_cpu(c, &cur_key, slot);
5621 else
5622 btrfs_node_key_to_cpu(c, &cur_key, slot);
5623
5624 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005625 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005626 path->lowest_level = level;
5627 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5628 0, 0);
5629 path->lowest_level = orig_lowest;
5630 if (ret < 0)
5631 return ret;
5632
5633 c = path->nodes[level];
5634 slot = path->slots[level];
5635 if (ret == 0)
5636 slot++;
5637 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005638 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005639
Chris Masone7a84562008-06-25 16:01:31 -04005640 if (level == 0)
5641 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005642 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005643 u64 gen = btrfs_node_ptr_generation(c, slot);
5644
Chris Mason3f157a22008-06-25 16:01:31 -04005645 if (gen < min_trans) {
5646 slot++;
5647 goto next;
5648 }
Chris Masone7a84562008-06-25 16:01:31 -04005649 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005650 }
Chris Masone7a84562008-06-25 16:01:31 -04005651 return 0;
5652 }
5653 return 1;
5654}
5655
Chris Mason7bb86312007-12-11 09:25:06 -05005656/*
Chris Mason925baed2008-06-25 16:01:30 -04005657 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005658 * returns 0 if it found something or 1 if there are no greater leaves.
5659 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005660 */
Chris Mason234b63a2007-03-13 10:46:10 -04005661int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005662{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005663 return btrfs_next_old_leaf(root, path, 0);
5664}
5665
5666int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5667 u64 time_seq)
5668{
Chris Masond97e63b2007-02-20 16:40:44 -05005669 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005670 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005671 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005672 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005673 struct btrfs_key key;
5674 u32 nritems;
5675 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005676 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005677 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005678
5679 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005680 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005681 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005682
Chris Mason8e73f272009-04-03 10:14:18 -04005683 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5684again:
5685 level = 1;
5686 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005687 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005688 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005689
Chris Masona2135012008-06-25 16:01:30 -04005690 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005691 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005692
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005693 if (time_seq)
5694 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5695 else
5696 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005697 path->keep_locks = 0;
5698
5699 if (ret < 0)
5700 return ret;
5701
Chris Masona2135012008-06-25 16:01:30 -04005702 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005703 /*
5704 * by releasing the path above we dropped all our locks. A balance
5705 * could have added more items next to the key that used to be
5706 * at the very end of the block. So, check again here and
5707 * advance the path if there are now more items available.
5708 */
Chris Masona2135012008-06-25 16:01:30 -04005709 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005710 if (ret == 0)
5711 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005712 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005713 goto done;
5714 }
Liu Bo0b43e042014-06-09 11:04:49 +08005715 /*
5716 * So the above check misses one case:
5717 * - after releasing the path above, someone has removed the item that
5718 * used to be at the very end of the block, and balance between leafs
5719 * gets another one with bigger key.offset to replace it.
5720 *
5721 * This one should be returned as well, or we can get leaf corruption
5722 * later(esp. in __btrfs_drop_extents()).
5723 *
5724 * And a bit more explanation about this check,
5725 * with ret > 0, the key isn't found, the path points to the slot
5726 * where it should be inserted, so the path->slots[0] item must be the
5727 * bigger one.
5728 */
5729 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5730 ret = 0;
5731 goto done;
5732 }
Chris Masond97e63b2007-02-20 16:40:44 -05005733
Chris Masond3977122009-01-05 21:25:51 -05005734 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005735 if (!path->nodes[level]) {
5736 ret = 1;
5737 goto done;
5738 }
Chris Mason5f39d392007-10-15 16:14:19 -04005739
Chris Masond97e63b2007-02-20 16:40:44 -05005740 slot = path->slots[level] + 1;
5741 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005742 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005743 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005744 if (level == BTRFS_MAX_LEVEL) {
5745 ret = 1;
5746 goto done;
5747 }
Chris Masond97e63b2007-02-20 16:40:44 -05005748 continue;
5749 }
Chris Mason5f39d392007-10-15 16:14:19 -04005750
Chris Mason925baed2008-06-25 16:01:30 -04005751 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005752 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005753 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005754 }
Chris Mason5f39d392007-10-15 16:14:19 -04005755
Chris Mason8e73f272009-04-03 10:14:18 -04005756 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005757 next_rw_lock = path->locks[level];
Liu Bod07b8522017-01-30 12:23:42 -08005758 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005759 slot, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005760 if (ret == -EAGAIN)
5761 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005762
Chris Mason76a05b32009-05-14 13:24:30 -04005763 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005764 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005765 goto done;
5766 }
5767
Chris Mason5cd57b22008-06-25 16:01:30 -04005768 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005769 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005770 if (!ret && time_seq) {
5771 /*
5772 * If we don't get the lock, we may be racing
5773 * with push_leaf_left, holding that lock while
5774 * itself waiting for the leaf we've currently
5775 * locked. To solve this situation, we give up
5776 * on our lock and cycle.
5777 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005778 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005779 btrfs_release_path(path);
5780 cond_resched();
5781 goto again;
5782 }
Chris Mason8e73f272009-04-03 10:14:18 -04005783 if (!ret) {
5784 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005785 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005786 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005787 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005788 }
Chris Mason31533fb2011-07-26 16:01:59 -04005789 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005790 }
Chris Masond97e63b2007-02-20 16:40:44 -05005791 break;
5792 }
5793 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005794 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005795 level--;
5796 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005797 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005798 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005799
Chris Mason5f39d392007-10-15 16:14:19 -04005800 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005801 path->nodes[level] = next;
5802 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005803 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005804 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005805 if (!level)
5806 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005807
Liu Bod07b8522017-01-30 12:23:42 -08005808 ret = read_block_for_search(root, path, &next, level,
David Sterbacda79c52017-02-10 18:44:32 +01005809 0, &key);
Chris Mason8e73f272009-04-03 10:14:18 -04005810 if (ret == -EAGAIN)
5811 goto again;
5812
Chris Mason76a05b32009-05-14 13:24:30 -04005813 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005814 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005815 goto done;
5816 }
5817
Chris Mason5cd57b22008-06-25 16:01:30 -04005818 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005819 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005820 if (!ret) {
5821 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005822 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005823 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005824 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005825 }
Chris Mason31533fb2011-07-26 16:01:59 -04005826 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005827 }
Chris Masond97e63b2007-02-20 16:40:44 -05005828 }
Chris Mason8e73f272009-04-03 10:14:18 -04005829 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005830done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005831 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005832 path->leave_spinning = old_spinning;
5833 if (!old_spinning)
5834 btrfs_set_path_blocking(path);
5835
5836 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005837}
Chris Mason0b86a832008-03-24 15:01:56 -04005838
Chris Mason3f157a22008-06-25 16:01:31 -04005839/*
5840 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5841 * searching until it gets past min_objectid or finds an item of 'type'
5842 *
5843 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5844 */
Chris Mason0b86a832008-03-24 15:01:56 -04005845int btrfs_previous_item(struct btrfs_root *root,
5846 struct btrfs_path *path, u64 min_objectid,
5847 int type)
5848{
5849 struct btrfs_key found_key;
5850 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005851 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005852 int ret;
5853
Chris Masond3977122009-01-05 21:25:51 -05005854 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005855 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005856 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005857 ret = btrfs_prev_leaf(root, path);
5858 if (ret != 0)
5859 return ret;
5860 } else {
5861 path->slots[0]--;
5862 }
5863 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005864 nritems = btrfs_header_nritems(leaf);
5865 if (nritems == 0)
5866 return 1;
5867 if (path->slots[0] == nritems)
5868 path->slots[0]--;
5869
Chris Mason0b86a832008-03-24 15:01:56 -04005870 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005871 if (found_key.objectid < min_objectid)
5872 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005873 if (found_key.type == type)
5874 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005875 if (found_key.objectid == min_objectid &&
5876 found_key.type < type)
5877 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005878 }
5879 return 1;
5880}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005881
5882/*
5883 * search in extent tree to find a previous Metadata/Data extent item with
5884 * min objecitd.
5885 *
5886 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5887 */
5888int btrfs_previous_extent_item(struct btrfs_root *root,
5889 struct btrfs_path *path, u64 min_objectid)
5890{
5891 struct btrfs_key found_key;
5892 struct extent_buffer *leaf;
5893 u32 nritems;
5894 int ret;
5895
5896 while (1) {
5897 if (path->slots[0] == 0) {
5898 btrfs_set_path_blocking(path);
5899 ret = btrfs_prev_leaf(root, path);
5900 if (ret != 0)
5901 return ret;
5902 } else {
5903 path->slots[0]--;
5904 }
5905 leaf = path->nodes[0];
5906 nritems = btrfs_header_nritems(leaf);
5907 if (nritems == 0)
5908 return 1;
5909 if (path->slots[0] == nritems)
5910 path->slots[0]--;
5911
5912 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5913 if (found_key.objectid < min_objectid)
5914 break;
5915 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5916 found_key.type == BTRFS_METADATA_ITEM_KEY)
5917 return 0;
5918 if (found_key.objectid == min_objectid &&
5919 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5920 break;
5921 }
5922 return 1;
5923}