blob: b5ebb43b1824fd1275893a00a584565731716158 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
David Sterba8f282f72016-03-30 16:01:12 +020022#include <linux/vmalloc.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050023#include "ctree.h"
24#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040025#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040026#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040027#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050028
Chris Masone089f052007-03-16 16:20:31 -040029static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
30 *root, struct btrfs_path *path, int level);
31static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040032 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040033 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040034static int push_node_left(struct btrfs_trans_handle *trans,
35 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040036 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040037static int balance_node_right(struct btrfs_trans_handle *trans,
38 struct btrfs_root *root,
39 struct extent_buffer *dst_buf,
40 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000041static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
42 int level, int slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +000043static int tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
Jan Schmidtf2304752012-05-26 11:43:17 +020044 struct extent_buffer *eb);
Chris Masond97e63b2007-02-20 16:40:44 -050045
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Masahiro Yamadae2c89902016-09-13 04:35:52 +090048 return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Mason2c90e5d2007-04-02 10:50:19 -040049}
50
Chris Masonb4ce94d2009-02-04 09:25:08 -050051/*
52 * set all locked nodes in the path to blocking locks. This should
53 * be done before scheduling
54 */
55noinline void btrfs_set_path_blocking(struct btrfs_path *p)
56{
57 int i;
58 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040059 if (!p->nodes[i] || !p->locks[i])
60 continue;
61 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
62 if (p->locks[i] == BTRFS_READ_LOCK)
63 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
64 else if (p->locks[i] == BTRFS_WRITE_LOCK)
65 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050066 }
67}
68
69/*
70 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050071 *
72 * held is used to keep lockdep happy, when lockdep is enabled
73 * we set held to a blocking lock before we go around and
74 * retake all the spinlocks in the path. You can safely use NULL
75 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050076 */
Chris Mason4008c042009-02-12 14:09:45 -050077noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040078 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050079{
80 int i;
Chris Mason4008c042009-02-12 14:09:45 -050081
Chris Masonbd681512011-07-16 15:23:14 -040082 if (held) {
83 btrfs_set_lock_blocking_rw(held, held_rw);
84 if (held_rw == BTRFS_WRITE_LOCK)
85 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
86 else if (held_rw == BTRFS_READ_LOCK)
87 held_rw = BTRFS_READ_LOCK_BLOCKING;
88 }
Chris Mason4008c042009-02-12 14:09:45 -050089 btrfs_set_path_blocking(p);
Chris Mason4008c042009-02-12 14:09:45 -050090
91 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040092 if (p->nodes[i] && p->locks[i]) {
93 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
94 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
95 p->locks[i] = BTRFS_WRITE_LOCK;
96 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
97 p->locks[i] = BTRFS_READ_LOCK;
98 }
Chris Masonb4ce94d2009-02-04 09:25:08 -050099 }
Chris Mason4008c042009-02-12 14:09:45 -0500100
Chris Mason4008c042009-02-12 14:09:45 -0500101 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400102 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Masonb4ce94d2009-02-04 09:25:08 -0500103}
104
Chris Masond352ac62008-09-29 15:18:18 -0400105/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400106void btrfs_free_path(struct btrfs_path *p)
107{
Jesper Juhlff175d52010-12-25 21:22:30 +0000108 if (!p)
109 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200110 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400111 kmem_cache_free(btrfs_path_cachep, p);
112}
113
Chris Masond352ac62008-09-29 15:18:18 -0400114/*
115 * path release drops references on the extent buffers in the path
116 * and it drops any locks held by this path
117 *
118 * It is safe to call this on paths that no locks or extent buffers held.
119 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200120noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500121{
122 int i;
Chris Masona2135012008-06-25 16:01:30 -0400123
Chris Mason234b63a2007-03-13 10:46:10 -0400124 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400125 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500126 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400127 continue;
128 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400129 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400130 p->locks[i] = 0;
131 }
Chris Mason5f39d392007-10-15 16:14:19 -0400132 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400133 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500134 }
135}
136
Chris Masond352ac62008-09-29 15:18:18 -0400137/*
138 * safely gets a reference on the root node of a tree. A lock
139 * is not taken, so a concurrent writer may put a different node
140 * at the root of the tree. See btrfs_lock_root_node for the
141 * looping required.
142 *
143 * The extent buffer returned by this has a reference taken, so
144 * it won't disappear. It may stop being the root of the tree
145 * at any time because there are no locks held.
146 */
Chris Mason925baed2008-06-25 16:01:30 -0400147struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
148{
149 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400150
Josef Bacik3083ee22012-03-09 16:01:49 -0500151 while (1) {
152 rcu_read_lock();
153 eb = rcu_dereference(root->node);
154
155 /*
156 * RCU really hurts here, we could free up the root node because
Nicholas D Steeves01327612016-05-19 21:18:45 -0400157 * it was COWed but we may not get the new root node yet so do
Josef Bacik3083ee22012-03-09 16:01:49 -0500158 * the inc_not_zero dance and if it doesn't work then
159 * synchronize_rcu and try again.
160 */
161 if (atomic_inc_not_zero(&eb->refs)) {
162 rcu_read_unlock();
163 break;
164 }
165 rcu_read_unlock();
166 synchronize_rcu();
167 }
Chris Mason925baed2008-06-25 16:01:30 -0400168 return eb;
169}
170
Chris Masond352ac62008-09-29 15:18:18 -0400171/* loop around taking references on and locking the root node of the
172 * tree until you end up with a lock on the root. A locked buffer
173 * is returned, with a reference held.
174 */
Chris Mason925baed2008-06-25 16:01:30 -0400175struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
176{
177 struct extent_buffer *eb;
178
Chris Masond3977122009-01-05 21:25:51 -0500179 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400180 eb = btrfs_root_node(root);
181 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400182 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400183 break;
Chris Mason925baed2008-06-25 16:01:30 -0400184 btrfs_tree_unlock(eb);
185 free_extent_buffer(eb);
186 }
187 return eb;
188}
189
Chris Masonbd681512011-07-16 15:23:14 -0400190/* loop around taking references on and locking the root node of the
191 * tree until you end up with a lock on the root. A locked buffer
192 * is returned, with a reference held.
193 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000194static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400195{
196 struct extent_buffer *eb;
197
198 while (1) {
199 eb = btrfs_root_node(root);
200 btrfs_tree_read_lock(eb);
201 if (eb == root->node)
202 break;
203 btrfs_tree_read_unlock(eb);
204 free_extent_buffer(eb);
205 }
206 return eb;
207}
208
Chris Masond352ac62008-09-29 15:18:18 -0400209/* cowonly root (everything not a reference counted cow subvolume), just get
210 * put onto a simple dirty list. transaction.c walks this to make sure they
211 * get properly updated on disk.
212 */
Chris Mason0b86a832008-03-24 15:01:56 -0400213static void add_root_to_dirty_list(struct btrfs_root *root)
214{
Josef Bacike7070be2014-12-16 08:54:43 -0800215 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
216 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
217 return;
218
Chris Masone5846fc2012-05-03 12:08:48 -0400219 spin_lock(&root->fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800220 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
221 /* Want the extent tree to be the last on the list */
222 if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
223 list_move_tail(&root->dirty_list,
224 &root->fs_info->dirty_cowonly_roots);
225 else
226 list_move(&root->dirty_list,
227 &root->fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400228 }
Chris Masone5846fc2012-05-03 12:08:48 -0400229 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400230}
231
Chris Masond352ac62008-09-29 15:18:18 -0400232/*
233 * used by snapshot creation to make a copy of a root for a tree with
234 * a given objectid. The buffer with the new root node is returned in
235 * cow_ret, and this func returns zero on success or a negative error code.
236 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500237int btrfs_copy_root(struct btrfs_trans_handle *trans,
238 struct btrfs_root *root,
239 struct extent_buffer *buf,
240 struct extent_buffer **cow_ret, u64 new_root_objectid)
241{
242 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 int ret = 0;
244 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400245 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246
Miao Xie27cdeb72014-04-02 19:51:05 +0800247 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
248 trans->transid != root->fs_info->running_transaction->transid);
249 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
250 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500251
252 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400253 if (level == 0)
254 btrfs_item_key(buf, &disk_key, 0);
255 else
256 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400257
David Sterba4d75f8a2014-06-15 01:54:12 +0200258 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
259 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500261 return PTR_ERR(cow);
262
263 copy_extent_buffer(cow, buf, 0, 0, cow->len);
264 btrfs_set_header_bytenr(cow, cow->start);
265 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
267 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
268 BTRFS_HEADER_FLAG_RELOC);
269 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
270 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
271 else
272 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500273
Ross Kirk0a4e5582013-09-24 10:12:38 +0100274 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500275 BTRFS_FSID_SIZE);
276
Chris Masonbe20aa92007-12-17 20:14:01 -0500277 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700279 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700281 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500282
Chris Masonbe20aa92007-12-17 20:14:01 -0500283 if (ret)
284 return ret;
285
286 btrfs_mark_buffer_dirty(cow);
287 *cow_ret = cow;
288 return 0;
289}
290
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200291enum mod_log_op {
292 MOD_LOG_KEY_REPLACE,
293 MOD_LOG_KEY_ADD,
294 MOD_LOG_KEY_REMOVE,
295 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
296 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
297 MOD_LOG_MOVE_KEYS,
298 MOD_LOG_ROOT_REPLACE,
299};
300
301struct tree_mod_move {
302 int dst_slot;
303 int nr_items;
304};
305
306struct tree_mod_root {
307 u64 logical;
308 u8 level;
309};
310
311struct tree_mod_elem {
312 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530313 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200314 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200315 enum mod_log_op op;
316
317 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
318 int slot;
319
320 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
321 u64 generation;
322
323 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
324 struct btrfs_disk_key key;
325 u64 blockptr;
326
327 /* this is used for op == MOD_LOG_MOVE_KEYS */
328 struct tree_mod_move move;
329
330 /* this is used for op == MOD_LOG_ROOT_REPLACE */
331 struct tree_mod_root old_root;
332};
333
Jan Schmidt097b8a72012-06-21 11:08:04 +0200334/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700335 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000336 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700337static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000338{
339 return atomic64_inc_return(&fs_info->tree_mod_seq);
340}
341
342/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343 * This adds a new blocker to the tree mod log's blocker list if the @elem
344 * passed does not already have a sequence number set. So when a caller expects
345 * to record tree modifications, it should ensure to set elem->seq to zero
346 * before calling btrfs_get_tree_mod_seq.
347 * Returns a fresh, unused tree log modification sequence number, even if no new
348 * blocker was added.
349 */
350u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
351 struct seq_list *elem)
352{
David Sterbaa71561b2018-03-05 15:43:41 +0100353 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200354 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700355 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200356 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
357 }
David Sterbaa71561b2018-03-05 15:43:41 +0100358 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200359
Josef Bacikfcebe452014-05-13 17:30:47 -0700360 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200361}
362
363void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
364 struct seq_list *elem)
365{
366 struct rb_root *tm_root;
367 struct rb_node *node;
368 struct rb_node *next;
369 struct seq_list *cur_elem;
370 struct tree_mod_elem *tm;
371 u64 min_seq = (u64)-1;
372 u64 seq_putting = elem->seq;
373
374 if (!seq_putting)
375 return;
376
Filipe Manana24e9e6b2020-01-22 12:23:20 +0000377 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200378 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200379 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200380
381 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200382 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200383 if (seq_putting > cur_elem->seq) {
384 /*
385 * blocker with lower sequence number exists, we
386 * cannot remove anything from the log
387 */
Filipe Manana24e9e6b2020-01-22 12:23:20 +0000388 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200389 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200390 }
391 min_seq = cur_elem->seq;
392 }
393 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200394
395 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200396 * anything that's lower than the lowest existing (read: blocked)
397 * sequence number can be removed from the tree.
398 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200399 tm_root = &fs_info->tree_mod_log;
400 for (node = rb_first(tm_root); node; node = next) {
401 next = rb_next(node);
402 tm = container_of(node, struct tree_mod_elem, node);
Filipe Mananabe9a42b2019-12-06 12:27:39 +0000403 if (tm->seq >= min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200404 continue;
405 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200406 kfree(tm);
407 }
David Sterbaa71561b2018-03-05 15:43:41 +0100408 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409}
410
411/*
412 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530413 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200414 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530415 * The 'start address' is the logical address of the *new* root node
416 * for root replace operations, or the logical address of the affected
417 * block for all other operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000418 *
David Sterbaa71561b2018-03-05 15:43:41 +0100419 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420 */
421static noinline int
422__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
423{
424 struct rb_root *tm_root;
425 struct rb_node **new;
426 struct rb_node *parent = NULL;
427 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200428
Josef Bacikc8cc6342013-07-01 16:18:19 -0400429 BUG_ON(!tm);
430
Josef Bacikfcebe452014-05-13 17:30:47 -0700431 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200432
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200433 tm_root = &fs_info->tree_mod_log;
434 new = &tm_root->rb_node;
435 while (*new) {
436 cur = container_of(*new, struct tree_mod_elem, node);
437 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530438 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200439 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530440 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200441 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200442 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200443 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200444 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200445 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000446 else
447 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200448 }
449
450 rb_link_node(&tm->node, parent, new);
451 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000452 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200453}
454
Jan Schmidt097b8a72012-06-21 11:08:04 +0200455/*
456 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
457 * returns zero with the tree_mod_log_lock acquired. The caller must hold
458 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbaa71561b2018-03-05 15:43:41 +0100459 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200460 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200461static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
462 struct extent_buffer *eb) {
463 smp_mb();
464 if (list_empty(&(fs_info)->tree_mod_seq_list))
465 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200466 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200467 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000468
David Sterbaa71561b2018-03-05 15:43:41 +0100469 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000470 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbaa71561b2018-03-05 15:43:41 +0100471 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000472 return 1;
473 }
474
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200475 return 0;
476}
477
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000478/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
479static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
480 struct extent_buffer *eb)
481{
482 smp_mb();
483 if (list_empty(&(fs_info)->tree_mod_seq_list))
484 return 0;
485 if (eb && btrfs_header_level(eb) == 0)
486 return 0;
487
488 return 1;
489}
490
491static struct tree_mod_elem *
492alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
493 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200494{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200495 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200496
Josef Bacikc8cc6342013-07-01 16:18:19 -0400497 tm = kzalloc(sizeof(*tm), flags);
498 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000499 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200500
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530501 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200502 if (op != MOD_LOG_KEY_ADD) {
503 btrfs_node_key(eb, &tm->key, slot);
504 tm->blockptr = btrfs_node_blockptr(eb, slot);
505 }
506 tm->op = op;
507 tm->slot = slot;
508 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000509 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200510
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000511 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200512}
513
514static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400515tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
516 struct extent_buffer *eb, int slot,
517 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200518{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000519 struct tree_mod_elem *tm;
520 int ret;
521
522 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200523 return 0;
524
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000525 tm = alloc_tree_mod_elem(eb, slot, op, flags);
526 if (!tm)
527 return -ENOMEM;
528
529 if (tree_mod_dont_log(fs_info, eb)) {
530 kfree(tm);
531 return 0;
532 }
533
534 ret = __tree_mod_log_insert(fs_info, tm);
David Sterbaa71561b2018-03-05 15:43:41 +0100535 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000536 if (ret)
537 kfree(tm);
538
539 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200540}
541
542static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200543tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
544 struct extent_buffer *eb, int dst_slot, int src_slot,
545 int nr_items, gfp_t flags)
546{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000547 struct tree_mod_elem *tm = NULL;
548 struct tree_mod_elem **tm_list = NULL;
549 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200550 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000551 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200552
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000553 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200554 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200555
David Sterba31e818f2015-02-20 18:00:26 +0100556 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000557 if (!tm_list)
558 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200559
Josef Bacikc8cc6342013-07-01 16:18:19 -0400560 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000561 if (!tm) {
562 ret = -ENOMEM;
563 goto free_tms;
564 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200565
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530566 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200567 tm->slot = src_slot;
568 tm->move.dst_slot = dst_slot;
569 tm->move.nr_items = nr_items;
570 tm->op = MOD_LOG_MOVE_KEYS;
571
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000572 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
573 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
574 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
575 if (!tm_list[i]) {
576 ret = -ENOMEM;
577 goto free_tms;
578 }
579 }
580
581 if (tree_mod_dont_log(fs_info, eb))
582 goto free_tms;
583 locked = 1;
584
585 /*
586 * When we override something during the move, we log these removals.
587 * This can only happen when we move towards the beginning of the
588 * buffer, i.e. dst_slot < src_slot.
589 */
590 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
591 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
592 if (ret)
593 goto free_tms;
594 }
595
596 ret = __tree_mod_log_insert(fs_info, tm);
597 if (ret)
598 goto free_tms;
David Sterbaa71561b2018-03-05 15:43:41 +0100599 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000600 kfree(tm_list);
601
602 return 0;
603free_tms:
604 for (i = 0; i < nr_items; i++) {
605 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
606 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
607 kfree(tm_list[i]);
608 }
609 if (locked)
David Sterbaa71561b2018-03-05 15:43:41 +0100610 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000611 kfree(tm_list);
612 kfree(tm);
613
614 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200615}
616
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000617static inline int
618__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
619 struct tree_mod_elem **tm_list,
620 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200621{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000622 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200623 int ret;
624
Jan Schmidt097b8a72012-06-21 11:08:04 +0200625 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000626 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
627 if (ret) {
628 for (j = nritems - 1; j > i; j--)
629 rb_erase(&tm_list[j]->node,
630 &fs_info->tree_mod_log);
631 return ret;
632 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200633 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000634
635 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200636}
637
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200638static noinline int
639tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
640 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000641 struct extent_buffer *new_root, gfp_t flags,
642 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200643{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000644 struct tree_mod_elem *tm = NULL;
645 struct tree_mod_elem **tm_list = NULL;
646 int nritems = 0;
647 int ret = 0;
648 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200649
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000650 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200651 return 0;
652
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000653 if (log_removal && btrfs_header_level(old_root) > 0) {
654 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100655 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000656 flags);
657 if (!tm_list) {
658 ret = -ENOMEM;
659 goto free_tms;
660 }
661 for (i = 0; i < nritems; i++) {
662 tm_list[i] = alloc_tree_mod_elem(old_root, i,
663 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
664 if (!tm_list[i]) {
665 ret = -ENOMEM;
666 goto free_tms;
667 }
668 }
669 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000670
Josef Bacikc8cc6342013-07-01 16:18:19 -0400671 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000672 if (!tm) {
673 ret = -ENOMEM;
674 goto free_tms;
675 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200676
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530677 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200678 tm->old_root.logical = old_root->start;
679 tm->old_root.level = btrfs_header_level(old_root);
680 tm->generation = btrfs_header_generation(old_root);
681 tm->op = MOD_LOG_ROOT_REPLACE;
682
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000683 if (tree_mod_dont_log(fs_info, NULL))
684 goto free_tms;
685
686 if (tm_list)
687 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
688 if (!ret)
689 ret = __tree_mod_log_insert(fs_info, tm);
690
David Sterbaa71561b2018-03-05 15:43:41 +0100691 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000692 if (ret)
693 goto free_tms;
694 kfree(tm_list);
695
696 return ret;
697
698free_tms:
699 if (tm_list) {
700 for (i = 0; i < nritems; i++)
701 kfree(tm_list[i]);
702 kfree(tm_list);
703 }
704 kfree(tm);
705
706 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200707}
708
709static struct tree_mod_elem *
710__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
711 int smallest)
712{
713 struct rb_root *tm_root;
714 struct rb_node *node;
715 struct tree_mod_elem *cur = NULL;
716 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200717
David Sterbaa71561b2018-03-05 15:43:41 +0100718 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200719 tm_root = &fs_info->tree_mod_log;
720 node = tm_root->rb_node;
721 while (node) {
722 cur = container_of(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530723 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200724 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530725 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200726 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200727 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200728 node = node->rb_left;
729 } else if (!smallest) {
730 /* we want the node with the highest seq */
731 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200732 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200733 found = cur;
734 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200735 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200736 /* we want the node with the smallest seq */
737 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200738 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200739 found = cur;
740 node = node->rb_right;
741 } else {
742 found = cur;
743 break;
744 }
745 }
David Sterbaa71561b2018-03-05 15:43:41 +0100746 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200747
748 return found;
749}
750
751/*
752 * this returns the element from the log with the smallest time sequence
753 * value that's in the log (the oldest log item). any element with a time
754 * sequence lower than min_seq will be ignored.
755 */
756static struct tree_mod_elem *
757tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
758 u64 min_seq)
759{
760 return __tree_mod_log_search(fs_info, start, min_seq, 1);
761}
762
763/*
764 * this returns the element from the log with the largest time sequence
765 * value that's in the log (the most recent log item). any element with
766 * a time sequence lower than min_seq will be ignored.
767 */
768static struct tree_mod_elem *
769tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
770{
771 return __tree_mod_log_search(fs_info, start, min_seq, 0);
772}
773
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000774static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200775tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
776 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000777 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200778{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000779 int ret = 0;
780 struct tree_mod_elem **tm_list = NULL;
781 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200782 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000783 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200784
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000785 if (!tree_mod_need_log(fs_info, NULL))
786 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200787
Josef Bacikc8cc6342013-07-01 16:18:19 -0400788 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000789 return 0;
790
David Sterba31e818f2015-02-20 18:00:26 +0100791 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000792 GFP_NOFS);
793 if (!tm_list)
794 return -ENOMEM;
795
796 tm_list_add = tm_list;
797 tm_list_rem = tm_list + nr_items;
798 for (i = 0; i < nr_items; i++) {
799 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
800 MOD_LOG_KEY_REMOVE, GFP_NOFS);
801 if (!tm_list_rem[i]) {
802 ret = -ENOMEM;
803 goto free_tms;
804 }
805
806 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
807 MOD_LOG_KEY_ADD, GFP_NOFS);
808 if (!tm_list_add[i]) {
809 ret = -ENOMEM;
810 goto free_tms;
811 }
812 }
813
814 if (tree_mod_dont_log(fs_info, NULL))
815 goto free_tms;
816 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200817
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200818 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000819 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
820 if (ret)
821 goto free_tms;
822 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
823 if (ret)
824 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200825 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000826
David Sterbaa71561b2018-03-05 15:43:41 +0100827 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000828 kfree(tm_list);
829
830 return 0;
831
832free_tms:
833 for (i = 0; i < nr_items * 2; i++) {
834 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
835 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
836 kfree(tm_list[i]);
837 }
838 if (locked)
David Sterbaa71561b2018-03-05 15:43:41 +0100839 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000840 kfree(tm_list);
841
842 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200843}
844
845static inline void
846tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
847 int dst_offset, int src_offset, int nr_items)
848{
849 int ret;
850 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
851 nr_items, GFP_NOFS);
852 BUG_ON(ret < 0);
853}
854
Jan Schmidt097b8a72012-06-21 11:08:04 +0200855static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200856tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000857 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200858{
859 int ret;
860
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000861 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400862 MOD_LOG_KEY_REPLACE,
863 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200864 BUG_ON(ret < 0);
865}
866
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000867static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200868tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200869{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000870 struct tree_mod_elem **tm_list = NULL;
871 int nritems = 0;
872 int i;
873 int ret = 0;
874
875 if (btrfs_header_level(eb) == 0)
876 return 0;
877
878 if (!tree_mod_need_log(fs_info, NULL))
879 return 0;
880
881 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100882 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000883 if (!tm_list)
884 return -ENOMEM;
885
886 for (i = 0; i < nritems; i++) {
887 tm_list[i] = alloc_tree_mod_elem(eb, i,
888 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
889 if (!tm_list[i]) {
890 ret = -ENOMEM;
891 goto free_tms;
892 }
893 }
894
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200895 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000896 goto free_tms;
897
898 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
David Sterbaa71561b2018-03-05 15:43:41 +0100899 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000900 if (ret)
901 goto free_tms;
902 kfree(tm_list);
903
904 return 0;
905
906free_tms:
907 for (i = 0; i < nritems; i++)
908 kfree(tm_list[i]);
909 kfree(tm_list);
910
911 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200912}
913
Jan Schmidt097b8a72012-06-21 11:08:04 +0200914static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200915tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000916 struct extent_buffer *new_root_node,
917 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200918{
919 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200920 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000921 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200922 BUG_ON(ret < 0);
923}
924
Chris Masond352ac62008-09-29 15:18:18 -0400925/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 * check if the tree block can be shared by multiple trees
927 */
928int btrfs_block_can_be_shared(struct btrfs_root *root,
929 struct extent_buffer *buf)
930{
931 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400932 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400933 * are never shared. If a block was allocated after the last
934 * snapshot and the block was not allocated by tree relocation,
935 * we know the block is not shared.
936 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800937 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 buf != root->node && buf != root->commit_root &&
939 (btrfs_header_generation(buf) <=
940 btrfs_root_last_snapshot(&root->root_item) ||
941 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
942 return 1;
943#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800944 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
946 return 1;
947#endif
948 return 0;
949}
950
951static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
952 struct btrfs_root *root,
953 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400954 struct extent_buffer *cow,
955 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956{
957 u64 refs;
958 u64 owner;
959 u64 flags;
960 u64 new_flags = 0;
961 int ret;
962
963 /*
964 * Backrefs update rules:
965 *
966 * Always use full backrefs for extent pointers in tree block
967 * allocated by tree relocation.
968 *
969 * If a shared tree block is no longer referenced by its owner
970 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
971 * use full backrefs for extent pointers in tree block.
972 *
973 * If a tree block is been relocating
974 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
975 * use full backrefs for extent pointers in tree block.
976 * The reason for this is some operations (such as drop tree)
977 * are only allowed for blocks use full backrefs.
978 */
979
980 if (btrfs_block_can_be_shared(root, buf)) {
981 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500982 btrfs_header_level(buf), 1,
983 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700984 if (ret)
985 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700986 if (refs == 0) {
987 ret = -EROFS;
Anand Jain34d97002016-03-16 16:43:06 +0800988 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700989 return ret;
990 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 } else {
992 refs = 1;
993 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
994 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
995 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
996 else
997 flags = 0;
998 }
999
1000 owner = btrfs_header_owner(buf);
1001 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1002 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1003
1004 if (refs > 1) {
1005 if ((owner == root->root_key.objectid ||
1006 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1007 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001008 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001009 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010
1011 if (root->root_key.objectid ==
1012 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001013 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001014 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001015 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001016 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 }
1018 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1019 } else {
1020
1021 if (root->root_key.objectid ==
1022 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001023 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001024 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001025 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001026 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027 }
1028 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001029 int level = btrfs_header_level(buf);
1030
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001031 ret = btrfs_set_disk_extent_flags(trans, root,
1032 buf->start,
1033 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001034 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001035 if (ret)
1036 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037 }
1038 } else {
1039 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1040 if (root->root_key.objectid ==
1041 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001042 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001043 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001044 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001045 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001046 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001047 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048 }
Daniel Dressler01d58472014-11-21 17:15:07 +09001049 clean_tree_block(trans, root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001050 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051 }
1052 return 0;
1053}
1054
1055/*
Chris Masond3977122009-01-05 21:25:51 -05001056 * does the dirty work in cow of a single block. The parent block (if
1057 * supplied) is updated to point to the new cow copy. The new buffer is marked
1058 * dirty and returned locked. If you modify the block it needs to be marked
1059 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001060 *
1061 * search_start -- an allocation hint for the new block
1062 *
Chris Masond3977122009-01-05 21:25:51 -05001063 * empty_size -- a hint that you plan on doing more cow. This is the size in
1064 * bytes the allocator should try to find free next to the block it returns.
1065 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001066 */
Chris Masond3977122009-01-05 21:25:51 -05001067static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001068 struct btrfs_root *root,
1069 struct extent_buffer *buf,
1070 struct extent_buffer *parent, int parent_slot,
1071 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001072 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001073{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001074 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001075 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001076 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001077 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001078 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001079 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001080
Chris Mason925baed2008-06-25 16:01:30 -04001081 if (*cow_ret == buf)
1082 unlock_orig = 1;
1083
Chris Masonb9447ef2009-03-09 11:45:38 -04001084 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001085
Miao Xie27cdeb72014-04-02 19:51:05 +08001086 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1087 trans->transid != root->fs_info->running_transaction->transid);
1088 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1089 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001090
Chris Mason7bb86312007-12-11 09:25:06 -05001091 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001092
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001093 if (level == 0)
1094 btrfs_item_key(buf, &disk_key, 0);
1095 else
1096 btrfs_node_key(buf, &disk_key, 0);
1097
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001098 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1099 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001100
David Sterba4d75f8a2014-06-15 01:54:12 +02001101 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1102 root->root_key.objectid, &disk_key, level,
1103 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001104 if (IS_ERR(cow))
1105 return PTR_ERR(cow);
1106
Chris Masonb4ce94d2009-02-04 09:25:08 -05001107 /* cow is set to blocking by btrfs_init_new_buffer */
1108
Chris Mason5f39d392007-10-15 16:14:19 -04001109 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001110 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001111 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001112 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1113 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1114 BTRFS_HEADER_FLAG_RELOC);
1115 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1116 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1117 else
1118 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001119
Ross Kirk0a4e5582013-09-24 10:12:38 +01001120 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001121 BTRFS_FSID_SIZE);
1122
Mark Fashehbe1a5562011-08-08 13:20:18 -07001123 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001124 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001125 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001126 return ret;
1127 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001128
Miao Xie27cdeb72014-04-02 19:51:05 +08001129 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001130 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001131 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001132 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001133 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001134 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001135 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001136
Chris Mason6702ed42007-08-07 16:15:09 -04001137 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001138 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001139 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1140 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1141 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001142
Chris Mason5f39d392007-10-15 16:14:19 -04001143 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001144 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001145 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001146
Yan, Zhengf0486c62010-05-16 10:46:25 -04001147 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001148 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001149 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001150 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001151 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001152 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001153 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001154 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001155 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001156 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001157 btrfs_set_node_ptr_generation(parent, parent_slot,
1158 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001159 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001160 if (last_ref) {
1161 ret = tree_mod_log_free_eb(root->fs_info, buf);
1162 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001163 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001164 return ret;
1165 }
1166 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001167 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001168 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001169 }
Chris Mason925baed2008-06-25 16:01:30 -04001170 if (unlock_orig)
1171 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001172 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001173 btrfs_mark_buffer_dirty(cow);
1174 *cow_ret = cow;
1175 return 0;
1176}
1177
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001178/*
1179 * returns the logical address of the oldest predecessor of the given root.
1180 * entries older than time_seq are ignored.
1181 */
1182static struct tree_mod_elem *
1183__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001184 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001185{
1186 struct tree_mod_elem *tm;
1187 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001188 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001189 int looped = 0;
1190
1191 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001192 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001193
1194 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301195 * the very last operation that's logged for a root is the
1196 * replacement operation (if it is replaced at all). this has
1197 * the logical address of the *new* root, making it the very
1198 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001199 */
1200 while (1) {
1201 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1202 time_seq);
1203 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001204 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001205 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001206 * if there are no tree operation for the oldest root, we simply
1207 * return it. this should only happen if that (old) root is at
1208 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001209 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001210 if (!tm)
1211 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001212
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001213 /*
1214 * if there's an operation that's not a root replacement, we
1215 * found the oldest version of our root. normally, we'll find a
1216 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1217 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001218 if (tm->op != MOD_LOG_ROOT_REPLACE)
1219 break;
1220
1221 found = tm;
1222 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001223 looped = 1;
1224 }
1225
Jan Schmidta95236d2012-06-05 16:41:24 +02001226 /* if there's no old root to return, return what we found instead */
1227 if (!found)
1228 found = tm;
1229
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001230 return found;
1231}
1232
1233/*
1234 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001235 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001236 * time_seq).
1237 */
1238static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001239__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1240 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001241{
1242 u32 n;
1243 struct rb_node *next;
1244 struct tree_mod_elem *tm = first_tm;
1245 unsigned long o_dst;
1246 unsigned long o_src;
1247 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1248
1249 n = btrfs_header_nritems(eb);
David Sterbaa71561b2018-03-05 15:43:41 +01001250 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001251 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001252 /*
1253 * all the operations are recorded with the operator used for
1254 * the modification. as we're going backwards, we do the
1255 * opposite of each operation here.
1256 */
1257 switch (tm->op) {
1258 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1259 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001260 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001261 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001262 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001263 btrfs_set_node_key(eb, &tm->key, tm->slot);
1264 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1265 btrfs_set_node_ptr_generation(eb, tm->slot,
1266 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001267 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268 break;
1269 case MOD_LOG_KEY_REPLACE:
1270 BUG_ON(tm->slot >= n);
1271 btrfs_set_node_key(eb, &tm->key, tm->slot);
1272 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1273 btrfs_set_node_ptr_generation(eb, tm->slot,
1274 tm->generation);
1275 break;
1276 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001277 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001278 n--;
1279 break;
1280 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001281 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1282 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1283 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001284 tm->move.nr_items * p_size);
1285 break;
1286 case MOD_LOG_ROOT_REPLACE:
1287 /*
1288 * this operation is special. for roots, this must be
1289 * handled explicitly before rewinding.
1290 * for non-roots, this operation may exist if the node
1291 * was a root: root A -> child B; then A gets empty and
1292 * B is promoted to the new root. in the mod log, we'll
1293 * have a root-replace operation for B, a tree block
1294 * that is no root. we simply ignore that operation.
1295 */
1296 break;
1297 }
1298 next = rb_next(&tm->node);
1299 if (!next)
1300 break;
1301 tm = container_of(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301302 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001303 break;
1304 }
David Sterbaa71561b2018-03-05 15:43:41 +01001305 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001306 btrfs_set_header_nritems(eb, n);
1307}
1308
Jan Schmidt47fb0912013-04-13 13:19:55 +00001309/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001310 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001311 * is returned. If rewind operations happen, a fresh buffer is returned. The
1312 * returned buffer is always read-locked. If the returned buffer is not the
1313 * input buffer, the lock on the input buffer is released and the input buffer
1314 * is freed (its refcount is decremented).
1315 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001316static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001317tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1318 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001319{
1320 struct extent_buffer *eb_rewin;
1321 struct tree_mod_elem *tm;
1322
1323 if (!time_seq)
1324 return eb;
1325
1326 if (btrfs_header_level(eb) == 0)
1327 return eb;
1328
1329 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1330 if (!tm)
1331 return eb;
1332
Josef Bacik9ec72672013-08-07 16:57:23 -04001333 btrfs_set_path_blocking(path);
1334 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1335
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001336 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1337 BUG_ON(tm->slot != 0);
Feifei Xub9ef22d2016-06-01 19:18:25 +08001338 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start,
1339 eb->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001340 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001341 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001342 free_extent_buffer(eb);
1343 return NULL;
1344 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001345 btrfs_set_header_bytenr(eb_rewin, eb->start);
1346 btrfs_set_header_backref_rev(eb_rewin,
1347 btrfs_header_backref_rev(eb));
1348 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001349 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001350 } else {
1351 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001352 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001353 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001354 free_extent_buffer(eb);
1355 return NULL;
1356 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001357 }
1358
Josef Bacik9ec72672013-08-07 16:57:23 -04001359 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1360 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001361 free_extent_buffer(eb);
1362
Jan Schmidt47fb0912013-04-13 13:19:55 +00001363 extent_buffer_get(eb_rewin);
1364 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001365 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001366 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001367 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001368
1369 return eb_rewin;
1370}
1371
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001372/*
1373 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1374 * value. If there are no changes, the current root->root_node is returned. If
1375 * anything changed in between, there's a fresh buffer allocated on which the
1376 * rewind operations are done. In any case, the returned buffer is read locked.
1377 * Returns NULL on error (with no locks held).
1378 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001379static inline struct extent_buffer *
1380get_old_root(struct btrfs_root *root, u64 time_seq)
1381{
1382 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001383 struct extent_buffer *eb = NULL;
1384 struct extent_buffer *eb_root;
Filipe Mananaf0ed93d2019-08-12 19:14:29 +01001385 u64 eb_root_owner = 0;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001386 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001387 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001388 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001389 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001390
Jan Schmidt30b04632013-04-13 13:19:54 +00001391 eb_root = btrfs_read_lock_root_node(root);
1392 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001393 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001394 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001395
Jan Schmidta95236d2012-06-05 16:41:24 +02001396 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1397 old_root = &tm->old_root;
1398 old_generation = tm->generation;
1399 logical = old_root->logical;
1400 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001401 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001402 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001403
Jan Schmidta95236d2012-06-05 16:41:24 +02001404 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001405 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001406 btrfs_tree_read_unlock(eb_root);
1407 free_extent_buffer(eb_root);
David Sterbace86cd52014-06-15 01:07:32 +02001408 old = read_tree_block(root, logical, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001409 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1410 if (!IS_ERR(old))
1411 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001412 btrfs_warn(root->fs_info,
1413 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001414 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001415 eb = btrfs_clone_extent_buffer(old);
1416 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001417 }
1418 } else if (old_root) {
Filipe Mananaf0ed93d2019-08-12 19:14:29 +01001419 eb_root_owner = btrfs_header_owner(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001420 btrfs_tree_read_unlock(eb_root);
1421 free_extent_buffer(eb_root);
Feifei Xub9ef22d2016-06-01 19:18:25 +08001422 eb = alloc_dummy_extent_buffer(root->fs_info, logical,
1423 root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001424 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001425 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001426 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001427 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001428 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001429 }
1430
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001431 if (!eb)
1432 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001433 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001434 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001435 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001436 btrfs_set_header_bytenr(eb, eb->start);
1437 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Filipe Mananaf0ed93d2019-08-12 19:14:29 +01001438 btrfs_set_header_owner(eb, eb_root_owner);
Jan Schmidta95236d2012-06-05 16:41:24 +02001439 btrfs_set_header_level(eb, old_root->level);
1440 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001441 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001442 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001443 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001444 else
1445 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001446 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001447
1448 return eb;
1449}
1450
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001451int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1452{
1453 struct tree_mod_elem *tm;
1454 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001455 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001456
Jan Schmidt30b04632013-04-13 13:19:54 +00001457 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001458 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1459 level = tm->old_root.level;
1460 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001461 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001462 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001463 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001464
1465 return level;
1466}
1467
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001468static inline int should_cow_block(struct btrfs_trans_handle *trans,
1469 struct btrfs_root *root,
1470 struct extent_buffer *buf)
1471{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001472 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001473 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001474
Liu Bof1ebcc72011-11-14 20:48:06 -05001475 /* ensure we can see the force_cow */
1476 smp_rmb();
1477
1478 /*
1479 * We do not need to cow a block if
1480 * 1) this block is not created or changed in this transaction;
1481 * 2) this block does not belong to TREE_RELOC tree;
1482 * 3) the root is not forced COW.
1483 *
1484 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001485 * when we create snapshot during committing the transaction,
Liu Bof1ebcc72011-11-14 20:48:06 -05001486 * after we've finished coping src root, we must COW the shared
1487 * block to ensure the metadata consistency.
1488 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001489 if (btrfs_header_generation(buf) == trans->transid &&
1490 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1491 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001492 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001493 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001494 return 0;
1495 return 1;
1496}
1497
Chris Masond352ac62008-09-29 15:18:18 -04001498/*
1499 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001500 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001501 * once per transaction, as long as it hasn't been written yet
1502 */
Chris Masond3977122009-01-05 21:25:51 -05001503noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001504 struct btrfs_root *root, struct extent_buffer *buf,
1505 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001506 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001507{
Chris Mason6702ed42007-08-07 16:15:09 -04001508 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001509 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001510
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001511 if (trans->transaction != root->fs_info->running_transaction)
1512 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001513 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001514 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001515
1516 if (trans->transid != root->fs_info->generation)
1517 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001518 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001519
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001520 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001521 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001522 *cow_ret = buf;
1523 return 0;
1524 }
Chris Masonc4876852009-02-04 09:24:25 -05001525
Byongho Leeee221842015-12-15 01:42:10 +09001526 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001527
1528 if (parent)
1529 btrfs_set_lock_blocking(parent);
1530 btrfs_set_lock_blocking(buf);
1531
Chris Masonf510cfe2007-10-15 16:14:48 -04001532 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001533 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001534
1535 trace_btrfs_cow_block(root, buf, *cow_ret);
1536
Chris Masonf510cfe2007-10-15 16:14:48 -04001537 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001538}
1539
Chris Masond352ac62008-09-29 15:18:18 -04001540/*
1541 * helper function for defrag to decide if two blocks pointed to by a
1542 * node are actually close by
1543 */
Chris Mason6b800532007-10-15 16:17:34 -04001544static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001545{
Chris Mason6b800532007-10-15 16:17:34 -04001546 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001547 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001548 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001549 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001550 return 0;
1551}
1552
Chris Mason081e9572007-11-06 10:26:24 -05001553/*
1554 * compare two keys in a memcmp fashion
1555 */
1556static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1557{
1558 struct btrfs_key k1;
1559
1560 btrfs_disk_key_to_cpu(&k1, disk);
1561
Diego Calleja20736ab2009-07-24 11:06:52 -04001562 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001563}
1564
Josef Bacikf3465ca2008-11-12 14:19:50 -05001565/*
1566 * same as comp_keys only with two btrfs_key's
1567 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001568int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001569{
1570 if (k1->objectid > k2->objectid)
1571 return 1;
1572 if (k1->objectid < k2->objectid)
1573 return -1;
1574 if (k1->type > k2->type)
1575 return 1;
1576 if (k1->type < k2->type)
1577 return -1;
1578 if (k1->offset > k2->offset)
1579 return 1;
1580 if (k1->offset < k2->offset)
1581 return -1;
1582 return 0;
1583}
Chris Mason081e9572007-11-06 10:26:24 -05001584
Chris Masond352ac62008-09-29 15:18:18 -04001585/*
1586 * this is used by the defrag code to go through all the
1587 * leaves pointed to by a node and reallocate them so that
1588 * disk order is close to key order
1589 */
Chris Mason6702ed42007-08-07 16:15:09 -04001590int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001591 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001592 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001593 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001594{
Chris Mason6b800532007-10-15 16:17:34 -04001595 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001596 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001597 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001598 u64 search_start = *last_ret;
1599 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001600 u64 other;
1601 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001602 int end_slot;
1603 int i;
1604 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001605 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001606 int uptodate;
1607 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001608 int progress_passed = 0;
1609 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001610
Chris Mason5708b952007-10-25 15:43:18 -04001611 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001612
Julia Lawall6c1500f2012-11-03 20:30:18 +00001613 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1614 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001615
Chris Mason6b800532007-10-15 16:17:34 -04001616 parent_nritems = btrfs_header_nritems(parent);
David Sterba707e8a02014-06-04 19:22:26 +02001617 blocksize = root->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001618 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001619
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001620 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001621 return 0;
1622
Chris Masonb4ce94d2009-02-04 09:25:08 -05001623 btrfs_set_lock_blocking(parent);
1624
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001625 for (i = start_slot; i <= end_slot; i++) {
Chris Mason6702ed42007-08-07 16:15:09 -04001626 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001627
Chris Mason081e9572007-11-06 10:26:24 -05001628 btrfs_node_key(parent, &disk_key, i);
1629 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1630 continue;
1631
1632 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001633 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001634 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001635 if (last_block == 0)
1636 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001637
Chris Mason6702ed42007-08-07 16:15:09 -04001638 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001639 other = btrfs_node_blockptr(parent, i - 1);
1640 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001641 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001642 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001643 other = btrfs_node_blockptr(parent, i + 1);
1644 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001645 }
Chris Masone9d0b132007-08-10 14:06:19 -04001646 if (close) {
1647 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001648 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001649 }
Chris Mason6702ed42007-08-07 16:15:09 -04001650
Daniel Dressler01d58472014-11-21 17:15:07 +09001651 cur = btrfs_find_tree_block(root->fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001652 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001653 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001654 else
1655 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001656 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001657 if (!cur) {
David Sterbace86cd52014-06-15 01:07:32 +02001658 cur = read_tree_block(root, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001659 if (IS_ERR(cur)) {
1660 return PTR_ERR(cur);
1661 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001662 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001663 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001664 }
Chris Mason6b800532007-10-15 16:17:34 -04001665 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001666 err = btrfs_read_buffer(cur, gen);
1667 if (err) {
1668 free_extent_buffer(cur);
1669 return err;
1670 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001671 }
Chris Mason6702ed42007-08-07 16:15:09 -04001672 }
Chris Masone9d0b132007-08-10 14:06:19 -04001673 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001674 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001675
Chris Masone7a84562008-06-25 16:01:31 -04001676 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001677 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001678 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001679 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001680 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001681 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001682 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001683 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001684 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001685 break;
Yan252c38f2007-08-29 09:11:44 -04001686 }
Chris Masone7a84562008-06-25 16:01:31 -04001687 search_start = cur->start;
1688 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001689 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001690 btrfs_tree_unlock(cur);
1691 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001692 }
1693 return err;
1694}
1695
Chris Masonaa5d6be2007-02-28 16:35:06 -05001696
Chris Mason74123bd2007-02-02 11:05:29 -05001697/*
Chris Mason5f39d392007-10-15 16:14:19 -04001698 * search for key in the extent_buffer. The items start at offset p,
1699 * and they are item_size apart. There are 'max' items in p.
1700 *
Chris Mason74123bd2007-02-02 11:05:29 -05001701 * the slot in the array is returned via slot, and it points to
1702 * the place where you would insert key if it is not found in
1703 * the array.
1704 *
1705 * slot may point to max if the key is bigger than all of the keys
1706 */
Chris Masone02119d2008-09-05 16:13:11 -04001707static noinline int generic_bin_search(struct extent_buffer *eb,
1708 unsigned long p,
1709 int item_size, struct btrfs_key *key,
1710 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001711{
1712 int low = 0;
1713 int high = max;
1714 int mid;
1715 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001716 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001717 struct btrfs_disk_key unaligned;
1718 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001719 char *kaddr = NULL;
1720 unsigned long map_start = 0;
1721 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001722 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001723
Liu Bo5e24e9a2016-06-23 16:32:45 -07001724 if (low > high) {
1725 btrfs_err(eb->fs_info,
1726 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1727 __func__, low, high, eb->start,
1728 btrfs_header_owner(eb), btrfs_header_level(eb));
1729 return -EINVAL;
1730 }
1731
Chris Masond3977122009-01-05 21:25:51 -05001732 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001733 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001734 offset = p + mid * item_size;
1735
Chris Masona6591712011-07-19 12:04:14 -04001736 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001737 (offset + sizeof(struct btrfs_disk_key)) >
1738 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001739
1740 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001741 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001742 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001743
Chris Mason479965d2007-10-15 16:14:27 -04001744 if (!err) {
1745 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1746 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001747 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001748 read_extent_buffer(eb, &unaligned,
1749 offset, sizeof(unaligned));
1750 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001751 } else {
1752 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001753 }
1754
Chris Mason5f39d392007-10-15 16:14:19 -04001755 } else {
1756 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1757 map_start);
1758 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001759 ret = comp_keys(tmp, key);
1760
1761 if (ret < 0)
1762 low = mid + 1;
1763 else if (ret > 0)
1764 high = mid;
1765 else {
1766 *slot = mid;
1767 return 0;
1768 }
1769 }
1770 *slot = low;
1771 return 1;
1772}
1773
Chris Mason97571fd2007-02-24 13:39:08 -05001774/*
1775 * simple bin_search frontend that does the right thing for
1776 * leaves vs nodes
1777 */
Chris Mason5f39d392007-10-15 16:14:19 -04001778static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1779 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001780{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001781 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001782 return generic_bin_search(eb,
1783 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001784 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001785 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001786 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001787 else
Chris Mason5f39d392007-10-15 16:14:19 -04001788 return generic_bin_search(eb,
1789 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001790 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001791 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001792 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001793}
1794
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001795int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1796 int level, int *slot)
1797{
1798 return bin_search(eb, key, level, slot);
1799}
1800
Yan, Zhengf0486c62010-05-16 10:46:25 -04001801static void root_add_used(struct btrfs_root *root, u32 size)
1802{
1803 spin_lock(&root->accounting_lock);
1804 btrfs_set_root_used(&root->root_item,
1805 btrfs_root_used(&root->root_item) + size);
1806 spin_unlock(&root->accounting_lock);
1807}
1808
1809static void root_sub_used(struct btrfs_root *root, u32 size)
1810{
1811 spin_lock(&root->accounting_lock);
1812 btrfs_set_root_used(&root->root_item,
1813 btrfs_root_used(&root->root_item) - size);
1814 spin_unlock(&root->accounting_lock);
1815}
1816
Chris Masond352ac62008-09-29 15:18:18 -04001817/* given a node and slot number, this reads the blocks it points to. The
1818 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001819 */
Chris Masone02119d2008-09-05 16:13:11 -04001820static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001821 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001822{
Chris Masonca7a79a2008-05-12 12:59:19 -04001823 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001824 struct extent_buffer *eb;
1825
Liu Bofb770ae2016-07-05 12:10:14 -07001826 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1827 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001828
1829 BUG_ON(level == 0);
1830
Josef Bacik416bc652013-04-23 14:17:42 -04001831 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001832 btrfs_node_ptr_generation(parent, slot));
Liu Bofb770ae2016-07-05 12:10:14 -07001833 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1834 free_extent_buffer(eb);
1835 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001836 }
1837
1838 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001839}
1840
Chris Masond352ac62008-09-29 15:18:18 -04001841/*
1842 * node level balancing, used to make sure nodes are in proper order for
1843 * item deletion. We balance from the top down, so we have to make sure
1844 * that a deletion won't leave an node completely empty later on.
1845 */
Chris Masone02119d2008-09-05 16:13:11 -04001846static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001847 struct btrfs_root *root,
1848 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001849{
Chris Mason5f39d392007-10-15 16:14:19 -04001850 struct extent_buffer *right = NULL;
1851 struct extent_buffer *mid;
1852 struct extent_buffer *left = NULL;
1853 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001854 int ret = 0;
1855 int wret;
1856 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001857 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001858 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001859
1860 if (level == 0)
1861 return 0;
1862
Chris Mason5f39d392007-10-15 16:14:19 -04001863 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001864
Chris Masonbd681512011-07-16 15:23:14 -04001865 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1866 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001867 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1868
Chris Mason1d4f8a02007-03-13 09:28:32 -04001869 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001870
Li Zefana05a9bb2011-09-06 16:55:34 +08001871 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001872 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001873 pslot = path->slots[level + 1];
1874 }
Chris Masonbb803952007-03-01 12:04:21 -05001875
Chris Mason40689472007-03-17 14:29:23 -04001876 /*
1877 * deal with the case where there is only one pointer in the root
1878 * by promoting the node below to a root
1879 */
Chris Mason5f39d392007-10-15 16:14:19 -04001880 if (!parent) {
1881 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001882
Chris Mason5f39d392007-10-15 16:14:19 -04001883 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001884 return 0;
1885
1886 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001887 child = read_node_slot(root, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001888 if (IS_ERR(child)) {
1889 ret = PTR_ERR(child);
Anand Jain34d97002016-03-16 16:43:06 +08001890 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001891 goto enospc;
1892 }
1893
Chris Mason925baed2008-06-25 16:01:30 -04001894 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001895 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001896 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001897 if (ret) {
1898 btrfs_tree_unlock(child);
1899 free_extent_buffer(child);
1900 goto enospc;
1901 }
Yan2f375ab2008-02-01 14:58:07 -05001902
Jan Schmidt90f8d622013-04-13 13:19:53 +00001903 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001904 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001905
Chris Mason0b86a832008-03-24 15:01:56 -04001906 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001907 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001908
Chris Mason925baed2008-06-25 16:01:30 -04001909 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001910 path->nodes[level] = NULL;
Daniel Dressler01d58472014-11-21 17:15:07 +09001911 clean_tree_block(trans, root->fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001912 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001913 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001914 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001915
1916 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001917 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001918 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001919 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001920 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001921 }
Chris Mason5f39d392007-10-15 16:14:19 -04001922 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001923 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001924 return 0;
1925
Chris Mason5f39d392007-10-15 16:14:19 -04001926 left = read_node_slot(root, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001927 if (IS_ERR(left))
1928 left = NULL;
1929
Chris Mason5f39d392007-10-15 16:14:19 -04001930 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001931 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001932 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001933 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001934 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001935 if (wret) {
1936 ret = wret;
1937 goto enospc;
1938 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001939 }
Liu Bofb770ae2016-07-05 12:10:14 -07001940
Chris Mason5f39d392007-10-15 16:14:19 -04001941 right = read_node_slot(root, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001942 if (IS_ERR(right))
1943 right = NULL;
1944
Chris Mason5f39d392007-10-15 16:14:19 -04001945 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001946 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001947 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001948 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001949 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001950 if (wret) {
1951 ret = wret;
1952 goto enospc;
1953 }
1954 }
1955
1956 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001957 if (left) {
1958 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001959 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001960 if (wret < 0)
1961 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001962 }
Chris Mason79f95c82007-03-01 15:16:26 -05001963
1964 /*
1965 * then try to empty the right most buffer into the middle
1966 */
Chris Mason5f39d392007-10-15 16:14:19 -04001967 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001968 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001969 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001970 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001971 if (btrfs_header_nritems(right) == 0) {
Daniel Dressler01d58472014-11-21 17:15:07 +09001972 clean_tree_block(trans, root->fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04001973 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001974 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001975 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001976 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001977 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001978 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001979 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001980 struct btrfs_disk_key right_key;
1981 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001982 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001983 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001984 btrfs_set_node_key(parent, &right_key, pslot + 1);
1985 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001986 }
1987 }
Chris Mason5f39d392007-10-15 16:14:19 -04001988 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001989 /*
1990 * we're not allowed to leave a node with one item in the
1991 * tree during a delete. A deletion from lower in the tree
1992 * could try to delete the only pointer in this node.
1993 * So, pull some keys from the left.
1994 * There has to be a left pointer at this point because
1995 * otherwise we would have pulled some pointers from the
1996 * right
1997 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001998 if (!left) {
1999 ret = -EROFS;
Anand Jain34d97002016-03-16 16:43:06 +08002000 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07002001 goto enospc;
2002 }
Chris Mason5f39d392007-10-15 16:14:19 -04002003 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002004 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002005 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002006 goto enospc;
2007 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002008 if (wret == 1) {
2009 wret = push_node_left(trans, root, left, mid, 1);
2010 if (wret < 0)
2011 ret = wret;
2012 }
Chris Mason79f95c82007-03-01 15:16:26 -05002013 BUG_ON(wret == 1);
2014 }
Chris Mason5f39d392007-10-15 16:14:19 -04002015 if (btrfs_header_nritems(mid) == 0) {
Daniel Dressler01d58472014-11-21 17:15:07 +09002016 clean_tree_block(trans, root->fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002017 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002018 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002019 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002020 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002021 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002022 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002023 } else {
2024 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002025 struct btrfs_disk_key mid_key;
2026 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002027 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002028 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002029 btrfs_set_node_key(parent, &mid_key, pslot);
2030 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002031 }
Chris Masonbb803952007-03-01 12:04:21 -05002032
Chris Mason79f95c82007-03-01 15:16:26 -05002033 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002034 if (left) {
2035 if (btrfs_header_nritems(left) > orig_slot) {
2036 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002037 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002038 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002039 path->slots[level + 1] -= 1;
2040 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002041 if (mid) {
2042 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002043 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002044 }
Chris Masonbb803952007-03-01 12:04:21 -05002045 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002046 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002047 path->slots[level] = orig_slot;
2048 }
2049 }
Chris Mason79f95c82007-03-01 15:16:26 -05002050 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002051 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002052 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002053 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002054enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002055 if (right) {
2056 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002057 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002058 }
2059 if (left) {
2060 if (path->nodes[level] != left)
2061 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002062 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002063 }
Chris Masonbb803952007-03-01 12:04:21 -05002064 return ret;
2065}
2066
Chris Masond352ac62008-09-29 15:18:18 -04002067/* Node balancing for insertion. Here we only split or push nodes around
2068 * when they are completely full. This is also done top down, so we
2069 * have to be pessimistic.
2070 */
Chris Masond3977122009-01-05 21:25:51 -05002071static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002072 struct btrfs_root *root,
2073 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002074{
Chris Mason5f39d392007-10-15 16:14:19 -04002075 struct extent_buffer *right = NULL;
2076 struct extent_buffer *mid;
2077 struct extent_buffer *left = NULL;
2078 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002079 int ret = 0;
2080 int wret;
2081 int pslot;
2082 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002083
2084 if (level == 0)
2085 return 1;
2086
Chris Mason5f39d392007-10-15 16:14:19 -04002087 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002088 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002089
Li Zefana05a9bb2011-09-06 16:55:34 +08002090 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002091 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002092 pslot = path->slots[level + 1];
2093 }
Chris Masone66f7092007-04-20 13:16:02 -04002094
Chris Mason5f39d392007-10-15 16:14:19 -04002095 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002096 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002097
Chris Mason5f39d392007-10-15 16:14:19 -04002098 left = read_node_slot(root, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002099 if (IS_ERR(left))
2100 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002101
2102 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002103 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002104 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002105
2106 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002107 btrfs_set_lock_blocking(left);
2108
Chris Mason5f39d392007-10-15 16:14:19 -04002109 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002110 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2111 wret = 1;
2112 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002113 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002114 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002115 if (ret)
2116 wret = 1;
2117 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002118 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002119 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002120 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002121 }
Chris Masone66f7092007-04-20 13:16:02 -04002122 if (wret < 0)
2123 ret = wret;
2124 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002125 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002126 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002127 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002128 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002129 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002130 btrfs_set_node_key(parent, &disk_key, pslot);
2131 btrfs_mark_buffer_dirty(parent);
2132 if (btrfs_header_nritems(left) > orig_slot) {
2133 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002134 path->slots[level + 1] -= 1;
2135 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002136 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002137 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002138 } else {
2139 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002141 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002142 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002144 }
Chris Masone66f7092007-04-20 13:16:02 -04002145 return 0;
2146 }
Chris Mason925baed2008-06-25 16:01:30 -04002147 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002148 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002149 }
Chris Mason925baed2008-06-25 16:01:30 -04002150 right = read_node_slot(root, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002151 if (IS_ERR(right))
2152 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002153
2154 /*
2155 * then try to empty the right most buffer into the middle
2156 */
Chris Mason5f39d392007-10-15 16:14:19 -04002157 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002158 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002159
Chris Mason925baed2008-06-25 16:01:30 -04002160 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002161 btrfs_set_lock_blocking(right);
2162
Chris Mason5f39d392007-10-15 16:14:19 -04002163 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002164 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2165 wret = 1;
2166 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002167 ret = btrfs_cow_block(trans, root, right,
2168 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002169 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002170 if (ret)
2171 wret = 1;
2172 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002173 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002174 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002175 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002176 }
Chris Masone66f7092007-04-20 13:16:02 -04002177 if (wret < 0)
2178 ret = wret;
2179 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002180 struct btrfs_disk_key disk_key;
2181
2182 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002183 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002184 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002185 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2186 btrfs_mark_buffer_dirty(parent);
2187
2188 if (btrfs_header_nritems(mid) <= orig_slot) {
2189 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002190 path->slots[level + 1] += 1;
2191 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002192 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002193 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002194 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002195 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002196 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002197 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002198 }
Chris Masone66f7092007-04-20 13:16:02 -04002199 return 0;
2200 }
Chris Mason925baed2008-06-25 16:01:30 -04002201 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002202 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002203 }
Chris Masone66f7092007-04-20 13:16:02 -04002204 return 1;
2205}
2206
Chris Mason74123bd2007-02-02 11:05:29 -05002207/*
Chris Masond352ac62008-09-29 15:18:18 -04002208 * readahead one full node of leaves, finding things that are close
2209 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002210 */
Chris Masonc8c42862009-04-03 10:14:18 -04002211static void reada_for_search(struct btrfs_root *root,
2212 struct btrfs_path *path,
2213 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002214{
Chris Mason5f39d392007-10-15 16:14:19 -04002215 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002216 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002217 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002218 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002219 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002220 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002221 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002222 u32 nr;
2223 u32 blocksize;
2224 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002225
Chris Masona6b6e752007-10-15 16:22:39 -04002226 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002227 return;
2228
Chris Mason6702ed42007-08-07 16:15:09 -04002229 if (!path->nodes[level])
2230 return;
2231
Chris Mason5f39d392007-10-15 16:14:19 -04002232 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002233
Chris Mason3c69fae2007-08-07 15:52:22 -04002234 search = btrfs_node_blockptr(node, slot);
David Sterba707e8a02014-06-04 19:22:26 +02002235 blocksize = root->nodesize;
Daniel Dressler01d58472014-11-21 17:15:07 +09002236 eb = btrfs_find_tree_block(root->fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002237 if (eb) {
2238 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002239 return;
2240 }
2241
Chris Masona7175312009-01-22 09:23:10 -05002242 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002243
Chris Mason5f39d392007-10-15 16:14:19 -04002244 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002245 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002246
Chris Masond3977122009-01-05 21:25:51 -05002247 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002248 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002249 if (nr == 0)
2250 break;
2251 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002252 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002253 nr++;
2254 if (nr >= nritems)
2255 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002256 }
David Sterbae4058b52015-11-27 16:31:35 +01002257 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002258 btrfs_node_key(node, &disk_key, nr);
2259 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2260 break;
2261 }
Chris Mason6b800532007-10-15 16:17:34 -04002262 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002263 if ((search <= target && target - search <= 65536) ||
2264 (search > target && search - target <= 65536)) {
David Sterbad3e46fe2014-06-15 02:04:19 +02002265 readahead_tree_block(root, search);
Chris Mason6b800532007-10-15 16:17:34 -04002266 nread += blocksize;
2267 }
2268 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002269 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002270 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002271 }
2272}
Chris Mason925baed2008-06-25 16:01:30 -04002273
Josef Bacik0b088512013-06-17 14:23:02 -04002274static noinline void reada_for_balance(struct btrfs_root *root,
2275 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002276{
2277 int slot;
2278 int nritems;
2279 struct extent_buffer *parent;
2280 struct extent_buffer *eb;
2281 u64 gen;
2282 u64 block1 = 0;
2283 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002284
Chris Mason8c594ea2009-04-20 15:50:10 -04002285 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002286 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002287 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002288
2289 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002290 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002291
2292 if (slot > 0) {
2293 block1 = btrfs_node_blockptr(parent, slot - 1);
2294 gen = btrfs_node_ptr_generation(parent, slot - 1);
Daniel Dressler01d58472014-11-21 17:15:07 +09002295 eb = btrfs_find_tree_block(root->fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002296 /*
2297 * if we get -eagain from btrfs_buffer_uptodate, we
2298 * don't want to return eagain here. That will loop
2299 * forever
2300 */
2301 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002302 block1 = 0;
2303 free_extent_buffer(eb);
2304 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002305 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002306 block2 = btrfs_node_blockptr(parent, slot + 1);
2307 gen = btrfs_node_ptr_generation(parent, slot + 1);
Daniel Dressler01d58472014-11-21 17:15:07 +09002308 eb = btrfs_find_tree_block(root->fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002309 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002310 block2 = 0;
2311 free_extent_buffer(eb);
2312 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002313
Josef Bacik0b088512013-06-17 14:23:02 -04002314 if (block1)
David Sterbad3e46fe2014-06-15 02:04:19 +02002315 readahead_tree_block(root, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002316 if (block2)
David Sterbad3e46fe2014-06-15 02:04:19 +02002317 readahead_tree_block(root, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002318}
2319
2320
2321/*
Chris Masond3977122009-01-05 21:25:51 -05002322 * when we walk down the tree, it is usually safe to unlock the higher layers
2323 * in the tree. The exceptions are when our path goes through slot 0, because
2324 * operations on the tree might require changing key pointers higher up in the
2325 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002326 *
Chris Masond3977122009-01-05 21:25:51 -05002327 * callers might also have set path->keep_locks, which tells this code to keep
2328 * the lock if the path points to the last slot in the block. This is part of
2329 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002330 *
Chris Masond3977122009-01-05 21:25:51 -05002331 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2332 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002333 */
Chris Masone02119d2008-09-05 16:13:11 -04002334static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002335 int lowest_unlock, int min_write_lock_level,
2336 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002337{
2338 int i;
2339 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002340 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002341 struct extent_buffer *t;
2342
2343 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2344 if (!path->nodes[i])
2345 break;
2346 if (!path->locks[i])
2347 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002348 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002349 skip_level = i + 1;
2350 continue;
2351 }
Chris Mason051e1b92008-06-25 16:01:30 -04002352 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002353 u32 nritems;
2354 t = path->nodes[i];
2355 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002356 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002357 skip_level = i + 1;
2358 continue;
2359 }
2360 }
Chris Mason051e1b92008-06-25 16:01:30 -04002361 if (skip_level < i && i >= lowest_unlock)
2362 no_skips = 1;
2363
Chris Mason925baed2008-06-25 16:01:30 -04002364 t = path->nodes[i];
2365 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002366 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002367 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002368 if (write_lock_level &&
2369 i > min_write_lock_level &&
2370 i <= *write_lock_level) {
2371 *write_lock_level = i - 1;
2372 }
Chris Mason925baed2008-06-25 16:01:30 -04002373 }
2374 }
2375}
2376
Chris Mason3c69fae2007-08-07 15:52:22 -04002377/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002378 * This releases any locks held in the path starting at level and
2379 * going all the way up to the root.
2380 *
2381 * btrfs_search_slot will keep the lock held on higher nodes in a few
2382 * corner cases, such as COW of the block at slot zero in the node. This
2383 * ignores those rules, and it should only be called when there are no
2384 * more updates to be done higher up in the tree.
2385 */
2386noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2387{
2388 int i;
2389
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002390 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002391 return;
2392
2393 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2394 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002395 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002396 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002397 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002398 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002399 path->locks[i] = 0;
2400 }
2401}
2402
2403/*
Chris Masonc8c42862009-04-03 10:14:18 -04002404 * helper function for btrfs_search_slot. The goal is to find a block
2405 * in cache without setting the path to blocking. If we find the block
2406 * we return zero and the path is unchanged.
2407 *
2408 * If we can't find the block, we set the path blocking and do some
2409 * reada. -EAGAIN is returned and the search must be repeated.
2410 */
2411static int
2412read_block_for_search(struct btrfs_trans_handle *trans,
2413 struct btrfs_root *root, struct btrfs_path *p,
2414 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002415 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002416{
2417 u64 blocknr;
2418 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002419 struct extent_buffer *b = *eb_ret;
2420 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002421 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002422
2423 blocknr = btrfs_node_blockptr(b, slot);
2424 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002425
Daniel Dressler01d58472014-11-21 17:15:07 +09002426 tmp = btrfs_find_tree_block(root->fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002427 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002428 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002429 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2430 *eb_ret = tmp;
2431 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002432 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002433
2434 /* the pages were up to date, but we failed
2435 * the generation number check. Do a full
2436 * read for the generation number that is correct.
2437 * We must do this without dropping locks so
2438 * we can trust our generation number
2439 */
2440 btrfs_set_path_blocking(p);
2441
2442 /* now we're allowed to do a blocking uptodate check */
2443 ret = btrfs_read_buffer(tmp, gen);
2444 if (!ret) {
2445 *eb_ret = tmp;
2446 return 0;
2447 }
2448 free_extent_buffer(tmp);
2449 btrfs_release_path(p);
2450 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002451 }
2452
2453 /*
2454 * reduce lock contention at high levels
2455 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002456 * we read. Don't release the lock on the current
2457 * level because we need to walk this node to figure
2458 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002459 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002460 btrfs_unlock_up_safe(p, level + 1);
2461 btrfs_set_path_blocking(p);
2462
Chris Masoncb449212010-10-24 11:01:27 -04002463 free_extent_buffer(tmp);
David Sterbae4058b52015-11-27 16:31:35 +01002464 if (p->reada != READA_NONE)
Chris Masonc8c42862009-04-03 10:14:18 -04002465 reada_for_search(root, p, level, slot, key->objectid);
2466
Chris Mason76a05b32009-05-14 13:24:30 -04002467 ret = -EAGAIN;
Liu Bo298d5db2018-05-16 01:37:36 +08002468 tmp = read_tree_block(root, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08002469 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002470 /*
2471 * If the read above didn't mark this buffer up to date,
2472 * it will never end up being up to date. Set ret to EIO now
2473 * and give up so that our caller doesn't loop forever
2474 * on our EAGAINs.
2475 */
Chris Masonb9fab912012-05-06 07:23:47 -04002476 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002477 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002478 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002479 } else {
2480 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002481 }
Liu Bo298d5db2018-05-16 01:37:36 +08002482
2483 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002484 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002485}
2486
2487/*
2488 * helper function for btrfs_search_slot. This does all of the checks
2489 * for node-level blocks and does any balancing required based on
2490 * the ins_len.
2491 *
2492 * If no extra work was required, zero is returned. If we had to
2493 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2494 * start over
2495 */
2496static int
2497setup_nodes_for_search(struct btrfs_trans_handle *trans,
2498 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002499 struct extent_buffer *b, int level, int ins_len,
2500 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002501{
2502 int ret;
2503 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2504 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2505 int sret;
2506
Chris Masonbd681512011-07-16 15:23:14 -04002507 if (*write_lock_level < level + 1) {
2508 *write_lock_level = level + 1;
2509 btrfs_release_path(p);
2510 goto again;
2511 }
2512
Chris Masonc8c42862009-04-03 10:14:18 -04002513 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002514 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002515 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002516 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002517
2518 BUG_ON(sret > 0);
2519 if (sret) {
2520 ret = sret;
2521 goto done;
2522 }
2523 b = p->nodes[level];
2524 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002525 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002526 int sret;
2527
Chris Masonbd681512011-07-16 15:23:14 -04002528 if (*write_lock_level < level + 1) {
2529 *write_lock_level = level + 1;
2530 btrfs_release_path(p);
2531 goto again;
2532 }
2533
Chris Masonc8c42862009-04-03 10:14:18 -04002534 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002535 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002536 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002537 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002538
2539 if (sret) {
2540 ret = sret;
2541 goto done;
2542 }
2543 b = p->nodes[level];
2544 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002545 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002546 goto again;
2547 }
2548 BUG_ON(btrfs_header_nritems(b) == 1);
2549 }
2550 return 0;
2551
2552again:
2553 ret = -EAGAIN;
2554done:
2555 return ret;
2556}
2557
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002558static void key_search_validate(struct extent_buffer *b,
2559 struct btrfs_key *key,
2560 int level)
2561{
2562#ifdef CONFIG_BTRFS_ASSERT
2563 struct btrfs_disk_key disk_key;
2564
2565 btrfs_cpu_key_to_disk(&disk_key, key);
2566
2567 if (level == 0)
2568 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2569 offsetof(struct btrfs_leaf, items[0].key),
2570 sizeof(disk_key)));
2571 else
2572 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2573 offsetof(struct btrfs_node, ptrs[0].key),
2574 sizeof(disk_key)));
2575#endif
2576}
2577
2578static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2579 int level, int *prev_cmp, int *slot)
2580{
2581 if (*prev_cmp != 0) {
2582 *prev_cmp = bin_search(b, key, level, slot);
2583 return *prev_cmp;
2584 }
2585
2586 key_search_validate(b, key, level);
2587 *slot = 0;
2588
2589 return 0;
2590}
2591
David Sterba381cf652015-01-02 18:45:16 +01002592int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002593 u64 iobjectid, u64 ioff, u8 key_type,
2594 struct btrfs_key *found_key)
2595{
2596 int ret;
2597 struct btrfs_key key;
2598 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002599
2600 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002601 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002602
2603 key.type = key_type;
2604 key.objectid = iobjectid;
2605 key.offset = ioff;
2606
2607 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002608 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002609 return ret;
2610
2611 eb = path->nodes[0];
2612 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2613 ret = btrfs_next_leaf(fs_root, path);
2614 if (ret)
2615 return ret;
2616 eb = path->nodes[0];
2617 }
2618
2619 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2620 if (found_key->type != key.type ||
2621 found_key->objectid != key.objectid)
2622 return 1;
2623
2624 return 0;
2625}
2626
Chris Masonc8c42862009-04-03 10:14:18 -04002627/*
Chris Mason74123bd2007-02-02 11:05:29 -05002628 * look for key in the tree. path is filled in with nodes along the way
2629 * if key is found, we return zero and you can find the item in the leaf
2630 * level of the path (level 0)
2631 *
2632 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002633 * be inserted, and 1 is returned. If there are other errors during the
2634 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002635 *
2636 * if ins_len > 0, nodes and leaves will be split as we walk down the
2637 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2638 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002639 */
Chris Masone089f052007-03-16 16:20:31 -04002640int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2641 *root, struct btrfs_key *key, struct btrfs_path *p, int
2642 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002643{
Chris Mason5f39d392007-10-15 16:14:19 -04002644 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002645 int slot;
2646 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002647 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002648 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002649 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002650 int root_lock;
2651 /* everything at write_lock_level or lower must be write locked */
2652 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002653 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002654 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002655 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002656
Chris Mason6702ed42007-08-07 16:15:09 -04002657 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002658 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002659 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002660 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002661
Chris Masonbd681512011-07-16 15:23:14 -04002662 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002663 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002664
Chris Masonbd681512011-07-16 15:23:14 -04002665 /* when we are removing items, we might have to go up to level
2666 * two as we update tree pointers Make sure we keep write
2667 * for those levels as well
2668 */
2669 write_lock_level = 2;
2670 } else if (ins_len > 0) {
2671 /*
2672 * for inserting items, make sure we have a write lock on
2673 * level 1 so we can update keys
2674 */
2675 write_lock_level = 1;
2676 }
2677
2678 if (!cow)
2679 write_lock_level = -1;
2680
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002681 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002682 write_lock_level = BTRFS_MAX_LEVEL;
2683
Chris Masonf7c79f32012-03-19 15:54:38 -04002684 min_write_lock_level = write_lock_level;
2685
Chris Masonbb803952007-03-01 12:04:21 -05002686again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002687 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002688 /*
2689 * we try very hard to do read locks on the root
2690 */
2691 root_lock = BTRFS_READ_LOCK;
2692 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002693 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002694 /*
2695 * the commit roots are read only
2696 * so we always do read locks
2697 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002698 if (p->need_commit_sem)
2699 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002700 b = root->commit_root;
2701 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002702 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002703 if (p->need_commit_sem)
2704 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002706 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002708 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002709 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002710 level = btrfs_header_level(b);
2711 } else {
2712 /* we don't know the level of the root node
2713 * until we actually have it read locked
2714 */
2715 b = btrfs_read_lock_root_node(root);
2716 level = btrfs_header_level(b);
2717 if (level <= write_lock_level) {
2718 /* whoops, must trade for write lock */
2719 btrfs_tree_read_unlock(b);
2720 free_extent_buffer(b);
2721 b = btrfs_lock_root_node(root);
2722 root_lock = BTRFS_WRITE_LOCK;
2723
2724 /* the level might have changed, check again */
2725 level = btrfs_header_level(b);
2726 }
2727 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002728 }
Chris Masonbd681512011-07-16 15:23:14 -04002729 p->nodes[level] = b;
2730 if (!p->skip_locking)
2731 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002732
Chris Masoneb60cea2007-02-02 09:18:22 -05002733 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002734 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002735
2736 /*
2737 * setup the path here so we can release it under lock
2738 * contention with the cow code
2739 */
Chris Mason02217ed2007-03-02 16:08:05 -05002740 if (cow) {
Nikolay Borisove23c0972017-12-12 11:14:49 +02002741 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2742
Chris Masonc8c42862009-04-03 10:14:18 -04002743 /*
2744 * if we don't really need to cow this block
2745 * then we don't want to set the path blocking,
2746 * so we test it here
2747 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002748 if (!should_cow_block(trans, root, b)) {
2749 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002750 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002751 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002752
Chris Masonbd681512011-07-16 15:23:14 -04002753 /*
2754 * must have write locks on this node and the
2755 * parent
2756 */
Josef Bacik5124e002012-11-07 13:44:13 -05002757 if (level > write_lock_level ||
2758 (level + 1 > write_lock_level &&
2759 level + 1 < BTRFS_MAX_LEVEL &&
2760 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002761 write_lock_level = level + 1;
2762 btrfs_release_path(p);
2763 goto again;
2764 }
2765
Filipe Manana160f4082014-07-28 19:37:17 +01002766 btrfs_set_path_blocking(p);
Nikolay Borisove23c0972017-12-12 11:14:49 +02002767 if (last_level)
2768 err = btrfs_cow_block(trans, root, b, NULL, 0,
2769 &b);
2770 else
2771 err = btrfs_cow_block(trans, root, b,
2772 p->nodes[level + 1],
2773 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002774 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002775 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002776 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002777 }
Chris Mason02217ed2007-03-02 16:08:05 -05002778 }
Chris Mason65b51a02008-08-01 15:11:20 -04002779cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002780 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002781 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002782
2783 /*
2784 * we have a lock on b and as long as we aren't changing
2785 * the tree, there is no way to for the items in b to change.
2786 * It is safe to drop the lock on our parent before we
2787 * go through the expensive btree search on b.
2788 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002789 * If we're inserting or deleting (ins_len != 0), then we might
2790 * be changing slot zero, which may require changing the parent.
2791 * So, we can't drop the lock until after we know which slot
2792 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002793 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002794 if (!ins_len && !p->keep_locks) {
2795 int u = level + 1;
2796
2797 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2798 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2799 p->locks[u] = 0;
2800 }
2801 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002802
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002803 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002804 if (ret < 0)
2805 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002806
Chris Mason5f39d392007-10-15 16:14:19 -04002807 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002808 int dec = 0;
2809 if (ret && slot > 0) {
2810 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002811 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002812 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002813 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002814 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002815 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002816 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002817 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002818 if (err) {
2819 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002820 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002821 }
Chris Masonc8c42862009-04-03 10:14:18 -04002822 b = p->nodes[level];
2823 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002824
Chris Masonbd681512011-07-16 15:23:14 -04002825 /*
2826 * slot 0 is special, if we change the key
2827 * we have to update the parent pointer
2828 * which means we must have a write lock
2829 * on the parent
2830 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002831 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002832 write_lock_level < level + 1) {
2833 write_lock_level = level + 1;
2834 btrfs_release_path(p);
2835 goto again;
2836 }
2837
Chris Masonf7c79f32012-03-19 15:54:38 -04002838 unlock_up(p, level, lowest_unlock,
2839 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002840
Chris Mason925baed2008-06-25 16:01:30 -04002841 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002842 if (dec)
2843 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002844 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002845 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002846
Yan Zheng33c66f42009-07-22 09:59:00 -04002847 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002848 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002849 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002850 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002851 if (err) {
2852 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002853 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002854 }
Chris Mason76a05b32009-05-14 13:24:30 -04002855
Chris Masonb4ce94d2009-02-04 09:25:08 -05002856 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002857 level = btrfs_header_level(b);
2858 if (level <= write_lock_level) {
2859 err = btrfs_try_tree_write_lock(b);
2860 if (!err) {
2861 btrfs_set_path_blocking(p);
2862 btrfs_tree_lock(b);
2863 btrfs_clear_path_blocking(p, b,
2864 BTRFS_WRITE_LOCK);
2865 }
2866 p->locks[level] = BTRFS_WRITE_LOCK;
2867 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002868 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002869 if (!err) {
2870 btrfs_set_path_blocking(p);
2871 btrfs_tree_read_lock(b);
2872 btrfs_clear_path_blocking(p, b,
2873 BTRFS_READ_LOCK);
2874 }
2875 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002876 }
Chris Masonbd681512011-07-16 15:23:14 -04002877 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002878 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002879 } else {
2880 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002881 if (ins_len > 0 &&
2882 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002883 if (write_lock_level < 1) {
2884 write_lock_level = 1;
2885 btrfs_release_path(p);
2886 goto again;
2887 }
2888
Chris Masonb4ce94d2009-02-04 09:25:08 -05002889 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002890 err = split_leaf(trans, root, key,
2891 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002892 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002893
Yan Zheng33c66f42009-07-22 09:59:00 -04002894 BUG_ON(err > 0);
2895 if (err) {
2896 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002897 goto done;
2898 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002899 }
Chris Mason459931e2008-12-10 09:10:46 -05002900 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002901 unlock_up(p, level, lowest_unlock,
2902 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002903 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002904 }
2905 }
Chris Mason65b51a02008-08-01 15:11:20 -04002906 ret = 1;
2907done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002908 /*
2909 * we don't really know what they plan on doing with the path
2910 * from here on, so for now just mark it as blocking
2911 */
Chris Masonb9473432009-03-13 11:00:37 -04002912 if (!p->leave_spinning)
2913 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002914 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002915 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002916 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002917}
2918
Chris Mason74123bd2007-02-02 11:05:29 -05002919/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002920 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2921 * current state of the tree together with the operations recorded in the tree
2922 * modification log to search for the key in a previous version of this tree, as
2923 * denoted by the time_seq parameter.
2924 *
2925 * Naturally, there is no support for insert, delete or cow operations.
2926 *
2927 * The resulting path and return value will be set up as if we called
2928 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2929 */
2930int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2931 struct btrfs_path *p, u64 time_seq)
2932{
2933 struct extent_buffer *b;
2934 int slot;
2935 int ret;
2936 int err;
2937 int level;
2938 int lowest_unlock = 1;
2939 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002940 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002941
2942 lowest_level = p->lowest_level;
2943 WARN_ON(p->nodes[0] != NULL);
2944
2945 if (p->search_commit_root) {
2946 BUG_ON(time_seq);
2947 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2948 }
2949
2950again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002951 b = get_old_root(root, time_seq);
Nikolay Borisov56af2412018-09-13 11:35:10 +03002952 if (!b) {
2953 ret = -EIO;
2954 goto done;
2955 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002956 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002957 p->locks[level] = BTRFS_READ_LOCK;
2958
2959 while (b) {
2960 level = btrfs_header_level(b);
2961 p->nodes[level] = b;
2962 btrfs_clear_path_blocking(p, NULL, 0);
2963
2964 /*
2965 * we have a lock on b and as long as we aren't changing
2966 * the tree, there is no way to for the items in b to change.
2967 * It is safe to drop the lock on our parent before we
2968 * go through the expensive btree search on b.
2969 */
2970 btrfs_unlock_up_safe(p, level + 1);
2971
Josef Bacikd4b40872013-09-24 14:09:34 -04002972 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002973 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002974 * time.
2975 */
2976 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002977 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002978
2979 if (level != 0) {
2980 int dec = 0;
2981 if (ret && slot > 0) {
2982 dec = 1;
2983 slot -= 1;
2984 }
2985 p->slots[level] = slot;
2986 unlock_up(p, level, lowest_unlock, 0, NULL);
2987
2988 if (level == lowest_level) {
2989 if (dec)
2990 p->slots[level]++;
2991 goto done;
2992 }
2993
2994 err = read_block_for_search(NULL, root, p, &b, level,
2995 slot, key, time_seq);
2996 if (err == -EAGAIN)
2997 goto again;
2998 if (err) {
2999 ret = err;
3000 goto done;
3001 }
3002
3003 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003004 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003005 if (!err) {
3006 btrfs_set_path_blocking(p);
3007 btrfs_tree_read_lock(b);
3008 btrfs_clear_path_blocking(p, b,
3009 BTRFS_READ_LOCK);
3010 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003011 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003012 if (!b) {
3013 ret = -ENOMEM;
3014 goto done;
3015 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003016 p->locks[level] = BTRFS_READ_LOCK;
3017 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003018 } else {
3019 p->slots[level] = slot;
3020 unlock_up(p, level, lowest_unlock, 0, NULL);
3021 goto done;
3022 }
3023 }
3024 ret = 1;
3025done:
3026 if (!p->leave_spinning)
3027 btrfs_set_path_blocking(p);
3028 if (ret < 0)
3029 btrfs_release_path(p);
3030
3031 return ret;
3032}
3033
3034/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003035 * helper to use instead of search slot if no exact match is needed but
3036 * instead the next or previous item should be returned.
3037 * When find_higher is true, the next higher item is returned, the next lower
3038 * otherwise.
3039 * When return_any and find_higher are both true, and no higher item is found,
3040 * return the next lower instead.
3041 * When return_any is true and find_higher is false, and no lower item is found,
3042 * return the next higher instead.
3043 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3044 * < 0 on error
3045 */
3046int btrfs_search_slot_for_read(struct btrfs_root *root,
3047 struct btrfs_key *key, struct btrfs_path *p,
3048 int find_higher, int return_any)
3049{
3050 int ret;
3051 struct extent_buffer *leaf;
3052
3053again:
3054 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3055 if (ret <= 0)
3056 return ret;
3057 /*
3058 * a return value of 1 means the path is at the position where the
3059 * item should be inserted. Normally this is the next bigger item,
3060 * but in case the previous item is the last in a leaf, path points
3061 * to the first free slot in the previous leaf, i.e. at an invalid
3062 * item.
3063 */
3064 leaf = p->nodes[0];
3065
3066 if (find_higher) {
3067 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3068 ret = btrfs_next_leaf(root, p);
3069 if (ret <= 0)
3070 return ret;
3071 if (!return_any)
3072 return 1;
3073 /*
3074 * no higher item found, return the next
3075 * lower instead
3076 */
3077 return_any = 0;
3078 find_higher = 0;
3079 btrfs_release_path(p);
3080 goto again;
3081 }
3082 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003083 if (p->slots[0] == 0) {
3084 ret = btrfs_prev_leaf(root, p);
3085 if (ret < 0)
3086 return ret;
3087 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003088 leaf = p->nodes[0];
3089 if (p->slots[0] == btrfs_header_nritems(leaf))
3090 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003091 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003092 }
Arne Jansene6793762011-09-13 11:18:10 +02003093 if (!return_any)
3094 return 1;
3095 /*
3096 * no lower item found, return the next
3097 * higher instead
3098 */
3099 return_any = 0;
3100 find_higher = 1;
3101 btrfs_release_path(p);
3102 goto again;
3103 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003104 --p->slots[0];
3105 }
3106 }
3107 return 0;
3108}
3109
3110/*
Chris Mason74123bd2007-02-02 11:05:29 -05003111 * adjust the pointers going up the tree, starting at level
3112 * making sure the right key of each node is points to 'key'.
3113 * This is used after shifting pointers to the left, so it stops
3114 * fixing up pointers when a given leaf/node is not in slot 0 of the
3115 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003116 *
Chris Mason74123bd2007-02-02 11:05:29 -05003117 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003118static void fixup_low_keys(struct btrfs_fs_info *fs_info,
3119 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003120 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003121{
3122 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003123 struct extent_buffer *t;
3124
Chris Mason234b63a2007-03-13 10:46:10 -04003125 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003126 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003127 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003128 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003129 t = path->nodes[i];
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003130 tree_mod_log_set_node_key(fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003131 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003132 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003133 if (tslot != 0)
3134 break;
3135 }
3136}
3137
Chris Mason74123bd2007-02-02 11:05:29 -05003138/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003139 * update item key.
3140 *
3141 * This function isn't completely safe. It's the caller's responsibility
3142 * that the new key won't break the order
3143 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003144void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3145 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003146 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003147{
3148 struct btrfs_disk_key disk_key;
3149 struct extent_buffer *eb;
3150 int slot;
3151
3152 eb = path->nodes[0];
3153 slot = path->slots[0];
3154 if (slot > 0) {
3155 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003156 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003157 }
3158 if (slot < btrfs_header_nritems(eb) - 1) {
3159 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003160 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003161 }
3162
3163 btrfs_cpu_key_to_disk(&disk_key, new_key);
3164 btrfs_set_item_key(eb, &disk_key, slot);
3165 btrfs_mark_buffer_dirty(eb);
3166 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003167 fixup_low_keys(fs_info, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003168}
3169
3170/*
Chris Mason74123bd2007-02-02 11:05:29 -05003171 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003172 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003173 *
3174 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3175 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003176 */
Chris Mason98ed5172008-01-03 10:01:48 -05003177static int push_node_left(struct btrfs_trans_handle *trans,
3178 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003179 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003180{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003181 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003182 int src_nritems;
3183 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003184 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003185
Chris Mason5f39d392007-10-15 16:14:19 -04003186 src_nritems = btrfs_header_nritems(src);
3187 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003188 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003189 WARN_ON(btrfs_header_generation(src) != trans->transid);
3190 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003191
Chris Masonbce4eae2008-04-24 14:42:46 -04003192 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003193 return 1;
3194
Chris Masond3977122009-01-05 21:25:51 -05003195 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003196 return 1;
3197
Chris Masonbce4eae2008-04-24 14:42:46 -04003198 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003199 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003200 if (push_items < src_nritems) {
3201 /* leave at least 8 pointers in the node if
3202 * we aren't going to empty it
3203 */
3204 if (src_nritems - push_items < 8) {
3205 if (push_items <= 8)
3206 return 1;
3207 push_items -= 8;
3208 }
3209 }
3210 } else
3211 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003212
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003213 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3214 push_items);
3215 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003216 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003217 return ret;
3218 }
Chris Mason5f39d392007-10-15 16:14:19 -04003219 copy_extent_buffer(dst, src,
3220 btrfs_node_key_ptr_offset(dst_nritems),
3221 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003222 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003223
Chris Masonbb803952007-03-01 12:04:21 -05003224 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003225 /*
3226 * don't call tree_mod_log_eb_move here, key removal was already
3227 * fully logged by tree_mod_log_eb_copy above.
3228 */
Chris Mason5f39d392007-10-15 16:14:19 -04003229 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3230 btrfs_node_key_ptr_offset(push_items),
3231 (src_nritems - push_items) *
3232 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003233 }
Chris Mason5f39d392007-10-15 16:14:19 -04003234 btrfs_set_header_nritems(src, src_nritems - push_items);
3235 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3236 btrfs_mark_buffer_dirty(src);
3237 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003238
Chris Masonbb803952007-03-01 12:04:21 -05003239 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003240}
3241
Chris Mason97571fd2007-02-24 13:39:08 -05003242/*
Chris Mason79f95c82007-03-01 15:16:26 -05003243 * try to push data from one node into the next node right in the
3244 * tree.
3245 *
3246 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3247 * error, and > 0 if there was no room in the right hand block.
3248 *
3249 * this will only push up to 1/2 the contents of the left node over
3250 */
Chris Mason5f39d392007-10-15 16:14:19 -04003251static int balance_node_right(struct btrfs_trans_handle *trans,
3252 struct btrfs_root *root,
3253 struct extent_buffer *dst,
3254 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003255{
Chris Mason79f95c82007-03-01 15:16:26 -05003256 int push_items = 0;
3257 int max_push;
3258 int src_nritems;
3259 int dst_nritems;
3260 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003261
Chris Mason7bb86312007-12-11 09:25:06 -05003262 WARN_ON(btrfs_header_generation(src) != trans->transid);
3263 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3264
Chris Mason5f39d392007-10-15 16:14:19 -04003265 src_nritems = btrfs_header_nritems(src);
3266 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003267 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003268 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003269 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003270
Chris Masond3977122009-01-05 21:25:51 -05003271 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003272 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003273
3274 max_push = src_nritems / 2 + 1;
3275 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003276 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003277 return 1;
Yan252c38f2007-08-29 09:11:44 -04003278
Chris Mason79f95c82007-03-01 15:16:26 -05003279 if (max_push < push_items)
3280 push_items = max_push;
3281
Jan Schmidtf2304752012-05-26 11:43:17 +02003282 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003283 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3284 btrfs_node_key_ptr_offset(0),
3285 (dst_nritems) *
3286 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003287
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003288 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3289 src_nritems - push_items, push_items);
3290 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003291 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003292 return ret;
3293 }
Chris Mason5f39d392007-10-15 16:14:19 -04003294 copy_extent_buffer(dst, src,
3295 btrfs_node_key_ptr_offset(0),
3296 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003297 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003298
Chris Mason5f39d392007-10-15 16:14:19 -04003299 btrfs_set_header_nritems(src, src_nritems - push_items);
3300 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003301
Chris Mason5f39d392007-10-15 16:14:19 -04003302 btrfs_mark_buffer_dirty(src);
3303 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003304
Chris Mason79f95c82007-03-01 15:16:26 -05003305 return ret;
3306}
3307
3308/*
Chris Mason97571fd2007-02-24 13:39:08 -05003309 * helper function to insert a new root level in the tree.
3310 * A new node is allocated, and a single item is inserted to
3311 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003312 *
3313 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003314 */
Chris Masond3977122009-01-05 21:25:51 -05003315static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003316 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003317 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003318{
Chris Mason7bb86312007-12-11 09:25:06 -05003319 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003320 struct extent_buffer *lower;
3321 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003322 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003323 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003324
3325 BUG_ON(path->nodes[level]);
3326 BUG_ON(path->nodes[level-1] != root->node);
3327
Chris Mason7bb86312007-12-11 09:25:06 -05003328 lower = path->nodes[level-1];
3329 if (level == 1)
3330 btrfs_item_key(lower, &lower_key, 0);
3331 else
3332 btrfs_node_key(lower, &lower_key, 0);
3333
David Sterba4d75f8a2014-06-15 01:54:12 +02003334 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3335 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003336 if (IS_ERR(c))
3337 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003338
Yan, Zhengf0486c62010-05-16 10:46:25 -04003339 root_add_used(root, root->nodesize);
3340
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003341 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003342 btrfs_set_header_nritems(c, 1);
3343 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003344 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003345 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003346 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003347 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003348
Ross Kirk0a4e5582013-09-24 10:12:38 +01003349 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003350 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003351
3352 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003353 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003354
Chris Mason5f39d392007-10-15 16:14:19 -04003355 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003356 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003357 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003358 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003359
3360 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003361
3362 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003363
Chris Mason925baed2008-06-25 16:01:30 -04003364 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003365 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003366 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003367
3368 /* the super has an extra ref to root->node */
3369 free_extent_buffer(old);
3370
Chris Mason0b86a832008-03-24 15:01:56 -04003371 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003372 extent_buffer_get(c);
3373 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303374 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003375 path->slots[level] = 0;
3376 return 0;
3377}
3378
Chris Mason74123bd2007-02-02 11:05:29 -05003379/*
3380 * worker function to insert a single pointer in a node.
3381 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003382 *
Chris Mason74123bd2007-02-02 11:05:29 -05003383 * slot and level indicate where you want the key to go, and
3384 * blocknr is the block the key points to.
3385 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003386static void insert_ptr(struct btrfs_trans_handle *trans,
3387 struct btrfs_root *root, struct btrfs_path *path,
3388 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003389 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003390{
Chris Mason5f39d392007-10-15 16:14:19 -04003391 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003392 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003393 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003394
3395 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003396 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003397 lower = path->nodes[level];
3398 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003399 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003400 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003401 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003402 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003403 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3404 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003405 memmove_extent_buffer(lower,
3406 btrfs_node_key_ptr_offset(slot + 1),
3407 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003408 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003409 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003410 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003411 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003412 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003413 BUG_ON(ret < 0);
3414 }
Chris Mason5f39d392007-10-15 16:14:19 -04003415 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003416 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003417 WARN_ON(trans->transid == 0);
3418 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003419 btrfs_set_header_nritems(lower, nritems + 1);
3420 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003421}
3422
Chris Mason97571fd2007-02-24 13:39:08 -05003423/*
3424 * split the node at the specified level in path in two.
3425 * The path is corrected to point to the appropriate node after the split
3426 *
3427 * Before splitting this tries to make some room in the node by pushing
3428 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003429 *
3430 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003431 */
Chris Masone02119d2008-09-05 16:13:11 -04003432static noinline int split_node(struct btrfs_trans_handle *trans,
3433 struct btrfs_root *root,
3434 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003435{
Chris Mason5f39d392007-10-15 16:14:19 -04003436 struct extent_buffer *c;
3437 struct extent_buffer *split;
3438 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003439 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003440 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003441 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003442
Chris Mason5f39d392007-10-15 16:14:19 -04003443 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003444 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003445 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003446 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003447 * trying to split the root, lets make a new one
3448 *
Liu Bofdd99c72013-05-22 12:06:51 +00003449 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003450 * insert_new_root, because that root buffer will be kept as a
3451 * normal node. We are going to log removal of half of the
3452 * elements below with tree_mod_log_eb_copy. We're holding a
3453 * tree lock on the buffer, which is why we cannot race with
3454 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003455 */
Liu Bofdd99c72013-05-22 12:06:51 +00003456 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003457 if (ret)
3458 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003459 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003460 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003461 c = path->nodes[level];
3462 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003463 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003464 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003465 if (ret < 0)
3466 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003467 }
Chris Masone66f7092007-04-20 13:16:02 -04003468
Chris Mason5f39d392007-10-15 16:14:19 -04003469 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003470 mid = (c_nritems + 1) / 2;
3471 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003472
David Sterba4d75f8a2014-06-15 01:54:12 +02003473 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3474 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003475 if (IS_ERR(split))
3476 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003477
Yan, Zhengf0486c62010-05-16 10:46:25 -04003478 root_add_used(root, root->nodesize);
3479
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003480 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003481 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003482 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003483 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003484 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003485 btrfs_set_header_owner(split, root->root_key.objectid);
3486 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003487 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003488 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003489 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003490 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003491
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003492 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3493 mid, c_nritems - mid);
3494 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003495 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003496 return ret;
3497 }
Chris Mason5f39d392007-10-15 16:14:19 -04003498 copy_extent_buffer(split, c,
3499 btrfs_node_key_ptr_offset(0),
3500 btrfs_node_key_ptr_offset(mid),
3501 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3502 btrfs_set_header_nritems(split, c_nritems - mid);
3503 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003504 ret = 0;
3505
Chris Mason5f39d392007-10-15 16:14:19 -04003506 btrfs_mark_buffer_dirty(c);
3507 btrfs_mark_buffer_dirty(split);
3508
Jeff Mahoney143bede2012-03-01 14:56:26 +01003509 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003510 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003511
Chris Mason5de08d72007-02-24 06:24:44 -05003512 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003513 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003514 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003515 free_extent_buffer(c);
3516 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003517 path->slots[level + 1] += 1;
3518 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003519 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003520 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003521 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003522 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003523}
3524
Chris Mason74123bd2007-02-02 11:05:29 -05003525/*
3526 * how many bytes are required to store the items in a leaf. start
3527 * and nr indicate which items in the leaf to check. This totals up the
3528 * space used both by the item structs and the item data
3529 */
Chris Mason5f39d392007-10-15 16:14:19 -04003530static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003531{
Josef Bacik41be1f32012-10-15 13:43:18 -04003532 struct btrfs_item *start_item;
3533 struct btrfs_item *end_item;
3534 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003535 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003536 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003537 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003538
3539 if (!nr)
3540 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003541 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003542 start_item = btrfs_item_nr(start);
3543 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003544 data_len = btrfs_token_item_offset(l, start_item, &token) +
3545 btrfs_token_item_size(l, start_item, &token);
3546 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003547 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003548 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003549 return data_len;
3550}
3551
Chris Mason74123bd2007-02-02 11:05:29 -05003552/*
Chris Masond4dbff92007-04-04 14:08:15 -04003553 * The space between the end of the leaf items and
3554 * the start of the leaf data. IOW, how much room
3555 * the leaf has left for both items and data
3556 */
Chris Masond3977122009-01-05 21:25:51 -05003557noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003558 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003559{
Chris Mason5f39d392007-10-15 16:14:19 -04003560 int nritems = btrfs_header_nritems(leaf);
3561 int ret;
3562 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3563 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003564 btrfs_crit(root->fs_info,
3565 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003566 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003567 leaf_space_used(leaf, 0, nritems), nritems);
3568 }
3569 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003570}
3571
Chris Mason99d8f832010-07-07 10:51:48 -04003572/*
3573 * min slot controls the lowest index we're willing to push to the
3574 * right. We'll push up to and including min_slot, but no lower
3575 */
Chris Mason44871b12009-03-13 10:04:31 -04003576static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3577 struct btrfs_root *root,
3578 struct btrfs_path *path,
3579 int data_size, int empty,
3580 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003581 int free_space, u32 left_nritems,
3582 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003583{
Chris Mason5f39d392007-10-15 16:14:19 -04003584 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003585 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003586 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003587 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003588 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003589 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003590 int push_space = 0;
3591 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003592 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003593 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003594 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003595 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003596 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003597
Chris Masoncfed81a2012-03-03 07:40:03 -05003598 btrfs_init_map_token(&token);
3599
Chris Mason34a38212007-11-07 13:31:03 -05003600 if (empty)
3601 nr = 0;
3602 else
Chris Mason99d8f832010-07-07 10:51:48 -04003603 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003604
Zheng Yan31840ae2008-09-23 13:14:14 -04003605 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003606 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003607
Chris Mason44871b12009-03-13 10:04:31 -04003608 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003609 i = left_nritems - 1;
3610 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003611 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003612
Zheng Yan31840ae2008-09-23 13:14:14 -04003613 if (!empty && push_items > 0) {
3614 if (path->slots[0] > i)
3615 break;
3616 if (path->slots[0] == i) {
3617 int space = btrfs_leaf_free_space(root, left);
3618 if (space + push_space * 2 > free_space)
3619 break;
3620 }
3621 }
3622
Chris Mason00ec4c52007-02-24 12:47:20 -05003623 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003624 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003625
Chris Masondb945352007-10-15 16:15:53 -04003626 this_item_size = btrfs_item_size(left, item);
3627 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003628 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003629
Chris Mason00ec4c52007-02-24 12:47:20 -05003630 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003631 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003632 if (i == 0)
3633 break;
3634 i--;
Chris Masondb945352007-10-15 16:15:53 -04003635 }
Chris Mason5f39d392007-10-15 16:14:19 -04003636
Chris Mason925baed2008-06-25 16:01:30 -04003637 if (push_items == 0)
3638 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003639
Julia Lawall6c1500f2012-11-03 20:30:18 +00003640 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003641
Chris Mason00ec4c52007-02-24 12:47:20 -05003642 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003643 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003644
Chris Mason5f39d392007-10-15 16:14:19 -04003645 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003646 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003647
Chris Mason00ec4c52007-02-24 12:47:20 -05003648 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003649 data_end = leaf_data_end(root, right);
3650 memmove_extent_buffer(right,
3651 btrfs_leaf_data(right) + data_end - push_space,
3652 btrfs_leaf_data(right) + data_end,
3653 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3654
Chris Mason00ec4c52007-02-24 12:47:20 -05003655 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003656 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003657 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3658 btrfs_leaf_data(left) + leaf_data_end(root, left),
3659 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003660
3661 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3662 btrfs_item_nr_offset(0),
3663 right_nritems * sizeof(struct btrfs_item));
3664
Chris Mason00ec4c52007-02-24 12:47:20 -05003665 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003666 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3667 btrfs_item_nr_offset(left_nritems - push_items),
3668 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003669
3670 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003671 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003672 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003673 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003674 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003675 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003676 push_space -= btrfs_token_item_size(right, item, &token);
3677 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003678 }
3679
Chris Mason7518a232007-03-12 12:01:18 -04003680 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003681 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003682
Chris Mason34a38212007-11-07 13:31:03 -05003683 if (left_nritems)
3684 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003685 else
Daniel Dressler01d58472014-11-21 17:15:07 +09003686 clean_tree_block(trans, root->fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003687
Chris Mason5f39d392007-10-15 16:14:19 -04003688 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003689
Chris Mason5f39d392007-10-15 16:14:19 -04003690 btrfs_item_key(right, &disk_key, 0);
3691 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003692 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003693
Chris Mason00ec4c52007-02-24 12:47:20 -05003694 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003695 if (path->slots[0] >= left_nritems) {
3696 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003697 if (btrfs_header_nritems(path->nodes[0]) == 0)
Daniel Dressler01d58472014-11-21 17:15:07 +09003698 clean_tree_block(trans, root->fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003699 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003700 free_extent_buffer(path->nodes[0]);
3701 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003702 path->slots[1] += 1;
3703 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003704 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003705 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003706 }
3707 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003708
3709out_unlock:
3710 btrfs_tree_unlock(right);
3711 free_extent_buffer(right);
3712 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003713}
Chris Mason925baed2008-06-25 16:01:30 -04003714
Chris Mason00ec4c52007-02-24 12:47:20 -05003715/*
Chris Mason44871b12009-03-13 10:04:31 -04003716 * push some data in the path leaf to the right, trying to free up at
3717 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3718 *
3719 * returns 1 if the push failed because the other node didn't have enough
3720 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003721 *
3722 * this will push starting from min_slot to the end of the leaf. It won't
3723 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003724 */
3725static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003726 *root, struct btrfs_path *path,
3727 int min_data_size, int data_size,
3728 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003729{
3730 struct extent_buffer *left = path->nodes[0];
3731 struct extent_buffer *right;
3732 struct extent_buffer *upper;
3733 int slot;
3734 int free_space;
3735 u32 left_nritems;
3736 int ret;
3737
3738 if (!path->nodes[1])
3739 return 1;
3740
3741 slot = path->slots[1];
3742 upper = path->nodes[1];
3743 if (slot >= btrfs_header_nritems(upper) - 1)
3744 return 1;
3745
3746 btrfs_assert_tree_locked(path->nodes[1]);
3747
3748 right = read_node_slot(root, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003749 /*
3750 * slot + 1 is not valid or we fail to read the right node,
3751 * no big deal, just return.
3752 */
3753 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003754 return 1;
3755
Chris Mason44871b12009-03-13 10:04:31 -04003756 btrfs_tree_lock(right);
3757 btrfs_set_lock_blocking(right);
3758
3759 free_space = btrfs_leaf_free_space(root, right);
3760 if (free_space < data_size)
3761 goto out_unlock;
3762
3763 /* cow and double check */
3764 ret = btrfs_cow_block(trans, root, right, upper,
3765 slot + 1, &right);
3766 if (ret)
3767 goto out_unlock;
3768
3769 free_space = btrfs_leaf_free_space(root, right);
3770 if (free_space < data_size)
3771 goto out_unlock;
3772
3773 left_nritems = btrfs_header_nritems(left);
3774 if (left_nritems == 0)
3775 goto out_unlock;
3776
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003777 if (path->slots[0] == left_nritems && !empty) {
3778 /* Key greater than all keys in the leaf, right neighbor has
3779 * enough room for it and we're not emptying our leaf to delete
3780 * it, therefore use right neighbor to insert the new item and
3781 * no need to touch/dirty our left leaft. */
3782 btrfs_tree_unlock(left);
3783 free_extent_buffer(left);
3784 path->nodes[0] = right;
3785 path->slots[0] = 0;
3786 path->slots[1]++;
3787 return 0;
3788 }
3789
Chris Mason99d8f832010-07-07 10:51:48 -04003790 return __push_leaf_right(trans, root, path, min_data_size, empty,
3791 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003792out_unlock:
3793 btrfs_tree_unlock(right);
3794 free_extent_buffer(right);
3795 return 1;
3796}
3797
3798/*
Chris Mason74123bd2007-02-02 11:05:29 -05003799 * push some data in the path leaf to the left, trying to free up at
3800 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003801 *
3802 * max_slot can put a limit on how far into the leaf we'll push items. The
3803 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3804 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003805 */
Chris Mason44871b12009-03-13 10:04:31 -04003806static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3807 struct btrfs_root *root,
3808 struct btrfs_path *path, int data_size,
3809 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003810 int free_space, u32 right_nritems,
3811 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003812{
Chris Mason5f39d392007-10-15 16:14:19 -04003813 struct btrfs_disk_key disk_key;
3814 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003815 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003816 int push_space = 0;
3817 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003818 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003819 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003820 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003821 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003822 u32 this_item_size;
3823 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003824 struct btrfs_map_token token;
3825
3826 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003827
Chris Mason34a38212007-11-07 13:31:03 -05003828 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003829 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003830 else
Chris Mason99d8f832010-07-07 10:51:48 -04003831 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003832
3833 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003834 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003835
Zheng Yan31840ae2008-09-23 13:14:14 -04003836 if (!empty && push_items > 0) {
3837 if (path->slots[0] < i)
3838 break;
3839 if (path->slots[0] == i) {
3840 int space = btrfs_leaf_free_space(root, right);
3841 if (space + push_space * 2 > free_space)
3842 break;
3843 }
3844 }
3845
Chris Masonbe0e5c02007-01-26 15:51:26 -05003846 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003847 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003848
3849 this_item_size = btrfs_item_size(right, item);
3850 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851 break;
Chris Masondb945352007-10-15 16:15:53 -04003852
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003854 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 }
Chris Masondb945352007-10-15 16:15:53 -04003856
Chris Masonbe0e5c02007-01-26 15:51:26 -05003857 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003858 ret = 1;
3859 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003860 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303861 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003862
Chris Masonbe0e5c02007-01-26 15:51:26 -05003863 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003864 copy_extent_buffer(left, right,
3865 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3866 btrfs_item_nr_offset(0),
3867 push_items * sizeof(struct btrfs_item));
3868
Chris Mason123abc82007-03-14 14:14:43 -04003869 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003870 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003871
3872 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003873 leaf_data_end(root, left) - push_space,
3874 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003875 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003876 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003877 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003878 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003879
Chris Masondb945352007-10-15 16:15:53 -04003880 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003881 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003882 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003883
Ross Kirkdd3cc162013-09-16 15:58:09 +01003884 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003885
Chris Masoncfed81a2012-03-03 07:40:03 -05003886 ioff = btrfs_token_item_offset(left, item, &token);
3887 btrfs_set_token_item_offset(left, item,
3888 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3889 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003890 }
Chris Mason5f39d392007-10-15 16:14:19 -04003891 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003892
3893 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003894 if (push_items > right_nritems)
3895 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003896 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003897
Chris Mason34a38212007-11-07 13:31:03 -05003898 if (push_items < right_nritems) {
3899 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3900 leaf_data_end(root, right);
3901 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3902 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3903 btrfs_leaf_data(right) +
3904 leaf_data_end(root, right), push_space);
3905
3906 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003907 btrfs_item_nr_offset(push_items),
3908 (btrfs_header_nritems(right) - push_items) *
3909 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003910 }
Yaneef1c492007-11-26 10:58:13 -05003911 right_nritems -= push_items;
3912 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003913 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003914 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003915 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003916
Chris Masoncfed81a2012-03-03 07:40:03 -05003917 push_space = push_space - btrfs_token_item_size(right,
3918 item, &token);
3919 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003920 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003921
Chris Mason5f39d392007-10-15 16:14:19 -04003922 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003923 if (right_nritems)
3924 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003925 else
Daniel Dressler01d58472014-11-21 17:15:07 +09003926 clean_tree_block(trans, root->fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003927
Chris Mason5f39d392007-10-15 16:14:19 -04003928 btrfs_item_key(right, &disk_key, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003929 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003930
3931 /* then fixup the leaf pointer in the path */
3932 if (path->slots[0] < push_items) {
3933 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003934 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003935 free_extent_buffer(path->nodes[0]);
3936 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003937 path->slots[1] -= 1;
3938 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003939 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003940 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003941 path->slots[0] -= push_items;
3942 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003943 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003944 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003945out:
3946 btrfs_tree_unlock(left);
3947 free_extent_buffer(left);
3948 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003949}
3950
Chris Mason74123bd2007-02-02 11:05:29 -05003951/*
Chris Mason44871b12009-03-13 10:04:31 -04003952 * push some data in the path leaf to the left, trying to free up at
3953 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003954 *
3955 * max_slot can put a limit on how far into the leaf we'll push items. The
3956 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3957 * items
Chris Mason44871b12009-03-13 10:04:31 -04003958 */
3959static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003960 *root, struct btrfs_path *path, int min_data_size,
3961 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003962{
3963 struct extent_buffer *right = path->nodes[0];
3964 struct extent_buffer *left;
3965 int slot;
3966 int free_space;
3967 u32 right_nritems;
3968 int ret = 0;
3969
3970 slot = path->slots[1];
3971 if (slot == 0)
3972 return 1;
3973 if (!path->nodes[1])
3974 return 1;
3975
3976 right_nritems = btrfs_header_nritems(right);
3977 if (right_nritems == 0)
3978 return 1;
3979
3980 btrfs_assert_tree_locked(path->nodes[1]);
3981
3982 left = read_node_slot(root, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003983 /*
3984 * slot - 1 is not valid or we fail to read the left node,
3985 * no big deal, just return.
3986 */
3987 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003988 return 1;
3989
Chris Mason44871b12009-03-13 10:04:31 -04003990 btrfs_tree_lock(left);
3991 btrfs_set_lock_blocking(left);
3992
3993 free_space = btrfs_leaf_free_space(root, left);
3994 if (free_space < data_size) {
3995 ret = 1;
3996 goto out;
3997 }
3998
3999 /* cow and double check */
4000 ret = btrfs_cow_block(trans, root, left,
4001 path->nodes[1], slot - 1, &left);
4002 if (ret) {
4003 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004004 if (ret == -ENOSPC)
4005 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004006 goto out;
4007 }
4008
4009 free_space = btrfs_leaf_free_space(root, left);
4010 if (free_space < data_size) {
4011 ret = 1;
4012 goto out;
4013 }
4014
Chris Mason99d8f832010-07-07 10:51:48 -04004015 return __push_leaf_left(trans, root, path, min_data_size,
4016 empty, left, free_space, right_nritems,
4017 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004018out:
4019 btrfs_tree_unlock(left);
4020 free_extent_buffer(left);
4021 return ret;
4022}
4023
4024/*
Chris Mason74123bd2007-02-02 11:05:29 -05004025 * split the path's leaf in two, making sure there is at least data_size
4026 * available for the resulting leaf level of the path.
4027 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004028static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4029 struct btrfs_root *root,
4030 struct btrfs_path *path,
4031 struct extent_buffer *l,
4032 struct extent_buffer *right,
4033 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004034{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004035 int data_copy_size;
4036 int rt_data_off;
4037 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004038 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004039 struct btrfs_map_token token;
4040
4041 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004042
Chris Mason5f39d392007-10-15 16:14:19 -04004043 nritems = nritems - mid;
4044 btrfs_set_header_nritems(right, nritems);
4045 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4046
4047 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4048 btrfs_item_nr_offset(mid),
4049 nritems * sizeof(struct btrfs_item));
4050
4051 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004052 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4053 data_copy_size, btrfs_leaf_data(l) +
4054 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004055
Chris Mason5f39d392007-10-15 16:14:19 -04004056 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4057 btrfs_item_end_nr(l, mid);
4058
4059 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004060 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004061 u32 ioff;
4062
Chris Masoncfed81a2012-03-03 07:40:03 -05004063 ioff = btrfs_token_item_offset(right, item, &token);
4064 btrfs_set_token_item_offset(right, item,
4065 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004066 }
Chris Mason74123bd2007-02-02 11:05:29 -05004067
Chris Mason5f39d392007-10-15 16:14:19 -04004068 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004069 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004070 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004071 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004072
4073 btrfs_mark_buffer_dirty(right);
4074 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004075 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004076
Chris Masonbe0e5c02007-01-26 15:51:26 -05004077 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004078 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004079 free_extent_buffer(path->nodes[0]);
4080 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004081 path->slots[0] -= mid;
4082 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004083 } else {
4084 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004085 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004086 }
Chris Mason5f39d392007-10-15 16:14:19 -04004087
Chris Masoneb60cea2007-02-02 09:18:22 -05004088 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004089}
4090
4091/*
Chris Mason99d8f832010-07-07 10:51:48 -04004092 * double splits happen when we need to insert a big item in the middle
4093 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4094 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4095 * A B C
4096 *
4097 * We avoid this by trying to push the items on either side of our target
4098 * into the adjacent leaves. If all goes well we can avoid the double split
4099 * completely.
4100 */
4101static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4102 struct btrfs_root *root,
4103 struct btrfs_path *path,
4104 int data_size)
4105{
4106 int ret;
4107 int progress = 0;
4108 int slot;
4109 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004110 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004111
4112 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004113 if (slot < btrfs_header_nritems(path->nodes[0]))
4114 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004115
4116 /*
4117 * try to push all the items after our slot into the
4118 * right leaf
4119 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004120 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004121 if (ret < 0)
4122 return ret;
4123
4124 if (ret == 0)
4125 progress++;
4126
4127 nritems = btrfs_header_nritems(path->nodes[0]);
4128 /*
4129 * our goal is to get our slot at the start or end of a leaf. If
4130 * we've done so we're done
4131 */
4132 if (path->slots[0] == 0 || path->slots[0] == nritems)
4133 return 0;
4134
4135 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4136 return 0;
4137
4138 /* try to push all the items before our slot into the next leaf */
4139 slot = path->slots[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,
4160 struct btrfs_key *ins_key,
4161 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) +
4180 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4181 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))
4188 space_needed -= btrfs_leaf_free_space(root, l);
4189
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 David Borba Manana5a4267c2013-11-25 03:20:46 +00004195 wret = push_leaf_left(trans, root, path, space_needed,
4196 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004197 if (wret < 0)
4198 return wret;
4199 }
4200 l = path->nodes[0];
4201
4202 /* did the pushes work? */
4203 if (btrfs_leaf_free_space(root, l) >= data_size)
4204 return 0;
4205 }
4206
4207 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004208 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004209 if (ret)
4210 return ret;
4211 }
4212again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004213 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004214 l = path->nodes[0];
4215 slot = path->slots[0];
4216 nritems = btrfs_header_nritems(l);
4217 mid = (nritems + 1) / 2;
4218
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004219 if (mid <= slot) {
4220 if (nritems == 1 ||
4221 leaf_space_used(l, mid, nritems - mid) + data_size >
4222 BTRFS_LEAF_DATA_SIZE(root)) {
4223 if (slot >= nritems) {
4224 split = 0;
4225 } else {
4226 mid = slot;
4227 if (mid != nritems &&
4228 leaf_space_used(l, mid, nritems - mid) +
4229 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004230 if (data_size && !tried_avoid_double)
4231 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004232 split = 2;
4233 }
4234 }
4235 }
4236 } else {
4237 if (leaf_space_used(l, 0, mid) + data_size >
4238 BTRFS_LEAF_DATA_SIZE(root)) {
4239 if (!extend && data_size && slot == 0) {
4240 split = 0;
4241 } else if ((extend || !data_size) && slot == 0) {
4242 mid = 1;
4243 } else {
4244 mid = slot;
4245 if (mid != nritems &&
4246 leaf_space_used(l, mid, nritems - mid) +
4247 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004248 if (data_size && !tried_avoid_double)
4249 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304250 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004251 }
4252 }
4253 }
4254 }
4255
4256 if (split == 0)
4257 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4258 else
4259 btrfs_item_key(l, &disk_key, mid);
4260
David Sterba4d75f8a2014-06-15 01:54:12 +02004261 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4262 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004263 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004264 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004265
David Sterba707e8a02014-06-04 19:22:26 +02004266 root_add_used(root, root->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004267
4268 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4269 btrfs_set_header_bytenr(right, right->start);
4270 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004271 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004272 btrfs_set_header_owner(right, root->root_key.objectid);
4273 btrfs_set_header_level(right, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004274 write_extent_buffer(right, fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004275 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004276
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004277 write_extent_buffer(right, fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004278 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004279 BTRFS_UUID_SIZE);
4280
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004281 if (split == 0) {
4282 if (mid <= slot) {
4283 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004284 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004285 path->slots[1] + 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;
4290 path->slots[1] += 1;
4291 } else {
4292 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004293 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004294 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004295 btrfs_tree_unlock(path->nodes[0]);
4296 free_extent_buffer(path->nodes[0]);
4297 path->nodes[0] = right;
4298 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004299 if (path->slots[1] == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004300 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004301 }
Liu Bo196e0242016-09-07 14:48:28 -07004302 /*
4303 * We create a new leaf 'right' for the required ins_len and
4304 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4305 * the content of ins_len to 'right'.
4306 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004307 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004308 }
4309
Jeff Mahoney143bede2012-03-01 14:56:26 +01004310 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004311
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004313 BUG_ON(num_doubles != 0);
4314 num_doubles++;
4315 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004316 }
Chris Mason44871b12009-03-13 10:04:31 -04004317
Jeff Mahoney143bede2012-03-01 14:56:26 +01004318 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004319
4320push_for_double:
4321 push_for_double_split(trans, root, path, data_size);
4322 tried_avoid_double = 1;
4323 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4324 return 0;
4325 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004326}
4327
Yan, Zhengad48fd752009-11-12 09:33:58 +00004328static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4329 struct btrfs_root *root,
4330 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004331{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004332 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004333 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004334 struct btrfs_file_extent_item *fi;
4335 u64 extent_len = 0;
4336 u32 item_size;
4337 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004338
4339 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4341
4342 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4343 key.type != BTRFS_EXTENT_CSUM_KEY);
4344
4345 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4346 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004347
4348 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004349 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4350 fi = btrfs_item_ptr(leaf, path->slots[0],
4351 struct btrfs_file_extent_item);
4352 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4353 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004354 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004355
Chris Mason459931e2008-12-10 09:10:46 -05004356 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004357 path->search_for_split = 1;
4358 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004359 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004360 if (ret > 0)
4361 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004362 if (ret < 0)
4363 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004364
Yan, Zhengad48fd752009-11-12 09:33:58 +00004365 ret = -EAGAIN;
4366 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004367 /* if our item isn't there, return now */
4368 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004369 goto err;
4370
Chris Mason109f6ae2010-04-02 09:20:18 -04004371 /* the leaf has changed, it now has room. return now */
4372 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4373 goto err;
4374
Yan, Zhengad48fd752009-11-12 09:33:58 +00004375 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4376 fi = btrfs_item_ptr(leaf, path->slots[0],
4377 struct btrfs_file_extent_item);
4378 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4379 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004380 }
4381
Chris Masonb9473432009-03-13 11:00:37 -04004382 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004383 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004384 if (ret)
4385 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004386
Yan, Zhengad48fd752009-11-12 09:33:58 +00004387 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004388 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004389 return 0;
4390err:
4391 path->keep_locks = 0;
4392 return ret;
4393}
4394
4395static noinline int split_item(struct btrfs_trans_handle *trans,
4396 struct btrfs_root *root,
4397 struct btrfs_path *path,
4398 struct btrfs_key *new_key,
4399 unsigned long split_offset)
4400{
4401 struct extent_buffer *leaf;
4402 struct btrfs_item *item;
4403 struct btrfs_item *new_item;
4404 int slot;
4405 char *buf;
4406 u32 nritems;
4407 u32 item_size;
4408 u32 orig_offset;
4409 struct btrfs_disk_key disk_key;
4410
Chris Masonb9473432009-03-13 11:00:37 -04004411 leaf = path->nodes[0];
4412 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4413
Chris Masonb4ce94d2009-02-04 09:25:08 -05004414 btrfs_set_path_blocking(path);
4415
Ross Kirkdd3cc162013-09-16 15:58:09 +01004416 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004417 orig_offset = btrfs_item_offset(leaf, item);
4418 item_size = btrfs_item_size(leaf, item);
4419
Chris Mason459931e2008-12-10 09:10:46 -05004420 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004421 if (!buf)
4422 return -ENOMEM;
4423
Chris Mason459931e2008-12-10 09:10:46 -05004424 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4425 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004426
Chris Mason459931e2008-12-10 09:10:46 -05004427 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004428 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004429 if (slot != nritems) {
4430 /* shift the items */
4431 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004432 btrfs_item_nr_offset(slot),
4433 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004434 }
4435
4436 btrfs_cpu_key_to_disk(&disk_key, new_key);
4437 btrfs_set_item_key(leaf, &disk_key, slot);
4438
Ross Kirkdd3cc162013-09-16 15:58:09 +01004439 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004440
4441 btrfs_set_item_offset(leaf, new_item, orig_offset);
4442 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4443
4444 btrfs_set_item_offset(leaf, item,
4445 orig_offset + item_size - split_offset);
4446 btrfs_set_item_size(leaf, item, split_offset);
4447
4448 btrfs_set_header_nritems(leaf, nritems + 1);
4449
4450 /* write the data for the start of the original item */
4451 write_extent_buffer(leaf, buf,
4452 btrfs_item_ptr_offset(leaf, path->slots[0]),
4453 split_offset);
4454
4455 /* write the data for the new item */
4456 write_extent_buffer(leaf, buf + split_offset,
4457 btrfs_item_ptr_offset(leaf, slot),
4458 item_size - split_offset);
4459 btrfs_mark_buffer_dirty(leaf);
4460
Yan, Zhengad48fd752009-11-12 09:33:58 +00004461 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004462 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004463 return 0;
4464}
4465
4466/*
4467 * This function splits a single item into two items,
4468 * giving 'new_key' to the new item and splitting the
4469 * old one at split_offset (from the start of the item).
4470 *
4471 * The path may be released by this operation. After
4472 * the split, the path is pointing to the old item. The
4473 * new item is going to be in the same node as the old one.
4474 *
4475 * Note, the item being split must be smaller enough to live alone on
4476 * a tree block with room for one extra struct btrfs_item
4477 *
4478 * This allows us to split the item in place, keeping a lock on the
4479 * leaf the entire time.
4480 */
4481int btrfs_split_item(struct btrfs_trans_handle *trans,
4482 struct btrfs_root *root,
4483 struct btrfs_path *path,
4484 struct btrfs_key *new_key,
4485 unsigned long split_offset)
4486{
4487 int ret;
4488 ret = setup_leaf_for_split(trans, root, path,
4489 sizeof(struct btrfs_item));
4490 if (ret)
4491 return ret;
4492
4493 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004494 return ret;
4495}
4496
4497/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004498 * This function duplicate a item, giving 'new_key' to the new item.
4499 * It guarantees both items live in the same tree leaf and the new item
4500 * is contiguous with the original item.
4501 *
4502 * This allows us to split file extent in place, keeping a lock on the
4503 * leaf the entire time.
4504 */
4505int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4506 struct btrfs_root *root,
4507 struct btrfs_path *path,
4508 struct btrfs_key *new_key)
4509{
4510 struct extent_buffer *leaf;
4511 int ret;
4512 u32 item_size;
4513
4514 leaf = path->nodes[0];
4515 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4516 ret = setup_leaf_for_split(trans, root, path,
4517 item_size + sizeof(struct btrfs_item));
4518 if (ret)
4519 return ret;
4520
4521 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004522 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004523 item_size, item_size +
4524 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004525 leaf = path->nodes[0];
4526 memcpy_extent_buffer(leaf,
4527 btrfs_item_ptr_offset(leaf, path->slots[0]),
4528 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4529 item_size);
4530 return 0;
4531}
4532
4533/*
Chris Masond352ac62008-09-29 15:18:18 -04004534 * make the item pointed to by the path smaller. new_size indicates
4535 * how small to make it, and from_end tells us if we just chop bytes
4536 * off the end of the item or if we shift the item to chop bytes off
4537 * the front.
4538 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004539void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004540 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004541{
Chris Masonb18c6682007-04-17 13:26:50 -04004542 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004543 struct extent_buffer *leaf;
4544 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004545 u32 nritems;
4546 unsigned int data_end;
4547 unsigned int old_data_start;
4548 unsigned int old_size;
4549 unsigned int size_diff;
4550 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004551 struct btrfs_map_token token;
4552
4553 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004554
Chris Mason5f39d392007-10-15 16:14:19 -04004555 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004556 slot = path->slots[0];
4557
4558 old_size = btrfs_item_size_nr(leaf, slot);
4559 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004560 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004561
Chris Mason5f39d392007-10-15 16:14:19 -04004562 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004563 data_end = leaf_data_end(root, leaf);
4564
Chris Mason5f39d392007-10-15 16:14:19 -04004565 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004566
Chris Masonb18c6682007-04-17 13:26:50 -04004567 size_diff = old_size - new_size;
4568
4569 BUG_ON(slot < 0);
4570 BUG_ON(slot >= nritems);
4571
4572 /*
4573 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4574 */
4575 /* first correct the data pointers */
4576 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004577 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004578 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004579
Chris Masoncfed81a2012-03-03 07:40:03 -05004580 ioff = btrfs_token_item_offset(leaf, item, &token);
4581 btrfs_set_token_item_offset(leaf, item,
4582 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004583 }
Chris Masondb945352007-10-15 16:15:53 -04004584
Chris Masonb18c6682007-04-17 13:26:50 -04004585 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004586 if (from_end) {
4587 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4588 data_end + size_diff, btrfs_leaf_data(leaf) +
4589 data_end, old_data_start + new_size - data_end);
4590 } else {
4591 struct btrfs_disk_key disk_key;
4592 u64 offset;
4593
4594 btrfs_item_key(leaf, &disk_key, slot);
4595
4596 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4597 unsigned long ptr;
4598 struct btrfs_file_extent_item *fi;
4599
4600 fi = btrfs_item_ptr(leaf, slot,
4601 struct btrfs_file_extent_item);
4602 fi = (struct btrfs_file_extent_item *)(
4603 (unsigned long)fi - size_diff);
4604
4605 if (btrfs_file_extent_type(leaf, fi) ==
4606 BTRFS_FILE_EXTENT_INLINE) {
4607 ptr = btrfs_item_ptr_offset(leaf, slot);
4608 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004609 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004610 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004611 }
4612 }
4613
4614 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4615 data_end + size_diff, btrfs_leaf_data(leaf) +
4616 data_end, old_data_start - data_end);
4617
4618 offset = btrfs_disk_key_offset(&disk_key);
4619 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4620 btrfs_set_item_key(leaf, &disk_key, slot);
4621 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004622 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004623 }
Chris Mason5f39d392007-10-15 16:14:19 -04004624
Ross Kirkdd3cc162013-09-16 15:58:09 +01004625 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004626 btrfs_set_item_size(leaf, item, new_size);
4627 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004628
Chris Mason5f39d392007-10-15 16:14:19 -04004629 if (btrfs_leaf_free_space(root, leaf) < 0) {
4630 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004631 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004632 }
Chris Masonb18c6682007-04-17 13:26:50 -04004633}
4634
Chris Masond352ac62008-09-29 15:18:18 -04004635/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004636 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004637 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004638void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004639 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004640{
Chris Mason6567e832007-04-16 09:22:45 -04004641 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004642 struct extent_buffer *leaf;
4643 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004644 u32 nritems;
4645 unsigned int data_end;
4646 unsigned int old_data;
4647 unsigned int old_size;
4648 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004649 struct btrfs_map_token token;
4650
4651 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004652
Chris Mason5f39d392007-10-15 16:14:19 -04004653 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004654
Chris Mason5f39d392007-10-15 16:14:19 -04004655 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004656 data_end = leaf_data_end(root, leaf);
4657
Chris Mason5f39d392007-10-15 16:14:19 -04004658 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4659 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004660 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004661 }
Chris Mason6567e832007-04-16 09:22:45 -04004662 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004663 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004664
4665 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004666 if (slot >= nritems) {
4667 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004668 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004669 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004670 BUG_ON(1);
4671 }
Chris Mason6567e832007-04-16 09:22:45 -04004672
4673 /*
4674 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4675 */
4676 /* first correct the data pointers */
4677 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004678 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004679 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004680
Chris Masoncfed81a2012-03-03 07:40:03 -05004681 ioff = btrfs_token_item_offset(leaf, item, &token);
4682 btrfs_set_token_item_offset(leaf, item,
4683 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004684 }
Chris Mason5f39d392007-10-15 16:14:19 -04004685
Chris Mason6567e832007-04-16 09:22:45 -04004686 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004687 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004688 data_end - data_size, btrfs_leaf_data(leaf) +
4689 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004690
Chris Mason6567e832007-04-16 09:22:45 -04004691 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004692 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004693 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004694 btrfs_set_item_size(leaf, item, old_size + data_size);
4695 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004696
Chris Mason5f39d392007-10-15 16:14:19 -04004697 if (btrfs_leaf_free_space(root, leaf) < 0) {
4698 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004699 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004700 }
Chris Mason6567e832007-04-16 09:22:45 -04004701}
4702
Chris Mason74123bd2007-02-02 11:05:29 -05004703/*
Chris Mason44871b12009-03-13 10:04:31 -04004704 * this is a helper for btrfs_insert_empty_items, the main goal here is
4705 * to save stack depth by doing the bulk of the work in a function
4706 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004707 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004708void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004709 struct btrfs_key *cpu_key, u32 *data_size,
4710 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004711{
Chris Mason5f39d392007-10-15 16:14:19 -04004712 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004713 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004714 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004716 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004717 struct extent_buffer *leaf;
4718 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004719 struct btrfs_map_token token;
4720
Filipe Manana24cdc842014-07-28 19:34:35 +01004721 if (path->slots[0] == 0) {
4722 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004723 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004724 }
4725 btrfs_unlock_up_safe(path, 1);
4726
Chris Masoncfed81a2012-03-03 07:40:03 -05004727 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004728
Chris Mason5f39d392007-10-15 16:14:19 -04004729 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004730 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004731
Chris Mason5f39d392007-10-15 16:14:19 -04004732 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004733 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004734
Chris Masonf25956c2008-09-12 15:32:53 -04004735 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004736 btrfs_print_leaf(root, leaf);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004737 btrfs_crit(root->fs_info,
4738 "not enough freespace need %u have %d",
4739 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004740 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004741 }
Chris Mason5f39d392007-10-15 16:14:19 -04004742
Chris Masonbe0e5c02007-01-26 15:51:26 -05004743 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004744 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004745
Chris Mason5f39d392007-10-15 16:14:19 -04004746 if (old_data < data_end) {
4747 btrfs_print_leaf(root, leaf);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004748 btrfs_crit(root->fs_info,
4749 "slot %d old_data %d data_end %d",
4750 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004751 BUG_ON(1);
4752 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004753 /*
4754 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4755 */
4756 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004757 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004758 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004759
Jeff Mahoney62e85572016-09-20 10:05:01 -04004760 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004761 ioff = btrfs_token_item_offset(leaf, item, &token);
4762 btrfs_set_token_item_offset(leaf, item,
4763 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004764 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004765 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004766 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004767 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004768 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004769
4770 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004771 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004772 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004773 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004774 data_end = old_data;
4775 }
Chris Mason5f39d392007-10-15 16:14:19 -04004776
Chris Mason62e27492007-03-15 12:56:47 -04004777 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004778 for (i = 0; i < nr; i++) {
4779 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4780 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004781 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004782 btrfs_set_token_item_offset(leaf, item,
4783 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004784 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004785 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004786 }
Chris Mason44871b12009-03-13 10:04:31 -04004787
Chris Mason9c583092008-01-29 15:15:18 -05004788 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004789 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004790
Chris Mason5f39d392007-10-15 16:14:19 -04004791 if (btrfs_leaf_free_space(root, leaf) < 0) {
4792 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004793 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004794 }
Chris Mason44871b12009-03-13 10:04:31 -04004795}
4796
4797/*
4798 * Given a key and some data, insert items into the tree.
4799 * This does all the path init required, making room in the tree if needed.
4800 */
4801int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4802 struct btrfs_root *root,
4803 struct btrfs_path *path,
4804 struct btrfs_key *cpu_key, u32 *data_size,
4805 int nr)
4806{
Chris Mason44871b12009-03-13 10:04:31 -04004807 int ret = 0;
4808 int slot;
4809 int i;
4810 u32 total_size = 0;
4811 u32 total_data = 0;
4812
4813 for (i = 0; i < nr; i++)
4814 total_data += data_size[i];
4815
4816 total_size = total_data + (nr * sizeof(struct btrfs_item));
4817 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4818 if (ret == 0)
4819 return -EEXIST;
4820 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004821 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004822
Chris Mason44871b12009-03-13 10:04:31 -04004823 slot = path->slots[0];
4824 BUG_ON(slot < 0);
4825
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004826 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004827 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004828 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004829}
4830
4831/*
4832 * Given a key and some data, insert an item into the tree.
4833 * This does all the path init required, making room in the tree if needed.
4834 */
Chris Masone089f052007-03-16 16:20:31 -04004835int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4836 *root, struct btrfs_key *cpu_key, void *data, u32
4837 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004838{
4839 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004840 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004841 struct extent_buffer *leaf;
4842 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004843
Chris Mason2c90e5d2007-04-02 10:50:19 -04004844 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004845 if (!path)
4846 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004847 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004848 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004849 leaf = path->nodes[0];
4850 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4851 write_extent_buffer(leaf, data, ptr, data_size);
4852 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004853 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004854 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004855 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004856}
4857
Chris Mason74123bd2007-02-02 11:05:29 -05004858/*
Chris Mason5de08d72007-02-24 06:24:44 -05004859 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004860 *
Chris Masond352ac62008-09-29 15:18:18 -04004861 * the tree should have been previously balanced so the deletion does not
4862 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004863 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004864static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4865 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004866{
Chris Mason5f39d392007-10-15 16:14:19 -04004867 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004868 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004869 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004870
Chris Mason5f39d392007-10-15 16:14:19 -04004871 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004872 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004873 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004874 tree_mod_log_eb_move(root->fs_info, parent, slot,
4875 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004876 memmove_extent_buffer(parent,
4877 btrfs_node_key_ptr_offset(slot),
4878 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004879 sizeof(struct btrfs_key_ptr) *
4880 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004881 } else if (level) {
4882 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004883 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004884 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004885 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004886
Chris Mason7518a232007-03-12 12:01:18 -04004887 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004888 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004889 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004890 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004891 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004892 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004893 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004894 struct btrfs_disk_key disk_key;
4895
4896 btrfs_node_key(parent, &disk_key, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004897 fixup_low_keys(root->fs_info, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004898 }
Chris Masond6025572007-03-30 14:27:56 -04004899 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004900}
4901
Chris Mason74123bd2007-02-02 11:05:29 -05004902/*
Chris Mason323ac952008-10-01 19:05:46 -04004903 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004904 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004905 *
4906 * This deletes the pointer in path->nodes[1] and frees the leaf
4907 * block extent. zero is returned if it all worked out, < 0 otherwise.
4908 *
4909 * The path must have already been setup for deleting the leaf, including
4910 * all the proper balancing. path->nodes[1] must be locked.
4911 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004912static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4913 struct btrfs_root *root,
4914 struct btrfs_path *path,
4915 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004916{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004917 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004918 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004919
Chris Mason4d081c42009-02-04 09:31:28 -05004920 /*
4921 * btrfs_free_extent is expensive, we want to make sure we
4922 * aren't holding any locks when we call it
4923 */
4924 btrfs_unlock_up_safe(path, 0);
4925
Yan, Zhengf0486c62010-05-16 10:46:25 -04004926 root_sub_used(root, leaf->len);
4927
Josef Bacik3083ee22012-03-09 16:01:49 -05004928 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004929 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004930 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004931}
4932/*
Chris Mason74123bd2007-02-02 11:05:29 -05004933 * delete the item at the leaf level in path. If that empties
4934 * the leaf, remove it from the tree
4935 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004936int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4937 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004938{
Chris Mason5f39d392007-10-15 16:14:19 -04004939 struct extent_buffer *leaf;
4940 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004941 u32 last_off;
4942 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004943 int ret = 0;
4944 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004945 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004946 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004947 struct btrfs_map_token token;
4948
4949 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004950
Chris Mason5f39d392007-10-15 16:14:19 -04004951 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004952 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4953
4954 for (i = 0; i < nr; i++)
4955 dsize += btrfs_item_size_nr(leaf, slot + i);
4956
Chris Mason5f39d392007-10-15 16:14:19 -04004957 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004958
Chris Mason85e21ba2008-01-29 15:11:36 -05004959 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004960 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004961
4962 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004963 data_end + dsize,
4964 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004965 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004966
Chris Mason85e21ba2008-01-29 15:11:36 -05004967 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004968 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004969
Ross Kirkdd3cc162013-09-16 15:58:09 +01004970 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004971 ioff = btrfs_token_item_offset(leaf, item, &token);
4972 btrfs_set_token_item_offset(leaf, item,
4973 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004974 }
Chris Masondb945352007-10-15 16:15:53 -04004975
Chris Mason5f39d392007-10-15 16:14:19 -04004976 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004977 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004978 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004979 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004980 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004981 btrfs_set_header_nritems(leaf, nritems - nr);
4982 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004983
Chris Mason74123bd2007-02-02 11:05:29 -05004984 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004985 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004986 if (leaf == root->node) {
4987 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004988 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004989 btrfs_set_path_blocking(path);
Daniel Dressler01d58472014-11-21 17:15:07 +09004990 clean_tree_block(trans, root->fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004991 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004992 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004993 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004994 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004995 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004996 struct btrfs_disk_key disk_key;
4997
4998 btrfs_item_key(leaf, &disk_key, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004999 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005000 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005001
Chris Mason74123bd2007-02-02 11:05:29 -05005002 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04005003 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005004 /* push_leaf_left fixes the path.
5005 * make sure the path still points to our leaf
5006 * for possible call to del_ptr below
5007 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005008 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005009 extent_buffer_get(leaf);
5010
Chris Masonb9473432009-03-13 11:00:37 -04005011 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005012 wret = push_leaf_left(trans, root, path, 1, 1,
5013 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005014 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005015 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005016
5017 if (path->nodes[0] == leaf &&
5018 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005019 wret = push_leaf_right(trans, root, path, 1,
5020 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005021 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005022 ret = wret;
5023 }
Chris Mason5f39d392007-10-15 16:14:19 -04005024
5025 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005026 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005027 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005028 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005029 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005030 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005031 /* if we're still in the path, make sure
5032 * we're dirty. Otherwise, one of the
5033 * push_leaf functions must have already
5034 * dirtied this buffer
5035 */
5036 if (path->nodes[0] == leaf)
5037 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005038 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005039 }
Chris Masond5719762007-03-23 10:01:08 -04005040 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005041 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005042 }
5043 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005044 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005045}
5046
Chris Mason97571fd2007-02-24 13:39:08 -05005047/*
Chris Mason925baed2008-06-25 16:01:30 -04005048 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005049 * returns 0 if it found something or 1 if there are no lesser leaves.
5050 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005051 *
5052 * This may release the path, and so you may lose any locks held at the
5053 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005054 */
Josef Bacik16e75492013-10-22 12:18:51 -04005055int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005056{
Chris Mason925baed2008-06-25 16:01:30 -04005057 struct btrfs_key key;
5058 struct btrfs_disk_key found_key;
5059 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005060
Chris Mason925baed2008-06-25 16:01:30 -04005061 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005062
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005063 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005064 key.offset--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005065 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005066 key.type--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005067 key.offset = (u64)-1;
5068 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005069 key.objectid--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005070 key.type = (u8)-1;
5071 key.offset = (u64)-1;
5072 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005073 return 1;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005074 }
Chris Mason7bb86312007-12-11 09:25:06 -05005075
David Sterbab3b4aa72011-04-21 01:20:15 +02005076 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005077 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5078 if (ret < 0)
5079 return ret;
5080 btrfs_item_key(path->nodes[0], &found_key, 0);
5081 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005082 /*
5083 * We might have had an item with the previous key in the tree right
5084 * before we released our path. And after we released our path, that
5085 * item might have been pushed to the first slot (0) of the leaf we
5086 * were holding due to a tree balance. Alternatively, an item with the
5087 * previous key can exist as the only element of a leaf (big fat item).
5088 * Therefore account for these 2 cases, so that our callers (like
5089 * btrfs_previous_item) don't miss an existing item with a key matching
5090 * the previous key we computed above.
5091 */
5092 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005093 return 0;
5094 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005095}
5096
Chris Mason3f157a22008-06-25 16:01:31 -04005097/*
5098 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005099 * for nodes or leaves that are have a minimum transaction id.
5100 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005101 *
5102 * This does not cow, but it does stuff the starting key it finds back
5103 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5104 * key and get a writable path.
5105 *
5106 * This does lock as it descends, and path->keep_locks should be set
5107 * to 1 by the caller.
5108 *
5109 * This honors path->lowest_level to prevent descent past a given level
5110 * of the tree.
5111 *
Chris Masond352ac62008-09-29 15:18:18 -04005112 * min_trans indicates the oldest transaction that you are interested
5113 * in walking through. Any nodes or leaves older than min_trans are
5114 * skipped over (without reading them).
5115 *
Chris Mason3f157a22008-06-25 16:01:31 -04005116 * returns zero if something useful was found, < 0 on error and 1 if there
5117 * was nothing in the tree that matched the search criteria.
5118 */
5119int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005120 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005121 u64 min_trans)
5122{
5123 struct extent_buffer *cur;
5124 struct btrfs_key found_key;
5125 int slot;
Yan96524802008-07-24 12:19:49 -04005126 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005127 u32 nritems;
5128 int level;
5129 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005130 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005131
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005132 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005133again:
Chris Masonbd681512011-07-16 15:23:14 -04005134 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005135 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005136 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005137 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005138 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005139
5140 if (btrfs_header_generation(cur) < min_trans) {
5141 ret = 1;
5142 goto out;
5143 }
Chris Masond3977122009-01-05 21:25:51 -05005144 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005145 nritems = btrfs_header_nritems(cur);
5146 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005147 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005148
Chris Mason323ac952008-10-01 19:05:46 -04005149 /* at the lowest level, we're done, setup the path and exit */
5150 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005151 if (slot >= nritems)
5152 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005153 ret = 0;
5154 path->slots[level] = slot;
5155 btrfs_item_key_to_cpu(cur, &found_key, slot);
5156 goto out;
5157 }
Yan96524802008-07-24 12:19:49 -04005158 if (sret && slot > 0)
5159 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005160 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005161 * check this node pointer against the min_trans parameters.
5162 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005163 */
Chris Masond3977122009-01-05 21:25:51 -05005164 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005165 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005166
Chris Mason3f157a22008-06-25 16:01:31 -04005167 gen = btrfs_node_ptr_generation(cur, slot);
5168 if (gen < min_trans) {
5169 slot++;
5170 continue;
5171 }
Eric Sandeende78b512013-01-31 18:21:12 +00005172 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005173 }
Chris Masone02119d2008-09-05 16:13:11 -04005174find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005175 /*
5176 * we didn't find a candidate key in this node, walk forward
5177 * and find another one
5178 */
5179 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005180 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005181 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005182 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005183 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005184 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005185 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005186 goto again;
5187 } else {
5188 goto out;
5189 }
5190 }
5191 /* save our key for returning back */
5192 btrfs_node_key_to_cpu(cur, &found_key, slot);
5193 path->slots[level] = slot;
5194 if (level == path->lowest_level) {
5195 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005196 goto out;
5197 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005198 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005199 cur = read_node_slot(root, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005200 if (IS_ERR(cur)) {
5201 ret = PTR_ERR(cur);
5202 goto out;
5203 }
Chris Mason3f157a22008-06-25 16:01:31 -04005204
Chris Masonbd681512011-07-16 15:23:14 -04005205 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005206
Chris Masonbd681512011-07-16 15:23:14 -04005207 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005208 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005209 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005210 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005211 }
5212out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005213 path->keep_locks = keep_locks;
5214 if (ret == 0) {
5215 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5216 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005217 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005218 }
Chris Mason3f157a22008-06-25 16:01:31 -04005219 return ret;
5220}
5221
Liu Bofb770ae2016-07-05 12:10:14 -07005222static int tree_move_down(struct btrfs_root *root,
Alexander Block70698302012-06-05 21:07:48 +02005223 struct btrfs_path *path,
5224 int *level, int root_level)
5225{
Liu Bofb770ae2016-07-05 12:10:14 -07005226 struct extent_buffer *eb;
5227
Chris Mason74dd17f2012-08-07 16:25:13 -04005228 BUG_ON(*level == 0);
Liu Bofb770ae2016-07-05 12:10:14 -07005229 eb = read_node_slot(root, path->nodes[*level], path->slots[*level]);
5230 if (IS_ERR(eb))
5231 return PTR_ERR(eb);
5232
5233 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005234 path->slots[*level - 1] = 0;
5235 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005236 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005237}
5238
5239static int tree_move_next_or_upnext(struct btrfs_root *root,
5240 struct btrfs_path *path,
5241 int *level, int root_level)
5242{
5243 int ret = 0;
5244 int nritems;
5245 nritems = btrfs_header_nritems(path->nodes[*level]);
5246
5247 path->slots[*level]++;
5248
Chris Mason74dd17f2012-08-07 16:25:13 -04005249 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005250 if (*level == root_level)
5251 return -1;
5252
5253 /* move upnext */
5254 path->slots[*level] = 0;
5255 free_extent_buffer(path->nodes[*level]);
5256 path->nodes[*level] = NULL;
5257 (*level)++;
5258 path->slots[*level]++;
5259
5260 nritems = btrfs_header_nritems(path->nodes[*level]);
5261 ret = 1;
5262 }
5263 return ret;
5264}
5265
5266/*
5267 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5268 * or down.
5269 */
5270static int tree_advance(struct btrfs_root *root,
5271 struct btrfs_path *path,
5272 int *level, int root_level,
5273 int allow_down,
5274 struct btrfs_key *key)
5275{
5276 int ret;
5277
5278 if (*level == 0 || !allow_down) {
5279 ret = tree_move_next_or_upnext(root, path, level, root_level);
5280 } else {
Liu Bofb770ae2016-07-05 12:10:14 -07005281 ret = tree_move_down(root, path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005282 }
5283 if (ret >= 0) {
5284 if (*level == 0)
5285 btrfs_item_key_to_cpu(path->nodes[*level], key,
5286 path->slots[*level]);
5287 else
5288 btrfs_node_key_to_cpu(path->nodes[*level], key,
5289 path->slots[*level]);
5290 }
5291 return ret;
5292}
5293
5294static int tree_compare_item(struct btrfs_root *left_root,
5295 struct btrfs_path *left_path,
5296 struct btrfs_path *right_path,
5297 char *tmp_buf)
5298{
5299 int cmp;
5300 int len1, len2;
5301 unsigned long off1, off2;
5302
5303 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5304 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5305 if (len1 != len2)
5306 return 1;
5307
5308 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5309 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5310 right_path->slots[0]);
5311
5312 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5313
5314 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5315 if (cmp)
5316 return 1;
5317 return 0;
5318}
5319
5320#define ADVANCE 1
5321#define ADVANCE_ONLY_NEXT -1
5322
5323/*
5324 * This function compares two trees and calls the provided callback for
5325 * every changed/new/deleted item it finds.
5326 * If shared tree blocks are encountered, whole subtrees are skipped, making
5327 * the compare pretty fast on snapshotted subvolumes.
5328 *
5329 * This currently works on commit roots only. As commit roots are read only,
5330 * we don't do any locking. The commit roots are protected with transactions.
5331 * Transactions are ended and rejoined when a commit is tried in between.
5332 *
5333 * This function checks for modifications done to the trees while comparing.
5334 * If it detects a change, it aborts immediately.
5335 */
5336int btrfs_compare_trees(struct btrfs_root *left_root,
5337 struct btrfs_root *right_root,
5338 btrfs_changed_cb_t changed_cb, void *ctx)
5339{
5340 int ret;
5341 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005342 struct btrfs_path *left_path = NULL;
5343 struct btrfs_path *right_path = NULL;
5344 struct btrfs_key left_key;
5345 struct btrfs_key right_key;
5346 char *tmp_buf = NULL;
5347 int left_root_level;
5348 int right_root_level;
5349 int left_level;
5350 int right_level;
5351 int left_end_reached;
5352 int right_end_reached;
5353 int advance_left;
5354 int advance_right;
5355 u64 left_blockptr;
5356 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005357 u64 left_gen;
5358 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005359
5360 left_path = btrfs_alloc_path();
5361 if (!left_path) {
5362 ret = -ENOMEM;
5363 goto out;
5364 }
5365 right_path = btrfs_alloc_path();
5366 if (!right_path) {
5367 ret = -ENOMEM;
5368 goto out;
5369 }
5370
David Sterba8f282f72016-03-30 16:01:12 +02005371 tmp_buf = kmalloc(left_root->nodesize, GFP_KERNEL | __GFP_NOWARN);
Alexander Block70698302012-06-05 21:07:48 +02005372 if (!tmp_buf) {
David Sterba8f282f72016-03-30 16:01:12 +02005373 tmp_buf = vmalloc(left_root->nodesize);
5374 if (!tmp_buf) {
5375 ret = -ENOMEM;
5376 goto out;
5377 }
Alexander Block70698302012-06-05 21:07:48 +02005378 }
5379
5380 left_path->search_commit_root = 1;
5381 left_path->skip_locking = 1;
5382 right_path->search_commit_root = 1;
5383 right_path->skip_locking = 1;
5384
Alexander Block70698302012-06-05 21:07:48 +02005385 /*
5386 * Strategy: Go to the first items of both trees. Then do
5387 *
5388 * If both trees are at level 0
5389 * Compare keys of current items
5390 * If left < right treat left item as new, advance left tree
5391 * and repeat
5392 * If left > right treat right item as deleted, advance right tree
5393 * and repeat
5394 * If left == right do deep compare of items, treat as changed if
5395 * needed, advance both trees and repeat
5396 * If both trees are at the same level but not at level 0
5397 * Compare keys of current nodes/leafs
5398 * If left < right advance left tree and repeat
5399 * If left > right advance right tree and repeat
5400 * If left == right compare blockptrs of the next nodes/leafs
5401 * If they match advance both trees but stay at the same level
5402 * and repeat
5403 * If they don't match advance both trees while allowing to go
5404 * deeper and repeat
5405 * If tree levels are different
5406 * Advance the tree that needs it and repeat
5407 *
5408 * Advancing a tree means:
5409 * If we are at level 0, try to go to the next slot. If that's not
5410 * possible, go one level up and repeat. Stop when we found a level
5411 * where we could go to the next slot. We may at this point be on a
5412 * node or a leaf.
5413 *
5414 * If we are not at level 0 and not on shared tree blocks, go one
5415 * level deeper.
5416 *
5417 * If we are not at level 0 and on shared tree blocks, go one slot to
5418 * the right if possible or go up and right.
5419 */
5420
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005421 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005422 left_level = btrfs_header_level(left_root->commit_root);
5423 left_root_level = left_level;
5424 left_path->nodes[left_level] = left_root->commit_root;
5425 extent_buffer_get(left_path->nodes[left_level]);
5426
5427 right_level = btrfs_header_level(right_root->commit_root);
5428 right_root_level = right_level;
5429 right_path->nodes[right_level] = right_root->commit_root;
5430 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005431 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005432
5433 if (left_level == 0)
5434 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5435 &left_key, left_path->slots[left_level]);
5436 else
5437 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5438 &left_key, left_path->slots[left_level]);
5439 if (right_level == 0)
5440 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5441 &right_key, right_path->slots[right_level]);
5442 else
5443 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5444 &right_key, right_path->slots[right_level]);
5445
5446 left_end_reached = right_end_reached = 0;
5447 advance_left = advance_right = 0;
5448
5449 while (1) {
Nikolay Borisov13f99142019-09-04 19:33:58 +03005450 cond_resched();
Alexander Block70698302012-06-05 21:07:48 +02005451 if (advance_left && !left_end_reached) {
5452 ret = tree_advance(left_root, left_path, &left_level,
5453 left_root_level,
5454 advance_left != ADVANCE_ONLY_NEXT,
5455 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005456 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005457 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005458 else if (ret < 0)
5459 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005460 advance_left = 0;
5461 }
5462 if (advance_right && !right_end_reached) {
5463 ret = tree_advance(right_root, right_path, &right_level,
5464 right_root_level,
5465 advance_right != ADVANCE_ONLY_NEXT,
5466 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005467 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005468 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005469 else if (ret < 0)
5470 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005471 advance_right = 0;
5472 }
5473
5474 if (left_end_reached && right_end_reached) {
5475 ret = 0;
5476 goto out;
5477 } else if (left_end_reached) {
5478 if (right_level == 0) {
5479 ret = changed_cb(left_root, right_root,
5480 left_path, right_path,
5481 &right_key,
5482 BTRFS_COMPARE_TREE_DELETED,
5483 ctx);
5484 if (ret < 0)
5485 goto out;
5486 }
5487 advance_right = ADVANCE;
5488 continue;
5489 } else if (right_end_reached) {
5490 if (left_level == 0) {
5491 ret = changed_cb(left_root, right_root,
5492 left_path, right_path,
5493 &left_key,
5494 BTRFS_COMPARE_TREE_NEW,
5495 ctx);
5496 if (ret < 0)
5497 goto out;
5498 }
5499 advance_left = ADVANCE;
5500 continue;
5501 }
5502
5503 if (left_level == 0 && right_level == 0) {
5504 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5505 if (cmp < 0) {
5506 ret = changed_cb(left_root, right_root,
5507 left_path, right_path,
5508 &left_key,
5509 BTRFS_COMPARE_TREE_NEW,
5510 ctx);
5511 if (ret < 0)
5512 goto out;
5513 advance_left = ADVANCE;
5514 } else if (cmp > 0) {
5515 ret = changed_cb(left_root, right_root,
5516 left_path, right_path,
5517 &right_key,
5518 BTRFS_COMPARE_TREE_DELETED,
5519 ctx);
5520 if (ret < 0)
5521 goto out;
5522 advance_right = ADVANCE;
5523 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005524 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005525
Chris Mason74dd17f2012-08-07 16:25:13 -04005526 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005527 ret = tree_compare_item(left_root, left_path,
5528 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005529 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005530 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005531 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005532 result = BTRFS_COMPARE_TREE_SAME;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005533 ret = changed_cb(left_root, right_root,
5534 left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005535 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005536 if (ret < 0)
5537 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005538 advance_left = ADVANCE;
5539 advance_right = ADVANCE;
5540 }
5541 } else if (left_level == right_level) {
5542 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5543 if (cmp < 0) {
5544 advance_left = ADVANCE;
5545 } else if (cmp > 0) {
5546 advance_right = ADVANCE;
5547 } else {
5548 left_blockptr = btrfs_node_blockptr(
5549 left_path->nodes[left_level],
5550 left_path->slots[left_level]);
5551 right_blockptr = btrfs_node_blockptr(
5552 right_path->nodes[right_level],
5553 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005554 left_gen = btrfs_node_ptr_generation(
5555 left_path->nodes[left_level],
5556 left_path->slots[left_level]);
5557 right_gen = btrfs_node_ptr_generation(
5558 right_path->nodes[right_level],
5559 right_path->slots[right_level]);
5560 if (left_blockptr == right_blockptr &&
5561 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005562 /*
5563 * As we're on a shared block, don't
5564 * allow to go deeper.
5565 */
5566 advance_left = ADVANCE_ONLY_NEXT;
5567 advance_right = ADVANCE_ONLY_NEXT;
5568 } else {
5569 advance_left = ADVANCE;
5570 advance_right = ADVANCE;
5571 }
5572 }
5573 } else if (left_level < right_level) {
5574 advance_right = ADVANCE;
5575 } else {
5576 advance_left = ADVANCE;
5577 }
5578 }
5579
5580out:
5581 btrfs_free_path(left_path);
5582 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005583 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005584 return ret;
5585}
5586
Chris Mason3f157a22008-06-25 16:01:31 -04005587/*
5588 * this is similar to btrfs_next_leaf, but does not try to preserve
5589 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005590 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005591 *
5592 * 0 is returned if another key is found, < 0 if there are any errors
5593 * and 1 is returned if there are no higher keys in the tree
5594 *
5595 * path->keep_locks should be set to 1 on the search made before
5596 * calling this function.
5597 */
Chris Masone7a84562008-06-25 16:01:31 -04005598int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005599 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005600{
Chris Masone7a84562008-06-25 16:01:31 -04005601 int slot;
5602 struct extent_buffer *c;
5603
Chris Mason934d3752008-12-08 16:43:10 -05005604 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005605 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005606 if (!path->nodes[level])
5607 return 1;
5608
5609 slot = path->slots[level] + 1;
5610 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005611next:
Chris Masone7a84562008-06-25 16:01:31 -04005612 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005613 int ret;
5614 int orig_lowest;
5615 struct btrfs_key cur_key;
5616 if (level + 1 >= BTRFS_MAX_LEVEL ||
5617 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005618 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005619
5620 if (path->locks[level + 1]) {
5621 level++;
5622 continue;
5623 }
5624
5625 slot = btrfs_header_nritems(c) - 1;
5626 if (level == 0)
5627 btrfs_item_key_to_cpu(c, &cur_key, slot);
5628 else
5629 btrfs_node_key_to_cpu(c, &cur_key, slot);
5630
5631 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005632 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005633 path->lowest_level = level;
5634 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5635 0, 0);
5636 path->lowest_level = orig_lowest;
5637 if (ret < 0)
5638 return ret;
5639
5640 c = path->nodes[level];
5641 slot = path->slots[level];
5642 if (ret == 0)
5643 slot++;
5644 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005645 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005646
Chris Masone7a84562008-06-25 16:01:31 -04005647 if (level == 0)
5648 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005649 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005650 u64 gen = btrfs_node_ptr_generation(c, slot);
5651
Chris Mason3f157a22008-06-25 16:01:31 -04005652 if (gen < min_trans) {
5653 slot++;
5654 goto next;
5655 }
Chris Masone7a84562008-06-25 16:01:31 -04005656 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005657 }
Chris Masone7a84562008-06-25 16:01:31 -04005658 return 0;
5659 }
5660 return 1;
5661}
5662
Chris Mason7bb86312007-12-11 09:25:06 -05005663/*
Chris Mason925baed2008-06-25 16:01:30 -04005664 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005665 * returns 0 if it found something or 1 if there are no greater leaves.
5666 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005667 */
Chris Mason234b63a2007-03-13 10:46:10 -04005668int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005669{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005670 return btrfs_next_old_leaf(root, path, 0);
5671}
5672
5673int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5674 u64 time_seq)
5675{
Chris Masond97e63b2007-02-20 16:40:44 -05005676 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005677 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005678 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005679 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005680 struct btrfs_key key;
5681 u32 nritems;
5682 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005683 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005684 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005685
5686 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005687 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005688 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005689
Chris Mason8e73f272009-04-03 10:14:18 -04005690 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5691again:
5692 level = 1;
5693 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005694 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005695 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005696
Chris Masona2135012008-06-25 16:01:30 -04005697 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005698 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005699
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005700 if (time_seq)
5701 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5702 else
5703 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005704 path->keep_locks = 0;
5705
5706 if (ret < 0)
5707 return ret;
5708
Chris Masona2135012008-06-25 16:01:30 -04005709 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005710 /*
5711 * by releasing the path above we dropped all our locks. A balance
5712 * could have added more items next to the key that used to be
5713 * at the very end of the block. So, check again here and
5714 * advance the path if there are now more items available.
5715 */
Chris Masona2135012008-06-25 16:01:30 -04005716 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005717 if (ret == 0)
5718 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005719 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005720 goto done;
5721 }
Liu Bo0b43e042014-06-09 11:04:49 +08005722 /*
5723 * So the above check misses one case:
5724 * - after releasing the path above, someone has removed the item that
5725 * used to be at the very end of the block, and balance between leafs
5726 * gets another one with bigger key.offset to replace it.
5727 *
5728 * This one should be returned as well, or we can get leaf corruption
5729 * later(esp. in __btrfs_drop_extents()).
5730 *
5731 * And a bit more explanation about this check,
5732 * with ret > 0, the key isn't found, the path points to the slot
5733 * where it should be inserted, so the path->slots[0] item must be the
5734 * bigger one.
5735 */
5736 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5737 ret = 0;
5738 goto done;
5739 }
Chris Masond97e63b2007-02-20 16:40:44 -05005740
Chris Masond3977122009-01-05 21:25:51 -05005741 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005742 if (!path->nodes[level]) {
5743 ret = 1;
5744 goto done;
5745 }
Chris Mason5f39d392007-10-15 16:14:19 -04005746
Chris Masond97e63b2007-02-20 16:40:44 -05005747 slot = path->slots[level] + 1;
5748 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005749 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005750 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005751 if (level == BTRFS_MAX_LEVEL) {
5752 ret = 1;
5753 goto done;
5754 }
Chris Masond97e63b2007-02-20 16:40:44 -05005755 continue;
5756 }
Chris Mason5f39d392007-10-15 16:14:19 -04005757
Chris Mason925baed2008-06-25 16:01:30 -04005758 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005759 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005760 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005761 }
Chris Mason5f39d392007-10-15 16:14:19 -04005762
Chris Mason8e73f272009-04-03 10:14:18 -04005763 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005764 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005765 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005766 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005767 if (ret == -EAGAIN)
5768 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005769
Chris Mason76a05b32009-05-14 13:24:30 -04005770 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005771 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005772 goto done;
5773 }
5774
Chris Mason5cd57b22008-06-25 16:01:30 -04005775 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005776 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005777 if (!ret && time_seq) {
5778 /*
5779 * If we don't get the lock, we may be racing
5780 * with push_leaf_left, holding that lock while
5781 * itself waiting for the leaf we've currently
5782 * locked. To solve this situation, we give up
5783 * on our lock and cycle.
5784 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005785 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005786 btrfs_release_path(path);
5787 cond_resched();
5788 goto again;
5789 }
Chris Mason8e73f272009-04-03 10:14:18 -04005790 if (!ret) {
5791 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005792 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005793 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005794 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005795 }
Chris Mason31533fb2011-07-26 16:01:59 -04005796 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005797 }
Chris Masond97e63b2007-02-20 16:40:44 -05005798 break;
5799 }
5800 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005801 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005802 level--;
5803 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005804 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005805 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005806
Chris Mason5f39d392007-10-15 16:14:19 -04005807 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005808 path->nodes[level] = next;
5809 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005810 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005811 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005812 if (!level)
5813 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005814
Chris Mason8e73f272009-04-03 10:14:18 -04005815 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005816 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005817 if (ret == -EAGAIN)
5818 goto again;
5819
Chris Mason76a05b32009-05-14 13:24:30 -04005820 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005821 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005822 goto done;
5823 }
5824
Chris Mason5cd57b22008-06-25 16:01:30 -04005825 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005826 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005827 if (!ret) {
5828 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005829 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005830 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005831 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005832 }
Chris Mason31533fb2011-07-26 16:01:59 -04005833 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005834 }
Chris Masond97e63b2007-02-20 16:40:44 -05005835 }
Chris Mason8e73f272009-04-03 10:14:18 -04005836 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005837done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005838 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005839 path->leave_spinning = old_spinning;
5840 if (!old_spinning)
5841 btrfs_set_path_blocking(path);
5842
5843 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005844}
Chris Mason0b86a832008-03-24 15:01:56 -04005845
Chris Mason3f157a22008-06-25 16:01:31 -04005846/*
5847 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5848 * searching until it gets past min_objectid or finds an item of 'type'
5849 *
5850 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5851 */
Chris Mason0b86a832008-03-24 15:01:56 -04005852int btrfs_previous_item(struct btrfs_root *root,
5853 struct btrfs_path *path, u64 min_objectid,
5854 int type)
5855{
5856 struct btrfs_key found_key;
5857 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005858 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005859 int ret;
5860
Chris Masond3977122009-01-05 21:25:51 -05005861 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005862 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005863 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005864 ret = btrfs_prev_leaf(root, path);
5865 if (ret != 0)
5866 return ret;
5867 } else {
5868 path->slots[0]--;
5869 }
5870 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005871 nritems = btrfs_header_nritems(leaf);
5872 if (nritems == 0)
5873 return 1;
5874 if (path->slots[0] == nritems)
5875 path->slots[0]--;
5876
Chris Mason0b86a832008-03-24 15:01:56 -04005877 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005878 if (found_key.objectid < min_objectid)
5879 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005880 if (found_key.type == type)
5881 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005882 if (found_key.objectid == min_objectid &&
5883 found_key.type < type)
5884 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005885 }
5886 return 1;
5887}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005888
5889/*
5890 * search in extent tree to find a previous Metadata/Data extent item with
5891 * min objecitd.
5892 *
5893 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5894 */
5895int btrfs_previous_extent_item(struct btrfs_root *root,
5896 struct btrfs_path *path, u64 min_objectid)
5897{
5898 struct btrfs_key found_key;
5899 struct extent_buffer *leaf;
5900 u32 nritems;
5901 int ret;
5902
5903 while (1) {
5904 if (path->slots[0] == 0) {
5905 btrfs_set_path_blocking(path);
5906 ret = btrfs_prev_leaf(root, path);
5907 if (ret != 0)
5908 return ret;
5909 } else {
5910 path->slots[0]--;
5911 }
5912 leaf = path->nodes[0];
5913 nritems = btrfs_header_nritems(leaf);
5914 if (nritems == 0)
5915 return 1;
5916 if (path->slots[0] == nritems)
5917 path->slots[0]--;
5918
5919 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5920 if (found_key.objectid < min_objectid)
5921 break;
5922 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5923 found_key.type == BTRFS_METADATA_ITEM_KEY)
5924 return 0;
5925 if (found_key.objectid == min_objectid &&
5926 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5927 break;
5928 }
5929 return 1;
5930}