blob: 11f9a1848c7becd912bd29da2b19c45dedaa3690 [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>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +000040static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
41 int level, int slot);
Jan Schmidtf2304752012-05-26 11:43:17 +020042static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
43 struct extent_buffer *eb);
Chris Masond97e63b2007-02-20 16:40:44 -050044
Chris Mason2c90e5d2007-04-02 10:50:19 -040045struct btrfs_path *btrfs_alloc_path(void)
46{
Chris Masondf24a2b2007-04-04 09:36:31 -040047 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050048 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040049 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040050}
51
Chris Masonb4ce94d2009-02-04 09:25:08 -050052/*
53 * set all locked nodes in the path to blocking locks. This should
54 * be done before scheduling
55 */
56noinline void btrfs_set_path_blocking(struct btrfs_path *p)
57{
58 int i;
59 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040060 if (!p->nodes[i] || !p->locks[i])
61 continue;
62 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
63 if (p->locks[i] == BTRFS_READ_LOCK)
64 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
65 else if (p->locks[i] == BTRFS_WRITE_LOCK)
66 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050067 }
68}
69
70/*
71 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050072 *
73 * held is used to keep lockdep happy, when lockdep is enabled
74 * we set held to a blocking lock before we go around and
75 * retake all the spinlocks in the path. You can safely use NULL
76 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050077 */
Chris Mason4008c042009-02-12 14:09:45 -050078noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040079 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050080{
81 int i;
Chris Mason4008c042009-02-12 14:09:45 -050082
83#ifdef CONFIG_DEBUG_LOCK_ALLOC
84 /* lockdep really cares that we take all of these spinlocks
85 * in the right order. If any of the locks in the path are not
86 * currently blocking, it is going to complain. So, make really
87 * really sure by forcing the path to blocking before we clear
88 * the path blocking.
89 */
Chris Masonbd681512011-07-16 15:23:14 -040090 if (held) {
91 btrfs_set_lock_blocking_rw(held, held_rw);
92 if (held_rw == BTRFS_WRITE_LOCK)
93 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
94 else if (held_rw == BTRFS_READ_LOCK)
95 held_rw = BTRFS_READ_LOCK_BLOCKING;
96 }
Chris Mason4008c042009-02-12 14:09:45 -050097 btrfs_set_path_blocking(p);
98#endif
99
100 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400101 if (p->nodes[i] && p->locks[i]) {
102 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
103 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
104 p->locks[i] = BTRFS_WRITE_LOCK;
105 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
106 p->locks[i] = BTRFS_READ_LOCK;
107 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500108 }
Chris Mason4008c042009-02-12 14:09:45 -0500109
110#ifdef CONFIG_DEBUG_LOCK_ALLOC
111 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400112 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500113#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400117void btrfs_free_path(struct btrfs_path *p)
118{
Jesper Juhlff175d52010-12-25 21:22:30 +0000119 if (!p)
120 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200121 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400122 kmem_cache_free(btrfs_path_cachep, p);
123}
124
Chris Masond352ac62008-09-29 15:18:18 -0400125/*
126 * path release drops references on the extent buffers in the path
127 * and it drops any locks held by this path
128 *
129 * It is safe to call this on paths that no locks or extent buffers held.
130 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200131noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500132{
133 int i;
Chris Masona2135012008-06-25 16:01:30 -0400134
Chris Mason234b63a2007-03-13 10:46:10 -0400135 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400136 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500137 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400138 continue;
139 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400140 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400141 p->locks[i] = 0;
142 }
Chris Mason5f39d392007-10-15 16:14:19 -0400143 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400144 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500145 }
146}
147
Chris Masond352ac62008-09-29 15:18:18 -0400148/*
149 * safely gets a reference on the root node of a tree. A lock
150 * is not taken, so a concurrent writer may put a different node
151 * at the root of the tree. See btrfs_lock_root_node for the
152 * looping required.
153 *
154 * The extent buffer returned by this has a reference taken, so
155 * it won't disappear. It may stop being the root of the tree
156 * at any time because there are no locks held.
157 */
Chris Mason925baed2008-06-25 16:01:30 -0400158struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
159{
160 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400161
Josef Bacik3083ee22012-03-09 16:01:49 -0500162 while (1) {
163 rcu_read_lock();
164 eb = rcu_dereference(root->node);
165
166 /*
167 * RCU really hurts here, we could free up the root node because
168 * it was cow'ed but we may not get the new root node yet so do
169 * the inc_not_zero dance and if it doesn't work then
170 * synchronize_rcu and try again.
171 */
172 if (atomic_inc_not_zero(&eb->refs)) {
173 rcu_read_unlock();
174 break;
175 }
176 rcu_read_unlock();
177 synchronize_rcu();
178 }
Chris Mason925baed2008-06-25 16:01:30 -0400179 return eb;
180}
181
Chris Masond352ac62008-09-29 15:18:18 -0400182/* loop around taking references on and locking the root node of the
183 * tree until you end up with a lock on the root. A locked buffer
184 * is returned, with a reference held.
185 */
Chris Mason925baed2008-06-25 16:01:30 -0400186struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
187{
188 struct extent_buffer *eb;
189
Chris Masond3977122009-01-05 21:25:51 -0500190 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400191 eb = btrfs_root_node(root);
192 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400193 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400194 break;
Chris Mason925baed2008-06-25 16:01:30 -0400195 btrfs_tree_unlock(eb);
196 free_extent_buffer(eb);
197 }
198 return eb;
199}
200
Chris Masonbd681512011-07-16 15:23:14 -0400201/* loop around taking references on and locking the root node of the
202 * tree until you end up with a lock on the root. A locked buffer
203 * is returned, with a reference held.
204 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000205static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400206{
207 struct extent_buffer *eb;
208
209 while (1) {
210 eb = btrfs_root_node(root);
211 btrfs_tree_read_lock(eb);
212 if (eb == root->node)
213 break;
214 btrfs_tree_read_unlock(eb);
215 free_extent_buffer(eb);
216 }
217 return eb;
218}
219
Chris Masond352ac62008-09-29 15:18:18 -0400220/* cowonly root (everything not a reference counted cow subvolume), just get
221 * put onto a simple dirty list. transaction.c walks this to make sure they
222 * get properly updated on disk.
223 */
Chris Mason0b86a832008-03-24 15:01:56 -0400224static void add_root_to_dirty_list(struct btrfs_root *root)
225{
Chris Masone5846fc2012-05-03 12:08:48 -0400226 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400227 if (root->track_dirty && list_empty(&root->dirty_list)) {
228 list_add(&root->dirty_list,
229 &root->fs_info->dirty_cowonly_roots);
230 }
Chris Masone5846fc2012-05-03 12:08:48 -0400231 spin_unlock(&root->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{
244 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 int ret = 0;
246 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400247 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500248
249 WARN_ON(root->ref_cows && trans->transid !=
250 root->fs_info->running_transaction->transid);
251 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
252
253 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 if (level == 0)
255 btrfs_item_key(buf, &disk_key, 0);
256 else
257 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400258
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400259 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
260 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200261 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400262 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 return PTR_ERR(cow);
264
265 copy_extent_buffer(cow, buf, 0, 0, cow->len);
266 btrfs_set_header_bytenr(cow, cow->start);
267 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400268 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
269 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
270 BTRFS_HEADER_FLAG_RELOC);
271 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
272 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
273 else
274 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500275
Ross Kirk0a4e5582013-09-24 10:12:38 +0100276 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500277 BTRFS_FSID_SIZE);
278
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)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200281 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200283 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
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;
315 u64 index; /* shifted 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/*
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000357 * Increment the upper half of tree_mod_seq, set lower half zero.
358 *
359 * Must be called with fs_info->tree_mod_seq_lock held.
360 */
361static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
362{
363 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
364 seq &= 0xffffffff00000000ull;
365 seq += 1ull << 32;
366 atomic64_set(&fs_info->tree_mod_seq, seq);
367 return seq;
368}
369
370/*
371 * Increment the lower half of tree_mod_seq.
372 *
373 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
374 * are generated should not technically require a spin lock here. (Rationale:
375 * incrementing the minor while incrementing the major seq number is between its
376 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
377 * just returns a unique sequence number as usual.) We have decided to leave
378 * that requirement in here and rethink it once we notice it really imposes a
379 * problem on some workload.
380 */
381static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
382{
383 return atomic64_inc_return(&fs_info->tree_mod_seq);
384}
385
386/*
387 * return the last minor in the previous major tree_mod_seq number
388 */
389u64 btrfs_tree_mod_seq_prev(u64 seq)
390{
391 return (seq & 0xffffffff00000000ull) - 1ull;
392}
393
394/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200395 * This adds a new blocker to the tree mod log's blocker list if the @elem
396 * passed does not already have a sequence number set. So when a caller expects
397 * to record tree modifications, it should ensure to set elem->seq to zero
398 * before calling btrfs_get_tree_mod_seq.
399 * Returns a fresh, unused tree log modification sequence number, even if no new
400 * blocker was added.
401 */
402u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
403 struct seq_list *elem)
404{
405 u64 seq;
406
407 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200408 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200409 if (!elem->seq) {
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000410 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200411 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
412 }
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +0000413 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200414 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200415 tree_mod_log_write_unlock(fs_info);
416
417 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200418}
419
420void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
421 struct seq_list *elem)
422{
423 struct rb_root *tm_root;
424 struct rb_node *node;
425 struct rb_node *next;
426 struct seq_list *cur_elem;
427 struct tree_mod_elem *tm;
428 u64 min_seq = (u64)-1;
429 u64 seq_putting = elem->seq;
430
431 if (!seq_putting)
432 return;
433
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200434 spin_lock(&fs_info->tree_mod_seq_lock);
435 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200436 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200437
438 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200439 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200440 if (seq_putting > cur_elem->seq) {
441 /*
442 * blocker with lower sequence number exists, we
443 * cannot remove anything from the log
444 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200445 spin_unlock(&fs_info->tree_mod_seq_lock);
446 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200447 }
448 min_seq = cur_elem->seq;
449 }
450 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200451 spin_unlock(&fs_info->tree_mod_seq_lock);
452
453 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200454 * anything that's lower than the lowest existing (read: blocked)
455 * sequence number can be removed from the tree.
456 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200457 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200458 tm_root = &fs_info->tree_mod_log;
459 for (node = rb_first(tm_root); node; node = next) {
460 next = rb_next(node);
461 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200462 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200463 continue;
464 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 kfree(tm);
466 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200467 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200468}
469
470/*
471 * key order of the log:
472 * index -> sequence
473 *
474 * the index is the shifted logical of the *new* root node for root replace
475 * operations, or the shifted logical of the affected block for all other
476 * operations.
477 */
478static noinline int
479__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
480{
481 struct rb_root *tm_root;
482 struct rb_node **new;
483 struct rb_node *parent = NULL;
484 struct tree_mod_elem *cur;
Josef Bacikc8cc6342013-07-01 16:18:19 -0400485 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200486
Josef Bacikc8cc6342013-07-01 16:18:19 -0400487 BUG_ON(!tm);
488
489 tree_mod_log_write_lock(fs_info);
490 if (list_empty(&fs_info->tree_mod_seq_list)) {
491 tree_mod_log_write_unlock(fs_info);
492 /*
493 * Ok we no longer care about logging modifications, free up tm
494 * and return 0. Any callers shouldn't be using tm after
495 * calling tree_mod_log_insert, but if they do we can just
496 * change this to return a special error code to let the callers
497 * do their own thing.
498 */
499 kfree(tm);
500 return 0;
501 }
502
503 spin_lock(&fs_info->tree_mod_seq_lock);
504 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
505 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200506
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200507 tm_root = &fs_info->tree_mod_log;
508 new = &tm_root->rb_node;
509 while (*new) {
510 cur = container_of(*new, struct tree_mod_elem, node);
511 parent = *new;
512 if (cur->index < tm->index)
513 new = &((*new)->rb_left);
514 else if (cur->index > tm->index)
515 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200516 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200517 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200518 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200519 new = &((*new)->rb_right);
520 else {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400521 ret = -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200522 kfree(tm);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400523 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200524 }
525 }
526
527 rb_link_node(&tm->node, parent, new);
528 rb_insert_color(&tm->node, tm_root);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400529out:
530 tree_mod_log_write_unlock(fs_info);
531 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200532}
533
Jan Schmidt097b8a72012-06-21 11:08:04 +0200534/*
535 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
536 * returns zero with the tree_mod_log_lock acquired. The caller must hold
537 * this until all tree mod log insertions are recorded in the rb tree and then
538 * call tree_mod_log_write_unlock() to release.
539 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200540static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
541 struct extent_buffer *eb) {
542 smp_mb();
543 if (list_empty(&(fs_info)->tree_mod_seq_list))
544 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200545 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200546 return 1;
547 return 0;
548}
549
Jan Schmidt097b8a72012-06-21 11:08:04 +0200550static inline int
551__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
552 struct extent_buffer *eb, int slot,
553 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200554{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200555 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556
Josef Bacikc8cc6342013-07-01 16:18:19 -0400557 tm = kzalloc(sizeof(*tm), flags);
558 if (!tm)
559 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200560
561 tm->index = eb->start >> PAGE_CACHE_SHIFT;
562 if (op != MOD_LOG_KEY_ADD) {
563 btrfs_node_key(eb, &tm->key, slot);
564 tm->blockptr = btrfs_node_blockptr(eb, slot);
565 }
566 tm->op = op;
567 tm->slot = slot;
568 tm->generation = btrfs_node_ptr_generation(eb, slot);
569
Jan Schmidt097b8a72012-06-21 11:08:04 +0200570 return __tree_mod_log_insert(fs_info, tm);
571}
572
573static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400574tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
575 struct extent_buffer *eb, int slot,
576 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200577{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200578 if (tree_mod_dont_log(fs_info, eb))
579 return 0;
580
Josef Bacikc8cc6342013-07-01 16:18:19 -0400581 return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200582}
583
584static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200585tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
586 struct extent_buffer *eb, int dst_slot, int src_slot,
587 int nr_items, gfp_t flags)
588{
589 struct tree_mod_elem *tm;
590 int ret;
591 int i;
592
Jan Schmidtf3956942012-05-31 15:02:32 +0200593 if (tree_mod_dont_log(fs_info, eb))
594 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200595
Jan Schmidt01763a22012-10-23 15:02:12 +0200596 /*
597 * When we override something during the move, we log these removals.
598 * This can only happen when we move towards the beginning of the
599 * buffer, i.e. dst_slot < src_slot.
600 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200601 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400602 ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
603 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200604 BUG_ON(ret < 0);
605 }
606
Josef Bacikc8cc6342013-07-01 16:18:19 -0400607 tm = kzalloc(sizeof(*tm), flags);
608 if (!tm)
609 return -ENOMEM;
Jan Schmidtf3956942012-05-31 15:02:32 +0200610
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200611 tm->index = eb->start >> PAGE_CACHE_SHIFT;
612 tm->slot = src_slot;
613 tm->move.dst_slot = dst_slot;
614 tm->move.nr_items = nr_items;
615 tm->op = MOD_LOG_MOVE_KEYS;
616
Josef Bacikc8cc6342013-07-01 16:18:19 -0400617 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200618}
619
Jan Schmidt097b8a72012-06-21 11:08:04 +0200620static inline void
621__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
622{
623 int i;
624 u32 nritems;
625 int ret;
626
Chris Masonb12a3b12012-08-07 15:34:49 -0400627 if (btrfs_header_level(eb) == 0)
628 return;
629
Jan Schmidt097b8a72012-06-21 11:08:04 +0200630 nritems = btrfs_header_nritems(eb);
631 for (i = nritems - 1; i >= 0; i--) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400632 ret = __tree_mod_log_insert_key(fs_info, eb, i,
633 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200634 BUG_ON(ret < 0);
635 }
636}
637
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200638static noinline int
639tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
640 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000641 struct extent_buffer *new_root, gfp_t flags,
642 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200643{
644 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200645
Jan Schmidt097b8a72012-06-21 11:08:04 +0200646 if (tree_mod_dont_log(fs_info, NULL))
647 return 0;
648
Jan Schmidt90f8d622013-04-13 13:19:53 +0000649 if (log_removal)
650 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000651
Josef Bacikc8cc6342013-07-01 16:18:19 -0400652 tm = kzalloc(sizeof(*tm), flags);
653 if (!tm)
654 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200655
656 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
657 tm->old_root.logical = old_root->start;
658 tm->old_root.level = btrfs_header_level(old_root);
659 tm->generation = btrfs_header_generation(old_root);
660 tm->op = MOD_LOG_ROOT_REPLACE;
661
Josef Bacikc8cc6342013-07-01 16:18:19 -0400662 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200663}
664
665static struct tree_mod_elem *
666__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
667 int smallest)
668{
669 struct rb_root *tm_root;
670 struct rb_node *node;
671 struct tree_mod_elem *cur = NULL;
672 struct tree_mod_elem *found = NULL;
673 u64 index = start >> PAGE_CACHE_SHIFT;
674
Jan Schmidt097b8a72012-06-21 11:08:04 +0200675 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200676 tm_root = &fs_info->tree_mod_log;
677 node = tm_root->rb_node;
678 while (node) {
679 cur = container_of(node, struct tree_mod_elem, node);
680 if (cur->index < index) {
681 node = node->rb_left;
682 } else if (cur->index > index) {
683 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200684 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200685 node = node->rb_left;
686 } else if (!smallest) {
687 /* we want the node with the highest seq */
688 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200689 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200690 found = cur;
691 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200692 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200693 /* we want the node with the smallest seq */
694 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200695 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200696 found = cur;
697 node = node->rb_right;
698 } else {
699 found = cur;
700 break;
701 }
702 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200703 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200704
705 return found;
706}
707
708/*
709 * this returns the element from the log with the smallest time sequence
710 * value that's in the log (the oldest log item). any element with a time
711 * sequence lower than min_seq will be ignored.
712 */
713static struct tree_mod_elem *
714tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
715 u64 min_seq)
716{
717 return __tree_mod_log_search(fs_info, start, min_seq, 1);
718}
719
720/*
721 * this returns the element from the log with the largest time sequence
722 * value that's in the log (the most recent log item). any element with
723 * a time sequence lower than min_seq will be ignored.
724 */
725static struct tree_mod_elem *
726tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
727{
728 return __tree_mod_log_search(fs_info, start, min_seq, 0);
729}
730
Jan Schmidt097b8a72012-06-21 11:08:04 +0200731static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200732tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
733 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000734 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735{
736 int ret;
737 int i;
738
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200739 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200740 return;
741
Josef Bacikc8cc6342013-07-01 16:18:19 -0400742 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200743 return;
744
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200745 for (i = 0; i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400746 ret = __tree_mod_log_insert_key(fs_info, src,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000747 i + src_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400748 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Jan Schmidt90f8d622013-04-13 13:19:53 +0000749 BUG_ON(ret < 0);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400750 ret = __tree_mod_log_insert_key(fs_info, dst,
Jan Schmidt097b8a72012-06-21 11:08:04 +0200751 i + dst_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400752 MOD_LOG_KEY_ADD,
753 GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200754 BUG_ON(ret < 0);
755 }
756}
757
758static inline void
759tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
760 int dst_offset, int src_offset, int nr_items)
761{
762 int ret;
763 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
764 nr_items, GFP_NOFS);
765 BUG_ON(ret < 0);
766}
767
Jan Schmidt097b8a72012-06-21 11:08:04 +0200768static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200769tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000770 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200771{
772 int ret;
773
Josef Bacikc8cc6342013-07-01 16:18:19 -0400774 ret = __tree_mod_log_insert_key(fs_info, eb, slot,
775 MOD_LOG_KEY_REPLACE,
776 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200777 BUG_ON(ret < 0);
778}
779
Jan Schmidt097b8a72012-06-21 11:08:04 +0200780static noinline void
781tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200782{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200783 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200784 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200785 __tree_mod_log_free_eb(fs_info, eb);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200786}
787
Jan Schmidt097b8a72012-06-21 11:08:04 +0200788static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000790 struct extent_buffer *new_root_node,
791 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792{
793 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200794 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000795 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200796 BUG_ON(ret < 0);
797}
798
Chris Masond352ac62008-09-29 15:18:18 -0400799/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400800 * check if the tree block can be shared by multiple trees
801 */
802int btrfs_block_can_be_shared(struct btrfs_root *root,
803 struct extent_buffer *buf)
804{
805 /*
806 * Tree blocks not in refernece counted trees and tree roots
807 * are never shared. If a block was allocated after the last
808 * snapshot and the block was not allocated by tree relocation,
809 * we know the block is not shared.
810 */
811 if (root->ref_cows &&
812 buf != root->node && buf != root->commit_root &&
813 (btrfs_header_generation(buf) <=
814 btrfs_root_last_snapshot(&root->root_item) ||
815 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
816 return 1;
817#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
818 if (root->ref_cows &&
819 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
820 return 1;
821#endif
822 return 0;
823}
824
825static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
826 struct btrfs_root *root,
827 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400828 struct extent_buffer *cow,
829 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830{
831 u64 refs;
832 u64 owner;
833 u64 flags;
834 u64 new_flags = 0;
835 int ret;
836
837 /*
838 * Backrefs update rules:
839 *
840 * Always use full backrefs for extent pointers in tree block
841 * allocated by tree relocation.
842 *
843 * If a shared tree block is no longer referenced by its owner
844 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
845 * use full backrefs for extent pointers in tree block.
846 *
847 * If a tree block is been relocating
848 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
849 * use full backrefs for extent pointers in tree block.
850 * The reason for this is some operations (such as drop tree)
851 * are only allowed for blocks use full backrefs.
852 */
853
854 if (btrfs_block_can_be_shared(root, buf)) {
855 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500856 btrfs_header_level(buf), 1,
857 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700858 if (ret)
859 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700860 if (refs == 0) {
861 ret = -EROFS;
862 btrfs_std_error(root->fs_info, ret);
863 return ret;
864 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400865 } else {
866 refs = 1;
867 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
868 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
869 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
870 else
871 flags = 0;
872 }
873
874 owner = btrfs_header_owner(buf);
875 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
876 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
877
878 if (refs > 1) {
879 if ((owner == root->root_key.objectid ||
880 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
881 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200882 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100883 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400884
885 if (root->root_key.objectid ==
886 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200887 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100888 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200889 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100890 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400891 }
892 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
893 } else {
894
895 if (root->root_key.objectid ==
896 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200897 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200899 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400901 }
902 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400903 int level = btrfs_header_level(buf);
904
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400905 ret = btrfs_set_disk_extent_flags(trans, root,
906 buf->start,
907 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400908 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700909 if (ret)
910 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400911 }
912 } else {
913 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
914 if (root->root_key.objectid ==
915 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200916 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400917 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200918 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100919 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200920 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100921 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400922 }
923 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400924 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400925 }
926 return 0;
927}
928
929/*
Chris Masond3977122009-01-05 21:25:51 -0500930 * does the dirty work in cow of a single block. The parent block (if
931 * supplied) is updated to point to the new cow copy. The new buffer is marked
932 * dirty and returned locked. If you modify the block it needs to be marked
933 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400934 *
935 * search_start -- an allocation hint for the new block
936 *
Chris Masond3977122009-01-05 21:25:51 -0500937 * empty_size -- a hint that you plan on doing more cow. This is the size in
938 * bytes the allocator should try to find free next to the block it returns.
939 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400940 */
Chris Masond3977122009-01-05 21:25:51 -0500941static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400942 struct btrfs_root *root,
943 struct extent_buffer *buf,
944 struct extent_buffer *parent, int parent_slot,
945 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400946 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400947{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400948 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400949 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700950 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400951 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400952 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400954
Chris Mason925baed2008-06-25 16:01:30 -0400955 if (*cow_ret == buf)
956 unlock_orig = 1;
957
Chris Masonb9447ef82009-03-09 11:45:38 -0400958 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400959
Chris Mason7bb86312007-12-11 09:25:06 -0500960 WARN_ON(root->ref_cows && trans->transid !=
961 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400962 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400963
Chris Mason7bb86312007-12-11 09:25:06 -0500964 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400965
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400966 if (level == 0)
967 btrfs_item_key(buf, &disk_key, 0);
968 else
969 btrfs_node_key(buf, &disk_key, 0);
970
971 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
972 if (parent)
973 parent_start = parent->start;
974 else
975 parent_start = 0;
976 } else
977 parent_start = 0;
978
979 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
980 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200981 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400982 if (IS_ERR(cow))
983 return PTR_ERR(cow);
984
Chris Masonb4ce94d2009-02-04 09:25:08 -0500985 /* cow is set to blocking by btrfs_init_new_buffer */
986
Chris Mason5f39d392007-10-15 16:14:19 -0400987 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400988 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400989 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
991 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
992 BTRFS_HEADER_FLAG_RELOC);
993 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
994 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
995 else
996 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400997
Ross Kirk0a4e5582013-09-24 10:12:38 +0100998 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(),
Yan Zheng2b820322008-11-17 21:11:30 -0500999 BTRFS_FSID_SIZE);
1000
Mark Fashehbe1a5562011-08-08 13:20:18 -07001001 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001002 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001003 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001004 return ret;
1005 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001006
Josef Bacik83d4cfd2013-08-30 15:09:51 -04001007 if (root->ref_cows) {
1008 ret = btrfs_reloc_cow_block(trans, root, buf, cow);
1009 if (ret)
1010 return ret;
1011 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001012
Chris Mason6702ed42007-08-07 16:15:09 -04001013 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001014 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001015 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1016 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1017 parent_start = buf->start;
1018 else
1019 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001020
Chris Mason5f39d392007-10-15 16:14:19 -04001021 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001022 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001023 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001024
Yan, Zhengf0486c62010-05-16 10:46:25 -04001025 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001026 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001027 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001028 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001029 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001030 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1031 parent_start = parent->start;
1032 else
1033 parent_start = 0;
1034
1035 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001036 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001037 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001038 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001039 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001040 btrfs_set_node_ptr_generation(parent, parent_slot,
1041 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001042 btrfs_mark_buffer_dirty(parent);
Josef Bacik7fb7d76f2013-07-01 16:10:16 -04001043 if (last_ref)
1044 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001045 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001046 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001047 }
Chris Mason925baed2008-06-25 16:01:30 -04001048 if (unlock_orig)
1049 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001050 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001051 btrfs_mark_buffer_dirty(cow);
1052 *cow_ret = cow;
1053 return 0;
1054}
1055
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001056/*
1057 * returns the logical address of the oldest predecessor of the given root.
1058 * entries older than time_seq are ignored.
1059 */
1060static struct tree_mod_elem *
1061__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001062 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001063{
1064 struct tree_mod_elem *tm;
1065 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001066 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001067 int looped = 0;
1068
1069 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001070 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001071
1072 /*
1073 * the very last operation that's logged for a root is the replacement
1074 * operation (if it is replaced at all). this has the index of the *new*
1075 * root, making it the very first operation that's logged for this root.
1076 */
1077 while (1) {
1078 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1079 time_seq);
1080 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001081 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001082 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001083 * if there are no tree operation for the oldest root, we simply
1084 * return it. this should only happen if that (old) root is at
1085 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001086 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001087 if (!tm)
1088 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001089
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001090 /*
1091 * if there's an operation that's not a root replacement, we
1092 * found the oldest version of our root. normally, we'll find a
1093 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1094 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001095 if (tm->op != MOD_LOG_ROOT_REPLACE)
1096 break;
1097
1098 found = tm;
1099 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001100 looped = 1;
1101 }
1102
Jan Schmidta95236d2012-06-05 16:41:24 +02001103 /* if there's no old root to return, return what we found instead */
1104 if (!found)
1105 found = tm;
1106
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001107 return found;
1108}
1109
1110/*
1111 * tm is a pointer to the first operation to rewind within eb. then, all
1112 * previous operations will be rewinded (until we reach something older than
1113 * time_seq).
1114 */
1115static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001116__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1117 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001118{
1119 u32 n;
1120 struct rb_node *next;
1121 struct tree_mod_elem *tm = first_tm;
1122 unsigned long o_dst;
1123 unsigned long o_src;
1124 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1125
1126 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001127 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001128 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001129 /*
1130 * all the operations are recorded with the operator used for
1131 * the modification. as we're going backwards, we do the
1132 * opposite of each operation here.
1133 */
1134 switch (tm->op) {
1135 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1136 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001137 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001138 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001139 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001140 btrfs_set_node_key(eb, &tm->key, tm->slot);
1141 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1142 btrfs_set_node_ptr_generation(eb, tm->slot,
1143 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001144 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001145 break;
1146 case MOD_LOG_KEY_REPLACE:
1147 BUG_ON(tm->slot >= n);
1148 btrfs_set_node_key(eb, &tm->key, tm->slot);
1149 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1150 btrfs_set_node_ptr_generation(eb, tm->slot,
1151 tm->generation);
1152 break;
1153 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001154 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 n--;
1156 break;
1157 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001158 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1159 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1160 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001161 tm->move.nr_items * p_size);
1162 break;
1163 case MOD_LOG_ROOT_REPLACE:
1164 /*
1165 * this operation is special. for roots, this must be
1166 * handled explicitly before rewinding.
1167 * for non-roots, this operation may exist if the node
1168 * was a root: root A -> child B; then A gets empty and
1169 * B is promoted to the new root. in the mod log, we'll
1170 * have a root-replace operation for B, a tree block
1171 * that is no root. we simply ignore that operation.
1172 */
1173 break;
1174 }
1175 next = rb_next(&tm->node);
1176 if (!next)
1177 break;
1178 tm = container_of(next, struct tree_mod_elem, node);
1179 if (tm->index != first_tm->index)
1180 break;
1181 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001182 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001183 btrfs_set_header_nritems(eb, n);
1184}
1185
Jan Schmidt47fb0912013-04-13 13:19:55 +00001186/*
1187 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1188 * is returned. If rewind operations happen, a fresh buffer is returned. The
1189 * returned buffer is always read-locked. If the returned buffer is not the
1190 * input buffer, the lock on the input buffer is released and the input buffer
1191 * is freed (its refcount is decremented).
1192 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001193static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001194tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1195 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001196{
1197 struct extent_buffer *eb_rewin;
1198 struct tree_mod_elem *tm;
1199
1200 if (!time_seq)
1201 return eb;
1202
1203 if (btrfs_header_level(eb) == 0)
1204 return eb;
1205
1206 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1207 if (!tm)
1208 return eb;
1209
Josef Bacik9ec72672013-08-07 16:57:23 -04001210 btrfs_set_path_blocking(path);
1211 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1212
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001213 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1214 BUG_ON(tm->slot != 0);
1215 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1216 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001217 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001218 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001219 free_extent_buffer(eb);
1220 return NULL;
1221 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001222 btrfs_set_header_bytenr(eb_rewin, eb->start);
1223 btrfs_set_header_backref_rev(eb_rewin,
1224 btrfs_header_backref_rev(eb));
1225 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001226 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001227 } else {
1228 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001229 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001230 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001231 free_extent_buffer(eb);
1232 return NULL;
1233 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001234 }
1235
Josef Bacik9ec72672013-08-07 16:57:23 -04001236 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1237 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001238 free_extent_buffer(eb);
1239
Jan Schmidt47fb0912013-04-13 13:19:55 +00001240 extent_buffer_get(eb_rewin);
1241 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001242 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001243 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001244 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001245
1246 return eb_rewin;
1247}
1248
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001249/*
1250 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1251 * value. If there are no changes, the current root->root_node is returned. If
1252 * anything changed in between, there's a fresh buffer allocated on which the
1253 * rewind operations are done. In any case, the returned buffer is read locked.
1254 * Returns NULL on error (with no locks held).
1255 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001256static inline struct extent_buffer *
1257get_old_root(struct btrfs_root *root, u64 time_seq)
1258{
1259 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001260 struct extent_buffer *eb = NULL;
1261 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001262 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001263 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001264 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001265 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001266 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001267
Jan Schmidt30b04632013-04-13 13:19:54 +00001268 eb_root = btrfs_read_lock_root_node(root);
1269 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001270 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001271 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001272
Jan Schmidta95236d2012-06-05 16:41:24 +02001273 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1274 old_root = &tm->old_root;
1275 old_generation = tm->generation;
1276 logical = old_root->logical;
1277 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001278 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001279 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280
Jan Schmidta95236d2012-06-05 16:41:24 +02001281 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001282 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001283 btrfs_tree_read_unlock(eb_root);
1284 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001285 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001286 old = read_tree_block(root, logical, blocksize, 0);
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301287 if (WARN_ON(!old || !extent_buffer_uptodate(old))) {
Josef Bacik416bc652013-04-23 14:17:42 -04001288 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001289 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1290 logical);
Jan Schmidt834328a2012-10-23 11:27:33 +02001291 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001292 eb = btrfs_clone_extent_buffer(old);
1293 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001294 }
1295 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001296 btrfs_tree_read_unlock(eb_root);
1297 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001298 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001299 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001300 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001301 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001302 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001303 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001304 }
1305
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001306 if (!eb)
1307 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001308 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001309 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001310 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001311 btrfs_set_header_bytenr(eb, eb->start);
1312 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001313 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001314 btrfs_set_header_level(eb, old_root->level);
1315 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001316 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001317 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001318 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001319 else
1320 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001321 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001322
1323 return eb;
1324}
1325
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001326int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1327{
1328 struct tree_mod_elem *tm;
1329 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001330 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001331
Jan Schmidt30b04632013-04-13 13:19:54 +00001332 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001333 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1334 level = tm->old_root.level;
1335 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001336 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001337 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001338 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001339
1340 return level;
1341}
1342
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001343static inline int should_cow_block(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *root,
1345 struct extent_buffer *buf)
1346{
Liu Bof1ebcc72011-11-14 20:48:06 -05001347 /* ensure we can see the force_cow */
1348 smp_rmb();
1349
1350 /*
1351 * We do not need to cow a block if
1352 * 1) this block is not created or changed in this transaction;
1353 * 2) this block does not belong to TREE_RELOC tree;
1354 * 3) the root is not forced COW.
1355 *
1356 * What is forced COW:
1357 * when we create snapshot during commiting the transaction,
1358 * after we've finished coping src root, we must COW the shared
1359 * block to ensure the metadata consistency.
1360 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001361 if (btrfs_header_generation(buf) == trans->transid &&
1362 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1363 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001364 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1365 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001366 return 0;
1367 return 1;
1368}
1369
Chris Masond352ac62008-09-29 15:18:18 -04001370/*
1371 * cows a single block, see __btrfs_cow_block for the real work.
1372 * This version of it has extra checks so that a block isn't cow'd more than
1373 * once per transaction, as long as it hasn't been written yet
1374 */
Chris Masond3977122009-01-05 21:25:51 -05001375noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001376 struct btrfs_root *root, struct extent_buffer *buf,
1377 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001378 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001379{
Chris Mason6702ed42007-08-07 16:15:09 -04001380 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001381 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001382
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001383 if (trans->transaction != root->fs_info->running_transaction)
1384 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001385 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001386 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001387
1388 if (trans->transid != root->fs_info->generation)
1389 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001390 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001391
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001392 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001393 *cow_ret = buf;
1394 return 0;
1395 }
Chris Masonc4876852009-02-04 09:24:25 -05001396
Chris Mason0b86a832008-03-24 15:01:56 -04001397 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001398
1399 if (parent)
1400 btrfs_set_lock_blocking(parent);
1401 btrfs_set_lock_blocking(buf);
1402
Chris Masonf510cfe2007-10-15 16:14:48 -04001403 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001404 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001405
1406 trace_btrfs_cow_block(root, buf, *cow_ret);
1407
Chris Masonf510cfe2007-10-15 16:14:48 -04001408 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001409}
1410
Chris Masond352ac62008-09-29 15:18:18 -04001411/*
1412 * helper function for defrag to decide if two blocks pointed to by a
1413 * node are actually close by
1414 */
Chris Mason6b800532007-10-15 16:17:34 -04001415static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001416{
Chris Mason6b800532007-10-15 16:17:34 -04001417 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001418 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001419 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001420 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001421 return 0;
1422}
1423
Chris Mason081e9572007-11-06 10:26:24 -05001424/*
1425 * compare two keys in a memcmp fashion
1426 */
1427static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1428{
1429 struct btrfs_key k1;
1430
1431 btrfs_disk_key_to_cpu(&k1, disk);
1432
Diego Calleja20736ab2009-07-24 11:06:52 -04001433 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001434}
1435
Josef Bacikf3465ca2008-11-12 14:19:50 -05001436/*
1437 * same as comp_keys only with two btrfs_key's
1438 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001439int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001440{
1441 if (k1->objectid > k2->objectid)
1442 return 1;
1443 if (k1->objectid < k2->objectid)
1444 return -1;
1445 if (k1->type > k2->type)
1446 return 1;
1447 if (k1->type < k2->type)
1448 return -1;
1449 if (k1->offset > k2->offset)
1450 return 1;
1451 if (k1->offset < k2->offset)
1452 return -1;
1453 return 0;
1454}
Chris Mason081e9572007-11-06 10:26:24 -05001455
Chris Masond352ac62008-09-29 15:18:18 -04001456/*
1457 * this is used by the defrag code to go through all the
1458 * leaves pointed to by a node and reallocate them so that
1459 * disk order is close to key order
1460 */
Chris Mason6702ed42007-08-07 16:15:09 -04001461int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001462 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001463 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001464 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001465{
Chris Mason6b800532007-10-15 16:17:34 -04001466 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001467 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001468 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001469 u64 search_start = *last_ret;
1470 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001471 u64 other;
1472 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001473 int end_slot;
1474 int i;
1475 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001476 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001477 int uptodate;
1478 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001479 int progress_passed = 0;
1480 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001481
Chris Mason5708b952007-10-25 15:43:18 -04001482 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001483
Julia Lawall6c1500f2012-11-03 20:30:18 +00001484 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1485 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001486
Chris Mason6b800532007-10-15 16:17:34 -04001487 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001488 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001489 end_slot = parent_nritems;
1490
1491 if (parent_nritems == 1)
1492 return 0;
1493
Chris Masonb4ce94d2009-02-04 09:25:08 -05001494 btrfs_set_lock_blocking(parent);
1495
Chris Mason6702ed42007-08-07 16:15:09 -04001496 for (i = start_slot; i < end_slot; i++) {
1497 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001498
Chris Mason081e9572007-11-06 10:26:24 -05001499 btrfs_node_key(parent, &disk_key, i);
1500 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1501 continue;
1502
1503 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001504 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001505 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001506 if (last_block == 0)
1507 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001508
Chris Mason6702ed42007-08-07 16:15:09 -04001509 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001510 other = btrfs_node_blockptr(parent, i - 1);
1511 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001512 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001513 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001514 other = btrfs_node_blockptr(parent, i + 1);
1515 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001516 }
Chris Masone9d0b132007-08-10 14:06:19 -04001517 if (close) {
1518 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001519 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001520 }
Chris Mason6702ed42007-08-07 16:15:09 -04001521
Chris Mason6b800532007-10-15 16:17:34 -04001522 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1523 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001524 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001525 else
1526 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001527 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001528 if (!cur) {
1529 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001530 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001531 if (!cur || !extent_buffer_uptodate(cur)) {
1532 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001533 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001534 }
Chris Mason6b800532007-10-15 16:17:34 -04001535 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001536 err = btrfs_read_buffer(cur, gen);
1537 if (err) {
1538 free_extent_buffer(cur);
1539 return err;
1540 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001541 }
Chris Mason6702ed42007-08-07 16:15:09 -04001542 }
Chris Masone9d0b132007-08-10 14:06:19 -04001543 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001544 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001545
Chris Masone7a84562008-06-25 16:01:31 -04001546 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001547 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001548 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001549 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001550 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001551 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001552 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001553 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001554 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001555 break;
Yan252c38f2007-08-29 09:11:44 -04001556 }
Chris Masone7a84562008-06-25 16:01:31 -04001557 search_start = cur->start;
1558 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001559 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001560 btrfs_tree_unlock(cur);
1561 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001562 }
1563 return err;
1564}
1565
Chris Mason74123bd2007-02-02 11:05:29 -05001566/*
1567 * The leaf data grows from end-to-front in the node.
1568 * this returns the address of the start of the last item,
1569 * which is the stop of the leaf data stack
1570 */
Chris Mason123abc82007-03-14 14:14:43 -04001571static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001572 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001573{
Chris Mason5f39d392007-10-15 16:14:19 -04001574 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001575 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001576 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001577 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001578}
1579
Chris Masonaa5d6be2007-02-28 16:35:06 -05001580
Chris Mason74123bd2007-02-02 11:05:29 -05001581/*
Chris Mason5f39d392007-10-15 16:14:19 -04001582 * search for key in the extent_buffer. The items start at offset p,
1583 * and they are item_size apart. There are 'max' items in p.
1584 *
Chris Mason74123bd2007-02-02 11:05:29 -05001585 * the slot in the array is returned via slot, and it points to
1586 * the place where you would insert key if it is not found in
1587 * the array.
1588 *
1589 * slot may point to max if the key is bigger than all of the keys
1590 */
Chris Masone02119d2008-09-05 16:13:11 -04001591static noinline int generic_bin_search(struct extent_buffer *eb,
1592 unsigned long p,
1593 int item_size, struct btrfs_key *key,
1594 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001595{
1596 int low = 0;
1597 int high = max;
1598 int mid;
1599 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001600 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001601 struct btrfs_disk_key unaligned;
1602 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001603 char *kaddr = NULL;
1604 unsigned long map_start = 0;
1605 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001606 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001607
Chris Masond3977122009-01-05 21:25:51 -05001608 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001609 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001610 offset = p + mid * item_size;
1611
Chris Masona6591712011-07-19 12:04:14 -04001612 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001613 (offset + sizeof(struct btrfs_disk_key)) >
1614 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001615
1616 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001617 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001618 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001619
Chris Mason479965d2007-10-15 16:14:27 -04001620 if (!err) {
1621 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1622 map_start);
1623 } else {
1624 read_extent_buffer(eb, &unaligned,
1625 offset, sizeof(unaligned));
1626 tmp = &unaligned;
1627 }
1628
Chris Mason5f39d392007-10-15 16:14:19 -04001629 } else {
1630 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1631 map_start);
1632 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001633 ret = comp_keys(tmp, key);
1634
1635 if (ret < 0)
1636 low = mid + 1;
1637 else if (ret > 0)
1638 high = mid;
1639 else {
1640 *slot = mid;
1641 return 0;
1642 }
1643 }
1644 *slot = low;
1645 return 1;
1646}
1647
Chris Mason97571fd2007-02-24 13:39:08 -05001648/*
1649 * simple bin_search frontend that does the right thing for
1650 * leaves vs nodes
1651 */
Chris Mason5f39d392007-10-15 16:14:19 -04001652static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1653 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001654{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001655 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001656 return generic_bin_search(eb,
1657 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001658 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001659 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001660 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001661 else
Chris Mason5f39d392007-10-15 16:14:19 -04001662 return generic_bin_search(eb,
1663 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001664 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001665 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001666 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001667}
1668
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001669int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1670 int level, int *slot)
1671{
1672 return bin_search(eb, key, level, slot);
1673}
1674
Yan, Zhengf0486c62010-05-16 10:46:25 -04001675static void root_add_used(struct btrfs_root *root, u32 size)
1676{
1677 spin_lock(&root->accounting_lock);
1678 btrfs_set_root_used(&root->root_item,
1679 btrfs_root_used(&root->root_item) + size);
1680 spin_unlock(&root->accounting_lock);
1681}
1682
1683static void root_sub_used(struct btrfs_root *root, u32 size)
1684{
1685 spin_lock(&root->accounting_lock);
1686 btrfs_set_root_used(&root->root_item,
1687 btrfs_root_used(&root->root_item) - size);
1688 spin_unlock(&root->accounting_lock);
1689}
1690
Chris Masond352ac62008-09-29 15:18:18 -04001691/* given a node and slot number, this reads the blocks it points to. The
1692 * extent buffer is returned with a reference taken (but unlocked).
1693 * NULL is returned on error.
1694 */
Chris Masone02119d2008-09-05 16:13:11 -04001695static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001696 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001697{
Chris Masonca7a79a2008-05-12 12:59:19 -04001698 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001699 struct extent_buffer *eb;
1700
Chris Masonbb803952007-03-01 12:04:21 -05001701 if (slot < 0)
1702 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001703 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001704 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001705
1706 BUG_ON(level == 0);
1707
Josef Bacik416bc652013-04-23 14:17:42 -04001708 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1709 btrfs_level_size(root, level - 1),
1710 btrfs_node_ptr_generation(parent, slot));
1711 if (eb && !extent_buffer_uptodate(eb)) {
1712 free_extent_buffer(eb);
1713 eb = NULL;
1714 }
1715
1716 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001717}
1718
Chris Masond352ac62008-09-29 15:18:18 -04001719/*
1720 * node level balancing, used to make sure nodes are in proper order for
1721 * item deletion. We balance from the top down, so we have to make sure
1722 * that a deletion won't leave an node completely empty later on.
1723 */
Chris Masone02119d2008-09-05 16:13:11 -04001724static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001725 struct btrfs_root *root,
1726 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001727{
Chris Mason5f39d392007-10-15 16:14:19 -04001728 struct extent_buffer *right = NULL;
1729 struct extent_buffer *mid;
1730 struct extent_buffer *left = NULL;
1731 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001732 int ret = 0;
1733 int wret;
1734 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001735 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001736 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001737
1738 if (level == 0)
1739 return 0;
1740
Chris Mason5f39d392007-10-15 16:14:19 -04001741 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001742
Chris Masonbd681512011-07-16 15:23:14 -04001743 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1744 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001745 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1746
Chris Mason1d4f8a02007-03-13 09:28:32 -04001747 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001748
Li Zefana05a9bb2011-09-06 16:55:34 +08001749 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001750 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001751 pslot = path->slots[level + 1];
1752 }
Chris Masonbb803952007-03-01 12:04:21 -05001753
Chris Mason40689472007-03-17 14:29:23 -04001754 /*
1755 * deal with the case where there is only one pointer in the root
1756 * by promoting the node below to a root
1757 */
Chris Mason5f39d392007-10-15 16:14:19 -04001758 if (!parent) {
1759 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001760
Chris Mason5f39d392007-10-15 16:14:19 -04001761 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001762 return 0;
1763
1764 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001765 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001766 if (!child) {
1767 ret = -EROFS;
1768 btrfs_std_error(root->fs_info, ret);
1769 goto enospc;
1770 }
1771
Chris Mason925baed2008-06-25 16:01:30 -04001772 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001773 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001774 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001775 if (ret) {
1776 btrfs_tree_unlock(child);
1777 free_extent_buffer(child);
1778 goto enospc;
1779 }
Yan2f375ab2008-02-01 14:58:07 -05001780
Jan Schmidt90f8d622013-04-13 13:19:53 +00001781 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001782 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001783
Chris Mason0b86a832008-03-24 15:01:56 -04001784 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001785 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001786
Chris Mason925baed2008-06-25 16:01:30 -04001787 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001788 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001789 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001790 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001791 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001792 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001793
1794 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001795 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001796 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001797 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001798 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001799 }
Chris Mason5f39d392007-10-15 16:14:19 -04001800 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001801 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001802 return 0;
1803
Chris Mason5f39d392007-10-15 16:14:19 -04001804 left = read_node_slot(root, parent, pslot - 1);
1805 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001806 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001807 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001808 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001809 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001810 if (wret) {
1811 ret = wret;
1812 goto enospc;
1813 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001814 }
Chris Mason5f39d392007-10-15 16:14:19 -04001815 right = read_node_slot(root, parent, pslot + 1);
1816 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001817 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001818 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001819 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001820 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001821 if (wret) {
1822 ret = wret;
1823 goto enospc;
1824 }
1825 }
1826
1827 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001828 if (left) {
1829 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001830 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001831 if (wret < 0)
1832 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001833 }
Chris Mason79f95c82007-03-01 15:16:26 -05001834
1835 /*
1836 * then try to empty the right most buffer into the middle
1837 */
Chris Mason5f39d392007-10-15 16:14:19 -04001838 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001839 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001840 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001841 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001842 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001843 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001844 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001845 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001846 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001847 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001848 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001849 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001850 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001851 struct btrfs_disk_key right_key;
1852 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001853 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001854 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001855 btrfs_set_node_key(parent, &right_key, pslot + 1);
1856 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001857 }
1858 }
Chris Mason5f39d392007-10-15 16:14:19 -04001859 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001860 /*
1861 * we're not allowed to leave a node with one item in the
1862 * tree during a delete. A deletion from lower in the tree
1863 * could try to delete the only pointer in this node.
1864 * So, pull some keys from the left.
1865 * There has to be a left pointer at this point because
1866 * otherwise we would have pulled some pointers from the
1867 * right
1868 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001869 if (!left) {
1870 ret = -EROFS;
1871 btrfs_std_error(root->fs_info, ret);
1872 goto enospc;
1873 }
Chris Mason5f39d392007-10-15 16:14:19 -04001874 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001875 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001876 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001877 goto enospc;
1878 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001879 if (wret == 1) {
1880 wret = push_node_left(trans, root, left, mid, 1);
1881 if (wret < 0)
1882 ret = wret;
1883 }
Chris Mason79f95c82007-03-01 15:16:26 -05001884 BUG_ON(wret == 1);
1885 }
Chris Mason5f39d392007-10-15 16:14:19 -04001886 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001887 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001888 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001889 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001890 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001891 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001892 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001893 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001894 } else {
1895 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001896 struct btrfs_disk_key mid_key;
1897 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001898 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001899 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001900 btrfs_set_node_key(parent, &mid_key, pslot);
1901 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001902 }
Chris Masonbb803952007-03-01 12:04:21 -05001903
Chris Mason79f95c82007-03-01 15:16:26 -05001904 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001905 if (left) {
1906 if (btrfs_header_nritems(left) > orig_slot) {
1907 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001908 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001909 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001910 path->slots[level + 1] -= 1;
1911 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001912 if (mid) {
1913 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001914 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001915 }
Chris Masonbb803952007-03-01 12:04:21 -05001916 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001917 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001918 path->slots[level] = orig_slot;
1919 }
1920 }
Chris Mason79f95c82007-03-01 15:16:26 -05001921 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001922 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001923 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001924 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001925enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001926 if (right) {
1927 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001928 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001929 }
1930 if (left) {
1931 if (path->nodes[level] != left)
1932 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001933 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001934 }
Chris Masonbb803952007-03-01 12:04:21 -05001935 return ret;
1936}
1937
Chris Masond352ac62008-09-29 15:18:18 -04001938/* Node balancing for insertion. Here we only split or push nodes around
1939 * when they are completely full. This is also done top down, so we
1940 * have to be pessimistic.
1941 */
Chris Masond3977122009-01-05 21:25:51 -05001942static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001943 struct btrfs_root *root,
1944 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001945{
Chris Mason5f39d392007-10-15 16:14:19 -04001946 struct extent_buffer *right = NULL;
1947 struct extent_buffer *mid;
1948 struct extent_buffer *left = NULL;
1949 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001950 int ret = 0;
1951 int wret;
1952 int pslot;
1953 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001954
1955 if (level == 0)
1956 return 1;
1957
Chris Mason5f39d392007-10-15 16:14:19 -04001958 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001959 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001960
Li Zefana05a9bb2011-09-06 16:55:34 +08001961 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001962 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001963 pslot = path->slots[level + 1];
1964 }
Chris Masone66f7092007-04-20 13:16:02 -04001965
Chris Mason5f39d392007-10-15 16:14:19 -04001966 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001967 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001968
Chris Mason5f39d392007-10-15 16:14:19 -04001969 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001970
1971 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001972 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001973 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001974
1975 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001976 btrfs_set_lock_blocking(left);
1977
Chris Mason5f39d392007-10-15 16:14:19 -04001978 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001979 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1980 wret = 1;
1981 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001982 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001983 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001984 if (ret)
1985 wret = 1;
1986 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001987 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001988 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001989 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001990 }
Chris Masone66f7092007-04-20 13:16:02 -04001991 if (wret < 0)
1992 ret = wret;
1993 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001994 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001995 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001996 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001997 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001998 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001999 btrfs_set_node_key(parent, &disk_key, pslot);
2000 btrfs_mark_buffer_dirty(parent);
2001 if (btrfs_header_nritems(left) > orig_slot) {
2002 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002003 path->slots[level + 1] -= 1;
2004 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002005 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002006 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002007 } else {
2008 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002009 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002010 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002011 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002012 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002013 }
Chris Masone66f7092007-04-20 13:16:02 -04002014 return 0;
2015 }
Chris Mason925baed2008-06-25 16:01:30 -04002016 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002017 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002018 }
Chris Mason925baed2008-06-25 16:01:30 -04002019 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002020
2021 /*
2022 * then try to empty the right most buffer into the middle
2023 */
Chris Mason5f39d392007-10-15 16:14:19 -04002024 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002025 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002026
Chris Mason925baed2008-06-25 16:01:30 -04002027 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002028 btrfs_set_lock_blocking(right);
2029
Chris Mason5f39d392007-10-15 16:14:19 -04002030 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002031 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2032 wret = 1;
2033 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002034 ret = btrfs_cow_block(trans, root, right,
2035 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002036 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002037 if (ret)
2038 wret = 1;
2039 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002040 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002041 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002042 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002043 }
Chris Masone66f7092007-04-20 13:16:02 -04002044 if (wret < 0)
2045 ret = wret;
2046 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002047 struct btrfs_disk_key disk_key;
2048
2049 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002050 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002051 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002052 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2053 btrfs_mark_buffer_dirty(parent);
2054
2055 if (btrfs_header_nritems(mid) <= orig_slot) {
2056 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002057 path->slots[level + 1] += 1;
2058 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002059 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002060 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002062 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002063 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002064 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002065 }
Chris Masone66f7092007-04-20 13:16:02 -04002066 return 0;
2067 }
Chris Mason925baed2008-06-25 16:01:30 -04002068 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002069 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002070 }
Chris Masone66f7092007-04-20 13:16:02 -04002071 return 1;
2072}
2073
Chris Mason74123bd2007-02-02 11:05:29 -05002074/*
Chris Masond352ac62008-09-29 15:18:18 -04002075 * readahead one full node of leaves, finding things that are close
2076 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002077 */
Chris Masonc8c42862009-04-03 10:14:18 -04002078static void reada_for_search(struct btrfs_root *root,
2079 struct btrfs_path *path,
2080 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002081{
Chris Mason5f39d392007-10-15 16:14:19 -04002082 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002083 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002084 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002085 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002086 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002087 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002088 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002089 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002090 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002091 u32 nr;
2092 u32 blocksize;
2093 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002094
Chris Masona6b6e752007-10-15 16:22:39 -04002095 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002096 return;
2097
Chris Mason6702ed42007-08-07 16:15:09 -04002098 if (!path->nodes[level])
2099 return;
2100
Chris Mason5f39d392007-10-15 16:14:19 -04002101 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002102
Chris Mason3c69fae2007-08-07 15:52:22 -04002103 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002104 blocksize = btrfs_level_size(root, level - 1);
2105 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 if (eb) {
2107 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002108 return;
2109 }
2110
Chris Masona7175312009-01-22 09:23:10 -05002111 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002112
Chris Mason5f39d392007-10-15 16:14:19 -04002113 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002114 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002115
Chris Masond3977122009-01-05 21:25:51 -05002116 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002117 if (direction < 0) {
2118 if (nr == 0)
2119 break;
2120 nr--;
2121 } else if (direction > 0) {
2122 nr++;
2123 if (nr >= nritems)
2124 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002125 }
Chris Mason01f46652007-12-21 16:24:26 -05002126 if (path->reada < 0 && objectid) {
2127 btrfs_node_key(node, &disk_key, nr);
2128 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2129 break;
2130 }
Chris Mason6b800532007-10-15 16:17:34 -04002131 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002132 if ((search <= target && target - search <= 65536) ||
2133 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002134 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002135 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002136 nread += blocksize;
2137 }
2138 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002139 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002140 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002141 }
2142}
Chris Mason925baed2008-06-25 16:01:30 -04002143
Josef Bacik0b088512013-06-17 14:23:02 -04002144static noinline void reada_for_balance(struct btrfs_root *root,
2145 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002146{
2147 int slot;
2148 int nritems;
2149 struct extent_buffer *parent;
2150 struct extent_buffer *eb;
2151 u64 gen;
2152 u64 block1 = 0;
2153 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002154 int blocksize;
2155
Chris Mason8c594ea2009-04-20 15:50:10 -04002156 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002157 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002158 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002159
2160 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002161 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002162 blocksize = btrfs_level_size(root, level);
2163
2164 if (slot > 0) {
2165 block1 = btrfs_node_blockptr(parent, slot - 1);
2166 gen = btrfs_node_ptr_generation(parent, slot - 1);
2167 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002168 /*
2169 * if we get -eagain from btrfs_buffer_uptodate, we
2170 * don't want to return eagain here. That will loop
2171 * forever
2172 */
2173 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002174 block1 = 0;
2175 free_extent_buffer(eb);
2176 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002177 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002178 block2 = btrfs_node_blockptr(parent, slot + 1);
2179 gen = btrfs_node_ptr_generation(parent, slot + 1);
2180 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002181 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002182 block2 = 0;
2183 free_extent_buffer(eb);
2184 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002185
Josef Bacik0b088512013-06-17 14:23:02 -04002186 if (block1)
2187 readahead_tree_block(root, block1, blocksize, 0);
2188 if (block2)
2189 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002190}
2191
2192
2193/*
Chris Masond3977122009-01-05 21:25:51 -05002194 * when we walk down the tree, it is usually safe to unlock the higher layers
2195 * in the tree. The exceptions are when our path goes through slot 0, because
2196 * operations on the tree might require changing key pointers higher up in the
2197 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002198 *
Chris Masond3977122009-01-05 21:25:51 -05002199 * callers might also have set path->keep_locks, which tells this code to keep
2200 * the lock if the path points to the last slot in the block. This is part of
2201 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002202 *
Chris Masond3977122009-01-05 21:25:51 -05002203 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2204 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002205 */
Chris Masone02119d2008-09-05 16:13:11 -04002206static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002207 int lowest_unlock, int min_write_lock_level,
2208 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002209{
2210 int i;
2211 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002212 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002213 struct extent_buffer *t;
2214
2215 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2216 if (!path->nodes[i])
2217 break;
2218 if (!path->locks[i])
2219 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002220 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002221 skip_level = i + 1;
2222 continue;
2223 }
Chris Mason051e1b92008-06-25 16:01:30 -04002224 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002225 u32 nritems;
2226 t = path->nodes[i];
2227 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002228 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002229 skip_level = i + 1;
2230 continue;
2231 }
2232 }
Chris Mason051e1b92008-06-25 16:01:30 -04002233 if (skip_level < i && i >= lowest_unlock)
2234 no_skips = 1;
2235
Chris Mason925baed2008-06-25 16:01:30 -04002236 t = path->nodes[i];
2237 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002238 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002239 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002240 if (write_lock_level &&
2241 i > min_write_lock_level &&
2242 i <= *write_lock_level) {
2243 *write_lock_level = i - 1;
2244 }
Chris Mason925baed2008-06-25 16:01:30 -04002245 }
2246 }
2247}
2248
Chris Mason3c69fae2007-08-07 15:52:22 -04002249/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002250 * This releases any locks held in the path starting at level and
2251 * going all the way up to the root.
2252 *
2253 * btrfs_search_slot will keep the lock held on higher nodes in a few
2254 * corner cases, such as COW of the block at slot zero in the node. This
2255 * ignores those rules, and it should only be called when there are no
2256 * more updates to be done higher up in the tree.
2257 */
2258noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2259{
2260 int i;
2261
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002262 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002263 return;
2264
2265 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2266 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002267 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002268 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002269 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002270 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002271 path->locks[i] = 0;
2272 }
2273}
2274
2275/*
Chris Masonc8c42862009-04-03 10:14:18 -04002276 * helper function for btrfs_search_slot. The goal is to find a block
2277 * in cache without setting the path to blocking. If we find the block
2278 * we return zero and the path is unchanged.
2279 *
2280 * If we can't find the block, we set the path blocking and do some
2281 * reada. -EAGAIN is returned and the search must be repeated.
2282 */
2283static int
2284read_block_for_search(struct btrfs_trans_handle *trans,
2285 struct btrfs_root *root, struct btrfs_path *p,
2286 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002287 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002288{
2289 u64 blocknr;
2290 u64 gen;
2291 u32 blocksize;
2292 struct extent_buffer *b = *eb_ret;
2293 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002294 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002295
2296 blocknr = btrfs_node_blockptr(b, slot);
2297 gen = btrfs_node_ptr_generation(b, slot);
2298 blocksize = btrfs_level_size(root, level - 1);
2299
2300 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002301 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002302 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002303 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2304 *eb_ret = tmp;
2305 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002306 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002307
2308 /* the pages were up to date, but we failed
2309 * the generation number check. Do a full
2310 * read for the generation number that is correct.
2311 * We must do this without dropping locks so
2312 * we can trust our generation number
2313 */
2314 btrfs_set_path_blocking(p);
2315
2316 /* now we're allowed to do a blocking uptodate check */
2317 ret = btrfs_read_buffer(tmp, gen);
2318 if (!ret) {
2319 *eb_ret = tmp;
2320 return 0;
2321 }
2322 free_extent_buffer(tmp);
2323 btrfs_release_path(p);
2324 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002325 }
2326
2327 /*
2328 * reduce lock contention at high levels
2329 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002330 * we read. Don't release the lock on the current
2331 * level because we need to walk this node to figure
2332 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002333 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002334 btrfs_unlock_up_safe(p, level + 1);
2335 btrfs_set_path_blocking(p);
2336
Chris Masoncb449212010-10-24 11:01:27 -04002337 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002338 if (p->reada)
2339 reada_for_search(root, p, level, slot, key->objectid);
2340
David Sterbab3b4aa72011-04-21 01:20:15 +02002341 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002342
2343 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002344 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002345 if (tmp) {
2346 /*
2347 * If the read above didn't mark this buffer up to date,
2348 * it will never end up being up to date. Set ret to EIO now
2349 * and give up so that our caller doesn't loop forever
2350 * on our EAGAINs.
2351 */
Chris Masonb9fab912012-05-06 07:23:47 -04002352 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002353 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002354 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002355 }
2356 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002357}
2358
2359/*
2360 * helper function for btrfs_search_slot. This does all of the checks
2361 * for node-level blocks and does any balancing required based on
2362 * the ins_len.
2363 *
2364 * If no extra work was required, zero is returned. If we had to
2365 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2366 * start over
2367 */
2368static int
2369setup_nodes_for_search(struct btrfs_trans_handle *trans,
2370 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002371 struct extent_buffer *b, int level, int ins_len,
2372 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002373{
2374 int ret;
2375 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2376 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2377 int sret;
2378
Chris Masonbd681512011-07-16 15:23:14 -04002379 if (*write_lock_level < level + 1) {
2380 *write_lock_level = level + 1;
2381 btrfs_release_path(p);
2382 goto again;
2383 }
2384
Chris Masonc8c42862009-04-03 10:14:18 -04002385 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002386 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002387 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002388 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002389
2390 BUG_ON(sret > 0);
2391 if (sret) {
2392 ret = sret;
2393 goto done;
2394 }
2395 b = p->nodes[level];
2396 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002397 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002398 int sret;
2399
Chris Masonbd681512011-07-16 15:23:14 -04002400 if (*write_lock_level < level + 1) {
2401 *write_lock_level = level + 1;
2402 btrfs_release_path(p);
2403 goto again;
2404 }
2405
Chris Masonc8c42862009-04-03 10:14:18 -04002406 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002407 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002408 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002409 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002410
2411 if (sret) {
2412 ret = sret;
2413 goto done;
2414 }
2415 b = p->nodes[level];
2416 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002417 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002418 goto again;
2419 }
2420 BUG_ON(btrfs_header_nritems(b) == 1);
2421 }
2422 return 0;
2423
2424again:
2425 ret = -EAGAIN;
2426done:
2427 return ret;
2428}
2429
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002430static void key_search_validate(struct extent_buffer *b,
2431 struct btrfs_key *key,
2432 int level)
2433{
2434#ifdef CONFIG_BTRFS_ASSERT
2435 struct btrfs_disk_key disk_key;
2436
2437 btrfs_cpu_key_to_disk(&disk_key, key);
2438
2439 if (level == 0)
2440 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2441 offsetof(struct btrfs_leaf, items[0].key),
2442 sizeof(disk_key)));
2443 else
2444 ASSERT(!memcmp_extent_buffer(b, &disk_key,
2445 offsetof(struct btrfs_node, ptrs[0].key),
2446 sizeof(disk_key)));
2447#endif
2448}
2449
2450static int key_search(struct extent_buffer *b, struct btrfs_key *key,
2451 int level, int *prev_cmp, int *slot)
2452{
2453 if (*prev_cmp != 0) {
2454 *prev_cmp = bin_search(b, key, level, slot);
2455 return *prev_cmp;
2456 }
2457
2458 key_search_validate(b, key, level);
2459 *slot = 0;
2460
2461 return 0;
2462}
2463
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002464int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *found_path,
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002465 u64 iobjectid, u64 ioff, u8 key_type,
2466 struct btrfs_key *found_key)
2467{
2468 int ret;
2469 struct btrfs_key key;
2470 struct extent_buffer *eb;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002471 struct btrfs_path *path;
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002472
2473 key.type = key_type;
2474 key.objectid = iobjectid;
2475 key.offset = ioff;
2476
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002477 if (found_path == NULL) {
2478 path = btrfs_alloc_path();
2479 if (!path)
2480 return -ENOMEM;
2481 } else
2482 path = found_path;
2483
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002484 ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002485 if ((ret < 0) || (found_key == NULL)) {
2486 if (path != found_path)
2487 btrfs_free_path(path);
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002488 return ret;
Kelley Nielsen3f870c22013-11-04 19:37:39 -08002489 }
Kelley Nielsene33d5c32013-11-04 19:33:33 -08002490
2491 eb = path->nodes[0];
2492 if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
2493 ret = btrfs_next_leaf(fs_root, path);
2494 if (ret)
2495 return ret;
2496 eb = path->nodes[0];
2497 }
2498
2499 btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
2500 if (found_key->type != key.type ||
2501 found_key->objectid != key.objectid)
2502 return 1;
2503
2504 return 0;
2505}
2506
Chris Masonc8c42862009-04-03 10:14:18 -04002507/*
Chris Mason74123bd2007-02-02 11:05:29 -05002508 * look for key in the tree. path is filled in with nodes along the way
2509 * if key is found, we return zero and you can find the item in the leaf
2510 * level of the path (level 0)
2511 *
2512 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002513 * be inserted, and 1 is returned. If there are other errors during the
2514 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002515 *
2516 * if ins_len > 0, nodes and leaves will be split as we walk down the
2517 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2518 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002519 */
Chris Masone089f052007-03-16 16:20:31 -04002520int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2521 *root, struct btrfs_key *key, struct btrfs_path *p, int
2522 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002523{
Chris Mason5f39d392007-10-15 16:14:19 -04002524 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002525 int slot;
2526 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002527 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002528 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002529 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002530 int root_lock;
2531 /* everything at write_lock_level or lower must be write locked */
2532 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002533 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002534 int min_write_lock_level;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002535 int prev_cmp;
Chris Mason9f3a7422007-08-07 15:52:19 -04002536
Chris Mason6702ed42007-08-07 16:15:09 -04002537 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002538 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002539 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002540
Chris Masonbd681512011-07-16 15:23:14 -04002541 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002542 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002543
Chris Masonbd681512011-07-16 15:23:14 -04002544 /* when we are removing items, we might have to go up to level
2545 * two as we update tree pointers Make sure we keep write
2546 * for those levels as well
2547 */
2548 write_lock_level = 2;
2549 } else if (ins_len > 0) {
2550 /*
2551 * for inserting items, make sure we have a write lock on
2552 * level 1 so we can update keys
2553 */
2554 write_lock_level = 1;
2555 }
2556
2557 if (!cow)
2558 write_lock_level = -1;
2559
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002560 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002561 write_lock_level = BTRFS_MAX_LEVEL;
2562
Chris Masonf7c79f32012-03-19 15:54:38 -04002563 min_write_lock_level = write_lock_level;
2564
Chris Masonbb803952007-03-01 12:04:21 -05002565again:
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002566 prev_cmp = -1;
Chris Masonbd681512011-07-16 15:23:14 -04002567 /*
2568 * we try very hard to do read locks on the root
2569 */
2570 root_lock = BTRFS_READ_LOCK;
2571 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002572 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002573 /*
2574 * the commit roots are read only
2575 * so we always do read locks
2576 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002577 b = root->commit_root;
2578 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002579 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002580 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002581 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002582 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002583 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002584 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002585 level = btrfs_header_level(b);
2586 } else {
2587 /* we don't know the level of the root node
2588 * until we actually have it read locked
2589 */
2590 b = btrfs_read_lock_root_node(root);
2591 level = btrfs_header_level(b);
2592 if (level <= write_lock_level) {
2593 /* whoops, must trade for write lock */
2594 btrfs_tree_read_unlock(b);
2595 free_extent_buffer(b);
2596 b = btrfs_lock_root_node(root);
2597 root_lock = BTRFS_WRITE_LOCK;
2598
2599 /* the level might have changed, check again */
2600 level = btrfs_header_level(b);
2601 }
2602 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002603 }
Chris Masonbd681512011-07-16 15:23:14 -04002604 p->nodes[level] = b;
2605 if (!p->skip_locking)
2606 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002607
Chris Masoneb60cea2007-02-02 09:18:22 -05002608 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002609 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002610
2611 /*
2612 * setup the path here so we can release it under lock
2613 * contention with the cow code
2614 */
Chris Mason02217ed2007-03-02 16:08:05 -05002615 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002616 /*
2617 * if we don't really need to cow this block
2618 * then we don't want to set the path blocking,
2619 * so we test it here
2620 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002621 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002622 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002623
Chris Masonb4ce94d2009-02-04 09:25:08 -05002624 btrfs_set_path_blocking(p);
2625
Chris Masonbd681512011-07-16 15:23:14 -04002626 /*
2627 * must have write locks on this node and the
2628 * parent
2629 */
Josef Bacik5124e002012-11-07 13:44:13 -05002630 if (level > write_lock_level ||
2631 (level + 1 > write_lock_level &&
2632 level + 1 < BTRFS_MAX_LEVEL &&
2633 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002634 write_lock_level = level + 1;
2635 btrfs_release_path(p);
2636 goto again;
2637 }
2638
Yan Zheng33c66f42009-07-22 09:59:00 -04002639 err = btrfs_cow_block(trans, root, b,
2640 p->nodes[level + 1],
2641 p->slots[level + 1], &b);
2642 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002643 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002644 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002645 }
Chris Mason02217ed2007-03-02 16:08:05 -05002646 }
Chris Mason65b51a02008-08-01 15:11:20 -04002647cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002648 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002649
Chris Masoneb60cea2007-02-02 09:18:22 -05002650 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002651 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002652
2653 /*
2654 * we have a lock on b and as long as we aren't changing
2655 * the tree, there is no way to for the items in b to change.
2656 * It is safe to drop the lock on our parent before we
2657 * go through the expensive btree search on b.
2658 *
2659 * If cow is true, then we might be changing slot zero,
2660 * which may require changing the parent. So, we can't
2661 * drop the lock until after we know which slot we're
2662 * operating on.
2663 */
2664 if (!cow)
2665 btrfs_unlock_up_safe(p, level + 1);
2666
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002667 ret = key_search(b, key, level, &prev_cmp, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002668
Chris Mason5f39d392007-10-15 16:14:19 -04002669 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002670 int dec = 0;
2671 if (ret && slot > 0) {
2672 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002673 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002674 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002675 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002676 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002677 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002678 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002679 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002680 if (err) {
2681 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002682 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002683 }
Chris Masonc8c42862009-04-03 10:14:18 -04002684 b = p->nodes[level];
2685 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002686
Chris Masonbd681512011-07-16 15:23:14 -04002687 /*
2688 * slot 0 is special, if we change the key
2689 * we have to update the parent pointer
2690 * which means we must have a write lock
2691 * on the parent
2692 */
2693 if (slot == 0 && cow &&
2694 write_lock_level < level + 1) {
2695 write_lock_level = level + 1;
2696 btrfs_release_path(p);
2697 goto again;
2698 }
2699
Chris Masonf7c79f32012-03-19 15:54:38 -04002700 unlock_up(p, level, lowest_unlock,
2701 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002702
Chris Mason925baed2008-06-25 16:01:30 -04002703 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002704 if (dec)
2705 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002706 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002707 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002708
Yan Zheng33c66f42009-07-22 09:59:00 -04002709 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002710 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002711 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002712 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002713 if (err) {
2714 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002715 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002716 }
Chris Mason76a05b32009-05-14 13:24:30 -04002717
Chris Masonb4ce94d2009-02-04 09:25:08 -05002718 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002719 level = btrfs_header_level(b);
2720 if (level <= write_lock_level) {
2721 err = btrfs_try_tree_write_lock(b);
2722 if (!err) {
2723 btrfs_set_path_blocking(p);
2724 btrfs_tree_lock(b);
2725 btrfs_clear_path_blocking(p, b,
2726 BTRFS_WRITE_LOCK);
2727 }
2728 p->locks[level] = BTRFS_WRITE_LOCK;
2729 } else {
2730 err = btrfs_try_tree_read_lock(b);
2731 if (!err) {
2732 btrfs_set_path_blocking(p);
2733 btrfs_tree_read_lock(b);
2734 btrfs_clear_path_blocking(p, b,
2735 BTRFS_READ_LOCK);
2736 }
2737 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002738 }
Chris Masonbd681512011-07-16 15:23:14 -04002739 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002740 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002741 } else {
2742 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002743 if (ins_len > 0 &&
2744 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002745 if (write_lock_level < 1) {
2746 write_lock_level = 1;
2747 btrfs_release_path(p);
2748 goto again;
2749 }
2750
Chris Masonb4ce94d2009-02-04 09:25:08 -05002751 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002752 err = split_leaf(trans, root, key,
2753 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002754 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002755
Yan Zheng33c66f42009-07-22 09:59:00 -04002756 BUG_ON(err > 0);
2757 if (err) {
2758 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002759 goto done;
2760 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002761 }
Chris Mason459931e2008-12-10 09:10:46 -05002762 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002763 unlock_up(p, level, lowest_unlock,
2764 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002765 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002766 }
2767 }
Chris Mason65b51a02008-08-01 15:11:20 -04002768 ret = 1;
2769done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002770 /*
2771 * we don't really know what they plan on doing with the path
2772 * from here on, so for now just mark it as blocking
2773 */
Chris Masonb9473432009-03-13 11:00:37 -04002774 if (!p->leave_spinning)
2775 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002776 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002777 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002778 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002779}
2780
Chris Mason74123bd2007-02-02 11:05:29 -05002781/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002782 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2783 * current state of the tree together with the operations recorded in the tree
2784 * modification log to search for the key in a previous version of this tree, as
2785 * denoted by the time_seq parameter.
2786 *
2787 * Naturally, there is no support for insert, delete or cow operations.
2788 *
2789 * The resulting path and return value will be set up as if we called
2790 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2791 */
2792int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2793 struct btrfs_path *p, u64 time_seq)
2794{
2795 struct extent_buffer *b;
2796 int slot;
2797 int ret;
2798 int err;
2799 int level;
2800 int lowest_unlock = 1;
2801 u8 lowest_level = 0;
Josef Bacikd4b40872013-09-24 14:09:34 -04002802 int prev_cmp = -1;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002803
2804 lowest_level = p->lowest_level;
2805 WARN_ON(p->nodes[0] != NULL);
2806
2807 if (p->search_commit_root) {
2808 BUG_ON(time_seq);
2809 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2810 }
2811
2812again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002813 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002814 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002815 p->locks[level] = BTRFS_READ_LOCK;
2816
2817 while (b) {
2818 level = btrfs_header_level(b);
2819 p->nodes[level] = b;
2820 btrfs_clear_path_blocking(p, NULL, 0);
2821
2822 /*
2823 * we have a lock on b and as long as we aren't changing
2824 * the tree, there is no way to for the items in b to change.
2825 * It is safe to drop the lock on our parent before we
2826 * go through the expensive btree search on b.
2827 */
2828 btrfs_unlock_up_safe(p, level + 1);
2829
Josef Bacikd4b40872013-09-24 14:09:34 -04002830 /*
2831 * Since we can unwind eb's we want to do a real search every
2832 * time.
2833 */
2834 prev_cmp = -1;
Filipe David Borba Mananad7396f02013-08-30 15:46:43 +01002835 ret = key_search(b, key, level, &prev_cmp, &slot);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002836
2837 if (level != 0) {
2838 int dec = 0;
2839 if (ret && slot > 0) {
2840 dec = 1;
2841 slot -= 1;
2842 }
2843 p->slots[level] = slot;
2844 unlock_up(p, level, lowest_unlock, 0, NULL);
2845
2846 if (level == lowest_level) {
2847 if (dec)
2848 p->slots[level]++;
2849 goto done;
2850 }
2851
2852 err = read_block_for_search(NULL, root, p, &b, level,
2853 slot, key, time_seq);
2854 if (err == -EAGAIN)
2855 goto again;
2856 if (err) {
2857 ret = err;
2858 goto done;
2859 }
2860
2861 level = btrfs_header_level(b);
2862 err = btrfs_try_tree_read_lock(b);
2863 if (!err) {
2864 btrfs_set_path_blocking(p);
2865 btrfs_tree_read_lock(b);
2866 btrfs_clear_path_blocking(p, b,
2867 BTRFS_READ_LOCK);
2868 }
Josef Bacik9ec72672013-08-07 16:57:23 -04002869 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04002870 if (!b) {
2871 ret = -ENOMEM;
2872 goto done;
2873 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002874 p->locks[level] = BTRFS_READ_LOCK;
2875 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002876 } else {
2877 p->slots[level] = slot;
2878 unlock_up(p, level, lowest_unlock, 0, NULL);
2879 goto done;
2880 }
2881 }
2882 ret = 1;
2883done:
2884 if (!p->leave_spinning)
2885 btrfs_set_path_blocking(p);
2886 if (ret < 0)
2887 btrfs_release_path(p);
2888
2889 return ret;
2890}
2891
2892/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002893 * helper to use instead of search slot if no exact match is needed but
2894 * instead the next or previous item should be returned.
2895 * When find_higher is true, the next higher item is returned, the next lower
2896 * otherwise.
2897 * When return_any and find_higher are both true, and no higher item is found,
2898 * return the next lower instead.
2899 * When return_any is true and find_higher is false, and no lower item is found,
2900 * return the next higher instead.
2901 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2902 * < 0 on error
2903 */
2904int btrfs_search_slot_for_read(struct btrfs_root *root,
2905 struct btrfs_key *key, struct btrfs_path *p,
2906 int find_higher, int return_any)
2907{
2908 int ret;
2909 struct extent_buffer *leaf;
2910
2911again:
2912 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2913 if (ret <= 0)
2914 return ret;
2915 /*
2916 * a return value of 1 means the path is at the position where the
2917 * item should be inserted. Normally this is the next bigger item,
2918 * but in case the previous item is the last in a leaf, path points
2919 * to the first free slot in the previous leaf, i.e. at an invalid
2920 * item.
2921 */
2922 leaf = p->nodes[0];
2923
2924 if (find_higher) {
2925 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2926 ret = btrfs_next_leaf(root, p);
2927 if (ret <= 0)
2928 return ret;
2929 if (!return_any)
2930 return 1;
2931 /*
2932 * no higher item found, return the next
2933 * lower instead
2934 */
2935 return_any = 0;
2936 find_higher = 0;
2937 btrfs_release_path(p);
2938 goto again;
2939 }
2940 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002941 if (p->slots[0] == 0) {
2942 ret = btrfs_prev_leaf(root, p);
2943 if (ret < 0)
2944 return ret;
2945 if (!ret) {
2946 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2947 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002948 }
Arne Jansene6793762011-09-13 11:18:10 +02002949 if (!return_any)
2950 return 1;
2951 /*
2952 * no lower item found, return the next
2953 * higher instead
2954 */
2955 return_any = 0;
2956 find_higher = 1;
2957 btrfs_release_path(p);
2958 goto again;
2959 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002960 --p->slots[0];
2961 }
2962 }
2963 return 0;
2964}
2965
2966/*
Chris Mason74123bd2007-02-02 11:05:29 -05002967 * adjust the pointers going up the tree, starting at level
2968 * making sure the right key of each node is points to 'key'.
2969 * This is used after shifting pointers to the left, so it stops
2970 * fixing up pointers when a given leaf/node is not in slot 0 of the
2971 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002972 *
Chris Mason74123bd2007-02-02 11:05:29 -05002973 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002974static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002975 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002976{
2977 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002978 struct extent_buffer *t;
2979
Chris Mason234b63a2007-03-13 10:46:10 -04002980 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002981 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002982 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002983 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002984 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002985 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002986 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002987 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002988 if (tslot != 0)
2989 break;
2990 }
2991}
2992
Chris Mason74123bd2007-02-02 11:05:29 -05002993/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002994 * update item key.
2995 *
2996 * This function isn't completely safe. It's the caller's responsibility
2997 * that the new key won't break the order
2998 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002999void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01003000 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04003001{
3002 struct btrfs_disk_key disk_key;
3003 struct extent_buffer *eb;
3004 int slot;
3005
3006 eb = path->nodes[0];
3007 slot = path->slots[0];
3008 if (slot > 0) {
3009 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003010 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003011 }
3012 if (slot < btrfs_header_nritems(eb) - 1) {
3013 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003014 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003015 }
3016
3017 btrfs_cpu_key_to_disk(&disk_key, new_key);
3018 btrfs_set_item_key(eb, &disk_key, slot);
3019 btrfs_mark_buffer_dirty(eb);
3020 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003021 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04003022}
3023
3024/*
Chris Mason74123bd2007-02-02 11:05:29 -05003025 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05003026 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003027 *
3028 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
3029 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05003030 */
Chris Mason98ed5172008-01-03 10:01:48 -05003031static int push_node_left(struct btrfs_trans_handle *trans,
3032 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04003033 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003034{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003035 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05003036 int src_nritems;
3037 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003038 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003039
Chris Mason5f39d392007-10-15 16:14:19 -04003040 src_nritems = btrfs_header_nritems(src);
3041 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003042 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05003043 WARN_ON(btrfs_header_generation(src) != trans->transid);
3044 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04003045
Chris Masonbce4eae2008-04-24 14:42:46 -04003046 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04003047 return 1;
3048
Chris Masond3977122009-01-05 21:25:51 -05003049 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003050 return 1;
3051
Chris Masonbce4eae2008-04-24 14:42:46 -04003052 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003053 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003054 if (push_items < src_nritems) {
3055 /* leave at least 8 pointers in the node if
3056 * we aren't going to empty it
3057 */
3058 if (src_nritems - push_items < 8) {
3059 if (push_items <= 8)
3060 return 1;
3061 push_items -= 8;
3062 }
3063 }
3064 } else
3065 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003066
Jan Schmidtf2304752012-05-26 11:43:17 +02003067 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003068 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003069 copy_extent_buffer(dst, src,
3070 btrfs_node_key_ptr_offset(dst_nritems),
3071 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003072 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003073
Chris Masonbb803952007-03-01 12:04:21 -05003074 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003075 /*
3076 * don't call tree_mod_log_eb_move here, key removal was already
3077 * fully logged by tree_mod_log_eb_copy above.
3078 */
Chris Mason5f39d392007-10-15 16:14:19 -04003079 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3080 btrfs_node_key_ptr_offset(push_items),
3081 (src_nritems - push_items) *
3082 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003083 }
Chris Mason5f39d392007-10-15 16:14:19 -04003084 btrfs_set_header_nritems(src, src_nritems - push_items);
3085 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3086 btrfs_mark_buffer_dirty(src);
3087 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003088
Chris Masonbb803952007-03-01 12:04:21 -05003089 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003090}
3091
Chris Mason97571fd2007-02-24 13:39:08 -05003092/*
Chris Mason79f95c82007-03-01 15:16:26 -05003093 * try to push data from one node into the next node right in the
3094 * tree.
3095 *
3096 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3097 * error, and > 0 if there was no room in the right hand block.
3098 *
3099 * this will only push up to 1/2 the contents of the left node over
3100 */
Chris Mason5f39d392007-10-15 16:14:19 -04003101static int balance_node_right(struct btrfs_trans_handle *trans,
3102 struct btrfs_root *root,
3103 struct extent_buffer *dst,
3104 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003105{
Chris Mason79f95c82007-03-01 15:16:26 -05003106 int push_items = 0;
3107 int max_push;
3108 int src_nritems;
3109 int dst_nritems;
3110 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003111
Chris Mason7bb86312007-12-11 09:25:06 -05003112 WARN_ON(btrfs_header_generation(src) != trans->transid);
3113 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3114
Chris Mason5f39d392007-10-15 16:14:19 -04003115 src_nritems = btrfs_header_nritems(src);
3116 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003117 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003118 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003119 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003120
Chris Masond3977122009-01-05 21:25:51 -05003121 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003122 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003123
3124 max_push = src_nritems / 2 + 1;
3125 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003126 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003127 return 1;
Yan252c38f2007-08-29 09:11:44 -04003128
Chris Mason79f95c82007-03-01 15:16:26 -05003129 if (max_push < push_items)
3130 push_items = max_push;
3131
Jan Schmidtf2304752012-05-26 11:43:17 +02003132 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003133 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3134 btrfs_node_key_ptr_offset(0),
3135 (dst_nritems) *
3136 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003137
Jan Schmidtf2304752012-05-26 11:43:17 +02003138 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003139 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003140 copy_extent_buffer(dst, src,
3141 btrfs_node_key_ptr_offset(0),
3142 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003143 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003144
Chris Mason5f39d392007-10-15 16:14:19 -04003145 btrfs_set_header_nritems(src, src_nritems - push_items);
3146 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003147
Chris Mason5f39d392007-10-15 16:14:19 -04003148 btrfs_mark_buffer_dirty(src);
3149 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003150
Chris Mason79f95c82007-03-01 15:16:26 -05003151 return ret;
3152}
3153
3154/*
Chris Mason97571fd2007-02-24 13:39:08 -05003155 * helper function to insert a new root level in the tree.
3156 * A new node is allocated, and a single item is inserted to
3157 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003158 *
3159 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003160 */
Chris Masond3977122009-01-05 21:25:51 -05003161static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003162 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003163 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003164{
Chris Mason7bb86312007-12-11 09:25:06 -05003165 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003166 struct extent_buffer *lower;
3167 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003168 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003169 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003170
3171 BUG_ON(path->nodes[level]);
3172 BUG_ON(path->nodes[level-1] != root->node);
3173
Chris Mason7bb86312007-12-11 09:25:06 -05003174 lower = path->nodes[level-1];
3175 if (level == 1)
3176 btrfs_item_key(lower, &lower_key, 0);
3177 else
3178 btrfs_node_key(lower, &lower_key, 0);
3179
Zheng Yan31840ae2008-09-23 13:14:14 -04003180 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003181 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003182 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003183 if (IS_ERR(c))
3184 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003185
Yan, Zhengf0486c62010-05-16 10:46:25 -04003186 root_add_used(root, root->nodesize);
3187
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003188 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003189 btrfs_set_header_nritems(c, 1);
3190 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003191 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003192 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003193 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003194 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003195
Ross Kirk0a4e5582013-09-24 10:12:38 +01003196 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(),
Chris Mason5f39d392007-10-15 16:14:19 -04003197 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003198
3199 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003200 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003201
Chris Mason5f39d392007-10-15 16:14:19 -04003202 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003203 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003204 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003205 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003206
3207 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003208
3209 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003210
Chris Mason925baed2008-06-25 16:01:30 -04003211 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003212 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003213 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003214
3215 /* the super has an extra ref to root->node */
3216 free_extent_buffer(old);
3217
Chris Mason0b86a832008-03-24 15:01:56 -04003218 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003219 extent_buffer_get(c);
3220 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003221 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003222 path->slots[level] = 0;
3223 return 0;
3224}
3225
Chris Mason74123bd2007-02-02 11:05:29 -05003226/*
3227 * worker function to insert a single pointer in a node.
3228 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003229 *
Chris Mason74123bd2007-02-02 11:05:29 -05003230 * slot and level indicate where you want the key to go, and
3231 * blocknr is the block the key points to.
3232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003233static void insert_ptr(struct btrfs_trans_handle *trans,
3234 struct btrfs_root *root, struct btrfs_path *path,
3235 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003236 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003237{
Chris Mason5f39d392007-10-15 16:14:19 -04003238 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003239 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003240 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003241
3242 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003243 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003244 lower = path->nodes[level];
3245 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003246 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003247 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003248 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003249 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003250 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3251 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003252 memmove_extent_buffer(lower,
3253 btrfs_node_key_ptr_offset(slot + 1),
3254 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003255 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003256 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003257 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003258 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003259 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003260 BUG_ON(ret < 0);
3261 }
Chris Mason5f39d392007-10-15 16:14:19 -04003262 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003263 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003264 WARN_ON(trans->transid == 0);
3265 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003266 btrfs_set_header_nritems(lower, nritems + 1);
3267 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003268}
3269
Chris Mason97571fd2007-02-24 13:39:08 -05003270/*
3271 * split the node at the specified level in path in two.
3272 * The path is corrected to point to the appropriate node after the split
3273 *
3274 * Before splitting this tries to make some room in the node by pushing
3275 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003276 *
3277 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003278 */
Chris Masone02119d2008-09-05 16:13:11 -04003279static noinline int split_node(struct btrfs_trans_handle *trans,
3280 struct btrfs_root *root,
3281 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003282{
Chris Mason5f39d392007-10-15 16:14:19 -04003283 struct extent_buffer *c;
3284 struct extent_buffer *split;
3285 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003286 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003287 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003288 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003289
Chris Mason5f39d392007-10-15 16:14:19 -04003290 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003291 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003292 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003293 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003294 * trying to split the root, lets make a new one
3295 *
Liu Bofdd99c72013-05-22 12:06:51 +00003296 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003297 * insert_new_root, because that root buffer will be kept as a
3298 * normal node. We are going to log removal of half of the
3299 * elements below with tree_mod_log_eb_copy. We're holding a
3300 * tree lock on the buffer, which is why we cannot race with
3301 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003302 */
Liu Bofdd99c72013-05-22 12:06:51 +00003303 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003304 if (ret)
3305 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003306 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003307 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003308 c = path->nodes[level];
3309 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003310 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003311 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003312 if (ret < 0)
3313 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003314 }
Chris Masone66f7092007-04-20 13:16:02 -04003315
Chris Mason5f39d392007-10-15 16:14:19 -04003316 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003317 mid = (c_nritems + 1) / 2;
3318 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003321 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003322 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003323 if (IS_ERR(split))
3324 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003325
Yan, Zhengf0486c62010-05-16 10:46:25 -04003326 root_add_used(root, root->nodesize);
3327
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003328 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003329 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003330 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003331 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003332 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003333 btrfs_set_header_owner(split, root->root_key.objectid);
3334 write_extent_buffer(split, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01003335 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003336 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003337 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003338 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003339
Jan Schmidt90f8d622013-04-13 13:19:53 +00003340 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003341 copy_extent_buffer(split, c,
3342 btrfs_node_key_ptr_offset(0),
3343 btrfs_node_key_ptr_offset(mid),
3344 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3345 btrfs_set_header_nritems(split, c_nritems - mid);
3346 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003347 ret = 0;
3348
Chris Mason5f39d392007-10-15 16:14:19 -04003349 btrfs_mark_buffer_dirty(c);
3350 btrfs_mark_buffer_dirty(split);
3351
Jeff Mahoney143bede2012-03-01 14:56:26 +01003352 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003353 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003354
Chris Mason5de08d72007-02-24 06:24:44 -05003355 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003356 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003357 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003358 free_extent_buffer(c);
3359 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003360 path->slots[level + 1] += 1;
3361 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003362 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003363 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003364 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003365 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003366}
3367
Chris Mason74123bd2007-02-02 11:05:29 -05003368/*
3369 * how many bytes are required to store the items in a leaf. start
3370 * and nr indicate which items in the leaf to check. This totals up the
3371 * space used both by the item structs and the item data
3372 */
Chris Mason5f39d392007-10-15 16:14:19 -04003373static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003374{
Josef Bacik41be1f32012-10-15 13:43:18 -04003375 struct btrfs_item *start_item;
3376 struct btrfs_item *end_item;
3377 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003378 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003379 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003380 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003381
3382 if (!nr)
3383 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003384 btrfs_init_map_token(&token);
Ross Kirkdd3cc162013-09-16 15:58:09 +01003385 start_item = btrfs_item_nr(start);
3386 end_item = btrfs_item_nr(end);
Josef Bacik41be1f32012-10-15 13:43:18 -04003387 data_len = btrfs_token_item_offset(l, start_item, &token) +
3388 btrfs_token_item_size(l, start_item, &token);
3389 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003390 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003391 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003392 return data_len;
3393}
3394
Chris Mason74123bd2007-02-02 11:05:29 -05003395/*
Chris Masond4dbff92007-04-04 14:08:15 -04003396 * The space between the end of the leaf items and
3397 * the start of the leaf data. IOW, how much room
3398 * the leaf has left for both items and data
3399 */
Chris Masond3977122009-01-05 21:25:51 -05003400noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003401 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003402{
Chris Mason5f39d392007-10-15 16:14:19 -04003403 int nritems = btrfs_header_nritems(leaf);
3404 int ret;
3405 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3406 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003407 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3408 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003409 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003410 leaf_space_used(leaf, 0, nritems), nritems);
3411 }
3412 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003413}
3414
Chris Mason99d8f832010-07-07 10:51:48 -04003415/*
3416 * min slot controls the lowest index we're willing to push to the
3417 * right. We'll push up to and including min_slot, but no lower
3418 */
Chris Mason44871b12009-03-13 10:04:31 -04003419static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3420 struct btrfs_root *root,
3421 struct btrfs_path *path,
3422 int data_size, int empty,
3423 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003424 int free_space, u32 left_nritems,
3425 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003426{
Chris Mason5f39d392007-10-15 16:14:19 -04003427 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003428 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003429 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003430 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003431 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003432 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003433 int push_space = 0;
3434 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003435 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003436 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003437 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003438 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003439 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003440
Chris Masoncfed81a2012-03-03 07:40:03 -05003441 btrfs_init_map_token(&token);
3442
Chris Mason34a38212007-11-07 13:31:03 -05003443 if (empty)
3444 nr = 0;
3445 else
Chris Mason99d8f832010-07-07 10:51:48 -04003446 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003447
Zheng Yan31840ae2008-09-23 13:14:14 -04003448 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003449 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003450
Chris Mason44871b12009-03-13 10:04:31 -04003451 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003452 i = left_nritems - 1;
3453 while (i >= nr) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003454 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003455
Zheng Yan31840ae2008-09-23 13:14:14 -04003456 if (!empty && push_items > 0) {
3457 if (path->slots[0] > i)
3458 break;
3459 if (path->slots[0] == i) {
3460 int space = btrfs_leaf_free_space(root, left);
3461 if (space + push_space * 2 > free_space)
3462 break;
3463 }
3464 }
3465
Chris Mason00ec4c52007-02-24 12:47:20 -05003466 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003467 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003468
Chris Masondb945352007-10-15 16:15:53 -04003469 this_item_size = btrfs_item_size(left, item);
3470 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003471 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003472
Chris Mason00ec4c52007-02-24 12:47:20 -05003473 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003474 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003475 if (i == 0)
3476 break;
3477 i--;
Chris Masondb945352007-10-15 16:15:53 -04003478 }
Chris Mason5f39d392007-10-15 16:14:19 -04003479
Chris Mason925baed2008-06-25 16:01:30 -04003480 if (push_items == 0)
3481 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003482
Julia Lawall6c1500f2012-11-03 20:30:18 +00003483 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003484
Chris Mason00ec4c52007-02-24 12:47:20 -05003485 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003486 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003489 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003490
Chris Mason00ec4c52007-02-24 12:47:20 -05003491 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003492 data_end = leaf_data_end(root, right);
3493 memmove_extent_buffer(right,
3494 btrfs_leaf_data(right) + data_end - push_space,
3495 btrfs_leaf_data(right) + data_end,
3496 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3497
Chris Mason00ec4c52007-02-24 12:47:20 -05003498 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003499 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003500 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3501 btrfs_leaf_data(left) + leaf_data_end(root, left),
3502 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003503
3504 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3505 btrfs_item_nr_offset(0),
3506 right_nritems * sizeof(struct btrfs_item));
3507
Chris Mason00ec4c52007-02-24 12:47:20 -05003508 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003509 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3510 btrfs_item_nr_offset(left_nritems - push_items),
3511 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003512
3513 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003514 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003515 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003516 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003517 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003518 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003519 push_space -= btrfs_token_item_size(right, item, &token);
3520 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003521 }
3522
Chris Mason7518a232007-03-12 12:01:18 -04003523 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003524 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003525
Chris Mason34a38212007-11-07 13:31:03 -05003526 if (left_nritems)
3527 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003528 else
3529 clean_tree_block(trans, root, left);
3530
Chris Mason5f39d392007-10-15 16:14:19 -04003531 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003532
Chris Mason5f39d392007-10-15 16:14:19 -04003533 btrfs_item_key(right, &disk_key, 0);
3534 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003535 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003536
Chris Mason00ec4c52007-02-24 12:47:20 -05003537 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003538 if (path->slots[0] >= left_nritems) {
3539 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003540 if (btrfs_header_nritems(path->nodes[0]) == 0)
3541 clean_tree_block(trans, root, path->nodes[0]);
3542 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003543 free_extent_buffer(path->nodes[0]);
3544 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003545 path->slots[1] += 1;
3546 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003547 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003548 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003549 }
3550 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003551
3552out_unlock:
3553 btrfs_tree_unlock(right);
3554 free_extent_buffer(right);
3555 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003556}
Chris Mason925baed2008-06-25 16:01:30 -04003557
Chris Mason00ec4c52007-02-24 12:47:20 -05003558/*
Chris Mason44871b12009-03-13 10:04:31 -04003559 * push some data in the path leaf to the right, trying to free up at
3560 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3561 *
3562 * returns 1 if the push failed because the other node didn't have enough
3563 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003564 *
3565 * this will push starting from min_slot to the end of the leaf. It won't
3566 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003567 */
3568static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003569 *root, struct btrfs_path *path,
3570 int min_data_size, int data_size,
3571 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003572{
3573 struct extent_buffer *left = path->nodes[0];
3574 struct extent_buffer *right;
3575 struct extent_buffer *upper;
3576 int slot;
3577 int free_space;
3578 u32 left_nritems;
3579 int ret;
3580
3581 if (!path->nodes[1])
3582 return 1;
3583
3584 slot = path->slots[1];
3585 upper = path->nodes[1];
3586 if (slot >= btrfs_header_nritems(upper) - 1)
3587 return 1;
3588
3589 btrfs_assert_tree_locked(path->nodes[1]);
3590
3591 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003592 if (right == NULL)
3593 return 1;
3594
Chris Mason44871b12009-03-13 10:04:31 -04003595 btrfs_tree_lock(right);
3596 btrfs_set_lock_blocking(right);
3597
3598 free_space = btrfs_leaf_free_space(root, right);
3599 if (free_space < data_size)
3600 goto out_unlock;
3601
3602 /* cow and double check */
3603 ret = btrfs_cow_block(trans, root, right, upper,
3604 slot + 1, &right);
3605 if (ret)
3606 goto out_unlock;
3607
3608 free_space = btrfs_leaf_free_space(root, right);
3609 if (free_space < data_size)
3610 goto out_unlock;
3611
3612 left_nritems = btrfs_header_nritems(left);
3613 if (left_nritems == 0)
3614 goto out_unlock;
3615
Chris Mason99d8f832010-07-07 10:51:48 -04003616 return __push_leaf_right(trans, root, path, min_data_size, empty,
3617 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003618out_unlock:
3619 btrfs_tree_unlock(right);
3620 free_extent_buffer(right);
3621 return 1;
3622}
3623
3624/*
Chris Mason74123bd2007-02-02 11:05:29 -05003625 * push some data in the path leaf to the left, trying to free up at
3626 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003627 *
3628 * max_slot can put a limit on how far into the leaf we'll push items. The
3629 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3630 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003631 */
Chris Mason44871b12009-03-13 10:04:31 -04003632static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3633 struct btrfs_root *root,
3634 struct btrfs_path *path, int data_size,
3635 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003636 int free_space, u32 right_nritems,
3637 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003638{
Chris Mason5f39d392007-10-15 16:14:19 -04003639 struct btrfs_disk_key disk_key;
3640 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003642 int push_space = 0;
3643 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003644 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003645 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003646 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003647 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003648 u32 this_item_size;
3649 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003650 struct btrfs_map_token token;
3651
3652 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003653
Chris Mason34a38212007-11-07 13:31:03 -05003654 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003655 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003656 else
Chris Mason99d8f832010-07-07 10:51:48 -04003657 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003658
3659 for (i = 0; i < nr; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003660 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003661
Zheng Yan31840ae2008-09-23 13:14:14 -04003662 if (!empty && push_items > 0) {
3663 if (path->slots[0] < i)
3664 break;
3665 if (path->slots[0] == i) {
3666 int space = btrfs_leaf_free_space(root, right);
3667 if (space + push_space * 2 > free_space)
3668 break;
3669 }
3670 }
3671
Chris Masonbe0e5c02007-01-26 15:51:26 -05003672 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003673 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003674
3675 this_item_size = btrfs_item_size(right, item);
3676 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003677 break;
Chris Masondb945352007-10-15 16:15:53 -04003678
Chris Masonbe0e5c02007-01-26 15:51:26 -05003679 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003680 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003681 }
Chris Masondb945352007-10-15 16:15:53 -04003682
Chris Masonbe0e5c02007-01-26 15:51:26 -05003683 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003684 ret = 1;
3685 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003686 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303687 WARN_ON(!empty && push_items == btrfs_header_nritems(right));
Chris Mason5f39d392007-10-15 16:14:19 -04003688
Chris Masonbe0e5c02007-01-26 15:51:26 -05003689 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003690 copy_extent_buffer(left, right,
3691 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3692 btrfs_item_nr_offset(0),
3693 push_items * sizeof(struct btrfs_item));
3694
Chris Mason123abc82007-03-14 14:14:43 -04003695 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003696 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003697
3698 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003699 leaf_data_end(root, left) - push_space,
3700 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003701 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003702 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003703 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003704 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003705
Chris Masondb945352007-10-15 16:15:53 -04003706 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003707 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003708 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003709
Ross Kirkdd3cc162013-09-16 15:58:09 +01003710 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003711
Chris Masoncfed81a2012-03-03 07:40:03 -05003712 ioff = btrfs_token_item_offset(left, item, &token);
3713 btrfs_set_token_item_offset(left, item,
3714 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3715 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003716 }
Chris Mason5f39d392007-10-15 16:14:19 -04003717 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003718
3719 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003720 if (push_items > right_nritems)
3721 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003722 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003723
Chris Mason34a38212007-11-07 13:31:03 -05003724 if (push_items < right_nritems) {
3725 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3726 leaf_data_end(root, right);
3727 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3728 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3729 btrfs_leaf_data(right) +
3730 leaf_data_end(root, right), push_space);
3731
3732 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003733 btrfs_item_nr_offset(push_items),
3734 (btrfs_header_nritems(right) - push_items) *
3735 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003736 }
Yaneef1c492007-11-26 10:58:13 -05003737 right_nritems -= push_items;
3738 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003739 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003740 for (i = 0; i < right_nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003741 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003742
Chris Masoncfed81a2012-03-03 07:40:03 -05003743 push_space = push_space - btrfs_token_item_size(right,
3744 item, &token);
3745 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003746 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003747
Chris Mason5f39d392007-10-15 16:14:19 -04003748 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003749 if (right_nritems)
3750 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003751 else
3752 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003753
Chris Mason5f39d392007-10-15 16:14:19 -04003754 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003755 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003756
3757 /* then fixup the leaf pointer in the path */
3758 if (path->slots[0] < push_items) {
3759 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003760 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003761 free_extent_buffer(path->nodes[0]);
3762 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003763 path->slots[1] -= 1;
3764 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003765 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003766 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003767 path->slots[0] -= push_items;
3768 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003769 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003770 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003771out:
3772 btrfs_tree_unlock(left);
3773 free_extent_buffer(left);
3774 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003775}
3776
Chris Mason74123bd2007-02-02 11:05:29 -05003777/*
Chris Mason44871b12009-03-13 10:04:31 -04003778 * push some data in the path leaf to the left, trying to free up at
3779 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003780 *
3781 * max_slot can put a limit on how far into the leaf we'll push items. The
3782 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3783 * items
Chris Mason44871b12009-03-13 10:04:31 -04003784 */
3785static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003786 *root, struct btrfs_path *path, int min_data_size,
3787 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003788{
3789 struct extent_buffer *right = path->nodes[0];
3790 struct extent_buffer *left;
3791 int slot;
3792 int free_space;
3793 u32 right_nritems;
3794 int ret = 0;
3795
3796 slot = path->slots[1];
3797 if (slot == 0)
3798 return 1;
3799 if (!path->nodes[1])
3800 return 1;
3801
3802 right_nritems = btrfs_header_nritems(right);
3803 if (right_nritems == 0)
3804 return 1;
3805
3806 btrfs_assert_tree_locked(path->nodes[1]);
3807
3808 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003809 if (left == NULL)
3810 return 1;
3811
Chris Mason44871b12009-03-13 10:04:31 -04003812 btrfs_tree_lock(left);
3813 btrfs_set_lock_blocking(left);
3814
3815 free_space = btrfs_leaf_free_space(root, left);
3816 if (free_space < data_size) {
3817 ret = 1;
3818 goto out;
3819 }
3820
3821 /* cow and double check */
3822 ret = btrfs_cow_block(trans, root, left,
3823 path->nodes[1], slot - 1, &left);
3824 if (ret) {
3825 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003826 if (ret == -ENOSPC)
3827 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003828 goto out;
3829 }
3830
3831 free_space = btrfs_leaf_free_space(root, left);
3832 if (free_space < data_size) {
3833 ret = 1;
3834 goto out;
3835 }
3836
Chris Mason99d8f832010-07-07 10:51:48 -04003837 return __push_leaf_left(trans, root, path, min_data_size,
3838 empty, left, free_space, right_nritems,
3839 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003840out:
3841 btrfs_tree_unlock(left);
3842 free_extent_buffer(left);
3843 return ret;
3844}
3845
3846/*
Chris Mason74123bd2007-02-02 11:05:29 -05003847 * split the path's leaf in two, making sure there is at least data_size
3848 * available for the resulting leaf level of the path.
3849 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003850static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3851 struct btrfs_root *root,
3852 struct btrfs_path *path,
3853 struct extent_buffer *l,
3854 struct extent_buffer *right,
3855 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003856{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003857 int data_copy_size;
3858 int rt_data_off;
3859 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003860 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003861 struct btrfs_map_token token;
3862
3863 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003864
Chris Mason5f39d392007-10-15 16:14:19 -04003865 nritems = nritems - mid;
3866 btrfs_set_header_nritems(right, nritems);
3867 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3868
3869 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3870 btrfs_item_nr_offset(mid),
3871 nritems * sizeof(struct btrfs_item));
3872
3873 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003874 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3875 data_copy_size, btrfs_leaf_data(l) +
3876 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003877
Chris Mason5f39d392007-10-15 16:14:19 -04003878 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3879 btrfs_item_end_nr(l, mid);
3880
3881 for (i = 0; i < nritems; i++) {
Ross Kirkdd3cc162013-09-16 15:58:09 +01003882 struct btrfs_item *item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04003883 u32 ioff;
3884
Chris Masoncfed81a2012-03-03 07:40:03 -05003885 ioff = btrfs_token_item_offset(right, item, &token);
3886 btrfs_set_token_item_offset(right, item,
3887 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003888 }
Chris Mason74123bd2007-02-02 11:05:29 -05003889
Chris Mason5f39d392007-10-15 16:14:19 -04003890 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003891 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003892 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003893 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003894
3895 btrfs_mark_buffer_dirty(right);
3896 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003897 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003898
Chris Masonbe0e5c02007-01-26 15:51:26 -05003899 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003900 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003901 free_extent_buffer(path->nodes[0]);
3902 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003903 path->slots[0] -= mid;
3904 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003905 } else {
3906 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003907 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003908 }
Chris Mason5f39d392007-10-15 16:14:19 -04003909
Chris Masoneb60cea2007-02-02 09:18:22 -05003910 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003911}
3912
3913/*
Chris Mason99d8f832010-07-07 10:51:48 -04003914 * double splits happen when we need to insert a big item in the middle
3915 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3916 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3917 * A B C
3918 *
3919 * We avoid this by trying to push the items on either side of our target
3920 * into the adjacent leaves. If all goes well we can avoid the double split
3921 * completely.
3922 */
3923static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3924 struct btrfs_root *root,
3925 struct btrfs_path *path,
3926 int data_size)
3927{
3928 int ret;
3929 int progress = 0;
3930 int slot;
3931 u32 nritems;
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003932 int space_needed = data_size;
Chris Mason99d8f832010-07-07 10:51:48 -04003933
3934 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003935 if (slot < btrfs_header_nritems(path->nodes[0]))
3936 space_needed -= btrfs_leaf_free_space(root, path->nodes[0]);
Chris Mason99d8f832010-07-07 10:51:48 -04003937
3938 /*
3939 * try to push all the items after our slot into the
3940 * right leaf
3941 */
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003942 ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04003943 if (ret < 0)
3944 return ret;
3945
3946 if (ret == 0)
3947 progress++;
3948
3949 nritems = btrfs_header_nritems(path->nodes[0]);
3950 /*
3951 * our goal is to get our slot at the start or end of a leaf. If
3952 * we've done so we're done
3953 */
3954 if (path->slots[0] == 0 || path->slots[0] == nritems)
3955 return 0;
3956
3957 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3958 return 0;
3959
3960 /* try to push all the items before our slot into the next leaf */
3961 slot = path->slots[0];
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00003962 ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
Chris Mason99d8f832010-07-07 10:51:48 -04003963 if (ret < 0)
3964 return ret;
3965
3966 if (ret == 0)
3967 progress++;
3968
3969 if (progress)
3970 return 0;
3971 return 1;
3972}
3973
3974/*
Chris Mason44871b12009-03-13 10:04:31 -04003975 * split the path's leaf in two, making sure there is at least data_size
3976 * available for the resulting leaf level of the path.
3977 *
3978 * returns 0 if all went well and < 0 on failure.
3979 */
3980static noinline int split_leaf(struct btrfs_trans_handle *trans,
3981 struct btrfs_root *root,
3982 struct btrfs_key *ins_key,
3983 struct btrfs_path *path, int data_size,
3984 int extend)
3985{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003986 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003987 struct extent_buffer *l;
3988 u32 nritems;
3989 int mid;
3990 int slot;
3991 struct extent_buffer *right;
3992 int ret = 0;
3993 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003994 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003995 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003996 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003997
Yan, Zhenga5719522009-09-24 09:17:31 -04003998 l = path->nodes[0];
3999 slot = path->slots[0];
4000 if (extend && data_size + btrfs_item_size_nr(l, slot) +
4001 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
4002 return -EOVERFLOW;
4003
Chris Mason44871b12009-03-13 10:04:31 -04004004 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00004005 if (data_size && path->nodes[1]) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004006 int space_needed = data_size;
4007
4008 if (slot < btrfs_header_nritems(l))
4009 space_needed -= btrfs_leaf_free_space(root, l);
4010
4011 wret = push_leaf_right(trans, root, path, space_needed,
4012 space_needed, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04004013 if (wret < 0)
4014 return wret;
4015 if (wret) {
Filipe David Borba Manana5a4267c2013-11-25 03:20:46 +00004016 wret = push_leaf_left(trans, root, path, space_needed,
4017 space_needed, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04004018 if (wret < 0)
4019 return wret;
4020 }
4021 l = path->nodes[0];
4022
4023 /* did the pushes work? */
4024 if (btrfs_leaf_free_space(root, l) >= data_size)
4025 return 0;
4026 }
4027
4028 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00004029 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004030 if (ret)
4031 return ret;
4032 }
4033again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004034 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04004035 l = path->nodes[0];
4036 slot = path->slots[0];
4037 nritems = btrfs_header_nritems(l);
4038 mid = (nritems + 1) / 2;
4039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004040 if (mid <= slot) {
4041 if (nritems == 1 ||
4042 leaf_space_used(l, mid, nritems - mid) + data_size >
4043 BTRFS_LEAF_DATA_SIZE(root)) {
4044 if (slot >= nritems) {
4045 split = 0;
4046 } else {
4047 mid = slot;
4048 if (mid != nritems &&
4049 leaf_space_used(l, mid, nritems - mid) +
4050 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004051 if (data_size && !tried_avoid_double)
4052 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053 split = 2;
4054 }
4055 }
4056 }
4057 } else {
4058 if (leaf_space_used(l, 0, mid) + data_size >
4059 BTRFS_LEAF_DATA_SIZE(root)) {
4060 if (!extend && data_size && slot == 0) {
4061 split = 0;
4062 } else if ((extend || !data_size) && slot == 0) {
4063 mid = 1;
4064 } else {
4065 mid = slot;
4066 if (mid != nritems &&
4067 leaf_space_used(l, mid, nritems - mid) +
4068 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004069 if (data_size && !tried_avoid_double)
4070 goto push_for_double;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304071 split = 2;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004072 }
4073 }
4074 }
4075 }
4076
4077 if (split == 0)
4078 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4079 else
4080 btrfs_item_key(l, &disk_key, mid);
4081
4082 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004083 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004084 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004085 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004086 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004087
4088 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004089
4090 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4091 btrfs_set_header_bytenr(right, right->start);
4092 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004093 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004094 btrfs_set_header_owner(right, root->root_key.objectid);
4095 btrfs_set_header_level(right, 0);
4096 write_extent_buffer(right, root->fs_info->fsid,
Ross Kirk0a4e5582013-09-24 10:12:38 +01004097 btrfs_header_fsid(), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004098
4099 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004100 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004101 BTRFS_UUID_SIZE);
4102
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004103 if (split == 0) {
4104 if (mid <= slot) {
4105 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004106 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004107 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004108 btrfs_tree_unlock(path->nodes[0]);
4109 free_extent_buffer(path->nodes[0]);
4110 path->nodes[0] = right;
4111 path->slots[0] = 0;
4112 path->slots[1] += 1;
4113 } else {
4114 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004115 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004116 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004117 btrfs_tree_unlock(path->nodes[0]);
4118 free_extent_buffer(path->nodes[0]);
4119 path->nodes[0] = right;
4120 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004121 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004122 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004123 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004124 btrfs_mark_buffer_dirty(right);
4125 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004126 }
4127
Jeff Mahoney143bede2012-03-01 14:56:26 +01004128 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004129
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004130 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004131 BUG_ON(num_doubles != 0);
4132 num_doubles++;
4133 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004134 }
Chris Mason44871b12009-03-13 10:04:31 -04004135
Jeff Mahoney143bede2012-03-01 14:56:26 +01004136 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004137
4138push_for_double:
4139 push_for_double_split(trans, root, path, data_size);
4140 tried_avoid_double = 1;
4141 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4142 return 0;
4143 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004144}
4145
Yan, Zhengad48fd752009-11-12 09:33:58 +00004146static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4147 struct btrfs_root *root,
4148 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004149{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004150 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004151 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004152 struct btrfs_file_extent_item *fi;
4153 u64 extent_len = 0;
4154 u32 item_size;
4155 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004156
4157 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004158 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4159
4160 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4161 key.type != BTRFS_EXTENT_CSUM_KEY);
4162
4163 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4164 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004165
4166 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004167 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4168 fi = btrfs_item_ptr(leaf, path->slots[0],
4169 struct btrfs_file_extent_item);
4170 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4171 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004172 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004173
Chris Mason459931e2008-12-10 09:10:46 -05004174 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004175 path->search_for_split = 1;
4176 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004177 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004178 if (ret < 0)
4179 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004180
Yan, Zhengad48fd752009-11-12 09:33:58 +00004181 ret = -EAGAIN;
4182 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004183 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004184 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4185 goto err;
4186
Chris Mason109f6ae2010-04-02 09:20:18 -04004187 /* the leaf has changed, it now has room. return now */
4188 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4189 goto err;
4190
Yan, Zhengad48fd752009-11-12 09:33:58 +00004191 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4192 fi = btrfs_item_ptr(leaf, path->slots[0],
4193 struct btrfs_file_extent_item);
4194 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4195 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004196 }
4197
Chris Masonb9473432009-03-13 11:00:37 -04004198 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004199 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004200 if (ret)
4201 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004202
Yan, Zhengad48fd752009-11-12 09:33:58 +00004203 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004204 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004205 return 0;
4206err:
4207 path->keep_locks = 0;
4208 return ret;
4209}
4210
4211static noinline int split_item(struct btrfs_trans_handle *trans,
4212 struct btrfs_root *root,
4213 struct btrfs_path *path,
4214 struct btrfs_key *new_key,
4215 unsigned long split_offset)
4216{
4217 struct extent_buffer *leaf;
4218 struct btrfs_item *item;
4219 struct btrfs_item *new_item;
4220 int slot;
4221 char *buf;
4222 u32 nritems;
4223 u32 item_size;
4224 u32 orig_offset;
4225 struct btrfs_disk_key disk_key;
4226
Chris Masonb9473432009-03-13 11:00:37 -04004227 leaf = path->nodes[0];
4228 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4229
Chris Masonb4ce94d2009-02-04 09:25:08 -05004230 btrfs_set_path_blocking(path);
4231
Ross Kirkdd3cc162013-09-16 15:58:09 +01004232 item = btrfs_item_nr(path->slots[0]);
Chris Mason459931e2008-12-10 09:10:46 -05004233 orig_offset = btrfs_item_offset(leaf, item);
4234 item_size = btrfs_item_size(leaf, item);
4235
Chris Mason459931e2008-12-10 09:10:46 -05004236 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004237 if (!buf)
4238 return -ENOMEM;
4239
Chris Mason459931e2008-12-10 09:10:46 -05004240 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4241 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004242
Chris Mason459931e2008-12-10 09:10:46 -05004243 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004244 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004245 if (slot != nritems) {
4246 /* shift the items */
4247 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004248 btrfs_item_nr_offset(slot),
4249 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004250 }
4251
4252 btrfs_cpu_key_to_disk(&disk_key, new_key);
4253 btrfs_set_item_key(leaf, &disk_key, slot);
4254
Ross Kirkdd3cc162013-09-16 15:58:09 +01004255 new_item = btrfs_item_nr(slot);
Chris Mason459931e2008-12-10 09:10:46 -05004256
4257 btrfs_set_item_offset(leaf, new_item, orig_offset);
4258 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4259
4260 btrfs_set_item_offset(leaf, item,
4261 orig_offset + item_size - split_offset);
4262 btrfs_set_item_size(leaf, item, split_offset);
4263
4264 btrfs_set_header_nritems(leaf, nritems + 1);
4265
4266 /* write the data for the start of the original item */
4267 write_extent_buffer(leaf, buf,
4268 btrfs_item_ptr_offset(leaf, path->slots[0]),
4269 split_offset);
4270
4271 /* write the data for the new item */
4272 write_extent_buffer(leaf, buf + split_offset,
4273 btrfs_item_ptr_offset(leaf, slot),
4274 item_size - split_offset);
4275 btrfs_mark_buffer_dirty(leaf);
4276
Yan, Zhengad48fd752009-11-12 09:33:58 +00004277 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004278 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004279 return 0;
4280}
4281
4282/*
4283 * This function splits a single item into two items,
4284 * giving 'new_key' to the new item and splitting the
4285 * old one at split_offset (from the start of the item).
4286 *
4287 * The path may be released by this operation. After
4288 * the split, the path is pointing to the old item. The
4289 * new item is going to be in the same node as the old one.
4290 *
4291 * Note, the item being split must be smaller enough to live alone on
4292 * a tree block with room for one extra struct btrfs_item
4293 *
4294 * This allows us to split the item in place, keeping a lock on the
4295 * leaf the entire time.
4296 */
4297int btrfs_split_item(struct btrfs_trans_handle *trans,
4298 struct btrfs_root *root,
4299 struct btrfs_path *path,
4300 struct btrfs_key *new_key,
4301 unsigned long split_offset)
4302{
4303 int ret;
4304 ret = setup_leaf_for_split(trans, root, path,
4305 sizeof(struct btrfs_item));
4306 if (ret)
4307 return ret;
4308
4309 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004310 return ret;
4311}
4312
4313/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004314 * This function duplicate a item, giving 'new_key' to the new item.
4315 * It guarantees both items live in the same tree leaf and the new item
4316 * is contiguous with the original item.
4317 *
4318 * This allows us to split file extent in place, keeping a lock on the
4319 * leaf the entire time.
4320 */
4321int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4322 struct btrfs_root *root,
4323 struct btrfs_path *path,
4324 struct btrfs_key *new_key)
4325{
4326 struct extent_buffer *leaf;
4327 int ret;
4328 u32 item_size;
4329
4330 leaf = path->nodes[0];
4331 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4332 ret = setup_leaf_for_split(trans, root, path,
4333 item_size + sizeof(struct btrfs_item));
4334 if (ret)
4335 return ret;
4336
4337 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004338 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004339 item_size, item_size +
4340 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004341 leaf = path->nodes[0];
4342 memcpy_extent_buffer(leaf,
4343 btrfs_item_ptr_offset(leaf, path->slots[0]),
4344 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4345 item_size);
4346 return 0;
4347}
4348
4349/*
Chris Masond352ac62008-09-29 15:18:18 -04004350 * make the item pointed to by the path smaller. new_size indicates
4351 * how small to make it, and from_end tells us if we just chop bytes
4352 * off the end of the item or if we shift the item to chop bytes off
4353 * the front.
4354 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004355void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004356 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004357{
Chris Masonb18c6682007-04-17 13:26:50 -04004358 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004359 struct extent_buffer *leaf;
4360 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004361 u32 nritems;
4362 unsigned int data_end;
4363 unsigned int old_data_start;
4364 unsigned int old_size;
4365 unsigned int size_diff;
4366 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004367 struct btrfs_map_token token;
4368
4369 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004370
Chris Mason5f39d392007-10-15 16:14:19 -04004371 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004372 slot = path->slots[0];
4373
4374 old_size = btrfs_item_size_nr(leaf, slot);
4375 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004376 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004377
Chris Mason5f39d392007-10-15 16:14:19 -04004378 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004379 data_end = leaf_data_end(root, leaf);
4380
Chris Mason5f39d392007-10-15 16:14:19 -04004381 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004382
Chris Masonb18c6682007-04-17 13:26:50 -04004383 size_diff = old_size - new_size;
4384
4385 BUG_ON(slot < 0);
4386 BUG_ON(slot >= nritems);
4387
4388 /*
4389 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4390 */
4391 /* first correct the data pointers */
4392 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004393 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004394 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004395
Chris Masoncfed81a2012-03-03 07:40:03 -05004396 ioff = btrfs_token_item_offset(leaf, item, &token);
4397 btrfs_set_token_item_offset(leaf, item,
4398 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004399 }
Chris Masondb945352007-10-15 16:15:53 -04004400
Chris Masonb18c6682007-04-17 13:26:50 -04004401 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004402 if (from_end) {
4403 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4404 data_end + size_diff, btrfs_leaf_data(leaf) +
4405 data_end, old_data_start + new_size - data_end);
4406 } else {
4407 struct btrfs_disk_key disk_key;
4408 u64 offset;
4409
4410 btrfs_item_key(leaf, &disk_key, slot);
4411
4412 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4413 unsigned long ptr;
4414 struct btrfs_file_extent_item *fi;
4415
4416 fi = btrfs_item_ptr(leaf, slot,
4417 struct btrfs_file_extent_item);
4418 fi = (struct btrfs_file_extent_item *)(
4419 (unsigned long)fi - size_diff);
4420
4421 if (btrfs_file_extent_type(leaf, fi) ==
4422 BTRFS_FILE_EXTENT_INLINE) {
4423 ptr = btrfs_item_ptr_offset(leaf, slot);
4424 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004425 (unsigned long)fi,
4426 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004427 disk_bytenr));
4428 }
4429 }
4430
4431 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4432 data_end + size_diff, btrfs_leaf_data(leaf) +
4433 data_end, old_data_start - data_end);
4434
4435 offset = btrfs_disk_key_offset(&disk_key);
4436 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4437 btrfs_set_item_key(leaf, &disk_key, slot);
4438 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004439 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004440 }
Chris Mason5f39d392007-10-15 16:14:19 -04004441
Ross Kirkdd3cc162013-09-16 15:58:09 +01004442 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004443 btrfs_set_item_size(leaf, item, new_size);
4444 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004445
Chris Mason5f39d392007-10-15 16:14:19 -04004446 if (btrfs_leaf_free_space(root, leaf) < 0) {
4447 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004448 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004449 }
Chris Masonb18c6682007-04-17 13:26:50 -04004450}
4451
Chris Masond352ac62008-09-29 15:18:18 -04004452/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004453 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004454 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004455void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004456 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004457{
Chris Mason6567e832007-04-16 09:22:45 -04004458 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004459 struct extent_buffer *leaf;
4460 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004461 u32 nritems;
4462 unsigned int data_end;
4463 unsigned int old_data;
4464 unsigned int old_size;
4465 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004466 struct btrfs_map_token token;
4467
4468 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004469
Chris Mason5f39d392007-10-15 16:14:19 -04004470 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004471
Chris Mason5f39d392007-10-15 16:14:19 -04004472 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004473 data_end = leaf_data_end(root, leaf);
4474
Chris Mason5f39d392007-10-15 16:14:19 -04004475 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4476 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004477 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004478 }
Chris Mason6567e832007-04-16 09:22:45 -04004479 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004480 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004481
4482 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004483 if (slot >= nritems) {
4484 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004485 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4486 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004487 BUG_ON(1);
4488 }
Chris Mason6567e832007-04-16 09:22:45 -04004489
4490 /*
4491 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4492 */
4493 /* first correct the data pointers */
4494 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004495 u32 ioff;
Ross Kirkdd3cc162013-09-16 15:58:09 +01004496 item = btrfs_item_nr(i);
Chris Masondb945352007-10-15 16:15:53 -04004497
Chris Masoncfed81a2012-03-03 07:40:03 -05004498 ioff = btrfs_token_item_offset(leaf, item, &token);
4499 btrfs_set_token_item_offset(leaf, item,
4500 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004501 }
Chris Mason5f39d392007-10-15 16:14:19 -04004502
Chris Mason6567e832007-04-16 09:22:45 -04004503 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004504 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004505 data_end - data_size, btrfs_leaf_data(leaf) +
4506 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004507
Chris Mason6567e832007-04-16 09:22:45 -04004508 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004509 old_size = btrfs_item_size_nr(leaf, slot);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004510 item = btrfs_item_nr(slot);
Chris Mason5f39d392007-10-15 16:14:19 -04004511 btrfs_set_item_size(leaf, item, old_size + data_size);
4512 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004513
Chris Mason5f39d392007-10-15 16:14:19 -04004514 if (btrfs_leaf_free_space(root, leaf) < 0) {
4515 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004516 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004517 }
Chris Mason6567e832007-04-16 09:22:45 -04004518}
4519
Chris Mason74123bd2007-02-02 11:05:29 -05004520/*
Chris Mason44871b12009-03-13 10:04:31 -04004521 * this is a helper for btrfs_insert_empty_items, the main goal here is
4522 * to save stack depth by doing the bulk of the work in a function
4523 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004524 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004525void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004526 struct btrfs_key *cpu_key, u32 *data_size,
4527 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004528{
Chris Mason5f39d392007-10-15 16:14:19 -04004529 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004530 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004531 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004532 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004533 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004534 struct extent_buffer *leaf;
4535 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004536 struct btrfs_map_token token;
4537
4538 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004539
Chris Mason5f39d392007-10-15 16:14:19 -04004540 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004541 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004542
Chris Mason5f39d392007-10-15 16:14:19 -04004543 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004544 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004545
Chris Masonf25956c2008-09-12 15:32:53 -04004546 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004547 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004548 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004549 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004550 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004551 }
Chris Mason5f39d392007-10-15 16:14:19 -04004552
Chris Masonbe0e5c02007-01-26 15:51:26 -05004553 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004554 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004555
Chris Mason5f39d392007-10-15 16:14:19 -04004556 if (old_data < data_end) {
4557 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004558 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004559 slot, old_data, data_end);
4560 BUG_ON(1);
4561 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004562 /*
4563 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4564 */
4565 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004566 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004567 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004568
Ross Kirkdd3cc162013-09-16 15:58:09 +01004569 item = btrfs_item_nr( i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004570 ioff = btrfs_token_item_offset(leaf, item, &token);
4571 btrfs_set_token_item_offset(leaf, item,
4572 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004573 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004574 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004575 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004576 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004577 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004578
4579 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004580 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004581 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004582 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004583 data_end = old_data;
4584 }
Chris Mason5f39d392007-10-15 16:14:19 -04004585
Chris Mason62e27492007-03-15 12:56:47 -04004586 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004587 for (i = 0; i < nr; i++) {
4588 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4589 btrfs_set_item_key(leaf, &disk_key, slot + i);
Ross Kirkdd3cc162013-09-16 15:58:09 +01004590 item = btrfs_item_nr(slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004591 btrfs_set_token_item_offset(leaf, item,
4592 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004593 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004594 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004595 }
Chris Mason44871b12009-03-13 10:04:31 -04004596
Chris Mason9c583092008-01-29 15:15:18 -05004597 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004598
Chris Mason5a01a2e2008-01-30 11:43:54 -05004599 if (slot == 0) {
4600 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004601 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004602 }
Chris Masonb9473432009-03-13 11:00:37 -04004603 btrfs_unlock_up_safe(path, 1);
4604 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004605
Chris Mason5f39d392007-10-15 16:14:19 -04004606 if (btrfs_leaf_free_space(root, leaf) < 0) {
4607 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004608 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004609 }
Chris Mason44871b12009-03-13 10:04:31 -04004610}
4611
4612/*
4613 * Given a key and some data, insert items into the tree.
4614 * This does all the path init required, making room in the tree if needed.
4615 */
4616int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4617 struct btrfs_root *root,
4618 struct btrfs_path *path,
4619 struct btrfs_key *cpu_key, u32 *data_size,
4620 int nr)
4621{
Chris Mason44871b12009-03-13 10:04:31 -04004622 int ret = 0;
4623 int slot;
4624 int i;
4625 u32 total_size = 0;
4626 u32 total_data = 0;
4627
4628 for (i = 0; i < nr; i++)
4629 total_data += data_size[i];
4630
4631 total_size = total_data + (nr * sizeof(struct btrfs_item));
4632 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4633 if (ret == 0)
4634 return -EEXIST;
4635 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004636 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004637
Chris Mason44871b12009-03-13 10:04:31 -04004638 slot = path->slots[0];
4639 BUG_ON(slot < 0);
4640
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004641 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004642 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004643 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004644}
4645
4646/*
4647 * Given a key and some data, insert an item into the tree.
4648 * This does all the path init required, making room in the tree if needed.
4649 */
Chris Masone089f052007-03-16 16:20:31 -04004650int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4651 *root, struct btrfs_key *cpu_key, void *data, u32
4652 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004653{
4654 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004655 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004656 struct extent_buffer *leaf;
4657 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004658
Chris Mason2c90e5d2007-04-02 10:50:19 -04004659 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004660 if (!path)
4661 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004662 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004663 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004664 leaf = path->nodes[0];
4665 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4666 write_extent_buffer(leaf, data, ptr, data_size);
4667 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004668 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004669 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004670 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004671}
4672
Chris Mason74123bd2007-02-02 11:05:29 -05004673/*
Chris Mason5de08d72007-02-24 06:24:44 -05004674 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004675 *
Chris Masond352ac62008-09-29 15:18:18 -04004676 * the tree should have been previously balanced so the deletion does not
4677 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004678 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004679static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4680 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004681{
Chris Mason5f39d392007-10-15 16:14:19 -04004682 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004683 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004684 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004685
Chris Mason5f39d392007-10-15 16:14:19 -04004686 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004687 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004688 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004689 tree_mod_log_eb_move(root->fs_info, parent, slot,
4690 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004691 memmove_extent_buffer(parent,
4692 btrfs_node_key_ptr_offset(slot),
4693 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004694 sizeof(struct btrfs_key_ptr) *
4695 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004696 } else if (level) {
4697 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004698 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004699 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004700 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004701
Chris Mason7518a232007-03-12 12:01:18 -04004702 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004703 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004704 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004705 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004706 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004707 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004708 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004709 struct btrfs_disk_key disk_key;
4710
4711 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004712 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004713 }
Chris Masond6025572007-03-30 14:27:56 -04004714 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715}
4716
Chris Mason74123bd2007-02-02 11:05:29 -05004717/*
Chris Mason323ac952008-10-01 19:05:46 -04004718 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004719 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004720 *
4721 * This deletes the pointer in path->nodes[1] and frees the leaf
4722 * block extent. zero is returned if it all worked out, < 0 otherwise.
4723 *
4724 * The path must have already been setup for deleting the leaf, including
4725 * all the proper balancing. path->nodes[1] must be locked.
4726 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004727static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4728 struct btrfs_root *root,
4729 struct btrfs_path *path,
4730 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004731{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004732 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004733 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004734
Chris Mason4d081c42009-02-04 09:31:28 -05004735 /*
4736 * btrfs_free_extent is expensive, we want to make sure we
4737 * aren't holding any locks when we call it
4738 */
4739 btrfs_unlock_up_safe(path, 0);
4740
Yan, Zhengf0486c62010-05-16 10:46:25 -04004741 root_sub_used(root, leaf->len);
4742
Josef Bacik3083ee22012-03-09 16:01:49 -05004743 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004744 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004745 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004746}
4747/*
Chris Mason74123bd2007-02-02 11:05:29 -05004748 * delete the item at the leaf level in path. If that empties
4749 * the leaf, remove it from the tree
4750 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004751int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4752 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004753{
Chris Mason5f39d392007-10-15 16:14:19 -04004754 struct extent_buffer *leaf;
4755 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004756 int last_off;
4757 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004758 int ret = 0;
4759 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004760 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004761 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004762 struct btrfs_map_token token;
4763
4764 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004765
Chris Mason5f39d392007-10-15 16:14:19 -04004766 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004767 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4768
4769 for (i = 0; i < nr; i++)
4770 dsize += btrfs_item_size_nr(leaf, slot + i);
4771
Chris Mason5f39d392007-10-15 16:14:19 -04004772 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004773
Chris Mason85e21ba2008-01-29 15:11:36 -05004774 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004775 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004776
4777 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004778 data_end + dsize,
4779 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004780 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004781
Chris Mason85e21ba2008-01-29 15:11:36 -05004782 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004783 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004784
Ross Kirkdd3cc162013-09-16 15:58:09 +01004785 item = btrfs_item_nr(i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004786 ioff = btrfs_token_item_offset(leaf, item, &token);
4787 btrfs_set_token_item_offset(leaf, item,
4788 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004789 }
Chris Masondb945352007-10-15 16:15:53 -04004790
Chris Mason5f39d392007-10-15 16:14:19 -04004791 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004792 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004793 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004794 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004795 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004796 btrfs_set_header_nritems(leaf, nritems - nr);
4797 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004798
Chris Mason74123bd2007-02-02 11:05:29 -05004799 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004800 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004801 if (leaf == root->node) {
4802 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004803 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004804 btrfs_set_path_blocking(path);
4805 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004806 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004807 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004808 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004809 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004810 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004811 struct btrfs_disk_key disk_key;
4812
4813 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004814 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004815 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004816
Chris Mason74123bd2007-02-02 11:05:29 -05004817 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004818 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004819 /* push_leaf_left fixes the path.
4820 * make sure the path still points to our leaf
4821 * for possible call to del_ptr below
4822 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004823 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004824 extent_buffer_get(leaf);
4825
Chris Masonb9473432009-03-13 11:00:37 -04004826 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004827 wret = push_leaf_left(trans, root, path, 1, 1,
4828 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004829 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004830 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004831
4832 if (path->nodes[0] == leaf &&
4833 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004834 wret = push_leaf_right(trans, root, path, 1,
4835 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004836 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004837 ret = wret;
4838 }
Chris Mason5f39d392007-10-15 16:14:19 -04004839
4840 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004841 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004842 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004843 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004844 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004845 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004846 /* if we're still in the path, make sure
4847 * we're dirty. Otherwise, one of the
4848 * push_leaf functions must have already
4849 * dirtied this buffer
4850 */
4851 if (path->nodes[0] == leaf)
4852 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004853 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004854 }
Chris Masond5719762007-03-23 10:01:08 -04004855 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004856 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004857 }
4858 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004859 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004860}
4861
Chris Mason97571fd2007-02-24 13:39:08 -05004862/*
Chris Mason925baed2008-06-25 16:01:30 -04004863 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004864 * returns 0 if it found something or 1 if there are no lesser leaves.
4865 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004866 *
4867 * This may release the path, and so you may lose any locks held at the
4868 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004869 */
Josef Bacik16e75492013-10-22 12:18:51 -04004870int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05004871{
Chris Mason925baed2008-06-25 16:01:30 -04004872 struct btrfs_key key;
4873 struct btrfs_disk_key found_key;
4874 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004875
Chris Mason925baed2008-06-25 16:01:30 -04004876 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004877
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004878 if (key.offset > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004879 key.offset--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004880 } else if (key.type > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004881 key.type--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004882 key.offset = (u64)-1;
4883 } else if (key.objectid > 0) {
Chris Mason925baed2008-06-25 16:01:30 -04004884 key.objectid--;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004885 key.type = (u8)-1;
4886 key.offset = (u64)-1;
4887 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004888 return 1;
Filipe David Borba Mananae8b0d7242013-10-15 00:12:27 +01004889 }
Chris Mason7bb86312007-12-11 09:25:06 -05004890
David Sterbab3b4aa72011-04-21 01:20:15 +02004891 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004892 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4893 if (ret < 0)
4894 return ret;
4895 btrfs_item_key(path->nodes[0], &found_key, 0);
4896 ret = comp_keys(&found_key, &key);
4897 if (ret < 0)
4898 return 0;
4899 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004900}
4901
Chris Mason3f157a22008-06-25 16:01:31 -04004902/*
4903 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004904 * for nodes or leaves that are have a minimum transaction id.
4905 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004906 *
4907 * This does not cow, but it does stuff the starting key it finds back
4908 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4909 * key and get a writable path.
4910 *
4911 * This does lock as it descends, and path->keep_locks should be set
4912 * to 1 by the caller.
4913 *
4914 * This honors path->lowest_level to prevent descent past a given level
4915 * of the tree.
4916 *
Chris Masond352ac62008-09-29 15:18:18 -04004917 * min_trans indicates the oldest transaction that you are interested
4918 * in walking through. Any nodes or leaves older than min_trans are
4919 * skipped over (without reading them).
4920 *
Chris Mason3f157a22008-06-25 16:01:31 -04004921 * returns zero if something useful was found, < 0 on error and 1 if there
4922 * was nothing in the tree that matched the search criteria.
4923 */
4924int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004925 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004926 u64 min_trans)
4927{
4928 struct extent_buffer *cur;
4929 struct btrfs_key found_key;
4930 int slot;
Yan96524802008-07-24 12:19:49 -04004931 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004932 u32 nritems;
4933 int level;
4934 int ret = 1;
4935
Chris Mason934d3752008-12-08 16:43:10 -05004936 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004937again:
Chris Masonbd681512011-07-16 15:23:14 -04004938 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004939 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004940 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004941 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004942 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004943
4944 if (btrfs_header_generation(cur) < min_trans) {
4945 ret = 1;
4946 goto out;
4947 }
Chris Masond3977122009-01-05 21:25:51 -05004948 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004949 nritems = btrfs_header_nritems(cur);
4950 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004951 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004952
Chris Mason323ac952008-10-01 19:05:46 -04004953 /* at the lowest level, we're done, setup the path and exit */
4954 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004955 if (slot >= nritems)
4956 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004957 ret = 0;
4958 path->slots[level] = slot;
4959 btrfs_item_key_to_cpu(cur, &found_key, slot);
4960 goto out;
4961 }
Yan96524802008-07-24 12:19:49 -04004962 if (sret && slot > 0)
4963 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004964 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004965 * check this node pointer against the min_trans parameters.
4966 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004967 */
Chris Masond3977122009-01-05 21:25:51 -05004968 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004969 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004970
Chris Mason3f157a22008-06-25 16:01:31 -04004971 gen = btrfs_node_ptr_generation(cur, slot);
4972 if (gen < min_trans) {
4973 slot++;
4974 continue;
4975 }
Eric Sandeende78b512013-01-31 18:21:12 +00004976 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004977 }
Chris Masone02119d2008-09-05 16:13:11 -04004978find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004979 /*
4980 * we didn't find a candidate key in this node, walk forward
4981 * and find another one
4982 */
4983 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004984 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004985 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004986 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004987 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004988 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004989 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004990 goto again;
4991 } else {
4992 goto out;
4993 }
4994 }
4995 /* save our key for returning back */
4996 btrfs_node_key_to_cpu(cur, &found_key, slot);
4997 path->slots[level] = slot;
4998 if (level == path->lowest_level) {
4999 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04005000 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04005001 goto out;
5002 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05005003 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005004 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005005 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04005006
Chris Masonbd681512011-07-16 15:23:14 -04005007 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05005008
Chris Masonbd681512011-07-16 15:23:14 -04005009 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04005010 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04005011 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04005012 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04005013 }
5014out:
5015 if (ret == 0)
5016 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05005017 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04005018 return ret;
5019}
5020
Alexander Block70698302012-06-05 21:07:48 +02005021static void tree_move_down(struct btrfs_root *root,
5022 struct btrfs_path *path,
5023 int *level, int root_level)
5024{
Chris Mason74dd17f2012-08-07 16:25:13 -04005025 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02005026 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
5027 path->slots[*level]);
5028 path->slots[*level - 1] = 0;
5029 (*level)--;
5030}
5031
5032static int tree_move_next_or_upnext(struct btrfs_root *root,
5033 struct btrfs_path *path,
5034 int *level, int root_level)
5035{
5036 int ret = 0;
5037 int nritems;
5038 nritems = btrfs_header_nritems(path->nodes[*level]);
5039
5040 path->slots[*level]++;
5041
Chris Mason74dd17f2012-08-07 16:25:13 -04005042 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02005043 if (*level == root_level)
5044 return -1;
5045
5046 /* move upnext */
5047 path->slots[*level] = 0;
5048 free_extent_buffer(path->nodes[*level]);
5049 path->nodes[*level] = NULL;
5050 (*level)++;
5051 path->slots[*level]++;
5052
5053 nritems = btrfs_header_nritems(path->nodes[*level]);
5054 ret = 1;
5055 }
5056 return ret;
5057}
5058
5059/*
5060 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5061 * or down.
5062 */
5063static int tree_advance(struct btrfs_root *root,
5064 struct btrfs_path *path,
5065 int *level, int root_level,
5066 int allow_down,
5067 struct btrfs_key *key)
5068{
5069 int ret;
5070
5071 if (*level == 0 || !allow_down) {
5072 ret = tree_move_next_or_upnext(root, path, level, root_level);
5073 } else {
5074 tree_move_down(root, path, level, root_level);
5075 ret = 0;
5076 }
5077 if (ret >= 0) {
5078 if (*level == 0)
5079 btrfs_item_key_to_cpu(path->nodes[*level], key,
5080 path->slots[*level]);
5081 else
5082 btrfs_node_key_to_cpu(path->nodes[*level], key,
5083 path->slots[*level]);
5084 }
5085 return ret;
5086}
5087
5088static int tree_compare_item(struct btrfs_root *left_root,
5089 struct btrfs_path *left_path,
5090 struct btrfs_path *right_path,
5091 char *tmp_buf)
5092{
5093 int cmp;
5094 int len1, len2;
5095 unsigned long off1, off2;
5096
5097 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5098 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5099 if (len1 != len2)
5100 return 1;
5101
5102 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5103 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5104 right_path->slots[0]);
5105
5106 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5107
5108 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5109 if (cmp)
5110 return 1;
5111 return 0;
5112}
5113
5114#define ADVANCE 1
5115#define ADVANCE_ONLY_NEXT -1
5116
5117/*
5118 * This function compares two trees and calls the provided callback for
5119 * every changed/new/deleted item it finds.
5120 * If shared tree blocks are encountered, whole subtrees are skipped, making
5121 * the compare pretty fast on snapshotted subvolumes.
5122 *
5123 * This currently works on commit roots only. As commit roots are read only,
5124 * we don't do any locking. The commit roots are protected with transactions.
5125 * Transactions are ended and rejoined when a commit is tried in between.
5126 *
5127 * This function checks for modifications done to the trees while comparing.
5128 * If it detects a change, it aborts immediately.
5129 */
5130int btrfs_compare_trees(struct btrfs_root *left_root,
5131 struct btrfs_root *right_root,
5132 btrfs_changed_cb_t changed_cb, void *ctx)
5133{
5134 int ret;
5135 int cmp;
5136 struct btrfs_trans_handle *trans = NULL;
5137 struct btrfs_path *left_path = NULL;
5138 struct btrfs_path *right_path = NULL;
5139 struct btrfs_key left_key;
5140 struct btrfs_key right_key;
5141 char *tmp_buf = NULL;
5142 int left_root_level;
5143 int right_root_level;
5144 int left_level;
5145 int right_level;
5146 int left_end_reached;
5147 int right_end_reached;
5148 int advance_left;
5149 int advance_right;
5150 u64 left_blockptr;
5151 u64 right_blockptr;
5152 u64 left_start_ctransid;
5153 u64 right_start_ctransid;
5154 u64 ctransid;
5155
5156 left_path = btrfs_alloc_path();
5157 if (!left_path) {
5158 ret = -ENOMEM;
5159 goto out;
5160 }
5161 right_path = btrfs_alloc_path();
5162 if (!right_path) {
5163 ret = -ENOMEM;
5164 goto out;
5165 }
5166
5167 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5168 if (!tmp_buf) {
5169 ret = -ENOMEM;
5170 goto out;
5171 }
5172
5173 left_path->search_commit_root = 1;
5174 left_path->skip_locking = 1;
5175 right_path->search_commit_root = 1;
5176 right_path->skip_locking = 1;
5177
Anand Jain5f3ab902012-12-07 09:28:54 +00005178 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005179 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005180 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005181
Anand Jain5f3ab902012-12-07 09:28:54 +00005182 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005183 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005184 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005185
5186 trans = btrfs_join_transaction(left_root);
5187 if (IS_ERR(trans)) {
5188 ret = PTR_ERR(trans);
5189 trans = NULL;
5190 goto out;
5191 }
5192
5193 /*
5194 * Strategy: Go to the first items of both trees. Then do
5195 *
5196 * If both trees are at level 0
5197 * Compare keys of current items
5198 * If left < right treat left item as new, advance left tree
5199 * and repeat
5200 * If left > right treat right item as deleted, advance right tree
5201 * and repeat
5202 * If left == right do deep compare of items, treat as changed if
5203 * needed, advance both trees and repeat
5204 * If both trees are at the same level but not at level 0
5205 * Compare keys of current nodes/leafs
5206 * If left < right advance left tree and repeat
5207 * If left > right advance right tree and repeat
5208 * If left == right compare blockptrs of the next nodes/leafs
5209 * If they match advance both trees but stay at the same level
5210 * and repeat
5211 * If they don't match advance both trees while allowing to go
5212 * deeper and repeat
5213 * If tree levels are different
5214 * Advance the tree that needs it and repeat
5215 *
5216 * Advancing a tree means:
5217 * If we are at level 0, try to go to the next slot. If that's not
5218 * possible, go one level up and repeat. Stop when we found a level
5219 * where we could go to the next slot. We may at this point be on a
5220 * node or a leaf.
5221 *
5222 * If we are not at level 0 and not on shared tree blocks, go one
5223 * level deeper.
5224 *
5225 * If we are not at level 0 and on shared tree blocks, go one slot to
5226 * the right if possible or go up and right.
5227 */
5228
5229 left_level = btrfs_header_level(left_root->commit_root);
5230 left_root_level = left_level;
5231 left_path->nodes[left_level] = left_root->commit_root;
5232 extent_buffer_get(left_path->nodes[left_level]);
5233
5234 right_level = btrfs_header_level(right_root->commit_root);
5235 right_root_level = right_level;
5236 right_path->nodes[right_level] = right_root->commit_root;
5237 extent_buffer_get(right_path->nodes[right_level]);
5238
5239 if (left_level == 0)
5240 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5241 &left_key, left_path->slots[left_level]);
5242 else
5243 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5244 &left_key, left_path->slots[left_level]);
5245 if (right_level == 0)
5246 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5247 &right_key, right_path->slots[right_level]);
5248 else
5249 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5250 &right_key, right_path->slots[right_level]);
5251
5252 left_end_reached = right_end_reached = 0;
5253 advance_left = advance_right = 0;
5254
5255 while (1) {
5256 /*
5257 * We need to make sure the transaction does not get committed
5258 * while we do anything on commit roots. This means, we need to
5259 * join and leave transactions for every item that we process.
5260 */
5261 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5262 btrfs_release_path(left_path);
5263 btrfs_release_path(right_path);
5264
5265 ret = btrfs_end_transaction(trans, left_root);
5266 trans = NULL;
5267 if (ret < 0)
5268 goto out;
5269 }
5270 /* now rejoin the transaction */
5271 if (!trans) {
5272 trans = btrfs_join_transaction(left_root);
5273 if (IS_ERR(trans)) {
5274 ret = PTR_ERR(trans);
5275 trans = NULL;
5276 goto out;
5277 }
5278
Anand Jain5f3ab902012-12-07 09:28:54 +00005279 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005280 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005281 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005282 if (ctransid != left_start_ctransid)
5283 left_start_ctransid = 0;
5284
Anand Jain5f3ab902012-12-07 09:28:54 +00005285 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005286 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005287 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005288 if (ctransid != right_start_ctransid)
5289 right_start_ctransid = 0;
5290
5291 if (!left_start_ctransid || !right_start_ctransid) {
5292 WARN(1, KERN_WARNING
5293 "btrfs: btrfs_compare_tree detected "
5294 "a change in one of the trees while "
5295 "iterating. This is probably a "
5296 "bug.\n");
5297 ret = -EIO;
5298 goto out;
5299 }
5300
5301 /*
5302 * the commit root may have changed, so start again
5303 * where we stopped
5304 */
5305 left_path->lowest_level = left_level;
5306 right_path->lowest_level = right_level;
5307 ret = btrfs_search_slot(NULL, left_root,
5308 &left_key, left_path, 0, 0);
5309 if (ret < 0)
5310 goto out;
5311 ret = btrfs_search_slot(NULL, right_root,
5312 &right_key, right_path, 0, 0);
5313 if (ret < 0)
5314 goto out;
5315 }
5316
5317 if (advance_left && !left_end_reached) {
5318 ret = tree_advance(left_root, left_path, &left_level,
5319 left_root_level,
5320 advance_left != ADVANCE_ONLY_NEXT,
5321 &left_key);
5322 if (ret < 0)
5323 left_end_reached = ADVANCE;
5324 advance_left = 0;
5325 }
5326 if (advance_right && !right_end_reached) {
5327 ret = tree_advance(right_root, right_path, &right_level,
5328 right_root_level,
5329 advance_right != ADVANCE_ONLY_NEXT,
5330 &right_key);
5331 if (ret < 0)
5332 right_end_reached = ADVANCE;
5333 advance_right = 0;
5334 }
5335
5336 if (left_end_reached && right_end_reached) {
5337 ret = 0;
5338 goto out;
5339 } else if (left_end_reached) {
5340 if (right_level == 0) {
5341 ret = changed_cb(left_root, right_root,
5342 left_path, right_path,
5343 &right_key,
5344 BTRFS_COMPARE_TREE_DELETED,
5345 ctx);
5346 if (ret < 0)
5347 goto out;
5348 }
5349 advance_right = ADVANCE;
5350 continue;
5351 } else if (right_end_reached) {
5352 if (left_level == 0) {
5353 ret = changed_cb(left_root, right_root,
5354 left_path, right_path,
5355 &left_key,
5356 BTRFS_COMPARE_TREE_NEW,
5357 ctx);
5358 if (ret < 0)
5359 goto out;
5360 }
5361 advance_left = ADVANCE;
5362 continue;
5363 }
5364
5365 if (left_level == 0 && right_level == 0) {
5366 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5367 if (cmp < 0) {
5368 ret = changed_cb(left_root, right_root,
5369 left_path, right_path,
5370 &left_key,
5371 BTRFS_COMPARE_TREE_NEW,
5372 ctx);
5373 if (ret < 0)
5374 goto out;
5375 advance_left = ADVANCE;
5376 } else if (cmp > 0) {
5377 ret = changed_cb(left_root, right_root,
5378 left_path, right_path,
5379 &right_key,
5380 BTRFS_COMPARE_TREE_DELETED,
5381 ctx);
5382 if (ret < 0)
5383 goto out;
5384 advance_right = ADVANCE;
5385 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005386 enum btrfs_compare_tree_result cmp;
5387
Chris Mason74dd17f2012-08-07 16:25:13 -04005388 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005389 ret = tree_compare_item(left_root, left_path,
5390 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005391 if (ret)
5392 cmp = BTRFS_COMPARE_TREE_CHANGED;
5393 else
5394 cmp = BTRFS_COMPARE_TREE_SAME;
5395 ret = changed_cb(left_root, right_root,
5396 left_path, right_path,
5397 &left_key, cmp, ctx);
5398 if (ret < 0)
5399 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005400 advance_left = ADVANCE;
5401 advance_right = ADVANCE;
5402 }
5403 } else if (left_level == right_level) {
5404 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5405 if (cmp < 0) {
5406 advance_left = ADVANCE;
5407 } else if (cmp > 0) {
5408 advance_right = ADVANCE;
5409 } else {
5410 left_blockptr = btrfs_node_blockptr(
5411 left_path->nodes[left_level],
5412 left_path->slots[left_level]);
5413 right_blockptr = btrfs_node_blockptr(
5414 right_path->nodes[right_level],
5415 right_path->slots[right_level]);
5416 if (left_blockptr == right_blockptr) {
5417 /*
5418 * As we're on a shared block, don't
5419 * allow to go deeper.
5420 */
5421 advance_left = ADVANCE_ONLY_NEXT;
5422 advance_right = ADVANCE_ONLY_NEXT;
5423 } else {
5424 advance_left = ADVANCE;
5425 advance_right = ADVANCE;
5426 }
5427 }
5428 } else if (left_level < right_level) {
5429 advance_right = ADVANCE;
5430 } else {
5431 advance_left = ADVANCE;
5432 }
5433 }
5434
5435out:
5436 btrfs_free_path(left_path);
5437 btrfs_free_path(right_path);
5438 kfree(tmp_buf);
5439
5440 if (trans) {
5441 if (!ret)
5442 ret = btrfs_end_transaction(trans, left_root);
5443 else
5444 btrfs_end_transaction(trans, left_root);
5445 }
5446
5447 return ret;
5448}
5449
Chris Mason3f157a22008-06-25 16:01:31 -04005450/*
5451 * this is similar to btrfs_next_leaf, but does not try to preserve
5452 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005453 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005454 *
5455 * 0 is returned if another key is found, < 0 if there are any errors
5456 * and 1 is returned if there are no higher keys in the tree
5457 *
5458 * path->keep_locks should be set to 1 on the search made before
5459 * calling this function.
5460 */
Chris Masone7a84562008-06-25 16:01:31 -04005461int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005462 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005463{
Chris Masone7a84562008-06-25 16:01:31 -04005464 int slot;
5465 struct extent_buffer *c;
5466
Chris Mason934d3752008-12-08 16:43:10 -05005467 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005468 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005469 if (!path->nodes[level])
5470 return 1;
5471
5472 slot = path->slots[level] + 1;
5473 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005474next:
Chris Masone7a84562008-06-25 16:01:31 -04005475 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005476 int ret;
5477 int orig_lowest;
5478 struct btrfs_key cur_key;
5479 if (level + 1 >= BTRFS_MAX_LEVEL ||
5480 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005481 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005482
5483 if (path->locks[level + 1]) {
5484 level++;
5485 continue;
5486 }
5487
5488 slot = btrfs_header_nritems(c) - 1;
5489 if (level == 0)
5490 btrfs_item_key_to_cpu(c, &cur_key, slot);
5491 else
5492 btrfs_node_key_to_cpu(c, &cur_key, slot);
5493
5494 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005495 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005496 path->lowest_level = level;
5497 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5498 0, 0);
5499 path->lowest_level = orig_lowest;
5500 if (ret < 0)
5501 return ret;
5502
5503 c = path->nodes[level];
5504 slot = path->slots[level];
5505 if (ret == 0)
5506 slot++;
5507 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005508 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005509
Chris Masone7a84562008-06-25 16:01:31 -04005510 if (level == 0)
5511 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005512 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005513 u64 gen = btrfs_node_ptr_generation(c, slot);
5514
Chris Mason3f157a22008-06-25 16:01:31 -04005515 if (gen < min_trans) {
5516 slot++;
5517 goto next;
5518 }
Chris Masone7a84562008-06-25 16:01:31 -04005519 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005520 }
Chris Masone7a84562008-06-25 16:01:31 -04005521 return 0;
5522 }
5523 return 1;
5524}
5525
Chris Mason7bb86312007-12-11 09:25:06 -05005526/*
Chris Mason925baed2008-06-25 16:01:30 -04005527 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005528 * returns 0 if it found something or 1 if there are no greater leaves.
5529 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005530 */
Chris Mason234b63a2007-03-13 10:46:10 -04005531int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005532{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005533 return btrfs_next_old_leaf(root, path, 0);
5534}
5535
5536int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5537 u64 time_seq)
5538{
Chris Masond97e63b2007-02-20 16:40:44 -05005539 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005540 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005541 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005542 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005543 struct btrfs_key key;
5544 u32 nritems;
5545 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005546 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005547 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005548
5549 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005550 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005551 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005552
Chris Mason8e73f272009-04-03 10:14:18 -04005553 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5554again:
5555 level = 1;
5556 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005557 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005558 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005559
Chris Masona2135012008-06-25 16:01:30 -04005560 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005561 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005562
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005563 if (time_seq)
5564 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5565 else
5566 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005567 path->keep_locks = 0;
5568
5569 if (ret < 0)
5570 return ret;
5571
Chris Masona2135012008-06-25 16:01:30 -04005572 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005573 /*
5574 * by releasing the path above we dropped all our locks. A balance
5575 * could have added more items next to the key that used to be
5576 * at the very end of the block. So, check again here and
5577 * advance the path if there are now more items available.
5578 */
Chris Masona2135012008-06-25 16:01:30 -04005579 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005580 if (ret == 0)
5581 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005582 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005583 goto done;
5584 }
Chris Masond97e63b2007-02-20 16:40:44 -05005585
Chris Masond3977122009-01-05 21:25:51 -05005586 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005587 if (!path->nodes[level]) {
5588 ret = 1;
5589 goto done;
5590 }
Chris Mason5f39d392007-10-15 16:14:19 -04005591
Chris Masond97e63b2007-02-20 16:40:44 -05005592 slot = path->slots[level] + 1;
5593 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005594 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005595 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005596 if (level == BTRFS_MAX_LEVEL) {
5597 ret = 1;
5598 goto done;
5599 }
Chris Masond97e63b2007-02-20 16:40:44 -05005600 continue;
5601 }
Chris Mason5f39d392007-10-15 16:14:19 -04005602
Chris Mason925baed2008-06-25 16:01:30 -04005603 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005604 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005605 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005606 }
Chris Mason5f39d392007-10-15 16:14:19 -04005607
Chris Mason8e73f272009-04-03 10:14:18 -04005608 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005609 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005610 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005611 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005612 if (ret == -EAGAIN)
5613 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005614
Chris Mason76a05b32009-05-14 13:24:30 -04005615 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005616 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005617 goto done;
5618 }
5619
Chris Mason5cd57b22008-06-25 16:01:30 -04005620 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005621 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005622 if (!ret && time_seq) {
5623 /*
5624 * If we don't get the lock, we may be racing
5625 * with push_leaf_left, holding that lock while
5626 * itself waiting for the leaf we've currently
5627 * locked. To solve this situation, we give up
5628 * on our lock and cycle.
5629 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005630 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005631 btrfs_release_path(path);
5632 cond_resched();
5633 goto again;
5634 }
Chris Mason8e73f272009-04-03 10:14:18 -04005635 if (!ret) {
5636 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005637 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005638 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005639 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005640 }
Chris Mason31533fb2011-07-26 16:01:59 -04005641 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005642 }
Chris Masond97e63b2007-02-20 16:40:44 -05005643 break;
5644 }
5645 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005646 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005647 level--;
5648 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005649 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005650 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005651
Chris Mason5f39d392007-10-15 16:14:19 -04005652 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005653 path->nodes[level] = next;
5654 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005655 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005656 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005657 if (!level)
5658 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005659
Chris Mason8e73f272009-04-03 10:14:18 -04005660 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005661 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005662 if (ret == -EAGAIN)
5663 goto again;
5664
Chris Mason76a05b32009-05-14 13:24:30 -04005665 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005666 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005667 goto done;
5668 }
5669
Chris Mason5cd57b22008-06-25 16:01:30 -04005670 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005671 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005672 if (!ret) {
5673 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005674 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005675 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005676 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005677 }
Chris Mason31533fb2011-07-26 16:01:59 -04005678 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005679 }
Chris Masond97e63b2007-02-20 16:40:44 -05005680 }
Chris Mason8e73f272009-04-03 10:14:18 -04005681 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005682done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005683 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005684 path->leave_spinning = old_spinning;
5685 if (!old_spinning)
5686 btrfs_set_path_blocking(path);
5687
5688 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005689}
Chris Mason0b86a832008-03-24 15:01:56 -04005690
Chris Mason3f157a22008-06-25 16:01:31 -04005691/*
5692 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5693 * searching until it gets past min_objectid or finds an item of 'type'
5694 *
5695 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5696 */
Chris Mason0b86a832008-03-24 15:01:56 -04005697int btrfs_previous_item(struct btrfs_root *root,
5698 struct btrfs_path *path, u64 min_objectid,
5699 int type)
5700{
5701 struct btrfs_key found_key;
5702 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005703 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005704 int ret;
5705
Chris Masond3977122009-01-05 21:25:51 -05005706 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005707 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005708 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005709 ret = btrfs_prev_leaf(root, path);
5710 if (ret != 0)
5711 return ret;
5712 } else {
5713 path->slots[0]--;
5714 }
5715 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005716 nritems = btrfs_header_nritems(leaf);
5717 if (nritems == 0)
5718 return 1;
5719 if (path->slots[0] == nritems)
5720 path->slots[0]--;
5721
Chris Mason0b86a832008-03-24 15:01:56 -04005722 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005723 if (found_key.objectid < min_objectid)
5724 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005725 if (found_key.type == type)
5726 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005727 if (found_key.objectid == min_objectid &&
5728 found_key.type < type)
5729 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005730 }
5731 return 1;
5732}