blob: 2cde7b0a0106a2a34a337ae48a311e88f1838dad [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);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +020041 struct btrfs_path *path, int level, int slot,
42 int tree_mod_log);
Jan Schmidtf2304752012-05-26 11:43:17 +020043static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
47 u64 time_seq);
48struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
50 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050051
Chris Mason2c90e5d2007-04-02 10:50:19 -040052struct btrfs_path *btrfs_alloc_path(void)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050055 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040056 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 }
75}
76
77/*
78 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050079 *
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
83 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050084 */
Chris Mason4008c042009-02-12 14:09:45 -050085noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040086 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050087{
88 int i;
Chris Mason4008c042009-02-12 14:09:45 -050089
90#ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
95 * the path blocking.
96 */
Chris Masonbd681512011-07-16 15:23:14 -040097 if (held) {
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
103 }
Chris Mason4008c042009-02-12 14:09:45 -0500104 btrfs_set_path_blocking(p);
105#endif
106
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
114 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115 }
Chris Mason4008c042009-02-12 14:09:45 -0500116
117#ifdef CONFIG_DEBUG_LOCK_ALLOC
118 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400119 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500120#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400124void btrfs_free_path(struct btrfs_path *p)
125{
Jesper Juhlff175d52010-12-25 21:22:30 +0000126 if (!p)
127 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200128 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400129 kmem_cache_free(btrfs_path_cachep, p);
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
135 *
136 * It is safe to call this on paths that no locks or extent buffers held.
137 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200138noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500139{
140 int i;
Chris Masona2135012008-06-25 16:01:30 -0400141
Chris Mason234b63a2007-03-13 10:46:10 -0400142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400143 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400145 continue;
146 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400148 p->locks[i] = 0;
149 }
Chris Mason5f39d392007-10-15 16:14:19 -0400150 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400151 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
159 * looping required.
160 *
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
164 */
Chris Mason925baed2008-06-25 16:01:30 -0400165struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166{
167 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400168
Josef Bacik3083ee22012-03-09 16:01:49 -0500169 while (1) {
170 rcu_read_lock();
171 eb = rcu_dereference(root->node);
172
173 /*
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
178 */
179 if (atomic_inc_not_zero(&eb->refs)) {
180 rcu_read_unlock();
181 break;
182 }
183 rcu_read_unlock();
184 synchronize_rcu();
185 }
Chris Mason925baed2008-06-25 16:01:30 -0400186 return eb;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Chris Mason925baed2008-06-25 16:01:30 -0400193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194{
195 struct extent_buffer *eb;
196
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400198 eb = btrfs_root_node(root);
199 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400200 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400201 break;
Chris Mason925baed2008-06-25 16:01:30 -0400202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masonbd681512011-07-16 15:23:14 -0400208/* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
211 */
212struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213{
214 struct extent_buffer *eb;
215
216 while (1) {
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
220 break;
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
223 }
224 return eb;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
230 */
Chris Mason0b86a832008-03-24 15:01:56 -0400231static void add_root_to_dirty_list(struct btrfs_root *root)
232{
Chris Masone5846fc2012-05-03 12:08:48 -0400233 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
237 }
Chris Masone5846fc2012-05-03 12:08:48 -0400238 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400239}
240
Chris Masond352ac62008-09-29 15:18:18 -0400241/*
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
245 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500246int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
250{
251 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 int ret = 0;
253 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259
260 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 if (level == 0)
262 btrfs_item_key(buf, &disk_key, 0);
263 else
264 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200268 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 return PTR_ERR(cow);
271
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
280 else
281 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500282
Yan Zheng2b820322008-11-17 21:11:30 -0500283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
285 BTRFS_FSID_SIZE);
286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500292
Chris Masonbe20aa92007-12-17 20:14:01 -0500293 if (ret)
294 return ret;
295
296 btrfs_mark_buffer_dirty(cow);
297 *cow_ret = cow;
298 return 0;
299}
300
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200301enum mod_log_op {
302 MOD_LOG_KEY_REPLACE,
303 MOD_LOG_KEY_ADD,
304 MOD_LOG_KEY_REMOVE,
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307 MOD_LOG_MOVE_KEYS,
308 MOD_LOG_ROOT_REPLACE,
309};
310
311struct tree_mod_move {
312 int dst_slot;
313 int nr_items;
314};
315
316struct tree_mod_root {
317 u64 logical;
318 u8 level;
319};
320
321struct tree_mod_elem {
322 struct rb_node node;
323 u64 index; /* shifted logical */
324 struct seq_list elem;
325 enum mod_log_op op;
326
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328 int slot;
329
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331 u64 generation;
332
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
335 u64 blockptr;
336
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
339
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
342};
343
344static inline void
345__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
346{
347 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
349}
350
351void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 elem->flags = 1;
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 __get_tree_mod_seq(fs_info, elem);
357 spin_unlock(&fs_info->tree_mod_seq_lock);
358}
359
360void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361 struct seq_list *elem)
362{
363 struct rb_root *tm_root;
364 struct rb_node *node;
365 struct rb_node *next;
366 struct seq_list *cur_elem;
367 struct tree_mod_elem *tm;
368 u64 min_seq = (u64)-1;
369 u64 seq_putting = elem->seq;
370
371 if (!seq_putting)
372 return;
373
374 BUG_ON(!(elem->flags & 1));
375 spin_lock(&fs_info->tree_mod_seq_lock);
376 list_del(&elem->list);
377
378 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380 if (seq_putting > cur_elem->seq) {
381 /*
382 * blocker with lower sequence number exists, we
383 * cannot remove anything from the log
384 */
385 goto out;
386 }
387 min_seq = cur_elem->seq;
388 }
389 }
390
391 /*
392 * anything that's lower than the lowest existing (read: blocked)
393 * sequence number can be removed from the tree.
394 */
395 write_lock(&fs_info->tree_mod_log_lock);
396 tm_root = &fs_info->tree_mod_log;
397 for (node = rb_first(tm_root); node; node = next) {
398 next = rb_next(node);
399 tm = container_of(node, struct tree_mod_elem, node);
400 if (tm->elem.seq > min_seq)
401 continue;
402 rb_erase(node, tm_root);
403 list_del(&tm->elem.list);
404 kfree(tm);
405 }
406 write_unlock(&fs_info->tree_mod_log_lock);
407out:
408 spin_unlock(&fs_info->tree_mod_seq_lock);
409}
410
411/*
412 * key order of the log:
413 * index -> sequence
414 *
415 * the index is the shifted logical of the *new* root node for root replace
416 * operations, or the shifted logical of the affected block for all other
417 * operations.
418 */
419static noinline int
420__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
421{
422 struct rb_root *tm_root;
423 struct rb_node **new;
424 struct rb_node *parent = NULL;
425 struct tree_mod_elem *cur;
426 int ret = 0;
427
428 BUG_ON(!tm || !tm->elem.seq);
429
430 write_lock(&fs_info->tree_mod_log_lock);
431 tm_root = &fs_info->tree_mod_log;
432 new = &tm_root->rb_node;
433 while (*new) {
434 cur = container_of(*new, struct tree_mod_elem, node);
435 parent = *new;
436 if (cur->index < tm->index)
437 new = &((*new)->rb_left);
438 else if (cur->index > tm->index)
439 new = &((*new)->rb_right);
440 else if (cur->elem.seq < tm->elem.seq)
441 new = &((*new)->rb_left);
442 else if (cur->elem.seq > tm->elem.seq)
443 new = &((*new)->rb_right);
444 else {
445 kfree(tm);
446 ret = -EEXIST;
447 goto unlock;
448 }
449 }
450
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
453unlock:
454 write_unlock(&fs_info->tree_mod_log_lock);
455 return ret;
456}
457
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200458static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
459 struct extent_buffer *eb) {
460 smp_mb();
461 if (list_empty(&(fs_info)->tree_mod_seq_list))
462 return 1;
463 if (!eb)
464 return 0;
465 if (btrfs_header_level(eb) == 0)
466 return 1;
467 return 0;
468}
469
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200470static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
471 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200472{
473 struct tree_mod_elem *tm;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200474 int seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200475
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200476 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200477 return 0;
478
479 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
480 if (!tm)
481 return -ENOMEM;
482
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200483 tm->elem.flags = 0;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200484 spin_lock(&fs_info->tree_mod_seq_lock);
485 if (list_empty(&fs_info->tree_mod_seq_list)) {
486 /*
487 * someone emptied the list while we were waiting for the lock.
488 * we must not add to the list, because no blocker exists. items
489 * are removed from the list only when the existing blocker is
490 * removed from the list.
491 */
492 kfree(tm);
493 seq = 0;
494 } else {
495 __get_tree_mod_seq(fs_info, &tm->elem);
496 seq = tm->elem.seq;
497 }
498 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200499
500 return seq;
501}
502
503static noinline int
504tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
505 struct extent_buffer *eb, int slot,
506 enum mod_log_op op, gfp_t flags)
507{
508 struct tree_mod_elem *tm;
509 int ret;
510
511 ret = tree_mod_alloc(fs_info, flags, &tm);
512 if (ret <= 0)
513 return ret;
514
515 tm->index = eb->start >> PAGE_CACHE_SHIFT;
516 if (op != MOD_LOG_KEY_ADD) {
517 btrfs_node_key(eb, &tm->key, slot);
518 tm->blockptr = btrfs_node_blockptr(eb, slot);
519 }
520 tm->op = op;
521 tm->slot = slot;
522 tm->generation = btrfs_node_ptr_generation(eb, slot);
523
524 return __tree_mod_log_insert(fs_info, tm);
525}
526
527static noinline int
528tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
529 int slot, enum mod_log_op op)
530{
531 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
532}
533
534static noinline int
535tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
536 struct extent_buffer *eb, int dst_slot, int src_slot,
537 int nr_items, gfp_t flags)
538{
539 struct tree_mod_elem *tm;
540 int ret;
541 int i;
542
Jan Schmidtf3956942012-05-31 15:02:32 +0200543 if (tree_mod_dont_log(fs_info, eb))
544 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200545
546 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
547 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
548 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
549 BUG_ON(ret < 0);
550 }
551
Jan Schmidtf3956942012-05-31 15:02:32 +0200552 ret = tree_mod_alloc(fs_info, flags, &tm);
553 if (ret <= 0)
554 return ret;
555
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556 tm->index = eb->start >> PAGE_CACHE_SHIFT;
557 tm->slot = src_slot;
558 tm->move.dst_slot = dst_slot;
559 tm->move.nr_items = nr_items;
560 tm->op = MOD_LOG_MOVE_KEYS;
561
562 return __tree_mod_log_insert(fs_info, tm);
563}
564
565static noinline int
566tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
567 struct extent_buffer *old_root,
568 struct extent_buffer *new_root, gfp_t flags)
569{
570 struct tree_mod_elem *tm;
571 int ret;
572
573 ret = tree_mod_alloc(fs_info, flags, &tm);
574 if (ret <= 0)
575 return ret;
576
577 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
578 tm->old_root.logical = old_root->start;
579 tm->old_root.level = btrfs_header_level(old_root);
580 tm->generation = btrfs_header_generation(old_root);
581 tm->op = MOD_LOG_ROOT_REPLACE;
582
583 return __tree_mod_log_insert(fs_info, tm);
584}
585
586static struct tree_mod_elem *
587__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
588 int smallest)
589{
590 struct rb_root *tm_root;
591 struct rb_node *node;
592 struct tree_mod_elem *cur = NULL;
593 struct tree_mod_elem *found = NULL;
594 u64 index = start >> PAGE_CACHE_SHIFT;
595
596 read_lock(&fs_info->tree_mod_log_lock);
597 tm_root = &fs_info->tree_mod_log;
598 node = tm_root->rb_node;
599 while (node) {
600 cur = container_of(node, struct tree_mod_elem, node);
601 if (cur->index < index) {
602 node = node->rb_left;
603 } else if (cur->index > index) {
604 node = node->rb_right;
605 } else if (cur->elem.seq < min_seq) {
606 node = node->rb_left;
607 } else if (!smallest) {
608 /* we want the node with the highest seq */
609 if (found)
610 BUG_ON(found->elem.seq > cur->elem.seq);
611 found = cur;
612 node = node->rb_left;
613 } else if (cur->elem.seq > min_seq) {
614 /* we want the node with the smallest seq */
615 if (found)
616 BUG_ON(found->elem.seq < cur->elem.seq);
617 found = cur;
618 node = node->rb_right;
619 } else {
620 found = cur;
621 break;
622 }
623 }
624 read_unlock(&fs_info->tree_mod_log_lock);
625
626 return found;
627}
628
629/*
630 * this returns the element from the log with the smallest time sequence
631 * value that's in the log (the oldest log item). any element with a time
632 * sequence lower than min_seq will be ignored.
633 */
634static struct tree_mod_elem *
635tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
636 u64 min_seq)
637{
638 return __tree_mod_log_search(fs_info, start, min_seq, 1);
639}
640
641/*
642 * this returns the element from the log with the largest time sequence
643 * value that's in the log (the most recent log item). any element with
644 * a time sequence lower than min_seq will be ignored.
645 */
646static struct tree_mod_elem *
647tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
648{
649 return __tree_mod_log_search(fs_info, start, min_seq, 0);
650}
651
652static inline void
653tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
654 struct extent_buffer *src, unsigned long dst_offset,
655 unsigned long src_offset, int nr_items)
656{
657 int ret;
658 int i;
659
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200660 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200661 return;
662
663 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
664 return;
665
666 /* speed this up by single seq for all operations? */
667 for (i = 0; i < nr_items; i++) {
668 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
669 MOD_LOG_KEY_REMOVE);
670 BUG_ON(ret < 0);
671 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
672 MOD_LOG_KEY_ADD);
673 BUG_ON(ret < 0);
674 }
675}
676
677static inline void
678tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
679 int dst_offset, int src_offset, int nr_items)
680{
681 int ret;
682 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
683 nr_items, GFP_NOFS);
684 BUG_ON(ret < 0);
685}
686
687static inline void
688tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
689 struct extent_buffer *eb,
690 struct btrfs_disk_key *disk_key, int slot, int atomic)
691{
692 int ret;
693
694 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
695 MOD_LOG_KEY_REPLACE,
696 atomic ? GFP_ATOMIC : GFP_NOFS);
697 BUG_ON(ret < 0);
698}
699
700static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
701 struct extent_buffer *eb)
702{
703 int i;
704 int ret;
705 u32 nritems;
706
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200707 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708 return;
709
710 nritems = btrfs_header_nritems(eb);
711 for (i = nritems - 1; i >= 0; i--) {
712 ret = tree_mod_log_insert_key(fs_info, eb, i,
713 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
714 BUG_ON(ret < 0);
715 }
716}
717
718static inline void
719tree_mod_log_set_root_pointer(struct btrfs_root *root,
720 struct extent_buffer *new_root_node)
721{
722 int ret;
723 tree_mod_log_free_eb(root->fs_info, root->node);
724 ret = tree_mod_log_insert_root(root->fs_info, root->node,
725 new_root_node, GFP_NOFS);
726 BUG_ON(ret < 0);
727}
728
Chris Masond352ac62008-09-29 15:18:18 -0400729/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400730 * check if the tree block can be shared by multiple trees
731 */
732int btrfs_block_can_be_shared(struct btrfs_root *root,
733 struct extent_buffer *buf)
734{
735 /*
736 * Tree blocks not in refernece counted trees and tree roots
737 * are never shared. If a block was allocated after the last
738 * snapshot and the block was not allocated by tree relocation,
739 * we know the block is not shared.
740 */
741 if (root->ref_cows &&
742 buf != root->node && buf != root->commit_root &&
743 (btrfs_header_generation(buf) <=
744 btrfs_root_last_snapshot(&root->root_item) ||
745 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
746 return 1;
747#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
748 if (root->ref_cows &&
749 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
750 return 1;
751#endif
752 return 0;
753}
754
755static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root,
757 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400758 struct extent_buffer *cow,
759 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400760{
761 u64 refs;
762 u64 owner;
763 u64 flags;
764 u64 new_flags = 0;
765 int ret;
766
767 /*
768 * Backrefs update rules:
769 *
770 * Always use full backrefs for extent pointers in tree block
771 * allocated by tree relocation.
772 *
773 * If a shared tree block is no longer referenced by its owner
774 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
775 * use full backrefs for extent pointers in tree block.
776 *
777 * If a tree block is been relocating
778 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
779 * use full backrefs for extent pointers in tree block.
780 * The reason for this is some operations (such as drop tree)
781 * are only allowed for blocks use full backrefs.
782 */
783
784 if (btrfs_block_can_be_shared(root, buf)) {
785 ret = btrfs_lookup_extent_info(trans, root, buf->start,
786 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700787 if (ret)
788 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700789 if (refs == 0) {
790 ret = -EROFS;
791 btrfs_std_error(root->fs_info, ret);
792 return ret;
793 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 } else {
795 refs = 1;
796 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
797 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
798 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
799 else
800 flags = 0;
801 }
802
803 owner = btrfs_header_owner(buf);
804 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
805 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
806
807 if (refs > 1) {
808 if ((owner == root->root_key.objectid ||
809 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
810 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200811 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100812 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813
814 if (root->root_key.objectid ==
815 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200816 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100817 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200818 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100819 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 }
821 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
822 } else {
823
824 if (root->root_key.objectid ==
825 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200826 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200828 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100829 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 }
831 if (new_flags != 0) {
832 ret = btrfs_set_disk_extent_flags(trans, root,
833 buf->start,
834 buf->len,
835 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700836 if (ret)
837 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 }
839 } else {
840 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
841 if (root->root_key.objectid ==
842 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200843 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400844 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200845 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100846 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200847 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100848 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200850 /*
851 * don't log freeing in case we're freeing the root node, this
852 * is done by tree_mod_log_set_root_pointer later
853 */
854 if (buf != root->node && btrfs_header_level(buf) != 0)
855 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400857 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 }
859 return 0;
860}
861
862/*
Chris Masond3977122009-01-05 21:25:51 -0500863 * does the dirty work in cow of a single block. The parent block (if
864 * supplied) is updated to point to the new cow copy. The new buffer is marked
865 * dirty and returned locked. If you modify the block it needs to be marked
866 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400867 *
868 * search_start -- an allocation hint for the new block
869 *
Chris Masond3977122009-01-05 21:25:51 -0500870 * empty_size -- a hint that you plan on doing more cow. This is the size in
871 * bytes the allocator should try to find free next to the block it returns.
872 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400873 */
Chris Masond3977122009-01-05 21:25:51 -0500874static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400875 struct btrfs_root *root,
876 struct extent_buffer *buf,
877 struct extent_buffer *parent, int parent_slot,
878 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400879 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400880{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700883 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400884 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400885 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400887
Chris Mason925baed2008-06-25 16:01:30 -0400888 if (*cow_ret == buf)
889 unlock_orig = 1;
890
Chris Masonb9447ef82009-03-09 11:45:38 -0400891 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400892
Chris Mason7bb86312007-12-11 09:25:06 -0500893 WARN_ON(root->ref_cows && trans->transid !=
894 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400895 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400896
Chris Mason7bb86312007-12-11 09:25:06 -0500897 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400898
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 if (level == 0)
900 btrfs_item_key(buf, &disk_key, 0);
901 else
902 btrfs_node_key(buf, &disk_key, 0);
903
904 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
905 if (parent)
906 parent_start = parent->start;
907 else
908 parent_start = 0;
909 } else
910 parent_start = 0;
911
912 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
913 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200914 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400915 if (IS_ERR(cow))
916 return PTR_ERR(cow);
917
Chris Masonb4ce94d2009-02-04 09:25:08 -0500918 /* cow is set to blocking by btrfs_init_new_buffer */
919
Chris Mason5f39d392007-10-15 16:14:19 -0400920 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400921 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400922 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
924 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
925 BTRFS_HEADER_FLAG_RELOC);
926 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
927 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
928 else
929 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400930
Yan Zheng2b820322008-11-17 21:11:30 -0500931 write_extent_buffer(cow, root->fs_info->fsid,
932 (unsigned long)btrfs_header_fsid(cow),
933 BTRFS_FSID_SIZE);
934
Mark Fashehbe1a5562011-08-08 13:20:18 -0700935 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700936 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100937 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700938 return ret;
939 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400940
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400941 if (root->ref_cows)
942 btrfs_reloc_cow_block(trans, root, buf, cow);
943
Chris Mason6702ed42007-08-07 16:15:09 -0400944 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400945 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
947 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
948 parent_start = buf->start;
949 else
950 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200953 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400954 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400955
Yan, Zhengf0486c62010-05-16 10:46:25 -0400956 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200957 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400958 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400959 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400960 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
962 parent_start = parent->start;
963 else
964 parent_start = 0;
965
966 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200967 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
968 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400969 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400970 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500971 btrfs_set_node_ptr_generation(parent, parent_slot,
972 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400973 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400974 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200975 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400976 }
Chris Mason925baed2008-06-25 16:01:30 -0400977 if (unlock_orig)
978 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500979 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400980 btrfs_mark_buffer_dirty(cow);
981 *cow_ret = cow;
982 return 0;
983}
984
Jan Schmidt5d9e75c42012-05-16 18:25:47 +0200985/*
986 * returns the logical address of the oldest predecessor of the given root.
987 * entries older than time_seq are ignored.
988 */
989static struct tree_mod_elem *
990__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
991 struct btrfs_root *root, u64 time_seq)
992{
993 struct tree_mod_elem *tm;
994 struct tree_mod_elem *found = NULL;
995 u64 root_logical = root->node->start;
996 int looped = 0;
997
998 if (!time_seq)
999 return 0;
1000
1001 /*
1002 * the very last operation that's logged for a root is the replacement
1003 * operation (if it is replaced at all). this has the index of the *new*
1004 * root, making it the very first operation that's logged for this root.
1005 */
1006 while (1) {
1007 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1008 time_seq);
1009 if (!looped && !tm)
1010 return 0;
1011 /*
1012 * we must have key remove operations in the log before the
1013 * replace operation.
1014 */
1015 BUG_ON(!tm);
1016
1017 if (tm->op != MOD_LOG_ROOT_REPLACE)
1018 break;
1019
1020 found = tm;
1021 root_logical = tm->old_root.logical;
1022 BUG_ON(root_logical == root->node->start);
1023 looped = 1;
1024 }
1025
1026 return found;
1027}
1028
1029/*
1030 * tm is a pointer to the first operation to rewind within eb. then, all
1031 * previous operations will be rewinded (until we reach something older than
1032 * time_seq).
1033 */
1034static void
1035__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1036 struct tree_mod_elem *first_tm)
1037{
1038 u32 n;
1039 struct rb_node *next;
1040 struct tree_mod_elem *tm = first_tm;
1041 unsigned long o_dst;
1042 unsigned long o_src;
1043 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1044
1045 n = btrfs_header_nritems(eb);
1046 while (tm && tm->elem.seq >= time_seq) {
1047 /*
1048 * all the operations are recorded with the operator used for
1049 * the modification. as we're going backwards, we do the
1050 * opposite of each operation here.
1051 */
1052 switch (tm->op) {
1053 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1054 BUG_ON(tm->slot < n);
1055 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1056 case MOD_LOG_KEY_REMOVE:
1057 btrfs_set_node_key(eb, &tm->key, tm->slot);
1058 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1059 btrfs_set_node_ptr_generation(eb, tm->slot,
1060 tm->generation);
1061 n++;
1062 break;
1063 case MOD_LOG_KEY_REPLACE:
1064 BUG_ON(tm->slot >= n);
1065 btrfs_set_node_key(eb, &tm->key, tm->slot);
1066 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1067 btrfs_set_node_ptr_generation(eb, tm->slot,
1068 tm->generation);
1069 break;
1070 case MOD_LOG_KEY_ADD:
1071 if (tm->slot != n - 1) {
1072 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1073 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1074 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1075 }
1076 n--;
1077 break;
1078 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001079 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1080 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1081 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001082 tm->move.nr_items * p_size);
1083 break;
1084 case MOD_LOG_ROOT_REPLACE:
1085 /*
1086 * this operation is special. for roots, this must be
1087 * handled explicitly before rewinding.
1088 * for non-roots, this operation may exist if the node
1089 * was a root: root A -> child B; then A gets empty and
1090 * B is promoted to the new root. in the mod log, we'll
1091 * have a root-replace operation for B, a tree block
1092 * that is no root. we simply ignore that operation.
1093 */
1094 break;
1095 }
1096 next = rb_next(&tm->node);
1097 if (!next)
1098 break;
1099 tm = container_of(next, struct tree_mod_elem, node);
1100 if (tm->index != first_tm->index)
1101 break;
1102 }
1103 btrfs_set_header_nritems(eb, n);
1104}
1105
1106static struct extent_buffer *
1107tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1108 u64 time_seq)
1109{
1110 struct extent_buffer *eb_rewin;
1111 struct tree_mod_elem *tm;
1112
1113 if (!time_seq)
1114 return eb;
1115
1116 if (btrfs_header_level(eb) == 0)
1117 return eb;
1118
1119 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1120 if (!tm)
1121 return eb;
1122
1123 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1124 BUG_ON(tm->slot != 0);
1125 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1126 fs_info->tree_root->nodesize);
1127 BUG_ON(!eb_rewin);
1128 btrfs_set_header_bytenr(eb_rewin, eb->start);
1129 btrfs_set_header_backref_rev(eb_rewin,
1130 btrfs_header_backref_rev(eb));
1131 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001132 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001133 } else {
1134 eb_rewin = btrfs_clone_extent_buffer(eb);
1135 BUG_ON(!eb_rewin);
1136 }
1137
1138 extent_buffer_get(eb_rewin);
1139 free_extent_buffer(eb);
1140
1141 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1142
1143 return eb_rewin;
1144}
1145
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001146/*
1147 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1148 * value. If there are no changes, the current root->root_node is returned. If
1149 * anything changed in between, there's a fresh buffer allocated on which the
1150 * rewind operations are done. In any case, the returned buffer is read locked.
1151 * Returns NULL on error (with no locks held).
1152 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001153static inline struct extent_buffer *
1154get_old_root(struct btrfs_root *root, u64 time_seq)
1155{
1156 struct tree_mod_elem *tm;
1157 struct extent_buffer *eb;
1158 struct tree_mod_root *old_root;
1159 u64 old_generation;
1160
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001161 eb = btrfs_read_lock_root_node(root);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001162 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1163 if (!tm)
1164 return root->node;
1165
1166 old_root = &tm->old_root;
1167 old_generation = tm->generation;
1168
1169 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1170 /*
1171 * there was an item in the log when __tree_mod_log_oldest_root
1172 * returned. this one must not go away, because the time_seq passed to
1173 * us must be blocking its removal.
1174 */
1175 BUG_ON(!tm);
1176
1177 if (old_root->logical == root->node->start) {
1178 /* there are logged operations for the current root */
1179 eb = btrfs_clone_extent_buffer(root->node);
1180 } else {
1181 /* there's a root replace operation for the current root */
1182 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1183 root->nodesize);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001184 }
1185 btrfs_tree_read_unlock(root->node);
1186 free_extent_buffer(root->node);
1187 if (!eb)
1188 return NULL;
1189 btrfs_tree_read_lock(eb);
1190 if (old_root->logical != root->node->start) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001191 btrfs_set_header_bytenr(eb, eb->start);
1192 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1193 btrfs_set_header_owner(eb, root->root_key.objectid);
1194 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001195 btrfs_set_header_level(eb, old_root->level);
1196 btrfs_set_header_generation(eb, old_generation);
1197 __tree_mod_log_rewind(eb, time_seq, tm);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001198 extent_buffer_get(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001199
1200 return eb;
1201}
1202
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203static inline int should_cow_block(struct btrfs_trans_handle *trans,
1204 struct btrfs_root *root,
1205 struct extent_buffer *buf)
1206{
Liu Bof1ebcc72011-11-14 20:48:06 -05001207 /* ensure we can see the force_cow */
1208 smp_rmb();
1209
1210 /*
1211 * We do not need to cow a block if
1212 * 1) this block is not created or changed in this transaction;
1213 * 2) this block does not belong to TREE_RELOC tree;
1214 * 3) the root is not forced COW.
1215 *
1216 * What is forced COW:
1217 * when we create snapshot during commiting the transaction,
1218 * after we've finished coping src root, we must COW the shared
1219 * block to ensure the metadata consistency.
1220 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001221 if (btrfs_header_generation(buf) == trans->transid &&
1222 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1223 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001224 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1225 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001226 return 0;
1227 return 1;
1228}
1229
Chris Masond352ac62008-09-29 15:18:18 -04001230/*
1231 * cows a single block, see __btrfs_cow_block for the real work.
1232 * This version of it has extra checks so that a block isn't cow'd more than
1233 * once per transaction, as long as it hasn't been written yet
1234 */
Chris Masond3977122009-01-05 21:25:51 -05001235noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001236 struct btrfs_root *root, struct extent_buffer *buf,
1237 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001238 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001239{
Chris Mason6702ed42007-08-07 16:15:09 -04001240 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001241 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001242
Chris Masonccd467d2007-06-28 15:57:36 -04001243 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001244 printk(KERN_CRIT "trans %llu running %llu\n",
1245 (unsigned long long)trans->transid,
1246 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001247 root->fs_info->running_transaction->transid);
1248 WARN_ON(1);
1249 }
1250 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001251 printk(KERN_CRIT "trans %llu running %llu\n",
1252 (unsigned long long)trans->transid,
1253 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001254 WARN_ON(1);
1255 }
Chris Masondc17ff82008-01-08 15:46:30 -05001256
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001257 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001258 *cow_ret = buf;
1259 return 0;
1260 }
Chris Masonc4876852009-02-04 09:24:25 -05001261
Chris Mason0b86a832008-03-24 15:01:56 -04001262 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001263
1264 if (parent)
1265 btrfs_set_lock_blocking(parent);
1266 btrfs_set_lock_blocking(buf);
1267
Chris Masonf510cfe2007-10-15 16:14:48 -04001268 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001269 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001270
1271 trace_btrfs_cow_block(root, buf, *cow_ret);
1272
Chris Masonf510cfe2007-10-15 16:14:48 -04001273 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001274}
1275
Chris Masond352ac62008-09-29 15:18:18 -04001276/*
1277 * helper function for defrag to decide if two blocks pointed to by a
1278 * node are actually close by
1279 */
Chris Mason6b800532007-10-15 16:17:34 -04001280static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001281{
Chris Mason6b800532007-10-15 16:17:34 -04001282 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001283 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001284 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001285 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001286 return 0;
1287}
1288
Chris Mason081e9572007-11-06 10:26:24 -05001289/*
1290 * compare two keys in a memcmp fashion
1291 */
1292static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1293{
1294 struct btrfs_key k1;
1295
1296 btrfs_disk_key_to_cpu(&k1, disk);
1297
Diego Calleja20736ab2009-07-24 11:06:52 -04001298 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001299}
1300
Josef Bacikf3465ca2008-11-12 14:19:50 -05001301/*
1302 * same as comp_keys only with two btrfs_key's
1303 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001304int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001305{
1306 if (k1->objectid > k2->objectid)
1307 return 1;
1308 if (k1->objectid < k2->objectid)
1309 return -1;
1310 if (k1->type > k2->type)
1311 return 1;
1312 if (k1->type < k2->type)
1313 return -1;
1314 if (k1->offset > k2->offset)
1315 return 1;
1316 if (k1->offset < k2->offset)
1317 return -1;
1318 return 0;
1319}
Chris Mason081e9572007-11-06 10:26:24 -05001320
Chris Masond352ac62008-09-29 15:18:18 -04001321/*
1322 * this is used by the defrag code to go through all the
1323 * leaves pointed to by a node and reallocate them so that
1324 * disk order is close to key order
1325 */
Chris Mason6702ed42007-08-07 16:15:09 -04001326int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001327 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001328 int start_slot, int cache_only, u64 *last_ret,
1329 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001330{
Chris Mason6b800532007-10-15 16:17:34 -04001331 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001332 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001333 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001334 u64 search_start = *last_ret;
1335 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001336 u64 other;
1337 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001338 int end_slot;
1339 int i;
1340 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001341 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001342 int uptodate;
1343 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001344 int progress_passed = 0;
1345 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001346
Chris Mason5708b952007-10-25 15:43:18 -04001347 parent_level = btrfs_header_level(parent);
1348 if (cache_only && parent_level != 1)
1349 return 0;
1350
Chris Masond3977122009-01-05 21:25:51 -05001351 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001352 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001353 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001354 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001355
Chris Mason6b800532007-10-15 16:17:34 -04001356 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001357 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001358 end_slot = parent_nritems;
1359
1360 if (parent_nritems == 1)
1361 return 0;
1362
Chris Masonb4ce94d2009-02-04 09:25:08 -05001363 btrfs_set_lock_blocking(parent);
1364
Chris Mason6702ed42007-08-07 16:15:09 -04001365 for (i = start_slot; i < end_slot; i++) {
1366 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001367
Chris Mason081e9572007-11-06 10:26:24 -05001368 btrfs_node_key(parent, &disk_key, i);
1369 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1370 continue;
1371
1372 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001373 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001374 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001375 if (last_block == 0)
1376 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001377
Chris Mason6702ed42007-08-07 16:15:09 -04001378 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001379 other = btrfs_node_blockptr(parent, i - 1);
1380 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001381 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001382 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001383 other = btrfs_node_blockptr(parent, i + 1);
1384 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001385 }
Chris Masone9d0b132007-08-10 14:06:19 -04001386 if (close) {
1387 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001388 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001389 }
Chris Mason6702ed42007-08-07 16:15:09 -04001390
Chris Mason6b800532007-10-15 16:17:34 -04001391 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1392 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001393 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001394 else
1395 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001396 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001397 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001398 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001399 continue;
1400 }
Chris Mason6b800532007-10-15 16:17:34 -04001401 if (!cur) {
1402 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001403 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001404 if (!cur)
1405 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001406 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001407 err = btrfs_read_buffer(cur, gen);
1408 if (err) {
1409 free_extent_buffer(cur);
1410 return err;
1411 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001412 }
Chris Mason6702ed42007-08-07 16:15:09 -04001413 }
Chris Masone9d0b132007-08-10 14:06:19 -04001414 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001415 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001416
Chris Masone7a84562008-06-25 16:01:31 -04001417 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001418 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001419 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001420 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001421 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001422 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001423 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001424 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001425 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001426 break;
Yan252c38f2007-08-29 09:11:44 -04001427 }
Chris Masone7a84562008-06-25 16:01:31 -04001428 search_start = cur->start;
1429 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001430 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001431 btrfs_tree_unlock(cur);
1432 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001433 }
1434 return err;
1435}
1436
Chris Mason74123bd2007-02-02 11:05:29 -05001437/*
1438 * The leaf data grows from end-to-front in the node.
1439 * this returns the address of the start of the last item,
1440 * which is the stop of the leaf data stack
1441 */
Chris Mason123abc82007-03-14 14:14:43 -04001442static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001443 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001444{
Chris Mason5f39d392007-10-15 16:14:19 -04001445 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001446 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001447 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001448 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001449}
1450
Chris Masonaa5d6be2007-02-28 16:35:06 -05001451
Chris Mason74123bd2007-02-02 11:05:29 -05001452/*
Chris Mason5f39d392007-10-15 16:14:19 -04001453 * search for key in the extent_buffer. The items start at offset p,
1454 * and they are item_size apart. There are 'max' items in p.
1455 *
Chris Mason74123bd2007-02-02 11:05:29 -05001456 * the slot in the array is returned via slot, and it points to
1457 * the place where you would insert key if it is not found in
1458 * the array.
1459 *
1460 * slot may point to max if the key is bigger than all of the keys
1461 */
Chris Masone02119d2008-09-05 16:13:11 -04001462static noinline int generic_bin_search(struct extent_buffer *eb,
1463 unsigned long p,
1464 int item_size, struct btrfs_key *key,
1465 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001466{
1467 int low = 0;
1468 int high = max;
1469 int mid;
1470 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001471 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001472 struct btrfs_disk_key unaligned;
1473 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001474 char *kaddr = NULL;
1475 unsigned long map_start = 0;
1476 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001477 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001478
Chris Masond3977122009-01-05 21:25:51 -05001479 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001480 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001481 offset = p + mid * item_size;
1482
Chris Masona6591712011-07-19 12:04:14 -04001483 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001484 (offset + sizeof(struct btrfs_disk_key)) >
1485 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001486
1487 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001488 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001489 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001490
Chris Mason479965d2007-10-15 16:14:27 -04001491 if (!err) {
1492 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1493 map_start);
1494 } else {
1495 read_extent_buffer(eb, &unaligned,
1496 offset, sizeof(unaligned));
1497 tmp = &unaligned;
1498 }
1499
Chris Mason5f39d392007-10-15 16:14:19 -04001500 } else {
1501 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1502 map_start);
1503 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001504 ret = comp_keys(tmp, key);
1505
1506 if (ret < 0)
1507 low = mid + 1;
1508 else if (ret > 0)
1509 high = mid;
1510 else {
1511 *slot = mid;
1512 return 0;
1513 }
1514 }
1515 *slot = low;
1516 return 1;
1517}
1518
Chris Mason97571fd2007-02-24 13:39:08 -05001519/*
1520 * simple bin_search frontend that does the right thing for
1521 * leaves vs nodes
1522 */
Chris Mason5f39d392007-10-15 16:14:19 -04001523static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1524 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001525{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001526 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001527 return generic_bin_search(eb,
1528 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001529 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001530 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001531 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001532 else
Chris Mason5f39d392007-10-15 16:14:19 -04001533 return generic_bin_search(eb,
1534 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001535 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001536 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001537 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001538}
1539
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001540int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1541 int level, int *slot)
1542{
1543 return bin_search(eb, key, level, slot);
1544}
1545
Yan, Zhengf0486c62010-05-16 10:46:25 -04001546static void root_add_used(struct btrfs_root *root, u32 size)
1547{
1548 spin_lock(&root->accounting_lock);
1549 btrfs_set_root_used(&root->root_item,
1550 btrfs_root_used(&root->root_item) + size);
1551 spin_unlock(&root->accounting_lock);
1552}
1553
1554static void root_sub_used(struct btrfs_root *root, u32 size)
1555{
1556 spin_lock(&root->accounting_lock);
1557 btrfs_set_root_used(&root->root_item,
1558 btrfs_root_used(&root->root_item) - size);
1559 spin_unlock(&root->accounting_lock);
1560}
1561
Chris Masond352ac62008-09-29 15:18:18 -04001562/* given a node and slot number, this reads the blocks it points to. The
1563 * extent buffer is returned with a reference taken (but unlocked).
1564 * NULL is returned on error.
1565 */
Chris Masone02119d2008-09-05 16:13:11 -04001566static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001567 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001568{
Chris Masonca7a79a2008-05-12 12:59:19 -04001569 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001570 if (slot < 0)
1571 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001572 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001573 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001574
1575 BUG_ON(level == 0);
1576
Chris Masondb945352007-10-15 16:15:53 -04001577 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001578 btrfs_level_size(root, level - 1),
1579 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001580}
1581
Chris Masond352ac62008-09-29 15:18:18 -04001582/*
1583 * node level balancing, used to make sure nodes are in proper order for
1584 * item deletion. We balance from the top down, so we have to make sure
1585 * that a deletion won't leave an node completely empty later on.
1586 */
Chris Masone02119d2008-09-05 16:13:11 -04001587static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001588 struct btrfs_root *root,
1589 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001590{
Chris Mason5f39d392007-10-15 16:14:19 -04001591 struct extent_buffer *right = NULL;
1592 struct extent_buffer *mid;
1593 struct extent_buffer *left = NULL;
1594 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001595 int ret = 0;
1596 int wret;
1597 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001598 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001599 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001600
1601 if (level == 0)
1602 return 0;
1603
Chris Mason5f39d392007-10-15 16:14:19 -04001604 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001605
Chris Masonbd681512011-07-16 15:23:14 -04001606 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1607 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001608 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1609
Chris Mason1d4f8a02007-03-13 09:28:32 -04001610 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001611
Li Zefana05a9bb2011-09-06 16:55:34 +08001612 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001613 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001614 pslot = path->slots[level + 1];
1615 }
Chris Masonbb803952007-03-01 12:04:21 -05001616
Chris Mason40689472007-03-17 14:29:23 -04001617 /*
1618 * deal with the case where there is only one pointer in the root
1619 * by promoting the node below to a root
1620 */
Chris Mason5f39d392007-10-15 16:14:19 -04001621 if (!parent) {
1622 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001623
Chris Mason5f39d392007-10-15 16:14:19 -04001624 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001625 return 0;
1626
1627 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001628 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001629 if (!child) {
1630 ret = -EROFS;
1631 btrfs_std_error(root->fs_info, ret);
1632 goto enospc;
1633 }
1634
Chris Mason925baed2008-06-25 16:01:30 -04001635 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001636 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001637 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001638 if (ret) {
1639 btrfs_tree_unlock(child);
1640 free_extent_buffer(child);
1641 goto enospc;
1642 }
Yan2f375ab2008-02-01 14:58:07 -05001643
Jan Schmidtf2304752012-05-26 11:43:17 +02001644 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001645 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001646
Chris Mason0b86a832008-03-24 15:01:56 -04001647 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001648 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001649
Chris Mason925baed2008-06-25 16:01:30 -04001650 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001651 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001652 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001653 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001654 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001655 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001656
1657 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001658 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001659 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001660 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001661 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001662 }
Chris Mason5f39d392007-10-15 16:14:19 -04001663 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001664 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001665 return 0;
1666
Chris Mason5f39d392007-10-15 16:14:19 -04001667 left = read_node_slot(root, parent, pslot - 1);
1668 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001669 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001670 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001671 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001672 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001673 if (wret) {
1674 ret = wret;
1675 goto enospc;
1676 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001677 }
Chris Mason5f39d392007-10-15 16:14:19 -04001678 right = read_node_slot(root, parent, pslot + 1);
1679 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001680 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001681 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001682 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001683 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001684 if (wret) {
1685 ret = wret;
1686 goto enospc;
1687 }
1688 }
1689
1690 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001691 if (left) {
1692 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001693 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001694 if (wret < 0)
1695 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001696 }
Chris Mason79f95c82007-03-01 15:16:26 -05001697
1698 /*
1699 * then try to empty the right most buffer into the middle
1700 */
Chris Mason5f39d392007-10-15 16:14:19 -04001701 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001702 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001703 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001704 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001705 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001706 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001707 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001708 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001709 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001710 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001711 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001712 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001713 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001714 struct btrfs_disk_key right_key;
1715 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001716 tree_mod_log_set_node_key(root->fs_info, parent,
1717 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001718 btrfs_set_node_key(parent, &right_key, pslot + 1);
1719 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001720 }
1721 }
Chris Mason5f39d392007-10-15 16:14:19 -04001722 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001723 /*
1724 * we're not allowed to leave a node with one item in the
1725 * tree during a delete. A deletion from lower in the tree
1726 * could try to delete the only pointer in this node.
1727 * So, pull some keys from the left.
1728 * There has to be a left pointer at this point because
1729 * otherwise we would have pulled some pointers from the
1730 * right
1731 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001732 if (!left) {
1733 ret = -EROFS;
1734 btrfs_std_error(root->fs_info, ret);
1735 goto enospc;
1736 }
Chris Mason5f39d392007-10-15 16:14:19 -04001737 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001738 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001739 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001740 goto enospc;
1741 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001742 if (wret == 1) {
1743 wret = push_node_left(trans, root, left, mid, 1);
1744 if (wret < 0)
1745 ret = wret;
1746 }
Chris Mason79f95c82007-03-01 15:16:26 -05001747 BUG_ON(wret == 1);
1748 }
Chris Mason5f39d392007-10-15 16:14:19 -04001749 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001750 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001751 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001752 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001753 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001754 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001755 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001756 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001757 } else {
1758 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001759 struct btrfs_disk_key mid_key;
1760 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001761 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1762 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001763 btrfs_set_node_key(parent, &mid_key, pslot);
1764 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001765 }
Chris Masonbb803952007-03-01 12:04:21 -05001766
Chris Mason79f95c82007-03-01 15:16:26 -05001767 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001768 if (left) {
1769 if (btrfs_header_nritems(left) > orig_slot) {
1770 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001771 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001772 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001773 path->slots[level + 1] -= 1;
1774 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001775 if (mid) {
1776 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001777 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001778 }
Chris Masonbb803952007-03-01 12:04:21 -05001779 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001780 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001781 path->slots[level] = orig_slot;
1782 }
1783 }
Chris Mason79f95c82007-03-01 15:16:26 -05001784 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001785 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001786 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001787 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001788enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001789 if (right) {
1790 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001791 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001792 }
1793 if (left) {
1794 if (path->nodes[level] != left)
1795 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001796 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001797 }
Chris Masonbb803952007-03-01 12:04:21 -05001798 return ret;
1799}
1800
Chris Masond352ac62008-09-29 15:18:18 -04001801/* Node balancing for insertion. Here we only split or push nodes around
1802 * when they are completely full. This is also done top down, so we
1803 * have to be pessimistic.
1804 */
Chris Masond3977122009-01-05 21:25:51 -05001805static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001806 struct btrfs_root *root,
1807 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001808{
Chris Mason5f39d392007-10-15 16:14:19 -04001809 struct extent_buffer *right = NULL;
1810 struct extent_buffer *mid;
1811 struct extent_buffer *left = NULL;
1812 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001813 int ret = 0;
1814 int wret;
1815 int pslot;
1816 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001817
1818 if (level == 0)
1819 return 1;
1820
Chris Mason5f39d392007-10-15 16:14:19 -04001821 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001822 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001823
Li Zefana05a9bb2011-09-06 16:55:34 +08001824 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001825 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001826 pslot = path->slots[level + 1];
1827 }
Chris Masone66f7092007-04-20 13:16:02 -04001828
Chris Mason5f39d392007-10-15 16:14:19 -04001829 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001830 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001831
Chris Mason5f39d392007-10-15 16:14:19 -04001832 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001833
1834 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001835 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001836 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001837
1838 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001839 btrfs_set_lock_blocking(left);
1840
Chris Mason5f39d392007-10-15 16:14:19 -04001841 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001842 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1843 wret = 1;
1844 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001845 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001846 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001847 if (ret)
1848 wret = 1;
1849 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001850 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001851 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001852 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001853 }
Chris Masone66f7092007-04-20 13:16:02 -04001854 if (wret < 0)
1855 ret = wret;
1856 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001857 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001858 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001859 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001860 tree_mod_log_set_node_key(root->fs_info, parent,
1861 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001862 btrfs_set_node_key(parent, &disk_key, pslot);
1863 btrfs_mark_buffer_dirty(parent);
1864 if (btrfs_header_nritems(left) > orig_slot) {
1865 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001866 path->slots[level + 1] -= 1;
1867 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001868 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001869 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001870 } else {
1871 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001872 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001873 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001874 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001875 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001876 }
Chris Masone66f7092007-04-20 13:16:02 -04001877 return 0;
1878 }
Chris Mason925baed2008-06-25 16:01:30 -04001879 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001880 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001881 }
Chris Mason925baed2008-06-25 16:01:30 -04001882 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001883
1884 /*
1885 * then try to empty the right most buffer into the middle
1886 */
Chris Mason5f39d392007-10-15 16:14:19 -04001887 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001888 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001889
Chris Mason925baed2008-06-25 16:01:30 -04001890 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001891 btrfs_set_lock_blocking(right);
1892
Chris Mason5f39d392007-10-15 16:14:19 -04001893 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001894 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1895 wret = 1;
1896 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001897 ret = btrfs_cow_block(trans, root, right,
1898 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001899 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001900 if (ret)
1901 wret = 1;
1902 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001903 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001904 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001905 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001906 }
Chris Masone66f7092007-04-20 13:16:02 -04001907 if (wret < 0)
1908 ret = wret;
1909 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001910 struct btrfs_disk_key disk_key;
1911
1912 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001913 tree_mod_log_set_node_key(root->fs_info, parent,
1914 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001915 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1916 btrfs_mark_buffer_dirty(parent);
1917
1918 if (btrfs_header_nritems(mid) <= orig_slot) {
1919 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001920 path->slots[level + 1] += 1;
1921 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001922 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001923 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001924 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001925 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001926 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001927 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001928 }
Chris Masone66f7092007-04-20 13:16:02 -04001929 return 0;
1930 }
Chris Mason925baed2008-06-25 16:01:30 -04001931 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001932 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001933 }
Chris Masone66f7092007-04-20 13:16:02 -04001934 return 1;
1935}
1936
Chris Mason74123bd2007-02-02 11:05:29 -05001937/*
Chris Masond352ac62008-09-29 15:18:18 -04001938 * readahead one full node of leaves, finding things that are close
1939 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001940 */
Chris Masonc8c42862009-04-03 10:14:18 -04001941static void reada_for_search(struct btrfs_root *root,
1942 struct btrfs_path *path,
1943 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001944{
Chris Mason5f39d392007-10-15 16:14:19 -04001945 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001946 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001947 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001948 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001949 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001950 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001951 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001952 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001953 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001954 u32 nr;
1955 u32 blocksize;
1956 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001957
Chris Masona6b6e752007-10-15 16:22:39 -04001958 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001959 return;
1960
Chris Mason6702ed42007-08-07 16:15:09 -04001961 if (!path->nodes[level])
1962 return;
1963
Chris Mason5f39d392007-10-15 16:14:19 -04001964 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001965
Chris Mason3c69fae2007-08-07 15:52:22 -04001966 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001967 blocksize = btrfs_level_size(root, level - 1);
1968 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001969 if (eb) {
1970 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001971 return;
1972 }
1973
Chris Masona7175312009-01-22 09:23:10 -05001974 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001975
Chris Mason5f39d392007-10-15 16:14:19 -04001976 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001977 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001978
Chris Masond3977122009-01-05 21:25:51 -05001979 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001980 if (direction < 0) {
1981 if (nr == 0)
1982 break;
1983 nr--;
1984 } else if (direction > 0) {
1985 nr++;
1986 if (nr >= nritems)
1987 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001988 }
Chris Mason01f46652007-12-21 16:24:26 -05001989 if (path->reada < 0 && objectid) {
1990 btrfs_node_key(node, &disk_key, nr);
1991 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1992 break;
1993 }
Chris Mason6b800532007-10-15 16:17:34 -04001994 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001995 if ((search <= target && target - search <= 65536) ||
1996 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001997 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001998 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001999 nread += blocksize;
2000 }
2001 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002002 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002003 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002004 }
2005}
Chris Mason925baed2008-06-25 16:01:30 -04002006
Chris Masond352ac62008-09-29 15:18:18 -04002007/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002008 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2009 * cache
2010 */
2011static noinline int reada_for_balance(struct btrfs_root *root,
2012 struct btrfs_path *path, int level)
2013{
2014 int slot;
2015 int nritems;
2016 struct extent_buffer *parent;
2017 struct extent_buffer *eb;
2018 u64 gen;
2019 u64 block1 = 0;
2020 u64 block2 = 0;
2021 int ret = 0;
2022 int blocksize;
2023
Chris Mason8c594ea2009-04-20 15:50:10 -04002024 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002025 if (!parent)
2026 return 0;
2027
2028 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002029 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002030 blocksize = btrfs_level_size(root, level);
2031
2032 if (slot > 0) {
2033 block1 = btrfs_node_blockptr(parent, slot - 1);
2034 gen = btrfs_node_ptr_generation(parent, slot - 1);
2035 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002036 /*
2037 * if we get -eagain from btrfs_buffer_uptodate, we
2038 * don't want to return eagain here. That will loop
2039 * forever
2040 */
2041 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002042 block1 = 0;
2043 free_extent_buffer(eb);
2044 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002045 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002046 block2 = btrfs_node_blockptr(parent, slot + 1);
2047 gen = btrfs_node_ptr_generation(parent, slot + 1);
2048 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002049 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002050 block2 = 0;
2051 free_extent_buffer(eb);
2052 }
2053 if (block1 || block2) {
2054 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002055
2056 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002057 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002058
2059 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002060 if (block1)
2061 readahead_tree_block(root, block1, blocksize, 0);
2062 if (block2)
2063 readahead_tree_block(root, block2, blocksize, 0);
2064
2065 if (block1) {
2066 eb = read_tree_block(root, block1, blocksize, 0);
2067 free_extent_buffer(eb);
2068 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002069 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002070 eb = read_tree_block(root, block2, blocksize, 0);
2071 free_extent_buffer(eb);
2072 }
2073 }
2074 return ret;
2075}
2076
2077
2078/*
Chris Masond3977122009-01-05 21:25:51 -05002079 * when we walk down the tree, it is usually safe to unlock the higher layers
2080 * in the tree. The exceptions are when our path goes through slot 0, because
2081 * operations on the tree might require changing key pointers higher up in the
2082 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002083 *
Chris Masond3977122009-01-05 21:25:51 -05002084 * callers might also have set path->keep_locks, which tells this code to keep
2085 * the lock if the path points to the last slot in the block. This is part of
2086 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002087 *
Chris Masond3977122009-01-05 21:25:51 -05002088 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2089 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002090 */
Chris Masone02119d2008-09-05 16:13:11 -04002091static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002092 int lowest_unlock, int min_write_lock_level,
2093 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002094{
2095 int i;
2096 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002097 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002098 struct extent_buffer *t;
2099
2100 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2101 if (!path->nodes[i])
2102 break;
2103 if (!path->locks[i])
2104 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002105 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002106 skip_level = i + 1;
2107 continue;
2108 }
Chris Mason051e1b92008-06-25 16:01:30 -04002109 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002110 u32 nritems;
2111 t = path->nodes[i];
2112 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002113 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002114 skip_level = i + 1;
2115 continue;
2116 }
2117 }
Chris Mason051e1b92008-06-25 16:01:30 -04002118 if (skip_level < i && i >= lowest_unlock)
2119 no_skips = 1;
2120
Chris Mason925baed2008-06-25 16:01:30 -04002121 t = path->nodes[i];
2122 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002123 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002124 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002125 if (write_lock_level &&
2126 i > min_write_lock_level &&
2127 i <= *write_lock_level) {
2128 *write_lock_level = i - 1;
2129 }
Chris Mason925baed2008-06-25 16:01:30 -04002130 }
2131 }
2132}
2133
Chris Mason3c69fae2007-08-07 15:52:22 -04002134/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002135 * This releases any locks held in the path starting at level and
2136 * going all the way up to the root.
2137 *
2138 * btrfs_search_slot will keep the lock held on higher nodes in a few
2139 * corner cases, such as COW of the block at slot zero in the node. This
2140 * ignores those rules, and it should only be called when there are no
2141 * more updates to be done higher up in the tree.
2142 */
2143noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2144{
2145 int i;
2146
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002147 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002148 return;
2149
2150 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2151 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002152 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002153 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002154 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002155 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002156 path->locks[i] = 0;
2157 }
2158}
2159
2160/*
Chris Masonc8c42862009-04-03 10:14:18 -04002161 * helper function for btrfs_search_slot. The goal is to find a block
2162 * in cache without setting the path to blocking. If we find the block
2163 * we return zero and the path is unchanged.
2164 *
2165 * If we can't find the block, we set the path blocking and do some
2166 * reada. -EAGAIN is returned and the search must be repeated.
2167 */
2168static int
2169read_block_for_search(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root, struct btrfs_path *p,
2171 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002172 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002173{
2174 u64 blocknr;
2175 u64 gen;
2176 u32 blocksize;
2177 struct extent_buffer *b = *eb_ret;
2178 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002179 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002180
2181 blocknr = btrfs_node_blockptr(b, slot);
2182 gen = btrfs_node_ptr_generation(b, slot);
2183 blocksize = btrfs_level_size(root, level - 1);
2184
2185 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002186 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002187 /* first we do an atomic uptodate check */
2188 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2189 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002190 /*
2191 * we found an up to date block without
2192 * sleeping, return
2193 * right away
2194 */
2195 *eb_ret = tmp;
2196 return 0;
2197 }
2198 /* the pages were up to date, but we failed
2199 * the generation number check. Do a full
2200 * read for the generation number that is correct.
2201 * We must do this without dropping locks so
2202 * we can trust our generation number
2203 */
2204 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002205 btrfs_set_path_blocking(p);
2206
Chris Masonb9fab912012-05-06 07:23:47 -04002207 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002208 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002209 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002210 *eb_ret = tmp;
2211 return 0;
2212 }
2213 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002214 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002215 return -EIO;
2216 }
Chris Masonc8c42862009-04-03 10:14:18 -04002217 }
2218
2219 /*
2220 * reduce lock contention at high levels
2221 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002222 * we read. Don't release the lock on the current
2223 * level because we need to walk this node to figure
2224 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002225 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002226 btrfs_unlock_up_safe(p, level + 1);
2227 btrfs_set_path_blocking(p);
2228
Chris Masoncb449212010-10-24 11:01:27 -04002229 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002230 if (p->reada)
2231 reada_for_search(root, p, level, slot, key->objectid);
2232
David Sterbab3b4aa72011-04-21 01:20:15 +02002233 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002234
2235 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002236 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002237 if (tmp) {
2238 /*
2239 * If the read above didn't mark this buffer up to date,
2240 * it will never end up being up to date. Set ret to EIO now
2241 * and give up so that our caller doesn't loop forever
2242 * on our EAGAINs.
2243 */
Chris Masonb9fab912012-05-06 07:23:47 -04002244 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002245 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002246 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002247 }
2248 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002249}
2250
2251/*
2252 * helper function for btrfs_search_slot. This does all of the checks
2253 * for node-level blocks and does any balancing required based on
2254 * the ins_len.
2255 *
2256 * If no extra work was required, zero is returned. If we had to
2257 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2258 * start over
2259 */
2260static int
2261setup_nodes_for_search(struct btrfs_trans_handle *trans,
2262 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002263 struct extent_buffer *b, int level, int ins_len,
2264 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002265{
2266 int ret;
2267 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2268 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2269 int sret;
2270
Chris Masonbd681512011-07-16 15:23:14 -04002271 if (*write_lock_level < level + 1) {
2272 *write_lock_level = level + 1;
2273 btrfs_release_path(p);
2274 goto again;
2275 }
2276
Chris Masonc8c42862009-04-03 10:14:18 -04002277 sret = reada_for_balance(root, p, level);
2278 if (sret)
2279 goto again;
2280
2281 btrfs_set_path_blocking(p);
2282 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002283 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002284
2285 BUG_ON(sret > 0);
2286 if (sret) {
2287 ret = sret;
2288 goto done;
2289 }
2290 b = p->nodes[level];
2291 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002292 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002293 int sret;
2294
Chris Masonbd681512011-07-16 15:23:14 -04002295 if (*write_lock_level < level + 1) {
2296 *write_lock_level = level + 1;
2297 btrfs_release_path(p);
2298 goto again;
2299 }
2300
Chris Masonc8c42862009-04-03 10:14:18 -04002301 sret = reada_for_balance(root, p, level);
2302 if (sret)
2303 goto again;
2304
2305 btrfs_set_path_blocking(p);
2306 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002307 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002308
2309 if (sret) {
2310 ret = sret;
2311 goto done;
2312 }
2313 b = p->nodes[level];
2314 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002315 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002316 goto again;
2317 }
2318 BUG_ON(btrfs_header_nritems(b) == 1);
2319 }
2320 return 0;
2321
2322again:
2323 ret = -EAGAIN;
2324done:
2325 return ret;
2326}
2327
2328/*
Chris Mason74123bd2007-02-02 11:05:29 -05002329 * look for key in the tree. path is filled in with nodes along the way
2330 * if key is found, we return zero and you can find the item in the leaf
2331 * level of the path (level 0)
2332 *
2333 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002334 * be inserted, and 1 is returned. If there are other errors during the
2335 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002336 *
2337 * if ins_len > 0, nodes and leaves will be split as we walk down the
2338 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2339 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002340 */
Chris Masone089f052007-03-16 16:20:31 -04002341int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2342 *root, struct btrfs_key *key, struct btrfs_path *p, int
2343 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002344{
Chris Mason5f39d392007-10-15 16:14:19 -04002345 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002346 int slot;
2347 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002348 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002349 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002350 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002351 int root_lock;
2352 /* everything at write_lock_level or lower must be write locked */
2353 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002354 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002355 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002356
Chris Mason6702ed42007-08-07 16:15:09 -04002357 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002358 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002359 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002360
Chris Masonbd681512011-07-16 15:23:14 -04002361 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002362 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002363
Chris Masonbd681512011-07-16 15:23:14 -04002364 /* when we are removing items, we might have to go up to level
2365 * two as we update tree pointers Make sure we keep write
2366 * for those levels as well
2367 */
2368 write_lock_level = 2;
2369 } else if (ins_len > 0) {
2370 /*
2371 * for inserting items, make sure we have a write lock on
2372 * level 1 so we can update keys
2373 */
2374 write_lock_level = 1;
2375 }
2376
2377 if (!cow)
2378 write_lock_level = -1;
2379
2380 if (cow && (p->keep_locks || p->lowest_level))
2381 write_lock_level = BTRFS_MAX_LEVEL;
2382
Chris Masonf7c79f32012-03-19 15:54:38 -04002383 min_write_lock_level = write_lock_level;
2384
Chris Masonbb803952007-03-01 12:04:21 -05002385again:
Chris Masonbd681512011-07-16 15:23:14 -04002386 /*
2387 * we try very hard to do read locks on the root
2388 */
2389 root_lock = BTRFS_READ_LOCK;
2390 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002391 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002392 /*
2393 * the commit roots are read only
2394 * so we always do read locks
2395 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002396 b = root->commit_root;
2397 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002398 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002399 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002400 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002401 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002402 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002403 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002404 level = btrfs_header_level(b);
2405 } else {
2406 /* we don't know the level of the root node
2407 * until we actually have it read locked
2408 */
2409 b = btrfs_read_lock_root_node(root);
2410 level = btrfs_header_level(b);
2411 if (level <= write_lock_level) {
2412 /* whoops, must trade for write lock */
2413 btrfs_tree_read_unlock(b);
2414 free_extent_buffer(b);
2415 b = btrfs_lock_root_node(root);
2416 root_lock = BTRFS_WRITE_LOCK;
2417
2418 /* the level might have changed, check again */
2419 level = btrfs_header_level(b);
2420 }
2421 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002422 }
Chris Masonbd681512011-07-16 15:23:14 -04002423 p->nodes[level] = b;
2424 if (!p->skip_locking)
2425 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002426
Chris Masoneb60cea2007-02-02 09:18:22 -05002427 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002428 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002429
2430 /*
2431 * setup the path here so we can release it under lock
2432 * contention with the cow code
2433 */
Chris Mason02217ed2007-03-02 16:08:05 -05002434 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002435 /*
2436 * if we don't really need to cow this block
2437 * then we don't want to set the path blocking,
2438 * so we test it here
2439 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002440 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002441 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002442
Chris Masonb4ce94d2009-02-04 09:25:08 -05002443 btrfs_set_path_blocking(p);
2444
Chris Masonbd681512011-07-16 15:23:14 -04002445 /*
2446 * must have write locks on this node and the
2447 * parent
2448 */
2449 if (level + 1 > write_lock_level) {
2450 write_lock_level = level + 1;
2451 btrfs_release_path(p);
2452 goto again;
2453 }
2454
Yan Zheng33c66f42009-07-22 09:59:00 -04002455 err = btrfs_cow_block(trans, root, b,
2456 p->nodes[level + 1],
2457 p->slots[level + 1], &b);
2458 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002459 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002460 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002461 }
Chris Mason02217ed2007-03-02 16:08:05 -05002462 }
Chris Mason65b51a02008-08-01 15:11:20 -04002463cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002464 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002465
Chris Masoneb60cea2007-02-02 09:18:22 -05002466 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002467 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002468
2469 /*
2470 * we have a lock on b and as long as we aren't changing
2471 * the tree, there is no way to for the items in b to change.
2472 * It is safe to drop the lock on our parent before we
2473 * go through the expensive btree search on b.
2474 *
2475 * If cow is true, then we might be changing slot zero,
2476 * which may require changing the parent. So, we can't
2477 * drop the lock until after we know which slot we're
2478 * operating on.
2479 */
2480 if (!cow)
2481 btrfs_unlock_up_safe(p, level + 1);
2482
Chris Mason5f39d392007-10-15 16:14:19 -04002483 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002484
Chris Mason5f39d392007-10-15 16:14:19 -04002485 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002486 int dec = 0;
2487 if (ret && slot > 0) {
2488 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002489 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002490 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002491 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002492 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002493 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002494 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002495 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002496 if (err) {
2497 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002498 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002499 }
Chris Masonc8c42862009-04-03 10:14:18 -04002500 b = p->nodes[level];
2501 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002502
Chris Masonbd681512011-07-16 15:23:14 -04002503 /*
2504 * slot 0 is special, if we change the key
2505 * we have to update the parent pointer
2506 * which means we must have a write lock
2507 * on the parent
2508 */
2509 if (slot == 0 && cow &&
2510 write_lock_level < level + 1) {
2511 write_lock_level = level + 1;
2512 btrfs_release_path(p);
2513 goto again;
2514 }
2515
Chris Masonf7c79f32012-03-19 15:54:38 -04002516 unlock_up(p, level, lowest_unlock,
2517 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002518
Chris Mason925baed2008-06-25 16:01:30 -04002519 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002520 if (dec)
2521 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002522 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002523 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002524
Yan Zheng33c66f42009-07-22 09:59:00 -04002525 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002526 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002527 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002528 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002529 if (err) {
2530 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002531 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002532 }
Chris Mason76a05b32009-05-14 13:24:30 -04002533
Chris Masonb4ce94d2009-02-04 09:25:08 -05002534 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002535 level = btrfs_header_level(b);
2536 if (level <= write_lock_level) {
2537 err = btrfs_try_tree_write_lock(b);
2538 if (!err) {
2539 btrfs_set_path_blocking(p);
2540 btrfs_tree_lock(b);
2541 btrfs_clear_path_blocking(p, b,
2542 BTRFS_WRITE_LOCK);
2543 }
2544 p->locks[level] = BTRFS_WRITE_LOCK;
2545 } else {
2546 err = btrfs_try_tree_read_lock(b);
2547 if (!err) {
2548 btrfs_set_path_blocking(p);
2549 btrfs_tree_read_lock(b);
2550 btrfs_clear_path_blocking(p, b,
2551 BTRFS_READ_LOCK);
2552 }
2553 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002554 }
Chris Masonbd681512011-07-16 15:23:14 -04002555 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002556 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 } else {
2558 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002559 if (ins_len > 0 &&
2560 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002561 if (write_lock_level < 1) {
2562 write_lock_level = 1;
2563 btrfs_release_path(p);
2564 goto again;
2565 }
2566
Chris Masonb4ce94d2009-02-04 09:25:08 -05002567 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002568 err = split_leaf(trans, root, key,
2569 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002570 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002571
Yan Zheng33c66f42009-07-22 09:59:00 -04002572 BUG_ON(err > 0);
2573 if (err) {
2574 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002575 goto done;
2576 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002577 }
Chris Mason459931e2008-12-10 09:10:46 -05002578 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002579 unlock_up(p, level, lowest_unlock,
2580 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002581 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002582 }
2583 }
Chris Mason65b51a02008-08-01 15:11:20 -04002584 ret = 1;
2585done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002586 /*
2587 * we don't really know what they plan on doing with the path
2588 * from here on, so for now just mark it as blocking
2589 */
Chris Masonb9473432009-03-13 11:00:37 -04002590 if (!p->leave_spinning)
2591 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002592 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002593 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002594 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002595}
2596
Chris Mason74123bd2007-02-02 11:05:29 -05002597/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002598 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2599 * current state of the tree together with the operations recorded in the tree
2600 * modification log to search for the key in a previous version of this tree, as
2601 * denoted by the time_seq parameter.
2602 *
2603 * Naturally, there is no support for insert, delete or cow operations.
2604 *
2605 * The resulting path and return value will be set up as if we called
2606 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2607 */
2608int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2609 struct btrfs_path *p, u64 time_seq)
2610{
2611 struct extent_buffer *b;
2612 int slot;
2613 int ret;
2614 int err;
2615 int level;
2616 int lowest_unlock = 1;
2617 u8 lowest_level = 0;
2618
2619 lowest_level = p->lowest_level;
2620 WARN_ON(p->nodes[0] != NULL);
2621
2622 if (p->search_commit_root) {
2623 BUG_ON(time_seq);
2624 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2625 }
2626
2627again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002628 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002629 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002630 p->locks[level] = BTRFS_READ_LOCK;
2631
2632 while (b) {
2633 level = btrfs_header_level(b);
2634 p->nodes[level] = b;
2635 btrfs_clear_path_blocking(p, NULL, 0);
2636
2637 /*
2638 * we have a lock on b and as long as we aren't changing
2639 * the tree, there is no way to for the items in b to change.
2640 * It is safe to drop the lock on our parent before we
2641 * go through the expensive btree search on b.
2642 */
2643 btrfs_unlock_up_safe(p, level + 1);
2644
2645 ret = bin_search(b, key, level, &slot);
2646
2647 if (level != 0) {
2648 int dec = 0;
2649 if (ret && slot > 0) {
2650 dec = 1;
2651 slot -= 1;
2652 }
2653 p->slots[level] = slot;
2654 unlock_up(p, level, lowest_unlock, 0, NULL);
2655
2656 if (level == lowest_level) {
2657 if (dec)
2658 p->slots[level]++;
2659 goto done;
2660 }
2661
2662 err = read_block_for_search(NULL, root, p, &b, level,
2663 slot, key, time_seq);
2664 if (err == -EAGAIN)
2665 goto again;
2666 if (err) {
2667 ret = err;
2668 goto done;
2669 }
2670
2671 level = btrfs_header_level(b);
2672 err = btrfs_try_tree_read_lock(b);
2673 if (!err) {
2674 btrfs_set_path_blocking(p);
2675 btrfs_tree_read_lock(b);
2676 btrfs_clear_path_blocking(p, b,
2677 BTRFS_READ_LOCK);
2678 }
2679 p->locks[level] = BTRFS_READ_LOCK;
2680 p->nodes[level] = b;
2681 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2682 if (b != p->nodes[level]) {
2683 btrfs_tree_unlock_rw(p->nodes[level],
2684 p->locks[level]);
2685 p->locks[level] = 0;
2686 p->nodes[level] = b;
2687 }
2688 } else {
2689 p->slots[level] = slot;
2690 unlock_up(p, level, lowest_unlock, 0, NULL);
2691 goto done;
2692 }
2693 }
2694 ret = 1;
2695done:
2696 if (!p->leave_spinning)
2697 btrfs_set_path_blocking(p);
2698 if (ret < 0)
2699 btrfs_release_path(p);
2700
2701 return ret;
2702}
2703
2704/*
Chris Mason74123bd2007-02-02 11:05:29 -05002705 * adjust the pointers going up the tree, starting at level
2706 * making sure the right key of each node is points to 'key'.
2707 * This is used after shifting pointers to the left, so it stops
2708 * fixing up pointers when a given leaf/node is not in slot 0 of the
2709 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002710 *
Chris Mason74123bd2007-02-02 11:05:29 -05002711 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002712static void fixup_low_keys(struct btrfs_trans_handle *trans,
2713 struct btrfs_root *root, struct btrfs_path *path,
2714 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002715{
2716 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002717 struct extent_buffer *t;
2718
Chris Mason234b63a2007-03-13 10:46:10 -04002719 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002720 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002721 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002722 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002723 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002724 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002726 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002727 if (tslot != 0)
2728 break;
2729 }
2730}
2731
Chris Mason74123bd2007-02-02 11:05:29 -05002732/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002733 * update item key.
2734 *
2735 * This function isn't completely safe. It's the caller's responsibility
2736 * that the new key won't break the order
2737 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002738void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2739 struct btrfs_root *root, struct btrfs_path *path,
2740 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002741{
2742 struct btrfs_disk_key disk_key;
2743 struct extent_buffer *eb;
2744 int slot;
2745
2746 eb = path->nodes[0];
2747 slot = path->slots[0];
2748 if (slot > 0) {
2749 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002750 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002751 }
2752 if (slot < btrfs_header_nritems(eb) - 1) {
2753 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002754 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002755 }
2756
2757 btrfs_cpu_key_to_disk(&disk_key, new_key);
2758 btrfs_set_item_key(eb, &disk_key, slot);
2759 btrfs_mark_buffer_dirty(eb);
2760 if (slot == 0)
2761 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002762}
2763
2764/*
Chris Mason74123bd2007-02-02 11:05:29 -05002765 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002766 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002767 *
2768 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2769 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002770 */
Chris Mason98ed5172008-01-03 10:01:48 -05002771static int push_node_left(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002773 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002774{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002775 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002776 int src_nritems;
2777 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002778 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002779
Chris Mason5f39d392007-10-15 16:14:19 -04002780 src_nritems = btrfs_header_nritems(src);
2781 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002782 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002783 WARN_ON(btrfs_header_generation(src) != trans->transid);
2784 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002785
Chris Masonbce4eae2008-04-24 14:42:46 -04002786 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002787 return 1;
2788
Chris Masond3977122009-01-05 21:25:51 -05002789 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002790 return 1;
2791
Chris Masonbce4eae2008-04-24 14:42:46 -04002792 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002793 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002794 if (push_items < src_nritems) {
2795 /* leave at least 8 pointers in the node if
2796 * we aren't going to empty it
2797 */
2798 if (src_nritems - push_items < 8) {
2799 if (push_items <= 8)
2800 return 1;
2801 push_items -= 8;
2802 }
2803 }
2804 } else
2805 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002806
Jan Schmidtf2304752012-05-26 11:43:17 +02002807 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2808 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002809 copy_extent_buffer(dst, src,
2810 btrfs_node_key_ptr_offset(dst_nritems),
2811 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002812 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002813
Chris Masonbb803952007-03-01 12:04:21 -05002814 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002815 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2816 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002817 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2818 btrfs_node_key_ptr_offset(push_items),
2819 (src_nritems - push_items) *
2820 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002821 }
Chris Mason5f39d392007-10-15 16:14:19 -04002822 btrfs_set_header_nritems(src, src_nritems - push_items);
2823 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2824 btrfs_mark_buffer_dirty(src);
2825 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002826
Chris Masonbb803952007-03-01 12:04:21 -05002827 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002828}
2829
Chris Mason97571fd2007-02-24 13:39:08 -05002830/*
Chris Mason79f95c82007-03-01 15:16:26 -05002831 * try to push data from one node into the next node right in the
2832 * tree.
2833 *
2834 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2835 * error, and > 0 if there was no room in the right hand block.
2836 *
2837 * this will only push up to 1/2 the contents of the left node over
2838 */
Chris Mason5f39d392007-10-15 16:14:19 -04002839static int balance_node_right(struct btrfs_trans_handle *trans,
2840 struct btrfs_root *root,
2841 struct extent_buffer *dst,
2842 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002843{
Chris Mason79f95c82007-03-01 15:16:26 -05002844 int push_items = 0;
2845 int max_push;
2846 int src_nritems;
2847 int dst_nritems;
2848 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002849
Chris Mason7bb86312007-12-11 09:25:06 -05002850 WARN_ON(btrfs_header_generation(src) != trans->transid);
2851 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2852
Chris Mason5f39d392007-10-15 16:14:19 -04002853 src_nritems = btrfs_header_nritems(src);
2854 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002855 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002856 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002857 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002858
Chris Masond3977122009-01-05 21:25:51 -05002859 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002860 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002861
2862 max_push = src_nritems / 2 + 1;
2863 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002864 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002865 return 1;
Yan252c38f2007-08-29 09:11:44 -04002866
Chris Mason79f95c82007-03-01 15:16:26 -05002867 if (max_push < push_items)
2868 push_items = max_push;
2869
Jan Schmidtf2304752012-05-26 11:43:17 +02002870 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002871 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2872 btrfs_node_key_ptr_offset(0),
2873 (dst_nritems) *
2874 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002875
Jan Schmidtf2304752012-05-26 11:43:17 +02002876 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2877 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002878 copy_extent_buffer(dst, src,
2879 btrfs_node_key_ptr_offset(0),
2880 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002881 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002882
Chris Mason5f39d392007-10-15 16:14:19 -04002883 btrfs_set_header_nritems(src, src_nritems - push_items);
2884 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002885
Chris Mason5f39d392007-10-15 16:14:19 -04002886 btrfs_mark_buffer_dirty(src);
2887 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002888
Chris Mason79f95c82007-03-01 15:16:26 -05002889 return ret;
2890}
2891
2892/*
Chris Mason97571fd2007-02-24 13:39:08 -05002893 * helper function to insert a new root level in the tree.
2894 * A new node is allocated, and a single item is inserted to
2895 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002896 *
2897 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002898 */
Chris Masond3977122009-01-05 21:25:51 -05002899static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002900 struct btrfs_root *root,
2901 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002902{
Chris Mason7bb86312007-12-11 09:25:06 -05002903 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002904 struct extent_buffer *lower;
2905 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002906 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002907 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002908
2909 BUG_ON(path->nodes[level]);
2910 BUG_ON(path->nodes[level-1] != root->node);
2911
Chris Mason7bb86312007-12-11 09:25:06 -05002912 lower = path->nodes[level-1];
2913 if (level == 1)
2914 btrfs_item_key(lower, &lower_key, 0);
2915 else
2916 btrfs_node_key(lower, &lower_key, 0);
2917
Zheng Yan31840ae2008-09-23 13:14:14 -04002918 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002919 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002920 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002921 if (IS_ERR(c))
2922 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002923
Yan, Zhengf0486c62010-05-16 10:46:25 -04002924 root_add_used(root, root->nodesize);
2925
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002926 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002927 btrfs_set_header_nritems(c, 1);
2928 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002929 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002930 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002931 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002932 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002933
Chris Mason5f39d392007-10-15 16:14:19 -04002934 write_extent_buffer(c, root->fs_info->fsid,
2935 (unsigned long)btrfs_header_fsid(c),
2936 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002937
2938 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2939 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2940 BTRFS_UUID_SIZE);
2941
Chris Mason5f39d392007-10-15 16:14:19 -04002942 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002943 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002944 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002945 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002946
2947 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002948
2949 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002950
Chris Mason925baed2008-06-25 16:01:30 -04002951 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002952 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002953 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002954
2955 /* the super has an extra ref to root->node */
2956 free_extent_buffer(old);
2957
Chris Mason0b86a832008-03-24 15:01:56 -04002958 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002959 extent_buffer_get(c);
2960 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002961 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002962 path->slots[level] = 0;
2963 return 0;
2964}
2965
Chris Mason74123bd2007-02-02 11:05:29 -05002966/*
2967 * worker function to insert a single pointer in a node.
2968 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002969 *
Chris Mason74123bd2007-02-02 11:05:29 -05002970 * slot and level indicate where you want the key to go, and
2971 * blocknr is the block the key points to.
2972 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002973static void insert_ptr(struct btrfs_trans_handle *trans,
2974 struct btrfs_root *root, struct btrfs_path *path,
2975 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002976 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002977{
Chris Mason5f39d392007-10-15 16:14:19 -04002978 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002979 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002980 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002981
2982 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002983 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002984 lower = path->nodes[level];
2985 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002986 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002987 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002988 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002989 if (tree_mod_log && level)
2990 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2991 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002992 memmove_extent_buffer(lower,
2993 btrfs_node_key_ptr_offset(slot + 1),
2994 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002995 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002996 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002997 if (tree_mod_log && level) {
2998 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2999 MOD_LOG_KEY_ADD);
3000 BUG_ON(ret < 0);
3001 }
Chris Mason5f39d392007-10-15 16:14:19 -04003002 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003003 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003004 WARN_ON(trans->transid == 0);
3005 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003006 btrfs_set_header_nritems(lower, nritems + 1);
3007 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003008}
3009
Chris Mason97571fd2007-02-24 13:39:08 -05003010/*
3011 * split the node at the specified level in path in two.
3012 * The path is corrected to point to the appropriate node after the split
3013 *
3014 * Before splitting this tries to make some room in the node by pushing
3015 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003016 *
3017 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003018 */
Chris Masone02119d2008-09-05 16:13:11 -04003019static noinline int split_node(struct btrfs_trans_handle *trans,
3020 struct btrfs_root *root,
3021 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003022{
Chris Mason5f39d392007-10-15 16:14:19 -04003023 struct extent_buffer *c;
3024 struct extent_buffer *split;
3025 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003026 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003027 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003028 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003029
Chris Mason5f39d392007-10-15 16:14:19 -04003030 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003031 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003032 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003033 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003034 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003035 if (ret)
3036 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003037 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003038 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003039 c = path->nodes[level];
3040 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003041 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003042 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003043 if (ret < 0)
3044 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003045 }
Chris Masone66f7092007-04-20 13:16:02 -04003046
Chris Mason5f39d392007-10-15 16:14:19 -04003047 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048 mid = (c_nritems + 1) / 2;
3049 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003050
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003051 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003052 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003053 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003054 if (IS_ERR(split))
3055 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003056
Yan, Zhengf0486c62010-05-16 10:46:25 -04003057 root_add_used(root, root->nodesize);
3058
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003059 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003060 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003061 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003062 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003063 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003064 btrfs_set_header_owner(split, root->root_key.objectid);
3065 write_extent_buffer(split, root->fs_info->fsid,
3066 (unsigned long)btrfs_header_fsid(split),
3067 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003068 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3069 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3070 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003071
Jan Schmidtf2304752012-05-26 11:43:17 +02003072 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003073 copy_extent_buffer(split, c,
3074 btrfs_node_key_ptr_offset(0),
3075 btrfs_node_key_ptr_offset(mid),
3076 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3077 btrfs_set_header_nritems(split, c_nritems - mid);
3078 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003079 ret = 0;
3080
Chris Mason5f39d392007-10-15 16:14:19 -04003081 btrfs_mark_buffer_dirty(c);
3082 btrfs_mark_buffer_dirty(split);
3083
Jeff Mahoney143bede2012-03-01 14:56:26 +01003084 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003085 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003086
Chris Mason5de08d72007-02-24 06:24:44 -05003087 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003088 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003089 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003090 free_extent_buffer(c);
3091 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003092 path->slots[level + 1] += 1;
3093 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003094 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003095 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003096 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003097 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003098}
3099
Chris Mason74123bd2007-02-02 11:05:29 -05003100/*
3101 * how many bytes are required to store the items in a leaf. start
3102 * and nr indicate which items in the leaf to check. This totals up the
3103 * space used both by the item structs and the item data
3104 */
Chris Mason5f39d392007-10-15 16:14:19 -04003105static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003106{
3107 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003108 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003109 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003110
3111 if (!nr)
3112 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003113 data_len = btrfs_item_end_nr(l, start);
3114 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003115 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003116 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003117 return data_len;
3118}
3119
Chris Mason74123bd2007-02-02 11:05:29 -05003120/*
Chris Masond4dbff92007-04-04 14:08:15 -04003121 * The space between the end of the leaf items and
3122 * the start of the leaf data. IOW, how much room
3123 * the leaf has left for both items and data
3124 */
Chris Masond3977122009-01-05 21:25:51 -05003125noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003126 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003127{
Chris Mason5f39d392007-10-15 16:14:19 -04003128 int nritems = btrfs_header_nritems(leaf);
3129 int ret;
3130 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3131 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003132 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3133 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003134 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003135 leaf_space_used(leaf, 0, nritems), nritems);
3136 }
3137 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003138}
3139
Chris Mason99d8f832010-07-07 10:51:48 -04003140/*
3141 * min slot controls the lowest index we're willing to push to the
3142 * right. We'll push up to and including min_slot, but no lower
3143 */
Chris Mason44871b12009-03-13 10:04:31 -04003144static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3145 struct btrfs_root *root,
3146 struct btrfs_path *path,
3147 int data_size, int empty,
3148 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003149 int free_space, u32 left_nritems,
3150 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003151{
Chris Mason5f39d392007-10-15 16:14:19 -04003152 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003153 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003154 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003155 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003156 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003157 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003158 int push_space = 0;
3159 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003160 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003161 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003162 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003163 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003164 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003165
Chris Masoncfed81a2012-03-03 07:40:03 -05003166 btrfs_init_map_token(&token);
3167
Chris Mason34a38212007-11-07 13:31:03 -05003168 if (empty)
3169 nr = 0;
3170 else
Chris Mason99d8f832010-07-07 10:51:48 -04003171 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003172
Zheng Yan31840ae2008-09-23 13:14:14 -04003173 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003174 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003175
Chris Mason44871b12009-03-13 10:04:31 -04003176 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003177 i = left_nritems - 1;
3178 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003179 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003180
Zheng Yan31840ae2008-09-23 13:14:14 -04003181 if (!empty && push_items > 0) {
3182 if (path->slots[0] > i)
3183 break;
3184 if (path->slots[0] == i) {
3185 int space = btrfs_leaf_free_space(root, left);
3186 if (space + push_space * 2 > free_space)
3187 break;
3188 }
3189 }
3190
Chris Mason00ec4c52007-02-24 12:47:20 -05003191 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003192 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003193
Chris Masondb945352007-10-15 16:15:53 -04003194 this_item_size = btrfs_item_size(left, item);
3195 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003196 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003197
Chris Mason00ec4c52007-02-24 12:47:20 -05003198 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003199 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003200 if (i == 0)
3201 break;
3202 i--;
Chris Masondb945352007-10-15 16:15:53 -04003203 }
Chris Mason5f39d392007-10-15 16:14:19 -04003204
Chris Mason925baed2008-06-25 16:01:30 -04003205 if (push_items == 0)
3206 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003207
Chris Mason34a38212007-11-07 13:31:03 -05003208 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003209 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003210
Chris Mason00ec4c52007-02-24 12:47:20 -05003211 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003212 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003213
Chris Mason5f39d392007-10-15 16:14:19 -04003214 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003215 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003216
Chris Mason00ec4c52007-02-24 12:47:20 -05003217 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003218 data_end = leaf_data_end(root, right);
3219 memmove_extent_buffer(right,
3220 btrfs_leaf_data(right) + data_end - push_space,
3221 btrfs_leaf_data(right) + data_end,
3222 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3223
Chris Mason00ec4c52007-02-24 12:47:20 -05003224 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003225 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003226 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3227 btrfs_leaf_data(left) + leaf_data_end(root, left),
3228 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003229
3230 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3231 btrfs_item_nr_offset(0),
3232 right_nritems * sizeof(struct btrfs_item));
3233
Chris Mason00ec4c52007-02-24 12:47:20 -05003234 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003235 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3236 btrfs_item_nr_offset(left_nritems - push_items),
3237 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003238
3239 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003240 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003241 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003242 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003243 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003244 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003245 push_space -= btrfs_token_item_size(right, item, &token);
3246 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003247 }
3248
Chris Mason7518a232007-03-12 12:01:18 -04003249 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003250 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003251
Chris Mason34a38212007-11-07 13:31:03 -05003252 if (left_nritems)
3253 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003254 else
3255 clean_tree_block(trans, root, left);
3256
Chris Mason5f39d392007-10-15 16:14:19 -04003257 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003258
Chris Mason5f39d392007-10-15 16:14:19 -04003259 btrfs_item_key(right, &disk_key, 0);
3260 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003261 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003262
Chris Mason00ec4c52007-02-24 12:47:20 -05003263 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003264 if (path->slots[0] >= left_nritems) {
3265 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003266 if (btrfs_header_nritems(path->nodes[0]) == 0)
3267 clean_tree_block(trans, root, path->nodes[0]);
3268 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003269 free_extent_buffer(path->nodes[0]);
3270 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003271 path->slots[1] += 1;
3272 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003273 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003274 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003275 }
3276 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003277
3278out_unlock:
3279 btrfs_tree_unlock(right);
3280 free_extent_buffer(right);
3281 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003282}
Chris Mason925baed2008-06-25 16:01:30 -04003283
Chris Mason00ec4c52007-02-24 12:47:20 -05003284/*
Chris Mason44871b12009-03-13 10:04:31 -04003285 * push some data in the path leaf to the right, trying to free up at
3286 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3287 *
3288 * returns 1 if the push failed because the other node didn't have enough
3289 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003290 *
3291 * this will push starting from min_slot to the end of the leaf. It won't
3292 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003293 */
3294static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003295 *root, struct btrfs_path *path,
3296 int min_data_size, int data_size,
3297 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003298{
3299 struct extent_buffer *left = path->nodes[0];
3300 struct extent_buffer *right;
3301 struct extent_buffer *upper;
3302 int slot;
3303 int free_space;
3304 u32 left_nritems;
3305 int ret;
3306
3307 if (!path->nodes[1])
3308 return 1;
3309
3310 slot = path->slots[1];
3311 upper = path->nodes[1];
3312 if (slot >= btrfs_header_nritems(upper) - 1)
3313 return 1;
3314
3315 btrfs_assert_tree_locked(path->nodes[1]);
3316
3317 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003318 if (right == NULL)
3319 return 1;
3320
Chris Mason44871b12009-03-13 10:04:31 -04003321 btrfs_tree_lock(right);
3322 btrfs_set_lock_blocking(right);
3323
3324 free_space = btrfs_leaf_free_space(root, right);
3325 if (free_space < data_size)
3326 goto out_unlock;
3327
3328 /* cow and double check */
3329 ret = btrfs_cow_block(trans, root, right, upper,
3330 slot + 1, &right);
3331 if (ret)
3332 goto out_unlock;
3333
3334 free_space = btrfs_leaf_free_space(root, right);
3335 if (free_space < data_size)
3336 goto out_unlock;
3337
3338 left_nritems = btrfs_header_nritems(left);
3339 if (left_nritems == 0)
3340 goto out_unlock;
3341
Chris Mason99d8f832010-07-07 10:51:48 -04003342 return __push_leaf_right(trans, root, path, min_data_size, empty,
3343 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003344out_unlock:
3345 btrfs_tree_unlock(right);
3346 free_extent_buffer(right);
3347 return 1;
3348}
3349
3350/*
Chris Mason74123bd2007-02-02 11:05:29 -05003351 * push some data in the path leaf to the left, trying to free up at
3352 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003353 *
3354 * max_slot can put a limit on how far into the leaf we'll push items. The
3355 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3356 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003357 */
Chris Mason44871b12009-03-13 10:04:31 -04003358static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3359 struct btrfs_root *root,
3360 struct btrfs_path *path, int data_size,
3361 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003362 int free_space, u32 right_nritems,
3363 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003364{
Chris Mason5f39d392007-10-15 16:14:19 -04003365 struct btrfs_disk_key disk_key;
3366 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003367 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003368 int push_space = 0;
3369 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003370 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003371 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003372 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003373 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003374 u32 this_item_size;
3375 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003376 struct btrfs_map_token token;
3377
3378 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003379
Chris Mason34a38212007-11-07 13:31:03 -05003380 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003381 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003382 else
Chris Mason99d8f832010-07-07 10:51:48 -04003383 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003384
3385 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003386 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003387
Zheng Yan31840ae2008-09-23 13:14:14 -04003388 if (!empty && push_items > 0) {
3389 if (path->slots[0] < i)
3390 break;
3391 if (path->slots[0] == i) {
3392 int space = btrfs_leaf_free_space(root, right);
3393 if (space + push_space * 2 > free_space)
3394 break;
3395 }
3396 }
3397
Chris Masonbe0e5c02007-01-26 15:51:26 -05003398 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003399 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003400
3401 this_item_size = btrfs_item_size(right, item);
3402 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003403 break;
Chris Masondb945352007-10-15 16:15:53 -04003404
Chris Masonbe0e5c02007-01-26 15:51:26 -05003405 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003406 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003407 }
Chris Masondb945352007-10-15 16:15:53 -04003408
Chris Masonbe0e5c02007-01-26 15:51:26 -05003409 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003410 ret = 1;
3411 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003412 }
Chris Mason34a38212007-11-07 13:31:03 -05003413 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003414 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003415
Chris Masonbe0e5c02007-01-26 15:51:26 -05003416 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003417 copy_extent_buffer(left, right,
3418 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3419 btrfs_item_nr_offset(0),
3420 push_items * sizeof(struct btrfs_item));
3421
Chris Mason123abc82007-03-14 14:14:43 -04003422 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003423 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003424
3425 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003426 leaf_data_end(root, left) - push_space,
3427 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003428 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003429 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003430 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003431 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003432
Chris Masondb945352007-10-15 16:15:53 -04003433 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003434 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003435 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003436
Chris Mason5f39d392007-10-15 16:14:19 -04003437 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003438
Chris Masoncfed81a2012-03-03 07:40:03 -05003439 ioff = btrfs_token_item_offset(left, item, &token);
3440 btrfs_set_token_item_offset(left, item,
3441 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3442 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003443 }
Chris Mason5f39d392007-10-15 16:14:19 -04003444 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003445
3446 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003447 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003448 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3449 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003450 WARN_ON(1);
3451 }
Chris Mason5f39d392007-10-15 16:14:19 -04003452
Chris Mason34a38212007-11-07 13:31:03 -05003453 if (push_items < right_nritems) {
3454 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3455 leaf_data_end(root, right);
3456 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3457 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3458 btrfs_leaf_data(right) +
3459 leaf_data_end(root, right), push_space);
3460
3461 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003462 btrfs_item_nr_offset(push_items),
3463 (btrfs_header_nritems(right) - push_items) *
3464 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003465 }
Yaneef1c492007-11-26 10:58:13 -05003466 right_nritems -= push_items;
3467 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003468 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003469 for (i = 0; i < right_nritems; i++) {
3470 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003471
Chris Masoncfed81a2012-03-03 07:40:03 -05003472 push_space = push_space - btrfs_token_item_size(right,
3473 item, &token);
3474 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003475 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003476
Chris Mason5f39d392007-10-15 16:14:19 -04003477 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003478 if (right_nritems)
3479 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003480 else
3481 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003482
Chris Mason5f39d392007-10-15 16:14:19 -04003483 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003484 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003485
3486 /* then fixup the leaf pointer in the path */
3487 if (path->slots[0] < push_items) {
3488 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003489 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003490 free_extent_buffer(path->nodes[0]);
3491 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003492 path->slots[1] -= 1;
3493 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003494 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003496 path->slots[0] -= push_items;
3497 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003498 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003499 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003500out:
3501 btrfs_tree_unlock(left);
3502 free_extent_buffer(left);
3503 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003504}
3505
Chris Mason74123bd2007-02-02 11:05:29 -05003506/*
Chris Mason44871b12009-03-13 10:04:31 -04003507 * push some data in the path leaf to the left, trying to free up at
3508 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003509 *
3510 * max_slot can put a limit on how far into the leaf we'll push items. The
3511 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3512 * items
Chris Mason44871b12009-03-13 10:04:31 -04003513 */
3514static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003515 *root, struct btrfs_path *path, int min_data_size,
3516 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003517{
3518 struct extent_buffer *right = path->nodes[0];
3519 struct extent_buffer *left;
3520 int slot;
3521 int free_space;
3522 u32 right_nritems;
3523 int ret = 0;
3524
3525 slot = path->slots[1];
3526 if (slot == 0)
3527 return 1;
3528 if (!path->nodes[1])
3529 return 1;
3530
3531 right_nritems = btrfs_header_nritems(right);
3532 if (right_nritems == 0)
3533 return 1;
3534
3535 btrfs_assert_tree_locked(path->nodes[1]);
3536
3537 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003538 if (left == NULL)
3539 return 1;
3540
Chris Mason44871b12009-03-13 10:04:31 -04003541 btrfs_tree_lock(left);
3542 btrfs_set_lock_blocking(left);
3543
3544 free_space = btrfs_leaf_free_space(root, left);
3545 if (free_space < data_size) {
3546 ret = 1;
3547 goto out;
3548 }
3549
3550 /* cow and double check */
3551 ret = btrfs_cow_block(trans, root, left,
3552 path->nodes[1], slot - 1, &left);
3553 if (ret) {
3554 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003555 if (ret == -ENOSPC)
3556 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003557 goto out;
3558 }
3559
3560 free_space = btrfs_leaf_free_space(root, left);
3561 if (free_space < data_size) {
3562 ret = 1;
3563 goto out;
3564 }
3565
Chris Mason99d8f832010-07-07 10:51:48 -04003566 return __push_leaf_left(trans, root, path, min_data_size,
3567 empty, left, free_space, right_nritems,
3568 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003569out:
3570 btrfs_tree_unlock(left);
3571 free_extent_buffer(left);
3572 return ret;
3573}
3574
3575/*
Chris Mason74123bd2007-02-02 11:05:29 -05003576 * split the path's leaf in two, making sure there is at least data_size
3577 * available for the resulting leaf level of the path.
3578 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003579static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3580 struct btrfs_root *root,
3581 struct btrfs_path *path,
3582 struct extent_buffer *l,
3583 struct extent_buffer *right,
3584 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003585{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003586 int data_copy_size;
3587 int rt_data_off;
3588 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003589 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003590 struct btrfs_map_token token;
3591
3592 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593
Chris Mason5f39d392007-10-15 16:14:19 -04003594 nritems = nritems - mid;
3595 btrfs_set_header_nritems(right, nritems);
3596 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3597
3598 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3599 btrfs_item_nr_offset(mid),
3600 nritems * sizeof(struct btrfs_item));
3601
3602 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003603 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3604 data_copy_size, btrfs_leaf_data(l) +
3605 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003606
Chris Mason5f39d392007-10-15 16:14:19 -04003607 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3608 btrfs_item_end_nr(l, mid);
3609
3610 for (i = 0; i < nritems; i++) {
3611 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003612 u32 ioff;
3613
Chris Masoncfed81a2012-03-03 07:40:03 -05003614 ioff = btrfs_token_item_offset(right, item, &token);
3615 btrfs_set_token_item_offset(right, item,
3616 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003617 }
Chris Mason74123bd2007-02-02 11:05:29 -05003618
Chris Mason5f39d392007-10-15 16:14:19 -04003619 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003620 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003621 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003622 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003623
3624 btrfs_mark_buffer_dirty(right);
3625 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003626 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003627
Chris Masonbe0e5c02007-01-26 15:51:26 -05003628 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003629 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003630 free_extent_buffer(path->nodes[0]);
3631 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003632 path->slots[0] -= mid;
3633 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003634 } else {
3635 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003636 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003637 }
Chris Mason5f39d392007-10-15 16:14:19 -04003638
Chris Masoneb60cea2007-02-02 09:18:22 -05003639 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003640}
3641
3642/*
Chris Mason99d8f832010-07-07 10:51:48 -04003643 * double splits happen when we need to insert a big item in the middle
3644 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3645 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3646 * A B C
3647 *
3648 * We avoid this by trying to push the items on either side of our target
3649 * into the adjacent leaves. If all goes well we can avoid the double split
3650 * completely.
3651 */
3652static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3653 struct btrfs_root *root,
3654 struct btrfs_path *path,
3655 int data_size)
3656{
3657 int ret;
3658 int progress = 0;
3659 int slot;
3660 u32 nritems;
3661
3662 slot = path->slots[0];
3663
3664 /*
3665 * try to push all the items after our slot into the
3666 * right leaf
3667 */
3668 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3669 if (ret < 0)
3670 return ret;
3671
3672 if (ret == 0)
3673 progress++;
3674
3675 nritems = btrfs_header_nritems(path->nodes[0]);
3676 /*
3677 * our goal is to get our slot at the start or end of a leaf. If
3678 * we've done so we're done
3679 */
3680 if (path->slots[0] == 0 || path->slots[0] == nritems)
3681 return 0;
3682
3683 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3684 return 0;
3685
3686 /* try to push all the items before our slot into the next leaf */
3687 slot = path->slots[0];
3688 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3689 if (ret < 0)
3690 return ret;
3691
3692 if (ret == 0)
3693 progress++;
3694
3695 if (progress)
3696 return 0;
3697 return 1;
3698}
3699
3700/*
Chris Mason44871b12009-03-13 10:04:31 -04003701 * split the path's leaf in two, making sure there is at least data_size
3702 * available for the resulting leaf level of the path.
3703 *
3704 * returns 0 if all went well and < 0 on failure.
3705 */
3706static noinline int split_leaf(struct btrfs_trans_handle *trans,
3707 struct btrfs_root *root,
3708 struct btrfs_key *ins_key,
3709 struct btrfs_path *path, int data_size,
3710 int extend)
3711{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003712 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003713 struct extent_buffer *l;
3714 u32 nritems;
3715 int mid;
3716 int slot;
3717 struct extent_buffer *right;
3718 int ret = 0;
3719 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003720 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003721 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003722 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003723
Yan, Zhenga5719522009-09-24 09:17:31 -04003724 l = path->nodes[0];
3725 slot = path->slots[0];
3726 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3727 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3728 return -EOVERFLOW;
3729
Chris Mason44871b12009-03-13 10:04:31 -04003730 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003731 if (data_size) {
3732 wret = push_leaf_right(trans, root, path, data_size,
3733 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003734 if (wret < 0)
3735 return wret;
3736 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003737 wret = push_leaf_left(trans, root, path, data_size,
3738 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003739 if (wret < 0)
3740 return wret;
3741 }
3742 l = path->nodes[0];
3743
3744 /* did the pushes work? */
3745 if (btrfs_leaf_free_space(root, l) >= data_size)
3746 return 0;
3747 }
3748
3749 if (!path->nodes[1]) {
3750 ret = insert_new_root(trans, root, path, 1);
3751 if (ret)
3752 return ret;
3753 }
3754again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003755 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003756 l = path->nodes[0];
3757 slot = path->slots[0];
3758 nritems = btrfs_header_nritems(l);
3759 mid = (nritems + 1) / 2;
3760
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003761 if (mid <= slot) {
3762 if (nritems == 1 ||
3763 leaf_space_used(l, mid, nritems - mid) + data_size >
3764 BTRFS_LEAF_DATA_SIZE(root)) {
3765 if (slot >= nritems) {
3766 split = 0;
3767 } else {
3768 mid = slot;
3769 if (mid != nritems &&
3770 leaf_space_used(l, mid, nritems - mid) +
3771 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003772 if (data_size && !tried_avoid_double)
3773 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003774 split = 2;
3775 }
3776 }
3777 }
3778 } else {
3779 if (leaf_space_used(l, 0, mid) + data_size >
3780 BTRFS_LEAF_DATA_SIZE(root)) {
3781 if (!extend && data_size && slot == 0) {
3782 split = 0;
3783 } else if ((extend || !data_size) && slot == 0) {
3784 mid = 1;
3785 } else {
3786 mid = slot;
3787 if (mid != nritems &&
3788 leaf_space_used(l, mid, nritems - mid) +
3789 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003790 if (data_size && !tried_avoid_double)
3791 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003792 split = 2 ;
3793 }
3794 }
3795 }
3796 }
3797
3798 if (split == 0)
3799 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3800 else
3801 btrfs_item_key(l, &disk_key, mid);
3802
3803 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003804 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003805 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003806 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003807 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003808
3809 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003810
3811 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3812 btrfs_set_header_bytenr(right, right->start);
3813 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003814 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003815 btrfs_set_header_owner(right, root->root_key.objectid);
3816 btrfs_set_header_level(right, 0);
3817 write_extent_buffer(right, root->fs_info->fsid,
3818 (unsigned long)btrfs_header_fsid(right),
3819 BTRFS_FSID_SIZE);
3820
3821 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3822 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3823 BTRFS_UUID_SIZE);
3824
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003825 if (split == 0) {
3826 if (mid <= slot) {
3827 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003828 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003829 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003830 btrfs_tree_unlock(path->nodes[0]);
3831 free_extent_buffer(path->nodes[0]);
3832 path->nodes[0] = right;
3833 path->slots[0] = 0;
3834 path->slots[1] += 1;
3835 } else {
3836 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003837 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003838 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003839 btrfs_tree_unlock(path->nodes[0]);
3840 free_extent_buffer(path->nodes[0]);
3841 path->nodes[0] = right;
3842 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003843 if (path->slots[1] == 0)
3844 fixup_low_keys(trans, root, path,
3845 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003846 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003847 btrfs_mark_buffer_dirty(right);
3848 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003849 }
3850
Jeff Mahoney143bede2012-03-01 14:56:26 +01003851 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003852
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003853 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003854 BUG_ON(num_doubles != 0);
3855 num_doubles++;
3856 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003857 }
Chris Mason44871b12009-03-13 10:04:31 -04003858
Jeff Mahoney143bede2012-03-01 14:56:26 +01003859 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003860
3861push_for_double:
3862 push_for_double_split(trans, root, path, data_size);
3863 tried_avoid_double = 1;
3864 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3865 return 0;
3866 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003867}
3868
Yan, Zhengad48fd752009-11-12 09:33:58 +00003869static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3870 struct btrfs_root *root,
3871 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003872{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003873 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003874 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003875 struct btrfs_file_extent_item *fi;
3876 u64 extent_len = 0;
3877 u32 item_size;
3878 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003879
3880 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003881 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3882
3883 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3884 key.type != BTRFS_EXTENT_CSUM_KEY);
3885
3886 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3887 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003888
3889 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003890 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3891 fi = btrfs_item_ptr(leaf, path->slots[0],
3892 struct btrfs_file_extent_item);
3893 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3894 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003895 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003896
Chris Mason459931e2008-12-10 09:10:46 -05003897 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003898 path->search_for_split = 1;
3899 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003900 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003901 if (ret < 0)
3902 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003903
Yan, Zhengad48fd752009-11-12 09:33:58 +00003904 ret = -EAGAIN;
3905 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003906 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003907 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3908 goto err;
3909
Chris Mason109f6ae2010-04-02 09:20:18 -04003910 /* the leaf has changed, it now has room. return now */
3911 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3912 goto err;
3913
Yan, Zhengad48fd752009-11-12 09:33:58 +00003914 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3915 fi = btrfs_item_ptr(leaf, path->slots[0],
3916 struct btrfs_file_extent_item);
3917 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3918 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003919 }
3920
Chris Masonb9473432009-03-13 11:00:37 -04003921 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003922 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003923 if (ret)
3924 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003925
Yan, Zhengad48fd752009-11-12 09:33:58 +00003926 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003927 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003928 return 0;
3929err:
3930 path->keep_locks = 0;
3931 return ret;
3932}
3933
3934static noinline int split_item(struct btrfs_trans_handle *trans,
3935 struct btrfs_root *root,
3936 struct btrfs_path *path,
3937 struct btrfs_key *new_key,
3938 unsigned long split_offset)
3939{
3940 struct extent_buffer *leaf;
3941 struct btrfs_item *item;
3942 struct btrfs_item *new_item;
3943 int slot;
3944 char *buf;
3945 u32 nritems;
3946 u32 item_size;
3947 u32 orig_offset;
3948 struct btrfs_disk_key disk_key;
3949
Chris Masonb9473432009-03-13 11:00:37 -04003950 leaf = path->nodes[0];
3951 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3952
Chris Masonb4ce94d2009-02-04 09:25:08 -05003953 btrfs_set_path_blocking(path);
3954
Chris Mason459931e2008-12-10 09:10:46 -05003955 item = btrfs_item_nr(leaf, path->slots[0]);
3956 orig_offset = btrfs_item_offset(leaf, item);
3957 item_size = btrfs_item_size(leaf, item);
3958
Chris Mason459931e2008-12-10 09:10:46 -05003959 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003960 if (!buf)
3961 return -ENOMEM;
3962
Chris Mason459931e2008-12-10 09:10:46 -05003963 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3964 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003965
Chris Mason459931e2008-12-10 09:10:46 -05003966 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003967 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003968 if (slot != nritems) {
3969 /* shift the items */
3970 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003971 btrfs_item_nr_offset(slot),
3972 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003973 }
3974
3975 btrfs_cpu_key_to_disk(&disk_key, new_key);
3976 btrfs_set_item_key(leaf, &disk_key, slot);
3977
3978 new_item = btrfs_item_nr(leaf, slot);
3979
3980 btrfs_set_item_offset(leaf, new_item, orig_offset);
3981 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3982
3983 btrfs_set_item_offset(leaf, item,
3984 orig_offset + item_size - split_offset);
3985 btrfs_set_item_size(leaf, item, split_offset);
3986
3987 btrfs_set_header_nritems(leaf, nritems + 1);
3988
3989 /* write the data for the start of the original item */
3990 write_extent_buffer(leaf, buf,
3991 btrfs_item_ptr_offset(leaf, path->slots[0]),
3992 split_offset);
3993
3994 /* write the data for the new item */
3995 write_extent_buffer(leaf, buf + split_offset,
3996 btrfs_item_ptr_offset(leaf, slot),
3997 item_size - split_offset);
3998 btrfs_mark_buffer_dirty(leaf);
3999
Yan, Zhengad48fd752009-11-12 09:33:58 +00004000 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004001 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004002 return 0;
4003}
4004
4005/*
4006 * This function splits a single item into two items,
4007 * giving 'new_key' to the new item and splitting the
4008 * old one at split_offset (from the start of the item).
4009 *
4010 * The path may be released by this operation. After
4011 * the split, the path is pointing to the old item. The
4012 * new item is going to be in the same node as the old one.
4013 *
4014 * Note, the item being split must be smaller enough to live alone on
4015 * a tree block with room for one extra struct btrfs_item
4016 *
4017 * This allows us to split the item in place, keeping a lock on the
4018 * leaf the entire time.
4019 */
4020int btrfs_split_item(struct btrfs_trans_handle *trans,
4021 struct btrfs_root *root,
4022 struct btrfs_path *path,
4023 struct btrfs_key *new_key,
4024 unsigned long split_offset)
4025{
4026 int ret;
4027 ret = setup_leaf_for_split(trans, root, path,
4028 sizeof(struct btrfs_item));
4029 if (ret)
4030 return ret;
4031
4032 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004033 return ret;
4034}
4035
4036/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004037 * This function duplicate a item, giving 'new_key' to the new item.
4038 * It guarantees both items live in the same tree leaf and the new item
4039 * is contiguous with the original item.
4040 *
4041 * This allows us to split file extent in place, keeping a lock on the
4042 * leaf the entire time.
4043 */
4044int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4045 struct btrfs_root *root,
4046 struct btrfs_path *path,
4047 struct btrfs_key *new_key)
4048{
4049 struct extent_buffer *leaf;
4050 int ret;
4051 u32 item_size;
4052
4053 leaf = path->nodes[0];
4054 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4055 ret = setup_leaf_for_split(trans, root, path,
4056 item_size + sizeof(struct btrfs_item));
4057 if (ret)
4058 return ret;
4059
4060 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004061 setup_items_for_insert(trans, root, path, new_key, &item_size,
4062 item_size, item_size +
4063 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004064 leaf = path->nodes[0];
4065 memcpy_extent_buffer(leaf,
4066 btrfs_item_ptr_offset(leaf, path->slots[0]),
4067 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4068 item_size);
4069 return 0;
4070}
4071
4072/*
Chris Masond352ac62008-09-29 15:18:18 -04004073 * make the item pointed to by the path smaller. new_size indicates
4074 * how small to make it, and from_end tells us if we just chop bytes
4075 * off the end of the item or if we shift the item to chop bytes off
4076 * the front.
4077 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004078void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4079 struct btrfs_root *root,
4080 struct btrfs_path *path,
4081 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004082{
Chris Masonb18c6682007-04-17 13:26:50 -04004083 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004084 struct extent_buffer *leaf;
4085 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004086 u32 nritems;
4087 unsigned int data_end;
4088 unsigned int old_data_start;
4089 unsigned int old_size;
4090 unsigned int size_diff;
4091 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004092 struct btrfs_map_token token;
4093
4094 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004095
Chris Mason5f39d392007-10-15 16:14:19 -04004096 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004097 slot = path->slots[0];
4098
4099 old_size = btrfs_item_size_nr(leaf, slot);
4100 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004101 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004102
Chris Mason5f39d392007-10-15 16:14:19 -04004103 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004104 data_end = leaf_data_end(root, leaf);
4105
Chris Mason5f39d392007-10-15 16:14:19 -04004106 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004107
Chris Masonb18c6682007-04-17 13:26:50 -04004108 size_diff = old_size - new_size;
4109
4110 BUG_ON(slot < 0);
4111 BUG_ON(slot >= nritems);
4112
4113 /*
4114 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4115 */
4116 /* first correct the data pointers */
4117 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004118 u32 ioff;
4119 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004120
Chris Masoncfed81a2012-03-03 07:40:03 -05004121 ioff = btrfs_token_item_offset(leaf, item, &token);
4122 btrfs_set_token_item_offset(leaf, item,
4123 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004124 }
Chris Masondb945352007-10-15 16:15:53 -04004125
Chris Masonb18c6682007-04-17 13:26:50 -04004126 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004127 if (from_end) {
4128 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4129 data_end + size_diff, btrfs_leaf_data(leaf) +
4130 data_end, old_data_start + new_size - data_end);
4131 } else {
4132 struct btrfs_disk_key disk_key;
4133 u64 offset;
4134
4135 btrfs_item_key(leaf, &disk_key, slot);
4136
4137 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4138 unsigned long ptr;
4139 struct btrfs_file_extent_item *fi;
4140
4141 fi = btrfs_item_ptr(leaf, slot,
4142 struct btrfs_file_extent_item);
4143 fi = (struct btrfs_file_extent_item *)(
4144 (unsigned long)fi - size_diff);
4145
4146 if (btrfs_file_extent_type(leaf, fi) ==
4147 BTRFS_FILE_EXTENT_INLINE) {
4148 ptr = btrfs_item_ptr_offset(leaf, slot);
4149 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004150 (unsigned long)fi,
4151 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004152 disk_bytenr));
4153 }
4154 }
4155
4156 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4157 data_end + size_diff, btrfs_leaf_data(leaf) +
4158 data_end, old_data_start - data_end);
4159
4160 offset = btrfs_disk_key_offset(&disk_key);
4161 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4162 btrfs_set_item_key(leaf, &disk_key, slot);
4163 if (slot == 0)
4164 fixup_low_keys(trans, root, path, &disk_key, 1);
4165 }
Chris Mason5f39d392007-10-15 16:14:19 -04004166
4167 item = btrfs_item_nr(leaf, slot);
4168 btrfs_set_item_size(leaf, item, new_size);
4169 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004170
Chris Mason5f39d392007-10-15 16:14:19 -04004171 if (btrfs_leaf_free_space(root, leaf) < 0) {
4172 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004173 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004174 }
Chris Masonb18c6682007-04-17 13:26:50 -04004175}
4176
Chris Masond352ac62008-09-29 15:18:18 -04004177/*
4178 * make the item pointed to by the path bigger, data_size is the new size.
4179 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004180void btrfs_extend_item(struct btrfs_trans_handle *trans,
4181 struct btrfs_root *root, struct btrfs_path *path,
4182 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004183{
Chris Mason6567e832007-04-16 09:22:45 -04004184 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004185 struct extent_buffer *leaf;
4186 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004187 u32 nritems;
4188 unsigned int data_end;
4189 unsigned int old_data;
4190 unsigned int old_size;
4191 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004192 struct btrfs_map_token token;
4193
4194 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004195
Chris Mason5f39d392007-10-15 16:14:19 -04004196 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004197
Chris Mason5f39d392007-10-15 16:14:19 -04004198 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004199 data_end = leaf_data_end(root, leaf);
4200
Chris Mason5f39d392007-10-15 16:14:19 -04004201 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4202 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004203 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004204 }
Chris Mason6567e832007-04-16 09:22:45 -04004205 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004206 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004207
4208 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004209 if (slot >= nritems) {
4210 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004211 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4212 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004213 BUG_ON(1);
4214 }
Chris Mason6567e832007-04-16 09:22:45 -04004215
4216 /*
4217 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4218 */
4219 /* first correct the data pointers */
4220 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004221 u32 ioff;
4222 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004223
Chris Masoncfed81a2012-03-03 07:40:03 -05004224 ioff = btrfs_token_item_offset(leaf, item, &token);
4225 btrfs_set_token_item_offset(leaf, item,
4226 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004227 }
Chris Mason5f39d392007-10-15 16:14:19 -04004228
Chris Mason6567e832007-04-16 09:22:45 -04004229 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004230 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004231 data_end - data_size, btrfs_leaf_data(leaf) +
4232 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004233
Chris Mason6567e832007-04-16 09:22:45 -04004234 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004235 old_size = btrfs_item_size_nr(leaf, slot);
4236 item = btrfs_item_nr(leaf, slot);
4237 btrfs_set_item_size(leaf, item, old_size + data_size);
4238 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004239
Chris Mason5f39d392007-10-15 16:14:19 -04004240 if (btrfs_leaf_free_space(root, leaf) < 0) {
4241 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004242 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004243 }
Chris Mason6567e832007-04-16 09:22:45 -04004244}
4245
Chris Mason74123bd2007-02-02 11:05:29 -05004246/*
Chris Masond352ac62008-09-29 15:18:18 -04004247 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004248 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004249 * Returns the number of keys that were inserted.
4250 */
4251int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4252 struct btrfs_root *root,
4253 struct btrfs_path *path,
4254 struct btrfs_key *cpu_key, u32 *data_size,
4255 int nr)
4256{
4257 struct extent_buffer *leaf;
4258 struct btrfs_item *item;
4259 int ret = 0;
4260 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004261 int i;
4262 u32 nritems;
4263 u32 total_data = 0;
4264 u32 total_size = 0;
4265 unsigned int data_end;
4266 struct btrfs_disk_key disk_key;
4267 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004268 struct btrfs_map_token token;
4269
4270 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004271
Yan Zheng87b29b22008-12-17 10:21:48 -05004272 for (i = 0; i < nr; i++) {
4273 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4274 BTRFS_LEAF_DATA_SIZE(root)) {
4275 break;
4276 nr = i;
4277 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004278 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004279 total_size += data_size[i] + sizeof(struct btrfs_item);
4280 }
4281 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004282
Josef Bacikf3465ca2008-11-12 14:19:50 -05004283 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4284 if (ret == 0)
4285 return -EEXIST;
4286 if (ret < 0)
4287 goto out;
4288
Josef Bacikf3465ca2008-11-12 14:19:50 -05004289 leaf = path->nodes[0];
4290
4291 nritems = btrfs_header_nritems(leaf);
4292 data_end = leaf_data_end(root, leaf);
4293
4294 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4295 for (i = nr; i >= 0; i--) {
4296 total_data -= data_size[i];
4297 total_size -= data_size[i] + sizeof(struct btrfs_item);
4298 if (total_size < btrfs_leaf_free_space(root, leaf))
4299 break;
4300 }
4301 nr = i;
4302 }
4303
4304 slot = path->slots[0];
4305 BUG_ON(slot < 0);
4306
4307 if (slot != nritems) {
4308 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4309
4310 item = btrfs_item_nr(leaf, slot);
4311 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4312
4313 /* figure out how many keys we can insert in here */
4314 total_data = data_size[0];
4315 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004316 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004317 break;
4318 total_data += data_size[i];
4319 }
4320 nr = i;
4321
4322 if (old_data < data_end) {
4323 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004324 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004325 slot, old_data, data_end);
4326 BUG_ON(1);
4327 }
4328 /*
4329 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4330 */
4331 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004332 for (i = slot; i < nritems; i++) {
4333 u32 ioff;
4334
4335 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004336 ioff = btrfs_token_item_offset(leaf, item, &token);
4337 btrfs_set_token_item_offset(leaf, item,
4338 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004339 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004340 /* shift the items */
4341 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4342 btrfs_item_nr_offset(slot),
4343 (nritems - slot) * sizeof(struct btrfs_item));
4344
4345 /* shift the data */
4346 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4347 data_end - total_data, btrfs_leaf_data(leaf) +
4348 data_end, old_data - data_end);
4349 data_end = old_data;
4350 } else {
4351 /*
4352 * this sucks but it has to be done, if we are inserting at
4353 * the end of the leaf only insert 1 of the items, since we
4354 * have no way of knowing whats on the next leaf and we'd have
4355 * to drop our current locks to figure it out
4356 */
4357 nr = 1;
4358 }
4359
4360 /* setup the item for the new data */
4361 for (i = 0; i < nr; i++) {
4362 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4363 btrfs_set_item_key(leaf, &disk_key, slot + i);
4364 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004365 btrfs_set_token_item_offset(leaf, item,
4366 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004367 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004368 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004369 }
4370 btrfs_set_header_nritems(leaf, nritems + nr);
4371 btrfs_mark_buffer_dirty(leaf);
4372
4373 ret = 0;
4374 if (slot == 0) {
4375 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004376 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004377 }
4378
4379 if (btrfs_leaf_free_space(root, leaf) < 0) {
4380 btrfs_print_leaf(root, leaf);
4381 BUG();
4382 }
4383out:
4384 if (!ret)
4385 ret = nr;
4386 return ret;
4387}
4388
4389/*
Chris Mason44871b12009-03-13 10:04:31 -04004390 * this is a helper for btrfs_insert_empty_items, the main goal here is
4391 * to save stack depth by doing the bulk of the work in a function
4392 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004393 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004394void setup_items_for_insert(struct btrfs_trans_handle *trans,
4395 struct btrfs_root *root, struct btrfs_path *path,
4396 struct btrfs_key *cpu_key, u32 *data_size,
4397 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004398{
Chris Mason5f39d392007-10-15 16:14:19 -04004399 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004400 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004401 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004402 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004403 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004404 struct extent_buffer *leaf;
4405 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004406 struct btrfs_map_token token;
4407
4408 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004409
Chris Mason5f39d392007-10-15 16:14:19 -04004410 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004411 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004412
Chris Mason5f39d392007-10-15 16:14:19 -04004413 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004414 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004415
Chris Masonf25956c2008-09-12 15:32:53 -04004416 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004417 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004418 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004419 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004420 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004421 }
Chris Mason5f39d392007-10-15 16:14:19 -04004422
Chris Masonbe0e5c02007-01-26 15:51:26 -05004423 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004424 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004425
Chris Mason5f39d392007-10-15 16:14:19 -04004426 if (old_data < data_end) {
4427 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004428 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004429 slot, old_data, data_end);
4430 BUG_ON(1);
4431 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004432 /*
4433 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4434 */
4435 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004436 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004437 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004438
Chris Mason5f39d392007-10-15 16:14:19 -04004439 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004440 ioff = btrfs_token_item_offset(leaf, item, &token);
4441 btrfs_set_token_item_offset(leaf, item,
4442 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004443 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004444 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004445 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004446 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004447 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004448
4449 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004450 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004451 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004452 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004453 data_end = old_data;
4454 }
Chris Mason5f39d392007-10-15 16:14:19 -04004455
Chris Mason62e27492007-03-15 12:56:47 -04004456 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004457 for (i = 0; i < nr; i++) {
4458 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4459 btrfs_set_item_key(leaf, &disk_key, slot + i);
4460 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004461 btrfs_set_token_item_offset(leaf, item,
4462 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004463 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004464 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004465 }
Chris Mason44871b12009-03-13 10:04:31 -04004466
Chris Mason9c583092008-01-29 15:15:18 -05004467 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004468
Chris Mason5a01a2e2008-01-30 11:43:54 -05004469 if (slot == 0) {
4470 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004471 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004472 }
Chris Masonb9473432009-03-13 11:00:37 -04004473 btrfs_unlock_up_safe(path, 1);
4474 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004475
Chris Mason5f39d392007-10-15 16:14:19 -04004476 if (btrfs_leaf_free_space(root, leaf) < 0) {
4477 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004478 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004479 }
Chris Mason44871b12009-03-13 10:04:31 -04004480}
4481
4482/*
4483 * Given a key and some data, insert items into the tree.
4484 * This does all the path init required, making room in the tree if needed.
4485 */
4486int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4487 struct btrfs_root *root,
4488 struct btrfs_path *path,
4489 struct btrfs_key *cpu_key, u32 *data_size,
4490 int nr)
4491{
Chris Mason44871b12009-03-13 10:04:31 -04004492 int ret = 0;
4493 int slot;
4494 int i;
4495 u32 total_size = 0;
4496 u32 total_data = 0;
4497
4498 for (i = 0; i < nr; i++)
4499 total_data += data_size[i];
4500
4501 total_size = total_data + (nr * sizeof(struct btrfs_item));
4502 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4503 if (ret == 0)
4504 return -EEXIST;
4505 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004506 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004507
Chris Mason44871b12009-03-13 10:04:31 -04004508 slot = path->slots[0];
4509 BUG_ON(slot < 0);
4510
Jeff Mahoney143bede2012-03-01 14:56:26 +01004511 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004512 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004513 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004514}
4515
4516/*
4517 * Given a key and some data, insert an item into the tree.
4518 * This does all the path init required, making room in the tree if needed.
4519 */
Chris Masone089f052007-03-16 16:20:31 -04004520int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4521 *root, struct btrfs_key *cpu_key, void *data, u32
4522 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004523{
4524 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004525 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004526 struct extent_buffer *leaf;
4527 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004528
Chris Mason2c90e5d2007-04-02 10:50:19 -04004529 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004530 if (!path)
4531 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004532 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004533 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004534 leaf = path->nodes[0];
4535 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4536 write_extent_buffer(leaf, data, ptr, data_size);
4537 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004538 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004539 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004540 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004541}
4542
Chris Mason74123bd2007-02-02 11:05:29 -05004543/*
Chris Mason5de08d72007-02-24 06:24:44 -05004544 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004545 *
Chris Masond352ac62008-09-29 15:18:18 -04004546 * the tree should have been previously balanced so the deletion does not
4547 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004548 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004549static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004550 struct btrfs_path *path, int level, int slot,
4551 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004552{
Chris Mason5f39d392007-10-15 16:14:19 -04004553 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004554 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004555 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004556
Chris Mason5f39d392007-10-15 16:14:19 -04004557 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004558 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004559 if (tree_mod_log && level)
4560 tree_mod_log_eb_move(root->fs_info, parent, slot,
4561 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004562 memmove_extent_buffer(parent,
4563 btrfs_node_key_ptr_offset(slot),
4564 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004565 sizeof(struct btrfs_key_ptr) *
4566 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004567 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004568 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4569 MOD_LOG_KEY_REMOVE);
4570 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004571 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004572
Chris Mason7518a232007-03-12 12:01:18 -04004573 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004574 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004575 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004576 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004577 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004578 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004579 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004580 struct btrfs_disk_key disk_key;
4581
4582 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004583 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004584 }
Chris Masond6025572007-03-30 14:27:56 -04004585 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004586}
4587
Chris Mason74123bd2007-02-02 11:05:29 -05004588/*
Chris Mason323ac952008-10-01 19:05:46 -04004589 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004590 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004591 *
4592 * This deletes the pointer in path->nodes[1] and frees the leaf
4593 * block extent. zero is returned if it all worked out, < 0 otherwise.
4594 *
4595 * The path must have already been setup for deleting the leaf, including
4596 * all the proper balancing. path->nodes[1] must be locked.
4597 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004598static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4599 struct btrfs_root *root,
4600 struct btrfs_path *path,
4601 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004602{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004603 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004604 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004605
Chris Mason4d081c42009-02-04 09:31:28 -05004606 /*
4607 * btrfs_free_extent is expensive, we want to make sure we
4608 * aren't holding any locks when we call it
4609 */
4610 btrfs_unlock_up_safe(path, 0);
4611
Yan, Zhengf0486c62010-05-16 10:46:25 -04004612 root_sub_used(root, leaf->len);
4613
Josef Bacik3083ee22012-03-09 16:01:49 -05004614 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004615 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004616 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004617}
4618/*
Chris Mason74123bd2007-02-02 11:05:29 -05004619 * delete the item at the leaf level in path. If that empties
4620 * the leaf, remove it from the tree
4621 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004622int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4623 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004624{
Chris Mason5f39d392007-10-15 16:14:19 -04004625 struct extent_buffer *leaf;
4626 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004627 int last_off;
4628 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004629 int ret = 0;
4630 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004631 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004632 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004633 struct btrfs_map_token token;
4634
4635 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004636
Chris Mason5f39d392007-10-15 16:14:19 -04004637 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004638 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4639
4640 for (i = 0; i < nr; i++)
4641 dsize += btrfs_item_size_nr(leaf, slot + i);
4642
Chris Mason5f39d392007-10-15 16:14:19 -04004643 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004644
Chris Mason85e21ba2008-01-29 15:11:36 -05004645 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004646 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004647
4648 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004649 data_end + dsize,
4650 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004651 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004652
Chris Mason85e21ba2008-01-29 15:11:36 -05004653 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004654 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004655
Chris Mason5f39d392007-10-15 16:14:19 -04004656 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004657 ioff = btrfs_token_item_offset(leaf, item, &token);
4658 btrfs_set_token_item_offset(leaf, item,
4659 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004660 }
Chris Masondb945352007-10-15 16:15:53 -04004661
Chris Mason5f39d392007-10-15 16:14:19 -04004662 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004663 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004664 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004665 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004666 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004667 btrfs_set_header_nritems(leaf, nritems - nr);
4668 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004669
Chris Mason74123bd2007-02-02 11:05:29 -05004670 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004671 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004672 if (leaf == root->node) {
4673 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004674 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004675 btrfs_set_path_blocking(path);
4676 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004677 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004678 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004679 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004680 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004681 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004682 struct btrfs_disk_key disk_key;
4683
4684 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004685 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004686 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004687
Chris Mason74123bd2007-02-02 11:05:29 -05004688 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004689 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004690 /* push_leaf_left fixes the path.
4691 * make sure the path still points to our leaf
4692 * for possible call to del_ptr below
4693 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004694 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004695 extent_buffer_get(leaf);
4696
Chris Masonb9473432009-03-13 11:00:37 -04004697 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004698 wret = push_leaf_left(trans, root, path, 1, 1,
4699 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004700 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004701 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004702
4703 if (path->nodes[0] == leaf &&
4704 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004705 wret = push_leaf_right(trans, root, path, 1,
4706 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004707 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004708 ret = wret;
4709 }
Chris Mason5f39d392007-10-15 16:14:19 -04004710
4711 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004712 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004713 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004714 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004715 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004716 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004717 /* if we're still in the path, make sure
4718 * we're dirty. Otherwise, one of the
4719 * push_leaf functions must have already
4720 * dirtied this buffer
4721 */
4722 if (path->nodes[0] == leaf)
4723 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004724 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004725 }
Chris Masond5719762007-03-23 10:01:08 -04004726 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004727 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004728 }
4729 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004730 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004731}
4732
Chris Mason97571fd2007-02-24 13:39:08 -05004733/*
Chris Mason925baed2008-06-25 16:01:30 -04004734 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004735 * returns 0 if it found something or 1 if there are no lesser leaves.
4736 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004737 *
4738 * This may release the path, and so you may lose any locks held at the
4739 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004740 */
4741int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4742{
Chris Mason925baed2008-06-25 16:01:30 -04004743 struct btrfs_key key;
4744 struct btrfs_disk_key found_key;
4745 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004746
Chris Mason925baed2008-06-25 16:01:30 -04004747 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004748
Chris Mason925baed2008-06-25 16:01:30 -04004749 if (key.offset > 0)
4750 key.offset--;
4751 else if (key.type > 0)
4752 key.type--;
4753 else if (key.objectid > 0)
4754 key.objectid--;
4755 else
4756 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004757
David Sterbab3b4aa72011-04-21 01:20:15 +02004758 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004759 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4760 if (ret < 0)
4761 return ret;
4762 btrfs_item_key(path->nodes[0], &found_key, 0);
4763 ret = comp_keys(&found_key, &key);
4764 if (ret < 0)
4765 return 0;
4766 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004767}
4768
Chris Mason3f157a22008-06-25 16:01:31 -04004769/*
4770 * A helper function to walk down the tree starting at min_key, and looking
4771 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004772 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004773 *
4774 * This does not cow, but it does stuff the starting key it finds back
4775 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4776 * key and get a writable path.
4777 *
4778 * This does lock as it descends, and path->keep_locks should be set
4779 * to 1 by the caller.
4780 *
4781 * This honors path->lowest_level to prevent descent past a given level
4782 * of the tree.
4783 *
Chris Masond352ac62008-09-29 15:18:18 -04004784 * min_trans indicates the oldest transaction that you are interested
4785 * in walking through. Any nodes or leaves older than min_trans are
4786 * skipped over (without reading them).
4787 *
Chris Mason3f157a22008-06-25 16:01:31 -04004788 * returns zero if something useful was found, < 0 on error and 1 if there
4789 * was nothing in the tree that matched the search criteria.
4790 */
4791int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004792 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004793 struct btrfs_path *path, int cache_only,
4794 u64 min_trans)
4795{
4796 struct extent_buffer *cur;
4797 struct btrfs_key found_key;
4798 int slot;
Yan96524802008-07-24 12:19:49 -04004799 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004800 u32 nritems;
4801 int level;
4802 int ret = 1;
4803
Chris Mason934d3752008-12-08 16:43:10 -05004804 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004805again:
Chris Masonbd681512011-07-16 15:23:14 -04004806 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004807 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004808 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004809 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004810 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004811
4812 if (btrfs_header_generation(cur) < min_trans) {
4813 ret = 1;
4814 goto out;
4815 }
Chris Masond3977122009-01-05 21:25:51 -05004816 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004817 nritems = btrfs_header_nritems(cur);
4818 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004819 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004820
Chris Mason323ac952008-10-01 19:05:46 -04004821 /* at the lowest level, we're done, setup the path and exit */
4822 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004823 if (slot >= nritems)
4824 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004825 ret = 0;
4826 path->slots[level] = slot;
4827 btrfs_item_key_to_cpu(cur, &found_key, slot);
4828 goto out;
4829 }
Yan96524802008-07-24 12:19:49 -04004830 if (sret && slot > 0)
4831 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004832 /*
4833 * check this node pointer against the cache_only and
4834 * min_trans parameters. If it isn't in cache or is too
4835 * old, skip to the next one.
4836 */
Chris Masond3977122009-01-05 21:25:51 -05004837 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004838 u64 blockptr;
4839 u64 gen;
4840 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004841 struct btrfs_disk_key disk_key;
4842
Chris Mason3f157a22008-06-25 16:01:31 -04004843 blockptr = btrfs_node_blockptr(cur, slot);
4844 gen = btrfs_node_ptr_generation(cur, slot);
4845 if (gen < min_trans) {
4846 slot++;
4847 continue;
4848 }
4849 if (!cache_only)
4850 break;
4851
Chris Masone02119d2008-09-05 16:13:11 -04004852 if (max_key) {
4853 btrfs_node_key(cur, &disk_key, slot);
4854 if (comp_keys(&disk_key, max_key) >= 0) {
4855 ret = 1;
4856 goto out;
4857 }
4858 }
4859
Chris Mason3f157a22008-06-25 16:01:31 -04004860 tmp = btrfs_find_tree_block(root, blockptr,
4861 btrfs_level_size(root, level - 1));
4862
Chris Masonb9fab912012-05-06 07:23:47 -04004863 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004864 free_extent_buffer(tmp);
4865 break;
4866 }
4867 if (tmp)
4868 free_extent_buffer(tmp);
4869 slot++;
4870 }
Chris Masone02119d2008-09-05 16:13:11 -04004871find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004872 /*
4873 * we didn't find a candidate key in this node, walk forward
4874 * and find another one
4875 */
4876 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004877 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004878 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004879 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004880 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004881 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004882 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004883 goto again;
4884 } else {
4885 goto out;
4886 }
4887 }
4888 /* save our key for returning back */
4889 btrfs_node_key_to_cpu(cur, &found_key, slot);
4890 path->slots[level] = slot;
4891 if (level == path->lowest_level) {
4892 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004893 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004894 goto out;
4895 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004896 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004897 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004898 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004899
Chris Masonbd681512011-07-16 15:23:14 -04004900 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004901
Chris Masonbd681512011-07-16 15:23:14 -04004902 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004903 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004904 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004905 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004906 }
4907out:
4908 if (ret == 0)
4909 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004910 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004911 return ret;
4912}
4913
4914/*
4915 * this is similar to btrfs_next_leaf, but does not try to preserve
4916 * and fixup the path. It looks for and returns the next key in the
4917 * tree based on the current path and the cache_only and min_trans
4918 * parameters.
4919 *
4920 * 0 is returned if another key is found, < 0 if there are any errors
4921 * and 1 is returned if there are no higher keys in the tree
4922 *
4923 * path->keep_locks should be set to 1 on the search made before
4924 * calling this function.
4925 */
Chris Masone7a84562008-06-25 16:01:31 -04004926int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004927 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004928 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004929{
Chris Masone7a84562008-06-25 16:01:31 -04004930 int slot;
4931 struct extent_buffer *c;
4932
Chris Mason934d3752008-12-08 16:43:10 -05004933 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004934 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004935 if (!path->nodes[level])
4936 return 1;
4937
4938 slot = path->slots[level] + 1;
4939 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004940next:
Chris Masone7a84562008-06-25 16:01:31 -04004941 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004942 int ret;
4943 int orig_lowest;
4944 struct btrfs_key cur_key;
4945 if (level + 1 >= BTRFS_MAX_LEVEL ||
4946 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004947 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004948
4949 if (path->locks[level + 1]) {
4950 level++;
4951 continue;
4952 }
4953
4954 slot = btrfs_header_nritems(c) - 1;
4955 if (level == 0)
4956 btrfs_item_key_to_cpu(c, &cur_key, slot);
4957 else
4958 btrfs_node_key_to_cpu(c, &cur_key, slot);
4959
4960 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004961 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004962 path->lowest_level = level;
4963 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4964 0, 0);
4965 path->lowest_level = orig_lowest;
4966 if (ret < 0)
4967 return ret;
4968
4969 c = path->nodes[level];
4970 slot = path->slots[level];
4971 if (ret == 0)
4972 slot++;
4973 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004974 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004975
Chris Masone7a84562008-06-25 16:01:31 -04004976 if (level == 0)
4977 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004978 else {
4979 u64 blockptr = btrfs_node_blockptr(c, slot);
4980 u64 gen = btrfs_node_ptr_generation(c, slot);
4981
4982 if (cache_only) {
4983 struct extent_buffer *cur;
4984 cur = btrfs_find_tree_block(root, blockptr,
4985 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004986 if (!cur ||
4987 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004988 slot++;
4989 if (cur)
4990 free_extent_buffer(cur);
4991 goto next;
4992 }
4993 free_extent_buffer(cur);
4994 }
4995 if (gen < min_trans) {
4996 slot++;
4997 goto next;
4998 }
Chris Masone7a84562008-06-25 16:01:31 -04004999 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005000 }
Chris Masone7a84562008-06-25 16:01:31 -04005001 return 0;
5002 }
5003 return 1;
5004}
5005
Chris Mason7bb86312007-12-11 09:25:06 -05005006/*
Chris Mason925baed2008-06-25 16:01:30 -04005007 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005008 * returns 0 if it found something or 1 if there are no greater leaves.
5009 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005010 */
Chris Mason234b63a2007-03-13 10:46:10 -04005011int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005012{
5013 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005014 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005015 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005016 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005017 struct btrfs_key key;
5018 u32 nritems;
5019 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005020 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005021 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005022
5023 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005024 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005025 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005026
Chris Mason8e73f272009-04-03 10:14:18 -04005027 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5028again:
5029 level = 1;
5030 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005031 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005032 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005033
Chris Masona2135012008-06-25 16:01:30 -04005034 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005035 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005036
Chris Mason925baed2008-06-25 16:01:30 -04005037 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5038 path->keep_locks = 0;
5039
5040 if (ret < 0)
5041 return ret;
5042
Chris Masona2135012008-06-25 16:01:30 -04005043 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005044 /*
5045 * by releasing the path above we dropped all our locks. A balance
5046 * could have added more items next to the key that used to be
5047 * at the very end of the block. So, check again here and
5048 * advance the path if there are now more items available.
5049 */
Chris Masona2135012008-06-25 16:01:30 -04005050 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005051 if (ret == 0)
5052 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005053 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005054 goto done;
5055 }
Chris Masond97e63b2007-02-20 16:40:44 -05005056
Chris Masond3977122009-01-05 21:25:51 -05005057 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005058 if (!path->nodes[level]) {
5059 ret = 1;
5060 goto done;
5061 }
Chris Mason5f39d392007-10-15 16:14:19 -04005062
Chris Masond97e63b2007-02-20 16:40:44 -05005063 slot = path->slots[level] + 1;
5064 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005065 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005066 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005067 if (level == BTRFS_MAX_LEVEL) {
5068 ret = 1;
5069 goto done;
5070 }
Chris Masond97e63b2007-02-20 16:40:44 -05005071 continue;
5072 }
Chris Mason5f39d392007-10-15 16:14:19 -04005073
Chris Mason925baed2008-06-25 16:01:30 -04005074 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005075 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005076 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005077 }
Chris Mason5f39d392007-10-15 16:14:19 -04005078
Chris Mason8e73f272009-04-03 10:14:18 -04005079 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005080 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005081 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005082 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005083 if (ret == -EAGAIN)
5084 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005085
Chris Mason76a05b32009-05-14 13:24:30 -04005086 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005087 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005088 goto done;
5089 }
5090
Chris Mason5cd57b22008-06-25 16:01:30 -04005091 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005092 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005093 if (!ret) {
5094 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005095 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005096 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005097 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005098 }
Chris Mason31533fb2011-07-26 16:01:59 -04005099 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005100 }
Chris Masond97e63b2007-02-20 16:40:44 -05005101 break;
5102 }
5103 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005104 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005105 level--;
5106 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005107 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005108 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005109
Chris Mason5f39d392007-10-15 16:14:19 -04005110 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005111 path->nodes[level] = next;
5112 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005113 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005114 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005115 if (!level)
5116 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005117
Chris Mason8e73f272009-04-03 10:14:18 -04005118 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005119 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005120 if (ret == -EAGAIN)
5121 goto again;
5122
Chris Mason76a05b32009-05-14 13:24:30 -04005123 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005124 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005125 goto done;
5126 }
5127
Chris Mason5cd57b22008-06-25 16:01:30 -04005128 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005129 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005130 if (!ret) {
5131 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005132 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005133 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005134 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005135 }
Chris Mason31533fb2011-07-26 16:01:59 -04005136 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005137 }
Chris Masond97e63b2007-02-20 16:40:44 -05005138 }
Chris Mason8e73f272009-04-03 10:14:18 -04005139 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005140done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005141 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005142 path->leave_spinning = old_spinning;
5143 if (!old_spinning)
5144 btrfs_set_path_blocking(path);
5145
5146 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005147}
Chris Mason0b86a832008-03-24 15:01:56 -04005148
Chris Mason3f157a22008-06-25 16:01:31 -04005149/*
5150 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5151 * searching until it gets past min_objectid or finds an item of 'type'
5152 *
5153 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5154 */
Chris Mason0b86a832008-03-24 15:01:56 -04005155int btrfs_previous_item(struct btrfs_root *root,
5156 struct btrfs_path *path, u64 min_objectid,
5157 int type)
5158{
5159 struct btrfs_key found_key;
5160 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005161 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005162 int ret;
5163
Chris Masond3977122009-01-05 21:25:51 -05005164 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005165 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005166 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005167 ret = btrfs_prev_leaf(root, path);
5168 if (ret != 0)
5169 return ret;
5170 } else {
5171 path->slots[0]--;
5172 }
5173 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005174 nritems = btrfs_header_nritems(leaf);
5175 if (nritems == 0)
5176 return 1;
5177 if (path->slots[0] == nritems)
5178 path->slots[0]--;
5179
Chris Mason0b86a832008-03-24 15:01:56 -04005180 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005181 if (found_key.objectid < min_objectid)
5182 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005183 if (found_key.type == type)
5184 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005185 if (found_key.objectid == min_objectid &&
5186 found_key.type < type)
5187 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005188 }
5189 return 1;
5190}