blob: 07c1a96aa4a83ebf43c55247dfe2514c6766c53e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Masone089f052007-03-16 16:20:31 -040041 struct btrfs_path *path, int level, int slot);
Jan Schmidtf2304752012-05-26 11:43:17 +020042static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
43 struct extent_buffer *eb);
44struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
45 u32 blocksize, u64 parent_transid,
46 u64 time_seq);
47struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
48 u64 bytenr, u32 blocksize,
49 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050050
Chris Mason2c90e5d2007-04-02 10:50:19 -040051struct btrfs_path *btrfs_alloc_path(void)
52{
Chris Masondf24a2b2007-04-04 09:36:31 -040053 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050054 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040055 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040056}
57
Chris Masonb4ce94d2009-02-04 09:25:08 -050058/*
59 * set all locked nodes in the path to blocking locks. This should
60 * be done before scheduling
61 */
62noinline void btrfs_set_path_blocking(struct btrfs_path *p)
63{
64 int i;
65 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040066 if (!p->nodes[i] || !p->locks[i])
67 continue;
68 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
69 if (p->locks[i] == BTRFS_READ_LOCK)
70 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
71 else if (p->locks[i] == BTRFS_WRITE_LOCK)
72 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050073 }
74}
75
76/*
77 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050078 *
79 * held is used to keep lockdep happy, when lockdep is enabled
80 * we set held to a blocking lock before we go around and
81 * retake all the spinlocks in the path. You can safely use NULL
82 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050083 */
Chris Mason4008c042009-02-12 14:09:45 -050084noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040085 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050086{
87 int i;
Chris Mason4008c042009-02-12 14:09:45 -050088
89#ifdef CONFIG_DEBUG_LOCK_ALLOC
90 /* lockdep really cares that we take all of these spinlocks
91 * in the right order. If any of the locks in the path are not
92 * currently blocking, it is going to complain. So, make really
93 * really sure by forcing the path to blocking before we clear
94 * the path blocking.
95 */
Chris Masonbd681512011-07-16 15:23:14 -040096 if (held) {
97 btrfs_set_lock_blocking_rw(held, held_rw);
98 if (held_rw == BTRFS_WRITE_LOCK)
99 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
100 else if (held_rw == BTRFS_READ_LOCK)
101 held_rw = BTRFS_READ_LOCK_BLOCKING;
102 }
Chris Mason4008c042009-02-12 14:09:45 -0500103 btrfs_set_path_blocking(p);
104#endif
105
106 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400107 if (p->nodes[i] && p->locks[i]) {
108 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
109 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
110 p->locks[i] = BTRFS_WRITE_LOCK;
111 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
112 p->locks[i] = BTRFS_READ_LOCK;
113 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500114 }
Chris Mason4008c042009-02-12 14:09:45 -0500115
116#ifdef CONFIG_DEBUG_LOCK_ALLOC
117 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400118 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500119#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500120}
121
Chris Masond352ac62008-09-29 15:18:18 -0400122/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400123void btrfs_free_path(struct btrfs_path *p)
124{
Jesper Juhlff175d52010-12-25 21:22:30 +0000125 if (!p)
126 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200127 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400128 kmem_cache_free(btrfs_path_cachep, p);
129}
130
Chris Masond352ac62008-09-29 15:18:18 -0400131/*
132 * path release drops references on the extent buffers in the path
133 * and it drops any locks held by this path
134 *
135 * It is safe to call this on paths that no locks or extent buffers held.
136 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200137noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500138{
139 int i;
Chris Masona2135012008-06-25 16:01:30 -0400140
Chris Mason234b63a2007-03-13 10:46:10 -0400141 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400142 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500143 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400144 continue;
145 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400146 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400147 p->locks[i] = 0;
148 }
Chris Mason5f39d392007-10-15 16:14:19 -0400149 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400150 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500151 }
152}
153
Chris Masond352ac62008-09-29 15:18:18 -0400154/*
155 * safely gets a reference on the root node of a tree. A lock
156 * is not taken, so a concurrent writer may put a different node
157 * at the root of the tree. See btrfs_lock_root_node for the
158 * looping required.
159 *
160 * The extent buffer returned by this has a reference taken, so
161 * it won't disappear. It may stop being the root of the tree
162 * at any time because there are no locks held.
163 */
Chris Mason925baed2008-06-25 16:01:30 -0400164struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
165{
166 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400167
Josef Bacik3083ee22012-03-09 16:01:49 -0500168 while (1) {
169 rcu_read_lock();
170 eb = rcu_dereference(root->node);
171
172 /*
173 * RCU really hurts here, we could free up the root node because
174 * it was cow'ed but we may not get the new root node yet so do
175 * the inc_not_zero dance and if it doesn't work then
176 * synchronize_rcu and try again.
177 */
178 if (atomic_inc_not_zero(&eb->refs)) {
179 rcu_read_unlock();
180 break;
181 }
182 rcu_read_unlock();
183 synchronize_rcu();
184 }
Chris Mason925baed2008-06-25 16:01:30 -0400185 return eb;
186}
187
Chris Masond352ac62008-09-29 15:18:18 -0400188/* loop around taking references on and locking the root node of the
189 * tree until you end up with a lock on the root. A locked buffer
190 * is returned, with a reference held.
191 */
Chris Mason925baed2008-06-25 16:01:30 -0400192struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
193{
194 struct extent_buffer *eb;
195
Chris Masond3977122009-01-05 21:25:51 -0500196 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400197 eb = btrfs_root_node(root);
198 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400199 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400200 break;
Chris Mason925baed2008-06-25 16:01:30 -0400201 btrfs_tree_unlock(eb);
202 free_extent_buffer(eb);
203 }
204 return eb;
205}
206
Chris Masonbd681512011-07-16 15:23:14 -0400207/* loop around taking references on and locking the root node of the
208 * tree until you end up with a lock on the root. A locked buffer
209 * is returned, with a reference held.
210 */
211struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
212{
213 struct extent_buffer *eb;
214
215 while (1) {
216 eb = btrfs_root_node(root);
217 btrfs_tree_read_lock(eb);
218 if (eb == root->node)
219 break;
220 btrfs_tree_read_unlock(eb);
221 free_extent_buffer(eb);
222 }
223 return eb;
224}
225
Chris Masond352ac62008-09-29 15:18:18 -0400226/* cowonly root (everything not a reference counted cow subvolume), just get
227 * put onto a simple dirty list. transaction.c walks this to make sure they
228 * get properly updated on disk.
229 */
Chris Mason0b86a832008-03-24 15:01:56 -0400230static void add_root_to_dirty_list(struct btrfs_root *root)
231{
Chris Masone5846fc2012-05-03 12:08:48 -0400232 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400233 if (root->track_dirty && list_empty(&root->dirty_list)) {
234 list_add(&root->dirty_list,
235 &root->fs_info->dirty_cowonly_roots);
236 }
Chris Masone5846fc2012-05-03 12:08:48 -0400237 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400238}
239
Chris Masond352ac62008-09-29 15:18:18 -0400240/*
241 * used by snapshot creation to make a copy of a root for a tree with
242 * a given objectid. The buffer with the new root node is returned in
243 * cow_ret, and this func returns zero on success or a negative error code.
244 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500245int btrfs_copy_root(struct btrfs_trans_handle *trans,
246 struct btrfs_root *root,
247 struct extent_buffer *buf,
248 struct extent_buffer **cow_ret, u64 new_root_objectid)
249{
250 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500251 int ret = 0;
252 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400253 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500254
255 WARN_ON(root->ref_cows && trans->transid !=
256 root->fs_info->running_transaction->transid);
257 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
258
259 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400260 if (level == 0)
261 btrfs_item_key(buf, &disk_key, 0);
262 else
263 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400264
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400265 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
266 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200267 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400268 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500269 return PTR_ERR(cow);
270
271 copy_extent_buffer(cow, buf, 0, 0, cow->len);
272 btrfs_set_header_bytenr(cow, cow->start);
273 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400274 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
275 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
276 BTRFS_HEADER_FLAG_RELOC);
277 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
278 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
279 else
280 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500281
Yan Zheng2b820322008-11-17 21:11:30 -0500282 write_extent_buffer(cow, root->fs_info->fsid,
283 (unsigned long)btrfs_header_fsid(cow),
284 BTRFS_FSID_SIZE);
285
Chris Masonbe20aa92007-12-17 20:14:01 -0500286 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400287 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200288 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400289 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200290 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500291
Chris Masonbe20aa92007-12-17 20:14:01 -0500292 if (ret)
293 return ret;
294
295 btrfs_mark_buffer_dirty(cow);
296 *cow_ret = cow;
297 return 0;
298}
299
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200300enum mod_log_op {
301 MOD_LOG_KEY_REPLACE,
302 MOD_LOG_KEY_ADD,
303 MOD_LOG_KEY_REMOVE,
304 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
305 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
306 MOD_LOG_MOVE_KEYS,
307 MOD_LOG_ROOT_REPLACE,
308};
309
310struct tree_mod_move {
311 int dst_slot;
312 int nr_items;
313};
314
315struct tree_mod_root {
316 u64 logical;
317 u8 level;
318};
319
320struct tree_mod_elem {
321 struct rb_node node;
322 u64 index; /* shifted logical */
323 struct seq_list elem;
324 enum mod_log_op op;
325
326 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
327 int slot;
328
329 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
330 u64 generation;
331
332 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
333 struct btrfs_disk_key key;
334 u64 blockptr;
335
336 /* this is used for op == MOD_LOG_MOVE_KEYS */
337 struct tree_mod_move move;
338
339 /* this is used for op == MOD_LOG_ROOT_REPLACE */
340 struct tree_mod_root old_root;
341};
342
343static inline void
344__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
345{
346 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
347 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
348}
349
350void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
351 struct seq_list *elem)
352{
353 elem->flags = 1;
354 spin_lock(&fs_info->tree_mod_seq_lock);
355 __get_tree_mod_seq(fs_info, elem);
356 spin_unlock(&fs_info->tree_mod_seq_lock);
357}
358
359void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
360 struct seq_list *elem)
361{
362 struct rb_root *tm_root;
363 struct rb_node *node;
364 struct rb_node *next;
365 struct seq_list *cur_elem;
366 struct tree_mod_elem *tm;
367 u64 min_seq = (u64)-1;
368 u64 seq_putting = elem->seq;
369
370 if (!seq_putting)
371 return;
372
373 BUG_ON(!(elem->flags & 1));
374 spin_lock(&fs_info->tree_mod_seq_lock);
375 list_del(&elem->list);
376
377 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
378 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
379 if (seq_putting > cur_elem->seq) {
380 /*
381 * blocker with lower sequence number exists, we
382 * cannot remove anything from the log
383 */
384 goto out;
385 }
386 min_seq = cur_elem->seq;
387 }
388 }
389
390 /*
391 * anything that's lower than the lowest existing (read: blocked)
392 * sequence number can be removed from the tree.
393 */
394 write_lock(&fs_info->tree_mod_log_lock);
395 tm_root = &fs_info->tree_mod_log;
396 for (node = rb_first(tm_root); node; node = next) {
397 next = rb_next(node);
398 tm = container_of(node, struct tree_mod_elem, node);
399 if (tm->elem.seq > min_seq)
400 continue;
401 rb_erase(node, tm_root);
402 list_del(&tm->elem.list);
403 kfree(tm);
404 }
405 write_unlock(&fs_info->tree_mod_log_lock);
406out:
407 spin_unlock(&fs_info->tree_mod_seq_lock);
408}
409
410/*
411 * key order of the log:
412 * index -> sequence
413 *
414 * the index is the shifted logical of the *new* root node for root replace
415 * operations, or the shifted logical of the affected block for all other
416 * operations.
417 */
418static noinline int
419__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
420{
421 struct rb_root *tm_root;
422 struct rb_node **new;
423 struct rb_node *parent = NULL;
424 struct tree_mod_elem *cur;
425 int ret = 0;
426
427 BUG_ON(!tm || !tm->elem.seq);
428
429 write_lock(&fs_info->tree_mod_log_lock);
430 tm_root = &fs_info->tree_mod_log;
431 new = &tm_root->rb_node;
432 while (*new) {
433 cur = container_of(*new, struct tree_mod_elem, node);
434 parent = *new;
435 if (cur->index < tm->index)
436 new = &((*new)->rb_left);
437 else if (cur->index > tm->index)
438 new = &((*new)->rb_right);
439 else if (cur->elem.seq < tm->elem.seq)
440 new = &((*new)->rb_left);
441 else if (cur->elem.seq > tm->elem.seq)
442 new = &((*new)->rb_right);
443 else {
444 kfree(tm);
445 ret = -EEXIST;
446 goto unlock;
447 }
448 }
449
450 rb_link_node(&tm->node, parent, new);
451 rb_insert_color(&tm->node, tm_root);
452unlock:
453 write_unlock(&fs_info->tree_mod_log_lock);
454 return ret;
455}
456
457int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
458 struct tree_mod_elem **tm_ret)
459{
460 struct tree_mod_elem *tm;
461 u64 seq = 0;
462
463 smp_mb();
464 if (list_empty(&fs_info->tree_mod_seq_list))
465 return 0;
466
467 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
468 if (!tm)
469 return -ENOMEM;
470
471 __get_tree_mod_seq(fs_info, &tm->elem);
472 seq = tm->elem.seq;
473 tm->elem.flags = 0;
474
475 return seq;
476}
477
478static noinline int
479tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
480 struct extent_buffer *eb, int slot,
481 enum mod_log_op op, gfp_t flags)
482{
483 struct tree_mod_elem *tm;
484 int ret;
485
486 ret = tree_mod_alloc(fs_info, flags, &tm);
487 if (ret <= 0)
488 return ret;
489
490 tm->index = eb->start >> PAGE_CACHE_SHIFT;
491 if (op != MOD_LOG_KEY_ADD) {
492 btrfs_node_key(eb, &tm->key, slot);
493 tm->blockptr = btrfs_node_blockptr(eb, slot);
494 }
495 tm->op = op;
496 tm->slot = slot;
497 tm->generation = btrfs_node_ptr_generation(eb, slot);
498
499 return __tree_mod_log_insert(fs_info, tm);
500}
501
502static noinline int
503tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
504 int slot, enum mod_log_op op)
505{
506 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
507}
508
509static noinline int
510tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
511 struct extent_buffer *eb, int dst_slot, int src_slot,
512 int nr_items, gfp_t flags)
513{
514 struct tree_mod_elem *tm;
515 int ret;
516 int i;
517
518 ret = tree_mod_alloc(fs_info, flags, &tm);
519 if (ret <= 0)
520 return ret;
521
522 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
523 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
524 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
525 BUG_ON(ret < 0);
526 }
527
528 tm->index = eb->start >> PAGE_CACHE_SHIFT;
529 tm->slot = src_slot;
530 tm->move.dst_slot = dst_slot;
531 tm->move.nr_items = nr_items;
532 tm->op = MOD_LOG_MOVE_KEYS;
533
534 return __tree_mod_log_insert(fs_info, tm);
535}
536
537static noinline int
538tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
539 struct extent_buffer *old_root,
540 struct extent_buffer *new_root, gfp_t flags)
541{
542 struct tree_mod_elem *tm;
543 int ret;
544
545 ret = tree_mod_alloc(fs_info, flags, &tm);
546 if (ret <= 0)
547 return ret;
548
549 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
550 tm->old_root.logical = old_root->start;
551 tm->old_root.level = btrfs_header_level(old_root);
552 tm->generation = btrfs_header_generation(old_root);
553 tm->op = MOD_LOG_ROOT_REPLACE;
554
555 return __tree_mod_log_insert(fs_info, tm);
556}
557
558static struct tree_mod_elem *
559__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
560 int smallest)
561{
562 struct rb_root *tm_root;
563 struct rb_node *node;
564 struct tree_mod_elem *cur = NULL;
565 struct tree_mod_elem *found = NULL;
566 u64 index = start >> PAGE_CACHE_SHIFT;
567
568 read_lock(&fs_info->tree_mod_log_lock);
569 tm_root = &fs_info->tree_mod_log;
570 node = tm_root->rb_node;
571 while (node) {
572 cur = container_of(node, struct tree_mod_elem, node);
573 if (cur->index < index) {
574 node = node->rb_left;
575 } else if (cur->index > index) {
576 node = node->rb_right;
577 } else if (cur->elem.seq < min_seq) {
578 node = node->rb_left;
579 } else if (!smallest) {
580 /* we want the node with the highest seq */
581 if (found)
582 BUG_ON(found->elem.seq > cur->elem.seq);
583 found = cur;
584 node = node->rb_left;
585 } else if (cur->elem.seq > min_seq) {
586 /* we want the node with the smallest seq */
587 if (found)
588 BUG_ON(found->elem.seq < cur->elem.seq);
589 found = cur;
590 node = node->rb_right;
591 } else {
592 found = cur;
593 break;
594 }
595 }
596 read_unlock(&fs_info->tree_mod_log_lock);
597
598 return found;
599}
600
601/*
602 * this returns the element from the log with the smallest time sequence
603 * value that's in the log (the oldest log item). any element with a time
604 * sequence lower than min_seq will be ignored.
605 */
606static struct tree_mod_elem *
607tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
608 u64 min_seq)
609{
610 return __tree_mod_log_search(fs_info, start, min_seq, 1);
611}
612
613/*
614 * this returns the element from the log with the largest time sequence
615 * value that's in the log (the most recent log item). any element with
616 * a time sequence lower than min_seq will be ignored.
617 */
618static struct tree_mod_elem *
619tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
620{
621 return __tree_mod_log_search(fs_info, start, min_seq, 0);
622}
623
624static inline void
625tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
626 struct extent_buffer *src, unsigned long dst_offset,
627 unsigned long src_offset, int nr_items)
628{
629 int ret;
630 int i;
631
632 smp_mb();
633 if (list_empty(&fs_info->tree_mod_seq_list))
634 return;
635
636 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
637 return;
638
639 /* speed this up by single seq for all operations? */
640 for (i = 0; i < nr_items; i++) {
641 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
642 MOD_LOG_KEY_REMOVE);
643 BUG_ON(ret < 0);
644 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
645 MOD_LOG_KEY_ADD);
646 BUG_ON(ret < 0);
647 }
648}
649
650static inline void
651tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
652 int dst_offset, int src_offset, int nr_items)
653{
654 int ret;
655 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
656 nr_items, GFP_NOFS);
657 BUG_ON(ret < 0);
658}
659
660static inline void
661tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
662 struct extent_buffer *eb,
663 struct btrfs_disk_key *disk_key, int slot, int atomic)
664{
665 int ret;
666
667 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
668 MOD_LOG_KEY_REPLACE,
669 atomic ? GFP_ATOMIC : GFP_NOFS);
670 BUG_ON(ret < 0);
671}
672
673static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
674 struct extent_buffer *eb)
675{
676 int i;
677 int ret;
678 u32 nritems;
679
680 smp_mb();
681 if (list_empty(&fs_info->tree_mod_seq_list))
682 return;
683
684 if (btrfs_header_level(eb) == 0)
685 return;
686
687 nritems = btrfs_header_nritems(eb);
688 for (i = nritems - 1; i >= 0; i--) {
689 ret = tree_mod_log_insert_key(fs_info, eb, i,
690 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
691 BUG_ON(ret < 0);
692 }
693}
694
695static inline void
696tree_mod_log_set_root_pointer(struct btrfs_root *root,
697 struct extent_buffer *new_root_node)
698{
699 int ret;
700 tree_mod_log_free_eb(root->fs_info, root->node);
701 ret = tree_mod_log_insert_root(root->fs_info, root->node,
702 new_root_node, GFP_NOFS);
703 BUG_ON(ret < 0);
704}
705
Chris Masond352ac62008-09-29 15:18:18 -0400706/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400707 * check if the tree block can be shared by multiple trees
708 */
709int btrfs_block_can_be_shared(struct btrfs_root *root,
710 struct extent_buffer *buf)
711{
712 /*
713 * Tree blocks not in refernece counted trees and tree roots
714 * are never shared. If a block was allocated after the last
715 * snapshot and the block was not allocated by tree relocation,
716 * we know the block is not shared.
717 */
718 if (root->ref_cows &&
719 buf != root->node && buf != root->commit_root &&
720 (btrfs_header_generation(buf) <=
721 btrfs_root_last_snapshot(&root->root_item) ||
722 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
723 return 1;
724#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
725 if (root->ref_cows &&
726 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
727 return 1;
728#endif
729 return 0;
730}
731
732static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
733 struct btrfs_root *root,
734 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400735 struct extent_buffer *cow,
736 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400737{
738 u64 refs;
739 u64 owner;
740 u64 flags;
741 u64 new_flags = 0;
742 int ret;
743
744 /*
745 * Backrefs update rules:
746 *
747 * Always use full backrefs for extent pointers in tree block
748 * allocated by tree relocation.
749 *
750 * If a shared tree block is no longer referenced by its owner
751 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
752 * use full backrefs for extent pointers in tree block.
753 *
754 * If a tree block is been relocating
755 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
756 * use full backrefs for extent pointers in tree block.
757 * The reason for this is some operations (such as drop tree)
758 * are only allowed for blocks use full backrefs.
759 */
760
761 if (btrfs_block_can_be_shared(root, buf)) {
762 ret = btrfs_lookup_extent_info(trans, root, buf->start,
763 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700764 if (ret)
765 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700766 if (refs == 0) {
767 ret = -EROFS;
768 btrfs_std_error(root->fs_info, ret);
769 return ret;
770 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400771 } else {
772 refs = 1;
773 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
774 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
775 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
776 else
777 flags = 0;
778 }
779
780 owner = btrfs_header_owner(buf);
781 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
782 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
783
784 if (refs > 1) {
785 if ((owner == root->root_key.objectid ||
786 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
787 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200788 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100789 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400790
791 if (root->root_key.objectid ==
792 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200793 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200795 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100796 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400797 }
798 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
799 } else {
800
801 if (root->root_key.objectid ==
802 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200803 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400804 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200805 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100806 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400807 }
808 if (new_flags != 0) {
809 ret = btrfs_set_disk_extent_flags(trans, root,
810 buf->start,
811 buf->len,
812 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700813 if (ret)
814 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400815 }
816 } else {
817 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
818 if (root->root_key.objectid ==
819 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200820 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200822 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100823 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200824 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100825 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400826 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200827 /*
828 * don't log freeing in case we're freeing the root node, this
829 * is done by tree_mod_log_set_root_pointer later
830 */
831 if (buf != root->node && btrfs_header_level(buf) != 0)
832 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400833 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400834 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 }
836 return 0;
837}
838
839/*
Chris Masond3977122009-01-05 21:25:51 -0500840 * does the dirty work in cow of a single block. The parent block (if
841 * supplied) is updated to point to the new cow copy. The new buffer is marked
842 * dirty and returned locked. If you modify the block it needs to be marked
843 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400844 *
845 * search_start -- an allocation hint for the new block
846 *
Chris Masond3977122009-01-05 21:25:51 -0500847 * empty_size -- a hint that you plan on doing more cow. This is the size in
848 * bytes the allocator should try to find free next to the block it returns.
849 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400850 */
Chris Masond3977122009-01-05 21:25:51 -0500851static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400852 struct btrfs_root *root,
853 struct extent_buffer *buf,
854 struct extent_buffer *parent, int parent_slot,
855 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400856 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400857{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400859 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700860 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400861 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400862 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400863 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400864
Chris Mason925baed2008-06-25 16:01:30 -0400865 if (*cow_ret == buf)
866 unlock_orig = 1;
867
Chris Masonb9447ef2009-03-09 11:45:38 -0400868 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400869
Chris Mason7bb86312007-12-11 09:25:06 -0500870 WARN_ON(root->ref_cows && trans->transid !=
871 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400872 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400873
Chris Mason7bb86312007-12-11 09:25:06 -0500874 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400875
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400876 if (level == 0)
877 btrfs_item_key(buf, &disk_key, 0);
878 else
879 btrfs_node_key(buf, &disk_key, 0);
880
881 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
882 if (parent)
883 parent_start = parent->start;
884 else
885 parent_start = 0;
886 } else
887 parent_start = 0;
888
889 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
890 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200891 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400892 if (IS_ERR(cow))
893 return PTR_ERR(cow);
894
Chris Masonb4ce94d2009-02-04 09:25:08 -0500895 /* cow is set to blocking by btrfs_init_new_buffer */
896
Chris Mason5f39d392007-10-15 16:14:19 -0400897 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400898 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400899 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
901 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
902 BTRFS_HEADER_FLAG_RELOC);
903 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
904 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
905 else
906 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400907
Yan Zheng2b820322008-11-17 21:11:30 -0500908 write_extent_buffer(cow, root->fs_info->fsid,
909 (unsigned long)btrfs_header_fsid(cow),
910 BTRFS_FSID_SIZE);
911
Mark Fashehbe1a5562011-08-08 13:20:18 -0700912 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700913 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100914 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700915 return ret;
916 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400917
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400918 if (root->ref_cows)
919 btrfs_reloc_cow_block(trans, root, buf, cow);
920
Chris Mason6702ed42007-08-07 16:15:09 -0400921 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400922 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
924 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
925 parent_start = buf->start;
926 else
927 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400928
Chris Mason5f39d392007-10-15 16:14:19 -0400929 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200930 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400931 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400932
Yan, Zhengf0486c62010-05-16 10:46:25 -0400933 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200934 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400935 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400936 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400937 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
939 parent_start = parent->start;
940 else
941 parent_start = 0;
942
943 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200944 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
945 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400946 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400947 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500948 btrfs_set_node_ptr_generation(parent, parent_slot,
949 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400950 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400951 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200952 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400953 }
Chris Mason925baed2008-06-25 16:01:30 -0400954 if (unlock_orig)
955 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500956 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400957 btrfs_mark_buffer_dirty(cow);
958 *cow_ret = cow;
959 return 0;
960}
961
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400962static inline int should_cow_block(struct btrfs_trans_handle *trans,
963 struct btrfs_root *root,
964 struct extent_buffer *buf)
965{
Liu Bof1ebcc72011-11-14 20:48:06 -0500966 /* ensure we can see the force_cow */
967 smp_rmb();
968
969 /*
970 * We do not need to cow a block if
971 * 1) this block is not created or changed in this transaction;
972 * 2) this block does not belong to TREE_RELOC tree;
973 * 3) the root is not forced COW.
974 *
975 * What is forced COW:
976 * when we create snapshot during commiting the transaction,
977 * after we've finished coping src root, we must COW the shared
978 * block to ensure the metadata consistency.
979 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400980 if (btrfs_header_generation(buf) == trans->transid &&
981 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
982 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -0500983 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
984 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 return 0;
986 return 1;
987}
988
Chris Masond352ac62008-09-29 15:18:18 -0400989/*
990 * cows a single block, see __btrfs_cow_block for the real work.
991 * This version of it has extra checks so that a block isn't cow'd more than
992 * once per transaction, as long as it hasn't been written yet
993 */
Chris Masond3977122009-01-05 21:25:51 -0500994noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400995 struct btrfs_root *root, struct extent_buffer *buf,
996 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400997 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -0500998{
Chris Mason6702ed42007-08-07 16:15:09 -0400999 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001000 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001001
Chris Masonccd467d2007-06-28 15:57:36 -04001002 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001003 printk(KERN_CRIT "trans %llu running %llu\n",
1004 (unsigned long long)trans->transid,
1005 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001006 root->fs_info->running_transaction->transid);
1007 WARN_ON(1);
1008 }
1009 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001010 printk(KERN_CRIT "trans %llu running %llu\n",
1011 (unsigned long long)trans->transid,
1012 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001013 WARN_ON(1);
1014 }
Chris Masondc17ff82008-01-08 15:46:30 -05001015
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001016 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001017 *cow_ret = buf;
1018 return 0;
1019 }
Chris Masonc4876852009-02-04 09:24:25 -05001020
Chris Mason0b86a832008-03-24 15:01:56 -04001021 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001022
1023 if (parent)
1024 btrfs_set_lock_blocking(parent);
1025 btrfs_set_lock_blocking(buf);
1026
Chris Masonf510cfe2007-10-15 16:14:48 -04001027 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001028 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001029
1030 trace_btrfs_cow_block(root, buf, *cow_ret);
1031
Chris Masonf510cfe2007-10-15 16:14:48 -04001032 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001033}
1034
Chris Masond352ac62008-09-29 15:18:18 -04001035/*
1036 * helper function for defrag to decide if two blocks pointed to by a
1037 * node are actually close by
1038 */
Chris Mason6b800532007-10-15 16:17:34 -04001039static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001040{
Chris Mason6b800532007-10-15 16:17:34 -04001041 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001042 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001043 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001044 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001045 return 0;
1046}
1047
Chris Mason081e9572007-11-06 10:26:24 -05001048/*
1049 * compare two keys in a memcmp fashion
1050 */
1051static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1052{
1053 struct btrfs_key k1;
1054
1055 btrfs_disk_key_to_cpu(&k1, disk);
1056
Diego Calleja20736ab2009-07-24 11:06:52 -04001057 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001058}
1059
Josef Bacikf3465ca2008-11-12 14:19:50 -05001060/*
1061 * same as comp_keys only with two btrfs_key's
1062 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001063int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001064{
1065 if (k1->objectid > k2->objectid)
1066 return 1;
1067 if (k1->objectid < k2->objectid)
1068 return -1;
1069 if (k1->type > k2->type)
1070 return 1;
1071 if (k1->type < k2->type)
1072 return -1;
1073 if (k1->offset > k2->offset)
1074 return 1;
1075 if (k1->offset < k2->offset)
1076 return -1;
1077 return 0;
1078}
Chris Mason081e9572007-11-06 10:26:24 -05001079
Chris Masond352ac62008-09-29 15:18:18 -04001080/*
1081 * this is used by the defrag code to go through all the
1082 * leaves pointed to by a node and reallocate them so that
1083 * disk order is close to key order
1084 */
Chris Mason6702ed42007-08-07 16:15:09 -04001085int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001086 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001087 int start_slot, int cache_only, u64 *last_ret,
1088 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001089{
Chris Mason6b800532007-10-15 16:17:34 -04001090 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001091 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001092 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001093 u64 search_start = *last_ret;
1094 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001095 u64 other;
1096 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001097 int end_slot;
1098 int i;
1099 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001100 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001101 int uptodate;
1102 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001103 int progress_passed = 0;
1104 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001105
Chris Mason5708b952007-10-25 15:43:18 -04001106 parent_level = btrfs_header_level(parent);
1107 if (cache_only && parent_level != 1)
1108 return 0;
1109
Chris Masond3977122009-01-05 21:25:51 -05001110 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001111 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001112 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001113 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001114
Chris Mason6b800532007-10-15 16:17:34 -04001115 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001116 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001117 end_slot = parent_nritems;
1118
1119 if (parent_nritems == 1)
1120 return 0;
1121
Chris Masonb4ce94d2009-02-04 09:25:08 -05001122 btrfs_set_lock_blocking(parent);
1123
Chris Mason6702ed42007-08-07 16:15:09 -04001124 for (i = start_slot; i < end_slot; i++) {
1125 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001126
Chris Mason081e9572007-11-06 10:26:24 -05001127 btrfs_node_key(parent, &disk_key, i);
1128 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1129 continue;
1130
1131 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001132 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001133 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001134 if (last_block == 0)
1135 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001136
Chris Mason6702ed42007-08-07 16:15:09 -04001137 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001138 other = btrfs_node_blockptr(parent, i - 1);
1139 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001140 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001141 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001142 other = btrfs_node_blockptr(parent, i + 1);
1143 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001144 }
Chris Masone9d0b132007-08-10 14:06:19 -04001145 if (close) {
1146 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001147 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001148 }
Chris Mason6702ed42007-08-07 16:15:09 -04001149
Chris Mason6b800532007-10-15 16:17:34 -04001150 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1151 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001152 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001153 else
1154 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001155 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001156 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001157 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001158 continue;
1159 }
Chris Mason6b800532007-10-15 16:17:34 -04001160 if (!cur) {
1161 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001162 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001163 if (!cur)
1164 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001165 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001166 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001167 }
Chris Mason6702ed42007-08-07 16:15:09 -04001168 }
Chris Masone9d0b132007-08-10 14:06:19 -04001169 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001170 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001171
Chris Masone7a84562008-06-25 16:01:31 -04001172 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001173 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001174 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001175 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001176 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001177 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001178 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001179 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001180 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001181 break;
Yan252c38f2007-08-29 09:11:44 -04001182 }
Chris Masone7a84562008-06-25 16:01:31 -04001183 search_start = cur->start;
1184 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001185 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001186 btrfs_tree_unlock(cur);
1187 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001188 }
1189 return err;
1190}
1191
Chris Mason74123bd2007-02-02 11:05:29 -05001192/*
1193 * The leaf data grows from end-to-front in the node.
1194 * this returns the address of the start of the last item,
1195 * which is the stop of the leaf data stack
1196 */
Chris Mason123abc82007-03-14 14:14:43 -04001197static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001198 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001199{
Chris Mason5f39d392007-10-15 16:14:19 -04001200 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001201 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001202 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001204}
1205
Chris Masonaa5d6be2007-02-28 16:35:06 -05001206
Chris Mason74123bd2007-02-02 11:05:29 -05001207/*
Chris Mason5f39d392007-10-15 16:14:19 -04001208 * search for key in the extent_buffer. The items start at offset p,
1209 * and they are item_size apart. There are 'max' items in p.
1210 *
Chris Mason74123bd2007-02-02 11:05:29 -05001211 * the slot in the array is returned via slot, and it points to
1212 * the place where you would insert key if it is not found in
1213 * the array.
1214 *
1215 * slot may point to max if the key is bigger than all of the keys
1216 */
Chris Masone02119d2008-09-05 16:13:11 -04001217static noinline int generic_bin_search(struct extent_buffer *eb,
1218 unsigned long p,
1219 int item_size, struct btrfs_key *key,
1220 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001221{
1222 int low = 0;
1223 int high = max;
1224 int mid;
1225 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001226 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001227 struct btrfs_disk_key unaligned;
1228 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001229 char *kaddr = NULL;
1230 unsigned long map_start = 0;
1231 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001232 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001233
Chris Masond3977122009-01-05 21:25:51 -05001234 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001235 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001236 offset = p + mid * item_size;
1237
Chris Masona6591712011-07-19 12:04:14 -04001238 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001239 (offset + sizeof(struct btrfs_disk_key)) >
1240 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001241
1242 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001243 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001244 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001245
Chris Mason479965d2007-10-15 16:14:27 -04001246 if (!err) {
1247 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1248 map_start);
1249 } else {
1250 read_extent_buffer(eb, &unaligned,
1251 offset, sizeof(unaligned));
1252 tmp = &unaligned;
1253 }
1254
Chris Mason5f39d392007-10-15 16:14:19 -04001255 } else {
1256 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1257 map_start);
1258 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001259 ret = comp_keys(tmp, key);
1260
1261 if (ret < 0)
1262 low = mid + 1;
1263 else if (ret > 0)
1264 high = mid;
1265 else {
1266 *slot = mid;
1267 return 0;
1268 }
1269 }
1270 *slot = low;
1271 return 1;
1272}
1273
Chris Mason97571fd2007-02-24 13:39:08 -05001274/*
1275 * simple bin_search frontend that does the right thing for
1276 * leaves vs nodes
1277 */
Chris Mason5f39d392007-10-15 16:14:19 -04001278static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1279 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001280{
Chris Mason5f39d392007-10-15 16:14:19 -04001281 if (level == 0) {
1282 return generic_bin_search(eb,
1283 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001284 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001285 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001286 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001287 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001288 return generic_bin_search(eb,
1289 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001290 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001291 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001292 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001293 }
1294 return -1;
1295}
1296
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001297int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1298 int level, int *slot)
1299{
1300 return bin_search(eb, key, level, slot);
1301}
1302
Yan, Zhengf0486c62010-05-16 10:46:25 -04001303static void root_add_used(struct btrfs_root *root, u32 size)
1304{
1305 spin_lock(&root->accounting_lock);
1306 btrfs_set_root_used(&root->root_item,
1307 btrfs_root_used(&root->root_item) + size);
1308 spin_unlock(&root->accounting_lock);
1309}
1310
1311static void root_sub_used(struct btrfs_root *root, u32 size)
1312{
1313 spin_lock(&root->accounting_lock);
1314 btrfs_set_root_used(&root->root_item,
1315 btrfs_root_used(&root->root_item) - size);
1316 spin_unlock(&root->accounting_lock);
1317}
1318
Chris Masond352ac62008-09-29 15:18:18 -04001319/* given a node and slot number, this reads the blocks it points to. The
1320 * extent buffer is returned with a reference taken (but unlocked).
1321 * NULL is returned on error.
1322 */
Chris Masone02119d2008-09-05 16:13:11 -04001323static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001324 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001325{
Chris Masonca7a79a2008-05-12 12:59:19 -04001326 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001327 if (slot < 0)
1328 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001329 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001330 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001331
1332 BUG_ON(level == 0);
1333
Chris Masondb945352007-10-15 16:15:53 -04001334 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001335 btrfs_level_size(root, level - 1),
1336 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001337}
1338
Chris Masond352ac62008-09-29 15:18:18 -04001339/*
1340 * node level balancing, used to make sure nodes are in proper order for
1341 * item deletion. We balance from the top down, so we have to make sure
1342 * that a deletion won't leave an node completely empty later on.
1343 */
Chris Masone02119d2008-09-05 16:13:11 -04001344static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001345 struct btrfs_root *root,
1346 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001347{
Chris Mason5f39d392007-10-15 16:14:19 -04001348 struct extent_buffer *right = NULL;
1349 struct extent_buffer *mid;
1350 struct extent_buffer *left = NULL;
1351 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001352 int ret = 0;
1353 int wret;
1354 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001355 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001356 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001357
1358 if (level == 0)
1359 return 0;
1360
Chris Mason5f39d392007-10-15 16:14:19 -04001361 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001362
Chris Masonbd681512011-07-16 15:23:14 -04001363 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1364 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001365 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1366
Chris Mason1d4f8a02007-03-13 09:28:32 -04001367 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001368
Li Zefana05a9bb2011-09-06 16:55:34 +08001369 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001370 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001371 pslot = path->slots[level + 1];
1372 }
Chris Masonbb803952007-03-01 12:04:21 -05001373
Chris Mason40689472007-03-17 14:29:23 -04001374 /*
1375 * deal with the case where there is only one pointer in the root
1376 * by promoting the node below to a root
1377 */
Chris Mason5f39d392007-10-15 16:14:19 -04001378 if (!parent) {
1379 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001380
Chris Mason5f39d392007-10-15 16:14:19 -04001381 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001382 return 0;
1383
1384 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001385 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001386 if (!child) {
1387 ret = -EROFS;
1388 btrfs_std_error(root->fs_info, ret);
1389 goto enospc;
1390 }
1391
Chris Mason925baed2008-06-25 16:01:30 -04001392 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001393 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001394 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001395 if (ret) {
1396 btrfs_tree_unlock(child);
1397 free_extent_buffer(child);
1398 goto enospc;
1399 }
Yan2f375ab2008-02-01 14:58:07 -05001400
Jan Schmidtf2304752012-05-26 11:43:17 +02001401 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001402 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001403
Chris Mason0b86a832008-03-24 15:01:56 -04001404 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001405 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001406
Chris Mason925baed2008-06-25 16:01:30 -04001407 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001408 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001409 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001410 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001411 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001412 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001413
1414 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001415 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001416 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001417 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001418 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001419 }
Chris Mason5f39d392007-10-15 16:14:19 -04001420 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001421 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001422 return 0;
1423
Andi Kleen559af822010-10-29 15:14:37 -04001424 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001425
Chris Mason5f39d392007-10-15 16:14:19 -04001426 left = read_node_slot(root, parent, pslot - 1);
1427 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001428 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001429 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001430 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001431 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001432 if (wret) {
1433 ret = wret;
1434 goto enospc;
1435 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001436 }
Chris Mason5f39d392007-10-15 16:14:19 -04001437 right = read_node_slot(root, parent, pslot + 1);
1438 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001439 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001440 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001441 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001442 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001443 if (wret) {
1444 ret = wret;
1445 goto enospc;
1446 }
1447 }
1448
1449 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001450 if (left) {
1451 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001452 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001453 if (wret < 0)
1454 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001455 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001456 }
Chris Mason79f95c82007-03-01 15:16:26 -05001457
1458 /*
1459 * then try to empty the right most buffer into the middle
1460 */
Chris Mason5f39d392007-10-15 16:14:19 -04001461 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001462 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001463 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001464 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001465 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001466 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001467 btrfs_tree_unlock(right);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001468 del_ptr(trans, root, path, level + 1, pslot + 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001469 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001470 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001471 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001472 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001473 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001474 struct btrfs_disk_key right_key;
1475 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001476 tree_mod_log_set_node_key(root->fs_info, parent,
1477 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001478 btrfs_set_node_key(parent, &right_key, pslot + 1);
1479 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001480 }
1481 }
Chris Mason5f39d392007-10-15 16:14:19 -04001482 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001483 /*
1484 * we're not allowed to leave a node with one item in the
1485 * tree during a delete. A deletion from lower in the tree
1486 * could try to delete the only pointer in this node.
1487 * So, pull some keys from the left.
1488 * There has to be a left pointer at this point because
1489 * otherwise we would have pulled some pointers from the
1490 * right
1491 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001492 if (!left) {
1493 ret = -EROFS;
1494 btrfs_std_error(root->fs_info, ret);
1495 goto enospc;
1496 }
Chris Mason5f39d392007-10-15 16:14:19 -04001497 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001498 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001499 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001500 goto enospc;
1501 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001502 if (wret == 1) {
1503 wret = push_node_left(trans, root, left, mid, 1);
1504 if (wret < 0)
1505 ret = wret;
1506 }
Chris Mason79f95c82007-03-01 15:16:26 -05001507 BUG_ON(wret == 1);
1508 }
Chris Mason5f39d392007-10-15 16:14:19 -04001509 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001510 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001511 btrfs_tree_unlock(mid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01001512 del_ptr(trans, root, path, level + 1, pslot);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001513 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001514 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001515 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001516 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001517 } else {
1518 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001519 struct btrfs_disk_key mid_key;
1520 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001521 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1522 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001523 btrfs_set_node_key(parent, &mid_key, pslot);
1524 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001525 }
Chris Masonbb803952007-03-01 12:04:21 -05001526
Chris Mason79f95c82007-03-01 15:16:26 -05001527 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001528 if (left) {
1529 if (btrfs_header_nritems(left) > orig_slot) {
1530 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001531 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001532 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001533 path->slots[level + 1] -= 1;
1534 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001535 if (mid) {
1536 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001537 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001538 }
Chris Masonbb803952007-03-01 12:04:21 -05001539 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001540 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001541 path->slots[level] = orig_slot;
1542 }
1543 }
Chris Mason79f95c82007-03-01 15:16:26 -05001544 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001545 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001546 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001547 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001548enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001549 if (right) {
1550 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001551 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001552 }
1553 if (left) {
1554 if (path->nodes[level] != left)
1555 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001556 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001557 }
Chris Masonbb803952007-03-01 12:04:21 -05001558 return ret;
1559}
1560
Chris Masond352ac62008-09-29 15:18:18 -04001561/* Node balancing for insertion. Here we only split or push nodes around
1562 * when they are completely full. This is also done top down, so we
1563 * have to be pessimistic.
1564 */
Chris Masond3977122009-01-05 21:25:51 -05001565static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001566 struct btrfs_root *root,
1567 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001568{
Chris Mason5f39d392007-10-15 16:14:19 -04001569 struct extent_buffer *right = NULL;
1570 struct extent_buffer *mid;
1571 struct extent_buffer *left = NULL;
1572 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001573 int ret = 0;
1574 int wret;
1575 int pslot;
1576 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001577
1578 if (level == 0)
1579 return 1;
1580
Chris Mason5f39d392007-10-15 16:14:19 -04001581 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001582 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001583
Li Zefana05a9bb2011-09-06 16:55:34 +08001584 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001585 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001586 pslot = path->slots[level + 1];
1587 }
Chris Masone66f7092007-04-20 13:16:02 -04001588
Chris Mason5f39d392007-10-15 16:14:19 -04001589 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001590 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001591
Chris Mason5f39d392007-10-15 16:14:19 -04001592 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001593
1594 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001595 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001596 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001597
1598 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001599 btrfs_set_lock_blocking(left);
1600
Chris Mason5f39d392007-10-15 16:14:19 -04001601 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001602 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1603 wret = 1;
1604 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001605 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001606 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001607 if (ret)
1608 wret = 1;
1609 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001610 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001611 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001612 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001613 }
Chris Masone66f7092007-04-20 13:16:02 -04001614 if (wret < 0)
1615 ret = wret;
1616 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001617 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001618 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001619 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001620 tree_mod_log_set_node_key(root->fs_info, parent,
1621 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001622 btrfs_set_node_key(parent, &disk_key, pslot);
1623 btrfs_mark_buffer_dirty(parent);
1624 if (btrfs_header_nritems(left) > orig_slot) {
1625 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001626 path->slots[level + 1] -= 1;
1627 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001628 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001629 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001630 } else {
1631 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001632 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001633 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001634 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001635 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001636 }
Chris Masone66f7092007-04-20 13:16:02 -04001637 return 0;
1638 }
Chris Mason925baed2008-06-25 16:01:30 -04001639 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001640 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001641 }
Chris Mason925baed2008-06-25 16:01:30 -04001642 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001643
1644 /*
1645 * then try to empty the right most buffer into the middle
1646 */
Chris Mason5f39d392007-10-15 16:14:19 -04001647 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001648 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001649
Chris Mason925baed2008-06-25 16:01:30 -04001650 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001651 btrfs_set_lock_blocking(right);
1652
Chris Mason5f39d392007-10-15 16:14:19 -04001653 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001654 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1655 wret = 1;
1656 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001657 ret = btrfs_cow_block(trans, root, right,
1658 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001659 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001660 if (ret)
1661 wret = 1;
1662 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001663 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001664 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001665 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001666 }
Chris Masone66f7092007-04-20 13:16:02 -04001667 if (wret < 0)
1668 ret = wret;
1669 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001670 struct btrfs_disk_key disk_key;
1671
1672 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001673 tree_mod_log_set_node_key(root->fs_info, parent,
1674 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001675 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1676 btrfs_mark_buffer_dirty(parent);
1677
1678 if (btrfs_header_nritems(mid) <= orig_slot) {
1679 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001680 path->slots[level + 1] += 1;
1681 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001682 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001683 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001684 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001685 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001686 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001687 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001688 }
Chris Masone66f7092007-04-20 13:16:02 -04001689 return 0;
1690 }
Chris Mason925baed2008-06-25 16:01:30 -04001691 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001692 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001693 }
Chris Masone66f7092007-04-20 13:16:02 -04001694 return 1;
1695}
1696
Chris Mason74123bd2007-02-02 11:05:29 -05001697/*
Chris Masond352ac62008-09-29 15:18:18 -04001698 * readahead one full node of leaves, finding things that are close
1699 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001700 */
Chris Masonc8c42862009-04-03 10:14:18 -04001701static void reada_for_search(struct btrfs_root *root,
1702 struct btrfs_path *path,
1703 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001704{
Chris Mason5f39d392007-10-15 16:14:19 -04001705 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001706 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001707 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001708 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001709 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001710 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001711 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001712 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001713 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001714 u32 nr;
1715 u32 blocksize;
1716 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001717
Chris Masona6b6e752007-10-15 16:22:39 -04001718 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001719 return;
1720
Chris Mason6702ed42007-08-07 16:15:09 -04001721 if (!path->nodes[level])
1722 return;
1723
Chris Mason5f39d392007-10-15 16:14:19 -04001724 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001725
Chris Mason3c69fae2007-08-07 15:52:22 -04001726 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001727 blocksize = btrfs_level_size(root, level - 1);
1728 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001729 if (eb) {
1730 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001731 return;
1732 }
1733
Chris Masona7175312009-01-22 09:23:10 -05001734 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001735
Chris Mason5f39d392007-10-15 16:14:19 -04001736 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001737 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001738
Chris Masond3977122009-01-05 21:25:51 -05001739 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001740 if (direction < 0) {
1741 if (nr == 0)
1742 break;
1743 nr--;
1744 } else if (direction > 0) {
1745 nr++;
1746 if (nr >= nritems)
1747 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001748 }
Chris Mason01f46652007-12-21 16:24:26 -05001749 if (path->reada < 0 && objectid) {
1750 btrfs_node_key(node, &disk_key, nr);
1751 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1752 break;
1753 }
Chris Mason6b800532007-10-15 16:17:34 -04001754 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001755 if ((search <= target && target - search <= 65536) ||
1756 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001757 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001758 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001759 nread += blocksize;
1760 }
1761 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001762 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001763 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001764 }
1765}
Chris Mason925baed2008-06-25 16:01:30 -04001766
Chris Masond352ac62008-09-29 15:18:18 -04001767/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001768 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1769 * cache
1770 */
1771static noinline int reada_for_balance(struct btrfs_root *root,
1772 struct btrfs_path *path, int level)
1773{
1774 int slot;
1775 int nritems;
1776 struct extent_buffer *parent;
1777 struct extent_buffer *eb;
1778 u64 gen;
1779 u64 block1 = 0;
1780 u64 block2 = 0;
1781 int ret = 0;
1782 int blocksize;
1783
Chris Mason8c594ea2009-04-20 15:50:10 -04001784 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001785 if (!parent)
1786 return 0;
1787
1788 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04001789 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001790 blocksize = btrfs_level_size(root, level);
1791
1792 if (slot > 0) {
1793 block1 = btrfs_node_blockptr(parent, slot - 1);
1794 gen = btrfs_node_ptr_generation(parent, slot - 1);
1795 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001796 /*
1797 * if we get -eagain from btrfs_buffer_uptodate, we
1798 * don't want to return eagain here. That will loop
1799 * forever
1800 */
1801 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001802 block1 = 0;
1803 free_extent_buffer(eb);
1804 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001805 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001806 block2 = btrfs_node_blockptr(parent, slot + 1);
1807 gen = btrfs_node_ptr_generation(parent, slot + 1);
1808 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04001809 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001810 block2 = 0;
1811 free_extent_buffer(eb);
1812 }
1813 if (block1 || block2) {
1814 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04001815
1816 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02001817 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04001818
1819 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820 if (block1)
1821 readahead_tree_block(root, block1, blocksize, 0);
1822 if (block2)
1823 readahead_tree_block(root, block2, blocksize, 0);
1824
1825 if (block1) {
1826 eb = read_tree_block(root, block1, blocksize, 0);
1827 free_extent_buffer(eb);
1828 }
Chris Mason8c594ea2009-04-20 15:50:10 -04001829 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05001830 eb = read_tree_block(root, block2, blocksize, 0);
1831 free_extent_buffer(eb);
1832 }
1833 }
1834 return ret;
1835}
1836
1837
1838/*
Chris Masond3977122009-01-05 21:25:51 -05001839 * when we walk down the tree, it is usually safe to unlock the higher layers
1840 * in the tree. The exceptions are when our path goes through slot 0, because
1841 * operations on the tree might require changing key pointers higher up in the
1842 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04001843 *
Chris Masond3977122009-01-05 21:25:51 -05001844 * callers might also have set path->keep_locks, which tells this code to keep
1845 * the lock if the path points to the last slot in the block. This is part of
1846 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04001847 *
Chris Masond3977122009-01-05 21:25:51 -05001848 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
1849 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04001850 */
Chris Masone02119d2008-09-05 16:13:11 -04001851static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04001852 int lowest_unlock, int min_write_lock_level,
1853 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04001854{
1855 int i;
1856 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04001857 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04001858 struct extent_buffer *t;
1859
1860 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1861 if (!path->nodes[i])
1862 break;
1863 if (!path->locks[i])
1864 break;
Chris Mason051e1b92008-06-25 16:01:30 -04001865 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04001866 skip_level = i + 1;
1867 continue;
1868 }
Chris Mason051e1b92008-06-25 16:01:30 -04001869 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04001870 u32 nritems;
1871 t = path->nodes[i];
1872 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04001873 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04001874 skip_level = i + 1;
1875 continue;
1876 }
1877 }
Chris Mason051e1b92008-06-25 16:01:30 -04001878 if (skip_level < i && i >= lowest_unlock)
1879 no_skips = 1;
1880
Chris Mason925baed2008-06-25 16:01:30 -04001881 t = path->nodes[i];
1882 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04001883 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04001884 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04001885 if (write_lock_level &&
1886 i > min_write_lock_level &&
1887 i <= *write_lock_level) {
1888 *write_lock_level = i - 1;
1889 }
Chris Mason925baed2008-06-25 16:01:30 -04001890 }
1891 }
1892}
1893
Chris Mason3c69fae2007-08-07 15:52:22 -04001894/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001895 * This releases any locks held in the path starting at level and
1896 * going all the way up to the root.
1897 *
1898 * btrfs_search_slot will keep the lock held on higher nodes in a few
1899 * corner cases, such as COW of the block at slot zero in the node. This
1900 * ignores those rules, and it should only be called when there are no
1901 * more updates to be done higher up in the tree.
1902 */
1903noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
1904{
1905 int i;
1906
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001907 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05001908 return;
1909
1910 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
1911 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001912 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001913 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05001914 continue;
Chris Masonbd681512011-07-16 15:23:14 -04001915 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001916 path->locks[i] = 0;
1917 }
1918}
1919
1920/*
Chris Masonc8c42862009-04-03 10:14:18 -04001921 * helper function for btrfs_search_slot. The goal is to find a block
1922 * in cache without setting the path to blocking. If we find the block
1923 * we return zero and the path is unchanged.
1924 *
1925 * If we can't find the block, we set the path blocking and do some
1926 * reada. -EAGAIN is returned and the search must be repeated.
1927 */
1928static int
1929read_block_for_search(struct btrfs_trans_handle *trans,
1930 struct btrfs_root *root, struct btrfs_path *p,
1931 struct extent_buffer **eb_ret, int level, int slot,
1932 struct btrfs_key *key)
1933{
1934 u64 blocknr;
1935 u64 gen;
1936 u32 blocksize;
1937 struct extent_buffer *b = *eb_ret;
1938 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04001939 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04001940
1941 blocknr = btrfs_node_blockptr(b, slot);
1942 gen = btrfs_node_ptr_generation(b, slot);
1943 blocksize = btrfs_level_size(root, level - 1);
1944
1945 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04001946 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04001947 /* first we do an atomic uptodate check */
1948 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
1949 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001950 /*
1951 * we found an up to date block without
1952 * sleeping, return
1953 * right away
1954 */
1955 *eb_ret = tmp;
1956 return 0;
1957 }
1958 /* the pages were up to date, but we failed
1959 * the generation number check. Do a full
1960 * read for the generation number that is correct.
1961 * We must do this without dropping locks so
1962 * we can trust our generation number
1963 */
1964 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04001965 btrfs_set_path_blocking(p);
1966
Chris Masonb9fab912012-05-06 07:23:47 -04001967 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04001968 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04001969 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04001970 *eb_ret = tmp;
1971 return 0;
1972 }
1973 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02001974 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04001975 return -EIO;
1976 }
Chris Masonc8c42862009-04-03 10:14:18 -04001977 }
1978
1979 /*
1980 * reduce lock contention at high levels
1981 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04001982 * we read. Don't release the lock on the current
1983 * level because we need to walk this node to figure
1984 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04001985 */
Chris Mason8c594ea2009-04-20 15:50:10 -04001986 btrfs_unlock_up_safe(p, level + 1);
1987 btrfs_set_path_blocking(p);
1988
Chris Masoncb449212010-10-24 11:01:27 -04001989 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04001990 if (p->reada)
1991 reada_for_search(root, p, level, slot, key->objectid);
1992
David Sterbab3b4aa72011-04-21 01:20:15 +02001993 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04001994
1995 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04001996 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04001997 if (tmp) {
1998 /*
1999 * If the read above didn't mark this buffer up to date,
2000 * it will never end up being up to date. Set ret to EIO now
2001 * and give up so that our caller doesn't loop forever
2002 * on our EAGAINs.
2003 */
Chris Masonb9fab912012-05-06 07:23:47 -04002004 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002005 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002006 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002007 }
2008 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002009}
2010
2011/*
2012 * helper function for btrfs_search_slot. This does all of the checks
2013 * for node-level blocks and does any balancing required based on
2014 * the ins_len.
2015 *
2016 * If no extra work was required, zero is returned. If we had to
2017 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2018 * start over
2019 */
2020static int
2021setup_nodes_for_search(struct btrfs_trans_handle *trans,
2022 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002023 struct extent_buffer *b, int level, int ins_len,
2024 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002025{
2026 int ret;
2027 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2028 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2029 int sret;
2030
Chris Masonbd681512011-07-16 15:23:14 -04002031 if (*write_lock_level < level + 1) {
2032 *write_lock_level = level + 1;
2033 btrfs_release_path(p);
2034 goto again;
2035 }
2036
Chris Masonc8c42862009-04-03 10:14:18 -04002037 sret = reada_for_balance(root, p, level);
2038 if (sret)
2039 goto again;
2040
2041 btrfs_set_path_blocking(p);
2042 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002043 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002044
2045 BUG_ON(sret > 0);
2046 if (sret) {
2047 ret = sret;
2048 goto done;
2049 }
2050 b = p->nodes[level];
2051 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002052 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002053 int sret;
2054
Chris Masonbd681512011-07-16 15:23:14 -04002055 if (*write_lock_level < level + 1) {
2056 *write_lock_level = level + 1;
2057 btrfs_release_path(p);
2058 goto again;
2059 }
2060
Chris Masonc8c42862009-04-03 10:14:18 -04002061 sret = reada_for_balance(root, p, level);
2062 if (sret)
2063 goto again;
2064
2065 btrfs_set_path_blocking(p);
2066 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002067 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002068
2069 if (sret) {
2070 ret = sret;
2071 goto done;
2072 }
2073 b = p->nodes[level];
2074 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002075 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002076 goto again;
2077 }
2078 BUG_ON(btrfs_header_nritems(b) == 1);
2079 }
2080 return 0;
2081
2082again:
2083 ret = -EAGAIN;
2084done:
2085 return ret;
2086}
2087
2088/*
Chris Mason74123bd2007-02-02 11:05:29 -05002089 * look for key in the tree. path is filled in with nodes along the way
2090 * if key is found, we return zero and you can find the item in the leaf
2091 * level of the path (level 0)
2092 *
2093 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002094 * be inserted, and 1 is returned. If there are other errors during the
2095 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002096 *
2097 * if ins_len > 0, nodes and leaves will be split as we walk down the
2098 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2099 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002100 */
Chris Masone089f052007-03-16 16:20:31 -04002101int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2102 *root, struct btrfs_key *key, struct btrfs_path *p, int
2103 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002104{
Chris Mason5f39d392007-10-15 16:14:19 -04002105 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002106 int slot;
2107 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002108 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002109 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002110 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002111 int root_lock;
2112 /* everything at write_lock_level or lower must be write locked */
2113 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002114 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002115 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002116
Chris Mason6702ed42007-08-07 16:15:09 -04002117 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002118 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002119 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002120
Chris Masonbd681512011-07-16 15:23:14 -04002121 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002122 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002123
Chris Masonbd681512011-07-16 15:23:14 -04002124 /* when we are removing items, we might have to go up to level
2125 * two as we update tree pointers Make sure we keep write
2126 * for those levels as well
2127 */
2128 write_lock_level = 2;
2129 } else if (ins_len > 0) {
2130 /*
2131 * for inserting items, make sure we have a write lock on
2132 * level 1 so we can update keys
2133 */
2134 write_lock_level = 1;
2135 }
2136
2137 if (!cow)
2138 write_lock_level = -1;
2139
2140 if (cow && (p->keep_locks || p->lowest_level))
2141 write_lock_level = BTRFS_MAX_LEVEL;
2142
Chris Masonf7c79f32012-03-19 15:54:38 -04002143 min_write_lock_level = write_lock_level;
2144
Chris Masonbb803952007-03-01 12:04:21 -05002145again:
Chris Masonbd681512011-07-16 15:23:14 -04002146 /*
2147 * we try very hard to do read locks on the root
2148 */
2149 root_lock = BTRFS_READ_LOCK;
2150 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002151 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002152 /*
2153 * the commit roots are read only
2154 * so we always do read locks
2155 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002156 b = root->commit_root;
2157 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002158 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002159 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002160 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002162 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002163 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002164 level = btrfs_header_level(b);
2165 } else {
2166 /* we don't know the level of the root node
2167 * until we actually have it read locked
2168 */
2169 b = btrfs_read_lock_root_node(root);
2170 level = btrfs_header_level(b);
2171 if (level <= write_lock_level) {
2172 /* whoops, must trade for write lock */
2173 btrfs_tree_read_unlock(b);
2174 free_extent_buffer(b);
2175 b = btrfs_lock_root_node(root);
2176 root_lock = BTRFS_WRITE_LOCK;
2177
2178 /* the level might have changed, check again */
2179 level = btrfs_header_level(b);
2180 }
2181 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002182 }
Chris Masonbd681512011-07-16 15:23:14 -04002183 p->nodes[level] = b;
2184 if (!p->skip_locking)
2185 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002186
Chris Masoneb60cea2007-02-02 09:18:22 -05002187 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002188 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002189
2190 /*
2191 * setup the path here so we can release it under lock
2192 * contention with the cow code
2193 */
Chris Mason02217ed2007-03-02 16:08:05 -05002194 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002195 /*
2196 * if we don't really need to cow this block
2197 * then we don't want to set the path blocking,
2198 * so we test it here
2199 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002200 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002201 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202
Chris Masonb4ce94d2009-02-04 09:25:08 -05002203 btrfs_set_path_blocking(p);
2204
Chris Masonbd681512011-07-16 15:23:14 -04002205 /*
2206 * must have write locks on this node and the
2207 * parent
2208 */
2209 if (level + 1 > write_lock_level) {
2210 write_lock_level = level + 1;
2211 btrfs_release_path(p);
2212 goto again;
2213 }
2214
Yan Zheng33c66f42009-07-22 09:59:00 -04002215 err = btrfs_cow_block(trans, root, b,
2216 p->nodes[level + 1],
2217 p->slots[level + 1], &b);
2218 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002219 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002220 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002221 }
Chris Mason02217ed2007-03-02 16:08:05 -05002222 }
Chris Mason65b51a02008-08-01 15:11:20 -04002223cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002224 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002225
Chris Masoneb60cea2007-02-02 09:18:22 -05002226 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002227 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002228
2229 /*
2230 * we have a lock on b and as long as we aren't changing
2231 * the tree, there is no way to for the items in b to change.
2232 * It is safe to drop the lock on our parent before we
2233 * go through the expensive btree search on b.
2234 *
2235 * If cow is true, then we might be changing slot zero,
2236 * which may require changing the parent. So, we can't
2237 * drop the lock until after we know which slot we're
2238 * operating on.
2239 */
2240 if (!cow)
2241 btrfs_unlock_up_safe(p, level + 1);
2242
Chris Mason5f39d392007-10-15 16:14:19 -04002243 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002244
Chris Mason5f39d392007-10-15 16:14:19 -04002245 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002246 int dec = 0;
2247 if (ret && slot > 0) {
2248 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002249 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002250 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002251 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002252 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002253 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002254 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002255 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002256 if (err) {
2257 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002258 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002259 }
Chris Masonc8c42862009-04-03 10:14:18 -04002260 b = p->nodes[level];
2261 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002262
Chris Masonbd681512011-07-16 15:23:14 -04002263 /*
2264 * slot 0 is special, if we change the key
2265 * we have to update the parent pointer
2266 * which means we must have a write lock
2267 * on the parent
2268 */
2269 if (slot == 0 && cow &&
2270 write_lock_level < level + 1) {
2271 write_lock_level = level + 1;
2272 btrfs_release_path(p);
2273 goto again;
2274 }
2275
Chris Masonf7c79f32012-03-19 15:54:38 -04002276 unlock_up(p, level, lowest_unlock,
2277 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002278
Chris Mason925baed2008-06-25 16:01:30 -04002279 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002280 if (dec)
2281 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002282 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002283 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002284
Yan Zheng33c66f42009-07-22 09:59:00 -04002285 err = read_block_for_search(trans, root, p,
Chris Masonc8c42862009-04-03 10:14:18 -04002286 &b, level, slot, key);
Yan Zheng33c66f42009-07-22 09:59:00 -04002287 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002288 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002289 if (err) {
2290 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002291 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002292 }
Chris Mason76a05b32009-05-14 13:24:30 -04002293
Chris Masonb4ce94d2009-02-04 09:25:08 -05002294 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002295 level = btrfs_header_level(b);
2296 if (level <= write_lock_level) {
2297 err = btrfs_try_tree_write_lock(b);
2298 if (!err) {
2299 btrfs_set_path_blocking(p);
2300 btrfs_tree_lock(b);
2301 btrfs_clear_path_blocking(p, b,
2302 BTRFS_WRITE_LOCK);
2303 }
2304 p->locks[level] = BTRFS_WRITE_LOCK;
2305 } else {
2306 err = btrfs_try_tree_read_lock(b);
2307 if (!err) {
2308 btrfs_set_path_blocking(p);
2309 btrfs_tree_read_lock(b);
2310 btrfs_clear_path_blocking(p, b,
2311 BTRFS_READ_LOCK);
2312 }
2313 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002314 }
Chris Masonbd681512011-07-16 15:23:14 -04002315 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002316 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002317 } else {
2318 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002319 if (ins_len > 0 &&
2320 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002321 if (write_lock_level < 1) {
2322 write_lock_level = 1;
2323 btrfs_release_path(p);
2324 goto again;
2325 }
2326
Chris Masonb4ce94d2009-02-04 09:25:08 -05002327 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002328 err = split_leaf(trans, root, key,
2329 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002330 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002331
Yan Zheng33c66f42009-07-22 09:59:00 -04002332 BUG_ON(err > 0);
2333 if (err) {
2334 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002335 goto done;
2336 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002337 }
Chris Mason459931e2008-12-10 09:10:46 -05002338 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002339 unlock_up(p, level, lowest_unlock,
2340 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002341 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002342 }
2343 }
Chris Mason65b51a02008-08-01 15:11:20 -04002344 ret = 1;
2345done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002346 /*
2347 * we don't really know what they plan on doing with the path
2348 * from here on, so for now just mark it as blocking
2349 */
Chris Masonb9473432009-03-13 11:00:37 -04002350 if (!p->leave_spinning)
2351 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002352 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002353 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002354 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002355}
2356
Chris Mason74123bd2007-02-02 11:05:29 -05002357/*
2358 * adjust the pointers going up the tree, starting at level
2359 * making sure the right key of each node is points to 'key'.
2360 * This is used after shifting pointers to the left, so it stops
2361 * fixing up pointers when a given leaf/node is not in slot 0 of the
2362 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002363 *
Chris Mason74123bd2007-02-02 11:05:29 -05002364 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002365static void fixup_low_keys(struct btrfs_trans_handle *trans,
2366 struct btrfs_root *root, struct btrfs_path *path,
2367 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002368{
2369 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002370 struct extent_buffer *t;
2371
Chris Mason234b63a2007-03-13 10:46:10 -04002372 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002373 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002374 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002375 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002376 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002377 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002378 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002379 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002380 if (tslot != 0)
2381 break;
2382 }
2383}
2384
Chris Mason74123bd2007-02-02 11:05:29 -05002385/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002386 * update item key.
2387 *
2388 * This function isn't completely safe. It's the caller's responsibility
2389 * that the new key won't break the order
2390 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002391void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2392 struct btrfs_root *root, struct btrfs_path *path,
2393 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002394{
2395 struct btrfs_disk_key disk_key;
2396 struct extent_buffer *eb;
2397 int slot;
2398
2399 eb = path->nodes[0];
2400 slot = path->slots[0];
2401 if (slot > 0) {
2402 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002403 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002404 }
2405 if (slot < btrfs_header_nritems(eb) - 1) {
2406 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002407 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002408 }
2409
2410 btrfs_cpu_key_to_disk(&disk_key, new_key);
2411 btrfs_set_item_key(eb, &disk_key, slot);
2412 btrfs_mark_buffer_dirty(eb);
2413 if (slot == 0)
2414 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002415}
2416
2417/*
Chris Mason74123bd2007-02-02 11:05:29 -05002418 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002419 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002420 *
2421 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2422 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002423 */
Chris Mason98ed5172008-01-03 10:01:48 -05002424static int push_node_left(struct btrfs_trans_handle *trans,
2425 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002426 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002427{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002428 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002429 int src_nritems;
2430 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002431 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002432
Chris Mason5f39d392007-10-15 16:14:19 -04002433 src_nritems = btrfs_header_nritems(src);
2434 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002435 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002436 WARN_ON(btrfs_header_generation(src) != trans->transid);
2437 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002438
Chris Masonbce4eae2008-04-24 14:42:46 -04002439 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002440 return 1;
2441
Chris Masond3977122009-01-05 21:25:51 -05002442 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002443 return 1;
2444
Chris Masonbce4eae2008-04-24 14:42:46 -04002445 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002446 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002447 if (push_items < src_nritems) {
2448 /* leave at least 8 pointers in the node if
2449 * we aren't going to empty it
2450 */
2451 if (src_nritems - push_items < 8) {
2452 if (push_items <= 8)
2453 return 1;
2454 push_items -= 8;
2455 }
2456 }
2457 } else
2458 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002459
Jan Schmidtf2304752012-05-26 11:43:17 +02002460 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2461 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002462 copy_extent_buffer(dst, src,
2463 btrfs_node_key_ptr_offset(dst_nritems),
2464 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002465 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002466
Chris Masonbb803952007-03-01 12:04:21 -05002467 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002468 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2469 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002470 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2471 btrfs_node_key_ptr_offset(push_items),
2472 (src_nritems - push_items) *
2473 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002474 }
Chris Mason5f39d392007-10-15 16:14:19 -04002475 btrfs_set_header_nritems(src, src_nritems - push_items);
2476 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2477 btrfs_mark_buffer_dirty(src);
2478 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002479
Chris Masonbb803952007-03-01 12:04:21 -05002480 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002481}
2482
Chris Mason97571fd2007-02-24 13:39:08 -05002483/*
Chris Mason79f95c82007-03-01 15:16:26 -05002484 * try to push data from one node into the next node right in the
2485 * tree.
2486 *
2487 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2488 * error, and > 0 if there was no room in the right hand block.
2489 *
2490 * this will only push up to 1/2 the contents of the left node over
2491 */
Chris Mason5f39d392007-10-15 16:14:19 -04002492static int balance_node_right(struct btrfs_trans_handle *trans,
2493 struct btrfs_root *root,
2494 struct extent_buffer *dst,
2495 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002496{
Chris Mason79f95c82007-03-01 15:16:26 -05002497 int push_items = 0;
2498 int max_push;
2499 int src_nritems;
2500 int dst_nritems;
2501 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002502
Chris Mason7bb86312007-12-11 09:25:06 -05002503 WARN_ON(btrfs_header_generation(src) != trans->transid);
2504 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2505
Chris Mason5f39d392007-10-15 16:14:19 -04002506 src_nritems = btrfs_header_nritems(src);
2507 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002508 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002509 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002510 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002511
Chris Masond3977122009-01-05 21:25:51 -05002512 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002513 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002514
2515 max_push = src_nritems / 2 + 1;
2516 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002517 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002518 return 1;
Yan252c38f2007-08-29 09:11:44 -04002519
Chris Mason79f95c82007-03-01 15:16:26 -05002520 if (max_push < push_items)
2521 push_items = max_push;
2522
Jan Schmidtf2304752012-05-26 11:43:17 +02002523 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002524 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2525 btrfs_node_key_ptr_offset(0),
2526 (dst_nritems) *
2527 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002528
Jan Schmidtf2304752012-05-26 11:43:17 +02002529 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2530 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002531 copy_extent_buffer(dst, src,
2532 btrfs_node_key_ptr_offset(0),
2533 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002534 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002535
Chris Mason5f39d392007-10-15 16:14:19 -04002536 btrfs_set_header_nritems(src, src_nritems - push_items);
2537 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002538
Chris Mason5f39d392007-10-15 16:14:19 -04002539 btrfs_mark_buffer_dirty(src);
2540 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002541
Chris Mason79f95c82007-03-01 15:16:26 -05002542 return ret;
2543}
2544
2545/*
Chris Mason97571fd2007-02-24 13:39:08 -05002546 * helper function to insert a new root level in the tree.
2547 * A new node is allocated, and a single item is inserted to
2548 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002549 *
2550 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002551 */
Chris Masond3977122009-01-05 21:25:51 -05002552static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002553 struct btrfs_root *root,
2554 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002555{
Chris Mason7bb86312007-12-11 09:25:06 -05002556 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002557 struct extent_buffer *lower;
2558 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002559 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002560 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002561
2562 BUG_ON(path->nodes[level]);
2563 BUG_ON(path->nodes[level-1] != root->node);
2564
Chris Mason7bb86312007-12-11 09:25:06 -05002565 lower = path->nodes[level-1];
2566 if (level == 1)
2567 btrfs_item_key(lower, &lower_key, 0);
2568 else
2569 btrfs_node_key(lower, &lower_key, 0);
2570
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002572 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002573 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002574 if (IS_ERR(c))
2575 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002576
Yan, Zhengf0486c62010-05-16 10:46:25 -04002577 root_add_used(root, root->nodesize);
2578
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002579 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002580 btrfs_set_header_nritems(c, 1);
2581 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002582 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002583 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002584 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002585 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002586
Chris Mason5f39d392007-10-15 16:14:19 -04002587 write_extent_buffer(c, root->fs_info->fsid,
2588 (unsigned long)btrfs_header_fsid(c),
2589 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002590
2591 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2592 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2593 BTRFS_UUID_SIZE);
2594
Chris Mason5f39d392007-10-15 16:14:19 -04002595 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002596 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002597 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002598 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002599
2600 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002601
2602 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002603
Chris Mason925baed2008-06-25 16:01:30 -04002604 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002605 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002606 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002607
2608 /* the super has an extra ref to root->node */
2609 free_extent_buffer(old);
2610
Chris Mason0b86a832008-03-24 15:01:56 -04002611 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002612 extent_buffer_get(c);
2613 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002614 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002615 path->slots[level] = 0;
2616 return 0;
2617}
2618
Chris Mason74123bd2007-02-02 11:05:29 -05002619/*
2620 * worker function to insert a single pointer in a node.
2621 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002622 *
Chris Mason74123bd2007-02-02 11:05:29 -05002623 * slot and level indicate where you want the key to go, and
2624 * blocknr is the block the key points to.
2625 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002626static void insert_ptr(struct btrfs_trans_handle *trans,
2627 struct btrfs_root *root, struct btrfs_path *path,
2628 struct btrfs_disk_key *key, u64 bytenr,
2629 int slot, int level)
Chris Mason74123bd2007-02-02 11:05:29 -05002630{
Chris Mason5f39d392007-10-15 16:14:19 -04002631 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002632 int nritems;
Chris Mason5c680ed2007-02-22 11:39:13 -05002633
2634 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002635 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002636 lower = path->nodes[level];
2637 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002638 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002639 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002640 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04002641 memmove_extent_buffer(lower,
2642 btrfs_node_key_ptr_offset(slot + 1),
2643 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002644 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002645 }
Chris Mason5f39d392007-10-15 16:14:19 -04002646 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002647 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002648 WARN_ON(trans->transid == 0);
2649 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002650 btrfs_set_header_nritems(lower, nritems + 1);
2651 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002652}
2653
Chris Mason97571fd2007-02-24 13:39:08 -05002654/*
2655 * split the node at the specified level in path in two.
2656 * The path is corrected to point to the appropriate node after the split
2657 *
2658 * Before splitting this tries to make some room in the node by pushing
2659 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002660 *
2661 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002662 */
Chris Masone02119d2008-09-05 16:13:11 -04002663static noinline int split_node(struct btrfs_trans_handle *trans,
2664 struct btrfs_root *root,
2665 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002666{
Chris Mason5f39d392007-10-15 16:14:19 -04002667 struct extent_buffer *c;
2668 struct extent_buffer *split;
2669 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002670 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05002671 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04002672 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002673
Chris Mason5f39d392007-10-15 16:14:19 -04002674 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05002675 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002676 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002677 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04002678 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05002679 if (ret)
2680 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04002681 } else {
Chris Masone66f7092007-04-20 13:16:02 -04002682 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04002683 c = path->nodes[level];
2684 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04002685 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04002686 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04002687 if (ret < 0)
2688 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002689 }
Chris Masone66f7092007-04-20 13:16:02 -04002690
Chris Mason5f39d392007-10-15 16:14:19 -04002691 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002692 mid = (c_nritems + 1) / 2;
2693 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05002694
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002695 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04002696 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02002697 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002698 if (IS_ERR(split))
2699 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04002700
Yan, Zhengf0486c62010-05-16 10:46:25 -04002701 root_add_used(root, root->nodesize);
2702
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002703 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002704 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04002705 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002706 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002707 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002708 btrfs_set_header_owner(split, root->root_key.objectid);
2709 write_extent_buffer(split, root->fs_info->fsid,
2710 (unsigned long)btrfs_header_fsid(split),
2711 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002712 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
2713 (unsigned long)btrfs_header_chunk_tree_uuid(split),
2714 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04002715
Jan Schmidtf2304752012-05-26 11:43:17 +02002716 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04002717 copy_extent_buffer(split, c,
2718 btrfs_node_key_ptr_offset(0),
2719 btrfs_node_key_ptr_offset(mid),
2720 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
2721 btrfs_set_header_nritems(split, c_nritems - mid);
2722 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002723 ret = 0;
2724
Chris Mason5f39d392007-10-15 16:14:19 -04002725 btrfs_mark_buffer_dirty(c);
2726 btrfs_mark_buffer_dirty(split);
2727
Jeff Mahoney143bede2012-03-01 14:56:26 +01002728 insert_ptr(trans, root, path, &disk_key, split->start,
2729 path->slots[level + 1] + 1, level + 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05002730
Chris Mason5de08d72007-02-24 06:24:44 -05002731 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05002732 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04002733 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04002734 free_extent_buffer(c);
2735 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05002736 path->slots[level + 1] += 1;
2737 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002738 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04002739 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002740 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05002741 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002742}
2743
Chris Mason74123bd2007-02-02 11:05:29 -05002744/*
2745 * how many bytes are required to store the items in a leaf. start
2746 * and nr indicate which items in the leaf to check. This totals up the
2747 * space used both by the item structs and the item data
2748 */
Chris Mason5f39d392007-10-15 16:14:19 -04002749static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002750{
2751 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04002752 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04002753 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002754
2755 if (!nr)
2756 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002757 data_len = btrfs_item_end_nr(l, start);
2758 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04002759 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04002760 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761 return data_len;
2762}
2763
Chris Mason74123bd2007-02-02 11:05:29 -05002764/*
Chris Masond4dbff92007-04-04 14:08:15 -04002765 * The space between the end of the leaf items and
2766 * the start of the leaf data. IOW, how much room
2767 * the leaf has left for both items and data
2768 */
Chris Masond3977122009-01-05 21:25:51 -05002769noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04002770 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04002771{
Chris Mason5f39d392007-10-15 16:14:19 -04002772 int nritems = btrfs_header_nritems(leaf);
2773 int ret;
2774 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
2775 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05002776 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
2777 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04002778 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04002779 leaf_space_used(leaf, 0, nritems), nritems);
2780 }
2781 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04002782}
2783
Chris Mason99d8f832010-07-07 10:51:48 -04002784/*
2785 * min slot controls the lowest index we're willing to push to the
2786 * right. We'll push up to and including min_slot, but no lower
2787 */
Chris Mason44871b12009-03-13 10:04:31 -04002788static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
2789 struct btrfs_root *root,
2790 struct btrfs_path *path,
2791 int data_size, int empty,
2792 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04002793 int free_space, u32 left_nritems,
2794 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05002795{
Chris Mason5f39d392007-10-15 16:14:19 -04002796 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04002797 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05002798 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04002799 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05002800 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05002801 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05002802 int push_space = 0;
2803 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04002804 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05002805 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04002806 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002807 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04002808 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05002809
Chris Masoncfed81a2012-03-03 07:40:03 -05002810 btrfs_init_map_token(&token);
2811
Chris Mason34a38212007-11-07 13:31:03 -05002812 if (empty)
2813 nr = 0;
2814 else
Chris Mason99d8f832010-07-07 10:51:48 -04002815 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05002816
Zheng Yan31840ae2008-09-23 13:14:14 -04002817 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05002818 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04002819
Chris Mason44871b12009-03-13 10:04:31 -04002820 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05002821 i = left_nritems - 1;
2822 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04002823 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04002824
Zheng Yan31840ae2008-09-23 13:14:14 -04002825 if (!empty && push_items > 0) {
2826 if (path->slots[0] > i)
2827 break;
2828 if (path->slots[0] == i) {
2829 int space = btrfs_leaf_free_space(root, left);
2830 if (space + push_space * 2 > free_space)
2831 break;
2832 }
2833 }
2834
Chris Mason00ec4c52007-02-24 12:47:20 -05002835 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05002836 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04002837
Chris Masondb945352007-10-15 16:15:53 -04002838 this_item_size = btrfs_item_size(left, item);
2839 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05002840 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04002841
Chris Mason00ec4c52007-02-24 12:47:20 -05002842 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04002843 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05002844 if (i == 0)
2845 break;
2846 i--;
Chris Masondb945352007-10-15 16:15:53 -04002847 }
Chris Mason5f39d392007-10-15 16:14:19 -04002848
Chris Mason925baed2008-06-25 16:01:30 -04002849 if (push_items == 0)
2850 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04002851
Chris Mason34a38212007-11-07 13:31:03 -05002852 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04002853 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04002854
Chris Mason00ec4c52007-02-24 12:47:20 -05002855 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002856 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05002857
Chris Mason5f39d392007-10-15 16:14:19 -04002858 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04002859 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04002860
Chris Mason00ec4c52007-02-24 12:47:20 -05002861 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002862 data_end = leaf_data_end(root, right);
2863 memmove_extent_buffer(right,
2864 btrfs_leaf_data(right) + data_end - push_space,
2865 btrfs_leaf_data(right) + data_end,
2866 BTRFS_LEAF_DATA_SIZE(root) - data_end);
2867
Chris Mason00ec4c52007-02-24 12:47:20 -05002868 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04002869 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04002870 BTRFS_LEAF_DATA_SIZE(root) - push_space,
2871 btrfs_leaf_data(left) + leaf_data_end(root, left),
2872 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04002873
2874 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
2875 btrfs_item_nr_offset(0),
2876 right_nritems * sizeof(struct btrfs_item));
2877
Chris Mason00ec4c52007-02-24 12:47:20 -05002878 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04002879 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
2880 btrfs_item_nr_offset(left_nritems - push_items),
2881 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05002882
2883 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04002884 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002885 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04002886 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04002887 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04002888 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05002889 push_space -= btrfs_token_item_size(right, item, &token);
2890 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04002891 }
2892
Chris Mason7518a232007-03-12 12:01:18 -04002893 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04002894 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05002895
Chris Mason34a38212007-11-07 13:31:03 -05002896 if (left_nritems)
2897 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002898 else
2899 clean_tree_block(trans, root, left);
2900
Chris Mason5f39d392007-10-15 16:14:19 -04002901 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04002902
Chris Mason5f39d392007-10-15 16:14:19 -04002903 btrfs_item_key(right, &disk_key, 0);
2904 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04002905 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05002906
Chris Mason00ec4c52007-02-24 12:47:20 -05002907 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04002908 if (path->slots[0] >= left_nritems) {
2909 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04002910 if (btrfs_header_nritems(path->nodes[0]) == 0)
2911 clean_tree_block(trans, root, path->nodes[0]);
2912 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04002913 free_extent_buffer(path->nodes[0]);
2914 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05002915 path->slots[1] += 1;
2916 } else {
Chris Mason925baed2008-06-25 16:01:30 -04002917 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04002918 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05002919 }
2920 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04002921
2922out_unlock:
2923 btrfs_tree_unlock(right);
2924 free_extent_buffer(right);
2925 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05002926}
Chris Mason925baed2008-06-25 16:01:30 -04002927
Chris Mason00ec4c52007-02-24 12:47:20 -05002928/*
Chris Mason44871b12009-03-13 10:04:31 -04002929 * push some data in the path leaf to the right, trying to free up at
2930 * least data_size bytes. returns zero if the push worked, nonzero otherwise
2931 *
2932 * returns 1 if the push failed because the other node didn't have enough
2933 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04002934 *
2935 * this will push starting from min_slot to the end of the leaf. It won't
2936 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04002937 */
2938static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04002939 *root, struct btrfs_path *path,
2940 int min_data_size, int data_size,
2941 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04002942{
2943 struct extent_buffer *left = path->nodes[0];
2944 struct extent_buffer *right;
2945 struct extent_buffer *upper;
2946 int slot;
2947 int free_space;
2948 u32 left_nritems;
2949 int ret;
2950
2951 if (!path->nodes[1])
2952 return 1;
2953
2954 slot = path->slots[1];
2955 upper = path->nodes[1];
2956 if (slot >= btrfs_header_nritems(upper) - 1)
2957 return 1;
2958
2959 btrfs_assert_tree_locked(path->nodes[1]);
2960
2961 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00002962 if (right == NULL)
2963 return 1;
2964
Chris Mason44871b12009-03-13 10:04:31 -04002965 btrfs_tree_lock(right);
2966 btrfs_set_lock_blocking(right);
2967
2968 free_space = btrfs_leaf_free_space(root, right);
2969 if (free_space < data_size)
2970 goto out_unlock;
2971
2972 /* cow and double check */
2973 ret = btrfs_cow_block(trans, root, right, upper,
2974 slot + 1, &right);
2975 if (ret)
2976 goto out_unlock;
2977
2978 free_space = btrfs_leaf_free_space(root, right);
2979 if (free_space < data_size)
2980 goto out_unlock;
2981
2982 left_nritems = btrfs_header_nritems(left);
2983 if (left_nritems == 0)
2984 goto out_unlock;
2985
Chris Mason99d8f832010-07-07 10:51:48 -04002986 return __push_leaf_right(trans, root, path, min_data_size, empty,
2987 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04002988out_unlock:
2989 btrfs_tree_unlock(right);
2990 free_extent_buffer(right);
2991 return 1;
2992}
2993
2994/*
Chris Mason74123bd2007-02-02 11:05:29 -05002995 * push some data in the path leaf to the left, trying to free up at
2996 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04002997 *
2998 * max_slot can put a limit on how far into the leaf we'll push items. The
2999 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3000 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003001 */
Chris Mason44871b12009-03-13 10:04:31 -04003002static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3003 struct btrfs_root *root,
3004 struct btrfs_path *path, int data_size,
3005 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003006 int free_space, u32 right_nritems,
3007 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003008{
Chris Mason5f39d392007-10-15 16:14:19 -04003009 struct btrfs_disk_key disk_key;
3010 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003011 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003012 int push_space = 0;
3013 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003014 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003015 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003016 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003017 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003018 u32 this_item_size;
3019 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003020 struct btrfs_map_token token;
3021
3022 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003023
Chris Mason34a38212007-11-07 13:31:03 -05003024 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003025 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003026 else
Chris Mason99d8f832010-07-07 10:51:48 -04003027 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003028
3029 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003030 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003031
Zheng Yan31840ae2008-09-23 13:14:14 -04003032 if (!empty && push_items > 0) {
3033 if (path->slots[0] < i)
3034 break;
3035 if (path->slots[0] == i) {
3036 int space = btrfs_leaf_free_space(root, right);
3037 if (space + push_space * 2 > free_space)
3038 break;
3039 }
3040 }
3041
Chris Masonbe0e5c02007-01-26 15:51:26 -05003042 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003043 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003044
3045 this_item_size = btrfs_item_size(right, item);
3046 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003047 break;
Chris Masondb945352007-10-15 16:15:53 -04003048
Chris Masonbe0e5c02007-01-26 15:51:26 -05003049 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003050 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003051 }
Chris Masondb945352007-10-15 16:15:53 -04003052
Chris Masonbe0e5c02007-01-26 15:51:26 -05003053 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003054 ret = 1;
3055 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003056 }
Chris Mason34a38212007-11-07 13:31:03 -05003057 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003058 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003059
Chris Masonbe0e5c02007-01-26 15:51:26 -05003060 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003061 copy_extent_buffer(left, right,
3062 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3063 btrfs_item_nr_offset(0),
3064 push_items * sizeof(struct btrfs_item));
3065
Chris Mason123abc82007-03-14 14:14:43 -04003066 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003067 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003068
3069 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003070 leaf_data_end(root, left) - push_space,
3071 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003072 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003073 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003074 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003075 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003076
Chris Masondb945352007-10-15 16:15:53 -04003077 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003078 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003079 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003080
Chris Mason5f39d392007-10-15 16:14:19 -04003081 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003082
Chris Masoncfed81a2012-03-03 07:40:03 -05003083 ioff = btrfs_token_item_offset(left, item, &token);
3084 btrfs_set_token_item_offset(left, item,
3085 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3086 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003087 }
Chris Mason5f39d392007-10-15 16:14:19 -04003088 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003089
3090 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003091 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003092 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3093 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003094 WARN_ON(1);
3095 }
Chris Mason5f39d392007-10-15 16:14:19 -04003096
Chris Mason34a38212007-11-07 13:31:03 -05003097 if (push_items < right_nritems) {
3098 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3099 leaf_data_end(root, right);
3100 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3101 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3102 btrfs_leaf_data(right) +
3103 leaf_data_end(root, right), push_space);
3104
3105 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003106 btrfs_item_nr_offset(push_items),
3107 (btrfs_header_nritems(right) - push_items) *
3108 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003109 }
Yaneef1c492007-11-26 10:58:13 -05003110 right_nritems -= push_items;
3111 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003112 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003113 for (i = 0; i < right_nritems; i++) {
3114 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003115
Chris Masoncfed81a2012-03-03 07:40:03 -05003116 push_space = push_space - btrfs_token_item_size(right,
3117 item, &token);
3118 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003119 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003120
Chris Mason5f39d392007-10-15 16:14:19 -04003121 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003122 if (right_nritems)
3123 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003124 else
3125 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003126
Chris Mason5f39d392007-10-15 16:14:19 -04003127 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003128 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003129
3130 /* then fixup the leaf pointer in the path */
3131 if (path->slots[0] < push_items) {
3132 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003133 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003134 free_extent_buffer(path->nodes[0]);
3135 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003136 path->slots[1] -= 1;
3137 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003138 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003139 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003140 path->slots[0] -= push_items;
3141 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003142 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003143 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003144out:
3145 btrfs_tree_unlock(left);
3146 free_extent_buffer(left);
3147 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003148}
3149
Chris Mason74123bd2007-02-02 11:05:29 -05003150/*
Chris Mason44871b12009-03-13 10:04:31 -04003151 * push some data in the path leaf to the left, trying to free up at
3152 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003153 *
3154 * max_slot can put a limit on how far into the leaf we'll push items. The
3155 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3156 * items
Chris Mason44871b12009-03-13 10:04:31 -04003157 */
3158static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003159 *root, struct btrfs_path *path, int min_data_size,
3160 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003161{
3162 struct extent_buffer *right = path->nodes[0];
3163 struct extent_buffer *left;
3164 int slot;
3165 int free_space;
3166 u32 right_nritems;
3167 int ret = 0;
3168
3169 slot = path->slots[1];
3170 if (slot == 0)
3171 return 1;
3172 if (!path->nodes[1])
3173 return 1;
3174
3175 right_nritems = btrfs_header_nritems(right);
3176 if (right_nritems == 0)
3177 return 1;
3178
3179 btrfs_assert_tree_locked(path->nodes[1]);
3180
3181 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003182 if (left == NULL)
3183 return 1;
3184
Chris Mason44871b12009-03-13 10:04:31 -04003185 btrfs_tree_lock(left);
3186 btrfs_set_lock_blocking(left);
3187
3188 free_space = btrfs_leaf_free_space(root, left);
3189 if (free_space < data_size) {
3190 ret = 1;
3191 goto out;
3192 }
3193
3194 /* cow and double check */
3195 ret = btrfs_cow_block(trans, root, left,
3196 path->nodes[1], slot - 1, &left);
3197 if (ret) {
3198 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003199 if (ret == -ENOSPC)
3200 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003201 goto out;
3202 }
3203
3204 free_space = btrfs_leaf_free_space(root, left);
3205 if (free_space < data_size) {
3206 ret = 1;
3207 goto out;
3208 }
3209
Chris Mason99d8f832010-07-07 10:51:48 -04003210 return __push_leaf_left(trans, root, path, min_data_size,
3211 empty, left, free_space, right_nritems,
3212 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003213out:
3214 btrfs_tree_unlock(left);
3215 free_extent_buffer(left);
3216 return ret;
3217}
3218
3219/*
Chris Mason74123bd2007-02-02 11:05:29 -05003220 * split the path's leaf in two, making sure there is at least data_size
3221 * available for the resulting leaf level of the path.
3222 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003223static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3224 struct btrfs_root *root,
3225 struct btrfs_path *path,
3226 struct extent_buffer *l,
3227 struct extent_buffer *right,
3228 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003229{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003230 int data_copy_size;
3231 int rt_data_off;
3232 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003233 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003234 struct btrfs_map_token token;
3235
3236 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003237
Chris Mason5f39d392007-10-15 16:14:19 -04003238 nritems = nritems - mid;
3239 btrfs_set_header_nritems(right, nritems);
3240 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3241
3242 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3243 btrfs_item_nr_offset(mid),
3244 nritems * sizeof(struct btrfs_item));
3245
3246 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003247 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3248 data_copy_size, btrfs_leaf_data(l) +
3249 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003250
Chris Mason5f39d392007-10-15 16:14:19 -04003251 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3252 btrfs_item_end_nr(l, mid);
3253
3254 for (i = 0; i < nritems; i++) {
3255 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003256 u32 ioff;
3257
Chris Masoncfed81a2012-03-03 07:40:03 -05003258 ioff = btrfs_token_item_offset(right, item, &token);
3259 btrfs_set_token_item_offset(right, item,
3260 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003261 }
Chris Mason74123bd2007-02-02 11:05:29 -05003262
Chris Mason5f39d392007-10-15 16:14:19 -04003263 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003264 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003265 insert_ptr(trans, root, path, &disk_key, right->start,
3266 path->slots[1] + 1, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003267
3268 btrfs_mark_buffer_dirty(right);
3269 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003270 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003271
Chris Masonbe0e5c02007-01-26 15:51:26 -05003272 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003273 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003274 free_extent_buffer(path->nodes[0]);
3275 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003276 path->slots[0] -= mid;
3277 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003278 } else {
3279 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003280 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003281 }
Chris Mason5f39d392007-10-15 16:14:19 -04003282
Chris Masoneb60cea2007-02-02 09:18:22 -05003283 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003284}
3285
3286/*
Chris Mason99d8f832010-07-07 10:51:48 -04003287 * double splits happen when we need to insert a big item in the middle
3288 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3289 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3290 * A B C
3291 *
3292 * We avoid this by trying to push the items on either side of our target
3293 * into the adjacent leaves. If all goes well we can avoid the double split
3294 * completely.
3295 */
3296static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3297 struct btrfs_root *root,
3298 struct btrfs_path *path,
3299 int data_size)
3300{
3301 int ret;
3302 int progress = 0;
3303 int slot;
3304 u32 nritems;
3305
3306 slot = path->slots[0];
3307
3308 /*
3309 * try to push all the items after our slot into the
3310 * right leaf
3311 */
3312 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3313 if (ret < 0)
3314 return ret;
3315
3316 if (ret == 0)
3317 progress++;
3318
3319 nritems = btrfs_header_nritems(path->nodes[0]);
3320 /*
3321 * our goal is to get our slot at the start or end of a leaf. If
3322 * we've done so we're done
3323 */
3324 if (path->slots[0] == 0 || path->slots[0] == nritems)
3325 return 0;
3326
3327 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3328 return 0;
3329
3330 /* try to push all the items before our slot into the next leaf */
3331 slot = path->slots[0];
3332 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3333 if (ret < 0)
3334 return ret;
3335
3336 if (ret == 0)
3337 progress++;
3338
3339 if (progress)
3340 return 0;
3341 return 1;
3342}
3343
3344/*
Chris Mason44871b12009-03-13 10:04:31 -04003345 * split the path's leaf in two, making sure there is at least data_size
3346 * available for the resulting leaf level of the path.
3347 *
3348 * returns 0 if all went well and < 0 on failure.
3349 */
3350static noinline int split_leaf(struct btrfs_trans_handle *trans,
3351 struct btrfs_root *root,
3352 struct btrfs_key *ins_key,
3353 struct btrfs_path *path, int data_size,
3354 int extend)
3355{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003356 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003357 struct extent_buffer *l;
3358 u32 nritems;
3359 int mid;
3360 int slot;
3361 struct extent_buffer *right;
3362 int ret = 0;
3363 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003364 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003365 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003366 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003367
Yan, Zhenga5719522009-09-24 09:17:31 -04003368 l = path->nodes[0];
3369 slot = path->slots[0];
3370 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3371 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3372 return -EOVERFLOW;
3373
Chris Mason44871b12009-03-13 10:04:31 -04003374 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003375 if (data_size) {
3376 wret = push_leaf_right(trans, root, path, data_size,
3377 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003378 if (wret < 0)
3379 return wret;
3380 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003381 wret = push_leaf_left(trans, root, path, data_size,
3382 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003383 if (wret < 0)
3384 return wret;
3385 }
3386 l = path->nodes[0];
3387
3388 /* did the pushes work? */
3389 if (btrfs_leaf_free_space(root, l) >= data_size)
3390 return 0;
3391 }
3392
3393 if (!path->nodes[1]) {
3394 ret = insert_new_root(trans, root, path, 1);
3395 if (ret)
3396 return ret;
3397 }
3398again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003399 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003400 l = path->nodes[0];
3401 slot = path->slots[0];
3402 nritems = btrfs_header_nritems(l);
3403 mid = (nritems + 1) / 2;
3404
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003405 if (mid <= slot) {
3406 if (nritems == 1 ||
3407 leaf_space_used(l, mid, nritems - mid) + data_size >
3408 BTRFS_LEAF_DATA_SIZE(root)) {
3409 if (slot >= nritems) {
3410 split = 0;
3411 } else {
3412 mid = slot;
3413 if (mid != nritems &&
3414 leaf_space_used(l, mid, nritems - mid) +
3415 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003416 if (data_size && !tried_avoid_double)
3417 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003418 split = 2;
3419 }
3420 }
3421 }
3422 } else {
3423 if (leaf_space_used(l, 0, mid) + data_size >
3424 BTRFS_LEAF_DATA_SIZE(root)) {
3425 if (!extend && data_size && slot == 0) {
3426 split = 0;
3427 } else if ((extend || !data_size) && slot == 0) {
3428 mid = 1;
3429 } else {
3430 mid = slot;
3431 if (mid != nritems &&
3432 leaf_space_used(l, mid, nritems - mid) +
3433 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003434 if (data_size && !tried_avoid_double)
3435 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003436 split = 2 ;
3437 }
3438 }
3439 }
3440 }
3441
3442 if (split == 0)
3443 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3444 else
3445 btrfs_item_key(l, &disk_key, mid);
3446
3447 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003448 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003449 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003450 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003451 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003452
3453 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003454
3455 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3456 btrfs_set_header_bytenr(right, right->start);
3457 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003458 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003459 btrfs_set_header_owner(right, root->root_key.objectid);
3460 btrfs_set_header_level(right, 0);
3461 write_extent_buffer(right, root->fs_info->fsid,
3462 (unsigned long)btrfs_header_fsid(right),
3463 BTRFS_FSID_SIZE);
3464
3465 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3466 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3467 BTRFS_UUID_SIZE);
3468
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003469 if (split == 0) {
3470 if (mid <= slot) {
3471 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003472 insert_ptr(trans, root, path, &disk_key, right->start,
3473 path->slots[1] + 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003474 btrfs_tree_unlock(path->nodes[0]);
3475 free_extent_buffer(path->nodes[0]);
3476 path->nodes[0] = right;
3477 path->slots[0] = 0;
3478 path->slots[1] += 1;
3479 } else {
3480 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003481 insert_ptr(trans, root, path, &disk_key, right->start,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003482 path->slots[1], 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003483 btrfs_tree_unlock(path->nodes[0]);
3484 free_extent_buffer(path->nodes[0]);
3485 path->nodes[0] = right;
3486 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003487 if (path->slots[1] == 0)
3488 fixup_low_keys(trans, root, path,
3489 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003490 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003491 btrfs_mark_buffer_dirty(right);
3492 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003493 }
3494
Jeff Mahoney143bede2012-03-01 14:56:26 +01003495 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003496
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003497 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003498 BUG_ON(num_doubles != 0);
3499 num_doubles++;
3500 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003501 }
Chris Mason44871b12009-03-13 10:04:31 -04003502
Jeff Mahoney143bede2012-03-01 14:56:26 +01003503 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003504
3505push_for_double:
3506 push_for_double_split(trans, root, path, data_size);
3507 tried_avoid_double = 1;
3508 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3509 return 0;
3510 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003511}
3512
Yan, Zhengad48fd752009-11-12 09:33:58 +00003513static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3514 struct btrfs_root *root,
3515 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003516{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003517 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003518 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003519 struct btrfs_file_extent_item *fi;
3520 u64 extent_len = 0;
3521 u32 item_size;
3522 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003523
3524 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003525 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3526
3527 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3528 key.type != BTRFS_EXTENT_CSUM_KEY);
3529
3530 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3531 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003532
3533 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003534 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3535 fi = btrfs_item_ptr(leaf, path->slots[0],
3536 struct btrfs_file_extent_item);
3537 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3538 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003539 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003540
Chris Mason459931e2008-12-10 09:10:46 -05003541 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003542 path->search_for_split = 1;
3543 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003544 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003545 if (ret < 0)
3546 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003547
Yan, Zhengad48fd752009-11-12 09:33:58 +00003548 ret = -EAGAIN;
3549 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003550 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003551 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3552 goto err;
3553
Chris Mason109f6ae2010-04-02 09:20:18 -04003554 /* the leaf has changed, it now has room. return now */
3555 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3556 goto err;
3557
Yan, Zhengad48fd752009-11-12 09:33:58 +00003558 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3559 fi = btrfs_item_ptr(leaf, path->slots[0],
3560 struct btrfs_file_extent_item);
3561 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3562 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003563 }
3564
Chris Masonb9473432009-03-13 11:00:37 -04003565 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003566 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003567 if (ret)
3568 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003569
Yan, Zhengad48fd752009-11-12 09:33:58 +00003570 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003571 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003572 return 0;
3573err:
3574 path->keep_locks = 0;
3575 return ret;
3576}
3577
3578static noinline int split_item(struct btrfs_trans_handle *trans,
3579 struct btrfs_root *root,
3580 struct btrfs_path *path,
3581 struct btrfs_key *new_key,
3582 unsigned long split_offset)
3583{
3584 struct extent_buffer *leaf;
3585 struct btrfs_item *item;
3586 struct btrfs_item *new_item;
3587 int slot;
3588 char *buf;
3589 u32 nritems;
3590 u32 item_size;
3591 u32 orig_offset;
3592 struct btrfs_disk_key disk_key;
3593
Chris Masonb9473432009-03-13 11:00:37 -04003594 leaf = path->nodes[0];
3595 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3596
Chris Masonb4ce94d2009-02-04 09:25:08 -05003597 btrfs_set_path_blocking(path);
3598
Chris Mason459931e2008-12-10 09:10:46 -05003599 item = btrfs_item_nr(leaf, path->slots[0]);
3600 orig_offset = btrfs_item_offset(leaf, item);
3601 item_size = btrfs_item_size(leaf, item);
3602
Chris Mason459931e2008-12-10 09:10:46 -05003603 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003604 if (!buf)
3605 return -ENOMEM;
3606
Chris Mason459931e2008-12-10 09:10:46 -05003607 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3608 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003609
Chris Mason459931e2008-12-10 09:10:46 -05003610 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003611 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003612 if (slot != nritems) {
3613 /* shift the items */
3614 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003615 btrfs_item_nr_offset(slot),
3616 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003617 }
3618
3619 btrfs_cpu_key_to_disk(&disk_key, new_key);
3620 btrfs_set_item_key(leaf, &disk_key, slot);
3621
3622 new_item = btrfs_item_nr(leaf, slot);
3623
3624 btrfs_set_item_offset(leaf, new_item, orig_offset);
3625 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3626
3627 btrfs_set_item_offset(leaf, item,
3628 orig_offset + item_size - split_offset);
3629 btrfs_set_item_size(leaf, item, split_offset);
3630
3631 btrfs_set_header_nritems(leaf, nritems + 1);
3632
3633 /* write the data for the start of the original item */
3634 write_extent_buffer(leaf, buf,
3635 btrfs_item_ptr_offset(leaf, path->slots[0]),
3636 split_offset);
3637
3638 /* write the data for the new item */
3639 write_extent_buffer(leaf, buf + split_offset,
3640 btrfs_item_ptr_offset(leaf, slot),
3641 item_size - split_offset);
3642 btrfs_mark_buffer_dirty(leaf);
3643
Yan, Zhengad48fd752009-11-12 09:33:58 +00003644 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003645 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003646 return 0;
3647}
3648
3649/*
3650 * This function splits a single item into two items,
3651 * giving 'new_key' to the new item and splitting the
3652 * old one at split_offset (from the start of the item).
3653 *
3654 * The path may be released by this operation. After
3655 * the split, the path is pointing to the old item. The
3656 * new item is going to be in the same node as the old one.
3657 *
3658 * Note, the item being split must be smaller enough to live alone on
3659 * a tree block with room for one extra struct btrfs_item
3660 *
3661 * This allows us to split the item in place, keeping a lock on the
3662 * leaf the entire time.
3663 */
3664int btrfs_split_item(struct btrfs_trans_handle *trans,
3665 struct btrfs_root *root,
3666 struct btrfs_path *path,
3667 struct btrfs_key *new_key,
3668 unsigned long split_offset)
3669{
3670 int ret;
3671 ret = setup_leaf_for_split(trans, root, path,
3672 sizeof(struct btrfs_item));
3673 if (ret)
3674 return ret;
3675
3676 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05003677 return ret;
3678}
3679
3680/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00003681 * This function duplicate a item, giving 'new_key' to the new item.
3682 * It guarantees both items live in the same tree leaf and the new item
3683 * is contiguous with the original item.
3684 *
3685 * This allows us to split file extent in place, keeping a lock on the
3686 * leaf the entire time.
3687 */
3688int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
3689 struct btrfs_root *root,
3690 struct btrfs_path *path,
3691 struct btrfs_key *new_key)
3692{
3693 struct extent_buffer *leaf;
3694 int ret;
3695 u32 item_size;
3696
3697 leaf = path->nodes[0];
3698 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3699 ret = setup_leaf_for_split(trans, root, path,
3700 item_size + sizeof(struct btrfs_item));
3701 if (ret)
3702 return ret;
3703
3704 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003705 setup_items_for_insert(trans, root, path, new_key, &item_size,
3706 item_size, item_size +
3707 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003708 leaf = path->nodes[0];
3709 memcpy_extent_buffer(leaf,
3710 btrfs_item_ptr_offset(leaf, path->slots[0]),
3711 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
3712 item_size);
3713 return 0;
3714}
3715
3716/*
Chris Masond352ac62008-09-29 15:18:18 -04003717 * make the item pointed to by the path smaller. new_size indicates
3718 * how small to make it, and from_end tells us if we just chop bytes
3719 * off the end of the item or if we shift the item to chop bytes off
3720 * the front.
3721 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003722void btrfs_truncate_item(struct btrfs_trans_handle *trans,
3723 struct btrfs_root *root,
3724 struct btrfs_path *path,
3725 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04003726{
Chris Masonb18c6682007-04-17 13:26:50 -04003727 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003728 struct extent_buffer *leaf;
3729 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04003730 u32 nritems;
3731 unsigned int data_end;
3732 unsigned int old_data_start;
3733 unsigned int old_size;
3734 unsigned int size_diff;
3735 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003736 struct btrfs_map_token token;
3737
3738 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04003739
Chris Mason5f39d392007-10-15 16:14:19 -04003740 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04003741 slot = path->slots[0];
3742
3743 old_size = btrfs_item_size_nr(leaf, slot);
3744 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01003745 return;
Chris Masonb18c6682007-04-17 13:26:50 -04003746
Chris Mason5f39d392007-10-15 16:14:19 -04003747 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003748 data_end = leaf_data_end(root, leaf);
3749
Chris Mason5f39d392007-10-15 16:14:19 -04003750 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04003751
Chris Masonb18c6682007-04-17 13:26:50 -04003752 size_diff = old_size - new_size;
3753
3754 BUG_ON(slot < 0);
3755 BUG_ON(slot >= nritems);
3756
3757 /*
3758 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3759 */
3760 /* first correct the data pointers */
3761 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003762 u32 ioff;
3763 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003764
Chris Masoncfed81a2012-03-03 07:40:03 -05003765 ioff = btrfs_token_item_offset(leaf, item, &token);
3766 btrfs_set_token_item_offset(leaf, item,
3767 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04003768 }
Chris Masondb945352007-10-15 16:15:53 -04003769
Chris Masonb18c6682007-04-17 13:26:50 -04003770 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04003771 if (from_end) {
3772 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3773 data_end + size_diff, btrfs_leaf_data(leaf) +
3774 data_end, old_data_start + new_size - data_end);
3775 } else {
3776 struct btrfs_disk_key disk_key;
3777 u64 offset;
3778
3779 btrfs_item_key(leaf, &disk_key, slot);
3780
3781 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
3782 unsigned long ptr;
3783 struct btrfs_file_extent_item *fi;
3784
3785 fi = btrfs_item_ptr(leaf, slot,
3786 struct btrfs_file_extent_item);
3787 fi = (struct btrfs_file_extent_item *)(
3788 (unsigned long)fi - size_diff);
3789
3790 if (btrfs_file_extent_type(leaf, fi) ==
3791 BTRFS_FILE_EXTENT_INLINE) {
3792 ptr = btrfs_item_ptr_offset(leaf, slot);
3793 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05003794 (unsigned long)fi,
3795 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04003796 disk_bytenr));
3797 }
3798 }
3799
3800 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3801 data_end + size_diff, btrfs_leaf_data(leaf) +
3802 data_end, old_data_start - data_end);
3803
3804 offset = btrfs_disk_key_offset(&disk_key);
3805 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
3806 btrfs_set_item_key(leaf, &disk_key, slot);
3807 if (slot == 0)
3808 fixup_low_keys(trans, root, path, &disk_key, 1);
3809 }
Chris Mason5f39d392007-10-15 16:14:19 -04003810
3811 item = btrfs_item_nr(leaf, slot);
3812 btrfs_set_item_size(leaf, item, new_size);
3813 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003814
Chris Mason5f39d392007-10-15 16:14:19 -04003815 if (btrfs_leaf_free_space(root, leaf) < 0) {
3816 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04003817 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003818 }
Chris Masonb18c6682007-04-17 13:26:50 -04003819}
3820
Chris Masond352ac62008-09-29 15:18:18 -04003821/*
3822 * make the item pointed to by the path bigger, data_size is the new size.
3823 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003824void btrfs_extend_item(struct btrfs_trans_handle *trans,
3825 struct btrfs_root *root, struct btrfs_path *path,
3826 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04003827{
Chris Mason6567e832007-04-16 09:22:45 -04003828 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04003829 struct extent_buffer *leaf;
3830 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04003831 u32 nritems;
3832 unsigned int data_end;
3833 unsigned int old_data;
3834 unsigned int old_size;
3835 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05003836 struct btrfs_map_token token;
3837
3838 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04003839
Chris Mason5f39d392007-10-15 16:14:19 -04003840 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04003841
Chris Mason5f39d392007-10-15 16:14:19 -04003842 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003843 data_end = leaf_data_end(root, leaf);
3844
Chris Mason5f39d392007-10-15 16:14:19 -04003845 if (btrfs_leaf_free_space(root, leaf) < data_size) {
3846 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003847 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003848 }
Chris Mason6567e832007-04-16 09:22:45 -04003849 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04003850 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04003851
3852 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04003853 if (slot >= nritems) {
3854 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003855 printk(KERN_CRIT "slot %d too large, nritems %d\n",
3856 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04003857 BUG_ON(1);
3858 }
Chris Mason6567e832007-04-16 09:22:45 -04003859
3860 /*
3861 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3862 */
3863 /* first correct the data pointers */
3864 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003865 u32 ioff;
3866 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04003867
Chris Masoncfed81a2012-03-03 07:40:03 -05003868 ioff = btrfs_token_item_offset(leaf, item, &token);
3869 btrfs_set_token_item_offset(leaf, item,
3870 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04003871 }
Chris Mason5f39d392007-10-15 16:14:19 -04003872
Chris Mason6567e832007-04-16 09:22:45 -04003873 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04003874 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04003875 data_end - data_size, btrfs_leaf_data(leaf) +
3876 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04003877
Chris Mason6567e832007-04-16 09:22:45 -04003878 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04003879 old_size = btrfs_item_size_nr(leaf, slot);
3880 item = btrfs_item_nr(leaf, slot);
3881 btrfs_set_item_size(leaf, item, old_size + data_size);
3882 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003883
Chris Mason5f39d392007-10-15 16:14:19 -04003884 if (btrfs_leaf_free_space(root, leaf) < 0) {
3885 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04003886 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04003887 }
Chris Mason6567e832007-04-16 09:22:45 -04003888}
3889
Chris Mason74123bd2007-02-02 11:05:29 -05003890/*
Chris Masond352ac62008-09-29 15:18:18 -04003891 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05003892 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05003893 * Returns the number of keys that were inserted.
3894 */
3895int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
3896 struct btrfs_root *root,
3897 struct btrfs_path *path,
3898 struct btrfs_key *cpu_key, u32 *data_size,
3899 int nr)
3900{
3901 struct extent_buffer *leaf;
3902 struct btrfs_item *item;
3903 int ret = 0;
3904 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003905 int i;
3906 u32 nritems;
3907 u32 total_data = 0;
3908 u32 total_size = 0;
3909 unsigned int data_end;
3910 struct btrfs_disk_key disk_key;
3911 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003912 struct btrfs_map_token token;
3913
3914 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003915
Yan Zheng87b29b22008-12-17 10:21:48 -05003916 for (i = 0; i < nr; i++) {
3917 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
3918 BTRFS_LEAF_DATA_SIZE(root)) {
3919 break;
3920 nr = i;
3921 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003922 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05003923 total_size += data_size[i] + sizeof(struct btrfs_item);
3924 }
3925 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003926
Josef Bacikf3465ca2008-11-12 14:19:50 -05003927 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
3928 if (ret == 0)
3929 return -EEXIST;
3930 if (ret < 0)
3931 goto out;
3932
Josef Bacikf3465ca2008-11-12 14:19:50 -05003933 leaf = path->nodes[0];
3934
3935 nritems = btrfs_header_nritems(leaf);
3936 data_end = leaf_data_end(root, leaf);
3937
3938 if (btrfs_leaf_free_space(root, leaf) < total_size) {
3939 for (i = nr; i >= 0; i--) {
3940 total_data -= data_size[i];
3941 total_size -= data_size[i] + sizeof(struct btrfs_item);
3942 if (total_size < btrfs_leaf_free_space(root, leaf))
3943 break;
3944 }
3945 nr = i;
3946 }
3947
3948 slot = path->slots[0];
3949 BUG_ON(slot < 0);
3950
3951 if (slot != nritems) {
3952 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
3953
3954 item = btrfs_item_nr(leaf, slot);
3955 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3956
3957 /* figure out how many keys we can insert in here */
3958 total_data = data_size[0];
3959 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003960 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05003961 break;
3962 total_data += data_size[i];
3963 }
3964 nr = i;
3965
3966 if (old_data < data_end) {
3967 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05003968 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05003969 slot, old_data, data_end);
3970 BUG_ON(1);
3971 }
3972 /*
3973 * item0..itemN ... dataN.offset..dataN.size .. data0.size
3974 */
3975 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05003976 for (i = slot; i < nritems; i++) {
3977 u32 ioff;
3978
3979 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003980 ioff = btrfs_token_item_offset(leaf, item, &token);
3981 btrfs_set_token_item_offset(leaf, item,
3982 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05003983 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05003984 /* shift the items */
3985 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
3986 btrfs_item_nr_offset(slot),
3987 (nritems - slot) * sizeof(struct btrfs_item));
3988
3989 /* shift the data */
3990 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
3991 data_end - total_data, btrfs_leaf_data(leaf) +
3992 data_end, old_data - data_end);
3993 data_end = old_data;
3994 } else {
3995 /*
3996 * this sucks but it has to be done, if we are inserting at
3997 * the end of the leaf only insert 1 of the items, since we
3998 * have no way of knowing whats on the next leaf and we'd have
3999 * to drop our current locks to figure it out
4000 */
4001 nr = 1;
4002 }
4003
4004 /* setup the item for the new data */
4005 for (i = 0; i < nr; i++) {
4006 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4007 btrfs_set_item_key(leaf, &disk_key, slot + i);
4008 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004009 btrfs_set_token_item_offset(leaf, item,
4010 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004011 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004012 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004013 }
4014 btrfs_set_header_nritems(leaf, nritems + nr);
4015 btrfs_mark_buffer_dirty(leaf);
4016
4017 ret = 0;
4018 if (slot == 0) {
4019 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004020 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004021 }
4022
4023 if (btrfs_leaf_free_space(root, leaf) < 0) {
4024 btrfs_print_leaf(root, leaf);
4025 BUG();
4026 }
4027out:
4028 if (!ret)
4029 ret = nr;
4030 return ret;
4031}
4032
4033/*
Chris Mason44871b12009-03-13 10:04:31 -04004034 * this is a helper for btrfs_insert_empty_items, the main goal here is
4035 * to save stack depth by doing the bulk of the work in a function
4036 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004037 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004038void setup_items_for_insert(struct btrfs_trans_handle *trans,
4039 struct btrfs_root *root, struct btrfs_path *path,
4040 struct btrfs_key *cpu_key, u32 *data_size,
4041 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004042{
Chris Mason5f39d392007-10-15 16:14:19 -04004043 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004044 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004045 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004046 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004047 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004048 struct extent_buffer *leaf;
4049 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004050 struct btrfs_map_token token;
4051
4052 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004053
Chris Mason5f39d392007-10-15 16:14:19 -04004054 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004055 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004056
Chris Mason5f39d392007-10-15 16:14:19 -04004057 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004058 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004059
Chris Masonf25956c2008-09-12 15:32:53 -04004060 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004061 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004062 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004063 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004064 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004065 }
Chris Mason5f39d392007-10-15 16:14:19 -04004066
Chris Masonbe0e5c02007-01-26 15:51:26 -05004067 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004068 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004069
Chris Mason5f39d392007-10-15 16:14:19 -04004070 if (old_data < data_end) {
4071 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004072 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004073 slot, old_data, data_end);
4074 BUG_ON(1);
4075 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004076 /*
4077 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4078 */
4079 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004080 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004081 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004082
Chris Mason5f39d392007-10-15 16:14:19 -04004083 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004084 ioff = btrfs_token_item_offset(leaf, item, &token);
4085 btrfs_set_token_item_offset(leaf, item,
4086 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004087 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004088 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004089 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004090 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004091 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004092
4093 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004094 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004095 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004096 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004097 data_end = old_data;
4098 }
Chris Mason5f39d392007-10-15 16:14:19 -04004099
Chris Mason62e27492007-03-15 12:56:47 -04004100 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004101 for (i = 0; i < nr; i++) {
4102 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4103 btrfs_set_item_key(leaf, &disk_key, slot + i);
4104 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004105 btrfs_set_token_item_offset(leaf, item,
4106 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004107 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004108 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004109 }
Chris Mason44871b12009-03-13 10:04:31 -04004110
Chris Mason9c583092008-01-29 15:15:18 -05004111 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004112
Chris Mason5a01a2e2008-01-30 11:43:54 -05004113 if (slot == 0) {
4114 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004115 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004116 }
Chris Masonb9473432009-03-13 11:00:37 -04004117 btrfs_unlock_up_safe(path, 1);
4118 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004119
Chris Mason5f39d392007-10-15 16:14:19 -04004120 if (btrfs_leaf_free_space(root, leaf) < 0) {
4121 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004122 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004123 }
Chris Mason44871b12009-03-13 10:04:31 -04004124}
4125
4126/*
4127 * Given a key and some data, insert items into the tree.
4128 * This does all the path init required, making room in the tree if needed.
4129 */
4130int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4131 struct btrfs_root *root,
4132 struct btrfs_path *path,
4133 struct btrfs_key *cpu_key, u32 *data_size,
4134 int nr)
4135{
Chris Mason44871b12009-03-13 10:04:31 -04004136 int ret = 0;
4137 int slot;
4138 int i;
4139 u32 total_size = 0;
4140 u32 total_data = 0;
4141
4142 for (i = 0; i < nr; i++)
4143 total_data += data_size[i];
4144
4145 total_size = total_data + (nr * sizeof(struct btrfs_item));
4146 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4147 if (ret == 0)
4148 return -EEXIST;
4149 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004150 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004151
Chris Mason44871b12009-03-13 10:04:31 -04004152 slot = path->slots[0];
4153 BUG_ON(slot < 0);
4154
Jeff Mahoney143bede2012-03-01 14:56:26 +01004155 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004156 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004157 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004158}
4159
4160/*
4161 * Given a key and some data, insert an item into the tree.
4162 * This does all the path init required, making room in the tree if needed.
4163 */
Chris Masone089f052007-03-16 16:20:31 -04004164int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4165 *root, struct btrfs_key *cpu_key, void *data, u32
4166 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004167{
4168 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004169 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004170 struct extent_buffer *leaf;
4171 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004172
Chris Mason2c90e5d2007-04-02 10:50:19 -04004173 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004174 if (!path)
4175 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004176 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004177 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004178 leaf = path->nodes[0];
4179 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4180 write_extent_buffer(leaf, data, ptr, data_size);
4181 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004182 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004183 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004184 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004185}
4186
Chris Mason74123bd2007-02-02 11:05:29 -05004187/*
Chris Mason5de08d72007-02-24 06:24:44 -05004188 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004189 *
Chris Masond352ac62008-09-29 15:18:18 -04004190 * the tree should have been previously balanced so the deletion does not
4191 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004192 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004193static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4194 struct btrfs_path *path, int level, int slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004195{
Chris Mason5f39d392007-10-15 16:14:19 -04004196 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004197 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004198
Chris Mason5f39d392007-10-15 16:14:19 -04004199 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004200 if (slot != nritems - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04004201 memmove_extent_buffer(parent,
4202 btrfs_node_key_ptr_offset(slot),
4203 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004204 sizeof(struct btrfs_key_ptr) *
4205 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05004206 }
Chris Mason7518a232007-03-12 12:01:18 -04004207 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004208 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004209 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004210 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004211 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004212 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004213 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004214 struct btrfs_disk_key disk_key;
4215
4216 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004217 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004218 }
Chris Masond6025572007-03-30 14:27:56 -04004219 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004220}
4221
Chris Mason74123bd2007-02-02 11:05:29 -05004222/*
Chris Mason323ac952008-10-01 19:05:46 -04004223 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004224 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004225 *
4226 * This deletes the pointer in path->nodes[1] and frees the leaf
4227 * block extent. zero is returned if it all worked out, < 0 otherwise.
4228 *
4229 * The path must have already been setup for deleting the leaf, including
4230 * all the proper balancing. path->nodes[1] must be locked.
4231 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004232static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4233 struct btrfs_root *root,
4234 struct btrfs_path *path,
4235 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004236{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004237 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004238 del_ptr(trans, root, path, 1, path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04004239
Chris Mason4d081c42009-02-04 09:31:28 -05004240 /*
4241 * btrfs_free_extent is expensive, we want to make sure we
4242 * aren't holding any locks when we call it
4243 */
4244 btrfs_unlock_up_safe(path, 0);
4245
Yan, Zhengf0486c62010-05-16 10:46:25 -04004246 root_sub_used(root, leaf->len);
4247
Josef Bacik3083ee22012-03-09 16:01:49 -05004248 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004249 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004250 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004251}
4252/*
Chris Mason74123bd2007-02-02 11:05:29 -05004253 * delete the item at the leaf level in path. If that empties
4254 * the leaf, remove it from the tree
4255 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004256int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4257 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004258{
Chris Mason5f39d392007-10-15 16:14:19 -04004259 struct extent_buffer *leaf;
4260 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004261 int last_off;
4262 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004263 int ret = 0;
4264 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004265 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004266 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004267 struct btrfs_map_token token;
4268
4269 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004270
Chris Mason5f39d392007-10-15 16:14:19 -04004271 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004272 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4273
4274 for (i = 0; i < nr; i++)
4275 dsize += btrfs_item_size_nr(leaf, slot + i);
4276
Chris Mason5f39d392007-10-15 16:14:19 -04004277 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004278
Chris Mason85e21ba2008-01-29 15:11:36 -05004279 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004280 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004281
4282 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004283 data_end + dsize,
4284 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004285 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004286
Chris Mason85e21ba2008-01-29 15:11:36 -05004287 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004288 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004289
Chris Mason5f39d392007-10-15 16:14:19 -04004290 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004291 ioff = btrfs_token_item_offset(leaf, item, &token);
4292 btrfs_set_token_item_offset(leaf, item,
4293 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004294 }
Chris Masondb945352007-10-15 16:15:53 -04004295
Chris Mason5f39d392007-10-15 16:14:19 -04004296 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004297 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004298 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004299 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004300 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004301 btrfs_set_header_nritems(leaf, nritems - nr);
4302 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004303
Chris Mason74123bd2007-02-02 11:05:29 -05004304 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004305 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004306 if (leaf == root->node) {
4307 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004308 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004309 btrfs_set_path_blocking(path);
4310 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004311 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004312 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004313 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004314 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004315 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004316 struct btrfs_disk_key disk_key;
4317
4318 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004319 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004320 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004321
Chris Mason74123bd2007-02-02 11:05:29 -05004322 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004323 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004324 /* push_leaf_left fixes the path.
4325 * make sure the path still points to our leaf
4326 * for possible call to del_ptr below
4327 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004328 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004329 extent_buffer_get(leaf);
4330
Chris Masonb9473432009-03-13 11:00:37 -04004331 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004332 wret = push_leaf_left(trans, root, path, 1, 1,
4333 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004334 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004335 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004336
4337 if (path->nodes[0] == leaf &&
4338 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004339 wret = push_leaf_right(trans, root, path, 1,
4340 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004341 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004342 ret = wret;
4343 }
Chris Mason5f39d392007-10-15 16:14:19 -04004344
4345 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004346 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004347 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004348 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004349 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004350 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004351 /* if we're still in the path, make sure
4352 * we're dirty. Otherwise, one of the
4353 * push_leaf functions must have already
4354 * dirtied this buffer
4355 */
4356 if (path->nodes[0] == leaf)
4357 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004358 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004359 }
Chris Masond5719762007-03-23 10:01:08 -04004360 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004361 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004362 }
4363 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004364 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004365}
4366
Chris Mason97571fd2007-02-24 13:39:08 -05004367/*
Chris Mason925baed2008-06-25 16:01:30 -04004368 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004369 * returns 0 if it found something or 1 if there are no lesser leaves.
4370 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004371 *
4372 * This may release the path, and so you may lose any locks held at the
4373 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004374 */
4375int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4376{
Chris Mason925baed2008-06-25 16:01:30 -04004377 struct btrfs_key key;
4378 struct btrfs_disk_key found_key;
4379 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004380
Chris Mason925baed2008-06-25 16:01:30 -04004381 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004382
Chris Mason925baed2008-06-25 16:01:30 -04004383 if (key.offset > 0)
4384 key.offset--;
4385 else if (key.type > 0)
4386 key.type--;
4387 else if (key.objectid > 0)
4388 key.objectid--;
4389 else
4390 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004391
David Sterbab3b4aa72011-04-21 01:20:15 +02004392 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004393 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4394 if (ret < 0)
4395 return ret;
4396 btrfs_item_key(path->nodes[0], &found_key, 0);
4397 ret = comp_keys(&found_key, &key);
4398 if (ret < 0)
4399 return 0;
4400 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004401}
4402
Chris Mason3f157a22008-06-25 16:01:31 -04004403/*
4404 * A helper function to walk down the tree starting at min_key, and looking
4405 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004406 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004407 *
4408 * This does not cow, but it does stuff the starting key it finds back
4409 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4410 * key and get a writable path.
4411 *
4412 * This does lock as it descends, and path->keep_locks should be set
4413 * to 1 by the caller.
4414 *
4415 * This honors path->lowest_level to prevent descent past a given level
4416 * of the tree.
4417 *
Chris Masond352ac62008-09-29 15:18:18 -04004418 * min_trans indicates the oldest transaction that you are interested
4419 * in walking through. Any nodes or leaves older than min_trans are
4420 * skipped over (without reading them).
4421 *
Chris Mason3f157a22008-06-25 16:01:31 -04004422 * returns zero if something useful was found, < 0 on error and 1 if there
4423 * was nothing in the tree that matched the search criteria.
4424 */
4425int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004426 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004427 struct btrfs_path *path, int cache_only,
4428 u64 min_trans)
4429{
4430 struct extent_buffer *cur;
4431 struct btrfs_key found_key;
4432 int slot;
Yan96524802008-07-24 12:19:49 -04004433 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004434 u32 nritems;
4435 int level;
4436 int ret = 1;
4437
Chris Mason934d3752008-12-08 16:43:10 -05004438 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004439again:
Chris Masonbd681512011-07-16 15:23:14 -04004440 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004441 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004442 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004443 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004444 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004445
4446 if (btrfs_header_generation(cur) < min_trans) {
4447 ret = 1;
4448 goto out;
4449 }
Chris Masond3977122009-01-05 21:25:51 -05004450 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004451 nritems = btrfs_header_nritems(cur);
4452 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004453 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004454
Chris Mason323ac952008-10-01 19:05:46 -04004455 /* at the lowest level, we're done, setup the path and exit */
4456 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004457 if (slot >= nritems)
4458 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004459 ret = 0;
4460 path->slots[level] = slot;
4461 btrfs_item_key_to_cpu(cur, &found_key, slot);
4462 goto out;
4463 }
Yan96524802008-07-24 12:19:49 -04004464 if (sret && slot > 0)
4465 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004466 /*
4467 * check this node pointer against the cache_only and
4468 * min_trans parameters. If it isn't in cache or is too
4469 * old, skip to the next one.
4470 */
Chris Masond3977122009-01-05 21:25:51 -05004471 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004472 u64 blockptr;
4473 u64 gen;
4474 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004475 struct btrfs_disk_key disk_key;
4476
Chris Mason3f157a22008-06-25 16:01:31 -04004477 blockptr = btrfs_node_blockptr(cur, slot);
4478 gen = btrfs_node_ptr_generation(cur, slot);
4479 if (gen < min_trans) {
4480 slot++;
4481 continue;
4482 }
4483 if (!cache_only)
4484 break;
4485
Chris Masone02119d2008-09-05 16:13:11 -04004486 if (max_key) {
4487 btrfs_node_key(cur, &disk_key, slot);
4488 if (comp_keys(&disk_key, max_key) >= 0) {
4489 ret = 1;
4490 goto out;
4491 }
4492 }
4493
Chris Mason3f157a22008-06-25 16:01:31 -04004494 tmp = btrfs_find_tree_block(root, blockptr,
4495 btrfs_level_size(root, level - 1));
4496
Chris Masonb9fab912012-05-06 07:23:47 -04004497 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004498 free_extent_buffer(tmp);
4499 break;
4500 }
4501 if (tmp)
4502 free_extent_buffer(tmp);
4503 slot++;
4504 }
Chris Masone02119d2008-09-05 16:13:11 -04004505find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004506 /*
4507 * we didn't find a candidate key in this node, walk forward
4508 * and find another one
4509 */
4510 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004511 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004512 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004513 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004514 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004515 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004516 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004517 goto again;
4518 } else {
4519 goto out;
4520 }
4521 }
4522 /* save our key for returning back */
4523 btrfs_node_key_to_cpu(cur, &found_key, slot);
4524 path->slots[level] = slot;
4525 if (level == path->lowest_level) {
4526 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004527 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004528 goto out;
4529 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004530 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004531 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004532 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004533
Chris Masonbd681512011-07-16 15:23:14 -04004534 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004535
Chris Masonbd681512011-07-16 15:23:14 -04004536 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004537 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004538 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004539 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004540 }
4541out:
4542 if (ret == 0)
4543 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004544 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004545 return ret;
4546}
4547
4548/*
4549 * this is similar to btrfs_next_leaf, but does not try to preserve
4550 * and fixup the path. It looks for and returns the next key in the
4551 * tree based on the current path and the cache_only and min_trans
4552 * parameters.
4553 *
4554 * 0 is returned if another key is found, < 0 if there are any errors
4555 * and 1 is returned if there are no higher keys in the tree
4556 *
4557 * path->keep_locks should be set to 1 on the search made before
4558 * calling this function.
4559 */
Chris Masone7a84562008-06-25 16:01:31 -04004560int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004561 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004562 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004563{
Chris Masone7a84562008-06-25 16:01:31 -04004564 int slot;
4565 struct extent_buffer *c;
4566
Chris Mason934d3752008-12-08 16:43:10 -05004567 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004568 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004569 if (!path->nodes[level])
4570 return 1;
4571
4572 slot = path->slots[level] + 1;
4573 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004574next:
Chris Masone7a84562008-06-25 16:01:31 -04004575 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004576 int ret;
4577 int orig_lowest;
4578 struct btrfs_key cur_key;
4579 if (level + 1 >= BTRFS_MAX_LEVEL ||
4580 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004581 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004582
4583 if (path->locks[level + 1]) {
4584 level++;
4585 continue;
4586 }
4587
4588 slot = btrfs_header_nritems(c) - 1;
4589 if (level == 0)
4590 btrfs_item_key_to_cpu(c, &cur_key, slot);
4591 else
4592 btrfs_node_key_to_cpu(c, &cur_key, slot);
4593
4594 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004595 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004596 path->lowest_level = level;
4597 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4598 0, 0);
4599 path->lowest_level = orig_lowest;
4600 if (ret < 0)
4601 return ret;
4602
4603 c = path->nodes[level];
4604 slot = path->slots[level];
4605 if (ret == 0)
4606 slot++;
4607 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004608 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004609
Chris Masone7a84562008-06-25 16:01:31 -04004610 if (level == 0)
4611 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004612 else {
4613 u64 blockptr = btrfs_node_blockptr(c, slot);
4614 u64 gen = btrfs_node_ptr_generation(c, slot);
4615
4616 if (cache_only) {
4617 struct extent_buffer *cur;
4618 cur = btrfs_find_tree_block(root, blockptr,
4619 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004620 if (!cur ||
4621 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004622 slot++;
4623 if (cur)
4624 free_extent_buffer(cur);
4625 goto next;
4626 }
4627 free_extent_buffer(cur);
4628 }
4629 if (gen < min_trans) {
4630 slot++;
4631 goto next;
4632 }
Chris Masone7a84562008-06-25 16:01:31 -04004633 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004634 }
Chris Masone7a84562008-06-25 16:01:31 -04004635 return 0;
4636 }
4637 return 1;
4638}
4639
Chris Mason7bb86312007-12-11 09:25:06 -05004640/*
Chris Mason925baed2008-06-25 16:01:30 -04004641 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004642 * returns 0 if it found something or 1 if there are no greater leaves.
4643 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004644 */
Chris Mason234b63a2007-03-13 10:46:10 -04004645int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004646{
4647 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004648 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004649 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004650 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004651 struct btrfs_key key;
4652 u32 nritems;
4653 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04004654 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04004655 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004656
4657 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05004658 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04004659 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04004660
Chris Mason8e73f272009-04-03 10:14:18 -04004661 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
4662again:
4663 level = 1;
4664 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04004665 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02004666 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04004667
Chris Masona2135012008-06-25 16:01:30 -04004668 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04004669 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04004670
Chris Mason925baed2008-06-25 16:01:30 -04004671 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4672 path->keep_locks = 0;
4673
4674 if (ret < 0)
4675 return ret;
4676
Chris Masona2135012008-06-25 16:01:30 -04004677 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04004678 /*
4679 * by releasing the path above we dropped all our locks. A balance
4680 * could have added more items next to the key that used to be
4681 * at the very end of the block. So, check again here and
4682 * advance the path if there are now more items available.
4683 */
Chris Masona2135012008-06-25 16:01:30 -04004684 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04004685 if (ret == 0)
4686 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04004687 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004688 goto done;
4689 }
Chris Masond97e63b2007-02-20 16:40:44 -05004690
Chris Masond3977122009-01-05 21:25:51 -05004691 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04004692 if (!path->nodes[level]) {
4693 ret = 1;
4694 goto done;
4695 }
Chris Mason5f39d392007-10-15 16:14:19 -04004696
Chris Masond97e63b2007-02-20 16:40:44 -05004697 slot = path->slots[level] + 1;
4698 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04004699 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05004700 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04004701 if (level == BTRFS_MAX_LEVEL) {
4702 ret = 1;
4703 goto done;
4704 }
Chris Masond97e63b2007-02-20 16:40:44 -05004705 continue;
4706 }
Chris Mason5f39d392007-10-15 16:14:19 -04004707
Chris Mason925baed2008-06-25 16:01:30 -04004708 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04004709 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04004710 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04004711 }
Chris Mason5f39d392007-10-15 16:14:19 -04004712
Chris Mason8e73f272009-04-03 10:14:18 -04004713 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04004714 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04004715 ret = read_block_for_search(NULL, root, path, &next, level,
4716 slot, &key);
4717 if (ret == -EAGAIN)
4718 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04004719
Chris Mason76a05b32009-05-14 13:24:30 -04004720 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004721 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004722 goto done;
4723 }
4724
Chris Mason5cd57b22008-06-25 16:01:30 -04004725 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004726 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004727 if (!ret) {
4728 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004729 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004730 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004731 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004732 }
Chris Mason31533fb2011-07-26 16:01:59 -04004733 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004734 }
Chris Masond97e63b2007-02-20 16:40:44 -05004735 break;
4736 }
4737 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05004738 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05004739 level--;
4740 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04004741 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04004742 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04004743
Chris Mason5f39d392007-10-15 16:14:19 -04004744 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05004745 path->nodes[level] = next;
4746 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04004747 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04004748 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05004749 if (!level)
4750 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004751
Chris Mason8e73f272009-04-03 10:14:18 -04004752 ret = read_block_for_search(NULL, root, path, &next, level,
4753 0, &key);
4754 if (ret == -EAGAIN)
4755 goto again;
4756
Chris Mason76a05b32009-05-14 13:24:30 -04004757 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004758 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04004759 goto done;
4760 }
4761
Chris Mason5cd57b22008-06-25 16:01:30 -04004762 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04004763 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04004764 if (!ret) {
4765 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04004766 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04004767 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04004768 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04004769 }
Chris Mason31533fb2011-07-26 16:01:59 -04004770 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04004771 }
Chris Masond97e63b2007-02-20 16:40:44 -05004772 }
Chris Mason8e73f272009-04-03 10:14:18 -04004773 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04004774done:
Chris Masonf7c79f32012-03-19 15:54:38 -04004775 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04004776 path->leave_spinning = old_spinning;
4777 if (!old_spinning)
4778 btrfs_set_path_blocking(path);
4779
4780 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05004781}
Chris Mason0b86a832008-03-24 15:01:56 -04004782
Chris Mason3f157a22008-06-25 16:01:31 -04004783/*
4784 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
4785 * searching until it gets past min_objectid or finds an item of 'type'
4786 *
4787 * returns 0 if something is found, 1 if nothing was found and < 0 on error
4788 */
Chris Mason0b86a832008-03-24 15:01:56 -04004789int btrfs_previous_item(struct btrfs_root *root,
4790 struct btrfs_path *path, u64 min_objectid,
4791 int type)
4792{
4793 struct btrfs_key found_key;
4794 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04004795 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04004796 int ret;
4797
Chris Masond3977122009-01-05 21:25:51 -05004798 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04004799 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05004800 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04004801 ret = btrfs_prev_leaf(root, path);
4802 if (ret != 0)
4803 return ret;
4804 } else {
4805 path->slots[0]--;
4806 }
4807 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04004808 nritems = btrfs_header_nritems(leaf);
4809 if (nritems == 0)
4810 return 1;
4811 if (path->slots[0] == nritems)
4812 path->slots[0]--;
4813
Chris Mason0b86a832008-03-24 15:01:56 -04004814 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04004815 if (found_key.objectid < min_objectid)
4816 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04004817 if (found_key.type == type)
4818 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04004819 if (found_key.objectid == min_objectid &&
4820 found_key.type < type)
4821 break;
Chris Mason0b86a832008-03-24 15:01:56 -04004822 }
4823 return 1;
4824}