blob: b29c8d82e7414b654f412496fcbaa29f8e814271 [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{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400215 struct btrfs_fs_info *fs_info = root->fs_info;
216
Josef Bacike7070be2014-12-16 08:54:43 -0800217 if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
218 !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
219 return;
220
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400221 spin_lock(&fs_info->trans_lock);
Josef Bacike7070be2014-12-16 08:54:43 -0800222 if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
223 /* Want the extent tree to be the last on the list */
224 if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
225 list_move_tail(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400226 &fs_info->dirty_cowonly_roots);
Josef Bacike7070be2014-12-16 08:54:43 -0800227 else
228 list_move(&root->dirty_list,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400229 &fs_info->dirty_cowonly_roots);
Chris Mason0b86a832008-03-24 15:01:56 -0400230 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400231 spin_unlock(&fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400232}
233
Chris Masond352ac62008-09-29 15:18:18 -0400234/*
235 * used by snapshot creation to make a copy of a root for a tree with
236 * a given objectid. The buffer with the new root node is returned in
237 * cow_ret, and this func returns zero on success or a negative error code.
238 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500239int btrfs_copy_root(struct btrfs_trans_handle *trans,
240 struct btrfs_root *root,
241 struct extent_buffer *buf,
242 struct extent_buffer **cow_ret, u64 new_root_objectid)
243{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400244 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 int ret = 0;
247 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249
Miao Xie27cdeb72014-04-02 19:51:05 +0800250 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400251 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +0800252 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
253 trans->transid != root->last_trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500254
255 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400256 if (level == 0)
257 btrfs_item_key(buf, &disk_key, 0);
258 else
259 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400260
David Sterba4d75f8a2014-06-15 01:54:12 +0200261 cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
262 &disk_key, level, buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 return PTR_ERR(cow);
265
David Sterba58e80122016-11-08 18:30:31 +0100266 copy_extent_buffer_full(cow, buf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500267 btrfs_set_header_bytenr(cow, cow->start);
268 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
270 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
271 BTRFS_HEADER_FLAG_RELOC);
272 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
273 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
274 else
275 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500276
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400277 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -0500278
Chris Masonbe20aa92007-12-17 20:14:01 -0500279 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400280 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -0700281 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 else
Josef Bacike339a6b2014-07-02 10:54:25 -0700283 ret = btrfs_inc_ref(trans, root, cow, 0);
Chris Mason4aec2b52007-12-18 16:25:45 -0500284
Chris Masonbe20aa92007-12-17 20:14:01 -0500285 if (ret)
286 return ret;
287
288 btrfs_mark_buffer_dirty(cow);
289 *cow_ret = cow;
290 return 0;
291}
292
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200293enum mod_log_op {
294 MOD_LOG_KEY_REPLACE,
295 MOD_LOG_KEY_ADD,
296 MOD_LOG_KEY_REMOVE,
297 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
298 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
299 MOD_LOG_MOVE_KEYS,
300 MOD_LOG_ROOT_REPLACE,
301};
302
303struct tree_mod_move {
304 int dst_slot;
305 int nr_items;
306};
307
308struct tree_mod_root {
309 u64 logical;
310 u8 level;
311};
312
313struct tree_mod_elem {
314 struct rb_node node;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530315 u64 logical;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200316 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200317 enum mod_log_op op;
318
319 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
320 int slot;
321
322 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
323 u64 generation;
324
325 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
326 struct btrfs_disk_key key;
327 u64 blockptr;
328
329 /* this is used for op == MOD_LOG_MOVE_KEYS */
330 struct tree_mod_move move;
331
332 /* this is used for op == MOD_LOG_ROOT_REPLACE */
333 struct tree_mod_root old_root;
334};
335
Jan Schmidt097b8a72012-06-21 11:08:04 +0200336static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200337{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200339}
340
Jan Schmidt097b8a72012-06-21 11:08:04 +0200341static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200342{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343 read_unlock(&fs_info->tree_mod_log_lock);
344}
345
346static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
347{
348 write_lock(&fs_info->tree_mod_log_lock);
349}
350
351static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
352{
353 write_unlock(&fs_info->tree_mod_log_lock);
354}
355
356/*
Josef Bacikfcebe452014-05-13 17:30:47 -0700357 * Pull a new tree mod seq number for our operation.
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000358 */
Josef Bacikfcebe452014-05-13 17:30:47 -0700359static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000360{
361 return atomic64_inc_return(&fs_info->tree_mod_seq);
362}
363
364/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200365 * This adds a new blocker to the tree mod log's blocker list if the @elem
366 * passed does not already have a sequence number set. So when a caller expects
367 * to record tree modifications, it should ensure to set elem->seq to zero
368 * before calling btrfs_get_tree_mod_seq.
369 * Returns a fresh, unused tree log modification sequence number, even if no new
370 * blocker was added.
371 */
372u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
373 struct seq_list *elem)
374{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200375 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200376 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200377 if (!elem->seq) {
Josef Bacikfcebe452014-05-13 17:30:47 -0700378 elem->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200379 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
380 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200381 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200382 tree_mod_log_write_unlock(fs_info);
383
Josef Bacikfcebe452014-05-13 17:30:47 -0700384 return elem->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200385}
386
387void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
388 struct seq_list *elem)
389{
390 struct rb_root *tm_root;
391 struct rb_node *node;
392 struct rb_node *next;
393 struct seq_list *cur_elem;
394 struct tree_mod_elem *tm;
395 u64 min_seq = (u64)-1;
396 u64 seq_putting = elem->seq;
397
398 if (!seq_putting)
399 return;
400
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200401 spin_lock(&fs_info->tree_mod_seq_lock);
402 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200403 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200404
405 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200406 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200407 if (seq_putting > cur_elem->seq) {
408 /*
409 * blocker with lower sequence number exists, we
410 * cannot remove anything from the log
411 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200412 spin_unlock(&fs_info->tree_mod_seq_lock);
413 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200414 }
415 min_seq = cur_elem->seq;
416 }
417 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200418 spin_unlock(&fs_info->tree_mod_seq_lock);
419
420 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200421 * anything that's lower than the lowest existing (read: blocked)
422 * sequence number can be removed from the tree.
423 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200424 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200425 tm_root = &fs_info->tree_mod_log;
426 for (node = rb_first(tm_root); node; node = next) {
427 next = rb_next(node);
428 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200429 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200430 continue;
431 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200432 kfree(tm);
433 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200434 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200435}
436
437/*
438 * key order of the log:
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530439 * node/leaf start address -> sequence
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200440 *
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530441 * The 'start address' is the logical address of the *new* root node
442 * for root replace operations, or the logical address of the affected
443 * block for all other operations.
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000444 *
445 * Note: must be called with write lock (tree_mod_log_write_lock).
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200446 */
447static noinline int
448__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
449{
450 struct rb_root *tm_root;
451 struct rb_node **new;
452 struct rb_node *parent = NULL;
453 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200454
Josef Bacikc8cc6342013-07-01 16:18:19 -0400455 BUG_ON(!tm);
456
Josef Bacikfcebe452014-05-13 17:30:47 -0700457 tm->seq = btrfs_inc_tree_mod_seq(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200458
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200459 tm_root = &fs_info->tree_mod_log;
460 new = &tm_root->rb_node;
461 while (*new) {
462 cur = container_of(*new, struct tree_mod_elem, node);
463 parent = *new;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530464 if (cur->logical < tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 new = &((*new)->rb_left);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530466 else if (cur->logical > tm->logical)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200467 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200468 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200469 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200470 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200471 new = &((*new)->rb_right);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000472 else
473 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200474 }
475
476 rb_link_node(&tm->node, parent, new);
477 rb_insert_color(&tm->node, tm_root);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000478 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200479}
480
Jan Schmidt097b8a72012-06-21 11:08:04 +0200481/*
482 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
483 * returns zero with the tree_mod_log_lock acquired. The caller must hold
484 * this until all tree mod log insertions are recorded in the rb tree and then
485 * call tree_mod_log_write_unlock() to release.
486 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200487static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
488 struct extent_buffer *eb) {
489 smp_mb();
490 if (list_empty(&(fs_info)->tree_mod_seq_list))
491 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200492 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200493 return 1;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000494
495 tree_mod_log_write_lock(fs_info);
496 if (list_empty(&(fs_info)->tree_mod_seq_list)) {
497 tree_mod_log_write_unlock(fs_info);
498 return 1;
499 }
500
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200501 return 0;
502}
503
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000504/* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
505static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
506 struct extent_buffer *eb)
507{
508 smp_mb();
509 if (list_empty(&(fs_info)->tree_mod_seq_list))
510 return 0;
511 if (eb && btrfs_header_level(eb) == 0)
512 return 0;
513
514 return 1;
515}
516
517static struct tree_mod_elem *
518alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
519 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200520{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200521 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200522
Josef Bacikc8cc6342013-07-01 16:18:19 -0400523 tm = kzalloc(sizeof(*tm), flags);
524 if (!tm)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000525 return NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200526
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530527 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200528 if (op != MOD_LOG_KEY_ADD) {
529 btrfs_node_key(eb, &tm->key, slot);
530 tm->blockptr = btrfs_node_blockptr(eb, slot);
531 }
532 tm->op = op;
533 tm->slot = slot;
534 tm->generation = btrfs_node_ptr_generation(eb, slot);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000535 RB_CLEAR_NODE(&tm->node);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200536
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000537 return tm;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200538}
539
540static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400541tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
542 struct extent_buffer *eb, int slot,
543 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200544{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000545 struct tree_mod_elem *tm;
546 int ret;
547
548 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200549 return 0;
550
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000551 tm = alloc_tree_mod_elem(eb, slot, op, flags);
552 if (!tm)
553 return -ENOMEM;
554
555 if (tree_mod_dont_log(fs_info, eb)) {
556 kfree(tm);
557 return 0;
558 }
559
560 ret = __tree_mod_log_insert(fs_info, tm);
561 tree_mod_log_write_unlock(fs_info);
562 if (ret)
563 kfree(tm);
564
565 return ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200566}
567
568static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200569tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
570 struct extent_buffer *eb, int dst_slot, int src_slot,
571 int nr_items, gfp_t flags)
572{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000573 struct tree_mod_elem *tm = NULL;
574 struct tree_mod_elem **tm_list = NULL;
575 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200576 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000577 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200578
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000579 if (!tree_mod_need_log(fs_info, eb))
Jan Schmidtf3956942012-05-31 15:02:32 +0200580 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200581
David Sterba31e818f2015-02-20 18:00:26 +0100582 tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000583 if (!tm_list)
584 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200585
Josef Bacikc8cc6342013-07-01 16:18:19 -0400586 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000587 if (!tm) {
588 ret = -ENOMEM;
589 goto free_tms;
590 }
Jan Schmidtf3956942012-05-31 15:02:32 +0200591
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530592 tm->logical = eb->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200593 tm->slot = src_slot;
594 tm->move.dst_slot = dst_slot;
595 tm->move.nr_items = nr_items;
596 tm->op = MOD_LOG_MOVE_KEYS;
597
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000598 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
599 tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
600 MOD_LOG_KEY_REMOVE_WHILE_MOVING, flags);
601 if (!tm_list[i]) {
602 ret = -ENOMEM;
603 goto free_tms;
604 }
605 }
606
607 if (tree_mod_dont_log(fs_info, eb))
608 goto free_tms;
609 locked = 1;
610
611 /*
612 * When we override something during the move, we log these removals.
613 * This can only happen when we move towards the beginning of the
614 * buffer, i.e. dst_slot < src_slot.
615 */
616 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
617 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
618 if (ret)
619 goto free_tms;
620 }
621
622 ret = __tree_mod_log_insert(fs_info, tm);
623 if (ret)
624 goto free_tms;
625 tree_mod_log_write_unlock(fs_info);
626 kfree(tm_list);
627
628 return 0;
629free_tms:
630 for (i = 0; i < nr_items; i++) {
631 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
632 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
633 kfree(tm_list[i]);
634 }
635 if (locked)
636 tree_mod_log_write_unlock(fs_info);
637 kfree(tm_list);
638 kfree(tm);
639
640 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200641}
642
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000643static inline int
644__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
645 struct tree_mod_elem **tm_list,
646 int nritems)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200647{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000648 int i, j;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200649 int ret;
650
Jan Schmidt097b8a72012-06-21 11:08:04 +0200651 for (i = nritems - 1; i >= 0; i--) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000652 ret = __tree_mod_log_insert(fs_info, tm_list[i]);
653 if (ret) {
654 for (j = nritems - 1; j > i; j--)
655 rb_erase(&tm_list[j]->node,
656 &fs_info->tree_mod_log);
657 return ret;
658 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200659 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000660
661 return 0;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200662}
663
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200664static noinline int
665tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
666 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000667 struct extent_buffer *new_root, gfp_t flags,
668 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200669{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000670 struct tree_mod_elem *tm = NULL;
671 struct tree_mod_elem **tm_list = NULL;
672 int nritems = 0;
673 int ret = 0;
674 int i;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200675
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000676 if (!tree_mod_need_log(fs_info, NULL))
Jan Schmidt097b8a72012-06-21 11:08:04 +0200677 return 0;
678
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000679 if (log_removal && btrfs_header_level(old_root) > 0) {
680 nritems = btrfs_header_nritems(old_root);
David Sterba31e818f2015-02-20 18:00:26 +0100681 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000682 flags);
683 if (!tm_list) {
684 ret = -ENOMEM;
685 goto free_tms;
686 }
687 for (i = 0; i < nritems; i++) {
688 tm_list[i] = alloc_tree_mod_elem(old_root, i,
689 MOD_LOG_KEY_REMOVE_WHILE_FREEING, flags);
690 if (!tm_list[i]) {
691 ret = -ENOMEM;
692 goto free_tms;
693 }
694 }
695 }
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000696
Josef Bacikc8cc6342013-07-01 16:18:19 -0400697 tm = kzalloc(sizeof(*tm), flags);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000698 if (!tm) {
699 ret = -ENOMEM;
700 goto free_tms;
701 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200702
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530703 tm->logical = new_root->start;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200704 tm->old_root.logical = old_root->start;
705 tm->old_root.level = btrfs_header_level(old_root);
706 tm->generation = btrfs_header_generation(old_root);
707 tm->op = MOD_LOG_ROOT_REPLACE;
708
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000709 if (tree_mod_dont_log(fs_info, NULL))
710 goto free_tms;
711
712 if (tm_list)
713 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
714 if (!ret)
715 ret = __tree_mod_log_insert(fs_info, tm);
716
717 tree_mod_log_write_unlock(fs_info);
718 if (ret)
719 goto free_tms;
720 kfree(tm_list);
721
722 return ret;
723
724free_tms:
725 if (tm_list) {
726 for (i = 0; i < nritems; i++)
727 kfree(tm_list[i]);
728 kfree(tm_list);
729 }
730 kfree(tm);
731
732 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200733}
734
735static struct tree_mod_elem *
736__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
737 int smallest)
738{
739 struct rb_root *tm_root;
740 struct rb_node *node;
741 struct tree_mod_elem *cur = NULL;
742 struct tree_mod_elem *found = NULL;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200743
Jan Schmidt097b8a72012-06-21 11:08:04 +0200744 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200745 tm_root = &fs_info->tree_mod_log;
746 node = tm_root->rb_node;
747 while (node) {
748 cur = container_of(node, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530749 if (cur->logical < start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200750 node = node->rb_left;
Chandan Rajendra298cfd32016-01-21 15:55:59 +0530751 } else if (cur->logical > start) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200752 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200753 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200754 node = node->rb_left;
755 } else if (!smallest) {
756 /* we want the node with the highest seq */
757 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200758 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200759 found = cur;
760 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200761 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200762 /* we want the node with the smallest seq */
763 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200764 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200765 found = cur;
766 node = node->rb_right;
767 } else {
768 found = cur;
769 break;
770 }
771 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200772 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200773
774 return found;
775}
776
777/*
778 * this returns the element from the log with the smallest time sequence
779 * value that's in the log (the oldest log item). any element with a time
780 * sequence lower than min_seq will be ignored.
781 */
782static struct tree_mod_elem *
783tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
784 u64 min_seq)
785{
786 return __tree_mod_log_search(fs_info, start, min_seq, 1);
787}
788
789/*
790 * this returns the element from the log with the largest time sequence
791 * value that's in the log (the most recent log item). any element with
792 * a time sequence lower than min_seq will be ignored.
793 */
794static struct tree_mod_elem *
795tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
796{
797 return __tree_mod_log_search(fs_info, start, min_seq, 0);
798}
799
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000800static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200801tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
802 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000803 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200804{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000805 int ret = 0;
806 struct tree_mod_elem **tm_list = NULL;
807 struct tree_mod_elem **tm_list_add, **tm_list_rem;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200808 int i;
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000809 int locked = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200810
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000811 if (!tree_mod_need_log(fs_info, NULL))
812 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200813
Josef Bacikc8cc6342013-07-01 16:18:19 -0400814 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000815 return 0;
816
David Sterba31e818f2015-02-20 18:00:26 +0100817 tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000818 GFP_NOFS);
819 if (!tm_list)
820 return -ENOMEM;
821
822 tm_list_add = tm_list;
823 tm_list_rem = tm_list + nr_items;
824 for (i = 0; i < nr_items; i++) {
825 tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
826 MOD_LOG_KEY_REMOVE, GFP_NOFS);
827 if (!tm_list_rem[i]) {
828 ret = -ENOMEM;
829 goto free_tms;
830 }
831
832 tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
833 MOD_LOG_KEY_ADD, GFP_NOFS);
834 if (!tm_list_add[i]) {
835 ret = -ENOMEM;
836 goto free_tms;
837 }
838 }
839
840 if (tree_mod_dont_log(fs_info, NULL))
841 goto free_tms;
842 locked = 1;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200843
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200844 for (i = 0; i < nr_items; i++) {
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000845 ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
846 if (ret)
847 goto free_tms;
848 ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
849 if (ret)
850 goto free_tms;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200851 }
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000852
853 tree_mod_log_write_unlock(fs_info);
854 kfree(tm_list);
855
856 return 0;
857
858free_tms:
859 for (i = 0; i < nr_items * 2; i++) {
860 if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
861 rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
862 kfree(tm_list[i]);
863 }
864 if (locked)
865 tree_mod_log_write_unlock(fs_info);
866 kfree(tm_list);
867
868 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200869}
870
871static inline void
872tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
873 int dst_offset, int src_offset, int nr_items)
874{
875 int ret;
876 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
877 nr_items, GFP_NOFS);
878 BUG_ON(ret < 0);
879}
880
Jan Schmidt097b8a72012-06-21 11:08:04 +0200881static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200882tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000883 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200884{
885 int ret;
886
Filipe David Borba Manana78357762013-12-12 19:19:52 +0000887 ret = tree_mod_log_insert_key(fs_info, eb, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400888 MOD_LOG_KEY_REPLACE,
889 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200890 BUG_ON(ret < 0);
891}
892
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000893static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200894tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200895{
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000896 struct tree_mod_elem **tm_list = NULL;
897 int nritems = 0;
898 int i;
899 int ret = 0;
900
901 if (btrfs_header_level(eb) == 0)
902 return 0;
903
904 if (!tree_mod_need_log(fs_info, NULL))
905 return 0;
906
907 nritems = btrfs_header_nritems(eb);
David Sterba31e818f2015-02-20 18:00:26 +0100908 tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000909 if (!tm_list)
910 return -ENOMEM;
911
912 for (i = 0; i < nritems; i++) {
913 tm_list[i] = alloc_tree_mod_elem(eb, i,
914 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
915 if (!tm_list[i]) {
916 ret = -ENOMEM;
917 goto free_tms;
918 }
919 }
920
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200921 if (tree_mod_dont_log(fs_info, eb))
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +0000922 goto free_tms;
923
924 ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
925 tree_mod_log_write_unlock(fs_info);
926 if (ret)
927 goto free_tms;
928 kfree(tm_list);
929
930 return 0;
931
932free_tms:
933 for (i = 0; i < nritems; i++)
934 kfree(tm_list[i]);
935 kfree(tm_list);
936
937 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200938}
939
Jan Schmidt097b8a72012-06-21 11:08:04 +0200940static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200941tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000942 struct extent_buffer *new_root_node,
943 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200944{
945 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200946 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000947 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200948 BUG_ON(ret < 0);
949}
950
Chris Masond352ac62008-09-29 15:18:18 -0400951/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 * check if the tree block can be shared by multiple trees
953 */
954int btrfs_block_can_be_shared(struct btrfs_root *root,
955 struct extent_buffer *buf)
956{
957 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -0400958 * Tree blocks not in reference counted trees and tree roots
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 * are never shared. If a block was allocated after the last
960 * snapshot and the block was not allocated by tree relocation,
961 * we know the block is not shared.
962 */
Miao Xie27cdeb72014-04-02 19:51:05 +0800963 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400964 buf != root->node && buf != root->commit_root &&
965 (btrfs_header_generation(buf) <=
966 btrfs_root_last_snapshot(&root->root_item) ||
967 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
968 return 1;
969#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
Miao Xie27cdeb72014-04-02 19:51:05 +0800970 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400971 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
972 return 1;
973#endif
974 return 0;
975}
976
977static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
978 struct btrfs_root *root,
979 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400980 struct extent_buffer *cow,
981 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400982{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400983 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400984 u64 refs;
985 u64 owner;
986 u64 flags;
987 u64 new_flags = 0;
988 int ret;
989
990 /*
991 * Backrefs update rules:
992 *
993 * Always use full backrefs for extent pointers in tree block
994 * allocated by tree relocation.
995 *
996 * If a shared tree block is no longer referenced by its owner
997 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
998 * use full backrefs for extent pointers in tree block.
999 *
1000 * If a tree block is been relocating
1001 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
1002 * use full backrefs for extent pointers in tree block.
1003 * The reason for this is some operations (such as drop tree)
1004 * are only allowed for blocks use full backrefs.
1005 */
1006
1007 if (btrfs_block_can_be_shared(root, buf)) {
1008 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -05001009 btrfs_header_level(buf), 1,
1010 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001011 if (ret)
1012 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -07001013 if (refs == 0) {
1014 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001015 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fashehe5df9572011-08-29 14:17:04 -07001016 return ret;
1017 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001018 } else {
1019 refs = 1;
1020 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1021 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1022 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1023 else
1024 flags = 0;
1025 }
1026
1027 owner = btrfs_header_owner(buf);
1028 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
1029 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
1030
1031 if (refs > 1) {
1032 if ((owner == root->root_key.objectid ||
1033 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
1034 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001035 ret = btrfs_inc_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001036 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001037
1038 if (root->root_key.objectid ==
1039 BTRFS_TREE_RELOC_OBJECTID) {
Josef Bacike339a6b2014-07-02 10:54:25 -07001040 ret = btrfs_dec_ref(trans, root, buf, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001041 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001042 ret = btrfs_inc_ref(trans, root, cow, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001044 }
1045 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
1046 } else {
1047
1048 if (root->root_key.objectid ==
1049 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001050 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001051 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001052 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001053 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001054 }
1055 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -04001056 int level = btrfs_header_level(buf);
1057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001058 ret = btrfs_set_disk_extent_flags(trans, root,
1059 buf->start,
1060 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -04001061 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -07001062 if (ret)
1063 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 }
1065 } else {
1066 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
1067 if (root->root_key.objectid ==
1068 BTRFS_TREE_RELOC_OBJECTID)
Josef Bacike339a6b2014-07-02 10:54:25 -07001069 ret = btrfs_inc_ref(trans, root, cow, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070 else
Josef Bacike339a6b2014-07-02 10:54:25 -07001071 ret = btrfs_inc_ref(trans, root, cow, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001072 BUG_ON(ret); /* -ENOMEM */
Josef Bacike339a6b2014-07-02 10:54:25 -07001073 ret = btrfs_dec_ref(trans, root, buf, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001074 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001075 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001076 clean_tree_block(trans, fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001077 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001078 }
1079 return 0;
1080}
1081
1082/*
Chris Masond3977122009-01-05 21:25:51 -05001083 * does the dirty work in cow of a single block. The parent block (if
1084 * supplied) is updated to point to the new cow copy. The new buffer is marked
1085 * dirty and returned locked. If you modify the block it needs to be marked
1086 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -04001087 *
1088 * search_start -- an allocation hint for the new block
1089 *
Chris Masond3977122009-01-05 21:25:51 -05001090 * empty_size -- a hint that you plan on doing more cow. This is the size in
1091 * bytes the allocator should try to find free next to the block it returns.
1092 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -04001093 */
Chris Masond3977122009-01-05 21:25:51 -05001094static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001095 struct btrfs_root *root,
1096 struct extent_buffer *buf,
1097 struct extent_buffer *parent, int parent_slot,
1098 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001099 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -04001100{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001101 struct btrfs_fs_info *fs_info = root->fs_info;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001102 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001103 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001104 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001105 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001106 int unlock_orig = 0;
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001107 u64 parent_start = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001108
Chris Mason925baed2008-06-25 16:01:30 -04001109 if (*cow_ret == buf)
1110 unlock_orig = 1;
1111
Chris Masonb9447ef82009-03-09 11:45:38 -04001112 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001113
Miao Xie27cdeb72014-04-02 19:51:05 +08001114 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001115 trans->transid != fs_info->running_transaction->transid);
Miao Xie27cdeb72014-04-02 19:51:05 +08001116 WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
1117 trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001118
Chris Mason7bb86312007-12-11 09:25:06 -05001119 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001120
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001121 if (level == 0)
1122 btrfs_item_key(buf, &disk_key, 0);
1123 else
1124 btrfs_node_key(buf, &disk_key, 0);
1125
Goldwyn Rodrigues0f5053e2016-09-22 14:11:34 -05001126 if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
1127 parent_start = parent->start;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001128
David Sterba4d75f8a2014-06-15 01:54:12 +02001129 cow = btrfs_alloc_tree_block(trans, root, parent_start,
1130 root->root_key.objectid, &disk_key, level,
1131 search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001132 if (IS_ERR(cow))
1133 return PTR_ERR(cow);
1134
Chris Masonb4ce94d2009-02-04 09:25:08 -05001135 /* cow is set to blocking by btrfs_init_new_buffer */
1136
David Sterba58e80122016-11-08 18:30:31 +01001137 copy_extent_buffer_full(cow, buf);
Chris Masondb945352007-10-15 16:15:53 -04001138 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001139 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001140 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1141 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1142 BTRFS_HEADER_FLAG_RELOC);
1143 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1144 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1145 else
1146 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001147
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001148 write_extent_buffer_fsid(cow, fs_info->fsid);
Yan Zheng2b820322008-11-17 21:11:30 -05001149
Mark Fashehbe1a5562011-08-08 13:20:18 -07001150 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001151 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001152 btrfs_abort_transaction(trans, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001153 return ret;
1154 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001155
Miao Xie27cdeb72014-04-02 19:51:05 +08001156 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001157 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
Zhaolei93314e32015-08-06 21:56:58 +08001158 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001159 btrfs_abort_transaction(trans, ret);
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001160 return ret;
Zhaolei93314e32015-08-06 21:56:58 +08001161 }
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001162 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001163
Chris Mason6702ed42007-08-07 16:15:09 -04001164 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001165 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001166 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1167 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1168 parent_start = buf->start;
Chris Mason925baed2008-06-25 16:01:30 -04001169
Chris Mason5f39d392007-10-15 16:14:19 -04001170 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001171 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001172 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001173
Yan, Zhengf0486c62010-05-16 10:46:25 -04001174 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001175 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001176 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001177 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001178 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001179 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001180 tree_mod_log_insert_key(fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001181 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001182 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001183 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001184 btrfs_set_node_ptr_generation(parent, parent_slot,
1185 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001186 btrfs_mark_buffer_dirty(parent);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001187 if (last_ref) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001188 ret = tree_mod_log_free_eb(fs_info, buf);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001189 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04001190 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00001191 return ret;
1192 }
1193 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04001194 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001195 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001196 }
Chris Mason925baed2008-06-25 16:01:30 -04001197 if (unlock_orig)
1198 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001199 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001200 btrfs_mark_buffer_dirty(cow);
1201 *cow_ret = cow;
1202 return 0;
1203}
1204
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001205/*
1206 * returns the logical address of the oldest predecessor of the given root.
1207 * entries older than time_seq are ignored.
1208 */
1209static struct tree_mod_elem *
1210__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001211 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001212{
1213 struct tree_mod_elem *tm;
1214 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001215 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001216 int looped = 0;
1217
1218 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001219 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001220
1221 /*
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301222 * the very last operation that's logged for a root is the
1223 * replacement operation (if it is replaced at all). this has
1224 * the logical address of the *new* root, making it the very
1225 * first operation that's logged for this root.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001226 */
1227 while (1) {
1228 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1229 time_seq);
1230 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001231 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001232 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001233 * if there are no tree operation for the oldest root, we simply
1234 * return it. this should only happen if that (old) root is at
1235 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001236 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001237 if (!tm)
1238 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001240 /*
1241 * if there's an operation that's not a root replacement, we
1242 * found the oldest version of our root. normally, we'll find a
1243 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1244 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001245 if (tm->op != MOD_LOG_ROOT_REPLACE)
1246 break;
1247
1248 found = tm;
1249 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001250 looped = 1;
1251 }
1252
Jan Schmidta95236d2012-06-05 16:41:24 +02001253 /* if there's no old root to return, return what we found instead */
1254 if (!found)
1255 found = tm;
1256
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001257 return found;
1258}
1259
1260/*
1261 * tm is a pointer to the first operation to rewind within eb. then, all
Nicholas D Steeves01327612016-05-19 21:18:45 -04001262 * previous operations will be rewound (until we reach something older than
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001263 * time_seq).
1264 */
1265static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001266__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1267 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268{
1269 u32 n;
1270 struct rb_node *next;
1271 struct tree_mod_elem *tm = first_tm;
1272 unsigned long o_dst;
1273 unsigned long o_src;
1274 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1275
1276 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001277 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001278 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001279 /*
1280 * all the operations are recorded with the operator used for
1281 * the modification. as we're going backwards, we do the
1282 * opposite of each operation here.
1283 */
1284 switch (tm->op) {
1285 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1286 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001287 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001288 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001289 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001290 btrfs_set_node_key(eb, &tm->key, tm->slot);
1291 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1292 btrfs_set_node_ptr_generation(eb, tm->slot,
1293 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001294 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001295 break;
1296 case MOD_LOG_KEY_REPLACE:
1297 BUG_ON(tm->slot >= n);
1298 btrfs_set_node_key(eb, &tm->key, tm->slot);
1299 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1300 btrfs_set_node_ptr_generation(eb, tm->slot,
1301 tm->generation);
1302 break;
1303 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001304 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001305 n--;
1306 break;
1307 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001308 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1309 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1310 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001311 tm->move.nr_items * p_size);
1312 break;
1313 case MOD_LOG_ROOT_REPLACE:
1314 /*
1315 * this operation is special. for roots, this must be
1316 * handled explicitly before rewinding.
1317 * for non-roots, this operation may exist if the node
1318 * was a root: root A -> child B; then A gets empty and
1319 * B is promoted to the new root. in the mod log, we'll
1320 * have a root-replace operation for B, a tree block
1321 * that is no root. we simply ignore that operation.
1322 */
1323 break;
1324 }
1325 next = rb_next(&tm->node);
1326 if (!next)
1327 break;
1328 tm = container_of(next, struct tree_mod_elem, node);
Chandan Rajendra298cfd32016-01-21 15:55:59 +05301329 if (tm->logical != first_tm->logical)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001330 break;
1331 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001332 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001333 btrfs_set_header_nritems(eb, n);
1334}
1335
Jan Schmidt47fb0912013-04-13 13:19:55 +00001336/*
Nicholas D Steeves01327612016-05-19 21:18:45 -04001337 * Called with eb read locked. If the buffer cannot be rewound, the same buffer
Jan Schmidt47fb0912013-04-13 13:19:55 +00001338 * is returned. If rewind operations happen, a fresh buffer is returned. The
1339 * returned buffer is always read-locked. If the returned buffer is not the
1340 * input buffer, the lock on the input buffer is released and the input buffer
1341 * is freed (its refcount is decremented).
1342 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001343static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001344tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1345 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001346{
1347 struct extent_buffer *eb_rewin;
1348 struct tree_mod_elem *tm;
1349
1350 if (!time_seq)
1351 return eb;
1352
1353 if (btrfs_header_level(eb) == 0)
1354 return eb;
1355
1356 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1357 if (!tm)
1358 return eb;
1359
Josef Bacik9ec72672013-08-07 16:57:23 -04001360 btrfs_set_path_blocking(path);
1361 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1362
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001363 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1364 BUG_ON(tm->slot != 0);
Jeff Mahoneyda170662016-06-15 09:22:56 -04001365 eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001366 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001367 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001368 free_extent_buffer(eb);
1369 return NULL;
1370 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001371 btrfs_set_header_bytenr(eb_rewin, eb->start);
1372 btrfs_set_header_backref_rev(eb_rewin,
1373 btrfs_header_backref_rev(eb));
1374 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001375 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001376 } else {
1377 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001378 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001379 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001380 free_extent_buffer(eb);
1381 return NULL;
1382 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001383 }
1384
Josef Bacik9ec72672013-08-07 16:57:23 -04001385 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1386 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001387 free_extent_buffer(eb);
1388
Jan Schmidt47fb0912013-04-13 13:19:55 +00001389 extent_buffer_get(eb_rewin);
1390 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001391 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001392 WARN_ON(btrfs_header_nritems(eb_rewin) >
Jeff Mahoneyda170662016-06-15 09:22:56 -04001393 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001394
1395 return eb_rewin;
1396}
1397
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001398/*
1399 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1400 * value. If there are no changes, the current root->root_node is returned. If
1401 * anything changed in between, there's a fresh buffer allocated on which the
1402 * rewind operations are done. In any case, the returned buffer is read locked.
1403 * Returns NULL on error (with no locks held).
1404 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001405static inline struct extent_buffer *
1406get_old_root(struct btrfs_root *root, u64 time_seq)
1407{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001408 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001409 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001410 struct extent_buffer *eb = NULL;
1411 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001412 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001413 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001414 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001415 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001416
Jan Schmidt30b04632013-04-13 13:19:54 +00001417 eb_root = btrfs_read_lock_root_node(root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001418 tm = __tree_mod_log_oldest_root(fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001419 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001420 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001421
Jan Schmidta95236d2012-06-05 16:41:24 +02001422 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1423 old_root = &tm->old_root;
1424 old_generation = tm->generation;
1425 logical = old_root->logical;
1426 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001427 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001428 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001429
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001430 tm = tree_mod_log_search(fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001431 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001432 btrfs_tree_read_unlock(eb_root);
1433 free_extent_buffer(eb_root);
David Sterbace86cd52014-06-15 01:07:32 +02001434 old = read_tree_block(root, logical, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08001435 if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
1436 if (!IS_ERR(old))
1437 free_extent_buffer(old);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001438 btrfs_warn(fs_info,
1439 "failed to read tree block %llu from get_old_root",
1440 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001441 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001442 eb = btrfs_clone_extent_buffer(old);
1443 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001444 }
1445 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001446 btrfs_tree_read_unlock(eb_root);
1447 free_extent_buffer(eb_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001448 eb = alloc_dummy_extent_buffer(fs_info, logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001449 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001450 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001451 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001452 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001453 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001454 }
1455
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001456 if (!eb)
1457 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001458 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001459 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001460 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001461 btrfs_set_header_bytenr(eb, eb->start);
1462 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001463 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001464 btrfs_set_header_level(eb, old_root->level);
1465 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001466 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001467 if (tm)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001468 __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001469 else
1470 WARN_ON(btrfs_header_level(eb) != 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001471 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001472
1473 return eb;
1474}
1475
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001476int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1477{
1478 struct tree_mod_elem *tm;
1479 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001480 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001481
Jan Schmidt30b04632013-04-13 13:19:54 +00001482 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001483 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1484 level = tm->old_root.level;
1485 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001486 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001487 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001488 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001489
1490 return level;
1491}
1492
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493static inline int should_cow_block(struct btrfs_trans_handle *trans,
1494 struct btrfs_root *root,
1495 struct extent_buffer *buf)
1496{
Jeff Mahoneyf5ee5c92016-06-21 09:52:41 -04001497 if (btrfs_is_testing(root->fs_info))
Josef Bacikfaa2dbf2014-05-07 17:06:09 -04001498 return 0;
David Sterbafccb84c2014-09-29 23:53:21 +02001499
Liu Bof1ebcc72011-11-14 20:48:06 -05001500 /* ensure we can see the force_cow */
1501 smp_rmb();
1502
1503 /*
1504 * We do not need to cow a block if
1505 * 1) this block is not created or changed in this transaction;
1506 * 2) this block does not belong to TREE_RELOC tree;
1507 * 3) the root is not forced COW.
1508 *
1509 * What is forced COW:
Nicholas D Steeves01327612016-05-19 21:18:45 -04001510 * when we create snapshot during committing the transaction,
Liu Bof1ebcc72011-11-14 20:48:06 -05001511 * after we've finished coping src root, we must COW the shared
1512 * block to ensure the metadata consistency.
1513 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001514 if (btrfs_header_generation(buf) == trans->transid &&
1515 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1516 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001517 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
Miao Xie27cdeb72014-04-02 19:51:05 +08001518 !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001519 return 0;
1520 return 1;
1521}
1522
Chris Masond352ac62008-09-29 15:18:18 -04001523/*
1524 * cows a single block, see __btrfs_cow_block for the real work.
Nicholas D Steeves01327612016-05-19 21:18:45 -04001525 * This version of it has extra checks so that a block isn't COWed more than
Chris Masond352ac62008-09-29 15:18:18 -04001526 * once per transaction, as long as it hasn't been written yet
1527 */
Chris Masond3977122009-01-05 21:25:51 -05001528noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001529 struct btrfs_root *root, struct extent_buffer *buf,
1530 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001531 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001532{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001533 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6702ed42007-08-07 16:15:09 -04001534 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001535 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001537 if (trans->transaction != fs_info->running_transaction)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001538 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001539 trans->transid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001540 fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001542 if (trans->transid != fs_info->generation)
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001543 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001544 trans->transid, fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001545
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001546 if (!should_cow_block(trans, root, buf)) {
Jeff Mahoney64c12922016-06-08 00:36:38 -04001547 trans->dirty = true;
Chris Mason02217ed2007-03-02 16:08:05 -05001548 *cow_ret = buf;
1549 return 0;
1550 }
Chris Masonc4876852009-02-04 09:24:25 -05001551
Byongho Leeee221842015-12-15 01:42:10 +09001552 search_start = buf->start & ~((u64)SZ_1G - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001553
1554 if (parent)
1555 btrfs_set_lock_blocking(parent);
1556 btrfs_set_lock_blocking(buf);
1557
Chris Masonf510cfe2007-10-15 16:14:48 -04001558 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001559 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001560
1561 trace_btrfs_cow_block(root, buf, *cow_ret);
1562
Chris Masonf510cfe2007-10-15 16:14:48 -04001563 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001564}
1565
Chris Masond352ac62008-09-29 15:18:18 -04001566/*
1567 * helper function for defrag to decide if two blocks pointed to by a
1568 * node are actually close by
1569 */
Chris Mason6b800532007-10-15 16:17:34 -04001570static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001571{
Chris Mason6b800532007-10-15 16:17:34 -04001572 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001573 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001574 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001575 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001576 return 0;
1577}
1578
Chris Mason081e9572007-11-06 10:26:24 -05001579/*
1580 * compare two keys in a memcmp fashion
1581 */
1582static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1583{
1584 struct btrfs_key k1;
1585
1586 btrfs_disk_key_to_cpu(&k1, disk);
1587
Diego Calleja20736ab2009-07-24 11:06:52 -04001588 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001589}
1590
Josef Bacikf3465ca2008-11-12 14:19:50 -05001591/*
1592 * same as comp_keys only with two btrfs_key's
1593 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001594int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001595{
1596 if (k1->objectid > k2->objectid)
1597 return 1;
1598 if (k1->objectid < k2->objectid)
1599 return -1;
1600 if (k1->type > k2->type)
1601 return 1;
1602 if (k1->type < k2->type)
1603 return -1;
1604 if (k1->offset > k2->offset)
1605 return 1;
1606 if (k1->offset < k2->offset)
1607 return -1;
1608 return 0;
1609}
Chris Mason081e9572007-11-06 10:26:24 -05001610
Chris Masond352ac62008-09-29 15:18:18 -04001611/*
1612 * this is used by the defrag code to go through all the
1613 * leaves pointed to by a node and reallocate them so that
1614 * disk order is close to key order
1615 */
Chris Mason6702ed42007-08-07 16:15:09 -04001616int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001617 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001618 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001619 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001620{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001621 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6b800532007-10-15 16:17:34 -04001622 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001623 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001624 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001625 u64 search_start = *last_ret;
1626 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001627 u64 other;
1628 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001629 int end_slot;
1630 int i;
1631 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001632 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001633 int uptodate;
1634 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001635 int progress_passed = 0;
1636 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001637
Chris Mason5708b952007-10-25 15:43:18 -04001638 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001639
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001640 WARN_ON(trans->transaction != fs_info->running_transaction);
1641 WARN_ON(trans->transid != fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001642
Chris Mason6b800532007-10-15 16:17:34 -04001643 parent_nritems = btrfs_header_nritems(parent);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001644 blocksize = fs_info->nodesize;
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001645 end_slot = parent_nritems - 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001646
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001647 if (parent_nritems <= 1)
Chris Mason6702ed42007-08-07 16:15:09 -04001648 return 0;
1649
Chris Masonb4ce94d2009-02-04 09:25:08 -05001650 btrfs_set_lock_blocking(parent);
1651
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001652 for (i = start_slot; i <= end_slot; i++) {
Chris Mason6702ed42007-08-07 16:15:09 -04001653 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001654
Chris Mason081e9572007-11-06 10:26:24 -05001655 btrfs_node_key(parent, &disk_key, i);
1656 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1657 continue;
1658
1659 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001660 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001661 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001662 if (last_block == 0)
1663 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001664
Chris Mason6702ed42007-08-07 16:15:09 -04001665 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001666 other = btrfs_node_blockptr(parent, i - 1);
1667 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001668 }
Filipe Manana5dfe2be2015-02-23 19:48:52 +00001669 if (!close && i < end_slot) {
Chris Mason6b800532007-10-15 16:17:34 -04001670 other = btrfs_node_blockptr(parent, i + 1);
1671 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001672 }
Chris Masone9d0b132007-08-10 14:06:19 -04001673 if (close) {
1674 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001675 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001676 }
Chris Mason6702ed42007-08-07 16:15:09 -04001677
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001678 cur = find_extent_buffer(fs_info, blocknr);
Chris Mason6b800532007-10-15 16:17:34 -04001679 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001680 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001681 else
1682 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001683 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001684 if (!cur) {
David Sterbace86cd52014-06-15 01:07:32 +02001685 cur = read_tree_block(root, blocknr, gen);
Liu Bo64c043d2015-05-25 17:30:15 +08001686 if (IS_ERR(cur)) {
1687 return PTR_ERR(cur);
1688 } else if (!extent_buffer_uptodate(cur)) {
Josef Bacik416bc652013-04-23 14:17:42 -04001689 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001690 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001691 }
Chris Mason6b800532007-10-15 16:17:34 -04001692 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001693 err = btrfs_read_buffer(cur, gen);
1694 if (err) {
1695 free_extent_buffer(cur);
1696 return err;
1697 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001698 }
Chris Mason6702ed42007-08-07 16:15:09 -04001699 }
Chris Masone9d0b132007-08-10 14:06:19 -04001700 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001701 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001702
Chris Masone7a84562008-06-25 16:01:31 -04001703 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001704 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001705 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001706 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001707 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001708 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001709 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001710 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001711 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001712 break;
Yan252c38f2007-08-29 09:11:44 -04001713 }
Chris Masone7a84562008-06-25 16:01:31 -04001714 search_start = cur->start;
1715 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001716 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001717 btrfs_tree_unlock(cur);
1718 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001719 }
1720 return err;
1721}
1722
Chris Mason74123bd2007-02-02 11:05:29 -05001723/*
Chris Mason5f39d392007-10-15 16:14:19 -04001724 * search for key in the extent_buffer. The items start at offset p,
1725 * and they are item_size apart. There are 'max' items in p.
1726 *
Chris Mason74123bd2007-02-02 11:05:29 -05001727 * the slot in the array is returned via slot, and it points to
1728 * the place where you would insert key if it is not found in
1729 * the array.
1730 *
1731 * slot may point to max if the key is bigger than all of the keys
1732 */
Chris Masone02119d2008-09-05 16:13:11 -04001733static noinline int generic_bin_search(struct extent_buffer *eb,
1734 unsigned long p,
1735 int item_size, struct btrfs_key *key,
1736 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001737{
1738 int low = 0;
1739 int high = max;
1740 int mid;
1741 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001742 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001743 struct btrfs_disk_key unaligned;
1744 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001745 char *kaddr = NULL;
1746 unsigned long map_start = 0;
1747 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001748 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001749
Liu Bo5e24e9a2016-06-23 16:32:45 -07001750 if (low > high) {
1751 btrfs_err(eb->fs_info,
1752 "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
1753 __func__, low, high, eb->start,
1754 btrfs_header_owner(eb), btrfs_header_level(eb));
1755 return -EINVAL;
1756 }
1757
Chris Masond3977122009-01-05 21:25:51 -05001758 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001759 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001760 offset = p + mid * item_size;
1761
Chris Masona6591712011-07-19 12:04:14 -04001762 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001763 (offset + sizeof(struct btrfs_disk_key)) >
1764 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001765
1766 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001767 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001768 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001769
Chris Mason479965d2007-10-15 16:14:27 -04001770 if (!err) {
1771 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1772 map_start);
Liu Bo415b35a2016-06-17 19:16:21 -07001773 } else if (err == 1) {
Chris Mason479965d2007-10-15 16:14:27 -04001774 read_extent_buffer(eb, &unaligned,
1775 offset, sizeof(unaligned));
1776 tmp = &unaligned;
Liu Bo415b35a2016-06-17 19:16:21 -07001777 } else {
1778 return err;
Chris Mason479965d2007-10-15 16:14:27 -04001779 }
1780
Chris Mason5f39d392007-10-15 16:14:19 -04001781 } else {
1782 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1783 map_start);
1784 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001785 ret = comp_keys(tmp, key);
1786
1787 if (ret < 0)
1788 low = mid + 1;
1789 else if (ret > 0)
1790 high = mid;
1791 else {
1792 *slot = mid;
1793 return 0;
1794 }
1795 }
1796 *slot = low;
1797 return 1;
1798}
1799
Chris Mason97571fd2007-02-24 13:39:08 -05001800/*
1801 * simple bin_search frontend that does the right thing for
1802 * leaves vs nodes
1803 */
Chris Mason5f39d392007-10-15 16:14:19 -04001804static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1805 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001806{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001807 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001808 return generic_bin_search(eb,
1809 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001810 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001811 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001812 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001813 else
Chris Mason5f39d392007-10-15 16:14:19 -04001814 return generic_bin_search(eb,
1815 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001816 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001817 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001818 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001819}
1820
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001821int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1822 int level, int *slot)
1823{
1824 return bin_search(eb, key, level, slot);
1825}
1826
Yan, Zhengf0486c62010-05-16 10:46:25 -04001827static void root_add_used(struct btrfs_root *root, u32 size)
1828{
1829 spin_lock(&root->accounting_lock);
1830 btrfs_set_root_used(&root->root_item,
1831 btrfs_root_used(&root->root_item) + size);
1832 spin_unlock(&root->accounting_lock);
1833}
1834
1835static void root_sub_used(struct btrfs_root *root, u32 size)
1836{
1837 spin_lock(&root->accounting_lock);
1838 btrfs_set_root_used(&root->root_item,
1839 btrfs_root_used(&root->root_item) - size);
1840 spin_unlock(&root->accounting_lock);
1841}
1842
Chris Masond352ac62008-09-29 15:18:18 -04001843/* given a node and slot number, this reads the blocks it points to. The
1844 * extent buffer is returned with a reference taken (but unlocked).
Chris Masond352ac62008-09-29 15:18:18 -04001845 */
Chris Masone02119d2008-09-05 16:13:11 -04001846static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001847 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001848{
Chris Masonca7a79a2008-05-12 12:59:19 -04001849 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001850 struct extent_buffer *eb;
1851
Liu Bofb770ae2016-07-05 12:10:14 -07001852 if (slot < 0 || slot >= btrfs_header_nritems(parent))
1853 return ERR_PTR(-ENOENT);
Chris Masonca7a79a2008-05-12 12:59:19 -04001854
1855 BUG_ON(level == 0);
1856
Josef Bacik416bc652013-04-23 14:17:42 -04001857 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
Josef Bacik416bc652013-04-23 14:17:42 -04001858 btrfs_node_ptr_generation(parent, slot));
Liu Bofb770ae2016-07-05 12:10:14 -07001859 if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
1860 free_extent_buffer(eb);
1861 eb = ERR_PTR(-EIO);
Josef Bacik416bc652013-04-23 14:17:42 -04001862 }
1863
1864 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001865}
1866
Chris Masond352ac62008-09-29 15:18:18 -04001867/*
1868 * node level balancing, used to make sure nodes are in proper order for
1869 * item deletion. We balance from the top down, so we have to make sure
1870 * that a deletion won't leave an node completely empty later on.
1871 */
Chris Masone02119d2008-09-05 16:13:11 -04001872static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001873 struct btrfs_root *root,
1874 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04001877 struct extent_buffer *right = NULL;
1878 struct extent_buffer *mid;
1879 struct extent_buffer *left = NULL;
1880 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001881 int ret = 0;
1882 int wret;
1883 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001884 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001885 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001886
1887 if (level == 0)
1888 return 0;
1889
Chris Mason5f39d392007-10-15 16:14:19 -04001890 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001891
Chris Masonbd681512011-07-16 15:23:14 -04001892 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1893 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001894 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1895
Chris Mason1d4f8a02007-03-13 09:28:32 -04001896 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001897
Li Zefana05a9bb2011-09-06 16:55:34 +08001898 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001899 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001900 pslot = path->slots[level + 1];
1901 }
Chris Masonbb803952007-03-01 12:04:21 -05001902
Chris Mason40689472007-03-17 14:29:23 -04001903 /*
1904 * deal with the case where there is only one pointer in the root
1905 * by promoting the node below to a root
1906 */
Chris Mason5f39d392007-10-15 16:14:19 -04001907 if (!parent) {
1908 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001909
Chris Mason5f39d392007-10-15 16:14:19 -04001910 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001911 return 0;
1912
1913 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001914 child = read_node_slot(root, mid, 0);
Liu Bofb770ae2016-07-05 12:10:14 -07001915 if (IS_ERR(child)) {
1916 ret = PTR_ERR(child);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001917 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001918 goto enospc;
1919 }
1920
Chris Mason925baed2008-06-25 16:01:30 -04001921 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001922 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001923 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001924 if (ret) {
1925 btrfs_tree_unlock(child);
1926 free_extent_buffer(child);
1927 goto enospc;
1928 }
Yan2f375ab2008-02-01 14:58:07 -05001929
Jan Schmidt90f8d622013-04-13 13:19:53 +00001930 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001931 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001932
Chris Mason0b86a832008-03-24 15:01:56 -04001933 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001934 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001935
Chris Mason925baed2008-06-25 16:01:30 -04001936 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001937 path->nodes[level] = NULL;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001938 clean_tree_block(trans, fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001939 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001940 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001941 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001942
1943 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001944 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001945 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001946 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001947 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001948 }
Chris Mason5f39d392007-10-15 16:14:19 -04001949 if (btrfs_header_nritems(mid) >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001950 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001951 return 0;
1952
Chris Mason5f39d392007-10-15 16:14:19 -04001953 left = read_node_slot(root, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001954 if (IS_ERR(left))
1955 left = NULL;
1956
Chris Mason5f39d392007-10-15 16:14:19 -04001957 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001958 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001959 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001960 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001961 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001962 if (wret) {
1963 ret = wret;
1964 goto enospc;
1965 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001966 }
Liu Bofb770ae2016-07-05 12:10:14 -07001967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 right = read_node_slot(root, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07001969 if (IS_ERR(right))
1970 right = NULL;
1971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001973 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001974 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001975 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001976 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001977 if (wret) {
1978 ret = wret;
1979 goto enospc;
1980 }
1981 }
1982
1983 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001984 if (left) {
1985 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001986 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001987 if (wret < 0)
1988 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001989 }
Chris Mason79f95c82007-03-01 15:16:26 -05001990
1991 /*
1992 * then try to empty the right most buffer into the middle
1993 */
Chris Mason5f39d392007-10-15 16:14:19 -04001994 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001995 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001996 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001997 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001998 if (btrfs_header_nritems(right) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001999 clean_tree_block(trans, fs_info, right);
Chris Mason925baed2008-06-25 16:01:30 -04002000 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002001 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002002 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002003 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002004 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002005 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05002006 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002007 struct btrfs_disk_key right_key;
2008 btrfs_node_key(right, &right_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002009 tree_mod_log_set_node_key(fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002010 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 btrfs_set_node_key(parent, &right_key, pslot + 1);
2012 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05002013 }
2014 }
Chris Mason5f39d392007-10-15 16:14:19 -04002015 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05002016 /*
2017 * we're not allowed to leave a node with one item in the
2018 * tree during a delete. A deletion from lower in the tree
2019 * could try to delete the only pointer in this node.
2020 * So, pull some keys from the left.
2021 * There has to be a left pointer at this point because
2022 * otherwise we would have pulled some pointers from the
2023 * right
2024 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07002025 if (!left) {
2026 ret = -EROFS;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002027 btrfs_handle_fs_error(fs_info, ret, NULL);
Mark Fasheh305a26a2011-09-01 11:27:57 -07002028 goto enospc;
2029 }
Chris Mason5f39d392007-10-15 16:14:19 -04002030 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002031 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05002032 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002033 goto enospc;
2034 }
Chris Masonbce4eae2008-04-24 14:42:46 -04002035 if (wret == 1) {
2036 wret = push_node_left(trans, root, left, mid, 1);
2037 if (wret < 0)
2038 ret = wret;
2039 }
Chris Mason79f95c82007-03-01 15:16:26 -05002040 BUG_ON(wret == 1);
2041 }
Chris Mason5f39d392007-10-15 16:14:19 -04002042 if (btrfs_header_nritems(mid) == 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002043 clean_tree_block(trans, fs_info, mid);
Chris Mason925baed2008-06-25 16:01:30 -04002044 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002045 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002046 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02002047 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05002048 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002049 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05002050 } else {
2051 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04002052 struct btrfs_disk_key mid_key;
2053 btrfs_node_key(mid, &mid_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002054 tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002055 btrfs_set_node_key(parent, &mid_key, pslot);
2056 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05002057 }
Chris Masonbb803952007-03-01 12:04:21 -05002058
Chris Mason79f95c82007-03-01 15:16:26 -05002059 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04002060 if (left) {
2061 if (btrfs_header_nritems(left) > orig_slot) {
2062 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04002063 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04002064 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05002065 path->slots[level + 1] -= 1;
2066 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002067 if (mid) {
2068 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002069 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002070 }
Chris Masonbb803952007-03-01 12:04:21 -05002071 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002072 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05002073 path->slots[level] = orig_slot;
2074 }
2075 }
Chris Mason79f95c82007-03-01 15:16:26 -05002076 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04002077 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04002078 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05002079 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04002080enospc:
Chris Mason925baed2008-06-25 16:01:30 -04002081 if (right) {
2082 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002083 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04002084 }
2085 if (left) {
2086 if (path->nodes[level] != left)
2087 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002088 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04002089 }
Chris Masonbb803952007-03-01 12:04:21 -05002090 return ret;
2091}
2092
Chris Masond352ac62008-09-29 15:18:18 -04002093/* Node balancing for insertion. Here we only split or push nodes around
2094 * when they are completely full. This is also done top down, so we
2095 * have to be pessimistic.
2096 */
Chris Masond3977122009-01-05 21:25:51 -05002097static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002098 struct btrfs_root *root,
2099 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04002100{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002101 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002102 struct extent_buffer *right = NULL;
2103 struct extent_buffer *mid;
2104 struct extent_buffer *left = NULL;
2105 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002106 int ret = 0;
2107 int wret;
2108 int pslot;
2109 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04002110
2111 if (level == 0)
2112 return 1;
2113
Chris Mason5f39d392007-10-15 16:14:19 -04002114 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002115 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04002116
Li Zefana05a9bb2011-09-06 16:55:34 +08002117 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002118 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002119 pslot = path->slots[level + 1];
2120 }
Chris Masone66f7092007-04-20 13:16:02 -04002121
Chris Mason5f39d392007-10-15 16:14:19 -04002122 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002123 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002124
Chris Mason5f39d392007-10-15 16:14:19 -04002125 left = read_node_slot(root, parent, pslot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002126 if (IS_ERR(left))
2127 left = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002128
2129 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002130 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002131 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002132
2133 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002134 btrfs_set_lock_blocking(left);
2135
Chris Mason5f39d392007-10-15 16:14:19 -04002136 left_nr = btrfs_header_nritems(left);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002137 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002138 wret = 1;
2139 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002140 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002141 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002142 if (ret)
2143 wret = 1;
2144 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002145 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002146 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002147 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002148 }
Chris Masone66f7092007-04-20 13:16:02 -04002149 if (wret < 0)
2150 ret = wret;
2151 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002152 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002153 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002154 btrfs_node_key(mid, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002155 tree_mod_log_set_node_key(fs_info, parent, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002156 btrfs_set_node_key(parent, &disk_key, pslot);
2157 btrfs_mark_buffer_dirty(parent);
2158 if (btrfs_header_nritems(left) > orig_slot) {
2159 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002160 path->slots[level + 1] -= 1;
2161 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002162 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002163 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002164 } else {
2165 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002166 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002167 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002168 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002169 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002170 }
Chris Masone66f7092007-04-20 13:16:02 -04002171 return 0;
2172 }
Chris Mason925baed2008-06-25 16:01:30 -04002173 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002174 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002175 }
Chris Mason925baed2008-06-25 16:01:30 -04002176 right = read_node_slot(root, parent, pslot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07002177 if (IS_ERR(right))
2178 right = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04002179
2180 /*
2181 * then try to empty the right most buffer into the middle
2182 */
Chris Mason5f39d392007-10-15 16:14:19 -04002183 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002184 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002185
Chris Mason925baed2008-06-25 16:01:30 -04002186 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002187 btrfs_set_lock_blocking(right);
2188
Chris Mason5f39d392007-10-15 16:14:19 -04002189 right_nr = btrfs_header_nritems(right);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002191 wret = 1;
2192 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002193 ret = btrfs_cow_block(trans, root, right,
2194 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002195 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002196 if (ret)
2197 wret = 1;
2198 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002199 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002200 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002201 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002202 }
Chris Masone66f7092007-04-20 13:16:02 -04002203 if (wret < 0)
2204 ret = wret;
2205 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002206 struct btrfs_disk_key disk_key;
2207
2208 btrfs_node_key(right, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002209 tree_mod_log_set_node_key(fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002210 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002211 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2212 btrfs_mark_buffer_dirty(parent);
2213
2214 if (btrfs_header_nritems(mid) <= orig_slot) {
2215 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002216 path->slots[level + 1] += 1;
2217 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002218 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002219 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002220 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002221 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002222 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002223 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002224 }
Chris Masone66f7092007-04-20 13:16:02 -04002225 return 0;
2226 }
Chris Mason925baed2008-06-25 16:01:30 -04002227 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002228 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002229 }
Chris Masone66f7092007-04-20 13:16:02 -04002230 return 1;
2231}
2232
Chris Mason74123bd2007-02-02 11:05:29 -05002233/*
Chris Masond352ac62008-09-29 15:18:18 -04002234 * readahead one full node of leaves, finding things that are close
2235 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002236 */
Chris Masonc8c42862009-04-03 10:14:18 -04002237static void reada_for_search(struct btrfs_root *root,
2238 struct btrfs_path *path,
2239 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002240{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002241 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002242 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002243 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002244 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002245 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002246 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002247 u64 nread = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002248 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002249 u32 nr;
2250 u32 blocksize;
2251 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002252
Chris Masona6b6e752007-10-15 16:22:39 -04002253 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002254 return;
2255
Chris Mason6702ed42007-08-07 16:15:09 -04002256 if (!path->nodes[level])
2257 return;
2258
Chris Mason5f39d392007-10-15 16:14:19 -04002259 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002260
Chris Mason3c69fae2007-08-07 15:52:22 -04002261 search = btrfs_node_blockptr(node, slot);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002262 blocksize = fs_info->nodesize;
2263 eb = find_extent_buffer(fs_info, search);
Chris Mason5f39d392007-10-15 16:14:19 -04002264 if (eb) {
2265 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002266 return;
2267 }
2268
Chris Masona7175312009-01-22 09:23:10 -05002269 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002270
Chris Mason5f39d392007-10-15 16:14:19 -04002271 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002272 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002273
Chris Masond3977122009-01-05 21:25:51 -05002274 while (1) {
David Sterbae4058b52015-11-27 16:31:35 +01002275 if (path->reada == READA_BACK) {
Chris Mason6b800532007-10-15 16:17:34 -04002276 if (nr == 0)
2277 break;
2278 nr--;
David Sterbae4058b52015-11-27 16:31:35 +01002279 } else if (path->reada == READA_FORWARD) {
Chris Mason6b800532007-10-15 16:17:34 -04002280 nr++;
2281 if (nr >= nritems)
2282 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002283 }
David Sterbae4058b52015-11-27 16:31:35 +01002284 if (path->reada == READA_BACK && objectid) {
Chris Mason01f46652007-12-21 16:24:26 -05002285 btrfs_node_key(node, &disk_key, nr);
2286 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2287 break;
2288 }
Chris Mason6b800532007-10-15 16:17:34 -04002289 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002290 if ((search <= target && target - search <= 65536) ||
2291 (search > target && search - target <= 65536)) {
David Sterbad3e46fe2014-06-15 02:04:19 +02002292 readahead_tree_block(root, search);
Chris Mason6b800532007-10-15 16:17:34 -04002293 nread += blocksize;
2294 }
2295 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002296 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002297 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002298 }
2299}
Chris Mason925baed2008-06-25 16:01:30 -04002300
Josef Bacik0b088512013-06-17 14:23:02 -04002301static noinline void reada_for_balance(struct btrfs_root *root,
2302 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002303{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002304 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002305 int slot;
2306 int nritems;
2307 struct extent_buffer *parent;
2308 struct extent_buffer *eb;
2309 u64 gen;
2310 u64 block1 = 0;
2311 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002312
Chris Mason8c594ea2009-04-20 15:50:10 -04002313 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002314 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002315 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002316
2317 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002318 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002319
2320 if (slot > 0) {
2321 block1 = btrfs_node_blockptr(parent, slot - 1);
2322 gen = btrfs_node_ptr_generation(parent, slot - 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002323 eb = find_extent_buffer(fs_info, block1);
Chris Masonb9fab912012-05-06 07:23:47 -04002324 /*
2325 * if we get -eagain from btrfs_buffer_uptodate, we
2326 * don't want to return eagain here. That will loop
2327 * forever
2328 */
2329 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002330 block1 = 0;
2331 free_extent_buffer(eb);
2332 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002333 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002334 block2 = btrfs_node_blockptr(parent, slot + 1);
2335 gen = btrfs_node_ptr_generation(parent, slot + 1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002336 eb = find_extent_buffer(fs_info, block2);
Chris Masonb9fab912012-05-06 07:23:47 -04002337 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002338 block2 = 0;
2339 free_extent_buffer(eb);
2340 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002341
Josef Bacik0b088512013-06-17 14:23:02 -04002342 if (block1)
David Sterbad3e46fe2014-06-15 02:04:19 +02002343 readahead_tree_block(root, block1);
Josef Bacik0b088512013-06-17 14:23:02 -04002344 if (block2)
David Sterbad3e46fe2014-06-15 02:04:19 +02002345 readahead_tree_block(root, block2);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002346}
2347
2348
2349/*
Chris Masond3977122009-01-05 21:25:51 -05002350 * when we walk down the tree, it is usually safe to unlock the higher layers
2351 * in the tree. The exceptions are when our path goes through slot 0, because
2352 * operations on the tree might require changing key pointers higher up in the
2353 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002354 *
Chris Masond3977122009-01-05 21:25:51 -05002355 * callers might also have set path->keep_locks, which tells this code to keep
2356 * the lock if the path points to the last slot in the block. This is part of
2357 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002358 *
Chris Masond3977122009-01-05 21:25:51 -05002359 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2360 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002361 */
Chris Masone02119d2008-09-05 16:13:11 -04002362static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002363 int lowest_unlock, int min_write_lock_level,
2364 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002365{
2366 int i;
2367 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002368 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002369 struct extent_buffer *t;
2370
2371 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2372 if (!path->nodes[i])
2373 break;
2374 if (!path->locks[i])
2375 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002376 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002377 skip_level = i + 1;
2378 continue;
2379 }
Chris Mason051e1b92008-06-25 16:01:30 -04002380 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002381 u32 nritems;
2382 t = path->nodes[i];
2383 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002384 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002385 skip_level = i + 1;
2386 continue;
2387 }
2388 }
Chris Mason051e1b92008-06-25 16:01:30 -04002389 if (skip_level < i && i >= lowest_unlock)
2390 no_skips = 1;
2391
Chris Mason925baed2008-06-25 16:01:30 -04002392 t = path->nodes[i];
2393 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002394 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002395 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002396 if (write_lock_level &&
2397 i > min_write_lock_level &&
2398 i <= *write_lock_level) {
2399 *write_lock_level = i - 1;
2400 }
Chris Mason925baed2008-06-25 16:01:30 -04002401 }
2402 }
2403}
2404
Chris Mason3c69fae2007-08-07 15:52:22 -04002405/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002406 * This releases any locks held in the path starting at level and
2407 * going all the way up to the root.
2408 *
2409 * btrfs_search_slot will keep the lock held on higher nodes in a few
2410 * corner cases, such as COW of the block at slot zero in the node. This
2411 * ignores those rules, and it should only be called when there are no
2412 * more updates to be done higher up in the tree.
2413 */
2414noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2415{
2416 int i;
2417
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002418 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002419 return;
2420
2421 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2422 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002423 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002424 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002425 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002426 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002427 path->locks[i] = 0;
2428 }
2429}
2430
2431/*
Chris Masonc8c42862009-04-03 10:14:18 -04002432 * helper function for btrfs_search_slot. The goal is to find a block
2433 * in cache without setting the path to blocking. If we find the block
2434 * we return zero and the path is unchanged.
2435 *
2436 * If we can't find the block, we set the path blocking and do some
2437 * reada. -EAGAIN is returned and the search must be repeated.
2438 */
2439static int
2440read_block_for_search(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root, struct btrfs_path *p,
2442 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002443 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002445 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002446 u64 blocknr;
2447 u64 gen;
Chris Masonc8c42862009-04-03 10:14:18 -04002448 struct extent_buffer *b = *eb_ret;
2449 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002450 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002451
2452 blocknr = btrfs_node_blockptr(b, slot);
2453 gen = btrfs_node_ptr_generation(b, slot);
Chris Masonc8c42862009-04-03 10:14:18 -04002454
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002455 tmp = find_extent_buffer(fs_info, blocknr);
Chris Masoncb449212010-10-24 11:01:27 -04002456 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002457 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002458 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2459 *eb_ret = tmp;
2460 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002461 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002462
2463 /* the pages were up to date, but we failed
2464 * the generation number check. Do a full
2465 * read for the generation number that is correct.
2466 * We must do this without dropping locks so
2467 * we can trust our generation number
2468 */
2469 btrfs_set_path_blocking(p);
2470
2471 /* now we're allowed to do a blocking uptodate check */
2472 ret = btrfs_read_buffer(tmp, gen);
2473 if (!ret) {
2474 *eb_ret = tmp;
2475 return 0;
2476 }
2477 free_extent_buffer(tmp);
2478 btrfs_release_path(p);
2479 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002480 }
2481
2482 /*
2483 * reduce lock contention at high levels
2484 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002485 * we read. Don't release the lock on the current
2486 * level because we need to walk this node to figure
2487 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002488 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002489 btrfs_unlock_up_safe(p, level + 1);
2490 btrfs_set_path_blocking(p);
2491
Chris Masoncb449212010-10-24 11:01:27 -04002492 free_extent_buffer(tmp);
David Sterbae4058b52015-11-27 16:31:35 +01002493 if (p->reada != READA_NONE)
Chris Masonc8c42862009-04-03 10:14:18 -04002494 reada_for_search(root, p, level, slot, key->objectid);
2495
David Sterbab3b4aa72011-04-21 01:20:15 +02002496 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002497
2498 ret = -EAGAIN;
David Sterbace86cd52014-06-15 01:07:32 +02002499 tmp = read_tree_block(root, blocknr, 0);
Liu Bo64c043d2015-05-25 17:30:15 +08002500 if (!IS_ERR(tmp)) {
Chris Mason76a05b32009-05-14 13:24:30 -04002501 /*
2502 * If the read above didn't mark this buffer up to date,
2503 * it will never end up being up to date. Set ret to EIO now
2504 * and give up so that our caller doesn't loop forever
2505 * on our EAGAINs.
2506 */
Chris Masonb9fab912012-05-06 07:23:47 -04002507 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002508 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002509 free_extent_buffer(tmp);
Liu Boc871b0f2016-06-06 12:01:23 -07002510 } else {
2511 ret = PTR_ERR(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002512 }
2513 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002514}
2515
2516/*
2517 * helper function for btrfs_search_slot. This does all of the checks
2518 * for node-level blocks and does any balancing required based on
2519 * the ins_len.
2520 *
2521 * If no extra work was required, zero is returned. If we had to
2522 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2523 * start over
2524 */
2525static int
2526setup_nodes_for_search(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002528 struct extent_buffer *b, int level, int ins_len,
2529 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002530{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002531 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonc8c42862009-04-03 10:14:18 -04002532 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002533
Chris Masonc8c42862009-04-03 10:14:18 -04002534 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002535 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
Chris Masonc8c42862009-04-03 10:14:18 -04002536 int sret;
2537
Chris Masonbd681512011-07-16 15:23:14 -04002538 if (*write_lock_level < level + 1) {
2539 *write_lock_level = level + 1;
2540 btrfs_release_path(p);
2541 goto again;
2542 }
2543
Chris Masonc8c42862009-04-03 10:14:18 -04002544 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002545 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002546 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002547 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002548
2549 BUG_ON(sret > 0);
2550 if (sret) {
2551 ret = sret;
2552 goto done;
2553 }
2554 b = p->nodes[level];
2555 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002556 BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002557 int sret;
2558
Chris Masonbd681512011-07-16 15:23:14 -04002559 if (*write_lock_level < level + 1) {
2560 *write_lock_level = level + 1;
2561 btrfs_release_path(p);
2562 goto again;
2563 }
2564
Chris Masonc8c42862009-04-03 10:14:18 -04002565 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002566 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002567 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002568 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002569
2570 if (sret) {
2571 ret = sret;
2572 goto done;
2573 }
2574 b = p->nodes[level];
2575 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002576 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002577 goto again;
2578 }
2579 BUG_ON(btrfs_header_nritems(b) == 1);
2580 }
2581 return 0;
2582
2583again:
2584 ret = -EAGAIN;
2585done:
2586 return ret;
2587}
2588
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002589static void key_search_validate(struct extent_buffer *b,
2590 struct btrfs_key *key,
2591 int level)
2592{
2593#ifdef CONFIG_BTRFS_ASSERT
2594 struct btrfs_disk_key disk_key;
2595
2596 btrfs_cpu_key_to_disk(&disk_key, key);
2597
2598 if (level == 0)
2599 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2600 offsetof(struct btrfs_leaf, items[0].key),
2601 sizeof(disk_key)));
2602 else
2603 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2604 offsetof(struct btrfs_node, ptrs[0].key),
2605 sizeof(disk_key)));
2606#endif
2607}
2608
2609static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2610 int level, int *prev_cmp, int *slot)
2611{
2612 if (*prev_cmp != 0) {
2613 *prev_cmp = bin_search(b, key, level, slot);
2614 return *prev_cmp;
2615 }
2616
2617 key_search_validate(b, key, level);
2618 *slot = 0;
2619
2620 return 0;
2621}
2622
David Sterba381cf652015-01-02 18:45:16 +01002623int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002624 u64 iobjectid, u64 ioff, u8 key_type,
2625 struct btrfs_key *found_key)
2626{
2627 int ret;
2628 struct btrfs_key key;
2629 struct extent_buffer *eb;
David Sterba381cf652015-01-02 18:45:16 +01002630
2631 ASSERT(path);
David Sterba1d4c08e2015-01-02 19:36:14 +01002632 ASSERT(found_key);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002633
2634 key.type = key_type;
2635 key.objectid = iobjectid;
2636 key.offset = ioff;
2637
2638 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
David Sterba1d4c08e2015-01-02 19:36:14 +01002639 if (ret < 0)
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002640 return ret;
2641
2642 eb = path->nodes[0];
2643 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2644 ret = btrfs_next_leaf(fs_root, path);
2645 if (ret)
2646 return ret;
2647 eb = path->nodes[0];
2648 }
2649
2650 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2651 if (found_key->type != key.type ||
2652 found_key->objectid != key.objectid)
2653 return 1;
2654
2655 return 0;
2656}
2657
Chris Masonc8c42862009-04-03 10:14:18 -04002658/*
Chris Mason74123bd2007-02-02 11:05:29 -05002659 * look for key in the tree. path is filled in with nodes along the way
2660 * if key is found, we return zero and you can find the item in the leaf
2661 * level of the path (level 0)
2662 *
2663 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002664 * be inserted, and 1 is returned. If there are other errors during the
2665 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002666 *
2667 * if ins_len > 0, nodes and leaves will be split as we walk down the
2668 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2669 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002670 */
Chris Masone089f052007-03-16 16:20:31 -04002671int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2672 *root, struct btrfs_key *key, struct btrfs_path *p, int
2673 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002674{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04002676 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002677 int slot;
2678 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002679 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002681 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002682 int root_lock;
2683 /* everything at write_lock_level or lower must be write locked */
2684 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002685 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002686 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002687 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002688
Chris Mason6702ed42007-08-07 16:15:09 -04002689 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002690 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002691 WARN_ON(p->nodes[0] != NULL);
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002692 BUG_ON(!cow && ins_len);
Josef Bacik25179202008-10-29 14:49:05 -04002693
Chris Masonbd681512011-07-16 15:23:14 -04002694 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002695 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002696
Chris Masonbd681512011-07-16 15:23:14 -04002697 /* when we are removing items, we might have to go up to level
2698 * two as we update tree pointers Make sure we keep write
2699 * for those levels as well
2700 */
2701 write_lock_level = 2;
2702 } else if (ins_len > 0) {
2703 /*
2704 * for inserting items, make sure we have a write lock on
2705 * level 1 so we can update keys
2706 */
2707 write_lock_level = 1;
2708 }
2709
2710 if (!cow)
2711 write_lock_level = -1;
2712
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002713 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002714 write_lock_level = BTRFS_MAX_LEVEL;
2715
Chris Masonf7c79f32012-03-19 15:54:38 -04002716 min_write_lock_level = write_lock_level;
2717
Chris Masonbb803952007-03-01 12:04:21 -05002718again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002719 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002720 /*
2721 * we try very hard to do read locks on the root
2722 */
2723 root_lock = BTRFS_READ_LOCK;
2724 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002725 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002726 /*
2727 * the commit roots are read only
2728 * so we always do read locks
2729 */
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002730 if (p->need_commit_sem)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002731 down_read(&fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002732 b = root->commit_root;
2733 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002734 level = btrfs_header_level(b);
Josef Bacik3f8a18c2014-03-28 17:16:01 -04002735 if (p->need_commit_sem)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002736 up_read(&fs_info->commit_root_sem);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002737 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002738 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002739 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002740 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002741 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002742 level = btrfs_header_level(b);
2743 } else {
2744 /* we don't know the level of the root node
2745 * until we actually have it read locked
2746 */
2747 b = btrfs_read_lock_root_node(root);
2748 level = btrfs_header_level(b);
2749 if (level <= write_lock_level) {
2750 /* whoops, must trade for write lock */
2751 btrfs_tree_read_unlock(b);
2752 free_extent_buffer(b);
2753 b = btrfs_lock_root_node(root);
2754 root_lock = BTRFS_WRITE_LOCK;
2755
2756 /* the level might have changed, check again */
2757 level = btrfs_header_level(b);
2758 }
2759 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002760 }
Chris Masonbd681512011-07-16 15:23:14 -04002761 p->nodes[level] = b;
2762 if (!p->skip_locking)
2763 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002764
Chris Masoneb60cea2007-02-02 09:18:22 -05002765 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002766 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002767
2768 /*
2769 * setup the path here so we can release it under lock
2770 * contention with the cow code
2771 */
Chris Mason02217ed2007-03-02 16:08:05 -05002772 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002773 /*
2774 * if we don't really need to cow this block
2775 * then we don't want to set the path blocking,
2776 * so we test it here
2777 */
Jeff Mahoney64c12922016-06-08 00:36:38 -04002778 if (!should_cow_block(trans, root, b)) {
2779 trans->dirty = true;
Chris Mason65b51a02008-08-01 15:11:20 -04002780 goto cow_done;
Jeff Mahoney64c12922016-06-08 00:36:38 -04002781 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782
Chris Masonbd681512011-07-16 15:23:14 -04002783 /*
2784 * must have write locks on this node and the
2785 * parent
2786 */
Josef Bacik5124e002012-11-07 13:44:13 -05002787 if (level > write_lock_level ||
2788 (level + 1 > write_lock_level &&
2789 level + 1 < BTRFS_MAX_LEVEL &&
2790 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002791 write_lock_level = level + 1;
2792 btrfs_release_path(p);
2793 goto again;
2794 }
2795
Filipe Manana160f4082014-07-28 19:37:17 +01002796 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002797 err = btrfs_cow_block(trans, root, b,
2798 p->nodes[level + 1],
2799 p->slots[level + 1], &b);
2800 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002801 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002802 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002803 }
Chris Mason02217ed2007-03-02 16:08:05 -05002804 }
Chris Mason65b51a02008-08-01 15:11:20 -04002805cow_done:
Chris Masoneb60cea2007-02-02 09:18:22 -05002806 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002807 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002808
2809 /*
2810 * we have a lock on b and as long as we aren't changing
2811 * the tree, there is no way to for the items in b to change.
2812 * It is safe to drop the lock on our parent before we
2813 * go through the expensive btree search on b.
2814 *
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002815 * If we're inserting or deleting (ins_len != 0), then we might
2816 * be changing slot zero, which may require changing the parent.
2817 * So, we can't drop the lock until after we know which slot
2818 * we're operating on.
Chris Masonb4ce94d2009-02-04 09:25:08 -05002819 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002820 if (!ins_len && !p->keep_locks) {
2821 int u = level + 1;
2822
2823 if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
2824 btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
2825 p->locks[u] = 0;
2826 }
2827 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05002828
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002829 ret = key_search(b, key, level, &prev_cmp, &slot);
Liu Bo415b35a2016-06-17 19:16:21 -07002830 if (ret < 0)
2831 goto done;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002832
Chris Mason5f39d392007-10-15 16:14:19 -04002833 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002834 int dec = 0;
2835 if (ret && slot > 0) {
2836 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002837 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002838 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002839 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002840 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002841 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002842 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002843 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002844 if (err) {
2845 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002846 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002847 }
Chris Masonc8c42862009-04-03 10:14:18 -04002848 b = p->nodes[level];
2849 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002850
Chris Masonbd681512011-07-16 15:23:14 -04002851 /*
2852 * slot 0 is special, if we change the key
2853 * we have to update the parent pointer
2854 * which means we must have a write lock
2855 * on the parent
2856 */
Filipe David Borba Mananaeb653de2013-12-23 11:53:02 +00002857 if (slot == 0 && ins_len &&
Chris Masonbd681512011-07-16 15:23:14 -04002858 write_lock_level < level + 1) {
2859 write_lock_level = level + 1;
2860 btrfs_release_path(p);
2861 goto again;
2862 }
2863
Chris Masonf7c79f32012-03-19 15:54:38 -04002864 unlock_up(p, level, lowest_unlock,
2865 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002866
Chris Mason925baed2008-06-25 16:01:30 -04002867 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002868 if (dec)
2869 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002870 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002871 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002872
Yan Zheng33c66f42009-07-22 09:59:00 -04002873 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002874 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002875 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002876 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002877 if (err) {
2878 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002879 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002880 }
Chris Mason76a05b32009-05-14 13:24:30 -04002881
Chris Masonb4ce94d2009-02-04 09:25:08 -05002882 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002883 level = btrfs_header_level(b);
2884 if (level <= write_lock_level) {
2885 err = btrfs_try_tree_write_lock(b);
2886 if (!err) {
2887 btrfs_set_path_blocking(p);
2888 btrfs_tree_lock(b);
2889 btrfs_clear_path_blocking(p, b,
2890 BTRFS_WRITE_LOCK);
2891 }
2892 p->locks[level] = BTRFS_WRITE_LOCK;
2893 } else {
Chris Masonf82c4582014-11-19 10:25:09 -08002894 err = btrfs_tree_read_lock_atomic(b);
Chris Masonbd681512011-07-16 15:23:14 -04002895 if (!err) {
2896 btrfs_set_path_blocking(p);
2897 btrfs_tree_read_lock(b);
2898 btrfs_clear_path_blocking(p, b,
2899 BTRFS_READ_LOCK);
2900 }
2901 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002902 }
Chris Masonbd681512011-07-16 15:23:14 -04002903 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002904 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002905 } else {
2906 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002907 if (ins_len > 0 &&
2908 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002909 if (write_lock_level < 1) {
2910 write_lock_level = 1;
2911 btrfs_release_path(p);
2912 goto again;
2913 }
2914
Chris Masonb4ce94d2009-02-04 09:25:08 -05002915 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002916 err = split_leaf(trans, root, key,
2917 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002918 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002919
Yan Zheng33c66f42009-07-22 09:59:00 -04002920 BUG_ON(err > 0);
2921 if (err) {
2922 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002923 goto done;
2924 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002925 }
Chris Mason459931e2008-12-10 09:10:46 -05002926 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002927 unlock_up(p, level, lowest_unlock,
2928 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002929 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002930 }
2931 }
Chris Mason65b51a02008-08-01 15:11:20 -04002932 ret = 1;
2933done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002934 /*
2935 * we don't really know what they plan on doing with the path
2936 * from here on, so for now just mark it as blocking
2937 */
Chris Masonb9473432009-03-13 11:00:37 -04002938 if (!p->leave_spinning)
2939 btrfs_set_path_blocking(p);
Filipe Manana5f5bc6b2014-11-09 08:38:39 +00002940 if (ret < 0 && !p->skip_release_on_error)
David Sterbab3b4aa72011-04-21 01:20:15 +02002941 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002942 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002943}
2944
Chris Mason74123bd2007-02-02 11:05:29 -05002945/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002946 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2947 * current state of the tree together with the operations recorded in the tree
2948 * modification log to search for the key in a previous version of this tree, as
2949 * denoted by the time_seq parameter.
2950 *
2951 * Naturally, there is no support for insert, delete or cow operations.
2952 *
2953 * The resulting path and return value will be set up as if we called
2954 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2955 */
2956int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2957 struct btrfs_path *p, u64 time_seq)
2958{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002959 struct btrfs_fs_info *fs_info = root->fs_info;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002960 struct extent_buffer *b;
2961 int slot;
2962 int ret;
2963 int err;
2964 int level;
2965 int lowest_unlock = 1;
2966 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002967 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002968
2969 lowest_level = p->lowest_level;
2970 WARN_ON(p->nodes[0] != NULL);
2971
2972 if (p->search_commit_root) {
2973 BUG_ON(time_seq);
2974 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2975 }
2976
2977again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002978 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002979 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002980 p->locks[level] = BTRFS_READ_LOCK;
2981
2982 while (b) {
2983 level = btrfs_header_level(b);
2984 p->nodes[level] = b;
2985 btrfs_clear_path_blocking(p, NULL, 0);
2986
2987 /*
2988 * we have a lock on b and as long as we aren't changing
2989 * the tree, there is no way to for the items in b to change.
2990 * It is safe to drop the lock on our parent before we
2991 * go through the expensive btree search on b.
2992 */
2993 btrfs_unlock_up_safe(p, level + 1);
2994
Josef Bacikd4b40872013-09-24 14:09:34 -04002995 /*
Nicholas D Steeves01327612016-05-19 21:18:45 -04002996 * Since we can unwind ebs we want to do a real search every
Josef Bacikd4b40872013-09-24 14:09:34 -04002997 * time.
2998 */
2999 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01003000 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003001
3002 if (level != 0) {
3003 int dec = 0;
3004 if (ret && slot > 0) {
3005 dec = 1;
3006 slot -= 1;
3007 }
3008 p->slots[level] = slot;
3009 unlock_up(p, level, lowest_unlock, 0, NULL);
3010
3011 if (level == lowest_level) {
3012 if (dec)
3013 p->slots[level]++;
3014 goto done;
3015 }
3016
3017 err = read_block_for_search(NULL, root, p, &b, level,
3018 slot, key, time_seq);
3019 if (err == -EAGAIN)
3020 goto again;
3021 if (err) {
3022 ret = err;
3023 goto done;
3024 }
3025
3026 level = btrfs_header_level(b);
Chris Masonf82c4582014-11-19 10:25:09 -08003027 err = btrfs_tree_read_lock_atomic(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003028 if (!err) {
3029 btrfs_set_path_blocking(p);
3030 btrfs_tree_read_lock(b);
3031 btrfs_clear_path_blocking(p, b,
3032 BTRFS_READ_LOCK);
3033 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003034 b = tree_mod_log_rewind(fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04003035 if (!b) {
3036 ret = -ENOMEM;
3037 goto done;
3038 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003039 p->locks[level] = BTRFS_READ_LOCK;
3040 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02003041 } else {
3042 p->slots[level] = slot;
3043 unlock_up(p, level, lowest_unlock, 0, NULL);
3044 goto done;
3045 }
3046 }
3047 ret = 1;
3048done:
3049 if (!p->leave_spinning)
3050 btrfs_set_path_blocking(p);
3051 if (ret < 0)
3052 btrfs_release_path(p);
3053
3054 return ret;
3055}
3056
3057/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003058 * helper to use instead of search slot if no exact match is needed but
3059 * instead the next or previous item should be returned.
3060 * When find_higher is true, the next higher item is returned, the next lower
3061 * otherwise.
3062 * When return_any and find_higher are both true, and no higher item is found,
3063 * return the next lower instead.
3064 * When return_any is true and find_higher is false, and no lower item is found,
3065 * return the next higher instead.
3066 * It returns 0 if any item is found, 1 if none is found (tree empty), and
3067 * < 0 on error
3068 */
3069int btrfs_search_slot_for_read(struct btrfs_root *root,
3070 struct btrfs_key *key, struct btrfs_path *p,
3071 int find_higher, int return_any)
3072{
3073 int ret;
3074 struct extent_buffer *leaf;
3075
3076again:
3077 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
3078 if (ret <= 0)
3079 return ret;
3080 /*
3081 * a return value of 1 means the path is at the position where the
3082 * item should be inserted. Normally this is the next bigger item,
3083 * but in case the previous item is the last in a leaf, path points
3084 * to the first free slot in the previous leaf, i.e. at an invalid
3085 * item.
3086 */
3087 leaf = p->nodes[0];
3088
3089 if (find_higher) {
3090 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
3091 ret = btrfs_next_leaf(root, p);
3092 if (ret <= 0)
3093 return ret;
3094 if (!return_any)
3095 return 1;
3096 /*
3097 * no higher item found, return the next
3098 * lower instead
3099 */
3100 return_any = 0;
3101 find_higher = 0;
3102 btrfs_release_path(p);
3103 goto again;
3104 }
3105 } else {
Arne Jansene6793762011-09-13 11:18:10 +02003106 if (p->slots[0] == 0) {
3107 ret = btrfs_prev_leaf(root, p);
3108 if (ret < 0)
3109 return ret;
3110 if (!ret) {
Filipe David Borba Manana23c6bf62014-01-11 21:28:54 +00003111 leaf = p->nodes[0];
3112 if (p->slots[0] == btrfs_header_nritems(leaf))
3113 p->slots[0]--;
Arne Jansene6793762011-09-13 11:18:10 +02003114 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003115 }
Arne Jansene6793762011-09-13 11:18:10 +02003116 if (!return_any)
3117 return 1;
3118 /*
3119 * no lower item found, return the next
3120 * higher instead
3121 */
3122 return_any = 0;
3123 find_higher = 1;
3124 btrfs_release_path(p);
3125 goto again;
3126 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02003127 --p->slots[0];
3128 }
3129 }
3130 return 0;
3131}
3132
3133/*
Chris Mason74123bd2007-02-02 11:05:29 -05003134 * adjust the pointers going up the tree, starting at level
3135 * making sure the right key of each node is points to 'key'.
3136 * This is used after shifting pointers to the left, so it stops
3137 * fixing up pointers when a given leaf/node is not in slot 0 of the
3138 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05003139 *
Chris Mason74123bd2007-02-02 11:05:29 -05003140 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003141static void fixup_low_keys(struct btrfs_fs_info *fs_info,
3142 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003143 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003144{
3145 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04003146 struct extent_buffer *t;
3147
Chris Mason234b63a2007-03-13 10:46:10 -04003148 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05003149 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05003150 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05003151 break;
Chris Mason5f39d392007-10-15 16:14:19 -04003152 t = path->nodes[i];
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003153 tree_mod_log_set_node_key(fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003154 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04003155 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003156 if (tslot != 0)
3157 break;
3158 }
3159}
3160
Chris Mason74123bd2007-02-02 11:05:29 -05003161/*
Zheng Yan31840ae2008-09-23 13:14:14 -04003162 * update item key.
3163 *
3164 * This function isn't completely safe. It's the caller's responsibility
3165 * that the new key won't break the order
3166 */
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003167void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
3168 struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003169 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003170{
3171 struct btrfs_disk_key disk_key;
3172 struct extent_buffer *eb;
3173 int slot;
3174
3175 eb = path->nodes[0];
3176 slot = path->slots[0];
3177 if (slot > 0) {
3178 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003179 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003180 }
3181 if (slot < btrfs_header_nritems(eb) - 1) {
3182 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003183 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003184 }
3185
3186 btrfs_cpu_key_to_disk(&disk_key, new_key);
3187 btrfs_set_item_key(eb, &disk_key, slot);
3188 btrfs_mark_buffer_dirty(eb);
3189 if (slot == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09003190 fixup_low_keys(fs_info, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003191}
3192
3193/*
Chris Mason74123bd2007-02-02 11:05:29 -05003194 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003195 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003196 *
3197 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3198 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003199 */
Chris Mason98ed5172008-01-03 10:01:48 -05003200static int push_node_left(struct btrfs_trans_handle *trans,
3201 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003202 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003203{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003204 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003205 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003206 int src_nritems;
3207 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003208 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003209
Chris Mason5f39d392007-10-15 16:14:19 -04003210 src_nritems = btrfs_header_nritems(src);
3211 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003212 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003213 WARN_ON(btrfs_header_generation(src) != trans->transid);
3214 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003215
Chris Masonbce4eae2008-04-24 14:42:46 -04003216 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003217 return 1;
3218
Chris Masond3977122009-01-05 21:25:51 -05003219 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003220 return 1;
3221
Chris Masonbce4eae2008-04-24 14:42:46 -04003222 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003223 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003224 if (push_items < src_nritems) {
3225 /* leave at least 8 pointers in the node if
3226 * we aren't going to empty it
3227 */
3228 if (src_nritems - push_items < 8) {
3229 if (push_items <= 8)
3230 return 1;
3231 push_items -= 8;
3232 }
3233 }
3234 } else
3235 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003236
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003237 ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003238 push_items);
3239 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003240 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003241 return ret;
3242 }
Chris Mason5f39d392007-10-15 16:14:19 -04003243 copy_extent_buffer(dst, src,
3244 btrfs_node_key_ptr_offset(dst_nritems),
3245 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003246 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003247
Chris Masonbb803952007-03-01 12:04:21 -05003248 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003249 /*
3250 * don't call tree_mod_log_eb_move here, key removal was already
3251 * fully logged by tree_mod_log_eb_copy above.
3252 */
Chris Mason5f39d392007-10-15 16:14:19 -04003253 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3254 btrfs_node_key_ptr_offset(push_items),
3255 (src_nritems - push_items) *
3256 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003257 }
Chris Mason5f39d392007-10-15 16:14:19 -04003258 btrfs_set_header_nritems(src, src_nritems - push_items);
3259 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3260 btrfs_mark_buffer_dirty(src);
3261 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003262
Chris Masonbb803952007-03-01 12:04:21 -05003263 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264}
3265
Chris Mason97571fd2007-02-24 13:39:08 -05003266/*
Chris Mason79f95c82007-03-01 15:16:26 -05003267 * try to push data from one node into the next node right in the
3268 * tree.
3269 *
3270 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3271 * error, and > 0 if there was no room in the right hand block.
3272 *
3273 * this will only push up to 1/2 the contents of the left node over
3274 */
Chris Mason5f39d392007-10-15 16:14:19 -04003275static int balance_node_right(struct btrfs_trans_handle *trans,
3276 struct btrfs_root *root,
3277 struct extent_buffer *dst,
3278 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003279{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003280 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason79f95c82007-03-01 15:16:26 -05003281 int push_items = 0;
3282 int max_push;
3283 int src_nritems;
3284 int dst_nritems;
3285 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003286
Chris Mason7bb86312007-12-11 09:25:06 -05003287 WARN_ON(btrfs_header_generation(src) != trans->transid);
3288 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3289
Chris Mason5f39d392007-10-15 16:14:19 -04003290 src_nritems = btrfs_header_nritems(src);
3291 dst_nritems = btrfs_header_nritems(dst);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003292 push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003293 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003294 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003295
Chris Masond3977122009-01-05 21:25:51 -05003296 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003297 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003298
3299 max_push = src_nritems / 2 + 1;
3300 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003301 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003302 return 1;
Yan252c38f2007-08-29 09:11:44 -04003303
Chris Mason79f95c82007-03-01 15:16:26 -05003304 if (max_push < push_items)
3305 push_items = max_push;
3306
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 tree_mod_log_eb_move(fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003308 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3309 btrfs_node_key_ptr_offset(0),
3310 (dst_nritems) *
3311 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003312
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003313 ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003314 src_nritems - push_items, push_items);
3315 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003316 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003317 return ret;
3318 }
Chris Mason5f39d392007-10-15 16:14:19 -04003319 copy_extent_buffer(dst, src,
3320 btrfs_node_key_ptr_offset(0),
3321 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003322 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003323
Chris Mason5f39d392007-10-15 16:14:19 -04003324 btrfs_set_header_nritems(src, src_nritems - push_items);
3325 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003326
Chris Mason5f39d392007-10-15 16:14:19 -04003327 btrfs_mark_buffer_dirty(src);
3328 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003329
Chris Mason79f95c82007-03-01 15:16:26 -05003330 return ret;
3331}
3332
3333/*
Chris Mason97571fd2007-02-24 13:39:08 -05003334 * helper function to insert a new root level in the tree.
3335 * A new node is allocated, and a single item is inserted to
3336 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003337 *
3338 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003339 */
Chris Masond3977122009-01-05 21:25:51 -05003340static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003341 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003342 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003343{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003344 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason7bb86312007-12-11 09:25:06 -05003345 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003346 struct extent_buffer *lower;
3347 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003348 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003350
3351 BUG_ON(path->nodes[level]);
3352 BUG_ON(path->nodes[level-1] != root->node);
3353
Chris Mason7bb86312007-12-11 09:25:06 -05003354 lower = path->nodes[level-1];
3355 if (level == 1)
3356 btrfs_item_key(lower, &lower_key, 0);
3357 else
3358 btrfs_node_key(lower, &lower_key, 0);
3359
David Sterba4d75f8a2014-06-15 01:54:12 +02003360 c = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3361 &lower_key, level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003362 if (IS_ERR(c))
3363 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003364
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003365 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003366
David Sterbab159fa22016-11-08 18:09:03 +01003367 memzero_extent_buffer(c, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003368 btrfs_set_header_nritems(c, 1);
3369 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003370 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003371 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003372 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003373 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003374
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 write_extent_buffer_fsid(c, fs_info->fsid);
3376 write_extent_buffer_chunk_tree_uuid(c, fs_info->chunk_tree_uuid);
Chris Masone17cade2008-04-15 15:41:47 -04003377
Chris Mason5f39d392007-10-15 16:14:19 -04003378 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003379 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003380 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003381 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003382
3383 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003384
3385 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003386
Chris Mason925baed2008-06-25 16:01:30 -04003387 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003388 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003389 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003390
3391 /* the super has an extra ref to root->node */
3392 free_extent_buffer(old);
3393
Chris Mason0b86a832008-03-24 15:01:56 -04003394 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003395 extent_buffer_get(c);
3396 path->nodes[level] = c;
chandan95449a12015-01-15 12:22:03 +05303397 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Mason5c680ed2007-02-22 11:39:13 -05003398 path->slots[level] = 0;
3399 return 0;
3400}
3401
Chris Mason74123bd2007-02-02 11:05:29 -05003402/*
3403 * worker function to insert a single pointer in a node.
3404 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003405 *
Chris Mason74123bd2007-02-02 11:05:29 -05003406 * slot and level indicate where you want the key to go, and
3407 * blocknr is the block the key points to.
3408 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003409static void insert_ptr(struct btrfs_trans_handle *trans,
3410 struct btrfs_root *root, struct btrfs_path *path,
3411 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003412 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003414 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003415 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003416 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003417 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003418
3419 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003420 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003421 lower = path->nodes[level];
3422 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003423 BUG_ON(slot > nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003424 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Chris Mason74123bd2007-02-02 11:05:29 -05003425 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003426 if (level)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003427 tree_mod_log_eb_move(fs_info, lower, slot + 1,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003428 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003429 memmove_extent_buffer(lower,
3430 btrfs_node_key_ptr_offset(slot + 1),
3431 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003432 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003433 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003434 if (level) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003435 ret = tree_mod_log_insert_key(fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003436 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003437 BUG_ON(ret < 0);
3438 }
Chris Mason5f39d392007-10-15 16:14:19 -04003439 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003440 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003441 WARN_ON(trans->transid == 0);
3442 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003443 btrfs_set_header_nritems(lower, nritems + 1);
3444 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003445}
3446
Chris Mason97571fd2007-02-24 13:39:08 -05003447/*
3448 * split the node at the specified level in path in two.
3449 * The path is corrected to point to the appropriate node after the split
3450 *
3451 * Before splitting this tries to make some room in the node by pushing
3452 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003453 *
3454 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003455 */
Chris Masone02119d2008-09-05 16:13:11 -04003456static noinline int split_node(struct btrfs_trans_handle *trans,
3457 struct btrfs_root *root,
3458 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003460 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003461 struct extent_buffer *c;
3462 struct extent_buffer *split;
3463 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003464 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003465 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003466 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003467
Chris Mason5f39d392007-10-15 16:14:19 -04003468 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003469 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003470 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003471 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003472 * trying to split the root, lets make a new one
3473 *
Liu Bofdd99c72013-05-22 12:06:51 +00003474 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003475 * insert_new_root, because that root buffer will be kept as a
3476 * normal node. We are going to log removal of half of the
3477 * elements below with tree_mod_log_eb_copy. We're holding a
3478 * tree lock on the buffer, which is why we cannot race with
3479 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003480 */
Liu Bofdd99c72013-05-22 12:06:51 +00003481 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003482 if (ret)
3483 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003484 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003485 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003486 c = path->nodes[level];
3487 if (!ret && btrfs_header_nritems(c) <
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003489 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003490 if (ret < 0)
3491 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003492 }
Chris Masone66f7092007-04-20 13:16:02 -04003493
Chris Mason5f39d392007-10-15 16:14:19 -04003494 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003495 mid = (c_nritems + 1) / 2;
3496 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003497
David Sterba4d75f8a2014-06-15 01:54:12 +02003498 split = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
3499 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003500 if (IS_ERR(split))
3501 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003502
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003503 root_add_used(root, fs_info->nodesize);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003504
David Sterbab159fa22016-11-08 18:09:03 +01003505 memzero_extent_buffer(split, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003506 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003507 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003508 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003509 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003510 btrfs_set_header_owner(split, root->root_key.objectid);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003511 write_extent_buffer_fsid(split, fs_info->fsid);
3512 write_extent_buffer_chunk_tree_uuid(split, fs_info->chunk_tree_uuid);
Chris Mason5f39d392007-10-15 16:14:19 -04003513
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003514 ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003515 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003516 btrfs_abort_transaction(trans, ret);
Filipe David Borba Manana5de865e2013-12-20 15:17:46 +00003517 return ret;
3518 }
Chris Mason5f39d392007-10-15 16:14:19 -04003519 copy_extent_buffer(split, c,
3520 btrfs_node_key_ptr_offset(0),
3521 btrfs_node_key_ptr_offset(mid),
3522 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3523 btrfs_set_header_nritems(split, c_nritems - mid);
3524 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003525 ret = 0;
3526
Chris Mason5f39d392007-10-15 16:14:19 -04003527 btrfs_mark_buffer_dirty(c);
3528 btrfs_mark_buffer_dirty(split);
3529
Jeff Mahoney143bede2012-03-01 14:56:26 +01003530 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003531 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003532
Chris Mason5de08d72007-02-24 06:24:44 -05003533 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003534 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003535 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003536 free_extent_buffer(c);
3537 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003538 path->slots[level + 1] += 1;
3539 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003540 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003541 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003542 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003543 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003544}
3545
Chris Mason74123bd2007-02-02 11:05:29 -05003546/*
3547 * how many bytes are required to store the items in a leaf. start
3548 * and nr indicate which items in the leaf to check. This totals up the
3549 * space used both by the item structs and the item data
3550 */
Chris Mason5f39d392007-10-15 16:14:19 -04003551static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003552{
Josef Bacik41be1f32012-10-15 13:43:18 -04003553 struct btrfs_item *start_item;
3554 struct btrfs_item *end_item;
3555 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003556 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003557 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003558 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003559
3560 if (!nr)
3561 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003562 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003563 start_item = btrfs_item_nr(start);
3564 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003565 data_len = btrfs_token_item_offset(l, start_item, &token) +
3566 btrfs_token_item_size(l, start_item, &token);
3567 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003568 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003569 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003570 return data_len;
3571}
3572
Chris Mason74123bd2007-02-02 11:05:29 -05003573/*
Chris Masond4dbff92007-04-04 14:08:15 -04003574 * The space between the end of the leaf items and
3575 * the start of the leaf data. IOW, how much room
3576 * the leaf has left for both items and data
3577 */
Chris Masond3977122009-01-05 21:25:51 -05003578noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003579 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003580{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003581 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003582 int nritems = btrfs_header_nritems(leaf);
3583 int ret;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003584
3585 ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003586 if (ret < 0) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003587 btrfs_crit(fs_info,
3588 "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
3589 ret,
3590 (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
3591 leaf_space_used(leaf, 0, nritems), nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003592 }
3593 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003594}
3595
Chris Mason99d8f832010-07-07 10:51:48 -04003596/*
3597 * min slot controls the lowest index we're willing to push to the
3598 * right. We'll push up to and including min_slot, but no lower
3599 */
Chris Mason44871b12009-03-13 10:04:31 -04003600static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3601 struct btrfs_root *root,
3602 struct btrfs_path *path,
3603 int data_size, int empty,
3604 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003605 int free_space, u32 left_nritems,
3606 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003607{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003608 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003609 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003610 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003611 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003612 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003613 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003614 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003615 int push_space = 0;
3616 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003617 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003618 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003619 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003620 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003621 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003622
Chris Masoncfed81a2012-03-03 07:40:03 -05003623 btrfs_init_map_token(&token);
3624
Chris Mason34a38212007-11-07 13:31:03 -05003625 if (empty)
3626 nr = 0;
3627 else
Chris Mason99d8f832010-07-07 10:51:48 -04003628 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003629
Zheng Yan31840ae2008-09-23 13:14:14 -04003630 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003631 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003632
Chris Mason44871b12009-03-13 10:04:31 -04003633 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003634 i = left_nritems - 1;
3635 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003636 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003637
Zheng Yan31840ae2008-09-23 13:14:14 -04003638 if (!empty && push_items > 0) {
3639 if (path->slots[0] > i)
3640 break;
3641 if (path->slots[0] == i) {
3642 int space = btrfs_leaf_free_space(root, left);
3643 if (space + push_space * 2 > free_space)
3644 break;
3645 }
3646 }
3647
Chris Mason00ec4c52007-02-24 12:47:20 -05003648 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003649 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003650
Chris Masondb945352007-10-15 16:15:53 -04003651 this_item_size = btrfs_item_size(left, item);
3652 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003653 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003654
Chris Mason00ec4c52007-02-24 12:47:20 -05003655 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003656 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003657 if (i == 0)
3658 break;
3659 i--;
Chris Masondb945352007-10-15 16:15:53 -04003660 }
Chris Mason5f39d392007-10-15 16:14:19 -04003661
Chris Mason925baed2008-06-25 16:01:30 -04003662 if (push_items == 0)
3663 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003664
Julia Lawall6c1500f2012-11-03 20:30:18 +00003665 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003666
Chris Mason00ec4c52007-02-24 12:47:20 -05003667 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003668 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003669
Chris Mason5f39d392007-10-15 16:14:19 -04003670 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003671 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003672
Chris Mason00ec4c52007-02-24 12:47:20 -05003673 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003674 data_end = leaf_data_end(root, right);
3675 memmove_extent_buffer(right,
3676 btrfs_leaf_data(right) + data_end - push_space,
3677 btrfs_leaf_data(right) + data_end,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003678 BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003679
Chris Mason00ec4c52007-02-24 12:47:20 -05003680 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003681 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003682 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Chris Masond6025572007-03-30 14:27:56 -04003683 btrfs_leaf_data(left) + leaf_data_end(root, left),
3684 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003685
3686 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3687 btrfs_item_nr_offset(0),
3688 right_nritems * sizeof(struct btrfs_item));
3689
Chris Mason00ec4c52007-02-24 12:47:20 -05003690 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003691 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3692 btrfs_item_nr_offset(left_nritems - push_items),
3693 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003694
3695 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003696 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003697 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003698 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason7518a232007-03-12 12:01:18 -04003699 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003700 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003701 push_space -= btrfs_token_item_size(right, item, &token);
3702 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003703 }
3704
Chris Mason7518a232007-03-12 12:01:18 -04003705 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003706 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003707
Chris Mason34a38212007-11-07 13:31:03 -05003708 if (left_nritems)
3709 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003710 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003711 clean_tree_block(trans, fs_info, left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003712
Chris Mason5f39d392007-10-15 16:14:19 -04003713 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003714
Chris Mason5f39d392007-10-15 16:14:19 -04003715 btrfs_item_key(right, &disk_key, 0);
3716 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003717 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003718
Chris Mason00ec4c52007-02-24 12:47:20 -05003719 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003720 if (path->slots[0] >= left_nritems) {
3721 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003722 if (btrfs_header_nritems(path->nodes[0]) == 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003723 clean_tree_block(trans, fs_info, path->nodes[0]);
Chris Mason925baed2008-06-25 16:01:30 -04003724 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003725 free_extent_buffer(path->nodes[0]);
3726 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003727 path->slots[1] += 1;
3728 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003729 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003730 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003731 }
3732 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003733
3734out_unlock:
3735 btrfs_tree_unlock(right);
3736 free_extent_buffer(right);
3737 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003738}
Chris Mason925baed2008-06-25 16:01:30 -04003739
Chris Mason00ec4c52007-02-24 12:47:20 -05003740/*
Chris Mason44871b12009-03-13 10:04:31 -04003741 * push some data in the path leaf to the right, trying to free up at
3742 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3743 *
3744 * returns 1 if the push failed because the other node didn't have enough
3745 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003746 *
3747 * this will push starting from min_slot to the end of the leaf. It won't
3748 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003749 */
3750static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003751 *root, struct btrfs_path *path,
3752 int min_data_size, int data_size,
3753 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003754{
3755 struct extent_buffer *left = path->nodes[0];
3756 struct extent_buffer *right;
3757 struct extent_buffer *upper;
3758 int slot;
3759 int free_space;
3760 u32 left_nritems;
3761 int ret;
3762
3763 if (!path->nodes[1])
3764 return 1;
3765
3766 slot = path->slots[1];
3767 upper = path->nodes[1];
3768 if (slot >= btrfs_header_nritems(upper) - 1)
3769 return 1;
3770
3771 btrfs_assert_tree_locked(path->nodes[1]);
3772
3773 right = read_node_slot(root, upper, slot + 1);
Liu Bofb770ae2016-07-05 12:10:14 -07003774 /*
3775 * slot + 1 is not valid or we fail to read the right node,
3776 * no big deal, just return.
3777 */
3778 if (IS_ERR(right))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003779 return 1;
3780
Chris Mason44871b12009-03-13 10:04:31 -04003781 btrfs_tree_lock(right);
3782 btrfs_set_lock_blocking(right);
3783
3784 free_space = btrfs_leaf_free_space(root, right);
3785 if (free_space < data_size)
3786 goto out_unlock;
3787
3788 /* cow and double check */
3789 ret = btrfs_cow_block(trans, root, right, upper,
3790 slot + 1, &right);
3791 if (ret)
3792 goto out_unlock;
3793
3794 free_space = btrfs_leaf_free_space(root, right);
3795 if (free_space < data_size)
3796 goto out_unlock;
3797
3798 left_nritems = btrfs_header_nritems(left);
3799 if (left_nritems == 0)
3800 goto out_unlock;
3801
Filipe David Borba Manana2ef1fed2013-12-04 22:17:39 +00003802 if (path->slots[0] == left_nritems && !empty) {
3803 /* Key greater than all keys in the leaf, right neighbor has
3804 * enough room for it and we're not emptying our leaf to delete
3805 * it, therefore use right neighbor to insert the new item and
3806 * no need to touch/dirty our left leaft. */
3807 btrfs_tree_unlock(left);
3808 free_extent_buffer(left);
3809 path->nodes[0] = right;
3810 path->slots[0] = 0;
3811 path->slots[1]++;
3812 return 0;
3813 }
3814
Chris Mason99d8f832010-07-07 10:51:48 -04003815 return __push_leaf_right(trans, root, path, min_data_size, empty,
3816 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003817out_unlock:
3818 btrfs_tree_unlock(right);
3819 free_extent_buffer(right);
3820 return 1;
3821}
3822
3823/*
Chris Mason74123bd2007-02-02 11:05:29 -05003824 * push some data in the path leaf to the left, trying to free up at
3825 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003826 *
3827 * max_slot can put a limit on how far into the leaf we'll push items. The
3828 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3829 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003830 */
Chris Mason44871b12009-03-13 10:04:31 -04003831static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3832 struct btrfs_root *root,
3833 struct btrfs_path *path, int data_size,
3834 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003835 int free_space, u32 right_nritems,
3836 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003837{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003838 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04003839 struct btrfs_disk_key disk_key;
3840 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003841 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003842 int push_space = 0;
3843 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003844 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003845 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003846 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003847 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003848 u32 this_item_size;
3849 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003850 struct btrfs_map_token token;
3851
3852 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003853
Chris Mason34a38212007-11-07 13:31:03 -05003854 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003855 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003856 else
Chris Mason99d8f832010-07-07 10:51:48 -04003857 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003858
3859 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003860 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003861
Zheng Yan31840ae2008-09-23 13:14:14 -04003862 if (!empty && push_items > 0) {
3863 if (path->slots[0] < i)
3864 break;
3865 if (path->slots[0] == i) {
3866 int space = btrfs_leaf_free_space(root, right);
3867 if (space + push_space * 2 > free_space)
3868 break;
3869 }
3870 }
3871
Chris Masonbe0e5c02007-01-26 15:51:26 -05003872 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003873 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003874
3875 this_item_size = btrfs_item_size(right, item);
3876 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003877 break;
Chris Masondb945352007-10-15 16:15:53 -04003878
Chris Masonbe0e5c02007-01-26 15:51:26 -05003879 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003880 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003881 }
Chris Masondb945352007-10-15 16:15:53 -04003882
Chris Masonbe0e5c02007-01-26 15:51:26 -05003883 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003884 ret = 1;
3885 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003886 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303887 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003888
Chris Masonbe0e5c02007-01-26 15:51:26 -05003889 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003890 copy_extent_buffer(left, right,
3891 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3892 btrfs_item_nr_offset(0),
3893 push_items * sizeof(struct btrfs_item));
3894
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003895 push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond3977122009-01-05 21:25:51 -05003896 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003897
3898 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003899 leaf_data_end(root, left) - push_space,
3900 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003901 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003902 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003903 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003904 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003905
Chris Masondb945352007-10-15 16:15:53 -04003906 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003907 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003908 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003909
Ross Kirkdd3cc162013-09-16 15:58:09 +01003910 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003911
Chris Masoncfed81a2012-03-03 07:40:03 -05003912 ioff = btrfs_token_item_offset(left, item, &token);
3913 btrfs_set_token_item_offset(left, item,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003914 ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
Chris Masoncfed81a2012-03-03 07:40:03 -05003915 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003916 }
Chris Mason5f39d392007-10-15 16:14:19 -04003917 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003918
3919 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003920 if (push_items > right_nritems)
3921 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003922 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003923
Chris Mason34a38212007-11-07 13:31:03 -05003924 if (push_items < right_nritems) {
3925 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3926 leaf_data_end(root, right);
3927 memmove_extent_buffer(right, btrfs_leaf_data(right) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003928 BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
Chris Mason34a38212007-11-07 13:31:03 -05003929 btrfs_leaf_data(right) +
3930 leaf_data_end(root, right), push_space);
3931
3932 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003933 btrfs_item_nr_offset(push_items),
3934 (btrfs_header_nritems(right) - push_items) *
3935 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003936 }
Yaneef1c492007-11-26 10:58:13 -05003937 right_nritems -= push_items;
3938 btrfs_set_header_nritems(right, right_nritems);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003939 push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
Chris Mason5f39d392007-10-15 16:14:19 -04003940 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003941 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003942
Chris Masoncfed81a2012-03-03 07:40:03 -05003943 push_space = push_space - btrfs_token_item_size(right,
3944 item, &token);
3945 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003946 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003947
Chris Mason5f39d392007-10-15 16:14:19 -04003948 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003949 if (right_nritems)
3950 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003951 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003952 clean_tree_block(trans, fs_info, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003953
Chris Mason5f39d392007-10-15 16:14:19 -04003954 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003955 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003956
3957 /* then fixup the leaf pointer in the path */
3958 if (path->slots[0] < push_items) {
3959 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003960 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003961 free_extent_buffer(path->nodes[0]);
3962 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003963 path->slots[1] -= 1;
3964 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003965 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003966 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003967 path->slots[0] -= push_items;
3968 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003969 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003970 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003971out:
3972 btrfs_tree_unlock(left);
3973 free_extent_buffer(left);
3974 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003975}
3976
Chris Mason74123bd2007-02-02 11:05:29 -05003977/*
Chris Mason44871b12009-03-13 10:04:31 -04003978 * push some data in the path leaf to the left, trying to free up at
3979 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003980 *
3981 * max_slot can put a limit on how far into the leaf we'll push items. The
3982 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3983 * items
Chris Mason44871b12009-03-13 10:04:31 -04003984 */
3985static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003986 *root, struct btrfs_path *path, int min_data_size,
3987 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003988{
3989 struct extent_buffer *right = path->nodes[0];
3990 struct extent_buffer *left;
3991 int slot;
3992 int free_space;
3993 u32 right_nritems;
3994 int ret = 0;
3995
3996 slot = path->slots[1];
3997 if (slot == 0)
3998 return 1;
3999 if (!path->nodes[1])
4000 return 1;
4001
4002 right_nritems = btrfs_header_nritems(right);
4003 if (right_nritems == 0)
4004 return 1;
4005
4006 btrfs_assert_tree_locked(path->nodes[1]);
4007
4008 left = read_node_slot(root, path->nodes[1], slot - 1);
Liu Bofb770ae2016-07-05 12:10:14 -07004009 /*
4010 * slot - 1 is not valid or we fail to read the left node,
4011 * no big deal, just return.
4012 */
4013 if (IS_ERR(left))
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004014 return 1;
4015
Chris Mason44871b12009-03-13 10:04:31 -04004016 btrfs_tree_lock(left);
4017 btrfs_set_lock_blocking(left);
4018
4019 free_space = btrfs_leaf_free_space(root, left);
4020 if (free_space < data_size) {
4021 ret = 1;
4022 goto out;
4023 }
4024
4025 /* cow and double check */
4026 ret = btrfs_cow_block(trans, root, left,
4027 path->nodes[1], slot - 1, &left);
4028 if (ret) {
4029 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004030 if (ret == -ENOSPC)
4031 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004032 goto out;
4033 }
4034
4035 free_space = btrfs_leaf_free_space(root, left);
4036 if (free_space < data_size) {
4037 ret = 1;
4038 goto out;
4039 }
4040
Chris Mason99d8f832010-07-07 10:51:48 -04004041 return __push_leaf_left(trans, root, path, min_data_size,
4042 empty, left, free_space, right_nritems,
4043 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04004044out:
4045 btrfs_tree_unlock(left);
4046 free_extent_buffer(left);
4047 return ret;
4048}
4049
4050/*
Chris Mason74123bd2007-02-02 11:05:29 -05004051 * split the path's leaf in two, making sure there is at least data_size
4052 * available for the resulting leaf level of the path.
4053 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004054static noinline void copy_for_split(struct btrfs_trans_handle *trans,
4055 struct btrfs_root *root,
4056 struct btrfs_path *path,
4057 struct extent_buffer *l,
4058 struct extent_buffer *right,
4059 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004061 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004062 int data_copy_size;
4063 int rt_data_off;
4064 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04004065 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004066 struct btrfs_map_token token;
4067
4068 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004069
Chris Mason5f39d392007-10-15 16:14:19 -04004070 nritems = nritems - mid;
4071 btrfs_set_header_nritems(right, nritems);
4072 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
4073
4074 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
4075 btrfs_item_nr_offset(mid),
4076 nritems * sizeof(struct btrfs_item));
4077
4078 copy_extent_buffer(right, l,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004079 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(fs_info) -
Chris Masond6025572007-03-30 14:27:56 -04004080 data_copy_size, btrfs_leaf_data(l) +
4081 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05004082
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004083 rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004084
4085 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01004086 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004087 u32 ioff;
4088
Chris Masoncfed81a2012-03-03 07:40:03 -05004089 ioff = btrfs_token_item_offset(right, item, &token);
4090 btrfs_set_token_item_offset(right, item,
4091 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004092 }
Chris Mason74123bd2007-02-02 11:05:29 -05004093
Chris Mason5f39d392007-10-15 16:14:19 -04004094 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04004095 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004096 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004097 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004098
4099 btrfs_mark_buffer_dirty(right);
4100 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05004101 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004102
Chris Masonbe0e5c02007-01-26 15:51:26 -05004103 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04004104 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04004105 free_extent_buffer(path->nodes[0]);
4106 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004107 path->slots[0] -= mid;
4108 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04004109 } else {
4110 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04004111 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04004112 }
Chris Mason5f39d392007-10-15 16:14:19 -04004113
Chris Masoneb60cea2007-02-02 09:18:22 -05004114 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04004115}
4116
4117/*
Chris Mason99d8f832010-07-07 10:51:48 -04004118 * double splits happen when we need to insert a big item in the middle
4119 * of a leaf. A double split can leave us with 3 mostly empty leaves:
4120 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
4121 * A B C
4122 *
4123 * We avoid this by trying to push the items on either side of our target
4124 * into the adjacent leaves. If all goes well we can avoid the double split
4125 * completely.
4126 */
4127static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
4128 struct btrfs_root *root,
4129 struct btrfs_path *path,
4130 int data_size)
4131{
4132 int ret;
4133 int progress = 0;
4134 int slot;
4135 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004136 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04004137
4138 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004139 if (slot < btrfs_header_nritems(path->nodes[0]))
4140 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04004141
4142 /*
4143 * try to push all the items after our slot into the
4144 * right leaf
4145 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004146 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004147 if (ret < 0)
4148 return ret;
4149
4150 if (ret == 0)
4151 progress++;
4152
4153 nritems = btrfs_header_nritems(path->nodes[0]);
4154 /*
4155 * our goal is to get our slot at the start or end of a leaf. If
4156 * we've done so we're done
4157 */
4158 if (path->slots[0] == 0 || path->slots[0] == nritems)
4159 return 0;
4160
4161 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4162 return 0;
4163
4164 /* try to push all the items before our slot into the next leaf */
4165 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004166 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04004167 if (ret < 0)
4168 return ret;
4169
4170 if (ret == 0)
4171 progress++;
4172
4173 if (progress)
4174 return 0;
4175 return 1;
4176}
4177
4178/*
Chris Mason44871b12009-03-13 10:04:31 -04004179 * split the path's leaf in two, making sure there is at least data_size
4180 * available for the resulting leaf level of the path.
4181 *
4182 * returns 0 if all went well and < 0 on failure.
4183 */
4184static noinline int split_leaf(struct btrfs_trans_handle *trans,
4185 struct btrfs_root *root,
4186 struct btrfs_key *ins_key,
4187 struct btrfs_path *path, int data_size,
4188 int extend)
4189{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004190 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004191 struct extent_buffer *l;
4192 u32 nritems;
4193 int mid;
4194 int slot;
4195 struct extent_buffer *right;
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004196 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason44871b12009-03-13 10:04:31 -04004197 int ret = 0;
4198 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004199 int split;
Chris Mason44871b12009-03-13 10:04:31 -04004200 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004201 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04004202
Yan, Zhenga5719522009-09-24 09:17:31 -04004203 l = path->nodes[0];
4204 slot = path->slots[0];
4205 if (extend && data_size + btrfs_item_size_nr(l, slot) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004206 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
Yan, Zhenga5719522009-09-24 09:17:31 -04004207 return -EOVERFLOW;
4208
Chris Mason44871b12009-03-13 10:04:31 -04004209 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004210 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004211 int space_needed = data_size;
4212
4213 if (slot < btrfs_header_nritems(l))
4214 space_needed -= btrfs_leaf_free_space(root, l);
4215
4216 wret = push_leaf_right(trans, root, path, space_needed,
4217 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004218 if (wret < 0)
4219 return wret;
4220 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004221 wret = push_leaf_left(trans, root, path, space_needed,
4222 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004223 if (wret < 0)
4224 return wret;
4225 }
4226 l = path->nodes[0];
4227
4228 /* did the pushes work? */
4229 if (btrfs_leaf_free_space(root, l) >= data_size)
4230 return 0;
4231 }
4232
4233 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004234 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004235 if (ret)
4236 return ret;
4237 }
4238again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004239 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004240 l = path->nodes[0];
4241 slot = path->slots[0];
4242 nritems = btrfs_header_nritems(l);
4243 mid = (nritems + 1) / 2;
4244
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004245 if (mid <= slot) {
4246 if (nritems == 1 ||
4247 leaf_space_used(l, mid, nritems - mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004248 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004249 if (slot >= nritems) {
4250 split = 0;
4251 } else {
4252 mid = slot;
4253 if (mid != nritems &&
4254 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004255 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004256 if (data_size && !tried_avoid_double)
4257 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004258 split = 2;
4259 }
4260 }
4261 }
4262 } else {
4263 if (leaf_space_used(l, 0, mid) + data_size >
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004264 BTRFS_LEAF_DATA_SIZE(fs_info)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004265 if (!extend && data_size && slot == 0) {
4266 split = 0;
4267 } else if ((extend || !data_size) && slot == 0) {
4268 mid = 1;
4269 } else {
4270 mid = slot;
4271 if (mid != nritems &&
4272 leaf_space_used(l, mid, nritems - mid) +
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004273 data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004274 if (data_size && !tried_avoid_double)
4275 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304276 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004277 }
4278 }
4279 }
4280 }
4281
4282 if (split == 0)
4283 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4284 else
4285 btrfs_item_key(l, &disk_key, mid);
4286
David Sterba4d75f8a2014-06-15 01:54:12 +02004287 right = btrfs_alloc_tree_block(trans, root, 0, root->root_key.objectid,
4288 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004289 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004290 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004291
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004292 root_add_used(root, fs_info->nodesize);
Chris Mason44871b12009-03-13 10:04:31 -04004293
David Sterbab159fa22016-11-08 18:09:03 +01004294 memzero_extent_buffer(right, 0, sizeof(struct btrfs_header));
Chris Mason44871b12009-03-13 10:04:31 -04004295 btrfs_set_header_bytenr(right, right->start);
4296 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004297 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004298 btrfs_set_header_owner(right, root->root_key.objectid);
4299 btrfs_set_header_level(right, 0);
David Sterbad24ee972016-11-09 17:44:25 +01004300 write_extent_buffer_fsid(right, fs_info->fsid);
4301 write_extent_buffer_chunk_tree_uuid(right, fs_info->chunk_tree_uuid);
Chris Mason44871b12009-03-13 10:04:31 -04004302
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 if (split == 0) {
4304 if (mid <= slot) {
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, 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;
4312 path->slots[1] += 1;
4313 } else {
4314 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004315 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004316 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004317 btrfs_tree_unlock(path->nodes[0]);
4318 free_extent_buffer(path->nodes[0]);
4319 path->nodes[0] = right;
4320 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004321 if (path->slots[1] == 0)
Daniel Dresslerb7a03652014-11-12 13:43:09 +09004322 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004323 }
Liu Bo196e0242016-09-07 14:48:28 -07004324 /*
4325 * We create a new leaf 'right' for the required ins_len and
4326 * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
4327 * the content of ins_len to 'right'.
4328 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004329 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004330 }
4331
Jeff Mahoney143bede2012-03-01 14:56:26 +01004332 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004333
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004334 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004335 BUG_ON(num_doubles != 0);
4336 num_doubles++;
4337 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004338 }
Chris Mason44871b12009-03-13 10:04:31 -04004339
Jeff Mahoney143bede2012-03-01 14:56:26 +01004340 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004341
4342push_for_double:
4343 push_for_double_split(trans, root, path, data_size);
4344 tried_avoid_double = 1;
4345 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4346 return 0;
4347 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004348}
4349
Yan, Zhengad48fd752009-11-12 09:33:58 +00004350static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4351 struct btrfs_root *root,
4352 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004353{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004354 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004355 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004356 struct btrfs_file_extent_item *fi;
4357 u64 extent_len = 0;
4358 u32 item_size;
4359 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004360
4361 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004362 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4363
4364 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4365 key.type != BTRFS_EXTENT_CSUM_KEY);
4366
4367 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4368 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004369
4370 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004371 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4372 fi = btrfs_item_ptr(leaf, path->slots[0],
4373 struct btrfs_file_extent_item);
4374 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4375 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004376 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004377
Chris Mason459931e2008-12-10 09:10:46 -05004378 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004379 path->search_for_split = 1;
4380 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004381 path->search_for_split = 0;
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004382 if (ret > 0)
4383 ret = -EAGAIN;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004384 if (ret < 0)
4385 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004386
Yan, Zhengad48fd752009-11-12 09:33:58 +00004387 ret = -EAGAIN;
4388 leaf = path->nodes[0];
Filipe Mananaa8df6fe2015-01-20 12:40:53 +00004389 /* if our item isn't there, return now */
4390 if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
Yan, Zhengad48fd752009-11-12 09:33:58 +00004391 goto err;
4392
Chris Mason109f6ae2010-04-02 09:20:18 -04004393 /* the leaf has changed, it now has room. return now */
4394 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4395 goto err;
4396
Yan, Zhengad48fd752009-11-12 09:33:58 +00004397 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4398 fi = btrfs_item_ptr(leaf, path->slots[0],
4399 struct btrfs_file_extent_item);
4400 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4401 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004402 }
4403
Chris Masonb9473432009-03-13 11:00:37 -04004404 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004405 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004406 if (ret)
4407 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004408
Yan, Zhengad48fd752009-11-12 09:33:58 +00004409 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004410 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004411 return 0;
4412err:
4413 path->keep_locks = 0;
4414 return ret;
4415}
4416
4417static noinline int split_item(struct btrfs_trans_handle *trans,
4418 struct btrfs_root *root,
4419 struct btrfs_path *path,
4420 struct btrfs_key *new_key,
4421 unsigned long split_offset)
4422{
4423 struct extent_buffer *leaf;
4424 struct btrfs_item *item;
4425 struct btrfs_item *new_item;
4426 int slot;
4427 char *buf;
4428 u32 nritems;
4429 u32 item_size;
4430 u32 orig_offset;
4431 struct btrfs_disk_key disk_key;
4432
Chris Masonb9473432009-03-13 11:00:37 -04004433 leaf = path->nodes[0];
4434 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4435
Chris Masonb4ce94d2009-02-04 09:25:08 -05004436 btrfs_set_path_blocking(path);
4437
Ross Kirkdd3cc162013-09-16 15:58:09 +01004438 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004439 orig_offset = btrfs_item_offset(leaf, item);
4440 item_size = btrfs_item_size(leaf, item);
4441
Chris Mason459931e2008-12-10 09:10:46 -05004442 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004443 if (!buf)
4444 return -ENOMEM;
4445
Chris Mason459931e2008-12-10 09:10:46 -05004446 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4447 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004448
Chris Mason459931e2008-12-10 09:10:46 -05004449 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004450 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004451 if (slot != nritems) {
4452 /* shift the items */
4453 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004454 btrfs_item_nr_offset(slot),
4455 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004456 }
4457
4458 btrfs_cpu_key_to_disk(&disk_key, new_key);
4459 btrfs_set_item_key(leaf, &disk_key, slot);
4460
Ross Kirkdd3cc162013-09-16 15:58:09 +01004461 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004462
4463 btrfs_set_item_offset(leaf, new_item, orig_offset);
4464 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4465
4466 btrfs_set_item_offset(leaf, item,
4467 orig_offset + item_size - split_offset);
4468 btrfs_set_item_size(leaf, item, split_offset);
4469
4470 btrfs_set_header_nritems(leaf, nritems + 1);
4471
4472 /* write the data for the start of the original item */
4473 write_extent_buffer(leaf, buf,
4474 btrfs_item_ptr_offset(leaf, path->slots[0]),
4475 split_offset);
4476
4477 /* write the data for the new item */
4478 write_extent_buffer(leaf, buf + split_offset,
4479 btrfs_item_ptr_offset(leaf, slot),
4480 item_size - split_offset);
4481 btrfs_mark_buffer_dirty(leaf);
4482
Yan, Zhengad48fd752009-11-12 09:33:58 +00004483 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004484 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004485 return 0;
4486}
4487
4488/*
4489 * This function splits a single item into two items,
4490 * giving 'new_key' to the new item and splitting the
4491 * old one at split_offset (from the start of the item).
4492 *
4493 * The path may be released by this operation. After
4494 * the split, the path is pointing to the old item. The
4495 * new item is going to be in the same node as the old one.
4496 *
4497 * Note, the item being split must be smaller enough to live alone on
4498 * a tree block with room for one extra struct btrfs_item
4499 *
4500 * This allows us to split the item in place, keeping a lock on the
4501 * leaf the entire time.
4502 */
4503int btrfs_split_item(struct btrfs_trans_handle *trans,
4504 struct btrfs_root *root,
4505 struct btrfs_path *path,
4506 struct btrfs_key *new_key,
4507 unsigned long split_offset)
4508{
4509 int ret;
4510 ret = setup_leaf_for_split(trans, root, path,
4511 sizeof(struct btrfs_item));
4512 if (ret)
4513 return ret;
4514
4515 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004516 return ret;
4517}
4518
4519/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004520 * This function duplicate a item, giving 'new_key' to the new item.
4521 * It guarantees both items live in the same tree leaf and the new item
4522 * is contiguous with the original item.
4523 *
4524 * This allows us to split file extent in place, keeping a lock on the
4525 * leaf the entire time.
4526 */
4527int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4528 struct btrfs_root *root,
4529 struct btrfs_path *path,
4530 struct btrfs_key *new_key)
4531{
4532 struct extent_buffer *leaf;
4533 int ret;
4534 u32 item_size;
4535
4536 leaf = path->nodes[0];
4537 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4538 ret = setup_leaf_for_split(trans, root, path,
4539 item_size + sizeof(struct btrfs_item));
4540 if (ret)
4541 return ret;
4542
4543 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004544 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004545 item_size, item_size +
4546 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004547 leaf = path->nodes[0];
4548 memcpy_extent_buffer(leaf,
4549 btrfs_item_ptr_offset(leaf, path->slots[0]),
4550 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4551 item_size);
4552 return 0;
4553}
4554
4555/*
Chris Masond352ac62008-09-29 15:18:18 -04004556 * make the item pointed to by the path smaller. new_size indicates
4557 * how small to make it, and from_end tells us if we just chop bytes
4558 * off the end of the item or if we shift the item to chop bytes off
4559 * the front.
4560 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004561void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004562 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004563{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004564 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masonb18c6682007-04-17 13:26:50 -04004565 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004566 struct extent_buffer *leaf;
4567 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004568 u32 nritems;
4569 unsigned int data_end;
4570 unsigned int old_data_start;
4571 unsigned int old_size;
4572 unsigned int size_diff;
4573 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004574 struct btrfs_map_token token;
4575
4576 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004577
Chris Mason5f39d392007-10-15 16:14:19 -04004578 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004579 slot = path->slots[0];
4580
4581 old_size = btrfs_item_size_nr(leaf, slot);
4582 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004583 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004584
Chris Mason5f39d392007-10-15 16:14:19 -04004585 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004586 data_end = leaf_data_end(root, leaf);
4587
Chris Mason5f39d392007-10-15 16:14:19 -04004588 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004589
Chris Masonb18c6682007-04-17 13:26:50 -04004590 size_diff = old_size - new_size;
4591
4592 BUG_ON(slot < 0);
4593 BUG_ON(slot >= nritems);
4594
4595 /*
4596 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4597 */
4598 /* first correct the data pointers */
4599 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004600 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004601 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004602
Chris Masoncfed81a2012-03-03 07:40:03 -05004603 ioff = btrfs_token_item_offset(leaf, item, &token);
4604 btrfs_set_token_item_offset(leaf, item,
4605 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004606 }
Chris Masondb945352007-10-15 16:15:53 -04004607
Chris Masonb18c6682007-04-17 13:26:50 -04004608 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004609 if (from_end) {
4610 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4611 data_end + size_diff, btrfs_leaf_data(leaf) +
4612 data_end, old_data_start + new_size - data_end);
4613 } else {
4614 struct btrfs_disk_key disk_key;
4615 u64 offset;
4616
4617 btrfs_item_key(leaf, &disk_key, slot);
4618
4619 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4620 unsigned long ptr;
4621 struct btrfs_file_extent_item *fi;
4622
4623 fi = btrfs_item_ptr(leaf, slot,
4624 struct btrfs_file_extent_item);
4625 fi = (struct btrfs_file_extent_item *)(
4626 (unsigned long)fi - size_diff);
4627
4628 if (btrfs_file_extent_type(leaf, fi) ==
4629 BTRFS_FILE_EXTENT_INLINE) {
4630 ptr = btrfs_item_ptr_offset(leaf, slot);
4631 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004632 (unsigned long)fi,
David Sterba7ec20af2014-07-24 17:34:58 +02004633 BTRFS_FILE_EXTENT_INLINE_DATA_START);
Chris Mason179e29e2007-11-01 11:28:41 -04004634 }
4635 }
4636
4637 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4638 data_end + size_diff, btrfs_leaf_data(leaf) +
4639 data_end, old_data_start - data_end);
4640
4641 offset = btrfs_disk_key_offset(&disk_key);
4642 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4643 btrfs_set_item_key(leaf, &disk_key, slot);
4644 if (slot == 0)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004645 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004646 }
Chris Mason5f39d392007-10-15 16:14:19 -04004647
Ross Kirkdd3cc162013-09-16 15:58:09 +01004648 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004649 btrfs_set_item_size(leaf, item, new_size);
4650 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004651
Chris Mason5f39d392007-10-15 16:14:19 -04004652 if (btrfs_leaf_free_space(root, leaf) < 0) {
4653 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004654 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004655 }
Chris Masonb18c6682007-04-17 13:26:50 -04004656}
4657
Chris Masond352ac62008-09-29 15:18:18 -04004658/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004659 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004660 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004661void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004662 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004663{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004664 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason6567e832007-04-16 09:22:45 -04004665 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004666 struct extent_buffer *leaf;
4667 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004668 u32 nritems;
4669 unsigned int data_end;
4670 unsigned int old_data;
4671 unsigned int old_size;
4672 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004673 struct btrfs_map_token token;
4674
4675 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004676
Chris Mason5f39d392007-10-15 16:14:19 -04004677 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004678
Chris Mason5f39d392007-10-15 16:14:19 -04004679 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004680 data_end = leaf_data_end(root, leaf);
4681
Chris Mason5f39d392007-10-15 16:14:19 -04004682 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4683 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004684 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004685 }
Chris Mason6567e832007-04-16 09:22:45 -04004686 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004687 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004688
4689 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004690 if (slot >= nritems) {
4691 btrfs_print_leaf(root, leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004692 btrfs_crit(fs_info, "slot %d too large, nritems %d",
4693 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004694 BUG_ON(1);
4695 }
Chris Mason6567e832007-04-16 09:22:45 -04004696
4697 /*
4698 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4699 */
4700 /* first correct the data pointers */
4701 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004702 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004703 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004704
Chris Masoncfed81a2012-03-03 07:40:03 -05004705 ioff = btrfs_token_item_offset(leaf, item, &token);
4706 btrfs_set_token_item_offset(leaf, item,
4707 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004708 }
Chris Mason5f39d392007-10-15 16:14:19 -04004709
Chris Mason6567e832007-04-16 09:22:45 -04004710 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004711 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004712 data_end - data_size, btrfs_leaf_data(leaf) +
4713 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004714
Chris Mason6567e832007-04-16 09:22:45 -04004715 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004716 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004717 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004718 btrfs_set_item_size(leaf, item, old_size + data_size);
4719 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004720
Chris Mason5f39d392007-10-15 16:14:19 -04004721 if (btrfs_leaf_free_space(root, leaf) < 0) {
4722 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004723 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004724 }
Chris Mason6567e832007-04-16 09:22:45 -04004725}
4726
Chris Mason74123bd2007-02-02 11:05:29 -05004727/*
Chris Mason44871b12009-03-13 10:04:31 -04004728 * this is a helper for btrfs_insert_empty_items, the main goal here is
4729 * to save stack depth by doing the bulk of the work in a function
4730 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004731 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004732void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004733 struct btrfs_key *cpu_key, u32 *data_size,
4734 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004735{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004736 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004737 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004738 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004739 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004740 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004741 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004742 struct extent_buffer *leaf;
4743 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004744 struct btrfs_map_token token;
4745
Filipe Manana24cdc842014-07-28 19:34:35 +01004746 if (path->slots[0] == 0) {
4747 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004748 fixup_low_keys(fs_info, path, &disk_key, 1);
Filipe Manana24cdc842014-07-28 19:34:35 +01004749 }
4750 btrfs_unlock_up_safe(path, 1);
4751
Chris Masoncfed81a2012-03-03 07:40:03 -05004752 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004753
Chris Mason5f39d392007-10-15 16:14:19 -04004754 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004755 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004756
Chris Mason5f39d392007-10-15 16:14:19 -04004757 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004758 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004759
Chris Masonf25956c2008-09-12 15:32:53 -04004760 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004761 btrfs_print_leaf(root, leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004762 btrfs_crit(fs_info, "not enough freespace need %u have %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004763 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004764 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004765 }
Chris Mason5f39d392007-10-15 16:14:19 -04004766
Chris Masonbe0e5c02007-01-26 15:51:26 -05004767 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004768 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004769
Chris Mason5f39d392007-10-15 16:14:19 -04004770 if (old_data < data_end) {
4771 btrfs_print_leaf(root, leaf);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004772 btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
Jeff Mahoney5d163e02016-09-20 10:05:00 -04004773 slot, old_data, data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004774 BUG_ON(1);
4775 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004776 /*
4777 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4778 */
4779 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004780 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004781 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004782
Jeff Mahoney62e85572016-09-20 10:05:01 -04004783 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004784 ioff = btrfs_token_item_offset(leaf, item, &token);
4785 btrfs_set_token_item_offset(leaf, item,
4786 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004787 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004788 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004789 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004790 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004791 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004792
4793 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004794 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004795 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004796 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004797 data_end = old_data;
4798 }
Chris Mason5f39d392007-10-15 16:14:19 -04004799
Chris Mason62e27492007-03-15 12:56:47 -04004800 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004801 for (i = 0; i < nr; i++) {
4802 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4803 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004804 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004805 btrfs_set_token_item_offset(leaf, item,
4806 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004807 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004808 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004809 }
Chris Mason44871b12009-03-13 10:04:31 -04004810
Chris Mason9c583092008-01-29 15:15:18 -05004811 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonb9473432009-03-13 11:00:37 -04004812 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004813
Chris Mason5f39d392007-10-15 16:14:19 -04004814 if (btrfs_leaf_free_space(root, leaf) < 0) {
4815 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004816 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004817 }
Chris Mason44871b12009-03-13 10:04:31 -04004818}
4819
4820/*
4821 * Given a key and some data, insert items into the tree.
4822 * This does all the path init required, making room in the tree if needed.
4823 */
4824int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4825 struct btrfs_root *root,
4826 struct btrfs_path *path,
4827 struct btrfs_key *cpu_key, u32 *data_size,
4828 int nr)
4829{
Chris Mason44871b12009-03-13 10:04:31 -04004830 int ret = 0;
4831 int slot;
4832 int i;
4833 u32 total_size = 0;
4834 u32 total_data = 0;
4835
4836 for (i = 0; i < nr; i++)
4837 total_data += data_size[i];
4838
4839 total_size = total_data + (nr * sizeof(struct btrfs_item));
4840 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4841 if (ret == 0)
4842 return -EEXIST;
4843 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004844 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004845
Chris Mason44871b12009-03-13 10:04:31 -04004846 slot = path->slots[0];
4847 BUG_ON(slot < 0);
4848
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004849 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004850 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004851 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004852}
4853
4854/*
4855 * Given a key and some data, insert an item into the tree.
4856 * This does all the path init required, making room in the tree if needed.
4857 */
Chris Masone089f052007-03-16 16:20:31 -04004858int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4859 *root, struct btrfs_key *cpu_key, void *data, u32
4860 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004861{
4862 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004863 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004864 struct extent_buffer *leaf;
4865 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004866
Chris Mason2c90e5d2007-04-02 10:50:19 -04004867 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004868 if (!path)
4869 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004870 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004871 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004872 leaf = path->nodes[0];
4873 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4874 write_extent_buffer(leaf, data, ptr, data_size);
4875 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004876 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004877 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004878 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004879}
4880
Chris Mason74123bd2007-02-02 11:05:29 -05004881/*
Chris Mason5de08d72007-02-24 06:24:44 -05004882 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004883 *
Chris Masond352ac62008-09-29 15:18:18 -04004884 * the tree should have been previously balanced so the deletion does not
4885 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004886 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004887static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4888 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004889{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004890 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004891 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004892 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004893 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004894
Chris Mason5f39d392007-10-15 16:14:19 -04004895 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004896 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004897 if (level)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004898 tree_mod_log_eb_move(fs_info, parent, slot,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004899 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004900 memmove_extent_buffer(parent,
4901 btrfs_node_key_ptr_offset(slot),
4902 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004903 sizeof(struct btrfs_key_ptr) *
4904 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004905 } else if (level) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004906 ret = tree_mod_log_insert_key(fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004907 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004908 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004909 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004910
Chris Mason7518a232007-03-12 12:01:18 -04004911 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004912 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004913 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004914 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004915 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004916 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004917 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004918 struct btrfs_disk_key disk_key;
4919
4920 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004921 fixup_low_keys(fs_info, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004922 }
Chris Masond6025572007-03-30 14:27:56 -04004923 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004924}
4925
Chris Mason74123bd2007-02-02 11:05:29 -05004926/*
Chris Mason323ac952008-10-01 19:05:46 -04004927 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004928 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004929 *
4930 * This deletes the pointer in path->nodes[1] and frees the leaf
4931 * block extent. zero is returned if it all worked out, < 0 otherwise.
4932 *
4933 * The path must have already been setup for deleting the leaf, including
4934 * all the proper balancing. path->nodes[1] must be locked.
4935 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004936static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4937 struct btrfs_root *root,
4938 struct btrfs_path *path,
4939 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004940{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004941 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004942 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004943
Chris Mason4d081c42009-02-04 09:31:28 -05004944 /*
4945 * btrfs_free_extent is expensive, we want to make sure we
4946 * aren't holding any locks when we call it
4947 */
4948 btrfs_unlock_up_safe(path, 0);
4949
Yan, Zhengf0486c62010-05-16 10:46:25 -04004950 root_sub_used(root, leaf->len);
4951
Josef Bacik3083ee22012-03-09 16:01:49 -05004952 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004953 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004954 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004955}
4956/*
Chris Mason74123bd2007-02-02 11:05:29 -05004957 * delete the item at the leaf level in path. If that empties
4958 * the leaf, remove it from the tree
4959 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004960int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4961 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004962{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004963 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5f39d392007-10-15 16:14:19 -04004964 struct extent_buffer *leaf;
4965 struct btrfs_item *item;
Alexandru Moisece0eac22015-08-23 16:01:42 +00004966 u32 last_off;
4967 u32 dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004968 int ret = 0;
4969 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004970 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004971 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004972 struct btrfs_map_token token;
4973
4974 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004975
Chris Mason5f39d392007-10-15 16:14:19 -04004976 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004977 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4978
4979 for (i = 0; i < nr; i++)
4980 dsize += btrfs_item_size_nr(leaf, slot + i);
4981
Chris Mason5f39d392007-10-15 16:14:19 -04004982 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004983
Chris Mason85e21ba2008-01-29 15:11:36 -05004984 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004985 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004986
4987 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004988 data_end + dsize,
4989 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004990 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004991
Chris Mason85e21ba2008-01-29 15:11:36 -05004992 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004993 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004994
Ross Kirkdd3cc162013-09-16 15:58:09 +01004995 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004996 ioff = btrfs_token_item_offset(leaf, item, &token);
4997 btrfs_set_token_item_offset(leaf, item,
4998 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004999 }
Chris Masondb945352007-10-15 16:15:53 -04005000
Chris Mason5f39d392007-10-15 16:14:19 -04005001 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05005002 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04005003 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05005004 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05005005 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005006 btrfs_set_header_nritems(leaf, nritems - nr);
5007 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04005008
Chris Mason74123bd2007-02-02 11:05:29 -05005009 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04005010 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005011 if (leaf == root->node) {
5012 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05005013 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005014 btrfs_set_path_blocking(path);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005015 clean_tree_block(trans, fs_info, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005016 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05005017 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05005018 } else {
Chris Mason7518a232007-03-12 12:01:18 -04005019 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005020 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04005021 struct btrfs_disk_key disk_key;
5022
5023 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005024 fixup_low_keys(fs_info, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05005025 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005026
Chris Mason74123bd2007-02-02 11:05:29 -05005027 /* delete the leaf if it is mostly empty */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005028 if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05005029 /* push_leaf_left fixes the path.
5030 * make sure the path still points to our leaf
5031 * for possible call to del_ptr below
5032 */
Chris Mason4920c9a2007-01-26 16:38:42 -05005033 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04005034 extent_buffer_get(leaf);
5035
Chris Masonb9473432009-03-13 11:00:37 -04005036 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04005037 wret = push_leaf_left(trans, root, path, 1, 1,
5038 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04005039 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005040 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04005041
5042 if (path->nodes[0] == leaf &&
5043 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04005044 wret = push_leaf_right(trans, root, path, 1,
5045 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04005046 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05005047 ret = wret;
5048 }
Chris Mason5f39d392007-10-15 16:14:19 -04005049
5050 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04005051 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01005052 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005053 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01005054 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05005055 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005056 /* if we're still in the path, make sure
5057 * we're dirty. Otherwise, one of the
5058 * push_leaf functions must have already
5059 * dirtied this buffer
5060 */
5061 if (path->nodes[0] == leaf)
5062 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04005063 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005064 }
Chris Masond5719762007-03-23 10:01:08 -04005065 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04005066 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05005067 }
5068 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05005069 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05005070}
5071
Chris Mason97571fd2007-02-24 13:39:08 -05005072/*
Chris Mason925baed2008-06-25 16:01:30 -04005073 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05005074 * returns 0 if it found something or 1 if there are no lesser leaves.
5075 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04005076 *
5077 * This may release the path, and so you may lose any locks held at the
5078 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05005079 */
Josef Bacik16e75492013-10-22 12:18:51 -04005080int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05005081{
Chris Mason925baed2008-06-25 16:01:30 -04005082 struct btrfs_key key;
5083 struct btrfs_disk_key found_key;
5084 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05005085
Chris Mason925baed2008-06-25 16:01:30 -04005086 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05005087
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005088 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005089 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005090 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005091 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005092 key.offset = (u64)-1;
5093 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04005094 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005095 key.type = (u8)-1;
5096 key.offset = (u64)-1;
5097 } else {
Chris Mason925baed2008-06-25 16:01:30 -04005098 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01005099 }
Chris Mason7bb86312007-12-11 09:25:06 -05005100
David Sterbab3b4aa72011-04-21 01:20:15 +02005101 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04005102 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5103 if (ret < 0)
5104 return ret;
5105 btrfs_item_key(path->nodes[0], &found_key, 0);
5106 ret = comp_keys(&found_key, &key);
Filipe Manana337c6f62014-06-09 13:22:13 +01005107 /*
5108 * We might have had an item with the previous key in the tree right
5109 * before we released our path. And after we released our path, that
5110 * item might have been pushed to the first slot (0) of the leaf we
5111 * were holding due to a tree balance. Alternatively, an item with the
5112 * previous key can exist as the only element of a leaf (big fat item).
5113 * Therefore account for these 2 cases, so that our callers (like
5114 * btrfs_previous_item) don't miss an existing item with a key matching
5115 * the previous key we computed above.
5116 */
5117 if (ret <= 0)
Chris Mason925baed2008-06-25 16:01:30 -04005118 return 0;
5119 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05005120}
5121
Chris Mason3f157a22008-06-25 16:01:31 -04005122/*
5123 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00005124 * for nodes or leaves that are have a minimum transaction id.
5125 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04005126 *
5127 * This does not cow, but it does stuff the starting key it finds back
5128 * into min_key, so you can call btrfs_search_slot with cow=1 on the
5129 * key and get a writable path.
5130 *
5131 * This does lock as it descends, and path->keep_locks should be set
5132 * to 1 by the caller.
5133 *
5134 * This honors path->lowest_level to prevent descent past a given level
5135 * of the tree.
5136 *
Chris Masond352ac62008-09-29 15:18:18 -04005137 * min_trans indicates the oldest transaction that you are interested
5138 * in walking through. Any nodes or leaves older than min_trans are
5139 * skipped over (without reading them).
5140 *
Chris Mason3f157a22008-06-25 16:01:31 -04005141 * returns zero if something useful was found, < 0 on error and 1 if there
5142 * was nothing in the tree that matched the search criteria.
5143 */
5144int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00005145 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04005146 u64 min_trans)
5147{
5148 struct extent_buffer *cur;
5149 struct btrfs_key found_key;
5150 int slot;
Yan96524802008-07-24 12:19:49 -04005151 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04005152 u32 nritems;
5153 int level;
5154 int ret = 1;
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005155 int keep_locks = path->keep_locks;
Chris Mason3f157a22008-06-25 16:01:31 -04005156
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005157 path->keep_locks = 1;
Chris Mason3f157a22008-06-25 16:01:31 -04005158again:
Chris Masonbd681512011-07-16 15:23:14 -04005159 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04005160 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04005161 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04005162 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04005163 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005164
5165 if (btrfs_header_generation(cur) < min_trans) {
5166 ret = 1;
5167 goto out;
5168 }
Chris Masond3977122009-01-05 21:25:51 -05005169 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04005170 nritems = btrfs_header_nritems(cur);
5171 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04005172 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005173
Chris Mason323ac952008-10-01 19:05:46 -04005174 /* at the lowest level, we're done, setup the path and exit */
5175 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04005176 if (slot >= nritems)
5177 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04005178 ret = 0;
5179 path->slots[level] = slot;
5180 btrfs_item_key_to_cpu(cur, &found_key, slot);
5181 goto out;
5182 }
Yan96524802008-07-24 12:19:49 -04005183 if (sret && slot > 0)
5184 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04005185 /*
Eric Sandeende78b512013-01-31 18:21:12 +00005186 * check this node pointer against the min_trans parameters.
5187 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04005188 */
Chris Masond3977122009-01-05 21:25:51 -05005189 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04005190 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04005191
Chris Mason3f157a22008-06-25 16:01:31 -04005192 gen = btrfs_node_ptr_generation(cur, slot);
5193 if (gen < min_trans) {
5194 slot++;
5195 continue;
5196 }
Eric Sandeende78b512013-01-31 18:21:12 +00005197 break;
Chris Mason3f157a22008-06-25 16:01:31 -04005198 }
Chris Masone02119d2008-09-05 16:13:11 -04005199find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04005200 /*
5201 * we didn't find a candidate key in this node, walk forward
5202 * and find another one
5203 */
5204 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04005205 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005206 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04005207 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00005208 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04005209 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005210 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005211 goto again;
5212 } else {
5213 goto out;
5214 }
5215 }
5216 /* save our key for returning back */
5217 btrfs_node_key_to_cpu(cur, &found_key, slot);
5218 path->slots[level] = slot;
5219 if (level == path->lowest_level) {
5220 ret = 0;
Chris Mason3f157a22008-06-25 16:01:31 -04005221 goto out;
5222 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005223 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005224 cur = read_node_slot(root, cur, slot);
Liu Bofb770ae2016-07-05 12:10:14 -07005225 if (IS_ERR(cur)) {
5226 ret = PTR_ERR(cur);
5227 goto out;
5228 }
Chris Mason3f157a22008-06-25 16:01:31 -04005229
Chris Masonbd681512011-07-16 15:23:14 -04005230 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005231
Chris Masonbd681512011-07-16 15:23:14 -04005232 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005233 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005234 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005235 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005236 }
5237out:
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005238 path->keep_locks = keep_locks;
5239 if (ret == 0) {
5240 btrfs_unlock_up_safe(path, path->lowest_level + 1);
5241 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005242 memcpy(min_key, &found_key, sizeof(found_key));
Filipe Mananaf98de9b2014-08-04 19:37:21 +01005243 }
Chris Mason3f157a22008-06-25 16:01:31 -04005244 return ret;
5245}
5246
Liu Bofb770ae2016-07-05 12:10:14 -07005247static int tree_move_down(struct btrfs_root *root,
Alexander Block70698302012-06-05 21:07:48 +02005248 struct btrfs_path *path,
5249 int *level, int root_level)
5250{
Liu Bofb770ae2016-07-05 12:10:14 -07005251 struct extent_buffer *eb;
5252
Chris Mason74dd17f2012-08-07 16:25:13 -04005253 BUG_ON(*level == 0);
Liu Bofb770ae2016-07-05 12:10:14 -07005254 eb = read_node_slot(root, path->nodes[*level], path->slots[*level]);
5255 if (IS_ERR(eb))
5256 return PTR_ERR(eb);
5257
5258 path->nodes[*level - 1] = eb;
Alexander Block70698302012-06-05 21:07:48 +02005259 path->slots[*level - 1] = 0;
5260 (*level)--;
Liu Bofb770ae2016-07-05 12:10:14 -07005261 return 0;
Alexander Block70698302012-06-05 21:07:48 +02005262}
5263
5264static int tree_move_next_or_upnext(struct btrfs_root *root,
5265 struct btrfs_path *path,
5266 int *level, int root_level)
5267{
5268 int ret = 0;
5269 int nritems;
5270 nritems = btrfs_header_nritems(path->nodes[*level]);
5271
5272 path->slots[*level]++;
5273
Chris Mason74dd17f2012-08-07 16:25:13 -04005274 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005275 if (*level == root_level)
5276 return -1;
5277
5278 /* move upnext */
5279 path->slots[*level] = 0;
5280 free_extent_buffer(path->nodes[*level]);
5281 path->nodes[*level] = NULL;
5282 (*level)++;
5283 path->slots[*level]++;
5284
5285 nritems = btrfs_header_nritems(path->nodes[*level]);
5286 ret = 1;
5287 }
5288 return ret;
5289}
5290
5291/*
5292 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5293 * or down.
5294 */
5295static int tree_advance(struct btrfs_root *root,
5296 struct btrfs_path *path,
5297 int *level, int root_level,
5298 int allow_down,
5299 struct btrfs_key *key)
5300{
5301 int ret;
5302
5303 if (*level == 0 || !allow_down) {
5304 ret = tree_move_next_or_upnext(root, path, level, root_level);
5305 } else {
Liu Bofb770ae2016-07-05 12:10:14 -07005306 ret = tree_move_down(root, path, level, root_level);
Alexander Block70698302012-06-05 21:07:48 +02005307 }
5308 if (ret >= 0) {
5309 if (*level == 0)
5310 btrfs_item_key_to_cpu(path->nodes[*level], key,
5311 path->slots[*level]);
5312 else
5313 btrfs_node_key_to_cpu(path->nodes[*level], key,
5314 path->slots[*level]);
5315 }
5316 return ret;
5317}
5318
5319static int tree_compare_item(struct btrfs_root *left_root,
5320 struct btrfs_path *left_path,
5321 struct btrfs_path *right_path,
5322 char *tmp_buf)
5323{
5324 int cmp;
5325 int len1, len2;
5326 unsigned long off1, off2;
5327
5328 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5329 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5330 if (len1 != len2)
5331 return 1;
5332
5333 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5334 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5335 right_path->slots[0]);
5336
5337 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5338
5339 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5340 if (cmp)
5341 return 1;
5342 return 0;
5343}
5344
5345#define ADVANCE 1
5346#define ADVANCE_ONLY_NEXT -1
5347
5348/*
5349 * This function compares two trees and calls the provided callback for
5350 * every changed/new/deleted item it finds.
5351 * If shared tree blocks are encountered, whole subtrees are skipped, making
5352 * the compare pretty fast on snapshotted subvolumes.
5353 *
5354 * This currently works on commit roots only. As commit roots are read only,
5355 * we don't do any locking. The commit roots are protected with transactions.
5356 * Transactions are ended and rejoined when a commit is tried in between.
5357 *
5358 * This function checks for modifications done to the trees while comparing.
5359 * If it detects a change, it aborts immediately.
5360 */
5361int btrfs_compare_trees(struct btrfs_root *left_root,
5362 struct btrfs_root *right_root,
5363 btrfs_changed_cb_t changed_cb, void *ctx)
5364{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005365 struct btrfs_fs_info *fs_info = left_root->fs_info;
Alexander Block70698302012-06-05 21:07:48 +02005366 int ret;
5367 int cmp;
Alexander Block70698302012-06-05 21:07:48 +02005368 struct btrfs_path *left_path = NULL;
5369 struct btrfs_path *right_path = NULL;
5370 struct btrfs_key left_key;
5371 struct btrfs_key right_key;
5372 char *tmp_buf = NULL;
5373 int left_root_level;
5374 int right_root_level;
5375 int left_level;
5376 int right_level;
5377 int left_end_reached;
5378 int right_end_reached;
5379 int advance_left;
5380 int advance_right;
5381 u64 left_blockptr;
5382 u64 right_blockptr;
Filipe Manana6baa4292014-02-20 21:15:25 +00005383 u64 left_gen;
5384 u64 right_gen;
Alexander Block70698302012-06-05 21:07:48 +02005385
5386 left_path = btrfs_alloc_path();
5387 if (!left_path) {
5388 ret = -ENOMEM;
5389 goto out;
5390 }
5391 right_path = btrfs_alloc_path();
5392 if (!right_path) {
5393 ret = -ENOMEM;
5394 goto out;
5395 }
5396
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 tmp_buf = kmalloc(fs_info->nodesize, GFP_KERNEL | __GFP_NOWARN);
Alexander Block70698302012-06-05 21:07:48 +02005398 if (!tmp_buf) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 tmp_buf = vmalloc(fs_info->nodesize);
David Sterba8f282f72016-03-30 16:01:12 +02005400 if (!tmp_buf) {
5401 ret = -ENOMEM;
5402 goto out;
5403 }
Alexander Block70698302012-06-05 21:07:48 +02005404 }
5405
5406 left_path->search_commit_root = 1;
5407 left_path->skip_locking = 1;
5408 right_path->search_commit_root = 1;
5409 right_path->skip_locking = 1;
5410
Alexander Block70698302012-06-05 21:07:48 +02005411 /*
5412 * Strategy: Go to the first items of both trees. Then do
5413 *
5414 * If both trees are at level 0
5415 * Compare keys of current items
5416 * If left < right treat left item as new, advance left tree
5417 * and repeat
5418 * If left > right treat right item as deleted, advance right tree
5419 * and repeat
5420 * If left == right do deep compare of items, treat as changed if
5421 * needed, advance both trees and repeat
5422 * If both trees are at the same level but not at level 0
5423 * Compare keys of current nodes/leafs
5424 * If left < right advance left tree and repeat
5425 * If left > right advance right tree and repeat
5426 * If left == right compare blockptrs of the next nodes/leafs
5427 * If they match advance both trees but stay at the same level
5428 * and repeat
5429 * If they don't match advance both trees while allowing to go
5430 * deeper and repeat
5431 * If tree levels are different
5432 * Advance the tree that needs it and repeat
5433 *
5434 * Advancing a tree means:
5435 * If we are at level 0, try to go to the next slot. If that's not
5436 * possible, go one level up and repeat. Stop when we found a level
5437 * where we could go to the next slot. We may at this point be on a
5438 * node or a leaf.
5439 *
5440 * If we are not at level 0 and not on shared tree blocks, go one
5441 * level deeper.
5442 *
5443 * If we are not at level 0 and on shared tree blocks, go one slot to
5444 * the right if possible or go up and right.
5445 */
5446
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005447 down_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005448 left_level = btrfs_header_level(left_root->commit_root);
5449 left_root_level = left_level;
5450 left_path->nodes[left_level] = left_root->commit_root;
5451 extent_buffer_get(left_path->nodes[left_level]);
5452
5453 right_level = btrfs_header_level(right_root->commit_root);
5454 right_root_level = right_level;
5455 right_path->nodes[right_level] = right_root->commit_root;
5456 extent_buffer_get(right_path->nodes[right_level]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005457 up_read(&fs_info->commit_root_sem);
Alexander Block70698302012-06-05 21:07:48 +02005458
5459 if (left_level == 0)
5460 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5461 &left_key, left_path->slots[left_level]);
5462 else
5463 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5464 &left_key, left_path->slots[left_level]);
5465 if (right_level == 0)
5466 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5467 &right_key, right_path->slots[right_level]);
5468 else
5469 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5470 &right_key, right_path->slots[right_level]);
5471
5472 left_end_reached = right_end_reached = 0;
5473 advance_left = advance_right = 0;
5474
5475 while (1) {
Alexander Block70698302012-06-05 21:07:48 +02005476 if (advance_left && !left_end_reached) {
5477 ret = tree_advance(left_root, left_path, &left_level,
5478 left_root_level,
5479 advance_left != ADVANCE_ONLY_NEXT,
5480 &left_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005481 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005482 left_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005483 else if (ret < 0)
5484 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005485 advance_left = 0;
5486 }
5487 if (advance_right && !right_end_reached) {
5488 ret = tree_advance(right_root, right_path, &right_level,
5489 right_root_level,
5490 advance_right != ADVANCE_ONLY_NEXT,
5491 &right_key);
Liu Bofb770ae2016-07-05 12:10:14 -07005492 if (ret == -1)
Alexander Block70698302012-06-05 21:07:48 +02005493 right_end_reached = ADVANCE;
Liu Bofb770ae2016-07-05 12:10:14 -07005494 else if (ret < 0)
5495 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005496 advance_right = 0;
5497 }
5498
5499 if (left_end_reached && right_end_reached) {
5500 ret = 0;
5501 goto out;
5502 } else if (left_end_reached) {
5503 if (right_level == 0) {
5504 ret = changed_cb(left_root, right_root,
5505 left_path, right_path,
5506 &right_key,
5507 BTRFS_COMPARE_TREE_DELETED,
5508 ctx);
5509 if (ret < 0)
5510 goto out;
5511 }
5512 advance_right = ADVANCE;
5513 continue;
5514 } else if (right_end_reached) {
5515 if (left_level == 0) {
5516 ret = changed_cb(left_root, right_root,
5517 left_path, right_path,
5518 &left_key,
5519 BTRFS_COMPARE_TREE_NEW,
5520 ctx);
5521 if (ret < 0)
5522 goto out;
5523 }
5524 advance_left = ADVANCE;
5525 continue;
5526 }
5527
5528 if (left_level == 0 && right_level == 0) {
5529 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5530 if (cmp < 0) {
5531 ret = changed_cb(left_root, right_root,
5532 left_path, right_path,
5533 &left_key,
5534 BTRFS_COMPARE_TREE_NEW,
5535 ctx);
5536 if (ret < 0)
5537 goto out;
5538 advance_left = ADVANCE;
5539 } else if (cmp > 0) {
5540 ret = changed_cb(left_root, right_root,
5541 left_path, right_path,
5542 &right_key,
5543 BTRFS_COMPARE_TREE_DELETED,
5544 ctx);
5545 if (ret < 0)
5546 goto out;
5547 advance_right = ADVANCE;
5548 } else {
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005549 enum btrfs_compare_tree_result result;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005550
Chris Mason74dd17f2012-08-07 16:25:13 -04005551 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005552 ret = tree_compare_item(left_root, left_path,
5553 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005554 if (ret)
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005555 result = BTRFS_COMPARE_TREE_CHANGED;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005556 else
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005557 result = BTRFS_COMPARE_TREE_SAME;
Josef Bacikba5e8f22013-08-16 16:52:55 -04005558 ret = changed_cb(left_root, right_root,
5559 left_path, right_path,
Fabian Frederickb99d9a62014-09-25 19:35:02 +02005560 &left_key, result, ctx);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005561 if (ret < 0)
5562 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005563 advance_left = ADVANCE;
5564 advance_right = ADVANCE;
5565 }
5566 } else if (left_level == right_level) {
5567 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5568 if (cmp < 0) {
5569 advance_left = ADVANCE;
5570 } else if (cmp > 0) {
5571 advance_right = ADVANCE;
5572 } else {
5573 left_blockptr = btrfs_node_blockptr(
5574 left_path->nodes[left_level],
5575 left_path->slots[left_level]);
5576 right_blockptr = btrfs_node_blockptr(
5577 right_path->nodes[right_level],
5578 right_path->slots[right_level]);
Filipe Manana6baa4292014-02-20 21:15:25 +00005579 left_gen = btrfs_node_ptr_generation(
5580 left_path->nodes[left_level],
5581 left_path->slots[left_level]);
5582 right_gen = btrfs_node_ptr_generation(
5583 right_path->nodes[right_level],
5584 right_path->slots[right_level]);
5585 if (left_blockptr == right_blockptr &&
5586 left_gen == right_gen) {
Alexander Block70698302012-06-05 21:07:48 +02005587 /*
5588 * As we're on a shared block, don't
5589 * allow to go deeper.
5590 */
5591 advance_left = ADVANCE_ONLY_NEXT;
5592 advance_right = ADVANCE_ONLY_NEXT;
5593 } else {
5594 advance_left = ADVANCE;
5595 advance_right = ADVANCE;
5596 }
5597 }
5598 } else if (left_level < right_level) {
5599 advance_right = ADVANCE;
5600 } else {
5601 advance_left = ADVANCE;
5602 }
5603 }
5604
5605out:
5606 btrfs_free_path(left_path);
5607 btrfs_free_path(right_path);
David Sterba8f282f72016-03-30 16:01:12 +02005608 kvfree(tmp_buf);
Alexander Block70698302012-06-05 21:07:48 +02005609 return ret;
5610}
5611
Chris Mason3f157a22008-06-25 16:01:31 -04005612/*
5613 * this is similar to btrfs_next_leaf, but does not try to preserve
5614 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005615 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005616 *
5617 * 0 is returned if another key is found, < 0 if there are any errors
5618 * and 1 is returned if there are no higher keys in the tree
5619 *
5620 * path->keep_locks should be set to 1 on the search made before
5621 * calling this function.
5622 */
Chris Masone7a84562008-06-25 16:01:31 -04005623int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005624 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005625{
Chris Masone7a84562008-06-25 16:01:31 -04005626 int slot;
5627 struct extent_buffer *c;
5628
Chris Mason934d3752008-12-08 16:43:10 -05005629 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005630 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005631 if (!path->nodes[level])
5632 return 1;
5633
5634 slot = path->slots[level] + 1;
5635 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005636next:
Chris Masone7a84562008-06-25 16:01:31 -04005637 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005638 int ret;
5639 int orig_lowest;
5640 struct btrfs_key cur_key;
5641 if (level + 1 >= BTRFS_MAX_LEVEL ||
5642 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005643 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005644
5645 if (path->locks[level + 1]) {
5646 level++;
5647 continue;
5648 }
5649
5650 slot = btrfs_header_nritems(c) - 1;
5651 if (level == 0)
5652 btrfs_item_key_to_cpu(c, &cur_key, slot);
5653 else
5654 btrfs_node_key_to_cpu(c, &cur_key, slot);
5655
5656 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005657 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005658 path->lowest_level = level;
5659 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5660 0, 0);
5661 path->lowest_level = orig_lowest;
5662 if (ret < 0)
5663 return ret;
5664
5665 c = path->nodes[level];
5666 slot = path->slots[level];
5667 if (ret == 0)
5668 slot++;
5669 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005670 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005671
Chris Masone7a84562008-06-25 16:01:31 -04005672 if (level == 0)
5673 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005674 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005675 u64 gen = btrfs_node_ptr_generation(c, slot);
5676
Chris Mason3f157a22008-06-25 16:01:31 -04005677 if (gen < min_trans) {
5678 slot++;
5679 goto next;
5680 }
Chris Masone7a84562008-06-25 16:01:31 -04005681 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005682 }
Chris Masone7a84562008-06-25 16:01:31 -04005683 return 0;
5684 }
5685 return 1;
5686}
5687
Chris Mason7bb86312007-12-11 09:25:06 -05005688/*
Chris Mason925baed2008-06-25 16:01:30 -04005689 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005690 * returns 0 if it found something or 1 if there are no greater leaves.
5691 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005692 */
Chris Mason234b63a2007-03-13 10:46:10 -04005693int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005694{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005695 return btrfs_next_old_leaf(root, path, 0);
5696}
5697
5698int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5699 u64 time_seq)
5700{
Chris Masond97e63b2007-02-20 16:40:44 -05005701 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005702 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005703 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005704 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005705 struct btrfs_key key;
5706 u32 nritems;
5707 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005708 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005709 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005710
5711 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005712 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005713 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005714
Chris Mason8e73f272009-04-03 10:14:18 -04005715 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5716again:
5717 level = 1;
5718 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005719 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005720 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005721
Chris Masona2135012008-06-25 16:01:30 -04005722 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005723 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005724
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005725 if (time_seq)
5726 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5727 else
5728 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005729 path->keep_locks = 0;
5730
5731 if (ret < 0)
5732 return ret;
5733
Chris Masona2135012008-06-25 16:01:30 -04005734 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005735 /*
5736 * by releasing the path above we dropped all our locks. A balance
5737 * could have added more items next to the key that used to be
5738 * at the very end of the block. So, check again here and
5739 * advance the path if there are now more items available.
5740 */
Chris Masona2135012008-06-25 16:01:30 -04005741 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005742 if (ret == 0)
5743 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005744 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005745 goto done;
5746 }
Liu Bo0b43e042014-06-09 11:04:49 +08005747 /*
5748 * So the above check misses one case:
5749 * - after releasing the path above, someone has removed the item that
5750 * used to be at the very end of the block, and balance between leafs
5751 * gets another one with bigger key.offset to replace it.
5752 *
5753 * This one should be returned as well, or we can get leaf corruption
5754 * later(esp. in __btrfs_drop_extents()).
5755 *
5756 * And a bit more explanation about this check,
5757 * with ret > 0, the key isn't found, the path points to the slot
5758 * where it should be inserted, so the path->slots[0] item must be the
5759 * bigger one.
5760 */
5761 if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
5762 ret = 0;
5763 goto done;
5764 }
Chris Masond97e63b2007-02-20 16:40:44 -05005765
Chris Masond3977122009-01-05 21:25:51 -05005766 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005767 if (!path->nodes[level]) {
5768 ret = 1;
5769 goto done;
5770 }
Chris Mason5f39d392007-10-15 16:14:19 -04005771
Chris Masond97e63b2007-02-20 16:40:44 -05005772 slot = path->slots[level] + 1;
5773 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005774 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005775 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005776 if (level == BTRFS_MAX_LEVEL) {
5777 ret = 1;
5778 goto done;
5779 }
Chris Masond97e63b2007-02-20 16:40:44 -05005780 continue;
5781 }
Chris Mason5f39d392007-10-15 16:14:19 -04005782
Chris Mason925baed2008-06-25 16:01:30 -04005783 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005784 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005785 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005786 }
Chris Mason5f39d392007-10-15 16:14:19 -04005787
Chris Mason8e73f272009-04-03 10:14:18 -04005788 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005789 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005790 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005791 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005792 if (ret == -EAGAIN)
5793 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005794
Chris Mason76a05b32009-05-14 13:24:30 -04005795 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005796 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005797 goto done;
5798 }
5799
Chris Mason5cd57b22008-06-25 16:01:30 -04005800 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005801 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005802 if (!ret && time_seq) {
5803 /*
5804 * If we don't get the lock, we may be racing
5805 * with push_leaf_left, holding that lock while
5806 * itself waiting for the leaf we've currently
5807 * locked. To solve this situation, we give up
5808 * on our lock and cycle.
5809 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005810 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005811 btrfs_release_path(path);
5812 cond_resched();
5813 goto again;
5814 }
Chris Mason8e73f272009-04-03 10:14:18 -04005815 if (!ret) {
5816 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005817 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005818 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005819 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005820 }
Chris Mason31533fb2011-07-26 16:01:59 -04005821 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005822 }
Chris Masond97e63b2007-02-20 16:40:44 -05005823 break;
5824 }
5825 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005826 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005827 level--;
5828 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005829 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005830 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005831
Chris Mason5f39d392007-10-15 16:14:19 -04005832 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005833 path->nodes[level] = next;
5834 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005835 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005836 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005837 if (!level)
5838 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005839
Chris Mason8e73f272009-04-03 10:14:18 -04005840 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005841 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005842 if (ret == -EAGAIN)
5843 goto again;
5844
Chris Mason76a05b32009-05-14 13:24:30 -04005845 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005846 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005847 goto done;
5848 }
5849
Chris Mason5cd57b22008-06-25 16:01:30 -04005850 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005851 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005852 if (!ret) {
5853 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005854 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005855 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005856 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005857 }
Chris Mason31533fb2011-07-26 16:01:59 -04005858 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005859 }
Chris Masond97e63b2007-02-20 16:40:44 -05005860 }
Chris Mason8e73f272009-04-03 10:14:18 -04005861 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005862done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005863 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005864 path->leave_spinning = old_spinning;
5865 if (!old_spinning)
5866 btrfs_set_path_blocking(path);
5867
5868 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005869}
Chris Mason0b86a832008-03-24 15:01:56 -04005870
Chris Mason3f157a22008-06-25 16:01:31 -04005871/*
5872 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5873 * searching until it gets past min_objectid or finds an item of 'type'
5874 *
5875 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5876 */
Chris Mason0b86a832008-03-24 15:01:56 -04005877int btrfs_previous_item(struct btrfs_root *root,
5878 struct btrfs_path *path, u64 min_objectid,
5879 int type)
5880{
5881 struct btrfs_key found_key;
5882 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005883 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005884 int ret;
5885
Chris Masond3977122009-01-05 21:25:51 -05005886 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005887 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005888 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005889 ret = btrfs_prev_leaf(root, path);
5890 if (ret != 0)
5891 return ret;
5892 } else {
5893 path->slots[0]--;
5894 }
5895 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005896 nritems = btrfs_header_nritems(leaf);
5897 if (nritems == 0)
5898 return 1;
5899 if (path->slots[0] == nritems)
5900 path->slots[0]--;
5901
Chris Mason0b86a832008-03-24 15:01:56 -04005902 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005903 if (found_key.objectid < min_objectid)
5904 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005905 if (found_key.type == type)
5906 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005907 if (found_key.objectid == min_objectid &&
5908 found_key.type < type)
5909 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005910 }
5911 return 1;
5912}
Wang Shilongade2e0b2014-01-12 21:38:33 +08005913
5914/*
5915 * search in extent tree to find a previous Metadata/Data extent item with
5916 * min objecitd.
5917 *
5918 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5919 */
5920int btrfs_previous_extent_item(struct btrfs_root *root,
5921 struct btrfs_path *path, u64 min_objectid)
5922{
5923 struct btrfs_key found_key;
5924 struct extent_buffer *leaf;
5925 u32 nritems;
5926 int ret;
5927
5928 while (1) {
5929 if (path->slots[0] == 0) {
5930 btrfs_set_path_blocking(path);
5931 ret = btrfs_prev_leaf(root, path);
5932 if (ret != 0)
5933 return ret;
5934 } else {
5935 path->slots[0]--;
5936 }
5937 leaf = path->nodes[0];
5938 nritems = btrfs_header_nritems(leaf);
5939 if (nritems == 0)
5940 return 1;
5941 if (path->slots[0] == nritems)
5942 path->slots[0]--;
5943
5944 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5945 if (found_key.objectid < min_objectid)
5946 break;
5947 if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
5948 found_key.type == BTRFS_METADATA_ITEM_KEY)
5949 return 0;
5950 if (found_key.objectid == min_objectid &&
5951 found_key.type < BTRFS_EXTENT_ITEM_KEY)
5952 break;
5953 }
5954 return 1;
5955}