blob: 69ce3f7deeef5f508e609ce151f1582d71e1989e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +020041 struct btrfs_path *path, int level, int slot,
42 int tree_mod_log);
Jan Schmidtf2304752012-05-26 11:43:17 +020043static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
47 u64 time_seq);
48struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
50 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050051
Chris Mason2c90e5d2007-04-02 10:50:19 -040052struct btrfs_path *btrfs_alloc_path(void)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050055 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040056 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 }
75}
76
77/*
78 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050079 *
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
83 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050084 */
Chris Mason4008c042009-02-12 14:09:45 -050085noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040086 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050087{
88 int i;
Chris Mason4008c042009-02-12 14:09:45 -050089
90#ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
95 * the path blocking.
96 */
Chris Masonbd681512011-07-16 15:23:14 -040097 if (held) {
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
103 }
Chris Mason4008c042009-02-12 14:09:45 -0500104 btrfs_set_path_blocking(p);
105#endif
106
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
114 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115 }
Chris Mason4008c042009-02-12 14:09:45 -0500116
117#ifdef CONFIG_DEBUG_LOCK_ALLOC
118 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400119 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500120#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400124void btrfs_free_path(struct btrfs_path *p)
125{
Jesper Juhlff175d52010-12-25 21:22:30 +0000126 if (!p)
127 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200128 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400129 kmem_cache_free(btrfs_path_cachep, p);
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
135 *
136 * It is safe to call this on paths that no locks or extent buffers held.
137 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200138noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500139{
140 int i;
Chris Masona2135012008-06-25 16:01:30 -0400141
Chris Mason234b63a2007-03-13 10:46:10 -0400142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400143 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400145 continue;
146 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400148 p->locks[i] = 0;
149 }
Chris Mason5f39d392007-10-15 16:14:19 -0400150 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400151 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
159 * looping required.
160 *
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
164 */
Chris Mason925baed2008-06-25 16:01:30 -0400165struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166{
167 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400168
Josef Bacik3083ee22012-03-09 16:01:49 -0500169 while (1) {
170 rcu_read_lock();
171 eb = rcu_dereference(root->node);
172
173 /*
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
178 */
179 if (atomic_inc_not_zero(&eb->refs)) {
180 rcu_read_unlock();
181 break;
182 }
183 rcu_read_unlock();
184 synchronize_rcu();
185 }
Chris Mason925baed2008-06-25 16:01:30 -0400186 return eb;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Chris Mason925baed2008-06-25 16:01:30 -0400193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194{
195 struct extent_buffer *eb;
196
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400198 eb = btrfs_root_node(root);
199 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400200 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400201 break;
Chris Mason925baed2008-06-25 16:01:30 -0400202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masonbd681512011-07-16 15:23:14 -0400208/* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
211 */
212struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213{
214 struct extent_buffer *eb;
215
216 while (1) {
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
220 break;
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
223 }
224 return eb;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
230 */
Chris Mason0b86a832008-03-24 15:01:56 -0400231static void add_root_to_dirty_list(struct btrfs_root *root)
232{
Chris Masone5846fc2012-05-03 12:08:48 -0400233 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
237 }
Chris Masone5846fc2012-05-03 12:08:48 -0400238 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400239}
240
Chris Masond352ac62008-09-29 15:18:18 -0400241/*
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
245 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500246int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
250{
251 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 int ret = 0;
253 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259
260 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 if (level == 0)
262 btrfs_item_key(buf, &disk_key, 0);
263 else
264 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200268 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 return PTR_ERR(cow);
271
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
280 else
281 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500282
Yan Zheng2b820322008-11-17 21:11:30 -0500283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
285 BTRFS_FSID_SIZE);
286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500292
Chris Masonbe20aa92007-12-17 20:14:01 -0500293 if (ret)
294 return ret;
295
296 btrfs_mark_buffer_dirty(cow);
297 *cow_ret = cow;
298 return 0;
299}
300
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200301enum mod_log_op {
302 MOD_LOG_KEY_REPLACE,
303 MOD_LOG_KEY_ADD,
304 MOD_LOG_KEY_REMOVE,
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307 MOD_LOG_MOVE_KEYS,
308 MOD_LOG_ROOT_REPLACE,
309};
310
311struct tree_mod_move {
312 int dst_slot;
313 int nr_items;
314};
315
316struct tree_mod_root {
317 u64 logical;
318 u8 level;
319};
320
321struct tree_mod_elem {
322 struct rb_node node;
323 u64 index; /* shifted logical */
324 struct seq_list elem;
325 enum mod_log_op op;
326
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328 int slot;
329
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331 u64 generation;
332
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
335 u64 blockptr;
336
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
339
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
342};
343
344static inline void
345__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
346{
347 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
349}
350
351void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 elem->flags = 1;
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 __get_tree_mod_seq(fs_info, elem);
357 spin_unlock(&fs_info->tree_mod_seq_lock);
358}
359
360void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361 struct seq_list *elem)
362{
363 struct rb_root *tm_root;
364 struct rb_node *node;
365 struct rb_node *next;
366 struct seq_list *cur_elem;
367 struct tree_mod_elem *tm;
368 u64 min_seq = (u64)-1;
369 u64 seq_putting = elem->seq;
370
371 if (!seq_putting)
372 return;
373
374 BUG_ON(!(elem->flags & 1));
375 spin_lock(&fs_info->tree_mod_seq_lock);
376 list_del(&elem->list);
377
378 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380 if (seq_putting > cur_elem->seq) {
381 /*
382 * blocker with lower sequence number exists, we
383 * cannot remove anything from the log
384 */
385 goto out;
386 }
387 min_seq = cur_elem->seq;
388 }
389 }
390
391 /*
392 * anything that's lower than the lowest existing (read: blocked)
393 * sequence number can be removed from the tree.
394 */
395 write_lock(&fs_info->tree_mod_log_lock);
396 tm_root = &fs_info->tree_mod_log;
397 for (node = rb_first(tm_root); node; node = next) {
398 next = rb_next(node);
399 tm = container_of(node, struct tree_mod_elem, node);
400 if (tm->elem.seq > min_seq)
401 continue;
402 rb_erase(node, tm_root);
403 list_del(&tm->elem.list);
404 kfree(tm);
405 }
406 write_unlock(&fs_info->tree_mod_log_lock);
407out:
408 spin_unlock(&fs_info->tree_mod_seq_lock);
409}
410
411/*
412 * key order of the log:
413 * index -> sequence
414 *
415 * the index is the shifted logical of the *new* root node for root replace
416 * operations, or the shifted logical of the affected block for all other
417 * operations.
418 */
419static noinline int
420__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
421{
422 struct rb_root *tm_root;
423 struct rb_node **new;
424 struct rb_node *parent = NULL;
425 struct tree_mod_elem *cur;
426 int ret = 0;
427
428 BUG_ON(!tm || !tm->elem.seq);
429
430 write_lock(&fs_info->tree_mod_log_lock);
431 tm_root = &fs_info->tree_mod_log;
432 new = &tm_root->rb_node;
433 while (*new) {
434 cur = container_of(*new, struct tree_mod_elem, node);
435 parent = *new;
436 if (cur->index < tm->index)
437 new = &((*new)->rb_left);
438 else if (cur->index > tm->index)
439 new = &((*new)->rb_right);
440 else if (cur->elem.seq < tm->elem.seq)
441 new = &((*new)->rb_left);
442 else if (cur->elem.seq > tm->elem.seq)
443 new = &((*new)->rb_right);
444 else {
445 kfree(tm);
446 ret = -EEXIST;
447 goto unlock;
448 }
449 }
450
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
453unlock:
454 write_unlock(&fs_info->tree_mod_log_lock);
455 return ret;
456}
457
458int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
459 struct tree_mod_elem **tm_ret)
460{
461 struct tree_mod_elem *tm;
462 u64 seq = 0;
463
464 smp_mb();
465 if (list_empty(&fs_info->tree_mod_seq_list))
466 return 0;
467
468 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
469 if (!tm)
470 return -ENOMEM;
471
472 __get_tree_mod_seq(fs_info, &tm->elem);
473 seq = tm->elem.seq;
474 tm->elem.flags = 0;
475
476 return seq;
477}
478
479static noinline int
480tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
481 struct extent_buffer *eb, int slot,
482 enum mod_log_op op, gfp_t flags)
483{
484 struct tree_mod_elem *tm;
485 int ret;
486
487 ret = tree_mod_alloc(fs_info, flags, &tm);
488 if (ret <= 0)
489 return ret;
490
491 tm->index = eb->start >> PAGE_CACHE_SHIFT;
492 if (op != MOD_LOG_KEY_ADD) {
493 btrfs_node_key(eb, &tm->key, slot);
494 tm->blockptr = btrfs_node_blockptr(eb, slot);
495 }
496 tm->op = op;
497 tm->slot = slot;
498 tm->generation = btrfs_node_ptr_generation(eb, slot);
499
500 return __tree_mod_log_insert(fs_info, tm);
501}
502
503static noinline int
504tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
505 int slot, enum mod_log_op op)
506{
507 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
508}
509
510static noinline int
511tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
512 struct extent_buffer *eb, int dst_slot, int src_slot,
513 int nr_items, gfp_t flags)
514{
515 struct tree_mod_elem *tm;
516 int ret;
517 int i;
518
519 ret = tree_mod_alloc(fs_info, flags, &tm);
520 if (ret <= 0)
521 return ret;
522
523 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
524 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
525 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
526 BUG_ON(ret < 0);
527 }
528
529 tm->index = eb->start >> PAGE_CACHE_SHIFT;
530 tm->slot = src_slot;
531 tm->move.dst_slot = dst_slot;
532 tm->move.nr_items = nr_items;
533 tm->op = MOD_LOG_MOVE_KEYS;
534
535 return __tree_mod_log_insert(fs_info, tm);
536}
537
538static noinline int
539tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
540 struct extent_buffer *old_root,
541 struct extent_buffer *new_root, gfp_t flags)
542{
543 struct tree_mod_elem *tm;
544 int ret;
545
546 ret = tree_mod_alloc(fs_info, flags, &tm);
547 if (ret <= 0)
548 return ret;
549
550 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
551 tm->old_root.logical = old_root->start;
552 tm->old_root.level = btrfs_header_level(old_root);
553 tm->generation = btrfs_header_generation(old_root);
554 tm->op = MOD_LOG_ROOT_REPLACE;
555
556 return __tree_mod_log_insert(fs_info, tm);
557}
558
559static struct tree_mod_elem *
560__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
561 int smallest)
562{
563 struct rb_root *tm_root;
564 struct rb_node *node;
565 struct tree_mod_elem *cur = NULL;
566 struct tree_mod_elem *found = NULL;
567 u64 index = start >> PAGE_CACHE_SHIFT;
568
569 read_lock(&fs_info->tree_mod_log_lock);
570 tm_root = &fs_info->tree_mod_log;
571 node = tm_root->rb_node;
572 while (node) {
573 cur = container_of(node, struct tree_mod_elem, node);
574 if (cur->index < index) {
575 node = node->rb_left;
576 } else if (cur->index > index) {
577 node = node->rb_right;
578 } else if (cur->elem.seq < min_seq) {
579 node = node->rb_left;
580 } else if (!smallest) {
581 /* we want the node with the highest seq */
582 if (found)
583 BUG_ON(found->elem.seq > cur->elem.seq);
584 found = cur;
585 node = node->rb_left;
586 } else if (cur->elem.seq > min_seq) {
587 /* we want the node with the smallest seq */
588 if (found)
589 BUG_ON(found->elem.seq < cur->elem.seq);
590 found = cur;
591 node = node->rb_right;
592 } else {
593 found = cur;
594 break;
595 }
596 }
597 read_unlock(&fs_info->tree_mod_log_lock);
598
599 return found;
600}
601
602/*
603 * this returns the element from the log with the smallest time sequence
604 * value that's in the log (the oldest log item). any element with a time
605 * sequence lower than min_seq will be ignored.
606 */
607static struct tree_mod_elem *
608tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
609 u64 min_seq)
610{
611 return __tree_mod_log_search(fs_info, start, min_seq, 1);
612}
613
614/*
615 * this returns the element from the log with the largest time sequence
616 * value that's in the log (the most recent log item). any element with
617 * a time sequence lower than min_seq will be ignored.
618 */
619static struct tree_mod_elem *
620tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
621{
622 return __tree_mod_log_search(fs_info, start, min_seq, 0);
623}
624
625static inline void
626tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
627 struct extent_buffer *src, unsigned long dst_offset,
628 unsigned long src_offset, int nr_items)
629{
630 int ret;
631 int i;
632
633 smp_mb();
634 if (list_empty(&fs_info->tree_mod_seq_list))
635 return;
636
637 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
638 return;
639
640 /* speed this up by single seq for all operations? */
641 for (i = 0; i < nr_items; i++) {
642 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
643 MOD_LOG_KEY_REMOVE);
644 BUG_ON(ret < 0);
645 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
646 MOD_LOG_KEY_ADD);
647 BUG_ON(ret < 0);
648 }
649}
650
651static inline void
652tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
653 int dst_offset, int src_offset, int nr_items)
654{
655 int ret;
656 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
657 nr_items, GFP_NOFS);
658 BUG_ON(ret < 0);
659}
660
661static inline void
662tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
663 struct extent_buffer *eb,
664 struct btrfs_disk_key *disk_key, int slot, int atomic)
665{
666 int ret;
667
668 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
669 MOD_LOG_KEY_REPLACE,
670 atomic ? GFP_ATOMIC : GFP_NOFS);
671 BUG_ON(ret < 0);
672}
673
674static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
675 struct extent_buffer *eb)
676{
677 int i;
678 int ret;
679 u32 nritems;
680
681 smp_mb();
682 if (list_empty(&fs_info->tree_mod_seq_list))
683 return;
684
685 if (btrfs_header_level(eb) == 0)
686 return;
687
688 nritems = btrfs_header_nritems(eb);
689 for (i = nritems - 1; i >= 0; i--) {
690 ret = tree_mod_log_insert_key(fs_info, eb, i,
691 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
692 BUG_ON(ret < 0);
693 }
694}
695
696static inline void
697tree_mod_log_set_root_pointer(struct btrfs_root *root,
698 struct extent_buffer *new_root_node)
699{
700 int ret;
701 tree_mod_log_free_eb(root->fs_info, root->node);
702 ret = tree_mod_log_insert_root(root->fs_info, root->node,
703 new_root_node, GFP_NOFS);
704 BUG_ON(ret < 0);
705}
706
Chris Masond352ac62008-09-29 15:18:18 -0400707/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400708 * check if the tree block can be shared by multiple trees
709 */
710int btrfs_block_can_be_shared(struct btrfs_root *root,
711 struct extent_buffer *buf)
712{
713 /*
714 * Tree blocks not in refernece counted trees and tree roots
715 * are never shared. If a block was allocated after the last
716 * snapshot and the block was not allocated by tree relocation,
717 * we know the block is not shared.
718 */
719 if (root->ref_cows &&
720 buf != root->node && buf != root->commit_root &&
721 (btrfs_header_generation(buf) <=
722 btrfs_root_last_snapshot(&root->root_item) ||
723 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
724 return 1;
725#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
726 if (root->ref_cows &&
727 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
728 return 1;
729#endif
730 return 0;
731}
732
733static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
734 struct btrfs_root *root,
735 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400736 struct extent_buffer *cow,
737 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400738{
739 u64 refs;
740 u64 owner;
741 u64 flags;
742 u64 new_flags = 0;
743 int ret;
744
745 /*
746 * Backrefs update rules:
747 *
748 * Always use full backrefs for extent pointers in tree block
749 * allocated by tree relocation.
750 *
751 * If a shared tree block is no longer referenced by its owner
752 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
753 * use full backrefs for extent pointers in tree block.
754 *
755 * If a tree block is been relocating
756 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
757 * use full backrefs for extent pointers in tree block.
758 * The reason for this is some operations (such as drop tree)
759 * are only allowed for blocks use full backrefs.
760 */
761
762 if (btrfs_block_can_be_shared(root, buf)) {
763 ret = btrfs_lookup_extent_info(trans, root, buf->start,
764 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700765 if (ret)
766 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700767 if (refs == 0) {
768 ret = -EROFS;
769 btrfs_std_error(root->fs_info, ret);
770 return ret;
771 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400772 } else {
773 refs = 1;
774 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
775 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
776 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
777 else
778 flags = 0;
779 }
780
781 owner = btrfs_header_owner(buf);
782 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
783 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
784
785 if (refs > 1) {
786 if ((owner == root->root_key.objectid ||
787 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
788 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200789 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100790 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791
792 if (root->root_key.objectid ==
793 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200794 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200796 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400798 }
799 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
800 } else {
801
802 if (root->root_key.objectid ==
803 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200804 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400805 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200806 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100807 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400808 }
809 if (new_flags != 0) {
810 ret = btrfs_set_disk_extent_flags(trans, root,
811 buf->start,
812 buf->len,
813 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700814 if (ret)
815 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400816 }
817 } else {
818 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
819 if (root->root_key.objectid ==
820 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200821 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400822 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200823 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100824 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200825 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100826 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200828 /*
829 * don't log freeing in case we're freeing the root node, this
830 * is done by tree_mod_log_set_root_pointer later
831 */
832 if (buf != root->node && btrfs_header_level(buf) != 0)
833 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400834 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400835 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400836 }
837 return 0;
838}
839
840/*
Chris Masond3977122009-01-05 21:25:51 -0500841 * does the dirty work in cow of a single block. The parent block (if
842 * supplied) is updated to point to the new cow copy. The new buffer is marked
843 * dirty and returned locked. If you modify the block it needs to be marked
844 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400845 *
846 * search_start -- an allocation hint for the new block
847 *
Chris Masond3977122009-01-05 21:25:51 -0500848 * empty_size -- a hint that you plan on doing more cow. This is the size in
849 * bytes the allocator should try to find free next to the block it returns.
850 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400851 */
Chris Masond3977122009-01-05 21:25:51 -0500852static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400853 struct btrfs_root *root,
854 struct extent_buffer *buf,
855 struct extent_buffer *parent, int parent_slot,
856 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400857 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400858{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400859 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400860 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700861 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400862 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400863 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400864 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400865
Chris Mason925baed2008-06-25 16:01:30 -0400866 if (*cow_ret == buf)
867 unlock_orig = 1;
868
Chris Masonb9447ef82009-03-09 11:45:38 -0400869 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400870
Chris Mason7bb86312007-12-11 09:25:06 -0500871 WARN_ON(root->ref_cows && trans->transid !=
872 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400873 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400874
Chris Mason7bb86312007-12-11 09:25:06 -0500875 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400876
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400877 if (level == 0)
878 btrfs_item_key(buf, &disk_key, 0);
879 else
880 btrfs_node_key(buf, &disk_key, 0);
881
882 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
883 if (parent)
884 parent_start = parent->start;
885 else
886 parent_start = 0;
887 } else
888 parent_start = 0;
889
890 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
891 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200892 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400893 if (IS_ERR(cow))
894 return PTR_ERR(cow);
895
Chris Masonb4ce94d2009-02-04 09:25:08 -0500896 /* cow is set to blocking by btrfs_init_new_buffer */
897
Chris Mason5f39d392007-10-15 16:14:19 -0400898 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400899 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400900 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400901 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
902 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
903 BTRFS_HEADER_FLAG_RELOC);
904 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
905 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
906 else
907 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400908
Yan Zheng2b820322008-11-17 21:11:30 -0500909 write_extent_buffer(cow, root->fs_info->fsid,
910 (unsigned long)btrfs_header_fsid(cow),
911 BTRFS_FSID_SIZE);
912
Mark Fashehbe1a5562011-08-08 13:20:18 -0700913 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700914 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700916 return ret;
917 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400918
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400919 if (root->ref_cows)
920 btrfs_reloc_cow_block(trans, root, buf, cow);
921
Chris Mason6702ed42007-08-07 16:15:09 -0400922 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400923 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400924 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
925 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
926 parent_start = buf->start;
927 else
928 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400929
Chris Mason5f39d392007-10-15 16:14:19 -0400930 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200931 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400932 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400933
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 Mason5f39d392007-10-15 16:14:19 -0400936 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400937 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400938 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400939 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
940 parent_start = parent->start;
941 else
942 parent_start = 0;
943
944 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200945 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
946 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400947 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400948 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500949 btrfs_set_node_ptr_generation(parent, parent_slot,
950 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400951 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400952 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200953 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400954 }
Chris Mason925baed2008-06-25 16:01:30 -0400955 if (unlock_orig)
956 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500957 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400958 btrfs_mark_buffer_dirty(cow);
959 *cow_ret = cow;
960 return 0;
961}
962
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963static inline int should_cow_block(struct btrfs_trans_handle *trans,
964 struct btrfs_root *root,
965 struct extent_buffer *buf)
966{
Liu Bof1ebcc72011-11-14 20:48:06 -0500967 /* ensure we can see the force_cow */
968 smp_rmb();
969
970 /*
971 * We do not need to cow a block if
972 * 1) this block is not created or changed in this transaction;
973 * 2) this block does not belong to TREE_RELOC tree;
974 * 3) the root is not forced COW.
975 *
976 * What is forced COW:
977 * when we create snapshot during commiting the transaction,
978 * after we've finished coping src root, we must COW the shared
979 * block to ensure the metadata consistency.
980 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400981 if (btrfs_header_generation(buf) == trans->transid &&
982 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
983 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500984 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
985 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 return 0;
987 return 1;
988}
989
Chris Masond352ac62008-09-29 15:18:18 -0400990/*
991 * cows a single block, see __btrfs_cow_block for the real work.
992 * This version of it has extra checks so that a block isn't cow'd more than
993 * once per transaction, as long as it hasn't been written yet
994 */
Chris Masond3977122009-01-05 21:25:51 -0500995noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400996 struct btrfs_root *root, struct extent_buffer *buf,
997 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400998 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500999{
Chris Mason6702ed42007-08-07 16:15:09 -04001000 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001001 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001002
Chris Masonccd467d2007-06-28 15:57:36 -04001003 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001004 printk(KERN_CRIT "trans %llu running %llu\n",
1005 (unsigned long long)trans->transid,
1006 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001007 root->fs_info->running_transaction->transid);
1008 WARN_ON(1);
1009 }
1010 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001011 printk(KERN_CRIT "trans %llu running %llu\n",
1012 (unsigned long long)trans->transid,
1013 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001014 WARN_ON(1);
1015 }
Chris Masondc17ff82008-01-08 15:46:30 -05001016
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001017 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001018 *cow_ret = buf;
1019 return 0;
1020 }
Chris Masonc4876852009-02-04 09:24:25 -05001021
Chris Mason0b86a832008-03-24 15:01:56 -04001022 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001023
1024 if (parent)
1025 btrfs_set_lock_blocking(parent);
1026 btrfs_set_lock_blocking(buf);
1027
Chris Masonf510cfe2007-10-15 16:14:48 -04001028 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001029 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001030
1031 trace_btrfs_cow_block(root, buf, *cow_ret);
1032
Chris Masonf510cfe2007-10-15 16:14:48 -04001033 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001034}
1035
Chris Masond352ac62008-09-29 15:18:18 -04001036/*
1037 * helper function for defrag to decide if two blocks pointed to by a
1038 * node are actually close by
1039 */
Chris Mason6b800532007-10-15 16:17:34 -04001040static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001041{
Chris Mason6b800532007-10-15 16:17:34 -04001042 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001043 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001044 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001045 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001046 return 0;
1047}
1048
Chris Mason081e9572007-11-06 10:26:24 -05001049/*
1050 * compare two keys in a memcmp fashion
1051 */
1052static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1053{
1054 struct btrfs_key k1;
1055
1056 btrfs_disk_key_to_cpu(&k1, disk);
1057
Diego Calleja20736ab2009-07-24 11:06:52 -04001058 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001059}
1060
Josef Bacikf3465ca2008-11-12 14:19:50 -05001061/*
1062 * same as comp_keys only with two btrfs_key's
1063 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001064int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001065{
1066 if (k1->objectid > k2->objectid)
1067 return 1;
1068 if (k1->objectid < k2->objectid)
1069 return -1;
1070 if (k1->type > k2->type)
1071 return 1;
1072 if (k1->type < k2->type)
1073 return -1;
1074 if (k1->offset > k2->offset)
1075 return 1;
1076 if (k1->offset < k2->offset)
1077 return -1;
1078 return 0;
1079}
Chris Mason081e9572007-11-06 10:26:24 -05001080
Chris Masond352ac62008-09-29 15:18:18 -04001081/*
1082 * this is used by the defrag code to go through all the
1083 * leaves pointed to by a node and reallocate them so that
1084 * disk order is close to key order
1085 */
Chris Mason6702ed42007-08-07 16:15:09 -04001086int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001087 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001088 int start_slot, int cache_only, u64 *last_ret,
1089 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001090{
Chris Mason6b800532007-10-15 16:17:34 -04001091 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001092 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001093 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001094 u64 search_start = *last_ret;
1095 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001096 u64 other;
1097 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001098 int end_slot;
1099 int i;
1100 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001101 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001102 int uptodate;
1103 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001104 int progress_passed = 0;
1105 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001106
Chris Mason5708b952007-10-25 15:43:18 -04001107 parent_level = btrfs_header_level(parent);
1108 if (cache_only && parent_level != 1)
1109 return 0;
1110
Chris Masond3977122009-01-05 21:25:51 -05001111 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001112 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001113 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001114 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001115
Chris Mason6b800532007-10-15 16:17:34 -04001116 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001117 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001118 end_slot = parent_nritems;
1119
1120 if (parent_nritems == 1)
1121 return 0;
1122
Chris Masonb4ce94d2009-02-04 09:25:08 -05001123 btrfs_set_lock_blocking(parent);
1124
Chris Mason6702ed42007-08-07 16:15:09 -04001125 for (i = start_slot; i < end_slot; i++) {
1126 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001127
Chris Mason081e9572007-11-06 10:26:24 -05001128 btrfs_node_key(parent, &disk_key, i);
1129 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1130 continue;
1131
1132 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001133 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001134 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001135 if (last_block == 0)
1136 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001137
Chris Mason6702ed42007-08-07 16:15:09 -04001138 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001139 other = btrfs_node_blockptr(parent, i - 1);
1140 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001141 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001142 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001143 other = btrfs_node_blockptr(parent, i + 1);
1144 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001145 }
Chris Masone9d0b132007-08-10 14:06:19 -04001146 if (close) {
1147 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001148 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001149 }
Chris Mason6702ed42007-08-07 16:15:09 -04001150
Chris Mason6b800532007-10-15 16:17:34 -04001151 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1152 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001153 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001154 else
1155 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001156 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001157 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001158 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001159 continue;
1160 }
Chris Mason6b800532007-10-15 16:17:34 -04001161 if (!cur) {
1162 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001163 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001164 if (!cur)
1165 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001166 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001167 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001168 }
Chris Mason6702ed42007-08-07 16:15:09 -04001169 }
Chris Masone9d0b132007-08-10 14:06:19 -04001170 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001171 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001172
Chris Masone7a84562008-06-25 16:01:31 -04001173 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001174 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001175 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001176 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001177 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001178 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001179 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001180 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001181 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001182 break;
Yan252c38f2007-08-29 09:11:44 -04001183 }
Chris Masone7a84562008-06-25 16:01:31 -04001184 search_start = cur->start;
1185 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001186 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001187 btrfs_tree_unlock(cur);
1188 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001189 }
1190 return err;
1191}
1192
Chris Mason74123bd2007-02-02 11:05:29 -05001193/*
1194 * The leaf data grows from end-to-front in the node.
1195 * this returns the address of the start of the last item,
1196 * which is the stop of the leaf data stack
1197 */
Chris Mason123abc82007-03-14 14:14:43 -04001198static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001199 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001200{
Chris Mason5f39d392007-10-15 16:14:19 -04001201 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001202 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001203 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001204 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001205}
1206
Chris Masonaa5d6be2007-02-28 16:35:06 -05001207
Chris Mason74123bd2007-02-02 11:05:29 -05001208/*
Chris Mason5f39d392007-10-15 16:14:19 -04001209 * search for key in the extent_buffer. The items start at offset p,
1210 * and they are item_size apart. There are 'max' items in p.
1211 *
Chris Mason74123bd2007-02-02 11:05:29 -05001212 * the slot in the array is returned via slot, and it points to
1213 * the place where you would insert key if it is not found in
1214 * the array.
1215 *
1216 * slot may point to max if the key is bigger than all of the keys
1217 */
Chris Masone02119d2008-09-05 16:13:11 -04001218static noinline int generic_bin_search(struct extent_buffer *eb,
1219 unsigned long p,
1220 int item_size, struct btrfs_key *key,
1221 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001222{
1223 int low = 0;
1224 int high = max;
1225 int mid;
1226 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001227 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001228 struct btrfs_disk_key unaligned;
1229 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001230 char *kaddr = NULL;
1231 unsigned long map_start = 0;
1232 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001233 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001234
Chris Masond3977122009-01-05 21:25:51 -05001235 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001236 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001237 offset = p + mid * item_size;
1238
Chris Masona6591712011-07-19 12:04:14 -04001239 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001240 (offset + sizeof(struct btrfs_disk_key)) >
1241 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001242
1243 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001244 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001245 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001246
Chris Mason479965d2007-10-15 16:14:27 -04001247 if (!err) {
1248 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1249 map_start);
1250 } else {
1251 read_extent_buffer(eb, &unaligned,
1252 offset, sizeof(unaligned));
1253 tmp = &unaligned;
1254 }
1255
Chris Mason5f39d392007-10-15 16:14:19 -04001256 } else {
1257 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1258 map_start);
1259 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001260 ret = comp_keys(tmp, key);
1261
1262 if (ret < 0)
1263 low = mid + 1;
1264 else if (ret > 0)
1265 high = mid;
1266 else {
1267 *slot = mid;
1268 return 0;
1269 }
1270 }
1271 *slot = low;
1272 return 1;
1273}
1274
Chris Mason97571fd2007-02-24 13:39:08 -05001275/*
1276 * simple bin_search frontend that does the right thing for
1277 * leaves vs nodes
1278 */
Chris Mason5f39d392007-10-15 16:14:19 -04001279static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1280 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001281{
Chris Mason5f39d392007-10-15 16:14:19 -04001282 if (level == 0) {
1283 return generic_bin_search(eb,
1284 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001285 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001286 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001287 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001288 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001289 return generic_bin_search(eb,
1290 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001291 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001292 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001293 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001294 }
1295 return -1;
1296}
1297
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001298int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1299 int level, int *slot)
1300{
1301 return bin_search(eb, key, level, slot);
1302}
1303
Yan, Zhengf0486c62010-05-16 10:46:25 -04001304static void root_add_used(struct btrfs_root *root, u32 size)
1305{
1306 spin_lock(&root->accounting_lock);
1307 btrfs_set_root_used(&root->root_item,
1308 btrfs_root_used(&root->root_item) + size);
1309 spin_unlock(&root->accounting_lock);
1310}
1311
1312static void root_sub_used(struct btrfs_root *root, u32 size)
1313{
1314 spin_lock(&root->accounting_lock);
1315 btrfs_set_root_used(&root->root_item,
1316 btrfs_root_used(&root->root_item) - size);
1317 spin_unlock(&root->accounting_lock);
1318}
1319
Chris Masond352ac62008-09-29 15:18:18 -04001320/* given a node and slot number, this reads the blocks it points to. The
1321 * extent buffer is returned with a reference taken (but unlocked).
1322 * NULL is returned on error.
1323 */
Chris Masone02119d2008-09-05 16:13:11 -04001324static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001325 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001326{
Chris Masonca7a79a2008-05-12 12:59:19 -04001327 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001328 if (slot < 0)
1329 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001330 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001331 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001332
1333 BUG_ON(level == 0);
1334
Chris Masondb945352007-10-15 16:15:53 -04001335 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001336 btrfs_level_size(root, level - 1),
1337 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001338}
1339
Chris Masond352ac62008-09-29 15:18:18 -04001340/*
1341 * node level balancing, used to make sure nodes are in proper order for
1342 * item deletion. We balance from the top down, so we have to make sure
1343 * that a deletion won't leave an node completely empty later on.
1344 */
Chris Masone02119d2008-09-05 16:13:11 -04001345static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001346 struct btrfs_root *root,
1347 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001348{
Chris Mason5f39d392007-10-15 16:14:19 -04001349 struct extent_buffer *right = NULL;
1350 struct extent_buffer *mid;
1351 struct extent_buffer *left = NULL;
1352 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001353 int ret = 0;
1354 int wret;
1355 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001356 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001357 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001358
1359 if (level == 0)
1360 return 0;
1361
Chris Mason5f39d392007-10-15 16:14:19 -04001362 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001363
Chris Masonbd681512011-07-16 15:23:14 -04001364 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1365 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001366 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1367
Chris Mason1d4f8a02007-03-13 09:28:32 -04001368 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001369
Li Zefana05a9bb2011-09-06 16:55:34 +08001370 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001371 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001372 pslot = path->slots[level + 1];
1373 }
Chris Masonbb803952007-03-01 12:04:21 -05001374
Chris Mason40689472007-03-17 14:29:23 -04001375 /*
1376 * deal with the case where there is only one pointer in the root
1377 * by promoting the node below to a root
1378 */
Chris Mason5f39d392007-10-15 16:14:19 -04001379 if (!parent) {
1380 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001381
Chris Mason5f39d392007-10-15 16:14:19 -04001382 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001383 return 0;
1384
1385 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001386 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001387 if (!child) {
1388 ret = -EROFS;
1389 btrfs_std_error(root->fs_info, ret);
1390 goto enospc;
1391 }
1392
Chris Mason925baed2008-06-25 16:01:30 -04001393 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001394 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001395 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001396 if (ret) {
1397 btrfs_tree_unlock(child);
1398 free_extent_buffer(child);
1399 goto enospc;
1400 }
Yan2f375ab2008-02-01 14:58:07 -05001401
Jan Schmidtf2304752012-05-26 11:43:17 +02001402 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001403 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001404
Chris Mason0b86a832008-03-24 15:01:56 -04001405 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001406 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001407
Chris Mason925baed2008-06-25 16:01:30 -04001408 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001409 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001410 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001411 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001412 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001413 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001414
1415 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001416 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001417 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001418 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001419 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001420 }
Chris Mason5f39d392007-10-15 16:14:19 -04001421 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001422 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001423 return 0;
1424
Andi Kleen559af822010-10-29 15:14:37 -04001425 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001426
Chris Mason5f39d392007-10-15 16:14:19 -04001427 left = read_node_slot(root, parent, pslot - 1);
1428 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001429 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001430 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001431 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001432 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001433 if (wret) {
1434 ret = wret;
1435 goto enospc;
1436 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001437 }
Chris Mason5f39d392007-10-15 16:14:19 -04001438 right = read_node_slot(root, parent, pslot + 1);
1439 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001440 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001441 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001442 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001443 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001444 if (wret) {
1445 ret = wret;
1446 goto enospc;
1447 }
1448 }
1449
1450 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001451 if (left) {
1452 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001453 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001454 if (wret < 0)
1455 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001456 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001457 }
Chris Mason79f95c82007-03-01 15:16:26 -05001458
1459 /*
1460 * then try to empty the right most buffer into the middle
1461 */
Chris Mason5f39d392007-10-15 16:14:19 -04001462 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001463 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001464 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001465 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001466 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001467 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001468 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001469 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001470 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001471 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001472 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001473 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001474 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001475 struct btrfs_disk_key right_key;
1476 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001477 tree_mod_log_set_node_key(root->fs_info, parent,
1478 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001479 btrfs_set_node_key(parent, &right_key, pslot + 1);
1480 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001481 }
1482 }
Chris Mason5f39d392007-10-15 16:14:19 -04001483 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001484 /*
1485 * we're not allowed to leave a node with one item in the
1486 * tree during a delete. A deletion from lower in the tree
1487 * could try to delete the only pointer in this node.
1488 * So, pull some keys from the left.
1489 * There has to be a left pointer at this point because
1490 * otherwise we would have pulled some pointers from the
1491 * right
1492 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001493 if (!left) {
1494 ret = -EROFS;
1495 btrfs_std_error(root->fs_info, ret);
1496 goto enospc;
1497 }
Chris Mason5f39d392007-10-15 16:14:19 -04001498 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001499 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001500 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001501 goto enospc;
1502 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001503 if (wret == 1) {
1504 wret = push_node_left(trans, root, left, mid, 1);
1505 if (wret < 0)
1506 ret = wret;
1507 }
Chris Mason79f95c82007-03-01 15:16:26 -05001508 BUG_ON(wret == 1);
1509 }
Chris Mason5f39d392007-10-15 16:14:19 -04001510 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001511 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001512 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001513 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001514 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001515 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001516 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001517 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001518 } else {
1519 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001520 struct btrfs_disk_key mid_key;
1521 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001522 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1523 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001524 btrfs_set_node_key(parent, &mid_key, pslot);
1525 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001526 }
Chris Masonbb803952007-03-01 12:04:21 -05001527
Chris Mason79f95c82007-03-01 15:16:26 -05001528 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001529 if (left) {
1530 if (btrfs_header_nritems(left) > orig_slot) {
1531 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001532 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001533 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001534 path->slots[level + 1] -= 1;
1535 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001536 if (mid) {
1537 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001538 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001539 }
Chris Masonbb803952007-03-01 12:04:21 -05001540 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001541 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001542 path->slots[level] = orig_slot;
1543 }
1544 }
Chris Mason79f95c82007-03-01 15:16:26 -05001545 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001546 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001547 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001548 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001549enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001550 if (right) {
1551 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001552 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001553 }
1554 if (left) {
1555 if (path->nodes[level] != left)
1556 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001557 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001558 }
Chris Masonbb803952007-03-01 12:04:21 -05001559 return ret;
1560}
1561
Chris Masond352ac62008-09-29 15:18:18 -04001562/* Node balancing for insertion. Here we only split or push nodes around
1563 * when they are completely full. This is also done top down, so we
1564 * have to be pessimistic.
1565 */
Chris Masond3977122009-01-05 21:25:51 -05001566static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001567 struct btrfs_root *root,
1568 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001569{
Chris Mason5f39d392007-10-15 16:14:19 -04001570 struct extent_buffer *right = NULL;
1571 struct extent_buffer *mid;
1572 struct extent_buffer *left = NULL;
1573 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001574 int ret = 0;
1575 int wret;
1576 int pslot;
1577 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001578
1579 if (level == 0)
1580 return 1;
1581
Chris Mason5f39d392007-10-15 16:14:19 -04001582 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001583 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001584
Li Zefana05a9bb2011-09-06 16:55:34 +08001585 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001586 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001587 pslot = path->slots[level + 1];
1588 }
Chris Masone66f7092007-04-20 13:16:02 -04001589
Chris Mason5f39d392007-10-15 16:14:19 -04001590 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001591 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001592
Chris Mason5f39d392007-10-15 16:14:19 -04001593 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001594
1595 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001596 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001597 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001598
1599 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001600 btrfs_set_lock_blocking(left);
1601
Chris Mason5f39d392007-10-15 16:14:19 -04001602 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001603 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1604 wret = 1;
1605 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001606 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001607 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001608 if (ret)
1609 wret = 1;
1610 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001611 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001612 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001613 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001614 }
Chris Masone66f7092007-04-20 13:16:02 -04001615 if (wret < 0)
1616 ret = wret;
1617 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001618 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001619 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001620 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001621 tree_mod_log_set_node_key(root->fs_info, parent,
1622 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001623 btrfs_set_node_key(parent, &disk_key, pslot);
1624 btrfs_mark_buffer_dirty(parent);
1625 if (btrfs_header_nritems(left) > orig_slot) {
1626 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001627 path->slots[level + 1] -= 1;
1628 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001629 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001630 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001631 } else {
1632 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001633 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001634 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001635 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001636 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001637 }
Chris Masone66f7092007-04-20 13:16:02 -04001638 return 0;
1639 }
Chris Mason925baed2008-06-25 16:01:30 -04001640 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001641 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001642 }
Chris Mason925baed2008-06-25 16:01:30 -04001643 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001644
1645 /*
1646 * then try to empty the right most buffer into the middle
1647 */
Chris Mason5f39d392007-10-15 16:14:19 -04001648 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001649 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001650
Chris Mason925baed2008-06-25 16:01:30 -04001651 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001652 btrfs_set_lock_blocking(right);
1653
Chris Mason5f39d392007-10-15 16:14:19 -04001654 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001655 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1656 wret = 1;
1657 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001658 ret = btrfs_cow_block(trans, root, right,
1659 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001660 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001661 if (ret)
1662 wret = 1;
1663 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001664 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001665 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001666 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001667 }
Chris Masone66f7092007-04-20 13:16:02 -04001668 if (wret < 0)
1669 ret = wret;
1670 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001671 struct btrfs_disk_key disk_key;
1672
1673 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001674 tree_mod_log_set_node_key(root->fs_info, parent,
1675 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001676 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1677 btrfs_mark_buffer_dirty(parent);
1678
1679 if (btrfs_header_nritems(mid) <= orig_slot) {
1680 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001681 path->slots[level + 1] += 1;
1682 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001683 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001684 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001685 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001686 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001687 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001688 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001689 }
Chris Masone66f7092007-04-20 13:16:02 -04001690 return 0;
1691 }
Chris Mason925baed2008-06-25 16:01:30 -04001692 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001693 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001694 }
Chris Masone66f7092007-04-20 13:16:02 -04001695 return 1;
1696}
1697
Chris Mason74123bd2007-02-02 11:05:29 -05001698/*
Chris Masond352ac62008-09-29 15:18:18 -04001699 * readahead one full node of leaves, finding things that are close
1700 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001701 */
Chris Masonc8c42862009-04-03 10:14:18 -04001702static void reada_for_search(struct btrfs_root *root,
1703 struct btrfs_path *path,
1704 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001705{
Chris Mason5f39d392007-10-15 16:14:19 -04001706 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001707 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001708 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001709 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001710 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001711 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001712 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001713 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001714 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001715 u32 nr;
1716 u32 blocksize;
1717 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001718
Chris Masona6b6e752007-10-15 16:22:39 -04001719 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001720 return;
1721
Chris Mason6702ed42007-08-07 16:15:09 -04001722 if (!path->nodes[level])
1723 return;
1724
Chris Mason5f39d392007-10-15 16:14:19 -04001725 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001726
Chris Mason3c69fae2007-08-07 15:52:22 -04001727 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001728 blocksize = btrfs_level_size(root, level - 1);
1729 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001730 if (eb) {
1731 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001732 return;
1733 }
1734
Chris Masona7175312009-01-22 09:23:10 -05001735 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001736
Chris Mason5f39d392007-10-15 16:14:19 -04001737 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001738 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001739
Chris Masond3977122009-01-05 21:25:51 -05001740 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001741 if (direction < 0) {
1742 if (nr == 0)
1743 break;
1744 nr--;
1745 } else if (direction > 0) {
1746 nr++;
1747 if (nr >= nritems)
1748 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001749 }
Chris Mason01f46652007-12-21 16:24:26 -05001750 if (path->reada < 0 && objectid) {
1751 btrfs_node_key(node, &disk_key, nr);
1752 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1753 break;
1754 }
Chris Mason6b800532007-10-15 16:17:34 -04001755 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001756 if ((search <= target && target - search <= 65536) ||
1757 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001758 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001759 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001760 nread += blocksize;
1761 }
1762 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001763 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001764 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001765 }
1766}
Chris Mason925baed2008-06-25 16:01:30 -04001767
Chris Masond352ac62008-09-29 15:18:18 -04001768/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001769 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1770 * cache
1771 */
1772static noinline int reada_for_balance(struct btrfs_root *root,
1773 struct btrfs_path *path, int level)
1774{
1775 int slot;
1776 int nritems;
1777 struct extent_buffer *parent;
1778 struct extent_buffer *eb;
1779 u64 gen;
1780 u64 block1 = 0;
1781 u64 block2 = 0;
1782 int ret = 0;
1783 int blocksize;
1784
Chris Mason8c594ea2009-04-20 15:50:10 -04001785 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001786 if (!parent)
1787 return 0;
1788
1789 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001790 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001791 blocksize = btrfs_level_size(root, level);
1792
1793 if (slot > 0) {
1794 block1 = btrfs_node_blockptr(parent, slot - 1);
1795 gen = btrfs_node_ptr_generation(parent, slot - 1);
1796 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001797 /*
1798 * if we get -eagain from btrfs_buffer_uptodate, we
1799 * don't want to return eagain here. That will loop
1800 * forever
1801 */
1802 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001803 block1 = 0;
1804 free_extent_buffer(eb);
1805 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001806 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001807 block2 = btrfs_node_blockptr(parent, slot + 1);
1808 gen = btrfs_node_ptr_generation(parent, slot + 1);
1809 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001810 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001811 block2 = 0;
1812 free_extent_buffer(eb);
1813 }
1814 if (block1 || block2) {
1815 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001816
1817 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001818 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001819
1820 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001821 if (block1)
1822 readahead_tree_block(root, block1, blocksize, 0);
1823 if (block2)
1824 readahead_tree_block(root, block2, blocksize, 0);
1825
1826 if (block1) {
1827 eb = read_tree_block(root, block1, blocksize, 0);
1828 free_extent_buffer(eb);
1829 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001830 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001831 eb = read_tree_block(root, block2, blocksize, 0);
1832 free_extent_buffer(eb);
1833 }
1834 }
1835 return ret;
1836}
1837
1838
1839/*
Chris Masond3977122009-01-05 21:25:51 -05001840 * when we walk down the tree, it is usually safe to unlock the higher layers
1841 * in the tree. The exceptions are when our path goes through slot 0, because
1842 * operations on the tree might require changing key pointers higher up in the
1843 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001844 *
Chris Masond3977122009-01-05 21:25:51 -05001845 * callers might also have set path->keep_locks, which tells this code to keep
1846 * the lock if the path points to the last slot in the block. This is part of
1847 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001848 *
Chris Masond3977122009-01-05 21:25:51 -05001849 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1850 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001851 */
Chris Masone02119d2008-09-05 16:13:11 -04001852static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001853 int lowest_unlock, int min_write_lock_level,
1854 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001855{
1856 int i;
1857 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001858 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001859 struct extent_buffer *t;
1860
1861 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1862 if (!path->nodes[i])
1863 break;
1864 if (!path->locks[i])
1865 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001866 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001867 skip_level = i + 1;
1868 continue;
1869 }
Chris Mason051e1b92008-06-25 16:01:30 -04001870 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001871 u32 nritems;
1872 t = path->nodes[i];
1873 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001874 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001875 skip_level = i + 1;
1876 continue;
1877 }
1878 }
Chris Mason051e1b92008-06-25 16:01:30 -04001879 if (skip_level < i && i >= lowest_unlock)
1880 no_skips = 1;
1881
Chris Mason925baed2008-06-25 16:01:30 -04001882 t = path->nodes[i];
1883 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001884 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001885 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001886 if (write_lock_level &&
1887 i > min_write_lock_level &&
1888 i <= *write_lock_level) {
1889 *write_lock_level = i - 1;
1890 }
Chris Mason925baed2008-06-25 16:01:30 -04001891 }
1892 }
1893}
1894
Chris Mason3c69fae2007-08-07 15:52:22 -04001895/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001896 * This releases any locks held in the path starting at level and
1897 * going all the way up to the root.
1898 *
1899 * btrfs_search_slot will keep the lock held on higher nodes in a few
1900 * corner cases, such as COW of the block at slot zero in the node. This
1901 * ignores those rules, and it should only be called when there are no
1902 * more updates to be done higher up in the tree.
1903 */
1904noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1905{
1906 int i;
1907
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001908 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001909 return;
1910
1911 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1912 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001913 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001914 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001915 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001916 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001917 path->locks[i] = 0;
1918 }
1919}
1920
1921/*
Chris Masonc8c42862009-04-03 10:14:18 -04001922 * helper function for btrfs_search_slot. The goal is to find a block
1923 * in cache without setting the path to blocking. If we find the block
1924 * we return zero and the path is unchanged.
1925 *
1926 * If we can't find the block, we set the path blocking and do some
1927 * reada. -EAGAIN is returned and the search must be repeated.
1928 */
1929static int
1930read_block_for_search(struct btrfs_trans_handle *trans,
1931 struct btrfs_root *root, struct btrfs_path *p,
1932 struct extent_buffer **eb_ret, int level, int slot,
1933 struct btrfs_key *key)
1934{
1935 u64 blocknr;
1936 u64 gen;
1937 u32 blocksize;
1938 struct extent_buffer *b = *eb_ret;
1939 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001940 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001941
1942 blocknr = btrfs_node_blockptr(b, slot);
1943 gen = btrfs_node_ptr_generation(b, slot);
1944 blocksize = btrfs_level_size(root, level - 1);
1945
1946 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001947 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04001948 /* first we do an atomic uptodate check */
1949 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
1950 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001951 /*
1952 * we found an up to date block without
1953 * sleeping, return
1954 * right away
1955 */
1956 *eb_ret = tmp;
1957 return 0;
1958 }
1959 /* the pages were up to date, but we failed
1960 * the generation number check. Do a full
1961 * read for the generation number that is correct.
1962 * We must do this without dropping locks so
1963 * we can trust our generation number
1964 */
1965 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001966 btrfs_set_path_blocking(p);
1967
Chris Masonb9fab912012-05-06 07:23:47 -04001968 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04001969 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04001970 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001971 *eb_ret = tmp;
1972 return 0;
1973 }
1974 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001975 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001976 return -EIO;
1977 }
Chris Masonc8c42862009-04-03 10:14:18 -04001978 }
1979
1980 /*
1981 * reduce lock contention at high levels
1982 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001983 * we read. Don't release the lock on the current
1984 * level because we need to walk this node to figure
1985 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001986 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001987 btrfs_unlock_up_safe(p, level + 1);
1988 btrfs_set_path_blocking(p);
1989
Chris Masoncb449212010-10-24 11:01:27 -04001990 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001991 if (p->reada)
1992 reada_for_search(root, p, level, slot, key->objectid);
1993
David Sterbab3b4aa72011-04-21 01:20:15 +02001994 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001995
1996 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001997 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001998 if (tmp) {
1999 /*
2000 * If the read above didn't mark this buffer up to date,
2001 * it will never end up being up to date. Set ret to EIO now
2002 * and give up so that our caller doesn't loop forever
2003 * on our EAGAINs.
2004 */
Chris Masonb9fab912012-05-06 07:23:47 -04002005 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002006 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002007 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002008 }
2009 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002010}
2011
2012/*
2013 * helper function for btrfs_search_slot. This does all of the checks
2014 * for node-level blocks and does any balancing required based on
2015 * the ins_len.
2016 *
2017 * If no extra work was required, zero is returned. If we had to
2018 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2019 * start over
2020 */
2021static int
2022setup_nodes_for_search(struct btrfs_trans_handle *trans,
2023 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002024 struct extent_buffer *b, int level, int ins_len,
2025 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002026{
2027 int ret;
2028 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2029 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2030 int sret;
2031
Chris Masonbd681512011-07-16 15:23:14 -04002032 if (*write_lock_level < level + 1) {
2033 *write_lock_level = level + 1;
2034 btrfs_release_path(p);
2035 goto again;
2036 }
2037
Chris Masonc8c42862009-04-03 10:14:18 -04002038 sret = reada_for_balance(root, p, level);
2039 if (sret)
2040 goto again;
2041
2042 btrfs_set_path_blocking(p);
2043 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002044 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002045
2046 BUG_ON(sret > 0);
2047 if (sret) {
2048 ret = sret;
2049 goto done;
2050 }
2051 b = p->nodes[level];
2052 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002053 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002054 int sret;
2055
Chris Masonbd681512011-07-16 15:23:14 -04002056 if (*write_lock_level < level + 1) {
2057 *write_lock_level = level + 1;
2058 btrfs_release_path(p);
2059 goto again;
2060 }
2061
Chris Masonc8c42862009-04-03 10:14:18 -04002062 sret = reada_for_balance(root, p, level);
2063 if (sret)
2064 goto again;
2065
2066 btrfs_set_path_blocking(p);
2067 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002068 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002069
2070 if (sret) {
2071 ret = sret;
2072 goto done;
2073 }
2074 b = p->nodes[level];
2075 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002076 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002077 goto again;
2078 }
2079 BUG_ON(btrfs_header_nritems(b) == 1);
2080 }
2081 return 0;
2082
2083again:
2084 ret = -EAGAIN;
2085done:
2086 return ret;
2087}
2088
2089/*
Chris Mason74123bd2007-02-02 11:05:29 -05002090 * look for key in the tree. path is filled in with nodes along the way
2091 * if key is found, we return zero and you can find the item in the leaf
2092 * level of the path (level 0)
2093 *
2094 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002095 * be inserted, and 1 is returned. If there are other errors during the
2096 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002097 *
2098 * if ins_len > 0, nodes and leaves will be split as we walk down the
2099 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2100 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002101 */
Chris Masone089f052007-03-16 16:20:31 -04002102int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2103 *root, struct btrfs_key *key, struct btrfs_path *p, int
2104 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002105{
Chris Mason5f39d392007-10-15 16:14:19 -04002106 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002107 int slot;
2108 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002109 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002110 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002111 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002112 int root_lock;
2113 /* everything at write_lock_level or lower must be write locked */
2114 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002115 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002116 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002117
Chris Mason6702ed42007-08-07 16:15:09 -04002118 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002119 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002120 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002121
Chris Masonbd681512011-07-16 15:23:14 -04002122 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002123 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002124
Chris Masonbd681512011-07-16 15:23:14 -04002125 /* when we are removing items, we might have to go up to level
2126 * two as we update tree pointers Make sure we keep write
2127 * for those levels as well
2128 */
2129 write_lock_level = 2;
2130 } else if (ins_len > 0) {
2131 /*
2132 * for inserting items, make sure we have a write lock on
2133 * level 1 so we can update keys
2134 */
2135 write_lock_level = 1;
2136 }
2137
2138 if (!cow)
2139 write_lock_level = -1;
2140
2141 if (cow && (p->keep_locks || p->lowest_level))
2142 write_lock_level = BTRFS_MAX_LEVEL;
2143
Chris Masonf7c79f32012-03-19 15:54:38 -04002144 min_write_lock_level = write_lock_level;
2145
Chris Masonbb803952007-03-01 12:04:21 -05002146again:
Chris Masonbd681512011-07-16 15:23:14 -04002147 /*
2148 * we try very hard to do read locks on the root
2149 */
2150 root_lock = BTRFS_READ_LOCK;
2151 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002152 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002153 /*
2154 * the commit roots are read only
2155 * so we always do read locks
2156 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002157 b = root->commit_root;
2158 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002159 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002160 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002161 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002162 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002163 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002164 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002165 level = btrfs_header_level(b);
2166 } else {
2167 /* we don't know the level of the root node
2168 * until we actually have it read locked
2169 */
2170 b = btrfs_read_lock_root_node(root);
2171 level = btrfs_header_level(b);
2172 if (level <= write_lock_level) {
2173 /* whoops, must trade for write lock */
2174 btrfs_tree_read_unlock(b);
2175 free_extent_buffer(b);
2176 b = btrfs_lock_root_node(root);
2177 root_lock = BTRFS_WRITE_LOCK;
2178
2179 /* the level might have changed, check again */
2180 level = btrfs_header_level(b);
2181 }
2182 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002183 }
Chris Masonbd681512011-07-16 15:23:14 -04002184 p->nodes[level] = b;
2185 if (!p->skip_locking)
2186 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002187
Chris Masoneb60cea2007-02-02 09:18:22 -05002188 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002189 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002190
2191 /*
2192 * setup the path here so we can release it under lock
2193 * contention with the cow code
2194 */
Chris Mason02217ed2007-03-02 16:08:05 -05002195 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002196 /*
2197 * if we don't really need to cow this block
2198 * then we don't want to set the path blocking,
2199 * so we test it here
2200 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002201 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002202 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203
Chris Masonb4ce94d2009-02-04 09:25:08 -05002204 btrfs_set_path_blocking(p);
2205
Chris Masonbd681512011-07-16 15:23:14 -04002206 /*
2207 * must have write locks on this node and the
2208 * parent
2209 */
2210 if (level + 1 > write_lock_level) {
2211 write_lock_level = level + 1;
2212 btrfs_release_path(p);
2213 goto again;
2214 }
2215
Yan Zheng33c66f42009-07-22 09:59:00 -04002216 err = btrfs_cow_block(trans, root, b,
2217 p->nodes[level + 1],
2218 p->slots[level + 1], &b);
2219 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002220 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002221 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002222 }
Chris Mason02217ed2007-03-02 16:08:05 -05002223 }
Chris Mason65b51a02008-08-01 15:11:20 -04002224cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002225 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002226
Chris Masoneb60cea2007-02-02 09:18:22 -05002227 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002228 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002229
2230 /*
2231 * we have a lock on b and as long as we aren't changing
2232 * the tree, there is no way to for the items in b to change.
2233 * It is safe to drop the lock on our parent before we
2234 * go through the expensive btree search on b.
2235 *
2236 * If cow is true, then we might be changing slot zero,
2237 * which may require changing the parent. So, we can't
2238 * drop the lock until after we know which slot we're
2239 * operating on.
2240 */
2241 if (!cow)
2242 btrfs_unlock_up_safe(p, level + 1);
2243
Chris Mason5f39d392007-10-15 16:14:19 -04002244 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002245
Chris Mason5f39d392007-10-15 16:14:19 -04002246 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002247 int dec = 0;
2248 if (ret && slot > 0) {
2249 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002250 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002251 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002252 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002253 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002254 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002255 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002256 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002257 if (err) {
2258 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002259 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002260 }
Chris Masonc8c42862009-04-03 10:14:18 -04002261 b = p->nodes[level];
2262 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002263
Chris Masonbd681512011-07-16 15:23:14 -04002264 /*
2265 * slot 0 is special, if we change the key
2266 * we have to update the parent pointer
2267 * which means we must have a write lock
2268 * on the parent
2269 */
2270 if (slot == 0 && cow &&
2271 write_lock_level < level + 1) {
2272 write_lock_level = level + 1;
2273 btrfs_release_path(p);
2274 goto again;
2275 }
2276
Chris Masonf7c79f32012-03-19 15:54:38 -04002277 unlock_up(p, level, lowest_unlock,
2278 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002279
Chris Mason925baed2008-06-25 16:01:30 -04002280 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002281 if (dec)
2282 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002283 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002284 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002285
Yan Zheng33c66f42009-07-22 09:59:00 -04002286 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04002287 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002288 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002289 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002290 if (err) {
2291 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002292 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002293 }
Chris Mason76a05b32009-05-14 13:24:30 -04002294
Chris Masonb4ce94d2009-02-04 09:25:08 -05002295 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002296 level = btrfs_header_level(b);
2297 if (level <= write_lock_level) {
2298 err = btrfs_try_tree_write_lock(b);
2299 if (!err) {
2300 btrfs_set_path_blocking(p);
2301 btrfs_tree_lock(b);
2302 btrfs_clear_path_blocking(p, b,
2303 BTRFS_WRITE_LOCK);
2304 }
2305 p->locks[level] = BTRFS_WRITE_LOCK;
2306 } else {
2307 err = btrfs_try_tree_read_lock(b);
2308 if (!err) {
2309 btrfs_set_path_blocking(p);
2310 btrfs_tree_read_lock(b);
2311 btrfs_clear_path_blocking(p, b,
2312 BTRFS_READ_LOCK);
2313 }
2314 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002315 }
Chris Masonbd681512011-07-16 15:23:14 -04002316 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002317 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002318 } else {
2319 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002320 if (ins_len > 0 &&
2321 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002322 if (write_lock_level < 1) {
2323 write_lock_level = 1;
2324 btrfs_release_path(p);
2325 goto again;
2326 }
2327
Chris Masonb4ce94d2009-02-04 09:25:08 -05002328 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002329 err = split_leaf(trans, root, key,
2330 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002331 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002332
Yan Zheng33c66f42009-07-22 09:59:00 -04002333 BUG_ON(err > 0);
2334 if (err) {
2335 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002336 goto done;
2337 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002338 }
Chris Mason459931e2008-12-10 09:10:46 -05002339 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002340 unlock_up(p, level, lowest_unlock,
2341 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002342 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002343 }
2344 }
Chris Mason65b51a02008-08-01 15:11:20 -04002345 ret = 1;
2346done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002347 /*
2348 * we don't really know what they plan on doing with the path
2349 * from here on, so for now just mark it as blocking
2350 */
Chris Masonb9473432009-03-13 11:00:37 -04002351 if (!p->leave_spinning)
2352 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002353 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002354 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002355 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002356}
2357
Chris Mason74123bd2007-02-02 11:05:29 -05002358/*
2359 * adjust the pointers going up the tree, starting at level
2360 * making sure the right key of each node is points to 'key'.
2361 * This is used after shifting pointers to the left, so it stops
2362 * fixing up pointers when a given leaf/node is not in slot 0 of the
2363 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002364 *
Chris Mason74123bd2007-02-02 11:05:29 -05002365 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002366static void fixup_low_keys(struct btrfs_trans_handle *trans,
2367 struct btrfs_root *root, struct btrfs_path *path,
2368 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002369{
2370 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002371 struct extent_buffer *t;
2372
Chris Mason234b63a2007-03-13 10:46:10 -04002373 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002374 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002375 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002376 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002377 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002378 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002379 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002380 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002381 if (tslot != 0)
2382 break;
2383 }
2384}
2385
Chris Mason74123bd2007-02-02 11:05:29 -05002386/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002387 * update item key.
2388 *
2389 * This function isn't completely safe. It's the caller's responsibility
2390 * that the new key won't break the order
2391 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002392void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2393 struct btrfs_root *root, struct btrfs_path *path,
2394 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002395{
2396 struct btrfs_disk_key disk_key;
2397 struct extent_buffer *eb;
2398 int slot;
2399
2400 eb = path->nodes[0];
2401 slot = path->slots[0];
2402 if (slot > 0) {
2403 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002404 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002405 }
2406 if (slot < btrfs_header_nritems(eb) - 1) {
2407 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002408 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002409 }
2410
2411 btrfs_cpu_key_to_disk(&disk_key, new_key);
2412 btrfs_set_item_key(eb, &disk_key, slot);
2413 btrfs_mark_buffer_dirty(eb);
2414 if (slot == 0)
2415 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002416}
2417
2418/*
Chris Mason74123bd2007-02-02 11:05:29 -05002419 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002420 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002421 *
2422 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2423 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002424 */
Chris Mason98ed5172008-01-03 10:01:48 -05002425static int push_node_left(struct btrfs_trans_handle *trans,
2426 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002427 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002428{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002429 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002430 int src_nritems;
2431 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002432 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002433
Chris Mason5f39d392007-10-15 16:14:19 -04002434 src_nritems = btrfs_header_nritems(src);
2435 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002436 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002437 WARN_ON(btrfs_header_generation(src) != trans->transid);
2438 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002439
Chris Masonbce4eae2008-04-24 14:42:46 -04002440 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002441 return 1;
2442
Chris Masond3977122009-01-05 21:25:51 -05002443 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002444 return 1;
2445
Chris Masonbce4eae2008-04-24 14:42:46 -04002446 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002447 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002448 if (push_items < src_nritems) {
2449 /* leave at least 8 pointers in the node if
2450 * we aren't going to empty it
2451 */
2452 if (src_nritems - push_items < 8) {
2453 if (push_items <= 8)
2454 return 1;
2455 push_items -= 8;
2456 }
2457 }
2458 } else
2459 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002460
Jan Schmidtf2304752012-05-26 11:43:17 +02002461 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2462 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002463 copy_extent_buffer(dst, src,
2464 btrfs_node_key_ptr_offset(dst_nritems),
2465 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002466 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002467
Chris Masonbb803952007-03-01 12:04:21 -05002468 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002469 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2470 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002471 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2472 btrfs_node_key_ptr_offset(push_items),
2473 (src_nritems - push_items) *
2474 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002475 }
Chris Mason5f39d392007-10-15 16:14:19 -04002476 btrfs_set_header_nritems(src, src_nritems - push_items);
2477 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2478 btrfs_mark_buffer_dirty(src);
2479 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002480
Chris Masonbb803952007-03-01 12:04:21 -05002481 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002482}
2483
Chris Mason97571fd2007-02-24 13:39:08 -05002484/*
Chris Mason79f95c82007-03-01 15:16:26 -05002485 * try to push data from one node into the next node right in the
2486 * tree.
2487 *
2488 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2489 * error, and > 0 if there was no room in the right hand block.
2490 *
2491 * this will only push up to 1/2 the contents of the left node over
2492 */
Chris Mason5f39d392007-10-15 16:14:19 -04002493static int balance_node_right(struct btrfs_trans_handle *trans,
2494 struct btrfs_root *root,
2495 struct extent_buffer *dst,
2496 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002497{
Chris Mason79f95c82007-03-01 15:16:26 -05002498 int push_items = 0;
2499 int max_push;
2500 int src_nritems;
2501 int dst_nritems;
2502 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002503
Chris Mason7bb86312007-12-11 09:25:06 -05002504 WARN_ON(btrfs_header_generation(src) != trans->transid);
2505 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2506
Chris Mason5f39d392007-10-15 16:14:19 -04002507 src_nritems = btrfs_header_nritems(src);
2508 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002509 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002510 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002511 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002512
Chris Masond3977122009-01-05 21:25:51 -05002513 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002514 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002515
2516 max_push = src_nritems / 2 + 1;
2517 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002518 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002519 return 1;
Yan252c38f2007-08-29 09:11:44 -04002520
Chris Mason79f95c82007-03-01 15:16:26 -05002521 if (max_push < push_items)
2522 push_items = max_push;
2523
Jan Schmidtf2304752012-05-26 11:43:17 +02002524 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002525 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2526 btrfs_node_key_ptr_offset(0),
2527 (dst_nritems) *
2528 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002529
Jan Schmidtf2304752012-05-26 11:43:17 +02002530 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2531 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002532 copy_extent_buffer(dst, src,
2533 btrfs_node_key_ptr_offset(0),
2534 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002535 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002536
Chris Mason5f39d392007-10-15 16:14:19 -04002537 btrfs_set_header_nritems(src, src_nritems - push_items);
2538 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002539
Chris Mason5f39d392007-10-15 16:14:19 -04002540 btrfs_mark_buffer_dirty(src);
2541 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002542
Chris Mason79f95c82007-03-01 15:16:26 -05002543 return ret;
2544}
2545
2546/*
Chris Mason97571fd2007-02-24 13:39:08 -05002547 * helper function to insert a new root level in the tree.
2548 * A new node is allocated, and a single item is inserted to
2549 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002550 *
2551 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002552 */
Chris Masond3977122009-01-05 21:25:51 -05002553static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002554 struct btrfs_root *root,
2555 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002556{
Chris Mason7bb86312007-12-11 09:25:06 -05002557 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002558 struct extent_buffer *lower;
2559 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002560 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002561 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002562
2563 BUG_ON(path->nodes[level]);
2564 BUG_ON(path->nodes[level-1] != root->node);
2565
Chris Mason7bb86312007-12-11 09:25:06 -05002566 lower = path->nodes[level-1];
2567 if (level == 1)
2568 btrfs_item_key(lower, &lower_key, 0);
2569 else
2570 btrfs_node_key(lower, &lower_key, 0);
2571
Zheng Yan31840ae2008-09-23 13:14:14 -04002572 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002573 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002574 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002575 if (IS_ERR(c))
2576 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002577
Yan, Zhengf0486c62010-05-16 10:46:25 -04002578 root_add_used(root, root->nodesize);
2579
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002580 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002581 btrfs_set_header_nritems(c, 1);
2582 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002583 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002584 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002585 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002586 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002587
Chris Mason5f39d392007-10-15 16:14:19 -04002588 write_extent_buffer(c, root->fs_info->fsid,
2589 (unsigned long)btrfs_header_fsid(c),
2590 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002591
2592 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2593 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2594 BTRFS_UUID_SIZE);
2595
Chris Mason5f39d392007-10-15 16:14:19 -04002596 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002597 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002598 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002599 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002600
2601 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002602
2603 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002604
Chris Mason925baed2008-06-25 16:01:30 -04002605 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002606 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002607 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002608
2609 /* the super has an extra ref to root->node */
2610 free_extent_buffer(old);
2611
Chris Mason0b86a832008-03-24 15:01:56 -04002612 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002613 extent_buffer_get(c);
2614 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002615 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002616 path->slots[level] = 0;
2617 return 0;
2618}
2619
Chris Mason74123bd2007-02-02 11:05:29 -05002620/*
2621 * worker function to insert a single pointer in a node.
2622 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002623 *
Chris Mason74123bd2007-02-02 11:05:29 -05002624 * slot and level indicate where you want the key to go, and
2625 * blocknr is the block the key points to.
2626 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002627static void insert_ptr(struct btrfs_trans_handle *trans,
2628 struct btrfs_root *root, struct btrfs_path *path,
2629 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002630 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002631{
Chris Mason5f39d392007-10-15 16:14:19 -04002632 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002633 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002634 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002635
2636 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002637 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002638 lower = path->nodes[level];
2639 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002640 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002641 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002642 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002643 if (tree_mod_log && level)
2644 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2645 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002646 memmove_extent_buffer(lower,
2647 btrfs_node_key_ptr_offset(slot + 1),
2648 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002649 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002650 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002651 if (tree_mod_log && level) {
2652 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2653 MOD_LOG_KEY_ADD);
2654 BUG_ON(ret < 0);
2655 }
Chris Mason5f39d392007-10-15 16:14:19 -04002656 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002657 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002658 WARN_ON(trans->transid == 0);
2659 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002660 btrfs_set_header_nritems(lower, nritems + 1);
2661 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002662}
2663
Chris Mason97571fd2007-02-24 13:39:08 -05002664/*
2665 * split the node at the specified level in path in two.
2666 * The path is corrected to point to the appropriate node after the split
2667 *
2668 * Before splitting this tries to make some room in the node by pushing
2669 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002670 *
2671 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002672 */
Chris Masone02119d2008-09-05 16:13:11 -04002673static noinline int split_node(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *root,
2675 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002676{
Chris Mason5f39d392007-10-15 16:14:19 -04002677 struct extent_buffer *c;
2678 struct extent_buffer *split;
2679 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002680 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002681 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002682 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002683
Chris Mason5f39d392007-10-15 16:14:19 -04002684 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002685 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002686 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002687 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002688 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002689 if (ret)
2690 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002691 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002692 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002693 c = path->nodes[level];
2694 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002695 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002696 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002697 if (ret < 0)
2698 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699 }
Chris Masone66f7092007-04-20 13:16:02 -04002700
Chris Mason5f39d392007-10-15 16:14:19 -04002701 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002702 mid = (c_nritems + 1) / 2;
2703 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002704
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002705 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002706 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02002707 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002708 if (IS_ERR(split))
2709 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002710
Yan, Zhengf0486c62010-05-16 10:46:25 -04002711 root_add_used(root, root->nodesize);
2712
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002713 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002714 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002715 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002716 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002717 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002718 btrfs_set_header_owner(split, root->root_key.objectid);
2719 write_extent_buffer(split, root->fs_info->fsid,
2720 (unsigned long)btrfs_header_fsid(split),
2721 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002722 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2723 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2724 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002725
Jan Schmidtf2304752012-05-26 11:43:17 +02002726 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002727 copy_extent_buffer(split, c,
2728 btrfs_node_key_ptr_offset(0),
2729 btrfs_node_key_ptr_offset(mid),
2730 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2731 btrfs_set_header_nritems(split, c_nritems - mid);
2732 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002733 ret = 0;
2734
Chris Mason5f39d392007-10-15 16:14:19 -04002735 btrfs_mark_buffer_dirty(c);
2736 btrfs_mark_buffer_dirty(split);
2737
Jeff Mahoney143bede2012-03-01 14:56:26 +01002738 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002739 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002740
Chris Mason5de08d72007-02-24 06:24:44 -05002741 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002742 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002743 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002744 free_extent_buffer(c);
2745 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002746 path->slots[level + 1] += 1;
2747 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002748 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002749 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002750 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002751 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002752}
2753
Chris Mason74123bd2007-02-02 11:05:29 -05002754/*
2755 * how many bytes are required to store the items in a leaf. start
2756 * and nr indicate which items in the leaf to check. This totals up the
2757 * space used both by the item structs and the item data
2758 */
Chris Mason5f39d392007-10-15 16:14:19 -04002759static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002760{
2761 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002762 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002763 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002764
2765 if (!nr)
2766 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002767 data_len = btrfs_item_end_nr(l, start);
2768 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002769 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002770 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002771 return data_len;
2772}
2773
Chris Mason74123bd2007-02-02 11:05:29 -05002774/*
Chris Masond4dbff92007-04-04 14:08:15 -04002775 * The space between the end of the leaf items and
2776 * the start of the leaf data. IOW, how much room
2777 * the leaf has left for both items and data
2778 */
Chris Masond3977122009-01-05 21:25:51 -05002779noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002780 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002781{
Chris Mason5f39d392007-10-15 16:14:19 -04002782 int nritems = btrfs_header_nritems(leaf);
2783 int ret;
2784 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2785 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002786 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2787 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002788 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002789 leaf_space_used(leaf, 0, nritems), nritems);
2790 }
2791 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002792}
2793
Chris Mason99d8f832010-07-07 10:51:48 -04002794/*
2795 * min slot controls the lowest index we're willing to push to the
2796 * right. We'll push up to and including min_slot, but no lower
2797 */
Chris Mason44871b12009-03-13 10:04:31 -04002798static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2799 struct btrfs_root *root,
2800 struct btrfs_path *path,
2801 int data_size, int empty,
2802 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002803 int free_space, u32 left_nritems,
2804 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002805{
Chris Mason5f39d392007-10-15 16:14:19 -04002806 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002807 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002808 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002809 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002810 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002811 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002812 int push_space = 0;
2813 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002814 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002815 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002816 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002817 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002818 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002819
Chris Masoncfed81a2012-03-03 07:40:03 -05002820 btrfs_init_map_token(&token);
2821
Chris Mason34a38212007-11-07 13:31:03 -05002822 if (empty)
2823 nr = 0;
2824 else
Chris Mason99d8f832010-07-07 10:51:48 -04002825 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002826
Zheng Yan31840ae2008-09-23 13:14:14 -04002827 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002828 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002829
Chris Mason44871b12009-03-13 10:04:31 -04002830 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002831 i = left_nritems - 1;
2832 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002833 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002834
Zheng Yan31840ae2008-09-23 13:14:14 -04002835 if (!empty && push_items > 0) {
2836 if (path->slots[0] > i)
2837 break;
2838 if (path->slots[0] == i) {
2839 int space = btrfs_leaf_free_space(root, left);
2840 if (space + push_space * 2 > free_space)
2841 break;
2842 }
2843 }
2844
Chris Mason00ec4c52007-02-24 12:47:20 -05002845 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002846 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002847
Chris Masondb945352007-10-15 16:15:53 -04002848 this_item_size = btrfs_item_size(left, item);
2849 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002850 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002851
Chris Mason00ec4c52007-02-24 12:47:20 -05002852 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002853 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002854 if (i == 0)
2855 break;
2856 i--;
Chris Masondb945352007-10-15 16:15:53 -04002857 }
Chris Mason5f39d392007-10-15 16:14:19 -04002858
Chris Mason925baed2008-06-25 16:01:30 -04002859 if (push_items == 0)
2860 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002861
Chris Mason34a38212007-11-07 13:31:03 -05002862 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002863 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002864
Chris Mason00ec4c52007-02-24 12:47:20 -05002865 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002866 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002867
Chris Mason5f39d392007-10-15 16:14:19 -04002868 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002869 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002870
Chris Mason00ec4c52007-02-24 12:47:20 -05002871 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002872 data_end = leaf_data_end(root, right);
2873 memmove_extent_buffer(right,
2874 btrfs_leaf_data(right) + data_end - push_space,
2875 btrfs_leaf_data(right) + data_end,
2876 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2877
Chris Mason00ec4c52007-02-24 12:47:20 -05002878 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002879 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002880 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2881 btrfs_leaf_data(left) + leaf_data_end(root, left),
2882 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002883
2884 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2885 btrfs_item_nr_offset(0),
2886 right_nritems * sizeof(struct btrfs_item));
2887
Chris Mason00ec4c52007-02-24 12:47:20 -05002888 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002889 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2890 btrfs_item_nr_offset(left_nritems - push_items),
2891 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002892
2893 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002894 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002895 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002896 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002897 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002898 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002899 push_space -= btrfs_token_item_size(right, item, &token);
2900 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002901 }
2902
Chris Mason7518a232007-03-12 12:01:18 -04002903 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002904 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002905
Chris Mason34a38212007-11-07 13:31:03 -05002906 if (left_nritems)
2907 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002908 else
2909 clean_tree_block(trans, root, left);
2910
Chris Mason5f39d392007-10-15 16:14:19 -04002911 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002912
Chris Mason5f39d392007-10-15 16:14:19 -04002913 btrfs_item_key(right, &disk_key, 0);
2914 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002915 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002916
Chris Mason00ec4c52007-02-24 12:47:20 -05002917 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002918 if (path->slots[0] >= left_nritems) {
2919 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002920 if (btrfs_header_nritems(path->nodes[0]) == 0)
2921 clean_tree_block(trans, root, path->nodes[0]);
2922 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002923 free_extent_buffer(path->nodes[0]);
2924 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002925 path->slots[1] += 1;
2926 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002927 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002928 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002929 }
2930 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002931
2932out_unlock:
2933 btrfs_tree_unlock(right);
2934 free_extent_buffer(right);
2935 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002936}
Chris Mason925baed2008-06-25 16:01:30 -04002937
Chris Mason00ec4c52007-02-24 12:47:20 -05002938/*
Chris Mason44871b12009-03-13 10:04:31 -04002939 * push some data in the path leaf to the right, trying to free up at
2940 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2941 *
2942 * returns 1 if the push failed because the other node didn't have enough
2943 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002944 *
2945 * this will push starting from min_slot to the end of the leaf. It won't
2946 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002947 */
2948static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002949 *root, struct btrfs_path *path,
2950 int min_data_size, int data_size,
2951 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002952{
2953 struct extent_buffer *left = path->nodes[0];
2954 struct extent_buffer *right;
2955 struct extent_buffer *upper;
2956 int slot;
2957 int free_space;
2958 u32 left_nritems;
2959 int ret;
2960
2961 if (!path->nodes[1])
2962 return 1;
2963
2964 slot = path->slots[1];
2965 upper = path->nodes[1];
2966 if (slot >= btrfs_header_nritems(upper) - 1)
2967 return 1;
2968
2969 btrfs_assert_tree_locked(path->nodes[1]);
2970
2971 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002972 if (right == NULL)
2973 return 1;
2974
Chris Mason44871b12009-03-13 10:04:31 -04002975 btrfs_tree_lock(right);
2976 btrfs_set_lock_blocking(right);
2977
2978 free_space = btrfs_leaf_free_space(root, right);
2979 if (free_space < data_size)
2980 goto out_unlock;
2981
2982 /* cow and double check */
2983 ret = btrfs_cow_block(trans, root, right, upper,
2984 slot + 1, &right);
2985 if (ret)
2986 goto out_unlock;
2987
2988 free_space = btrfs_leaf_free_space(root, right);
2989 if (free_space < data_size)
2990 goto out_unlock;
2991
2992 left_nritems = btrfs_header_nritems(left);
2993 if (left_nritems == 0)
2994 goto out_unlock;
2995
Chris Mason99d8f832010-07-07 10:51:48 -04002996 return __push_leaf_right(trans, root, path, min_data_size, empty,
2997 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002998out_unlock:
2999 btrfs_tree_unlock(right);
3000 free_extent_buffer(right);
3001 return 1;
3002}
3003
3004/*
Chris Mason74123bd2007-02-02 11:05:29 -05003005 * push some data in the path leaf to the left, trying to free up at
3006 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003007 *
3008 * max_slot can put a limit on how far into the leaf we'll push items. The
3009 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3010 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003011 */
Chris Mason44871b12009-03-13 10:04:31 -04003012static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3013 struct btrfs_root *root,
3014 struct btrfs_path *path, int data_size,
3015 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003016 int free_space, u32 right_nritems,
3017 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003018{
Chris Mason5f39d392007-10-15 16:14:19 -04003019 struct btrfs_disk_key disk_key;
3020 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003021 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003022 int push_space = 0;
3023 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003024 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003025 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003026 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003027 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003028 u32 this_item_size;
3029 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003030 struct btrfs_map_token token;
3031
3032 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003033
Chris Mason34a38212007-11-07 13:31:03 -05003034 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003035 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003036 else
Chris Mason99d8f832010-07-07 10:51:48 -04003037 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003038
3039 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003040 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003041
Zheng Yan31840ae2008-09-23 13:14:14 -04003042 if (!empty && push_items > 0) {
3043 if (path->slots[0] < i)
3044 break;
3045 if (path->slots[0] == i) {
3046 int space = btrfs_leaf_free_space(root, right);
3047 if (space + push_space * 2 > free_space)
3048 break;
3049 }
3050 }
3051
Chris Masonbe0e5c02007-01-26 15:51:26 -05003052 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003053 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003054
3055 this_item_size = btrfs_item_size(right, item);
3056 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003057 break;
Chris Masondb945352007-10-15 16:15:53 -04003058
Chris Masonbe0e5c02007-01-26 15:51:26 -05003059 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003060 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003061 }
Chris Masondb945352007-10-15 16:15:53 -04003062
Chris Masonbe0e5c02007-01-26 15:51:26 -05003063 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003064 ret = 1;
3065 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003066 }
Chris Mason34a38212007-11-07 13:31:03 -05003067 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003068 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003069
Chris Masonbe0e5c02007-01-26 15:51:26 -05003070 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003071 copy_extent_buffer(left, right,
3072 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3073 btrfs_item_nr_offset(0),
3074 push_items * sizeof(struct btrfs_item));
3075
Chris Mason123abc82007-03-14 14:14:43 -04003076 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003077 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003078
3079 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003080 leaf_data_end(root, left) - push_space,
3081 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003082 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003083 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003084 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003085 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003086
Chris Masondb945352007-10-15 16:15:53 -04003087 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003088 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003089 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003090
Chris Mason5f39d392007-10-15 16:14:19 -04003091 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003092
Chris Masoncfed81a2012-03-03 07:40:03 -05003093 ioff = btrfs_token_item_offset(left, item, &token);
3094 btrfs_set_token_item_offset(left, item,
3095 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3096 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003097 }
Chris Mason5f39d392007-10-15 16:14:19 -04003098 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003099
3100 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003101 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003102 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3103 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003104 WARN_ON(1);
3105 }
Chris Mason5f39d392007-10-15 16:14:19 -04003106
Chris Mason34a38212007-11-07 13:31:03 -05003107 if (push_items < right_nritems) {
3108 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3109 leaf_data_end(root, right);
3110 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3111 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3112 btrfs_leaf_data(right) +
3113 leaf_data_end(root, right), push_space);
3114
3115 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003116 btrfs_item_nr_offset(push_items),
3117 (btrfs_header_nritems(right) - push_items) *
3118 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003119 }
Yaneef1c492007-11-26 10:58:13 -05003120 right_nritems -= push_items;
3121 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003122 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003123 for (i = 0; i < right_nritems; i++) {
3124 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003125
Chris Masoncfed81a2012-03-03 07:40:03 -05003126 push_space = push_space - btrfs_token_item_size(right,
3127 item, &token);
3128 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003129 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003130
Chris Mason5f39d392007-10-15 16:14:19 -04003131 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003132 if (right_nritems)
3133 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003134 else
3135 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003136
Chris Mason5f39d392007-10-15 16:14:19 -04003137 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003138 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003139
3140 /* then fixup the leaf pointer in the path */
3141 if (path->slots[0] < push_items) {
3142 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003143 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003144 free_extent_buffer(path->nodes[0]);
3145 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003146 path->slots[1] -= 1;
3147 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003148 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003149 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003150 path->slots[0] -= push_items;
3151 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003152 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003153 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003154out:
3155 btrfs_tree_unlock(left);
3156 free_extent_buffer(left);
3157 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003158}
3159
Chris Mason74123bd2007-02-02 11:05:29 -05003160/*
Chris Mason44871b12009-03-13 10:04:31 -04003161 * push some data in the path leaf to the left, trying to free up at
3162 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003163 *
3164 * max_slot can put a limit on how far into the leaf we'll push items. The
3165 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3166 * items
Chris Mason44871b12009-03-13 10:04:31 -04003167 */
3168static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003169 *root, struct btrfs_path *path, int min_data_size,
3170 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003171{
3172 struct extent_buffer *right = path->nodes[0];
3173 struct extent_buffer *left;
3174 int slot;
3175 int free_space;
3176 u32 right_nritems;
3177 int ret = 0;
3178
3179 slot = path->slots[1];
3180 if (slot == 0)
3181 return 1;
3182 if (!path->nodes[1])
3183 return 1;
3184
3185 right_nritems = btrfs_header_nritems(right);
3186 if (right_nritems == 0)
3187 return 1;
3188
3189 btrfs_assert_tree_locked(path->nodes[1]);
3190
3191 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003192 if (left == NULL)
3193 return 1;
3194
Chris Mason44871b12009-03-13 10:04:31 -04003195 btrfs_tree_lock(left);
3196 btrfs_set_lock_blocking(left);
3197
3198 free_space = btrfs_leaf_free_space(root, left);
3199 if (free_space < data_size) {
3200 ret = 1;
3201 goto out;
3202 }
3203
3204 /* cow and double check */
3205 ret = btrfs_cow_block(trans, root, left,
3206 path->nodes[1], slot - 1, &left);
3207 if (ret) {
3208 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003209 if (ret == -ENOSPC)
3210 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003211 goto out;
3212 }
3213
3214 free_space = btrfs_leaf_free_space(root, left);
3215 if (free_space < data_size) {
3216 ret = 1;
3217 goto out;
3218 }
3219
Chris Mason99d8f832010-07-07 10:51:48 -04003220 return __push_leaf_left(trans, root, path, min_data_size,
3221 empty, left, free_space, right_nritems,
3222 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003223out:
3224 btrfs_tree_unlock(left);
3225 free_extent_buffer(left);
3226 return ret;
3227}
3228
3229/*
Chris Mason74123bd2007-02-02 11:05:29 -05003230 * split the path's leaf in two, making sure there is at least data_size
3231 * available for the resulting leaf level of the path.
3232 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003233static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3234 struct btrfs_root *root,
3235 struct btrfs_path *path,
3236 struct extent_buffer *l,
3237 struct extent_buffer *right,
3238 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003239{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003240 int data_copy_size;
3241 int rt_data_off;
3242 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003243 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003244 struct btrfs_map_token token;
3245
3246 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003247
Chris Mason5f39d392007-10-15 16:14:19 -04003248 nritems = nritems - mid;
3249 btrfs_set_header_nritems(right, nritems);
3250 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3251
3252 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3253 btrfs_item_nr_offset(mid),
3254 nritems * sizeof(struct btrfs_item));
3255
3256 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003257 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3258 data_copy_size, btrfs_leaf_data(l) +
3259 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003260
Chris Mason5f39d392007-10-15 16:14:19 -04003261 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3262 btrfs_item_end_nr(l, mid);
3263
3264 for (i = 0; i < nritems; i++) {
3265 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003266 u32 ioff;
3267
Chris Masoncfed81a2012-03-03 07:40:03 -05003268 ioff = btrfs_token_item_offset(right, item, &token);
3269 btrfs_set_token_item_offset(right, item,
3270 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003271 }
Chris Mason74123bd2007-02-02 11:05:29 -05003272
Chris Mason5f39d392007-10-15 16:14:19 -04003273 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003274 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003275 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003276 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003277
3278 btrfs_mark_buffer_dirty(right);
3279 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003280 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003281
Chris Masonbe0e5c02007-01-26 15:51:26 -05003282 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003283 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003284 free_extent_buffer(path->nodes[0]);
3285 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003286 path->slots[0] -= mid;
3287 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003288 } else {
3289 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003290 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003291 }
Chris Mason5f39d392007-10-15 16:14:19 -04003292
Chris Masoneb60cea2007-02-02 09:18:22 -05003293 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003294}
3295
3296/*
Chris Mason99d8f832010-07-07 10:51:48 -04003297 * double splits happen when we need to insert a big item in the middle
3298 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3299 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3300 * A B C
3301 *
3302 * We avoid this by trying to push the items on either side of our target
3303 * into the adjacent leaves. If all goes well we can avoid the double split
3304 * completely.
3305 */
3306static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3307 struct btrfs_root *root,
3308 struct btrfs_path *path,
3309 int data_size)
3310{
3311 int ret;
3312 int progress = 0;
3313 int slot;
3314 u32 nritems;
3315
3316 slot = path->slots[0];
3317
3318 /*
3319 * try to push all the items after our slot into the
3320 * right leaf
3321 */
3322 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3323 if (ret < 0)
3324 return ret;
3325
3326 if (ret == 0)
3327 progress++;
3328
3329 nritems = btrfs_header_nritems(path->nodes[0]);
3330 /*
3331 * our goal is to get our slot at the start or end of a leaf. If
3332 * we've done so we're done
3333 */
3334 if (path->slots[0] == 0 || path->slots[0] == nritems)
3335 return 0;
3336
3337 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3338 return 0;
3339
3340 /* try to push all the items before our slot into the next leaf */
3341 slot = path->slots[0];
3342 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3343 if (ret < 0)
3344 return ret;
3345
3346 if (ret == 0)
3347 progress++;
3348
3349 if (progress)
3350 return 0;
3351 return 1;
3352}
3353
3354/*
Chris Mason44871b12009-03-13 10:04:31 -04003355 * split the path's leaf in two, making sure there is at least data_size
3356 * available for the resulting leaf level of the path.
3357 *
3358 * returns 0 if all went well and < 0 on failure.
3359 */
3360static noinline int split_leaf(struct btrfs_trans_handle *trans,
3361 struct btrfs_root *root,
3362 struct btrfs_key *ins_key,
3363 struct btrfs_path *path, int data_size,
3364 int extend)
3365{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003366 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003367 struct extent_buffer *l;
3368 u32 nritems;
3369 int mid;
3370 int slot;
3371 struct extent_buffer *right;
3372 int ret = 0;
3373 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003374 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003375 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003376 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003377
Yan, Zhenga5719522009-09-24 09:17:31 -04003378 l = path->nodes[0];
3379 slot = path->slots[0];
3380 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3381 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3382 return -EOVERFLOW;
3383
Chris Mason44871b12009-03-13 10:04:31 -04003384 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003385 if (data_size) {
3386 wret = push_leaf_right(trans, root, path, data_size,
3387 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003388 if (wret < 0)
3389 return wret;
3390 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003391 wret = push_leaf_left(trans, root, path, data_size,
3392 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003393 if (wret < 0)
3394 return wret;
3395 }
3396 l = path->nodes[0];
3397
3398 /* did the pushes work? */
3399 if (btrfs_leaf_free_space(root, l) >= data_size)
3400 return 0;
3401 }
3402
3403 if (!path->nodes[1]) {
3404 ret = insert_new_root(trans, root, path, 1);
3405 if (ret)
3406 return ret;
3407 }
3408again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003409 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003410 l = path->nodes[0];
3411 slot = path->slots[0];
3412 nritems = btrfs_header_nritems(l);
3413 mid = (nritems + 1) / 2;
3414
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003415 if (mid <= slot) {
3416 if (nritems == 1 ||
3417 leaf_space_used(l, mid, nritems - mid) + data_size >
3418 BTRFS_LEAF_DATA_SIZE(root)) {
3419 if (slot >= nritems) {
3420 split = 0;
3421 } else {
3422 mid = slot;
3423 if (mid != nritems &&
3424 leaf_space_used(l, mid, nritems - mid) +
3425 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003426 if (data_size && !tried_avoid_double)
3427 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003428 split = 2;
3429 }
3430 }
3431 }
3432 } else {
3433 if (leaf_space_used(l, 0, mid) + data_size >
3434 BTRFS_LEAF_DATA_SIZE(root)) {
3435 if (!extend && data_size && slot == 0) {
3436 split = 0;
3437 } else if ((extend || !data_size) && slot == 0) {
3438 mid = 1;
3439 } else {
3440 mid = slot;
3441 if (mid != nritems &&
3442 leaf_space_used(l, mid, nritems - mid) +
3443 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003444 if (data_size && !tried_avoid_double)
3445 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003446 split = 2 ;
3447 }
3448 }
3449 }
3450 }
3451
3452 if (split == 0)
3453 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3454 else
3455 btrfs_item_key(l, &disk_key, mid);
3456
3457 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003458 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003459 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003460 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003461 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003462
3463 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003464
3465 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3466 btrfs_set_header_bytenr(right, right->start);
3467 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003468 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003469 btrfs_set_header_owner(right, root->root_key.objectid);
3470 btrfs_set_header_level(right, 0);
3471 write_extent_buffer(right, root->fs_info->fsid,
3472 (unsigned long)btrfs_header_fsid(right),
3473 BTRFS_FSID_SIZE);
3474
3475 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3476 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3477 BTRFS_UUID_SIZE);
3478
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003479 if (split == 0) {
3480 if (mid <= slot) {
3481 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003482 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003483 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003484 btrfs_tree_unlock(path->nodes[0]);
3485 free_extent_buffer(path->nodes[0]);
3486 path->nodes[0] = right;
3487 path->slots[0] = 0;
3488 path->slots[1] += 1;
3489 } else {
3490 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003491 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003492 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003493 btrfs_tree_unlock(path->nodes[0]);
3494 free_extent_buffer(path->nodes[0]);
3495 path->nodes[0] = right;
3496 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003497 if (path->slots[1] == 0)
3498 fixup_low_keys(trans, root, path,
3499 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003500 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003501 btrfs_mark_buffer_dirty(right);
3502 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003503 }
3504
Jeff Mahoney143bede2012-03-01 14:56:26 +01003505 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003506
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003507 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003508 BUG_ON(num_doubles != 0);
3509 num_doubles++;
3510 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003511 }
Chris Mason44871b12009-03-13 10:04:31 -04003512
Jeff Mahoney143bede2012-03-01 14:56:26 +01003513 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003514
3515push_for_double:
3516 push_for_double_split(trans, root, path, data_size);
3517 tried_avoid_double = 1;
3518 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3519 return 0;
3520 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003521}
3522
Yan, Zhengad48fd752009-11-12 09:33:58 +00003523static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3524 struct btrfs_root *root,
3525 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003526{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003527 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003528 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003529 struct btrfs_file_extent_item *fi;
3530 u64 extent_len = 0;
3531 u32 item_size;
3532 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003533
3534 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003535 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3536
3537 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3538 key.type != BTRFS_EXTENT_CSUM_KEY);
3539
3540 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3541 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003542
3543 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003544 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3545 fi = btrfs_item_ptr(leaf, path->slots[0],
3546 struct btrfs_file_extent_item);
3547 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3548 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003549 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003550
Chris Mason459931e2008-12-10 09:10:46 -05003551 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003552 path->search_for_split = 1;
3553 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003554 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003555 if (ret < 0)
3556 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003557
Yan, Zhengad48fd752009-11-12 09:33:58 +00003558 ret = -EAGAIN;
3559 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003560 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003561 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3562 goto err;
3563
Chris Mason109f6ae2010-04-02 09:20:18 -04003564 /* the leaf has changed, it now has room. return now */
3565 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3566 goto err;
3567
Yan, Zhengad48fd752009-11-12 09:33:58 +00003568 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3569 fi = btrfs_item_ptr(leaf, path->slots[0],
3570 struct btrfs_file_extent_item);
3571 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3572 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003573 }
3574
Chris Masonb9473432009-03-13 11:00:37 -04003575 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003576 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003577 if (ret)
3578 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003579
Yan, Zhengad48fd752009-11-12 09:33:58 +00003580 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003581 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003582 return 0;
3583err:
3584 path->keep_locks = 0;
3585 return ret;
3586}
3587
3588static noinline int split_item(struct btrfs_trans_handle *trans,
3589 struct btrfs_root *root,
3590 struct btrfs_path *path,
3591 struct btrfs_key *new_key,
3592 unsigned long split_offset)
3593{
3594 struct extent_buffer *leaf;
3595 struct btrfs_item *item;
3596 struct btrfs_item *new_item;
3597 int slot;
3598 char *buf;
3599 u32 nritems;
3600 u32 item_size;
3601 u32 orig_offset;
3602 struct btrfs_disk_key disk_key;
3603
Chris Masonb9473432009-03-13 11:00:37 -04003604 leaf = path->nodes[0];
3605 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3606
Chris Masonb4ce94d2009-02-04 09:25:08 -05003607 btrfs_set_path_blocking(path);
3608
Chris Mason459931e2008-12-10 09:10:46 -05003609 item = btrfs_item_nr(leaf, path->slots[0]);
3610 orig_offset = btrfs_item_offset(leaf, item);
3611 item_size = btrfs_item_size(leaf, item);
3612
Chris Mason459931e2008-12-10 09:10:46 -05003613 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003614 if (!buf)
3615 return -ENOMEM;
3616
Chris Mason459931e2008-12-10 09:10:46 -05003617 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3618 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003619
Chris Mason459931e2008-12-10 09:10:46 -05003620 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003621 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003622 if (slot != nritems) {
3623 /* shift the items */
3624 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003625 btrfs_item_nr_offset(slot),
3626 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003627 }
3628
3629 btrfs_cpu_key_to_disk(&disk_key, new_key);
3630 btrfs_set_item_key(leaf, &disk_key, slot);
3631
3632 new_item = btrfs_item_nr(leaf, slot);
3633
3634 btrfs_set_item_offset(leaf, new_item, orig_offset);
3635 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3636
3637 btrfs_set_item_offset(leaf, item,
3638 orig_offset + item_size - split_offset);
3639 btrfs_set_item_size(leaf, item, split_offset);
3640
3641 btrfs_set_header_nritems(leaf, nritems + 1);
3642
3643 /* write the data for the start of the original item */
3644 write_extent_buffer(leaf, buf,
3645 btrfs_item_ptr_offset(leaf, path->slots[0]),
3646 split_offset);
3647
3648 /* write the data for the new item */
3649 write_extent_buffer(leaf, buf + split_offset,
3650 btrfs_item_ptr_offset(leaf, slot),
3651 item_size - split_offset);
3652 btrfs_mark_buffer_dirty(leaf);
3653
Yan, Zhengad48fd752009-11-12 09:33:58 +00003654 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003655 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003656 return 0;
3657}
3658
3659/*
3660 * This function splits a single item into two items,
3661 * giving 'new_key' to the new item and splitting the
3662 * old one at split_offset (from the start of the item).
3663 *
3664 * The path may be released by this operation. After
3665 * the split, the path is pointing to the old item. The
3666 * new item is going to be in the same node as the old one.
3667 *
3668 * Note, the item being split must be smaller enough to live alone on
3669 * a tree block with room for one extra struct btrfs_item
3670 *
3671 * This allows us to split the item in place, keeping a lock on the
3672 * leaf the entire time.
3673 */
3674int btrfs_split_item(struct btrfs_trans_handle *trans,
3675 struct btrfs_root *root,
3676 struct btrfs_path *path,
3677 struct btrfs_key *new_key,
3678 unsigned long split_offset)
3679{
3680 int ret;
3681 ret = setup_leaf_for_split(trans, root, path,
3682 sizeof(struct btrfs_item));
3683 if (ret)
3684 return ret;
3685
3686 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003687 return ret;
3688}
3689
3690/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003691 * This function duplicate a item, giving 'new_key' to the new item.
3692 * It guarantees both items live in the same tree leaf and the new item
3693 * is contiguous with the original item.
3694 *
3695 * This allows us to split file extent in place, keeping a lock on the
3696 * leaf the entire time.
3697 */
3698int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root,
3700 struct btrfs_path *path,
3701 struct btrfs_key *new_key)
3702{
3703 struct extent_buffer *leaf;
3704 int ret;
3705 u32 item_size;
3706
3707 leaf = path->nodes[0];
3708 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3709 ret = setup_leaf_for_split(trans, root, path,
3710 item_size + sizeof(struct btrfs_item));
3711 if (ret)
3712 return ret;
3713
3714 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003715 setup_items_for_insert(trans, root, path, new_key, &item_size,
3716 item_size, item_size +
3717 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003718 leaf = path->nodes[0];
3719 memcpy_extent_buffer(leaf,
3720 btrfs_item_ptr_offset(leaf, path->slots[0]),
3721 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3722 item_size);
3723 return 0;
3724}
3725
3726/*
Chris Masond352ac62008-09-29 15:18:18 -04003727 * make the item pointed to by the path smaller. new_size indicates
3728 * how small to make it, and from_end tells us if we just chop bytes
3729 * off the end of the item or if we shift the item to chop bytes off
3730 * the front.
3731 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003732void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3733 struct btrfs_root *root,
3734 struct btrfs_path *path,
3735 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003736{
Chris Masonb18c6682007-04-17 13:26:50 -04003737 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003738 struct extent_buffer *leaf;
3739 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003740 u32 nritems;
3741 unsigned int data_end;
3742 unsigned int old_data_start;
3743 unsigned int old_size;
3744 unsigned int size_diff;
3745 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003746 struct btrfs_map_token token;
3747
3748 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003749
Chris Mason5f39d392007-10-15 16:14:19 -04003750 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003751 slot = path->slots[0];
3752
3753 old_size = btrfs_item_size_nr(leaf, slot);
3754 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003755 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003756
Chris Mason5f39d392007-10-15 16:14:19 -04003757 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003758 data_end = leaf_data_end(root, leaf);
3759
Chris Mason5f39d392007-10-15 16:14:19 -04003760 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003761
Chris Masonb18c6682007-04-17 13:26:50 -04003762 size_diff = old_size - new_size;
3763
3764 BUG_ON(slot < 0);
3765 BUG_ON(slot >= nritems);
3766
3767 /*
3768 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3769 */
3770 /* first correct the data pointers */
3771 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003772 u32 ioff;
3773 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003774
Chris Masoncfed81a2012-03-03 07:40:03 -05003775 ioff = btrfs_token_item_offset(leaf, item, &token);
3776 btrfs_set_token_item_offset(leaf, item,
3777 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003778 }
Chris Masondb945352007-10-15 16:15:53 -04003779
Chris Masonb18c6682007-04-17 13:26:50 -04003780 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003781 if (from_end) {
3782 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3783 data_end + size_diff, btrfs_leaf_data(leaf) +
3784 data_end, old_data_start + new_size - data_end);
3785 } else {
3786 struct btrfs_disk_key disk_key;
3787 u64 offset;
3788
3789 btrfs_item_key(leaf, &disk_key, slot);
3790
3791 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3792 unsigned long ptr;
3793 struct btrfs_file_extent_item *fi;
3794
3795 fi = btrfs_item_ptr(leaf, slot,
3796 struct btrfs_file_extent_item);
3797 fi = (struct btrfs_file_extent_item *)(
3798 (unsigned long)fi - size_diff);
3799
3800 if (btrfs_file_extent_type(leaf, fi) ==
3801 BTRFS_FILE_EXTENT_INLINE) {
3802 ptr = btrfs_item_ptr_offset(leaf, slot);
3803 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003804 (unsigned long)fi,
3805 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003806 disk_bytenr));
3807 }
3808 }
3809
3810 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3811 data_end + size_diff, btrfs_leaf_data(leaf) +
3812 data_end, old_data_start - data_end);
3813
3814 offset = btrfs_disk_key_offset(&disk_key);
3815 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3816 btrfs_set_item_key(leaf, &disk_key, slot);
3817 if (slot == 0)
3818 fixup_low_keys(trans, root, path, &disk_key, 1);
3819 }
Chris Mason5f39d392007-10-15 16:14:19 -04003820
3821 item = btrfs_item_nr(leaf, slot);
3822 btrfs_set_item_size(leaf, item, new_size);
3823 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003824
Chris Mason5f39d392007-10-15 16:14:19 -04003825 if (btrfs_leaf_free_space(root, leaf) < 0) {
3826 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003827 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003828 }
Chris Masonb18c6682007-04-17 13:26:50 -04003829}
3830
Chris Masond352ac62008-09-29 15:18:18 -04003831/*
3832 * make the item pointed to by the path bigger, data_size is the new size.
3833 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003834void btrfs_extend_item(struct btrfs_trans_handle *trans,
3835 struct btrfs_root *root, struct btrfs_path *path,
3836 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003837{
Chris Mason6567e832007-04-16 09:22:45 -04003838 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003839 struct extent_buffer *leaf;
3840 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003841 u32 nritems;
3842 unsigned int data_end;
3843 unsigned int old_data;
3844 unsigned int old_size;
3845 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003846 struct btrfs_map_token token;
3847
3848 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003849
Chris Mason5f39d392007-10-15 16:14:19 -04003850 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003851
Chris Mason5f39d392007-10-15 16:14:19 -04003852 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003853 data_end = leaf_data_end(root, leaf);
3854
Chris Mason5f39d392007-10-15 16:14:19 -04003855 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3856 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003857 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003858 }
Chris Mason6567e832007-04-16 09:22:45 -04003859 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003860 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003861
3862 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003863 if (slot >= nritems) {
3864 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003865 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3866 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003867 BUG_ON(1);
3868 }
Chris Mason6567e832007-04-16 09:22:45 -04003869
3870 /*
3871 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3872 */
3873 /* first correct the data pointers */
3874 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003875 u32 ioff;
3876 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003877
Chris Masoncfed81a2012-03-03 07:40:03 -05003878 ioff = btrfs_token_item_offset(leaf, item, &token);
3879 btrfs_set_token_item_offset(leaf, item,
3880 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003881 }
Chris Mason5f39d392007-10-15 16:14:19 -04003882
Chris Mason6567e832007-04-16 09:22:45 -04003883 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003884 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003885 data_end - data_size, btrfs_leaf_data(leaf) +
3886 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003887
Chris Mason6567e832007-04-16 09:22:45 -04003888 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003889 old_size = btrfs_item_size_nr(leaf, slot);
3890 item = btrfs_item_nr(leaf, slot);
3891 btrfs_set_item_size(leaf, item, old_size + data_size);
3892 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003893
Chris Mason5f39d392007-10-15 16:14:19 -04003894 if (btrfs_leaf_free_space(root, leaf) < 0) {
3895 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003896 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003897 }
Chris Mason6567e832007-04-16 09:22:45 -04003898}
3899
Chris Mason74123bd2007-02-02 11:05:29 -05003900/*
Chris Masond352ac62008-09-29 15:18:18 -04003901 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003902 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003903 * Returns the number of keys that were inserted.
3904 */
3905int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3906 struct btrfs_root *root,
3907 struct btrfs_path *path,
3908 struct btrfs_key *cpu_key, u32 *data_size,
3909 int nr)
3910{
3911 struct extent_buffer *leaf;
3912 struct btrfs_item *item;
3913 int ret = 0;
3914 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003915 int i;
3916 u32 nritems;
3917 u32 total_data = 0;
3918 u32 total_size = 0;
3919 unsigned int data_end;
3920 struct btrfs_disk_key disk_key;
3921 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003922 struct btrfs_map_token token;
3923
3924 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003925
Yan Zheng87b29b22008-12-17 10:21:48 -05003926 for (i = 0; i < nr; i++) {
3927 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3928 BTRFS_LEAF_DATA_SIZE(root)) {
3929 break;
3930 nr = i;
3931 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003932 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003933 total_size += data_size[i] + sizeof(struct btrfs_item);
3934 }
3935 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003936
Josef Bacikf3465ca2008-11-12 14:19:50 -05003937 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3938 if (ret == 0)
3939 return -EEXIST;
3940 if (ret < 0)
3941 goto out;
3942
Josef Bacikf3465ca2008-11-12 14:19:50 -05003943 leaf = path->nodes[0];
3944
3945 nritems = btrfs_header_nritems(leaf);
3946 data_end = leaf_data_end(root, leaf);
3947
3948 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3949 for (i = nr; i >= 0; i--) {
3950 total_data -= data_size[i];
3951 total_size -= data_size[i] + sizeof(struct btrfs_item);
3952 if (total_size < btrfs_leaf_free_space(root, leaf))
3953 break;
3954 }
3955 nr = i;
3956 }
3957
3958 slot = path->slots[0];
3959 BUG_ON(slot < 0);
3960
3961 if (slot != nritems) {
3962 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3963
3964 item = btrfs_item_nr(leaf, slot);
3965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3966
3967 /* figure out how many keys we can insert in here */
3968 total_data = data_size[0];
3969 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003970 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003971 break;
3972 total_data += data_size[i];
3973 }
3974 nr = i;
3975
3976 if (old_data < data_end) {
3977 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003978 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003979 slot, old_data, data_end);
3980 BUG_ON(1);
3981 }
3982 /*
3983 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3984 */
3985 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003986 for (i = slot; i < nritems; i++) {
3987 u32 ioff;
3988
3989 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003990 ioff = btrfs_token_item_offset(leaf, item, &token);
3991 btrfs_set_token_item_offset(leaf, item,
3992 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003993 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003994 /* shift the items */
3995 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3996 btrfs_item_nr_offset(slot),
3997 (nritems - slot) * sizeof(struct btrfs_item));
3998
3999 /* shift the data */
4000 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4001 data_end - total_data, btrfs_leaf_data(leaf) +
4002 data_end, old_data - data_end);
4003 data_end = old_data;
4004 } else {
4005 /*
4006 * this sucks but it has to be done, if we are inserting at
4007 * the end of the leaf only insert 1 of the items, since we
4008 * have no way of knowing whats on the next leaf and we'd have
4009 * to drop our current locks to figure it out
4010 */
4011 nr = 1;
4012 }
4013
4014 /* setup the item for the new data */
4015 for (i = 0; i < nr; i++) {
4016 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4017 btrfs_set_item_key(leaf, &disk_key, slot + i);
4018 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004019 btrfs_set_token_item_offset(leaf, item,
4020 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004021 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004022 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004023 }
4024 btrfs_set_header_nritems(leaf, nritems + nr);
4025 btrfs_mark_buffer_dirty(leaf);
4026
4027 ret = 0;
4028 if (slot == 0) {
4029 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004030 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004031 }
4032
4033 if (btrfs_leaf_free_space(root, leaf) < 0) {
4034 btrfs_print_leaf(root, leaf);
4035 BUG();
4036 }
4037out:
4038 if (!ret)
4039 ret = nr;
4040 return ret;
4041}
4042
4043/*
Chris Mason44871b12009-03-13 10:04:31 -04004044 * this is a helper for btrfs_insert_empty_items, the main goal here is
4045 * to save stack depth by doing the bulk of the work in a function
4046 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004047 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004048void setup_items_for_insert(struct btrfs_trans_handle *trans,
4049 struct btrfs_root *root, struct btrfs_path *path,
4050 struct btrfs_key *cpu_key, u32 *data_size,
4051 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004052{
Chris Mason5f39d392007-10-15 16:14:19 -04004053 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004054 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004055 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004056 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004057 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004058 struct extent_buffer *leaf;
4059 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004060 struct btrfs_map_token token;
4061
4062 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004063
Chris Mason5f39d392007-10-15 16:14:19 -04004064 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004065 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004066
Chris Mason5f39d392007-10-15 16:14:19 -04004067 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004068 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004069
Chris Masonf25956c2008-09-12 15:32:53 -04004070 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004071 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004072 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004073 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004074 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004075 }
Chris Mason5f39d392007-10-15 16:14:19 -04004076
Chris Masonbe0e5c02007-01-26 15:51:26 -05004077 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004078 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004079
Chris Mason5f39d392007-10-15 16:14:19 -04004080 if (old_data < data_end) {
4081 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004082 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004083 slot, old_data, data_end);
4084 BUG_ON(1);
4085 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004086 /*
4087 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4088 */
4089 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004090 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004091 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004092
Chris Mason5f39d392007-10-15 16:14:19 -04004093 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004094 ioff = btrfs_token_item_offset(leaf, item, &token);
4095 btrfs_set_token_item_offset(leaf, item,
4096 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004097 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004098 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004099 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004100 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004101 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004102
4103 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004104 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004105 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004106 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004107 data_end = old_data;
4108 }
Chris Mason5f39d392007-10-15 16:14:19 -04004109
Chris Mason62e27492007-03-15 12:56:47 -04004110 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004111 for (i = 0; i < nr; i++) {
4112 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4113 btrfs_set_item_key(leaf, &disk_key, slot + i);
4114 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004115 btrfs_set_token_item_offset(leaf, item,
4116 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004117 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004118 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004119 }
Chris Mason44871b12009-03-13 10:04:31 -04004120
Chris Mason9c583092008-01-29 15:15:18 -05004121 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004122
Chris Mason5a01a2e2008-01-30 11:43:54 -05004123 if (slot == 0) {
4124 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004125 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004126 }
Chris Masonb9473432009-03-13 11:00:37 -04004127 btrfs_unlock_up_safe(path, 1);
4128 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004129
Chris Mason5f39d392007-10-15 16:14:19 -04004130 if (btrfs_leaf_free_space(root, leaf) < 0) {
4131 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004132 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004133 }
Chris Mason44871b12009-03-13 10:04:31 -04004134}
4135
4136/*
4137 * Given a key and some data, insert items into the tree.
4138 * This does all the path init required, making room in the tree if needed.
4139 */
4140int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4141 struct btrfs_root *root,
4142 struct btrfs_path *path,
4143 struct btrfs_key *cpu_key, u32 *data_size,
4144 int nr)
4145{
Chris Mason44871b12009-03-13 10:04:31 -04004146 int ret = 0;
4147 int slot;
4148 int i;
4149 u32 total_size = 0;
4150 u32 total_data = 0;
4151
4152 for (i = 0; i < nr; i++)
4153 total_data += data_size[i];
4154
4155 total_size = total_data + (nr * sizeof(struct btrfs_item));
4156 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4157 if (ret == 0)
4158 return -EEXIST;
4159 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004160 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004161
Chris Mason44871b12009-03-13 10:04:31 -04004162 slot = path->slots[0];
4163 BUG_ON(slot < 0);
4164
Jeff Mahoney143bede2012-03-01 14:56:26 +01004165 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004166 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004167 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004168}
4169
4170/*
4171 * Given a key and some data, insert an item into the tree.
4172 * This does all the path init required, making room in the tree if needed.
4173 */
Chris Masone089f052007-03-16 16:20:31 -04004174int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4175 *root, struct btrfs_key *cpu_key, void *data, u32
4176 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004177{
4178 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004179 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004180 struct extent_buffer *leaf;
4181 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004182
Chris Mason2c90e5d2007-04-02 10:50:19 -04004183 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004184 if (!path)
4185 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004186 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004187 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004188 leaf = path->nodes[0];
4189 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4190 write_extent_buffer(leaf, data, ptr, data_size);
4191 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004192 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004193 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004194 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004195}
4196
Chris Mason74123bd2007-02-02 11:05:29 -05004197/*
Chris Mason5de08d72007-02-24 06:24:44 -05004198 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004199 *
Chris Masond352ac62008-09-29 15:18:18 -04004200 * the tree should have been previously balanced so the deletion does not
4201 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004202 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004203static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004204 struct btrfs_path *path, int level, int slot,
4205 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004206{
Chris Mason5f39d392007-10-15 16:14:19 -04004207 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004208 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004209 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004210
Chris Mason5f39d392007-10-15 16:14:19 -04004211 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004212 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004213 if (tree_mod_log && level)
4214 tree_mod_log_eb_move(root->fs_info, parent, slot,
4215 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004216 memmove_extent_buffer(parent,
4217 btrfs_node_key_ptr_offset(slot),
4218 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004219 sizeof(struct btrfs_key_ptr) *
4220 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05004221 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004222
4223 if (tree_mod_log && level) {
4224 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4225 MOD_LOG_KEY_REMOVE);
4226 BUG_ON(ret < 0);
4227 }
4228
Chris Mason7518a232007-03-12 12:01:18 -04004229 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004230 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004231 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004232 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004233 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004234 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004235 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004236 struct btrfs_disk_key disk_key;
4237
4238 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004239 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004240 }
Chris Masond6025572007-03-30 14:27:56 -04004241 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004242}
4243
Chris Mason74123bd2007-02-02 11:05:29 -05004244/*
Chris Mason323ac952008-10-01 19:05:46 -04004245 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004246 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004247 *
4248 * This deletes the pointer in path->nodes[1] and frees the leaf
4249 * block extent. zero is returned if it all worked out, < 0 otherwise.
4250 *
4251 * The path must have already been setup for deleting the leaf, including
4252 * all the proper balancing. path->nodes[1] must be locked.
4253 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004254static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4255 struct btrfs_root *root,
4256 struct btrfs_path *path,
4257 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004258{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004259 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004260 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004261
Chris Mason4d081c42009-02-04 09:31:28 -05004262 /*
4263 * btrfs_free_extent is expensive, we want to make sure we
4264 * aren't holding any locks when we call it
4265 */
4266 btrfs_unlock_up_safe(path, 0);
4267
Yan, Zhengf0486c62010-05-16 10:46:25 -04004268 root_sub_used(root, leaf->len);
4269
Josef Bacik3083ee22012-03-09 16:01:49 -05004270 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004271 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004272 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004273}
4274/*
Chris Mason74123bd2007-02-02 11:05:29 -05004275 * delete the item at the leaf level in path. If that empties
4276 * the leaf, remove it from the tree
4277 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004278int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4279 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004280{
Chris Mason5f39d392007-10-15 16:14:19 -04004281 struct extent_buffer *leaf;
4282 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004283 int last_off;
4284 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004285 int ret = 0;
4286 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004287 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004288 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004289 struct btrfs_map_token token;
4290
4291 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004292
Chris Mason5f39d392007-10-15 16:14:19 -04004293 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004294 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4295
4296 for (i = 0; i < nr; i++)
4297 dsize += btrfs_item_size_nr(leaf, slot + i);
4298
Chris Mason5f39d392007-10-15 16:14:19 -04004299 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004300
Chris Mason85e21ba2008-01-29 15:11:36 -05004301 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004302 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004303
4304 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004305 data_end + dsize,
4306 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004307 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004308
Chris Mason85e21ba2008-01-29 15:11:36 -05004309 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004310 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004311
Chris Mason5f39d392007-10-15 16:14:19 -04004312 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004313 ioff = btrfs_token_item_offset(leaf, item, &token);
4314 btrfs_set_token_item_offset(leaf, item,
4315 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004316 }
Chris Masondb945352007-10-15 16:15:53 -04004317
Chris Mason5f39d392007-10-15 16:14:19 -04004318 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004319 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004320 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004321 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004322 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004323 btrfs_set_header_nritems(leaf, nritems - nr);
4324 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004325
Chris Mason74123bd2007-02-02 11:05:29 -05004326 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004327 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004328 if (leaf == root->node) {
4329 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004330 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004331 btrfs_set_path_blocking(path);
4332 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004333 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004334 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004335 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004336 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004337 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004338 struct btrfs_disk_key disk_key;
4339
4340 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004341 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004342 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004343
Chris Mason74123bd2007-02-02 11:05:29 -05004344 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004345 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004346 /* push_leaf_left fixes the path.
4347 * make sure the path still points to our leaf
4348 * for possible call to del_ptr below
4349 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004350 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004351 extent_buffer_get(leaf);
4352
Chris Masonb9473432009-03-13 11:00:37 -04004353 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004354 wret = push_leaf_left(trans, root, path, 1, 1,
4355 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004356 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004357 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004358
4359 if (path->nodes[0] == leaf &&
4360 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004361 wret = push_leaf_right(trans, root, path, 1,
4362 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004363 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004364 ret = wret;
4365 }
Chris Mason5f39d392007-10-15 16:14:19 -04004366
4367 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004368 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004369 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004370 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004371 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004372 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004373 /* if we're still in the path, make sure
4374 * we're dirty. Otherwise, one of the
4375 * push_leaf functions must have already
4376 * dirtied this buffer
4377 */
4378 if (path->nodes[0] == leaf)
4379 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004380 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004381 }
Chris Masond5719762007-03-23 10:01:08 -04004382 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004383 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004384 }
4385 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004386 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004387}
4388
Chris Mason97571fd2007-02-24 13:39:08 -05004389/*
Chris Mason925baed2008-06-25 16:01:30 -04004390 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004391 * returns 0 if it found something or 1 if there are no lesser leaves.
4392 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004393 *
4394 * This may release the path, and so you may lose any locks held at the
4395 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004396 */
4397int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4398{
Chris Mason925baed2008-06-25 16:01:30 -04004399 struct btrfs_key key;
4400 struct btrfs_disk_key found_key;
4401 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004402
Chris Mason925baed2008-06-25 16:01:30 -04004403 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004404
Chris Mason925baed2008-06-25 16:01:30 -04004405 if (key.offset > 0)
4406 key.offset--;
4407 else if (key.type > 0)
4408 key.type--;
4409 else if (key.objectid > 0)
4410 key.objectid--;
4411 else
4412 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004413
David Sterbab3b4aa72011-04-21 01:20:15 +02004414 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004415 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4416 if (ret < 0)
4417 return ret;
4418 btrfs_item_key(path->nodes[0], &found_key, 0);
4419 ret = comp_keys(&found_key, &key);
4420 if (ret < 0)
4421 return 0;
4422 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004423}
4424
Chris Mason3f157a22008-06-25 16:01:31 -04004425/*
4426 * A helper function to walk down the tree starting at min_key, and looking
4427 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004428 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004429 *
4430 * This does not cow, but it does stuff the starting key it finds back
4431 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4432 * key and get a writable path.
4433 *
4434 * This does lock as it descends, and path->keep_locks should be set
4435 * to 1 by the caller.
4436 *
4437 * This honors path->lowest_level to prevent descent past a given level
4438 * of the tree.
4439 *
Chris Masond352ac62008-09-29 15:18:18 -04004440 * min_trans indicates the oldest transaction that you are interested
4441 * in walking through. Any nodes or leaves older than min_trans are
4442 * skipped over (without reading them).
4443 *
Chris Mason3f157a22008-06-25 16:01:31 -04004444 * returns zero if something useful was found, < 0 on error and 1 if there
4445 * was nothing in the tree that matched the search criteria.
4446 */
4447int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004448 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004449 struct btrfs_path *path, int cache_only,
4450 u64 min_trans)
4451{
4452 struct extent_buffer *cur;
4453 struct btrfs_key found_key;
4454 int slot;
Yan96524802008-07-24 12:19:49 -04004455 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004456 u32 nritems;
4457 int level;
4458 int ret = 1;
4459
Chris Mason934d3752008-12-08 16:43:10 -05004460 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004461again:
Chris Masonbd681512011-07-16 15:23:14 -04004462 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004463 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004464 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004465 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004466 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004467
4468 if (btrfs_header_generation(cur) < min_trans) {
4469 ret = 1;
4470 goto out;
4471 }
Chris Masond3977122009-01-05 21:25:51 -05004472 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004473 nritems = btrfs_header_nritems(cur);
4474 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004475 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004476
Chris Mason323ac952008-10-01 19:05:46 -04004477 /* at the lowest level, we're done, setup the path and exit */
4478 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004479 if (slot >= nritems)
4480 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004481 ret = 0;
4482 path->slots[level] = slot;
4483 btrfs_item_key_to_cpu(cur, &found_key, slot);
4484 goto out;
4485 }
Yan96524802008-07-24 12:19:49 -04004486 if (sret && slot > 0)
4487 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004488 /*
4489 * check this node pointer against the cache_only and
4490 * min_trans parameters. If it isn't in cache or is too
4491 * old, skip to the next one.
4492 */
Chris Masond3977122009-01-05 21:25:51 -05004493 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004494 u64 blockptr;
4495 u64 gen;
4496 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004497 struct btrfs_disk_key disk_key;
4498
Chris Mason3f157a22008-06-25 16:01:31 -04004499 blockptr = btrfs_node_blockptr(cur, slot);
4500 gen = btrfs_node_ptr_generation(cur, slot);
4501 if (gen < min_trans) {
4502 slot++;
4503 continue;
4504 }
4505 if (!cache_only)
4506 break;
4507
Chris Masone02119d2008-09-05 16:13:11 -04004508 if (max_key) {
4509 btrfs_node_key(cur, &disk_key, slot);
4510 if (comp_keys(&disk_key, max_key) >= 0) {
4511 ret = 1;
4512 goto out;
4513 }
4514 }
4515
Chris Mason3f157a22008-06-25 16:01:31 -04004516 tmp = btrfs_find_tree_block(root, blockptr,
4517 btrfs_level_size(root, level - 1));
4518
Chris Masonb9fab912012-05-06 07:23:47 -04004519 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004520 free_extent_buffer(tmp);
4521 break;
4522 }
4523 if (tmp)
4524 free_extent_buffer(tmp);
4525 slot++;
4526 }
Chris Masone02119d2008-09-05 16:13:11 -04004527find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004528 /*
4529 * we didn't find a candidate key in this node, walk forward
4530 * and find another one
4531 */
4532 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004533 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004534 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004535 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004536 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004537 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004538 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004539 goto again;
4540 } else {
4541 goto out;
4542 }
4543 }
4544 /* save our key for returning back */
4545 btrfs_node_key_to_cpu(cur, &found_key, slot);
4546 path->slots[level] = slot;
4547 if (level == path->lowest_level) {
4548 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004549 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004550 goto out;
4551 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004552 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004553 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004554 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004555
Chris Masonbd681512011-07-16 15:23:14 -04004556 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004557
Chris Masonbd681512011-07-16 15:23:14 -04004558 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004559 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004560 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004561 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004562 }
4563out:
4564 if (ret == 0)
4565 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004566 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004567 return ret;
4568}
4569
4570/*
4571 * this is similar to btrfs_next_leaf, but does not try to preserve
4572 * and fixup the path. It looks for and returns the next key in the
4573 * tree based on the current path and the cache_only and min_trans
4574 * parameters.
4575 *
4576 * 0 is returned if another key is found, < 0 if there are any errors
4577 * and 1 is returned if there are no higher keys in the tree
4578 *
4579 * path->keep_locks should be set to 1 on the search made before
4580 * calling this function.
4581 */
Chris Masone7a84562008-06-25 16:01:31 -04004582int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004583 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004584 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004585{
Chris Masone7a84562008-06-25 16:01:31 -04004586 int slot;
4587 struct extent_buffer *c;
4588
Chris Mason934d3752008-12-08 16:43:10 -05004589 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004590 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004591 if (!path->nodes[level])
4592 return 1;
4593
4594 slot = path->slots[level] + 1;
4595 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004596next:
Chris Masone7a84562008-06-25 16:01:31 -04004597 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004598 int ret;
4599 int orig_lowest;
4600 struct btrfs_key cur_key;
4601 if (level + 1 >= BTRFS_MAX_LEVEL ||
4602 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004603 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004604
4605 if (path->locks[level + 1]) {
4606 level++;
4607 continue;
4608 }
4609
4610 slot = btrfs_header_nritems(c) - 1;
4611 if (level == 0)
4612 btrfs_item_key_to_cpu(c, &cur_key, slot);
4613 else
4614 btrfs_node_key_to_cpu(c, &cur_key, slot);
4615
4616 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004617 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004618 path->lowest_level = level;
4619 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4620 0, 0);
4621 path->lowest_level = orig_lowest;
4622 if (ret < 0)
4623 return ret;
4624
4625 c = path->nodes[level];
4626 slot = path->slots[level];
4627 if (ret == 0)
4628 slot++;
4629 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004630 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004631
Chris Masone7a84562008-06-25 16:01:31 -04004632 if (level == 0)
4633 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004634 else {
4635 u64 blockptr = btrfs_node_blockptr(c, slot);
4636 u64 gen = btrfs_node_ptr_generation(c, slot);
4637
4638 if (cache_only) {
4639 struct extent_buffer *cur;
4640 cur = btrfs_find_tree_block(root, blockptr,
4641 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004642 if (!cur ||
4643 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004644 slot++;
4645 if (cur)
4646 free_extent_buffer(cur);
4647 goto next;
4648 }
4649 free_extent_buffer(cur);
4650 }
4651 if (gen < min_trans) {
4652 slot++;
4653 goto next;
4654 }
Chris Masone7a84562008-06-25 16:01:31 -04004655 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004656 }
Chris Masone7a84562008-06-25 16:01:31 -04004657 return 0;
4658 }
4659 return 1;
4660}
4661
Chris Mason7bb86312007-12-11 09:25:06 -05004662/*
Chris Mason925baed2008-06-25 16:01:30 -04004663 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004664 * returns 0 if it found something or 1 if there are no greater leaves.
4665 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004666 */
Chris Mason234b63a2007-03-13 10:46:10 -04004667int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004668{
4669 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004670 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004671 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004672 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004673 struct btrfs_key key;
4674 u32 nritems;
4675 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004676 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004677 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004678
4679 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004680 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004681 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004682
Chris Mason8e73f272009-04-03 10:14:18 -04004683 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4684again:
4685 level = 1;
4686 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004687 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004688 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004689
Chris Masona2135012008-06-25 16:01:30 -04004690 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004691 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004692
Chris Mason925baed2008-06-25 16:01:30 -04004693 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4694 path->keep_locks = 0;
4695
4696 if (ret < 0)
4697 return ret;
4698
Chris Masona2135012008-06-25 16:01:30 -04004699 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004700 /*
4701 * by releasing the path above we dropped all our locks. A balance
4702 * could have added more items next to the key that used to be
4703 * at the very end of the block. So, check again here and
4704 * advance the path if there are now more items available.
4705 */
Chris Masona2135012008-06-25 16:01:30 -04004706 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004707 if (ret == 0)
4708 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004709 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004710 goto done;
4711 }
Chris Masond97e63b2007-02-20 16:40:44 -05004712
Chris Masond3977122009-01-05 21:25:51 -05004713 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004714 if (!path->nodes[level]) {
4715 ret = 1;
4716 goto done;
4717 }
Chris Mason5f39d392007-10-15 16:14:19 -04004718
Chris Masond97e63b2007-02-20 16:40:44 -05004719 slot = path->slots[level] + 1;
4720 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004721 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004722 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004723 if (level == BTRFS_MAX_LEVEL) {
4724 ret = 1;
4725 goto done;
4726 }
Chris Masond97e63b2007-02-20 16:40:44 -05004727 continue;
4728 }
Chris Mason5f39d392007-10-15 16:14:19 -04004729
Chris Mason925baed2008-06-25 16:01:30 -04004730 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004731 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004732 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004733 }
Chris Mason5f39d392007-10-15 16:14:19 -04004734
Chris Mason8e73f272009-04-03 10:14:18 -04004735 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004736 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004737 ret = read_block_for_search(NULL, root, path, &next, level,
4738 slot, &key);
4739 if (ret == -EAGAIN)
4740 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004741
Chris Mason76a05b32009-05-14 13:24:30 -04004742 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004743 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004744 goto done;
4745 }
4746
Chris Mason5cd57b22008-06-25 16:01:30 -04004747 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004748 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004749 if (!ret) {
4750 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004751 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004752 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004753 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004754 }
Chris Mason31533fb2011-07-26 16:01:59 -04004755 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004756 }
Chris Masond97e63b2007-02-20 16:40:44 -05004757 break;
4758 }
4759 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004760 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004761 level--;
4762 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004763 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004764 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004765
Chris Mason5f39d392007-10-15 16:14:19 -04004766 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004767 path->nodes[level] = next;
4768 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004769 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004770 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004771 if (!level)
4772 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004773
Chris Mason8e73f272009-04-03 10:14:18 -04004774 ret = read_block_for_search(NULL, root, path, &next, level,
4775 0, &key);
4776 if (ret == -EAGAIN)
4777 goto again;
4778
Chris Mason76a05b32009-05-14 13:24:30 -04004779 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004780 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004781 goto done;
4782 }
4783
Chris Mason5cd57b22008-06-25 16:01:30 -04004784 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004785 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004786 if (!ret) {
4787 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004788 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004789 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004790 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004791 }
Chris Mason31533fb2011-07-26 16:01:59 -04004792 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004793 }
Chris Masond97e63b2007-02-20 16:40:44 -05004794 }
Chris Mason8e73f272009-04-03 10:14:18 -04004795 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004796done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004797 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004798 path->leave_spinning = old_spinning;
4799 if (!old_spinning)
4800 btrfs_set_path_blocking(path);
4801
4802 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004803}
Chris Mason0b86a832008-03-24 15:01:56 -04004804
Chris Mason3f157a22008-06-25 16:01:31 -04004805/*
4806 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4807 * searching until it gets past min_objectid or finds an item of 'type'
4808 *
4809 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4810 */
Chris Mason0b86a832008-03-24 15:01:56 -04004811int btrfs_previous_item(struct btrfs_root *root,
4812 struct btrfs_path *path, u64 min_objectid,
4813 int type)
4814{
4815 struct btrfs_key found_key;
4816 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004817 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004818 int ret;
4819
Chris Masond3977122009-01-05 21:25:51 -05004820 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004821 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004822 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004823 ret = btrfs_prev_leaf(root, path);
4824 if (ret != 0)
4825 return ret;
4826 } else {
4827 path->slots[0]--;
4828 }
4829 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004830 nritems = btrfs_header_nritems(leaf);
4831 if (nritems == 0)
4832 return 1;
4833 if (path->slots[0] == nritems)
4834 path->slots[0]--;
4835
Chris Mason0b86a832008-03-24 15:01:56 -04004836 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004837 if (found_key.objectid < min_objectid)
4838 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004839 if (found_key.type == type)
4840 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004841 if (found_key.objectid == min_objectid &&
4842 found_key.type < type)
4843 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004844 }
4845 return 1;
4846}