blob: cad06c60ad66357ca49fa10b8d8d522faf327432 [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);
Josef Bacike7e9bb32021-01-14 14:02:46 -0500282 if (ret) {
Filipe Manana5fdd05d2021-02-04 14:35:44 +0000283 btrfs_tree_unlock(cow);
284 free_extent_buffer(cow);
Josef Bacike7e9bb32021-01-14 14:02:46 -0500285 btrfs_abort_transaction(trans, ret);
Chris Masonbe20aa92007-12-17 20:14:01 -0500286 return ret;
Josef Bacike7e9bb32021-01-14 14:02:46 -0500287 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500288
289 btrfs_mark_buffer_dirty(cow);
290 *cow_ret = cow;
291 return 0;
292}
293
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200294enum mod_log_op {
295 MOD_LOG_KEY_REPLACE,
296 MOD_LOG_KEY_ADD,
297 MOD_LOG_KEY_REMOVE,
298 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
299 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
300 MOD_LOG_MOVE_KEYS,
301 MOD_LOG_ROOT_REPLACE,
302};
303
304struct tree_mod_move {
305 int dst_slot;
306 int nr_items;
307};
308
309struct tree_mod_root {
310 u64 logical;
311 u8 level;
312};
313
314struct tree_mod_elem {
315 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530316 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200317 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200318 enum mod_log_op op;
319
320 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
321 int slot;
322
323 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
324 u64 generation;
325
326 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
327 struct btrfs_disk_key key;
328 u64 blockptr;
329
330 /* this is used for op == MOD_LOG_MOVE_KEYS */
331 struct tree_mod_move move;
332
333 /* this is used for op == MOD_LOG_ROOT_REPLACE */
334 struct tree_mod_root old_root;
335};
336
Jan Schmidt097b8a72012-06-21 11:08:04 +0200337/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700338 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000339 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700340static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000341{
342 return atomic64_inc_return(&fs_info->tree_mod_seq);
343}
344
345/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200346 * This adds a new blocker to the tree mod log's blocker list if the @elem
347 * passed does not already have a sequence number set. So when a caller expects
348 * to record tree modifications, it should ensure to set elem->seq to zero
349 * before calling btrfs_get_tree_mod_seq.
350 * Returns a fresh, unused tree log modification sequence number, even if no new
351 * blocker was added.
352 */
353u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
354 struct seq_list *elem)
355{
David Sterbaa71561b2018-03-05 15:43:41 +0100356 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200357 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700358 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200359 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
360 }
David Sterbaa71561b2018-03-05 15:43:41 +0100361 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200362
Josef Bacikfcebe452014-05-13 17:30:47 -0700363 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200364}
365
366void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
367 struct seq_list *elem)
368{
369 struct rb_root *tm_root;
370 struct rb_node *node;
371 struct rb_node *next;
372 struct seq_list *cur_elem;
373 struct tree_mod_elem *tm;
374 u64 min_seq = (u64)-1;
375 u64 seq_putting = elem->seq;
376
377 if (!seq_putting)
378 return;
379
Filipe Manana24e9e6b2020-01-22 12:23:20 +0000380 write_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200381 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200382 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200383
384 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200385 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200386 if (seq_putting > cur_elem->seq) {
387 /*
388 * blocker with lower sequence number exists, we
389 * cannot remove anything from the log
390 */
Filipe Manana24e9e6b2020-01-22 12:23:20 +0000391 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200392 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200393 }
394 min_seq = cur_elem->seq;
395 }
396 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397
398 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200399 * anything that's lower than the lowest existing (read: blocked)
400 * sequence number can be removed from the tree.
401 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200402 tm_root = &fs_info->tree_mod_log;
403 for (node = rb_first(tm_root); node; node = next) {
404 next = rb_next(node);
405 tm = container_of(node, struct tree_mod_elem, node);
Filipe Mananabe9a42b2019-12-06 12:27:39 +0000406 if (tm->seq >= min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200407 continue;
408 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409 kfree(tm);
410 }
David Sterbaa71561b2018-03-05 15:43:41 +0100411 write_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200412}
413
414/*
415 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530416 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200417 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530418 * The 'start address' is the logical address of the *new* root node
419 * for root replace operations, or the logical address of the affected
420 * block for all other operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000421 *
David Sterbaa71561b2018-03-05 15:43:41 +0100422 * Note: must be called with write lock for fs_info::tree_mod_log_lock.
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200423 */
424static noinline int
425__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
426{
427 struct rb_root *tm_root;
428 struct rb_node **new;
429 struct rb_node *parent = NULL;
430 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200431
Josef Bacikc8cc6342013-07-01 16:18:19 -0400432 BUG_ON(!tm);
433
Josef Bacikfcebe452014-05-13 17:30:47 -0700434 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200435
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436 tm_root = &fs_info->tree_mod_log;
437 new = &tm_root->rb_node;
438 while (*new) {
439 cur = container_of(*new, struct tree_mod_elem, node);
440 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530441 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530443 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200444 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200445 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200446 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200447 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200448 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000449 else
450 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200451 }
452
453 rb_link_node(&tm->node, parent, new);
454 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000455 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456}
457
Jan Schmidt097b8a72012-06-21 11:08:04 +0200458/*
459 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
460 * returns zero with the tree_mod_log_lock acquired. The caller must hold
461 * this until all tree mod log insertions are recorded in the rb tree and then
David Sterbaa71561b2018-03-05 15:43:41 +0100462 * write unlock fs_info::tree_mod_log_lock.
Jan Schmidt097b8a72012-06-21 11:08:04 +0200463 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200464static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
465 struct extent_buffer *eb) {
466 smp_mb();
467 if (list_empty(&(fs_info)->tree_mod_seq_list))
468 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200469 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200470 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000471
David Sterbaa71561b2018-03-05 15:43:41 +0100472 write_lock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000473 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
David Sterbaa71561b2018-03-05 15:43:41 +0100474 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000475 return 1;
476 }
477
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200478 return 0;
479}
480
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000481/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
482static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
483 struct extent_buffer *eb)
484{
485 smp_mb();
486 if (list_empty(&(fs_info)->tree_mod_seq_list))
487 return 0;
488 if (eb && btrfs_header_level(eb) == 0)
489 return 0;
490
491 return 1;
492}
493
494static struct tree_mod_elem *
495alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
496 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200497{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200498 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200499
Josef Bacikc8cc6342013-07-01 16:18:19 -0400500 tm = kzalloc(sizeof(*tm), flags);
501 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000502 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200503
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530504 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200505 if (op != MOD_LOG_KEY_ADD) {
506 btrfs_node_key(eb, &tm->key, slot);
507 tm->blockptr = btrfs_node_blockptr(eb, slot);
508 }
509 tm->op = op;
510 tm->slot = slot;
511 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000512 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200513
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000514 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200515}
516
517static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400518tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
519 struct extent_buffer *eb, int slot,
520 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200521{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000522 struct tree_mod_elem *tm;
523 int ret;
524
525 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200526 return 0;
527
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000528 tm = alloc_tree_mod_elem(eb, slot, op, flags);
529 if (!tm)
530 return -ENOMEM;
531
532 if (tree_mod_dont_log(fs_info, eb)) {
533 kfree(tm);
534 return 0;
535 }
536
537 ret = __tree_mod_log_insert(fs_info, tm);
David Sterbaa71561b2018-03-05 15:43:41 +0100538 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000539 if (ret)
540 kfree(tm);
541
542 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200543}
544
545static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200546tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
547 struct extent_buffer *eb, int dst_slot, int src_slot,
548 int nr_items, gfp_t flags)
549{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000550 struct tree_mod_elem *tm = NULL;
551 struct tree_mod_elem **tm_list = NULL;
552 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200553 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000554 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200555
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000556 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200557 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200558
David Sterba31e818f2015-02-20 18:00:26 +0100559 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000560 if (!tm_list)
561 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200562
Josef Bacikc8cc6342013-07-01 16:18:19 -0400563 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000564 if (!tm) {
565 ret = -ENOMEM;
566 goto free_tms;
567 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200568
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530569 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200570 tm->slot = src_slot;
571 tm->move.dst_slot = dst_slot;
572 tm->move.nr_items = nr_items;
573 tm->op = MOD_LOG_MOVE_KEYS;
574
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000575 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
576 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
577 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
578 if (!tm_list[i]) {
579 ret = -ENOMEM;
580 goto free_tms;
581 }
582 }
583
584 if (tree_mod_dont_log(fs_info, eb))
585 goto free_tms;
586 locked = 1;
587
588 /*
589 * When we override something during the move, we log these removals.
590 * This can only happen when we move towards the beginning of the
591 * buffer, i.e. dst_slot < src_slot.
592 */
593 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
594 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
595 if (ret)
596 goto free_tms;
597 }
598
599 ret = __tree_mod_log_insert(fs_info, tm);
600 if (ret)
601 goto free_tms;
David Sterbaa71561b2018-03-05 15:43:41 +0100602 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000603 kfree(tm_list);
604
605 return 0;
606free_tms:
607 for (i = 0; i < nr_items; i++) {
608 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
609 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
610 kfree(tm_list[i]);
611 }
612 if (locked)
David Sterbaa71561b2018-03-05 15:43:41 +0100613 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000614 kfree(tm_list);
615 kfree(tm);
616
617 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200618}
619
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000620static inline int
621__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
622 struct tree_mod_elem **tm_list,
623 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200624{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000625 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200626 int ret;
627
Jan Schmidt097b8a72012-06-21 11:08:04 +0200628 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000629 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
630 if (ret) {
631 for (j = nritems - 1; j > i; j--)
632 rb_erase(&tm_list[j]->node,
633 &fs_info->tree_mod_log);
634 return ret;
635 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200636 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000637
638 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200639}
640
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200641static noinline int
642tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
643 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000644 struct extent_buffer *new_root, gfp_t flags,
645 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200646{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000647 struct tree_mod_elem *tm = NULL;
648 struct tree_mod_elem **tm_list = NULL;
649 int nritems = 0;
650 int ret = 0;
651 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200652
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000653 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200654 return 0;
655
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000656 if (log_removal && btrfs_header_level(old_root) > 0) {
657 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100658 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000659 flags);
660 if (!tm_list) {
661 ret = -ENOMEM;
662 goto free_tms;
663 }
664 for (i = 0; i < nritems; i++) {
665 tm_list[i] = alloc_tree_mod_elem(old_root, i,
666 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
667 if (!tm_list[i]) {
668 ret = -ENOMEM;
669 goto free_tms;
670 }
671 }
672 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000673
Josef Bacikc8cc6342013-07-01 16:18:19 -0400674 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000675 if (!tm) {
676 ret = -ENOMEM;
677 goto free_tms;
678 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200679
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530680 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200681 tm->old_root.logical = old_root->start;
682 tm->old_root.level = btrfs_header_level(old_root);
683 tm->generation = btrfs_header_generation(old_root);
684 tm->op = MOD_LOG_ROOT_REPLACE;
685
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000686 if (tree_mod_dont_log(fs_info, NULL))
687 goto free_tms;
688
689 if (tm_list)
690 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
691 if (!ret)
692 ret = __tree_mod_log_insert(fs_info, tm);
693
David Sterbaa71561b2018-03-05 15:43:41 +0100694 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000695 if (ret)
696 goto free_tms;
697 kfree(tm_list);
698
699 return ret;
700
701free_tms:
702 if (tm_list) {
703 for (i = 0; i < nritems; i++)
704 kfree(tm_list[i]);
705 kfree(tm_list);
706 }
707 kfree(tm);
708
709 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200710}
711
712static struct tree_mod_elem *
713__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
714 int smallest)
715{
716 struct rb_root *tm_root;
717 struct rb_node *node;
718 struct tree_mod_elem *cur = NULL;
719 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200720
David Sterbaa71561b2018-03-05 15:43:41 +0100721 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200722 tm_root = &fs_info->tree_mod_log;
723 node = tm_root->rb_node;
724 while (node) {
725 cur = container_of(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530726 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200727 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530728 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200729 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200730 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200731 node = node->rb_left;
732 } else if (!smallest) {
733 /* we want the node with the highest seq */
734 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200735 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200736 found = cur;
737 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200738 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200739 /* we want the node with the smallest seq */
740 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200741 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200742 found = cur;
743 node = node->rb_right;
744 } else {
745 found = cur;
746 break;
747 }
748 }
David Sterbaa71561b2018-03-05 15:43:41 +0100749 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200750
751 return found;
752}
753
754/*
755 * this returns the element from the log with the smallest time sequence
756 * value that's in the log (the oldest log item). any element with a time
757 * sequence lower than min_seq will be ignored.
758 */
759static struct tree_mod_elem *
760tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
761 u64 min_seq)
762{
763 return __tree_mod_log_search(fs_info, start, min_seq, 1);
764}
765
766/*
767 * this returns the element from the log with the largest time sequence
768 * value that's in the log (the most recent log item). any element with
769 * a time sequence lower than min_seq will be ignored.
770 */
771static struct tree_mod_elem *
772tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
773{
774 return __tree_mod_log_search(fs_info, start, min_seq, 0);
775}
776
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000777static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200778tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
779 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000780 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200781{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000782 int ret = 0;
783 struct tree_mod_elem **tm_list = NULL;
784 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200785 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000786 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200787
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000788 if (!tree_mod_need_log(fs_info, NULL))
789 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200790
Josef Bacikc8cc6342013-07-01 16:18:19 -0400791 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000792 return 0;
793
David Sterba31e818f2015-02-20 18:00:26 +0100794 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000795 GFP_NOFS);
796 if (!tm_list)
797 return -ENOMEM;
798
799 tm_list_add = tm_list;
800 tm_list_rem = tm_list + nr_items;
801 for (i = 0; i < nr_items; i++) {
802 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
803 MOD_LOG_KEY_REMOVE, GFP_NOFS);
804 if (!tm_list_rem[i]) {
805 ret = -ENOMEM;
806 goto free_tms;
807 }
808
809 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
810 MOD_LOG_KEY_ADD, GFP_NOFS);
811 if (!tm_list_add[i]) {
812 ret = -ENOMEM;
813 goto free_tms;
814 }
815 }
816
817 if (tree_mod_dont_log(fs_info, NULL))
818 goto free_tms;
819 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200820
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200821 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000822 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
823 if (ret)
824 goto free_tms;
825 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
826 if (ret)
827 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200828 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000829
David Sterbaa71561b2018-03-05 15:43:41 +0100830 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000831 kfree(tm_list);
832
833 return 0;
834
835free_tms:
836 for (i = 0; i < nr_items * 2; i++) {
837 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
838 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
839 kfree(tm_list[i]);
840 }
841 if (locked)
David Sterbaa71561b2018-03-05 15:43:41 +0100842 write_unlock(&fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000843 kfree(tm_list);
844
845 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200846}
847
848static inline void
849tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
850 int dst_offset, int src_offset, int nr_items)
851{
852 int ret;
853 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
854 nr_items, GFP_NOFS);
855 BUG_ON(ret < 0);
856}
857
Jan Schmidt097b8a72012-06-21 11:08:04 +0200858static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200859tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000860 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200861{
862 int ret;
863
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000864 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400865 MOD_LOG_KEY_REPLACE,
866 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200867 BUG_ON(ret < 0);
868}
869
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000870static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200871tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200872{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000873 struct tree_mod_elem **tm_list = NULL;
874 int nritems = 0;
875 int i;
876 int ret = 0;
877
878 if (btrfs_header_level(eb) == 0)
879 return 0;
880
881 if (!tree_mod_need_log(fs_info, NULL))
882 return 0;
883
884 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100885 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000886 if (!tm_list)
887 return -ENOMEM;
888
889 for (i = 0; i < nritems; i++) {
890 tm_list[i] = alloc_tree_mod_elem(eb, i,
891 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
892 if (!tm_list[i]) {
893 ret = -ENOMEM;
894 goto free_tms;
895 }
896 }
897
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200898 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000899 goto free_tms;
900
901 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
David Sterbaa71561b2018-03-05 15:43:41 +0100902 write_unlock(&eb->fs_info->tree_mod_log_lock);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000903 if (ret)
904 goto free_tms;
905 kfree(tm_list);
906
907 return 0;
908
909free_tms:
910 for (i = 0; i < nritems; i++)
911 kfree(tm_list[i]);
912 kfree(tm_list);
913
914 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200915}
916
Jan Schmidt097b8a72012-06-21 11:08:04 +0200917static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200918tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000919 struct extent_buffer *new_root_node,
920 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200921{
922 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200923 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000924 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200925 BUG_ON(ret < 0);
926}
927
Chris Masond352ac62008-09-29 15:18:18 -0400928/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400929 * check if the tree block can be shared by multiple trees
930 */
931int btrfs_block_can_be_shared(struct btrfs_root *root,
932 struct extent_buffer *buf)
933{
934 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400935 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400936 * are never shared. If a block was allocated after the last
937 * snapshot and the block was not allocated by tree relocation,
938 * we know the block is not shared.
939 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800940 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400941 buf != root->node && buf != root->commit_root &&
942 (btrfs_header_generation(buf) <=
943 btrfs_root_last_snapshot(&root->root_item) ||
944 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
945 return 1;
946#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800947 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
949 return 1;
950#endif
951 return 0;
952}
953
954static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
955 struct btrfs_root *root,
956 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400957 struct extent_buffer *cow,
958 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959{
960 u64 refs;
961 u64 owner;
962 u64 flags;
963 u64 new_flags = 0;
964 int ret;
965
966 /*
967 * Backrefs update rules:
968 *
969 * Always use full backrefs for extent pointers in tree block
970 * allocated by tree relocation.
971 *
972 * If a shared tree block is no longer referenced by its owner
973 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
974 * use full backrefs for extent pointers in tree block.
975 *
976 * If a tree block is been relocating
977 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
978 * use full backrefs for extent pointers in tree block.
979 * The reason for this is some operations (such as drop tree)
980 * are only allowed for blocks use full backrefs.
981 */
982
983 if (btrfs_block_can_be_shared(root, buf)) {
984 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500985 btrfs_header_level(buf), 1,
986 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700987 if (ret)
988 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700989 if (refs == 0) {
990 ret = -EROFS;
Anand Jain34d97002016-03-16 16:43:06 +0800991 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -0700992 return ret;
993 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400994 } else {
995 refs = 1;
996 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
997 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
998 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
999 else
1000 flags = 0;
1001 }
1002
1003 owner = btrfs_header_owner(buf);
1004 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1005 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1006
1007 if (refs > 1) {
1008 if ((owner == root->root_key.objectid ||
1009 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1010 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001011 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001012 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013
1014 if (root->root_key.objectid ==
1015 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001016 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001017 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001018 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001019 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001020 }
1021 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1022 } else {
1023
1024 if (root->root_key.objectid ==
1025 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001026 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001027 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001028 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001029 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 }
1031 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001032 int level = btrfs_header_level(buf);
1033
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001034 ret = btrfs_set_disk_extent_flags(trans, root,
1035 buf->start,
1036 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001037 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001038 if (ret)
1039 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040 }
1041 } else {
1042 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1043 if (root->root_key.objectid ==
1044 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001045 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001047 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001048 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001049 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001050 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051 }
Daniel Dressler01d58472014-11-21 17:15:07 +09001052 clean_tree_block(trans, root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001053 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054 }
1055 return 0;
1056}
1057
1058/*
Chris Masond3977122009-01-05 21:25:51 -05001059 * does the dirty work in cow of a single block. The parent block (if
1060 * supplied) is updated to point to the new cow copy. The new buffer is marked
1061 * dirty and returned locked. If you modify the block it needs to be marked
1062 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001063 *
1064 * search_start -- an allocation hint for the new block
1065 *
Chris Masond3977122009-01-05 21:25:51 -05001066 * empty_size -- a hint that you plan on doing more cow. This is the size in
1067 * bytes the allocator should try to find free next to the block it returns.
1068 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001069 */
Chris Masond3977122009-01-05 21:25:51 -05001070static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001071 struct btrfs_root *root,
1072 struct extent_buffer *buf,
1073 struct extent_buffer *parent, int parent_slot,
1074 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001075 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001076{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001077 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001078 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001079 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001080 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001081 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001082 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001083
Chris Mason925baed2008-06-25 16:01:30 -04001084 if (*cow_ret == buf)
1085 unlock_orig = 1;
1086
Chris Masonb9447ef2009-03-09 11:45:38 -04001087 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001088
Miao Xie27cdeb72014-04-02 19:51:05 +08001089 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1090 trans->transid != root->fs_info->running_transaction->transid);
1091 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1092 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001093
Chris Mason7bb86312007-12-11 09:25:06 -05001094 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001095
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001096 if (level == 0)
1097 btrfs_item_key(buf, &disk_key, 0);
1098 else
1099 btrfs_node_key(buf, &disk_key, 0);
1100
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001101 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1102 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001103
David Sterba4d75f8a2014-06-15 01:54:12 +02001104 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1105 root->root_key.objectid, &disk_key, level,
1106 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001107 if (IS_ERR(cow))
1108 return PTR_ERR(cow);
1109
Chris Masonb4ce94d2009-02-04 09:25:08 -05001110 /* cow is set to blocking by btrfs_init_new_buffer */
1111
Chris Mason5f39d392007-10-15 16:14:19 -04001112 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001113 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001114 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001115 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1116 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1117 BTRFS_HEADER_FLAG_RELOC);
1118 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1119 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1120 else
1121 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001122
Ross Kirk0a4e5582013-09-24 10:12:38 +01001123 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -05001124 BTRFS_FSID_SIZE);
1125
Mark Fashehbe1a5562011-08-08 13:20:18 -07001126 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001127 if (ret) {
Josef Bacikb7aabd72020-09-29 08:53:54 -04001128 btrfs_tree_unlock(cow);
1129 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -04001130 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001131 return ret;
1132 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001133
Miao Xie27cdeb72014-04-02 19:51:05 +08001134 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001135 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001136 if (ret) {
Josef Bacikb7aabd72020-09-29 08:53:54 -04001137 btrfs_tree_unlock(cow);
1138 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -04001139 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001140 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001141 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001142 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001143
Chris Mason6702ed42007-08-07 16:15:09 -04001144 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001145 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001146 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1147 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1148 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001149
Chris Mason5f39d392007-10-15 16:14:19 -04001150 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001151 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001152 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001153
Yan, Zhengf0486c62010-05-16 10:46:25 -04001154 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001155 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001156 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001157 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001158 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001159 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001160 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001161 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001162 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001163 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001164 btrfs_set_node_ptr_generation(parent, parent_slot,
1165 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001166 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001167 if (last_ref) {
1168 ret = tree_mod_log_free_eb(root->fs_info, buf);
1169 if (ret) {
Josef Bacikb7aabd72020-09-29 08:53:54 -04001170 btrfs_tree_unlock(cow);
1171 free_extent_buffer(cow);
Jeff Mahoney66642832016-06-10 18:19:25 -04001172 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001173 return ret;
1174 }
1175 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001176 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001177 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001178 }
Chris Mason925baed2008-06-25 16:01:30 -04001179 if (unlock_orig)
1180 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001181 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001182 btrfs_mark_buffer_dirty(cow);
1183 *cow_ret = cow;
1184 return 0;
1185}
1186
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001187/*
1188 * returns the logical address of the oldest predecessor of the given root.
1189 * entries older than time_seq are ignored.
1190 */
1191static struct tree_mod_elem *
1192__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001193 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001194{
1195 struct tree_mod_elem *tm;
1196 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001197 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001198 int looped = 0;
1199
1200 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001201 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001202
1203 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301204 * the very last operation that's logged for a root is the
1205 * replacement operation (if it is replaced at all). this has
1206 * the logical address of the *new* root, making it the very
1207 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001208 */
1209 while (1) {
1210 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1211 time_seq);
1212 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001213 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001214 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001215 * if there are no tree operation for the oldest root, we simply
1216 * return it. this should only happen if that (old) root is at
1217 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001218 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001219 if (!tm)
1220 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001221
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001222 /*
1223 * if there's an operation that's not a root replacement, we
1224 * found the oldest version of our root. normally, we'll find a
1225 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1226 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001227 if (tm->op != MOD_LOG_ROOT_REPLACE)
1228 break;
1229
1230 found = tm;
1231 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001232 looped = 1;
1233 }
1234
Jan Schmidta95236d2012-06-05 16:41:24 +02001235 /* if there's no old root to return, return what we found instead */
1236 if (!found)
1237 found = tm;
1238
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239 return found;
1240}
1241
1242/*
1243 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001244 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001245 * time_seq).
1246 */
1247static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001248__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1249 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001250{
1251 u32 n;
1252 struct rb_node *next;
1253 struct tree_mod_elem *tm = first_tm;
1254 unsigned long o_dst;
1255 unsigned long o_src;
1256 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1257
1258 n = btrfs_header_nritems(eb);
David Sterbaa71561b2018-03-05 15:43:41 +01001259 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001260 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001261 /*
1262 * all the operations are recorded with the operator used for
1263 * the modification. as we're going backwards, we do the
1264 * opposite of each operation here.
1265 */
1266 switch (tm->op) {
1267 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1268 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001269 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001270 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001271 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001272 btrfs_set_node_key(eb, &tm->key, tm->slot);
1273 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1274 btrfs_set_node_ptr_generation(eb, tm->slot,
1275 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001276 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001277 break;
1278 case MOD_LOG_KEY_REPLACE:
1279 BUG_ON(tm->slot >= n);
1280 btrfs_set_node_key(eb, &tm->key, tm->slot);
1281 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1282 btrfs_set_node_ptr_generation(eb, tm->slot,
1283 tm->generation);
1284 break;
1285 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001286 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001287 n--;
1288 break;
1289 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001290 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1291 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1292 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001293 tm->move.nr_items * p_size);
1294 break;
1295 case MOD_LOG_ROOT_REPLACE:
1296 /*
1297 * this operation is special. for roots, this must be
1298 * handled explicitly before rewinding.
1299 * for non-roots, this operation may exist if the node
1300 * was a root: root A -> child B; then A gets empty and
1301 * B is promoted to the new root. in the mod log, we'll
1302 * have a root-replace operation for B, a tree block
1303 * that is no root. we simply ignore that operation.
1304 */
1305 break;
1306 }
1307 next = rb_next(&tm->node);
1308 if (!next)
1309 break;
1310 tm = container_of(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301311 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001312 break;
1313 }
David Sterbaa71561b2018-03-05 15:43:41 +01001314 read_unlock(&fs_info->tree_mod_log_lock);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001315 btrfs_set_header_nritems(eb, n);
1316}
1317
Jan Schmidt47fb0912013-04-13 13:19:55 +00001318/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001319 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001320 * is returned. If rewind operations happen, a fresh buffer is returned. The
1321 * returned buffer is always read-locked. If the returned buffer is not the
1322 * input buffer, the lock on the input buffer is released and the input buffer
1323 * is freed (its refcount is decremented).
1324 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001325static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001326tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1327 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001328{
1329 struct extent_buffer *eb_rewin;
1330 struct tree_mod_elem *tm;
1331
1332 if (!time_seq)
1333 return eb;
1334
1335 if (btrfs_header_level(eb) == 0)
1336 return eb;
1337
1338 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1339 if (!tm)
1340 return eb;
1341
Josef Bacik9ec72672013-08-07 16:57:23 -04001342 btrfs_set_path_blocking(path);
1343 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1344
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001345 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1346 BUG_ON(tm->slot != 0);
Feifei Xub9ef22d2016-06-01 19:18:25 +08001347 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start,
1348 eb->len);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001349 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001350 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001351 free_extent_buffer(eb);
1352 return NULL;
1353 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001354 btrfs_set_header_bytenr(eb_rewin, eb->start);
1355 btrfs_set_header_backref_rev(eb_rewin,
1356 btrfs_header_backref_rev(eb));
1357 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001358 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001359 } else {
1360 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001361 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001362 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001363 free_extent_buffer(eb);
1364 return NULL;
1365 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001366 }
1367
Josef Bacik9ec72672013-08-07 16:57:23 -04001368 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1369 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001370 free_extent_buffer(eb);
1371
Josef Bacikcc037d62020-08-10 11:42:31 -04001372 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
1373 eb_rewin, btrfs_header_level(eb_rewin));
Jan Schmidt47fb0912013-04-13 13:19:55 +00001374 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001375 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001376 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001377 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001378
1379 return eb_rewin;
1380}
1381
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001382/*
1383 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1384 * value. If there are no changes, the current root->root_node is returned. If
1385 * anything changed in between, there's a fresh buffer allocated on which the
1386 * rewind operations are done. In any case, the returned buffer is read locked.
1387 * Returns NULL on error (with no locks held).
1388 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001389static inline struct extent_buffer *
1390get_old_root(struct btrfs_root *root, u64 time_seq)
1391{
1392 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001393 struct extent_buffer *eb = NULL;
1394 struct extent_buffer *eb_root;
Filipe Mananaf0ed93d2019-08-12 19:14:29 +01001395 u64 eb_root_owner = 0;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001396 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001397 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001398 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001399 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001400
Jan Schmidt30b04632013-04-13 13:19:54 +00001401 eb_root = btrfs_read_lock_root_node(root);
1402 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001403 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001404 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001405
Jan Schmidta95236d2012-06-05 16:41:24 +02001406 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1407 old_root = &tm->old_root;
1408 old_generation = tm->generation;
1409 logical = old_root->logical;
1410 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001411 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001412 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001413
Jan Schmidta95236d2012-06-05 16:41:24 +02001414 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001415 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001416 btrfs_tree_read_unlock(eb_root);
1417 free_extent_buffer(eb_root);
David Sterbace86cd52014-06-15 01:07:32 +02001418 old = read_tree_block(root, logical, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001419 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1420 if (!IS_ERR(old))
1421 free_extent_buffer(old);
Frank Holtonefe120a2013-12-20 11:37:06 -05001422 btrfs_warn(root->fs_info,
1423 "failed to read tree block %llu from get_old_root", logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001424 } else {
Filipe Mananaca403b72021-03-11 14:31:05 +00001425 btrfs_tree_read_lock(old);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001426 eb = btrfs_clone_extent_buffer(old);
Filipe Mananaca403b72021-03-11 14:31:05 +00001427 btrfs_tree_read_unlock(old);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001428 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001429 }
1430 } else if (old_root) {
Filipe Mananaf0ed93d2019-08-12 19:14:29 +01001431 eb_root_owner = btrfs_header_owner(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001432 btrfs_tree_read_unlock(eb_root);
1433 free_extent_buffer(eb_root);
Feifei Xub9ef22d2016-06-01 19:18:25 +08001434 eb = alloc_dummy_extent_buffer(root->fs_info, logical,
1435 root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001436 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001437 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001438 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001439 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001440 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001441 }
1442
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001443 if (!eb)
1444 return NULL;
Jan Schmidta95236d2012-06-05 16:41:24 +02001445 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001446 btrfs_set_header_bytenr(eb, eb->start);
1447 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Filipe Mananaf0ed93d2019-08-12 19:14:29 +01001448 btrfs_set_header_owner(eb, eb_root_owner);
Jan Schmidta95236d2012-06-05 16:41:24 +02001449 btrfs_set_header_level(eb, old_root->level);
1450 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001451 }
Josef Bacikcc037d62020-08-10 11:42:31 -04001452 btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
1453 btrfs_header_level(eb));
1454 btrfs_tree_read_lock(eb);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001455 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001456 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001457 else
1458 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001459 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001460
1461 return eb;
1462}
1463
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001464int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1465{
1466 struct tree_mod_elem *tm;
1467 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001468 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001469
Jan Schmidt30b04632013-04-13 13:19:54 +00001470 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001471 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1472 level = tm->old_root.level;
1473 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001474 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001475 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001476 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001477
1478 return level;
1479}
1480
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001481static inline int should_cow_block(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct extent_buffer *buf)
1484{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001485 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001486 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001487
Liu Bof1ebcc72011-11-14 20:48:06 -05001488 /* ensure we can see the force_cow */
1489 smp_rmb();
1490
1491 /*
1492 * We do not need to cow a block if
1493 * 1) this block is not created or changed in this transaction;
1494 * 2) this block does not belong to TREE_RELOC tree;
1495 * 3) the root is not forced COW.
1496 *
1497 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001498 * when we create snapshot during committing the transaction,
Liu Bof1ebcc72011-11-14 20:48:06 -05001499 * after we've finished coping src root, we must COW the shared
1500 * block to ensure the metadata consistency.
1501 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001502 if (btrfs_header_generation(buf) == trans->transid &&
1503 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1504 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001505 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001506 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001507 return 0;
1508 return 1;
1509}
1510
Chris Masond352ac62008-09-29 15:18:18 -04001511/*
1512 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001513 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001514 * once per transaction, as long as it hasn't been written yet
1515 */
Chris Masond3977122009-01-05 21:25:51 -05001516noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001517 struct btrfs_root *root, struct extent_buffer *buf,
1518 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001519 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001520{
Chris Mason6702ed42007-08-07 16:15:09 -04001521 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001522 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001523
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001524 if (trans->transaction != root->fs_info->running_transaction)
1525 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001526 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001527 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001528
1529 if (trans->transid != root->fs_info->generation)
1530 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001531 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001532
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001533 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001534 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001535 *cow_ret = buf;
1536 return 0;
1537 }
Chris Masonc4876852009-02-04 09:24:25 -05001538
Byongho Leeee221842015-12-15 01:42:10 +09001539 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001540
1541 if (parent)
1542 btrfs_set_lock_blocking(parent);
1543 btrfs_set_lock_blocking(buf);
1544
Chris Masonf510cfe2007-10-15 16:14:48 -04001545 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001546 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001547
1548 trace_btrfs_cow_block(root, buf, *cow_ret);
1549
Chris Masonf510cfe2007-10-15 16:14:48 -04001550 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001551}
1552
Chris Masond352ac62008-09-29 15:18:18 -04001553/*
1554 * helper function for defrag to decide if two blocks pointed to by a
1555 * node are actually close by
1556 */
Chris Mason6b800532007-10-15 16:17:34 -04001557static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001558{
Chris Mason6b800532007-10-15 16:17:34 -04001559 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001560 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001561 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001562 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001563 return 0;
1564}
1565
Chris Mason081e9572007-11-06 10:26:24 -05001566/*
1567 * compare two keys in a memcmp fashion
1568 */
1569static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1570{
1571 struct btrfs_key k1;
1572
1573 btrfs_disk_key_to_cpu(&k1, disk);
1574
Diego Calleja20736ab2009-07-24 11:06:52 -04001575 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001576}
1577
Josef Bacikf3465ca2008-11-12 14:19:50 -05001578/*
1579 * same as comp_keys only with two btrfs_key's
1580 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001581int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001582{
1583 if (k1->objectid > k2->objectid)
1584 return 1;
1585 if (k1->objectid < k2->objectid)
1586 return -1;
1587 if (k1->type > k2->type)
1588 return 1;
1589 if (k1->type < k2->type)
1590 return -1;
1591 if (k1->offset > k2->offset)
1592 return 1;
1593 if (k1->offset < k2->offset)
1594 return -1;
1595 return 0;
1596}
Chris Mason081e9572007-11-06 10:26:24 -05001597
Chris Masond352ac62008-09-29 15:18:18 -04001598/*
1599 * this is used by the defrag code to go through all the
1600 * leaves pointed to by a node and reallocate them so that
1601 * disk order is close to key order
1602 */
Chris Mason6702ed42007-08-07 16:15:09 -04001603int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001604 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001605 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001606 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001607{
Chris Mason6b800532007-10-15 16:17:34 -04001608 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001609 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001610 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001611 u64 search_start = *last_ret;
1612 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001613 u64 other;
1614 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001615 int end_slot;
1616 int i;
1617 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001618 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001619 int uptodate;
1620 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001621 int progress_passed = 0;
1622 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001623
Chris Mason5708b952007-10-25 15:43:18 -04001624 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001625
Julia Lawall6c1500f2012-11-03 20:30:18 +00001626 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1627 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001628
Chris Mason6b800532007-10-15 16:17:34 -04001629 parent_nritems = btrfs_header_nritems(parent);
David Sterba707e8a02014-06-04 19:22:26 +02001630 blocksize = root->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001631 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001632
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001633 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001634 return 0;
1635
Chris Masonb4ce94d2009-02-04 09:25:08 -05001636 btrfs_set_lock_blocking(parent);
1637
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001638 for (i = start_slot; i <= end_slot; i++) {
Chris Mason6702ed42007-08-07 16:15:09 -04001639 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001640
Chris Mason081e9572007-11-06 10:26:24 -05001641 btrfs_node_key(parent, &disk_key, i);
1642 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1643 continue;
1644
1645 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001646 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001647 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001648 if (last_block == 0)
1649 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001650
Chris Mason6702ed42007-08-07 16:15:09 -04001651 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001652 other = btrfs_node_blockptr(parent, i - 1);
1653 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001654 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001655 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001656 other = btrfs_node_blockptr(parent, i + 1);
1657 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001658 }
Chris Masone9d0b132007-08-10 14:06:19 -04001659 if (close) {
1660 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001661 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001662 }
Chris Mason6702ed42007-08-07 16:15:09 -04001663
Daniel Dressler01d58472014-11-21 17:15:07 +09001664 cur = btrfs_find_tree_block(root->fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001665 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001666 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001667 else
1668 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001669 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001670 if (!cur) {
David Sterbace86cd52014-06-15 01:07:32 +02001671 cur = read_tree_block(root, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001672 if (IS_ERR(cur)) {
1673 return PTR_ERR(cur);
1674 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001675 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001676 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001677 }
Chris Mason6b800532007-10-15 16:17:34 -04001678 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001679 err = btrfs_read_buffer(cur, gen);
1680 if (err) {
1681 free_extent_buffer(cur);
1682 return err;
1683 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001684 }
Chris Mason6702ed42007-08-07 16:15:09 -04001685 }
Chris Masone9d0b132007-08-10 14:06:19 -04001686 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001687 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001688
Chris Masone7a84562008-06-25 16:01:31 -04001689 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001690 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001691 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001692 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001693 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001694 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001695 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001696 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001697 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001698 break;
Yan252c38f2007-08-29 09:11:44 -04001699 }
Chris Masone7a84562008-06-25 16:01:31 -04001700 search_start = cur->start;
1701 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001702 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001703 btrfs_tree_unlock(cur);
1704 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001705 }
1706 return err;
1707}
1708
Chris Masonaa5d6be2007-02-28 16:35:06 -05001709
Chris Mason74123bd2007-02-02 11:05:29 -05001710/*
Chris Mason5f39d392007-10-15 16:14:19 -04001711 * search for key in the extent_buffer. The items start at offset p,
1712 * and they are item_size apart. There are 'max' items in p.
1713 *
Chris Mason74123bd2007-02-02 11:05:29 -05001714 * the slot in the array is returned via slot, and it points to
1715 * the place where you would insert key if it is not found in
1716 * the array.
1717 *
1718 * slot may point to max if the key is bigger than all of the keys
1719 */
Chris Masone02119d2008-09-05 16:13:11 -04001720static noinline int generic_bin_search(struct extent_buffer *eb,
1721 unsigned long p,
1722 int item_size, struct btrfs_key *key,
1723 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001724{
1725 int low = 0;
1726 int high = max;
1727 int mid;
1728 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001729 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001730 struct btrfs_disk_key unaligned;
1731 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001732 char *kaddr = NULL;
1733 unsigned long map_start = 0;
1734 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001735 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001736
Liu Bo5e24e9a2016-06-23 16:32:45 -07001737 if (low > high) {
1738 btrfs_err(eb->fs_info,
1739 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1740 __func__, low, high, eb->start,
1741 btrfs_header_owner(eb), btrfs_header_level(eb));
1742 return -EINVAL;
1743 }
1744
Chris Masond3977122009-01-05 21:25:51 -05001745 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001746 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001747 offset = p + mid * item_size;
1748
Chris Masona6591712011-07-19 12:04:14 -04001749 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001750 (offset + sizeof(struct btrfs_disk_key)) >
1751 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001752
1753 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001754 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001755 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001756
Chris Mason479965d2007-10-15 16:14:27 -04001757 if (!err) {
1758 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1759 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001760 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001761 read_extent_buffer(eb, &unaligned,
1762 offset, sizeof(unaligned));
1763 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001764 } else {
1765 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001766 }
1767
Chris Mason5f39d392007-10-15 16:14:19 -04001768 } else {
1769 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1770 map_start);
1771 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001772 ret = comp_keys(tmp, key);
1773
1774 if (ret < 0)
1775 low = mid + 1;
1776 else if (ret > 0)
1777 high = mid;
1778 else {
1779 *slot = mid;
1780 return 0;
1781 }
1782 }
1783 *slot = low;
1784 return 1;
1785}
1786
Chris Mason97571fd2007-02-24 13:39:08 -05001787/*
1788 * simple bin_search frontend that does the right thing for
1789 * leaves vs nodes
1790 */
Chris Mason5f39d392007-10-15 16:14:19 -04001791static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1792 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001793{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001794 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001795 return generic_bin_search(eb,
1796 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001797 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001798 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001799 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001800 else
Chris Mason5f39d392007-10-15 16:14:19 -04001801 return generic_bin_search(eb,
1802 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001803 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001804 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001805 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806}
1807
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001808int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1809 int level, int *slot)
1810{
1811 return bin_search(eb, key, level, slot);
1812}
1813
Yan, Zhengf0486c62010-05-16 10:46:25 -04001814static void root_add_used(struct btrfs_root *root, u32 size)
1815{
1816 spin_lock(&root->accounting_lock);
1817 btrfs_set_root_used(&root->root_item,
1818 btrfs_root_used(&root->root_item) + size);
1819 spin_unlock(&root->accounting_lock);
1820}
1821
1822static void root_sub_used(struct btrfs_root *root, u32 size)
1823{
1824 spin_lock(&root->accounting_lock);
1825 btrfs_set_root_used(&root->root_item,
1826 btrfs_root_used(&root->root_item) - size);
1827 spin_unlock(&root->accounting_lock);
1828}
1829
Chris Masond352ac62008-09-29 15:18:18 -04001830/* given a node and slot number, this reads the blocks it points to. The
1831 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001832 */
Chris Masone02119d2008-09-05 16:13:11 -04001833static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001834 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001835{
Chris Masonca7a79a2008-05-12 12:59:19 -04001836 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001837 struct extent_buffer *eb;
1838
Liu Bofb770ae2016-07-05 12:10:14 -07001839 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1840 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001841
1842 BUG_ON(level == 0);
1843
Josef Bacik416bc652013-04-23 14:17:42 -04001844 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001845 btrfs_node_ptr_generation(parent, slot));
Liu Bofb770ae2016-07-05 12:10:14 -07001846 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1847 free_extent_buffer(eb);
1848 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001849 }
1850
1851 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001852}
1853
Chris Masond352ac62008-09-29 15:18:18 -04001854/*
1855 * node level balancing, used to make sure nodes are in proper order for
1856 * item deletion. We balance from the top down, so we have to make sure
1857 * that a deletion won't leave an node completely empty later on.
1858 */
Chris Masone02119d2008-09-05 16:13:11 -04001859static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001860 struct btrfs_root *root,
1861 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001862{
Chris Mason5f39d392007-10-15 16:14:19 -04001863 struct extent_buffer *right = NULL;
1864 struct extent_buffer *mid;
1865 struct extent_buffer *left = NULL;
1866 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001867 int ret = 0;
1868 int wret;
1869 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001870 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001871 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001872
1873 if (level == 0)
1874 return 0;
1875
Chris Mason5f39d392007-10-15 16:14:19 -04001876 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001877
Chris Masonbd681512011-07-16 15:23:14 -04001878 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1879 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001880 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1881
Chris Mason1d4f8a02007-03-13 09:28:32 -04001882 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001883
Li Zefana05a9bb2011-09-06 16:55:34 +08001884 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001885 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001886 pslot = path->slots[level + 1];
1887 }
Chris Masonbb803952007-03-01 12:04:21 -05001888
Chris Mason40689472007-03-17 14:29:23 -04001889 /*
1890 * deal with the case where there is only one pointer in the root
1891 * by promoting the node below to a root
1892 */
Chris Mason5f39d392007-10-15 16:14:19 -04001893 if (!parent) {
1894 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001895
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001897 return 0;
1898
1899 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001900 child = read_node_slot(root, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001901 if (IS_ERR(child)) {
1902 ret = PTR_ERR(child);
Anand Jain34d97002016-03-16 16:43:06 +08001903 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001904 goto enospc;
1905 }
1906
Chris Mason925baed2008-06-25 16:01:30 -04001907 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001908 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001909 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001910 if (ret) {
1911 btrfs_tree_unlock(child);
1912 free_extent_buffer(child);
1913 goto enospc;
1914 }
Yan2f375ab2008-02-01 14:58:07 -05001915
Jan Schmidt90f8d622013-04-13 13:19:53 +00001916 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001917 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001918
Chris Mason0b86a832008-03-24 15:01:56 -04001919 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001920 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001921
Chris Mason925baed2008-06-25 16:01:30 -04001922 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001923 path->nodes[level] = NULL;
Daniel Dressler01d58472014-11-21 17:15:07 +09001924 clean_tree_block(trans, root->fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001925 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001926 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001927 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001928
1929 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001930 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001931 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001932 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001933 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001934 }
Chris Mason5f39d392007-10-15 16:14:19 -04001935 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001936 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001937 return 0;
1938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 left = read_node_slot(root, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001940 if (IS_ERR(left))
1941 left = NULL;
1942
Chris Mason5f39d392007-10-15 16:14:19 -04001943 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001944 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001945 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001946 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001947 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001948 if (wret) {
1949 ret = wret;
1950 goto enospc;
1951 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001952 }
Liu Bofb770ae2016-07-05 12:10:14 -07001953
Chris Mason5f39d392007-10-15 16:14:19 -04001954 right = read_node_slot(root, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001955 if (IS_ERR(right))
1956 right = NULL;
1957
Chris Mason5f39d392007-10-15 16:14:19 -04001958 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001959 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001960 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001961 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001962 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001963 if (wret) {
1964 ret = wret;
1965 goto enospc;
1966 }
1967 }
1968
1969 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001970 if (left) {
1971 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001972 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001973 if (wret < 0)
1974 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001975 }
Chris Mason79f95c82007-03-01 15:16:26 -05001976
1977 /*
1978 * then try to empty the right most buffer into the middle
1979 */
Chris Mason5f39d392007-10-15 16:14:19 -04001980 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001981 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001982 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001983 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001984 if (btrfs_header_nritems(right) == 0) {
Daniel Dressler01d58472014-11-21 17:15:07 +09001985 clean_tree_block(trans, root->fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04001986 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001987 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001988 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001989 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001990 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001991 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001992 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001993 struct btrfs_disk_key right_key;
1994 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001995 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001996 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001997 btrfs_set_node_key(parent, &right_key, pslot + 1);
1998 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001999 }
2000 }
Chris Mason5f39d392007-10-15 16:14:19 -04002001 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002002 /*
2003 * we're not allowed to leave a node with one item in the
2004 * tree during a delete. A deletion from lower in the tree
2005 * could try to delete the only pointer in this node.
2006 * So, pull some keys from the left.
2007 * There has to be a left pointer at this point because
2008 * otherwise we would have pulled some pointers from the
2009 * right
2010 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002011 if (!left) {
2012 ret = -EROFS;
Anand Jain34d97002016-03-16 16:43:06 +08002013 btrfs_handle_fs_error(root->fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07002014 goto enospc;
2015 }
Chris Mason5f39d392007-10-15 16:14:19 -04002016 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002017 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002018 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002019 goto enospc;
2020 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002021 if (wret == 1) {
2022 wret = push_node_left(trans, root, left, mid, 1);
2023 if (wret < 0)
2024 ret = wret;
2025 }
Chris Mason79f95c82007-03-01 15:16:26 -05002026 BUG_ON(wret == 1);
2027 }
Chris Mason5f39d392007-10-15 16:14:19 -04002028 if (btrfs_header_nritems(mid) == 0) {
Daniel Dressler01d58472014-11-21 17:15:07 +09002029 clean_tree_block(trans, root->fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002030 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002031 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002032 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002033 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002034 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002035 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002036 } else {
2037 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002038 struct btrfs_disk_key mid_key;
2039 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00002040 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02002041 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002042 btrfs_set_node_key(parent, &mid_key, pslot);
2043 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002044 }
Chris Masonbb803952007-03-01 12:04:21 -05002045
Chris Mason79f95c82007-03-01 15:16:26 -05002046 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002047 if (left) {
2048 if (btrfs_header_nritems(left) > orig_slot) {
2049 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002050 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002051 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002052 path->slots[level + 1] -= 1;
2053 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002054 if (mid) {
2055 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002056 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002057 }
Chris Masonbb803952007-03-01 12:04:21 -05002058 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002059 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002060 path->slots[level] = orig_slot;
2061 }
2062 }
Chris Mason79f95c82007-03-01 15:16:26 -05002063 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002064 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002065 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002066 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002067enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002068 if (right) {
2069 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002070 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002071 }
2072 if (left) {
2073 if (path->nodes[level] != left)
2074 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002075 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002076 }
Chris Masonbb803952007-03-01 12:04:21 -05002077 return ret;
2078}
2079
Chris Masond352ac62008-09-29 15:18:18 -04002080/* Node balancing for insertion. Here we only split or push nodes around
2081 * when they are completely full. This is also done top down, so we
2082 * have to be pessimistic.
2083 */
Chris Masond3977122009-01-05 21:25:51 -05002084static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002085 struct btrfs_root *root,
2086 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002087{
Chris Mason5f39d392007-10-15 16:14:19 -04002088 struct extent_buffer *right = NULL;
2089 struct extent_buffer *mid;
2090 struct extent_buffer *left = NULL;
2091 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002092 int ret = 0;
2093 int wret;
2094 int pslot;
2095 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002096
2097 if (level == 0)
2098 return 1;
2099
Chris Mason5f39d392007-10-15 16:14:19 -04002100 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002101 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002102
Li Zefana05a9bb2011-09-06 16:55:34 +08002103 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002104 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002105 pslot = path->slots[level + 1];
2106 }
Chris Masone66f7092007-04-20 13:16:02 -04002107
Chris Mason5f39d392007-10-15 16:14:19 -04002108 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002109 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002110
Chris Mason5f39d392007-10-15 16:14:19 -04002111 left = read_node_slot(root, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002112 if (IS_ERR(left))
2113 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002114
2115 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002116 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002117 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002118
2119 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002120 btrfs_set_lock_blocking(left);
2121
Chris Mason5f39d392007-10-15 16:14:19 -04002122 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002123 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2124 wret = 1;
2125 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002126 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002127 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002128 if (ret)
2129 wret = 1;
2130 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002131 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002132 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002133 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002134 }
Chris Masone66f7092007-04-20 13:16:02 -04002135 if (wret < 0)
2136 ret = wret;
2137 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002138 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002139 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002141 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002142 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 btrfs_set_node_key(parent, &disk_key, pslot);
2144 btrfs_mark_buffer_dirty(parent);
2145 if (btrfs_header_nritems(left) > orig_slot) {
2146 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002147 path->slots[level + 1] -= 1;
2148 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002149 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002150 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002151 } else {
2152 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002153 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002154 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002155 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002156 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002157 }
Chris Masone66f7092007-04-20 13:16:02 -04002158 return 0;
2159 }
Chris Mason925baed2008-06-25 16:01:30 -04002160 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002162 }
Chris Mason925baed2008-06-25 16:01:30 -04002163 right = read_node_slot(root, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002164 if (IS_ERR(right))
2165 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002166
2167 /*
2168 * then try to empty the right most buffer into the middle
2169 */
Chris Mason5f39d392007-10-15 16:14:19 -04002170 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002171 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002172
Chris Mason925baed2008-06-25 16:01:30 -04002173 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002174 btrfs_set_lock_blocking(right);
2175
Chris Mason5f39d392007-10-15 16:14:19 -04002176 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002177 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2178 wret = 1;
2179 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002180 ret = btrfs_cow_block(trans, root, right,
2181 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002182 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002183 if (ret)
2184 wret = 1;
2185 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002186 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002187 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002188 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002189 }
Chris Masone66f7092007-04-20 13:16:02 -04002190 if (wret < 0)
2191 ret = wret;
2192 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002193 struct btrfs_disk_key disk_key;
2194
2195 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002196 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002197 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002198 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2199 btrfs_mark_buffer_dirty(parent);
2200
2201 if (btrfs_header_nritems(mid) <= orig_slot) {
2202 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002203 path->slots[level + 1] += 1;
2204 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002205 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002206 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002207 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002208 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002209 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002210 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002211 }
Chris Masone66f7092007-04-20 13:16:02 -04002212 return 0;
2213 }
Chris Mason925baed2008-06-25 16:01:30 -04002214 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002215 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002216 }
Chris Masone66f7092007-04-20 13:16:02 -04002217 return 1;
2218}
2219
Chris Mason74123bd2007-02-02 11:05:29 -05002220/*
Chris Masond352ac62008-09-29 15:18:18 -04002221 * readahead one full node of leaves, finding things that are close
2222 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002223 */
Chris Masonc8c42862009-04-03 10:14:18 -04002224static void reada_for_search(struct btrfs_root *root,
2225 struct btrfs_path *path,
2226 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002227{
Chris Mason5f39d392007-10-15 16:14:19 -04002228 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002229 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002230 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002231 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002232 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002233 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002234 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002235 u32 nr;
2236 u32 blocksize;
2237 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002238
Chris Masona6b6e752007-10-15 16:22:39 -04002239 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002240 return;
2241
Chris Mason6702ed42007-08-07 16:15:09 -04002242 if (!path->nodes[level])
2243 return;
2244
Chris Mason5f39d392007-10-15 16:14:19 -04002245 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002246
Chris Mason3c69fae2007-08-07 15:52:22 -04002247 search = btrfs_node_blockptr(node, slot);
David Sterba707e8a02014-06-04 19:22:26 +02002248 blocksize = root->nodesize;
Daniel Dressler01d58472014-11-21 17:15:07 +09002249 eb = btrfs_find_tree_block(root->fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002250 if (eb) {
2251 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002252 return;
2253 }
2254
Chris Masona7175312009-01-22 09:23:10 -05002255 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002256
Chris Mason5f39d392007-10-15 16:14:19 -04002257 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002258 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002259
Chris Masond3977122009-01-05 21:25:51 -05002260 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002261 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002262 if (nr == 0)
2263 break;
2264 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002265 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002266 nr++;
2267 if (nr >= nritems)
2268 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002269 }
David Sterbae4058b52015-11-27 16:31:35 +01002270 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002271 btrfs_node_key(node, &disk_key, nr);
2272 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2273 break;
2274 }
Chris Mason6b800532007-10-15 16:17:34 -04002275 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002276 if ((search <= target && target - search <= 65536) ||
2277 (search > target && search - target <= 65536)) {
David Sterbad3e46fe2014-06-15 02:04:19 +02002278 readahead_tree_block(root, search);
Chris Mason6b800532007-10-15 16:17:34 -04002279 nread += blocksize;
2280 }
2281 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002282 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002283 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002284 }
2285}
Chris Mason925baed2008-06-25 16:01:30 -04002286
Josef Bacik0b088512013-06-17 14:23:02 -04002287static noinline void reada_for_balance(struct btrfs_root *root,
2288 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002289{
2290 int slot;
2291 int nritems;
2292 struct extent_buffer *parent;
2293 struct extent_buffer *eb;
2294 u64 gen;
2295 u64 block1 = 0;
2296 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002297
Chris Mason8c594ea2009-04-20 15:50:10 -04002298 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002299 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002300 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002301
2302 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002303 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002304
2305 if (slot > 0) {
2306 block1 = 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, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002309 /*
2310 * if we get -eagain from btrfs_buffer_uptodate, we
2311 * don't want to return eagain here. That will loop
2312 * forever
2313 */
2314 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002315 block1 = 0;
2316 free_extent_buffer(eb);
2317 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002318 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002319 block2 = btrfs_node_blockptr(parent, slot + 1);
2320 gen = btrfs_node_ptr_generation(parent, slot + 1);
Daniel Dressler01d58472014-11-21 17:15:07 +09002321 eb = btrfs_find_tree_block(root->fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002322 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002323 block2 = 0;
2324 free_extent_buffer(eb);
2325 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002326
Josef Bacik0b088512013-06-17 14:23:02 -04002327 if (block1)
David Sterbad3e46fe2014-06-15 02:04:19 +02002328 readahead_tree_block(root, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002329 if (block2)
David Sterbad3e46fe2014-06-15 02:04:19 +02002330 readahead_tree_block(root, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002331}
2332
2333
2334/*
Chris Masond3977122009-01-05 21:25:51 -05002335 * when we walk down the tree, it is usually safe to unlock the higher layers
2336 * in the tree. The exceptions are when our path goes through slot 0, because
2337 * operations on the tree might require changing key pointers higher up in the
2338 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002339 *
Chris Masond3977122009-01-05 21:25:51 -05002340 * callers might also have set path->keep_locks, which tells this code to keep
2341 * the lock if the path points to the last slot in the block. This is part of
2342 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002343 *
Chris Masond3977122009-01-05 21:25:51 -05002344 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2345 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002346 */
Chris Masone02119d2008-09-05 16:13:11 -04002347static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002348 int lowest_unlock, int min_write_lock_level,
2349 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002350{
2351 int i;
2352 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002353 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002354 struct extent_buffer *t;
2355
2356 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2357 if (!path->nodes[i])
2358 break;
2359 if (!path->locks[i])
2360 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002361 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002362 skip_level = i + 1;
2363 continue;
2364 }
Chris Mason051e1b92008-06-25 16:01:30 -04002365 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002366 u32 nritems;
2367 t = path->nodes[i];
2368 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002369 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002370 skip_level = i + 1;
2371 continue;
2372 }
2373 }
Chris Mason051e1b92008-06-25 16:01:30 -04002374 if (skip_level < i && i >= lowest_unlock)
2375 no_skips = 1;
2376
Chris Mason925baed2008-06-25 16:01:30 -04002377 t = path->nodes[i];
2378 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002379 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002380 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002381 if (write_lock_level &&
2382 i > min_write_lock_level &&
2383 i <= *write_lock_level) {
2384 *write_lock_level = i - 1;
2385 }
Chris Mason925baed2008-06-25 16:01:30 -04002386 }
2387 }
2388}
2389
Chris Mason3c69fae2007-08-07 15:52:22 -04002390/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002391 * This releases any locks held in the path starting at level and
2392 * going all the way up to the root.
2393 *
2394 * btrfs_search_slot will keep the lock held on higher nodes in a few
2395 * corner cases, such as COW of the block at slot zero in the node. This
2396 * ignores those rules, and it should only be called when there are no
2397 * more updates to be done higher up in the tree.
2398 */
2399noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2400{
2401 int i;
2402
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002403 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002404 return;
2405
2406 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2407 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002408 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002409 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002410 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002411 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002412 path->locks[i] = 0;
2413 }
2414}
2415
2416/*
Chris Masonc8c42862009-04-03 10:14:18 -04002417 * helper function for btrfs_search_slot. The goal is to find a block
2418 * in cache without setting the path to blocking. If we find the block
2419 * we return zero and the path is unchanged.
2420 *
2421 * If we can't find the block, we set the path blocking and do some
2422 * reada. -EAGAIN is returned and the search must be repeated.
2423 */
2424static int
2425read_block_for_search(struct btrfs_trans_handle *trans,
2426 struct btrfs_root *root, struct btrfs_path *p,
2427 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002428 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002429{
2430 u64 blocknr;
2431 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002432 struct extent_buffer *b = *eb_ret;
2433 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002434 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002435
2436 blocknr = btrfs_node_blockptr(b, slot);
2437 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002438
Daniel Dressler01d58472014-11-21 17:15:07 +09002439 tmp = btrfs_find_tree_block(root->fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002440 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002441 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002442 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2443 *eb_ret = tmp;
2444 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002445 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002446
2447 /* the pages were up to date, but we failed
2448 * the generation number check. Do a full
2449 * read for the generation number that is correct.
2450 * We must do this without dropping locks so
2451 * we can trust our generation number
2452 */
2453 btrfs_set_path_blocking(p);
2454
2455 /* now we're allowed to do a blocking uptodate check */
2456 ret = btrfs_read_buffer(tmp, gen);
2457 if (!ret) {
2458 *eb_ret = tmp;
2459 return 0;
2460 }
2461 free_extent_buffer(tmp);
2462 btrfs_release_path(p);
2463 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002464 }
2465
2466 /*
2467 * reduce lock contention at high levels
2468 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002469 * we read. Don't release the lock on the current
2470 * level because we need to walk this node to figure
2471 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002472 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002473 btrfs_unlock_up_safe(p, level + 1);
2474 btrfs_set_path_blocking(p);
2475
Chris Masoncb449212010-10-24 11:01:27 -04002476 free_extent_buffer(tmp);
David Sterbae4058b52015-11-27 16:31:35 +01002477 if (p->reada != READA_NONE)
Chris Masonc8c42862009-04-03 10:14:18 -04002478 reada_for_search(root, p, level, slot, key->objectid);
2479
Chris Mason76a05b32009-05-14 13:24:30 -04002480 ret = -EAGAIN;
Liu Bo298d5db2018-05-16 01:37:36 +08002481 tmp = read_tree_block(root, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08002482 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002483 /*
2484 * If the read above didn't mark this buffer up to date,
2485 * it will never end up being up to date. Set ret to EIO now
2486 * and give up so that our caller doesn't loop forever
2487 * on our EAGAINs.
2488 */
Chris Masonb9fab912012-05-06 07:23:47 -04002489 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002490 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002491 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002492 } else {
2493 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002494 }
Liu Bo298d5db2018-05-16 01:37:36 +08002495
2496 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002497 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002498}
2499
2500/*
2501 * helper function for btrfs_search_slot. This does all of the checks
2502 * for node-level blocks and does any balancing required based on
2503 * the ins_len.
2504 *
2505 * If no extra work was required, zero is returned. If we had to
2506 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2507 * start over
2508 */
2509static int
2510setup_nodes_for_search(struct btrfs_trans_handle *trans,
2511 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002512 struct extent_buffer *b, int level, int ins_len,
2513 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002514{
2515 int ret;
2516 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2517 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2518 int sret;
2519
Chris Masonbd681512011-07-16 15:23:14 -04002520 if (*write_lock_level < level + 1) {
2521 *write_lock_level = level + 1;
2522 btrfs_release_path(p);
2523 goto again;
2524 }
2525
Chris Masonc8c42862009-04-03 10:14:18 -04002526 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002527 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002528 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002529 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002530
2531 BUG_ON(sret > 0);
2532 if (sret) {
2533 ret = sret;
2534 goto done;
2535 }
2536 b = p->nodes[level];
2537 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002538 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002539 int sret;
2540
Chris Masonbd681512011-07-16 15:23:14 -04002541 if (*write_lock_level < level + 1) {
2542 *write_lock_level = level + 1;
2543 btrfs_release_path(p);
2544 goto again;
2545 }
2546
Chris Masonc8c42862009-04-03 10:14:18 -04002547 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002548 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002549 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002550 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002551
2552 if (sret) {
2553 ret = sret;
2554 goto done;
2555 }
2556 b = p->nodes[level];
2557 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002558 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002559 goto again;
2560 }
2561 BUG_ON(btrfs_header_nritems(b) == 1);
2562 }
2563 return 0;
2564
2565again:
2566 ret = -EAGAIN;
2567done:
2568 return ret;
2569}
2570
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002571static void key_search_validate(struct extent_buffer *b,
2572 struct btrfs_key *key,
2573 int level)
2574{
2575#ifdef CONFIG_BTRFS_ASSERT
2576 struct btrfs_disk_key disk_key;
2577
2578 btrfs_cpu_key_to_disk(&disk_key, key);
2579
2580 if (level == 0)
2581 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2582 offsetof(struct btrfs_leaf, items[0].key),
2583 sizeof(disk_key)));
2584 else
2585 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2586 offsetof(struct btrfs_node, ptrs[0].key),
2587 sizeof(disk_key)));
2588#endif
2589}
2590
2591static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2592 int level, int *prev_cmp, int *slot)
2593{
2594 if (*prev_cmp != 0) {
2595 *prev_cmp = bin_search(b, key, level, slot);
2596 return *prev_cmp;
2597 }
2598
2599 key_search_validate(b, key, level);
2600 *slot = 0;
2601
2602 return 0;
2603}
2604
David Sterba381cf652015-01-02 18:45:16 +01002605int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002606 u64 iobjectid, u64 ioff, u8 key_type,
2607 struct btrfs_key *found_key)
2608{
2609 int ret;
2610 struct btrfs_key key;
2611 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002612
2613 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002614 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002615
2616 key.type = key_type;
2617 key.objectid = iobjectid;
2618 key.offset = ioff;
2619
2620 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002621 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002622 return ret;
2623
2624 eb = path->nodes[0];
2625 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2626 ret = btrfs_next_leaf(fs_root, path);
2627 if (ret)
2628 return ret;
2629 eb = path->nodes[0];
2630 }
2631
2632 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2633 if (found_key->type != key.type ||
2634 found_key->objectid != key.objectid)
2635 return 1;
2636
2637 return 0;
2638}
2639
Chris Masonc8c42862009-04-03 10:14:18 -04002640/*
Chris Mason74123bd2007-02-02 11:05:29 -05002641 * look for key in the tree. path is filled in with nodes along the way
2642 * if key is found, we return zero and you can find the item in the leaf
2643 * level of the path (level 0)
2644 *
2645 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002646 * be inserted, and 1 is returned. If there are other errors during the
2647 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002648 *
2649 * if ins_len > 0, nodes and leaves will be split as we walk down the
2650 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2651 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002652 */
Chris Masone089f052007-03-16 16:20:31 -04002653int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2654 *root, struct btrfs_key *key, struct btrfs_path *p, int
2655 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002656{
Chris Mason5f39d392007-10-15 16:14:19 -04002657 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002658 int slot;
2659 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002660 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002661 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002662 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002663 int root_lock;
2664 /* everything at write_lock_level or lower must be write locked */
2665 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002666 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002667 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002668 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002669
Chris Mason6702ed42007-08-07 16:15:09 -04002670 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002671 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002672 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002673 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002674
Chris Masonbd681512011-07-16 15:23:14 -04002675 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002676 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002677
Chris Masonbd681512011-07-16 15:23:14 -04002678 /* when we are removing items, we might have to go up to level
2679 * two as we update tree pointers Make sure we keep write
2680 * for those levels as well
2681 */
2682 write_lock_level = 2;
2683 } else if (ins_len > 0) {
2684 /*
2685 * for inserting items, make sure we have a write lock on
2686 * level 1 so we can update keys
2687 */
2688 write_lock_level = 1;
2689 }
2690
2691 if (!cow)
2692 write_lock_level = -1;
2693
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002694 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002695 write_lock_level = BTRFS_MAX_LEVEL;
2696
Chris Masonf7c79f32012-03-19 15:54:38 -04002697 min_write_lock_level = write_lock_level;
2698
Chris Masonbb803952007-03-01 12:04:21 -05002699again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002700 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002701 /*
2702 * we try very hard to do read locks on the root
2703 */
2704 root_lock = BTRFS_READ_LOCK;
2705 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002706 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002707 /*
2708 * the commit roots are read only
2709 * so we always do read locks
2710 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002711 if (p->need_commit_sem)
2712 down_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002713 b = root->commit_root;
2714 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002715 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002716 if (p->need_commit_sem)
2717 up_read(&root->fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002718 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002719 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002720 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002721 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002722 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002723 level = btrfs_header_level(b);
2724 } else {
2725 /* we don't know the level of the root node
2726 * until we actually have it read locked
2727 */
2728 b = btrfs_read_lock_root_node(root);
2729 level = btrfs_header_level(b);
2730 if (level <= write_lock_level) {
2731 /* whoops, must trade for write lock */
2732 btrfs_tree_read_unlock(b);
2733 free_extent_buffer(b);
2734 b = btrfs_lock_root_node(root);
2735 root_lock = BTRFS_WRITE_LOCK;
2736
2737 /* the level might have changed, check again */
2738 level = btrfs_header_level(b);
2739 }
2740 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002741 }
Chris Masonbd681512011-07-16 15:23:14 -04002742 p->nodes[level] = b;
2743 if (!p->skip_locking)
2744 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002745
Chris Masoneb60cea2007-02-02 09:18:22 -05002746 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002747 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002748
2749 /*
2750 * setup the path here so we can release it under lock
2751 * contention with the cow code
2752 */
Chris Mason02217ed2007-03-02 16:08:05 -05002753 if (cow) {
Nikolay Borisove23c0972017-12-12 11:14:49 +02002754 bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
2755
Chris Masonc8c42862009-04-03 10:14:18 -04002756 /*
2757 * if we don't really need to cow this block
2758 * then we don't want to set the path blocking,
2759 * so we test it here
2760 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002761 if (!should_cow_block(trans, root, b)) {
2762 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002763 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002764 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002765
Chris Masonbd681512011-07-16 15:23:14 -04002766 /*
2767 * must have write locks on this node and the
2768 * parent
2769 */
Josef Bacik5124e002012-11-07 13:44:13 -05002770 if (level > write_lock_level ||
2771 (level + 1 > write_lock_level &&
2772 level + 1 < BTRFS_MAX_LEVEL &&
2773 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002774 write_lock_level = level + 1;
2775 btrfs_release_path(p);
2776 goto again;
2777 }
2778
Filipe Manana160f4082014-07-28 19:37:17 +01002779 btrfs_set_path_blocking(p);
Nikolay Borisove23c0972017-12-12 11:14:49 +02002780 if (last_level)
2781 err = btrfs_cow_block(trans, root, b, NULL, 0,
2782 &b);
2783 else
2784 err = btrfs_cow_block(trans, root, b,
2785 p->nodes[level + 1],
2786 p->slots[level + 1], &b);
Yan Zheng33c66f42009-07-22 09:59:00 -04002787 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002788 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002789 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002790 }
Chris Mason02217ed2007-03-02 16:08:05 -05002791 }
Chris Mason65b51a02008-08-01 15:11:20 -04002792cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002793 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002794 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002795
2796 /*
2797 * we have a lock on b and as long as we aren't changing
2798 * the tree, there is no way to for the items in b to change.
2799 * It is safe to drop the lock on our parent before we
2800 * go through the expensive btree search on b.
2801 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002802 * If we're inserting or deleting (ins_len != 0), then we might
2803 * be changing slot zero, which may require changing the parent.
2804 * So, we can't drop the lock until after we know which slot
2805 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002806 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002807 if (!ins_len && !p->keep_locks) {
2808 int u = level + 1;
2809
2810 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2811 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2812 p->locks[u] = 0;
2813 }
2814 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002815
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002816 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002817 if (ret < 0)
2818 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002819
Chris Mason5f39d392007-10-15 16:14:19 -04002820 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002821 int dec = 0;
2822 if (ret && slot > 0) {
2823 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002824 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002825 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002826 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002827 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002828 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002829 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002830 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002831 if (err) {
2832 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002833 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002834 }
Chris Masonc8c42862009-04-03 10:14:18 -04002835 b = p->nodes[level];
2836 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002837
Chris Masonbd681512011-07-16 15:23:14 -04002838 /*
2839 * slot 0 is special, if we change the key
2840 * we have to update the parent pointer
2841 * which means we must have a write lock
2842 * on the parent
2843 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002844 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002845 write_lock_level < level + 1) {
2846 write_lock_level = level + 1;
2847 btrfs_release_path(p);
2848 goto again;
2849 }
2850
Chris Masonf7c79f32012-03-19 15:54:38 -04002851 unlock_up(p, level, lowest_unlock,
2852 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002853
Chris Mason925baed2008-06-25 16:01:30 -04002854 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002855 if (dec)
2856 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002857 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002858 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002859
Yan Zheng33c66f42009-07-22 09:59:00 -04002860 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002861 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002862 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002863 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002864 if (err) {
2865 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002866 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002867 }
Chris Mason76a05b32009-05-14 13:24:30 -04002868
Chris Masonb4ce94d2009-02-04 09:25:08 -05002869 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002870 level = btrfs_header_level(b);
2871 if (level <= write_lock_level) {
2872 err = btrfs_try_tree_write_lock(b);
2873 if (!err) {
2874 btrfs_set_path_blocking(p);
2875 btrfs_tree_lock(b);
2876 btrfs_clear_path_blocking(p, b,
2877 BTRFS_WRITE_LOCK);
2878 }
2879 p->locks[level] = BTRFS_WRITE_LOCK;
2880 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002881 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002882 if (!err) {
2883 btrfs_set_path_blocking(p);
2884 btrfs_tree_read_lock(b);
2885 btrfs_clear_path_blocking(p, b,
2886 BTRFS_READ_LOCK);
2887 }
2888 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002889 }
Chris Masonbd681512011-07-16 15:23:14 -04002890 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002891 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002892 } else {
2893 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002894 if (ins_len > 0 &&
2895 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002896 if (write_lock_level < 1) {
2897 write_lock_level = 1;
2898 btrfs_release_path(p);
2899 goto again;
2900 }
2901
Chris Masonb4ce94d2009-02-04 09:25:08 -05002902 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002903 err = split_leaf(trans, root, key,
2904 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002905 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002906
Yan Zheng33c66f42009-07-22 09:59:00 -04002907 BUG_ON(err > 0);
2908 if (err) {
2909 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002910 goto done;
2911 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002912 }
Chris Mason459931e2008-12-10 09:10:46 -05002913 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002914 unlock_up(p, level, lowest_unlock,
2915 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002916 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002917 }
2918 }
Chris Mason65b51a02008-08-01 15:11:20 -04002919 ret = 1;
2920done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002921 /*
2922 * we don't really know what they plan on doing with the path
2923 * from here on, so for now just mark it as blocking
2924 */
Chris Masonb9473432009-03-13 11:00:37 -04002925 if (!p->leave_spinning)
2926 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002927 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002928 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002929 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002930}
2931
Chris Mason74123bd2007-02-02 11:05:29 -05002932/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002933 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2934 * current state of the tree together with the operations recorded in the tree
2935 * modification log to search for the key in a previous version of this tree, as
2936 * denoted by the time_seq parameter.
2937 *
2938 * Naturally, there is no support for insert, delete or cow operations.
2939 *
2940 * The resulting path and return value will be set up as if we called
2941 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2942 */
2943int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2944 struct btrfs_path *p, u64 time_seq)
2945{
2946 struct extent_buffer *b;
2947 int slot;
2948 int ret;
2949 int err;
2950 int level;
2951 int lowest_unlock = 1;
2952 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002953 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002954
2955 lowest_level = p->lowest_level;
2956 WARN_ON(p->nodes[0] != NULL);
2957
2958 if (p->search_commit_root) {
2959 BUG_ON(time_seq);
2960 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2961 }
2962
2963again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002964 b = get_old_root(root, time_seq);
Nikolay Borisov56af2412018-09-13 11:35:10 +03002965 if (!b) {
2966 ret = -EIO;
2967 goto done;
2968 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002969 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002970 p->locks[level] = BTRFS_READ_LOCK;
2971
2972 while (b) {
2973 level = btrfs_header_level(b);
2974 p->nodes[level] = b;
2975 btrfs_clear_path_blocking(p, NULL, 0);
2976
2977 /*
2978 * we have a lock on b and as long as we aren't changing
2979 * the tree, there is no way to for the items in b to change.
2980 * It is safe to drop the lock on our parent before we
2981 * go through the expensive btree search on b.
2982 */
2983 btrfs_unlock_up_safe(p, level + 1);
2984
Josef Bacikd4b40872013-09-24 14:09:34 -04002985 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002986 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002987 * time.
2988 */
2989 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002990 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002991
2992 if (level != 0) {
2993 int dec = 0;
2994 if (ret && slot > 0) {
2995 dec = 1;
2996 slot -= 1;
2997 }
2998 p->slots[level] = slot;
2999 unlock_up(p, level, lowest_unlock, 0, NULL);
3000
3001 if (level == lowest_level) {
3002 if (dec)
3003 p->slots[level]++;
3004 goto done;
3005 }
3006
3007 err = read_block_for_search(NULL, root, p, &b, level,
3008 slot, key, time_seq);
3009 if (err == -EAGAIN)
3010 goto again;
3011 if (err) {
3012 ret = err;
3013 goto done;
3014 }
3015
3016 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003017 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003018 if (!err) {
3019 btrfs_set_path_blocking(p);
3020 btrfs_tree_read_lock(b);
3021 btrfs_clear_path_blocking(p, b,
3022 BTRFS_READ_LOCK);
3023 }
Josef Bacik9ec72672013-08-07 16:57:23 -04003024 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003025 if (!b) {
3026 ret = -ENOMEM;
3027 goto done;
3028 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003029 p->locks[level] = BTRFS_READ_LOCK;
3030 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003031 } else {
3032 p->slots[level] = slot;
3033 unlock_up(p, level, lowest_unlock, 0, NULL);
3034 goto done;
3035 }
3036 }
3037 ret = 1;
3038done:
3039 if (!p->leave_spinning)
3040 btrfs_set_path_blocking(p);
3041 if (ret < 0)
3042 btrfs_release_path(p);
3043
3044 return ret;
3045}
3046
3047/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003048 * helper to use instead of search slot if no exact match is needed but
3049 * instead the next or previous item should be returned.
3050 * When find_higher is true, the next higher item is returned, the next lower
3051 * otherwise.
3052 * When return_any and find_higher are both true, and no higher item is found,
3053 * return the next lower instead.
3054 * When return_any is true and find_higher is false, and no lower item is found,
3055 * return the next higher instead.
3056 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3057 * < 0 on error
3058 */
3059int btrfs_search_slot_for_read(struct btrfs_root *root,
3060 struct btrfs_key *key, struct btrfs_path *p,
3061 int find_higher, int return_any)
3062{
3063 int ret;
3064 struct extent_buffer *leaf;
3065
3066again:
3067 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3068 if (ret <= 0)
3069 return ret;
3070 /*
3071 * a return value of 1 means the path is at the position where the
3072 * item should be inserted. Normally this is the next bigger item,
3073 * but in case the previous item is the last in a leaf, path points
3074 * to the first free slot in the previous leaf, i.e. at an invalid
3075 * item.
3076 */
3077 leaf = p->nodes[0];
3078
3079 if (find_higher) {
3080 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3081 ret = btrfs_next_leaf(root, p);
3082 if (ret <= 0)
3083 return ret;
3084 if (!return_any)
3085 return 1;
3086 /*
3087 * no higher item found, return the next
3088 * lower instead
3089 */
3090 return_any = 0;
3091 find_higher = 0;
3092 btrfs_release_path(p);
3093 goto again;
3094 }
3095 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003096 if (p->slots[0] == 0) {
3097 ret = btrfs_prev_leaf(root, p);
3098 if (ret < 0)
3099 return ret;
3100 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003101 leaf = p->nodes[0];
3102 if (p->slots[0] == btrfs_header_nritems(leaf))
3103 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003104 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003105 }
Arne Jansene6793762011-09-13 11:18:10 +02003106 if (!return_any)
3107 return 1;
3108 /*
3109 * no lower item found, return the next
3110 * higher instead
3111 */
3112 return_any = 0;
3113 find_higher = 1;
3114 btrfs_release_path(p);
3115 goto again;
3116 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003117 --p->slots[0];
3118 }
3119 }
3120 return 0;
3121}
3122
3123/*
Chris Mason74123bd2007-02-02 11:05:29 -05003124 * adjust the pointers going up the tree, starting at level
3125 * making sure the right key of each node is points to 'key'.
3126 * This is used after shifting pointers to the left, so it stops
3127 * fixing up pointers when a given leaf/node is not in slot 0 of the
3128 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003129 *
Chris Mason74123bd2007-02-02 11:05:29 -05003130 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003131static void fixup_low_keys(struct btrfs_fs_info *fs_info,
3132 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003133 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003134{
3135 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003136 struct extent_buffer *t;
3137
Chris Mason234b63a2007-03-13 10:46:10 -04003138 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003139 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003140 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003141 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003142 t = path->nodes[i];
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003143 tree_mod_log_set_node_key(fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003144 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003145 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003146 if (tslot != 0)
3147 break;
3148 }
3149}
3150
Chris Mason74123bd2007-02-02 11:05:29 -05003151/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003152 * update item key.
3153 *
3154 * This function isn't completely safe. It's the caller's responsibility
3155 * that the new key won't break the order
3156 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003157void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3158 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003159 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003160{
3161 struct btrfs_disk_key disk_key;
3162 struct extent_buffer *eb;
3163 int slot;
3164
3165 eb = path->nodes[0];
3166 slot = path->slots[0];
3167 if (slot > 0) {
3168 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003169 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003170 }
3171 if (slot < btrfs_header_nritems(eb) - 1) {
3172 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003173 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003174 }
3175
3176 btrfs_cpu_key_to_disk(&disk_key, new_key);
3177 btrfs_set_item_key(eb, &disk_key, slot);
3178 btrfs_mark_buffer_dirty(eb);
3179 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003180 fixup_low_keys(fs_info, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003181}
3182
3183/*
Chris Mason74123bd2007-02-02 11:05:29 -05003184 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003185 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003186 *
3187 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3188 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003189 */
Chris Mason98ed5172008-01-03 10:01:48 -05003190static int push_node_left(struct btrfs_trans_handle *trans,
3191 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003192 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003193{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003194 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003195 int src_nritems;
3196 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003197 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003198
Chris Mason5f39d392007-10-15 16:14:19 -04003199 src_nritems = btrfs_header_nritems(src);
3200 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003201 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003202 WARN_ON(btrfs_header_generation(src) != trans->transid);
3203 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003204
Chris Masonbce4eae2008-04-24 14:42:46 -04003205 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003206 return 1;
3207
Chris Masond3977122009-01-05 21:25:51 -05003208 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003209 return 1;
3210
Chris Masonbce4eae2008-04-24 14:42:46 -04003211 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003212 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003213 if (push_items < src_nritems) {
3214 /* leave at least 8 pointers in the node if
3215 * we aren't going to empty it
3216 */
3217 if (src_nritems - push_items < 8) {
3218 if (push_items <= 8)
3219 return 1;
3220 push_items -= 8;
3221 }
3222 }
3223 } else
3224 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003225
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003226 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
3227 push_items);
3228 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003229 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003230 return ret;
3231 }
Chris Mason5f39d392007-10-15 16:14:19 -04003232 copy_extent_buffer(dst, src,
3233 btrfs_node_key_ptr_offset(dst_nritems),
3234 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003235 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003236
Chris Masonbb803952007-03-01 12:04:21 -05003237 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003238 /*
3239 * don't call tree_mod_log_eb_move here, key removal was already
3240 * fully logged by tree_mod_log_eb_copy above.
3241 */
Chris Mason5f39d392007-10-15 16:14:19 -04003242 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3243 btrfs_node_key_ptr_offset(push_items),
3244 (src_nritems - push_items) *
3245 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003246 }
Chris Mason5f39d392007-10-15 16:14:19 -04003247 btrfs_set_header_nritems(src, src_nritems - push_items);
3248 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3249 btrfs_mark_buffer_dirty(src);
3250 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003251
Chris Masonbb803952007-03-01 12:04:21 -05003252 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003253}
3254
Chris Mason97571fd2007-02-24 13:39:08 -05003255/*
Chris Mason79f95c82007-03-01 15:16:26 -05003256 * try to push data from one node into the next node right in the
3257 * tree.
3258 *
3259 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3260 * error, and > 0 if there was no room in the right hand block.
3261 *
3262 * this will only push up to 1/2 the contents of the left node over
3263 */
Chris Mason5f39d392007-10-15 16:14:19 -04003264static int balance_node_right(struct btrfs_trans_handle *trans,
3265 struct btrfs_root *root,
3266 struct extent_buffer *dst,
3267 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003268{
Chris Mason79f95c82007-03-01 15:16:26 -05003269 int push_items = 0;
3270 int max_push;
3271 int src_nritems;
3272 int dst_nritems;
3273 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003274
Chris Mason7bb86312007-12-11 09:25:06 -05003275 WARN_ON(btrfs_header_generation(src) != trans->transid);
3276 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3277
Chris Mason5f39d392007-10-15 16:14:19 -04003278 src_nritems = btrfs_header_nritems(src);
3279 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003280 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003281 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003282 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003283
Chris Masond3977122009-01-05 21:25:51 -05003284 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003285 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003286
3287 max_push = src_nritems / 2 + 1;
3288 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003289 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003290 return 1;
Yan252c38f2007-08-29 09:11:44 -04003291
Chris Mason79f95c82007-03-01 15:16:26 -05003292 if (max_push < push_items)
3293 push_items = max_push;
3294
Jan Schmidtf2304752012-05-26 11:43:17 +02003295 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003296 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3297 btrfs_node_key_ptr_offset(0),
3298 (dst_nritems) *
3299 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003300
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003301 ret = tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
3302 src_nritems - push_items, push_items);
3303 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003304 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003305 return ret;
3306 }
Chris Mason5f39d392007-10-15 16:14:19 -04003307 copy_extent_buffer(dst, src,
3308 btrfs_node_key_ptr_offset(0),
3309 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003310 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003311
Chris Mason5f39d392007-10-15 16:14:19 -04003312 btrfs_set_header_nritems(src, src_nritems - push_items);
3313 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003314
Chris Mason5f39d392007-10-15 16:14:19 -04003315 btrfs_mark_buffer_dirty(src);
3316 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003317
Chris Mason79f95c82007-03-01 15:16:26 -05003318 return ret;
3319}
3320
3321/*
Chris Mason97571fd2007-02-24 13:39:08 -05003322 * helper function to insert a new root level in the tree.
3323 * A new node is allocated, and a single item is inserted to
3324 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003325 *
3326 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003327 */
Chris Masond3977122009-01-05 21:25:51 -05003328static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003329 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003330 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003331{
Chris Mason7bb86312007-12-11 09:25:06 -05003332 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003333 struct extent_buffer *lower;
3334 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003335 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003336 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003337
3338 BUG_ON(path->nodes[level]);
3339 BUG_ON(path->nodes[level-1] != root->node);
3340
Chris Mason7bb86312007-12-11 09:25:06 -05003341 lower = path->nodes[level-1];
3342 if (level == 1)
3343 btrfs_item_key(lower, &lower_key, 0);
3344 else
3345 btrfs_node_key(lower, &lower_key, 0);
3346
David Sterba4d75f8a2014-06-15 01:54:12 +02003347 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3348 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003349 if (IS_ERR(c))
3350 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003351
Yan, Zhengf0486c62010-05-16 10:46:25 -04003352 root_add_used(root, root->nodesize);
3353
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003354 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003355 btrfs_set_header_nritems(c, 1);
3356 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003357 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003358 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003359 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003360 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003361
Ross Kirk0a4e5582013-09-24 10:12:38 +01003362 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003363 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003364
3365 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003366 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003367
Chris Mason5f39d392007-10-15 16:14:19 -04003368 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003369 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003370 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003371 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003372
3373 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003374
3375 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003376
Chris Mason925baed2008-06-25 16:01:30 -04003377 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003378 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003379 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003380
3381 /* the super has an extra ref to root->node */
3382 free_extent_buffer(old);
3383
Chris Mason0b86a832008-03-24 15:01:56 -04003384 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003385 extent_buffer_get(c);
3386 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303387 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003388 path->slots[level] = 0;
3389 return 0;
3390}
3391
Chris Mason74123bd2007-02-02 11:05:29 -05003392/*
3393 * worker function to insert a single pointer in a node.
3394 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003395 *
Chris Mason74123bd2007-02-02 11:05:29 -05003396 * slot and level indicate where you want the key to go, and
3397 * blocknr is the block the key points to.
3398 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003399static void insert_ptr(struct btrfs_trans_handle *trans,
3400 struct btrfs_root *root, struct btrfs_path *path,
3401 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003402 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003403{
Chris Mason5f39d392007-10-15 16:14:19 -04003404 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003405 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003406 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003407
3408 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003409 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003410 lower = path->nodes[level];
3411 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003412 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003413 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003414 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003415 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003416 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3417 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003418 memmove_extent_buffer(lower,
3419 btrfs_node_key_ptr_offset(slot + 1),
3420 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003421 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003422 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003423 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003424 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003425 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003426 BUG_ON(ret < 0);
3427 }
Chris Mason5f39d392007-10-15 16:14:19 -04003428 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003429 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003430 WARN_ON(trans->transid == 0);
3431 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003432 btrfs_set_header_nritems(lower, nritems + 1);
3433 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003434}
3435
Chris Mason97571fd2007-02-24 13:39:08 -05003436/*
3437 * split the node at the specified level in path in two.
3438 * The path is corrected to point to the appropriate node after the split
3439 *
3440 * Before splitting this tries to make some room in the node by pushing
3441 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003442 *
3443 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003444 */
Chris Masone02119d2008-09-05 16:13:11 -04003445static noinline int split_node(struct btrfs_trans_handle *trans,
3446 struct btrfs_root *root,
3447 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003448{
Chris Mason5f39d392007-10-15 16:14:19 -04003449 struct extent_buffer *c;
3450 struct extent_buffer *split;
3451 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003452 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003453 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003454 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003455
Chris Mason5f39d392007-10-15 16:14:19 -04003456 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003457 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003458 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003459 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003460 * trying to split the root, lets make a new one
3461 *
Liu Bofdd99c72013-05-22 12:06:51 +00003462 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003463 * insert_new_root, because that root buffer will be kept as a
3464 * normal node. We are going to log removal of half of the
3465 * elements below with tree_mod_log_eb_copy. We're holding a
3466 * tree lock on the buffer, which is why we cannot race with
3467 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003468 */
Liu Bofdd99c72013-05-22 12:06:51 +00003469 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003470 if (ret)
3471 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003472 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003473 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003474 c = path->nodes[level];
3475 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003476 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003477 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003478 if (ret < 0)
3479 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003480 }
Chris Masone66f7092007-04-20 13:16:02 -04003481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003483 mid = (c_nritems + 1) / 2;
3484 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003485
David Sterba4d75f8a2014-06-15 01:54:12 +02003486 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3487 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003488 if (IS_ERR(split))
3489 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003490
Yan, Zhengf0486c62010-05-16 10:46:25 -04003491 root_add_used(root, root->nodesize);
3492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003493 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003494 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003495 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003496 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003497 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003498 btrfs_set_header_owner(split, root->root_key.objectid);
3499 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003500 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003501 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003502 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003503 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003504
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003505 ret = tree_mod_log_eb_copy(root->fs_info, split, c, 0,
3506 mid, c_nritems - mid);
3507 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003508 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003509 return ret;
3510 }
Chris Mason5f39d392007-10-15 16:14:19 -04003511 copy_extent_buffer(split, c,
3512 btrfs_node_key_ptr_offset(0),
3513 btrfs_node_key_ptr_offset(mid),
3514 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3515 btrfs_set_header_nritems(split, c_nritems - mid);
3516 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003517 ret = 0;
3518
Chris Mason5f39d392007-10-15 16:14:19 -04003519 btrfs_mark_buffer_dirty(c);
3520 btrfs_mark_buffer_dirty(split);
3521
Jeff Mahoney143bede2012-03-01 14:56:26 +01003522 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003523 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003524
Chris Mason5de08d72007-02-24 06:24:44 -05003525 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003526 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003527 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003528 free_extent_buffer(c);
3529 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003530 path->slots[level + 1] += 1;
3531 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003532 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003533 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003534 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003535 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003536}
3537
Chris Mason74123bd2007-02-02 11:05:29 -05003538/*
3539 * how many bytes are required to store the items in a leaf. start
3540 * and nr indicate which items in the leaf to check. This totals up the
3541 * space used both by the item structs and the item data
3542 */
Chris Mason5f39d392007-10-15 16:14:19 -04003543static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544{
Josef Bacik41be1f32012-10-15 13:43:18 -04003545 struct btrfs_item *start_item;
3546 struct btrfs_item *end_item;
3547 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003548 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003549 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003550 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003551
3552 if (!nr)
3553 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003554 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003555 start_item = btrfs_item_nr(start);
3556 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003557 data_len = btrfs_token_item_offset(l, start_item, &token) +
3558 btrfs_token_item_size(l, start_item, &token);
3559 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003560 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003561 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003562 return data_len;
3563}
3564
Chris Mason74123bd2007-02-02 11:05:29 -05003565/*
Chris Masond4dbff92007-04-04 14:08:15 -04003566 * The space between the end of the leaf items and
3567 * the start of the leaf data. IOW, how much room
3568 * the leaf has left for both items and data
3569 */
Chris Masond3977122009-01-05 21:25:51 -05003570noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003571 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003572{
Chris Mason5f39d392007-10-15 16:14:19 -04003573 int nritems = btrfs_header_nritems(leaf);
3574 int ret;
3575 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3576 if (ret < 0) {
Frank Holtonefe120a2013-12-20 11:37:06 -05003577 btrfs_crit(root->fs_info,
3578 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
Jens Axboeae2f5412007-10-19 09:22:59 -04003579 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003580 leaf_space_used(leaf, 0, nritems), nritems);
3581 }
3582 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003583}
3584
Chris Mason99d8f832010-07-07 10:51:48 -04003585/*
3586 * min slot controls the lowest index we're willing to push to the
3587 * right. We'll push up to and including min_slot, but no lower
3588 */
Chris Mason44871b12009-03-13 10:04:31 -04003589static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3590 struct btrfs_root *root,
3591 struct btrfs_path *path,
3592 int data_size, int empty,
3593 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003594 int free_space, u32 left_nritems,
3595 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003596{
Chris Mason5f39d392007-10-15 16:14:19 -04003597 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003598 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003599 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003600 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003601 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003602 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003603 int push_space = 0;
3604 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003605 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003606 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003607 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003608 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003609 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003610
Chris Masoncfed81a2012-03-03 07:40:03 -05003611 btrfs_init_map_token(&token);
3612
Chris Mason34a38212007-11-07 13:31:03 -05003613 if (empty)
3614 nr = 0;
3615 else
Chris Mason99d8f832010-07-07 10:51:48 -04003616 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003617
Zheng Yan31840ae2008-09-23 13:14:14 -04003618 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003619 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003620
Chris Mason44871b12009-03-13 10:04:31 -04003621 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003622 i = left_nritems - 1;
3623 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003624 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003625
Zheng Yan31840ae2008-09-23 13:14:14 -04003626 if (!empty && push_items > 0) {
3627 if (path->slots[0] > i)
3628 break;
3629 if (path->slots[0] == i) {
3630 int space = btrfs_leaf_free_space(root, left);
3631 if (space + push_space * 2 > free_space)
3632 break;
3633 }
3634 }
3635
Chris Mason00ec4c52007-02-24 12:47:20 -05003636 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003637 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003638
Chris Masondb945352007-10-15 16:15:53 -04003639 this_item_size = btrfs_item_size(left, item);
3640 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003641 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003642
Chris Mason00ec4c52007-02-24 12:47:20 -05003643 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003644 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003645 if (i == 0)
3646 break;
3647 i--;
Chris Masondb945352007-10-15 16:15:53 -04003648 }
Chris Mason5f39d392007-10-15 16:14:19 -04003649
Chris Mason925baed2008-06-25 16:01:30 -04003650 if (push_items == 0)
3651 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003652
Julia Lawall6c1500f2012-11-03 20:30:18 +00003653 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003654
Chris Mason00ec4c52007-02-24 12:47:20 -05003655 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003656 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003657
Chris Mason5f39d392007-10-15 16:14:19 -04003658 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003659 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003660
Chris Mason00ec4c52007-02-24 12:47:20 -05003661 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003662 data_end = leaf_data_end(root, right);
3663 memmove_extent_buffer(right,
3664 btrfs_leaf_data(right) + data_end - push_space,
3665 btrfs_leaf_data(right) + data_end,
3666 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3667
Chris Mason00ec4c52007-02-24 12:47:20 -05003668 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003669 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003670 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3671 btrfs_leaf_data(left) + leaf_data_end(root, left),
3672 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003673
3674 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3675 btrfs_item_nr_offset(0),
3676 right_nritems * sizeof(struct btrfs_item));
3677
Chris Mason00ec4c52007-02-24 12:47:20 -05003678 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003679 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3680 btrfs_item_nr_offset(left_nritems - push_items),
3681 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003682
3683 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003684 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003685 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003686 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003687 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003688 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003689 push_space -= btrfs_token_item_size(right, item, &token);
3690 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003691 }
3692
Chris Mason7518a232007-03-12 12:01:18 -04003693 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003694 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003695
Chris Mason34a38212007-11-07 13:31:03 -05003696 if (left_nritems)
3697 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003698 else
Daniel Dressler01d58472014-11-21 17:15:07 +09003699 clean_tree_block(trans, root->fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003700
Chris Mason5f39d392007-10-15 16:14:19 -04003701 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003702
Chris Mason5f39d392007-10-15 16:14:19 -04003703 btrfs_item_key(right, &disk_key, 0);
3704 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003705 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003706
Chris Mason00ec4c52007-02-24 12:47:20 -05003707 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003708 if (path->slots[0] >= left_nritems) {
3709 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003710 if (btrfs_header_nritems(path->nodes[0]) == 0)
Daniel Dressler01d58472014-11-21 17:15:07 +09003711 clean_tree_block(trans, root->fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003712 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003713 free_extent_buffer(path->nodes[0]);
3714 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003715 path->slots[1] += 1;
3716 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003717 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003718 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003719 }
3720 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003721
3722out_unlock:
3723 btrfs_tree_unlock(right);
3724 free_extent_buffer(right);
3725 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003726}
Chris Mason925baed2008-06-25 16:01:30 -04003727
Chris Mason00ec4c52007-02-24 12:47:20 -05003728/*
Chris Mason44871b12009-03-13 10:04:31 -04003729 * push some data in the path leaf to the right, trying to free up at
3730 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3731 *
3732 * returns 1 if the push failed because the other node didn't have enough
3733 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003734 *
3735 * this will push starting from min_slot to the end of the leaf. It won't
3736 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003737 */
3738static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003739 *root, struct btrfs_path *path,
3740 int min_data_size, int data_size,
3741 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003742{
3743 struct extent_buffer *left = path->nodes[0];
3744 struct extent_buffer *right;
3745 struct extent_buffer *upper;
3746 int slot;
3747 int free_space;
3748 u32 left_nritems;
3749 int ret;
3750
3751 if (!path->nodes[1])
3752 return 1;
3753
3754 slot = path->slots[1];
3755 upper = path->nodes[1];
3756 if (slot >= btrfs_header_nritems(upper) - 1)
3757 return 1;
3758
3759 btrfs_assert_tree_locked(path->nodes[1]);
3760
3761 right = read_node_slot(root, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003762 /*
3763 * slot + 1 is not valid or we fail to read the right node,
3764 * no big deal, just return.
3765 */
3766 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003767 return 1;
3768
Chris Mason44871b12009-03-13 10:04:31 -04003769 btrfs_tree_lock(right);
3770 btrfs_set_lock_blocking(right);
3771
3772 free_space = btrfs_leaf_free_space(root, right);
3773 if (free_space < data_size)
3774 goto out_unlock;
3775
3776 /* cow and double check */
3777 ret = btrfs_cow_block(trans, root, right, upper,
3778 slot + 1, &right);
3779 if (ret)
3780 goto out_unlock;
3781
3782 free_space = btrfs_leaf_free_space(root, right);
3783 if (free_space < data_size)
3784 goto out_unlock;
3785
3786 left_nritems = btrfs_header_nritems(left);
3787 if (left_nritems == 0)
3788 goto out_unlock;
3789
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003790 if (path->slots[0] == left_nritems && !empty) {
3791 /* Key greater than all keys in the leaf, right neighbor has
3792 * enough room for it and we're not emptying our leaf to delete
3793 * it, therefore use right neighbor to insert the new item and
3794 * no need to touch/dirty our left leaft. */
3795 btrfs_tree_unlock(left);
3796 free_extent_buffer(left);
3797 path->nodes[0] = right;
3798 path->slots[0] = 0;
3799 path->slots[1]++;
3800 return 0;
3801 }
3802
Chris Mason99d8f832010-07-07 10:51:48 -04003803 return __push_leaf_right(trans, root, path, min_data_size, empty,
3804 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003805out_unlock:
3806 btrfs_tree_unlock(right);
3807 free_extent_buffer(right);
3808 return 1;
3809}
3810
3811/*
Chris Mason74123bd2007-02-02 11:05:29 -05003812 * push some data in the path leaf to the left, trying to free up at
3813 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003814 *
3815 * max_slot can put a limit on how far into the leaf we'll push items. The
3816 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3817 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003818 */
Chris Mason44871b12009-03-13 10:04:31 -04003819static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3820 struct btrfs_root *root,
3821 struct btrfs_path *path, int data_size,
3822 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003823 int free_space, u32 right_nritems,
3824 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003825{
Chris Mason5f39d392007-10-15 16:14:19 -04003826 struct btrfs_disk_key disk_key;
3827 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003828 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003829 int push_space = 0;
3830 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003831 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003832 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003833 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003834 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003835 u32 this_item_size;
3836 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003837 struct btrfs_map_token token;
3838
3839 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003840
Chris Mason34a38212007-11-07 13:31:03 -05003841 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003842 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003843 else
Chris Mason99d8f832010-07-07 10:51:48 -04003844 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003845
3846 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003847 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003848
Zheng Yan31840ae2008-09-23 13:14:14 -04003849 if (!empty && push_items > 0) {
3850 if (path->slots[0] < i)
3851 break;
3852 if (path->slots[0] == i) {
3853 int space = btrfs_leaf_free_space(root, right);
3854 if (space + push_space * 2 > free_space)
3855 break;
3856 }
3857 }
3858
Chris Masonbe0e5c02007-01-26 15:51:26 -05003859 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003860 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003861
3862 this_item_size = btrfs_item_size(right, item);
3863 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003864 break;
Chris Masondb945352007-10-15 16:15:53 -04003865
Chris Masonbe0e5c02007-01-26 15:51:26 -05003866 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003867 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003868 }
Chris Masondb945352007-10-15 16:15:53 -04003869
Chris Masonbe0e5c02007-01-26 15:51:26 -05003870 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003871 ret = 1;
3872 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003873 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303874 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003875
Chris Masonbe0e5c02007-01-26 15:51:26 -05003876 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003877 copy_extent_buffer(left, right,
3878 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3879 btrfs_item_nr_offset(0),
3880 push_items * sizeof(struct btrfs_item));
3881
Chris Mason123abc82007-03-14 14:14:43 -04003882 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003883 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003884
3885 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003886 leaf_data_end(root, left) - push_space,
3887 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003888 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003889 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003890 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003891 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003892
Chris Masondb945352007-10-15 16:15:53 -04003893 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003894 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003895 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003896
Ross Kirkdd3cc162013-09-16 15:58:09 +01003897 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003898
Chris Masoncfed81a2012-03-03 07:40:03 -05003899 ioff = btrfs_token_item_offset(left, item, &token);
3900 btrfs_set_token_item_offset(left, item,
3901 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3902 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003903 }
Chris Mason5f39d392007-10-15 16:14:19 -04003904 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003905
3906 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003907 if (push_items > right_nritems)
3908 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003909 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003910
Chris Mason34a38212007-11-07 13:31:03 -05003911 if (push_items < right_nritems) {
3912 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3913 leaf_data_end(root, right);
3914 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3915 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3916 btrfs_leaf_data(right) +
3917 leaf_data_end(root, right), push_space);
3918
3919 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003920 btrfs_item_nr_offset(push_items),
3921 (btrfs_header_nritems(right) - push_items) *
3922 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003923 }
Yaneef1c492007-11-26 10:58:13 -05003924 right_nritems -= push_items;
3925 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003926 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003927 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003928 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003929
Chris Masoncfed81a2012-03-03 07:40:03 -05003930 push_space = push_space - btrfs_token_item_size(right,
3931 item, &token);
3932 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003933 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003934
Chris Mason5f39d392007-10-15 16:14:19 -04003935 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003936 if (right_nritems)
3937 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003938 else
Daniel Dressler01d58472014-11-21 17:15:07 +09003939 clean_tree_block(trans, root->fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003940
Chris Mason5f39d392007-10-15 16:14:19 -04003941 btrfs_item_key(right, &disk_key, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003942 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003943
3944 /* then fixup the leaf pointer in the path */
3945 if (path->slots[0] < push_items) {
3946 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003947 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003948 free_extent_buffer(path->nodes[0]);
3949 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003950 path->slots[1] -= 1;
3951 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003952 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003953 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003954 path->slots[0] -= push_items;
3955 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003956 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003957 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003958out:
3959 btrfs_tree_unlock(left);
3960 free_extent_buffer(left);
3961 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003962}
3963
Chris Mason74123bd2007-02-02 11:05:29 -05003964/*
Chris Mason44871b12009-03-13 10:04:31 -04003965 * push some data in the path leaf to the left, trying to free up at
3966 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003967 *
3968 * max_slot can put a limit on how far into the leaf we'll push items. The
3969 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3970 * items
Chris Mason44871b12009-03-13 10:04:31 -04003971 */
3972static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003973 *root, struct btrfs_path *path, int min_data_size,
3974 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003975{
3976 struct extent_buffer *right = path->nodes[0];
3977 struct extent_buffer *left;
3978 int slot;
3979 int free_space;
3980 u32 right_nritems;
3981 int ret = 0;
3982
3983 slot = path->slots[1];
3984 if (slot == 0)
3985 return 1;
3986 if (!path->nodes[1])
3987 return 1;
3988
3989 right_nritems = btrfs_header_nritems(right);
3990 if (right_nritems == 0)
3991 return 1;
3992
3993 btrfs_assert_tree_locked(path->nodes[1]);
3994
3995 left = read_node_slot(root, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003996 /*
3997 * slot - 1 is not valid or we fail to read the left node,
3998 * no big deal, just return.
3999 */
4000 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004001 return 1;
4002
Chris Mason44871b12009-03-13 10:04:31 -04004003 btrfs_tree_lock(left);
4004 btrfs_set_lock_blocking(left);
4005
4006 free_space = btrfs_leaf_free_space(root, left);
4007 if (free_space < data_size) {
4008 ret = 1;
4009 goto out;
4010 }
4011
4012 /* cow and double check */
4013 ret = btrfs_cow_block(trans, root, left,
4014 path->nodes[1], slot - 1, &left);
4015 if (ret) {
4016 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004017 if (ret == -ENOSPC)
4018 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004019 goto out;
4020 }
4021
4022 free_space = btrfs_leaf_free_space(root, left);
4023 if (free_space < data_size) {
4024 ret = 1;
4025 goto out;
4026 }
4027
Chris Mason99d8f832010-07-07 10:51:48 -04004028 return __push_leaf_left(trans, root, path, min_data_size,
4029 empty, left, free_space, right_nritems,
4030 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004031out:
4032 btrfs_tree_unlock(left);
4033 free_extent_buffer(left);
4034 return ret;
4035}
4036
4037/*
Chris Mason74123bd2007-02-02 11:05:29 -05004038 * split the path's leaf in two, making sure there is at least data_size
4039 * available for the resulting leaf level of the path.
4040 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004041static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4042 struct btrfs_root *root,
4043 struct btrfs_path *path,
4044 struct extent_buffer *l,
4045 struct extent_buffer *right,
4046 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004047{
Chris Masonbe0e5c02007-01-26 15:51:26 -05004048 int data_copy_size;
4049 int rt_data_off;
4050 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004051 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004052 struct btrfs_map_token token;
4053
4054 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004055
Chris Mason5f39d392007-10-15 16:14:19 -04004056 nritems = nritems - mid;
4057 btrfs_set_header_nritems(right, nritems);
4058 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4059
4060 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4061 btrfs_item_nr_offset(mid),
4062 nritems * sizeof(struct btrfs_item));
4063
4064 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04004065 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
4066 data_copy_size, btrfs_leaf_data(l) +
4067 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004068
Chris Mason5f39d392007-10-15 16:14:19 -04004069 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
4070 btrfs_item_end_nr(l, mid);
4071
4072 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004073 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004074 u32 ioff;
4075
Chris Masoncfed81a2012-03-03 07:40:03 -05004076 ioff = btrfs_token_item_offset(right, item, &token);
4077 btrfs_set_token_item_offset(right, item,
4078 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004079 }
Chris Mason74123bd2007-02-02 11:05:29 -05004080
Chris Mason5f39d392007-10-15 16:14:19 -04004081 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004082 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004083 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004084 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004085
4086 btrfs_mark_buffer_dirty(right);
4087 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004088 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004089
Chris Masonbe0e5c02007-01-26 15:51:26 -05004090 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004091 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004092 free_extent_buffer(path->nodes[0]);
4093 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004094 path->slots[0] -= mid;
4095 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004096 } else {
4097 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004098 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004099 }
Chris Mason5f39d392007-10-15 16:14:19 -04004100
Chris Masoneb60cea2007-02-02 09:18:22 -05004101 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004102}
4103
4104/*
Chris Mason99d8f832010-07-07 10:51:48 -04004105 * double splits happen when we need to insert a big item in the middle
4106 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4107 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4108 * A B C
4109 *
4110 * We avoid this by trying to push the items on either side of our target
4111 * into the adjacent leaves. If all goes well we can avoid the double split
4112 * completely.
4113 */
4114static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4115 struct btrfs_root *root,
4116 struct btrfs_path *path,
4117 int data_size)
4118{
4119 int ret;
4120 int progress = 0;
4121 int slot;
4122 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004123 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004124
4125 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004126 if (slot < btrfs_header_nritems(path->nodes[0]))
4127 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004128
4129 /*
4130 * try to push all the items after our slot into the
4131 * right leaf
4132 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004133 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004134 if (ret < 0)
4135 return ret;
4136
4137 if (ret == 0)
4138 progress++;
4139
4140 nritems = btrfs_header_nritems(path->nodes[0]);
4141 /*
4142 * our goal is to get our slot at the start or end of a leaf. If
4143 * we've done so we're done
4144 */
4145 if (path->slots[0] == 0 || path->slots[0] == nritems)
4146 return 0;
4147
4148 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4149 return 0;
4150
4151 /* try to push all the items before our slot into the next leaf */
4152 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004153 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004154 if (ret < 0)
4155 return ret;
4156
4157 if (ret == 0)
4158 progress++;
4159
4160 if (progress)
4161 return 0;
4162 return 1;
4163}
4164
4165/*
Chris Mason44871b12009-03-13 10:04:31 -04004166 * split the path's leaf in two, making sure there is at least data_size
4167 * available for the resulting leaf level of the path.
4168 *
4169 * returns 0 if all went well and < 0 on failure.
4170 */
4171static noinline int split_leaf(struct btrfs_trans_handle *trans,
4172 struct btrfs_root *root,
4173 struct btrfs_key *ins_key,
4174 struct btrfs_path *path, int data_size,
4175 int extend)
4176{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004177 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004178 struct extent_buffer *l;
4179 u32 nritems;
4180 int mid;
4181 int slot;
4182 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004183 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004184 int ret = 0;
4185 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004186 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004187 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004188 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004189
Yan, Zhenga5719522009-09-24 09:17:31 -04004190 l = path->nodes[0];
4191 slot = path->slots[0];
4192 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4193 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4194 return -EOVERFLOW;
4195
Chris Mason44871b12009-03-13 10:04:31 -04004196 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004197 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004198 int space_needed = data_size;
4199
4200 if (slot < btrfs_header_nritems(l))
4201 space_needed -= btrfs_leaf_free_space(root, l);
4202
4203 wret = push_leaf_right(trans, root, path, space_needed,
4204 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004205 if (wret < 0)
4206 return wret;
4207 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004208 wret = push_leaf_left(trans, root, path, space_needed,
4209 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004210 if (wret < 0)
4211 return wret;
4212 }
4213 l = path->nodes[0];
4214
4215 /* did the pushes work? */
4216 if (btrfs_leaf_free_space(root, l) >= data_size)
4217 return 0;
4218 }
4219
4220 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004221 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004222 if (ret)
4223 return ret;
4224 }
4225again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004226 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004227 l = path->nodes[0];
4228 slot = path->slots[0];
4229 nritems = btrfs_header_nritems(l);
4230 mid = (nritems + 1) / 2;
4231
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004232 if (mid <= slot) {
4233 if (nritems == 1 ||
4234 leaf_space_used(l, mid, nritems - mid) + data_size >
4235 BTRFS_LEAF_DATA_SIZE(root)) {
4236 if (slot >= nritems) {
4237 split = 0;
4238 } else {
4239 mid = slot;
4240 if (mid != nritems &&
4241 leaf_space_used(l, mid, nritems - mid) +
4242 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004243 if (data_size && !tried_avoid_double)
4244 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004245 split = 2;
4246 }
4247 }
4248 }
4249 } else {
4250 if (leaf_space_used(l, 0, mid) + data_size >
4251 BTRFS_LEAF_DATA_SIZE(root)) {
4252 if (!extend && data_size && slot == 0) {
4253 split = 0;
4254 } else if ((extend || !data_size) && slot == 0) {
4255 mid = 1;
4256 } else {
4257 mid = slot;
4258 if (mid != nritems &&
4259 leaf_space_used(l, mid, nritems - mid) +
4260 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004261 if (data_size && !tried_avoid_double)
4262 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304263 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004264 }
4265 }
4266 }
4267 }
4268
4269 if (split == 0)
4270 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4271 else
4272 btrfs_item_key(l, &disk_key, mid);
4273
David Sterba4d75f8a2014-06-15 01:54:12 +02004274 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4275 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004276 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004277 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004278
David Sterba707e8a02014-06-04 19:22:26 +02004279 root_add_used(root, root->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004280
4281 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4282 btrfs_set_header_bytenr(right, right->start);
4283 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004284 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004285 btrfs_set_header_owner(right, root->root_key.objectid);
4286 btrfs_set_header_level(right, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004287 write_extent_buffer(right, fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004288 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004289
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004290 write_extent_buffer(right, fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004291 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004292 BTRFS_UUID_SIZE);
4293
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004294 if (split == 0) {
4295 if (mid <= slot) {
4296 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004297 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004298 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004299 btrfs_tree_unlock(path->nodes[0]);
4300 free_extent_buffer(path->nodes[0]);
4301 path->nodes[0] = right;
4302 path->slots[0] = 0;
4303 path->slots[1] += 1;
4304 } else {
4305 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004306 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004307 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004308 btrfs_tree_unlock(path->nodes[0]);
4309 free_extent_buffer(path->nodes[0]);
4310 path->nodes[0] = right;
4311 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004312 if (path->slots[1] == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004313 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004314 }
Liu Bo196e0242016-09-07 14:48:28 -07004315 /*
4316 * We create a new leaf 'right' for the required ins_len and
4317 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4318 * the content of ins_len to 'right'.
4319 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004320 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004321 }
4322
Jeff Mahoney143bede2012-03-01 14:56:26 +01004323 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004324
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004326 BUG_ON(num_doubles != 0);
4327 num_doubles++;
4328 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004329 }
Chris Mason44871b12009-03-13 10:04:31 -04004330
Jeff Mahoney143bede2012-03-01 14:56:26 +01004331 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004332
4333push_for_double:
4334 push_for_double_split(trans, root, path, data_size);
4335 tried_avoid_double = 1;
4336 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4337 return 0;
4338 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004339}
4340
Yan, Zhengad48fd752009-11-12 09:33:58 +00004341static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4342 struct btrfs_root *root,
4343 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004344{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004345 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004346 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004347 struct btrfs_file_extent_item *fi;
4348 u64 extent_len = 0;
4349 u32 item_size;
4350 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004351
4352 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004353 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4354
4355 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4356 key.type != BTRFS_EXTENT_CSUM_KEY);
4357
4358 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4359 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004360
4361 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004362 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4363 fi = btrfs_item_ptr(leaf, path->slots[0],
4364 struct btrfs_file_extent_item);
4365 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4366 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004367 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004368
Chris Mason459931e2008-12-10 09:10:46 -05004369 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004370 path->search_for_split = 1;
4371 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004372 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004373 if (ret > 0)
4374 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004375 if (ret < 0)
4376 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004377
Yan, Zhengad48fd752009-11-12 09:33:58 +00004378 ret = -EAGAIN;
4379 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004380 /* if our item isn't there, return now */
4381 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004382 goto err;
4383
Chris Mason109f6ae2010-04-02 09:20:18 -04004384 /* the leaf has changed, it now has room. return now */
4385 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4386 goto err;
4387
Yan, Zhengad48fd752009-11-12 09:33:58 +00004388 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4389 fi = btrfs_item_ptr(leaf, path->slots[0],
4390 struct btrfs_file_extent_item);
4391 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4392 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004393 }
4394
Chris Masonb9473432009-03-13 11:00:37 -04004395 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004396 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004397 if (ret)
4398 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004399
Yan, Zhengad48fd752009-11-12 09:33:58 +00004400 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004401 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004402 return 0;
4403err:
4404 path->keep_locks = 0;
4405 return ret;
4406}
4407
4408static noinline int split_item(struct btrfs_trans_handle *trans,
4409 struct btrfs_root *root,
4410 struct btrfs_path *path,
4411 struct btrfs_key *new_key,
4412 unsigned long split_offset)
4413{
4414 struct extent_buffer *leaf;
4415 struct btrfs_item *item;
4416 struct btrfs_item *new_item;
4417 int slot;
4418 char *buf;
4419 u32 nritems;
4420 u32 item_size;
4421 u32 orig_offset;
4422 struct btrfs_disk_key disk_key;
4423
Chris Masonb9473432009-03-13 11:00:37 -04004424 leaf = path->nodes[0];
4425 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4426
Chris Masonb4ce94d2009-02-04 09:25:08 -05004427 btrfs_set_path_blocking(path);
4428
Ross Kirkdd3cc162013-09-16 15:58:09 +01004429 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004430 orig_offset = btrfs_item_offset(leaf, item);
4431 item_size = btrfs_item_size(leaf, item);
4432
Chris Mason459931e2008-12-10 09:10:46 -05004433 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004434 if (!buf)
4435 return -ENOMEM;
4436
Chris Mason459931e2008-12-10 09:10:46 -05004437 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4438 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004439
Chris Mason459931e2008-12-10 09:10:46 -05004440 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004441 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004442 if (slot != nritems) {
4443 /* shift the items */
4444 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004445 btrfs_item_nr_offset(slot),
4446 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004447 }
4448
4449 btrfs_cpu_key_to_disk(&disk_key, new_key);
4450 btrfs_set_item_key(leaf, &disk_key, slot);
4451
Ross Kirkdd3cc162013-09-16 15:58:09 +01004452 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004453
4454 btrfs_set_item_offset(leaf, new_item, orig_offset);
4455 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4456
4457 btrfs_set_item_offset(leaf, item,
4458 orig_offset + item_size - split_offset);
4459 btrfs_set_item_size(leaf, item, split_offset);
4460
4461 btrfs_set_header_nritems(leaf, nritems + 1);
4462
4463 /* write the data for the start of the original item */
4464 write_extent_buffer(leaf, buf,
4465 btrfs_item_ptr_offset(leaf, path->slots[0]),
4466 split_offset);
4467
4468 /* write the data for the new item */
4469 write_extent_buffer(leaf, buf + split_offset,
4470 btrfs_item_ptr_offset(leaf, slot),
4471 item_size - split_offset);
4472 btrfs_mark_buffer_dirty(leaf);
4473
Yan, Zhengad48fd752009-11-12 09:33:58 +00004474 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004475 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004476 return 0;
4477}
4478
4479/*
4480 * This function splits a single item into two items,
4481 * giving 'new_key' to the new item and splitting the
4482 * old one at split_offset (from the start of the item).
4483 *
4484 * The path may be released by this operation. After
4485 * the split, the path is pointing to the old item. The
4486 * new item is going to be in the same node as the old one.
4487 *
4488 * Note, the item being split must be smaller enough to live alone on
4489 * a tree block with room for one extra struct btrfs_item
4490 *
4491 * This allows us to split the item in place, keeping a lock on the
4492 * leaf the entire time.
4493 */
4494int btrfs_split_item(struct btrfs_trans_handle *trans,
4495 struct btrfs_root *root,
4496 struct btrfs_path *path,
4497 struct btrfs_key *new_key,
4498 unsigned long split_offset)
4499{
4500 int ret;
4501 ret = setup_leaf_for_split(trans, root, path,
4502 sizeof(struct btrfs_item));
4503 if (ret)
4504 return ret;
4505
4506 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004507 return ret;
4508}
4509
4510/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004511 * This function duplicate a item, giving 'new_key' to the new item.
4512 * It guarantees both items live in the same tree leaf and the new item
4513 * is contiguous with the original item.
4514 *
4515 * This allows us to split file extent in place, keeping a lock on the
4516 * leaf the entire time.
4517 */
4518int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4519 struct btrfs_root *root,
4520 struct btrfs_path *path,
4521 struct btrfs_key *new_key)
4522{
4523 struct extent_buffer *leaf;
4524 int ret;
4525 u32 item_size;
4526
4527 leaf = path->nodes[0];
4528 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4529 ret = setup_leaf_for_split(trans, root, path,
4530 item_size + sizeof(struct btrfs_item));
4531 if (ret)
4532 return ret;
4533
4534 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004535 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004536 item_size, item_size +
4537 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004538 leaf = path->nodes[0];
4539 memcpy_extent_buffer(leaf,
4540 btrfs_item_ptr_offset(leaf, path->slots[0]),
4541 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4542 item_size);
4543 return 0;
4544}
4545
4546/*
Chris Masond352ac62008-09-29 15:18:18 -04004547 * make the item pointed to by the path smaller. new_size indicates
4548 * how small to make it, and from_end tells us if we just chop bytes
4549 * off the end of the item or if we shift the item to chop bytes off
4550 * the front.
4551 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004552void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004553 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004554{
Chris Masonb18c6682007-04-17 13:26:50 -04004555 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004556 struct extent_buffer *leaf;
4557 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004558 u32 nritems;
4559 unsigned int data_end;
4560 unsigned int old_data_start;
4561 unsigned int old_size;
4562 unsigned int size_diff;
4563 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004564 struct btrfs_map_token token;
4565
4566 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004567
Chris Mason5f39d392007-10-15 16:14:19 -04004568 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004569 slot = path->slots[0];
4570
4571 old_size = btrfs_item_size_nr(leaf, slot);
4572 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004573 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004574
Chris Mason5f39d392007-10-15 16:14:19 -04004575 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004576 data_end = leaf_data_end(root, leaf);
4577
Chris Mason5f39d392007-10-15 16:14:19 -04004578 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004579
Chris Masonb18c6682007-04-17 13:26:50 -04004580 size_diff = old_size - new_size;
4581
4582 BUG_ON(slot < 0);
4583 BUG_ON(slot >= nritems);
4584
4585 /*
4586 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4587 */
4588 /* first correct the data pointers */
4589 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004590 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004591 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004592
Chris Masoncfed81a2012-03-03 07:40:03 -05004593 ioff = btrfs_token_item_offset(leaf, item, &token);
4594 btrfs_set_token_item_offset(leaf, item,
4595 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004596 }
Chris Masondb945352007-10-15 16:15:53 -04004597
Chris Masonb18c6682007-04-17 13:26:50 -04004598 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004599 if (from_end) {
4600 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4601 data_end + size_diff, btrfs_leaf_data(leaf) +
4602 data_end, old_data_start + new_size - data_end);
4603 } else {
4604 struct btrfs_disk_key disk_key;
4605 u64 offset;
4606
4607 btrfs_item_key(leaf, &disk_key, slot);
4608
4609 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4610 unsigned long ptr;
4611 struct btrfs_file_extent_item *fi;
4612
4613 fi = btrfs_item_ptr(leaf, slot,
4614 struct btrfs_file_extent_item);
4615 fi = (struct btrfs_file_extent_item *)(
4616 (unsigned long)fi - size_diff);
4617
4618 if (btrfs_file_extent_type(leaf, fi) ==
4619 BTRFS_FILE_EXTENT_INLINE) {
4620 ptr = btrfs_item_ptr_offset(leaf, slot);
4621 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004622 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004623 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004624 }
4625 }
4626
4627 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4628 data_end + size_diff, btrfs_leaf_data(leaf) +
4629 data_end, old_data_start - data_end);
4630
4631 offset = btrfs_disk_key_offset(&disk_key);
4632 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4633 btrfs_set_item_key(leaf, &disk_key, slot);
4634 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004635 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004636 }
Chris Mason5f39d392007-10-15 16:14:19 -04004637
Ross Kirkdd3cc162013-09-16 15:58:09 +01004638 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004639 btrfs_set_item_size(leaf, item, new_size);
4640 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004641
Chris Mason5f39d392007-10-15 16:14:19 -04004642 if (btrfs_leaf_free_space(root, leaf) < 0) {
4643 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004644 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004645 }
Chris Masonb18c6682007-04-17 13:26:50 -04004646}
4647
Chris Masond352ac62008-09-29 15:18:18 -04004648/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004649 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004650 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004651void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004652 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004653{
Chris Mason6567e832007-04-16 09:22:45 -04004654 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004655 struct extent_buffer *leaf;
4656 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004657 u32 nritems;
4658 unsigned int data_end;
4659 unsigned int old_data;
4660 unsigned int old_size;
4661 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004662 struct btrfs_map_token token;
4663
4664 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004665
Chris Mason5f39d392007-10-15 16:14:19 -04004666 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004667
Chris Mason5f39d392007-10-15 16:14:19 -04004668 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004669 data_end = leaf_data_end(root, leaf);
4670
Chris Mason5f39d392007-10-15 16:14:19 -04004671 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4672 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004673 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004674 }
Chris Mason6567e832007-04-16 09:22:45 -04004675 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004676 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004677
4678 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004679 if (slot >= nritems) {
4680 btrfs_print_leaf(root, leaf);
Frank Holtonefe120a2013-12-20 11:37:06 -05004681 btrfs_crit(root->fs_info, "slot %d too large, nritems %d",
Chris Masond3977122009-01-05 21:25:51 -05004682 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004683 BUG_ON(1);
4684 }
Chris Mason6567e832007-04-16 09:22:45 -04004685
4686 /*
4687 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4688 */
4689 /* first correct the data pointers */
4690 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004691 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004692 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004693
Chris Masoncfed81a2012-03-03 07:40:03 -05004694 ioff = btrfs_token_item_offset(leaf, item, &token);
4695 btrfs_set_token_item_offset(leaf, item,
4696 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004697 }
Chris Mason5f39d392007-10-15 16:14:19 -04004698
Chris Mason6567e832007-04-16 09:22:45 -04004699 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004700 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004701 data_end - data_size, btrfs_leaf_data(leaf) +
4702 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004703
Chris Mason6567e832007-04-16 09:22:45 -04004704 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004705 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004706 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004707 btrfs_set_item_size(leaf, item, old_size + data_size);
4708 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004709
Chris Mason5f39d392007-10-15 16:14:19 -04004710 if (btrfs_leaf_free_space(root, leaf) < 0) {
4711 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004712 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004713 }
Chris Mason6567e832007-04-16 09:22:45 -04004714}
4715
Chris Mason74123bd2007-02-02 11:05:29 -05004716/*
Chris Mason44871b12009-03-13 10:04:31 -04004717 * this is a helper for btrfs_insert_empty_items, the main goal here is
4718 * to save stack depth by doing the bulk of the work in a function
4719 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004720 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004721void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004722 struct btrfs_key *cpu_key, u32 *data_size,
4723 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004724{
Chris Mason5f39d392007-10-15 16:14:19 -04004725 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004726 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004727 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004728 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004729 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004730 struct extent_buffer *leaf;
4731 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004732 struct btrfs_map_token token;
4733
Filipe Manana24cdc842014-07-28 19:34:35 +01004734 if (path->slots[0] == 0) {
4735 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004736 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004737 }
4738 btrfs_unlock_up_safe(path, 1);
4739
Chris Masoncfed81a2012-03-03 07:40:03 -05004740 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004741
Chris Mason5f39d392007-10-15 16:14:19 -04004742 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004743 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004744
Chris Mason5f39d392007-10-15 16:14:19 -04004745 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004746 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004747
Chris Masonf25956c2008-09-12 15:32:53 -04004748 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004749 btrfs_print_leaf(root, leaf);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004750 btrfs_crit(root->fs_info,
4751 "not enough freespace need %u have %d",
4752 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004753 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004754 }
Chris Mason5f39d392007-10-15 16:14:19 -04004755
Chris Masonbe0e5c02007-01-26 15:51:26 -05004756 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004757 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004758
Chris Mason5f39d392007-10-15 16:14:19 -04004759 if (old_data < data_end) {
4760 btrfs_print_leaf(root, leaf);
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004761 btrfs_crit(root->fs_info,
4762 "slot %d old_data %d data_end %d",
4763 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004764 BUG_ON(1);
4765 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004766 /*
4767 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4768 */
4769 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004770 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004771 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004772
Jeff Mahoney62e85572016-09-20 10:05:01 -04004773 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004774 ioff = btrfs_token_item_offset(leaf, item, &token);
4775 btrfs_set_token_item_offset(leaf, item,
4776 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004777 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004778 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004779 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004780 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004781 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004782
4783 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004784 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004785 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004786 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004787 data_end = old_data;
4788 }
Chris Mason5f39d392007-10-15 16:14:19 -04004789
Chris Mason62e27492007-03-15 12:56:47 -04004790 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004791 for (i = 0; i < nr; i++) {
4792 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4793 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004794 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004795 btrfs_set_token_item_offset(leaf, item,
4796 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004797 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004798 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004799 }
Chris Mason44871b12009-03-13 10:04:31 -04004800
Chris Mason9c583092008-01-29 15:15:18 -05004801 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004802 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004803
Chris Mason5f39d392007-10-15 16:14:19 -04004804 if (btrfs_leaf_free_space(root, leaf) < 0) {
4805 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004806 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004807 }
Chris Mason44871b12009-03-13 10:04:31 -04004808}
4809
4810/*
4811 * Given a key and some data, insert items into the tree.
4812 * This does all the path init required, making room in the tree if needed.
4813 */
4814int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4815 struct btrfs_root *root,
4816 struct btrfs_path *path,
4817 struct btrfs_key *cpu_key, u32 *data_size,
4818 int nr)
4819{
Chris Mason44871b12009-03-13 10:04:31 -04004820 int ret = 0;
4821 int slot;
4822 int i;
4823 u32 total_size = 0;
4824 u32 total_data = 0;
4825
4826 for (i = 0; i < nr; i++)
4827 total_data += data_size[i];
4828
4829 total_size = total_data + (nr * sizeof(struct btrfs_item));
4830 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4831 if (ret == 0)
4832 return -EEXIST;
4833 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004834 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004835
Chris Mason44871b12009-03-13 10:04:31 -04004836 slot = path->slots[0];
4837 BUG_ON(slot < 0);
4838
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004839 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004840 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004841 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004842}
4843
4844/*
4845 * Given a key and some data, insert an item into the tree.
4846 * This does all the path init required, making room in the tree if needed.
4847 */
Chris Masone089f052007-03-16 16:20:31 -04004848int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4849 *root, struct btrfs_key *cpu_key, void *data, u32
4850 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004851{
4852 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004853 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004854 struct extent_buffer *leaf;
4855 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004856
Chris Mason2c90e5d2007-04-02 10:50:19 -04004857 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004858 if (!path)
4859 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004860 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004861 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004862 leaf = path->nodes[0];
4863 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4864 write_extent_buffer(leaf, data, ptr, data_size);
4865 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004866 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004867 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004868 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004869}
4870
Chris Mason74123bd2007-02-02 11:05:29 -05004871/*
Chris Mason5de08d72007-02-24 06:24:44 -05004872 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004873 *
Chris Masond352ac62008-09-29 15:18:18 -04004874 * the tree should have been previously balanced so the deletion does not
4875 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004876 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004877static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4878 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004879{
Chris Mason5f39d392007-10-15 16:14:19 -04004880 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004881 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004882 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004883
Chris Mason5f39d392007-10-15 16:14:19 -04004884 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004885 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004886 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004887 tree_mod_log_eb_move(root->fs_info, parent, slot,
4888 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004889 memmove_extent_buffer(parent,
4890 btrfs_node_key_ptr_offset(slot),
4891 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004892 sizeof(struct btrfs_key_ptr) *
4893 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004894 } else if (level) {
4895 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004896 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004897 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004898 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004899
Chris Mason7518a232007-03-12 12:01:18 -04004900 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004901 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004902 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004903 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004904 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004905 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004906 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004907 struct btrfs_disk_key disk_key;
4908
4909 btrfs_node_key(parent, &disk_key, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004910 fixup_low_keys(root->fs_info, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004911 }
Chris Masond6025572007-03-30 14:27:56 -04004912 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004913}
4914
Chris Mason74123bd2007-02-02 11:05:29 -05004915/*
Chris Mason323ac952008-10-01 19:05:46 -04004916 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004917 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004918 *
4919 * This deletes the pointer in path->nodes[1] and frees the leaf
4920 * block extent. zero is returned if it all worked out, < 0 otherwise.
4921 *
4922 * The path must have already been setup for deleting the leaf, including
4923 * all the proper balancing. path->nodes[1] must be locked.
4924 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004925static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4926 struct btrfs_root *root,
4927 struct btrfs_path *path,
4928 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004929{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004930 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004931 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004932
Chris Mason4d081c42009-02-04 09:31:28 -05004933 /*
4934 * btrfs_free_extent is expensive, we want to make sure we
4935 * aren't holding any locks when we call it
4936 */
4937 btrfs_unlock_up_safe(path, 0);
4938
Yan, Zhengf0486c62010-05-16 10:46:25 -04004939 root_sub_used(root, leaf->len);
4940
Josef Bacik3083ee22012-03-09 16:01:49 -05004941 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004942 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004943 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004944}
4945/*
Chris Mason74123bd2007-02-02 11:05:29 -05004946 * delete the item at the leaf level in path. If that empties
4947 * the leaf, remove it from the tree
4948 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004949int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4950 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004951{
Chris Mason5f39d392007-10-15 16:14:19 -04004952 struct extent_buffer *leaf;
4953 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004954 u32 last_off;
4955 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004956 int ret = 0;
4957 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004958 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004959 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004960 struct btrfs_map_token token;
4961
4962 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004963
Chris Mason5f39d392007-10-15 16:14:19 -04004964 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004965 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4966
4967 for (i = 0; i < nr; i++)
4968 dsize += btrfs_item_size_nr(leaf, slot + i);
4969
Chris Mason5f39d392007-10-15 16:14:19 -04004970 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004971
Chris Mason85e21ba2008-01-29 15:11:36 -05004972 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004973 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004974
4975 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004976 data_end + dsize,
4977 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004978 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004979
Chris Mason85e21ba2008-01-29 15:11:36 -05004980 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004981 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004982
Ross Kirkdd3cc162013-09-16 15:58:09 +01004983 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004984 ioff = btrfs_token_item_offset(leaf, item, &token);
4985 btrfs_set_token_item_offset(leaf, item,
4986 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004987 }
Chris Masondb945352007-10-15 16:15:53 -04004988
Chris Mason5f39d392007-10-15 16:14:19 -04004989 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004990 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004991 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004992 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004993 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004994 btrfs_set_header_nritems(leaf, nritems - nr);
4995 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004996
Chris Mason74123bd2007-02-02 11:05:29 -05004997 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004998 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004999 if (leaf == root->node) {
5000 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005001 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005002 btrfs_set_path_blocking(path);
Daniel Dressler01d58472014-11-21 17:15:07 +09005003 clean_tree_block(trans, root->fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005004 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005005 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005006 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005007 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005008 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005009 struct btrfs_disk_key disk_key;
5010
5011 btrfs_item_key(leaf, &disk_key, 0);
Daniel Dresslerb7a03652014-11-12 13:43:09 +09005012 fixup_low_keys(root->fs_info, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005013 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005014
Chris Mason74123bd2007-02-02 11:05:29 -05005015 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04005016 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005017 /* push_leaf_left fixes the path.
5018 * make sure the path still points to our leaf
5019 * for possible call to del_ptr below
5020 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005021 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005022 extent_buffer_get(leaf);
5023
Chris Masonb9473432009-03-13 11:00:37 -04005024 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005025 wret = push_leaf_left(trans, root, path, 1, 1,
5026 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005027 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005028 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005029
5030 if (path->nodes[0] == leaf &&
5031 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005032 wret = push_leaf_right(trans, root, path, 1,
5033 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005034 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005035 ret = wret;
5036 }
Chris Mason5f39d392007-10-15 16:14:19 -04005037
5038 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005039 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005040 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005041 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005042 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005043 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005044 /* if we're still in the path, make sure
5045 * we're dirty. Otherwise, one of the
5046 * push_leaf functions must have already
5047 * dirtied this buffer
5048 */
5049 if (path->nodes[0] == leaf)
5050 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005051 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005052 }
Chris Masond5719762007-03-23 10:01:08 -04005053 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005054 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005055 }
5056 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005057 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005058}
5059
Chris Mason97571fd2007-02-24 13:39:08 -05005060/*
Chris Mason925baed2008-06-25 16:01:30 -04005061 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005062 * returns 0 if it found something or 1 if there are no lesser leaves.
5063 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005064 *
5065 * This may release the path, and so you may lose any locks held at the
5066 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005067 */
Josef Bacik16e75492013-10-22 12:18:51 -04005068int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005069{
Chris Mason925baed2008-06-25 16:01:30 -04005070 struct btrfs_key key;
5071 struct btrfs_disk_key found_key;
5072 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005073
Chris Mason925baed2008-06-25 16:01:30 -04005074 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005075
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005076 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005077 key.offset--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005078 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005079 key.type--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005080 key.offset = (u64)-1;
5081 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005082 key.objectid--;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005083 key.type = (u8)-1;
5084 key.offset = (u64)-1;
5085 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005086 return 1;
Filipe David Borba Mananae8b0d722013-10-15 00:12:27 +01005087 }
Chris Mason7bb86312007-12-11 09:25:06 -05005088
David Sterbab3b4aa72011-04-21 01:20:15 +02005089 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005090 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5091 if (ret < 0)
5092 return ret;
5093 btrfs_item_key(path->nodes[0], &found_key, 0);
5094 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005095 /*
5096 * We might have had an item with the previous key in the tree right
5097 * before we released our path. And after we released our path, that
5098 * item might have been pushed to the first slot (0) of the leaf we
5099 * were holding due to a tree balance. Alternatively, an item with the
5100 * previous key can exist as the only element of a leaf (big fat item).
5101 * Therefore account for these 2 cases, so that our callers (like
5102 * btrfs_previous_item) don't miss an existing item with a key matching
5103 * the previous key we computed above.
5104 */
5105 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005106 return 0;
5107 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005108}
5109
Chris Mason3f157a22008-06-25 16:01:31 -04005110/*
5111 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005112 * for nodes or leaves that are have a minimum transaction id.
5113 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005114 *
5115 * This does not cow, but it does stuff the starting key it finds back
5116 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5117 * key and get a writable path.
5118 *
5119 * This does lock as it descends, and path->keep_locks should be set
5120 * to 1 by the caller.
5121 *
5122 * This honors path->lowest_level to prevent descent past a given level
5123 * of the tree.
5124 *
Chris Masond352ac62008-09-29 15:18:18 -04005125 * min_trans indicates the oldest transaction that you are interested
5126 * in walking through. Any nodes or leaves older than min_trans are
5127 * skipped over (without reading them).
5128 *
Chris Mason3f157a22008-06-25 16:01:31 -04005129 * returns zero if something useful was found, < 0 on error and 1 if there
5130 * was nothing in the tree that matched the search criteria.
5131 */
5132int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005133 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005134 u64 min_trans)
5135{
5136 struct extent_buffer *cur;
5137 struct btrfs_key found_key;
5138 int slot;
Yan96524802008-07-24 12:19:49 -04005139 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005140 u32 nritems;
5141 int level;
5142 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005143 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005144
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005145 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005146again:
Chris Masonbd681512011-07-16 15:23:14 -04005147 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005148 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005149 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005150 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005151 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005152
5153 if (btrfs_header_generation(cur) < min_trans) {
5154 ret = 1;
5155 goto out;
5156 }
Chris Masond3977122009-01-05 21:25:51 -05005157 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005158 nritems = btrfs_header_nritems(cur);
5159 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005160 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005161
Chris Mason323ac952008-10-01 19:05:46 -04005162 /* at the lowest level, we're done, setup the path and exit */
5163 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005164 if (slot >= nritems)
5165 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005166 ret = 0;
5167 path->slots[level] = slot;
5168 btrfs_item_key_to_cpu(cur, &found_key, slot);
5169 goto out;
5170 }
Yan96524802008-07-24 12:19:49 -04005171 if (sret && slot > 0)
5172 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005173 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005174 * check this node pointer against the min_trans parameters.
5175 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005176 */
Chris Masond3977122009-01-05 21:25:51 -05005177 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005178 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005179
Chris Mason3f157a22008-06-25 16:01:31 -04005180 gen = btrfs_node_ptr_generation(cur, slot);
5181 if (gen < min_trans) {
5182 slot++;
5183 continue;
5184 }
Eric Sandeende78b512013-01-31 18:21:12 +00005185 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005186 }
Chris Masone02119d2008-09-05 16:13:11 -04005187find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005188 /*
5189 * we didn't find a candidate key in this node, walk forward
5190 * and find another one
5191 */
5192 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005193 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005194 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005195 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005196 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005197 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005198 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005199 goto again;
5200 } else {
5201 goto out;
5202 }
5203 }
5204 /* save our key for returning back */
5205 btrfs_node_key_to_cpu(cur, &found_key, slot);
5206 path->slots[level] = slot;
5207 if (level == path->lowest_level) {
5208 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005209 goto out;
5210 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005211 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005212 cur = read_node_slot(root, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005213 if (IS_ERR(cur)) {
5214 ret = PTR_ERR(cur);
5215 goto out;
5216 }
Chris Mason3f157a22008-06-25 16:01:31 -04005217
Chris Masonbd681512011-07-16 15:23:14 -04005218 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005219
Chris Masonbd681512011-07-16 15:23:14 -04005220 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005221 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005222 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005223 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005224 }
5225out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005226 path->keep_locks = keep_locks;
5227 if (ret == 0) {
5228 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5229 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005230 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005231 }
Chris Mason3f157a22008-06-25 16:01:31 -04005232 return ret;
5233}
5234
Liu Bofb770ae2016-07-05 12:10:14 -07005235static int tree_move_down(struct btrfs_root *root,
Alexander Block70698302012-06-05 21:07:48 +02005236 struct btrfs_path *path,
5237 int *level, int root_level)
5238{
Liu Bofb770ae2016-07-05 12:10:14 -07005239 struct extent_buffer *eb;
5240
Chris Mason74dd17f2012-08-07 16:25:13 -04005241 BUG_ON(*level == 0);
Liu Bofb770ae2016-07-05 12:10:14 -07005242 eb = read_node_slot(root, path->nodes[*level], path->slots[*level]);
5243 if (IS_ERR(eb))
5244 return PTR_ERR(eb);
5245
5246 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005247 path->slots[*level - 1] = 0;
5248 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005249 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005250}
5251
5252static int tree_move_next_or_upnext(struct btrfs_root *root,
5253 struct btrfs_path *path,
5254 int *level, int root_level)
5255{
5256 int ret = 0;
5257 int nritems;
5258 nritems = btrfs_header_nritems(path->nodes[*level]);
5259
5260 path->slots[*level]++;
5261
Chris Mason74dd17f2012-08-07 16:25:13 -04005262 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005263 if (*level == root_level)
5264 return -1;
5265
5266 /* move upnext */
5267 path->slots[*level] = 0;
5268 free_extent_buffer(path->nodes[*level]);
5269 path->nodes[*level] = NULL;
5270 (*level)++;
5271 path->slots[*level]++;
5272
5273 nritems = btrfs_header_nritems(path->nodes[*level]);
5274 ret = 1;
5275 }
5276 return ret;
5277}
5278
5279/*
5280 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5281 * or down.
5282 */
5283static int tree_advance(struct btrfs_root *root,
5284 struct btrfs_path *path,
5285 int *level, int root_level,
5286 int allow_down,
5287 struct btrfs_key *key)
5288{
5289 int ret;
5290
5291 if (*level == 0 || !allow_down) {
5292 ret = tree_move_next_or_upnext(root, path, level, root_level);
5293 } else {
Liu Bofb770ae2016-07-05 12:10:14 -07005294 ret = tree_move_down(root, path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005295 }
5296 if (ret >= 0) {
5297 if (*level == 0)
5298 btrfs_item_key_to_cpu(path->nodes[*level], key,
5299 path->slots[*level]);
5300 else
5301 btrfs_node_key_to_cpu(path->nodes[*level], key,
5302 path->slots[*level]);
5303 }
5304 return ret;
5305}
5306
5307static int tree_compare_item(struct btrfs_root *left_root,
5308 struct btrfs_path *left_path,
5309 struct btrfs_path *right_path,
5310 char *tmp_buf)
5311{
5312 int cmp;
5313 int len1, len2;
5314 unsigned long off1, off2;
5315
5316 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5317 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5318 if (len1 != len2)
5319 return 1;
5320
5321 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5322 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5323 right_path->slots[0]);
5324
5325 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5326
5327 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5328 if (cmp)
5329 return 1;
5330 return 0;
5331}
5332
5333#define ADVANCE 1
5334#define ADVANCE_ONLY_NEXT -1
5335
5336/*
5337 * This function compares two trees and calls the provided callback for
5338 * every changed/new/deleted item it finds.
5339 * If shared tree blocks are encountered, whole subtrees are skipped, making
5340 * the compare pretty fast on snapshotted subvolumes.
5341 *
5342 * This currently works on commit roots only. As commit roots are read only,
5343 * we don't do any locking. The commit roots are protected with transactions.
5344 * Transactions are ended and rejoined when a commit is tried in between.
5345 *
5346 * This function checks for modifications done to the trees while comparing.
5347 * If it detects a change, it aborts immediately.
5348 */
5349int btrfs_compare_trees(struct btrfs_root *left_root,
5350 struct btrfs_root *right_root,
5351 btrfs_changed_cb_t changed_cb, void *ctx)
5352{
5353 int ret;
5354 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005355 struct btrfs_path *left_path = NULL;
5356 struct btrfs_path *right_path = NULL;
5357 struct btrfs_key left_key;
5358 struct btrfs_key right_key;
5359 char *tmp_buf = NULL;
5360 int left_root_level;
5361 int right_root_level;
5362 int left_level;
5363 int right_level;
5364 int left_end_reached;
5365 int right_end_reached;
5366 int advance_left;
5367 int advance_right;
5368 u64 left_blockptr;
5369 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005370 u64 left_gen;
5371 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005372
5373 left_path = btrfs_alloc_path();
5374 if (!left_path) {
5375 ret = -ENOMEM;
5376 goto out;
5377 }
5378 right_path = btrfs_alloc_path();
5379 if (!right_path) {
5380 ret = -ENOMEM;
5381 goto out;
5382 }
5383
David Sterba8f282f72016-03-30 16:01:12 +02005384 tmp_buf = kmalloc(left_root->nodesize, GFP_KERNEL | __GFP_NOWARN);
Alexander Block70698302012-06-05 21:07:48 +02005385 if (!tmp_buf) {
David Sterba8f282f72016-03-30 16:01:12 +02005386 tmp_buf = vmalloc(left_root->nodesize);
5387 if (!tmp_buf) {
5388 ret = -ENOMEM;
5389 goto out;
5390 }
Alexander Block70698302012-06-05 21:07:48 +02005391 }
5392
5393 left_path->search_commit_root = 1;
5394 left_path->skip_locking = 1;
5395 right_path->search_commit_root = 1;
5396 right_path->skip_locking = 1;
5397
Alexander Block70698302012-06-05 21:07:48 +02005398 /*
5399 * Strategy: Go to the first items of both trees. Then do
5400 *
5401 * If both trees are at level 0
5402 * Compare keys of current items
5403 * If left < right treat left item as new, advance left tree
5404 * and repeat
5405 * If left > right treat right item as deleted, advance right tree
5406 * and repeat
5407 * If left == right do deep compare of items, treat as changed if
5408 * needed, advance both trees and repeat
5409 * If both trees are at the same level but not at level 0
5410 * Compare keys of current nodes/leafs
5411 * If left < right advance left tree and repeat
5412 * If left > right advance right tree and repeat
5413 * If left == right compare blockptrs of the next nodes/leafs
5414 * If they match advance both trees but stay at the same level
5415 * and repeat
5416 * If they don't match advance both trees while allowing to go
5417 * deeper and repeat
5418 * If tree levels are different
5419 * Advance the tree that needs it and repeat
5420 *
5421 * Advancing a tree means:
5422 * If we are at level 0, try to go to the next slot. If that's not
5423 * possible, go one level up and repeat. Stop when we found a level
5424 * where we could go to the next slot. We may at this point be on a
5425 * node or a leaf.
5426 *
5427 * If we are not at level 0 and not on shared tree blocks, go one
5428 * level deeper.
5429 *
5430 * If we are not at level 0 and on shared tree blocks, go one slot to
5431 * the right if possible or go up and right.
5432 */
5433
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005434 down_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005435 left_level = btrfs_header_level(left_root->commit_root);
5436 left_root_level = left_level;
5437 left_path->nodes[left_level] = left_root->commit_root;
5438 extent_buffer_get(left_path->nodes[left_level]);
5439
5440 right_level = btrfs_header_level(right_root->commit_root);
5441 right_root_level = right_level;
5442 right_path->nodes[right_level] = right_root->commit_root;
5443 extent_buffer_get(right_path->nodes[right_level]);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04005444 up_read(&left_root->fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005445
5446 if (left_level == 0)
5447 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5448 &left_key, left_path->slots[left_level]);
5449 else
5450 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5451 &left_key, left_path->slots[left_level]);
5452 if (right_level == 0)
5453 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5454 &right_key, right_path->slots[right_level]);
5455 else
5456 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5457 &right_key, right_path->slots[right_level]);
5458
5459 left_end_reached = right_end_reached = 0;
5460 advance_left = advance_right = 0;
5461
5462 while (1) {
Nikolay Borisov13f99142019-09-04 19:33:58 +03005463 cond_resched();
Alexander Block70698302012-06-05 21:07:48 +02005464 if (advance_left && !left_end_reached) {
5465 ret = tree_advance(left_root, left_path, &left_level,
5466 left_root_level,
5467 advance_left != ADVANCE_ONLY_NEXT,
5468 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005469 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005470 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005471 else if (ret < 0)
5472 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005473 advance_left = 0;
5474 }
5475 if (advance_right && !right_end_reached) {
5476 ret = tree_advance(right_root, right_path, &right_level,
5477 right_root_level,
5478 advance_right != ADVANCE_ONLY_NEXT,
5479 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005480 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005481 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005482 else if (ret < 0)
5483 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005484 advance_right = 0;
5485 }
5486
5487 if (left_end_reached && right_end_reached) {
5488 ret = 0;
5489 goto out;
5490 } else if (left_end_reached) {
5491 if (right_level == 0) {
5492 ret = changed_cb(left_root, right_root,
5493 left_path, right_path,
5494 &right_key,
5495 BTRFS_COMPARE_TREE_DELETED,
5496 ctx);
5497 if (ret < 0)
5498 goto out;
5499 }
5500 advance_right = ADVANCE;
5501 continue;
5502 } else if (right_end_reached) {
5503 if (left_level == 0) {
5504 ret = changed_cb(left_root, right_root,
5505 left_path, right_path,
5506 &left_key,
5507 BTRFS_COMPARE_TREE_NEW,
5508 ctx);
5509 if (ret < 0)
5510 goto out;
5511 }
5512 advance_left = ADVANCE;
5513 continue;
5514 }
5515
5516 if (left_level == 0 && right_level == 0) {
5517 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5518 if (cmp < 0) {
5519 ret = changed_cb(left_root, right_root,
5520 left_path, right_path,
5521 &left_key,
5522 BTRFS_COMPARE_TREE_NEW,
5523 ctx);
5524 if (ret < 0)
5525 goto out;
5526 advance_left = ADVANCE;
5527 } else if (cmp > 0) {
5528 ret = changed_cb(left_root, right_root,
5529 left_path, right_path,
5530 &right_key,
5531 BTRFS_COMPARE_TREE_DELETED,
5532 ctx);
5533 if (ret < 0)
5534 goto out;
5535 advance_right = ADVANCE;
5536 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005537 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005538
Chris Mason74dd17f2012-08-07 16:25:13 -04005539 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005540 ret = tree_compare_item(left_root, left_path,
5541 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005542 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005543 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005544 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005545 result = BTRFS_COMPARE_TREE_SAME;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005546 ret = changed_cb(left_root, right_root,
5547 left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005548 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005549 if (ret < 0)
5550 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005551 advance_left = ADVANCE;
5552 advance_right = ADVANCE;
5553 }
5554 } else if (left_level == right_level) {
5555 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5556 if (cmp < 0) {
5557 advance_left = ADVANCE;
5558 } else if (cmp > 0) {
5559 advance_right = ADVANCE;
5560 } else {
5561 left_blockptr = btrfs_node_blockptr(
5562 left_path->nodes[left_level],
5563 left_path->slots[left_level]);
5564 right_blockptr = btrfs_node_blockptr(
5565 right_path->nodes[right_level],
5566 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005567 left_gen = btrfs_node_ptr_generation(
5568 left_path->nodes[left_level],
5569 left_path->slots[left_level]);
5570 right_gen = btrfs_node_ptr_generation(
5571 right_path->nodes[right_level],
5572 right_path->slots[right_level]);
5573 if (left_blockptr == right_blockptr &&
5574 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005575 /*
5576 * As we're on a shared block, don't
5577 * allow to go deeper.
5578 */
5579 advance_left = ADVANCE_ONLY_NEXT;
5580 advance_right = ADVANCE_ONLY_NEXT;
5581 } else {
5582 advance_left = ADVANCE;
5583 advance_right = ADVANCE;
5584 }
5585 }
5586 } else if (left_level < right_level) {
5587 advance_right = ADVANCE;
5588 } else {
5589 advance_left = ADVANCE;
5590 }
5591 }
5592
5593out:
5594 btrfs_free_path(left_path);
5595 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005596 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005597 return ret;
5598}
5599
Chris Mason3f157a22008-06-25 16:01:31 -04005600/*
5601 * this is similar to btrfs_next_leaf, but does not try to preserve
5602 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005603 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005604 *
5605 * 0 is returned if another key is found, < 0 if there are any errors
5606 * and 1 is returned if there are no higher keys in the tree
5607 *
5608 * path->keep_locks should be set to 1 on the search made before
5609 * calling this function.
5610 */
Chris Masone7a84562008-06-25 16:01:31 -04005611int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005612 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005613{
Chris Masone7a84562008-06-25 16:01:31 -04005614 int slot;
5615 struct extent_buffer *c;
5616
Chris Mason934d3752008-12-08 16:43:10 -05005617 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005618 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005619 if (!path->nodes[level])
5620 return 1;
5621
5622 slot = path->slots[level] + 1;
5623 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005624next:
Chris Masone7a84562008-06-25 16:01:31 -04005625 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005626 int ret;
5627 int orig_lowest;
5628 struct btrfs_key cur_key;
5629 if (level + 1 >= BTRFS_MAX_LEVEL ||
5630 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005631 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005632
5633 if (path->locks[level + 1]) {
5634 level++;
5635 continue;
5636 }
5637
5638 slot = btrfs_header_nritems(c) - 1;
5639 if (level == 0)
5640 btrfs_item_key_to_cpu(c, &cur_key, slot);
5641 else
5642 btrfs_node_key_to_cpu(c, &cur_key, slot);
5643
5644 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005645 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005646 path->lowest_level = level;
5647 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5648 0, 0);
5649 path->lowest_level = orig_lowest;
5650 if (ret < 0)
5651 return ret;
5652
5653 c = path->nodes[level];
5654 slot = path->slots[level];
5655 if (ret == 0)
5656 slot++;
5657 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005658 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005659
Chris Masone7a84562008-06-25 16:01:31 -04005660 if (level == 0)
5661 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005662 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005663 u64 gen = btrfs_node_ptr_generation(c, slot);
5664
Chris Mason3f157a22008-06-25 16:01:31 -04005665 if (gen < min_trans) {
5666 slot++;
5667 goto next;
5668 }
Chris Masone7a84562008-06-25 16:01:31 -04005669 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005670 }
Chris Masone7a84562008-06-25 16:01:31 -04005671 return 0;
5672 }
5673 return 1;
5674}
5675
Chris Mason7bb86312007-12-11 09:25:06 -05005676/*
Chris Mason925baed2008-06-25 16:01:30 -04005677 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005678 * returns 0 if it found something or 1 if there are no greater leaves.
5679 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005680 */
Chris Mason234b63a2007-03-13 10:46:10 -04005681int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005682{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005683 return btrfs_next_old_leaf(root, path, 0);
5684}
5685
5686int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5687 u64 time_seq)
5688{
Chris Masond97e63b2007-02-20 16:40:44 -05005689 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005690 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005691 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005692 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005693 struct btrfs_key key;
5694 u32 nritems;
5695 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005696 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005697 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005698
5699 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005700 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005701 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005702
Chris Mason8e73f272009-04-03 10:14:18 -04005703 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5704again:
5705 level = 1;
5706 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005707 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005708 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005709
Chris Masona2135012008-06-25 16:01:30 -04005710 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005711 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005712
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005713 if (time_seq)
5714 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5715 else
5716 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005717 path->keep_locks = 0;
5718
5719 if (ret < 0)
5720 return ret;
5721
Chris Masona2135012008-06-25 16:01:30 -04005722 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005723 /*
5724 * by releasing the path above we dropped all our locks. A balance
5725 * could have added more items next to the key that used to be
5726 * at the very end of the block. So, check again here and
5727 * advance the path if there are now more items available.
5728 */
Chris Masona2135012008-06-25 16:01:30 -04005729 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005730 if (ret == 0)
5731 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005732 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005733 goto done;
5734 }
Liu Bo0b43e042014-06-09 11:04:49 +08005735 /*
5736 * So the above check misses one case:
5737 * - after releasing the path above, someone has removed the item that
5738 * used to be at the very end of the block, and balance between leafs
5739 * gets another one with bigger key.offset to replace it.
5740 *
5741 * This one should be returned as well, or we can get leaf corruption
5742 * later(esp. in __btrfs_drop_extents()).
5743 *
5744 * And a bit more explanation about this check,
5745 * with ret > 0, the key isn't found, the path points to the slot
5746 * where it should be inserted, so the path->slots[0] item must be the
5747 * bigger one.
5748 */
5749 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5750 ret = 0;
5751 goto done;
5752 }
Chris Masond97e63b2007-02-20 16:40:44 -05005753
Chris Masond3977122009-01-05 21:25:51 -05005754 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005755 if (!path->nodes[level]) {
5756 ret = 1;
5757 goto done;
5758 }
Chris Mason5f39d392007-10-15 16:14:19 -04005759
Chris Masond97e63b2007-02-20 16:40:44 -05005760 slot = path->slots[level] + 1;
5761 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005762 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005763 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005764 if (level == BTRFS_MAX_LEVEL) {
5765 ret = 1;
5766 goto done;
5767 }
Chris Masond97e63b2007-02-20 16:40:44 -05005768 continue;
5769 }
Chris Mason5f39d392007-10-15 16:14:19 -04005770
Chris Mason925baed2008-06-25 16:01:30 -04005771 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005772 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005773 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005774 }
Chris Mason5f39d392007-10-15 16:14:19 -04005775
Chris Mason8e73f272009-04-03 10:14:18 -04005776 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005777 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005778 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005779 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005780 if (ret == -EAGAIN)
5781 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005782
Chris Mason76a05b32009-05-14 13:24:30 -04005783 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005784 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005785 goto done;
5786 }
5787
Chris Mason5cd57b22008-06-25 16:01:30 -04005788 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005789 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005790 if (!ret && time_seq) {
5791 /*
5792 * If we don't get the lock, we may be racing
5793 * with push_leaf_left, holding that lock while
5794 * itself waiting for the leaf we've currently
5795 * locked. To solve this situation, we give up
5796 * on our lock and cycle.
5797 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005798 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005799 btrfs_release_path(path);
5800 cond_resched();
5801 goto again;
5802 }
Chris Mason8e73f272009-04-03 10:14:18 -04005803 if (!ret) {
5804 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005805 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005806 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005807 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005808 }
Chris Mason31533fb2011-07-26 16:01:59 -04005809 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005810 }
Chris Masond97e63b2007-02-20 16:40:44 -05005811 break;
5812 }
5813 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005814 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005815 level--;
5816 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005817 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005818 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005819
Chris Mason5f39d392007-10-15 16:14:19 -04005820 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005821 path->nodes[level] = next;
5822 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005823 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005824 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005825 if (!level)
5826 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005827
Chris Mason8e73f272009-04-03 10:14:18 -04005828 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005829 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005830 if (ret == -EAGAIN)
5831 goto again;
5832
Chris Mason76a05b32009-05-14 13:24:30 -04005833 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005834 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005835 goto done;
5836 }
5837
Chris Mason5cd57b22008-06-25 16:01:30 -04005838 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005839 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005840 if (!ret) {
5841 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005842 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005843 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005844 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005845 }
Chris Mason31533fb2011-07-26 16:01:59 -04005846 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005847 }
Chris Masond97e63b2007-02-20 16:40:44 -05005848 }
Chris Mason8e73f272009-04-03 10:14:18 -04005849 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005850done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005851 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005852 path->leave_spinning = old_spinning;
5853 if (!old_spinning)
5854 btrfs_set_path_blocking(path);
5855
5856 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005857}
Chris Mason0b86a832008-03-24 15:01:56 -04005858
Chris Mason3f157a22008-06-25 16:01:31 -04005859/*
5860 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5861 * searching until it gets past min_objectid or finds an item of 'type'
5862 *
5863 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5864 */
Chris Mason0b86a832008-03-24 15:01:56 -04005865int btrfs_previous_item(struct btrfs_root *root,
5866 struct btrfs_path *path, u64 min_objectid,
5867 int type)
5868{
5869 struct btrfs_key found_key;
5870 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005871 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005872 int ret;
5873
Chris Masond3977122009-01-05 21:25:51 -05005874 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005875 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005876 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005877 ret = btrfs_prev_leaf(root, path);
5878 if (ret != 0)
5879 return ret;
5880 } else {
5881 path->slots[0]--;
5882 }
5883 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005884 nritems = btrfs_header_nritems(leaf);
5885 if (nritems == 0)
5886 return 1;
5887 if (path->slots[0] == nritems)
5888 path->slots[0]--;
5889
Chris Mason0b86a832008-03-24 15:01:56 -04005890 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005891 if (found_key.objectid < min_objectid)
5892 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005893 if (found_key.type == type)
5894 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005895 if (found_key.objectid == min_objectid &&
5896 found_key.type < type)
5897 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005898 }
5899 return 1;
5900}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005901
5902/*
5903 * search in extent tree to find a previous Metadata/Data extent item with
5904 * min objecitd.
5905 *
5906 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5907 */
5908int btrfs_previous_extent_item(struct btrfs_root *root,
5909 struct btrfs_path *path, u64 min_objectid)
5910{
5911 struct btrfs_key found_key;
5912 struct extent_buffer *leaf;
5913 u32 nritems;
5914 int ret;
5915
5916 while (1) {
5917 if (path->slots[0] == 0) {
5918 btrfs_set_path_blocking(path);
5919 ret = btrfs_prev_leaf(root, path);
5920 if (ret != 0)
5921 return ret;
5922 } else {
5923 path->slots[0]--;
5924 }
5925 leaf = path->nodes[0];
5926 nritems = btrfs_header_nritems(leaf);
5927 if (nritems == 0)
5928 return 1;
5929 if (path->slots[0] == nritems)
5930 path->slots[0]--;
5931
5932 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5933 if (found_key.objectid < min_objectid)
5934 break;
5935 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5936 found_key.type == BTRFS_METADATA_ITEM_KEY)
5937 return 0;
5938 if (found_key.objectid == min_objectid &&
5939 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5940 break;
5941 }
5942 return 1;
5943}