blob: c32d03dff4fcf7ea3368c47c86c21f71c03baa04 [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
Yan Zheng2b820322008-11-17 21:11:30 -0500277 write_extent_buffer(cow, root->fs_info->fsid,
278 (unsigned long)btrfs_header_fsid(cow),
279 BTRFS_FSID_SIZE);
280
Chris Masonbe20aa92007-12-17 20:14:01 -0500281 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400282 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200283 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400284 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200285 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 if (ret)
288 return ret;
289
290 btrfs_mark_buffer_dirty(cow);
291 *cow_ret = cow;
292 return 0;
293}
294
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200295enum mod_log_op {
296 MOD_LOG_KEY_REPLACE,
297 MOD_LOG_KEY_ADD,
298 MOD_LOG_KEY_REMOVE,
299 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
300 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
301 MOD_LOG_MOVE_KEYS,
302 MOD_LOG_ROOT_REPLACE,
303};
304
305struct tree_mod_move {
306 int dst_slot;
307 int nr_items;
308};
309
310struct tree_mod_root {
311 u64 logical;
312 u8 level;
313};
314
315struct tree_mod_elem {
316 struct rb_node node;
317 u64 index; /* shifted logical */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200318 u64 seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200319 enum mod_log_op op;
320
321 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
322 int slot;
323
324 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
325 u64 generation;
326
327 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
328 struct btrfs_disk_key key;
329 u64 blockptr;
330
331 /* this is used for op == MOD_LOG_MOVE_KEYS */
332 struct tree_mod_move move;
333
334 /* this is used for op == MOD_LOG_ROOT_REPLACE */
335 struct tree_mod_root old_root;
336};
337
Jan Schmidt097b8a72012-06-21 11:08:04 +0200338static inline void tree_mod_log_read_lock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200339{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200340 read_lock(&fs_info->tree_mod_log_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200341}
342
Jan Schmidt097b8a72012-06-21 11:08:04 +0200343static inline void tree_mod_log_read_unlock(struct btrfs_fs_info *fs_info)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200344{
Jan Schmidt097b8a72012-06-21 11:08:04 +0200345 read_unlock(&fs_info->tree_mod_log_lock);
346}
347
348static inline void tree_mod_log_write_lock(struct btrfs_fs_info *fs_info)
349{
350 write_lock(&fs_info->tree_mod_log_lock);
351}
352
353static inline void tree_mod_log_write_unlock(struct btrfs_fs_info *fs_info)
354{
355 write_unlock(&fs_info->tree_mod_log_lock);
356}
357
358/*
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000359 * Increment the upper half of tree_mod_seq, set lower half zero.
360 *
361 * Must be called with fs_info->tree_mod_seq_lock held.
362 */
363static inline u64 btrfs_inc_tree_mod_seq_major(struct btrfs_fs_info *fs_info)
364{
365 u64 seq = atomic64_read(&fs_info->tree_mod_seq);
366 seq &= 0xffffffff00000000ull;
367 seq += 1ull << 32;
368 atomic64_set(&fs_info->tree_mod_seq, seq);
369 return seq;
370}
371
372/*
373 * Increment the lower half of tree_mod_seq.
374 *
375 * Must be called with fs_info->tree_mod_seq_lock held. The way major numbers
376 * are generated should not technically require a spin lock here. (Rationale:
377 * incrementing the minor while incrementing the major seq number is between its
378 * atomic64_read and atomic64_set calls doesn't duplicate sequence numbers, it
379 * just returns a unique sequence number as usual.) We have decided to leave
380 * that requirement in here and rethink it once we notice it really imposes a
381 * problem on some workload.
382 */
383static inline u64 btrfs_inc_tree_mod_seq_minor(struct btrfs_fs_info *fs_info)
384{
385 return atomic64_inc_return(&fs_info->tree_mod_seq);
386}
387
388/*
389 * return the last minor in the previous major tree_mod_seq number
390 */
391u64 btrfs_tree_mod_seq_prev(u64 seq)
392{
393 return (seq & 0xffffffff00000000ull) - 1ull;
394}
395
396/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200397 * This adds a new blocker to the tree mod log's blocker list if the @elem
398 * passed does not already have a sequence number set. So when a caller expects
399 * to record tree modifications, it should ensure to set elem->seq to zero
400 * before calling btrfs_get_tree_mod_seq.
401 * Returns a fresh, unused tree log modification sequence number, even if no new
402 * blocker was added.
403 */
404u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
405 struct seq_list *elem)
406{
407 u64 seq;
408
409 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200410 spin_lock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200411 if (!elem->seq) {
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000412 elem->seq = btrfs_inc_tree_mod_seq_major(fs_info);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200413 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
414 }
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000415 seq = btrfs_inc_tree_mod_seq_minor(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200416 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200417 tree_mod_log_write_unlock(fs_info);
418
419 return seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200420}
421
422void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
423 struct seq_list *elem)
424{
425 struct rb_root *tm_root;
426 struct rb_node *node;
427 struct rb_node *next;
428 struct seq_list *cur_elem;
429 struct tree_mod_elem *tm;
430 u64 min_seq = (u64)-1;
431 u64 seq_putting = elem->seq;
432
433 if (!seq_putting)
434 return;
435
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200436 spin_lock(&fs_info->tree_mod_seq_lock);
437 list_del(&elem->list);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200438 elem->seq = 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200439
440 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200441 if (cur_elem->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200442 if (seq_putting > cur_elem->seq) {
443 /*
444 * blocker with lower sequence number exists, we
445 * cannot remove anything from the log
446 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200447 spin_unlock(&fs_info->tree_mod_seq_lock);
448 return;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200449 }
450 min_seq = cur_elem->seq;
451 }
452 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200453 spin_unlock(&fs_info->tree_mod_seq_lock);
454
455 /*
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200456 * anything that's lower than the lowest existing (read: blocked)
457 * sequence number can be removed from the tree.
458 */
Jan Schmidt097b8a72012-06-21 11:08:04 +0200459 tree_mod_log_write_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460 tm_root = &fs_info->tree_mod_log;
461 for (node = rb_first(tm_root); node; node = next) {
462 next = rb_next(node);
463 tm = container_of(node, struct tree_mod_elem, node);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200464 if (tm->seq > min_seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200465 continue;
466 rb_erase(node, tm_root);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200467 kfree(tm);
468 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200469 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200470}
471
472/*
473 * key order of the log:
474 * index -> sequence
475 *
476 * the index is the shifted logical of the *new* root node for root replace
477 * operations, or the shifted logical of the affected block for all other
478 * operations.
479 */
480static noinline int
481__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
482{
483 struct rb_root *tm_root;
484 struct rb_node **new;
485 struct rb_node *parent = NULL;
486 struct tree_mod_elem *cur;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200487
Jan Schmidt097b8a72012-06-21 11:08:04 +0200488 BUG_ON(!tm || !tm->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200489
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200490 tm_root = &fs_info->tree_mod_log;
491 new = &tm_root->rb_node;
492 while (*new) {
493 cur = container_of(*new, struct tree_mod_elem, node);
494 parent = *new;
495 if (cur->index < tm->index)
496 new = &((*new)->rb_left);
497 else if (cur->index > tm->index)
498 new = &((*new)->rb_right);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200499 else if (cur->seq < tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200500 new = &((*new)->rb_left);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200501 else if (cur->seq > tm->seq)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200502 new = &((*new)->rb_right);
503 else {
504 kfree(tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200505 return -EEXIST;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200506 }
507 }
508
509 rb_link_node(&tm->node, parent, new);
510 rb_insert_color(&tm->node, tm_root);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200511 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200512}
513
Jan Schmidt097b8a72012-06-21 11:08:04 +0200514/*
515 * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
516 * returns zero with the tree_mod_log_lock acquired. The caller must hold
517 * this until all tree mod log insertions are recorded in the rb tree and then
518 * call tree_mod_log_write_unlock() to release.
519 */
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200520static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
521 struct extent_buffer *eb) {
522 smp_mb();
523 if (list_empty(&(fs_info)->tree_mod_seq_list))
524 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200525 if (eb && btrfs_header_level(eb) == 0)
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200526 return 1;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200527
528 tree_mod_log_write_lock(fs_info);
529 if (list_empty(&fs_info->tree_mod_seq_list)) {
530 /*
531 * someone emptied the list while we were waiting for the lock.
532 * we must not add to the list when no blocker exists.
533 */
534 tree_mod_log_write_unlock(fs_info);
535 return 1;
536 }
537
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200538 return 0;
539}
540
Jan Schmidt3310c362012-06-11 10:52:38 +0200541/*
Jan Schmidt097b8a72012-06-21 11:08:04 +0200542 * This allocates memory and gets a tree modification sequence number.
Jan Schmidt3310c362012-06-11 10:52:38 +0200543 *
Jan Schmidt097b8a72012-06-21 11:08:04 +0200544 * Returns <0 on error.
545 * Returns >0 (the added sequence number) on success.
Jan Schmidt3310c362012-06-11 10:52:38 +0200546 */
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200547static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
548 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200549{
550 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200551
Jan Schmidt097b8a72012-06-21 11:08:04 +0200552 /*
553 * once we switch from spin locks to something different, we should
554 * honor the flags parameter here.
555 */
556 tm = *tm_ret = kzalloc(sizeof(*tm), GFP_ATOMIC);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200557 if (!tm)
558 return -ENOMEM;
559
Jan Schmidtfc36ed72013-04-24 16:57:33 +0000560 spin_lock(&fs_info->tree_mod_seq_lock);
561 tm->seq = btrfs_inc_tree_mod_seq_minor(fs_info);
562 spin_unlock(&fs_info->tree_mod_seq_lock);
563
Jan Schmidt097b8a72012-06-21 11:08:04 +0200564 return tm->seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200565}
566
Jan Schmidt097b8a72012-06-21 11:08:04 +0200567static inline int
568__tree_mod_log_insert_key(struct btrfs_fs_info *fs_info,
569 struct extent_buffer *eb, int slot,
570 enum mod_log_op op, gfp_t flags)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200571{
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200572 int ret;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200573 struct tree_mod_elem *tm;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200574
575 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200576 if (ret < 0)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200577 return ret;
578
579 tm->index = eb->start >> PAGE_CACHE_SHIFT;
580 if (op != MOD_LOG_KEY_ADD) {
581 btrfs_node_key(eb, &tm->key, slot);
582 tm->blockptr = btrfs_node_blockptr(eb, slot);
583 }
584 tm->op = op;
585 tm->slot = slot;
586 tm->generation = btrfs_node_ptr_generation(eb, slot);
587
Jan Schmidt097b8a72012-06-21 11:08:04 +0200588 return __tree_mod_log_insert(fs_info, tm);
589}
590
591static noinline int
592tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
593 struct extent_buffer *eb, int slot,
594 enum mod_log_op op, gfp_t flags)
595{
596 int ret;
597
598 if (tree_mod_dont_log(fs_info, eb))
599 return 0;
600
601 ret = __tree_mod_log_insert_key(fs_info, eb, slot, op, flags);
602
603 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200604 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200605}
606
607static noinline int
608tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
609 int slot, enum mod_log_op op)
610{
611 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
612}
613
614static noinline int
Jan Schmidt097b8a72012-06-21 11:08:04 +0200615tree_mod_log_insert_key_locked(struct btrfs_fs_info *fs_info,
616 struct extent_buffer *eb, int slot,
617 enum mod_log_op op)
618{
619 return __tree_mod_log_insert_key(fs_info, eb, slot, op, GFP_NOFS);
620}
621
622static noinline int
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200623tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
624 struct extent_buffer *eb, int dst_slot, int src_slot,
625 int nr_items, gfp_t flags)
626{
627 struct tree_mod_elem *tm;
628 int ret;
629 int i;
630
Jan Schmidtf3956942012-05-31 15:02:32 +0200631 if (tree_mod_dont_log(fs_info, eb))
632 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200633
Jan Schmidt01763a22012-10-23 15:02:12 +0200634 /*
635 * When we override something during the move, we log these removals.
636 * This can only happen when we move towards the beginning of the
637 * buffer, i.e. dst_slot < src_slot.
638 */
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200639 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
Jan Schmidt097b8a72012-06-21 11:08:04 +0200640 ret = tree_mod_log_insert_key_locked(fs_info, eb, i + dst_slot,
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200641 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
642 BUG_ON(ret < 0);
643 }
644
Jan Schmidtf3956942012-05-31 15:02:32 +0200645 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200646 if (ret < 0)
647 goto out;
Jan Schmidtf3956942012-05-31 15:02:32 +0200648
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200649 tm->index = eb->start >> PAGE_CACHE_SHIFT;
650 tm->slot = src_slot;
651 tm->move.dst_slot = dst_slot;
652 tm->move.nr_items = nr_items;
653 tm->op = MOD_LOG_MOVE_KEYS;
654
Jan Schmidt3310c362012-06-11 10:52:38 +0200655 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200656out:
657 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200658 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200659}
660
Jan Schmidt097b8a72012-06-21 11:08:04 +0200661static inline void
662__tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
663{
664 int i;
665 u32 nritems;
666 int ret;
667
Chris Masonb12a3b12012-08-07 15:34:49 -0400668 if (btrfs_header_level(eb) == 0)
669 return;
670
Jan Schmidt097b8a72012-06-21 11:08:04 +0200671 nritems = btrfs_header_nritems(eb);
672 for (i = nritems - 1; i >= 0; i--) {
673 ret = tree_mod_log_insert_key_locked(fs_info, eb, i,
674 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
675 BUG_ON(ret < 0);
676 }
677}
678
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200679static noinline int
680tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
681 struct extent_buffer *old_root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000682 struct extent_buffer *new_root, gfp_t flags,
683 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200684{
685 struct tree_mod_elem *tm;
686 int ret;
687
Jan Schmidt097b8a72012-06-21 11:08:04 +0200688 if (tree_mod_dont_log(fs_info, NULL))
689 return 0;
690
Jan Schmidt90f8d622013-04-13 13:19:53 +0000691 if (log_removal)
692 __tree_mod_log_free_eb(fs_info, old_root);
Jan Schmidtd9abbf12013-03-20 13:49:48 +0000693
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200694 ret = tree_mod_alloc(fs_info, flags, &tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200695 if (ret < 0)
696 goto out;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200697
698 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
699 tm->old_root.logical = old_root->start;
700 tm->old_root.level = btrfs_header_level(old_root);
701 tm->generation = btrfs_header_generation(old_root);
702 tm->op = MOD_LOG_ROOT_REPLACE;
703
Jan Schmidt3310c362012-06-11 10:52:38 +0200704 ret = __tree_mod_log_insert(fs_info, tm);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200705out:
706 tree_mod_log_write_unlock(fs_info);
Jan Schmidt3310c362012-06-11 10:52:38 +0200707 return ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708}
709
710static struct tree_mod_elem *
711__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
712 int smallest)
713{
714 struct rb_root *tm_root;
715 struct rb_node *node;
716 struct tree_mod_elem *cur = NULL;
717 struct tree_mod_elem *found = NULL;
718 u64 index = start >> PAGE_CACHE_SHIFT;
719
Jan Schmidt097b8a72012-06-21 11:08:04 +0200720 tree_mod_log_read_lock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200721 tm_root = &fs_info->tree_mod_log;
722 node = tm_root->rb_node;
723 while (node) {
724 cur = container_of(node, struct tree_mod_elem, node);
725 if (cur->index < index) {
726 node = node->rb_left;
727 } else if (cur->index > index) {
728 node = node->rb_right;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200729 } else if (cur->seq < min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200730 node = node->rb_left;
731 } else if (!smallest) {
732 /* we want the node with the highest seq */
733 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200734 BUG_ON(found->seq > cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200735 found = cur;
736 node = node->rb_left;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200737 } else if (cur->seq > min_seq) {
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200738 /* we want the node with the smallest seq */
739 if (found)
Jan Schmidt097b8a72012-06-21 11:08:04 +0200740 BUG_ON(found->seq < cur->seq);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200741 found = cur;
742 node = node->rb_right;
743 } else {
744 found = cur;
745 break;
746 }
747 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200748 tree_mod_log_read_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200749
750 return found;
751}
752
753/*
754 * this returns the element from the log with the smallest time sequence
755 * value that's in the log (the oldest log item). any element with a time
756 * sequence lower than min_seq will be ignored.
757 */
758static struct tree_mod_elem *
759tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
760 u64 min_seq)
761{
762 return __tree_mod_log_search(fs_info, start, min_seq, 1);
763}
764
765/*
766 * this returns the element from the log with the largest time sequence
767 * value that's in the log (the most recent log item). any element with
768 * a time sequence lower than min_seq will be ignored.
769 */
770static struct tree_mod_elem *
771tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
772{
773 return __tree_mod_log_search(fs_info, start, min_seq, 0);
774}
775
Jan Schmidt097b8a72012-06-21 11:08:04 +0200776static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200777tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
778 struct extent_buffer *src, unsigned long dst_offset,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000779 unsigned long src_offset, int nr_items)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200780{
781 int ret;
782 int i;
783
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200784 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200785 return;
786
Jan Schmidt097b8a72012-06-21 11:08:04 +0200787 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0) {
788 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200789 return;
Jan Schmidt097b8a72012-06-21 11:08:04 +0200790 }
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200791
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200792 for (i = 0; i < nr_items; i++) {
Jan Schmidt90f8d622013-04-13 13:19:53 +0000793 ret = tree_mod_log_insert_key_locked(fs_info, src,
794 i + src_offset,
795 MOD_LOG_KEY_REMOVE);
796 BUG_ON(ret < 0);
Jan Schmidt097b8a72012-06-21 11:08:04 +0200797 ret = tree_mod_log_insert_key_locked(fs_info, dst,
798 i + dst_offset,
799 MOD_LOG_KEY_ADD);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200800 BUG_ON(ret < 0);
801 }
Jan Schmidt097b8a72012-06-21 11:08:04 +0200802
803 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200804}
805
806static inline void
807tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
808 int dst_offset, int src_offset, int nr_items)
809{
810 int ret;
811 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
812 nr_items, GFP_NOFS);
813 BUG_ON(ret < 0);
814}
815
Jan Schmidt097b8a72012-06-21 11:08:04 +0200816static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200817tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
Liu Bo32adf092012-10-19 12:52:15 +0000818 struct extent_buffer *eb, int slot, int atomic)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200819{
820 int ret;
821
822 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
823 MOD_LOG_KEY_REPLACE,
824 atomic ? GFP_ATOMIC : GFP_NOFS);
825 BUG_ON(ret < 0);
826}
827
Jan Schmidt097b8a72012-06-21 11:08:04 +0200828static noinline void
829tree_mod_log_free_eb(struct btrfs_fs_info *fs_info, struct extent_buffer *eb)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200830{
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200831 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200832 return;
833
Jan Schmidt097b8a72012-06-21 11:08:04 +0200834 __tree_mod_log_free_eb(fs_info, eb);
835
836 tree_mod_log_write_unlock(fs_info);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200837}
838
Jan Schmidt097b8a72012-06-21 11:08:04 +0200839static noinline void
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200840tree_mod_log_set_root_pointer(struct btrfs_root *root,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000841 struct extent_buffer *new_root_node,
842 int log_removal)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200843{
844 int ret;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200845 ret = tree_mod_log_insert_root(root->fs_info, root->node,
Jan Schmidt90f8d622013-04-13 13:19:53 +0000846 new_root_node, GFP_NOFS, log_removal);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200847 BUG_ON(ret < 0);
848}
849
Chris Masond352ac62008-09-29 15:18:18 -0400850/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400851 * check if the tree block can be shared by multiple trees
852 */
853int btrfs_block_can_be_shared(struct btrfs_root *root,
854 struct extent_buffer *buf)
855{
856 /*
857 * Tree blocks not in refernece counted trees and tree roots
858 * are never shared. If a block was allocated after the last
859 * snapshot and the block was not allocated by tree relocation,
860 * we know the block is not shared.
861 */
862 if (root->ref_cows &&
863 buf != root->node && buf != root->commit_root &&
864 (btrfs_header_generation(buf) <=
865 btrfs_root_last_snapshot(&root->root_item) ||
866 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
867 return 1;
868#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
869 if (root->ref_cows &&
870 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
871 return 1;
872#endif
873 return 0;
874}
875
876static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
877 struct btrfs_root *root,
878 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400879 struct extent_buffer *cow,
880 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881{
882 u64 refs;
883 u64 owner;
884 u64 flags;
885 u64 new_flags = 0;
886 int ret;
887
888 /*
889 * Backrefs update rules:
890 *
891 * Always use full backrefs for extent pointers in tree block
892 * allocated by tree relocation.
893 *
894 * If a shared tree block is no longer referenced by its owner
895 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
896 * use full backrefs for extent pointers in tree block.
897 *
898 * If a tree block is been relocating
899 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
900 * use full backrefs for extent pointers in tree block.
901 * The reason for this is some operations (such as drop tree)
902 * are only allowed for blocks use full backrefs.
903 */
904
905 if (btrfs_block_can_be_shared(root, buf)) {
906 ret = btrfs_lookup_extent_info(trans, root, buf->start,
Josef Bacik3173a182013-03-07 14:22:04 -0500907 btrfs_header_level(buf), 1,
908 &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700909 if (ret)
910 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700911 if (refs == 0) {
912 ret = -EROFS;
913 btrfs_std_error(root->fs_info, ret);
914 return ret;
915 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400916 } else {
917 refs = 1;
918 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
919 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
920 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
921 else
922 flags = 0;
923 }
924
925 owner = btrfs_header_owner(buf);
926 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
927 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
928
929 if (refs > 1) {
930 if ((owner == root->root_key.objectid ||
931 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
932 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200933 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400935
936 if (root->root_key.objectid ==
937 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200938 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100939 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200940 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100941 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400942 }
943 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
944 } else {
945
946 if (root->root_key.objectid ==
947 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200948 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200950 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100951 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 }
953 if (new_flags != 0) {
Josef Bacikb1c79e02013-05-09 13:49:30 -0400954 int level = btrfs_header_level(buf);
955
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 ret = btrfs_set_disk_extent_flags(trans, root,
957 buf->start,
958 buf->len,
Josef Bacikb1c79e02013-05-09 13:49:30 -0400959 new_flags, level, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700960 if (ret)
961 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962 }
963 } else {
964 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
965 if (root->root_key.objectid ==
966 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200967 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200969 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100970 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200971 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100972 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400973 }
974 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400975 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400976 }
977 return 0;
978}
979
980/*
Chris Masond3977122009-01-05 21:25:51 -0500981 * does the dirty work in cow of a single block. The parent block (if
982 * supplied) is updated to point to the new cow copy. The new buffer is marked
983 * dirty and returned locked. If you modify the block it needs to be marked
984 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400985 *
986 * search_start -- an allocation hint for the new block
987 *
Chris Masond3977122009-01-05 21:25:51 -0500988 * empty_size -- a hint that you plan on doing more cow. This is the size in
989 * bytes the allocator should try to find free next to the block it returns.
990 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400991 */
Chris Masond3977122009-01-05 21:25:51 -0500992static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400993 struct btrfs_root *root,
994 struct extent_buffer *buf,
995 struct extent_buffer *parent, int parent_slot,
996 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400997 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400998{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -04001000 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -07001001 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04001002 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001003 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001004 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -04001005
Chris Mason925baed2008-06-25 16:01:30 -04001006 if (*cow_ret == buf)
1007 unlock_orig = 1;
1008
Chris Masonb9447ef2009-03-09 11:45:38 -04001009 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -04001010
Chris Mason7bb86312007-12-11 09:25:06 -05001011 WARN_ON(root->ref_cows && trans->transid !=
1012 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001013 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -04001014
Chris Mason7bb86312007-12-11 09:25:06 -05001015 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 if (level == 0)
1018 btrfs_item_key(buf, &disk_key, 0);
1019 else
1020 btrfs_node_key(buf, &disk_key, 0);
1021
1022 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
1023 if (parent)
1024 parent_start = parent->start;
1025 else
1026 parent_start = 0;
1027 } else
1028 parent_start = 0;
1029
1030 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
1031 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02001032 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -04001033 if (IS_ERR(cow))
1034 return PTR_ERR(cow);
1035
Chris Masonb4ce94d2009-02-04 09:25:08 -05001036 /* cow is set to blocking by btrfs_init_new_buffer */
1037
Chris Mason5f39d392007-10-15 16:14:19 -04001038 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -04001039 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -04001040 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001041 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
1042 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
1043 BTRFS_HEADER_FLAG_RELOC);
1044 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1045 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
1046 else
1047 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -04001048
Yan Zheng2b820322008-11-17 21:11:30 -05001049 write_extent_buffer(cow, root->fs_info->fsid,
1050 (unsigned long)btrfs_header_fsid(cow),
1051 BTRFS_FSID_SIZE);
1052
Mark Fashehbe1a5562011-08-08 13:20:18 -07001053 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001054 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001055 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -07001056 return ret;
1057 }
Zheng Yan1a40e232008-09-26 10:09:34 -04001058
Yan, Zheng3fd0a552010-05-16 10:49:59 -04001059 if (root->ref_cows)
1060 btrfs_reloc_cow_block(trans, root, buf, cow);
1061
Chris Mason6702ed42007-08-07 16:15:09 -04001062 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -04001063 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
1065 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
1066 parent_start = buf->start;
1067 else
1068 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001069
Chris Mason5f39d392007-10-15 16:14:19 -04001070 extent_buffer_get(cow);
Jan Schmidt90f8d622013-04-13 13:19:53 +00001071 tree_mod_log_set_root_pointer(root, cow, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001072 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -04001073
Yan, Zhengf0486c62010-05-16 10:46:25 -04001074 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001075 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -04001076 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -04001077 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -04001078 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001079 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
1080 parent_start = parent->start;
1081 else
1082 parent_start = 0;
1083
1084 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +02001085 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
1086 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -04001087 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -04001088 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -05001089 btrfs_set_node_ptr_generation(parent, parent_slot,
1090 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -04001091 btrfs_mark_buffer_dirty(parent);
Jan Schmidtd9abbf12013-03-20 13:49:48 +00001092 tree_mod_log_free_eb(root->fs_info, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001093 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +02001094 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -04001095 }
Chris Mason925baed2008-06-25 16:01:30 -04001096 if (unlock_orig)
1097 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05001098 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -04001099 btrfs_mark_buffer_dirty(cow);
1100 *cow_ret = cow;
1101 return 0;
1102}
1103
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001104/*
1105 * returns the logical address of the oldest predecessor of the given root.
1106 * entries older than time_seq are ignored.
1107 */
1108static struct tree_mod_elem *
1109__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
Jan Schmidt30b04632013-04-13 13:19:54 +00001110 struct extent_buffer *eb_root, u64 time_seq)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001111{
1112 struct tree_mod_elem *tm;
1113 struct tree_mod_elem *found = NULL;
Jan Schmidt30b04632013-04-13 13:19:54 +00001114 u64 root_logical = eb_root->start;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001115 int looped = 0;
1116
1117 if (!time_seq)
1118 return 0;
1119
1120 /*
1121 * the very last operation that's logged for a root is the replacement
1122 * operation (if it is replaced at all). this has the index of the *new*
1123 * root, making it the very first operation that's logged for this root.
1124 */
1125 while (1) {
1126 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1127 time_seq);
1128 if (!looped && !tm)
1129 return 0;
1130 /*
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001131 * if there are no tree operation for the oldest root, we simply
1132 * return it. this should only happen if that (old) root is at
1133 * level 0.
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001134 */
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001135 if (!tm)
1136 break;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001137
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001138 /*
1139 * if there's an operation that's not a root replacement, we
1140 * found the oldest version of our root. normally, we'll find a
1141 * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
1142 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001143 if (tm->op != MOD_LOG_ROOT_REPLACE)
1144 break;
1145
1146 found = tm;
1147 root_logical = tm->old_root.logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001148 looped = 1;
1149 }
1150
Jan Schmidta95236d2012-06-05 16:41:24 +02001151 /* if there's no old root to return, return what we found instead */
1152 if (!found)
1153 found = tm;
1154
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001155 return found;
1156}
1157
1158/*
1159 * tm is a pointer to the first operation to rewind within eb. then, all
1160 * previous operations will be rewinded (until we reach something older than
1161 * time_seq).
1162 */
1163static void
1164__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1165 struct tree_mod_elem *first_tm)
1166{
1167 u32 n;
1168 struct rb_node *next;
1169 struct tree_mod_elem *tm = first_tm;
1170 unsigned long o_dst;
1171 unsigned long o_src;
1172 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1173
1174 n = btrfs_header_nritems(eb);
Jan Schmidt097b8a72012-06-21 11:08:04 +02001175 while (tm && tm->seq >= time_seq) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001176 /*
1177 * all the operations are recorded with the operator used for
1178 * the modification. as we're going backwards, we do the
1179 * opposite of each operation here.
1180 */
1181 switch (tm->op) {
1182 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1183 BUG_ON(tm->slot < n);
Eric Sandeen1c697d42013-01-31 00:54:56 +00001184 /* Fallthrough */
Liu Bo95c80bb2012-10-19 09:50:52 +00001185 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
Chris Mason4c3e6962012-12-18 15:43:18 -05001186 case MOD_LOG_KEY_REMOVE:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001187 btrfs_set_node_key(eb, &tm->key, tm->slot);
1188 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1189 btrfs_set_node_ptr_generation(eb, tm->slot,
1190 tm->generation);
Chris Mason4c3e6962012-12-18 15:43:18 -05001191 n++;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001192 break;
1193 case MOD_LOG_KEY_REPLACE:
1194 BUG_ON(tm->slot >= n);
1195 btrfs_set_node_key(eb, &tm->key, tm->slot);
1196 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1197 btrfs_set_node_ptr_generation(eb, tm->slot,
1198 tm->generation);
1199 break;
1200 case MOD_LOG_KEY_ADD:
Jan Schmidt19956c72012-06-22 14:52:13 +02001201 /* if a move operation is needed it's in the log */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001202 n--;
1203 break;
1204 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001205 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1206 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1207 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001208 tm->move.nr_items * p_size);
1209 break;
1210 case MOD_LOG_ROOT_REPLACE:
1211 /*
1212 * this operation is special. for roots, this must be
1213 * handled explicitly before rewinding.
1214 * for non-roots, this operation may exist if the node
1215 * was a root: root A -> child B; then A gets empty and
1216 * B is promoted to the new root. in the mod log, we'll
1217 * have a root-replace operation for B, a tree block
1218 * that is no root. we simply ignore that operation.
1219 */
1220 break;
1221 }
1222 next = rb_next(&tm->node);
1223 if (!next)
1224 break;
1225 tm = container_of(next, struct tree_mod_elem, node);
1226 if (tm->index != first_tm->index)
1227 break;
1228 }
1229 btrfs_set_header_nritems(eb, n);
1230}
1231
Jan Schmidt47fb0912013-04-13 13:19:55 +00001232/*
1233 * Called with eb read locked. If the buffer cannot be rewinded, the same buffer
1234 * is returned. If rewind operations happen, a fresh buffer is returned. The
1235 * returned buffer is always read-locked. If the returned buffer is not the
1236 * input buffer, the lock on the input buffer is released and the input buffer
1237 * is freed (its refcount is decremented).
1238 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001239static struct extent_buffer *
1240tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1241 u64 time_seq)
1242{
1243 struct extent_buffer *eb_rewin;
1244 struct tree_mod_elem *tm;
1245
1246 if (!time_seq)
1247 return eb;
1248
1249 if (btrfs_header_level(eb) == 0)
1250 return eb;
1251
1252 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1253 if (!tm)
1254 return eb;
1255
1256 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1257 BUG_ON(tm->slot != 0);
1258 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1259 fs_info->tree_root->nodesize);
1260 BUG_ON(!eb_rewin);
1261 btrfs_set_header_bytenr(eb_rewin, eb->start);
1262 btrfs_set_header_backref_rev(eb_rewin,
1263 btrfs_header_backref_rev(eb));
1264 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001265 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001266 } else {
1267 eb_rewin = btrfs_clone_extent_buffer(eb);
1268 BUG_ON(!eb_rewin);
1269 }
1270
1271 extent_buffer_get(eb_rewin);
Jan Schmidt47fb0912013-04-13 13:19:55 +00001272 btrfs_tree_read_unlock(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001273 free_extent_buffer(eb);
1274
Jan Schmidt47fb0912013-04-13 13:19:55 +00001275 extent_buffer_get(eb_rewin);
1276 btrfs_tree_read_lock(eb_rewin);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001277 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
Jan Schmidt57911b82012-10-19 09:22:03 +02001278 WARN_ON(btrfs_header_nritems(eb_rewin) >
Arne Jansen2a745b12013-02-13 04:20:01 -07001279 BTRFS_NODEPTRS_PER_BLOCK(fs_info->tree_root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001280
1281 return eb_rewin;
1282}
1283
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001284/*
1285 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1286 * value. If there are no changes, the current root->root_node is returned. If
1287 * anything changed in between, there's a fresh buffer allocated on which the
1288 * rewind operations are done. In any case, the returned buffer is read locked.
1289 * Returns NULL on error (with no locks held).
1290 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001291static inline struct extent_buffer *
1292get_old_root(struct btrfs_root *root, u64 time_seq)
1293{
1294 struct tree_mod_elem *tm;
Jan Schmidt30b04632013-04-13 13:19:54 +00001295 struct extent_buffer *eb = NULL;
1296 struct extent_buffer *eb_root;
Liu Bo7bfdcf72012-10-25 07:30:19 -06001297 struct extent_buffer *old;
Jan Schmidta95236d2012-06-05 16:41:24 +02001298 struct tree_mod_root *old_root = NULL;
Chris Mason4325edd2012-06-15 20:02:02 -04001299 u64 old_generation = 0;
Jan Schmidta95236d2012-06-05 16:41:24 +02001300 u64 logical;
Jan Schmidt834328a2012-10-23 11:27:33 +02001301 u32 blocksize;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001302
Jan Schmidt30b04632013-04-13 13:19:54 +00001303 eb_root = btrfs_read_lock_root_node(root);
1304 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001305 if (!tm)
Jan Schmidt30b04632013-04-13 13:19:54 +00001306 return eb_root;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001307
Jan Schmidta95236d2012-06-05 16:41:24 +02001308 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1309 old_root = &tm->old_root;
1310 old_generation = tm->generation;
1311 logical = old_root->logical;
1312 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001313 logical = eb_root->start;
Jan Schmidta95236d2012-06-05 16:41:24 +02001314 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001315
Jan Schmidta95236d2012-06-05 16:41:24 +02001316 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt834328a2012-10-23 11:27:33 +02001317 if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001318 btrfs_tree_read_unlock(eb_root);
1319 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001320 blocksize = btrfs_level_size(root, old_root->level);
Liu Bo7bfdcf72012-10-25 07:30:19 -06001321 old = read_tree_block(root, logical, blocksize, 0);
Josef Bacik416bc652013-04-23 14:17:42 -04001322 if (!old || !extent_buffer_uptodate(old)) {
1323 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001324 pr_warn("btrfs: failed to read tree block %llu from get_old_root\n",
1325 logical);
1326 WARN_ON(1);
1327 } else {
Liu Bo7bfdcf72012-10-25 07:30:19 -06001328 eb = btrfs_clone_extent_buffer(old);
1329 free_extent_buffer(old);
Jan Schmidt834328a2012-10-23 11:27:33 +02001330 }
1331 } else if (old_root) {
Jan Schmidt30b04632013-04-13 13:19:54 +00001332 btrfs_tree_read_unlock(eb_root);
1333 free_extent_buffer(eb_root);
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001334 eb = alloc_dummy_extent_buffer(logical, root->nodesize);
Jan Schmidt834328a2012-10-23 11:27:33 +02001335 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001336 eb = btrfs_clone_extent_buffer(eb_root);
1337 btrfs_tree_read_unlock(eb_root);
1338 free_extent_buffer(eb_root);
Jan Schmidt834328a2012-10-23 11:27:33 +02001339 }
1340
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001341 if (!eb)
1342 return NULL;
Jan Schmidtd6381082012-10-23 14:21:05 +02001343 extent_buffer_get(eb);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001344 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001345 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001346 btrfs_set_header_bytenr(eb, eb->start);
1347 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
Jan Schmidt30b04632013-04-13 13:19:54 +00001348 btrfs_set_header_owner(eb, btrfs_header_owner(eb_root));
Jan Schmidta95236d2012-06-05 16:41:24 +02001349 btrfs_set_header_level(eb, old_root->level);
1350 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001351 }
Jan Schmidt28da9fb2012-06-21 10:59:13 +02001352 if (tm)
1353 __tree_mod_log_rewind(eb, time_seq, tm);
1354 else
1355 WARN_ON(btrfs_header_level(eb) != 0);
Jan Schmidt57911b82012-10-19 09:22:03 +02001356 WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(root));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001357
1358 return eb;
1359}
1360
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001361int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
1362{
1363 struct tree_mod_elem *tm;
1364 int level;
Jan Schmidt30b04632013-04-13 13:19:54 +00001365 struct extent_buffer *eb_root = btrfs_root_node(root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001366
Jan Schmidt30b04632013-04-13 13:19:54 +00001367 tm = __tree_mod_log_oldest_root(root->fs_info, eb_root, time_seq);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001368 if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
1369 level = tm->old_root.level;
1370 } else {
Jan Schmidt30b04632013-04-13 13:19:54 +00001371 level = btrfs_header_level(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001372 }
Jan Schmidt30b04632013-04-13 13:19:54 +00001373 free_extent_buffer(eb_root);
Jan Schmidt5b6602e2012-10-23 11:28:27 +02001374
1375 return level;
1376}
1377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001378static inline int should_cow_block(struct btrfs_trans_handle *trans,
1379 struct btrfs_root *root,
1380 struct extent_buffer *buf)
1381{
Liu Bof1ebcc72011-11-14 20:48:06 -05001382 /* ensure we can see the force_cow */
1383 smp_rmb();
1384
1385 /*
1386 * We do not need to cow a block if
1387 * 1) this block is not created or changed in this transaction;
1388 * 2) this block does not belong to TREE_RELOC tree;
1389 * 3) the root is not forced COW.
1390 *
1391 * What is forced COW:
1392 * when we create snapshot during commiting the transaction,
1393 * after we've finished coping src root, we must COW the shared
1394 * block to ensure the metadata consistency.
1395 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001396 if (btrfs_header_generation(buf) == trans->transid &&
1397 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1398 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001399 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1400 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001401 return 0;
1402 return 1;
1403}
1404
Chris Masond352ac62008-09-29 15:18:18 -04001405/*
1406 * cows a single block, see __btrfs_cow_block for the real work.
1407 * This version of it has extra checks so that a block isn't cow'd more than
1408 * once per transaction, as long as it hasn't been written yet
1409 */
Chris Masond3977122009-01-05 21:25:51 -05001410noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001411 struct btrfs_root *root, struct extent_buffer *buf,
1412 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001413 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001414{
Chris Mason6702ed42007-08-07 16:15:09 -04001415 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001416 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001417
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001418 if (trans->transaction != root->fs_info->running_transaction)
1419 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001420 (unsigned long long)trans->transid,
1421 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001422 root->fs_info->running_transaction->transid);
Julia Lawall31b1a2b2012-11-03 10:58:34 +00001423
1424 if (trans->transid != root->fs_info->generation)
1425 WARN(1, KERN_CRIT "trans %llu running %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05001426 (unsigned long long)trans->transid,
1427 (unsigned long long)root->fs_info->generation);
Chris Masondc17ff82008-01-08 15:46:30 -05001428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001430 *cow_ret = buf;
1431 return 0;
1432 }
Chris Masonc4876852009-02-04 09:24:25 -05001433
Chris Mason0b86a832008-03-24 15:01:56 -04001434 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001435
1436 if (parent)
1437 btrfs_set_lock_blocking(parent);
1438 btrfs_set_lock_blocking(buf);
1439
Chris Masonf510cfe2007-10-15 16:14:48 -04001440 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001441 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001442
1443 trace_btrfs_cow_block(root, buf, *cow_ret);
1444
Chris Masonf510cfe2007-10-15 16:14:48 -04001445 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001446}
1447
Chris Masond352ac62008-09-29 15:18:18 -04001448/*
1449 * helper function for defrag to decide if two blocks pointed to by a
1450 * node are actually close by
1451 */
Chris Mason6b800532007-10-15 16:17:34 -04001452static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001453{
Chris Mason6b800532007-10-15 16:17:34 -04001454 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001455 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001456 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001457 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001458 return 0;
1459}
1460
Chris Mason081e9572007-11-06 10:26:24 -05001461/*
1462 * compare two keys in a memcmp fashion
1463 */
1464static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1465{
1466 struct btrfs_key k1;
1467
1468 btrfs_disk_key_to_cpu(&k1, disk);
1469
Diego Calleja20736ab2009-07-24 11:06:52 -04001470 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001471}
1472
Josef Bacikf3465ca2008-11-12 14:19:50 -05001473/*
1474 * same as comp_keys only with two btrfs_key's
1475 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001476int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001477{
1478 if (k1->objectid > k2->objectid)
1479 return 1;
1480 if (k1->objectid < k2->objectid)
1481 return -1;
1482 if (k1->type > k2->type)
1483 return 1;
1484 if (k1->type < k2->type)
1485 return -1;
1486 if (k1->offset > k2->offset)
1487 return 1;
1488 if (k1->offset < k2->offset)
1489 return -1;
1490 return 0;
1491}
Chris Mason081e9572007-11-06 10:26:24 -05001492
Chris Masond352ac62008-09-29 15:18:18 -04001493/*
1494 * this is used by the defrag code to go through all the
1495 * leaves pointed to by a node and reallocate them so that
1496 * disk order is close to key order
1497 */
Chris Mason6702ed42007-08-07 16:15:09 -04001498int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001499 struct btrfs_root *root, struct extent_buffer *parent,
Eric Sandeende78b512013-01-31 18:21:12 +00001500 int start_slot, u64 *last_ret,
Chris Masona6b6e752007-10-15 16:22:39 -04001501 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001502{
Chris Mason6b800532007-10-15 16:17:34 -04001503 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001504 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001505 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001506 u64 search_start = *last_ret;
1507 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001508 u64 other;
1509 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001510 int end_slot;
1511 int i;
1512 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001513 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001514 int uptodate;
1515 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001516 int progress_passed = 0;
1517 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001518
Chris Mason5708b952007-10-25 15:43:18 -04001519 parent_level = btrfs_header_level(parent);
Chris Mason5708b952007-10-25 15:43:18 -04001520
Julia Lawall6c1500f2012-11-03 20:30:18 +00001521 WARN_ON(trans->transaction != root->fs_info->running_transaction);
1522 WARN_ON(trans->transid != root->fs_info->generation);
Chris Mason86479a02007-09-10 19:58:16 -04001523
Chris Mason6b800532007-10-15 16:17:34 -04001524 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001525 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001526 end_slot = parent_nritems;
1527
1528 if (parent_nritems == 1)
1529 return 0;
1530
Chris Masonb4ce94d2009-02-04 09:25:08 -05001531 btrfs_set_lock_blocking(parent);
1532
Chris Mason6702ed42007-08-07 16:15:09 -04001533 for (i = start_slot; i < end_slot; i++) {
1534 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001535
Chris Mason081e9572007-11-06 10:26:24 -05001536 btrfs_node_key(parent, &disk_key, i);
1537 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1538 continue;
1539
1540 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001541 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001542 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001543 if (last_block == 0)
1544 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001545
Chris Mason6702ed42007-08-07 16:15:09 -04001546 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001547 other = btrfs_node_blockptr(parent, i - 1);
1548 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001549 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001550 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001551 other = btrfs_node_blockptr(parent, i + 1);
1552 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001553 }
Chris Masone9d0b132007-08-10 14:06:19 -04001554 if (close) {
1555 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001556 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001557 }
Chris Mason6702ed42007-08-07 16:15:09 -04001558
Chris Mason6b800532007-10-15 16:17:34 -04001559 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1560 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001561 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001562 else
1563 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001564 if (!cur || !uptodate) {
Chris Mason6b800532007-10-15 16:17:34 -04001565 if (!cur) {
1566 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001567 blocksize, gen);
Josef Bacik416bc652013-04-23 14:17:42 -04001568 if (!cur || !extent_buffer_uptodate(cur)) {
1569 free_extent_buffer(cur);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001570 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04001571 }
Chris Mason6b800532007-10-15 16:17:34 -04001572 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001573 err = btrfs_read_buffer(cur, gen);
1574 if (err) {
1575 free_extent_buffer(cur);
1576 return err;
1577 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001578 }
Chris Mason6702ed42007-08-07 16:15:09 -04001579 }
Chris Masone9d0b132007-08-10 14:06:19 -04001580 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001581 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001582
Chris Masone7a84562008-06-25 16:01:31 -04001583 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001584 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001585 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001586 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001587 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001588 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001589 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001590 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001591 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001592 break;
Yan252c38f2007-08-29 09:11:44 -04001593 }
Chris Masone7a84562008-06-25 16:01:31 -04001594 search_start = cur->start;
1595 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001596 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001597 btrfs_tree_unlock(cur);
1598 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001599 }
1600 return err;
1601}
1602
Chris Mason74123bd2007-02-02 11:05:29 -05001603/*
1604 * The leaf data grows from end-to-front in the node.
1605 * this returns the address of the start of the last item,
1606 * which is the stop of the leaf data stack
1607 */
Chris Mason123abc82007-03-14 14:14:43 -04001608static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001609 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001610{
Chris Mason5f39d392007-10-15 16:14:19 -04001611 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001612 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001613 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001614 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001615}
1616
Chris Masonaa5d6be2007-02-28 16:35:06 -05001617
Chris Mason74123bd2007-02-02 11:05:29 -05001618/*
Chris Mason5f39d392007-10-15 16:14:19 -04001619 * search for key in the extent_buffer. The items start at offset p,
1620 * and they are item_size apart. There are 'max' items in p.
1621 *
Chris Mason74123bd2007-02-02 11:05:29 -05001622 * the slot in the array is returned via slot, and it points to
1623 * the place where you would insert key if it is not found in
1624 * the array.
1625 *
1626 * slot may point to max if the key is bigger than all of the keys
1627 */
Chris Masone02119d2008-09-05 16:13:11 -04001628static noinline int generic_bin_search(struct extent_buffer *eb,
1629 unsigned long p,
1630 int item_size, struct btrfs_key *key,
1631 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001632{
1633 int low = 0;
1634 int high = max;
1635 int mid;
1636 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001637 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001638 struct btrfs_disk_key unaligned;
1639 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001640 char *kaddr = NULL;
1641 unsigned long map_start = 0;
1642 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001643 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001644
Chris Masond3977122009-01-05 21:25:51 -05001645 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001646 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001647 offset = p + mid * item_size;
1648
Chris Masona6591712011-07-19 12:04:14 -04001649 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001650 (offset + sizeof(struct btrfs_disk_key)) >
1651 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001652
1653 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001654 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001655 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001656
Chris Mason479965d2007-10-15 16:14:27 -04001657 if (!err) {
1658 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1659 map_start);
1660 } else {
1661 read_extent_buffer(eb, &unaligned,
1662 offset, sizeof(unaligned));
1663 tmp = &unaligned;
1664 }
1665
Chris Mason5f39d392007-10-15 16:14:19 -04001666 } else {
1667 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1668 map_start);
1669 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001670 ret = comp_keys(tmp, key);
1671
1672 if (ret < 0)
1673 low = mid + 1;
1674 else if (ret > 0)
1675 high = mid;
1676 else {
1677 *slot = mid;
1678 return 0;
1679 }
1680 }
1681 *slot = low;
1682 return 1;
1683}
1684
Chris Mason97571fd2007-02-24 13:39:08 -05001685/*
1686 * simple bin_search frontend that does the right thing for
1687 * leaves vs nodes
1688 */
Chris Mason5f39d392007-10-15 16:14:19 -04001689static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1690 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001691{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001692 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001693 return generic_bin_search(eb,
1694 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001695 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001696 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001697 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001698 else
Chris Mason5f39d392007-10-15 16:14:19 -04001699 return generic_bin_search(eb,
1700 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001701 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001702 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001703 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001704}
1705
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001706int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1707 int level, int *slot)
1708{
1709 return bin_search(eb, key, level, slot);
1710}
1711
Yan, Zhengf0486c62010-05-16 10:46:25 -04001712static void root_add_used(struct btrfs_root *root, u32 size)
1713{
1714 spin_lock(&root->accounting_lock);
1715 btrfs_set_root_used(&root->root_item,
1716 btrfs_root_used(&root->root_item) + size);
1717 spin_unlock(&root->accounting_lock);
1718}
1719
1720static void root_sub_used(struct btrfs_root *root, u32 size)
1721{
1722 spin_lock(&root->accounting_lock);
1723 btrfs_set_root_used(&root->root_item,
1724 btrfs_root_used(&root->root_item) - size);
1725 spin_unlock(&root->accounting_lock);
1726}
1727
Chris Masond352ac62008-09-29 15:18:18 -04001728/* given a node and slot number, this reads the blocks it points to. The
1729 * extent buffer is returned with a reference taken (but unlocked).
1730 * NULL is returned on error.
1731 */
Chris Masone02119d2008-09-05 16:13:11 -04001732static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001733 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001734{
Chris Masonca7a79a2008-05-12 12:59:19 -04001735 int level = btrfs_header_level(parent);
Josef Bacik416bc652013-04-23 14:17:42 -04001736 struct extent_buffer *eb;
1737
Chris Masonbb803952007-03-01 12:04:21 -05001738 if (slot < 0)
1739 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001740 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001741 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001742
1743 BUG_ON(level == 0);
1744
Josef Bacik416bc652013-04-23 14:17:42 -04001745 eb = read_tree_block(root, btrfs_node_blockptr(parent, slot),
1746 btrfs_level_size(root, level - 1),
1747 btrfs_node_ptr_generation(parent, slot));
1748 if (eb && !extent_buffer_uptodate(eb)) {
1749 free_extent_buffer(eb);
1750 eb = NULL;
1751 }
1752
1753 return eb;
Chris Masonbb803952007-03-01 12:04:21 -05001754}
1755
Chris Masond352ac62008-09-29 15:18:18 -04001756/*
1757 * node level balancing, used to make sure nodes are in proper order for
1758 * item deletion. We balance from the top down, so we have to make sure
1759 * that a deletion won't leave an node completely empty later on.
1760 */
Chris Masone02119d2008-09-05 16:13:11 -04001761static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001762 struct btrfs_root *root,
1763 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001764{
Chris Mason5f39d392007-10-15 16:14:19 -04001765 struct extent_buffer *right = NULL;
1766 struct extent_buffer *mid;
1767 struct extent_buffer *left = NULL;
1768 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001769 int ret = 0;
1770 int wret;
1771 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001772 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001773 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001774
1775 if (level == 0)
1776 return 0;
1777
Chris Mason5f39d392007-10-15 16:14:19 -04001778 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001779
Chris Masonbd681512011-07-16 15:23:14 -04001780 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1781 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001782 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1783
Chris Mason1d4f8a02007-03-13 09:28:32 -04001784 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001785
Li Zefana05a9bb2011-09-06 16:55:34 +08001786 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001787 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001788 pslot = path->slots[level + 1];
1789 }
Chris Masonbb803952007-03-01 12:04:21 -05001790
Chris Mason40689472007-03-17 14:29:23 -04001791 /*
1792 * deal with the case where there is only one pointer in the root
1793 * by promoting the node below to a root
1794 */
Chris Mason5f39d392007-10-15 16:14:19 -04001795 if (!parent) {
1796 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001797
Chris Mason5f39d392007-10-15 16:14:19 -04001798 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001799 return 0;
1800
1801 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001802 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001803 if (!child) {
1804 ret = -EROFS;
1805 btrfs_std_error(root->fs_info, ret);
1806 goto enospc;
1807 }
1808
Chris Mason925baed2008-06-25 16:01:30 -04001809 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001810 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001811 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001812 if (ret) {
1813 btrfs_tree_unlock(child);
1814 free_extent_buffer(child);
1815 goto enospc;
1816 }
Yan2f375ab2008-02-01 14:58:07 -05001817
Jan Schmidt90f8d622013-04-13 13:19:53 +00001818 tree_mod_log_set_root_pointer(root, child, 1);
Chris Mason240f62c2011-03-23 14:54:42 -04001819 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001820
Chris Mason0b86a832008-03-24 15:01:56 -04001821 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001822 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001823
Chris Mason925baed2008-06-25 16:01:30 -04001824 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001825 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001826 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001827 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001828 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001829 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001830
1831 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001832 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001833 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001834 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001835 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001836 }
Chris Mason5f39d392007-10-15 16:14:19 -04001837 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001838 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001839 return 0;
1840
Chris Mason5f39d392007-10-15 16:14:19 -04001841 left = read_node_slot(root, parent, pslot - 1);
1842 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001843 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001844 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001845 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001846 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001847 if (wret) {
1848 ret = wret;
1849 goto enospc;
1850 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001851 }
Chris Mason5f39d392007-10-15 16:14:19 -04001852 right = read_node_slot(root, parent, pslot + 1);
1853 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001854 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001855 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001856 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001857 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001858 if (wret) {
1859 ret = wret;
1860 goto enospc;
1861 }
1862 }
1863
1864 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001865 if (left) {
1866 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001867 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001868 if (wret < 0)
1869 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001870 }
Chris Mason79f95c82007-03-01 15:16:26 -05001871
1872 /*
1873 * then try to empty the right most buffer into the middle
1874 */
Chris Mason5f39d392007-10-15 16:14:19 -04001875 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001876 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001877 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001878 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001879 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001880 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001881 btrfs_tree_unlock(right);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001882 del_ptr(root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001883 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001884 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001885 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001886 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001887 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001888 struct btrfs_disk_key right_key;
1889 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001890 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00001891 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001892 btrfs_set_node_key(parent, &right_key, pslot + 1);
1893 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001894 }
1895 }
Chris Mason5f39d392007-10-15 16:14:19 -04001896 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001897 /*
1898 * we're not allowed to leave a node with one item in the
1899 * tree during a delete. A deletion from lower in the tree
1900 * could try to delete the only pointer in this node.
1901 * So, pull some keys from the left.
1902 * There has to be a left pointer at this point because
1903 * otherwise we would have pulled some pointers from the
1904 * right
1905 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001906 if (!left) {
1907 ret = -EROFS;
1908 btrfs_std_error(root->fs_info, ret);
1909 goto enospc;
1910 }
Chris Mason5f39d392007-10-15 16:14:19 -04001911 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001912 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001913 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001914 goto enospc;
1915 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001916 if (wret == 1) {
1917 wret = push_node_left(trans, root, left, mid, 1);
1918 if (wret < 0)
1919 ret = wret;
1920 }
Chris Mason79f95c82007-03-01 15:16:26 -05001921 BUG_ON(wret == 1);
1922 }
Chris Mason5f39d392007-10-15 16:14:19 -04001923 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001924 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001925 btrfs_tree_unlock(mid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001926 del_ptr(root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001927 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001928 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001929 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001930 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001931 } else {
1932 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001933 struct btrfs_disk_key mid_key;
1934 btrfs_node_key(mid, &mid_key, 0);
Liu Bo32adf092012-10-19 12:52:15 +00001935 tree_mod_log_set_node_key(root->fs_info, parent,
Jan Schmidtf2304752012-05-26 11:43:17 +02001936 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001937 btrfs_set_node_key(parent, &mid_key, pslot);
1938 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001939 }
Chris Masonbb803952007-03-01 12:04:21 -05001940
Chris Mason79f95c82007-03-01 15:16:26 -05001941 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001942 if (left) {
1943 if (btrfs_header_nritems(left) > orig_slot) {
1944 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001945 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001946 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001947 path->slots[level + 1] -= 1;
1948 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001949 if (mid) {
1950 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001951 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001952 }
Chris Masonbb803952007-03-01 12:04:21 -05001953 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001954 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001955 path->slots[level] = orig_slot;
1956 }
1957 }
Chris Mason79f95c82007-03-01 15:16:26 -05001958 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001959 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001960 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001961 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001962enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001963 if (right) {
1964 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001965 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001966 }
1967 if (left) {
1968 if (path->nodes[level] != left)
1969 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001970 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001971 }
Chris Masonbb803952007-03-01 12:04:21 -05001972 return ret;
1973}
1974
Chris Masond352ac62008-09-29 15:18:18 -04001975/* Node balancing for insertion. Here we only split or push nodes around
1976 * when they are completely full. This is also done top down, so we
1977 * have to be pessimistic.
1978 */
Chris Masond3977122009-01-05 21:25:51 -05001979static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001980 struct btrfs_root *root,
1981 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001982{
Chris Mason5f39d392007-10-15 16:14:19 -04001983 struct extent_buffer *right = NULL;
1984 struct extent_buffer *mid;
1985 struct extent_buffer *left = NULL;
1986 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001987 int ret = 0;
1988 int wret;
1989 int pslot;
1990 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001991
1992 if (level == 0)
1993 return 1;
1994
Chris Mason5f39d392007-10-15 16:14:19 -04001995 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001996 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001997
Li Zefana05a9bb2011-09-06 16:55:34 +08001998 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001999 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08002000 pslot = path->slots[level + 1];
2001 }
Chris Masone66f7092007-04-20 13:16:02 -04002002
Chris Mason5f39d392007-10-15 16:14:19 -04002003 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04002004 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04002005
Chris Mason5f39d392007-10-15 16:14:19 -04002006 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04002007
2008 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04002009 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04002010 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04002011
2012 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002013 btrfs_set_lock_blocking(left);
2014
Chris Mason5f39d392007-10-15 16:14:19 -04002015 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04002016 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2017 wret = 1;
2018 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002019 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002020 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04002021 if (ret)
2022 wret = 1;
2023 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002024 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04002025 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04002026 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002027 }
Chris Masone66f7092007-04-20 13:16:02 -04002028 if (wret < 0)
2029 ret = wret;
2030 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002031 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04002032 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002034 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002035 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002036 btrfs_set_node_key(parent, &disk_key, pslot);
2037 btrfs_mark_buffer_dirty(parent);
2038 if (btrfs_header_nritems(left) > orig_slot) {
2039 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04002040 path->slots[level + 1] -= 1;
2041 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002042 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002043 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002044 } else {
2045 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04002046 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04002047 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04002048 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002049 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002050 }
Chris Masone66f7092007-04-20 13:16:02 -04002051 return 0;
2052 }
Chris Mason925baed2008-06-25 16:01:30 -04002053 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04002054 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04002055 }
Chris Mason925baed2008-06-25 16:01:30 -04002056 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04002057
2058 /*
2059 * then try to empty the right most buffer into the middle
2060 */
Chris Mason5f39d392007-10-15 16:14:19 -04002061 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04002062 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002063
Chris Mason925baed2008-06-25 16:01:30 -04002064 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002065 btrfs_set_lock_blocking(right);
2066
Chris Mason5f39d392007-10-15 16:14:19 -04002067 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04002068 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
2069 wret = 1;
2070 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04002071 ret = btrfs_cow_block(trans, root, right,
2072 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04002073 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04002074 if (ret)
2075 wret = 1;
2076 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04002077 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04002078 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002079 }
Chris Mason33ade1f2007-04-20 13:48:57 -04002080 }
Chris Masone66f7092007-04-20 13:16:02 -04002081 if (wret < 0)
2082 ret = wret;
2083 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04002084 struct btrfs_disk_key disk_key;
2085
2086 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02002087 tree_mod_log_set_node_key(root->fs_info, parent,
Liu Bo32adf092012-10-19 12:52:15 +00002088 pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002089 btrfs_set_node_key(parent, &disk_key, pslot + 1);
2090 btrfs_mark_buffer_dirty(parent);
2091
2092 if (btrfs_header_nritems(mid) <= orig_slot) {
2093 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04002094 path->slots[level + 1] += 1;
2095 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04002096 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04002097 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002098 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04002099 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002100 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002101 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002102 }
Chris Masone66f7092007-04-20 13:16:02 -04002103 return 0;
2104 }
Chris Mason925baed2008-06-25 16:01:30 -04002105 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002106 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04002107 }
Chris Masone66f7092007-04-20 13:16:02 -04002108 return 1;
2109}
2110
Chris Mason74123bd2007-02-02 11:05:29 -05002111/*
Chris Masond352ac62008-09-29 15:18:18 -04002112 * readahead one full node of leaves, finding things that are close
2113 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04002114 */
Chris Masonc8c42862009-04-03 10:14:18 -04002115static void reada_for_search(struct btrfs_root *root,
2116 struct btrfs_path *path,
2117 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04002118{
Chris Mason5f39d392007-10-15 16:14:19 -04002119 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05002120 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04002121 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04002122 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05002123 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04002124 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002125 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04002126 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04002127 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04002128 u32 nr;
2129 u32 blocksize;
2130 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04002131
Chris Masona6b6e752007-10-15 16:22:39 -04002132 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04002133 return;
2134
Chris Mason6702ed42007-08-07 16:15:09 -04002135 if (!path->nodes[level])
2136 return;
2137
Chris Mason5f39d392007-10-15 16:14:19 -04002138 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04002139
Chris Mason3c69fae2007-08-07 15:52:22 -04002140 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04002141 blocksize = btrfs_level_size(root, level - 1);
2142 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002143 if (eb) {
2144 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04002145 return;
2146 }
2147
Chris Masona7175312009-01-22 09:23:10 -05002148 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04002149
Chris Mason5f39d392007-10-15 16:14:19 -04002150 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04002151 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04002152
Chris Masond3977122009-01-05 21:25:51 -05002153 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04002154 if (direction < 0) {
2155 if (nr == 0)
2156 break;
2157 nr--;
2158 } else if (direction > 0) {
2159 nr++;
2160 if (nr >= nritems)
2161 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002162 }
Chris Mason01f46652007-12-21 16:24:26 -05002163 if (path->reada < 0 && objectid) {
2164 btrfs_node_key(node, &disk_key, nr);
2165 if (btrfs_disk_key_objectid(&disk_key) != objectid)
2166 break;
2167 }
Chris Mason6b800532007-10-15 16:17:34 -04002168 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002169 if ((search <= target && target - search <= 65536) ||
2170 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002171 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002172 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002173 nread += blocksize;
2174 }
2175 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002176 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002177 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002178 }
2179}
Chris Mason925baed2008-06-25 16:01:30 -04002180
Josef Bacik0b088512013-06-17 14:23:02 -04002181static noinline void reada_for_balance(struct btrfs_root *root,
2182 struct btrfs_path *path, int level)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002183{
2184 int slot;
2185 int nritems;
2186 struct extent_buffer *parent;
2187 struct extent_buffer *eb;
2188 u64 gen;
2189 u64 block1 = 0;
2190 u64 block2 = 0;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002191 int blocksize;
2192
Chris Mason8c594ea2009-04-20 15:50:10 -04002193 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002194 if (!parent)
Josef Bacik0b088512013-06-17 14:23:02 -04002195 return;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002196
2197 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002198 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002199 blocksize = btrfs_level_size(root, level);
2200
2201 if (slot > 0) {
2202 block1 = btrfs_node_blockptr(parent, slot - 1);
2203 gen = btrfs_node_ptr_generation(parent, slot - 1);
2204 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002205 /*
2206 * if we get -eagain from btrfs_buffer_uptodate, we
2207 * don't want to return eagain here. That will loop
2208 * forever
2209 */
2210 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002211 block1 = 0;
2212 free_extent_buffer(eb);
2213 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002214 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002215 block2 = btrfs_node_blockptr(parent, slot + 1);
2216 gen = btrfs_node_ptr_generation(parent, slot + 1);
2217 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002218 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002219 block2 = 0;
2220 free_extent_buffer(eb);
2221 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002222
Josef Bacik0b088512013-06-17 14:23:02 -04002223 if (block1)
2224 readahead_tree_block(root, block1, blocksize, 0);
2225 if (block2)
2226 readahead_tree_block(root, block2, blocksize, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002227}
2228
2229
2230/*
Chris Masond3977122009-01-05 21:25:51 -05002231 * when we walk down the tree, it is usually safe to unlock the higher layers
2232 * in the tree. The exceptions are when our path goes through slot 0, because
2233 * operations on the tree might require changing key pointers higher up in the
2234 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002235 *
Chris Masond3977122009-01-05 21:25:51 -05002236 * callers might also have set path->keep_locks, which tells this code to keep
2237 * the lock if the path points to the last slot in the block. This is part of
2238 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002239 *
Chris Masond3977122009-01-05 21:25:51 -05002240 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2241 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002242 */
Chris Masone02119d2008-09-05 16:13:11 -04002243static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002244 int lowest_unlock, int min_write_lock_level,
2245 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002246{
2247 int i;
2248 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002249 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002250 struct extent_buffer *t;
2251
2252 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2253 if (!path->nodes[i])
2254 break;
2255 if (!path->locks[i])
2256 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002257 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002258 skip_level = i + 1;
2259 continue;
2260 }
Chris Mason051e1b92008-06-25 16:01:30 -04002261 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002262 u32 nritems;
2263 t = path->nodes[i];
2264 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002265 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002266 skip_level = i + 1;
2267 continue;
2268 }
2269 }
Chris Mason051e1b92008-06-25 16:01:30 -04002270 if (skip_level < i && i >= lowest_unlock)
2271 no_skips = 1;
2272
Chris Mason925baed2008-06-25 16:01:30 -04002273 t = path->nodes[i];
2274 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002275 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002276 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002277 if (write_lock_level &&
2278 i > min_write_lock_level &&
2279 i <= *write_lock_level) {
2280 *write_lock_level = i - 1;
2281 }
Chris Mason925baed2008-06-25 16:01:30 -04002282 }
2283 }
2284}
2285
Chris Mason3c69fae2007-08-07 15:52:22 -04002286/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002287 * This releases any locks held in the path starting at level and
2288 * going all the way up to the root.
2289 *
2290 * btrfs_search_slot will keep the lock held on higher nodes in a few
2291 * corner cases, such as COW of the block at slot zero in the node. This
2292 * ignores those rules, and it should only be called when there are no
2293 * more updates to be done higher up in the tree.
2294 */
2295noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2296{
2297 int i;
2298
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002299 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002300 return;
2301
2302 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2303 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002304 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002305 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002306 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002307 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002308 path->locks[i] = 0;
2309 }
2310}
2311
2312/*
Chris Masonc8c42862009-04-03 10:14:18 -04002313 * helper function for btrfs_search_slot. The goal is to find a block
2314 * in cache without setting the path to blocking. If we find the block
2315 * we return zero and the path is unchanged.
2316 *
2317 * If we can't find the block, we set the path blocking and do some
2318 * reada. -EAGAIN is returned and the search must be repeated.
2319 */
2320static int
2321read_block_for_search(struct btrfs_trans_handle *trans,
2322 struct btrfs_root *root, struct btrfs_path *p,
2323 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002324 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002325{
2326 u64 blocknr;
2327 u64 gen;
2328 u32 blocksize;
2329 struct extent_buffer *b = *eb_ret;
2330 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002331 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002332
2333 blocknr = btrfs_node_blockptr(b, slot);
2334 gen = btrfs_node_ptr_generation(b, slot);
2335 blocksize = btrfs_level_size(root, level - 1);
2336
2337 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002338 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002339 /* first we do an atomic uptodate check */
Josef Bacikbdf7c002013-06-17 13:44:48 -04002340 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
2341 *eb_ret = tmp;
2342 return 0;
Chris Masoncb449212010-10-24 11:01:27 -04002343 }
Josef Bacikbdf7c002013-06-17 13:44:48 -04002344
2345 /* the pages were up to date, but we failed
2346 * the generation number check. Do a full
2347 * read for the generation number that is correct.
2348 * We must do this without dropping locks so
2349 * we can trust our generation number
2350 */
2351 btrfs_set_path_blocking(p);
2352
2353 /* now we're allowed to do a blocking uptodate check */
2354 ret = btrfs_read_buffer(tmp, gen);
2355 if (!ret) {
2356 *eb_ret = tmp;
2357 return 0;
2358 }
2359 free_extent_buffer(tmp);
2360 btrfs_release_path(p);
2361 return -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002362 }
2363
2364 /*
2365 * reduce lock contention at high levels
2366 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002367 * we read. Don't release the lock on the current
2368 * level because we need to walk this node to figure
2369 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002370 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002371 btrfs_unlock_up_safe(p, level + 1);
2372 btrfs_set_path_blocking(p);
2373
Chris Masoncb449212010-10-24 11:01:27 -04002374 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002375 if (p->reada)
2376 reada_for_search(root, p, level, slot, key->objectid);
2377
David Sterbab3b4aa72011-04-21 01:20:15 +02002378 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002379
2380 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002381 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002382 if (tmp) {
2383 /*
2384 * If the read above didn't mark this buffer up to date,
2385 * it will never end up being up to date. Set ret to EIO now
2386 * and give up so that our caller doesn't loop forever
2387 * on our EAGAINs.
2388 */
Chris Masonb9fab912012-05-06 07:23:47 -04002389 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002390 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002391 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002392 }
2393 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002394}
2395
2396/*
2397 * helper function for btrfs_search_slot. This does all of the checks
2398 * for node-level blocks and does any balancing required based on
2399 * the ins_len.
2400 *
2401 * If no extra work was required, zero is returned. If we had to
2402 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2403 * start over
2404 */
2405static int
2406setup_nodes_for_search(struct btrfs_trans_handle *trans,
2407 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002408 struct extent_buffer *b, int level, int ins_len,
2409 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002410{
2411 int ret;
2412 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2413 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2414 int sret;
2415
Chris Masonbd681512011-07-16 15:23:14 -04002416 if (*write_lock_level < level + 1) {
2417 *write_lock_level = level + 1;
2418 btrfs_release_path(p);
2419 goto again;
2420 }
2421
Chris Masonc8c42862009-04-03 10:14:18 -04002422 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002423 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002424 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002425 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002426
2427 BUG_ON(sret > 0);
2428 if (sret) {
2429 ret = sret;
2430 goto done;
2431 }
2432 b = p->nodes[level];
2433 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002434 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002435 int sret;
2436
Chris Masonbd681512011-07-16 15:23:14 -04002437 if (*write_lock_level < level + 1) {
2438 *write_lock_level = level + 1;
2439 btrfs_release_path(p);
2440 goto again;
2441 }
2442
Chris Masonc8c42862009-04-03 10:14:18 -04002443 btrfs_set_path_blocking(p);
Josef Bacik0b088512013-06-17 14:23:02 -04002444 reada_for_balance(root, p, level);
Chris Masonc8c42862009-04-03 10:14:18 -04002445 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002446 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002447
2448 if (sret) {
2449 ret = sret;
2450 goto done;
2451 }
2452 b = p->nodes[level];
2453 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002454 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002455 goto again;
2456 }
2457 BUG_ON(btrfs_header_nritems(b) == 1);
2458 }
2459 return 0;
2460
2461again:
2462 ret = -EAGAIN;
2463done:
2464 return ret;
2465}
2466
2467/*
Chris Mason74123bd2007-02-02 11:05:29 -05002468 * look for key in the tree. path is filled in with nodes along the way
2469 * if key is found, we return zero and you can find the item in the leaf
2470 * level of the path (level 0)
2471 *
2472 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002473 * be inserted, and 1 is returned. If there are other errors during the
2474 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002475 *
2476 * if ins_len > 0, nodes and leaves will be split as we walk down the
2477 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2478 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002479 */
Chris Masone089f052007-03-16 16:20:31 -04002480int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2481 *root, struct btrfs_key *key, struct btrfs_path *p, int
2482 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002483{
Chris Mason5f39d392007-10-15 16:14:19 -04002484 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002485 int slot;
2486 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002487 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002488 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002489 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002490 int root_lock;
2491 /* everything at write_lock_level or lower must be write locked */
2492 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002493 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002494 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002495
Chris Mason6702ed42007-08-07 16:15:09 -04002496 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002497 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002498 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002499
Chris Masonbd681512011-07-16 15:23:14 -04002500 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002501 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002502
Chris Masonbd681512011-07-16 15:23:14 -04002503 /* when we are removing items, we might have to go up to level
2504 * two as we update tree pointers Make sure we keep write
2505 * for those levels as well
2506 */
2507 write_lock_level = 2;
2508 } else if (ins_len > 0) {
2509 /*
2510 * for inserting items, make sure we have a write lock on
2511 * level 1 so we can update keys
2512 */
2513 write_lock_level = 1;
2514 }
2515
2516 if (!cow)
2517 write_lock_level = -1;
2518
Josef Bacik09a2a8f92013-04-05 16:51:15 -04002519 if (cow && (p->keep_locks || p->lowest_level))
Chris Masonbd681512011-07-16 15:23:14 -04002520 write_lock_level = BTRFS_MAX_LEVEL;
2521
Chris Masonf7c79f32012-03-19 15:54:38 -04002522 min_write_lock_level = write_lock_level;
2523
Chris Masonbb803952007-03-01 12:04:21 -05002524again:
Chris Masonbd681512011-07-16 15:23:14 -04002525 /*
2526 * we try very hard to do read locks on the root
2527 */
2528 root_lock = BTRFS_READ_LOCK;
2529 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002530 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002531 /*
2532 * the commit roots are read only
2533 * so we always do read locks
2534 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002535 b = root->commit_root;
2536 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002537 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002538 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002539 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002540 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002541 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002542 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002543 level = btrfs_header_level(b);
2544 } else {
2545 /* we don't know the level of the root node
2546 * until we actually have it read locked
2547 */
2548 b = btrfs_read_lock_root_node(root);
2549 level = btrfs_header_level(b);
2550 if (level <= write_lock_level) {
2551 /* whoops, must trade for write lock */
2552 btrfs_tree_read_unlock(b);
2553 free_extent_buffer(b);
2554 b = btrfs_lock_root_node(root);
2555 root_lock = BTRFS_WRITE_LOCK;
2556
2557 /* the level might have changed, check again */
2558 level = btrfs_header_level(b);
2559 }
2560 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002561 }
Chris Masonbd681512011-07-16 15:23:14 -04002562 p->nodes[level] = b;
2563 if (!p->skip_locking)
2564 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002565
Chris Masoneb60cea2007-02-02 09:18:22 -05002566 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002567 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002568
2569 /*
2570 * setup the path here so we can release it under lock
2571 * contention with the cow code
2572 */
Chris Mason02217ed2007-03-02 16:08:05 -05002573 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002574 /*
2575 * if we don't really need to cow this block
2576 * then we don't want to set the path blocking,
2577 * so we test it here
2578 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002580 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002581
Chris Masonb4ce94d2009-02-04 09:25:08 -05002582 btrfs_set_path_blocking(p);
2583
Chris Masonbd681512011-07-16 15:23:14 -04002584 /*
2585 * must have write locks on this node and the
2586 * parent
2587 */
Josef Bacik5124e002012-11-07 13:44:13 -05002588 if (level > write_lock_level ||
2589 (level + 1 > write_lock_level &&
2590 level + 1 < BTRFS_MAX_LEVEL &&
2591 p->nodes[level + 1])) {
Chris Masonbd681512011-07-16 15:23:14 -04002592 write_lock_level = level + 1;
2593 btrfs_release_path(p);
2594 goto again;
2595 }
2596
Yan Zheng33c66f42009-07-22 09:59:00 -04002597 err = btrfs_cow_block(trans, root, b,
2598 p->nodes[level + 1],
2599 p->slots[level + 1], &b);
2600 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002601 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002602 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002603 }
Chris Mason02217ed2007-03-02 16:08:05 -05002604 }
Chris Mason65b51a02008-08-01 15:11:20 -04002605cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002606 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002607
Chris Masoneb60cea2007-02-02 09:18:22 -05002608 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002609 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002610
2611 /*
2612 * we have a lock on b and as long as we aren't changing
2613 * the tree, there is no way to for the items in b to change.
2614 * It is safe to drop the lock on our parent before we
2615 * go through the expensive btree search on b.
2616 *
2617 * If cow is true, then we might be changing slot zero,
2618 * which may require changing the parent. So, we can't
2619 * drop the lock until after we know which slot we're
2620 * operating on.
2621 */
2622 if (!cow)
2623 btrfs_unlock_up_safe(p, level + 1);
2624
Chris Mason5f39d392007-10-15 16:14:19 -04002625 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002626
Chris Mason5f39d392007-10-15 16:14:19 -04002627 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002628 int dec = 0;
2629 if (ret && slot > 0) {
2630 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002631 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002632 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002633 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002634 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002635 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002636 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002637 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002638 if (err) {
2639 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002640 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002641 }
Chris Masonc8c42862009-04-03 10:14:18 -04002642 b = p->nodes[level];
2643 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002644
Chris Masonbd681512011-07-16 15:23:14 -04002645 /*
2646 * slot 0 is special, if we change the key
2647 * we have to update the parent pointer
2648 * which means we must have a write lock
2649 * on the parent
2650 */
2651 if (slot == 0 && cow &&
2652 write_lock_level < level + 1) {
2653 write_lock_level = level + 1;
2654 btrfs_release_path(p);
2655 goto again;
2656 }
2657
Chris Masonf7c79f32012-03-19 15:54:38 -04002658 unlock_up(p, level, lowest_unlock,
2659 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002660
Chris Mason925baed2008-06-25 16:01:30 -04002661 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002662 if (dec)
2663 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002664 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002665 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002666
Yan Zheng33c66f42009-07-22 09:59:00 -04002667 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002668 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002669 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002670 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002671 if (err) {
2672 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002673 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002674 }
Chris Mason76a05b32009-05-14 13:24:30 -04002675
Chris Masonb4ce94d2009-02-04 09:25:08 -05002676 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002677 level = btrfs_header_level(b);
2678 if (level <= write_lock_level) {
2679 err = btrfs_try_tree_write_lock(b);
2680 if (!err) {
2681 btrfs_set_path_blocking(p);
2682 btrfs_tree_lock(b);
2683 btrfs_clear_path_blocking(p, b,
2684 BTRFS_WRITE_LOCK);
2685 }
2686 p->locks[level] = BTRFS_WRITE_LOCK;
2687 } else {
2688 err = btrfs_try_tree_read_lock(b);
2689 if (!err) {
2690 btrfs_set_path_blocking(p);
2691 btrfs_tree_read_lock(b);
2692 btrfs_clear_path_blocking(p, b,
2693 BTRFS_READ_LOCK);
2694 }
2695 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002696 }
Chris Masonbd681512011-07-16 15:23:14 -04002697 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002698 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699 } else {
2700 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002701 if (ins_len > 0 &&
2702 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002703 if (write_lock_level < 1) {
2704 write_lock_level = 1;
2705 btrfs_release_path(p);
2706 goto again;
2707 }
2708
Chris Masonb4ce94d2009-02-04 09:25:08 -05002709 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002710 err = split_leaf(trans, root, key,
2711 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002712 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002713
Yan Zheng33c66f42009-07-22 09:59:00 -04002714 BUG_ON(err > 0);
2715 if (err) {
2716 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002717 goto done;
2718 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002719 }
Chris Mason459931e2008-12-10 09:10:46 -05002720 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002721 unlock_up(p, level, lowest_unlock,
2722 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002723 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002724 }
2725 }
Chris Mason65b51a02008-08-01 15:11:20 -04002726 ret = 1;
2727done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002728 /*
2729 * we don't really know what they plan on doing with the path
2730 * from here on, so for now just mark it as blocking
2731 */
Chris Masonb9473432009-03-13 11:00:37 -04002732 if (!p->leave_spinning)
2733 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002734 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002735 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002736 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002737}
2738
Chris Mason74123bd2007-02-02 11:05:29 -05002739/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002740 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2741 * current state of the tree together with the operations recorded in the tree
2742 * modification log to search for the key in a previous version of this tree, as
2743 * denoted by the time_seq parameter.
2744 *
2745 * Naturally, there is no support for insert, delete or cow operations.
2746 *
2747 * The resulting path and return value will be set up as if we called
2748 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2749 */
2750int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2751 struct btrfs_path *p, u64 time_seq)
2752{
2753 struct extent_buffer *b;
2754 int slot;
2755 int ret;
2756 int err;
2757 int level;
2758 int lowest_unlock = 1;
2759 u8 lowest_level = 0;
2760
2761 lowest_level = p->lowest_level;
2762 WARN_ON(p->nodes[0] != NULL);
2763
2764 if (p->search_commit_root) {
2765 BUG_ON(time_seq);
2766 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2767 }
2768
2769again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002770 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002771 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002772 p->locks[level] = BTRFS_READ_LOCK;
2773
2774 while (b) {
2775 level = btrfs_header_level(b);
2776 p->nodes[level] = b;
2777 btrfs_clear_path_blocking(p, NULL, 0);
2778
2779 /*
2780 * we have a lock on b and as long as we aren't changing
2781 * the tree, there is no way to for the items in b to change.
2782 * It is safe to drop the lock on our parent before we
2783 * go through the expensive btree search on b.
2784 */
2785 btrfs_unlock_up_safe(p, level + 1);
2786
2787 ret = bin_search(b, key, level, &slot);
2788
2789 if (level != 0) {
2790 int dec = 0;
2791 if (ret && slot > 0) {
2792 dec = 1;
2793 slot -= 1;
2794 }
2795 p->slots[level] = slot;
2796 unlock_up(p, level, lowest_unlock, 0, NULL);
2797
2798 if (level == lowest_level) {
2799 if (dec)
2800 p->slots[level]++;
2801 goto done;
2802 }
2803
2804 err = read_block_for_search(NULL, root, p, &b, level,
2805 slot, key, time_seq);
2806 if (err == -EAGAIN)
2807 goto again;
2808 if (err) {
2809 ret = err;
2810 goto done;
2811 }
2812
2813 level = btrfs_header_level(b);
2814 err = btrfs_try_tree_read_lock(b);
2815 if (!err) {
2816 btrfs_set_path_blocking(p);
2817 btrfs_tree_read_lock(b);
2818 btrfs_clear_path_blocking(p, b,
2819 BTRFS_READ_LOCK);
2820 }
Jan Schmidt47fb0912013-04-13 13:19:55 +00002821 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002822 p->locks[level] = BTRFS_READ_LOCK;
2823 p->nodes[level] = b;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002824 } else {
2825 p->slots[level] = slot;
2826 unlock_up(p, level, lowest_unlock, 0, NULL);
2827 goto done;
2828 }
2829 }
2830 ret = 1;
2831done:
2832 if (!p->leave_spinning)
2833 btrfs_set_path_blocking(p);
2834 if (ret < 0)
2835 btrfs_release_path(p);
2836
2837 return ret;
2838}
2839
2840/*
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002841 * helper to use instead of search slot if no exact match is needed but
2842 * instead the next or previous item should be returned.
2843 * When find_higher is true, the next higher item is returned, the next lower
2844 * otherwise.
2845 * When return_any and find_higher are both true, and no higher item is found,
2846 * return the next lower instead.
2847 * When return_any is true and find_higher is false, and no lower item is found,
2848 * return the next higher instead.
2849 * It returns 0 if any item is found, 1 if none is found (tree empty), and
2850 * < 0 on error
2851 */
2852int btrfs_search_slot_for_read(struct btrfs_root *root,
2853 struct btrfs_key *key, struct btrfs_path *p,
2854 int find_higher, int return_any)
2855{
2856 int ret;
2857 struct extent_buffer *leaf;
2858
2859again:
2860 ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
2861 if (ret <= 0)
2862 return ret;
2863 /*
2864 * a return value of 1 means the path is at the position where the
2865 * item should be inserted. Normally this is the next bigger item,
2866 * but in case the previous item is the last in a leaf, path points
2867 * to the first free slot in the previous leaf, i.e. at an invalid
2868 * item.
2869 */
2870 leaf = p->nodes[0];
2871
2872 if (find_higher) {
2873 if (p->slots[0] >= btrfs_header_nritems(leaf)) {
2874 ret = btrfs_next_leaf(root, p);
2875 if (ret <= 0)
2876 return ret;
2877 if (!return_any)
2878 return 1;
2879 /*
2880 * no higher item found, return the next
2881 * lower instead
2882 */
2883 return_any = 0;
2884 find_higher = 0;
2885 btrfs_release_path(p);
2886 goto again;
2887 }
2888 } else {
Arne Jansene6793762011-09-13 11:18:10 +02002889 if (p->slots[0] == 0) {
2890 ret = btrfs_prev_leaf(root, p);
2891 if (ret < 0)
2892 return ret;
2893 if (!ret) {
2894 p->slots[0] = btrfs_header_nritems(leaf) - 1;
2895 return 0;
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002896 }
Arne Jansene6793762011-09-13 11:18:10 +02002897 if (!return_any)
2898 return 1;
2899 /*
2900 * no lower item found, return the next
2901 * higher instead
2902 */
2903 return_any = 0;
2904 find_higher = 1;
2905 btrfs_release_path(p);
2906 goto again;
2907 } else {
Arne Jansen2f38b3e2011-09-13 11:18:10 +02002908 --p->slots[0];
2909 }
2910 }
2911 return 0;
2912}
2913
2914/*
Chris Mason74123bd2007-02-02 11:05:29 -05002915 * adjust the pointers going up the tree, starting at level
2916 * making sure the right key of each node is points to 'key'.
2917 * This is used after shifting pointers to the left, so it stops
2918 * fixing up pointers when a given leaf/node is not in slot 0 of the
2919 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002920 *
Chris Mason74123bd2007-02-02 11:05:29 -05002921 */
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002922static void fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002923 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002924{
2925 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002926 struct extent_buffer *t;
2927
Chris Mason234b63a2007-03-13 10:46:10 -04002928 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002929 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002930 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002931 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002932 t = path->nodes[i];
Liu Bo32adf092012-10-19 12:52:15 +00002933 tree_mod_log_set_node_key(root->fs_info, t, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002934 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002935 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002936 if (tslot != 0)
2937 break;
2938 }
2939}
2940
Chris Mason74123bd2007-02-02 11:05:29 -05002941/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002942 * update item key.
2943 *
2944 * This function isn't completely safe. It's the caller's responsibility
2945 * that the new key won't break the order
2946 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00002947void btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01002948 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002949{
2950 struct btrfs_disk_key disk_key;
2951 struct extent_buffer *eb;
2952 int slot;
2953
2954 eb = path->nodes[0];
2955 slot = path->slots[0];
2956 if (slot > 0) {
2957 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002958 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002959 }
2960 if (slot < btrfs_header_nritems(eb) - 1) {
2961 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002962 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002963 }
2964
2965 btrfs_cpu_key_to_disk(&disk_key, new_key);
2966 btrfs_set_item_key(eb, &disk_key, slot);
2967 btrfs_mark_buffer_dirty(eb);
2968 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00002969 fixup_low_keys(root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002970}
2971
2972/*
Chris Mason74123bd2007-02-02 11:05:29 -05002973 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002974 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002975 *
2976 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2977 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002978 */
Chris Mason98ed5172008-01-03 10:01:48 -05002979static int push_node_left(struct btrfs_trans_handle *trans,
2980 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002981 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002982{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002983 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002984 int src_nritems;
2985 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002986 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002987
Chris Mason5f39d392007-10-15 16:14:19 -04002988 src_nritems = btrfs_header_nritems(src);
2989 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002990 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002991 WARN_ON(btrfs_header_generation(src) != trans->transid);
2992 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002993
Chris Masonbce4eae2008-04-24 14:42:46 -04002994 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002995 return 1;
2996
Chris Masond3977122009-01-05 21:25:51 -05002997 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002998 return 1;
2999
Chris Masonbce4eae2008-04-24 14:42:46 -04003000 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04003001 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04003002 if (push_items < src_nritems) {
3003 /* leave at least 8 pointers in the node if
3004 * we aren't going to empty it
3005 */
3006 if (src_nritems - push_items < 8) {
3007 if (push_items <= 8)
3008 return 1;
3009 push_items -= 8;
3010 }
3011 }
3012 } else
3013 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003014
Jan Schmidtf2304752012-05-26 11:43:17 +02003015 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003016 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003017 copy_extent_buffer(dst, src,
3018 btrfs_node_key_ptr_offset(dst_nritems),
3019 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05003020 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04003021
Chris Masonbb803952007-03-01 12:04:21 -05003022 if (push_items < src_nritems) {
Jan Schmidt57911b82012-10-19 09:22:03 +02003023 /*
3024 * don't call tree_mod_log_eb_move here, key removal was already
3025 * fully logged by tree_mod_log_eb_copy above.
3026 */
Chris Mason5f39d392007-10-15 16:14:19 -04003027 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
3028 btrfs_node_key_ptr_offset(push_items),
3029 (src_nritems - push_items) *
3030 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05003031 }
Chris Mason5f39d392007-10-15 16:14:19 -04003032 btrfs_set_header_nritems(src, src_nritems - push_items);
3033 btrfs_set_header_nritems(dst, dst_nritems + push_items);
3034 btrfs_mark_buffer_dirty(src);
3035 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003036
Chris Masonbb803952007-03-01 12:04:21 -05003037 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003038}
3039
Chris Mason97571fd2007-02-24 13:39:08 -05003040/*
Chris Mason79f95c82007-03-01 15:16:26 -05003041 * try to push data from one node into the next node right in the
3042 * tree.
3043 *
3044 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
3045 * error, and > 0 if there was no room in the right hand block.
3046 *
3047 * this will only push up to 1/2 the contents of the left node over
3048 */
Chris Mason5f39d392007-10-15 16:14:19 -04003049static int balance_node_right(struct btrfs_trans_handle *trans,
3050 struct btrfs_root *root,
3051 struct extent_buffer *dst,
3052 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05003053{
Chris Mason79f95c82007-03-01 15:16:26 -05003054 int push_items = 0;
3055 int max_push;
3056 int src_nritems;
3057 int dst_nritems;
3058 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05003059
Chris Mason7bb86312007-12-11 09:25:06 -05003060 WARN_ON(btrfs_header_generation(src) != trans->transid);
3061 WARN_ON(btrfs_header_generation(dst) != trans->transid);
3062
Chris Mason5f39d392007-10-15 16:14:19 -04003063 src_nritems = btrfs_header_nritems(src);
3064 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04003065 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05003066 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05003067 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04003068
Chris Masond3977122009-01-05 21:25:51 -05003069 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04003070 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05003071
3072 max_push = src_nritems / 2 + 1;
3073 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05003074 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05003075 return 1;
Yan252c38f2007-08-29 09:11:44 -04003076
Chris Mason79f95c82007-03-01 15:16:26 -05003077 if (max_push < push_items)
3078 push_items = max_push;
3079
Jan Schmidtf2304752012-05-26 11:43:17 +02003080 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003081 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
3082 btrfs_node_key_ptr_offset(0),
3083 (dst_nritems) *
3084 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04003085
Jan Schmidtf2304752012-05-26 11:43:17 +02003086 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
Jan Schmidt90f8d622013-04-13 13:19:53 +00003087 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04003088 copy_extent_buffer(dst, src,
3089 btrfs_node_key_ptr_offset(0),
3090 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05003091 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05003092
Chris Mason5f39d392007-10-15 16:14:19 -04003093 btrfs_set_header_nritems(src, src_nritems - push_items);
3094 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05003095
Chris Mason5f39d392007-10-15 16:14:19 -04003096 btrfs_mark_buffer_dirty(src);
3097 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04003098
Chris Mason79f95c82007-03-01 15:16:26 -05003099 return ret;
3100}
3101
3102/*
Chris Mason97571fd2007-02-24 13:39:08 -05003103 * helper function to insert a new root level in the tree.
3104 * A new node is allocated, and a single item is inserted to
3105 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05003106 *
3107 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05003108 */
Chris Masond3977122009-01-05 21:25:51 -05003109static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04003110 struct btrfs_root *root,
Liu Bofdd99c72013-05-22 12:06:51 +00003111 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05003112{
Chris Mason7bb86312007-12-11 09:25:06 -05003113 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003114 struct extent_buffer *lower;
3115 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04003116 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04003117 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05003118
3119 BUG_ON(path->nodes[level]);
3120 BUG_ON(path->nodes[level-1] != root->node);
3121
Chris Mason7bb86312007-12-11 09:25:06 -05003122 lower = path->nodes[level-1];
3123 if (level == 1)
3124 btrfs_item_key(lower, &lower_key, 0);
3125 else
3126 btrfs_node_key(lower, &lower_key, 0);
3127
Zheng Yan31840ae2008-09-23 13:14:14 -04003128 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003129 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02003130 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003131 if (IS_ERR(c))
3132 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04003133
Yan, Zhengf0486c62010-05-16 10:46:25 -04003134 root_add_used(root, root->nodesize);
3135
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003136 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003137 btrfs_set_header_nritems(c, 1);
3138 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04003139 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003140 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003141 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003142 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04003143
Chris Mason5f39d392007-10-15 16:14:19 -04003144 write_extent_buffer(c, root->fs_info->fsid,
3145 (unsigned long)btrfs_header_fsid(c),
3146 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003147
3148 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
3149 (unsigned long)btrfs_header_chunk_tree_uuid(c),
3150 BTRFS_UUID_SIZE);
3151
Chris Mason5f39d392007-10-15 16:14:19 -04003152 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04003153 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05003154 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04003155 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05003156
3157 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04003158
3159 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04003160
Chris Mason925baed2008-06-25 16:01:30 -04003161 old = root->node;
Liu Bofdd99c72013-05-22 12:06:51 +00003162 tree_mod_log_set_root_pointer(root, c, 0);
Chris Mason240f62c2011-03-23 14:54:42 -04003163 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04003164
3165 /* the super has an extra ref to root->node */
3166 free_extent_buffer(old);
3167
Chris Mason0b86a832008-03-24 15:01:56 -04003168 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003169 extent_buffer_get(c);
3170 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04003171 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05003172 path->slots[level] = 0;
3173 return 0;
3174}
3175
Chris Mason74123bd2007-02-02 11:05:29 -05003176/*
3177 * worker function to insert a single pointer in a node.
3178 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05003179 *
Chris Mason74123bd2007-02-02 11:05:29 -05003180 * slot and level indicate where you want the key to go, and
3181 * blocknr is the block the key points to.
3182 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003183static void insert_ptr(struct btrfs_trans_handle *trans,
3184 struct btrfs_root *root, struct btrfs_path *path,
3185 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003186 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05003187{
Chris Mason5f39d392007-10-15 16:14:19 -04003188 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05003189 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003190 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05003191
3192 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003193 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04003194 lower = path->nodes[level];
3195 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04003196 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003197 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05003198 if (slot != nritems) {
Jan Schmidtc3e06962012-06-21 11:01:06 +02003199 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003200 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
3201 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003202 memmove_extent_buffer(lower,
3203 btrfs_node_key_ptr_offset(slot + 1),
3204 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003205 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003206 }
Jan Schmidtc3e06962012-06-21 11:01:06 +02003207 if (level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003208 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3209 MOD_LOG_KEY_ADD);
3210 BUG_ON(ret < 0);
3211 }
Chris Mason5f39d392007-10-15 16:14:19 -04003212 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003213 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003214 WARN_ON(trans->transid == 0);
3215 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003216 btrfs_set_header_nritems(lower, nritems + 1);
3217 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003218}
3219
Chris Mason97571fd2007-02-24 13:39:08 -05003220/*
3221 * split the node at the specified level in path in two.
3222 * The path is corrected to point to the appropriate node after the split
3223 *
3224 * Before splitting this tries to make some room in the node by pushing
3225 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003226 *
3227 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003228 */
Chris Masone02119d2008-09-05 16:13:11 -04003229static noinline int split_node(struct btrfs_trans_handle *trans,
3230 struct btrfs_root *root,
3231 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003232{
Chris Mason5f39d392007-10-15 16:14:19 -04003233 struct extent_buffer *c;
3234 struct extent_buffer *split;
3235 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003236 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003237 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003238 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003239
Chris Mason5f39d392007-10-15 16:14:19 -04003240 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003241 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003242 if (c == root->node) {
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003243 /*
Jan Schmidt90f8d622013-04-13 13:19:53 +00003244 * trying to split the root, lets make a new one
3245 *
Liu Bofdd99c72013-05-22 12:06:51 +00003246 * tree mod log: We don't log_removal old root in
Jan Schmidt90f8d622013-04-13 13:19:53 +00003247 * insert_new_root, because that root buffer will be kept as a
3248 * normal node. We are going to log removal of half of the
3249 * elements below with tree_mod_log_eb_copy. We're holding a
3250 * tree lock on the buffer, which is why we cannot race with
3251 * other tree_mod_log users.
Jan Schmidtd9abbf12013-03-20 13:49:48 +00003252 */
Liu Bofdd99c72013-05-22 12:06:51 +00003253 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003254 if (ret)
3255 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003256 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003257 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003258 c = path->nodes[level];
3259 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003260 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003261 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003262 if (ret < 0)
3263 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003264 }
Chris Masone66f7092007-04-20 13:16:02 -04003265
Chris Mason5f39d392007-10-15 16:14:19 -04003266 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003267 mid = (c_nritems + 1) / 2;
3268 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003269
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003270 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003271 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003272 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003273 if (IS_ERR(split))
3274 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003275
Yan, Zhengf0486c62010-05-16 10:46:25 -04003276 root_add_used(root, root->nodesize);
3277
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003278 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003279 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003280 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003281 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003282 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003283 btrfs_set_header_owner(split, root->root_key.objectid);
3284 write_extent_buffer(split, root->fs_info->fsid,
3285 (unsigned long)btrfs_header_fsid(split),
3286 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003287 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3288 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3289 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003290
Jan Schmidt90f8d622013-04-13 13:19:53 +00003291 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003292 copy_extent_buffer(split, c,
3293 btrfs_node_key_ptr_offset(0),
3294 btrfs_node_key_ptr_offset(mid),
3295 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3296 btrfs_set_header_nritems(split, c_nritems - mid);
3297 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003298 ret = 0;
3299
Chris Mason5f39d392007-10-15 16:14:19 -04003300 btrfs_mark_buffer_dirty(c);
3301 btrfs_mark_buffer_dirty(split);
3302
Jeff Mahoney143bede2012-03-01 14:56:26 +01003303 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003304 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003305
Chris Mason5de08d72007-02-24 06:24:44 -05003306 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003307 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003308 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003309 free_extent_buffer(c);
3310 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003311 path->slots[level + 1] += 1;
3312 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003313 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003314 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003315 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003316 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003317}
3318
Chris Mason74123bd2007-02-02 11:05:29 -05003319/*
3320 * how many bytes are required to store the items in a leaf. start
3321 * and nr indicate which items in the leaf to check. This totals up the
3322 * space used both by the item structs and the item data
3323 */
Chris Mason5f39d392007-10-15 16:14:19 -04003324static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003325{
Josef Bacik41be1f32012-10-15 13:43:18 -04003326 struct btrfs_item *start_item;
3327 struct btrfs_item *end_item;
3328 struct btrfs_map_token token;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003329 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003330 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003331 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003332
3333 if (!nr)
3334 return 0;
Josef Bacik41be1f32012-10-15 13:43:18 -04003335 btrfs_init_map_token(&token);
3336 start_item = btrfs_item_nr(l, start);
3337 end_item = btrfs_item_nr(l, end);
3338 data_len = btrfs_token_item_offset(l, start_item, &token) +
3339 btrfs_token_item_size(l, start_item, &token);
3340 data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003341 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003342 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003343 return data_len;
3344}
3345
Chris Mason74123bd2007-02-02 11:05:29 -05003346/*
Chris Masond4dbff92007-04-04 14:08:15 -04003347 * The space between the end of the leaf items and
3348 * the start of the leaf data. IOW, how much room
3349 * the leaf has left for both items and data
3350 */
Chris Masond3977122009-01-05 21:25:51 -05003351noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003352 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003353{
Chris Mason5f39d392007-10-15 16:14:19 -04003354 int nritems = btrfs_header_nritems(leaf);
3355 int ret;
3356 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3357 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003358 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3359 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003360 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003361 leaf_space_used(leaf, 0, nritems), nritems);
3362 }
3363 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003364}
3365
Chris Mason99d8f832010-07-07 10:51:48 -04003366/*
3367 * min slot controls the lowest index we're willing to push to the
3368 * right. We'll push up to and including min_slot, but no lower
3369 */
Chris Mason44871b12009-03-13 10:04:31 -04003370static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3371 struct btrfs_root *root,
3372 struct btrfs_path *path,
3373 int data_size, int empty,
3374 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003375 int free_space, u32 left_nritems,
3376 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003377{
Chris Mason5f39d392007-10-15 16:14:19 -04003378 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003379 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003380 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003381 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003382 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003383 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003384 int push_space = 0;
3385 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003386 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003387 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003388 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003389 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003390 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003391
Chris Masoncfed81a2012-03-03 07:40:03 -05003392 btrfs_init_map_token(&token);
3393
Chris Mason34a38212007-11-07 13:31:03 -05003394 if (empty)
3395 nr = 0;
3396 else
Chris Mason99d8f832010-07-07 10:51:48 -04003397 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003398
Zheng Yan31840ae2008-09-23 13:14:14 -04003399 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003400 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003401
Chris Mason44871b12009-03-13 10:04:31 -04003402 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003403 i = left_nritems - 1;
3404 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003405 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003406
Zheng Yan31840ae2008-09-23 13:14:14 -04003407 if (!empty && push_items > 0) {
3408 if (path->slots[0] > i)
3409 break;
3410 if (path->slots[0] == i) {
3411 int space = btrfs_leaf_free_space(root, left);
3412 if (space + push_space * 2 > free_space)
3413 break;
3414 }
3415 }
3416
Chris Mason00ec4c52007-02-24 12:47:20 -05003417 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003418 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003419
Chris Masondb945352007-10-15 16:15:53 -04003420 this_item_size = btrfs_item_size(left, item);
3421 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003422 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003423
Chris Mason00ec4c52007-02-24 12:47:20 -05003424 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003425 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003426 if (i == 0)
3427 break;
3428 i--;
Chris Masondb945352007-10-15 16:15:53 -04003429 }
Chris Mason5f39d392007-10-15 16:14:19 -04003430
Chris Mason925baed2008-06-25 16:01:30 -04003431 if (push_items == 0)
3432 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003433
Julia Lawall6c1500f2012-11-03 20:30:18 +00003434 WARN_ON(!empty && push_items == left_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003435
Chris Mason00ec4c52007-02-24 12:47:20 -05003436 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003437 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003438
Chris Mason5f39d392007-10-15 16:14:19 -04003439 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003440 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003441
Chris Mason00ec4c52007-02-24 12:47:20 -05003442 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003443 data_end = leaf_data_end(root, right);
3444 memmove_extent_buffer(right,
3445 btrfs_leaf_data(right) + data_end - push_space,
3446 btrfs_leaf_data(right) + data_end,
3447 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3448
Chris Mason00ec4c52007-02-24 12:47:20 -05003449 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003450 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003451 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3452 btrfs_leaf_data(left) + leaf_data_end(root, left),
3453 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003454
3455 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3456 btrfs_item_nr_offset(0),
3457 right_nritems * sizeof(struct btrfs_item));
3458
Chris Mason00ec4c52007-02-24 12:47:20 -05003459 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003460 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3461 btrfs_item_nr_offset(left_nritems - push_items),
3462 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003463
3464 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003465 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003466 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003467 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003468 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003469 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003470 push_space -= btrfs_token_item_size(right, item, &token);
3471 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003472 }
3473
Chris Mason7518a232007-03-12 12:01:18 -04003474 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003475 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003476
Chris Mason34a38212007-11-07 13:31:03 -05003477 if (left_nritems)
3478 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003479 else
3480 clean_tree_block(trans, root, left);
3481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003483
Chris Mason5f39d392007-10-15 16:14:19 -04003484 btrfs_item_key(right, &disk_key, 0);
3485 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003486 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003487
Chris Mason00ec4c52007-02-24 12:47:20 -05003488 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003489 if (path->slots[0] >= left_nritems) {
3490 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003491 if (btrfs_header_nritems(path->nodes[0]) == 0)
3492 clean_tree_block(trans, root, path->nodes[0]);
3493 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003494 free_extent_buffer(path->nodes[0]);
3495 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003496 path->slots[1] += 1;
3497 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003498 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003499 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003500 }
3501 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003502
3503out_unlock:
3504 btrfs_tree_unlock(right);
3505 free_extent_buffer(right);
3506 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003507}
Chris Mason925baed2008-06-25 16:01:30 -04003508
Chris Mason00ec4c52007-02-24 12:47:20 -05003509/*
Chris Mason44871b12009-03-13 10:04:31 -04003510 * push some data in the path leaf to the right, trying to free up at
3511 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3512 *
3513 * returns 1 if the push failed because the other node didn't have enough
3514 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003515 *
3516 * this will push starting from min_slot to the end of the leaf. It won't
3517 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003518 */
3519static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003520 *root, struct btrfs_path *path,
3521 int min_data_size, int data_size,
3522 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003523{
3524 struct extent_buffer *left = path->nodes[0];
3525 struct extent_buffer *right;
3526 struct extent_buffer *upper;
3527 int slot;
3528 int free_space;
3529 u32 left_nritems;
3530 int ret;
3531
3532 if (!path->nodes[1])
3533 return 1;
3534
3535 slot = path->slots[1];
3536 upper = path->nodes[1];
3537 if (slot >= btrfs_header_nritems(upper) - 1)
3538 return 1;
3539
3540 btrfs_assert_tree_locked(path->nodes[1]);
3541
3542 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003543 if (right == NULL)
3544 return 1;
3545
Chris Mason44871b12009-03-13 10:04:31 -04003546 btrfs_tree_lock(right);
3547 btrfs_set_lock_blocking(right);
3548
3549 free_space = btrfs_leaf_free_space(root, right);
3550 if (free_space < data_size)
3551 goto out_unlock;
3552
3553 /* cow and double check */
3554 ret = btrfs_cow_block(trans, root, right, upper,
3555 slot + 1, &right);
3556 if (ret)
3557 goto out_unlock;
3558
3559 free_space = btrfs_leaf_free_space(root, right);
3560 if (free_space < data_size)
3561 goto out_unlock;
3562
3563 left_nritems = btrfs_header_nritems(left);
3564 if (left_nritems == 0)
3565 goto out_unlock;
3566
Chris Mason99d8f832010-07-07 10:51:48 -04003567 return __push_leaf_right(trans, root, path, min_data_size, empty,
3568 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003569out_unlock:
3570 btrfs_tree_unlock(right);
3571 free_extent_buffer(right);
3572 return 1;
3573}
3574
3575/*
Chris Mason74123bd2007-02-02 11:05:29 -05003576 * push some data in the path leaf to the left, trying to free up at
3577 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003578 *
3579 * max_slot can put a limit on how far into the leaf we'll push items. The
3580 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3581 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003582 */
Chris Mason44871b12009-03-13 10:04:31 -04003583static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3584 struct btrfs_root *root,
3585 struct btrfs_path *path, int data_size,
3586 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003587 int free_space, u32 right_nritems,
3588 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003589{
Chris Mason5f39d392007-10-15 16:14:19 -04003590 struct btrfs_disk_key disk_key;
3591 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003592 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593 int push_space = 0;
3594 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003595 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003596 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003597 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003598 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003599 u32 this_item_size;
3600 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003601 struct btrfs_map_token token;
3602
3603 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003604
Chris Mason34a38212007-11-07 13:31:03 -05003605 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003606 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003607 else
Chris Mason99d8f832010-07-07 10:51:48 -04003608 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003609
3610 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003611 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003612
Zheng Yan31840ae2008-09-23 13:14:14 -04003613 if (!empty && push_items > 0) {
3614 if (path->slots[0] < i)
3615 break;
3616 if (path->slots[0] == i) {
3617 int space = btrfs_leaf_free_space(root, right);
3618 if (space + push_space * 2 > free_space)
3619 break;
3620 }
3621 }
3622
Chris Masonbe0e5c02007-01-26 15:51:26 -05003623 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003624 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003625
3626 this_item_size = btrfs_item_size(right, item);
3627 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003628 break;
Chris Masondb945352007-10-15 16:15:53 -04003629
Chris Masonbe0e5c02007-01-26 15:51:26 -05003630 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003631 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632 }
Chris Masondb945352007-10-15 16:15:53 -04003633
Chris Masonbe0e5c02007-01-26 15:51:26 -05003634 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003635 ret = 1;
3636 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003637 }
Chris Mason34a38212007-11-07 13:31:03 -05003638 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003639 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003640
Chris Masonbe0e5c02007-01-26 15:51:26 -05003641 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003642 copy_extent_buffer(left, right,
3643 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3644 btrfs_item_nr_offset(0),
3645 push_items * sizeof(struct btrfs_item));
3646
Chris Mason123abc82007-03-14 14:14:43 -04003647 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003648 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003649
3650 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003651 leaf_data_end(root, left) - push_space,
3652 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003653 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003654 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003655 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003656 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003657
Chris Masondb945352007-10-15 16:15:53 -04003658 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003659 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003660 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003661
Chris Mason5f39d392007-10-15 16:14:19 -04003662 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003663
Chris Masoncfed81a2012-03-03 07:40:03 -05003664 ioff = btrfs_token_item_offset(left, item, &token);
3665 btrfs_set_token_item_offset(left, item,
3666 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3667 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003668 }
Chris Mason5f39d392007-10-15 16:14:19 -04003669 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003670
3671 /* fixup right node */
Julia Lawall31b1a2b2012-11-03 10:58:34 +00003672 if (push_items > right_nritems)
3673 WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
Chris Masond3977122009-01-05 21:25:51 -05003674 right_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04003675
Chris Mason34a38212007-11-07 13:31:03 -05003676 if (push_items < right_nritems) {
3677 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3678 leaf_data_end(root, right);
3679 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3680 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3681 btrfs_leaf_data(right) +
3682 leaf_data_end(root, right), push_space);
3683
3684 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003685 btrfs_item_nr_offset(push_items),
3686 (btrfs_header_nritems(right) - push_items) *
3687 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003688 }
Yaneef1c492007-11-26 10:58:13 -05003689 right_nritems -= push_items;
3690 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003691 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003692 for (i = 0; i < right_nritems; i++) {
3693 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003694
Chris Masoncfed81a2012-03-03 07:40:03 -05003695 push_space = push_space - btrfs_token_item_size(right,
3696 item, &token);
3697 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003698 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003699
Chris Mason5f39d392007-10-15 16:14:19 -04003700 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003701 if (right_nritems)
3702 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003703 else
3704 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003705
Chris Mason5f39d392007-10-15 16:14:19 -04003706 btrfs_item_key(right, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00003707 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003708
3709 /* then fixup the leaf pointer in the path */
3710 if (path->slots[0] < push_items) {
3711 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003712 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003713 free_extent_buffer(path->nodes[0]);
3714 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003715 path->slots[1] -= 1;
3716 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003717 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003718 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003719 path->slots[0] -= push_items;
3720 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003721 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003722 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003723out:
3724 btrfs_tree_unlock(left);
3725 free_extent_buffer(left);
3726 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003727}
3728
Chris Mason74123bd2007-02-02 11:05:29 -05003729/*
Chris Mason44871b12009-03-13 10:04:31 -04003730 * push some data in the path leaf to the left, trying to free up at
3731 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003732 *
3733 * max_slot can put a limit on how far into the leaf we'll push items. The
3734 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3735 * items
Chris Mason44871b12009-03-13 10:04:31 -04003736 */
3737static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003738 *root, struct btrfs_path *path, int min_data_size,
3739 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003740{
3741 struct extent_buffer *right = path->nodes[0];
3742 struct extent_buffer *left;
3743 int slot;
3744 int free_space;
3745 u32 right_nritems;
3746 int ret = 0;
3747
3748 slot = path->slots[1];
3749 if (slot == 0)
3750 return 1;
3751 if (!path->nodes[1])
3752 return 1;
3753
3754 right_nritems = btrfs_header_nritems(right);
3755 if (right_nritems == 0)
3756 return 1;
3757
3758 btrfs_assert_tree_locked(path->nodes[1]);
3759
3760 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003761 if (left == NULL)
3762 return 1;
3763
Chris Mason44871b12009-03-13 10:04:31 -04003764 btrfs_tree_lock(left);
3765 btrfs_set_lock_blocking(left);
3766
3767 free_space = btrfs_leaf_free_space(root, left);
3768 if (free_space < data_size) {
3769 ret = 1;
3770 goto out;
3771 }
3772
3773 /* cow and double check */
3774 ret = btrfs_cow_block(trans, root, left,
3775 path->nodes[1], slot - 1, &left);
3776 if (ret) {
3777 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003778 if (ret == -ENOSPC)
3779 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003780 goto out;
3781 }
3782
3783 free_space = btrfs_leaf_free_space(root, left);
3784 if (free_space < data_size) {
3785 ret = 1;
3786 goto out;
3787 }
3788
Chris Mason99d8f832010-07-07 10:51:48 -04003789 return __push_leaf_left(trans, root, path, min_data_size,
3790 empty, left, free_space, right_nritems,
3791 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003792out:
3793 btrfs_tree_unlock(left);
3794 free_extent_buffer(left);
3795 return ret;
3796}
3797
3798/*
Chris Mason74123bd2007-02-02 11:05:29 -05003799 * split the path's leaf in two, making sure there is at least data_size
3800 * available for the resulting leaf level of the path.
3801 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003802static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3803 struct btrfs_root *root,
3804 struct btrfs_path *path,
3805 struct extent_buffer *l,
3806 struct extent_buffer *right,
3807 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003808{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003809 int data_copy_size;
3810 int rt_data_off;
3811 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003812 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003813 struct btrfs_map_token token;
3814
3815 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003816
Chris Mason5f39d392007-10-15 16:14:19 -04003817 nritems = nritems - mid;
3818 btrfs_set_header_nritems(right, nritems);
3819 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3820
3821 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3822 btrfs_item_nr_offset(mid),
3823 nritems * sizeof(struct btrfs_item));
3824
3825 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003826 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3827 data_copy_size, btrfs_leaf_data(l) +
3828 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003829
Chris Mason5f39d392007-10-15 16:14:19 -04003830 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3831 btrfs_item_end_nr(l, mid);
3832
3833 for (i = 0; i < nritems; i++) {
3834 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003835 u32 ioff;
3836
Chris Masoncfed81a2012-03-03 07:40:03 -05003837 ioff = btrfs_token_item_offset(right, item, &token);
3838 btrfs_set_token_item_offset(right, item,
3839 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003840 }
Chris Mason74123bd2007-02-02 11:05:29 -05003841
Chris Mason5f39d392007-10-15 16:14:19 -04003842 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003843 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003844 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02003845 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003846
3847 btrfs_mark_buffer_dirty(right);
3848 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003849 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003850
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003852 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003853 free_extent_buffer(path->nodes[0]);
3854 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003855 path->slots[0] -= mid;
3856 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003857 } else {
3858 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003859 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003860 }
Chris Mason5f39d392007-10-15 16:14:19 -04003861
Chris Masoneb60cea2007-02-02 09:18:22 -05003862 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003863}
3864
3865/*
Chris Mason99d8f832010-07-07 10:51:48 -04003866 * double splits happen when we need to insert a big item in the middle
3867 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3868 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3869 * A B C
3870 *
3871 * We avoid this by trying to push the items on either side of our target
3872 * into the adjacent leaves. If all goes well we can avoid the double split
3873 * completely.
3874 */
3875static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3876 struct btrfs_root *root,
3877 struct btrfs_path *path,
3878 int data_size)
3879{
3880 int ret;
3881 int progress = 0;
3882 int slot;
3883 u32 nritems;
3884
3885 slot = path->slots[0];
3886
3887 /*
3888 * try to push all the items after our slot into the
3889 * right leaf
3890 */
3891 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3892 if (ret < 0)
3893 return ret;
3894
3895 if (ret == 0)
3896 progress++;
3897
3898 nritems = btrfs_header_nritems(path->nodes[0]);
3899 /*
3900 * our goal is to get our slot at the start or end of a leaf. If
3901 * we've done so we're done
3902 */
3903 if (path->slots[0] == 0 || path->slots[0] == nritems)
3904 return 0;
3905
3906 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3907 return 0;
3908
3909 /* try to push all the items before our slot into the next leaf */
3910 slot = path->slots[0];
3911 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3912 if (ret < 0)
3913 return ret;
3914
3915 if (ret == 0)
3916 progress++;
3917
3918 if (progress)
3919 return 0;
3920 return 1;
3921}
3922
3923/*
Chris Mason44871b12009-03-13 10:04:31 -04003924 * split the path's leaf in two, making sure there is at least data_size
3925 * available for the resulting leaf level of the path.
3926 *
3927 * returns 0 if all went well and < 0 on failure.
3928 */
3929static noinline int split_leaf(struct btrfs_trans_handle *trans,
3930 struct btrfs_root *root,
3931 struct btrfs_key *ins_key,
3932 struct btrfs_path *path, int data_size,
3933 int extend)
3934{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003935 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003936 struct extent_buffer *l;
3937 u32 nritems;
3938 int mid;
3939 int slot;
3940 struct extent_buffer *right;
3941 int ret = 0;
3942 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003943 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003944 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003945 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003946
Yan, Zhenga5719522009-09-24 09:17:31 -04003947 l = path->nodes[0];
3948 slot = path->slots[0];
3949 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3950 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3951 return -EOVERFLOW;
3952
Chris Mason44871b12009-03-13 10:04:31 -04003953 /* first try to make some room by pushing left and right */
Liu Bo33157e02013-05-22 12:07:06 +00003954 if (data_size && path->nodes[1]) {
Chris Mason99d8f832010-07-07 10:51:48 -04003955 wret = push_leaf_right(trans, root, path, data_size,
3956 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003957 if (wret < 0)
3958 return wret;
3959 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003960 wret = push_leaf_left(trans, root, path, data_size,
3961 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003962 if (wret < 0)
3963 return wret;
3964 }
3965 l = path->nodes[0];
3966
3967 /* did the pushes work? */
3968 if (btrfs_leaf_free_space(root, l) >= data_size)
3969 return 0;
3970 }
3971
3972 if (!path->nodes[1]) {
Liu Bofdd99c72013-05-22 12:06:51 +00003973 ret = insert_new_root(trans, root, path, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003974 if (ret)
3975 return ret;
3976 }
3977again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003978 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003979 l = path->nodes[0];
3980 slot = path->slots[0];
3981 nritems = btrfs_header_nritems(l);
3982 mid = (nritems + 1) / 2;
3983
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003984 if (mid <= slot) {
3985 if (nritems == 1 ||
3986 leaf_space_used(l, mid, nritems - mid) + data_size >
3987 BTRFS_LEAF_DATA_SIZE(root)) {
3988 if (slot >= nritems) {
3989 split = 0;
3990 } else {
3991 mid = slot;
3992 if (mid != nritems &&
3993 leaf_space_used(l, mid, nritems - mid) +
3994 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003995 if (data_size && !tried_avoid_double)
3996 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003997 split = 2;
3998 }
3999 }
4000 }
4001 } else {
4002 if (leaf_space_used(l, 0, mid) + data_size >
4003 BTRFS_LEAF_DATA_SIZE(root)) {
4004 if (!extend && data_size && slot == 0) {
4005 split = 0;
4006 } else if ((extend || !data_size) && slot == 0) {
4007 mid = 1;
4008 } else {
4009 mid = slot;
4010 if (mid != nritems &&
4011 leaf_space_used(l, mid, nritems - mid) +
4012 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004013 if (data_size && !tried_avoid_double)
4014 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004015 split = 2 ;
4016 }
4017 }
4018 }
4019 }
4020
4021 if (split == 0)
4022 btrfs_cpu_key_to_disk(&disk_key, ins_key);
4023 else
4024 btrfs_item_key(l, &disk_key, mid);
4025
4026 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04004027 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02004028 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004029 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04004030 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004031
4032 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04004033
4034 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
4035 btrfs_set_header_bytenr(right, right->start);
4036 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004037 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04004038 btrfs_set_header_owner(right, root->root_key.objectid);
4039 btrfs_set_header_level(right, 0);
4040 write_extent_buffer(right, root->fs_info->fsid,
4041 (unsigned long)btrfs_header_fsid(right),
4042 BTRFS_FSID_SIZE);
4043
4044 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
4045 (unsigned long)btrfs_header_chunk_tree_uuid(right),
4046 BTRFS_UUID_SIZE);
4047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004048 if (split == 0) {
4049 if (mid <= slot) {
4050 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004051 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004052 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004053 btrfs_tree_unlock(path->nodes[0]);
4054 free_extent_buffer(path->nodes[0]);
4055 path->nodes[0] = right;
4056 path->slots[0] = 0;
4057 path->slots[1] += 1;
4058 } else {
4059 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004060 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtc3e06962012-06-21 11:01:06 +02004061 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004062 btrfs_tree_unlock(path->nodes[0]);
4063 free_extent_buffer(path->nodes[0]);
4064 path->nodes[0] = right;
4065 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004066 if (path->slots[1] == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004067 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04004068 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004069 btrfs_mark_buffer_dirty(right);
4070 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004071 }
4072
Jeff Mahoney143bede2012-03-01 14:56:26 +01004073 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04004074
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004075 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04004076 BUG_ON(num_doubles != 0);
4077 num_doubles++;
4078 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04004079 }
Chris Mason44871b12009-03-13 10:04:31 -04004080
Jeff Mahoney143bede2012-03-01 14:56:26 +01004081 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04004082
4083push_for_double:
4084 push_for_double_split(trans, root, path, data_size);
4085 tried_avoid_double = 1;
4086 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
4087 return 0;
4088 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004089}
4090
Yan, Zhengad48fd752009-11-12 09:33:58 +00004091static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
4092 struct btrfs_root *root,
4093 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05004094{
Yan, Zhengad48fd752009-11-12 09:33:58 +00004095 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05004096 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004097 struct btrfs_file_extent_item *fi;
4098 u64 extent_len = 0;
4099 u32 item_size;
4100 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05004101
4102 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00004103 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4104
4105 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
4106 key.type != BTRFS_EXTENT_CSUM_KEY);
4107
4108 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
4109 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05004110
4111 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004112 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4113 fi = btrfs_item_ptr(leaf, path->slots[0],
4114 struct btrfs_file_extent_item);
4115 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
4116 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004117 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05004118
Chris Mason459931e2008-12-10 09:10:46 -05004119 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004120 path->search_for_split = 1;
4121 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05004122 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00004123 if (ret < 0)
4124 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004125
Yan, Zhengad48fd752009-11-12 09:33:58 +00004126 ret = -EAGAIN;
4127 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05004128 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00004129 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
4130 goto err;
4131
Chris Mason109f6ae2010-04-02 09:20:18 -04004132 /* the leaf has changed, it now has room. return now */
4133 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
4134 goto err;
4135
Yan, Zhengad48fd752009-11-12 09:33:58 +00004136 if (key.type == BTRFS_EXTENT_DATA_KEY) {
4137 fi = btrfs_item_ptr(leaf, path->slots[0],
4138 struct btrfs_file_extent_item);
4139 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
4140 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004141 }
4142
Chris Masonb9473432009-03-13 11:00:37 -04004143 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004144 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004145 if (ret)
4146 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05004147
Yan, Zhengad48fd752009-11-12 09:33:58 +00004148 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04004149 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004150 return 0;
4151err:
4152 path->keep_locks = 0;
4153 return ret;
4154}
4155
4156static noinline int split_item(struct btrfs_trans_handle *trans,
4157 struct btrfs_root *root,
4158 struct btrfs_path *path,
4159 struct btrfs_key *new_key,
4160 unsigned long split_offset)
4161{
4162 struct extent_buffer *leaf;
4163 struct btrfs_item *item;
4164 struct btrfs_item *new_item;
4165 int slot;
4166 char *buf;
4167 u32 nritems;
4168 u32 item_size;
4169 u32 orig_offset;
4170 struct btrfs_disk_key disk_key;
4171
Chris Masonb9473432009-03-13 11:00:37 -04004172 leaf = path->nodes[0];
4173 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
4174
Chris Masonb4ce94d2009-02-04 09:25:08 -05004175 btrfs_set_path_blocking(path);
4176
Chris Mason459931e2008-12-10 09:10:46 -05004177 item = btrfs_item_nr(leaf, path->slots[0]);
4178 orig_offset = btrfs_item_offset(leaf, item);
4179 item_size = btrfs_item_size(leaf, item);
4180
Chris Mason459931e2008-12-10 09:10:46 -05004181 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004182 if (!buf)
4183 return -ENOMEM;
4184
Chris Mason459931e2008-12-10 09:10:46 -05004185 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
4186 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004187
Chris Mason459931e2008-12-10 09:10:46 -05004188 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05004189 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05004190 if (slot != nritems) {
4191 /* shift the items */
4192 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00004193 btrfs_item_nr_offset(slot),
4194 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05004195 }
4196
4197 btrfs_cpu_key_to_disk(&disk_key, new_key);
4198 btrfs_set_item_key(leaf, &disk_key, slot);
4199
4200 new_item = btrfs_item_nr(leaf, slot);
4201
4202 btrfs_set_item_offset(leaf, new_item, orig_offset);
4203 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
4204
4205 btrfs_set_item_offset(leaf, item,
4206 orig_offset + item_size - split_offset);
4207 btrfs_set_item_size(leaf, item, split_offset);
4208
4209 btrfs_set_header_nritems(leaf, nritems + 1);
4210
4211 /* write the data for the start of the original item */
4212 write_extent_buffer(leaf, buf,
4213 btrfs_item_ptr_offset(leaf, path->slots[0]),
4214 split_offset);
4215
4216 /* write the data for the new item */
4217 write_extent_buffer(leaf, buf + split_offset,
4218 btrfs_item_ptr_offset(leaf, slot),
4219 item_size - split_offset);
4220 btrfs_mark_buffer_dirty(leaf);
4221
Yan, Zhengad48fd752009-11-12 09:33:58 +00004222 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004223 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004224 return 0;
4225}
4226
4227/*
4228 * This function splits a single item into two items,
4229 * giving 'new_key' to the new item and splitting the
4230 * old one at split_offset (from the start of the item).
4231 *
4232 * The path may be released by this operation. After
4233 * the split, the path is pointing to the old item. The
4234 * new item is going to be in the same node as the old one.
4235 *
4236 * Note, the item being split must be smaller enough to live alone on
4237 * a tree block with room for one extra struct btrfs_item
4238 *
4239 * This allows us to split the item in place, keeping a lock on the
4240 * leaf the entire time.
4241 */
4242int btrfs_split_item(struct btrfs_trans_handle *trans,
4243 struct btrfs_root *root,
4244 struct btrfs_path *path,
4245 struct btrfs_key *new_key,
4246 unsigned long split_offset)
4247{
4248 int ret;
4249 ret = setup_leaf_for_split(trans, root, path,
4250 sizeof(struct btrfs_item));
4251 if (ret)
4252 return ret;
4253
4254 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004255 return ret;
4256}
4257
4258/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004259 * This function duplicate a item, giving 'new_key' to the new item.
4260 * It guarantees both items live in the same tree leaf and the new item
4261 * is contiguous with the original item.
4262 *
4263 * This allows us to split file extent in place, keeping a lock on the
4264 * leaf the entire time.
4265 */
4266int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4267 struct btrfs_root *root,
4268 struct btrfs_path *path,
4269 struct btrfs_key *new_key)
4270{
4271 struct extent_buffer *leaf;
4272 int ret;
4273 u32 item_size;
4274
4275 leaf = path->nodes[0];
4276 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4277 ret = setup_leaf_for_split(trans, root, path,
4278 item_size + sizeof(struct btrfs_item));
4279 if (ret)
4280 return ret;
4281
4282 path->slots[0]++;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004283 setup_items_for_insert(root, path, new_key, &item_size,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004284 item_size, item_size +
4285 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004286 leaf = path->nodes[0];
4287 memcpy_extent_buffer(leaf,
4288 btrfs_item_ptr_offset(leaf, path->slots[0]),
4289 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4290 item_size);
4291 return 0;
4292}
4293
4294/*
Chris Masond352ac62008-09-29 15:18:18 -04004295 * make the item pointed to by the path smaller. new_size indicates
4296 * how small to make it, and from_end tells us if we just chop bytes
4297 * off the end of the item or if we shift the item to chop bytes off
4298 * the front.
4299 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004300void btrfs_truncate_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004301 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004302{
Chris Masonb18c6682007-04-17 13:26:50 -04004303 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004304 struct extent_buffer *leaf;
4305 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004306 u32 nritems;
4307 unsigned int data_end;
4308 unsigned int old_data_start;
4309 unsigned int old_size;
4310 unsigned int size_diff;
4311 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004312 struct btrfs_map_token token;
4313
4314 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004315
Chris Mason5f39d392007-10-15 16:14:19 -04004316 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004317 slot = path->slots[0];
4318
4319 old_size = btrfs_item_size_nr(leaf, slot);
4320 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004321 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004322
Chris Mason5f39d392007-10-15 16:14:19 -04004323 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004324 data_end = leaf_data_end(root, leaf);
4325
Chris Mason5f39d392007-10-15 16:14:19 -04004326 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004327
Chris Masonb18c6682007-04-17 13:26:50 -04004328 size_diff = old_size - new_size;
4329
4330 BUG_ON(slot < 0);
4331 BUG_ON(slot >= nritems);
4332
4333 /*
4334 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4335 */
4336 /* first correct the data pointers */
4337 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004338 u32 ioff;
4339 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004340
Chris Masoncfed81a2012-03-03 07:40:03 -05004341 ioff = btrfs_token_item_offset(leaf, item, &token);
4342 btrfs_set_token_item_offset(leaf, item,
4343 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004344 }
Chris Masondb945352007-10-15 16:15:53 -04004345
Chris Masonb18c6682007-04-17 13:26:50 -04004346 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004347 if (from_end) {
4348 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4349 data_end + size_diff, btrfs_leaf_data(leaf) +
4350 data_end, old_data_start + new_size - data_end);
4351 } else {
4352 struct btrfs_disk_key disk_key;
4353 u64 offset;
4354
4355 btrfs_item_key(leaf, &disk_key, slot);
4356
4357 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4358 unsigned long ptr;
4359 struct btrfs_file_extent_item *fi;
4360
4361 fi = btrfs_item_ptr(leaf, slot,
4362 struct btrfs_file_extent_item);
4363 fi = (struct btrfs_file_extent_item *)(
4364 (unsigned long)fi - size_diff);
4365
4366 if (btrfs_file_extent_type(leaf, fi) ==
4367 BTRFS_FILE_EXTENT_INLINE) {
4368 ptr = btrfs_item_ptr_offset(leaf, slot);
4369 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004370 (unsigned long)fi,
4371 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004372 disk_bytenr));
4373 }
4374 }
4375
4376 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4377 data_end + size_diff, btrfs_leaf_data(leaf) +
4378 data_end, old_data_start - data_end);
4379
4380 offset = btrfs_disk_key_offset(&disk_key);
4381 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4382 btrfs_set_item_key(leaf, &disk_key, slot);
4383 if (slot == 0)
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004384 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04004385 }
Chris Mason5f39d392007-10-15 16:14:19 -04004386
4387 item = btrfs_item_nr(leaf, slot);
4388 btrfs_set_item_size(leaf, item, new_size);
4389 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004390
Chris Mason5f39d392007-10-15 16:14:19 -04004391 if (btrfs_leaf_free_space(root, leaf) < 0) {
4392 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004393 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004394 }
Chris Masonb18c6682007-04-17 13:26:50 -04004395}
4396
Chris Masond352ac62008-09-29 15:18:18 -04004397/*
Stefan Behrens8f69dbd2013-05-07 10:23:30 +00004398 * make the item pointed to by the path bigger, data_size is the added size.
Chris Masond352ac62008-09-29 15:18:18 -04004399 */
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00004400void btrfs_extend_item(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004401 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004402{
Chris Mason6567e832007-04-16 09:22:45 -04004403 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004404 struct extent_buffer *leaf;
4405 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004406 u32 nritems;
4407 unsigned int data_end;
4408 unsigned int old_data;
4409 unsigned int old_size;
4410 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004411 struct btrfs_map_token token;
4412
4413 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004414
Chris Mason5f39d392007-10-15 16:14:19 -04004415 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004416
Chris Mason5f39d392007-10-15 16:14:19 -04004417 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004418 data_end = leaf_data_end(root, leaf);
4419
Chris Mason5f39d392007-10-15 16:14:19 -04004420 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4421 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004422 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004423 }
Chris Mason6567e832007-04-16 09:22:45 -04004424 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004425 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004426
4427 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004428 if (slot >= nritems) {
4429 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004430 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4431 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004432 BUG_ON(1);
4433 }
Chris Mason6567e832007-04-16 09:22:45 -04004434
4435 /*
4436 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4437 */
4438 /* first correct the data pointers */
4439 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004440 u32 ioff;
4441 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004442
Chris Masoncfed81a2012-03-03 07:40:03 -05004443 ioff = btrfs_token_item_offset(leaf, item, &token);
4444 btrfs_set_token_item_offset(leaf, item,
4445 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004446 }
Chris Mason5f39d392007-10-15 16:14:19 -04004447
Chris Mason6567e832007-04-16 09:22:45 -04004448 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004449 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004450 data_end - data_size, btrfs_leaf_data(leaf) +
4451 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004452
Chris Mason6567e832007-04-16 09:22:45 -04004453 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004454 old_size = btrfs_item_size_nr(leaf, slot);
4455 item = btrfs_item_nr(leaf, slot);
4456 btrfs_set_item_size(leaf, item, old_size + data_size);
4457 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004458
Chris Mason5f39d392007-10-15 16:14:19 -04004459 if (btrfs_leaf_free_space(root, leaf) < 0) {
4460 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004461 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004462 }
Chris Mason6567e832007-04-16 09:22:45 -04004463}
4464
Chris Mason74123bd2007-02-02 11:05:29 -05004465/*
Chris Mason44871b12009-03-13 10:04:31 -04004466 * this is a helper for btrfs_insert_empty_items, the main goal here is
4467 * to save stack depth by doing the bulk of the work in a function
4468 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004469 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004470void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
Jeff Mahoney143bede2012-03-01 14:56:26 +01004471 struct btrfs_key *cpu_key, u32 *data_size,
4472 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004473{
Chris Mason5f39d392007-10-15 16:14:19 -04004474 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004475 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004476 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004477 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004478 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004479 struct extent_buffer *leaf;
4480 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004481 struct btrfs_map_token token;
4482
4483 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004484
Chris Mason5f39d392007-10-15 16:14:19 -04004485 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004486 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004487
Chris Mason5f39d392007-10-15 16:14:19 -04004488 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004489 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004490
Chris Masonf25956c2008-09-12 15:32:53 -04004491 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004492 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004493 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004494 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004495 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004496 }
Chris Mason5f39d392007-10-15 16:14:19 -04004497
Chris Masonbe0e5c02007-01-26 15:51:26 -05004498 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004499 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004500
Chris Mason5f39d392007-10-15 16:14:19 -04004501 if (old_data < data_end) {
4502 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004503 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004504 slot, old_data, data_end);
4505 BUG_ON(1);
4506 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004507 /*
4508 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4509 */
4510 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004511 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004512 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004513
Chris Mason5f39d392007-10-15 16:14:19 -04004514 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004515 ioff = btrfs_token_item_offset(leaf, item, &token);
4516 btrfs_set_token_item_offset(leaf, item,
4517 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004518 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004519 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004520 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004521 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004522 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004523
4524 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004525 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004526 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004527 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004528 data_end = old_data;
4529 }
Chris Mason5f39d392007-10-15 16:14:19 -04004530
Chris Mason62e27492007-03-15 12:56:47 -04004531 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004532 for (i = 0; i < nr; i++) {
4533 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4534 btrfs_set_item_key(leaf, &disk_key, slot + i);
4535 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004536 btrfs_set_token_item_offset(leaf, item,
4537 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004538 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004539 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004540 }
Chris Mason44871b12009-03-13 10:04:31 -04004541
Chris Mason9c583092008-01-29 15:15:18 -05004542 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004543
Chris Mason5a01a2e2008-01-30 11:43:54 -05004544 if (slot == 0) {
4545 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004546 fixup_low_keys(root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004547 }
Chris Masonb9473432009-03-13 11:00:37 -04004548 btrfs_unlock_up_safe(path, 1);
4549 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004550
Chris Mason5f39d392007-10-15 16:14:19 -04004551 if (btrfs_leaf_free_space(root, leaf) < 0) {
4552 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004553 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004554 }
Chris Mason44871b12009-03-13 10:04:31 -04004555}
4556
4557/*
4558 * Given a key and some data, insert items into the tree.
4559 * This does all the path init required, making room in the tree if needed.
4560 */
4561int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4562 struct btrfs_root *root,
4563 struct btrfs_path *path,
4564 struct btrfs_key *cpu_key, u32 *data_size,
4565 int nr)
4566{
Chris Mason44871b12009-03-13 10:04:31 -04004567 int ret = 0;
4568 int slot;
4569 int i;
4570 u32 total_size = 0;
4571 u32 total_data = 0;
4572
4573 for (i = 0; i < nr; i++)
4574 total_data += data_size[i];
4575
4576 total_size = total_data + (nr * sizeof(struct btrfs_item));
4577 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4578 if (ret == 0)
4579 return -EEXIST;
4580 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004581 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004582
Chris Mason44871b12009-03-13 10:04:31 -04004583 slot = path->slots[0];
4584 BUG_ON(slot < 0);
4585
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004586 setup_items_for_insert(root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004587 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004588 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004589}
4590
4591/*
4592 * Given a key and some data, insert an item into the tree.
4593 * This does all the path init required, making room in the tree if needed.
4594 */
Chris Masone089f052007-03-16 16:20:31 -04004595int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4596 *root, struct btrfs_key *cpu_key, void *data, u32
4597 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004598{
4599 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004600 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004601 struct extent_buffer *leaf;
4602 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004603
Chris Mason2c90e5d2007-04-02 10:50:19 -04004604 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004605 if (!path)
4606 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004607 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004608 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004609 leaf = path->nodes[0];
4610 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4611 write_extent_buffer(leaf, data, ptr, data_size);
4612 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004613 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004614 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004615 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004616}
4617
Chris Mason74123bd2007-02-02 11:05:29 -05004618/*
Chris Mason5de08d72007-02-24 06:24:44 -05004619 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004620 *
Chris Masond352ac62008-09-29 15:18:18 -04004621 * the tree should have been previously balanced so the deletion does not
4622 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004623 */
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004624static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
4625 int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004626{
Chris Mason5f39d392007-10-15 16:14:19 -04004627 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004628 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004629 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004630
Chris Mason5f39d392007-10-15 16:14:19 -04004631 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004632 if (slot != nritems - 1) {
Liu Bo0e411ec2012-10-19 09:50:54 +00004633 if (level)
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004634 tree_mod_log_eb_move(root->fs_info, parent, slot,
4635 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004636 memmove_extent_buffer(parent,
4637 btrfs_node_key_ptr_offset(slot),
4638 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004639 sizeof(struct btrfs_key_ptr) *
4640 (nritems - slot - 1));
Chris Mason57ba86c2012-12-18 19:35:32 -05004641 } else if (level) {
4642 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4643 MOD_LOG_KEY_REMOVE);
4644 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004645 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004646
Chris Mason7518a232007-03-12 12:01:18 -04004647 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004648 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004649 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004650 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004651 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004652 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004653 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004654 struct btrfs_disk_key disk_key;
4655
4656 btrfs_node_key(parent, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004657 fixup_low_keys(root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004658 }
Chris Masond6025572007-03-30 14:27:56 -04004659 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004660}
4661
Chris Mason74123bd2007-02-02 11:05:29 -05004662/*
Chris Mason323ac952008-10-01 19:05:46 -04004663 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004664 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004665 *
4666 * This deletes the pointer in path->nodes[1] and frees the leaf
4667 * block extent. zero is returned if it all worked out, < 0 otherwise.
4668 *
4669 * The path must have already been setup for deleting the leaf, including
4670 * all the proper balancing. path->nodes[1] must be locked.
4671 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004672static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4673 struct btrfs_root *root,
4674 struct btrfs_path *path,
4675 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004676{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004677 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00004678 del_ptr(root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004679
Chris Mason4d081c42009-02-04 09:31:28 -05004680 /*
4681 * btrfs_free_extent is expensive, we want to make sure we
4682 * aren't holding any locks when we call it
4683 */
4684 btrfs_unlock_up_safe(path, 0);
4685
Yan, Zhengf0486c62010-05-16 10:46:25 -04004686 root_sub_used(root, leaf->len);
4687
Josef Bacik3083ee22012-03-09 16:01:49 -05004688 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004689 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004690 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004691}
4692/*
Chris Mason74123bd2007-02-02 11:05:29 -05004693 * delete the item at the leaf level in path. If that empties
4694 * the leaf, remove it from the tree
4695 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004696int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4697 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004698{
Chris Mason5f39d392007-10-15 16:14:19 -04004699 struct extent_buffer *leaf;
4700 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004701 int last_off;
4702 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004703 int ret = 0;
4704 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004705 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004706 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004707 struct btrfs_map_token token;
4708
4709 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004710
Chris Mason5f39d392007-10-15 16:14:19 -04004711 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004712 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4713
4714 for (i = 0; i < nr; i++)
4715 dsize += btrfs_item_size_nr(leaf, slot + i);
4716
Chris Mason5f39d392007-10-15 16:14:19 -04004717 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004718
Chris Mason85e21ba2008-01-29 15:11:36 -05004719 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004720 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004721
4722 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004723 data_end + dsize,
4724 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004725 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004726
Chris Mason85e21ba2008-01-29 15:11:36 -05004727 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004728 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004729
Chris Mason5f39d392007-10-15 16:14:19 -04004730 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004731 ioff = btrfs_token_item_offset(leaf, item, &token);
4732 btrfs_set_token_item_offset(leaf, item,
4733 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004734 }
Chris Masondb945352007-10-15 16:15:53 -04004735
Chris Mason5f39d392007-10-15 16:14:19 -04004736 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004737 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004738 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004739 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004740 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004741 btrfs_set_header_nritems(leaf, nritems - nr);
4742 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004743
Chris Mason74123bd2007-02-02 11:05:29 -05004744 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004745 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004746 if (leaf == root->node) {
4747 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004748 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004749 btrfs_set_path_blocking(path);
4750 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004751 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004752 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004753 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004754 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004755 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004756 struct btrfs_disk_key disk_key;
4757
4758 btrfs_item_key(leaf, &disk_key, 0);
Tsutomu Itohd6a0a122013-04-16 05:18:02 +00004759 fixup_low_keys(root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004760 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004761
Chris Mason74123bd2007-02-02 11:05:29 -05004762 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004763 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004764 /* push_leaf_left fixes the path.
4765 * make sure the path still points to our leaf
4766 * for possible call to del_ptr below
4767 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004768 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004769 extent_buffer_get(leaf);
4770
Chris Masonb9473432009-03-13 11:00:37 -04004771 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004772 wret = push_leaf_left(trans, root, path, 1, 1,
4773 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004774 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004775 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004776
4777 if (path->nodes[0] == leaf &&
4778 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004779 wret = push_leaf_right(trans, root, path, 1,
4780 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004781 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004782 ret = wret;
4783 }
Chris Mason5f39d392007-10-15 16:14:19 -04004784
4785 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004786 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004787 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004788 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004789 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004790 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004791 /* if we're still in the path, make sure
4792 * we're dirty. Otherwise, one of the
4793 * push_leaf functions must have already
4794 * dirtied this buffer
4795 */
4796 if (path->nodes[0] == leaf)
4797 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004798 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004799 }
Chris Masond5719762007-03-23 10:01:08 -04004800 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004801 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004802 }
4803 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004804 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004805}
4806
Chris Mason97571fd2007-02-24 13:39:08 -05004807/*
Chris Mason925baed2008-06-25 16:01:30 -04004808 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004809 * returns 0 if it found something or 1 if there are no lesser leaves.
4810 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004811 *
4812 * This may release the path, and so you may lose any locks held at the
4813 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004814 */
4815int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4816{
Chris Mason925baed2008-06-25 16:01:30 -04004817 struct btrfs_key key;
4818 struct btrfs_disk_key found_key;
4819 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004820
Chris Mason925baed2008-06-25 16:01:30 -04004821 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004822
Chris Mason925baed2008-06-25 16:01:30 -04004823 if (key.offset > 0)
4824 key.offset--;
4825 else if (key.type > 0)
4826 key.type--;
4827 else if (key.objectid > 0)
4828 key.objectid--;
4829 else
4830 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004831
David Sterbab3b4aa72011-04-21 01:20:15 +02004832 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004833 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4834 if (ret < 0)
4835 return ret;
4836 btrfs_item_key(path->nodes[0], &found_key, 0);
4837 ret = comp_keys(&found_key, &key);
4838 if (ret < 0)
4839 return 0;
4840 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004841}
4842
Chris Mason3f157a22008-06-25 16:01:31 -04004843/*
4844 * A helper function to walk down the tree starting at min_key, and looking
Eric Sandeende78b512013-01-31 18:21:12 +00004845 * for nodes or leaves that are have a minimum transaction id.
4846 * This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004847 *
4848 * This does not cow, but it does stuff the starting key it finds back
4849 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4850 * key and get a writable path.
4851 *
4852 * This does lock as it descends, and path->keep_locks should be set
4853 * to 1 by the caller.
4854 *
4855 * This honors path->lowest_level to prevent descent past a given level
4856 * of the tree.
4857 *
Chris Masond352ac62008-09-29 15:18:18 -04004858 * min_trans indicates the oldest transaction that you are interested
4859 * in walking through. Any nodes or leaves older than min_trans are
4860 * skipped over (without reading them).
4861 *
Chris Mason3f157a22008-06-25 16:01:31 -04004862 * returns zero if something useful was found, < 0 on error and 1 if there
4863 * was nothing in the tree that matched the search criteria.
4864 */
4865int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004866 struct btrfs_key *max_key,
Eric Sandeende78b512013-01-31 18:21:12 +00004867 struct btrfs_path *path,
Chris Mason3f157a22008-06-25 16:01:31 -04004868 u64 min_trans)
4869{
4870 struct extent_buffer *cur;
4871 struct btrfs_key found_key;
4872 int slot;
Yan96524802008-07-24 12:19:49 -04004873 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004874 u32 nritems;
4875 int level;
4876 int ret = 1;
4877
Chris Mason934d3752008-12-08 16:43:10 -05004878 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004879again:
Chris Masonbd681512011-07-16 15:23:14 -04004880 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004881 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004882 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004883 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004884 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004885
4886 if (btrfs_header_generation(cur) < min_trans) {
4887 ret = 1;
4888 goto out;
4889 }
Chris Masond3977122009-01-05 21:25:51 -05004890 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004891 nritems = btrfs_header_nritems(cur);
4892 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004893 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004894
Chris Mason323ac952008-10-01 19:05:46 -04004895 /* at the lowest level, we're done, setup the path and exit */
4896 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004897 if (slot >= nritems)
4898 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004899 ret = 0;
4900 path->slots[level] = slot;
4901 btrfs_item_key_to_cpu(cur, &found_key, slot);
4902 goto out;
4903 }
Yan96524802008-07-24 12:19:49 -04004904 if (sret && slot > 0)
4905 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004906 /*
Eric Sandeende78b512013-01-31 18:21:12 +00004907 * check this node pointer against the min_trans parameters.
4908 * If it is too old, old, skip to the next one.
Chris Mason3f157a22008-06-25 16:01:31 -04004909 */
Chris Masond3977122009-01-05 21:25:51 -05004910 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004911 u64 blockptr;
4912 u64 gen;
Chris Masone02119d2008-09-05 16:13:11 -04004913
Chris Mason3f157a22008-06-25 16:01:31 -04004914 blockptr = btrfs_node_blockptr(cur, slot);
4915 gen = btrfs_node_ptr_generation(cur, slot);
4916 if (gen < min_trans) {
4917 slot++;
4918 continue;
4919 }
Eric Sandeende78b512013-01-31 18:21:12 +00004920 break;
Chris Mason3f157a22008-06-25 16:01:31 -04004921 }
Chris Masone02119d2008-09-05 16:13:11 -04004922find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004923 /*
4924 * we didn't find a candidate key in this node, walk forward
4925 * and find another one
4926 */
4927 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004928 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004929 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004930 sret = btrfs_find_next_key(root, path, min_key, level,
Eric Sandeende78b512013-01-31 18:21:12 +00004931 min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004932 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004933 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004934 goto again;
4935 } else {
4936 goto out;
4937 }
4938 }
4939 /* save our key for returning back */
4940 btrfs_node_key_to_cpu(cur, &found_key, slot);
4941 path->slots[level] = slot;
4942 if (level == path->lowest_level) {
4943 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004944 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004945 goto out;
4946 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004947 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004948 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004949 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004950
Chris Masonbd681512011-07-16 15:23:14 -04004951 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004952
Chris Masonbd681512011-07-16 15:23:14 -04004953 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004954 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004955 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004956 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004957 }
4958out:
4959 if (ret == 0)
4960 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004961 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004962 return ret;
4963}
4964
Alexander Block70698302012-06-05 21:07:48 +02004965static void tree_move_down(struct btrfs_root *root,
4966 struct btrfs_path *path,
4967 int *level, int root_level)
4968{
Chris Mason74dd17f2012-08-07 16:25:13 -04004969 BUG_ON(*level == 0);
Alexander Block70698302012-06-05 21:07:48 +02004970 path->nodes[*level - 1] = read_node_slot(root, path->nodes[*level],
4971 path->slots[*level]);
4972 path->slots[*level - 1] = 0;
4973 (*level)--;
4974}
4975
4976static int tree_move_next_or_upnext(struct btrfs_root *root,
4977 struct btrfs_path *path,
4978 int *level, int root_level)
4979{
4980 int ret = 0;
4981 int nritems;
4982 nritems = btrfs_header_nritems(path->nodes[*level]);
4983
4984 path->slots[*level]++;
4985
Chris Mason74dd17f2012-08-07 16:25:13 -04004986 while (path->slots[*level] >= nritems) {
Alexander Block70698302012-06-05 21:07:48 +02004987 if (*level == root_level)
4988 return -1;
4989
4990 /* move upnext */
4991 path->slots[*level] = 0;
4992 free_extent_buffer(path->nodes[*level]);
4993 path->nodes[*level] = NULL;
4994 (*level)++;
4995 path->slots[*level]++;
4996
4997 nritems = btrfs_header_nritems(path->nodes[*level]);
4998 ret = 1;
4999 }
5000 return ret;
5001}
5002
5003/*
5004 * Returns 1 if it had to move up and next. 0 is returned if it moved only next
5005 * or down.
5006 */
5007static int tree_advance(struct btrfs_root *root,
5008 struct btrfs_path *path,
5009 int *level, int root_level,
5010 int allow_down,
5011 struct btrfs_key *key)
5012{
5013 int ret;
5014
5015 if (*level == 0 || !allow_down) {
5016 ret = tree_move_next_or_upnext(root, path, level, root_level);
5017 } else {
5018 tree_move_down(root, path, level, root_level);
5019 ret = 0;
5020 }
5021 if (ret >= 0) {
5022 if (*level == 0)
5023 btrfs_item_key_to_cpu(path->nodes[*level], key,
5024 path->slots[*level]);
5025 else
5026 btrfs_node_key_to_cpu(path->nodes[*level], key,
5027 path->slots[*level]);
5028 }
5029 return ret;
5030}
5031
5032static int tree_compare_item(struct btrfs_root *left_root,
5033 struct btrfs_path *left_path,
5034 struct btrfs_path *right_path,
5035 char *tmp_buf)
5036{
5037 int cmp;
5038 int len1, len2;
5039 unsigned long off1, off2;
5040
5041 len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
5042 len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
5043 if (len1 != len2)
5044 return 1;
5045
5046 off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
5047 off2 = btrfs_item_ptr_offset(right_path->nodes[0],
5048 right_path->slots[0]);
5049
5050 read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
5051
5052 cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
5053 if (cmp)
5054 return 1;
5055 return 0;
5056}
5057
5058#define ADVANCE 1
5059#define ADVANCE_ONLY_NEXT -1
5060
5061/*
5062 * This function compares two trees and calls the provided callback for
5063 * every changed/new/deleted item it finds.
5064 * If shared tree blocks are encountered, whole subtrees are skipped, making
5065 * the compare pretty fast on snapshotted subvolumes.
5066 *
5067 * This currently works on commit roots only. As commit roots are read only,
5068 * we don't do any locking. The commit roots are protected with transactions.
5069 * Transactions are ended and rejoined when a commit is tried in between.
5070 *
5071 * This function checks for modifications done to the trees while comparing.
5072 * If it detects a change, it aborts immediately.
5073 */
5074int btrfs_compare_trees(struct btrfs_root *left_root,
5075 struct btrfs_root *right_root,
5076 btrfs_changed_cb_t changed_cb, void *ctx)
5077{
5078 int ret;
5079 int cmp;
5080 struct btrfs_trans_handle *trans = NULL;
5081 struct btrfs_path *left_path = NULL;
5082 struct btrfs_path *right_path = NULL;
5083 struct btrfs_key left_key;
5084 struct btrfs_key right_key;
5085 char *tmp_buf = NULL;
5086 int left_root_level;
5087 int right_root_level;
5088 int left_level;
5089 int right_level;
5090 int left_end_reached;
5091 int right_end_reached;
5092 int advance_left;
5093 int advance_right;
5094 u64 left_blockptr;
5095 u64 right_blockptr;
5096 u64 left_start_ctransid;
5097 u64 right_start_ctransid;
5098 u64 ctransid;
5099
5100 left_path = btrfs_alloc_path();
5101 if (!left_path) {
5102 ret = -ENOMEM;
5103 goto out;
5104 }
5105 right_path = btrfs_alloc_path();
5106 if (!right_path) {
5107 ret = -ENOMEM;
5108 goto out;
5109 }
5110
5111 tmp_buf = kmalloc(left_root->leafsize, GFP_NOFS);
5112 if (!tmp_buf) {
5113 ret = -ENOMEM;
5114 goto out;
5115 }
5116
5117 left_path->search_commit_root = 1;
5118 left_path->skip_locking = 1;
5119 right_path->search_commit_root = 1;
5120 right_path->skip_locking = 1;
5121
Anand Jain5f3ab902012-12-07 09:28:54 +00005122 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005123 left_start_ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005124 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005125
Anand Jain5f3ab902012-12-07 09:28:54 +00005126 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005127 right_start_ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005128 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005129
5130 trans = btrfs_join_transaction(left_root);
5131 if (IS_ERR(trans)) {
5132 ret = PTR_ERR(trans);
5133 trans = NULL;
5134 goto out;
5135 }
5136
5137 /*
5138 * Strategy: Go to the first items of both trees. Then do
5139 *
5140 * If both trees are at level 0
5141 * Compare keys of current items
5142 * If left < right treat left item as new, advance left tree
5143 * and repeat
5144 * If left > right treat right item as deleted, advance right tree
5145 * and repeat
5146 * If left == right do deep compare of items, treat as changed if
5147 * needed, advance both trees and repeat
5148 * If both trees are at the same level but not at level 0
5149 * Compare keys of current nodes/leafs
5150 * If left < right advance left tree and repeat
5151 * If left > right advance right tree and repeat
5152 * If left == right compare blockptrs of the next nodes/leafs
5153 * If they match advance both trees but stay at the same level
5154 * and repeat
5155 * If they don't match advance both trees while allowing to go
5156 * deeper and repeat
5157 * If tree levels are different
5158 * Advance the tree that needs it and repeat
5159 *
5160 * Advancing a tree means:
5161 * If we are at level 0, try to go to the next slot. If that's not
5162 * possible, go one level up and repeat. Stop when we found a level
5163 * where we could go to the next slot. We may at this point be on a
5164 * node or a leaf.
5165 *
5166 * If we are not at level 0 and not on shared tree blocks, go one
5167 * level deeper.
5168 *
5169 * If we are not at level 0 and on shared tree blocks, go one slot to
5170 * the right if possible or go up and right.
5171 */
5172
5173 left_level = btrfs_header_level(left_root->commit_root);
5174 left_root_level = left_level;
5175 left_path->nodes[left_level] = left_root->commit_root;
5176 extent_buffer_get(left_path->nodes[left_level]);
5177
5178 right_level = btrfs_header_level(right_root->commit_root);
5179 right_root_level = right_level;
5180 right_path->nodes[right_level] = right_root->commit_root;
5181 extent_buffer_get(right_path->nodes[right_level]);
5182
5183 if (left_level == 0)
5184 btrfs_item_key_to_cpu(left_path->nodes[left_level],
5185 &left_key, left_path->slots[left_level]);
5186 else
5187 btrfs_node_key_to_cpu(left_path->nodes[left_level],
5188 &left_key, left_path->slots[left_level]);
5189 if (right_level == 0)
5190 btrfs_item_key_to_cpu(right_path->nodes[right_level],
5191 &right_key, right_path->slots[right_level]);
5192 else
5193 btrfs_node_key_to_cpu(right_path->nodes[right_level],
5194 &right_key, right_path->slots[right_level]);
5195
5196 left_end_reached = right_end_reached = 0;
5197 advance_left = advance_right = 0;
5198
5199 while (1) {
5200 /*
5201 * We need to make sure the transaction does not get committed
5202 * while we do anything on commit roots. This means, we need to
5203 * join and leave transactions for every item that we process.
5204 */
5205 if (trans && btrfs_should_end_transaction(trans, left_root)) {
5206 btrfs_release_path(left_path);
5207 btrfs_release_path(right_path);
5208
5209 ret = btrfs_end_transaction(trans, left_root);
5210 trans = NULL;
5211 if (ret < 0)
5212 goto out;
5213 }
5214 /* now rejoin the transaction */
5215 if (!trans) {
5216 trans = btrfs_join_transaction(left_root);
5217 if (IS_ERR(trans)) {
5218 ret = PTR_ERR(trans);
5219 trans = NULL;
5220 goto out;
5221 }
5222
Anand Jain5f3ab902012-12-07 09:28:54 +00005223 spin_lock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005224 ctransid = btrfs_root_ctransid(&left_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005225 spin_unlock(&left_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005226 if (ctransid != left_start_ctransid)
5227 left_start_ctransid = 0;
5228
Anand Jain5f3ab902012-12-07 09:28:54 +00005229 spin_lock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005230 ctransid = btrfs_root_ctransid(&right_root->root_item);
Anand Jain5f3ab902012-12-07 09:28:54 +00005231 spin_unlock(&right_root->root_item_lock);
Alexander Block70698302012-06-05 21:07:48 +02005232 if (ctransid != right_start_ctransid)
5233 right_start_ctransid = 0;
5234
5235 if (!left_start_ctransid || !right_start_ctransid) {
5236 WARN(1, KERN_WARNING
5237 "btrfs: btrfs_compare_tree detected "
5238 "a change in one of the trees while "
5239 "iterating. This is probably a "
5240 "bug.\n");
5241 ret = -EIO;
5242 goto out;
5243 }
5244
5245 /*
5246 * the commit root may have changed, so start again
5247 * where we stopped
5248 */
5249 left_path->lowest_level = left_level;
5250 right_path->lowest_level = right_level;
5251 ret = btrfs_search_slot(NULL, left_root,
5252 &left_key, left_path, 0, 0);
5253 if (ret < 0)
5254 goto out;
5255 ret = btrfs_search_slot(NULL, right_root,
5256 &right_key, right_path, 0, 0);
5257 if (ret < 0)
5258 goto out;
5259 }
5260
5261 if (advance_left && !left_end_reached) {
5262 ret = tree_advance(left_root, left_path, &left_level,
5263 left_root_level,
5264 advance_left != ADVANCE_ONLY_NEXT,
5265 &left_key);
5266 if (ret < 0)
5267 left_end_reached = ADVANCE;
5268 advance_left = 0;
5269 }
5270 if (advance_right && !right_end_reached) {
5271 ret = tree_advance(right_root, right_path, &right_level,
5272 right_root_level,
5273 advance_right != ADVANCE_ONLY_NEXT,
5274 &right_key);
5275 if (ret < 0)
5276 right_end_reached = ADVANCE;
5277 advance_right = 0;
5278 }
5279
5280 if (left_end_reached && right_end_reached) {
5281 ret = 0;
5282 goto out;
5283 } else if (left_end_reached) {
5284 if (right_level == 0) {
5285 ret = changed_cb(left_root, right_root,
5286 left_path, right_path,
5287 &right_key,
5288 BTRFS_COMPARE_TREE_DELETED,
5289 ctx);
5290 if (ret < 0)
5291 goto out;
5292 }
5293 advance_right = ADVANCE;
5294 continue;
5295 } else if (right_end_reached) {
5296 if (left_level == 0) {
5297 ret = changed_cb(left_root, right_root,
5298 left_path, right_path,
5299 &left_key,
5300 BTRFS_COMPARE_TREE_NEW,
5301 ctx);
5302 if (ret < 0)
5303 goto out;
5304 }
5305 advance_left = ADVANCE;
5306 continue;
5307 }
5308
5309 if (left_level == 0 && right_level == 0) {
5310 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5311 if (cmp < 0) {
5312 ret = changed_cb(left_root, right_root,
5313 left_path, right_path,
5314 &left_key,
5315 BTRFS_COMPARE_TREE_NEW,
5316 ctx);
5317 if (ret < 0)
5318 goto out;
5319 advance_left = ADVANCE;
5320 } else if (cmp > 0) {
5321 ret = changed_cb(left_root, right_root,
5322 left_path, right_path,
5323 &right_key,
5324 BTRFS_COMPARE_TREE_DELETED,
5325 ctx);
5326 if (ret < 0)
5327 goto out;
5328 advance_right = ADVANCE;
5329 } else {
Chris Mason74dd17f2012-08-07 16:25:13 -04005330 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005331 ret = tree_compare_item(left_root, left_path,
5332 right_path, tmp_buf);
5333 if (ret) {
Chris Mason74dd17f2012-08-07 16:25:13 -04005334 WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
Alexander Block70698302012-06-05 21:07:48 +02005335 ret = changed_cb(left_root, right_root,
5336 left_path, right_path,
5337 &left_key,
5338 BTRFS_COMPARE_TREE_CHANGED,
5339 ctx);
5340 if (ret < 0)
5341 goto out;
5342 }
5343 advance_left = ADVANCE;
5344 advance_right = ADVANCE;
5345 }
5346 } else if (left_level == right_level) {
5347 cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
5348 if (cmp < 0) {
5349 advance_left = ADVANCE;
5350 } else if (cmp > 0) {
5351 advance_right = ADVANCE;
5352 } else {
5353 left_blockptr = btrfs_node_blockptr(
5354 left_path->nodes[left_level],
5355 left_path->slots[left_level]);
5356 right_blockptr = btrfs_node_blockptr(
5357 right_path->nodes[right_level],
5358 right_path->slots[right_level]);
5359 if (left_blockptr == right_blockptr) {
5360 /*
5361 * As we're on a shared block, don't
5362 * allow to go deeper.
5363 */
5364 advance_left = ADVANCE_ONLY_NEXT;
5365 advance_right = ADVANCE_ONLY_NEXT;
5366 } else {
5367 advance_left = ADVANCE;
5368 advance_right = ADVANCE;
5369 }
5370 }
5371 } else if (left_level < right_level) {
5372 advance_right = ADVANCE;
5373 } else {
5374 advance_left = ADVANCE;
5375 }
5376 }
5377
5378out:
5379 btrfs_free_path(left_path);
5380 btrfs_free_path(right_path);
5381 kfree(tmp_buf);
5382
5383 if (trans) {
5384 if (!ret)
5385 ret = btrfs_end_transaction(trans, left_root);
5386 else
5387 btrfs_end_transaction(trans, left_root);
5388 }
5389
5390 return ret;
5391}
5392
Chris Mason3f157a22008-06-25 16:01:31 -04005393/*
5394 * this is similar to btrfs_next_leaf, but does not try to preserve
5395 * and fixup the path. It looks for and returns the next key in the
Eric Sandeende78b512013-01-31 18:21:12 +00005396 * tree based on the current path and the min_trans parameters.
Chris Mason3f157a22008-06-25 16:01:31 -04005397 *
5398 * 0 is returned if another key is found, < 0 if there are any errors
5399 * and 1 is returned if there are no higher keys in the tree
5400 *
5401 * path->keep_locks should be set to 1 on the search made before
5402 * calling this function.
5403 */
Chris Masone7a84562008-06-25 16:01:31 -04005404int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Eric Sandeende78b512013-01-31 18:21:12 +00005405 struct btrfs_key *key, int level, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04005406{
Chris Masone7a84562008-06-25 16:01:31 -04005407 int slot;
5408 struct extent_buffer *c;
5409
Chris Mason934d3752008-12-08 16:43:10 -05005410 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05005411 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04005412 if (!path->nodes[level])
5413 return 1;
5414
5415 slot = path->slots[level] + 1;
5416 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04005417next:
Chris Masone7a84562008-06-25 16:01:31 -04005418 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04005419 int ret;
5420 int orig_lowest;
5421 struct btrfs_key cur_key;
5422 if (level + 1 >= BTRFS_MAX_LEVEL ||
5423 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04005424 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04005425
5426 if (path->locks[level + 1]) {
5427 level++;
5428 continue;
5429 }
5430
5431 slot = btrfs_header_nritems(c) - 1;
5432 if (level == 0)
5433 btrfs_item_key_to_cpu(c, &cur_key, slot);
5434 else
5435 btrfs_node_key_to_cpu(c, &cur_key, slot);
5436
5437 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02005438 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04005439 path->lowest_level = level;
5440 ret = btrfs_search_slot(NULL, root, &cur_key, path,
5441 0, 0);
5442 path->lowest_level = orig_lowest;
5443 if (ret < 0)
5444 return ret;
5445
5446 c = path->nodes[level];
5447 slot = path->slots[level];
5448 if (ret == 0)
5449 slot++;
5450 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04005451 }
Yan Zheng33c66f42009-07-22 09:59:00 -04005452
Chris Masone7a84562008-06-25 16:01:31 -04005453 if (level == 0)
5454 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005455 else {
Chris Mason3f157a22008-06-25 16:01:31 -04005456 u64 gen = btrfs_node_ptr_generation(c, slot);
5457
Chris Mason3f157a22008-06-25 16:01:31 -04005458 if (gen < min_trans) {
5459 slot++;
5460 goto next;
5461 }
Chris Masone7a84562008-06-25 16:01:31 -04005462 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005463 }
Chris Masone7a84562008-06-25 16:01:31 -04005464 return 0;
5465 }
5466 return 1;
5467}
5468
Chris Mason7bb86312007-12-11 09:25:06 -05005469/*
Chris Mason925baed2008-06-25 16:01:30 -04005470 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005471 * returns 0 if it found something or 1 if there are no greater leaves.
5472 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005473 */
Chris Mason234b63a2007-03-13 10:46:10 -04005474int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005475{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005476 return btrfs_next_old_leaf(root, path, 0);
5477}
5478
5479int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5480 u64 time_seq)
5481{
Chris Masond97e63b2007-02-20 16:40:44 -05005482 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005483 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005484 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005485 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005486 struct btrfs_key key;
5487 u32 nritems;
5488 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005489 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005490 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005491
5492 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005493 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005494 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005495
Chris Mason8e73f272009-04-03 10:14:18 -04005496 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5497again:
5498 level = 1;
5499 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005500 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005501 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005502
Chris Masona2135012008-06-25 16:01:30 -04005503 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005504 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005505
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005506 if (time_seq)
5507 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5508 else
5509 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005510 path->keep_locks = 0;
5511
5512 if (ret < 0)
5513 return ret;
5514
Chris Masona2135012008-06-25 16:01:30 -04005515 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005516 /*
5517 * by releasing the path above we dropped all our locks. A balance
5518 * could have added more items next to the key that used to be
5519 * at the very end of the block. So, check again here and
5520 * advance the path if there are now more items available.
5521 */
Chris Masona2135012008-06-25 16:01:30 -04005522 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005523 if (ret == 0)
5524 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005525 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005526 goto done;
5527 }
Chris Masond97e63b2007-02-20 16:40:44 -05005528
Chris Masond3977122009-01-05 21:25:51 -05005529 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005530 if (!path->nodes[level]) {
5531 ret = 1;
5532 goto done;
5533 }
Chris Mason5f39d392007-10-15 16:14:19 -04005534
Chris Masond97e63b2007-02-20 16:40:44 -05005535 slot = path->slots[level] + 1;
5536 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005537 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005538 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005539 if (level == BTRFS_MAX_LEVEL) {
5540 ret = 1;
5541 goto done;
5542 }
Chris Masond97e63b2007-02-20 16:40:44 -05005543 continue;
5544 }
Chris Mason5f39d392007-10-15 16:14:19 -04005545
Chris Mason925baed2008-06-25 16:01:30 -04005546 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005547 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005548 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005549 }
Chris Mason5f39d392007-10-15 16:14:19 -04005550
Chris Mason8e73f272009-04-03 10:14:18 -04005551 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005552 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005553 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005554 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005555 if (ret == -EAGAIN)
5556 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005557
Chris Mason76a05b32009-05-14 13:24:30 -04005558 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005559 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005560 goto done;
5561 }
5562
Chris Mason5cd57b22008-06-25 16:01:30 -04005563 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005564 ret = btrfs_try_tree_read_lock(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005565 if (!ret && time_seq) {
5566 /*
5567 * If we don't get the lock, we may be racing
5568 * with push_leaf_left, holding that lock while
5569 * itself waiting for the leaf we've currently
5570 * locked. To solve this situation, we give up
5571 * on our lock and cycle.
5572 */
Jan Schmidtcf538832012-07-04 15:42:48 +02005573 free_extent_buffer(next);
Jan Schmidtd42244a2012-06-22 14:51:15 +02005574 btrfs_release_path(path);
5575 cond_resched();
5576 goto again;
5577 }
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 break;
5587 }
5588 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005589 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005590 level--;
5591 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005592 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005593 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005594
Chris Mason5f39d392007-10-15 16:14:19 -04005595 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005596 path->nodes[level] = next;
5597 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005598 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005599 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005600 if (!level)
5601 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005602
Chris Mason8e73f272009-04-03 10:14:18 -04005603 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005604 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005605 if (ret == -EAGAIN)
5606 goto again;
5607
Chris Mason76a05b32009-05-14 13:24:30 -04005608 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005609 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005610 goto done;
5611 }
5612
Chris Mason5cd57b22008-06-25 16:01:30 -04005613 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005614 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005615 if (!ret) {
5616 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005617 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005618 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005619 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005620 }
Chris Mason31533fb2011-07-26 16:01:59 -04005621 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005622 }
Chris Masond97e63b2007-02-20 16:40:44 -05005623 }
Chris Mason8e73f272009-04-03 10:14:18 -04005624 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005625done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005626 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005627 path->leave_spinning = old_spinning;
5628 if (!old_spinning)
5629 btrfs_set_path_blocking(path);
5630
5631 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005632}
Chris Mason0b86a832008-03-24 15:01:56 -04005633
Chris Mason3f157a22008-06-25 16:01:31 -04005634/*
5635 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5636 * searching until it gets past min_objectid or finds an item of 'type'
5637 *
5638 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5639 */
Chris Mason0b86a832008-03-24 15:01:56 -04005640int btrfs_previous_item(struct btrfs_root *root,
5641 struct btrfs_path *path, u64 min_objectid,
5642 int type)
5643{
5644 struct btrfs_key found_key;
5645 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005646 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005647 int ret;
5648
Chris Masond3977122009-01-05 21:25:51 -05005649 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005650 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005651 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005652 ret = btrfs_prev_leaf(root, path);
5653 if (ret != 0)
5654 return ret;
5655 } else {
5656 path->slots[0]--;
5657 }
5658 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005659 nritems = btrfs_header_nritems(leaf);
5660 if (nritems == 0)
5661 return 1;
5662 if (path->slots[0] == nritems)
5663 path->slots[0]--;
5664
Chris Mason0b86a832008-03-24 15:01:56 -04005665 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005666 if (found_key.objectid < min_objectid)
5667 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005668 if (found_key.type == type)
5669 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005670 if (found_key.objectid == min_objectid &&
5671 found_key.type < type)
5672 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005673 }
5674 return 1;
5675}