blob: 5fa521bec07b367c79d66f1df50a23cb263de12d [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);
Eric Sandeen48a3b632013-04-25 20:41:01 +000044static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
Chris Masond97e63b2007-02-20 16:40:44 -050045
Chris Mason2c90e5d2007-04-02 10:50:19 -040046struct btrfs_path *btrfs_alloc_path(void)
47{
Chris Masondf24a2b2007-04-04 09:36:31 -040048 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050049 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040050 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040051}
52
Chris Masonb4ce94d2009-02-04 09:25:08 -050053/*
54 * set all locked nodes in the path to blocking locks. This should
55 * be done before scheduling
56 */
57noinline void btrfs_set_path_blocking(struct btrfs_path *p)
58{
59 int i;
60 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040061 if (!p->nodes[i] || !p->locks[i])
62 continue;
63 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
64 if (p->locks[i] == BTRFS_READ_LOCK)
65 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
66 else if (p->locks[i] == BTRFS_WRITE_LOCK)
67 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050068 }
69}
70
71/*
72 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050073 *
74 * held is used to keep lockdep happy, when lockdep is enabled
75 * we set held to a blocking lock before we go around and
76 * retake all the spinlocks in the path. You can safely use NULL
77 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050078 */
Chris Mason4008c042009-02-12 14:09:45 -050079noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040080 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050081{
82 int i;
Chris Mason4008c042009-02-12 14:09:45 -050083
84#ifdef CONFIG_DEBUG_LOCK_ALLOC
85 /* lockdep really cares that we take all of these spinlocks
86 * in the right order. If any of the locks in the path are not
87 * currently blocking, it is going to complain. So, make really
88 * really sure by forcing the path to blocking before we clear
89 * the path blocking.
90 */
Chris Masonbd681512011-07-16 15:23:14 -040091 if (held) {
92 btrfs_set_lock_blocking_rw(held, held_rw);
93 if (held_rw == BTRFS_WRITE_LOCK)
94 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
95 else if (held_rw == BTRFS_READ_LOCK)
96 held_rw = BTRFS_READ_LOCK_BLOCKING;
97 }
Chris Mason4008c042009-02-12 14:09:45 -050098 btrfs_set_path_blocking(p);
99#endif
100
101 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400102 if (p->nodes[i] && p->locks[i]) {
103 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
104 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
105 p->locks[i] = BTRFS_WRITE_LOCK;
106 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
107 p->locks[i] = BTRFS_READ_LOCK;
108 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500109 }
Chris Mason4008c042009-02-12 14:09:45 -0500110
111#ifdef CONFIG_DEBUG_LOCK_ALLOC
112 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400113 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500114#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115}
116
Chris Masond352ac62008-09-29 15:18:18 -0400117/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400118void btrfs_free_path(struct btrfs_path *p)
119{
Jesper Juhlff175d52010-12-25 21:22:30 +0000120 if (!p)
121 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200122 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400123 kmem_cache_free(btrfs_path_cachep, p);
124}
125
Chris Masond352ac62008-09-29 15:18:18 -0400126/*
127 * path release drops references on the extent buffers in the path
128 * and it drops any locks held by this path
129 *
130 * It is safe to call this on paths that no locks or extent buffers held.
131 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200132noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500133{
134 int i;
Chris Masona2135012008-06-25 16:01:30 -0400135
Chris Mason234b63a2007-03-13 10:46:10 -0400136 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400137 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500138 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400139 continue;
140 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400141 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400142 p->locks[i] = 0;
143 }
Chris Mason5f39d392007-10-15 16:14:19 -0400144 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400145 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500146 }
147}
148
Chris Masond352ac62008-09-29 15:18:18 -0400149/*
150 * safely gets a reference on the root node of a tree. A lock
151 * is not taken, so a concurrent writer may put a different node
152 * at the root of the tree. See btrfs_lock_root_node for the
153 * looping required.
154 *
155 * The extent buffer returned by this has a reference taken, so
156 * it won't disappear. It may stop being the root of the tree
157 * at any time because there are no locks held.
158 */
Chris Mason925baed2008-06-25 16:01:30 -0400159struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
160{
161 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400162
Josef Bacik3083ee22012-03-09 16:01:49 -0500163 while (1) {
164 rcu_read_lock();
165 eb = rcu_dereference(root->node);
166
167 /*
168 * RCU really hurts here, we could free up the root node because
169 * it was cow'ed but we may not get the new root node yet so do
170 * the inc_not_zero dance and if it doesn't work then
171 * synchronize_rcu and try again.
172 */
173 if (atomic_inc_not_zero(&eb->refs)) {
174 rcu_read_unlock();
175 break;
176 }
177 rcu_read_unlock();
178 synchronize_rcu();
179 }
Chris Mason925baed2008-06-25 16:01:30 -0400180 return eb;
181}
182
Chris Masond352ac62008-09-29 15:18:18 -0400183/* loop around taking references on and locking the root node of the
184 * tree until you end up with a lock on the root. A locked buffer
185 * is returned, with a reference held.
186 */
Chris Mason925baed2008-06-25 16:01:30 -0400187struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
188{
189 struct extent_buffer *eb;
190
Chris Masond3977122009-01-05 21:25:51 -0500191 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400192 eb = btrfs_root_node(root);
193 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400194 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400195 break;
Chris Mason925baed2008-06-25 16:01:30 -0400196 btrfs_tree_unlock(eb);
197 free_extent_buffer(eb);
198 }
199 return eb;
200}
201
Chris Masonbd681512011-07-16 15:23:14 -0400202/* loop around taking references on and locking the root node of the
203 * tree until you end up with a lock on the root. A locked buffer
204 * is returned, with a reference held.
205 */
Eric Sandeen48a3b632013-04-25 20:41:01 +0000206static struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
Chris Masonbd681512011-07-16 15:23:14 -0400207{
208 struct extent_buffer *eb;
209
210 while (1) {
211 eb = btrfs_root_node(root);
212 btrfs_tree_read_lock(eb);
213 if (eb == root->node)
214 break;
215 btrfs_tree_read_unlock(eb);
216 free_extent_buffer(eb);
217 }
218 return eb;
219}
220
Chris Masond352ac62008-09-29 15:18:18 -0400221/* cowonly root (everything not a reference counted cow subvolume), just get
222 * put onto a simple dirty list. transaction.c walks this to make sure they
223 * get properly updated on disk.
224 */
Chris Mason0b86a832008-03-24 15:01:56 -0400225static void add_root_to_dirty_list(struct btrfs_root *root)
226{
Chris Masone5846fc2012-05-03 12:08:48 -0400227 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400228 if (root->track_dirty && list_empty(&root->dirty_list)) {
229 list_add(&root->dirty_list,
230 &root->fs_info->dirty_cowonly_roots);
231 }
Chris Masone5846fc2012-05-03 12:08:48 -0400232 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400233}
234
Chris Masond352ac62008-09-29 15:18:18 -0400235/*
236 * used by snapshot creation to make a copy of a root for a tree with
237 * a given objectid. The buffer with the new root node is returned in
238 * cow_ret, and this func returns zero on success or a negative error code.
239 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500240int btrfs_copy_root(struct btrfs_trans_handle *trans,
241 struct btrfs_root *root,
242 struct extent_buffer *buf,
243 struct extent_buffer **cow_ret, u64 new_root_objectid)
244{
245 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 int ret = 0;
247 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400248 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500249
250 WARN_ON(root->ref_cows && trans->transid !=
251 root->fs_info->running_transaction->transid);
252 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
253
254 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400255 if (level == 0)
256 btrfs_item_key(buf, &disk_key, 0);
257 else
258 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400259
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
261 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200262 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400263 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 return PTR_ERR(cow);
265
266 copy_extent_buffer(cow, buf, 0, 0, cow->len);
267 btrfs_set_header_bytenr(cow, cow->start);
268 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
270 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
271 BTRFS_HEADER_FLAG_RELOC);
272 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
273 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
274 else
275 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500276
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +0200277 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(cow),
Yan Zheng2b820322008-11-17 21:11:30 -0500278 BTRFS_FSID_SIZE);
279
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400281 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200282 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400283 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200284 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500285
Chris Masonbe20aa92007-12-17 20:14:01 -0500286 if (ret)
287 return ret;
288
289 btrfs_mark_buffer_dirty(cow);
290 *cow_ret = cow;
291 return 0;
292}
293
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200294enum mod_log_op {
295 MOD_LOG_KEY_REPLACE,
296 MOD_LOG_KEY_ADD,
297 MOD_LOG_KEY_REMOVE,
298 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
299 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
300 MOD_LOG_MOVE_KEYS,
301 MOD_LOG_ROOT_REPLACE,
302};
303
304struct tree_mod_move {
305 int dst_slot;
306 int nr_items;
307};
308
309struct tree_mod_root {
310 u64 logical;
311 u8 level;
312};
313
314struct tree_mod_elem {
315 struct rb_node node;
316 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200317 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200318 enum mod_log_op op;
319
320 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
321 int slot;
322
323 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
324 u64 generation;
325
326 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
327 struct btrfs_disk_key key;
328 u64 blockptr;
329
330 /* this is used for op == MOD_LOG_MOVE_KEYS */
331 struct tree_mod_move move;
332
333 /* this is used for op == MOD_LOG_ROOT_REPLACE */
334 struct tree_mod_root old_root;
335};
336
Jan Schmidt097b8a72012-06-21 11:08:04 +0200337static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200338{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200339 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200340}
341
Jan Schmidt097b8a72012-06-21 11:08:04 +0200342static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200343{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200344 read_unlock(&fs_info->tree_mod_log_lock);
345}
346
347static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
348{
349 write_lock(&fs_info->tree_mod_log_lock);
350}
351
352static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
353{
354 write_unlock(&fs_info->tree_mod_log_lock);
355}
356
357/*
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000358 * Increment the upper half of tree_mod_seq, set lower half zero.
359 *
360 * Must be called with fs_info->tree_mod_seq_lock held.
361 */
362static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
363{
364 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
365 seq &= 0xffffffff00000000ull;
366 seq += 1ull << 32;
367 atomic64_set(&fs_info->tree_mod_seq, seq);
368 return seq;
369}
370
371/*
372 * Increment the lower half of tree_mod_seq.
373 *
374 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
375 * are generated should not technically require a spin lock here. (Rationale:
376 * incrementing the minor while incrementing the major seq number is between its
377 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
378 * just returns a unique sequence number as usual.) We have decided to leave
379 * that requirement in here and rethink it once we notice it really imposes a
380 * problem on some workload.
381 */
382static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
383{
384 return atomic64_inc_return(&fs_info->tree_mod_seq);
385}
386
387/*
388 * return the last minor in the previous major tree_mod_seq number
389 */
390u64 btrfs_tree_mod_seq_prev(u64 seq)
391{
392 return (seq & 0xffffffff00000000ull) - 1ull;
393}
394
395/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200396 * This adds a new blocker to the tree mod log's blocker list if the @elem
397 * passed does not already have a sequence number set. So when a caller expects
398 * to record tree modifications, it should ensure to set elem->seq to zero
399 * before calling btrfs_get_tree_mod_seq.
400 * Returns a fresh, unused tree log modification sequence number, even if no new
401 * blocker was added.
402 */
403u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
404 struct seq_list *elem)
405{
406 u64 seq;
407
408 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200409 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200410 if (!elem->seq) {
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000411 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200412 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
413 }
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000414 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200415 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200416 tree_mod_log_write_unlock(fs_info);
417
418 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200419}
420
421void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
422 struct seq_list *elem)
423{
424 struct rb_root *tm_root;
425 struct rb_node *node;
426 struct rb_node *next;
427 struct seq_list *cur_elem;
428 struct tree_mod_elem *tm;
429 u64 min_seq = (u64)-1;
430 u64 seq_putting = elem->seq;
431
432 if (!seq_putting)
433 return;
434
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200435 spin_lock(&fs_info->tree_mod_seq_lock);
436 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200437 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200438
439 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200440 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200441 if (seq_putting > cur_elem->seq) {
442 /*
443 * blocker with lower sequence number exists, we
444 * cannot remove anything from the log
445 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200446 spin_unlock(&fs_info->tree_mod_seq_lock);
447 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200448 }
449 min_seq = cur_elem->seq;
450 }
451 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200452 spin_unlock(&fs_info->tree_mod_seq_lock);
453
454 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200455 * anything that's lower than the lowest existing (read: blocked)
456 * sequence number can be removed from the tree.
457 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200458 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200459 tm_root = &fs_info->tree_mod_log;
460 for (node = rb_first(tm_root); node; node = next) {
461 next = rb_next(node);
462 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200463 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200464 continue;
465 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200466 kfree(tm);
467 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200468 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200469}
470
471/*
472 * key order of the log:
473 * index -> sequence
474 *
475 * the index is the shifted logical of the *new* root node for root replace
476 * operations, or the shifted logical of the affected block for all other
477 * operations.
478 */
479static noinline int
480__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
481{
482 struct rb_root *tm_root;
483 struct rb_node **new;
484 struct rb_node *parent = NULL;
485 struct tree_mod_elem *cur;
Josef Bacikc8cc6342013-07-01 16:18:19 -0400486 int ret = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200487
Josef Bacikc8cc6342013-07-01 16:18:19 -0400488 BUG_ON(!tm);
489
490 tree_mod_log_write_lock(fs_info);
491 if (list_empty(&fs_info->tree_mod_seq_list)) {
492 tree_mod_log_write_unlock(fs_info);
493 /*
494 * Ok we no longer care about logging modifications, free up tm
495 * and return 0. Any callers shouldn't be using tm after
496 * calling tree_mod_log_insert, but if they do we can just
497 * change this to return a special error code to let the callers
498 * do their own thing.
499 */
500 kfree(tm);
501 return 0;
502 }
503
504 spin_lock(&fs_info->tree_mod_seq_lock);
505 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
506 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200507
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200508 tm_root = &fs_info->tree_mod_log;
509 new = &tm_root->rb_node;
510 while (*new) {
511 cur = container_of(*new, struct tree_mod_elem, node);
512 parent = *new;
513 if (cur->index < tm->index)
514 new = &((*new)->rb_left);
515 else if (cur->index > tm->index)
516 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200517 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200518 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200519 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200520 new = &((*new)->rb_right);
521 else {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400522 ret = -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200523 kfree(tm);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400524 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200525 }
526 }
527
528 rb_link_node(&tm->node, parent, new);
529 rb_insert_color(&tm->node, tm_root);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400530out:
531 tree_mod_log_write_unlock(fs_info);
532 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200533}
534
Jan Schmidt097b8a72012-06-21 11:08:04 +0200535/*
536 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
537 * returns zero with the tree_mod_log_lock acquired. The caller must hold
538 * this until all tree mod log insertions are recorded in the rb tree and then
539 * call tree_mod_log_write_unlock() to release.
540 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200541static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
542 struct extent_buffer *eb) {
543 smp_mb();
544 if (list_empty(&(fs_info)->tree_mod_seq_list))
545 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200546 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200547 return 1;
548 return 0;
549}
550
Jan Schmidt097b8a72012-06-21 11:08:04 +0200551static inline int
552__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
553 struct extent_buffer *eb, int slot,
554 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200555{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200556 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200557
Josef Bacikc8cc6342013-07-01 16:18:19 -0400558 tm = kzalloc(sizeof(*tm), flags);
559 if (!tm)
560 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200561
562 tm->index = eb->start >> PAGE_CACHE_SHIFT;
563 if (op != MOD_LOG_KEY_ADD) {
564 btrfs_node_key(eb, &tm->key, slot);
565 tm->blockptr = btrfs_node_blockptr(eb, slot);
566 }
567 tm->op = op;
568 tm->slot = slot;
569 tm->generation = btrfs_node_ptr_generation(eb, slot);
570
Jan Schmidt097b8a72012-06-21 11:08:04 +0200571 return __tree_mod_log_insert(fs_info, tm);
572}
573
574static noinline int
Josef Bacikc8cc6342013-07-01 16:18:19 -0400575tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
576 struct extent_buffer *eb, int slot,
577 enum mod_log_op op, gfp_t flags)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200578{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200579 if (tree_mod_dont_log(fs_info, eb))
580 return 0;
581
Josef Bacikc8cc6342013-07-01 16:18:19 -0400582 return __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200583}
584
585static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200586tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
587 struct extent_buffer *eb, int dst_slot, int src_slot,
588 int nr_items, gfp_t flags)
589{
590 struct tree_mod_elem *tm;
591 int ret;
592 int i;
593
Jan Schmidtf3956942012-05-31 15:02:32 +0200594 if (tree_mod_dont_log(fs_info, eb))
595 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200596
Jan Schmidt01763a22012-10-23 15:02:12 +0200597 /*
598 * When we override something during the move, we log these removals.
599 * This can only happen when we move towards the beginning of the
600 * buffer, i.e. dst_slot < src_slot.
601 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200602 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400603 ret = __tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
604 MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200605 BUG_ON(ret < 0);
606 }
607
Josef Bacikc8cc6342013-07-01 16:18:19 -0400608 tm = kzalloc(sizeof(*tm), flags);
609 if (!tm)
610 return -ENOMEM;
Jan Schmidtf3956942012-05-31 15:02:32 +0200611
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200612 tm->index = eb->start >> PAGE_CACHE_SHIFT;
613 tm->slot = src_slot;
614 tm->move.dst_slot = dst_slot;
615 tm->move.nr_items = nr_items;
616 tm->op = MOD_LOG_MOVE_KEYS;
617
Josef Bacikc8cc6342013-07-01 16:18:19 -0400618 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200619}
620
Jan Schmidt097b8a72012-06-21 11:08:04 +0200621static inline void
622__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
623{
624 int i;
625 u32 nritems;
626 int ret;
627
Chris Masonb12a3b12012-08-07 15:34:49 -0400628 if (btrfs_header_level(eb) == 0)
629 return;
630
Jan Schmidt097b8a72012-06-21 11:08:04 +0200631 nritems = btrfs_header_nritems(eb);
632 for (i = nritems - 1; i >= 0; i--) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400633 ret = __tree_mod_log_insert_key(fs_info, eb, i,
634 MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200635 BUG_ON(ret < 0);
636 }
637}
638
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200639static noinline int
640tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
641 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000642 struct extent_buffer *new_root, gfp_t flags,
643 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200644{
645 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200646
Jan Schmidt097b8a72012-06-21 11:08:04 +0200647 if (tree_mod_dont_log(fs_info, NULL))
648 return 0;
649
Jan Schmidt90f8d622013-04-13 13:19:53 +0000650 if (log_removal)
651 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000652
Josef Bacikc8cc6342013-07-01 16:18:19 -0400653 tm = kzalloc(sizeof(*tm), flags);
654 if (!tm)
655 return -ENOMEM;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200656
657 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
658 tm->old_root.logical = old_root->start;
659 tm->old_root.level = btrfs_header_level(old_root);
660 tm->generation = btrfs_header_generation(old_root);
661 tm->op = MOD_LOG_ROOT_REPLACE;
662
Josef Bacikc8cc6342013-07-01 16:18:19 -0400663 return __tree_mod_log_insert(fs_info, tm);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200664}
665
666static struct tree_mod_elem *
667__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
668 int smallest)
669{
670 struct rb_root *tm_root;
671 struct rb_node *node;
672 struct tree_mod_elem *cur = NULL;
673 struct tree_mod_elem *found = NULL;
674 u64 index = start >> PAGE_CACHE_SHIFT;
675
Jan Schmidt097b8a72012-06-21 11:08:04 +0200676 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200677 tm_root = &fs_info->tree_mod_log;
678 node = tm_root->rb_node;
679 while (node) {
680 cur = container_of(node, struct tree_mod_elem, node);
681 if (cur->index < index) {
682 node = node->rb_left;
683 } else if (cur->index > index) {
684 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200685 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200686 node = node->rb_left;
687 } else if (!smallest) {
688 /* we want the node with the highest seq */
689 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200690 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200691 found = cur;
692 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200693 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 /* we want the node with the smallest seq */
695 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200696 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697 found = cur;
698 node = node->rb_right;
699 } else {
700 found = cur;
701 break;
702 }
703 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200704 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200705
706 return found;
707}
708
709/*
710 * this returns the element from the log with the smallest time sequence
711 * value that's in the log (the oldest log item). any element with a time
712 * sequence lower than min_seq will be ignored.
713 */
714static struct tree_mod_elem *
715tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
716 u64 min_seq)
717{
718 return __tree_mod_log_search(fs_info, start, min_seq, 1);
719}
720
721/*
722 * this returns the element from the log with the largest time sequence
723 * value that's in the log (the most recent log item). any element with
724 * a time sequence lower than min_seq will be ignored.
725 */
726static struct tree_mod_elem *
727tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
728{
729 return __tree_mod_log_search(fs_info, start, min_seq, 0);
730}
731
Jan Schmidt097b8a72012-06-21 11:08:04 +0200732static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200733tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
734 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000735 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200736{
737 int ret;
738 int i;
739
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200740 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200741 return;
742
Josef Bacikc8cc6342013-07-01 16:18:19 -0400743 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200744 return;
745
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200746 for (i = 0; i < nr_items; i++) {
Josef Bacikc8cc6342013-07-01 16:18:19 -0400747 ret = __tree_mod_log_insert_key(fs_info, src,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000748 i + src_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400749 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Jan Schmidt90f8d622013-04-13 13:19:53 +0000750 BUG_ON(ret < 0);
Josef Bacikc8cc6342013-07-01 16:18:19 -0400751 ret = __tree_mod_log_insert_key(fs_info, dst,
Jan Schmidt097b8a72012-06-21 11:08:04 +0200752 i + dst_offset,
Josef Bacikc8cc6342013-07-01 16:18:19 -0400753 MOD_LOG_KEY_ADD,
754 GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200755 BUG_ON(ret < 0);
756 }
757}
758
759static inline void
760tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
761 int dst_offset, int src_offset, int nr_items)
762{
763 int ret;
764 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
765 nr_items, GFP_NOFS);
766 BUG_ON(ret < 0);
767}
768
Jan Schmidt097b8a72012-06-21 11:08:04 +0200769static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200770tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000771 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200772{
773 int ret;
774
Josef Bacikc8cc6342013-07-01 16:18:19 -0400775 ret = __tree_mod_log_insert_key(fs_info, eb, slot,
776 MOD_LOG_KEY_REPLACE,
777 atomic ? GFP_ATOMIC : GFP_NOFS);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200778 BUG_ON(ret < 0);
779}
780
Jan Schmidt097b8a72012-06-21 11:08:04 +0200781static noinline void
782tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200783{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200784 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200785 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200786 __tree_mod_log_free_eb(fs_info, eb);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200787}
788
Jan Schmidt097b8a72012-06-21 11:08:04 +0200789static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200790tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000791 struct extent_buffer *new_root_node,
792 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200793{
794 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200795 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000796 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200797 BUG_ON(ret < 0);
798}
799
Chris Masond352ac62008-09-29 15:18:18 -0400800/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400801 * check if the tree block can be shared by multiple trees
802 */
803int btrfs_block_can_be_shared(struct btrfs_root *root,
804 struct extent_buffer *buf)
805{
806 /*
807 * Tree blocks not in refernece counted trees and tree roots
808 * are never shared. If a block was allocated after the last
809 * snapshot and the block was not allocated by tree relocation,
810 * we know the block is not shared.
811 */
812 if (root->ref_cows &&
813 buf != root->node && buf != root->commit_root &&
814 (btrfs_header_generation(buf) <=
815 btrfs_root_last_snapshot(&root->root_item) ||
816 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
817 return 1;
818#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
819 if (root->ref_cows &&
820 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
821 return 1;
822#endif
823 return 0;
824}
825
826static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400829 struct extent_buffer *cow,
830 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400831{
832 u64 refs;
833 u64 owner;
834 u64 flags;
835 u64 new_flags = 0;
836 int ret;
837
838 /*
839 * Backrefs update rules:
840 *
841 * Always use full backrefs for extent pointers in tree block
842 * allocated by tree relocation.
843 *
844 * If a shared tree block is no longer referenced by its owner
845 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
846 * use full backrefs for extent pointers in tree block.
847 *
848 * If a tree block is been relocating
849 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
850 * use full backrefs for extent pointers in tree block.
851 * The reason for this is some operations (such as drop tree)
852 * are only allowed for blocks use full backrefs.
853 */
854
855 if (btrfs_block_can_be_shared(root, buf)) {
856 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500857 btrfs_header_level(buf), 1,
858 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700859 if (ret)
860 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700861 if (refs == 0) {
862 ret = -EROFS;
863 btrfs_std_error(root->fs_info, ret);
864 return ret;
865 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400866 } else {
867 refs = 1;
868 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
869 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
870 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
871 else
872 flags = 0;
873 }
874
875 owner = btrfs_header_owner(buf);
876 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
877 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
878
879 if (refs > 1) {
880 if ((owner == root->root_key.objectid ||
881 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
882 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200883 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100884 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400885
886 if (root->root_key.objectid ==
887 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200888 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100889 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200890 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100891 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400892 }
893 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
894 } else {
895
896 if (root->root_key.objectid ==
897 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200898 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200900 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100901 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400902 }
903 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400904 int level = btrfs_header_level(buf);
905
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400906 ret = btrfs_set_disk_extent_flags(trans, root,
907 buf->start,
908 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400909 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700910 if (ret)
911 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400912 }
913 } else {
914 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
915 if (root->root_key.objectid ==
916 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200917 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400918 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200919 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100920 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200921 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100922 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 }
924 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400925 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400926 }
927 return 0;
928}
929
930/*
Chris Masond3977122009-01-05 21:25:51 -0500931 * does the dirty work in cow of a single block. The parent block (if
932 * supplied) is updated to point to the new cow copy. The new buffer is marked
933 * dirty and returned locked. If you modify the block it needs to be marked
934 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400935 *
936 * search_start -- an allocation hint for the new block
937 *
Chris Masond3977122009-01-05 21:25:51 -0500938 * empty_size -- a hint that you plan on doing more cow. This is the size in
939 * bytes the allocator should try to find free next to the block it returns.
940 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400941 */
Chris Masond3977122009-01-05 21:25:51 -0500942static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400943 struct btrfs_root *root,
944 struct extent_buffer *buf,
945 struct extent_buffer *parent, int parent_slot,
946 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400947 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400948{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400950 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700951 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400952 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400953 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400954 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400955
Chris Mason925baed2008-06-25 16:01:30 -0400956 if (*cow_ret == buf)
957 unlock_orig = 1;
958
Chris Masonb9447ef2009-03-09 11:45:38 -0400959 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400960
Chris Mason7bb86312007-12-11 09:25:06 -0500961 WARN_ON(root->ref_cows && trans->transid !=
962 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400963 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400964
Chris Mason7bb86312007-12-11 09:25:06 -0500965 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400966
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400967 if (level == 0)
968 btrfs_item_key(buf, &disk_key, 0);
969 else
970 btrfs_node_key(buf, &disk_key, 0);
971
972 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
973 if (parent)
974 parent_start = parent->start;
975 else
976 parent_start = 0;
977 } else
978 parent_start = 0;
979
980 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
981 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200982 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400983 if (IS_ERR(cow))
984 return PTR_ERR(cow);
985
Chris Masonb4ce94d2009-02-04 09:25:08 -0500986 /* cow is set to blocking by btrfs_init_new_buffer */
987
Chris Mason5f39d392007-10-15 16:14:19 -0400988 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400989 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400990 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
992 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
993 BTRFS_HEADER_FLAG_RELOC);
994 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
995 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
996 else
997 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400998
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +0200999 write_extent_buffer(cow, root->fs_info->fsid, btrfs_header_fsid(cow),
Yan Zheng2b820322008-11-17 21:11:30 -05001000 BTRFS_FSID_SIZE);
1001
Mark Fashehbe1a5562011-08-08 13:20:18 -07001002 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001003 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001004 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001005 return ret;
1006 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001007
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001008 if (root->ref_cows)
1009 btrfs_reloc_cow_block(trans, root, buf, cow);
1010
Chris Mason6702ed42007-08-07 16:15:09 -04001011 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001012 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001013 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1014 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1015 parent_start = buf->start;
1016 else
1017 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001018
Chris Mason5f39d392007-10-15 16:14:19 -04001019 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001020 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001021 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001022
Yan, Zhengf0486c62010-05-16 10:46:25 -04001023 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001024 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001025 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001026 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001027 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001028 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1029 parent_start = parent->start;
1030 else
1031 parent_start = 0;
1032
1033 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001034 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04001035 MOD_LOG_KEY_REPLACE, GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04001036 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001037 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001038 btrfs_set_node_ptr_generation(parent, parent_slot,
1039 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001040 btrfs_mark_buffer_dirty(parent);
Josef Bacik7fb7d76f2013-07-01 16:10:16 -04001041 if (last_ref)
1042 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001043 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001044 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001045 }
Chris Mason925baed2008-06-25 16:01:30 -04001046 if (unlock_orig)
1047 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001048 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001049 btrfs_mark_buffer_dirty(cow);
1050 *cow_ret = cow;
1051 return 0;
1052}
1053
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001054/*
1055 * returns the logical address of the oldest predecessor of the given root.
1056 * entries older than time_seq are ignored.
1057 */
1058static struct tree_mod_elem *
1059__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001060 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001061{
1062 struct tree_mod_elem *tm;
1063 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001064 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001065 int looped = 0;
1066
1067 if (!time_seq)
Stefan Behrens35a36212013-08-14 18:12:25 +02001068 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001069
1070 /*
1071 * the very last operation that's logged for a root is the replacement
1072 * operation (if it is replaced at all). this has the index of the *new*
1073 * root, making it the very first operation that's logged for this root.
1074 */
1075 while (1) {
1076 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1077 time_seq);
1078 if (!looped && !tm)
Stefan Behrens35a36212013-08-14 18:12:25 +02001079 return NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001080 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001081 * if there are no tree operation for the oldest root, we simply
1082 * return it. this should only happen if that (old) root is at
1083 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001084 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001085 if (!tm)
1086 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001087
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001088 /*
1089 * if there's an operation that's not a root replacement, we
1090 * found the oldest version of our root. normally, we'll find a
1091 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1092 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001093 if (tm->op != MOD_LOG_ROOT_REPLACE)
1094 break;
1095
1096 found = tm;
1097 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001098 looped = 1;
1099 }
1100
Jan Schmidta95236d2012-06-05 16:41:24 +02001101 /* if there's no old root to return, return what we found instead */
1102 if (!found)
1103 found = tm;
1104
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001105 return found;
1106}
1107
1108/*
1109 * tm is a pointer to the first operation to rewind within eb. then, all
1110 * previous operations will be rewinded (until we reach something older than
1111 * time_seq).
1112 */
1113static void
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001114__tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1115 u64 time_seq, struct tree_mod_elem *first_tm)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001116{
1117 u32 n;
1118 struct rb_node *next;
1119 struct tree_mod_elem *tm = first_tm;
1120 unsigned long o_dst;
1121 unsigned long o_src;
1122 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1123
1124 n = btrfs_header_nritems(eb);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001125 tree_mod_log_read_lock(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001126 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001127 /*
1128 * all the operations are recorded with the operator used for
1129 * the modification. as we're going backwards, we do the
1130 * opposite of each operation here.
1131 */
1132 switch (tm->op) {
1133 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1134 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001135 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001136 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001137 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001138 btrfs_set_node_key(eb, &tm->key, tm->slot);
1139 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1140 btrfs_set_node_ptr_generation(eb, tm->slot,
1141 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001142 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001143 break;
1144 case MOD_LOG_KEY_REPLACE:
1145 BUG_ON(tm->slot >= n);
1146 btrfs_set_node_key(eb, &tm->key, tm->slot);
1147 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1148 btrfs_set_node_ptr_generation(eb, tm->slot,
1149 tm->generation);
1150 break;
1151 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001152 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001153 n--;
1154 break;
1155 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001156 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1157 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1158 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001159 tm->move.nr_items * p_size);
1160 break;
1161 case MOD_LOG_ROOT_REPLACE:
1162 /*
1163 * this operation is special. for roots, this must be
1164 * handled explicitly before rewinding.
1165 * for non-roots, this operation may exist if the node
1166 * was a root: root A -> child B; then A gets empty and
1167 * B is promoted to the new root. in the mod log, we'll
1168 * have a root-replace operation for B, a tree block
1169 * that is no root. we simply ignore that operation.
1170 */
1171 break;
1172 }
1173 next = rb_next(&tm->node);
1174 if (!next)
1175 break;
1176 tm = container_of(next, struct tree_mod_elem, node);
1177 if (tm->index != first_tm->index)
1178 break;
1179 }
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001180 tree_mod_log_read_unlock(fs_info);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001181 btrfs_set_header_nritems(eb, n);
1182}
1183
Jan Schmidt47fb0912013-04-13 13:19:55 +00001184/*
1185 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1186 * is returned. If rewind operations happen, a fresh buffer is returned. The
1187 * returned buffer is always read-locked. If the returned buffer is not the
1188 * input buffer, the lock on the input buffer is released and the input buffer
1189 * is freed (its refcount is decremented).
1190 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001191static struct extent_buffer *
Josef Bacik9ec72672013-08-07 16:57:23 -04001192tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
1193 struct extent_buffer *eb, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001194{
1195 struct extent_buffer *eb_rewin;
1196 struct tree_mod_elem *tm;
1197
1198 if (!time_seq)
1199 return eb;
1200
1201 if (btrfs_header_level(eb) == 0)
1202 return eb;
1203
1204 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1205 if (!tm)
1206 return eb;
1207
Josef Bacik9ec72672013-08-07 16:57:23 -04001208 btrfs_set_path_blocking(path);
1209 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
1210
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001211 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1212 BUG_ON(tm->slot != 0);
1213 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1214 fs_info->tree_root->nodesize);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001215 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001216 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001217 free_extent_buffer(eb);
1218 return NULL;
1219 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001220 btrfs_set_header_bytenr(eb_rewin, eb->start);
1221 btrfs_set_header_backref_rev(eb_rewin,
1222 btrfs_header_backref_rev(eb));
1223 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001224 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001225 } else {
1226 eb_rewin = btrfs_clone_extent_buffer(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001227 if (!eb_rewin) {
Josef Bacik9ec72672013-08-07 16:57:23 -04001228 btrfs_tree_read_unlock_blocking(eb);
Josef Bacikdb7f3432013-08-07 14:54:37 -04001229 free_extent_buffer(eb);
1230 return NULL;
1231 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001232 }
1233
Josef Bacik9ec72672013-08-07 16:57:23 -04001234 btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
1235 btrfs_tree_read_unlock_blocking(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001236 free_extent_buffer(eb);
1237
Jan Schmidt47fb0912013-04-13 13:19:55 +00001238 extent_buffer_get(eb_rewin);
1239 btrfs_tree_read_lock(eb_rewin);
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001240 __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001241 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001242 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001243
1244 return eb_rewin;
1245}
1246
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001247/*
1248 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1249 * value. If there are no changes, the current root->root_node is returned. If
1250 * anything changed in between, there's a fresh buffer allocated on which the
1251 * rewind operations are done. In any case, the returned buffer is read locked.
1252 * Returns NULL on error (with no locks held).
1253 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001254static inline struct extent_buffer *
1255get_old_root(struct btrfs_root *root, u64 time_seq)
1256{
1257 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001258 struct extent_buffer *eb = NULL;
1259 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001260 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001261 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001262 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001263 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001264 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001265
Jan Schmidt30b04632013-04-13 13:19:54 +00001266 eb_root = btrfs_read_lock_root_node(root);
1267 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001268 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001269 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001270
Jan Schmidta95236d2012-06-05 16:41:24 +02001271 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1272 old_root = &tm->old_root;
1273 old_generation = tm->generation;
1274 logical = old_root->logical;
1275 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001276 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001277 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001278
Jan Schmidta95236d2012-06-05 16:41:24 +02001279 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001280 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001281 btrfs_tree_read_unlock(eb_root);
1282 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001283 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001284 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001285 if (!old || !extent_buffer_uptodate(old)) {
1286 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001287 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1288 logical);
1289 WARN_ON(1);
1290 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001291 eb = btrfs_clone_extent_buffer(old);
1292 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001293 }
1294 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001295 btrfs_tree_read_unlock(eb_root);
1296 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001297 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001298 } else {
Josef Bacik9ec72672013-08-07 16:57:23 -04001299 btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
Jan Schmidt30b04632013-04-13 13:19:54 +00001300 eb = btrfs_clone_extent_buffer(eb_root);
Josef Bacik9ec72672013-08-07 16:57:23 -04001301 btrfs_tree_read_unlock_blocking(eb_root);
Jan Schmidt30b04632013-04-13 13:19:54 +00001302 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001303 }
1304
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001305 if (!eb)
1306 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001307 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001308 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001309 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001310 btrfs_set_header_bytenr(eb, eb->start);
1311 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001312 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001313 btrfs_set_header_level(eb, old_root->level);
1314 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001315 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001316 if (tm)
Josef Bacikf1ca7e982013-06-29 23:15:19 -04001317 __tree_mod_log_rewind(root->fs_info, eb, time_seq, tm);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001318 else
1319 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001320 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001321
1322 return eb;
1323}
1324
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001325int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1326{
1327 struct tree_mod_elem *tm;
1328 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001329 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001330
Jan Schmidt30b04632013-04-13 13:19:54 +00001331 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001332 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1333 level = tm->old_root.level;
1334 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001335 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001336 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001337 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001338
1339 return level;
1340}
1341
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001342static inline int should_cow_block(struct btrfs_trans_handle *trans,
1343 struct btrfs_root *root,
1344 struct extent_buffer *buf)
1345{
Liu Bof1ebcc72011-11-14 20:48:06 -05001346 /* ensure we can see the force_cow */
1347 smp_rmb();
1348
1349 /*
1350 * We do not need to cow a block if
1351 * 1) this block is not created or changed in this transaction;
1352 * 2) this block does not belong to TREE_RELOC tree;
1353 * 3) the root is not forced COW.
1354 *
1355 * What is forced COW:
1356 * when we create snapshot during commiting the transaction,
1357 * after we've finished coping src root, we must COW the shared
1358 * block to ensure the metadata consistency.
1359 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001360 if (btrfs_header_generation(buf) == trans->transid &&
1361 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1362 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001363 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1364 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001365 return 0;
1366 return 1;
1367}
1368
Chris Masond352ac62008-09-29 15:18:18 -04001369/*
1370 * cows a single block, see __btrfs_cow_block for the real work.
1371 * This version of it has extra checks so that a block isn't cow'd more than
1372 * once per transaction, as long as it hasn't been written yet
1373 */
Chris Masond3977122009-01-05 21:25:51 -05001374noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001375 struct btrfs_root *root, struct extent_buffer *buf,
1376 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001377 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001378{
Chris Mason6702ed42007-08-07 16:15:09 -04001379 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001380 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001381
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001382 if (trans->transaction != root->fs_info->running_transaction)
1383 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001384 trans->transid,
Chris Masonccd467d2007-06-28 15:57:36 -04001385 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001386
1387 if (trans->transid != root->fs_info->generation)
1388 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02001389 trans->transid, root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001390
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001391 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001392 *cow_ret = buf;
1393 return 0;
1394 }
Chris Masonc4876852009-02-04 09:24:25 -05001395
Chris Mason0b86a832008-03-24 15:01:56 -04001396 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001397
1398 if (parent)
1399 btrfs_set_lock_blocking(parent);
1400 btrfs_set_lock_blocking(buf);
1401
Chris Masonf510cfe2007-10-15 16:14:48 -04001402 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001403 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001404
1405 trace_btrfs_cow_block(root, buf, *cow_ret);
1406
Chris Masonf510cfe2007-10-15 16:14:48 -04001407 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001408}
1409
Chris Masond352ac62008-09-29 15:18:18 -04001410/*
1411 * helper function for defrag to decide if two blocks pointed to by a
1412 * node are actually close by
1413 */
Chris Mason6b800532007-10-15 16:17:34 -04001414static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001415{
Chris Mason6b800532007-10-15 16:17:34 -04001416 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001417 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001418 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001419 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001420 return 0;
1421}
1422
Chris Mason081e9572007-11-06 10:26:24 -05001423/*
1424 * compare two keys in a memcmp fashion
1425 */
1426static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1427{
1428 struct btrfs_key k1;
1429
1430 btrfs_disk_key_to_cpu(&k1, disk);
1431
Diego Calleja20736ab2009-07-24 11:06:52 -04001432 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001433}
1434
Josef Bacikf3465ca2008-11-12 14:19:50 -05001435/*
1436 * same as comp_keys only with two btrfs_key's
1437 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001438int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001439{
1440 if (k1->objectid > k2->objectid)
1441 return 1;
1442 if (k1->objectid < k2->objectid)
1443 return -1;
1444 if (k1->type > k2->type)
1445 return 1;
1446 if (k1->type < k2->type)
1447 return -1;
1448 if (k1->offset > k2->offset)
1449 return 1;
1450 if (k1->offset < k2->offset)
1451 return -1;
1452 return 0;
1453}
Chris Mason081e9572007-11-06 10:26:24 -05001454
Chris Masond352ac62008-09-29 15:18:18 -04001455/*
1456 * this is used by the defrag code to go through all the
1457 * leaves pointed to by a node and reallocate them so that
1458 * disk order is close to key order
1459 */
Chris Mason6702ed42007-08-07 16:15:09 -04001460int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001461 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001462 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001463 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001464{
Chris Mason6b800532007-10-15 16:17:34 -04001465 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001466 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001467 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001468 u64 search_start = *last_ret;
1469 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001470 u64 other;
1471 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001472 int end_slot;
1473 int i;
1474 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001475 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001476 int uptodate;
1477 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001478 int progress_passed = 0;
1479 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001480
Chris Mason5708b952007-10-25 15:43:18 -04001481 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001482
Julia Lawall6c1500f2012-11-03 20:30:18 +00001483 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1484 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001485
Chris Mason6b800532007-10-15 16:17:34 -04001486 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001487 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001488 end_slot = parent_nritems;
1489
1490 if (parent_nritems == 1)
1491 return 0;
1492
Chris Masonb4ce94d2009-02-04 09:25:08 -05001493 btrfs_set_lock_blocking(parent);
1494
Chris Mason6702ed42007-08-07 16:15:09 -04001495 for (i = start_slot; i < end_slot; i++) {
1496 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001497
Chris Mason081e9572007-11-06 10:26:24 -05001498 btrfs_node_key(parent, &disk_key, i);
1499 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1500 continue;
1501
1502 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001503 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001504 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001505 if (last_block == 0)
1506 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001507
Chris Mason6702ed42007-08-07 16:15:09 -04001508 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001509 other = btrfs_node_blockptr(parent, i - 1);
1510 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001511 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001512 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001513 other = btrfs_node_blockptr(parent, i + 1);
1514 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001515 }
Chris Masone9d0b132007-08-10 14:06:19 -04001516 if (close) {
1517 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001518 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001519 }
Chris Mason6702ed42007-08-07 16:15:09 -04001520
Chris Mason6b800532007-10-15 16:17:34 -04001521 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1522 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001523 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001524 else
1525 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001526 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001527 if (!cur) {
1528 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001529 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001530 if (!cur || !extent_buffer_uptodate(cur)) {
1531 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001532 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001533 }
Chris Mason6b800532007-10-15 16:17:34 -04001534 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001535 err = btrfs_read_buffer(cur, gen);
1536 if (err) {
1537 free_extent_buffer(cur);
1538 return err;
1539 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001540 }
Chris Mason6702ed42007-08-07 16:15:09 -04001541 }
Chris Masone9d0b132007-08-10 14:06:19 -04001542 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001543 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001544
Chris Masone7a84562008-06-25 16:01:31 -04001545 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001546 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001547 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001548 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001549 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001550 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001551 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001552 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001553 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001554 break;
Yan252c38f2007-08-29 09:11:44 -04001555 }
Chris Masone7a84562008-06-25 16:01:31 -04001556 search_start = cur->start;
1557 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001558 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001559 btrfs_tree_unlock(cur);
1560 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001561 }
1562 return err;
1563}
1564
Chris Mason74123bd2007-02-02 11:05:29 -05001565/*
1566 * The leaf data grows from end-to-front in the node.
1567 * this returns the address of the start of the last item,
1568 * which is the stop of the leaf data stack
1569 */
Chris Mason123abc82007-03-14 14:14:43 -04001570static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001571 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001572{
Chris Mason5f39d392007-10-15 16:14:19 -04001573 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001574 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001575 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001576 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001577}
1578
Chris Masonaa5d6be2007-02-28 16:35:06 -05001579
Chris Mason74123bd2007-02-02 11:05:29 -05001580/*
Chris Mason5f39d392007-10-15 16:14:19 -04001581 * search for key in the extent_buffer. The items start at offset p,
1582 * and they are item_size apart. There are 'max' items in p.
1583 *
Chris Mason74123bd2007-02-02 11:05:29 -05001584 * the slot in the array is returned via slot, and it points to
1585 * the place where you would insert key if it is not found in
1586 * the array.
1587 *
1588 * slot may point to max if the key is bigger than all of the keys
1589 */
Chris Masone02119d2008-09-05 16:13:11 -04001590static noinline int generic_bin_search(struct extent_buffer *eb,
1591 unsigned long p,
1592 int item_size, struct btrfs_key *key,
1593 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001594{
1595 int low = 0;
1596 int high = max;
1597 int mid;
1598 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001599 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 struct btrfs_disk_key unaligned;
1601 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001602 char *kaddr = NULL;
1603 unsigned long map_start = 0;
1604 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001605 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001606
Chris Masond3977122009-01-05 21:25:51 -05001607 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001608 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001609 offset = p + mid * item_size;
1610
Chris Masona6591712011-07-19 12:04:14 -04001611 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001612 (offset + sizeof(struct btrfs_disk_key)) >
1613 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001614
1615 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001616 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001617 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001618
Chris Mason479965d2007-10-15 16:14:27 -04001619 if (!err) {
1620 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1621 map_start);
1622 } else {
1623 read_extent_buffer(eb, &unaligned,
1624 offset, sizeof(unaligned));
1625 tmp = &unaligned;
1626 }
1627
Chris Mason5f39d392007-10-15 16:14:19 -04001628 } else {
1629 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1630 map_start);
1631 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001632 ret = comp_keys(tmp, key);
1633
1634 if (ret < 0)
1635 low = mid + 1;
1636 else if (ret > 0)
1637 high = mid;
1638 else {
1639 *slot = mid;
1640 return 0;
1641 }
1642 }
1643 *slot = low;
1644 return 1;
1645}
1646
Chris Mason97571fd2007-02-24 13:39:08 -05001647/*
1648 * simple bin_search frontend that does the right thing for
1649 * leaves vs nodes
1650 */
Chris Mason5f39d392007-10-15 16:14:19 -04001651static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1652 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001653{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001654 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001655 return generic_bin_search(eb,
1656 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001657 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001658 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001659 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001660 else
Chris Mason5f39d392007-10-15 16:14:19 -04001661 return generic_bin_search(eb,
1662 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001663 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001664 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001665 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001666}
1667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001668int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1669 int level, int *slot)
1670{
1671 return bin_search(eb, key, level, slot);
1672}
1673
Yan, Zhengf0486c62010-05-16 10:46:25 -04001674static void root_add_used(struct btrfs_root *root, u32 size)
1675{
1676 spin_lock(&root->accounting_lock);
1677 btrfs_set_root_used(&root->root_item,
1678 btrfs_root_used(&root->root_item) + size);
1679 spin_unlock(&root->accounting_lock);
1680}
1681
1682static void root_sub_used(struct btrfs_root *root, u32 size)
1683{
1684 spin_lock(&root->accounting_lock);
1685 btrfs_set_root_used(&root->root_item,
1686 btrfs_root_used(&root->root_item) - size);
1687 spin_unlock(&root->accounting_lock);
1688}
1689
Chris Masond352ac62008-09-29 15:18:18 -04001690/* given a node and slot number, this reads the blocks it points to. The
1691 * extent buffer is returned with a reference taken (but unlocked).
1692 * NULL is returned on error.
1693 */
Chris Masone02119d2008-09-05 16:13:11 -04001694static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001695 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001696{
Chris Masonca7a79a2008-05-12 12:59:19 -04001697 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001698 struct extent_buffer *eb;
1699
Chris Masonbb803952007-03-01 12:04:21 -05001700 if (slot < 0)
1701 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001702 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001703 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001704
1705 BUG_ON(level == 0);
1706
Josef Bacik416bc652013-04-23 14:17:42 -04001707 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1708 btrfs_level_size(root, level - 1),
1709 btrfs_node_ptr_generation(parent, slot));
1710 if (eb && !extent_buffer_uptodate(eb)) {
1711 free_extent_buffer(eb);
1712 eb = NULL;
1713 }
1714
1715 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001716}
1717
Chris Masond352ac62008-09-29 15:18:18 -04001718/*
1719 * node level balancing, used to make sure nodes are in proper order for
1720 * item deletion. We balance from the top down, so we have to make sure
1721 * that a deletion won't leave an node completely empty later on.
1722 */
Chris Masone02119d2008-09-05 16:13:11 -04001723static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001724 struct btrfs_root *root,
1725 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001726{
Chris Mason5f39d392007-10-15 16:14:19 -04001727 struct extent_buffer *right = NULL;
1728 struct extent_buffer *mid;
1729 struct extent_buffer *left = NULL;
1730 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001731 int ret = 0;
1732 int wret;
1733 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001734 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001735 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001736
1737 if (level == 0)
1738 return 0;
1739
Chris Mason5f39d392007-10-15 16:14:19 -04001740 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001741
Chris Masonbd681512011-07-16 15:23:14 -04001742 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1743 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001744 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1745
Chris Mason1d4f8a02007-03-13 09:28:32 -04001746 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001747
Li Zefana05a9bb2011-09-06 16:55:34 +08001748 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001749 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001750 pslot = path->slots[level + 1];
1751 }
Chris Masonbb803952007-03-01 12:04:21 -05001752
Chris Mason40689472007-03-17 14:29:23 -04001753 /*
1754 * deal with the case where there is only one pointer in the root
1755 * by promoting the node below to a root
1756 */
Chris Mason5f39d392007-10-15 16:14:19 -04001757 if (!parent) {
1758 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001759
Chris Mason5f39d392007-10-15 16:14:19 -04001760 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001761 return 0;
1762
1763 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001764 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001765 if (!child) {
1766 ret = -EROFS;
1767 btrfs_std_error(root->fs_info, ret);
1768 goto enospc;
1769 }
1770
Chris Mason925baed2008-06-25 16:01:30 -04001771 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001772 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001773 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001774 if (ret) {
1775 btrfs_tree_unlock(child);
1776 free_extent_buffer(child);
1777 goto enospc;
1778 }
Yan2f375ab2008-02-01 14:58:07 -05001779
Jan Schmidt90f8d622013-04-13 13:19:53 +00001780 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001781 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001782
Chris Mason0b86a832008-03-24 15:01:56 -04001783 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001784 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001785
Chris Mason925baed2008-06-25 16:01:30 -04001786 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001787 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001788 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001789 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001790 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001791 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001792
1793 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001794 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001795 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001796 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001797 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001798 }
Chris Mason5f39d392007-10-15 16:14:19 -04001799 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001800 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001801 return 0;
1802
Chris Mason5f39d392007-10-15 16:14:19 -04001803 left = read_node_slot(root, parent, pslot - 1);
1804 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001805 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001806 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001807 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001808 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001809 if (wret) {
1810 ret = wret;
1811 goto enospc;
1812 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001813 }
Chris Mason5f39d392007-10-15 16:14:19 -04001814 right = read_node_slot(root, parent, pslot + 1);
1815 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001816 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001817 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001818 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001819 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001820 if (wret) {
1821 ret = wret;
1822 goto enospc;
1823 }
1824 }
1825
1826 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001827 if (left) {
1828 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001829 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001830 if (wret < 0)
1831 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001832 }
Chris Mason79f95c82007-03-01 15:16:26 -05001833
1834 /*
1835 * then try to empty the right most buffer into the middle
1836 */
Chris Mason5f39d392007-10-15 16:14:19 -04001837 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001838 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001839 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001840 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001841 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001842 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001843 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001844 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001845 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001846 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001847 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001848 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001849 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001850 struct btrfs_disk_key right_key;
1851 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001852 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001853 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001854 btrfs_set_node_key(parent, &right_key, pslot + 1);
1855 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001856 }
1857 }
Chris Mason5f39d392007-10-15 16:14:19 -04001858 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001859 /*
1860 * we're not allowed to leave a node with one item in the
1861 * tree during a delete. A deletion from lower in the tree
1862 * could try to delete the only pointer in this node.
1863 * So, pull some keys from the left.
1864 * There has to be a left pointer at this point because
1865 * otherwise we would have pulled some pointers from the
1866 * right
1867 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001868 if (!left) {
1869 ret = -EROFS;
1870 btrfs_std_error(root->fs_info, ret);
1871 goto enospc;
1872 }
Chris Mason5f39d392007-10-15 16:14:19 -04001873 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001874 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001875 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001876 goto enospc;
1877 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001878 if (wret == 1) {
1879 wret = push_node_left(trans, root, left, mid, 1);
1880 if (wret < 0)
1881 ret = wret;
1882 }
Chris Mason79f95c82007-03-01 15:16:26 -05001883 BUG_ON(wret == 1);
1884 }
Chris Mason5f39d392007-10-15 16:14:19 -04001885 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001886 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001887 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001888 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001889 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001890 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001891 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001892 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001893 } else {
1894 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001895 struct btrfs_disk_key mid_key;
1896 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001897 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001898 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001899 btrfs_set_node_key(parent, &mid_key, pslot);
1900 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001901 }
Chris Masonbb803952007-03-01 12:04:21 -05001902
Chris Mason79f95c82007-03-01 15:16:26 -05001903 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001904 if (left) {
1905 if (btrfs_header_nritems(left) > orig_slot) {
1906 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001907 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001908 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001909 path->slots[level + 1] -= 1;
1910 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001911 if (mid) {
1912 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001913 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001914 }
Chris Masonbb803952007-03-01 12:04:21 -05001915 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001916 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001917 path->slots[level] = orig_slot;
1918 }
1919 }
Chris Mason79f95c82007-03-01 15:16:26 -05001920 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001921 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001922 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001923 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001924enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001925 if (right) {
1926 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001927 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001928 }
1929 if (left) {
1930 if (path->nodes[level] != left)
1931 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001932 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001933 }
Chris Masonbb803952007-03-01 12:04:21 -05001934 return ret;
1935}
1936
Chris Masond352ac62008-09-29 15:18:18 -04001937/* Node balancing for insertion. Here we only split or push nodes around
1938 * when they are completely full. This is also done top down, so we
1939 * have to be pessimistic.
1940 */
Chris Masond3977122009-01-05 21:25:51 -05001941static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001942 struct btrfs_root *root,
1943 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001944{
Chris Mason5f39d392007-10-15 16:14:19 -04001945 struct extent_buffer *right = NULL;
1946 struct extent_buffer *mid;
1947 struct extent_buffer *left = NULL;
1948 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001949 int ret = 0;
1950 int wret;
1951 int pslot;
1952 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001953
1954 if (level == 0)
1955 return 1;
1956
Chris Mason5f39d392007-10-15 16:14:19 -04001957 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001958 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001959
Li Zefana05a9bb2011-09-06 16:55:34 +08001960 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001961 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001962 pslot = path->slots[level + 1];
1963 }
Chris Masone66f7092007-04-20 13:16:02 -04001964
Chris Mason5f39d392007-10-15 16:14:19 -04001965 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001966 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001967
Chris Mason5f39d392007-10-15 16:14:19 -04001968 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001969
1970 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001971 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001972 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001973
1974 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001975 btrfs_set_lock_blocking(left);
1976
Chris Mason5f39d392007-10-15 16:14:19 -04001977 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001978 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1979 wret = 1;
1980 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001981 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001982 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001983 if (ret)
1984 wret = 1;
1985 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001986 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001987 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001988 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001989 }
Chris Masone66f7092007-04-20 13:16:02 -04001990 if (wret < 0)
1991 ret = wret;
1992 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001993 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001994 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001995 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001996 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001997 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001998 btrfs_set_node_key(parent, &disk_key, pslot);
1999 btrfs_mark_buffer_dirty(parent);
2000 if (btrfs_header_nritems(left) > orig_slot) {
2001 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002002 path->slots[level + 1] -= 1;
2003 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002004 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002005 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002006 } else {
2007 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002008 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002009 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002010 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002011 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002012 }
Chris Masone66f7092007-04-20 13:16:02 -04002013 return 0;
2014 }
Chris Mason925baed2008-06-25 16:01:30 -04002015 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002016 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002017 }
Chris Mason925baed2008-06-25 16:01:30 -04002018 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002019
2020 /*
2021 * then try to empty the right most buffer into the middle
2022 */
Chris Mason5f39d392007-10-15 16:14:19 -04002023 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002024 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002025
Chris Mason925baed2008-06-25 16:01:30 -04002026 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002027 btrfs_set_lock_blocking(right);
2028
Chris Mason5f39d392007-10-15 16:14:19 -04002029 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002030 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2031 wret = 1;
2032 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002033 ret = btrfs_cow_block(trans, root, right,
2034 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002035 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002036 if (ret)
2037 wret = 1;
2038 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002039 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002040 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002041 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002042 }
Chris Masone66f7092007-04-20 13:16:02 -04002043 if (wret < 0)
2044 ret = wret;
2045 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002046 struct btrfs_disk_key disk_key;
2047
2048 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002049 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002050 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002051 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2052 btrfs_mark_buffer_dirty(parent);
2053
2054 if (btrfs_header_nritems(mid) <= orig_slot) {
2055 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002056 path->slots[level + 1] += 1;
2057 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002058 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002059 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002060 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002061 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002062 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002063 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002064 }
Chris Masone66f7092007-04-20 13:16:02 -04002065 return 0;
2066 }
Chris Mason925baed2008-06-25 16:01:30 -04002067 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002068 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002069 }
Chris Masone66f7092007-04-20 13:16:02 -04002070 return 1;
2071}
2072
Chris Mason74123bd2007-02-02 11:05:29 -05002073/*
Chris Masond352ac62008-09-29 15:18:18 -04002074 * readahead one full node of leaves, finding things that are close
2075 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002076 */
Chris Masonc8c42862009-04-03 10:14:18 -04002077static void reada_for_search(struct btrfs_root *root,
2078 struct btrfs_path *path,
2079 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002080{
Chris Mason5f39d392007-10-15 16:14:19 -04002081 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002082 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002083 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002084 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002085 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002086 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002087 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002088 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002089 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002090 u32 nr;
2091 u32 blocksize;
2092 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002093
Chris Masona6b6e752007-10-15 16:22:39 -04002094 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002095 return;
2096
Chris Mason6702ed42007-08-07 16:15:09 -04002097 if (!path->nodes[level])
2098 return;
2099
Chris Mason5f39d392007-10-15 16:14:19 -04002100 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002101
Chris Mason3c69fae2007-08-07 15:52:22 -04002102 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002103 blocksize = btrfs_level_size(root, level - 1);
2104 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002105 if (eb) {
2106 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002107 return;
2108 }
2109
Chris Masona7175312009-01-22 09:23:10 -05002110 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002111
Chris Mason5f39d392007-10-15 16:14:19 -04002112 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002113 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002114
Chris Masond3977122009-01-05 21:25:51 -05002115 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002116 if (direction < 0) {
2117 if (nr == 0)
2118 break;
2119 nr--;
2120 } else if (direction > 0) {
2121 nr++;
2122 if (nr >= nritems)
2123 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002124 }
Chris Mason01f46652007-12-21 16:24:26 -05002125 if (path->reada < 0 && objectid) {
2126 btrfs_node_key(node, &disk_key, nr);
2127 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2128 break;
2129 }
Chris Mason6b800532007-10-15 16:17:34 -04002130 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002131 if ((search <= target && target - search <= 65536) ||
2132 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002133 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002134 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002135 nread += blocksize;
2136 }
2137 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002138 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002139 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002140 }
2141}
Chris Mason925baed2008-06-25 16:01:30 -04002142
Josef Bacik0b088512013-06-17 14:23:02 -04002143static noinline void reada_for_balance(struct btrfs_root *root,
2144 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002145{
2146 int slot;
2147 int nritems;
2148 struct extent_buffer *parent;
2149 struct extent_buffer *eb;
2150 u64 gen;
2151 u64 block1 = 0;
2152 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002153 int blocksize;
2154
Chris Mason8c594ea2009-04-20 15:50:10 -04002155 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002156 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002157 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002158
2159 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002160 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002161 blocksize = btrfs_level_size(root, level);
2162
2163 if (slot > 0) {
2164 block1 = btrfs_node_blockptr(parent, slot - 1);
2165 gen = btrfs_node_ptr_generation(parent, slot - 1);
2166 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002167 /*
2168 * if we get -eagain from btrfs_buffer_uptodate, we
2169 * don't want to return eagain here. That will loop
2170 * forever
2171 */
2172 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002173 block1 = 0;
2174 free_extent_buffer(eb);
2175 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002176 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002177 block2 = btrfs_node_blockptr(parent, slot + 1);
2178 gen = btrfs_node_ptr_generation(parent, slot + 1);
2179 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002180 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002181 block2 = 0;
2182 free_extent_buffer(eb);
2183 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002184
Josef Bacik0b088512013-06-17 14:23:02 -04002185 if (block1)
2186 readahead_tree_block(root, block1, blocksize, 0);
2187 if (block2)
2188 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002189}
2190
2191
2192/*
Chris Masond3977122009-01-05 21:25:51 -05002193 * when we walk down the tree, it is usually safe to unlock the higher layers
2194 * in the tree. The exceptions are when our path goes through slot 0, because
2195 * operations on the tree might require changing key pointers higher up in the
2196 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002197 *
Chris Masond3977122009-01-05 21:25:51 -05002198 * callers might also have set path->keep_locks, which tells this code to keep
2199 * the lock if the path points to the last slot in the block. This is part of
2200 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002201 *
Chris Masond3977122009-01-05 21:25:51 -05002202 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2203 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002204 */
Chris Masone02119d2008-09-05 16:13:11 -04002205static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002206 int lowest_unlock, int min_write_lock_level,
2207 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002208{
2209 int i;
2210 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002211 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002212 struct extent_buffer *t;
2213
2214 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2215 if (!path->nodes[i])
2216 break;
2217 if (!path->locks[i])
2218 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002219 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002220 skip_level = i + 1;
2221 continue;
2222 }
Chris Mason051e1b92008-06-25 16:01:30 -04002223 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002224 u32 nritems;
2225 t = path->nodes[i];
2226 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002227 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002228 skip_level = i + 1;
2229 continue;
2230 }
2231 }
Chris Mason051e1b92008-06-25 16:01:30 -04002232 if (skip_level < i && i >= lowest_unlock)
2233 no_skips = 1;
2234
Chris Mason925baed2008-06-25 16:01:30 -04002235 t = path->nodes[i];
2236 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002237 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002238 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002239 if (write_lock_level &&
2240 i > min_write_lock_level &&
2241 i <= *write_lock_level) {
2242 *write_lock_level = i - 1;
2243 }
Chris Mason925baed2008-06-25 16:01:30 -04002244 }
2245 }
2246}
2247
Chris Mason3c69fae2007-08-07 15:52:22 -04002248/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002249 * This releases any locks held in the path starting at level and
2250 * going all the way up to the root.
2251 *
2252 * btrfs_search_slot will keep the lock held on higher nodes in a few
2253 * corner cases, such as COW of the block at slot zero in the node. This
2254 * ignores those rules, and it should only be called when there are no
2255 * more updates to be done higher up in the tree.
2256 */
2257noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2258{
2259 int i;
2260
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002261 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002262 return;
2263
2264 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2265 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002266 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002267 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002268 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002269 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002270 path->locks[i] = 0;
2271 }
2272}
2273
2274/*
Chris Masonc8c42862009-04-03 10:14:18 -04002275 * helper function for btrfs_search_slot. The goal is to find a block
2276 * in cache without setting the path to blocking. If we find the block
2277 * we return zero and the path is unchanged.
2278 *
2279 * If we can't find the block, we set the path blocking and do some
2280 * reada. -EAGAIN is returned and the search must be repeated.
2281 */
2282static int
2283read_block_for_search(struct btrfs_trans_handle *trans,
2284 struct btrfs_root *root, struct btrfs_path *p,
2285 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002286 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002287{
2288 u64 blocknr;
2289 u64 gen;
2290 u32 blocksize;
2291 struct extent_buffer *b = *eb_ret;
2292 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002293 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002294
2295 blocknr = btrfs_node_blockptr(b, slot);
2296 gen = btrfs_node_ptr_generation(b, slot);
2297 blocksize = btrfs_level_size(root, level - 1);
2298
2299 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002300 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002301 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002302 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2303 *eb_ret = tmp;
2304 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002305 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002306
2307 /* the pages were up to date, but we failed
2308 * the generation number check. Do a full
2309 * read for the generation number that is correct.
2310 * We must do this without dropping locks so
2311 * we can trust our generation number
2312 */
2313 btrfs_set_path_blocking(p);
2314
2315 /* now we're allowed to do a blocking uptodate check */
2316 ret = btrfs_read_buffer(tmp, gen);
2317 if (!ret) {
2318 *eb_ret = tmp;
2319 return 0;
2320 }
2321 free_extent_buffer(tmp);
2322 btrfs_release_path(p);
2323 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002324 }
2325
2326 /*
2327 * reduce lock contention at high levels
2328 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002329 * we read. Don't release the lock on the current
2330 * level because we need to walk this node to figure
2331 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002332 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002333 btrfs_unlock_up_safe(p, level + 1);
2334 btrfs_set_path_blocking(p);
2335
Chris Masoncb449212010-10-24 11:01:27 -04002336 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002337 if (p->reada)
2338 reada_for_search(root, p, level, slot, key->objectid);
2339
David Sterbab3b4aa72011-04-21 01:20:15 +02002340 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002341
2342 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002343 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002344 if (tmp) {
2345 /*
2346 * If the read above didn't mark this buffer up to date,
2347 * it will never end up being up to date. Set ret to EIO now
2348 * and give up so that our caller doesn't loop forever
2349 * on our EAGAINs.
2350 */
Chris Masonb9fab912012-05-06 07:23:47 -04002351 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002352 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002353 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002354 }
2355 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002356}
2357
2358/*
2359 * helper function for btrfs_search_slot. This does all of the checks
2360 * for node-level blocks and does any balancing required based on
2361 * the ins_len.
2362 *
2363 * If no extra work was required, zero is returned. If we had to
2364 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2365 * start over
2366 */
2367static int
2368setup_nodes_for_search(struct btrfs_trans_handle *trans,
2369 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002370 struct extent_buffer *b, int level, int ins_len,
2371 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002372{
2373 int ret;
2374 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2375 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2376 int sret;
2377
Chris Masonbd681512011-07-16 15:23:14 -04002378 if (*write_lock_level < level + 1) {
2379 *write_lock_level = level + 1;
2380 btrfs_release_path(p);
2381 goto again;
2382 }
2383
Chris Masonc8c42862009-04-03 10:14:18 -04002384 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002385 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002386 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002387 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002388
2389 BUG_ON(sret > 0);
2390 if (sret) {
2391 ret = sret;
2392 goto done;
2393 }
2394 b = p->nodes[level];
2395 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002396 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002397 int sret;
2398
Chris Masonbd681512011-07-16 15:23:14 -04002399 if (*write_lock_level < level + 1) {
2400 *write_lock_level = level + 1;
2401 btrfs_release_path(p);
2402 goto again;
2403 }
2404
Chris Masonc8c42862009-04-03 10:14:18 -04002405 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002406 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002407 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002408 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002409
2410 if (sret) {
2411 ret = sret;
2412 goto done;
2413 }
2414 b = p->nodes[level];
2415 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002416 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002417 goto again;
2418 }
2419 BUG_ON(btrfs_header_nritems(b) == 1);
2420 }
2421 return 0;
2422
2423again:
2424 ret = -EAGAIN;
2425done:
2426 return ret;
2427}
2428
2429/*
Chris Mason74123bd2007-02-02 11:05:29 -05002430 * look for key in the tree. path is filled in with nodes along the way
2431 * if key is found, we return zero and you can find the item in the leaf
2432 * level of the path (level 0)
2433 *
2434 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002435 * be inserted, and 1 is returned. If there are other errors during the
2436 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002437 *
2438 * if ins_len > 0, nodes and leaves will be split as we walk down the
2439 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2440 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002441 */
Chris Masone089f052007-03-16 16:20:31 -04002442int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2443 *root, struct btrfs_key *key, struct btrfs_path *p, int
2444 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002445{
Chris Mason5f39d392007-10-15 16:14:19 -04002446 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002447 int slot;
2448 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002449 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002450 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002451 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002452 int root_lock;
2453 /* everything at write_lock_level or lower must be write locked */
2454 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002455 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002456 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002457
Chris Mason6702ed42007-08-07 16:15:09 -04002458 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002459 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002460 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002461
Chris Masonbd681512011-07-16 15:23:14 -04002462 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002463 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002464
Chris Masonbd681512011-07-16 15:23:14 -04002465 /* when we are removing items, we might have to go up to level
2466 * two as we update tree pointers Make sure we keep write
2467 * for those levels as well
2468 */
2469 write_lock_level = 2;
2470 } else if (ins_len > 0) {
2471 /*
2472 * for inserting items, make sure we have a write lock on
2473 * level 1 so we can update keys
2474 */
2475 write_lock_level = 1;
2476 }
2477
2478 if (!cow)
2479 write_lock_level = -1;
2480
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002481 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002482 write_lock_level = BTRFS_MAX_LEVEL;
2483
Chris Masonf7c79f32012-03-19 15:54:38 -04002484 min_write_lock_level = write_lock_level;
2485
Chris Masonbb803952007-03-01 12:04:21 -05002486again:
Chris Masonbd681512011-07-16 15:23:14 -04002487 /*
2488 * we try very hard to do read locks on the root
2489 */
2490 root_lock = BTRFS_READ_LOCK;
2491 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002492 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002493 /*
2494 * the commit roots are read only
2495 * so we always do read locks
2496 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002497 b = root->commit_root;
2498 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002499 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002500 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002501 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002502 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002503 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002504 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002505 level = btrfs_header_level(b);
2506 } else {
2507 /* we don't know the level of the root node
2508 * until we actually have it read locked
2509 */
2510 b = btrfs_read_lock_root_node(root);
2511 level = btrfs_header_level(b);
2512 if (level <= write_lock_level) {
2513 /* whoops, must trade for write lock */
2514 btrfs_tree_read_unlock(b);
2515 free_extent_buffer(b);
2516 b = btrfs_lock_root_node(root);
2517 root_lock = BTRFS_WRITE_LOCK;
2518
2519 /* the level might have changed, check again */
2520 level = btrfs_header_level(b);
2521 }
2522 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002523 }
Chris Masonbd681512011-07-16 15:23:14 -04002524 p->nodes[level] = b;
2525 if (!p->skip_locking)
2526 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002527
Chris Masoneb60cea2007-02-02 09:18:22 -05002528 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002529 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002530
2531 /*
2532 * setup the path here so we can release it under lock
2533 * contention with the cow code
2534 */
Chris Mason02217ed2007-03-02 16:08:05 -05002535 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002536 /*
2537 * if we don't really need to cow this block
2538 * then we don't want to set the path blocking,
2539 * so we test it here
2540 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002541 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002542 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002543
Chris Masonb4ce94d2009-02-04 09:25:08 -05002544 btrfs_set_path_blocking(p);
2545
Chris Masonbd681512011-07-16 15:23:14 -04002546 /*
2547 * must have write locks on this node and the
2548 * parent
2549 */
Josef Bacik5124e002012-11-07 13:44:13 -05002550 if (level > write_lock_level ||
2551 (level + 1 > write_lock_level &&
2552 level + 1 < BTRFS_MAX_LEVEL &&
2553 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002554 write_lock_level = level + 1;
2555 btrfs_release_path(p);
2556 goto again;
2557 }
2558
Yan Zheng33c66f42009-07-22 09:59:00 -04002559 err = btrfs_cow_block(trans, root, b,
2560 p->nodes[level + 1],
2561 p->slots[level + 1], &b);
2562 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002563 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002564 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002565 }
Chris Mason02217ed2007-03-02 16:08:05 -05002566 }
Chris Mason65b51a02008-08-01 15:11:20 -04002567cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002568 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002569
Chris Masoneb60cea2007-02-02 09:18:22 -05002570 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002571 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002572
2573 /*
2574 * we have a lock on b and as long as we aren't changing
2575 * the tree, there is no way to for the items in b to change.
2576 * It is safe to drop the lock on our parent before we
2577 * go through the expensive btree search on b.
2578 *
2579 * If cow is true, then we might be changing slot zero,
2580 * which may require changing the parent. So, we can't
2581 * drop the lock until after we know which slot we're
2582 * operating on.
2583 */
2584 if (!cow)
2585 btrfs_unlock_up_safe(p, level + 1);
2586
Chris Mason5f39d392007-10-15 16:14:19 -04002587 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002588
Chris Mason5f39d392007-10-15 16:14:19 -04002589 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002590 int dec = 0;
2591 if (ret && slot > 0) {
2592 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002593 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002594 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002596 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002597 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002598 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002599 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002600 if (err) {
2601 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002602 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002603 }
Chris Masonc8c42862009-04-03 10:14:18 -04002604 b = p->nodes[level];
2605 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002606
Chris Masonbd681512011-07-16 15:23:14 -04002607 /*
2608 * slot 0 is special, if we change the key
2609 * we have to update the parent pointer
2610 * which means we must have a write lock
2611 * on the parent
2612 */
2613 if (slot == 0 && cow &&
2614 write_lock_level < level + 1) {
2615 write_lock_level = level + 1;
2616 btrfs_release_path(p);
2617 goto again;
2618 }
2619
Chris Masonf7c79f32012-03-19 15:54:38 -04002620 unlock_up(p, level, lowest_unlock,
2621 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002622
Chris Mason925baed2008-06-25 16:01:30 -04002623 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002624 if (dec)
2625 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002626 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002627 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002628
Yan Zheng33c66f42009-07-22 09:59:00 -04002629 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002630 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002631 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002632 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002633 if (err) {
2634 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002635 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002636 }
Chris Mason76a05b32009-05-14 13:24:30 -04002637
Chris Masonb4ce94d2009-02-04 09:25:08 -05002638 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002639 level = btrfs_header_level(b);
2640 if (level <= write_lock_level) {
2641 err = btrfs_try_tree_write_lock(b);
2642 if (!err) {
2643 btrfs_set_path_blocking(p);
2644 btrfs_tree_lock(b);
2645 btrfs_clear_path_blocking(p, b,
2646 BTRFS_WRITE_LOCK);
2647 }
2648 p->locks[level] = BTRFS_WRITE_LOCK;
2649 } else {
2650 err = btrfs_try_tree_read_lock(b);
2651 if (!err) {
2652 btrfs_set_path_blocking(p);
2653 btrfs_tree_read_lock(b);
2654 btrfs_clear_path_blocking(p, b,
2655 BTRFS_READ_LOCK);
2656 }
2657 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002658 }
Chris Masonbd681512011-07-16 15:23:14 -04002659 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002660 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002661 } else {
2662 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002663 if (ins_len > 0 &&
2664 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002665 if (write_lock_level < 1) {
2666 write_lock_level = 1;
2667 btrfs_release_path(p);
2668 goto again;
2669 }
2670
Chris Masonb4ce94d2009-02-04 09:25:08 -05002671 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002672 err = split_leaf(trans, root, key,
2673 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002674 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002675
Yan Zheng33c66f42009-07-22 09:59:00 -04002676 BUG_ON(err > 0);
2677 if (err) {
2678 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002679 goto done;
2680 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002681 }
Chris Mason459931e2008-12-10 09:10:46 -05002682 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002683 unlock_up(p, level, lowest_unlock,
2684 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002685 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002686 }
2687 }
Chris Mason65b51a02008-08-01 15:11:20 -04002688 ret = 1;
2689done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002690 /*
2691 * we don't really know what they plan on doing with the path
2692 * from here on, so for now just mark it as blocking
2693 */
Chris Masonb9473432009-03-13 11:00:37 -04002694 if (!p->leave_spinning)
2695 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002696 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002697 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002698 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699}
2700
Chris Mason74123bd2007-02-02 11:05:29 -05002701/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002702 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2703 * current state of the tree together with the operations recorded in the tree
2704 * modification log to search for the key in a previous version of this tree, as
2705 * denoted by the time_seq parameter.
2706 *
2707 * Naturally, there is no support for insert, delete or cow operations.
2708 *
2709 * The resulting path and return value will be set up as if we called
2710 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2711 */
2712int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2713 struct btrfs_path *p, u64 time_seq)
2714{
2715 struct extent_buffer *b;
2716 int slot;
2717 int ret;
2718 int err;
2719 int level;
2720 int lowest_unlock = 1;
2721 u8 lowest_level = 0;
2722
2723 lowest_level = p->lowest_level;
2724 WARN_ON(p->nodes[0] != NULL);
2725
2726 if (p->search_commit_root) {
2727 BUG_ON(time_seq);
2728 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2729 }
2730
2731again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002732 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002733 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002734 p->locks[level] = BTRFS_READ_LOCK;
2735
2736 while (b) {
2737 level = btrfs_header_level(b);
2738 p->nodes[level] = b;
2739 btrfs_clear_path_blocking(p, NULL, 0);
2740
2741 /*
2742 * we have a lock on b and as long as we aren't changing
2743 * the tree, there is no way to for the items in b to change.
2744 * It is safe to drop the lock on our parent before we
2745 * go through the expensive btree search on b.
2746 */
2747 btrfs_unlock_up_safe(p, level + 1);
2748
2749 ret = bin_search(b, key, level, &slot);
2750
2751 if (level != 0) {
2752 int dec = 0;
2753 if (ret && slot > 0) {
2754 dec = 1;
2755 slot -= 1;
2756 }
2757 p->slots[level] = slot;
2758 unlock_up(p, level, lowest_unlock, 0, NULL);
2759
2760 if (level == lowest_level) {
2761 if (dec)
2762 p->slots[level]++;
2763 goto done;
2764 }
2765
2766 err = read_block_for_search(NULL, root, p, &b, level,
2767 slot, key, time_seq);
2768 if (err == -EAGAIN)
2769 goto again;
2770 if (err) {
2771 ret = err;
2772 goto done;
2773 }
2774
2775 level = btrfs_header_level(b);
2776 err = btrfs_try_tree_read_lock(b);
2777 if (!err) {
2778 btrfs_set_path_blocking(p);
2779 btrfs_tree_read_lock(b);
2780 btrfs_clear_path_blocking(p, b,
2781 BTRFS_READ_LOCK);
2782 }
Josef Bacik9ec72672013-08-07 16:57:23 -04002783 b = tree_mod_log_rewind(root->fs_info, p, b, time_seq);
Josef Bacikdb7f3432013-08-07 14:54:37 -04002784 if (!b) {
2785 ret = -ENOMEM;
2786 goto done;
2787 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002788 p->locks[level] = BTRFS_READ_LOCK;
2789 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002790 } else {
2791 p->slots[level] = slot;
2792 unlock_up(p, level, lowest_unlock, 0, NULL);
2793 goto done;
2794 }
2795 }
2796 ret = 1;
2797done:
2798 if (!p->leave_spinning)
2799 btrfs_set_path_blocking(p);
2800 if (ret < 0)
2801 btrfs_release_path(p);
2802
2803 return ret;
2804}
2805
2806/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002807 * helper to use instead of search slot if no exact match is needed but
2808 * instead the next or previous item should be returned.
2809 * When find_higher is true, the next higher item is returned, the next lower
2810 * otherwise.
2811 * When return_any and find_higher are both true, and no higher item is found,
2812 * return the next lower instead.
2813 * When return_any is true and find_higher is false, and no lower item is found,
2814 * return the next higher instead.
2815 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2816 * < 0 on error
2817 */
2818int btrfs_search_slot_for_read(struct btrfs_root *root,
2819 struct btrfs_key *key, struct btrfs_path *p,
2820 int find_higher, int return_any)
2821{
2822 int ret;
2823 struct extent_buffer *leaf;
2824
2825again:
2826 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2827 if (ret <= 0)
2828 return ret;
2829 /*
2830 * a return value of 1 means the path is at the position where the
2831 * item should be inserted. Normally this is the next bigger item,
2832 * but in case the previous item is the last in a leaf, path points
2833 * to the first free slot in the previous leaf, i.e. at an invalid
2834 * item.
2835 */
2836 leaf = p->nodes[0];
2837
2838 if (find_higher) {
2839 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2840 ret = btrfs_next_leaf(root, p);
2841 if (ret <= 0)
2842 return ret;
2843 if (!return_any)
2844 return 1;
2845 /*
2846 * no higher item found, return the next
2847 * lower instead
2848 */
2849 return_any = 0;
2850 find_higher = 0;
2851 btrfs_release_path(p);
2852 goto again;
2853 }
2854 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002855 if (p->slots[0] == 0) {
2856 ret = btrfs_prev_leaf(root, p);
2857 if (ret < 0)
2858 return ret;
2859 if (!ret) {
2860 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2861 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002862 }
Arne Jansene6793762011-09-13 11:18:10 +02002863 if (!return_any)
2864 return 1;
2865 /*
2866 * no lower item found, return the next
2867 * higher instead
2868 */
2869 return_any = 0;
2870 find_higher = 1;
2871 btrfs_release_path(p);
2872 goto again;
2873 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002874 --p->slots[0];
2875 }
2876 }
2877 return 0;
2878}
2879
2880/*
Chris Mason74123bd2007-02-02 11:05:29 -05002881 * adjust the pointers going up the tree, starting at level
2882 * making sure the right key of each node is points to 'key'.
2883 * This is used after shifting pointers to the left, so it stops
2884 * fixing up pointers when a given leaf/node is not in slot 0 of the
2885 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002886 *
Chris Mason74123bd2007-02-02 11:05:29 -05002887 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002888static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002889 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002890{
2891 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002892 struct extent_buffer *t;
2893
Chris Mason234b63a2007-03-13 10:46:10 -04002894 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002895 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002896 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002897 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002898 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002899 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002900 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002901 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002902 if (tslot != 0)
2903 break;
2904 }
2905}
2906
Chris Mason74123bd2007-02-02 11:05:29 -05002907/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002908 * update item key.
2909 *
2910 * This function isn't completely safe. It's the caller's responsibility
2911 * that the new key won't break the order
2912 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002913void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002914 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002915{
2916 struct btrfs_disk_key disk_key;
2917 struct extent_buffer *eb;
2918 int slot;
2919
2920 eb = path->nodes[0];
2921 slot = path->slots[0];
2922 if (slot > 0) {
2923 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002924 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002925 }
2926 if (slot < btrfs_header_nritems(eb) - 1) {
2927 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002928 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002929 }
2930
2931 btrfs_cpu_key_to_disk(&disk_key, new_key);
2932 btrfs_set_item_key(eb, &disk_key, slot);
2933 btrfs_mark_buffer_dirty(eb);
2934 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002935 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002936}
2937
2938/*
Chris Mason74123bd2007-02-02 11:05:29 -05002939 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002940 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002941 *
2942 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2943 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002944 */
Chris Mason98ed5172008-01-03 10:01:48 -05002945static int push_node_left(struct btrfs_trans_handle *trans,
2946 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002947 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002948{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002949 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002950 int src_nritems;
2951 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002952 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002953
Chris Mason5f39d392007-10-15 16:14:19 -04002954 src_nritems = btrfs_header_nritems(src);
2955 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002956 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002957 WARN_ON(btrfs_header_generation(src) != trans->transid);
2958 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002959
Chris Masonbce4eae2008-04-24 14:42:46 -04002960 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002961 return 1;
2962
Chris Masond3977122009-01-05 21:25:51 -05002963 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002964 return 1;
2965
Chris Masonbce4eae2008-04-24 14:42:46 -04002966 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002967 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002968 if (push_items < src_nritems) {
2969 /* leave at least 8 pointers in the node if
2970 * we aren't going to empty it
2971 */
2972 if (src_nritems - push_items < 8) {
2973 if (push_items <= 8)
2974 return 1;
2975 push_items -= 8;
2976 }
2977 }
2978 } else
2979 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002980
Jan Schmidtf2304752012-05-26 11:43:17 +02002981 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00002982 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002983 copy_extent_buffer(dst, src,
2984 btrfs_node_key_ptr_offset(dst_nritems),
2985 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002986 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002987
Chris Masonbb803952007-03-01 12:04:21 -05002988 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02002989 /*
2990 * don't call tree_mod_log_eb_move here, key removal was already
2991 * fully logged by tree_mod_log_eb_copy above.
2992 */
Chris Mason5f39d392007-10-15 16:14:19 -04002993 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2994 btrfs_node_key_ptr_offset(push_items),
2995 (src_nritems - push_items) *
2996 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002997 }
Chris Mason5f39d392007-10-15 16:14:19 -04002998 btrfs_set_header_nritems(src, src_nritems - push_items);
2999 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3000 btrfs_mark_buffer_dirty(src);
3001 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003002
Chris Masonbb803952007-03-01 12:04:21 -05003003 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003004}
3005
Chris Mason97571fd2007-02-24 13:39:08 -05003006/*
Chris Mason79f95c82007-03-01 15:16:26 -05003007 * try to push data from one node into the next node right in the
3008 * tree.
3009 *
3010 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3011 * error, and > 0 if there was no room in the right hand block.
3012 *
3013 * this will only push up to 1/2 the contents of the left node over
3014 */
Chris Mason5f39d392007-10-15 16:14:19 -04003015static int balance_node_right(struct btrfs_trans_handle *trans,
3016 struct btrfs_root *root,
3017 struct extent_buffer *dst,
3018 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003019{
Chris Mason79f95c82007-03-01 15:16:26 -05003020 int push_items = 0;
3021 int max_push;
3022 int src_nritems;
3023 int dst_nritems;
3024 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003025
Chris Mason7bb86312007-12-11 09:25:06 -05003026 WARN_ON(btrfs_header_generation(src) != trans->transid);
3027 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3028
Chris Mason5f39d392007-10-15 16:14:19 -04003029 src_nritems = btrfs_header_nritems(src);
3030 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003031 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003032 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003033 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003034
Chris Masond3977122009-01-05 21:25:51 -05003035 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003036 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003037
3038 max_push = src_nritems / 2 + 1;
3039 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003040 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003041 return 1;
Yan252c38f2007-08-29 09:11:44 -04003042
Chris Mason79f95c82007-03-01 15:16:26 -05003043 if (max_push < push_items)
3044 push_items = max_push;
3045
Jan Schmidtf2304752012-05-26 11:43:17 +02003046 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003047 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3048 btrfs_node_key_ptr_offset(0),
3049 (dst_nritems) *
3050 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003051
Jan Schmidtf2304752012-05-26 11:43:17 +02003052 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003053 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003054 copy_extent_buffer(dst, src,
3055 btrfs_node_key_ptr_offset(0),
3056 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003057 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003058
Chris Mason5f39d392007-10-15 16:14:19 -04003059 btrfs_set_header_nritems(src, src_nritems - push_items);
3060 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003061
Chris Mason5f39d392007-10-15 16:14:19 -04003062 btrfs_mark_buffer_dirty(src);
3063 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003064
Chris Mason79f95c82007-03-01 15:16:26 -05003065 return ret;
3066}
3067
3068/*
Chris Mason97571fd2007-02-24 13:39:08 -05003069 * helper function to insert a new root level in the tree.
3070 * A new node is allocated, and a single item is inserted to
3071 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003072 *
3073 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003074 */
Chris Masond3977122009-01-05 21:25:51 -05003075static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003076 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003077 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003078{
Chris Mason7bb86312007-12-11 09:25:06 -05003079 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003080 struct extent_buffer *lower;
3081 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003082 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003083 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003084
3085 BUG_ON(path->nodes[level]);
3086 BUG_ON(path->nodes[level-1] != root->node);
3087
Chris Mason7bb86312007-12-11 09:25:06 -05003088 lower = path->nodes[level-1];
3089 if (level == 1)
3090 btrfs_item_key(lower, &lower_key, 0);
3091 else
3092 btrfs_node_key(lower, &lower_key, 0);
3093
Zheng Yan31840ae2008-09-23 13:14:14 -04003094 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003095 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003096 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003097 if (IS_ERR(c))
3098 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003099
Yan, Zhengf0486c62010-05-16 10:46:25 -04003100 root_add_used(root, root->nodesize);
3101
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003102 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003103 btrfs_set_header_nritems(c, 1);
3104 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003105 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003106 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003107 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003108 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003109
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +02003110 write_extent_buffer(c, root->fs_info->fsid, btrfs_header_fsid(c),
Chris Mason5f39d392007-10-15 16:14:19 -04003111 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003112
3113 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003114 btrfs_header_chunk_tree_uuid(c), BTRFS_UUID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003115
Chris Mason5f39d392007-10-15 16:14:19 -04003116 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003117 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003118 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003119 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003120
3121 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003122
3123 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003124
Chris Mason925baed2008-06-25 16:01:30 -04003125 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003126 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003127 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003128
3129 /* the super has an extra ref to root->node */
3130 free_extent_buffer(old);
3131
Chris Mason0b86a832008-03-24 15:01:56 -04003132 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003133 extent_buffer_get(c);
3134 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003135 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003136 path->slots[level] = 0;
3137 return 0;
3138}
3139
Chris Mason74123bd2007-02-02 11:05:29 -05003140/*
3141 * worker function to insert a single pointer in a node.
3142 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003143 *
Chris Mason74123bd2007-02-02 11:05:29 -05003144 * slot and level indicate where you want the key to go, and
3145 * blocknr is the block the key points to.
3146 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003147static void insert_ptr(struct btrfs_trans_handle *trans,
3148 struct btrfs_root *root, struct btrfs_path *path,
3149 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003150 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003151{
Chris Mason5f39d392007-10-15 16:14:19 -04003152 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003153 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003154 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003155
3156 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003157 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003158 lower = path->nodes[level];
3159 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003160 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003161 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003162 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003163 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003164 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3165 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003166 memmove_extent_buffer(lower,
3167 btrfs_node_key_ptr_offset(slot + 1),
3168 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003169 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003170 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003171 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003172 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04003173 MOD_LOG_KEY_ADD, GFP_NOFS);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003174 BUG_ON(ret < 0);
3175 }
Chris Mason5f39d392007-10-15 16:14:19 -04003176 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003177 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003178 WARN_ON(trans->transid == 0);
3179 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003180 btrfs_set_header_nritems(lower, nritems + 1);
3181 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003182}
3183
Chris Mason97571fd2007-02-24 13:39:08 -05003184/*
3185 * split the node at the specified level in path in two.
3186 * The path is corrected to point to the appropriate node after the split
3187 *
3188 * Before splitting this tries to make some room in the node by pushing
3189 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003190 *
3191 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003192 */
Chris Masone02119d2008-09-05 16:13:11 -04003193static noinline int split_node(struct btrfs_trans_handle *trans,
3194 struct btrfs_root *root,
3195 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003196{
Chris Mason5f39d392007-10-15 16:14:19 -04003197 struct extent_buffer *c;
3198 struct extent_buffer *split;
3199 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003200 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003201 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003202 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003203
Chris Mason5f39d392007-10-15 16:14:19 -04003204 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003205 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003206 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003207 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003208 * trying to split the root, lets make a new one
3209 *
Liu Bofdd99c72013-05-22 12:06:51 +00003210 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003211 * insert_new_root, because that root buffer will be kept as a
3212 * normal node. We are going to log removal of half of the
3213 * elements below with tree_mod_log_eb_copy. We're holding a
3214 * tree lock on the buffer, which is why we cannot race with
3215 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003216 */
Liu Bofdd99c72013-05-22 12:06:51 +00003217 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003218 if (ret)
3219 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003220 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003221 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003222 c = path->nodes[level];
3223 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003224 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003225 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003226 if (ret < 0)
3227 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003228 }
Chris Masone66f7092007-04-20 13:16:02 -04003229
Chris Mason5f39d392007-10-15 16:14:19 -04003230 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003231 mid = (c_nritems + 1) / 2;
3232 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003233
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003234 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003235 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003236 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003237 if (IS_ERR(split))
3238 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003239
Yan, Zhengf0486c62010-05-16 10:46:25 -04003240 root_add_used(root, root->nodesize);
3241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003242 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003243 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003244 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003245 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003246 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003247 btrfs_set_header_owner(split, root->root_key.objectid);
3248 write_extent_buffer(split, root->fs_info->fsid,
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +02003249 btrfs_header_fsid(split), BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003250 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02003251 btrfs_header_chunk_tree_uuid(split),
Chris Masone17cade2008-04-15 15:41:47 -04003252 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003253
Jan Schmidt90f8d622013-04-13 13:19:53 +00003254 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003255 copy_extent_buffer(split, c,
3256 btrfs_node_key_ptr_offset(0),
3257 btrfs_node_key_ptr_offset(mid),
3258 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3259 btrfs_set_header_nritems(split, c_nritems - mid);
3260 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003261 ret = 0;
3262
Chris Mason5f39d392007-10-15 16:14:19 -04003263 btrfs_mark_buffer_dirty(c);
3264 btrfs_mark_buffer_dirty(split);
3265
Jeff Mahoney143bede2012-03-01 14:56:26 +01003266 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003267 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003268
Chris Mason5de08d72007-02-24 06:24:44 -05003269 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003270 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003271 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003272 free_extent_buffer(c);
3273 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003274 path->slots[level + 1] += 1;
3275 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003276 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003277 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003278 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003279 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003280}
3281
Chris Mason74123bd2007-02-02 11:05:29 -05003282/*
3283 * how many bytes are required to store the items in a leaf. start
3284 * and nr indicate which items in the leaf to check. This totals up the
3285 * space used both by the item structs and the item data
3286 */
Chris Mason5f39d392007-10-15 16:14:19 -04003287static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003288{
Josef Bacik41be1f32012-10-15 13:43:18 -04003289 struct btrfs_item *start_item;
3290 struct btrfs_item *end_item;
3291 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003292 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003293 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003294 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003295
3296 if (!nr)
3297 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003298 btrfs_init_map_token(&token);
3299 start_item = btrfs_item_nr(l, start);
3300 end_item = btrfs_item_nr(l, end);
3301 data_len = btrfs_token_item_offset(l, start_item, &token) +
3302 btrfs_token_item_size(l, start_item, &token);
3303 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003304 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003305 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003306 return data_len;
3307}
3308
Chris Mason74123bd2007-02-02 11:05:29 -05003309/*
Chris Masond4dbff92007-04-04 14:08:15 -04003310 * The space between the end of the leaf items and
3311 * the start of the leaf data. IOW, how much room
3312 * the leaf has left for both items and data
3313 */
Chris Masond3977122009-01-05 21:25:51 -05003314noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003315 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003316{
Chris Mason5f39d392007-10-15 16:14:19 -04003317 int nritems = btrfs_header_nritems(leaf);
3318 int ret;
3319 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3320 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003321 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3322 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003323 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003324 leaf_space_used(leaf, 0, nritems), nritems);
3325 }
3326 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003327}
3328
Chris Mason99d8f832010-07-07 10:51:48 -04003329/*
3330 * min slot controls the lowest index we're willing to push to the
3331 * right. We'll push up to and including min_slot, but no lower
3332 */
Chris Mason44871b12009-03-13 10:04:31 -04003333static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3334 struct btrfs_root *root,
3335 struct btrfs_path *path,
3336 int data_size, int empty,
3337 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003338 int free_space, u32 left_nritems,
3339 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003340{
Chris Mason5f39d392007-10-15 16:14:19 -04003341 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003342 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003343 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003344 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003345 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003346 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003347 int push_space = 0;
3348 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003349 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003350 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003351 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003352 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003353 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003354
Chris Masoncfed81a2012-03-03 07:40:03 -05003355 btrfs_init_map_token(&token);
3356
Chris Mason34a38212007-11-07 13:31:03 -05003357 if (empty)
3358 nr = 0;
3359 else
Chris Mason99d8f832010-07-07 10:51:48 -04003360 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003361
Zheng Yan31840ae2008-09-23 13:14:14 -04003362 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003363 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003364
Chris Mason44871b12009-03-13 10:04:31 -04003365 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003366 i = left_nritems - 1;
3367 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003368 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003369
Zheng Yan31840ae2008-09-23 13:14:14 -04003370 if (!empty && push_items > 0) {
3371 if (path->slots[0] > i)
3372 break;
3373 if (path->slots[0] == i) {
3374 int space = btrfs_leaf_free_space(root, left);
3375 if (space + push_space * 2 > free_space)
3376 break;
3377 }
3378 }
3379
Chris Mason00ec4c52007-02-24 12:47:20 -05003380 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003381 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003382
Chris Masondb945352007-10-15 16:15:53 -04003383 this_item_size = btrfs_item_size(left, item);
3384 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003385 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003386
Chris Mason00ec4c52007-02-24 12:47:20 -05003387 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003388 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003389 if (i == 0)
3390 break;
3391 i--;
Chris Masondb945352007-10-15 16:15:53 -04003392 }
Chris Mason5f39d392007-10-15 16:14:19 -04003393
Chris Mason925baed2008-06-25 16:01:30 -04003394 if (push_items == 0)
3395 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003396
Julia Lawall6c1500f2012-11-03 20:30:18 +00003397 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003398
Chris Mason00ec4c52007-02-24 12:47:20 -05003399 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003400 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003401
Chris Mason5f39d392007-10-15 16:14:19 -04003402 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003403 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003404
Chris Mason00ec4c52007-02-24 12:47:20 -05003405 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003406 data_end = leaf_data_end(root, right);
3407 memmove_extent_buffer(right,
3408 btrfs_leaf_data(right) + data_end - push_space,
3409 btrfs_leaf_data(right) + data_end,
3410 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3411
Chris Mason00ec4c52007-02-24 12:47:20 -05003412 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003413 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003414 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3415 btrfs_leaf_data(left) + leaf_data_end(root, left),
3416 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003417
3418 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3419 btrfs_item_nr_offset(0),
3420 right_nritems * sizeof(struct btrfs_item));
3421
Chris Mason00ec4c52007-02-24 12:47:20 -05003422 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003423 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3424 btrfs_item_nr_offset(left_nritems - push_items),
3425 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003426
3427 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003428 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003429 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003430 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003431 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003432 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003433 push_space -= btrfs_token_item_size(right, item, &token);
3434 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003435 }
3436
Chris Mason7518a232007-03-12 12:01:18 -04003437 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003438 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003439
Chris Mason34a38212007-11-07 13:31:03 -05003440 if (left_nritems)
3441 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003442 else
3443 clean_tree_block(trans, root, left);
3444
Chris Mason5f39d392007-10-15 16:14:19 -04003445 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003446
Chris Mason5f39d392007-10-15 16:14:19 -04003447 btrfs_item_key(right, &disk_key, 0);
3448 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003449 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003450
Chris Mason00ec4c52007-02-24 12:47:20 -05003451 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003452 if (path->slots[0] >= left_nritems) {
3453 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003454 if (btrfs_header_nritems(path->nodes[0]) == 0)
3455 clean_tree_block(trans, root, path->nodes[0]);
3456 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003457 free_extent_buffer(path->nodes[0]);
3458 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003459 path->slots[1] += 1;
3460 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003461 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003462 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003463 }
3464 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003465
3466out_unlock:
3467 btrfs_tree_unlock(right);
3468 free_extent_buffer(right);
3469 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003470}
Chris Mason925baed2008-06-25 16:01:30 -04003471
Chris Mason00ec4c52007-02-24 12:47:20 -05003472/*
Chris Mason44871b12009-03-13 10:04:31 -04003473 * push some data in the path leaf to the right, trying to free up at
3474 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3475 *
3476 * returns 1 if the push failed because the other node didn't have enough
3477 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003478 *
3479 * this will push starting from min_slot to the end of the leaf. It won't
3480 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003481 */
3482static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003483 *root, struct btrfs_path *path,
3484 int min_data_size, int data_size,
3485 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003486{
3487 struct extent_buffer *left = path->nodes[0];
3488 struct extent_buffer *right;
3489 struct extent_buffer *upper;
3490 int slot;
3491 int free_space;
3492 u32 left_nritems;
3493 int ret;
3494
3495 if (!path->nodes[1])
3496 return 1;
3497
3498 slot = path->slots[1];
3499 upper = path->nodes[1];
3500 if (slot >= btrfs_header_nritems(upper) - 1)
3501 return 1;
3502
3503 btrfs_assert_tree_locked(path->nodes[1]);
3504
3505 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003506 if (right == NULL)
3507 return 1;
3508
Chris Mason44871b12009-03-13 10:04:31 -04003509 btrfs_tree_lock(right);
3510 btrfs_set_lock_blocking(right);
3511
3512 free_space = btrfs_leaf_free_space(root, right);
3513 if (free_space < data_size)
3514 goto out_unlock;
3515
3516 /* cow and double check */
3517 ret = btrfs_cow_block(trans, root, right, upper,
3518 slot + 1, &right);
3519 if (ret)
3520 goto out_unlock;
3521
3522 free_space = btrfs_leaf_free_space(root, right);
3523 if (free_space < data_size)
3524 goto out_unlock;
3525
3526 left_nritems = btrfs_header_nritems(left);
3527 if (left_nritems == 0)
3528 goto out_unlock;
3529
Chris Mason99d8f832010-07-07 10:51:48 -04003530 return __push_leaf_right(trans, root, path, min_data_size, empty,
3531 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003532out_unlock:
3533 btrfs_tree_unlock(right);
3534 free_extent_buffer(right);
3535 return 1;
3536}
3537
3538/*
Chris Mason74123bd2007-02-02 11:05:29 -05003539 * push some data in the path leaf to the left, trying to free up at
3540 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003541 *
3542 * max_slot can put a limit on how far into the leaf we'll push items. The
3543 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3544 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003545 */
Chris Mason44871b12009-03-13 10:04:31 -04003546static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3547 struct btrfs_root *root,
3548 struct btrfs_path *path, int data_size,
3549 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003550 int free_space, u32 right_nritems,
3551 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003552{
Chris Mason5f39d392007-10-15 16:14:19 -04003553 struct btrfs_disk_key disk_key;
3554 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003555 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003556 int push_space = 0;
3557 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003558 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003559 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003560 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003561 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003562 u32 this_item_size;
3563 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003564 struct btrfs_map_token token;
3565
3566 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003567
Chris Mason34a38212007-11-07 13:31:03 -05003568 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003569 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003570 else
Chris Mason99d8f832010-07-07 10:51:48 -04003571 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003572
3573 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003574 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003575
Zheng Yan31840ae2008-09-23 13:14:14 -04003576 if (!empty && push_items > 0) {
3577 if (path->slots[0] < i)
3578 break;
3579 if (path->slots[0] == i) {
3580 int space = btrfs_leaf_free_space(root, right);
3581 if (space + push_space * 2 > free_space)
3582 break;
3583 }
3584 }
3585
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003587 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003588
3589 this_item_size = btrfs_item_size(right, item);
3590 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003591 break;
Chris Masondb945352007-10-15 16:15:53 -04003592
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003594 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003595 }
Chris Masondb945352007-10-15 16:15:53 -04003596
Chris Masonbe0e5c02007-01-26 15:51:26 -05003597 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003598 ret = 1;
3599 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600 }
Chris Mason34a38212007-11-07 13:31:03 -05003601 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003602 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003603
Chris Masonbe0e5c02007-01-26 15:51:26 -05003604 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003605 copy_extent_buffer(left, right,
3606 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3607 btrfs_item_nr_offset(0),
3608 push_items * sizeof(struct btrfs_item));
3609
Chris Mason123abc82007-03-14 14:14:43 -04003610 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003611 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003612
3613 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003614 leaf_data_end(root, left) - push_space,
3615 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003616 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003617 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003618 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003619 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003620
Chris Masondb945352007-10-15 16:15:53 -04003621 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003622 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003623 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003624
Chris Mason5f39d392007-10-15 16:14:19 -04003625 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003626
Chris Masoncfed81a2012-03-03 07:40:03 -05003627 ioff = btrfs_token_item_offset(left, item, &token);
3628 btrfs_set_token_item_offset(left, item,
3629 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3630 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003631 }
Chris Mason5f39d392007-10-15 16:14:19 -04003632 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003633
3634 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003635 if (push_items > right_nritems)
3636 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003637 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003638
Chris Mason34a38212007-11-07 13:31:03 -05003639 if (push_items < right_nritems) {
3640 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3641 leaf_data_end(root, right);
3642 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3643 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3644 btrfs_leaf_data(right) +
3645 leaf_data_end(root, right), push_space);
3646
3647 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003648 btrfs_item_nr_offset(push_items),
3649 (btrfs_header_nritems(right) - push_items) *
3650 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003651 }
Yaneef1c492007-11-26 10:58:13 -05003652 right_nritems -= push_items;
3653 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003654 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003655 for (i = 0; i < right_nritems; i++) {
3656 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003657
Chris Masoncfed81a2012-03-03 07:40:03 -05003658 push_space = push_space - btrfs_token_item_size(right,
3659 item, &token);
3660 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003661 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003662
Chris Mason5f39d392007-10-15 16:14:19 -04003663 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003664 if (right_nritems)
3665 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003666 else
3667 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003668
Chris Mason5f39d392007-10-15 16:14:19 -04003669 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003670 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003671
3672 /* then fixup the leaf pointer in the path */
3673 if (path->slots[0] < push_items) {
3674 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003675 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003676 free_extent_buffer(path->nodes[0]);
3677 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003678 path->slots[1] -= 1;
3679 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003680 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003681 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003682 path->slots[0] -= push_items;
3683 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003684 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003685 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003686out:
3687 btrfs_tree_unlock(left);
3688 free_extent_buffer(left);
3689 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003690}
3691
Chris Mason74123bd2007-02-02 11:05:29 -05003692/*
Chris Mason44871b12009-03-13 10:04:31 -04003693 * push some data in the path leaf to the left, trying to free up at
3694 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003695 *
3696 * max_slot can put a limit on how far into the leaf we'll push items. The
3697 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3698 * items
Chris Mason44871b12009-03-13 10:04:31 -04003699 */
3700static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003701 *root, struct btrfs_path *path, int min_data_size,
3702 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003703{
3704 struct extent_buffer *right = path->nodes[0];
3705 struct extent_buffer *left;
3706 int slot;
3707 int free_space;
3708 u32 right_nritems;
3709 int ret = 0;
3710
3711 slot = path->slots[1];
3712 if (slot == 0)
3713 return 1;
3714 if (!path->nodes[1])
3715 return 1;
3716
3717 right_nritems = btrfs_header_nritems(right);
3718 if (right_nritems == 0)
3719 return 1;
3720
3721 btrfs_assert_tree_locked(path->nodes[1]);
3722
3723 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003724 if (left == NULL)
3725 return 1;
3726
Chris Mason44871b12009-03-13 10:04:31 -04003727 btrfs_tree_lock(left);
3728 btrfs_set_lock_blocking(left);
3729
3730 free_space = btrfs_leaf_free_space(root, left);
3731 if (free_space < data_size) {
3732 ret = 1;
3733 goto out;
3734 }
3735
3736 /* cow and double check */
3737 ret = btrfs_cow_block(trans, root, left,
3738 path->nodes[1], slot - 1, &left);
3739 if (ret) {
3740 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003741 if (ret == -ENOSPC)
3742 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003743 goto out;
3744 }
3745
3746 free_space = btrfs_leaf_free_space(root, left);
3747 if (free_space < data_size) {
3748 ret = 1;
3749 goto out;
3750 }
3751
Chris Mason99d8f832010-07-07 10:51:48 -04003752 return __push_leaf_left(trans, root, path, min_data_size,
3753 empty, left, free_space, right_nritems,
3754 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003755out:
3756 btrfs_tree_unlock(left);
3757 free_extent_buffer(left);
3758 return ret;
3759}
3760
3761/*
Chris Mason74123bd2007-02-02 11:05:29 -05003762 * split the path's leaf in two, making sure there is at least data_size
3763 * available for the resulting leaf level of the path.
3764 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003765static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3766 struct btrfs_root *root,
3767 struct btrfs_path *path,
3768 struct extent_buffer *l,
3769 struct extent_buffer *right,
3770 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003771{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003772 int data_copy_size;
3773 int rt_data_off;
3774 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003775 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003776 struct btrfs_map_token token;
3777
3778 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003779
Chris Mason5f39d392007-10-15 16:14:19 -04003780 nritems = nritems - mid;
3781 btrfs_set_header_nritems(right, nritems);
3782 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3783
3784 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3785 btrfs_item_nr_offset(mid),
3786 nritems * sizeof(struct btrfs_item));
3787
3788 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003789 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3790 data_copy_size, btrfs_leaf_data(l) +
3791 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003792
Chris Mason5f39d392007-10-15 16:14:19 -04003793 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3794 btrfs_item_end_nr(l, mid);
3795
3796 for (i = 0; i < nritems; i++) {
3797 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003798 u32 ioff;
3799
Chris Masoncfed81a2012-03-03 07:40:03 -05003800 ioff = btrfs_token_item_offset(right, item, &token);
3801 btrfs_set_token_item_offset(right, item,
3802 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003803 }
Chris Mason74123bd2007-02-02 11:05:29 -05003804
Chris Mason5f39d392007-10-15 16:14:19 -04003805 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003806 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003807 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003808 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003809
3810 btrfs_mark_buffer_dirty(right);
3811 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003812 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003813
Chris Masonbe0e5c02007-01-26 15:51:26 -05003814 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003815 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003816 free_extent_buffer(path->nodes[0]);
3817 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003818 path->slots[0] -= mid;
3819 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003820 } else {
3821 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003822 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003823 }
Chris Mason5f39d392007-10-15 16:14:19 -04003824
Chris Masoneb60cea2007-02-02 09:18:22 -05003825 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003826}
3827
3828/*
Chris Mason99d8f832010-07-07 10:51:48 -04003829 * double splits happen when we need to insert a big item in the middle
3830 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3831 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3832 * A B C
3833 *
3834 * We avoid this by trying to push the items on either side of our target
3835 * into the adjacent leaves. If all goes well we can avoid the double split
3836 * completely.
3837 */
3838static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3839 struct btrfs_root *root,
3840 struct btrfs_path *path,
3841 int data_size)
3842{
3843 int ret;
3844 int progress = 0;
3845 int slot;
3846 u32 nritems;
3847
3848 slot = path->slots[0];
3849
3850 /*
3851 * try to push all the items after our slot into the
3852 * right leaf
3853 */
3854 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3855 if (ret < 0)
3856 return ret;
3857
3858 if (ret == 0)
3859 progress++;
3860
3861 nritems = btrfs_header_nritems(path->nodes[0]);
3862 /*
3863 * our goal is to get our slot at the start or end of a leaf. If
3864 * we've done so we're done
3865 */
3866 if (path->slots[0] == 0 || path->slots[0] == nritems)
3867 return 0;
3868
3869 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3870 return 0;
3871
3872 /* try to push all the items before our slot into the next leaf */
3873 slot = path->slots[0];
3874 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3875 if (ret < 0)
3876 return ret;
3877
3878 if (ret == 0)
3879 progress++;
3880
3881 if (progress)
3882 return 0;
3883 return 1;
3884}
3885
3886/*
Chris Mason44871b12009-03-13 10:04:31 -04003887 * split the path's leaf in two, making sure there is at least data_size
3888 * available for the resulting leaf level of the path.
3889 *
3890 * returns 0 if all went well and < 0 on failure.
3891 */
3892static noinline int split_leaf(struct btrfs_trans_handle *trans,
3893 struct btrfs_root *root,
3894 struct btrfs_key *ins_key,
3895 struct btrfs_path *path, int data_size,
3896 int extend)
3897{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003898 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003899 struct extent_buffer *l;
3900 u32 nritems;
3901 int mid;
3902 int slot;
3903 struct extent_buffer *right;
3904 int ret = 0;
3905 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003906 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003907 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003908 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003909
Yan, Zhenga5719522009-09-24 09:17:31 -04003910 l = path->nodes[0];
3911 slot = path->slots[0];
3912 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3913 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3914 return -EOVERFLOW;
3915
Chris Mason44871b12009-03-13 10:04:31 -04003916 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003917 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003918 wret = push_leaf_right(trans, root, path, data_size,
3919 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003920 if (wret < 0)
3921 return wret;
3922 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003923 wret = push_leaf_left(trans, root, path, data_size,
3924 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003925 if (wret < 0)
3926 return wret;
3927 }
3928 l = path->nodes[0];
3929
3930 /* did the pushes work? */
3931 if (btrfs_leaf_free_space(root, l) >= data_size)
3932 return 0;
3933 }
3934
3935 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003936 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003937 if (ret)
3938 return ret;
3939 }
3940again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003941 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003942 l = path->nodes[0];
3943 slot = path->slots[0];
3944 nritems = btrfs_header_nritems(l);
3945 mid = (nritems + 1) / 2;
3946
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003947 if (mid <= slot) {
3948 if (nritems == 1 ||
3949 leaf_space_used(l, mid, nritems - mid) + data_size >
3950 BTRFS_LEAF_DATA_SIZE(root)) {
3951 if (slot >= nritems) {
3952 split = 0;
3953 } else {
3954 mid = slot;
3955 if (mid != nritems &&
3956 leaf_space_used(l, mid, nritems - mid) +
3957 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003958 if (data_size && !tried_avoid_double)
3959 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003960 split = 2;
3961 }
3962 }
3963 }
3964 } else {
3965 if (leaf_space_used(l, 0, mid) + data_size >
3966 BTRFS_LEAF_DATA_SIZE(root)) {
3967 if (!extend && data_size && slot == 0) {
3968 split = 0;
3969 } else if ((extend || !data_size) && slot == 0) {
3970 mid = 1;
3971 } else {
3972 mid = slot;
3973 if (mid != nritems &&
3974 leaf_space_used(l, mid, nritems - mid) +
3975 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003976 if (data_size && !tried_avoid_double)
3977 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003978 split = 2 ;
3979 }
3980 }
3981 }
3982 }
3983
3984 if (split == 0)
3985 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3986 else
3987 btrfs_item_key(l, &disk_key, mid);
3988
3989 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003990 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003991 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003992 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003993 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003994
3995 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003996
3997 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3998 btrfs_set_header_bytenr(right, right->start);
3999 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004000 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004001 btrfs_set_header_owner(right, root->root_key.objectid);
4002 btrfs_set_header_level(right, 0);
4003 write_extent_buffer(right, root->fs_info->fsid,
Geert Uytterhoevenfba6aa72013-08-20 13:20:14 +02004004 btrfs_header_fsid(right), BTRFS_FSID_SIZE);
Chris Mason44871b12009-03-13 10:04:31 -04004005
4006 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
Geert Uytterhoevenb308bc22013-08-20 13:20:15 +02004007 btrfs_header_chunk_tree_uuid(right),
Chris Mason44871b12009-03-13 10:04:31 -04004008 BTRFS_UUID_SIZE);
4009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004010 if (split == 0) {
4011 if (mid <= slot) {
4012 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004013 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004014 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004015 btrfs_tree_unlock(path->nodes[0]);
4016 free_extent_buffer(path->nodes[0]);
4017 path->nodes[0] = right;
4018 path->slots[0] = 0;
4019 path->slots[1] += 1;
4020 } else {
4021 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004022 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004023 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004024 btrfs_tree_unlock(path->nodes[0]);
4025 free_extent_buffer(path->nodes[0]);
4026 path->nodes[0] = right;
4027 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004028 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004029 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004030 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004031 btrfs_mark_buffer_dirty(right);
4032 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004033 }
4034
Jeff Mahoney143bede2012-03-01 14:56:26 +01004035 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004037 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004038 BUG_ON(num_doubles != 0);
4039 num_doubles++;
4040 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004041 }
Chris Mason44871b12009-03-13 10:04:31 -04004042
Jeff Mahoney143bede2012-03-01 14:56:26 +01004043 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004044
4045push_for_double:
4046 push_for_double_split(trans, root, path, data_size);
4047 tried_avoid_double = 1;
4048 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4049 return 0;
4050 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004051}
4052
Yan, Zhengad48fd752009-11-12 09:33:58 +00004053static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4054 struct btrfs_root *root,
4055 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004056{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004057 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004058 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004059 struct btrfs_file_extent_item *fi;
4060 u64 extent_len = 0;
4061 u32 item_size;
4062 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004063
4064 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004065 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4066
4067 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4068 key.type != BTRFS_EXTENT_CSUM_KEY);
4069
4070 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4071 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004072
4073 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004074 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4075 fi = btrfs_item_ptr(leaf, path->slots[0],
4076 struct btrfs_file_extent_item);
4077 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4078 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004079 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004080
Chris Mason459931e2008-12-10 09:10:46 -05004081 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004082 path->search_for_split = 1;
4083 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004084 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004085 if (ret < 0)
4086 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004087
Yan, Zhengad48fd752009-11-12 09:33:58 +00004088 ret = -EAGAIN;
4089 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004090 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004091 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4092 goto err;
4093
Chris Mason109f6ae2010-04-02 09:20:18 -04004094 /* the leaf has changed, it now has room. return now */
4095 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4096 goto err;
4097
Yan, Zhengad48fd752009-11-12 09:33:58 +00004098 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4099 fi = btrfs_item_ptr(leaf, path->slots[0],
4100 struct btrfs_file_extent_item);
4101 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4102 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004103 }
4104
Chris Masonb9473432009-03-13 11:00:37 -04004105 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004106 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004107 if (ret)
4108 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004109
Yan, Zhengad48fd752009-11-12 09:33:58 +00004110 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004111 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004112 return 0;
4113err:
4114 path->keep_locks = 0;
4115 return ret;
4116}
4117
4118static noinline int split_item(struct btrfs_trans_handle *trans,
4119 struct btrfs_root *root,
4120 struct btrfs_path *path,
4121 struct btrfs_key *new_key,
4122 unsigned long split_offset)
4123{
4124 struct extent_buffer *leaf;
4125 struct btrfs_item *item;
4126 struct btrfs_item *new_item;
4127 int slot;
4128 char *buf;
4129 u32 nritems;
4130 u32 item_size;
4131 u32 orig_offset;
4132 struct btrfs_disk_key disk_key;
4133
Chris Masonb9473432009-03-13 11:00:37 -04004134 leaf = path->nodes[0];
4135 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4136
Chris Masonb4ce94d2009-02-04 09:25:08 -05004137 btrfs_set_path_blocking(path);
4138
Chris Mason459931e2008-12-10 09:10:46 -05004139 item = btrfs_item_nr(leaf, path->slots[0]);
4140 orig_offset = btrfs_item_offset(leaf, item);
4141 item_size = btrfs_item_size(leaf, item);
4142
Chris Mason459931e2008-12-10 09:10:46 -05004143 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004144 if (!buf)
4145 return -ENOMEM;
4146
Chris Mason459931e2008-12-10 09:10:46 -05004147 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4148 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004149
Chris Mason459931e2008-12-10 09:10:46 -05004150 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004151 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004152 if (slot != nritems) {
4153 /* shift the items */
4154 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004155 btrfs_item_nr_offset(slot),
4156 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004157 }
4158
4159 btrfs_cpu_key_to_disk(&disk_key, new_key);
4160 btrfs_set_item_key(leaf, &disk_key, slot);
4161
4162 new_item = btrfs_item_nr(leaf, slot);
4163
4164 btrfs_set_item_offset(leaf, new_item, orig_offset);
4165 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4166
4167 btrfs_set_item_offset(leaf, item,
4168 orig_offset + item_size - split_offset);
4169 btrfs_set_item_size(leaf, item, split_offset);
4170
4171 btrfs_set_header_nritems(leaf, nritems + 1);
4172
4173 /* write the data for the start of the original item */
4174 write_extent_buffer(leaf, buf,
4175 btrfs_item_ptr_offset(leaf, path->slots[0]),
4176 split_offset);
4177
4178 /* write the data for the new item */
4179 write_extent_buffer(leaf, buf + split_offset,
4180 btrfs_item_ptr_offset(leaf, slot),
4181 item_size - split_offset);
4182 btrfs_mark_buffer_dirty(leaf);
4183
Yan, Zhengad48fd752009-11-12 09:33:58 +00004184 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004185 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004186 return 0;
4187}
4188
4189/*
4190 * This function splits a single item into two items,
4191 * giving 'new_key' to the new item and splitting the
4192 * old one at split_offset (from the start of the item).
4193 *
4194 * The path may be released by this operation. After
4195 * the split, the path is pointing to the old item. The
4196 * new item is going to be in the same node as the old one.
4197 *
4198 * Note, the item being split must be smaller enough to live alone on
4199 * a tree block with room for one extra struct btrfs_item
4200 *
4201 * This allows us to split the item in place, keeping a lock on the
4202 * leaf the entire time.
4203 */
4204int btrfs_split_item(struct btrfs_trans_handle *trans,
4205 struct btrfs_root *root,
4206 struct btrfs_path *path,
4207 struct btrfs_key *new_key,
4208 unsigned long split_offset)
4209{
4210 int ret;
4211 ret = setup_leaf_for_split(trans, root, path,
4212 sizeof(struct btrfs_item));
4213 if (ret)
4214 return ret;
4215
4216 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004217 return ret;
4218}
4219
4220/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004221 * This function duplicate a item, giving 'new_key' to the new item.
4222 * It guarantees both items live in the same tree leaf and the new item
4223 * is contiguous with the original item.
4224 *
4225 * This allows us to split file extent in place, keeping a lock on the
4226 * leaf the entire time.
4227 */
4228int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4229 struct btrfs_root *root,
4230 struct btrfs_path *path,
4231 struct btrfs_key *new_key)
4232{
4233 struct extent_buffer *leaf;
4234 int ret;
4235 u32 item_size;
4236
4237 leaf = path->nodes[0];
4238 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4239 ret = setup_leaf_for_split(trans, root, path,
4240 item_size + sizeof(struct btrfs_item));
4241 if (ret)
4242 return ret;
4243
4244 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004245 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004246 item_size, item_size +
4247 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004248 leaf = path->nodes[0];
4249 memcpy_extent_buffer(leaf,
4250 btrfs_item_ptr_offset(leaf, path->slots[0]),
4251 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4252 item_size);
4253 return 0;
4254}
4255
4256/*
Chris Masond352ac62008-09-29 15:18:18 -04004257 * make the item pointed to by the path smaller. new_size indicates
4258 * how small to make it, and from_end tells us if we just chop bytes
4259 * off the end of the item or if we shift the item to chop bytes off
4260 * the front.
4261 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004262void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004263 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004264{
Chris Masonb18c6682007-04-17 13:26:50 -04004265 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004266 struct extent_buffer *leaf;
4267 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004268 u32 nritems;
4269 unsigned int data_end;
4270 unsigned int old_data_start;
4271 unsigned int old_size;
4272 unsigned int size_diff;
4273 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004274 struct btrfs_map_token token;
4275
4276 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004277
Chris Mason5f39d392007-10-15 16:14:19 -04004278 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004279 slot = path->slots[0];
4280
4281 old_size = btrfs_item_size_nr(leaf, slot);
4282 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004283 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004284
Chris Mason5f39d392007-10-15 16:14:19 -04004285 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004286 data_end = leaf_data_end(root, leaf);
4287
Chris Mason5f39d392007-10-15 16:14:19 -04004288 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004289
Chris Masonb18c6682007-04-17 13:26:50 -04004290 size_diff = old_size - new_size;
4291
4292 BUG_ON(slot < 0);
4293 BUG_ON(slot >= nritems);
4294
4295 /*
4296 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4297 */
4298 /* first correct the data pointers */
4299 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004300 u32 ioff;
4301 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004302
Chris Masoncfed81a2012-03-03 07:40:03 -05004303 ioff = btrfs_token_item_offset(leaf, item, &token);
4304 btrfs_set_token_item_offset(leaf, item,
4305 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004306 }
Chris Masondb945352007-10-15 16:15:53 -04004307
Chris Masonb18c6682007-04-17 13:26:50 -04004308 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004309 if (from_end) {
4310 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4311 data_end + size_diff, btrfs_leaf_data(leaf) +
4312 data_end, old_data_start + new_size - data_end);
4313 } else {
4314 struct btrfs_disk_key disk_key;
4315 u64 offset;
4316
4317 btrfs_item_key(leaf, &disk_key, slot);
4318
4319 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4320 unsigned long ptr;
4321 struct btrfs_file_extent_item *fi;
4322
4323 fi = btrfs_item_ptr(leaf, slot,
4324 struct btrfs_file_extent_item);
4325 fi = (struct btrfs_file_extent_item *)(
4326 (unsigned long)fi - size_diff);
4327
4328 if (btrfs_file_extent_type(leaf, fi) ==
4329 BTRFS_FILE_EXTENT_INLINE) {
4330 ptr = btrfs_item_ptr_offset(leaf, slot);
4331 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004332 (unsigned long)fi,
4333 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004334 disk_bytenr));
4335 }
4336 }
4337
4338 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4339 data_end + size_diff, btrfs_leaf_data(leaf) +
4340 data_end, old_data_start - data_end);
4341
4342 offset = btrfs_disk_key_offset(&disk_key);
4343 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4344 btrfs_set_item_key(leaf, &disk_key, slot);
4345 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004346 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004347 }
Chris Mason5f39d392007-10-15 16:14:19 -04004348
4349 item = btrfs_item_nr(leaf, slot);
4350 btrfs_set_item_size(leaf, item, new_size);
4351 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004352
Chris Mason5f39d392007-10-15 16:14:19 -04004353 if (btrfs_leaf_free_space(root, leaf) < 0) {
4354 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004355 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004356 }
Chris Masonb18c6682007-04-17 13:26:50 -04004357}
4358
Chris Masond352ac62008-09-29 15:18:18 -04004359/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004360 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004361 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004362void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004363 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004364{
Chris Mason6567e832007-04-16 09:22:45 -04004365 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004366 struct extent_buffer *leaf;
4367 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004368 u32 nritems;
4369 unsigned int data_end;
4370 unsigned int old_data;
4371 unsigned int old_size;
4372 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004373 struct btrfs_map_token token;
4374
4375 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004376
Chris Mason5f39d392007-10-15 16:14:19 -04004377 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004378
Chris Mason5f39d392007-10-15 16:14:19 -04004379 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004380 data_end = leaf_data_end(root, leaf);
4381
Chris Mason5f39d392007-10-15 16:14:19 -04004382 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4383 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004384 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004385 }
Chris Mason6567e832007-04-16 09:22:45 -04004386 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004387 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004388
4389 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004390 if (slot >= nritems) {
4391 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004392 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4393 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004394 BUG_ON(1);
4395 }
Chris Mason6567e832007-04-16 09:22:45 -04004396
4397 /*
4398 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4399 */
4400 /* first correct the data pointers */
4401 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004402 u32 ioff;
4403 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004404
Chris Masoncfed81a2012-03-03 07:40:03 -05004405 ioff = btrfs_token_item_offset(leaf, item, &token);
4406 btrfs_set_token_item_offset(leaf, item,
4407 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004408 }
Chris Mason5f39d392007-10-15 16:14:19 -04004409
Chris Mason6567e832007-04-16 09:22:45 -04004410 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004411 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004412 data_end - data_size, btrfs_leaf_data(leaf) +
4413 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004414
Chris Mason6567e832007-04-16 09:22:45 -04004415 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004416 old_size = btrfs_item_size_nr(leaf, slot);
4417 item = btrfs_item_nr(leaf, slot);
4418 btrfs_set_item_size(leaf, item, old_size + data_size);
4419 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004420
Chris Mason5f39d392007-10-15 16:14:19 -04004421 if (btrfs_leaf_free_space(root, leaf) < 0) {
4422 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004423 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004424 }
Chris Mason6567e832007-04-16 09:22:45 -04004425}
4426
Chris Mason74123bd2007-02-02 11:05:29 -05004427/*
Chris Mason44871b12009-03-13 10:04:31 -04004428 * this is a helper for btrfs_insert_empty_items, the main goal here is
4429 * to save stack depth by doing the bulk of the work in a function
4430 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004431 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004432void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004433 struct btrfs_key *cpu_key, u32 *data_size,
4434 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004435{
Chris Mason5f39d392007-10-15 16:14:19 -04004436 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004437 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004438 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004439 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004440 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004441 struct extent_buffer *leaf;
4442 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004443 struct btrfs_map_token token;
4444
4445 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004446
Chris Mason5f39d392007-10-15 16:14:19 -04004447 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004448 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004449
Chris Mason5f39d392007-10-15 16:14:19 -04004450 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004451 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004452
Chris Masonf25956c2008-09-12 15:32:53 -04004453 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004454 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004455 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004456 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004457 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004458 }
Chris Mason5f39d392007-10-15 16:14:19 -04004459
Chris Masonbe0e5c02007-01-26 15:51:26 -05004460 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004461 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004462
Chris Mason5f39d392007-10-15 16:14:19 -04004463 if (old_data < data_end) {
4464 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004465 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004466 slot, old_data, data_end);
4467 BUG_ON(1);
4468 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004469 /*
4470 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4471 */
4472 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004473 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004474 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004475
Chris Mason5f39d392007-10-15 16:14:19 -04004476 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004477 ioff = btrfs_token_item_offset(leaf, item, &token);
4478 btrfs_set_token_item_offset(leaf, item,
4479 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004480 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004481 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004482 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004483 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004484 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004485
4486 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004487 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004488 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004489 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004490 data_end = old_data;
4491 }
Chris Mason5f39d392007-10-15 16:14:19 -04004492
Chris Mason62e27492007-03-15 12:56:47 -04004493 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004494 for (i = 0; i < nr; i++) {
4495 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4496 btrfs_set_item_key(leaf, &disk_key, slot + i);
4497 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004498 btrfs_set_token_item_offset(leaf, item,
4499 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004500 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004501 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004502 }
Chris Mason44871b12009-03-13 10:04:31 -04004503
Chris Mason9c583092008-01-29 15:15:18 -05004504 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004505
Chris Mason5a01a2e2008-01-30 11:43:54 -05004506 if (slot == 0) {
4507 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004508 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004509 }
Chris Masonb9473432009-03-13 11:00:37 -04004510 btrfs_unlock_up_safe(path, 1);
4511 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004512
Chris Mason5f39d392007-10-15 16:14:19 -04004513 if (btrfs_leaf_free_space(root, leaf) < 0) {
4514 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004515 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004516 }
Chris Mason44871b12009-03-13 10:04:31 -04004517}
4518
4519/*
4520 * Given a key and some data, insert items into the tree.
4521 * This does all the path init required, making room in the tree if needed.
4522 */
4523int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4524 struct btrfs_root *root,
4525 struct btrfs_path *path,
4526 struct btrfs_key *cpu_key, u32 *data_size,
4527 int nr)
4528{
Chris Mason44871b12009-03-13 10:04:31 -04004529 int ret = 0;
4530 int slot;
4531 int i;
4532 u32 total_size = 0;
4533 u32 total_data = 0;
4534
4535 for (i = 0; i < nr; i++)
4536 total_data += data_size[i];
4537
4538 total_size = total_data + (nr * sizeof(struct btrfs_item));
4539 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4540 if (ret == 0)
4541 return -EEXIST;
4542 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004543 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004544
Chris Mason44871b12009-03-13 10:04:31 -04004545 slot = path->slots[0];
4546 BUG_ON(slot < 0);
4547
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004548 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004549 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004550 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004551}
4552
4553/*
4554 * Given a key and some data, insert an item into the tree.
4555 * This does all the path init required, making room in the tree if needed.
4556 */
Chris Masone089f052007-03-16 16:20:31 -04004557int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4558 *root, struct btrfs_key *cpu_key, void *data, u32
4559 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004560{
4561 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004562 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004563 struct extent_buffer *leaf;
4564 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004565
Chris Mason2c90e5d2007-04-02 10:50:19 -04004566 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004567 if (!path)
4568 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004569 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004570 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004571 leaf = path->nodes[0];
4572 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4573 write_extent_buffer(leaf, data, ptr, data_size);
4574 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004575 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004576 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004577 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004578}
4579
Chris Mason74123bd2007-02-02 11:05:29 -05004580/*
Chris Mason5de08d72007-02-24 06:24:44 -05004581 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004582 *
Chris Masond352ac62008-09-29 15:18:18 -04004583 * the tree should have been previously balanced so the deletion does not
4584 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004585 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004586static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4587 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004588{
Chris Mason5f39d392007-10-15 16:14:19 -04004589 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004590 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004591 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004592
Chris Mason5f39d392007-10-15 16:14:19 -04004593 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004594 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004595 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004596 tree_mod_log_eb_move(root->fs_info, parent, slot,
4597 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004598 memmove_extent_buffer(parent,
4599 btrfs_node_key_ptr_offset(slot),
4600 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004601 sizeof(struct btrfs_key_ptr) *
4602 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004603 } else if (level) {
4604 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
Josef Bacikc8cc6342013-07-01 16:18:19 -04004605 MOD_LOG_KEY_REMOVE, GFP_NOFS);
Chris Mason57ba86c2012-12-18 19:35:32 -05004606 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004607 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004608
Chris Mason7518a232007-03-12 12:01:18 -04004609 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004610 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004611 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004612 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004613 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004614 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004615 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004616 struct btrfs_disk_key disk_key;
4617
4618 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004619 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004620 }
Chris Masond6025572007-03-30 14:27:56 -04004621 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004622}
4623
Chris Mason74123bd2007-02-02 11:05:29 -05004624/*
Chris Mason323ac952008-10-01 19:05:46 -04004625 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004626 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004627 *
4628 * This deletes the pointer in path->nodes[1] and frees the leaf
4629 * block extent. zero is returned if it all worked out, < 0 otherwise.
4630 *
4631 * The path must have already been setup for deleting the leaf, including
4632 * all the proper balancing. path->nodes[1] must be locked.
4633 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004634static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4635 struct btrfs_root *root,
4636 struct btrfs_path *path,
4637 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004638{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004639 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004640 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004641
Chris Mason4d081c42009-02-04 09:31:28 -05004642 /*
4643 * btrfs_free_extent is expensive, we want to make sure we
4644 * aren't holding any locks when we call it
4645 */
4646 btrfs_unlock_up_safe(path, 0);
4647
Yan, Zhengf0486c62010-05-16 10:46:25 -04004648 root_sub_used(root, leaf->len);
4649
Josef Bacik3083ee22012-03-09 16:01:49 -05004650 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004651 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004652 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004653}
4654/*
Chris Mason74123bd2007-02-02 11:05:29 -05004655 * delete the item at the leaf level in path. If that empties
4656 * the leaf, remove it from the tree
4657 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004658int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4659 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004660{
Chris Mason5f39d392007-10-15 16:14:19 -04004661 struct extent_buffer *leaf;
4662 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004663 int last_off;
4664 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004665 int ret = 0;
4666 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004667 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004668 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004669 struct btrfs_map_token token;
4670
4671 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004672
Chris Mason5f39d392007-10-15 16:14:19 -04004673 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004674 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4675
4676 for (i = 0; i < nr; i++)
4677 dsize += btrfs_item_size_nr(leaf, slot + i);
4678
Chris Mason5f39d392007-10-15 16:14:19 -04004679 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004680
Chris Mason85e21ba2008-01-29 15:11:36 -05004681 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004682 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004683
4684 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004685 data_end + dsize,
4686 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004687 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004688
Chris Mason85e21ba2008-01-29 15:11:36 -05004689 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004690 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004691
Chris Mason5f39d392007-10-15 16:14:19 -04004692 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004693 ioff = btrfs_token_item_offset(leaf, item, &token);
4694 btrfs_set_token_item_offset(leaf, item,
4695 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004696 }
Chris Masondb945352007-10-15 16:15:53 -04004697
Chris Mason5f39d392007-10-15 16:14:19 -04004698 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004699 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004700 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004701 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004702 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004703 btrfs_set_header_nritems(leaf, nritems - nr);
4704 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004705
Chris Mason74123bd2007-02-02 11:05:29 -05004706 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004707 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004708 if (leaf == root->node) {
4709 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004710 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004711 btrfs_set_path_blocking(path);
4712 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004713 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004714 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004716 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004717 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004718 struct btrfs_disk_key disk_key;
4719
4720 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004721 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004722 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004723
Chris Mason74123bd2007-02-02 11:05:29 -05004724 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004725 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004726 /* push_leaf_left fixes the path.
4727 * make sure the path still points to our leaf
4728 * for possible call to del_ptr below
4729 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004730 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004731 extent_buffer_get(leaf);
4732
Chris Masonb9473432009-03-13 11:00:37 -04004733 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004734 wret = push_leaf_left(trans, root, path, 1, 1,
4735 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004736 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004737 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004738
4739 if (path->nodes[0] == leaf &&
4740 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004741 wret = push_leaf_right(trans, root, path, 1,
4742 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004743 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004744 ret = wret;
4745 }
Chris Mason5f39d392007-10-15 16:14:19 -04004746
4747 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004748 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004749 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004750 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004751 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004752 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004753 /* if we're still in the path, make sure
4754 * we're dirty. Otherwise, one of the
4755 * push_leaf functions must have already
4756 * dirtied this buffer
4757 */
4758 if (path->nodes[0] == leaf)
4759 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004760 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004761 }
Chris Masond5719762007-03-23 10:01:08 -04004762 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004763 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004764 }
4765 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004766 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004767}
4768
Chris Mason97571fd2007-02-24 13:39:08 -05004769/*
Chris Mason925baed2008-06-25 16:01:30 -04004770 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004771 * returns 0 if it found something or 1 if there are no lesser leaves.
4772 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004773 *
4774 * This may release the path, and so you may lose any locks held at the
4775 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004776 */
Stefan Behrens35a36212013-08-14 18:12:25 +02004777static int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Mason7bb86312007-12-11 09:25:06 -05004778{
Chris Mason925baed2008-06-25 16:01:30 -04004779 struct btrfs_key key;
4780 struct btrfs_disk_key found_key;
4781 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004782
Chris Mason925baed2008-06-25 16:01:30 -04004783 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004784
Chris Mason925baed2008-06-25 16:01:30 -04004785 if (key.offset > 0)
4786 key.offset--;
4787 else if (key.type > 0)
4788 key.type--;
4789 else if (key.objectid > 0)
4790 key.objectid--;
4791 else
4792 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004793
David Sterbab3b4aa72011-04-21 01:20:15 +02004794 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004795 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4796 if (ret < 0)
4797 return ret;
4798 btrfs_item_key(path->nodes[0], &found_key, 0);
4799 ret = comp_keys(&found_key, &key);
4800 if (ret < 0)
4801 return 0;
4802 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004803}
4804
Chris Mason3f157a22008-06-25 16:01:31 -04004805/*
4806 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004807 * for nodes or leaves that are have a minimum transaction id.
4808 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004809 *
4810 * This does not cow, but it does stuff the starting key it finds back
4811 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4812 * key and get a writable path.
4813 *
4814 * This does lock as it descends, and path->keep_locks should be set
4815 * to 1 by the caller.
4816 *
4817 * This honors path->lowest_level to prevent descent past a given level
4818 * of the tree.
4819 *
Chris Masond352ac62008-09-29 15:18:18 -04004820 * min_trans indicates the oldest transaction that you are interested
4821 * in walking through. Any nodes or leaves older than min_trans are
4822 * skipped over (without reading them).
4823 *
Chris Mason3f157a22008-06-25 16:01:31 -04004824 * returns zero if something useful was found, < 0 on error and 1 if there
4825 * was nothing in the tree that matched the search criteria.
4826 */
4827int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004828 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004829 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004830 u64 min_trans)
4831{
4832 struct extent_buffer *cur;
4833 struct btrfs_key found_key;
4834 int slot;
Yan96524802008-07-24 12:19:49 -04004835 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004836 u32 nritems;
4837 int level;
4838 int ret = 1;
4839
Chris Mason934d3752008-12-08 16:43:10 -05004840 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004841again:
Chris Masonbd681512011-07-16 15:23:14 -04004842 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004843 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004844 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004845 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004846 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004847
4848 if (btrfs_header_generation(cur) < min_trans) {
4849 ret = 1;
4850 goto out;
4851 }
Chris Masond3977122009-01-05 21:25:51 -05004852 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004853 nritems = btrfs_header_nritems(cur);
4854 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004855 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004856
Chris Mason323ac952008-10-01 19:05:46 -04004857 /* at the lowest level, we're done, setup the path and exit */
4858 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004859 if (slot >= nritems)
4860 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004861 ret = 0;
4862 path->slots[level] = slot;
4863 btrfs_item_key_to_cpu(cur, &found_key, slot);
4864 goto out;
4865 }
Yan96524802008-07-24 12:19:49 -04004866 if (sret && slot > 0)
4867 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004868 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004869 * check this node pointer against the min_trans parameters.
4870 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004871 */
Chris Masond3977122009-01-05 21:25:51 -05004872 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004873 u64 blockptr;
4874 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004875
Chris Mason3f157a22008-06-25 16:01:31 -04004876 blockptr = btrfs_node_blockptr(cur, slot);
4877 gen = btrfs_node_ptr_generation(cur, slot);
4878 if (gen < min_trans) {
4879 slot++;
4880 continue;
4881 }
Eric Sandeende78b512013-01-31 18:21:12 +00004882 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004883 }
Chris Masone02119d2008-09-05 16:13:11 -04004884find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004885 /*
4886 * we didn't find a candidate key in this node, walk forward
4887 * and find another one
4888 */
4889 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004890 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004891 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004892 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004893 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004894 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004895 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004896 goto again;
4897 } else {
4898 goto out;
4899 }
4900 }
4901 /* save our key for returning back */
4902 btrfs_node_key_to_cpu(cur, &found_key, slot);
4903 path->slots[level] = slot;
4904 if (level == path->lowest_level) {
4905 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004906 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004907 goto out;
4908 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004909 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004910 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004911 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004912
Chris Masonbd681512011-07-16 15:23:14 -04004913 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004914
Chris Masonbd681512011-07-16 15:23:14 -04004915 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004916 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004917 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004918 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004919 }
4920out:
4921 if (ret == 0)
4922 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004923 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004924 return ret;
4925}
4926
Alexander Block70698302012-06-05 21:07:48 +02004927static void tree_move_down(struct btrfs_root *root,
4928 struct btrfs_path *path,
4929 int *level, int root_level)
4930{
Chris Mason74dd17f2012-08-07 16:25:13 -04004931 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004932 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4933 path->slots[*level]);
4934 path->slots[*level - 1] = 0;
4935 (*level)--;
4936}
4937
4938static int tree_move_next_or_upnext(struct btrfs_root *root,
4939 struct btrfs_path *path,
4940 int *level, int root_level)
4941{
4942 int ret = 0;
4943 int nritems;
4944 nritems = btrfs_header_nritems(path->nodes[*level]);
4945
4946 path->slots[*level]++;
4947
Chris Mason74dd17f2012-08-07 16:25:13 -04004948 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004949 if (*level == root_level)
4950 return -1;
4951
4952 /* move upnext */
4953 path->slots[*level] = 0;
4954 free_extent_buffer(path->nodes[*level]);
4955 path->nodes[*level] = NULL;
4956 (*level)++;
4957 path->slots[*level]++;
4958
4959 nritems = btrfs_header_nritems(path->nodes[*level]);
4960 ret = 1;
4961 }
4962 return ret;
4963}
4964
4965/*
4966 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
4967 * or down.
4968 */
4969static int tree_advance(struct btrfs_root *root,
4970 struct btrfs_path *path,
4971 int *level, int root_level,
4972 int allow_down,
4973 struct btrfs_key *key)
4974{
4975 int ret;
4976
4977 if (*level == 0 || !allow_down) {
4978 ret = tree_move_next_or_upnext(root, path, level, root_level);
4979 } else {
4980 tree_move_down(root, path, level, root_level);
4981 ret = 0;
4982 }
4983 if (ret >= 0) {
4984 if (*level == 0)
4985 btrfs_item_key_to_cpu(path->nodes[*level], key,
4986 path->slots[*level]);
4987 else
4988 btrfs_node_key_to_cpu(path->nodes[*level], key,
4989 path->slots[*level]);
4990 }
4991 return ret;
4992}
4993
4994static int tree_compare_item(struct btrfs_root *left_root,
4995 struct btrfs_path *left_path,
4996 struct btrfs_path *right_path,
4997 char *tmp_buf)
4998{
4999 int cmp;
5000 int len1, len2;
5001 unsigned long off1, off2;
5002
5003 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5004 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5005 if (len1 != len2)
5006 return 1;
5007
5008 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5009 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5010 right_path->slots[0]);
5011
5012 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5013
5014 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5015 if (cmp)
5016 return 1;
5017 return 0;
5018}
5019
5020#define ADVANCE 1
5021#define ADVANCE_ONLY_NEXT -1
5022
5023/*
5024 * This function compares two trees and calls the provided callback for
5025 * every changed/new/deleted item it finds.
5026 * If shared tree blocks are encountered, whole subtrees are skipped, making
5027 * the compare pretty fast on snapshotted subvolumes.
5028 *
5029 * This currently works on commit roots only. As commit roots are read only,
5030 * we don't do any locking. The commit roots are protected with transactions.
5031 * Transactions are ended and rejoined when a commit is tried in between.
5032 *
5033 * This function checks for modifications done to the trees while comparing.
5034 * If it detects a change, it aborts immediately.
5035 */
5036int btrfs_compare_trees(struct btrfs_root *left_root,
5037 struct btrfs_root *right_root,
5038 btrfs_changed_cb_t changed_cb, void *ctx)
5039{
5040 int ret;
5041 int cmp;
5042 struct btrfs_trans_handle *trans = NULL;
5043 struct btrfs_path *left_path = NULL;
5044 struct btrfs_path *right_path = NULL;
5045 struct btrfs_key left_key;
5046 struct btrfs_key right_key;
5047 char *tmp_buf = NULL;
5048 int left_root_level;
5049 int right_root_level;
5050 int left_level;
5051 int right_level;
5052 int left_end_reached;
5053 int right_end_reached;
5054 int advance_left;
5055 int advance_right;
5056 u64 left_blockptr;
5057 u64 right_blockptr;
5058 u64 left_start_ctransid;
5059 u64 right_start_ctransid;
5060 u64 ctransid;
5061
5062 left_path = btrfs_alloc_path();
5063 if (!left_path) {
5064 ret = -ENOMEM;
5065 goto out;
5066 }
5067 right_path = btrfs_alloc_path();
5068 if (!right_path) {
5069 ret = -ENOMEM;
5070 goto out;
5071 }
5072
5073 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5074 if (!tmp_buf) {
5075 ret = -ENOMEM;
5076 goto out;
5077 }
5078
5079 left_path->search_commit_root = 1;
5080 left_path->skip_locking = 1;
5081 right_path->search_commit_root = 1;
5082 right_path->skip_locking = 1;
5083
Anand Jain5f3ab902012-12-07 09:28:54 +00005084 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005085 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005086 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005087
Anand Jain5f3ab902012-12-07 09:28:54 +00005088 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005089 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005090 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005091
5092 trans = btrfs_join_transaction(left_root);
5093 if (IS_ERR(trans)) {
5094 ret = PTR_ERR(trans);
5095 trans = NULL;
5096 goto out;
5097 }
5098
5099 /*
5100 * Strategy: Go to the first items of both trees. Then do
5101 *
5102 * If both trees are at level 0
5103 * Compare keys of current items
5104 * If left < right treat left item as new, advance left tree
5105 * and repeat
5106 * If left > right treat right item as deleted, advance right tree
5107 * and repeat
5108 * If left == right do deep compare of items, treat as changed if
5109 * needed, advance both trees and repeat
5110 * If both trees are at the same level but not at level 0
5111 * Compare keys of current nodes/leafs
5112 * If left < right advance left tree and repeat
5113 * If left > right advance right tree and repeat
5114 * If left == right compare blockptrs of the next nodes/leafs
5115 * If they match advance both trees but stay at the same level
5116 * and repeat
5117 * If they don't match advance both trees while allowing to go
5118 * deeper and repeat
5119 * If tree levels are different
5120 * Advance the tree that needs it and repeat
5121 *
5122 * Advancing a tree means:
5123 * If we are at level 0, try to go to the next slot. If that's not
5124 * possible, go one level up and repeat. Stop when we found a level
5125 * where we could go to the next slot. We may at this point be on a
5126 * node or a leaf.
5127 *
5128 * If we are not at level 0 and not on shared tree blocks, go one
5129 * level deeper.
5130 *
5131 * If we are not at level 0 and on shared tree blocks, go one slot to
5132 * the right if possible or go up and right.
5133 */
5134
5135 left_level = btrfs_header_level(left_root->commit_root);
5136 left_root_level = left_level;
5137 left_path->nodes[left_level] = left_root->commit_root;
5138 extent_buffer_get(left_path->nodes[left_level]);
5139
5140 right_level = btrfs_header_level(right_root->commit_root);
5141 right_root_level = right_level;
5142 right_path->nodes[right_level] = right_root->commit_root;
5143 extent_buffer_get(right_path->nodes[right_level]);
5144
5145 if (left_level == 0)
5146 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5147 &left_key, left_path->slots[left_level]);
5148 else
5149 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5150 &left_key, left_path->slots[left_level]);
5151 if (right_level == 0)
5152 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5153 &right_key, right_path->slots[right_level]);
5154 else
5155 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5156 &right_key, right_path->slots[right_level]);
5157
5158 left_end_reached = right_end_reached = 0;
5159 advance_left = advance_right = 0;
5160
5161 while (1) {
5162 /*
5163 * We need to make sure the transaction does not get committed
5164 * while we do anything on commit roots. This means, we need to
5165 * join and leave transactions for every item that we process.
5166 */
5167 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5168 btrfs_release_path(left_path);
5169 btrfs_release_path(right_path);
5170
5171 ret = btrfs_end_transaction(trans, left_root);
5172 trans = NULL;
5173 if (ret < 0)
5174 goto out;
5175 }
5176 /* now rejoin the transaction */
5177 if (!trans) {
5178 trans = btrfs_join_transaction(left_root);
5179 if (IS_ERR(trans)) {
5180 ret = PTR_ERR(trans);
5181 trans = NULL;
5182 goto out;
5183 }
5184
Anand Jain5f3ab902012-12-07 09:28:54 +00005185 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005186 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005187 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005188 if (ctransid != left_start_ctransid)
5189 left_start_ctransid = 0;
5190
Anand Jain5f3ab902012-12-07 09:28:54 +00005191 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005192 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005193 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005194 if (ctransid != right_start_ctransid)
5195 right_start_ctransid = 0;
5196
5197 if (!left_start_ctransid || !right_start_ctransid) {
5198 WARN(1, KERN_WARNING
5199 "btrfs: btrfs_compare_tree detected "
5200 "a change in one of the trees while "
5201 "iterating. This is probably a "
5202 "bug.\n");
5203 ret = -EIO;
5204 goto out;
5205 }
5206
5207 /*
5208 * the commit root may have changed, so start again
5209 * where we stopped
5210 */
5211 left_path->lowest_level = left_level;
5212 right_path->lowest_level = right_level;
5213 ret = btrfs_search_slot(NULL, left_root,
5214 &left_key, left_path, 0, 0);
5215 if (ret < 0)
5216 goto out;
5217 ret = btrfs_search_slot(NULL, right_root,
5218 &right_key, right_path, 0, 0);
5219 if (ret < 0)
5220 goto out;
5221 }
5222
5223 if (advance_left && !left_end_reached) {
5224 ret = tree_advance(left_root, left_path, &left_level,
5225 left_root_level,
5226 advance_left != ADVANCE_ONLY_NEXT,
5227 &left_key);
5228 if (ret < 0)
5229 left_end_reached = ADVANCE;
5230 advance_left = 0;
5231 }
5232 if (advance_right && !right_end_reached) {
5233 ret = tree_advance(right_root, right_path, &right_level,
5234 right_root_level,
5235 advance_right != ADVANCE_ONLY_NEXT,
5236 &right_key);
5237 if (ret < 0)
5238 right_end_reached = ADVANCE;
5239 advance_right = 0;
5240 }
5241
5242 if (left_end_reached && right_end_reached) {
5243 ret = 0;
5244 goto out;
5245 } else if (left_end_reached) {
5246 if (right_level == 0) {
5247 ret = changed_cb(left_root, right_root,
5248 left_path, right_path,
5249 &right_key,
5250 BTRFS_COMPARE_TREE_DELETED,
5251 ctx);
5252 if (ret < 0)
5253 goto out;
5254 }
5255 advance_right = ADVANCE;
5256 continue;
5257 } else if (right_end_reached) {
5258 if (left_level == 0) {
5259 ret = changed_cb(left_root, right_root,
5260 left_path, right_path,
5261 &left_key,
5262 BTRFS_COMPARE_TREE_NEW,
5263 ctx);
5264 if (ret < 0)
5265 goto out;
5266 }
5267 advance_left = ADVANCE;
5268 continue;
5269 }
5270
5271 if (left_level == 0 && right_level == 0) {
5272 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5273 if (cmp < 0) {
5274 ret = changed_cb(left_root, right_root,
5275 left_path, right_path,
5276 &left_key,
5277 BTRFS_COMPARE_TREE_NEW,
5278 ctx);
5279 if (ret < 0)
5280 goto out;
5281 advance_left = ADVANCE;
5282 } else if (cmp > 0) {
5283 ret = changed_cb(left_root, right_root,
5284 left_path, right_path,
5285 &right_key,
5286 BTRFS_COMPARE_TREE_DELETED,
5287 ctx);
5288 if (ret < 0)
5289 goto out;
5290 advance_right = ADVANCE;
5291 } else {
Josef Bacikba5e8f22013-08-16 16:52:55 -04005292 enum btrfs_compare_tree_result cmp;
5293
Chris Mason74dd17f2012-08-07 16:25:13 -04005294 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005295 ret = tree_compare_item(left_root, left_path,
5296 right_path, tmp_buf);
Josef Bacikba5e8f22013-08-16 16:52:55 -04005297 if (ret)
5298 cmp = BTRFS_COMPARE_TREE_CHANGED;
5299 else
5300 cmp = BTRFS_COMPARE_TREE_SAME;
5301 ret = changed_cb(left_root, right_root,
5302 left_path, right_path,
5303 &left_key, cmp, ctx);
5304 if (ret < 0)
5305 goto out;
Alexander Block70698302012-06-05 21:07:48 +02005306 advance_left = ADVANCE;
5307 advance_right = ADVANCE;
5308 }
5309 } else if (left_level == right_level) {
5310 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5311 if (cmp < 0) {
5312 advance_left = ADVANCE;
5313 } else if (cmp > 0) {
5314 advance_right = ADVANCE;
5315 } else {
5316 left_blockptr = btrfs_node_blockptr(
5317 left_path->nodes[left_level],
5318 left_path->slots[left_level]);
5319 right_blockptr = btrfs_node_blockptr(
5320 right_path->nodes[right_level],
5321 right_path->slots[right_level]);
5322 if (left_blockptr == right_blockptr) {
5323 /*
5324 * As we're on a shared block, don't
5325 * allow to go deeper.
5326 */
5327 advance_left = ADVANCE_ONLY_NEXT;
5328 advance_right = ADVANCE_ONLY_NEXT;
5329 } else {
5330 advance_left = ADVANCE;
5331 advance_right = ADVANCE;
5332 }
5333 }
5334 } else if (left_level < right_level) {
5335 advance_right = ADVANCE;
5336 } else {
5337 advance_left = ADVANCE;
5338 }
5339 }
5340
5341out:
5342 btrfs_free_path(left_path);
5343 btrfs_free_path(right_path);
5344 kfree(tmp_buf);
5345
5346 if (trans) {
5347 if (!ret)
5348 ret = btrfs_end_transaction(trans, left_root);
5349 else
5350 btrfs_end_transaction(trans, left_root);
5351 }
5352
5353 return ret;
5354}
5355
Chris Mason3f157a22008-06-25 16:01:31 -04005356/*
5357 * this is similar to btrfs_next_leaf, but does not try to preserve
5358 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005359 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005360 *
5361 * 0 is returned if another key is found, < 0 if there are any errors
5362 * and 1 is returned if there are no higher keys in the tree
5363 *
5364 * path->keep_locks should be set to 1 on the search made before
5365 * calling this function.
5366 */
Chris Masone7a84562008-06-25 16:01:31 -04005367int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005368 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005369{
Chris Masone7a84562008-06-25 16:01:31 -04005370 int slot;
5371 struct extent_buffer *c;
5372
Chris Mason934d3752008-12-08 16:43:10 -05005373 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005374 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005375 if (!path->nodes[level])
5376 return 1;
5377
5378 slot = path->slots[level] + 1;
5379 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005380next:
Chris Masone7a84562008-06-25 16:01:31 -04005381 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005382 int ret;
5383 int orig_lowest;
5384 struct btrfs_key cur_key;
5385 if (level + 1 >= BTRFS_MAX_LEVEL ||
5386 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005387 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005388
5389 if (path->locks[level + 1]) {
5390 level++;
5391 continue;
5392 }
5393
5394 slot = btrfs_header_nritems(c) - 1;
5395 if (level == 0)
5396 btrfs_item_key_to_cpu(c, &cur_key, slot);
5397 else
5398 btrfs_node_key_to_cpu(c, &cur_key, slot);
5399
5400 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005401 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005402 path->lowest_level = level;
5403 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5404 0, 0);
5405 path->lowest_level = orig_lowest;
5406 if (ret < 0)
5407 return ret;
5408
5409 c = path->nodes[level];
5410 slot = path->slots[level];
5411 if (ret == 0)
5412 slot++;
5413 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005414 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005415
Chris Masone7a84562008-06-25 16:01:31 -04005416 if (level == 0)
5417 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005418 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005419 u64 gen = btrfs_node_ptr_generation(c, slot);
5420
Chris Mason3f157a22008-06-25 16:01:31 -04005421 if (gen < min_trans) {
5422 slot++;
5423 goto next;
5424 }
Chris Masone7a84562008-06-25 16:01:31 -04005425 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005426 }
Chris Masone7a84562008-06-25 16:01:31 -04005427 return 0;
5428 }
5429 return 1;
5430}
5431
Chris Mason7bb86312007-12-11 09:25:06 -05005432/*
Chris Mason925baed2008-06-25 16:01:30 -04005433 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005434 * returns 0 if it found something or 1 if there are no greater leaves.
5435 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005436 */
Chris Mason234b63a2007-03-13 10:46:10 -04005437int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005438{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005439 return btrfs_next_old_leaf(root, path, 0);
5440}
5441
5442int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5443 u64 time_seq)
5444{
Chris Masond97e63b2007-02-20 16:40:44 -05005445 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005446 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005447 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005448 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005449 struct btrfs_key key;
5450 u32 nritems;
5451 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005452 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005453 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005454
5455 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005456 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005457 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005458
Chris Mason8e73f272009-04-03 10:14:18 -04005459 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5460again:
5461 level = 1;
5462 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005463 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005464 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005465
Chris Masona2135012008-06-25 16:01:30 -04005466 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005467 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005468
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005469 if (time_seq)
5470 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5471 else
5472 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005473 path->keep_locks = 0;
5474
5475 if (ret < 0)
5476 return ret;
5477
Chris Masona2135012008-06-25 16:01:30 -04005478 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005479 /*
5480 * by releasing the path above we dropped all our locks. A balance
5481 * could have added more items next to the key that used to be
5482 * at the very end of the block. So, check again here and
5483 * advance the path if there are now more items available.
5484 */
Chris Masona2135012008-06-25 16:01:30 -04005485 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005486 if (ret == 0)
5487 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005488 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005489 goto done;
5490 }
Chris Masond97e63b2007-02-20 16:40:44 -05005491
Chris Masond3977122009-01-05 21:25:51 -05005492 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005493 if (!path->nodes[level]) {
5494 ret = 1;
5495 goto done;
5496 }
Chris Mason5f39d392007-10-15 16:14:19 -04005497
Chris Masond97e63b2007-02-20 16:40:44 -05005498 slot = path->slots[level] + 1;
5499 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005500 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005501 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005502 if (level == BTRFS_MAX_LEVEL) {
5503 ret = 1;
5504 goto done;
5505 }
Chris Masond97e63b2007-02-20 16:40:44 -05005506 continue;
5507 }
Chris Mason5f39d392007-10-15 16:14:19 -04005508
Chris Mason925baed2008-06-25 16:01:30 -04005509 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005510 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005511 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005512 }
Chris Mason5f39d392007-10-15 16:14:19 -04005513
Chris Mason8e73f272009-04-03 10:14:18 -04005514 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005515 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005516 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005517 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005518 if (ret == -EAGAIN)
5519 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005520
Chris Mason76a05b32009-05-14 13:24:30 -04005521 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005522 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005523 goto done;
5524 }
5525
Chris Mason5cd57b22008-06-25 16:01:30 -04005526 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005527 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005528 if (!ret && time_seq) {
5529 /*
5530 * If we don't get the lock, we may be racing
5531 * with push_leaf_left, holding that lock while
5532 * itself waiting for the leaf we've currently
5533 * locked. To solve this situation, we give up
5534 * on our lock and cycle.
5535 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005536 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005537 btrfs_release_path(path);
5538 cond_resched();
5539 goto again;
5540 }
Chris Mason8e73f272009-04-03 10:14:18 -04005541 if (!ret) {
5542 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005543 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005544 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005545 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005546 }
Chris Mason31533fb2011-07-26 16:01:59 -04005547 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005548 }
Chris Masond97e63b2007-02-20 16:40:44 -05005549 break;
5550 }
5551 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005552 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005553 level--;
5554 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005555 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005556 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005557
Chris Mason5f39d392007-10-15 16:14:19 -04005558 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005559 path->nodes[level] = next;
5560 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005561 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005562 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005563 if (!level)
5564 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005565
Chris Mason8e73f272009-04-03 10:14:18 -04005566 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005567 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005568 if (ret == -EAGAIN)
5569 goto again;
5570
Chris Mason76a05b32009-05-14 13:24:30 -04005571 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005572 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005573 goto done;
5574 }
5575
Chris Mason5cd57b22008-06-25 16:01:30 -04005576 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005577 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005578 if (!ret) {
5579 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005580 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005581 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005582 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005583 }
Chris Mason31533fb2011-07-26 16:01:59 -04005584 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005585 }
Chris Masond97e63b2007-02-20 16:40:44 -05005586 }
Chris Mason8e73f272009-04-03 10:14:18 -04005587 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005588done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005589 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005590 path->leave_spinning = old_spinning;
5591 if (!old_spinning)
5592 btrfs_set_path_blocking(path);
5593
5594 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005595}
Chris Mason0b86a832008-03-24 15:01:56 -04005596
Chris Mason3f157a22008-06-25 16:01:31 -04005597/*
5598 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5599 * searching until it gets past min_objectid or finds an item of 'type'
5600 *
5601 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5602 */
Chris Mason0b86a832008-03-24 15:01:56 -04005603int btrfs_previous_item(struct btrfs_root *root,
5604 struct btrfs_path *path, u64 min_objectid,
5605 int type)
5606{
5607 struct btrfs_key found_key;
5608 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005609 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005610 int ret;
5611
Chris Masond3977122009-01-05 21:25:51 -05005612 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005613 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005614 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005615 ret = btrfs_prev_leaf(root, path);
5616 if (ret != 0)
5617 return ret;
5618 } else {
5619 path->slots[0]--;
5620 }
5621 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005622 nritems = btrfs_header_nritems(leaf);
5623 if (nritems == 0)
5624 return 1;
5625 if (path->slots[0] == nritems)
5626 path->slots[0]--;
5627
Chris Mason0b86a832008-03-24 15:01:56 -04005628 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005629 if (found_key.objectid < min_objectid)
5630 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005631 if (found_key.type == type)
5632 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005633 if (found_key.objectid == min_objectid &&
5634 found_key.type < type)
5635 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005636 }
5637 return 1;
5638}