blob: 72b9f97e2fdcff590d3ec96b86752a9874ebb620 [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,
Chris Masone089f052007-03-16 16:20:31 -040041 struct btrfs_path *path, int level, int slot);
Chris Masond97e63b2007-02-20 16:40:44 -050042
Chris Mason2c90e5d2007-04-02 10:50:19 -040043struct btrfs_path *btrfs_alloc_path(void)
44{
Chris Masondf24a2b2007-04-04 09:36:31 -040045 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050046 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040047 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040048}
49
Chris Masonb4ce94d2009-02-04 09:25:08 -050050/*
51 * set all locked nodes in the path to blocking locks. This should
52 * be done before scheduling
53 */
54noinline void btrfs_set_path_blocking(struct btrfs_path *p)
55{
56 int i;
57 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040058 if (!p->nodes[i] || !p->locks[i])
59 continue;
60 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
61 if (p->locks[i] == BTRFS_READ_LOCK)
62 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
63 else if (p->locks[i] == BTRFS_WRITE_LOCK)
64 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050065 }
66}
67
68/*
69 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050070 *
71 * held is used to keep lockdep happy, when lockdep is enabled
72 * we set held to a blocking lock before we go around and
73 * retake all the spinlocks in the path. You can safely use NULL
74 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050075 */
Chris Mason4008c042009-02-12 14:09:45 -050076noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040077 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050078{
79 int i;
Chris Mason4008c042009-02-12 14:09:45 -050080
81#ifdef CONFIG_DEBUG_LOCK_ALLOC
82 /* lockdep really cares that we take all of these spinlocks
83 * in the right order. If any of the locks in the path are not
84 * currently blocking, it is going to complain. So, make really
85 * really sure by forcing the path to blocking before we clear
86 * the path blocking.
87 */
Chris Masonbd681512011-07-16 15:23:14 -040088 if (held) {
89 btrfs_set_lock_blocking_rw(held, held_rw);
90 if (held_rw == BTRFS_WRITE_LOCK)
91 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
92 else if (held_rw == BTRFS_READ_LOCK)
93 held_rw = BTRFS_READ_LOCK_BLOCKING;
94 }
Chris Mason4008c042009-02-12 14:09:45 -050095 btrfs_set_path_blocking(p);
96#endif
97
98 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -040099 if (p->nodes[i] && p->locks[i]) {
100 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
101 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
102 p->locks[i] = BTRFS_WRITE_LOCK;
103 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
104 p->locks[i] = BTRFS_READ_LOCK;
105 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500106 }
Chris Mason4008c042009-02-12 14:09:45 -0500107
108#ifdef CONFIG_DEBUG_LOCK_ALLOC
109 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400110 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500111#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500112}
113
Chris Masond352ac62008-09-29 15:18:18 -0400114/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400115void btrfs_free_path(struct btrfs_path *p)
116{
Jesper Juhlff175d52010-12-25 21:22:30 +0000117 if (!p)
118 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200119 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400120 kmem_cache_free(btrfs_path_cachep, p);
121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/*
124 * path release drops references on the extent buffers in the path
125 * and it drops any locks held by this path
126 *
127 * It is safe to call this on paths that no locks or extent buffers held.
128 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200129noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500130{
131 int i;
Chris Masona2135012008-06-25 16:01:30 -0400132
Chris Mason234b63a2007-03-13 10:46:10 -0400133 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400134 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500135 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400136 continue;
137 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400138 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400139 p->locks[i] = 0;
140 }
Chris Mason5f39d392007-10-15 16:14:19 -0400141 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400142 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500143 }
144}
145
Chris Masond352ac62008-09-29 15:18:18 -0400146/*
147 * safely gets a reference on the root node of a tree. A lock
148 * is not taken, so a concurrent writer may put a different node
149 * at the root of the tree. See btrfs_lock_root_node for the
150 * looping required.
151 *
152 * The extent buffer returned by this has a reference taken, so
153 * it won't disappear. It may stop being the root of the tree
154 * at any time because there are no locks held.
155 */
Chris Mason925baed2008-06-25 16:01:30 -0400156struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
157{
158 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400159
Josef Bacik3083ee22012-03-09 16:01:49 -0500160 while (1) {
161 rcu_read_lock();
162 eb = rcu_dereference(root->node);
163
164 /*
165 * RCU really hurts here, we could free up the root node because
166 * it was cow'ed but we may not get the new root node yet so do
167 * the inc_not_zero dance and if it doesn't work then
168 * synchronize_rcu and try again.
169 */
170 if (atomic_inc_not_zero(&eb->refs)) {
171 rcu_read_unlock();
172 break;
173 }
174 rcu_read_unlock();
175 synchronize_rcu();
176 }
Chris Mason925baed2008-06-25 16:01:30 -0400177 return eb;
178}
179
Chris Masond352ac62008-09-29 15:18:18 -0400180/* loop around taking references on and locking the root node of the
181 * tree until you end up with a lock on the root. A locked buffer
182 * is returned, with a reference held.
183 */
Chris Mason925baed2008-06-25 16:01:30 -0400184struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
185{
186 struct extent_buffer *eb;
187
Chris Masond3977122009-01-05 21:25:51 -0500188 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400189 eb = btrfs_root_node(root);
190 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400191 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400192 break;
Chris Mason925baed2008-06-25 16:01:30 -0400193 btrfs_tree_unlock(eb);
194 free_extent_buffer(eb);
195 }
196 return eb;
197}
198
Chris Masonbd681512011-07-16 15:23:14 -0400199/* loop around taking references on and locking the root node of the
200 * tree until you end up with a lock on the root. A locked buffer
201 * is returned, with a reference held.
202 */
203struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
204{
205 struct extent_buffer *eb;
206
207 while (1) {
208 eb = btrfs_root_node(root);
209 btrfs_tree_read_lock(eb);
210 if (eb == root->node)
211 break;
212 btrfs_tree_read_unlock(eb);
213 free_extent_buffer(eb);
214 }
215 return eb;
216}
217
Chris Masond352ac62008-09-29 15:18:18 -0400218/* cowonly root (everything not a reference counted cow subvolume), just get
219 * put onto a simple dirty list. transaction.c walks this to make sure they
220 * get properly updated on disk.
221 */
Chris Mason0b86a832008-03-24 15:01:56 -0400222static void add_root_to_dirty_list(struct btrfs_root *root)
223{
Chris Masone5846fc2012-05-03 12:08:48 -0400224 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400225 if (root->track_dirty && list_empty(&root->dirty_list)) {
226 list_add(&root->dirty_list,
227 &root->fs_info->dirty_cowonly_roots);
228 }
Chris Masone5846fc2012-05-03 12:08:48 -0400229 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400230}
231
Chris Masond352ac62008-09-29 15:18:18 -0400232/*
233 * used by snapshot creation to make a copy of a root for a tree with
234 * a given objectid. The buffer with the new root node is returned in
235 * cow_ret, and this func returns zero on success or a negative error code.
236 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500237int btrfs_copy_root(struct btrfs_trans_handle *trans,
238 struct btrfs_root *root,
239 struct extent_buffer *buf,
240 struct extent_buffer **cow_ret, u64 new_root_objectid)
241{
242 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 int ret = 0;
244 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400245 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500246
247 WARN_ON(root->ref_cows && trans->transid !=
248 root->fs_info->running_transaction->transid);
249 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
250
251 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400252 if (level == 0)
253 btrfs_item_key(buf, &disk_key, 0);
254 else
255 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400256
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400257 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
258 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200259 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500261 return PTR_ERR(cow);
262
263 copy_extent_buffer(cow, buf, 0, 0, cow->len);
264 btrfs_set_header_bytenr(cow, cow->start);
265 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
267 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
268 BTRFS_HEADER_FLAG_RELOC);
269 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
270 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
271 else
272 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500273
Yan Zheng2b820322008-11-17 21:11:30 -0500274 write_extent_buffer(cow, root->fs_info->fsid,
275 (unsigned long)btrfs_header_fsid(cow),
276 BTRFS_FSID_SIZE);
277
Chris Masonbe20aa92007-12-17 20:14:01 -0500278 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400279 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200280 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400281 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200282 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500283
Chris Masonbe20aa92007-12-17 20:14:01 -0500284 if (ret)
285 return ret;
286
287 btrfs_mark_buffer_dirty(cow);
288 *cow_ret = cow;
289 return 0;
290}
291
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200292enum mod_log_op {
293 MOD_LOG_KEY_REPLACE,
294 MOD_LOG_KEY_ADD,
295 MOD_LOG_KEY_REMOVE,
296 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
297 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
298 MOD_LOG_MOVE_KEYS,
299 MOD_LOG_ROOT_REPLACE,
300};
301
302struct tree_mod_move {
303 int dst_slot;
304 int nr_items;
305};
306
307struct tree_mod_root {
308 u64 logical;
309 u8 level;
310};
311
312struct tree_mod_elem {
313 struct rb_node node;
314 u64 index; /* shifted logical */
315 struct seq_list elem;
316 enum mod_log_op op;
317
318 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
319 int slot;
320
321 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
322 u64 generation;
323
324 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
325 struct btrfs_disk_key key;
326 u64 blockptr;
327
328 /* this is used for op == MOD_LOG_MOVE_KEYS */
329 struct tree_mod_move move;
330
331 /* this is used for op == MOD_LOG_ROOT_REPLACE */
332 struct tree_mod_root old_root;
333};
334
335static inline void
336__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
337{
338 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
339 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
340}
341
342void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
343 struct seq_list *elem)
344{
345 elem->flags = 1;
346 spin_lock(&fs_info->tree_mod_seq_lock);
347 __get_tree_mod_seq(fs_info, elem);
348 spin_unlock(&fs_info->tree_mod_seq_lock);
349}
350
351void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 struct rb_root *tm_root;
355 struct rb_node *node;
356 struct rb_node *next;
357 struct seq_list *cur_elem;
358 struct tree_mod_elem *tm;
359 u64 min_seq = (u64)-1;
360 u64 seq_putting = elem->seq;
361
362 if (!seq_putting)
363 return;
364
365 BUG_ON(!(elem->flags & 1));
366 spin_lock(&fs_info->tree_mod_seq_lock);
367 list_del(&elem->list);
368
369 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
370 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
371 if (seq_putting > cur_elem->seq) {
372 /*
373 * blocker with lower sequence number exists, we
374 * cannot remove anything from the log
375 */
376 goto out;
377 }
378 min_seq = cur_elem->seq;
379 }
380 }
381
382 /*
383 * anything that's lower than the lowest existing (read: blocked)
384 * sequence number can be removed from the tree.
385 */
386 write_lock(&fs_info->tree_mod_log_lock);
387 tm_root = &fs_info->tree_mod_log;
388 for (node = rb_first(tm_root); node; node = next) {
389 next = rb_next(node);
390 tm = container_of(node, struct tree_mod_elem, node);
391 if (tm->elem.seq > min_seq)
392 continue;
393 rb_erase(node, tm_root);
394 list_del(&tm->elem.list);
395 kfree(tm);
396 }
397 write_unlock(&fs_info->tree_mod_log_lock);
398out:
399 spin_unlock(&fs_info->tree_mod_seq_lock);
400}
401
402/*
403 * key order of the log:
404 * index -> sequence
405 *
406 * the index is the shifted logical of the *new* root node for root replace
407 * operations, or the shifted logical of the affected block for all other
408 * operations.
409 */
410static noinline int
411__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
412{
413 struct rb_root *tm_root;
414 struct rb_node **new;
415 struct rb_node *parent = NULL;
416 struct tree_mod_elem *cur;
417 int ret = 0;
418
419 BUG_ON(!tm || !tm->elem.seq);
420
421 write_lock(&fs_info->tree_mod_log_lock);
422 tm_root = &fs_info->tree_mod_log;
423 new = &tm_root->rb_node;
424 while (*new) {
425 cur = container_of(*new, struct tree_mod_elem, node);
426 parent = *new;
427 if (cur->index < tm->index)
428 new = &((*new)->rb_left);
429 else if (cur->index > tm->index)
430 new = &((*new)->rb_right);
431 else if (cur->elem.seq < tm->elem.seq)
432 new = &((*new)->rb_left);
433 else if (cur->elem.seq > tm->elem.seq)
434 new = &((*new)->rb_right);
435 else {
436 kfree(tm);
437 ret = -EEXIST;
438 goto unlock;
439 }
440 }
441
442 rb_link_node(&tm->node, parent, new);
443 rb_insert_color(&tm->node, tm_root);
444unlock:
445 write_unlock(&fs_info->tree_mod_log_lock);
446 return ret;
447}
448
449int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
450 struct tree_mod_elem **tm_ret)
451{
452 struct tree_mod_elem *tm;
453 u64 seq = 0;
454
455 smp_mb();
456 if (list_empty(&fs_info->tree_mod_seq_list))
457 return 0;
458
459 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
460 if (!tm)
461 return -ENOMEM;
462
463 __get_tree_mod_seq(fs_info, &tm->elem);
464 seq = tm->elem.seq;
465 tm->elem.flags = 0;
466
467 return seq;
468}
469
470static noinline int
471tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
472 struct extent_buffer *eb, int slot,
473 enum mod_log_op op, gfp_t flags)
474{
475 struct tree_mod_elem *tm;
476 int ret;
477
478 ret = tree_mod_alloc(fs_info, flags, &tm);
479 if (ret <= 0)
480 return ret;
481
482 tm->index = eb->start >> PAGE_CACHE_SHIFT;
483 if (op != MOD_LOG_KEY_ADD) {
484 btrfs_node_key(eb, &tm->key, slot);
485 tm->blockptr = btrfs_node_blockptr(eb, slot);
486 }
487 tm->op = op;
488 tm->slot = slot;
489 tm->generation = btrfs_node_ptr_generation(eb, slot);
490
491 return __tree_mod_log_insert(fs_info, tm);
492}
493
494static noinline int
495tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
496 int slot, enum mod_log_op op)
497{
498 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
499}
500
501static noinline int
502tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
503 struct extent_buffer *eb, int dst_slot, int src_slot,
504 int nr_items, gfp_t flags)
505{
506 struct tree_mod_elem *tm;
507 int ret;
508 int i;
509
510 ret = tree_mod_alloc(fs_info, flags, &tm);
511 if (ret <= 0)
512 return ret;
513
514 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
515 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
516 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
517 BUG_ON(ret < 0);
518 }
519
520 tm->index = eb->start >> PAGE_CACHE_SHIFT;
521 tm->slot = src_slot;
522 tm->move.dst_slot = dst_slot;
523 tm->move.nr_items = nr_items;
524 tm->op = MOD_LOG_MOVE_KEYS;
525
526 return __tree_mod_log_insert(fs_info, tm);
527}
528
529static noinline int
530tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
531 struct extent_buffer *old_root,
532 struct extent_buffer *new_root, gfp_t flags)
533{
534 struct tree_mod_elem *tm;
535 int ret;
536
537 ret = tree_mod_alloc(fs_info, flags, &tm);
538 if (ret <= 0)
539 return ret;
540
541 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
542 tm->old_root.logical = old_root->start;
543 tm->old_root.level = btrfs_header_level(old_root);
544 tm->generation = btrfs_header_generation(old_root);
545 tm->op = MOD_LOG_ROOT_REPLACE;
546
547 return __tree_mod_log_insert(fs_info, tm);
548}
549
550static struct tree_mod_elem *
551__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
552 int smallest)
553{
554 struct rb_root *tm_root;
555 struct rb_node *node;
556 struct tree_mod_elem *cur = NULL;
557 struct tree_mod_elem *found = NULL;
558 u64 index = start >> PAGE_CACHE_SHIFT;
559
560 read_lock(&fs_info->tree_mod_log_lock);
561 tm_root = &fs_info->tree_mod_log;
562 node = tm_root->rb_node;
563 while (node) {
564 cur = container_of(node, struct tree_mod_elem, node);
565 if (cur->index < index) {
566 node = node->rb_left;
567 } else if (cur->index > index) {
568 node = node->rb_right;
569 } else if (cur->elem.seq < min_seq) {
570 node = node->rb_left;
571 } else if (!smallest) {
572 /* we want the node with the highest seq */
573 if (found)
574 BUG_ON(found->elem.seq > cur->elem.seq);
575 found = cur;
576 node = node->rb_left;
577 } else if (cur->elem.seq > min_seq) {
578 /* we want the node with the smallest seq */
579 if (found)
580 BUG_ON(found->elem.seq < cur->elem.seq);
581 found = cur;
582 node = node->rb_right;
583 } else {
584 found = cur;
585 break;
586 }
587 }
588 read_unlock(&fs_info->tree_mod_log_lock);
589
590 return found;
591}
592
593/*
594 * this returns the element from the log with the smallest time sequence
595 * value that's in the log (the oldest log item). any element with a time
596 * sequence lower than min_seq will be ignored.
597 */
598static struct tree_mod_elem *
599tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
600 u64 min_seq)
601{
602 return __tree_mod_log_search(fs_info, start, min_seq, 1);
603}
604
605/*
606 * this returns the element from the log with the largest time sequence
607 * value that's in the log (the most recent log item). any element with
608 * a time sequence lower than min_seq will be ignored.
609 */
610static struct tree_mod_elem *
611tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
612{
613 return __tree_mod_log_search(fs_info, start, min_seq, 0);
614}
615
616static inline void
617tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
618 struct extent_buffer *src, unsigned long dst_offset,
619 unsigned long src_offset, int nr_items)
620{
621 int ret;
622 int i;
623
624 smp_mb();
625 if (list_empty(&fs_info->tree_mod_seq_list))
626 return;
627
628 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
629 return;
630
631 /* speed this up by single seq for all operations? */
632 for (i = 0; i < nr_items; i++) {
633 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
634 MOD_LOG_KEY_REMOVE);
635 BUG_ON(ret < 0);
636 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
637 MOD_LOG_KEY_ADD);
638 BUG_ON(ret < 0);
639 }
640}
641
642static inline void
643tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
644 int dst_offset, int src_offset, int nr_items)
645{
646 int ret;
647 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
648 nr_items, GFP_NOFS);
649 BUG_ON(ret < 0);
650}
651
652static inline void
653tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
654 struct extent_buffer *eb,
655 struct btrfs_disk_key *disk_key, int slot, int atomic)
656{
657 int ret;
658
659 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
660 MOD_LOG_KEY_REPLACE,
661 atomic ? GFP_ATOMIC : GFP_NOFS);
662 BUG_ON(ret < 0);
663}
664
665static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
666 struct extent_buffer *eb)
667{
668 int i;
669 int ret;
670 u32 nritems;
671
672 smp_mb();
673 if (list_empty(&fs_info->tree_mod_seq_list))
674 return;
675
676 if (btrfs_header_level(eb) == 0)
677 return;
678
679 nritems = btrfs_header_nritems(eb);
680 for (i = nritems - 1; i >= 0; i--) {
681 ret = tree_mod_log_insert_key(fs_info, eb, i,
682 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
683 BUG_ON(ret < 0);
684 }
685}
686
687static inline void
688tree_mod_log_set_root_pointer(struct btrfs_root *root,
689 struct extent_buffer *new_root_node)
690{
691 int ret;
692 tree_mod_log_free_eb(root->fs_info, root->node);
693 ret = tree_mod_log_insert_root(root->fs_info, root->node,
694 new_root_node, GFP_NOFS);
695 BUG_ON(ret < 0);
696}
697
Chris Masond352ac62008-09-29 15:18:18 -0400698/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400699 * check if the tree block can be shared by multiple trees
700 */
701int btrfs_block_can_be_shared(struct btrfs_root *root,
702 struct extent_buffer *buf)
703{
704 /*
705 * Tree blocks not in refernece counted trees and tree roots
706 * are never shared. If a block was allocated after the last
707 * snapshot and the block was not allocated by tree relocation,
708 * we know the block is not shared.
709 */
710 if (root->ref_cows &&
711 buf != root->node && buf != root->commit_root &&
712 (btrfs_header_generation(buf) <=
713 btrfs_root_last_snapshot(&root->root_item) ||
714 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
715 return 1;
716#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
717 if (root->ref_cows &&
718 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
719 return 1;
720#endif
721 return 0;
722}
723
724static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
725 struct btrfs_root *root,
726 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400727 struct extent_buffer *cow,
728 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400729{
730 u64 refs;
731 u64 owner;
732 u64 flags;
733 u64 new_flags = 0;
734 int ret;
735
736 /*
737 * Backrefs update rules:
738 *
739 * Always use full backrefs for extent pointers in tree block
740 * allocated by tree relocation.
741 *
742 * If a shared tree block is no longer referenced by its owner
743 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
744 * use full backrefs for extent pointers in tree block.
745 *
746 * If a tree block is been relocating
747 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
748 * use full backrefs for extent pointers in tree block.
749 * The reason for this is some operations (such as drop tree)
750 * are only allowed for blocks use full backrefs.
751 */
752
753 if (btrfs_block_can_be_shared(root, buf)) {
754 ret = btrfs_lookup_extent_info(trans, root, buf->start,
755 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700756 if (ret)
757 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700758 if (refs == 0) {
759 ret = -EROFS;
760 btrfs_std_error(root->fs_info, ret);
761 return ret;
762 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400763 } else {
764 refs = 1;
765 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
766 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
767 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
768 else
769 flags = 0;
770 }
771
772 owner = btrfs_header_owner(buf);
773 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
774 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
775
776 if (refs > 1) {
777 if ((owner == root->root_key.objectid ||
778 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
779 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200780 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100781 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400782
783 if (root->root_key.objectid ==
784 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200785 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100786 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200787 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100788 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400789 }
790 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
791 } else {
792
793 if (root->root_key.objectid ==
794 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200795 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400796 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200797 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400799 }
800 if (new_flags != 0) {
801 ret = btrfs_set_disk_extent_flags(trans, root,
802 buf->start,
803 buf->len,
804 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700805 if (ret)
806 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400807 }
808 } else {
809 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
810 if (root->root_key.objectid ==
811 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200812 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200814 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100815 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200816 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100817 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400818 }
819 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400820 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821 }
822 return 0;
823}
824
825/*
Chris Masond3977122009-01-05 21:25:51 -0500826 * does the dirty work in cow of a single block. The parent block (if
827 * supplied) is updated to point to the new cow copy. The new buffer is marked
828 * dirty and returned locked. If you modify the block it needs to be marked
829 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400830 *
831 * search_start -- an allocation hint for the new block
832 *
Chris Masond3977122009-01-05 21:25:51 -0500833 * empty_size -- a hint that you plan on doing more cow. This is the size in
834 * bytes the allocator should try to find free next to the block it returns.
835 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400836 */
Chris Masond3977122009-01-05 21:25:51 -0500837static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400838 struct btrfs_root *root,
839 struct extent_buffer *buf,
840 struct extent_buffer *parent, int parent_slot,
841 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400842 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400843{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400844 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400845 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700846 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400847 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400848 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400850
Chris Mason925baed2008-06-25 16:01:30 -0400851 if (*cow_ret == buf)
852 unlock_orig = 1;
853
Chris Masonb9447ef2009-03-09 11:45:38 -0400854 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400855
Chris Mason7bb86312007-12-11 09:25:06 -0500856 WARN_ON(root->ref_cows && trans->transid !=
857 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400858 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400859
Chris Mason7bb86312007-12-11 09:25:06 -0500860 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400861
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400862 if (level == 0)
863 btrfs_item_key(buf, &disk_key, 0);
864 else
865 btrfs_node_key(buf, &disk_key, 0);
866
867 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
868 if (parent)
869 parent_start = parent->start;
870 else
871 parent_start = 0;
872 } else
873 parent_start = 0;
874
875 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
876 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200877 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400878 if (IS_ERR(cow))
879 return PTR_ERR(cow);
880
Chris Masonb4ce94d2009-02-04 09:25:08 -0500881 /* cow is set to blocking by btrfs_init_new_buffer */
882
Chris Mason5f39d392007-10-15 16:14:19 -0400883 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400884 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400885 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
887 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
888 BTRFS_HEADER_FLAG_RELOC);
889 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
890 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
891 else
892 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400893
Yan Zheng2b820322008-11-17 21:11:30 -0500894 write_extent_buffer(cow, root->fs_info->fsid,
895 (unsigned long)btrfs_header_fsid(cow),
896 BTRFS_FSID_SIZE);
897
Mark Fashehbe1a5562011-08-08 13:20:18 -0700898 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700899 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100900 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700901 return ret;
902 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400903
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400904 if (root->ref_cows)
905 btrfs_reloc_cow_block(trans, root, buf, cow);
906
Chris Mason6702ed42007-08-07 16:15:09 -0400907 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400908 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400909 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
910 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
911 parent_start = buf->start;
912 else
913 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400914
Chris Mason5f39d392007-10-15 16:14:19 -0400915 extent_buffer_get(cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400916 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400917
Yan, Zhengf0486c62010-05-16 10:46:25 -0400918 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200919 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400920 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400921 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400922 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
924 parent_start = parent->start;
925 else
926 parent_start = 0;
927
928 WARN_ON(trans->transid != btrfs_header_generation(parent));
Chris Mason5f39d392007-10-15 16:14:19 -0400929 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400930 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500931 btrfs_set_node_ptr_generation(parent, parent_slot,
932 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400933 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400934 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200935 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400936 }
Chris Mason925baed2008-06-25 16:01:30 -0400937 if (unlock_orig)
938 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500939 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400940 btrfs_mark_buffer_dirty(cow);
941 *cow_ret = cow;
942 return 0;
943}
944
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945static inline int should_cow_block(struct btrfs_trans_handle *trans,
946 struct btrfs_root *root,
947 struct extent_buffer *buf)
948{
Liu Bof1ebcc72011-11-14 20:48:06 -0500949 /* ensure we can see the force_cow */
950 smp_rmb();
951
952 /*
953 * We do not need to cow a block if
954 * 1) this block is not created or changed in this transaction;
955 * 2) this block does not belong to TREE_RELOC tree;
956 * 3) the root is not forced COW.
957 *
958 * What is forced COW:
959 * when we create snapshot during commiting the transaction,
960 * after we've finished coping src root, we must COW the shared
961 * block to ensure the metadata consistency.
962 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 if (btrfs_header_generation(buf) == trans->transid &&
964 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
965 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500966 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
967 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400968 return 0;
969 return 1;
970}
971
Chris Masond352ac62008-09-29 15:18:18 -0400972/*
973 * cows a single block, see __btrfs_cow_block for the real work.
974 * This version of it has extra checks so that a block isn't cow'd more than
975 * once per transaction, as long as it hasn't been written yet
976 */
Chris Masond3977122009-01-05 21:25:51 -0500977noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400978 struct btrfs_root *root, struct extent_buffer *buf,
979 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400980 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500981{
Chris Mason6702ed42007-08-07 16:15:09 -0400982 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -0400983 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -0500984
Chris Masonccd467d2007-06-28 15:57:36 -0400985 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -0500986 printk(KERN_CRIT "trans %llu running %llu\n",
987 (unsigned long long)trans->transid,
988 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -0400989 root->fs_info->running_transaction->transid);
990 WARN_ON(1);
991 }
992 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -0500993 printk(KERN_CRIT "trans %llu running %llu\n",
994 (unsigned long long)trans->transid,
995 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -0400996 WARN_ON(1);
997 }
Chris Masondc17ff82008-01-08 15:46:30 -0500998
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400999 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001000 *cow_ret = buf;
1001 return 0;
1002 }
Chris Masonc4876852009-02-04 09:24:25 -05001003
Chris Mason0b86a832008-03-24 15:01:56 -04001004 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001005
1006 if (parent)
1007 btrfs_set_lock_blocking(parent);
1008 btrfs_set_lock_blocking(buf);
1009
Chris Masonf510cfe2007-10-15 16:14:48 -04001010 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001011 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001012
1013 trace_btrfs_cow_block(root, buf, *cow_ret);
1014
Chris Masonf510cfe2007-10-15 16:14:48 -04001015 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001016}
1017
Chris Masond352ac62008-09-29 15:18:18 -04001018/*
1019 * helper function for defrag to decide if two blocks pointed to by a
1020 * node are actually close by
1021 */
Chris Mason6b800532007-10-15 16:17:34 -04001022static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001023{
Chris Mason6b800532007-10-15 16:17:34 -04001024 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001025 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001026 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001027 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001028 return 0;
1029}
1030
Chris Mason081e9572007-11-06 10:26:24 -05001031/*
1032 * compare two keys in a memcmp fashion
1033 */
1034static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1035{
1036 struct btrfs_key k1;
1037
1038 btrfs_disk_key_to_cpu(&k1, disk);
1039
Diego Calleja20736ab2009-07-24 11:06:52 -04001040 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001041}
1042
Josef Bacikf3465ca2008-11-12 14:19:50 -05001043/*
1044 * same as comp_keys only with two btrfs_key's
1045 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001047{
1048 if (k1->objectid > k2->objectid)
1049 return 1;
1050 if (k1->objectid < k2->objectid)
1051 return -1;
1052 if (k1->type > k2->type)
1053 return 1;
1054 if (k1->type < k2->type)
1055 return -1;
1056 if (k1->offset > k2->offset)
1057 return 1;
1058 if (k1->offset < k2->offset)
1059 return -1;
1060 return 0;
1061}
Chris Mason081e9572007-11-06 10:26:24 -05001062
Chris Masond352ac62008-09-29 15:18:18 -04001063/*
1064 * this is used by the defrag code to go through all the
1065 * leaves pointed to by a node and reallocate them so that
1066 * disk order is close to key order
1067 */
Chris Mason6702ed42007-08-07 16:15:09 -04001068int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001069 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001070 int start_slot, int cache_only, u64 *last_ret,
1071 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001072{
Chris Mason6b800532007-10-15 16:17:34 -04001073 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001074 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001075 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001076 u64 search_start = *last_ret;
1077 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001078 u64 other;
1079 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001080 int end_slot;
1081 int i;
1082 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001083 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001084 int uptodate;
1085 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001086 int progress_passed = 0;
1087 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001088
Chris Mason5708b952007-10-25 15:43:18 -04001089 parent_level = btrfs_header_level(parent);
1090 if (cache_only && parent_level != 1)
1091 return 0;
1092
Chris Masond3977122009-01-05 21:25:51 -05001093 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001094 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001095 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001096 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001097
Chris Mason6b800532007-10-15 16:17:34 -04001098 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001099 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001100 end_slot = parent_nritems;
1101
1102 if (parent_nritems == 1)
1103 return 0;
1104
Chris Masonb4ce94d2009-02-04 09:25:08 -05001105 btrfs_set_lock_blocking(parent);
1106
Chris Mason6702ed42007-08-07 16:15:09 -04001107 for (i = start_slot; i < end_slot; i++) {
1108 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001109
Chris Mason081e9572007-11-06 10:26:24 -05001110 btrfs_node_key(parent, &disk_key, i);
1111 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1112 continue;
1113
1114 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001115 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001116 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001117 if (last_block == 0)
1118 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001119
Chris Mason6702ed42007-08-07 16:15:09 -04001120 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001121 other = btrfs_node_blockptr(parent, i - 1);
1122 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001123 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001124 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001125 other = btrfs_node_blockptr(parent, i + 1);
1126 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001127 }
Chris Masone9d0b132007-08-10 14:06:19 -04001128 if (close) {
1129 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001130 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001131 }
Chris Mason6702ed42007-08-07 16:15:09 -04001132
Chris Mason6b800532007-10-15 16:17:34 -04001133 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1134 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001135 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001136 else
1137 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001138 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001139 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001140 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001141 continue;
1142 }
Chris Mason6b800532007-10-15 16:17:34 -04001143 if (!cur) {
1144 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001145 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001146 if (!cur)
1147 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001148 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001149 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001150 }
Chris Mason6702ed42007-08-07 16:15:09 -04001151 }
Chris Masone9d0b132007-08-10 14:06:19 -04001152 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001153 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001154
Chris Masone7a84562008-06-25 16:01:31 -04001155 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001156 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001157 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001158 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001159 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001160 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001161 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001162 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001163 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001164 break;
Yan252c38f2007-08-29 09:11:44 -04001165 }
Chris Masone7a84562008-06-25 16:01:31 -04001166 search_start = cur->start;
1167 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001168 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001169 btrfs_tree_unlock(cur);
1170 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001171 }
1172 return err;
1173}
1174
Chris Mason74123bd2007-02-02 11:05:29 -05001175/*
1176 * The leaf data grows from end-to-front in the node.
1177 * this returns the address of the start of the last item,
1178 * which is the stop of the leaf data stack
1179 */
Chris Mason123abc82007-03-14 14:14:43 -04001180static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001181 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001182{
Chris Mason5f39d392007-10-15 16:14:19 -04001183 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001184 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001185 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001186 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001187}
1188
Chris Masonaa5d6be2007-02-28 16:35:06 -05001189
Chris Mason74123bd2007-02-02 11:05:29 -05001190/*
Chris Mason5f39d392007-10-15 16:14:19 -04001191 * search for key in the extent_buffer. The items start at offset p,
1192 * and they are item_size apart. There are 'max' items in p.
1193 *
Chris Mason74123bd2007-02-02 11:05:29 -05001194 * the slot in the array is returned via slot, and it points to
1195 * the place where you would insert key if it is not found in
1196 * the array.
1197 *
1198 * slot may point to max if the key is bigger than all of the keys
1199 */
Chris Masone02119d2008-09-05 16:13:11 -04001200static noinline int generic_bin_search(struct extent_buffer *eb,
1201 unsigned long p,
1202 int item_size, struct btrfs_key *key,
1203 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001204{
1205 int low = 0;
1206 int high = max;
1207 int mid;
1208 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001209 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001210 struct btrfs_disk_key unaligned;
1211 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001212 char *kaddr = NULL;
1213 unsigned long map_start = 0;
1214 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001215 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001216
Chris Masond3977122009-01-05 21:25:51 -05001217 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001218 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001219 offset = p + mid * item_size;
1220
Chris Masona6591712011-07-19 12:04:14 -04001221 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001222 (offset + sizeof(struct btrfs_disk_key)) >
1223 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001224
1225 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001226 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001227 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001228
Chris Mason479965d2007-10-15 16:14:27 -04001229 if (!err) {
1230 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1231 map_start);
1232 } else {
1233 read_extent_buffer(eb, &unaligned,
1234 offset, sizeof(unaligned));
1235 tmp = &unaligned;
1236 }
1237
Chris Mason5f39d392007-10-15 16:14:19 -04001238 } else {
1239 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1240 map_start);
1241 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001242 ret = comp_keys(tmp, key);
1243
1244 if (ret < 0)
1245 low = mid + 1;
1246 else if (ret > 0)
1247 high = mid;
1248 else {
1249 *slot = mid;
1250 return 0;
1251 }
1252 }
1253 *slot = low;
1254 return 1;
1255}
1256
Chris Mason97571fd2007-02-24 13:39:08 -05001257/*
1258 * simple bin_search frontend that does the right thing for
1259 * leaves vs nodes
1260 */
Chris Mason5f39d392007-10-15 16:14:19 -04001261static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1262 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001263{
Chris Mason5f39d392007-10-15 16:14:19 -04001264 if (level == 0) {
1265 return generic_bin_search(eb,
1266 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001267 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001268 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001269 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001270 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001271 return generic_bin_search(eb,
1272 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001273 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001274 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001275 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001276 }
1277 return -1;
1278}
1279
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001280int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1281 int level, int *slot)
1282{
1283 return bin_search(eb, key, level, slot);
1284}
1285
Yan, Zhengf0486c62010-05-16 10:46:25 -04001286static void root_add_used(struct btrfs_root *root, u32 size)
1287{
1288 spin_lock(&root->accounting_lock);
1289 btrfs_set_root_used(&root->root_item,
1290 btrfs_root_used(&root->root_item) + size);
1291 spin_unlock(&root->accounting_lock);
1292}
1293
1294static void root_sub_used(struct btrfs_root *root, u32 size)
1295{
1296 spin_lock(&root->accounting_lock);
1297 btrfs_set_root_used(&root->root_item,
1298 btrfs_root_used(&root->root_item) - size);
1299 spin_unlock(&root->accounting_lock);
1300}
1301
Chris Masond352ac62008-09-29 15:18:18 -04001302/* given a node and slot number, this reads the blocks it points to. The
1303 * extent buffer is returned with a reference taken (but unlocked).
1304 * NULL is returned on error.
1305 */
Chris Masone02119d2008-09-05 16:13:11 -04001306static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001307 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001308{
Chris Masonca7a79a2008-05-12 12:59:19 -04001309 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001310 if (slot < 0)
1311 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001312 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001313 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001314
1315 BUG_ON(level == 0);
1316
Chris Masondb945352007-10-15 16:15:53 -04001317 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001318 btrfs_level_size(root, level - 1),
1319 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001320}
1321
Chris Masond352ac62008-09-29 15:18:18 -04001322/*
1323 * node level balancing, used to make sure nodes are in proper order for
1324 * item deletion. We balance from the top down, so we have to make sure
1325 * that a deletion won't leave an node completely empty later on.
1326 */
Chris Masone02119d2008-09-05 16:13:11 -04001327static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001328 struct btrfs_root *root,
1329 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001330{
Chris Mason5f39d392007-10-15 16:14:19 -04001331 struct extent_buffer *right = NULL;
1332 struct extent_buffer *mid;
1333 struct extent_buffer *left = NULL;
1334 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001335 int ret = 0;
1336 int wret;
1337 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001338 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001339 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001340
1341 if (level == 0)
1342 return 0;
1343
Chris Mason5f39d392007-10-15 16:14:19 -04001344 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001345
Chris Masonbd681512011-07-16 15:23:14 -04001346 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1347 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001348 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1349
Chris Mason1d4f8a02007-03-13 09:28:32 -04001350 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001351
Li Zefana05a9bb2011-09-06 16:55:34 +08001352 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001353 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001354 pslot = path->slots[level + 1];
1355 }
Chris Masonbb803952007-03-01 12:04:21 -05001356
Chris Mason40689472007-03-17 14:29:23 -04001357 /*
1358 * deal with the case where there is only one pointer in the root
1359 * by promoting the node below to a root
1360 */
Chris Mason5f39d392007-10-15 16:14:19 -04001361 if (!parent) {
1362 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001363
Chris Mason5f39d392007-10-15 16:14:19 -04001364 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001365 return 0;
1366
1367 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001368 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001369 if (!child) {
1370 ret = -EROFS;
1371 btrfs_std_error(root->fs_info, ret);
1372 goto enospc;
1373 }
1374
Chris Mason925baed2008-06-25 16:01:30 -04001375 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001376 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001377 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001378 if (ret) {
1379 btrfs_tree_unlock(child);
1380 free_extent_buffer(child);
1381 goto enospc;
1382 }
Yan2f375ab2008-02-01 14:58:07 -05001383
Chris Mason240f62c2011-03-23 14:54:42 -04001384 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001385
Chris Mason0b86a832008-03-24 15:01:56 -04001386 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001387 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001388
Chris Mason925baed2008-06-25 16:01:30 -04001389 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001390 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001391 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001392 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001393 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001394 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001395
1396 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001397 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001398 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001399 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001400 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001401 }
Chris Mason5f39d392007-10-15 16:14:19 -04001402 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001403 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001404 return 0;
1405
Andi Kleen559af822010-10-29 15:14:37 -04001406 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001407
Chris Mason5f39d392007-10-15 16:14:19 -04001408 left = read_node_slot(root, parent, pslot - 1);
1409 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001410 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001411 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001412 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001413 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001414 if (wret) {
1415 ret = wret;
1416 goto enospc;
1417 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001418 }
Chris Mason5f39d392007-10-15 16:14:19 -04001419 right = read_node_slot(root, parent, pslot + 1);
1420 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001421 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001422 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001423 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001424 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001425 if (wret) {
1426 ret = wret;
1427 goto enospc;
1428 }
1429 }
1430
1431 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001432 if (left) {
1433 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001434 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001435 if (wret < 0)
1436 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001437 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001438 }
Chris Mason79f95c82007-03-01 15:16:26 -05001439
1440 /*
1441 * then try to empty the right most buffer into the middle
1442 */
Chris Mason5f39d392007-10-15 16:14:19 -04001443 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001444 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001445 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001446 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001447 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001448 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001449 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001450 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001451 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001452 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001453 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001454 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001455 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001456 struct btrfs_disk_key right_key;
1457 btrfs_node_key(right, &right_key, 0);
1458 btrfs_set_node_key(parent, &right_key, pslot + 1);
1459 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001460 }
1461 }
Chris Mason5f39d392007-10-15 16:14:19 -04001462 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001463 /*
1464 * we're not allowed to leave a node with one item in the
1465 * tree during a delete. A deletion from lower in the tree
1466 * could try to delete the only pointer in this node.
1467 * So, pull some keys from the left.
1468 * There has to be a left pointer at this point because
1469 * otherwise we would have pulled some pointers from the
1470 * right
1471 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001472 if (!left) {
1473 ret = -EROFS;
1474 btrfs_std_error(root->fs_info, ret);
1475 goto enospc;
1476 }
Chris Mason5f39d392007-10-15 16:14:19 -04001477 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001478 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001479 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001480 goto enospc;
1481 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001482 if (wret == 1) {
1483 wret = push_node_left(trans, root, left, mid, 1);
1484 if (wret < 0)
1485 ret = wret;
1486 }
Chris Mason79f95c82007-03-01 15:16:26 -05001487 BUG_ON(wret == 1);
1488 }
Chris Mason5f39d392007-10-15 16:14:19 -04001489 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001490 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001491 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001492 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001493 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001494 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001495 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001496 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001497 } else {
1498 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001499 struct btrfs_disk_key mid_key;
1500 btrfs_node_key(mid, &mid_key, 0);
1501 btrfs_set_node_key(parent, &mid_key, pslot);
1502 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001503 }
Chris Masonbb803952007-03-01 12:04:21 -05001504
Chris Mason79f95c82007-03-01 15:16:26 -05001505 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001506 if (left) {
1507 if (btrfs_header_nritems(left) > orig_slot) {
1508 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001509 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001510 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001511 path->slots[level + 1] -= 1;
1512 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001513 if (mid) {
1514 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001515 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001516 }
Chris Masonbb803952007-03-01 12:04:21 -05001517 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001518 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001519 path->slots[level] = orig_slot;
1520 }
1521 }
Chris Mason79f95c82007-03-01 15:16:26 -05001522 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001523 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001524 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001525 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001526enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001527 if (right) {
1528 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001529 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001530 }
1531 if (left) {
1532 if (path->nodes[level] != left)
1533 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001534 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001535 }
Chris Masonbb803952007-03-01 12:04:21 -05001536 return ret;
1537}
1538
Chris Masond352ac62008-09-29 15:18:18 -04001539/* Node balancing for insertion. Here we only split or push nodes around
1540 * when they are completely full. This is also done top down, so we
1541 * have to be pessimistic.
1542 */
Chris Masond3977122009-01-05 21:25:51 -05001543static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001544 struct btrfs_root *root,
1545 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001546{
Chris Mason5f39d392007-10-15 16:14:19 -04001547 struct extent_buffer *right = NULL;
1548 struct extent_buffer *mid;
1549 struct extent_buffer *left = NULL;
1550 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001551 int ret = 0;
1552 int wret;
1553 int pslot;
1554 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001555
1556 if (level == 0)
1557 return 1;
1558
Chris Mason5f39d392007-10-15 16:14:19 -04001559 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001560 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001561
Li Zefana05a9bb2011-09-06 16:55:34 +08001562 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001563 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001564 pslot = path->slots[level + 1];
1565 }
Chris Masone66f7092007-04-20 13:16:02 -04001566
Chris Mason5f39d392007-10-15 16:14:19 -04001567 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001568 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001569
Chris Mason5f39d392007-10-15 16:14:19 -04001570 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001571
1572 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001573 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001574 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001575
1576 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001577 btrfs_set_lock_blocking(left);
1578
Chris Mason5f39d392007-10-15 16:14:19 -04001579 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001580 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1581 wret = 1;
1582 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001583 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001584 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001585 if (ret)
1586 wret = 1;
1587 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001588 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001589 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001590 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001591 }
Chris Masone66f7092007-04-20 13:16:02 -04001592 if (wret < 0)
1593 ret = wret;
1594 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001595 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001596 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001597 btrfs_node_key(mid, &disk_key, 0);
1598 btrfs_set_node_key(parent, &disk_key, pslot);
1599 btrfs_mark_buffer_dirty(parent);
1600 if (btrfs_header_nritems(left) > orig_slot) {
1601 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001602 path->slots[level + 1] -= 1;
1603 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001604 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001605 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001606 } else {
1607 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001608 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001609 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001610 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001611 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001612 }
Chris Masone66f7092007-04-20 13:16:02 -04001613 return 0;
1614 }
Chris Mason925baed2008-06-25 16:01:30 -04001615 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001616 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001617 }
Chris Mason925baed2008-06-25 16:01:30 -04001618 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001619
1620 /*
1621 * then try to empty the right most buffer into the middle
1622 */
Chris Mason5f39d392007-10-15 16:14:19 -04001623 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001624 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001625
Chris Mason925baed2008-06-25 16:01:30 -04001626 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001627 btrfs_set_lock_blocking(right);
1628
Chris Mason5f39d392007-10-15 16:14:19 -04001629 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001630 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1631 wret = 1;
1632 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001633 ret = btrfs_cow_block(trans, root, right,
1634 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001635 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001636 if (ret)
1637 wret = 1;
1638 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001639 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001640 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001641 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001642 }
Chris Masone66f7092007-04-20 13:16:02 -04001643 if (wret < 0)
1644 ret = wret;
1645 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001646 struct btrfs_disk_key disk_key;
1647
1648 btrfs_node_key(right, &disk_key, 0);
1649 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1650 btrfs_mark_buffer_dirty(parent);
1651
1652 if (btrfs_header_nritems(mid) <= orig_slot) {
1653 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001654 path->slots[level + 1] += 1;
1655 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001656 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001657 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001658 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001659 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001660 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001661 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001662 }
Chris Masone66f7092007-04-20 13:16:02 -04001663 return 0;
1664 }
Chris Mason925baed2008-06-25 16:01:30 -04001665 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001666 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001667 }
Chris Masone66f7092007-04-20 13:16:02 -04001668 return 1;
1669}
1670
Chris Mason74123bd2007-02-02 11:05:29 -05001671/*
Chris Masond352ac62008-09-29 15:18:18 -04001672 * readahead one full node of leaves, finding things that are close
1673 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001674 */
Chris Masonc8c42862009-04-03 10:14:18 -04001675static void reada_for_search(struct btrfs_root *root,
1676 struct btrfs_path *path,
1677 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001678{
Chris Mason5f39d392007-10-15 16:14:19 -04001679 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001680 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001681 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001682 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001683 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001684 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001685 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001686 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001687 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001688 u32 nr;
1689 u32 blocksize;
1690 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001691
Chris Masona6b6e752007-10-15 16:22:39 -04001692 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001693 return;
1694
Chris Mason6702ed42007-08-07 16:15:09 -04001695 if (!path->nodes[level])
1696 return;
1697
Chris Mason5f39d392007-10-15 16:14:19 -04001698 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001699
Chris Mason3c69fae2007-08-07 15:52:22 -04001700 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001701 blocksize = btrfs_level_size(root, level - 1);
1702 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001703 if (eb) {
1704 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001705 return;
1706 }
1707
Chris Masona7175312009-01-22 09:23:10 -05001708 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001709
Chris Mason5f39d392007-10-15 16:14:19 -04001710 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001711 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001712
Chris Masond3977122009-01-05 21:25:51 -05001713 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001714 if (direction < 0) {
1715 if (nr == 0)
1716 break;
1717 nr--;
1718 } else if (direction > 0) {
1719 nr++;
1720 if (nr >= nritems)
1721 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001722 }
Chris Mason01f46652007-12-21 16:24:26 -05001723 if (path->reada < 0 && objectid) {
1724 btrfs_node_key(node, &disk_key, nr);
1725 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1726 break;
1727 }
Chris Mason6b800532007-10-15 16:17:34 -04001728 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001729 if ((search <= target && target - search <= 65536) ||
1730 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001731 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001732 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001733 nread += blocksize;
1734 }
1735 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001736 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001737 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001738 }
1739}
Chris Mason925baed2008-06-25 16:01:30 -04001740
Chris Masond352ac62008-09-29 15:18:18 -04001741/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001742 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1743 * cache
1744 */
1745static noinline int reada_for_balance(struct btrfs_root *root,
1746 struct btrfs_path *path, int level)
1747{
1748 int slot;
1749 int nritems;
1750 struct extent_buffer *parent;
1751 struct extent_buffer *eb;
1752 u64 gen;
1753 u64 block1 = 0;
1754 u64 block2 = 0;
1755 int ret = 0;
1756 int blocksize;
1757
Chris Mason8c594ea2009-04-20 15:50:10 -04001758 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001759 if (!parent)
1760 return 0;
1761
1762 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001763 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001764 blocksize = btrfs_level_size(root, level);
1765
1766 if (slot > 0) {
1767 block1 = btrfs_node_blockptr(parent, slot - 1);
1768 gen = btrfs_node_ptr_generation(parent, slot - 1);
1769 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001770 /*
1771 * if we get -eagain from btrfs_buffer_uptodate, we
1772 * don't want to return eagain here. That will loop
1773 * forever
1774 */
1775 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001776 block1 = 0;
1777 free_extent_buffer(eb);
1778 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001779 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001780 block2 = btrfs_node_blockptr(parent, slot + 1);
1781 gen = btrfs_node_ptr_generation(parent, slot + 1);
1782 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001783 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001784 block2 = 0;
1785 free_extent_buffer(eb);
1786 }
1787 if (block1 || block2) {
1788 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001789
1790 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001791 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001792
1793 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001794 if (block1)
1795 readahead_tree_block(root, block1, blocksize, 0);
1796 if (block2)
1797 readahead_tree_block(root, block2, blocksize, 0);
1798
1799 if (block1) {
1800 eb = read_tree_block(root, block1, blocksize, 0);
1801 free_extent_buffer(eb);
1802 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001803 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001804 eb = read_tree_block(root, block2, blocksize, 0);
1805 free_extent_buffer(eb);
1806 }
1807 }
1808 return ret;
1809}
1810
1811
1812/*
Chris Masond3977122009-01-05 21:25:51 -05001813 * when we walk down the tree, it is usually safe to unlock the higher layers
1814 * in the tree. The exceptions are when our path goes through slot 0, because
1815 * operations on the tree might require changing key pointers higher up in the
1816 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001817 *
Chris Masond3977122009-01-05 21:25:51 -05001818 * callers might also have set path->keep_locks, which tells this code to keep
1819 * the lock if the path points to the last slot in the block. This is part of
1820 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001821 *
Chris Masond3977122009-01-05 21:25:51 -05001822 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1823 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001824 */
Chris Masone02119d2008-09-05 16:13:11 -04001825static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001826 int lowest_unlock, int min_write_lock_level,
1827 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001828{
1829 int i;
1830 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001831 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001832 struct extent_buffer *t;
1833
1834 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1835 if (!path->nodes[i])
1836 break;
1837 if (!path->locks[i])
1838 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001839 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001840 skip_level = i + 1;
1841 continue;
1842 }
Chris Mason051e1b92008-06-25 16:01:30 -04001843 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001844 u32 nritems;
1845 t = path->nodes[i];
1846 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001847 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001848 skip_level = i + 1;
1849 continue;
1850 }
1851 }
Chris Mason051e1b92008-06-25 16:01:30 -04001852 if (skip_level < i && i >= lowest_unlock)
1853 no_skips = 1;
1854
Chris Mason925baed2008-06-25 16:01:30 -04001855 t = path->nodes[i];
1856 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001857 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001858 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001859 if (write_lock_level &&
1860 i > min_write_lock_level &&
1861 i <= *write_lock_level) {
1862 *write_lock_level = i - 1;
1863 }
Chris Mason925baed2008-06-25 16:01:30 -04001864 }
1865 }
1866}
1867
Chris Mason3c69fae2007-08-07 15:52:22 -04001868/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001869 * This releases any locks held in the path starting at level and
1870 * going all the way up to the root.
1871 *
1872 * btrfs_search_slot will keep the lock held on higher nodes in a few
1873 * corner cases, such as COW of the block at slot zero in the node. This
1874 * ignores those rules, and it should only be called when there are no
1875 * more updates to be done higher up in the tree.
1876 */
1877noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1878{
1879 int i;
1880
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001882 return;
1883
1884 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1885 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001886 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001887 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001888 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001889 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001890 path->locks[i] = 0;
1891 }
1892}
1893
1894/*
Chris Masonc8c42862009-04-03 10:14:18 -04001895 * helper function for btrfs_search_slot. The goal is to find a block
1896 * in cache without setting the path to blocking. If we find the block
1897 * we return zero and the path is unchanged.
1898 *
1899 * If we can't find the block, we set the path blocking and do some
1900 * reada. -EAGAIN is returned and the search must be repeated.
1901 */
1902static int
1903read_block_for_search(struct btrfs_trans_handle *trans,
1904 struct btrfs_root *root, struct btrfs_path *p,
1905 struct extent_buffer **eb_ret, int level, int slot,
1906 struct btrfs_key *key)
1907{
1908 u64 blocknr;
1909 u64 gen;
1910 u32 blocksize;
1911 struct extent_buffer *b = *eb_ret;
1912 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001913 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001914
1915 blocknr = btrfs_node_blockptr(b, slot);
1916 gen = btrfs_node_ptr_generation(b, slot);
1917 blocksize = btrfs_level_size(root, level - 1);
1918
1919 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001920 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04001921 /* first we do an atomic uptodate check */
1922 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
1923 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001924 /*
1925 * we found an up to date block without
1926 * sleeping, return
1927 * right away
1928 */
1929 *eb_ret = tmp;
1930 return 0;
1931 }
1932 /* the pages were up to date, but we failed
1933 * the generation number check. Do a full
1934 * read for the generation number that is correct.
1935 * We must do this without dropping locks so
1936 * we can trust our generation number
1937 */
1938 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001939 btrfs_set_path_blocking(p);
1940
Chris Masonb9fab912012-05-06 07:23:47 -04001941 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04001942 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04001943 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001944 *eb_ret = tmp;
1945 return 0;
1946 }
1947 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001948 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001949 return -EIO;
1950 }
Chris Masonc8c42862009-04-03 10:14:18 -04001951 }
1952
1953 /*
1954 * reduce lock contention at high levels
1955 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001956 * we read. Don't release the lock on the current
1957 * level because we need to walk this node to figure
1958 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001959 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001960 btrfs_unlock_up_safe(p, level + 1);
1961 btrfs_set_path_blocking(p);
1962
Chris Masoncb449212010-10-24 11:01:27 -04001963 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001964 if (p->reada)
1965 reada_for_search(root, p, level, slot, key->objectid);
1966
David Sterbab3b4aa72011-04-21 01:20:15 +02001967 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001968
1969 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001970 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001971 if (tmp) {
1972 /*
1973 * If the read above didn't mark this buffer up to date,
1974 * it will never end up being up to date. Set ret to EIO now
1975 * and give up so that our caller doesn't loop forever
1976 * on our EAGAINs.
1977 */
Chris Masonb9fab912012-05-06 07:23:47 -04001978 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04001979 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04001980 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04001981 }
1982 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001983}
1984
1985/*
1986 * helper function for btrfs_search_slot. This does all of the checks
1987 * for node-level blocks and does any balancing required based on
1988 * the ins_len.
1989 *
1990 * If no extra work was required, zero is returned. If we had to
1991 * drop the path, -EAGAIN is returned and btrfs_search_slot must
1992 * start over
1993 */
1994static int
1995setup_nodes_for_search(struct btrfs_trans_handle *trans,
1996 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04001997 struct extent_buffer *b, int level, int ins_len,
1998 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04001999{
2000 int ret;
2001 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2002 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2003 int sret;
2004
Chris Masonbd681512011-07-16 15:23:14 -04002005 if (*write_lock_level < level + 1) {
2006 *write_lock_level = level + 1;
2007 btrfs_release_path(p);
2008 goto again;
2009 }
2010
Chris Masonc8c42862009-04-03 10:14:18 -04002011 sret = reada_for_balance(root, p, level);
2012 if (sret)
2013 goto again;
2014
2015 btrfs_set_path_blocking(p);
2016 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002017 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002018
2019 BUG_ON(sret > 0);
2020 if (sret) {
2021 ret = sret;
2022 goto done;
2023 }
2024 b = p->nodes[level];
2025 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002026 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002027 int sret;
2028
Chris Masonbd681512011-07-16 15:23:14 -04002029 if (*write_lock_level < level + 1) {
2030 *write_lock_level = level + 1;
2031 btrfs_release_path(p);
2032 goto again;
2033 }
2034
Chris Masonc8c42862009-04-03 10:14:18 -04002035 sret = reada_for_balance(root, p, level);
2036 if (sret)
2037 goto again;
2038
2039 btrfs_set_path_blocking(p);
2040 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002041 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002042
2043 if (sret) {
2044 ret = sret;
2045 goto done;
2046 }
2047 b = p->nodes[level];
2048 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002049 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002050 goto again;
2051 }
2052 BUG_ON(btrfs_header_nritems(b) == 1);
2053 }
2054 return 0;
2055
2056again:
2057 ret = -EAGAIN;
2058done:
2059 return ret;
2060}
2061
2062/*
Chris Mason74123bd2007-02-02 11:05:29 -05002063 * look for key in the tree. path is filled in with nodes along the way
2064 * if key is found, we return zero and you can find the item in the leaf
2065 * level of the path (level 0)
2066 *
2067 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002068 * be inserted, and 1 is returned. If there are other errors during the
2069 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002070 *
2071 * if ins_len > 0, nodes and leaves will be split as we walk down the
2072 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2073 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002074 */
Chris Masone089f052007-03-16 16:20:31 -04002075int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2076 *root, struct btrfs_key *key, struct btrfs_path *p, int
2077 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002078{
Chris Mason5f39d392007-10-15 16:14:19 -04002079 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002080 int slot;
2081 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002082 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002083 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002084 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002085 int root_lock;
2086 /* everything at write_lock_level or lower must be write locked */
2087 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002088 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002089 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002090
Chris Mason6702ed42007-08-07 16:15:09 -04002091 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002092 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002093 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002094
Chris Masonbd681512011-07-16 15:23:14 -04002095 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002096 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002097
Chris Masonbd681512011-07-16 15:23:14 -04002098 /* when we are removing items, we might have to go up to level
2099 * two as we update tree pointers Make sure we keep write
2100 * for those levels as well
2101 */
2102 write_lock_level = 2;
2103 } else if (ins_len > 0) {
2104 /*
2105 * for inserting items, make sure we have a write lock on
2106 * level 1 so we can update keys
2107 */
2108 write_lock_level = 1;
2109 }
2110
2111 if (!cow)
2112 write_lock_level = -1;
2113
2114 if (cow && (p->keep_locks || p->lowest_level))
2115 write_lock_level = BTRFS_MAX_LEVEL;
2116
Chris Masonf7c79f32012-03-19 15:54:38 -04002117 min_write_lock_level = write_lock_level;
2118
Chris Masonbb803952007-03-01 12:04:21 -05002119again:
Chris Masonbd681512011-07-16 15:23:14 -04002120 /*
2121 * we try very hard to do read locks on the root
2122 */
2123 root_lock = BTRFS_READ_LOCK;
2124 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002125 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002126 /*
2127 * the commit roots are read only
2128 * so we always do read locks
2129 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002130 b = root->commit_root;
2131 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002132 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002133 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002134 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002135 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002136 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002137 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002138 level = btrfs_header_level(b);
2139 } else {
2140 /* we don't know the level of the root node
2141 * until we actually have it read locked
2142 */
2143 b = btrfs_read_lock_root_node(root);
2144 level = btrfs_header_level(b);
2145 if (level <= write_lock_level) {
2146 /* whoops, must trade for write lock */
2147 btrfs_tree_read_unlock(b);
2148 free_extent_buffer(b);
2149 b = btrfs_lock_root_node(root);
2150 root_lock = BTRFS_WRITE_LOCK;
2151
2152 /* the level might have changed, check again */
2153 level = btrfs_header_level(b);
2154 }
2155 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002156 }
Chris Masonbd681512011-07-16 15:23:14 -04002157 p->nodes[level] = b;
2158 if (!p->skip_locking)
2159 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002160
Chris Masoneb60cea2007-02-02 09:18:22 -05002161 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002162 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002163
2164 /*
2165 * setup the path here so we can release it under lock
2166 * contention with the cow code
2167 */
Chris Mason02217ed2007-03-02 16:08:05 -05002168 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002169 /*
2170 * if we don't really need to cow this block
2171 * then we don't want to set the path blocking,
2172 * so we test it here
2173 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002174 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002175 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002176
Chris Masonb4ce94d2009-02-04 09:25:08 -05002177 btrfs_set_path_blocking(p);
2178
Chris Masonbd681512011-07-16 15:23:14 -04002179 /*
2180 * must have write locks on this node and the
2181 * parent
2182 */
2183 if (level + 1 > write_lock_level) {
2184 write_lock_level = level + 1;
2185 btrfs_release_path(p);
2186 goto again;
2187 }
2188
Yan Zheng33c66f42009-07-22 09:59:00 -04002189 err = btrfs_cow_block(trans, root, b,
2190 p->nodes[level + 1],
2191 p->slots[level + 1], &b);
2192 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002193 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002194 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002195 }
Chris Mason02217ed2007-03-02 16:08:05 -05002196 }
Chris Mason65b51a02008-08-01 15:11:20 -04002197cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002198 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002199
Chris Masoneb60cea2007-02-02 09:18:22 -05002200 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002201 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002202
2203 /*
2204 * we have a lock on b and as long as we aren't changing
2205 * the tree, there is no way to for the items in b to change.
2206 * It is safe to drop the lock on our parent before we
2207 * go through the expensive btree search on b.
2208 *
2209 * If cow is true, then we might be changing slot zero,
2210 * which may require changing the parent. So, we can't
2211 * drop the lock until after we know which slot we're
2212 * operating on.
2213 */
2214 if (!cow)
2215 btrfs_unlock_up_safe(p, level + 1);
2216
Chris Mason5f39d392007-10-15 16:14:19 -04002217 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002218
Chris Mason5f39d392007-10-15 16:14:19 -04002219 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002220 int dec = 0;
2221 if (ret && slot > 0) {
2222 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002223 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002224 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002225 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002226 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002227 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002228 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002229 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002230 if (err) {
2231 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002232 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002233 }
Chris Masonc8c42862009-04-03 10:14:18 -04002234 b = p->nodes[level];
2235 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002236
Chris Masonbd681512011-07-16 15:23:14 -04002237 /*
2238 * slot 0 is special, if we change the key
2239 * we have to update the parent pointer
2240 * which means we must have a write lock
2241 * on the parent
2242 */
2243 if (slot == 0 && cow &&
2244 write_lock_level < level + 1) {
2245 write_lock_level = level + 1;
2246 btrfs_release_path(p);
2247 goto again;
2248 }
2249
Chris Masonf7c79f32012-03-19 15:54:38 -04002250 unlock_up(p, level, lowest_unlock,
2251 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002252
Chris Mason925baed2008-06-25 16:01:30 -04002253 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002254 if (dec)
2255 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002256 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002257 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002258
Yan Zheng33c66f42009-07-22 09:59:00 -04002259 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04002260 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002261 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002262 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002263 if (err) {
2264 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002265 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002266 }
Chris Mason76a05b32009-05-14 13:24:30 -04002267
Chris Masonb4ce94d2009-02-04 09:25:08 -05002268 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002269 level = btrfs_header_level(b);
2270 if (level <= write_lock_level) {
2271 err = btrfs_try_tree_write_lock(b);
2272 if (!err) {
2273 btrfs_set_path_blocking(p);
2274 btrfs_tree_lock(b);
2275 btrfs_clear_path_blocking(p, b,
2276 BTRFS_WRITE_LOCK);
2277 }
2278 p->locks[level] = BTRFS_WRITE_LOCK;
2279 } else {
2280 err = btrfs_try_tree_read_lock(b);
2281 if (!err) {
2282 btrfs_set_path_blocking(p);
2283 btrfs_tree_read_lock(b);
2284 btrfs_clear_path_blocking(p, b,
2285 BTRFS_READ_LOCK);
2286 }
2287 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002288 }
Chris Masonbd681512011-07-16 15:23:14 -04002289 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002290 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002291 } else {
2292 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002293 if (ins_len > 0 &&
2294 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002295 if (write_lock_level < 1) {
2296 write_lock_level = 1;
2297 btrfs_release_path(p);
2298 goto again;
2299 }
2300
Chris Masonb4ce94d2009-02-04 09:25:08 -05002301 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002302 err = split_leaf(trans, root, key,
2303 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002304 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002305
Yan Zheng33c66f42009-07-22 09:59:00 -04002306 BUG_ON(err > 0);
2307 if (err) {
2308 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002309 goto done;
2310 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002311 }
Chris Mason459931e2008-12-10 09:10:46 -05002312 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002313 unlock_up(p, level, lowest_unlock,
2314 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002315 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002316 }
2317 }
Chris Mason65b51a02008-08-01 15:11:20 -04002318 ret = 1;
2319done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002320 /*
2321 * we don't really know what they plan on doing with the path
2322 * from here on, so for now just mark it as blocking
2323 */
Chris Masonb9473432009-03-13 11:00:37 -04002324 if (!p->leave_spinning)
2325 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002326 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002327 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002328 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002329}
2330
Chris Mason74123bd2007-02-02 11:05:29 -05002331/*
2332 * adjust the pointers going up the tree, starting at level
2333 * making sure the right key of each node is points to 'key'.
2334 * This is used after shifting pointers to the left, so it stops
2335 * fixing up pointers when a given leaf/node is not in slot 0 of the
2336 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002337 *
Chris Mason74123bd2007-02-02 11:05:29 -05002338 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002339static void fixup_low_keys(struct btrfs_trans_handle *trans,
2340 struct btrfs_root *root, struct btrfs_path *path,
2341 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002342{
2343 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002344 struct extent_buffer *t;
2345
Chris Mason234b63a2007-03-13 10:46:10 -04002346 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002347 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002348 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002349 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002350 t = path->nodes[i];
2351 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002352 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002353 if (tslot != 0)
2354 break;
2355 }
2356}
2357
Chris Mason74123bd2007-02-02 11:05:29 -05002358/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 * update item key.
2360 *
2361 * This function isn't completely safe. It's the caller's responsibility
2362 * that the new key won't break the order
2363 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002364void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2365 struct btrfs_root *root, struct btrfs_path *path,
2366 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002367{
2368 struct btrfs_disk_key disk_key;
2369 struct extent_buffer *eb;
2370 int slot;
2371
2372 eb = path->nodes[0];
2373 slot = path->slots[0];
2374 if (slot > 0) {
2375 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002376 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002377 }
2378 if (slot < btrfs_header_nritems(eb) - 1) {
2379 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002380 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002381 }
2382
2383 btrfs_cpu_key_to_disk(&disk_key, new_key);
2384 btrfs_set_item_key(eb, &disk_key, slot);
2385 btrfs_mark_buffer_dirty(eb);
2386 if (slot == 0)
2387 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002388}
2389
2390/*
Chris Mason74123bd2007-02-02 11:05:29 -05002391 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002392 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002393 *
2394 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2395 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002396 */
Chris Mason98ed5172008-01-03 10:01:48 -05002397static int push_node_left(struct btrfs_trans_handle *trans,
2398 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002399 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002400{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002401 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002402 int src_nritems;
2403 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002404 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002405
Chris Mason5f39d392007-10-15 16:14:19 -04002406 src_nritems = btrfs_header_nritems(src);
2407 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002408 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002409 WARN_ON(btrfs_header_generation(src) != trans->transid);
2410 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002411
Chris Masonbce4eae2008-04-24 14:42:46 -04002412 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002413 return 1;
2414
Chris Masond3977122009-01-05 21:25:51 -05002415 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002416 return 1;
2417
Chris Masonbce4eae2008-04-24 14:42:46 -04002418 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002419 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002420 if (push_items < src_nritems) {
2421 /* leave at least 8 pointers in the node if
2422 * we aren't going to empty it
2423 */
2424 if (src_nritems - push_items < 8) {
2425 if (push_items <= 8)
2426 return 1;
2427 push_items -= 8;
2428 }
2429 }
2430 } else
2431 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 copy_extent_buffer(dst, src,
2434 btrfs_node_key_ptr_offset(dst_nritems),
2435 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002436 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002437
Chris Masonbb803952007-03-01 12:04:21 -05002438 if (push_items < src_nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002439 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2440 btrfs_node_key_ptr_offset(push_items),
2441 (src_nritems - push_items) *
2442 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002443 }
Chris Mason5f39d392007-10-15 16:14:19 -04002444 btrfs_set_header_nritems(src, src_nritems - push_items);
2445 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2446 btrfs_mark_buffer_dirty(src);
2447 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002448
Chris Masonbb803952007-03-01 12:04:21 -05002449 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002450}
2451
Chris Mason97571fd2007-02-24 13:39:08 -05002452/*
Chris Mason79f95c82007-03-01 15:16:26 -05002453 * try to push data from one node into the next node right in the
2454 * tree.
2455 *
2456 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2457 * error, and > 0 if there was no room in the right hand block.
2458 *
2459 * this will only push up to 1/2 the contents of the left node over
2460 */
Chris Mason5f39d392007-10-15 16:14:19 -04002461static int balance_node_right(struct btrfs_trans_handle *trans,
2462 struct btrfs_root *root,
2463 struct extent_buffer *dst,
2464 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002465{
Chris Mason79f95c82007-03-01 15:16:26 -05002466 int push_items = 0;
2467 int max_push;
2468 int src_nritems;
2469 int dst_nritems;
2470 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002471
Chris Mason7bb86312007-12-11 09:25:06 -05002472 WARN_ON(btrfs_header_generation(src) != trans->transid);
2473 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2474
Chris Mason5f39d392007-10-15 16:14:19 -04002475 src_nritems = btrfs_header_nritems(src);
2476 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002477 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002478 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002479 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002480
Chris Masond3977122009-01-05 21:25:51 -05002481 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002482 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002483
2484 max_push = src_nritems / 2 + 1;
2485 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002486 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002487 return 1;
Yan252c38f2007-08-29 09:11:44 -04002488
Chris Mason79f95c82007-03-01 15:16:26 -05002489 if (max_push < push_items)
2490 push_items = max_push;
2491
Chris Mason5f39d392007-10-15 16:14:19 -04002492 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2493 btrfs_node_key_ptr_offset(0),
2494 (dst_nritems) *
2495 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002496
Chris Mason5f39d392007-10-15 16:14:19 -04002497 copy_extent_buffer(dst, src,
2498 btrfs_node_key_ptr_offset(0),
2499 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002500 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002501
Chris Mason5f39d392007-10-15 16:14:19 -04002502 btrfs_set_header_nritems(src, src_nritems - push_items);
2503 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002504
Chris Mason5f39d392007-10-15 16:14:19 -04002505 btrfs_mark_buffer_dirty(src);
2506 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002507
Chris Mason79f95c82007-03-01 15:16:26 -05002508 return ret;
2509}
2510
2511/*
Chris Mason97571fd2007-02-24 13:39:08 -05002512 * helper function to insert a new root level in the tree.
2513 * A new node is allocated, and a single item is inserted to
2514 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002515 *
2516 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002517 */
Chris Masond3977122009-01-05 21:25:51 -05002518static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002519 struct btrfs_root *root,
2520 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002521{
Chris Mason7bb86312007-12-11 09:25:06 -05002522 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002523 struct extent_buffer *lower;
2524 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002525 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002526 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002527
2528 BUG_ON(path->nodes[level]);
2529 BUG_ON(path->nodes[level-1] != root->node);
2530
Chris Mason7bb86312007-12-11 09:25:06 -05002531 lower = path->nodes[level-1];
2532 if (level == 1)
2533 btrfs_item_key(lower, &lower_key, 0);
2534 else
2535 btrfs_node_key(lower, &lower_key, 0);
2536
Zheng Yan31840ae2008-09-23 13:14:14 -04002537 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002538 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002539 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002540 if (IS_ERR(c))
2541 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002542
Yan, Zhengf0486c62010-05-16 10:46:25 -04002543 root_add_used(root, root->nodesize);
2544
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002545 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002546 btrfs_set_header_nritems(c, 1);
2547 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002548 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002549 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002550 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002551 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002552
Chris Mason5f39d392007-10-15 16:14:19 -04002553 write_extent_buffer(c, root->fs_info->fsid,
2554 (unsigned long)btrfs_header_fsid(c),
2555 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002556
2557 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2558 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2559 BTRFS_UUID_SIZE);
2560
Chris Mason5f39d392007-10-15 16:14:19 -04002561 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002562 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002563 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002564 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002565
2566 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002567
2568 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002569
Chris Mason925baed2008-06-25 16:01:30 -04002570 old = root->node;
Chris Mason240f62c2011-03-23 14:54:42 -04002571 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002572
2573 /* the super has an extra ref to root->node */
2574 free_extent_buffer(old);
2575
Chris Mason0b86a832008-03-24 15:01:56 -04002576 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002577 extent_buffer_get(c);
2578 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002579 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002580 path->slots[level] = 0;
2581 return 0;
2582}
2583
Chris Mason74123bd2007-02-02 11:05:29 -05002584/*
2585 * worker function to insert a single pointer in a node.
2586 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002587 *
Chris Mason74123bd2007-02-02 11:05:29 -05002588 * slot and level indicate where you want the key to go, and
2589 * blocknr is the block the key points to.
2590 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002591static void insert_ptr(struct btrfs_trans_handle *trans,
2592 struct btrfs_root *root, struct btrfs_path *path,
2593 struct btrfs_disk_key *key, u64 bytenr,
2594 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002595{
Chris Mason5f39d392007-10-15 16:14:19 -04002596 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002597 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002598
2599 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002600 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002601 lower = path->nodes[level];
2602 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002603 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002604 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002605 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002606 memmove_extent_buffer(lower,
2607 btrfs_node_key_ptr_offset(slot + 1),
2608 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002609 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002610 }
Chris Mason5f39d392007-10-15 16:14:19 -04002611 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002612 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002613 WARN_ON(trans->transid == 0);
2614 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002615 btrfs_set_header_nritems(lower, nritems + 1);
2616 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002617}
2618
Chris Mason97571fd2007-02-24 13:39:08 -05002619/*
2620 * split the node at the specified level in path in two.
2621 * The path is corrected to point to the appropriate node after the split
2622 *
2623 * Before splitting this tries to make some room in the node by pushing
2624 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002625 *
2626 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002627 */
Chris Masone02119d2008-09-05 16:13:11 -04002628static noinline int split_node(struct btrfs_trans_handle *trans,
2629 struct btrfs_root *root,
2630 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002631{
Chris Mason5f39d392007-10-15 16:14:19 -04002632 struct extent_buffer *c;
2633 struct extent_buffer *split;
2634 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002635 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002636 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002637 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002638
Chris Mason5f39d392007-10-15 16:14:19 -04002639 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002640 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002641 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002642 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002643 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002644 if (ret)
2645 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002646 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002647 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002648 c = path->nodes[level];
2649 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002650 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002651 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002652 if (ret < 0)
2653 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002654 }
Chris Masone66f7092007-04-20 13:16:02 -04002655
Chris Mason5f39d392007-10-15 16:14:19 -04002656 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002657 mid = (c_nritems + 1) / 2;
2658 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002659
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002660 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002661 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02002662 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002663 if (IS_ERR(split))
2664 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002665
Yan, Zhengf0486c62010-05-16 10:46:25 -04002666 root_add_used(root, root->nodesize);
2667
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002668 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002669 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002670 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002672 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002673 btrfs_set_header_owner(split, root->root_key.objectid);
2674 write_extent_buffer(split, root->fs_info->fsid,
2675 (unsigned long)btrfs_header_fsid(split),
2676 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002677 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2678 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2679 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002680
Chris Mason5f39d392007-10-15 16:14:19 -04002681 copy_extent_buffer(split, c,
2682 btrfs_node_key_ptr_offset(0),
2683 btrfs_node_key_ptr_offset(mid),
2684 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2685 btrfs_set_header_nritems(split, c_nritems - mid);
2686 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002687 ret = 0;
2688
Chris Mason5f39d392007-10-15 16:14:19 -04002689 btrfs_mark_buffer_dirty(c);
2690 btrfs_mark_buffer_dirty(split);
2691
Jeff Mahoney143bede2012-03-01 14:56:26 +01002692 insert_ptr(trans, root, path, &disk_key, split->start,
2693 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002694
Chris Mason5de08d72007-02-24 06:24:44 -05002695 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002696 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002697 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 free_extent_buffer(c);
2699 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002700 path->slots[level + 1] += 1;
2701 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002702 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002703 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002705 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002706}
2707
Chris Mason74123bd2007-02-02 11:05:29 -05002708/*
2709 * how many bytes are required to store the items in a leaf. start
2710 * and nr indicate which items in the leaf to check. This totals up the
2711 * space used both by the item structs and the item data
2712 */
Chris Mason5f39d392007-10-15 16:14:19 -04002713static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002714{
2715 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002716 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002717 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002718
2719 if (!nr)
2720 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002721 data_len = btrfs_item_end_nr(l, start);
2722 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002723 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002724 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002725 return data_len;
2726}
2727
Chris Mason74123bd2007-02-02 11:05:29 -05002728/*
Chris Masond4dbff92007-04-04 14:08:15 -04002729 * The space between the end of the leaf items and
2730 * the start of the leaf data. IOW, how much room
2731 * the leaf has left for both items and data
2732 */
Chris Masond3977122009-01-05 21:25:51 -05002733noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002734 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002735{
Chris Mason5f39d392007-10-15 16:14:19 -04002736 int nritems = btrfs_header_nritems(leaf);
2737 int ret;
2738 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2739 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002740 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2741 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002742 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002743 leaf_space_used(leaf, 0, nritems), nritems);
2744 }
2745 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002746}
2747
Chris Mason99d8f832010-07-07 10:51:48 -04002748/*
2749 * min slot controls the lowest index we're willing to push to the
2750 * right. We'll push up to and including min_slot, but no lower
2751 */
Chris Mason44871b12009-03-13 10:04:31 -04002752static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2753 struct btrfs_root *root,
2754 struct btrfs_path *path,
2755 int data_size, int empty,
2756 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002757 int free_space, u32 left_nritems,
2758 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002759{
Chris Mason5f39d392007-10-15 16:14:19 -04002760 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002761 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002762 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002763 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002764 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002765 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002766 int push_space = 0;
2767 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002768 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002769 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002770 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002771 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002772 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002773
Chris Masoncfed81a2012-03-03 07:40:03 -05002774 btrfs_init_map_token(&token);
2775
Chris Mason34a38212007-11-07 13:31:03 -05002776 if (empty)
2777 nr = 0;
2778 else
Chris Mason99d8f832010-07-07 10:51:48 -04002779 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002780
Zheng Yan31840ae2008-09-23 13:14:14 -04002781 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002782 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002783
Chris Mason44871b12009-03-13 10:04:31 -04002784 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002785 i = left_nritems - 1;
2786 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002787 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002788
Zheng Yan31840ae2008-09-23 13:14:14 -04002789 if (!empty && push_items > 0) {
2790 if (path->slots[0] > i)
2791 break;
2792 if (path->slots[0] == i) {
2793 int space = btrfs_leaf_free_space(root, left);
2794 if (space + push_space * 2 > free_space)
2795 break;
2796 }
2797 }
2798
Chris Mason00ec4c52007-02-24 12:47:20 -05002799 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002800 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002801
Chris Masondb945352007-10-15 16:15:53 -04002802 this_item_size = btrfs_item_size(left, item);
2803 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002804 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002805
Chris Mason00ec4c52007-02-24 12:47:20 -05002806 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002807 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002808 if (i == 0)
2809 break;
2810 i--;
Chris Masondb945352007-10-15 16:15:53 -04002811 }
Chris Mason5f39d392007-10-15 16:14:19 -04002812
Chris Mason925baed2008-06-25 16:01:30 -04002813 if (push_items == 0)
2814 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002815
Chris Mason34a38212007-11-07 13:31:03 -05002816 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002817 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002818
Chris Mason00ec4c52007-02-24 12:47:20 -05002819 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002820 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002821
Chris Mason5f39d392007-10-15 16:14:19 -04002822 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002823 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002824
Chris Mason00ec4c52007-02-24 12:47:20 -05002825 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002826 data_end = leaf_data_end(root, right);
2827 memmove_extent_buffer(right,
2828 btrfs_leaf_data(right) + data_end - push_space,
2829 btrfs_leaf_data(right) + data_end,
2830 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2831
Chris Mason00ec4c52007-02-24 12:47:20 -05002832 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002833 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002834 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2835 btrfs_leaf_data(left) + leaf_data_end(root, left),
2836 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002837
2838 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2839 btrfs_item_nr_offset(0),
2840 right_nritems * sizeof(struct btrfs_item));
2841
Chris Mason00ec4c52007-02-24 12:47:20 -05002842 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002843 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2844 btrfs_item_nr_offset(left_nritems - push_items),
2845 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002846
2847 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002848 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002849 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002850 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002851 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002852 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002853 push_space -= btrfs_token_item_size(right, item, &token);
2854 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002855 }
2856
Chris Mason7518a232007-03-12 12:01:18 -04002857 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002858 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002859
Chris Mason34a38212007-11-07 13:31:03 -05002860 if (left_nritems)
2861 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002862 else
2863 clean_tree_block(trans, root, left);
2864
Chris Mason5f39d392007-10-15 16:14:19 -04002865 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002866
Chris Mason5f39d392007-10-15 16:14:19 -04002867 btrfs_item_key(right, &disk_key, 0);
2868 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002869 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002870
Chris Mason00ec4c52007-02-24 12:47:20 -05002871 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002872 if (path->slots[0] >= left_nritems) {
2873 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002874 if (btrfs_header_nritems(path->nodes[0]) == 0)
2875 clean_tree_block(trans, root, path->nodes[0]);
2876 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002877 free_extent_buffer(path->nodes[0]);
2878 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002879 path->slots[1] += 1;
2880 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002881 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002882 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002883 }
2884 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002885
2886out_unlock:
2887 btrfs_tree_unlock(right);
2888 free_extent_buffer(right);
2889 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002890}
Chris Mason925baed2008-06-25 16:01:30 -04002891
Chris Mason00ec4c52007-02-24 12:47:20 -05002892/*
Chris Mason44871b12009-03-13 10:04:31 -04002893 * push some data in the path leaf to the right, trying to free up at
2894 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2895 *
2896 * returns 1 if the push failed because the other node didn't have enough
2897 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002898 *
2899 * this will push starting from min_slot to the end of the leaf. It won't
2900 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002901 */
2902static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002903 *root, struct btrfs_path *path,
2904 int min_data_size, int data_size,
2905 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002906{
2907 struct extent_buffer *left = path->nodes[0];
2908 struct extent_buffer *right;
2909 struct extent_buffer *upper;
2910 int slot;
2911 int free_space;
2912 u32 left_nritems;
2913 int ret;
2914
2915 if (!path->nodes[1])
2916 return 1;
2917
2918 slot = path->slots[1];
2919 upper = path->nodes[1];
2920 if (slot >= btrfs_header_nritems(upper) - 1)
2921 return 1;
2922
2923 btrfs_assert_tree_locked(path->nodes[1]);
2924
2925 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002926 if (right == NULL)
2927 return 1;
2928
Chris Mason44871b12009-03-13 10:04:31 -04002929 btrfs_tree_lock(right);
2930 btrfs_set_lock_blocking(right);
2931
2932 free_space = btrfs_leaf_free_space(root, right);
2933 if (free_space < data_size)
2934 goto out_unlock;
2935
2936 /* cow and double check */
2937 ret = btrfs_cow_block(trans, root, right, upper,
2938 slot + 1, &right);
2939 if (ret)
2940 goto out_unlock;
2941
2942 free_space = btrfs_leaf_free_space(root, right);
2943 if (free_space < data_size)
2944 goto out_unlock;
2945
2946 left_nritems = btrfs_header_nritems(left);
2947 if (left_nritems == 0)
2948 goto out_unlock;
2949
Chris Mason99d8f832010-07-07 10:51:48 -04002950 return __push_leaf_right(trans, root, path, min_data_size, empty,
2951 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002952out_unlock:
2953 btrfs_tree_unlock(right);
2954 free_extent_buffer(right);
2955 return 1;
2956}
2957
2958/*
Chris Mason74123bd2007-02-02 11:05:29 -05002959 * push some data in the path leaf to the left, trying to free up at
2960 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002961 *
2962 * max_slot can put a limit on how far into the leaf we'll push items. The
2963 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
2964 * items
Chris Mason74123bd2007-02-02 11:05:29 -05002965 */
Chris Mason44871b12009-03-13 10:04:31 -04002966static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
2967 struct btrfs_root *root,
2968 struct btrfs_path *path, int data_size,
2969 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04002970 int free_space, u32 right_nritems,
2971 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002972{
Chris Mason5f39d392007-10-15 16:14:19 -04002973 struct btrfs_disk_key disk_key;
2974 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05002975 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002976 int push_space = 0;
2977 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002978 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04002979 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05002980 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002981 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04002982 u32 this_item_size;
2983 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05002984 struct btrfs_map_token token;
2985
2986 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002987
Chris Mason34a38212007-11-07 13:31:03 -05002988 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04002989 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002990 else
Chris Mason99d8f832010-07-07 10:51:48 -04002991 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002992
2993 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002994 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04002995
Zheng Yan31840ae2008-09-23 13:14:14 -04002996 if (!empty && push_items > 0) {
2997 if (path->slots[0] < i)
2998 break;
2999 if (path->slots[0] == i) {
3000 int space = btrfs_leaf_free_space(root, right);
3001 if (space + push_space * 2 > free_space)
3002 break;
3003 }
3004 }
3005
Chris Masonbe0e5c02007-01-26 15:51:26 -05003006 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003007 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003008
3009 this_item_size = btrfs_item_size(right, item);
3010 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011 break;
Chris Masondb945352007-10-15 16:15:53 -04003012
Chris Masonbe0e5c02007-01-26 15:51:26 -05003013 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003014 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003015 }
Chris Masondb945352007-10-15 16:15:53 -04003016
Chris Masonbe0e5c02007-01-26 15:51:26 -05003017 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003018 ret = 1;
3019 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003020 }
Chris Mason34a38212007-11-07 13:31:03 -05003021 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003022 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003023
Chris Masonbe0e5c02007-01-26 15:51:26 -05003024 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003025 copy_extent_buffer(left, right,
3026 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3027 btrfs_item_nr_offset(0),
3028 push_items * sizeof(struct btrfs_item));
3029
Chris Mason123abc82007-03-14 14:14:43 -04003030 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003031 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003032
3033 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003034 leaf_data_end(root, left) - push_space,
3035 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003036 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003037 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003038 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003039 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003040
Chris Masondb945352007-10-15 16:15:53 -04003041 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003042 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003043 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003044
Chris Mason5f39d392007-10-15 16:14:19 -04003045 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003046
Chris Masoncfed81a2012-03-03 07:40:03 -05003047 ioff = btrfs_token_item_offset(left, item, &token);
3048 btrfs_set_token_item_offset(left, item,
3049 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3050 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003051 }
Chris Mason5f39d392007-10-15 16:14:19 -04003052 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003053
3054 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003055 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003056 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3057 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003058 WARN_ON(1);
3059 }
Chris Mason5f39d392007-10-15 16:14:19 -04003060
Chris Mason34a38212007-11-07 13:31:03 -05003061 if (push_items < right_nritems) {
3062 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3063 leaf_data_end(root, right);
3064 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3065 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3066 btrfs_leaf_data(right) +
3067 leaf_data_end(root, right), push_space);
3068
3069 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003070 btrfs_item_nr_offset(push_items),
3071 (btrfs_header_nritems(right) - push_items) *
3072 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003073 }
Yaneef1c492007-11-26 10:58:13 -05003074 right_nritems -= push_items;
3075 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003076 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003077 for (i = 0; i < right_nritems; i++) {
3078 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003079
Chris Masoncfed81a2012-03-03 07:40:03 -05003080 push_space = push_space - btrfs_token_item_size(right,
3081 item, &token);
3082 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003083 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003084
Chris Mason5f39d392007-10-15 16:14:19 -04003085 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003086 if (right_nritems)
3087 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003088 else
3089 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003090
Chris Mason5f39d392007-10-15 16:14:19 -04003091 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003092 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003093
3094 /* then fixup the leaf pointer in the path */
3095 if (path->slots[0] < push_items) {
3096 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003097 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003098 free_extent_buffer(path->nodes[0]);
3099 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003100 path->slots[1] -= 1;
3101 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003102 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003103 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003104 path->slots[0] -= push_items;
3105 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003106 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003107 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003108out:
3109 btrfs_tree_unlock(left);
3110 free_extent_buffer(left);
3111 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003112}
3113
Chris Mason74123bd2007-02-02 11:05:29 -05003114/*
Chris Mason44871b12009-03-13 10:04:31 -04003115 * push some data in the path leaf to the left, trying to free up at
3116 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003117 *
3118 * max_slot can put a limit on how far into the leaf we'll push items. The
3119 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3120 * items
Chris Mason44871b12009-03-13 10:04:31 -04003121 */
3122static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003123 *root, struct btrfs_path *path, int min_data_size,
3124 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003125{
3126 struct extent_buffer *right = path->nodes[0];
3127 struct extent_buffer *left;
3128 int slot;
3129 int free_space;
3130 u32 right_nritems;
3131 int ret = 0;
3132
3133 slot = path->slots[1];
3134 if (slot == 0)
3135 return 1;
3136 if (!path->nodes[1])
3137 return 1;
3138
3139 right_nritems = btrfs_header_nritems(right);
3140 if (right_nritems == 0)
3141 return 1;
3142
3143 btrfs_assert_tree_locked(path->nodes[1]);
3144
3145 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003146 if (left == NULL)
3147 return 1;
3148
Chris Mason44871b12009-03-13 10:04:31 -04003149 btrfs_tree_lock(left);
3150 btrfs_set_lock_blocking(left);
3151
3152 free_space = btrfs_leaf_free_space(root, left);
3153 if (free_space < data_size) {
3154 ret = 1;
3155 goto out;
3156 }
3157
3158 /* cow and double check */
3159 ret = btrfs_cow_block(trans, root, left,
3160 path->nodes[1], slot - 1, &left);
3161 if (ret) {
3162 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003163 if (ret == -ENOSPC)
3164 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003165 goto out;
3166 }
3167
3168 free_space = btrfs_leaf_free_space(root, left);
3169 if (free_space < data_size) {
3170 ret = 1;
3171 goto out;
3172 }
3173
Chris Mason99d8f832010-07-07 10:51:48 -04003174 return __push_leaf_left(trans, root, path, min_data_size,
3175 empty, left, free_space, right_nritems,
3176 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003177out:
3178 btrfs_tree_unlock(left);
3179 free_extent_buffer(left);
3180 return ret;
3181}
3182
3183/*
Chris Mason74123bd2007-02-02 11:05:29 -05003184 * split the path's leaf in two, making sure there is at least data_size
3185 * available for the resulting leaf level of the path.
3186 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003187static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3188 struct btrfs_root *root,
3189 struct btrfs_path *path,
3190 struct extent_buffer *l,
3191 struct extent_buffer *right,
3192 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003193{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003194 int data_copy_size;
3195 int rt_data_off;
3196 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003197 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003198 struct btrfs_map_token token;
3199
3200 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003201
Chris Mason5f39d392007-10-15 16:14:19 -04003202 nritems = nritems - mid;
3203 btrfs_set_header_nritems(right, nritems);
3204 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3205
3206 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3207 btrfs_item_nr_offset(mid),
3208 nritems * sizeof(struct btrfs_item));
3209
3210 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003211 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3212 data_copy_size, btrfs_leaf_data(l) +
3213 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003214
Chris Mason5f39d392007-10-15 16:14:19 -04003215 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3216 btrfs_item_end_nr(l, mid);
3217
3218 for (i = 0; i < nritems; i++) {
3219 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003220 u32 ioff;
3221
Chris Masoncfed81a2012-03-03 07:40:03 -05003222 ioff = btrfs_token_item_offset(right, item, &token);
3223 btrfs_set_token_item_offset(right, item,
3224 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003225 }
Chris Mason74123bd2007-02-02 11:05:29 -05003226
Chris Mason5f39d392007-10-15 16:14:19 -04003227 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003228 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003229 insert_ptr(trans, root, path, &disk_key, right->start,
3230 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003231
3232 btrfs_mark_buffer_dirty(right);
3233 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003234 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003235
Chris Masonbe0e5c02007-01-26 15:51:26 -05003236 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003237 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003238 free_extent_buffer(path->nodes[0]);
3239 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003240 path->slots[0] -= mid;
3241 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003242 } else {
3243 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003244 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003245 }
Chris Mason5f39d392007-10-15 16:14:19 -04003246
Chris Masoneb60cea2007-02-02 09:18:22 -05003247 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003248}
3249
3250/*
Chris Mason99d8f832010-07-07 10:51:48 -04003251 * double splits happen when we need to insert a big item in the middle
3252 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3253 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3254 * A B C
3255 *
3256 * We avoid this by trying to push the items on either side of our target
3257 * into the adjacent leaves. If all goes well we can avoid the double split
3258 * completely.
3259 */
3260static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3261 struct btrfs_root *root,
3262 struct btrfs_path *path,
3263 int data_size)
3264{
3265 int ret;
3266 int progress = 0;
3267 int slot;
3268 u32 nritems;
3269
3270 slot = path->slots[0];
3271
3272 /*
3273 * try to push all the items after our slot into the
3274 * right leaf
3275 */
3276 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3277 if (ret < 0)
3278 return ret;
3279
3280 if (ret == 0)
3281 progress++;
3282
3283 nritems = btrfs_header_nritems(path->nodes[0]);
3284 /*
3285 * our goal is to get our slot at the start or end of a leaf. If
3286 * we've done so we're done
3287 */
3288 if (path->slots[0] == 0 || path->slots[0] == nritems)
3289 return 0;
3290
3291 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3292 return 0;
3293
3294 /* try to push all the items before our slot into the next leaf */
3295 slot = path->slots[0];
3296 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3297 if (ret < 0)
3298 return ret;
3299
3300 if (ret == 0)
3301 progress++;
3302
3303 if (progress)
3304 return 0;
3305 return 1;
3306}
3307
3308/*
Chris Mason44871b12009-03-13 10:04:31 -04003309 * split the path's leaf in two, making sure there is at least data_size
3310 * available for the resulting leaf level of the path.
3311 *
3312 * returns 0 if all went well and < 0 on failure.
3313 */
3314static noinline int split_leaf(struct btrfs_trans_handle *trans,
3315 struct btrfs_root *root,
3316 struct btrfs_key *ins_key,
3317 struct btrfs_path *path, int data_size,
3318 int extend)
3319{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003320 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003321 struct extent_buffer *l;
3322 u32 nritems;
3323 int mid;
3324 int slot;
3325 struct extent_buffer *right;
3326 int ret = 0;
3327 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003328 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003329 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003330 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003331
Yan, Zhenga5719522009-09-24 09:17:31 -04003332 l = path->nodes[0];
3333 slot = path->slots[0];
3334 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3335 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3336 return -EOVERFLOW;
3337
Chris Mason44871b12009-03-13 10:04:31 -04003338 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003339 if (data_size) {
3340 wret = push_leaf_right(trans, root, path, data_size,
3341 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003342 if (wret < 0)
3343 return wret;
3344 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003345 wret = push_leaf_left(trans, root, path, data_size,
3346 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003347 if (wret < 0)
3348 return wret;
3349 }
3350 l = path->nodes[0];
3351
3352 /* did the pushes work? */
3353 if (btrfs_leaf_free_space(root, l) >= data_size)
3354 return 0;
3355 }
3356
3357 if (!path->nodes[1]) {
3358 ret = insert_new_root(trans, root, path, 1);
3359 if (ret)
3360 return ret;
3361 }
3362again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003363 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003364 l = path->nodes[0];
3365 slot = path->slots[0];
3366 nritems = btrfs_header_nritems(l);
3367 mid = (nritems + 1) / 2;
3368
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003369 if (mid <= slot) {
3370 if (nritems == 1 ||
3371 leaf_space_used(l, mid, nritems - mid) + data_size >
3372 BTRFS_LEAF_DATA_SIZE(root)) {
3373 if (slot >= nritems) {
3374 split = 0;
3375 } else {
3376 mid = slot;
3377 if (mid != nritems &&
3378 leaf_space_used(l, mid, nritems - mid) +
3379 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003380 if (data_size && !tried_avoid_double)
3381 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003382 split = 2;
3383 }
3384 }
3385 }
3386 } else {
3387 if (leaf_space_used(l, 0, mid) + data_size >
3388 BTRFS_LEAF_DATA_SIZE(root)) {
3389 if (!extend && data_size && slot == 0) {
3390 split = 0;
3391 } else if ((extend || !data_size) && slot == 0) {
3392 mid = 1;
3393 } else {
3394 mid = slot;
3395 if (mid != nritems &&
3396 leaf_space_used(l, mid, nritems - mid) +
3397 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003398 if (data_size && !tried_avoid_double)
3399 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003400 split = 2 ;
3401 }
3402 }
3403 }
3404 }
3405
3406 if (split == 0)
3407 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3408 else
3409 btrfs_item_key(l, &disk_key, mid);
3410
3411 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003412 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003413 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003414 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003415 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003416
3417 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003418
3419 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3420 btrfs_set_header_bytenr(right, right->start);
3421 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003422 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003423 btrfs_set_header_owner(right, root->root_key.objectid);
3424 btrfs_set_header_level(right, 0);
3425 write_extent_buffer(right, root->fs_info->fsid,
3426 (unsigned long)btrfs_header_fsid(right),
3427 BTRFS_FSID_SIZE);
3428
3429 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3430 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3431 BTRFS_UUID_SIZE);
3432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003433 if (split == 0) {
3434 if (mid <= slot) {
3435 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003436 insert_ptr(trans, root, path, &disk_key, right->start,
3437 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003438 btrfs_tree_unlock(path->nodes[0]);
3439 free_extent_buffer(path->nodes[0]);
3440 path->nodes[0] = right;
3441 path->slots[0] = 0;
3442 path->slots[1] += 1;
3443 } else {
3444 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003445 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003446 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003447 btrfs_tree_unlock(path->nodes[0]);
3448 free_extent_buffer(path->nodes[0]);
3449 path->nodes[0] = right;
3450 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003451 if (path->slots[1] == 0)
3452 fixup_low_keys(trans, root, path,
3453 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003454 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003455 btrfs_mark_buffer_dirty(right);
3456 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003457 }
3458
Jeff Mahoney143bede2012-03-01 14:56:26 +01003459 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003460
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003461 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003462 BUG_ON(num_doubles != 0);
3463 num_doubles++;
3464 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003465 }
Chris Mason44871b12009-03-13 10:04:31 -04003466
Jeff Mahoney143bede2012-03-01 14:56:26 +01003467 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003468
3469push_for_double:
3470 push_for_double_split(trans, root, path, data_size);
3471 tried_avoid_double = 1;
3472 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3473 return 0;
3474 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003475}
3476
Yan, Zhengad48fd752009-11-12 09:33:58 +00003477static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3478 struct btrfs_root *root,
3479 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003480{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003481 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003482 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003483 struct btrfs_file_extent_item *fi;
3484 u64 extent_len = 0;
3485 u32 item_size;
3486 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003487
3488 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003489 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3490
3491 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3492 key.type != BTRFS_EXTENT_CSUM_KEY);
3493
3494 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3495 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003496
3497 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003498 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3499 fi = btrfs_item_ptr(leaf, path->slots[0],
3500 struct btrfs_file_extent_item);
3501 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3502 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003503 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003504
Chris Mason459931e2008-12-10 09:10:46 -05003505 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003506 path->search_for_split = 1;
3507 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003508 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003509 if (ret < 0)
3510 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003511
Yan, Zhengad48fd752009-11-12 09:33:58 +00003512 ret = -EAGAIN;
3513 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003514 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003515 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3516 goto err;
3517
Chris Mason109f6ae2010-04-02 09:20:18 -04003518 /* the leaf has changed, it now has room. return now */
3519 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3520 goto err;
3521
Yan, Zhengad48fd752009-11-12 09:33:58 +00003522 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3523 fi = btrfs_item_ptr(leaf, path->slots[0],
3524 struct btrfs_file_extent_item);
3525 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3526 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003527 }
3528
Chris Masonb9473432009-03-13 11:00:37 -04003529 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003530 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003531 if (ret)
3532 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003533
Yan, Zhengad48fd752009-11-12 09:33:58 +00003534 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003535 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003536 return 0;
3537err:
3538 path->keep_locks = 0;
3539 return ret;
3540}
3541
3542static noinline int split_item(struct btrfs_trans_handle *trans,
3543 struct btrfs_root *root,
3544 struct btrfs_path *path,
3545 struct btrfs_key *new_key,
3546 unsigned long split_offset)
3547{
3548 struct extent_buffer *leaf;
3549 struct btrfs_item *item;
3550 struct btrfs_item *new_item;
3551 int slot;
3552 char *buf;
3553 u32 nritems;
3554 u32 item_size;
3555 u32 orig_offset;
3556 struct btrfs_disk_key disk_key;
3557
Chris Masonb9473432009-03-13 11:00:37 -04003558 leaf = path->nodes[0];
3559 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3560
Chris Masonb4ce94d2009-02-04 09:25:08 -05003561 btrfs_set_path_blocking(path);
3562
Chris Mason459931e2008-12-10 09:10:46 -05003563 item = btrfs_item_nr(leaf, path->slots[0]);
3564 orig_offset = btrfs_item_offset(leaf, item);
3565 item_size = btrfs_item_size(leaf, item);
3566
Chris Mason459931e2008-12-10 09:10:46 -05003567 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003568 if (!buf)
3569 return -ENOMEM;
3570
Chris Mason459931e2008-12-10 09:10:46 -05003571 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3572 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003573
Chris Mason459931e2008-12-10 09:10:46 -05003574 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003575 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003576 if (slot != nritems) {
3577 /* shift the items */
3578 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003579 btrfs_item_nr_offset(slot),
3580 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003581 }
3582
3583 btrfs_cpu_key_to_disk(&disk_key, new_key);
3584 btrfs_set_item_key(leaf, &disk_key, slot);
3585
3586 new_item = btrfs_item_nr(leaf, slot);
3587
3588 btrfs_set_item_offset(leaf, new_item, orig_offset);
3589 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3590
3591 btrfs_set_item_offset(leaf, item,
3592 orig_offset + item_size - split_offset);
3593 btrfs_set_item_size(leaf, item, split_offset);
3594
3595 btrfs_set_header_nritems(leaf, nritems + 1);
3596
3597 /* write the data for the start of the original item */
3598 write_extent_buffer(leaf, buf,
3599 btrfs_item_ptr_offset(leaf, path->slots[0]),
3600 split_offset);
3601
3602 /* write the data for the new item */
3603 write_extent_buffer(leaf, buf + split_offset,
3604 btrfs_item_ptr_offset(leaf, slot),
3605 item_size - split_offset);
3606 btrfs_mark_buffer_dirty(leaf);
3607
Yan, Zhengad48fd752009-11-12 09:33:58 +00003608 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003609 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003610 return 0;
3611}
3612
3613/*
3614 * This function splits a single item into two items,
3615 * giving 'new_key' to the new item and splitting the
3616 * old one at split_offset (from the start of the item).
3617 *
3618 * The path may be released by this operation. After
3619 * the split, the path is pointing to the old item. The
3620 * new item is going to be in the same node as the old one.
3621 *
3622 * Note, the item being split must be smaller enough to live alone on
3623 * a tree block with room for one extra struct btrfs_item
3624 *
3625 * This allows us to split the item in place, keeping a lock on the
3626 * leaf the entire time.
3627 */
3628int btrfs_split_item(struct btrfs_trans_handle *trans,
3629 struct btrfs_root *root,
3630 struct btrfs_path *path,
3631 struct btrfs_key *new_key,
3632 unsigned long split_offset)
3633{
3634 int ret;
3635 ret = setup_leaf_for_split(trans, root, path,
3636 sizeof(struct btrfs_item));
3637 if (ret)
3638 return ret;
3639
3640 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003641 return ret;
3642}
3643
3644/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003645 * This function duplicate a item, giving 'new_key' to the new item.
3646 * It guarantees both items live in the same tree leaf and the new item
3647 * is contiguous with the original item.
3648 *
3649 * This allows us to split file extent in place, keeping a lock on the
3650 * leaf the entire time.
3651 */
3652int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3653 struct btrfs_root *root,
3654 struct btrfs_path *path,
3655 struct btrfs_key *new_key)
3656{
3657 struct extent_buffer *leaf;
3658 int ret;
3659 u32 item_size;
3660
3661 leaf = path->nodes[0];
3662 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3663 ret = setup_leaf_for_split(trans, root, path,
3664 item_size + sizeof(struct btrfs_item));
3665 if (ret)
3666 return ret;
3667
3668 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003669 setup_items_for_insert(trans, root, path, new_key, &item_size,
3670 item_size, item_size +
3671 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003672 leaf = path->nodes[0];
3673 memcpy_extent_buffer(leaf,
3674 btrfs_item_ptr_offset(leaf, path->slots[0]),
3675 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3676 item_size);
3677 return 0;
3678}
3679
3680/*
Chris Masond352ac62008-09-29 15:18:18 -04003681 * make the item pointed to by the path smaller. new_size indicates
3682 * how small to make it, and from_end tells us if we just chop bytes
3683 * off the end of the item or if we shift the item to chop bytes off
3684 * the front.
3685 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003686void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3687 struct btrfs_root *root,
3688 struct btrfs_path *path,
3689 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003690{
Chris Masonb18c6682007-04-17 13:26:50 -04003691 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003692 struct extent_buffer *leaf;
3693 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003694 u32 nritems;
3695 unsigned int data_end;
3696 unsigned int old_data_start;
3697 unsigned int old_size;
3698 unsigned int size_diff;
3699 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003700 struct btrfs_map_token token;
3701
3702 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003703
Chris Mason5f39d392007-10-15 16:14:19 -04003704 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003705 slot = path->slots[0];
3706
3707 old_size = btrfs_item_size_nr(leaf, slot);
3708 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003709 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003710
Chris Mason5f39d392007-10-15 16:14:19 -04003711 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003712 data_end = leaf_data_end(root, leaf);
3713
Chris Mason5f39d392007-10-15 16:14:19 -04003714 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003715
Chris Masonb18c6682007-04-17 13:26:50 -04003716 size_diff = old_size - new_size;
3717
3718 BUG_ON(slot < 0);
3719 BUG_ON(slot >= nritems);
3720
3721 /*
3722 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3723 */
3724 /* first correct the data pointers */
3725 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003726 u32 ioff;
3727 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003728
Chris Masoncfed81a2012-03-03 07:40:03 -05003729 ioff = btrfs_token_item_offset(leaf, item, &token);
3730 btrfs_set_token_item_offset(leaf, item,
3731 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003732 }
Chris Masondb945352007-10-15 16:15:53 -04003733
Chris Masonb18c6682007-04-17 13:26:50 -04003734 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003735 if (from_end) {
3736 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3737 data_end + size_diff, btrfs_leaf_data(leaf) +
3738 data_end, old_data_start + new_size - data_end);
3739 } else {
3740 struct btrfs_disk_key disk_key;
3741 u64 offset;
3742
3743 btrfs_item_key(leaf, &disk_key, slot);
3744
3745 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3746 unsigned long ptr;
3747 struct btrfs_file_extent_item *fi;
3748
3749 fi = btrfs_item_ptr(leaf, slot,
3750 struct btrfs_file_extent_item);
3751 fi = (struct btrfs_file_extent_item *)(
3752 (unsigned long)fi - size_diff);
3753
3754 if (btrfs_file_extent_type(leaf, fi) ==
3755 BTRFS_FILE_EXTENT_INLINE) {
3756 ptr = btrfs_item_ptr_offset(leaf, slot);
3757 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003758 (unsigned long)fi,
3759 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003760 disk_bytenr));
3761 }
3762 }
3763
3764 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3765 data_end + size_diff, btrfs_leaf_data(leaf) +
3766 data_end, old_data_start - data_end);
3767
3768 offset = btrfs_disk_key_offset(&disk_key);
3769 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3770 btrfs_set_item_key(leaf, &disk_key, slot);
3771 if (slot == 0)
3772 fixup_low_keys(trans, root, path, &disk_key, 1);
3773 }
Chris Mason5f39d392007-10-15 16:14:19 -04003774
3775 item = btrfs_item_nr(leaf, slot);
3776 btrfs_set_item_size(leaf, item, new_size);
3777 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003778
Chris Mason5f39d392007-10-15 16:14:19 -04003779 if (btrfs_leaf_free_space(root, leaf) < 0) {
3780 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003781 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003782 }
Chris Masonb18c6682007-04-17 13:26:50 -04003783}
3784
Chris Masond352ac62008-09-29 15:18:18 -04003785/*
3786 * make the item pointed to by the path bigger, data_size is the new size.
3787 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003788void btrfs_extend_item(struct btrfs_trans_handle *trans,
3789 struct btrfs_root *root, struct btrfs_path *path,
3790 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003791{
Chris Mason6567e832007-04-16 09:22:45 -04003792 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003793 struct extent_buffer *leaf;
3794 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003795 u32 nritems;
3796 unsigned int data_end;
3797 unsigned int old_data;
3798 unsigned int old_size;
3799 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003800 struct btrfs_map_token token;
3801
3802 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003803
Chris Mason5f39d392007-10-15 16:14:19 -04003804 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003805
Chris Mason5f39d392007-10-15 16:14:19 -04003806 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003807 data_end = leaf_data_end(root, leaf);
3808
Chris Mason5f39d392007-10-15 16:14:19 -04003809 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3810 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003811 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003812 }
Chris Mason6567e832007-04-16 09:22:45 -04003813 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003814 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003815
3816 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003817 if (slot >= nritems) {
3818 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003819 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3820 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003821 BUG_ON(1);
3822 }
Chris Mason6567e832007-04-16 09:22:45 -04003823
3824 /*
3825 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3826 */
3827 /* first correct the data pointers */
3828 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003829 u32 ioff;
3830 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003831
Chris Masoncfed81a2012-03-03 07:40:03 -05003832 ioff = btrfs_token_item_offset(leaf, item, &token);
3833 btrfs_set_token_item_offset(leaf, item,
3834 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003835 }
Chris Mason5f39d392007-10-15 16:14:19 -04003836
Chris Mason6567e832007-04-16 09:22:45 -04003837 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003838 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003839 data_end - data_size, btrfs_leaf_data(leaf) +
3840 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003841
Chris Mason6567e832007-04-16 09:22:45 -04003842 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003843 old_size = btrfs_item_size_nr(leaf, slot);
3844 item = btrfs_item_nr(leaf, slot);
3845 btrfs_set_item_size(leaf, item, old_size + data_size);
3846 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003847
Chris Mason5f39d392007-10-15 16:14:19 -04003848 if (btrfs_leaf_free_space(root, leaf) < 0) {
3849 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003850 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003851 }
Chris Mason6567e832007-04-16 09:22:45 -04003852}
3853
Chris Mason74123bd2007-02-02 11:05:29 -05003854/*
Chris Masond352ac62008-09-29 15:18:18 -04003855 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003856 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003857 * Returns the number of keys that were inserted.
3858 */
3859int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root,
3861 struct btrfs_path *path,
3862 struct btrfs_key *cpu_key, u32 *data_size,
3863 int nr)
3864{
3865 struct extent_buffer *leaf;
3866 struct btrfs_item *item;
3867 int ret = 0;
3868 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003869 int i;
3870 u32 nritems;
3871 u32 total_data = 0;
3872 u32 total_size = 0;
3873 unsigned int data_end;
3874 struct btrfs_disk_key disk_key;
3875 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003876 struct btrfs_map_token token;
3877
3878 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003879
Yan Zheng87b29b22008-12-17 10:21:48 -05003880 for (i = 0; i < nr; i++) {
3881 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3882 BTRFS_LEAF_DATA_SIZE(root)) {
3883 break;
3884 nr = i;
3885 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003886 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003887 total_size += data_size[i] + sizeof(struct btrfs_item);
3888 }
3889 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003890
Josef Bacikf3465ca2008-11-12 14:19:50 -05003891 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3892 if (ret == 0)
3893 return -EEXIST;
3894 if (ret < 0)
3895 goto out;
3896
Josef Bacikf3465ca2008-11-12 14:19:50 -05003897 leaf = path->nodes[0];
3898
3899 nritems = btrfs_header_nritems(leaf);
3900 data_end = leaf_data_end(root, leaf);
3901
3902 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3903 for (i = nr; i >= 0; i--) {
3904 total_data -= data_size[i];
3905 total_size -= data_size[i] + sizeof(struct btrfs_item);
3906 if (total_size < btrfs_leaf_free_space(root, leaf))
3907 break;
3908 }
3909 nr = i;
3910 }
3911
3912 slot = path->slots[0];
3913 BUG_ON(slot < 0);
3914
3915 if (slot != nritems) {
3916 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3917
3918 item = btrfs_item_nr(leaf, slot);
3919 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3920
3921 /* figure out how many keys we can insert in here */
3922 total_data = data_size[0];
3923 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003924 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003925 break;
3926 total_data += data_size[i];
3927 }
3928 nr = i;
3929
3930 if (old_data < data_end) {
3931 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003932 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003933 slot, old_data, data_end);
3934 BUG_ON(1);
3935 }
3936 /*
3937 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3938 */
3939 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003940 for (i = slot; i < nritems; i++) {
3941 u32 ioff;
3942
3943 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003944 ioff = btrfs_token_item_offset(leaf, item, &token);
3945 btrfs_set_token_item_offset(leaf, item,
3946 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003947 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003948 /* shift the items */
3949 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3950 btrfs_item_nr_offset(slot),
3951 (nritems - slot) * sizeof(struct btrfs_item));
3952
3953 /* shift the data */
3954 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3955 data_end - total_data, btrfs_leaf_data(leaf) +
3956 data_end, old_data - data_end);
3957 data_end = old_data;
3958 } else {
3959 /*
3960 * this sucks but it has to be done, if we are inserting at
3961 * the end of the leaf only insert 1 of the items, since we
3962 * have no way of knowing whats on the next leaf and we'd have
3963 * to drop our current locks to figure it out
3964 */
3965 nr = 1;
3966 }
3967
3968 /* setup the item for the new data */
3969 for (i = 0; i < nr; i++) {
3970 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
3971 btrfs_set_item_key(leaf, &disk_key, slot + i);
3972 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003973 btrfs_set_token_item_offset(leaf, item,
3974 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003975 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05003976 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003977 }
3978 btrfs_set_header_nritems(leaf, nritems + nr);
3979 btrfs_mark_buffer_dirty(leaf);
3980
3981 ret = 0;
3982 if (slot == 0) {
3983 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003984 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003985 }
3986
3987 if (btrfs_leaf_free_space(root, leaf) < 0) {
3988 btrfs_print_leaf(root, leaf);
3989 BUG();
3990 }
3991out:
3992 if (!ret)
3993 ret = nr;
3994 return ret;
3995}
3996
3997/*
Chris Mason44871b12009-03-13 10:04:31 -04003998 * this is a helper for btrfs_insert_empty_items, the main goal here is
3999 * to save stack depth by doing the bulk of the work in a function
4000 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004001 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004002void setup_items_for_insert(struct btrfs_trans_handle *trans,
4003 struct btrfs_root *root, struct btrfs_path *path,
4004 struct btrfs_key *cpu_key, u32 *data_size,
4005 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004006{
Chris Mason5f39d392007-10-15 16:14:19 -04004007 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004008 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004009 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004010 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004011 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004012 struct extent_buffer *leaf;
4013 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004014 struct btrfs_map_token token;
4015
4016 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004017
Chris Mason5f39d392007-10-15 16:14:19 -04004018 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004019 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004020
Chris Mason5f39d392007-10-15 16:14:19 -04004021 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004022 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004023
Chris Masonf25956c2008-09-12 15:32:53 -04004024 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004025 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004026 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004027 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004028 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004029 }
Chris Mason5f39d392007-10-15 16:14:19 -04004030
Chris Masonbe0e5c02007-01-26 15:51:26 -05004031 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004032 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004033
Chris Mason5f39d392007-10-15 16:14:19 -04004034 if (old_data < data_end) {
4035 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004036 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004037 slot, old_data, data_end);
4038 BUG_ON(1);
4039 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004040 /*
4041 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4042 */
4043 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004044 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004045 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004046
Chris Mason5f39d392007-10-15 16:14:19 -04004047 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004048 ioff = btrfs_token_item_offset(leaf, item, &token);
4049 btrfs_set_token_item_offset(leaf, item,
4050 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004051 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004052 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004053 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004054 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004055 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004056
4057 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004058 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004059 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004060 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004061 data_end = old_data;
4062 }
Chris Mason5f39d392007-10-15 16:14:19 -04004063
Chris Mason62e27492007-03-15 12:56:47 -04004064 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004065 for (i = 0; i < nr; i++) {
4066 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4067 btrfs_set_item_key(leaf, &disk_key, slot + i);
4068 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004069 btrfs_set_token_item_offset(leaf, item,
4070 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004071 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004072 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004073 }
Chris Mason44871b12009-03-13 10:04:31 -04004074
Chris Mason9c583092008-01-29 15:15:18 -05004075 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004076
Chris Mason5a01a2e2008-01-30 11:43:54 -05004077 if (slot == 0) {
4078 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004079 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004080 }
Chris Masonb9473432009-03-13 11:00:37 -04004081 btrfs_unlock_up_safe(path, 1);
4082 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004083
Chris Mason5f39d392007-10-15 16:14:19 -04004084 if (btrfs_leaf_free_space(root, leaf) < 0) {
4085 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004086 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004087 }
Chris Mason44871b12009-03-13 10:04:31 -04004088}
4089
4090/*
4091 * Given a key and some data, insert items into the tree.
4092 * This does all the path init required, making room in the tree if needed.
4093 */
4094int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4095 struct btrfs_root *root,
4096 struct btrfs_path *path,
4097 struct btrfs_key *cpu_key, u32 *data_size,
4098 int nr)
4099{
Chris Mason44871b12009-03-13 10:04:31 -04004100 int ret = 0;
4101 int slot;
4102 int i;
4103 u32 total_size = 0;
4104 u32 total_data = 0;
4105
4106 for (i = 0; i < nr; i++)
4107 total_data += data_size[i];
4108
4109 total_size = total_data + (nr * sizeof(struct btrfs_item));
4110 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4111 if (ret == 0)
4112 return -EEXIST;
4113 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004114 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004115
Chris Mason44871b12009-03-13 10:04:31 -04004116 slot = path->slots[0];
4117 BUG_ON(slot < 0);
4118
Jeff Mahoney143bede2012-03-01 14:56:26 +01004119 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004120 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004121 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004122}
4123
4124/*
4125 * Given a key and some data, insert an item into the tree.
4126 * This does all the path init required, making room in the tree if needed.
4127 */
Chris Masone089f052007-03-16 16:20:31 -04004128int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4129 *root, struct btrfs_key *cpu_key, void *data, u32
4130 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004131{
4132 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004133 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004134 struct extent_buffer *leaf;
4135 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004136
Chris Mason2c90e5d2007-04-02 10:50:19 -04004137 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004138 if (!path)
4139 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004140 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004141 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004142 leaf = path->nodes[0];
4143 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4144 write_extent_buffer(leaf, data, ptr, data_size);
4145 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004146 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004147 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004148 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004149}
4150
Chris Mason74123bd2007-02-02 11:05:29 -05004151/*
Chris Mason5de08d72007-02-24 06:24:44 -05004152 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004153 *
Chris Masond352ac62008-09-29 15:18:18 -04004154 * the tree should have been previously balanced so the deletion does not
4155 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004156 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004157static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4158 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004159{
Chris Mason5f39d392007-10-15 16:14:19 -04004160 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004161 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004162
Chris Mason5f39d392007-10-15 16:14:19 -04004163 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004164 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004165 memmove_extent_buffer(parent,
4166 btrfs_node_key_ptr_offset(slot),
4167 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004168 sizeof(struct btrfs_key_ptr) *
4169 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05004170 }
Chris Mason7518a232007-03-12 12:01:18 -04004171 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004172 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004173 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004174 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004175 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004176 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004177 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004178 struct btrfs_disk_key disk_key;
4179
4180 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004181 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004182 }
Chris Masond6025572007-03-30 14:27:56 -04004183 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004184}
4185
Chris Mason74123bd2007-02-02 11:05:29 -05004186/*
Chris Mason323ac952008-10-01 19:05:46 -04004187 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004188 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004189 *
4190 * This deletes the pointer in path->nodes[1] and frees the leaf
4191 * block extent. zero is returned if it all worked out, < 0 otherwise.
4192 *
4193 * The path must have already been setup for deleting the leaf, including
4194 * all the proper balancing. path->nodes[1] must be locked.
4195 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004196static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4197 struct btrfs_root *root,
4198 struct btrfs_path *path,
4199 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004200{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004201 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004202 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004203
Chris Mason4d081c42009-02-04 09:31:28 -05004204 /*
4205 * btrfs_free_extent is expensive, we want to make sure we
4206 * aren't holding any locks when we call it
4207 */
4208 btrfs_unlock_up_safe(path, 0);
4209
Yan, Zhengf0486c62010-05-16 10:46:25 -04004210 root_sub_used(root, leaf->len);
4211
Josef Bacik3083ee22012-03-09 16:01:49 -05004212 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004213 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004214 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004215}
4216/*
Chris Mason74123bd2007-02-02 11:05:29 -05004217 * delete the item at the leaf level in path. If that empties
4218 * the leaf, remove it from the tree
4219 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004220int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4221 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004222{
Chris Mason5f39d392007-10-15 16:14:19 -04004223 struct extent_buffer *leaf;
4224 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004225 int last_off;
4226 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004227 int ret = 0;
4228 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004229 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004230 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004231 struct btrfs_map_token token;
4232
4233 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004234
Chris Mason5f39d392007-10-15 16:14:19 -04004235 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004236 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4237
4238 for (i = 0; i < nr; i++)
4239 dsize += btrfs_item_size_nr(leaf, slot + i);
4240
Chris Mason5f39d392007-10-15 16:14:19 -04004241 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004242
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004244 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004245
4246 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004247 data_end + dsize,
4248 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004249 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004250
Chris Mason85e21ba2008-01-29 15:11:36 -05004251 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004252 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004253
Chris Mason5f39d392007-10-15 16:14:19 -04004254 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004255 ioff = btrfs_token_item_offset(leaf, item, &token);
4256 btrfs_set_token_item_offset(leaf, item,
4257 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004258 }
Chris Masondb945352007-10-15 16:15:53 -04004259
Chris Mason5f39d392007-10-15 16:14:19 -04004260 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004261 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004262 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004263 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004264 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004265 btrfs_set_header_nritems(leaf, nritems - nr);
4266 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004267
Chris Mason74123bd2007-02-02 11:05:29 -05004268 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004269 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004270 if (leaf == root->node) {
4271 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004272 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004273 btrfs_set_path_blocking(path);
4274 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004275 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004276 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004277 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004278 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004279 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004280 struct btrfs_disk_key disk_key;
4281
4282 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004283 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004284 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004285
Chris Mason74123bd2007-02-02 11:05:29 -05004286 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004287 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004288 /* push_leaf_left fixes the path.
4289 * make sure the path still points to our leaf
4290 * for possible call to del_ptr below
4291 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004292 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004293 extent_buffer_get(leaf);
4294
Chris Masonb9473432009-03-13 11:00:37 -04004295 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004296 wret = push_leaf_left(trans, root, path, 1, 1,
4297 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004298 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004299 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004300
4301 if (path->nodes[0] == leaf &&
4302 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004303 wret = push_leaf_right(trans, root, path, 1,
4304 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004305 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004306 ret = wret;
4307 }
Chris Mason5f39d392007-10-15 16:14:19 -04004308
4309 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004310 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004311 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004312 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004313 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004314 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004315 /* if we're still in the path, make sure
4316 * we're dirty. Otherwise, one of the
4317 * push_leaf functions must have already
4318 * dirtied this buffer
4319 */
4320 if (path->nodes[0] == leaf)
4321 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004322 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004323 }
Chris Masond5719762007-03-23 10:01:08 -04004324 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004325 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004326 }
4327 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004328 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004329}
4330
Chris Mason97571fd2007-02-24 13:39:08 -05004331/*
Chris Mason925baed2008-06-25 16:01:30 -04004332 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004333 * returns 0 if it found something or 1 if there are no lesser leaves.
4334 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004335 *
4336 * This may release the path, and so you may lose any locks held at the
4337 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004338 */
4339int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4340{
Chris Mason925baed2008-06-25 16:01:30 -04004341 struct btrfs_key key;
4342 struct btrfs_disk_key found_key;
4343 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004344
Chris Mason925baed2008-06-25 16:01:30 -04004345 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004346
Chris Mason925baed2008-06-25 16:01:30 -04004347 if (key.offset > 0)
4348 key.offset--;
4349 else if (key.type > 0)
4350 key.type--;
4351 else if (key.objectid > 0)
4352 key.objectid--;
4353 else
4354 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004355
David Sterbab3b4aa72011-04-21 01:20:15 +02004356 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004357 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4358 if (ret < 0)
4359 return ret;
4360 btrfs_item_key(path->nodes[0], &found_key, 0);
4361 ret = comp_keys(&found_key, &key);
4362 if (ret < 0)
4363 return 0;
4364 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004365}
4366
Chris Mason3f157a22008-06-25 16:01:31 -04004367/*
4368 * A helper function to walk down the tree starting at min_key, and looking
4369 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004370 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004371 *
4372 * This does not cow, but it does stuff the starting key it finds back
4373 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4374 * key and get a writable path.
4375 *
4376 * This does lock as it descends, and path->keep_locks should be set
4377 * to 1 by the caller.
4378 *
4379 * This honors path->lowest_level to prevent descent past a given level
4380 * of the tree.
4381 *
Chris Masond352ac62008-09-29 15:18:18 -04004382 * min_trans indicates the oldest transaction that you are interested
4383 * in walking through. Any nodes or leaves older than min_trans are
4384 * skipped over (without reading them).
4385 *
Chris Mason3f157a22008-06-25 16:01:31 -04004386 * returns zero if something useful was found, < 0 on error and 1 if there
4387 * was nothing in the tree that matched the search criteria.
4388 */
4389int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004390 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004391 struct btrfs_path *path, int cache_only,
4392 u64 min_trans)
4393{
4394 struct extent_buffer *cur;
4395 struct btrfs_key found_key;
4396 int slot;
Yan96524802008-07-24 12:19:49 -04004397 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004398 u32 nritems;
4399 int level;
4400 int ret = 1;
4401
Chris Mason934d3752008-12-08 16:43:10 -05004402 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004403again:
Chris Masonbd681512011-07-16 15:23:14 -04004404 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004405 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004406 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004407 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004408 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004409
4410 if (btrfs_header_generation(cur) < min_trans) {
4411 ret = 1;
4412 goto out;
4413 }
Chris Masond3977122009-01-05 21:25:51 -05004414 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004415 nritems = btrfs_header_nritems(cur);
4416 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004417 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004418
Chris Mason323ac952008-10-01 19:05:46 -04004419 /* at the lowest level, we're done, setup the path and exit */
4420 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004421 if (slot >= nritems)
4422 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004423 ret = 0;
4424 path->slots[level] = slot;
4425 btrfs_item_key_to_cpu(cur, &found_key, slot);
4426 goto out;
4427 }
Yan96524802008-07-24 12:19:49 -04004428 if (sret && slot > 0)
4429 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004430 /*
4431 * check this node pointer against the cache_only and
4432 * min_trans parameters. If it isn't in cache or is too
4433 * old, skip to the next one.
4434 */
Chris Masond3977122009-01-05 21:25:51 -05004435 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004436 u64 blockptr;
4437 u64 gen;
4438 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004439 struct btrfs_disk_key disk_key;
4440
Chris Mason3f157a22008-06-25 16:01:31 -04004441 blockptr = btrfs_node_blockptr(cur, slot);
4442 gen = btrfs_node_ptr_generation(cur, slot);
4443 if (gen < min_trans) {
4444 slot++;
4445 continue;
4446 }
4447 if (!cache_only)
4448 break;
4449
Chris Masone02119d2008-09-05 16:13:11 -04004450 if (max_key) {
4451 btrfs_node_key(cur, &disk_key, slot);
4452 if (comp_keys(&disk_key, max_key) >= 0) {
4453 ret = 1;
4454 goto out;
4455 }
4456 }
4457
Chris Mason3f157a22008-06-25 16:01:31 -04004458 tmp = btrfs_find_tree_block(root, blockptr,
4459 btrfs_level_size(root, level - 1));
4460
Chris Masonb9fab912012-05-06 07:23:47 -04004461 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004462 free_extent_buffer(tmp);
4463 break;
4464 }
4465 if (tmp)
4466 free_extent_buffer(tmp);
4467 slot++;
4468 }
Chris Masone02119d2008-09-05 16:13:11 -04004469find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004470 /*
4471 * we didn't find a candidate key in this node, walk forward
4472 * and find another one
4473 */
4474 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004475 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004476 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004477 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004478 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004479 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004480 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004481 goto again;
4482 } else {
4483 goto out;
4484 }
4485 }
4486 /* save our key for returning back */
4487 btrfs_node_key_to_cpu(cur, &found_key, slot);
4488 path->slots[level] = slot;
4489 if (level == path->lowest_level) {
4490 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004491 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004492 goto out;
4493 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004494 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004495 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004496 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004497
Chris Masonbd681512011-07-16 15:23:14 -04004498 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004499
Chris Masonbd681512011-07-16 15:23:14 -04004500 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004501 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004502 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004503 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004504 }
4505out:
4506 if (ret == 0)
4507 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004508 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004509 return ret;
4510}
4511
4512/*
4513 * this is similar to btrfs_next_leaf, but does not try to preserve
4514 * and fixup the path. It looks for and returns the next key in the
4515 * tree based on the current path and the cache_only and min_trans
4516 * parameters.
4517 *
4518 * 0 is returned if another key is found, < 0 if there are any errors
4519 * and 1 is returned if there are no higher keys in the tree
4520 *
4521 * path->keep_locks should be set to 1 on the search made before
4522 * calling this function.
4523 */
Chris Masone7a84562008-06-25 16:01:31 -04004524int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004525 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004526 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004527{
Chris Masone7a84562008-06-25 16:01:31 -04004528 int slot;
4529 struct extent_buffer *c;
4530
Chris Mason934d3752008-12-08 16:43:10 -05004531 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004532 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004533 if (!path->nodes[level])
4534 return 1;
4535
4536 slot = path->slots[level] + 1;
4537 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004538next:
Chris Masone7a84562008-06-25 16:01:31 -04004539 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004540 int ret;
4541 int orig_lowest;
4542 struct btrfs_key cur_key;
4543 if (level + 1 >= BTRFS_MAX_LEVEL ||
4544 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004545 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004546
4547 if (path->locks[level + 1]) {
4548 level++;
4549 continue;
4550 }
4551
4552 slot = btrfs_header_nritems(c) - 1;
4553 if (level == 0)
4554 btrfs_item_key_to_cpu(c, &cur_key, slot);
4555 else
4556 btrfs_node_key_to_cpu(c, &cur_key, slot);
4557
4558 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004559 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004560 path->lowest_level = level;
4561 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4562 0, 0);
4563 path->lowest_level = orig_lowest;
4564 if (ret < 0)
4565 return ret;
4566
4567 c = path->nodes[level];
4568 slot = path->slots[level];
4569 if (ret == 0)
4570 slot++;
4571 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004572 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004573
Chris Masone7a84562008-06-25 16:01:31 -04004574 if (level == 0)
4575 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004576 else {
4577 u64 blockptr = btrfs_node_blockptr(c, slot);
4578 u64 gen = btrfs_node_ptr_generation(c, slot);
4579
4580 if (cache_only) {
4581 struct extent_buffer *cur;
4582 cur = btrfs_find_tree_block(root, blockptr,
4583 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004584 if (!cur ||
4585 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004586 slot++;
4587 if (cur)
4588 free_extent_buffer(cur);
4589 goto next;
4590 }
4591 free_extent_buffer(cur);
4592 }
4593 if (gen < min_trans) {
4594 slot++;
4595 goto next;
4596 }
Chris Masone7a84562008-06-25 16:01:31 -04004597 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004598 }
Chris Masone7a84562008-06-25 16:01:31 -04004599 return 0;
4600 }
4601 return 1;
4602}
4603
Chris Mason7bb86312007-12-11 09:25:06 -05004604/*
Chris Mason925baed2008-06-25 16:01:30 -04004605 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004606 * returns 0 if it found something or 1 if there are no greater leaves.
4607 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004608 */
Chris Mason234b63a2007-03-13 10:46:10 -04004609int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004610{
4611 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004612 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004613 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004614 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004615 struct btrfs_key key;
4616 u32 nritems;
4617 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004618 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004619 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004620
4621 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004622 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004623 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004624
Chris Mason8e73f272009-04-03 10:14:18 -04004625 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4626again:
4627 level = 1;
4628 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004629 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004630 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004631
Chris Masona2135012008-06-25 16:01:30 -04004632 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004633 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004634
Chris Mason925baed2008-06-25 16:01:30 -04004635 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4636 path->keep_locks = 0;
4637
4638 if (ret < 0)
4639 return ret;
4640
Chris Masona2135012008-06-25 16:01:30 -04004641 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004642 /*
4643 * by releasing the path above we dropped all our locks. A balance
4644 * could have added more items next to the key that used to be
4645 * at the very end of the block. So, check again here and
4646 * advance the path if there are now more items available.
4647 */
Chris Masona2135012008-06-25 16:01:30 -04004648 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004649 if (ret == 0)
4650 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004651 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004652 goto done;
4653 }
Chris Masond97e63b2007-02-20 16:40:44 -05004654
Chris Masond3977122009-01-05 21:25:51 -05004655 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004656 if (!path->nodes[level]) {
4657 ret = 1;
4658 goto done;
4659 }
Chris Mason5f39d392007-10-15 16:14:19 -04004660
Chris Masond97e63b2007-02-20 16:40:44 -05004661 slot = path->slots[level] + 1;
4662 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004663 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004664 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004665 if (level == BTRFS_MAX_LEVEL) {
4666 ret = 1;
4667 goto done;
4668 }
Chris Masond97e63b2007-02-20 16:40:44 -05004669 continue;
4670 }
Chris Mason5f39d392007-10-15 16:14:19 -04004671
Chris Mason925baed2008-06-25 16:01:30 -04004672 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004673 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004674 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004675 }
Chris Mason5f39d392007-10-15 16:14:19 -04004676
Chris Mason8e73f272009-04-03 10:14:18 -04004677 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004678 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004679 ret = read_block_for_search(NULL, root, path, &next, level,
4680 slot, &key);
4681 if (ret == -EAGAIN)
4682 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004683
Chris Mason76a05b32009-05-14 13:24:30 -04004684 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004685 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004686 goto done;
4687 }
4688
Chris Mason5cd57b22008-06-25 16:01:30 -04004689 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004690 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004691 if (!ret) {
4692 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004693 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004694 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004695 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004696 }
Chris Mason31533fb2011-07-26 16:01:59 -04004697 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004698 }
Chris Masond97e63b2007-02-20 16:40:44 -05004699 break;
4700 }
4701 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004702 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004703 level--;
4704 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004705 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004706 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004707
Chris Mason5f39d392007-10-15 16:14:19 -04004708 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004709 path->nodes[level] = next;
4710 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004711 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004712 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004713 if (!level)
4714 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004715
Chris Mason8e73f272009-04-03 10:14:18 -04004716 ret = read_block_for_search(NULL, root, path, &next, level,
4717 0, &key);
4718 if (ret == -EAGAIN)
4719 goto again;
4720
Chris Mason76a05b32009-05-14 13:24:30 -04004721 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004722 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004723 goto done;
4724 }
4725
Chris Mason5cd57b22008-06-25 16:01:30 -04004726 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004727 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004728 if (!ret) {
4729 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004730 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004731 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004732 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004733 }
Chris Mason31533fb2011-07-26 16:01:59 -04004734 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004735 }
Chris Masond97e63b2007-02-20 16:40:44 -05004736 }
Chris Mason8e73f272009-04-03 10:14:18 -04004737 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004738done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004739 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004740 path->leave_spinning = old_spinning;
4741 if (!old_spinning)
4742 btrfs_set_path_blocking(path);
4743
4744 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004745}
Chris Mason0b86a832008-03-24 15:01:56 -04004746
Chris Mason3f157a22008-06-25 16:01:31 -04004747/*
4748 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4749 * searching until it gets past min_objectid or finds an item of 'type'
4750 *
4751 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4752 */
Chris Mason0b86a832008-03-24 15:01:56 -04004753int btrfs_previous_item(struct btrfs_root *root,
4754 struct btrfs_path *path, u64 min_objectid,
4755 int type)
4756{
4757 struct btrfs_key found_key;
4758 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004759 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004760 int ret;
4761
Chris Masond3977122009-01-05 21:25:51 -05004762 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004763 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004764 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004765 ret = btrfs_prev_leaf(root, path);
4766 if (ret != 0)
4767 return ret;
4768 } else {
4769 path->slots[0]--;
4770 }
4771 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004772 nritems = btrfs_header_nritems(leaf);
4773 if (nritems == 0)
4774 return 1;
4775 if (path->slots[0] == nritems)
4776 path->slots[0]--;
4777
Chris Mason0b86a832008-03-24 15:01:56 -04004778 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004779 if (found_key.objectid < min_objectid)
4780 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004781 if (found_key.type == type)
4782 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004783 if (found_key.objectid == min_objectid &&
4784 found_key.type < type)
4785 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004786 }
4787 return 1;
4788}