blob: 26e8dc1681b08278fbfb4bf5e831ce74d7b54d98 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +020041 struct btrfs_path *path, int level, int slot,
42 int tree_mod_log);
Jan Schmidtf2304752012-05-26 11:43:17 +020043static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
47 u64 time_seq);
48struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
50 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050051
Chris Mason2c90e5d2007-04-02 10:50:19 -040052struct btrfs_path *btrfs_alloc_path(void)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050055 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040056 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 }
75}
76
77/*
78 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050079 *
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
83 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050084 */
Chris Mason4008c042009-02-12 14:09:45 -050085noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040086 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050087{
88 int i;
Chris Mason4008c042009-02-12 14:09:45 -050089
90#ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
95 * the path blocking.
96 */
Chris Masonbd681512011-07-16 15:23:14 -040097 if (held) {
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
103 }
Chris Mason4008c042009-02-12 14:09:45 -0500104 btrfs_set_path_blocking(p);
105#endif
106
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
114 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115 }
Chris Mason4008c042009-02-12 14:09:45 -0500116
117#ifdef CONFIG_DEBUG_LOCK_ALLOC
118 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400119 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500120#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400124void btrfs_free_path(struct btrfs_path *p)
125{
Jesper Juhlff175d52010-12-25 21:22:30 +0000126 if (!p)
127 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200128 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400129 kmem_cache_free(btrfs_path_cachep, p);
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
135 *
136 * It is safe to call this on paths that no locks or extent buffers held.
137 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200138noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500139{
140 int i;
Chris Masona2135012008-06-25 16:01:30 -0400141
Chris Mason234b63a2007-03-13 10:46:10 -0400142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400143 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400145 continue;
146 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400148 p->locks[i] = 0;
149 }
Chris Mason5f39d392007-10-15 16:14:19 -0400150 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400151 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
159 * looping required.
160 *
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
164 */
Chris Mason925baed2008-06-25 16:01:30 -0400165struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166{
167 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400168
Josef Bacik3083ee22012-03-09 16:01:49 -0500169 while (1) {
170 rcu_read_lock();
171 eb = rcu_dereference(root->node);
172
173 /*
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
178 */
179 if (atomic_inc_not_zero(&eb->refs)) {
180 rcu_read_unlock();
181 break;
182 }
183 rcu_read_unlock();
184 synchronize_rcu();
185 }
Chris Mason925baed2008-06-25 16:01:30 -0400186 return eb;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Chris Mason925baed2008-06-25 16:01:30 -0400193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194{
195 struct extent_buffer *eb;
196
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400198 eb = btrfs_root_node(root);
199 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400200 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400201 break;
Chris Mason925baed2008-06-25 16:01:30 -0400202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masonbd681512011-07-16 15:23:14 -0400208/* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
211 */
212struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213{
214 struct extent_buffer *eb;
215
216 while (1) {
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
220 break;
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
223 }
224 return eb;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
230 */
Chris Mason0b86a832008-03-24 15:01:56 -0400231static void add_root_to_dirty_list(struct btrfs_root *root)
232{
Chris Masone5846fc2012-05-03 12:08:48 -0400233 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
237 }
Chris Masone5846fc2012-05-03 12:08:48 -0400238 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400239}
240
Chris Masond352ac62008-09-29 15:18:18 -0400241/*
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
245 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500246int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
250{
251 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 int ret = 0;
253 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259
260 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 if (level == 0)
262 btrfs_item_key(buf, &disk_key, 0);
263 else
264 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200268 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 return PTR_ERR(cow);
271
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
280 else
281 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500282
Yan Zheng2b820322008-11-17 21:11:30 -0500283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
285 BTRFS_FSID_SIZE);
286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500292
Chris Masonbe20aa92007-12-17 20:14:01 -0500293 if (ret)
294 return ret;
295
296 btrfs_mark_buffer_dirty(cow);
297 *cow_ret = cow;
298 return 0;
299}
300
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200301enum mod_log_op {
302 MOD_LOG_KEY_REPLACE,
303 MOD_LOG_KEY_ADD,
304 MOD_LOG_KEY_REMOVE,
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307 MOD_LOG_MOVE_KEYS,
308 MOD_LOG_ROOT_REPLACE,
309};
310
311struct tree_mod_move {
312 int dst_slot;
313 int nr_items;
314};
315
316struct tree_mod_root {
317 u64 logical;
318 u8 level;
319};
320
321struct tree_mod_elem {
322 struct rb_node node;
323 u64 index; /* shifted logical */
324 struct seq_list elem;
325 enum mod_log_op op;
326
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328 int slot;
329
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331 u64 generation;
332
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
335 u64 blockptr;
336
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
339
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
342};
343
344static inline void
345__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
346{
347 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
349}
350
351void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 elem->flags = 1;
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 __get_tree_mod_seq(fs_info, elem);
357 spin_unlock(&fs_info->tree_mod_seq_lock);
358}
359
360void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361 struct seq_list *elem)
362{
363 struct rb_root *tm_root;
364 struct rb_node *node;
365 struct rb_node *next;
366 struct seq_list *cur_elem;
367 struct tree_mod_elem *tm;
368 u64 min_seq = (u64)-1;
369 u64 seq_putting = elem->seq;
370
371 if (!seq_putting)
372 return;
373
374 BUG_ON(!(elem->flags & 1));
375 spin_lock(&fs_info->tree_mod_seq_lock);
376 list_del(&elem->list);
377
378 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380 if (seq_putting > cur_elem->seq) {
381 /*
382 * blocker with lower sequence number exists, we
383 * cannot remove anything from the log
384 */
385 goto out;
386 }
387 min_seq = cur_elem->seq;
388 }
389 }
390
391 /*
392 * anything that's lower than the lowest existing (read: blocked)
393 * sequence number can be removed from the tree.
394 */
395 write_lock(&fs_info->tree_mod_log_lock);
396 tm_root = &fs_info->tree_mod_log;
397 for (node = rb_first(tm_root); node; node = next) {
398 next = rb_next(node);
399 tm = container_of(node, struct tree_mod_elem, node);
400 if (tm->elem.seq > min_seq)
401 continue;
402 rb_erase(node, tm_root);
403 list_del(&tm->elem.list);
404 kfree(tm);
405 }
406 write_unlock(&fs_info->tree_mod_log_lock);
407out:
408 spin_unlock(&fs_info->tree_mod_seq_lock);
409}
410
411/*
412 * key order of the log:
413 * index -> sequence
414 *
415 * the index is the shifted logical of the *new* root node for root replace
416 * operations, or the shifted logical of the affected block for all other
417 * operations.
418 */
419static noinline int
420__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
421{
422 struct rb_root *tm_root;
423 struct rb_node **new;
424 struct rb_node *parent = NULL;
425 struct tree_mod_elem *cur;
426 int ret = 0;
427
428 BUG_ON(!tm || !tm->elem.seq);
429
430 write_lock(&fs_info->tree_mod_log_lock);
431 tm_root = &fs_info->tree_mod_log;
432 new = &tm_root->rb_node;
433 while (*new) {
434 cur = container_of(*new, struct tree_mod_elem, node);
435 parent = *new;
436 if (cur->index < tm->index)
437 new = &((*new)->rb_left);
438 else if (cur->index > tm->index)
439 new = &((*new)->rb_right);
440 else if (cur->elem.seq < tm->elem.seq)
441 new = &((*new)->rb_left);
442 else if (cur->elem.seq > tm->elem.seq)
443 new = &((*new)->rb_right);
444 else {
445 kfree(tm);
446 ret = -EEXIST;
447 goto unlock;
448 }
449 }
450
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
453unlock:
454 write_unlock(&fs_info->tree_mod_log_lock);
455 return ret;
456}
457
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200458static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
459 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200460{
461 struct tree_mod_elem *tm;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200462 int seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200463
464 smp_mb();
465 if (list_empty(&fs_info->tree_mod_seq_list))
466 return 0;
467
468 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
469 if (!tm)
470 return -ENOMEM;
471
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200472 tm->elem.flags = 0;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200473 spin_lock(&fs_info->tree_mod_seq_lock);
474 if (list_empty(&fs_info->tree_mod_seq_list)) {
475 /*
476 * someone emptied the list while we were waiting for the lock.
477 * we must not add to the list, because no blocker exists. items
478 * are removed from the list only when the existing blocker is
479 * removed from the list.
480 */
481 kfree(tm);
482 seq = 0;
483 } else {
484 __get_tree_mod_seq(fs_info, &tm->elem);
485 seq = tm->elem.seq;
486 }
487 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200488
489 return seq;
490}
491
492static noinline int
493tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
494 struct extent_buffer *eb, int slot,
495 enum mod_log_op op, gfp_t flags)
496{
497 struct tree_mod_elem *tm;
498 int ret;
499
500 ret = tree_mod_alloc(fs_info, flags, &tm);
501 if (ret <= 0)
502 return ret;
503
504 tm->index = eb->start >> PAGE_CACHE_SHIFT;
505 if (op != MOD_LOG_KEY_ADD) {
506 btrfs_node_key(eb, &tm->key, slot);
507 tm->blockptr = btrfs_node_blockptr(eb, slot);
508 }
509 tm->op = op;
510 tm->slot = slot;
511 tm->generation = btrfs_node_ptr_generation(eb, slot);
512
513 return __tree_mod_log_insert(fs_info, tm);
514}
515
516static noinline int
517tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
518 int slot, enum mod_log_op op)
519{
520 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
521}
522
523static noinline int
524tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
525 struct extent_buffer *eb, int dst_slot, int src_slot,
526 int nr_items, gfp_t flags)
527{
528 struct tree_mod_elem *tm;
529 int ret;
530 int i;
531
532 ret = tree_mod_alloc(fs_info, flags, &tm);
533 if (ret <= 0)
534 return ret;
535
536 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
537 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
538 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
539 BUG_ON(ret < 0);
540 }
541
542 tm->index = eb->start >> PAGE_CACHE_SHIFT;
543 tm->slot = src_slot;
544 tm->move.dst_slot = dst_slot;
545 tm->move.nr_items = nr_items;
546 tm->op = MOD_LOG_MOVE_KEYS;
547
548 return __tree_mod_log_insert(fs_info, tm);
549}
550
551static noinline int
552tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
553 struct extent_buffer *old_root,
554 struct extent_buffer *new_root, gfp_t flags)
555{
556 struct tree_mod_elem *tm;
557 int ret;
558
559 ret = tree_mod_alloc(fs_info, flags, &tm);
560 if (ret <= 0)
561 return ret;
562
563 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
564 tm->old_root.logical = old_root->start;
565 tm->old_root.level = btrfs_header_level(old_root);
566 tm->generation = btrfs_header_generation(old_root);
567 tm->op = MOD_LOG_ROOT_REPLACE;
568
569 return __tree_mod_log_insert(fs_info, tm);
570}
571
572static struct tree_mod_elem *
573__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
574 int smallest)
575{
576 struct rb_root *tm_root;
577 struct rb_node *node;
578 struct tree_mod_elem *cur = NULL;
579 struct tree_mod_elem *found = NULL;
580 u64 index = start >> PAGE_CACHE_SHIFT;
581
582 read_lock(&fs_info->tree_mod_log_lock);
583 tm_root = &fs_info->tree_mod_log;
584 node = tm_root->rb_node;
585 while (node) {
586 cur = container_of(node, struct tree_mod_elem, node);
587 if (cur->index < index) {
588 node = node->rb_left;
589 } else if (cur->index > index) {
590 node = node->rb_right;
591 } else if (cur->elem.seq < min_seq) {
592 node = node->rb_left;
593 } else if (!smallest) {
594 /* we want the node with the highest seq */
595 if (found)
596 BUG_ON(found->elem.seq > cur->elem.seq);
597 found = cur;
598 node = node->rb_left;
599 } else if (cur->elem.seq > min_seq) {
600 /* we want the node with the smallest seq */
601 if (found)
602 BUG_ON(found->elem.seq < cur->elem.seq);
603 found = cur;
604 node = node->rb_right;
605 } else {
606 found = cur;
607 break;
608 }
609 }
610 read_unlock(&fs_info->tree_mod_log_lock);
611
612 return found;
613}
614
615/*
616 * this returns the element from the log with the smallest time sequence
617 * value that's in the log (the oldest log item). any element with a time
618 * sequence lower than min_seq will be ignored.
619 */
620static struct tree_mod_elem *
621tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
622 u64 min_seq)
623{
624 return __tree_mod_log_search(fs_info, start, min_seq, 1);
625}
626
627/*
628 * this returns the element from the log with the largest time sequence
629 * value that's in the log (the most recent log item). any element with
630 * a time sequence lower than min_seq will be ignored.
631 */
632static struct tree_mod_elem *
633tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
634{
635 return __tree_mod_log_search(fs_info, start, min_seq, 0);
636}
637
638static inline void
639tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
640 struct extent_buffer *src, unsigned long dst_offset,
641 unsigned long src_offset, int nr_items)
642{
643 int ret;
644 int i;
645
646 smp_mb();
647 if (list_empty(&fs_info->tree_mod_seq_list))
648 return;
649
650 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
651 return;
652
653 /* speed this up by single seq for all operations? */
654 for (i = 0; i < nr_items; i++) {
655 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
656 MOD_LOG_KEY_REMOVE);
657 BUG_ON(ret < 0);
658 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
659 MOD_LOG_KEY_ADD);
660 BUG_ON(ret < 0);
661 }
662}
663
664static inline void
665tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
666 int dst_offset, int src_offset, int nr_items)
667{
668 int ret;
669 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
670 nr_items, GFP_NOFS);
671 BUG_ON(ret < 0);
672}
673
674static inline void
675tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
676 struct extent_buffer *eb,
677 struct btrfs_disk_key *disk_key, int slot, int atomic)
678{
679 int ret;
680
681 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
682 MOD_LOG_KEY_REPLACE,
683 atomic ? GFP_ATOMIC : GFP_NOFS);
684 BUG_ON(ret < 0);
685}
686
687static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
688 struct extent_buffer *eb)
689{
690 int i;
691 int ret;
692 u32 nritems;
693
694 smp_mb();
695 if (list_empty(&fs_info->tree_mod_seq_list))
696 return;
697
698 if (btrfs_header_level(eb) == 0)
699 return;
700
701 nritems = btrfs_header_nritems(eb);
702 for (i = nritems - 1; i >= 0; i--) {
703 ret = tree_mod_log_insert_key(fs_info, eb, i,
704 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
705 BUG_ON(ret < 0);
706 }
707}
708
709static inline void
710tree_mod_log_set_root_pointer(struct btrfs_root *root,
711 struct extent_buffer *new_root_node)
712{
713 int ret;
714 tree_mod_log_free_eb(root->fs_info, root->node);
715 ret = tree_mod_log_insert_root(root->fs_info, root->node,
716 new_root_node, GFP_NOFS);
717 BUG_ON(ret < 0);
718}
719
Chris Masond352ac62008-09-29 15:18:18 -0400720/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400721 * check if the tree block can be shared by multiple trees
722 */
723int btrfs_block_can_be_shared(struct btrfs_root *root,
724 struct extent_buffer *buf)
725{
726 /*
727 * Tree blocks not in refernece counted trees and tree roots
728 * are never shared. If a block was allocated after the last
729 * snapshot and the block was not allocated by tree relocation,
730 * we know the block is not shared.
731 */
732 if (root->ref_cows &&
733 buf != root->node && buf != root->commit_root &&
734 (btrfs_header_generation(buf) <=
735 btrfs_root_last_snapshot(&root->root_item) ||
736 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
737 return 1;
738#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
739 if (root->ref_cows &&
740 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
741 return 1;
742#endif
743 return 0;
744}
745
746static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
747 struct btrfs_root *root,
748 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400749 struct extent_buffer *cow,
750 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400751{
752 u64 refs;
753 u64 owner;
754 u64 flags;
755 u64 new_flags = 0;
756 int ret;
757
758 /*
759 * Backrefs update rules:
760 *
761 * Always use full backrefs for extent pointers in tree block
762 * allocated by tree relocation.
763 *
764 * If a shared tree block is no longer referenced by its owner
765 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
766 * use full backrefs for extent pointers in tree block.
767 *
768 * If a tree block is been relocating
769 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
770 * use full backrefs for extent pointers in tree block.
771 * The reason for this is some operations (such as drop tree)
772 * are only allowed for blocks use full backrefs.
773 */
774
775 if (btrfs_block_can_be_shared(root, buf)) {
776 ret = btrfs_lookup_extent_info(trans, root, buf->start,
777 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700778 if (ret)
779 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700780 if (refs == 0) {
781 ret = -EROFS;
782 btrfs_std_error(root->fs_info, ret);
783 return ret;
784 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400785 } else {
786 refs = 1;
787 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
788 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
789 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
790 else
791 flags = 0;
792 }
793
794 owner = btrfs_header_owner(buf);
795 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
796 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
797
798 if (refs > 1) {
799 if ((owner == root->root_key.objectid ||
800 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
801 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200802 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100803 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400804
805 if (root->root_key.objectid ==
806 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200807 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100808 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200809 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100810 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400811 }
812 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
813 } else {
814
815 if (root->root_key.objectid ==
816 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200817 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400818 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200819 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100820 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400821 }
822 if (new_flags != 0) {
823 ret = btrfs_set_disk_extent_flags(trans, root,
824 buf->start,
825 buf->len,
826 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700827 if (ret)
828 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400829 }
830 } else {
831 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
832 if (root->root_key.objectid ==
833 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200834 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200836 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100837 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200838 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100839 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400840 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200841 /*
842 * don't log freeing in case we're freeing the root node, this
843 * is done by tree_mod_log_set_root_pointer later
844 */
845 if (buf != root->node && btrfs_header_level(buf) != 0)
846 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400847 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400848 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 }
850 return 0;
851}
852
853/*
Chris Masond3977122009-01-05 21:25:51 -0500854 * does the dirty work in cow of a single block. The parent block (if
855 * supplied) is updated to point to the new cow copy. The new buffer is marked
856 * dirty and returned locked. If you modify the block it needs to be marked
857 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400858 *
859 * search_start -- an allocation hint for the new block
860 *
Chris Masond3977122009-01-05 21:25:51 -0500861 * empty_size -- a hint that you plan on doing more cow. This is the size in
862 * bytes the allocator should try to find free next to the block it returns.
863 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400864 */
Chris Masond3977122009-01-05 21:25:51 -0500865static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400866 struct btrfs_root *root,
867 struct extent_buffer *buf,
868 struct extent_buffer *parent, int parent_slot,
869 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400870 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400871{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400872 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400873 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700874 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400875 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400876 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400877 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400878
Chris Mason925baed2008-06-25 16:01:30 -0400879 if (*cow_ret == buf)
880 unlock_orig = 1;
881
Chris Masonb9447ef2009-03-09 11:45:38 -0400882 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400883
Chris Mason7bb86312007-12-11 09:25:06 -0500884 WARN_ON(root->ref_cows && trans->transid !=
885 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400886 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400887
Chris Mason7bb86312007-12-11 09:25:06 -0500888 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400889
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400890 if (level == 0)
891 btrfs_item_key(buf, &disk_key, 0);
892 else
893 btrfs_node_key(buf, &disk_key, 0);
894
895 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
896 if (parent)
897 parent_start = parent->start;
898 else
899 parent_start = 0;
900 } else
901 parent_start = 0;
902
903 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
904 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200905 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400906 if (IS_ERR(cow))
907 return PTR_ERR(cow);
908
Chris Masonb4ce94d2009-02-04 09:25:08 -0500909 /* cow is set to blocking by btrfs_init_new_buffer */
910
Chris Mason5f39d392007-10-15 16:14:19 -0400911 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400912 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400913 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400914 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
915 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
916 BTRFS_HEADER_FLAG_RELOC);
917 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
918 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
919 else
920 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400921
Yan Zheng2b820322008-11-17 21:11:30 -0500922 write_extent_buffer(cow, root->fs_info->fsid,
923 (unsigned long)btrfs_header_fsid(cow),
924 BTRFS_FSID_SIZE);
925
Mark Fashehbe1a5562011-08-08 13:20:18 -0700926 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700927 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700929 return ret;
930 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400931
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400932 if (root->ref_cows)
933 btrfs_reloc_cow_block(trans, root, buf, cow);
934
Chris Mason6702ed42007-08-07 16:15:09 -0400935 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400936 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400937 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
938 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
939 parent_start = buf->start;
940 else
941 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400942
Chris Mason5f39d392007-10-15 16:14:19 -0400943 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200944 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400945 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400946
Yan, Zhengf0486c62010-05-16 10:46:25 -0400947 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200948 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400949 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400950 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400951 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400952 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
953 parent_start = parent->start;
954 else
955 parent_start = 0;
956
957 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200958 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
959 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400960 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400961 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500962 btrfs_set_node_ptr_generation(parent, parent_slot,
963 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400964 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400965 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200966 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400967 }
Chris Mason925baed2008-06-25 16:01:30 -0400968 if (unlock_orig)
969 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500970 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400971 btrfs_mark_buffer_dirty(cow);
972 *cow_ret = cow;
973 return 0;
974}
975
Jan Schmidt5d9e75c42012-05-16 18:25:47 +0200976/*
977 * returns the logical address of the oldest predecessor of the given root.
978 * entries older than time_seq are ignored.
979 */
980static struct tree_mod_elem *
981__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
982 struct btrfs_root *root, u64 time_seq)
983{
984 struct tree_mod_elem *tm;
985 struct tree_mod_elem *found = NULL;
986 u64 root_logical = root->node->start;
987 int looped = 0;
988
989 if (!time_seq)
990 return 0;
991
992 /*
993 * the very last operation that's logged for a root is the replacement
994 * operation (if it is replaced at all). this has the index of the *new*
995 * root, making it the very first operation that's logged for this root.
996 */
997 while (1) {
998 tm = tree_mod_log_search_oldest(fs_info, root_logical,
999 time_seq);
1000 if (!looped && !tm)
1001 return 0;
1002 /*
1003 * we must have key remove operations in the log before the
1004 * replace operation.
1005 */
1006 BUG_ON(!tm);
1007
1008 if (tm->op != MOD_LOG_ROOT_REPLACE)
1009 break;
1010
1011 found = tm;
1012 root_logical = tm->old_root.logical;
1013 BUG_ON(root_logical == root->node->start);
1014 looped = 1;
1015 }
1016
1017 return found;
1018}
1019
1020/*
1021 * tm is a pointer to the first operation to rewind within eb. then, all
1022 * previous operations will be rewinded (until we reach something older than
1023 * time_seq).
1024 */
1025static void
1026__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1027 struct tree_mod_elem *first_tm)
1028{
1029 u32 n;
1030 struct rb_node *next;
1031 struct tree_mod_elem *tm = first_tm;
1032 unsigned long o_dst;
1033 unsigned long o_src;
1034 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1035
1036 n = btrfs_header_nritems(eb);
1037 while (tm && tm->elem.seq >= time_seq) {
1038 /*
1039 * all the operations are recorded with the operator used for
1040 * the modification. as we're going backwards, we do the
1041 * opposite of each operation here.
1042 */
1043 switch (tm->op) {
1044 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1045 BUG_ON(tm->slot < n);
1046 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1047 case MOD_LOG_KEY_REMOVE:
1048 btrfs_set_node_key(eb, &tm->key, tm->slot);
1049 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1050 btrfs_set_node_ptr_generation(eb, tm->slot,
1051 tm->generation);
1052 n++;
1053 break;
1054 case MOD_LOG_KEY_REPLACE:
1055 BUG_ON(tm->slot >= n);
1056 btrfs_set_node_key(eb, &tm->key, tm->slot);
1057 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1058 btrfs_set_node_ptr_generation(eb, tm->slot,
1059 tm->generation);
1060 break;
1061 case MOD_LOG_KEY_ADD:
1062 if (tm->slot != n - 1) {
1063 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1064 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1065 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1066 }
1067 n--;
1068 break;
1069 case MOD_LOG_MOVE_KEYS:
1070 memmove_extent_buffer(eb, tm->slot, tm->move.dst_slot,
1071 tm->move.nr_items * p_size);
1072 break;
1073 case MOD_LOG_ROOT_REPLACE:
1074 /*
1075 * this operation is special. for roots, this must be
1076 * handled explicitly before rewinding.
1077 * for non-roots, this operation may exist if the node
1078 * was a root: root A -> child B; then A gets empty and
1079 * B is promoted to the new root. in the mod log, we'll
1080 * have a root-replace operation for B, a tree block
1081 * that is no root. we simply ignore that operation.
1082 */
1083 break;
1084 }
1085 next = rb_next(&tm->node);
1086 if (!next)
1087 break;
1088 tm = container_of(next, struct tree_mod_elem, node);
1089 if (tm->index != first_tm->index)
1090 break;
1091 }
1092 btrfs_set_header_nritems(eb, n);
1093}
1094
1095static struct extent_buffer *
1096tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1097 u64 time_seq)
1098{
1099 struct extent_buffer *eb_rewin;
1100 struct tree_mod_elem *tm;
1101
1102 if (!time_seq)
1103 return eb;
1104
1105 if (btrfs_header_level(eb) == 0)
1106 return eb;
1107
1108 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1109 if (!tm)
1110 return eb;
1111
1112 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1113 BUG_ON(tm->slot != 0);
1114 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1115 fs_info->tree_root->nodesize);
1116 BUG_ON(!eb_rewin);
1117 btrfs_set_header_bytenr(eb_rewin, eb->start);
1118 btrfs_set_header_backref_rev(eb_rewin,
1119 btrfs_header_backref_rev(eb));
1120 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1121 } else {
1122 eb_rewin = btrfs_clone_extent_buffer(eb);
1123 BUG_ON(!eb_rewin);
1124 }
1125
1126 extent_buffer_get(eb_rewin);
1127 free_extent_buffer(eb);
1128
1129 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1130
1131 return eb_rewin;
1132}
1133
1134static inline struct extent_buffer *
1135get_old_root(struct btrfs_root *root, u64 time_seq)
1136{
1137 struct tree_mod_elem *tm;
1138 struct extent_buffer *eb;
1139 struct tree_mod_root *old_root;
1140 u64 old_generation;
1141
1142 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1143 if (!tm)
1144 return root->node;
1145
1146 old_root = &tm->old_root;
1147 old_generation = tm->generation;
1148
1149 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1150 /*
1151 * there was an item in the log when __tree_mod_log_oldest_root
1152 * returned. this one must not go away, because the time_seq passed to
1153 * us must be blocking its removal.
1154 */
1155 BUG_ON(!tm);
1156
1157 if (old_root->logical == root->node->start) {
1158 /* there are logged operations for the current root */
1159 eb = btrfs_clone_extent_buffer(root->node);
1160 } else {
1161 /* there's a root replace operation for the current root */
1162 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1163 root->nodesize);
1164 btrfs_set_header_bytenr(eb, eb->start);
1165 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1166 btrfs_set_header_owner(eb, root->root_key.objectid);
1167 }
1168 if (!eb)
1169 return NULL;
1170 btrfs_set_header_level(eb, old_root->level);
1171 btrfs_set_header_generation(eb, old_generation);
1172 __tree_mod_log_rewind(eb, time_seq, tm);
1173
1174 return eb;
1175}
1176
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001177static inline int should_cow_block(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root,
1179 struct extent_buffer *buf)
1180{
Liu Bof1ebcc72011-11-14 20:48:06 -05001181 /* ensure we can see the force_cow */
1182 smp_rmb();
1183
1184 /*
1185 * We do not need to cow a block if
1186 * 1) this block is not created or changed in this transaction;
1187 * 2) this block does not belong to TREE_RELOC tree;
1188 * 3) the root is not forced COW.
1189 *
1190 * What is forced COW:
1191 * when we create snapshot during commiting the transaction,
1192 * after we've finished coping src root, we must COW the shared
1193 * block to ensure the metadata consistency.
1194 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001195 if (btrfs_header_generation(buf) == trans->transid &&
1196 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1197 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001198 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1199 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001200 return 0;
1201 return 1;
1202}
1203
Chris Masond352ac62008-09-29 15:18:18 -04001204/*
1205 * cows a single block, see __btrfs_cow_block for the real work.
1206 * This version of it has extra checks so that a block isn't cow'd more than
1207 * once per transaction, as long as it hasn't been written yet
1208 */
Chris Masond3977122009-01-05 21:25:51 -05001209noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001210 struct btrfs_root *root, struct extent_buffer *buf,
1211 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001212 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001213{
Chris Mason6702ed42007-08-07 16:15:09 -04001214 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001215 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001216
Chris Masonccd467d2007-06-28 15:57:36 -04001217 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001218 printk(KERN_CRIT "trans %llu running %llu\n",
1219 (unsigned long long)trans->transid,
1220 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001221 root->fs_info->running_transaction->transid);
1222 WARN_ON(1);
1223 }
1224 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001225 printk(KERN_CRIT "trans %llu running %llu\n",
1226 (unsigned long long)trans->transid,
1227 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001228 WARN_ON(1);
1229 }
Chris Masondc17ff82008-01-08 15:46:30 -05001230
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001231 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001232 *cow_ret = buf;
1233 return 0;
1234 }
Chris Masonc4876852009-02-04 09:24:25 -05001235
Chris Mason0b86a832008-03-24 15:01:56 -04001236 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001237
1238 if (parent)
1239 btrfs_set_lock_blocking(parent);
1240 btrfs_set_lock_blocking(buf);
1241
Chris Masonf510cfe2007-10-15 16:14:48 -04001242 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001243 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001244
1245 trace_btrfs_cow_block(root, buf, *cow_ret);
1246
Chris Masonf510cfe2007-10-15 16:14:48 -04001247 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001248}
1249
Chris Masond352ac62008-09-29 15:18:18 -04001250/*
1251 * helper function for defrag to decide if two blocks pointed to by a
1252 * node are actually close by
1253 */
Chris Mason6b800532007-10-15 16:17:34 -04001254static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001255{
Chris Mason6b800532007-10-15 16:17:34 -04001256 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001257 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001258 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001259 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001260 return 0;
1261}
1262
Chris Mason081e9572007-11-06 10:26:24 -05001263/*
1264 * compare two keys in a memcmp fashion
1265 */
1266static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1267{
1268 struct btrfs_key k1;
1269
1270 btrfs_disk_key_to_cpu(&k1, disk);
1271
Diego Calleja20736ab2009-07-24 11:06:52 -04001272 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001273}
1274
Josef Bacikf3465ca2008-11-12 14:19:50 -05001275/*
1276 * same as comp_keys only with two btrfs_key's
1277 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001279{
1280 if (k1->objectid > k2->objectid)
1281 return 1;
1282 if (k1->objectid < k2->objectid)
1283 return -1;
1284 if (k1->type > k2->type)
1285 return 1;
1286 if (k1->type < k2->type)
1287 return -1;
1288 if (k1->offset > k2->offset)
1289 return 1;
1290 if (k1->offset < k2->offset)
1291 return -1;
1292 return 0;
1293}
Chris Mason081e9572007-11-06 10:26:24 -05001294
Chris Masond352ac62008-09-29 15:18:18 -04001295/*
1296 * this is used by the defrag code to go through all the
1297 * leaves pointed to by a node and reallocate them so that
1298 * disk order is close to key order
1299 */
Chris Mason6702ed42007-08-07 16:15:09 -04001300int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001301 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001302 int start_slot, int cache_only, u64 *last_ret,
1303 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001304{
Chris Mason6b800532007-10-15 16:17:34 -04001305 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001306 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001307 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001308 u64 search_start = *last_ret;
1309 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001310 u64 other;
1311 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001312 int end_slot;
1313 int i;
1314 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001315 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001316 int uptodate;
1317 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001318 int progress_passed = 0;
1319 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001320
Chris Mason5708b952007-10-25 15:43:18 -04001321 parent_level = btrfs_header_level(parent);
1322 if (cache_only && parent_level != 1)
1323 return 0;
1324
Chris Masond3977122009-01-05 21:25:51 -05001325 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001326 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001327 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001328 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001329
Chris Mason6b800532007-10-15 16:17:34 -04001330 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001331 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001332 end_slot = parent_nritems;
1333
1334 if (parent_nritems == 1)
1335 return 0;
1336
Chris Masonb4ce94d2009-02-04 09:25:08 -05001337 btrfs_set_lock_blocking(parent);
1338
Chris Mason6702ed42007-08-07 16:15:09 -04001339 for (i = start_slot; i < end_slot; i++) {
1340 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001341
Chris Mason081e9572007-11-06 10:26:24 -05001342 btrfs_node_key(parent, &disk_key, i);
1343 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1344 continue;
1345
1346 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001347 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001348 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001349 if (last_block == 0)
1350 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001351
Chris Mason6702ed42007-08-07 16:15:09 -04001352 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001353 other = btrfs_node_blockptr(parent, i - 1);
1354 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001355 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001356 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001357 other = btrfs_node_blockptr(parent, i + 1);
1358 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001359 }
Chris Masone9d0b132007-08-10 14:06:19 -04001360 if (close) {
1361 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001362 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001363 }
Chris Mason6702ed42007-08-07 16:15:09 -04001364
Chris Mason6b800532007-10-15 16:17:34 -04001365 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1366 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001367 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001368 else
1369 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001370 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001371 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001372 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001373 continue;
1374 }
Chris Mason6b800532007-10-15 16:17:34 -04001375 if (!cur) {
1376 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001377 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001378 if (!cur)
1379 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001380 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001381 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001382 }
Chris Mason6702ed42007-08-07 16:15:09 -04001383 }
Chris Masone9d0b132007-08-10 14:06:19 -04001384 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001385 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001386
Chris Masone7a84562008-06-25 16:01:31 -04001387 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001388 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001389 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001390 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001391 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001392 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001393 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001394 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001395 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001396 break;
Yan252c38f2007-08-29 09:11:44 -04001397 }
Chris Masone7a84562008-06-25 16:01:31 -04001398 search_start = cur->start;
1399 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001400 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001401 btrfs_tree_unlock(cur);
1402 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001403 }
1404 return err;
1405}
1406
Chris Mason74123bd2007-02-02 11:05:29 -05001407/*
1408 * The leaf data grows from end-to-front in the node.
1409 * this returns the address of the start of the last item,
1410 * which is the stop of the leaf data stack
1411 */
Chris Mason123abc82007-03-14 14:14:43 -04001412static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001413 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001414{
Chris Mason5f39d392007-10-15 16:14:19 -04001415 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001416 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001417 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001418 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001419}
1420
Chris Masonaa5d6be2007-02-28 16:35:06 -05001421
Chris Mason74123bd2007-02-02 11:05:29 -05001422/*
Chris Mason5f39d392007-10-15 16:14:19 -04001423 * search for key in the extent_buffer. The items start at offset p,
1424 * and they are item_size apart. There are 'max' items in p.
1425 *
Chris Mason74123bd2007-02-02 11:05:29 -05001426 * the slot in the array is returned via slot, and it points to
1427 * the place where you would insert key if it is not found in
1428 * the array.
1429 *
1430 * slot may point to max if the key is bigger than all of the keys
1431 */
Chris Masone02119d2008-09-05 16:13:11 -04001432static noinline int generic_bin_search(struct extent_buffer *eb,
1433 unsigned long p,
1434 int item_size, struct btrfs_key *key,
1435 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001436{
1437 int low = 0;
1438 int high = max;
1439 int mid;
1440 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001441 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001442 struct btrfs_disk_key unaligned;
1443 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001444 char *kaddr = NULL;
1445 unsigned long map_start = 0;
1446 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001447 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001448
Chris Masond3977122009-01-05 21:25:51 -05001449 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001450 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001451 offset = p + mid * item_size;
1452
Chris Masona6591712011-07-19 12:04:14 -04001453 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001454 (offset + sizeof(struct btrfs_disk_key)) >
1455 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001456
1457 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001458 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001459 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001460
Chris Mason479965d2007-10-15 16:14:27 -04001461 if (!err) {
1462 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1463 map_start);
1464 } else {
1465 read_extent_buffer(eb, &unaligned,
1466 offset, sizeof(unaligned));
1467 tmp = &unaligned;
1468 }
1469
Chris Mason5f39d392007-10-15 16:14:19 -04001470 } else {
1471 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1472 map_start);
1473 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001474 ret = comp_keys(tmp, key);
1475
1476 if (ret < 0)
1477 low = mid + 1;
1478 else if (ret > 0)
1479 high = mid;
1480 else {
1481 *slot = mid;
1482 return 0;
1483 }
1484 }
1485 *slot = low;
1486 return 1;
1487}
1488
Chris Mason97571fd2007-02-24 13:39:08 -05001489/*
1490 * simple bin_search frontend that does the right thing for
1491 * leaves vs nodes
1492 */
Chris Mason5f39d392007-10-15 16:14:19 -04001493static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1494 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001495{
Chris Mason5f39d392007-10-15 16:14:19 -04001496 if (level == 0) {
1497 return generic_bin_search(eb,
1498 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001499 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001500 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001501 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001502 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001503 return generic_bin_search(eb,
1504 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001505 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001506 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001507 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001508 }
1509 return -1;
1510}
1511
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001512int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1513 int level, int *slot)
1514{
1515 return bin_search(eb, key, level, slot);
1516}
1517
Yan, Zhengf0486c62010-05-16 10:46:25 -04001518static void root_add_used(struct btrfs_root *root, u32 size)
1519{
1520 spin_lock(&root->accounting_lock);
1521 btrfs_set_root_used(&root->root_item,
1522 btrfs_root_used(&root->root_item) + size);
1523 spin_unlock(&root->accounting_lock);
1524}
1525
1526static void root_sub_used(struct btrfs_root *root, u32 size)
1527{
1528 spin_lock(&root->accounting_lock);
1529 btrfs_set_root_used(&root->root_item,
1530 btrfs_root_used(&root->root_item) - size);
1531 spin_unlock(&root->accounting_lock);
1532}
1533
Chris Masond352ac62008-09-29 15:18:18 -04001534/* given a node and slot number, this reads the blocks it points to. The
1535 * extent buffer is returned with a reference taken (but unlocked).
1536 * NULL is returned on error.
1537 */
Chris Masone02119d2008-09-05 16:13:11 -04001538static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001539 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001540{
Chris Masonca7a79a2008-05-12 12:59:19 -04001541 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001542 if (slot < 0)
1543 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001544 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001545 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001546
1547 BUG_ON(level == 0);
1548
Chris Masondb945352007-10-15 16:15:53 -04001549 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001550 btrfs_level_size(root, level - 1),
1551 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001552}
1553
Chris Masond352ac62008-09-29 15:18:18 -04001554/*
1555 * node level balancing, used to make sure nodes are in proper order for
1556 * item deletion. We balance from the top down, so we have to make sure
1557 * that a deletion won't leave an node completely empty later on.
1558 */
Chris Masone02119d2008-09-05 16:13:11 -04001559static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001560 struct btrfs_root *root,
1561 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001562{
Chris Mason5f39d392007-10-15 16:14:19 -04001563 struct extent_buffer *right = NULL;
1564 struct extent_buffer *mid;
1565 struct extent_buffer *left = NULL;
1566 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001567 int ret = 0;
1568 int wret;
1569 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001570 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001571 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001572
1573 if (level == 0)
1574 return 0;
1575
Chris Mason5f39d392007-10-15 16:14:19 -04001576 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001577
Chris Masonbd681512011-07-16 15:23:14 -04001578 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1579 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001580 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1581
Chris Mason1d4f8a02007-03-13 09:28:32 -04001582 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001583
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 Masonbb803952007-03-01 12:04:21 -05001588
Chris Mason40689472007-03-17 14:29:23 -04001589 /*
1590 * deal with the case where there is only one pointer in the root
1591 * by promoting the node below to a root
1592 */
Chris Mason5f39d392007-10-15 16:14:19 -04001593 if (!parent) {
1594 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001595
Chris Mason5f39d392007-10-15 16:14:19 -04001596 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001597 return 0;
1598
1599 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001600 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001601 if (!child) {
1602 ret = -EROFS;
1603 btrfs_std_error(root->fs_info, ret);
1604 goto enospc;
1605 }
1606
Chris Mason925baed2008-06-25 16:01:30 -04001607 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001608 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001609 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001610 if (ret) {
1611 btrfs_tree_unlock(child);
1612 free_extent_buffer(child);
1613 goto enospc;
1614 }
Yan2f375ab2008-02-01 14:58:07 -05001615
Jan Schmidtf2304752012-05-26 11:43:17 +02001616 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001617 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001618
Chris Mason0b86a832008-03-24 15:01:56 -04001619 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001620 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001621
Chris Mason925baed2008-06-25 16:01:30 -04001622 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001623 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001624 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001625 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001626 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001627 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001628
1629 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001630 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001631 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001632 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001633 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001634 }
Chris Mason5f39d392007-10-15 16:14:19 -04001635 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001636 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001637 return 0;
1638
Andi Kleen559af822010-10-29 15:14:37 -04001639 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001640
Chris Mason5f39d392007-10-15 16:14:19 -04001641 left = read_node_slot(root, parent, pslot - 1);
1642 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001643 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001644 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001645 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001646 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001647 if (wret) {
1648 ret = wret;
1649 goto enospc;
1650 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001651 }
Chris Mason5f39d392007-10-15 16:14:19 -04001652 right = read_node_slot(root, parent, pslot + 1);
1653 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001654 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001655 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001656 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001657 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001658 if (wret) {
1659 ret = wret;
1660 goto enospc;
1661 }
1662 }
1663
1664 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001665 if (left) {
1666 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001667 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001668 if (wret < 0)
1669 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001670 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001671 }
Chris Mason79f95c82007-03-01 15:16:26 -05001672
1673 /*
1674 * then try to empty the right most buffer into the middle
1675 */
Chris Mason5f39d392007-10-15 16:14:19 -04001676 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001677 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001678 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001679 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001680 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001681 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001682 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001683 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001684 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001685 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001686 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001687 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001688 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001689 struct btrfs_disk_key right_key;
1690 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001691 tree_mod_log_set_node_key(root->fs_info, parent,
1692 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001693 btrfs_set_node_key(parent, &right_key, pslot + 1);
1694 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001695 }
1696 }
Chris Mason5f39d392007-10-15 16:14:19 -04001697 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001698 /*
1699 * we're not allowed to leave a node with one item in the
1700 * tree during a delete. A deletion from lower in the tree
1701 * could try to delete the only pointer in this node.
1702 * So, pull some keys from the left.
1703 * There has to be a left pointer at this point because
1704 * otherwise we would have pulled some pointers from the
1705 * right
1706 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001707 if (!left) {
1708 ret = -EROFS;
1709 btrfs_std_error(root->fs_info, ret);
1710 goto enospc;
1711 }
Chris Mason5f39d392007-10-15 16:14:19 -04001712 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001713 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001714 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001715 goto enospc;
1716 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001717 if (wret == 1) {
1718 wret = push_node_left(trans, root, left, mid, 1);
1719 if (wret < 0)
1720 ret = wret;
1721 }
Chris Mason79f95c82007-03-01 15:16:26 -05001722 BUG_ON(wret == 1);
1723 }
Chris Mason5f39d392007-10-15 16:14:19 -04001724 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001725 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001726 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001727 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001728 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001729 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001730 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001731 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001732 } else {
1733 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001734 struct btrfs_disk_key mid_key;
1735 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001736 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1737 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001738 btrfs_set_node_key(parent, &mid_key, pslot);
1739 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001740 }
Chris Masonbb803952007-03-01 12:04:21 -05001741
Chris Mason79f95c82007-03-01 15:16:26 -05001742 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001743 if (left) {
1744 if (btrfs_header_nritems(left) > orig_slot) {
1745 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001746 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001747 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001748 path->slots[level + 1] -= 1;
1749 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001750 if (mid) {
1751 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001752 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001753 }
Chris Masonbb803952007-03-01 12:04:21 -05001754 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001755 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001756 path->slots[level] = orig_slot;
1757 }
1758 }
Chris Mason79f95c82007-03-01 15:16:26 -05001759 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001760 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001761 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001762 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001763enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001764 if (right) {
1765 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001766 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001767 }
1768 if (left) {
1769 if (path->nodes[level] != left)
1770 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001771 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001772 }
Chris Masonbb803952007-03-01 12:04:21 -05001773 return ret;
1774}
1775
Chris Masond352ac62008-09-29 15:18:18 -04001776/* Node balancing for insertion. Here we only split or push nodes around
1777 * when they are completely full. This is also done top down, so we
1778 * have to be pessimistic.
1779 */
Chris Masond3977122009-01-05 21:25:51 -05001780static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001781 struct btrfs_root *root,
1782 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001783{
Chris Mason5f39d392007-10-15 16:14:19 -04001784 struct extent_buffer *right = NULL;
1785 struct extent_buffer *mid;
1786 struct extent_buffer *left = NULL;
1787 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001788 int ret = 0;
1789 int wret;
1790 int pslot;
1791 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001792
1793 if (level == 0)
1794 return 1;
1795
Chris Mason5f39d392007-10-15 16:14:19 -04001796 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001797 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001798
Li Zefana05a9bb2011-09-06 16:55:34 +08001799 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001800 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001801 pslot = path->slots[level + 1];
1802 }
Chris Masone66f7092007-04-20 13:16:02 -04001803
Chris Mason5f39d392007-10-15 16:14:19 -04001804 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001805 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001806
Chris Mason5f39d392007-10-15 16:14:19 -04001807 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001808
1809 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001810 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001811 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001812
1813 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001814 btrfs_set_lock_blocking(left);
1815
Chris Mason5f39d392007-10-15 16:14:19 -04001816 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001817 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1818 wret = 1;
1819 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001820 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001821 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001822 if (ret)
1823 wret = 1;
1824 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001825 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001826 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001827 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001828 }
Chris Masone66f7092007-04-20 13:16:02 -04001829 if (wret < 0)
1830 ret = wret;
1831 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001832 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001833 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001834 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001835 tree_mod_log_set_node_key(root->fs_info, parent,
1836 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001837 btrfs_set_node_key(parent, &disk_key, pslot);
1838 btrfs_mark_buffer_dirty(parent);
1839 if (btrfs_header_nritems(left) > orig_slot) {
1840 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001841 path->slots[level + 1] -= 1;
1842 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001843 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001844 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001845 } else {
1846 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001847 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001848 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001849 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001850 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001851 }
Chris Masone66f7092007-04-20 13:16:02 -04001852 return 0;
1853 }
Chris Mason925baed2008-06-25 16:01:30 -04001854 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001855 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001856 }
Chris Mason925baed2008-06-25 16:01:30 -04001857 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001858
1859 /*
1860 * then try to empty the right most buffer into the middle
1861 */
Chris Mason5f39d392007-10-15 16:14:19 -04001862 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001863 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001864
Chris Mason925baed2008-06-25 16:01:30 -04001865 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001866 btrfs_set_lock_blocking(right);
1867
Chris Mason5f39d392007-10-15 16:14:19 -04001868 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001869 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1870 wret = 1;
1871 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001872 ret = btrfs_cow_block(trans, root, right,
1873 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001874 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001875 if (ret)
1876 wret = 1;
1877 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001878 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001879 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001880 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001881 }
Chris Masone66f7092007-04-20 13:16:02 -04001882 if (wret < 0)
1883 ret = wret;
1884 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001885 struct btrfs_disk_key disk_key;
1886
1887 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001888 tree_mod_log_set_node_key(root->fs_info, parent,
1889 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001890 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1891 btrfs_mark_buffer_dirty(parent);
1892
1893 if (btrfs_header_nritems(mid) <= orig_slot) {
1894 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001895 path->slots[level + 1] += 1;
1896 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001897 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001898 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001899 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001900 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001901 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001902 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001903 }
Chris Masone66f7092007-04-20 13:16:02 -04001904 return 0;
1905 }
Chris Mason925baed2008-06-25 16:01:30 -04001906 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001907 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001908 }
Chris Masone66f7092007-04-20 13:16:02 -04001909 return 1;
1910}
1911
Chris Mason74123bd2007-02-02 11:05:29 -05001912/*
Chris Masond352ac62008-09-29 15:18:18 -04001913 * readahead one full node of leaves, finding things that are close
1914 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001915 */
Chris Masonc8c42862009-04-03 10:14:18 -04001916static void reada_for_search(struct btrfs_root *root,
1917 struct btrfs_path *path,
1918 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001919{
Chris Mason5f39d392007-10-15 16:14:19 -04001920 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001921 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001922 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001923 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001924 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001925 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001926 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001927 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001928 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001929 u32 nr;
1930 u32 blocksize;
1931 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001932
Chris Masona6b6e752007-10-15 16:22:39 -04001933 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001934 return;
1935
Chris Mason6702ed42007-08-07 16:15:09 -04001936 if (!path->nodes[level])
1937 return;
1938
Chris Mason5f39d392007-10-15 16:14:19 -04001939 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001940
Chris Mason3c69fae2007-08-07 15:52:22 -04001941 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001942 blocksize = btrfs_level_size(root, level - 1);
1943 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001944 if (eb) {
1945 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001946 return;
1947 }
1948
Chris Masona7175312009-01-22 09:23:10 -05001949 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001950
Chris Mason5f39d392007-10-15 16:14:19 -04001951 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001952 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001953
Chris Masond3977122009-01-05 21:25:51 -05001954 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001955 if (direction < 0) {
1956 if (nr == 0)
1957 break;
1958 nr--;
1959 } else if (direction > 0) {
1960 nr++;
1961 if (nr >= nritems)
1962 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001963 }
Chris Mason01f46652007-12-21 16:24:26 -05001964 if (path->reada < 0 && objectid) {
1965 btrfs_node_key(node, &disk_key, nr);
1966 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1967 break;
1968 }
Chris Mason6b800532007-10-15 16:17:34 -04001969 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001970 if ((search <= target && target - search <= 65536) ||
1971 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001972 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001973 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001974 nread += blocksize;
1975 }
1976 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001977 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001978 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001979 }
1980}
Chris Mason925baed2008-06-25 16:01:30 -04001981
Chris Masond352ac62008-09-29 15:18:18 -04001982/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001983 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1984 * cache
1985 */
1986static noinline int reada_for_balance(struct btrfs_root *root,
1987 struct btrfs_path *path, int level)
1988{
1989 int slot;
1990 int nritems;
1991 struct extent_buffer *parent;
1992 struct extent_buffer *eb;
1993 u64 gen;
1994 u64 block1 = 0;
1995 u64 block2 = 0;
1996 int ret = 0;
1997 int blocksize;
1998
Chris Mason8c594ea2009-04-20 15:50:10 -04001999 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002000 if (!parent)
2001 return 0;
2002
2003 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002004 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002005 blocksize = btrfs_level_size(root, level);
2006
2007 if (slot > 0) {
2008 block1 = btrfs_node_blockptr(parent, slot - 1);
2009 gen = btrfs_node_ptr_generation(parent, slot - 1);
2010 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002011 /*
2012 * if we get -eagain from btrfs_buffer_uptodate, we
2013 * don't want to return eagain here. That will loop
2014 * forever
2015 */
2016 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002017 block1 = 0;
2018 free_extent_buffer(eb);
2019 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002020 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002021 block2 = btrfs_node_blockptr(parent, slot + 1);
2022 gen = btrfs_node_ptr_generation(parent, slot + 1);
2023 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002024 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002025 block2 = 0;
2026 free_extent_buffer(eb);
2027 }
2028 if (block1 || block2) {
2029 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002030
2031 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002032 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002033
2034 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002035 if (block1)
2036 readahead_tree_block(root, block1, blocksize, 0);
2037 if (block2)
2038 readahead_tree_block(root, block2, blocksize, 0);
2039
2040 if (block1) {
2041 eb = read_tree_block(root, block1, blocksize, 0);
2042 free_extent_buffer(eb);
2043 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002044 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002045 eb = read_tree_block(root, block2, blocksize, 0);
2046 free_extent_buffer(eb);
2047 }
2048 }
2049 return ret;
2050}
2051
2052
2053/*
Chris Masond3977122009-01-05 21:25:51 -05002054 * when we walk down the tree, it is usually safe to unlock the higher layers
2055 * in the tree. The exceptions are when our path goes through slot 0, because
2056 * operations on the tree might require changing key pointers higher up in the
2057 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002058 *
Chris Masond3977122009-01-05 21:25:51 -05002059 * callers might also have set path->keep_locks, which tells this code to keep
2060 * the lock if the path points to the last slot in the block. This is part of
2061 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002062 *
Chris Masond3977122009-01-05 21:25:51 -05002063 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2064 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002065 */
Chris Masone02119d2008-09-05 16:13:11 -04002066static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002067 int lowest_unlock, int min_write_lock_level,
2068 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002069{
2070 int i;
2071 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002072 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002073 struct extent_buffer *t;
2074
2075 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2076 if (!path->nodes[i])
2077 break;
2078 if (!path->locks[i])
2079 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002080 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002081 skip_level = i + 1;
2082 continue;
2083 }
Chris Mason051e1b92008-06-25 16:01:30 -04002084 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002085 u32 nritems;
2086 t = path->nodes[i];
2087 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002088 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002089 skip_level = i + 1;
2090 continue;
2091 }
2092 }
Chris Mason051e1b92008-06-25 16:01:30 -04002093 if (skip_level < i && i >= lowest_unlock)
2094 no_skips = 1;
2095
Chris Mason925baed2008-06-25 16:01:30 -04002096 t = path->nodes[i];
2097 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002098 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002099 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002100 if (write_lock_level &&
2101 i > min_write_lock_level &&
2102 i <= *write_lock_level) {
2103 *write_lock_level = i - 1;
2104 }
Chris Mason925baed2008-06-25 16:01:30 -04002105 }
2106 }
2107}
2108
Chris Mason3c69fae2007-08-07 15:52:22 -04002109/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002110 * This releases any locks held in the path starting at level and
2111 * going all the way up to the root.
2112 *
2113 * btrfs_search_slot will keep the lock held on higher nodes in a few
2114 * corner cases, such as COW of the block at slot zero in the node. This
2115 * ignores those rules, and it should only be called when there are no
2116 * more updates to be done higher up in the tree.
2117 */
2118noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2119{
2120 int i;
2121
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002122 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002123 return;
2124
2125 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2126 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002127 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002128 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002129 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002130 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002131 path->locks[i] = 0;
2132 }
2133}
2134
2135/*
Chris Masonc8c42862009-04-03 10:14:18 -04002136 * helper function for btrfs_search_slot. The goal is to find a block
2137 * in cache without setting the path to blocking. If we find the block
2138 * we return zero and the path is unchanged.
2139 *
2140 * If we can't find the block, we set the path blocking and do some
2141 * reada. -EAGAIN is returned and the search must be repeated.
2142 */
2143static int
2144read_block_for_search(struct btrfs_trans_handle *trans,
2145 struct btrfs_root *root, struct btrfs_path *p,
2146 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002147 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002148{
2149 u64 blocknr;
2150 u64 gen;
2151 u32 blocksize;
2152 struct extent_buffer *b = *eb_ret;
2153 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002154 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002155
2156 blocknr = btrfs_node_blockptr(b, slot);
2157 gen = btrfs_node_ptr_generation(b, slot);
2158 blocksize = btrfs_level_size(root, level - 1);
2159
2160 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002161 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002162 /* first we do an atomic uptodate check */
2163 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2164 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002165 /*
2166 * we found an up to date block without
2167 * sleeping, return
2168 * right away
2169 */
2170 *eb_ret = tmp;
2171 return 0;
2172 }
2173 /* the pages were up to date, but we failed
2174 * the generation number check. Do a full
2175 * read for the generation number that is correct.
2176 * We must do this without dropping locks so
2177 * we can trust our generation number
2178 */
2179 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002180 btrfs_set_path_blocking(p);
2181
Chris Masonb9fab912012-05-06 07:23:47 -04002182 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002183 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002184 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002185 *eb_ret = tmp;
2186 return 0;
2187 }
2188 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002189 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002190 return -EIO;
2191 }
Chris Masonc8c42862009-04-03 10:14:18 -04002192 }
2193
2194 /*
2195 * reduce lock contention at high levels
2196 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002197 * we read. Don't release the lock on the current
2198 * level because we need to walk this node to figure
2199 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002200 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002201 btrfs_unlock_up_safe(p, level + 1);
2202 btrfs_set_path_blocking(p);
2203
Chris Masoncb449212010-10-24 11:01:27 -04002204 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002205 if (p->reada)
2206 reada_for_search(root, p, level, slot, key->objectid);
2207
David Sterbab3b4aa72011-04-21 01:20:15 +02002208 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002209
2210 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002211 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002212 if (tmp) {
2213 /*
2214 * If the read above didn't mark this buffer up to date,
2215 * it will never end up being up to date. Set ret to EIO now
2216 * and give up so that our caller doesn't loop forever
2217 * on our EAGAINs.
2218 */
Chris Masonb9fab912012-05-06 07:23:47 -04002219 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002220 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002221 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002222 }
2223 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002224}
2225
2226/*
2227 * helper function for btrfs_search_slot. This does all of the checks
2228 * for node-level blocks and does any balancing required based on
2229 * the ins_len.
2230 *
2231 * If no extra work was required, zero is returned. If we had to
2232 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2233 * start over
2234 */
2235static int
2236setup_nodes_for_search(struct btrfs_trans_handle *trans,
2237 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002238 struct extent_buffer *b, int level, int ins_len,
2239 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002240{
2241 int ret;
2242 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2243 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2244 int sret;
2245
Chris Masonbd681512011-07-16 15:23:14 -04002246 if (*write_lock_level < level + 1) {
2247 *write_lock_level = level + 1;
2248 btrfs_release_path(p);
2249 goto again;
2250 }
2251
Chris Masonc8c42862009-04-03 10:14:18 -04002252 sret = reada_for_balance(root, p, level);
2253 if (sret)
2254 goto again;
2255
2256 btrfs_set_path_blocking(p);
2257 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002258 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002259
2260 BUG_ON(sret > 0);
2261 if (sret) {
2262 ret = sret;
2263 goto done;
2264 }
2265 b = p->nodes[level];
2266 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002267 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002268 int sret;
2269
Chris Masonbd681512011-07-16 15:23:14 -04002270 if (*write_lock_level < level + 1) {
2271 *write_lock_level = level + 1;
2272 btrfs_release_path(p);
2273 goto again;
2274 }
2275
Chris Masonc8c42862009-04-03 10:14:18 -04002276 sret = reada_for_balance(root, p, level);
2277 if (sret)
2278 goto again;
2279
2280 btrfs_set_path_blocking(p);
2281 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002282 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002283
2284 if (sret) {
2285 ret = sret;
2286 goto done;
2287 }
2288 b = p->nodes[level];
2289 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002290 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002291 goto again;
2292 }
2293 BUG_ON(btrfs_header_nritems(b) == 1);
2294 }
2295 return 0;
2296
2297again:
2298 ret = -EAGAIN;
2299done:
2300 return ret;
2301}
2302
2303/*
Chris Mason74123bd2007-02-02 11:05:29 -05002304 * look for key in the tree. path is filled in with nodes along the way
2305 * if key is found, we return zero and you can find the item in the leaf
2306 * level of the path (level 0)
2307 *
2308 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002309 * be inserted, and 1 is returned. If there are other errors during the
2310 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002311 *
2312 * if ins_len > 0, nodes and leaves will be split as we walk down the
2313 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2314 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002315 */
Chris Masone089f052007-03-16 16:20:31 -04002316int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2317 *root, struct btrfs_key *key, struct btrfs_path *p, int
2318 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002319{
Chris Mason5f39d392007-10-15 16:14:19 -04002320 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002321 int slot;
2322 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002323 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002324 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002325 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002326 int root_lock;
2327 /* everything at write_lock_level or lower must be write locked */
2328 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002329 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002330 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002331
Chris Mason6702ed42007-08-07 16:15:09 -04002332 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002333 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002334 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002335
Chris Masonbd681512011-07-16 15:23:14 -04002336 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002337 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002338
Chris Masonbd681512011-07-16 15:23:14 -04002339 /* when we are removing items, we might have to go up to level
2340 * two as we update tree pointers Make sure we keep write
2341 * for those levels as well
2342 */
2343 write_lock_level = 2;
2344 } else if (ins_len > 0) {
2345 /*
2346 * for inserting items, make sure we have a write lock on
2347 * level 1 so we can update keys
2348 */
2349 write_lock_level = 1;
2350 }
2351
2352 if (!cow)
2353 write_lock_level = -1;
2354
2355 if (cow && (p->keep_locks || p->lowest_level))
2356 write_lock_level = BTRFS_MAX_LEVEL;
2357
Chris Masonf7c79f32012-03-19 15:54:38 -04002358 min_write_lock_level = write_lock_level;
2359
Chris Masonbb803952007-03-01 12:04:21 -05002360again:
Chris Masonbd681512011-07-16 15:23:14 -04002361 /*
2362 * we try very hard to do read locks on the root
2363 */
2364 root_lock = BTRFS_READ_LOCK;
2365 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002366 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002367 /*
2368 * the commit roots are read only
2369 * so we always do read locks
2370 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002371 b = root->commit_root;
2372 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002373 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002374 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002375 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002376 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002377 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002379 level = btrfs_header_level(b);
2380 } else {
2381 /* we don't know the level of the root node
2382 * until we actually have it read locked
2383 */
2384 b = btrfs_read_lock_root_node(root);
2385 level = btrfs_header_level(b);
2386 if (level <= write_lock_level) {
2387 /* whoops, must trade for write lock */
2388 btrfs_tree_read_unlock(b);
2389 free_extent_buffer(b);
2390 b = btrfs_lock_root_node(root);
2391 root_lock = BTRFS_WRITE_LOCK;
2392
2393 /* the level might have changed, check again */
2394 level = btrfs_header_level(b);
2395 }
2396 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002397 }
Chris Masonbd681512011-07-16 15:23:14 -04002398 p->nodes[level] = b;
2399 if (!p->skip_locking)
2400 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002401
Chris Masoneb60cea2007-02-02 09:18:22 -05002402 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002403 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002404
2405 /*
2406 * setup the path here so we can release it under lock
2407 * contention with the cow code
2408 */
Chris Mason02217ed2007-03-02 16:08:05 -05002409 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002410 /*
2411 * if we don't really need to cow this block
2412 * then we don't want to set the path blocking,
2413 * so we test it here
2414 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002415 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002416 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002417
Chris Masonb4ce94d2009-02-04 09:25:08 -05002418 btrfs_set_path_blocking(p);
2419
Chris Masonbd681512011-07-16 15:23:14 -04002420 /*
2421 * must have write locks on this node and the
2422 * parent
2423 */
2424 if (level + 1 > write_lock_level) {
2425 write_lock_level = level + 1;
2426 btrfs_release_path(p);
2427 goto again;
2428 }
2429
Yan Zheng33c66f42009-07-22 09:59:00 -04002430 err = btrfs_cow_block(trans, root, b,
2431 p->nodes[level + 1],
2432 p->slots[level + 1], &b);
2433 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002434 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002435 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002436 }
Chris Mason02217ed2007-03-02 16:08:05 -05002437 }
Chris Mason65b51a02008-08-01 15:11:20 -04002438cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002439 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002440
Chris Masoneb60cea2007-02-02 09:18:22 -05002441 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002442 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002443
2444 /*
2445 * we have a lock on b and as long as we aren't changing
2446 * the tree, there is no way to for the items in b to change.
2447 * It is safe to drop the lock on our parent before we
2448 * go through the expensive btree search on b.
2449 *
2450 * If cow is true, then we might be changing slot zero,
2451 * which may require changing the parent. So, we can't
2452 * drop the lock until after we know which slot we're
2453 * operating on.
2454 */
2455 if (!cow)
2456 btrfs_unlock_up_safe(p, level + 1);
2457
Chris Mason5f39d392007-10-15 16:14:19 -04002458 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002459
Chris Mason5f39d392007-10-15 16:14:19 -04002460 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002461 int dec = 0;
2462 if (ret && slot > 0) {
2463 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002464 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002465 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002466 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002467 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002468 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002469 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002470 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002471 if (err) {
2472 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002473 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002474 }
Chris Masonc8c42862009-04-03 10:14:18 -04002475 b = p->nodes[level];
2476 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002477
Chris Masonbd681512011-07-16 15:23:14 -04002478 /*
2479 * slot 0 is special, if we change the key
2480 * we have to update the parent pointer
2481 * which means we must have a write lock
2482 * on the parent
2483 */
2484 if (slot == 0 && cow &&
2485 write_lock_level < level + 1) {
2486 write_lock_level = level + 1;
2487 btrfs_release_path(p);
2488 goto again;
2489 }
2490
Chris Masonf7c79f32012-03-19 15:54:38 -04002491 unlock_up(p, level, lowest_unlock,
2492 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002493
Chris Mason925baed2008-06-25 16:01:30 -04002494 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002495 if (dec)
2496 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002497 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002498 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002499
Yan Zheng33c66f42009-07-22 09:59:00 -04002500 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002501 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002502 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002503 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002504 if (err) {
2505 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002506 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002507 }
Chris Mason76a05b32009-05-14 13:24:30 -04002508
Chris Masonb4ce94d2009-02-04 09:25:08 -05002509 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002510 level = btrfs_header_level(b);
2511 if (level <= write_lock_level) {
2512 err = btrfs_try_tree_write_lock(b);
2513 if (!err) {
2514 btrfs_set_path_blocking(p);
2515 btrfs_tree_lock(b);
2516 btrfs_clear_path_blocking(p, b,
2517 BTRFS_WRITE_LOCK);
2518 }
2519 p->locks[level] = BTRFS_WRITE_LOCK;
2520 } else {
2521 err = btrfs_try_tree_read_lock(b);
2522 if (!err) {
2523 btrfs_set_path_blocking(p);
2524 btrfs_tree_read_lock(b);
2525 btrfs_clear_path_blocking(p, b,
2526 BTRFS_READ_LOCK);
2527 }
2528 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002529 }
Chris Masonbd681512011-07-16 15:23:14 -04002530 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002531 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002532 } else {
2533 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002534 if (ins_len > 0 &&
2535 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002536 if (write_lock_level < 1) {
2537 write_lock_level = 1;
2538 btrfs_release_path(p);
2539 goto again;
2540 }
2541
Chris Masonb4ce94d2009-02-04 09:25:08 -05002542 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002543 err = split_leaf(trans, root, key,
2544 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002545 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002546
Yan Zheng33c66f42009-07-22 09:59:00 -04002547 BUG_ON(err > 0);
2548 if (err) {
2549 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002550 goto done;
2551 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002552 }
Chris Mason459931e2008-12-10 09:10:46 -05002553 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002554 unlock_up(p, level, lowest_unlock,
2555 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002556 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002557 }
2558 }
Chris Mason65b51a02008-08-01 15:11:20 -04002559 ret = 1;
2560done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002561 /*
2562 * we don't really know what they plan on doing with the path
2563 * from here on, so for now just mark it as blocking
2564 */
Chris Masonb9473432009-03-13 11:00:37 -04002565 if (!p->leave_spinning)
2566 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002567 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002568 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002569 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002570}
2571
Chris Mason74123bd2007-02-02 11:05:29 -05002572/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002573 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2574 * current state of the tree together with the operations recorded in the tree
2575 * modification log to search for the key in a previous version of this tree, as
2576 * denoted by the time_seq parameter.
2577 *
2578 * Naturally, there is no support for insert, delete or cow operations.
2579 *
2580 * The resulting path and return value will be set up as if we called
2581 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2582 */
2583int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2584 struct btrfs_path *p, u64 time_seq)
2585{
2586 struct extent_buffer *b;
2587 int slot;
2588 int ret;
2589 int err;
2590 int level;
2591 int lowest_unlock = 1;
2592 u8 lowest_level = 0;
2593
2594 lowest_level = p->lowest_level;
2595 WARN_ON(p->nodes[0] != NULL);
2596
2597 if (p->search_commit_root) {
2598 BUG_ON(time_seq);
2599 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2600 }
2601
2602again:
2603 level = 0;
2604 b = get_old_root(root, time_seq);
2605 extent_buffer_get(b);
2606 level = btrfs_header_level(b);
2607 btrfs_tree_read_lock(b);
2608 p->locks[level] = BTRFS_READ_LOCK;
2609
2610 while (b) {
2611 level = btrfs_header_level(b);
2612 p->nodes[level] = b;
2613 btrfs_clear_path_blocking(p, NULL, 0);
2614
2615 /*
2616 * we have a lock on b and as long as we aren't changing
2617 * the tree, there is no way to for the items in b to change.
2618 * It is safe to drop the lock on our parent before we
2619 * go through the expensive btree search on b.
2620 */
2621 btrfs_unlock_up_safe(p, level + 1);
2622
2623 ret = bin_search(b, key, level, &slot);
2624
2625 if (level != 0) {
2626 int dec = 0;
2627 if (ret && slot > 0) {
2628 dec = 1;
2629 slot -= 1;
2630 }
2631 p->slots[level] = slot;
2632 unlock_up(p, level, lowest_unlock, 0, NULL);
2633
2634 if (level == lowest_level) {
2635 if (dec)
2636 p->slots[level]++;
2637 goto done;
2638 }
2639
2640 err = read_block_for_search(NULL, root, p, &b, level,
2641 slot, key, time_seq);
2642 if (err == -EAGAIN)
2643 goto again;
2644 if (err) {
2645 ret = err;
2646 goto done;
2647 }
2648
2649 level = btrfs_header_level(b);
2650 err = btrfs_try_tree_read_lock(b);
2651 if (!err) {
2652 btrfs_set_path_blocking(p);
2653 btrfs_tree_read_lock(b);
2654 btrfs_clear_path_blocking(p, b,
2655 BTRFS_READ_LOCK);
2656 }
2657 p->locks[level] = BTRFS_READ_LOCK;
2658 p->nodes[level] = b;
2659 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2660 if (b != p->nodes[level]) {
2661 btrfs_tree_unlock_rw(p->nodes[level],
2662 p->locks[level]);
2663 p->locks[level] = 0;
2664 p->nodes[level] = b;
2665 }
2666 } else {
2667 p->slots[level] = slot;
2668 unlock_up(p, level, lowest_unlock, 0, NULL);
2669 goto done;
2670 }
2671 }
2672 ret = 1;
2673done:
2674 if (!p->leave_spinning)
2675 btrfs_set_path_blocking(p);
2676 if (ret < 0)
2677 btrfs_release_path(p);
2678
2679 return ret;
2680}
2681
2682/*
Chris Mason74123bd2007-02-02 11:05:29 -05002683 * adjust the pointers going up the tree, starting at level
2684 * making sure the right key of each node is points to 'key'.
2685 * This is used after shifting pointers to the left, so it stops
2686 * fixing up pointers when a given leaf/node is not in slot 0 of the
2687 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002688 *
Chris Mason74123bd2007-02-02 11:05:29 -05002689 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002690static void fixup_low_keys(struct btrfs_trans_handle *trans,
2691 struct btrfs_root *root, struct btrfs_path *path,
2692 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002693{
2694 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002695 struct extent_buffer *t;
2696
Chris Mason234b63a2007-03-13 10:46:10 -04002697 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002698 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002699 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002700 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002701 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002702 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002703 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002704 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002705 if (tslot != 0)
2706 break;
2707 }
2708}
2709
Chris Mason74123bd2007-02-02 11:05:29 -05002710/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002711 * update item key.
2712 *
2713 * This function isn't completely safe. It's the caller's responsibility
2714 * that the new key won't break the order
2715 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002716void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2717 struct btrfs_root *root, struct btrfs_path *path,
2718 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002719{
2720 struct btrfs_disk_key disk_key;
2721 struct extent_buffer *eb;
2722 int slot;
2723
2724 eb = path->nodes[0];
2725 slot = path->slots[0];
2726 if (slot > 0) {
2727 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002728 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002729 }
2730 if (slot < btrfs_header_nritems(eb) - 1) {
2731 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002732 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002733 }
2734
2735 btrfs_cpu_key_to_disk(&disk_key, new_key);
2736 btrfs_set_item_key(eb, &disk_key, slot);
2737 btrfs_mark_buffer_dirty(eb);
2738 if (slot == 0)
2739 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002740}
2741
2742/*
Chris Mason74123bd2007-02-02 11:05:29 -05002743 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002744 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002745 *
2746 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2747 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002748 */
Chris Mason98ed5172008-01-03 10:01:48 -05002749static int push_node_left(struct btrfs_trans_handle *trans,
2750 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002751 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002752{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002753 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002754 int src_nritems;
2755 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002756 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002757
Chris Mason5f39d392007-10-15 16:14:19 -04002758 src_nritems = btrfs_header_nritems(src);
2759 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002760 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002761 WARN_ON(btrfs_header_generation(src) != trans->transid);
2762 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002763
Chris Masonbce4eae2008-04-24 14:42:46 -04002764 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002765 return 1;
2766
Chris Masond3977122009-01-05 21:25:51 -05002767 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002768 return 1;
2769
Chris Masonbce4eae2008-04-24 14:42:46 -04002770 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002771 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002772 if (push_items < src_nritems) {
2773 /* leave at least 8 pointers in the node if
2774 * we aren't going to empty it
2775 */
2776 if (src_nritems - push_items < 8) {
2777 if (push_items <= 8)
2778 return 1;
2779 push_items -= 8;
2780 }
2781 }
2782 } else
2783 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002784
Jan Schmidtf2304752012-05-26 11:43:17 +02002785 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2786 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002787 copy_extent_buffer(dst, src,
2788 btrfs_node_key_ptr_offset(dst_nritems),
2789 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002790 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002791
Chris Masonbb803952007-03-01 12:04:21 -05002792 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002793 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2794 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002795 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2796 btrfs_node_key_ptr_offset(push_items),
2797 (src_nritems - push_items) *
2798 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002799 }
Chris Mason5f39d392007-10-15 16:14:19 -04002800 btrfs_set_header_nritems(src, src_nritems - push_items);
2801 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2802 btrfs_mark_buffer_dirty(src);
2803 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002804
Chris Masonbb803952007-03-01 12:04:21 -05002805 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002806}
2807
Chris Mason97571fd2007-02-24 13:39:08 -05002808/*
Chris Mason79f95c82007-03-01 15:16:26 -05002809 * try to push data from one node into the next node right in the
2810 * tree.
2811 *
2812 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2813 * error, and > 0 if there was no room in the right hand block.
2814 *
2815 * this will only push up to 1/2 the contents of the left node over
2816 */
Chris Mason5f39d392007-10-15 16:14:19 -04002817static int balance_node_right(struct btrfs_trans_handle *trans,
2818 struct btrfs_root *root,
2819 struct extent_buffer *dst,
2820 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002821{
Chris Mason79f95c82007-03-01 15:16:26 -05002822 int push_items = 0;
2823 int max_push;
2824 int src_nritems;
2825 int dst_nritems;
2826 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002827
Chris Mason7bb86312007-12-11 09:25:06 -05002828 WARN_ON(btrfs_header_generation(src) != trans->transid);
2829 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2830
Chris Mason5f39d392007-10-15 16:14:19 -04002831 src_nritems = btrfs_header_nritems(src);
2832 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002833 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002834 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002835 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002836
Chris Masond3977122009-01-05 21:25:51 -05002837 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002838 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002839
2840 max_push = src_nritems / 2 + 1;
2841 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002842 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002843 return 1;
Yan252c38f2007-08-29 09:11:44 -04002844
Chris Mason79f95c82007-03-01 15:16:26 -05002845 if (max_push < push_items)
2846 push_items = max_push;
2847
Jan Schmidtf2304752012-05-26 11:43:17 +02002848 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002849 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2850 btrfs_node_key_ptr_offset(0),
2851 (dst_nritems) *
2852 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002853
Jan Schmidtf2304752012-05-26 11:43:17 +02002854 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2855 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002856 copy_extent_buffer(dst, src,
2857 btrfs_node_key_ptr_offset(0),
2858 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002859 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002860
Chris Mason5f39d392007-10-15 16:14:19 -04002861 btrfs_set_header_nritems(src, src_nritems - push_items);
2862 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002863
Chris Mason5f39d392007-10-15 16:14:19 -04002864 btrfs_mark_buffer_dirty(src);
2865 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002866
Chris Mason79f95c82007-03-01 15:16:26 -05002867 return ret;
2868}
2869
2870/*
Chris Mason97571fd2007-02-24 13:39:08 -05002871 * helper function to insert a new root level in the tree.
2872 * A new node is allocated, and a single item is inserted to
2873 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002874 *
2875 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002876 */
Chris Masond3977122009-01-05 21:25:51 -05002877static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002878 struct btrfs_root *root,
2879 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002880{
Chris Mason7bb86312007-12-11 09:25:06 -05002881 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002882 struct extent_buffer *lower;
2883 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002884 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002885 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002886
2887 BUG_ON(path->nodes[level]);
2888 BUG_ON(path->nodes[level-1] != root->node);
2889
Chris Mason7bb86312007-12-11 09:25:06 -05002890 lower = path->nodes[level-1];
2891 if (level == 1)
2892 btrfs_item_key(lower, &lower_key, 0);
2893 else
2894 btrfs_node_key(lower, &lower_key, 0);
2895
Zheng Yan31840ae2008-09-23 13:14:14 -04002896 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002897 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002898 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002899 if (IS_ERR(c))
2900 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002901
Yan, Zhengf0486c62010-05-16 10:46:25 -04002902 root_add_used(root, root->nodesize);
2903
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002904 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002905 btrfs_set_header_nritems(c, 1);
2906 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002907 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002908 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002909 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002910 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002911
Chris Mason5f39d392007-10-15 16:14:19 -04002912 write_extent_buffer(c, root->fs_info->fsid,
2913 (unsigned long)btrfs_header_fsid(c),
2914 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002915
2916 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2917 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2918 BTRFS_UUID_SIZE);
2919
Chris Mason5f39d392007-10-15 16:14:19 -04002920 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002921 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002922 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002923 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002924
2925 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002926
2927 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002928
Chris Mason925baed2008-06-25 16:01:30 -04002929 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002930 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002931 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002932
2933 /* the super has an extra ref to root->node */
2934 free_extent_buffer(old);
2935
Chris Mason0b86a832008-03-24 15:01:56 -04002936 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002937 extent_buffer_get(c);
2938 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002939 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002940 path->slots[level] = 0;
2941 return 0;
2942}
2943
Chris Mason74123bd2007-02-02 11:05:29 -05002944/*
2945 * worker function to insert a single pointer in a node.
2946 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002947 *
Chris Mason74123bd2007-02-02 11:05:29 -05002948 * slot and level indicate where you want the key to go, and
2949 * blocknr is the block the key points to.
2950 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002951static void insert_ptr(struct btrfs_trans_handle *trans,
2952 struct btrfs_root *root, struct btrfs_path *path,
2953 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002954 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002955{
Chris Mason5f39d392007-10-15 16:14:19 -04002956 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002957 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002958 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002959
2960 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002961 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002962 lower = path->nodes[level];
2963 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002964 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002965 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002966 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002967 if (tree_mod_log && level)
2968 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2969 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002970 memmove_extent_buffer(lower,
2971 btrfs_node_key_ptr_offset(slot + 1),
2972 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002973 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002974 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002975 if (tree_mod_log && level) {
2976 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2977 MOD_LOG_KEY_ADD);
2978 BUG_ON(ret < 0);
2979 }
Chris Mason5f39d392007-10-15 16:14:19 -04002980 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002981 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002982 WARN_ON(trans->transid == 0);
2983 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002984 btrfs_set_header_nritems(lower, nritems + 1);
2985 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002986}
2987
Chris Mason97571fd2007-02-24 13:39:08 -05002988/*
2989 * split the node at the specified level in path in two.
2990 * The path is corrected to point to the appropriate node after the split
2991 *
2992 * Before splitting this tries to make some room in the node by pushing
2993 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002994 *
2995 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05002996 */
Chris Masone02119d2008-09-05 16:13:11 -04002997static noinline int split_node(struct btrfs_trans_handle *trans,
2998 struct btrfs_root *root,
2999 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003000{
Chris Mason5f39d392007-10-15 16:14:19 -04003001 struct extent_buffer *c;
3002 struct extent_buffer *split;
3003 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003004 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003005 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003006 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003007
Chris Mason5f39d392007-10-15 16:14:19 -04003008 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003009 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003010 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003011 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003012 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003013 if (ret)
3014 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003015 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003016 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003017 c = path->nodes[level];
3018 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003019 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003020 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003021 if (ret < 0)
3022 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003023 }
Chris Masone66f7092007-04-20 13:16:02 -04003024
Chris Mason5f39d392007-10-15 16:14:19 -04003025 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026 mid = (c_nritems + 1) / 2;
3027 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003028
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003029 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003030 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003031 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003032 if (IS_ERR(split))
3033 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003034
Yan, Zhengf0486c62010-05-16 10:46:25 -04003035 root_add_used(root, root->nodesize);
3036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003037 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003038 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003039 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003040 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003041 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003042 btrfs_set_header_owner(split, root->root_key.objectid);
3043 write_extent_buffer(split, root->fs_info->fsid,
3044 (unsigned long)btrfs_header_fsid(split),
3045 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003046 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3047 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3048 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003049
Jan Schmidtf2304752012-05-26 11:43:17 +02003050 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003051 copy_extent_buffer(split, c,
3052 btrfs_node_key_ptr_offset(0),
3053 btrfs_node_key_ptr_offset(mid),
3054 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3055 btrfs_set_header_nritems(split, c_nritems - mid);
3056 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003057 ret = 0;
3058
Chris Mason5f39d392007-10-15 16:14:19 -04003059 btrfs_mark_buffer_dirty(c);
3060 btrfs_mark_buffer_dirty(split);
3061
Jeff Mahoney143bede2012-03-01 14:56:26 +01003062 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003063 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003064
Chris Mason5de08d72007-02-24 06:24:44 -05003065 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003066 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003067 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003068 free_extent_buffer(c);
3069 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003070 path->slots[level + 1] += 1;
3071 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003072 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003073 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003074 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003075 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003076}
3077
Chris Mason74123bd2007-02-02 11:05:29 -05003078/*
3079 * how many bytes are required to store the items in a leaf. start
3080 * and nr indicate which items in the leaf to check. This totals up the
3081 * space used both by the item structs and the item data
3082 */
Chris Mason5f39d392007-10-15 16:14:19 -04003083static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003084{
3085 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003086 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003087 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003088
3089 if (!nr)
3090 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003091 data_len = btrfs_item_end_nr(l, start);
3092 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003093 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003094 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003095 return data_len;
3096}
3097
Chris Mason74123bd2007-02-02 11:05:29 -05003098/*
Chris Masond4dbff92007-04-04 14:08:15 -04003099 * The space between the end of the leaf items and
3100 * the start of the leaf data. IOW, how much room
3101 * the leaf has left for both items and data
3102 */
Chris Masond3977122009-01-05 21:25:51 -05003103noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003104 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003105{
Chris Mason5f39d392007-10-15 16:14:19 -04003106 int nritems = btrfs_header_nritems(leaf);
3107 int ret;
3108 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3109 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003110 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3111 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003112 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003113 leaf_space_used(leaf, 0, nritems), nritems);
3114 }
3115 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003116}
3117
Chris Mason99d8f832010-07-07 10:51:48 -04003118/*
3119 * min slot controls the lowest index we're willing to push to the
3120 * right. We'll push up to and including min_slot, but no lower
3121 */
Chris Mason44871b12009-03-13 10:04:31 -04003122static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3123 struct btrfs_root *root,
3124 struct btrfs_path *path,
3125 int data_size, int empty,
3126 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003127 int free_space, u32 left_nritems,
3128 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003129{
Chris Mason5f39d392007-10-15 16:14:19 -04003130 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003131 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003132 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003133 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003134 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003135 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003136 int push_space = 0;
3137 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003138 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003139 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003140 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003141 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003142 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003143
Chris Masoncfed81a2012-03-03 07:40:03 -05003144 btrfs_init_map_token(&token);
3145
Chris Mason34a38212007-11-07 13:31:03 -05003146 if (empty)
3147 nr = 0;
3148 else
Chris Mason99d8f832010-07-07 10:51:48 -04003149 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003150
Zheng Yan31840ae2008-09-23 13:14:14 -04003151 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003152 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003153
Chris Mason44871b12009-03-13 10:04:31 -04003154 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003155 i = left_nritems - 1;
3156 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003157 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003158
Zheng Yan31840ae2008-09-23 13:14:14 -04003159 if (!empty && push_items > 0) {
3160 if (path->slots[0] > i)
3161 break;
3162 if (path->slots[0] == i) {
3163 int space = btrfs_leaf_free_space(root, left);
3164 if (space + push_space * 2 > free_space)
3165 break;
3166 }
3167 }
3168
Chris Mason00ec4c52007-02-24 12:47:20 -05003169 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003170 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003171
Chris Masondb945352007-10-15 16:15:53 -04003172 this_item_size = btrfs_item_size(left, item);
3173 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003174 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003175
Chris Mason00ec4c52007-02-24 12:47:20 -05003176 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003177 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003178 if (i == 0)
3179 break;
3180 i--;
Chris Masondb945352007-10-15 16:15:53 -04003181 }
Chris Mason5f39d392007-10-15 16:14:19 -04003182
Chris Mason925baed2008-06-25 16:01:30 -04003183 if (push_items == 0)
3184 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003185
Chris Mason34a38212007-11-07 13:31:03 -05003186 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003187 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003188
Chris Mason00ec4c52007-02-24 12:47:20 -05003189 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003190 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003191
Chris Mason5f39d392007-10-15 16:14:19 -04003192 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003193 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003194
Chris Mason00ec4c52007-02-24 12:47:20 -05003195 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003196 data_end = leaf_data_end(root, right);
3197 memmove_extent_buffer(right,
3198 btrfs_leaf_data(right) + data_end - push_space,
3199 btrfs_leaf_data(right) + data_end,
3200 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3201
Chris Mason00ec4c52007-02-24 12:47:20 -05003202 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003203 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003204 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3205 btrfs_leaf_data(left) + leaf_data_end(root, left),
3206 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003207
3208 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3209 btrfs_item_nr_offset(0),
3210 right_nritems * sizeof(struct btrfs_item));
3211
Chris Mason00ec4c52007-02-24 12:47:20 -05003212 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003213 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3214 btrfs_item_nr_offset(left_nritems - push_items),
3215 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003216
3217 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003218 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003219 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003220 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003221 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003222 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003223 push_space -= btrfs_token_item_size(right, item, &token);
3224 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003225 }
3226
Chris Mason7518a232007-03-12 12:01:18 -04003227 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003228 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003229
Chris Mason34a38212007-11-07 13:31:03 -05003230 if (left_nritems)
3231 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003232 else
3233 clean_tree_block(trans, root, left);
3234
Chris Mason5f39d392007-10-15 16:14:19 -04003235 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003236
Chris Mason5f39d392007-10-15 16:14:19 -04003237 btrfs_item_key(right, &disk_key, 0);
3238 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003239 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003240
Chris Mason00ec4c52007-02-24 12:47:20 -05003241 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003242 if (path->slots[0] >= left_nritems) {
3243 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003244 if (btrfs_header_nritems(path->nodes[0]) == 0)
3245 clean_tree_block(trans, root, path->nodes[0]);
3246 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003247 free_extent_buffer(path->nodes[0]);
3248 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003249 path->slots[1] += 1;
3250 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003251 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003252 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003253 }
3254 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003255
3256out_unlock:
3257 btrfs_tree_unlock(right);
3258 free_extent_buffer(right);
3259 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003260}
Chris Mason925baed2008-06-25 16:01:30 -04003261
Chris Mason00ec4c52007-02-24 12:47:20 -05003262/*
Chris Mason44871b12009-03-13 10:04:31 -04003263 * push some data in the path leaf to the right, trying to free up at
3264 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3265 *
3266 * returns 1 if the push failed because the other node didn't have enough
3267 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003268 *
3269 * this will push starting from min_slot to the end of the leaf. It won't
3270 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003271 */
3272static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003273 *root, struct btrfs_path *path,
3274 int min_data_size, int data_size,
3275 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003276{
3277 struct extent_buffer *left = path->nodes[0];
3278 struct extent_buffer *right;
3279 struct extent_buffer *upper;
3280 int slot;
3281 int free_space;
3282 u32 left_nritems;
3283 int ret;
3284
3285 if (!path->nodes[1])
3286 return 1;
3287
3288 slot = path->slots[1];
3289 upper = path->nodes[1];
3290 if (slot >= btrfs_header_nritems(upper) - 1)
3291 return 1;
3292
3293 btrfs_assert_tree_locked(path->nodes[1]);
3294
3295 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003296 if (right == NULL)
3297 return 1;
3298
Chris Mason44871b12009-03-13 10:04:31 -04003299 btrfs_tree_lock(right);
3300 btrfs_set_lock_blocking(right);
3301
3302 free_space = btrfs_leaf_free_space(root, right);
3303 if (free_space < data_size)
3304 goto out_unlock;
3305
3306 /* cow and double check */
3307 ret = btrfs_cow_block(trans, root, right, upper,
3308 slot + 1, &right);
3309 if (ret)
3310 goto out_unlock;
3311
3312 free_space = btrfs_leaf_free_space(root, right);
3313 if (free_space < data_size)
3314 goto out_unlock;
3315
3316 left_nritems = btrfs_header_nritems(left);
3317 if (left_nritems == 0)
3318 goto out_unlock;
3319
Chris Mason99d8f832010-07-07 10:51:48 -04003320 return __push_leaf_right(trans, root, path, min_data_size, empty,
3321 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003322out_unlock:
3323 btrfs_tree_unlock(right);
3324 free_extent_buffer(right);
3325 return 1;
3326}
3327
3328/*
Chris Mason74123bd2007-02-02 11:05:29 -05003329 * push some data in the path leaf to the left, trying to free up at
3330 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003331 *
3332 * max_slot can put a limit on how far into the leaf we'll push items. The
3333 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3334 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003335 */
Chris Mason44871b12009-03-13 10:04:31 -04003336static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3337 struct btrfs_root *root,
3338 struct btrfs_path *path, int data_size,
3339 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003340 int free_space, u32 right_nritems,
3341 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003342{
Chris Mason5f39d392007-10-15 16:14:19 -04003343 struct btrfs_disk_key disk_key;
3344 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003345 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003346 int push_space = 0;
3347 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003348 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003349 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003350 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003351 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003352 u32 this_item_size;
3353 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003354 struct btrfs_map_token token;
3355
3356 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003357
Chris Mason34a38212007-11-07 13:31:03 -05003358 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003359 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003360 else
Chris Mason99d8f832010-07-07 10:51:48 -04003361 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003362
3363 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003364 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003365
Zheng Yan31840ae2008-09-23 13:14:14 -04003366 if (!empty && push_items > 0) {
3367 if (path->slots[0] < i)
3368 break;
3369 if (path->slots[0] == i) {
3370 int space = btrfs_leaf_free_space(root, right);
3371 if (space + push_space * 2 > free_space)
3372 break;
3373 }
3374 }
3375
Chris Masonbe0e5c02007-01-26 15:51:26 -05003376 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003377 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003378
3379 this_item_size = btrfs_item_size(right, item);
3380 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003381 break;
Chris Masondb945352007-10-15 16:15:53 -04003382
Chris Masonbe0e5c02007-01-26 15:51:26 -05003383 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003384 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003385 }
Chris Masondb945352007-10-15 16:15:53 -04003386
Chris Masonbe0e5c02007-01-26 15:51:26 -05003387 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003388 ret = 1;
3389 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003390 }
Chris Mason34a38212007-11-07 13:31:03 -05003391 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003392 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003393
Chris Masonbe0e5c02007-01-26 15:51:26 -05003394 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003395 copy_extent_buffer(left, right,
3396 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3397 btrfs_item_nr_offset(0),
3398 push_items * sizeof(struct btrfs_item));
3399
Chris Mason123abc82007-03-14 14:14:43 -04003400 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003401 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003402
3403 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003404 leaf_data_end(root, left) - push_space,
3405 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003406 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003407 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003408 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003409 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003410
Chris Masondb945352007-10-15 16:15:53 -04003411 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003412 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003413 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003414
Chris Mason5f39d392007-10-15 16:14:19 -04003415 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003416
Chris Masoncfed81a2012-03-03 07:40:03 -05003417 ioff = btrfs_token_item_offset(left, item, &token);
3418 btrfs_set_token_item_offset(left, item,
3419 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3420 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003421 }
Chris Mason5f39d392007-10-15 16:14:19 -04003422 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003423
3424 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003425 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003426 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3427 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003428 WARN_ON(1);
3429 }
Chris Mason5f39d392007-10-15 16:14:19 -04003430
Chris Mason34a38212007-11-07 13:31:03 -05003431 if (push_items < right_nritems) {
3432 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3433 leaf_data_end(root, right);
3434 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3435 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3436 btrfs_leaf_data(right) +
3437 leaf_data_end(root, right), push_space);
3438
3439 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003440 btrfs_item_nr_offset(push_items),
3441 (btrfs_header_nritems(right) - push_items) *
3442 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003443 }
Yaneef1c492007-11-26 10:58:13 -05003444 right_nritems -= push_items;
3445 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003446 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003447 for (i = 0; i < right_nritems; i++) {
3448 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003449
Chris Masoncfed81a2012-03-03 07:40:03 -05003450 push_space = push_space - btrfs_token_item_size(right,
3451 item, &token);
3452 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003453 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003454
Chris Mason5f39d392007-10-15 16:14:19 -04003455 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003456 if (right_nritems)
3457 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003458 else
3459 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003460
Chris Mason5f39d392007-10-15 16:14:19 -04003461 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003462 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003463
3464 /* then fixup the leaf pointer in the path */
3465 if (path->slots[0] < push_items) {
3466 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003467 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003468 free_extent_buffer(path->nodes[0]);
3469 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003470 path->slots[1] -= 1;
3471 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003472 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003473 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003474 path->slots[0] -= push_items;
3475 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003476 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003477 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003478out:
3479 btrfs_tree_unlock(left);
3480 free_extent_buffer(left);
3481 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003482}
3483
Chris Mason74123bd2007-02-02 11:05:29 -05003484/*
Chris Mason44871b12009-03-13 10:04:31 -04003485 * push some data in the path leaf to the left, trying to free up at
3486 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003487 *
3488 * max_slot can put a limit on how far into the leaf we'll push items. The
3489 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3490 * items
Chris Mason44871b12009-03-13 10:04:31 -04003491 */
3492static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003493 *root, struct btrfs_path *path, int min_data_size,
3494 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003495{
3496 struct extent_buffer *right = path->nodes[0];
3497 struct extent_buffer *left;
3498 int slot;
3499 int free_space;
3500 u32 right_nritems;
3501 int ret = 0;
3502
3503 slot = path->slots[1];
3504 if (slot == 0)
3505 return 1;
3506 if (!path->nodes[1])
3507 return 1;
3508
3509 right_nritems = btrfs_header_nritems(right);
3510 if (right_nritems == 0)
3511 return 1;
3512
3513 btrfs_assert_tree_locked(path->nodes[1]);
3514
3515 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003516 if (left == NULL)
3517 return 1;
3518
Chris Mason44871b12009-03-13 10:04:31 -04003519 btrfs_tree_lock(left);
3520 btrfs_set_lock_blocking(left);
3521
3522 free_space = btrfs_leaf_free_space(root, left);
3523 if (free_space < data_size) {
3524 ret = 1;
3525 goto out;
3526 }
3527
3528 /* cow and double check */
3529 ret = btrfs_cow_block(trans, root, left,
3530 path->nodes[1], slot - 1, &left);
3531 if (ret) {
3532 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003533 if (ret == -ENOSPC)
3534 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003535 goto out;
3536 }
3537
3538 free_space = btrfs_leaf_free_space(root, left);
3539 if (free_space < data_size) {
3540 ret = 1;
3541 goto out;
3542 }
3543
Chris Mason99d8f832010-07-07 10:51:48 -04003544 return __push_leaf_left(trans, root, path, min_data_size,
3545 empty, left, free_space, right_nritems,
3546 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003547out:
3548 btrfs_tree_unlock(left);
3549 free_extent_buffer(left);
3550 return ret;
3551}
3552
3553/*
Chris Mason74123bd2007-02-02 11:05:29 -05003554 * split the path's leaf in two, making sure there is at least data_size
3555 * available for the resulting leaf level of the path.
3556 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003557static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3558 struct btrfs_root *root,
3559 struct btrfs_path *path,
3560 struct extent_buffer *l,
3561 struct extent_buffer *right,
3562 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003563{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003564 int data_copy_size;
3565 int rt_data_off;
3566 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003567 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003568 struct btrfs_map_token token;
3569
3570 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003571
Chris Mason5f39d392007-10-15 16:14:19 -04003572 nritems = nritems - mid;
3573 btrfs_set_header_nritems(right, nritems);
3574 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3575
3576 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3577 btrfs_item_nr_offset(mid),
3578 nritems * sizeof(struct btrfs_item));
3579
3580 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003581 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3582 data_copy_size, btrfs_leaf_data(l) +
3583 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003584
Chris Mason5f39d392007-10-15 16:14:19 -04003585 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3586 btrfs_item_end_nr(l, mid);
3587
3588 for (i = 0; i < nritems; i++) {
3589 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003590 u32 ioff;
3591
Chris Masoncfed81a2012-03-03 07:40:03 -05003592 ioff = btrfs_token_item_offset(right, item, &token);
3593 btrfs_set_token_item_offset(right, item,
3594 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003595 }
Chris Mason74123bd2007-02-02 11:05:29 -05003596
Chris Mason5f39d392007-10-15 16:14:19 -04003597 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003598 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003599 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003600 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003601
3602 btrfs_mark_buffer_dirty(right);
3603 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003604 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003605
Chris Masonbe0e5c02007-01-26 15:51:26 -05003606 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003607 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003608 free_extent_buffer(path->nodes[0]);
3609 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003610 path->slots[0] -= mid;
3611 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003612 } else {
3613 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003614 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003615 }
Chris Mason5f39d392007-10-15 16:14:19 -04003616
Chris Masoneb60cea2007-02-02 09:18:22 -05003617 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003618}
3619
3620/*
Chris Mason99d8f832010-07-07 10:51:48 -04003621 * double splits happen when we need to insert a big item in the middle
3622 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3623 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3624 * A B C
3625 *
3626 * We avoid this by trying to push the items on either side of our target
3627 * into the adjacent leaves. If all goes well we can avoid the double split
3628 * completely.
3629 */
3630static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3631 struct btrfs_root *root,
3632 struct btrfs_path *path,
3633 int data_size)
3634{
3635 int ret;
3636 int progress = 0;
3637 int slot;
3638 u32 nritems;
3639
3640 slot = path->slots[0];
3641
3642 /*
3643 * try to push all the items after our slot into the
3644 * right leaf
3645 */
3646 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3647 if (ret < 0)
3648 return ret;
3649
3650 if (ret == 0)
3651 progress++;
3652
3653 nritems = btrfs_header_nritems(path->nodes[0]);
3654 /*
3655 * our goal is to get our slot at the start or end of a leaf. If
3656 * we've done so we're done
3657 */
3658 if (path->slots[0] == 0 || path->slots[0] == nritems)
3659 return 0;
3660
3661 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3662 return 0;
3663
3664 /* try to push all the items before our slot into the next leaf */
3665 slot = path->slots[0];
3666 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3667 if (ret < 0)
3668 return ret;
3669
3670 if (ret == 0)
3671 progress++;
3672
3673 if (progress)
3674 return 0;
3675 return 1;
3676}
3677
3678/*
Chris Mason44871b12009-03-13 10:04:31 -04003679 * split the path's leaf in two, making sure there is at least data_size
3680 * available for the resulting leaf level of the path.
3681 *
3682 * returns 0 if all went well and < 0 on failure.
3683 */
3684static noinline int split_leaf(struct btrfs_trans_handle *trans,
3685 struct btrfs_root *root,
3686 struct btrfs_key *ins_key,
3687 struct btrfs_path *path, int data_size,
3688 int extend)
3689{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003690 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003691 struct extent_buffer *l;
3692 u32 nritems;
3693 int mid;
3694 int slot;
3695 struct extent_buffer *right;
3696 int ret = 0;
3697 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003698 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003699 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003700 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003701
Yan, Zhenga5719522009-09-24 09:17:31 -04003702 l = path->nodes[0];
3703 slot = path->slots[0];
3704 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3705 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3706 return -EOVERFLOW;
3707
Chris Mason44871b12009-03-13 10:04:31 -04003708 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003709 if (data_size) {
3710 wret = push_leaf_right(trans, root, path, data_size,
3711 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003712 if (wret < 0)
3713 return wret;
3714 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003715 wret = push_leaf_left(trans, root, path, data_size,
3716 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003717 if (wret < 0)
3718 return wret;
3719 }
3720 l = path->nodes[0];
3721
3722 /* did the pushes work? */
3723 if (btrfs_leaf_free_space(root, l) >= data_size)
3724 return 0;
3725 }
3726
3727 if (!path->nodes[1]) {
3728 ret = insert_new_root(trans, root, path, 1);
3729 if (ret)
3730 return ret;
3731 }
3732again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003733 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003734 l = path->nodes[0];
3735 slot = path->slots[0];
3736 nritems = btrfs_header_nritems(l);
3737 mid = (nritems + 1) / 2;
3738
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739 if (mid <= slot) {
3740 if (nritems == 1 ||
3741 leaf_space_used(l, mid, nritems - mid) + data_size >
3742 BTRFS_LEAF_DATA_SIZE(root)) {
3743 if (slot >= nritems) {
3744 split = 0;
3745 } else {
3746 mid = slot;
3747 if (mid != nritems &&
3748 leaf_space_used(l, mid, nritems - mid) +
3749 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003750 if (data_size && !tried_avoid_double)
3751 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003752 split = 2;
3753 }
3754 }
3755 }
3756 } else {
3757 if (leaf_space_used(l, 0, mid) + data_size >
3758 BTRFS_LEAF_DATA_SIZE(root)) {
3759 if (!extend && data_size && slot == 0) {
3760 split = 0;
3761 } else if ((extend || !data_size) && slot == 0) {
3762 mid = 1;
3763 } else {
3764 mid = slot;
3765 if (mid != nritems &&
3766 leaf_space_used(l, mid, nritems - mid) +
3767 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003768 if (data_size && !tried_avoid_double)
3769 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003770 split = 2 ;
3771 }
3772 }
3773 }
3774 }
3775
3776 if (split == 0)
3777 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3778 else
3779 btrfs_item_key(l, &disk_key, mid);
3780
3781 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003782 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003783 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003784 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003785 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003786
3787 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003788
3789 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3790 btrfs_set_header_bytenr(right, right->start);
3791 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003792 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003793 btrfs_set_header_owner(right, root->root_key.objectid);
3794 btrfs_set_header_level(right, 0);
3795 write_extent_buffer(right, root->fs_info->fsid,
3796 (unsigned long)btrfs_header_fsid(right),
3797 BTRFS_FSID_SIZE);
3798
3799 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3800 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3801 BTRFS_UUID_SIZE);
3802
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003803 if (split == 0) {
3804 if (mid <= slot) {
3805 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003806 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003807 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003808 btrfs_tree_unlock(path->nodes[0]);
3809 free_extent_buffer(path->nodes[0]);
3810 path->nodes[0] = right;
3811 path->slots[0] = 0;
3812 path->slots[1] += 1;
3813 } else {
3814 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003815 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003816 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003817 btrfs_tree_unlock(path->nodes[0]);
3818 free_extent_buffer(path->nodes[0]);
3819 path->nodes[0] = right;
3820 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003821 if (path->slots[1] == 0)
3822 fixup_low_keys(trans, root, path,
3823 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003824 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003825 btrfs_mark_buffer_dirty(right);
3826 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003827 }
3828
Jeff Mahoney143bede2012-03-01 14:56:26 +01003829 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003830
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003831 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003832 BUG_ON(num_doubles != 0);
3833 num_doubles++;
3834 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003835 }
Chris Mason44871b12009-03-13 10:04:31 -04003836
Jeff Mahoney143bede2012-03-01 14:56:26 +01003837 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003838
3839push_for_double:
3840 push_for_double_split(trans, root, path, data_size);
3841 tried_avoid_double = 1;
3842 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3843 return 0;
3844 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003845}
3846
Yan, Zhengad48fd752009-11-12 09:33:58 +00003847static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3848 struct btrfs_root *root,
3849 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003850{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003851 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003852 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003853 struct btrfs_file_extent_item *fi;
3854 u64 extent_len = 0;
3855 u32 item_size;
3856 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003857
3858 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003859 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3860
3861 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3862 key.type != BTRFS_EXTENT_CSUM_KEY);
3863
3864 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3865 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003866
3867 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003868 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3869 fi = btrfs_item_ptr(leaf, path->slots[0],
3870 struct btrfs_file_extent_item);
3871 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3872 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003873 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003874
Chris Mason459931e2008-12-10 09:10:46 -05003875 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003876 path->search_for_split = 1;
3877 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003878 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003879 if (ret < 0)
3880 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003881
Yan, Zhengad48fd752009-11-12 09:33:58 +00003882 ret = -EAGAIN;
3883 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003884 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003885 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3886 goto err;
3887
Chris Mason109f6ae2010-04-02 09:20:18 -04003888 /* the leaf has changed, it now has room. return now */
3889 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3890 goto err;
3891
Yan, Zhengad48fd752009-11-12 09:33:58 +00003892 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3893 fi = btrfs_item_ptr(leaf, path->slots[0],
3894 struct btrfs_file_extent_item);
3895 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3896 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003897 }
3898
Chris Masonb9473432009-03-13 11:00:37 -04003899 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003900 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003901 if (ret)
3902 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003903
Yan, Zhengad48fd752009-11-12 09:33:58 +00003904 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003905 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003906 return 0;
3907err:
3908 path->keep_locks = 0;
3909 return ret;
3910}
3911
3912static noinline int split_item(struct btrfs_trans_handle *trans,
3913 struct btrfs_root *root,
3914 struct btrfs_path *path,
3915 struct btrfs_key *new_key,
3916 unsigned long split_offset)
3917{
3918 struct extent_buffer *leaf;
3919 struct btrfs_item *item;
3920 struct btrfs_item *new_item;
3921 int slot;
3922 char *buf;
3923 u32 nritems;
3924 u32 item_size;
3925 u32 orig_offset;
3926 struct btrfs_disk_key disk_key;
3927
Chris Masonb9473432009-03-13 11:00:37 -04003928 leaf = path->nodes[0];
3929 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3930
Chris Masonb4ce94d2009-02-04 09:25:08 -05003931 btrfs_set_path_blocking(path);
3932
Chris Mason459931e2008-12-10 09:10:46 -05003933 item = btrfs_item_nr(leaf, path->slots[0]);
3934 orig_offset = btrfs_item_offset(leaf, item);
3935 item_size = btrfs_item_size(leaf, item);
3936
Chris Mason459931e2008-12-10 09:10:46 -05003937 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003938 if (!buf)
3939 return -ENOMEM;
3940
Chris Mason459931e2008-12-10 09:10:46 -05003941 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3942 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003943
Chris Mason459931e2008-12-10 09:10:46 -05003944 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003945 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003946 if (slot != nritems) {
3947 /* shift the items */
3948 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003949 btrfs_item_nr_offset(slot),
3950 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003951 }
3952
3953 btrfs_cpu_key_to_disk(&disk_key, new_key);
3954 btrfs_set_item_key(leaf, &disk_key, slot);
3955
3956 new_item = btrfs_item_nr(leaf, slot);
3957
3958 btrfs_set_item_offset(leaf, new_item, orig_offset);
3959 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3960
3961 btrfs_set_item_offset(leaf, item,
3962 orig_offset + item_size - split_offset);
3963 btrfs_set_item_size(leaf, item, split_offset);
3964
3965 btrfs_set_header_nritems(leaf, nritems + 1);
3966
3967 /* write the data for the start of the original item */
3968 write_extent_buffer(leaf, buf,
3969 btrfs_item_ptr_offset(leaf, path->slots[0]),
3970 split_offset);
3971
3972 /* write the data for the new item */
3973 write_extent_buffer(leaf, buf + split_offset,
3974 btrfs_item_ptr_offset(leaf, slot),
3975 item_size - split_offset);
3976 btrfs_mark_buffer_dirty(leaf);
3977
Yan, Zhengad48fd752009-11-12 09:33:58 +00003978 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003979 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003980 return 0;
3981}
3982
3983/*
3984 * This function splits a single item into two items,
3985 * giving 'new_key' to the new item and splitting the
3986 * old one at split_offset (from the start of the item).
3987 *
3988 * The path may be released by this operation. After
3989 * the split, the path is pointing to the old item. The
3990 * new item is going to be in the same node as the old one.
3991 *
3992 * Note, the item being split must be smaller enough to live alone on
3993 * a tree block with room for one extra struct btrfs_item
3994 *
3995 * This allows us to split the item in place, keeping a lock on the
3996 * leaf the entire time.
3997 */
3998int btrfs_split_item(struct btrfs_trans_handle *trans,
3999 struct btrfs_root *root,
4000 struct btrfs_path *path,
4001 struct btrfs_key *new_key,
4002 unsigned long split_offset)
4003{
4004 int ret;
4005 ret = setup_leaf_for_split(trans, root, path,
4006 sizeof(struct btrfs_item));
4007 if (ret)
4008 return ret;
4009
4010 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004011 return ret;
4012}
4013
4014/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004015 * This function duplicate a item, giving 'new_key' to the new item.
4016 * It guarantees both items live in the same tree leaf and the new item
4017 * is contiguous with the original item.
4018 *
4019 * This allows us to split file extent in place, keeping a lock on the
4020 * leaf the entire time.
4021 */
4022int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4023 struct btrfs_root *root,
4024 struct btrfs_path *path,
4025 struct btrfs_key *new_key)
4026{
4027 struct extent_buffer *leaf;
4028 int ret;
4029 u32 item_size;
4030
4031 leaf = path->nodes[0];
4032 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4033 ret = setup_leaf_for_split(trans, root, path,
4034 item_size + sizeof(struct btrfs_item));
4035 if (ret)
4036 return ret;
4037
4038 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004039 setup_items_for_insert(trans, root, path, new_key, &item_size,
4040 item_size, item_size +
4041 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004042 leaf = path->nodes[0];
4043 memcpy_extent_buffer(leaf,
4044 btrfs_item_ptr_offset(leaf, path->slots[0]),
4045 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4046 item_size);
4047 return 0;
4048}
4049
4050/*
Chris Masond352ac62008-09-29 15:18:18 -04004051 * make the item pointed to by the path smaller. new_size indicates
4052 * how small to make it, and from_end tells us if we just chop bytes
4053 * off the end of the item or if we shift the item to chop bytes off
4054 * the front.
4055 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004056void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4057 struct btrfs_root *root,
4058 struct btrfs_path *path,
4059 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004060{
Chris Masonb18c6682007-04-17 13:26:50 -04004061 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004062 struct extent_buffer *leaf;
4063 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004064 u32 nritems;
4065 unsigned int data_end;
4066 unsigned int old_data_start;
4067 unsigned int old_size;
4068 unsigned int size_diff;
4069 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004070 struct btrfs_map_token token;
4071
4072 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004073
Chris Mason5f39d392007-10-15 16:14:19 -04004074 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004075 slot = path->slots[0];
4076
4077 old_size = btrfs_item_size_nr(leaf, slot);
4078 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004079 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004080
Chris Mason5f39d392007-10-15 16:14:19 -04004081 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004082 data_end = leaf_data_end(root, leaf);
4083
Chris Mason5f39d392007-10-15 16:14:19 -04004084 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004085
Chris Masonb18c6682007-04-17 13:26:50 -04004086 size_diff = old_size - new_size;
4087
4088 BUG_ON(slot < 0);
4089 BUG_ON(slot >= nritems);
4090
4091 /*
4092 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4093 */
4094 /* first correct the data pointers */
4095 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004096 u32 ioff;
4097 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004098
Chris Masoncfed81a2012-03-03 07:40:03 -05004099 ioff = btrfs_token_item_offset(leaf, item, &token);
4100 btrfs_set_token_item_offset(leaf, item,
4101 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004102 }
Chris Masondb945352007-10-15 16:15:53 -04004103
Chris Masonb18c6682007-04-17 13:26:50 -04004104 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004105 if (from_end) {
4106 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4107 data_end + size_diff, btrfs_leaf_data(leaf) +
4108 data_end, old_data_start + new_size - data_end);
4109 } else {
4110 struct btrfs_disk_key disk_key;
4111 u64 offset;
4112
4113 btrfs_item_key(leaf, &disk_key, slot);
4114
4115 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4116 unsigned long ptr;
4117 struct btrfs_file_extent_item *fi;
4118
4119 fi = btrfs_item_ptr(leaf, slot,
4120 struct btrfs_file_extent_item);
4121 fi = (struct btrfs_file_extent_item *)(
4122 (unsigned long)fi - size_diff);
4123
4124 if (btrfs_file_extent_type(leaf, fi) ==
4125 BTRFS_FILE_EXTENT_INLINE) {
4126 ptr = btrfs_item_ptr_offset(leaf, slot);
4127 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004128 (unsigned long)fi,
4129 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004130 disk_bytenr));
4131 }
4132 }
4133
4134 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4135 data_end + size_diff, btrfs_leaf_data(leaf) +
4136 data_end, old_data_start - data_end);
4137
4138 offset = btrfs_disk_key_offset(&disk_key);
4139 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4140 btrfs_set_item_key(leaf, &disk_key, slot);
4141 if (slot == 0)
4142 fixup_low_keys(trans, root, path, &disk_key, 1);
4143 }
Chris Mason5f39d392007-10-15 16:14:19 -04004144
4145 item = btrfs_item_nr(leaf, slot);
4146 btrfs_set_item_size(leaf, item, new_size);
4147 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004148
Chris Mason5f39d392007-10-15 16:14:19 -04004149 if (btrfs_leaf_free_space(root, leaf) < 0) {
4150 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004151 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004152 }
Chris Masonb18c6682007-04-17 13:26:50 -04004153}
4154
Chris Masond352ac62008-09-29 15:18:18 -04004155/*
4156 * make the item pointed to by the path bigger, data_size is the new size.
4157 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004158void btrfs_extend_item(struct btrfs_trans_handle *trans,
4159 struct btrfs_root *root, struct btrfs_path *path,
4160 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004161{
Chris Mason6567e832007-04-16 09:22:45 -04004162 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004163 struct extent_buffer *leaf;
4164 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004165 u32 nritems;
4166 unsigned int data_end;
4167 unsigned int old_data;
4168 unsigned int old_size;
4169 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004170 struct btrfs_map_token token;
4171
4172 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004173
Chris Mason5f39d392007-10-15 16:14:19 -04004174 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004175
Chris Mason5f39d392007-10-15 16:14:19 -04004176 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004177 data_end = leaf_data_end(root, leaf);
4178
Chris Mason5f39d392007-10-15 16:14:19 -04004179 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4180 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004181 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004182 }
Chris Mason6567e832007-04-16 09:22:45 -04004183 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004184 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004185
4186 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004187 if (slot >= nritems) {
4188 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004189 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4190 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004191 BUG_ON(1);
4192 }
Chris Mason6567e832007-04-16 09:22:45 -04004193
4194 /*
4195 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4196 */
4197 /* first correct the data pointers */
4198 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004199 u32 ioff;
4200 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004201
Chris Masoncfed81a2012-03-03 07:40:03 -05004202 ioff = btrfs_token_item_offset(leaf, item, &token);
4203 btrfs_set_token_item_offset(leaf, item,
4204 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004205 }
Chris Mason5f39d392007-10-15 16:14:19 -04004206
Chris Mason6567e832007-04-16 09:22:45 -04004207 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004208 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004209 data_end - data_size, btrfs_leaf_data(leaf) +
4210 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004211
Chris Mason6567e832007-04-16 09:22:45 -04004212 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004213 old_size = btrfs_item_size_nr(leaf, slot);
4214 item = btrfs_item_nr(leaf, slot);
4215 btrfs_set_item_size(leaf, item, old_size + data_size);
4216 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004217
Chris Mason5f39d392007-10-15 16:14:19 -04004218 if (btrfs_leaf_free_space(root, leaf) < 0) {
4219 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004220 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004221 }
Chris Mason6567e832007-04-16 09:22:45 -04004222}
4223
Chris Mason74123bd2007-02-02 11:05:29 -05004224/*
Chris Masond352ac62008-09-29 15:18:18 -04004225 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004226 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004227 * Returns the number of keys that were inserted.
4228 */
4229int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4230 struct btrfs_root *root,
4231 struct btrfs_path *path,
4232 struct btrfs_key *cpu_key, u32 *data_size,
4233 int nr)
4234{
4235 struct extent_buffer *leaf;
4236 struct btrfs_item *item;
4237 int ret = 0;
4238 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004239 int i;
4240 u32 nritems;
4241 u32 total_data = 0;
4242 u32 total_size = 0;
4243 unsigned int data_end;
4244 struct btrfs_disk_key disk_key;
4245 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004246 struct btrfs_map_token token;
4247
4248 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004249
Yan Zheng87b29b22008-12-17 10:21:48 -05004250 for (i = 0; i < nr; i++) {
4251 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4252 BTRFS_LEAF_DATA_SIZE(root)) {
4253 break;
4254 nr = i;
4255 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004256 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004257 total_size += data_size[i] + sizeof(struct btrfs_item);
4258 }
4259 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004260
Josef Bacikf3465ca2008-11-12 14:19:50 -05004261 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4262 if (ret == 0)
4263 return -EEXIST;
4264 if (ret < 0)
4265 goto out;
4266
Josef Bacikf3465ca2008-11-12 14:19:50 -05004267 leaf = path->nodes[0];
4268
4269 nritems = btrfs_header_nritems(leaf);
4270 data_end = leaf_data_end(root, leaf);
4271
4272 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4273 for (i = nr; i >= 0; i--) {
4274 total_data -= data_size[i];
4275 total_size -= data_size[i] + sizeof(struct btrfs_item);
4276 if (total_size < btrfs_leaf_free_space(root, leaf))
4277 break;
4278 }
4279 nr = i;
4280 }
4281
4282 slot = path->slots[0];
4283 BUG_ON(slot < 0);
4284
4285 if (slot != nritems) {
4286 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4287
4288 item = btrfs_item_nr(leaf, slot);
4289 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4290
4291 /* figure out how many keys we can insert in here */
4292 total_data = data_size[0];
4293 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004294 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004295 break;
4296 total_data += data_size[i];
4297 }
4298 nr = i;
4299
4300 if (old_data < data_end) {
4301 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004302 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004303 slot, old_data, data_end);
4304 BUG_ON(1);
4305 }
4306 /*
4307 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4308 */
4309 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004310 for (i = slot; i < nritems; i++) {
4311 u32 ioff;
4312
4313 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004314 ioff = btrfs_token_item_offset(leaf, item, &token);
4315 btrfs_set_token_item_offset(leaf, item,
4316 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004317 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004318 /* shift the items */
4319 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4320 btrfs_item_nr_offset(slot),
4321 (nritems - slot) * sizeof(struct btrfs_item));
4322
4323 /* shift the data */
4324 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4325 data_end - total_data, btrfs_leaf_data(leaf) +
4326 data_end, old_data - data_end);
4327 data_end = old_data;
4328 } else {
4329 /*
4330 * this sucks but it has to be done, if we are inserting at
4331 * the end of the leaf only insert 1 of the items, since we
4332 * have no way of knowing whats on the next leaf and we'd have
4333 * to drop our current locks to figure it out
4334 */
4335 nr = 1;
4336 }
4337
4338 /* setup the item for the new data */
4339 for (i = 0; i < nr; i++) {
4340 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4341 btrfs_set_item_key(leaf, &disk_key, slot + i);
4342 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004343 btrfs_set_token_item_offset(leaf, item,
4344 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004345 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004346 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004347 }
4348 btrfs_set_header_nritems(leaf, nritems + nr);
4349 btrfs_mark_buffer_dirty(leaf);
4350
4351 ret = 0;
4352 if (slot == 0) {
4353 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004354 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004355 }
4356
4357 if (btrfs_leaf_free_space(root, leaf) < 0) {
4358 btrfs_print_leaf(root, leaf);
4359 BUG();
4360 }
4361out:
4362 if (!ret)
4363 ret = nr;
4364 return ret;
4365}
4366
4367/*
Chris Mason44871b12009-03-13 10:04:31 -04004368 * this is a helper for btrfs_insert_empty_items, the main goal here is
4369 * to save stack depth by doing the bulk of the work in a function
4370 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004371 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004372void setup_items_for_insert(struct btrfs_trans_handle *trans,
4373 struct btrfs_root *root, struct btrfs_path *path,
4374 struct btrfs_key *cpu_key, u32 *data_size,
4375 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004376{
Chris Mason5f39d392007-10-15 16:14:19 -04004377 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004378 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004379 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004380 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004381 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004382 struct extent_buffer *leaf;
4383 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004384 struct btrfs_map_token token;
4385
4386 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004387
Chris Mason5f39d392007-10-15 16:14:19 -04004388 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004389 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004390
Chris Mason5f39d392007-10-15 16:14:19 -04004391 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004392 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004393
Chris Masonf25956c2008-09-12 15:32:53 -04004394 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004395 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004396 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004397 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004398 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004399 }
Chris Mason5f39d392007-10-15 16:14:19 -04004400
Chris Masonbe0e5c02007-01-26 15:51:26 -05004401 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004402 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004403
Chris Mason5f39d392007-10-15 16:14:19 -04004404 if (old_data < data_end) {
4405 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004406 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004407 slot, old_data, data_end);
4408 BUG_ON(1);
4409 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004410 /*
4411 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4412 */
4413 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004414 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004415 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004416
Chris Mason5f39d392007-10-15 16:14:19 -04004417 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004418 ioff = btrfs_token_item_offset(leaf, item, &token);
4419 btrfs_set_token_item_offset(leaf, item,
4420 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004421 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004422 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004423 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004424 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004425 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004426
4427 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004428 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004429 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004430 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004431 data_end = old_data;
4432 }
Chris Mason5f39d392007-10-15 16:14:19 -04004433
Chris Mason62e27492007-03-15 12:56:47 -04004434 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004435 for (i = 0; i < nr; i++) {
4436 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4437 btrfs_set_item_key(leaf, &disk_key, slot + i);
4438 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004439 btrfs_set_token_item_offset(leaf, item,
4440 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004441 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004442 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004443 }
Chris Mason44871b12009-03-13 10:04:31 -04004444
Chris Mason9c583092008-01-29 15:15:18 -05004445 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004446
Chris Mason5a01a2e2008-01-30 11:43:54 -05004447 if (slot == 0) {
4448 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004449 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004450 }
Chris Masonb9473432009-03-13 11:00:37 -04004451 btrfs_unlock_up_safe(path, 1);
4452 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004453
Chris Mason5f39d392007-10-15 16:14:19 -04004454 if (btrfs_leaf_free_space(root, leaf) < 0) {
4455 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004456 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004457 }
Chris Mason44871b12009-03-13 10:04:31 -04004458}
4459
4460/*
4461 * Given a key and some data, insert items into the tree.
4462 * This does all the path init required, making room in the tree if needed.
4463 */
4464int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4465 struct btrfs_root *root,
4466 struct btrfs_path *path,
4467 struct btrfs_key *cpu_key, u32 *data_size,
4468 int nr)
4469{
Chris Mason44871b12009-03-13 10:04:31 -04004470 int ret = 0;
4471 int slot;
4472 int i;
4473 u32 total_size = 0;
4474 u32 total_data = 0;
4475
4476 for (i = 0; i < nr; i++)
4477 total_data += data_size[i];
4478
4479 total_size = total_data + (nr * sizeof(struct btrfs_item));
4480 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4481 if (ret == 0)
4482 return -EEXIST;
4483 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004484 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004485
Chris Mason44871b12009-03-13 10:04:31 -04004486 slot = path->slots[0];
4487 BUG_ON(slot < 0);
4488
Jeff Mahoney143bede2012-03-01 14:56:26 +01004489 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004490 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004491 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004492}
4493
4494/*
4495 * Given a key and some data, insert an item into the tree.
4496 * This does all the path init required, making room in the tree if needed.
4497 */
Chris Masone089f052007-03-16 16:20:31 -04004498int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4499 *root, struct btrfs_key *cpu_key, void *data, u32
4500 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004501{
4502 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004503 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004504 struct extent_buffer *leaf;
4505 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004506
Chris Mason2c90e5d2007-04-02 10:50:19 -04004507 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004508 if (!path)
4509 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004510 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004511 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004512 leaf = path->nodes[0];
4513 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4514 write_extent_buffer(leaf, data, ptr, data_size);
4515 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004516 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004517 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004518 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004519}
4520
Chris Mason74123bd2007-02-02 11:05:29 -05004521/*
Chris Mason5de08d72007-02-24 06:24:44 -05004522 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004523 *
Chris Masond352ac62008-09-29 15:18:18 -04004524 * the tree should have been previously balanced so the deletion does not
4525 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004526 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004527static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004528 struct btrfs_path *path, int level, int slot,
4529 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004530{
Chris Mason5f39d392007-10-15 16:14:19 -04004531 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004532 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004533 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004534
Chris Mason5f39d392007-10-15 16:14:19 -04004535 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004536 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004537 if (tree_mod_log && level)
4538 tree_mod_log_eb_move(root->fs_info, parent, slot,
4539 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004540 memmove_extent_buffer(parent,
4541 btrfs_node_key_ptr_offset(slot),
4542 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004543 sizeof(struct btrfs_key_ptr) *
4544 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05004545 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004546
4547 if (tree_mod_log && level) {
4548 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4549 MOD_LOG_KEY_REMOVE);
4550 BUG_ON(ret < 0);
4551 }
4552
Chris Mason7518a232007-03-12 12:01:18 -04004553 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004554 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004555 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004556 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004557 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004558 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004559 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004560 struct btrfs_disk_key disk_key;
4561
4562 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004563 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004564 }
Chris Masond6025572007-03-30 14:27:56 -04004565 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004566}
4567
Chris Mason74123bd2007-02-02 11:05:29 -05004568/*
Chris Mason323ac952008-10-01 19:05:46 -04004569 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004570 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004571 *
4572 * This deletes the pointer in path->nodes[1] and frees the leaf
4573 * block extent. zero is returned if it all worked out, < 0 otherwise.
4574 *
4575 * The path must have already been setup for deleting the leaf, including
4576 * all the proper balancing. path->nodes[1] must be locked.
4577 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004578static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4579 struct btrfs_root *root,
4580 struct btrfs_path *path,
4581 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004582{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004583 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004584 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004585
Chris Mason4d081c42009-02-04 09:31:28 -05004586 /*
4587 * btrfs_free_extent is expensive, we want to make sure we
4588 * aren't holding any locks when we call it
4589 */
4590 btrfs_unlock_up_safe(path, 0);
4591
Yan, Zhengf0486c62010-05-16 10:46:25 -04004592 root_sub_used(root, leaf->len);
4593
Josef Bacik3083ee22012-03-09 16:01:49 -05004594 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004595 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004596 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004597}
4598/*
Chris Mason74123bd2007-02-02 11:05:29 -05004599 * delete the item at the leaf level in path. If that empties
4600 * the leaf, remove it from the tree
4601 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004602int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4603 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004604{
Chris Mason5f39d392007-10-15 16:14:19 -04004605 struct extent_buffer *leaf;
4606 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004607 int last_off;
4608 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004609 int ret = 0;
4610 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004611 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004612 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004613 struct btrfs_map_token token;
4614
4615 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004616
Chris Mason5f39d392007-10-15 16:14:19 -04004617 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4619
4620 for (i = 0; i < nr; i++)
4621 dsize += btrfs_item_size_nr(leaf, slot + i);
4622
Chris Mason5f39d392007-10-15 16:14:19 -04004623 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004624
Chris Mason85e21ba2008-01-29 15:11:36 -05004625 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004626 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004627
4628 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004629 data_end + dsize,
4630 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004631 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004632
Chris Mason85e21ba2008-01-29 15:11:36 -05004633 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004634 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004635
Chris Mason5f39d392007-10-15 16:14:19 -04004636 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004637 ioff = btrfs_token_item_offset(leaf, item, &token);
4638 btrfs_set_token_item_offset(leaf, item,
4639 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004640 }
Chris Masondb945352007-10-15 16:15:53 -04004641
Chris Mason5f39d392007-10-15 16:14:19 -04004642 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004643 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004644 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004645 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004646 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004647 btrfs_set_header_nritems(leaf, nritems - nr);
4648 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004649
Chris Mason74123bd2007-02-02 11:05:29 -05004650 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004651 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004652 if (leaf == root->node) {
4653 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004654 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004655 btrfs_set_path_blocking(path);
4656 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004657 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004658 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004659 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004660 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004661 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004662 struct btrfs_disk_key disk_key;
4663
4664 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004665 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004666 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004667
Chris Mason74123bd2007-02-02 11:05:29 -05004668 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004669 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004670 /* push_leaf_left fixes the path.
4671 * make sure the path still points to our leaf
4672 * for possible call to del_ptr below
4673 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004674 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004675 extent_buffer_get(leaf);
4676
Chris Masonb9473432009-03-13 11:00:37 -04004677 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004678 wret = push_leaf_left(trans, root, path, 1, 1,
4679 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004680 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004681 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004682
4683 if (path->nodes[0] == leaf &&
4684 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004685 wret = push_leaf_right(trans, root, path, 1,
4686 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004687 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004688 ret = wret;
4689 }
Chris Mason5f39d392007-10-15 16:14:19 -04004690
4691 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004692 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004693 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004694 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004695 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004696 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004697 /* if we're still in the path, make sure
4698 * we're dirty. Otherwise, one of the
4699 * push_leaf functions must have already
4700 * dirtied this buffer
4701 */
4702 if (path->nodes[0] == leaf)
4703 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004704 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004705 }
Chris Masond5719762007-03-23 10:01:08 -04004706 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004707 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004708 }
4709 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004710 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004711}
4712
Chris Mason97571fd2007-02-24 13:39:08 -05004713/*
Chris Mason925baed2008-06-25 16:01:30 -04004714 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004715 * returns 0 if it found something or 1 if there are no lesser leaves.
4716 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004717 *
4718 * This may release the path, and so you may lose any locks held at the
4719 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004720 */
4721int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4722{
Chris Mason925baed2008-06-25 16:01:30 -04004723 struct btrfs_key key;
4724 struct btrfs_disk_key found_key;
4725 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004726
Chris Mason925baed2008-06-25 16:01:30 -04004727 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004728
Chris Mason925baed2008-06-25 16:01:30 -04004729 if (key.offset > 0)
4730 key.offset--;
4731 else if (key.type > 0)
4732 key.type--;
4733 else if (key.objectid > 0)
4734 key.objectid--;
4735 else
4736 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004737
David Sterbab3b4aa72011-04-21 01:20:15 +02004738 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004739 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4740 if (ret < 0)
4741 return ret;
4742 btrfs_item_key(path->nodes[0], &found_key, 0);
4743 ret = comp_keys(&found_key, &key);
4744 if (ret < 0)
4745 return 0;
4746 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004747}
4748
Chris Mason3f157a22008-06-25 16:01:31 -04004749/*
4750 * A helper function to walk down the tree starting at min_key, and looking
4751 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004752 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004753 *
4754 * This does not cow, but it does stuff the starting key it finds back
4755 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4756 * key and get a writable path.
4757 *
4758 * This does lock as it descends, and path->keep_locks should be set
4759 * to 1 by the caller.
4760 *
4761 * This honors path->lowest_level to prevent descent past a given level
4762 * of the tree.
4763 *
Chris Masond352ac62008-09-29 15:18:18 -04004764 * min_trans indicates the oldest transaction that you are interested
4765 * in walking through. Any nodes or leaves older than min_trans are
4766 * skipped over (without reading them).
4767 *
Chris Mason3f157a22008-06-25 16:01:31 -04004768 * returns zero if something useful was found, < 0 on error and 1 if there
4769 * was nothing in the tree that matched the search criteria.
4770 */
4771int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004772 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004773 struct btrfs_path *path, int cache_only,
4774 u64 min_trans)
4775{
4776 struct extent_buffer *cur;
4777 struct btrfs_key found_key;
4778 int slot;
Yan96524802008-07-24 12:19:49 -04004779 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004780 u32 nritems;
4781 int level;
4782 int ret = 1;
4783
Chris Mason934d3752008-12-08 16:43:10 -05004784 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004785again:
Chris Masonbd681512011-07-16 15:23:14 -04004786 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004787 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004788 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004789 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004790 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004791
4792 if (btrfs_header_generation(cur) < min_trans) {
4793 ret = 1;
4794 goto out;
4795 }
Chris Masond3977122009-01-05 21:25:51 -05004796 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004797 nritems = btrfs_header_nritems(cur);
4798 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004799 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004800
Chris Mason323ac952008-10-01 19:05:46 -04004801 /* at the lowest level, we're done, setup the path and exit */
4802 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004803 if (slot >= nritems)
4804 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004805 ret = 0;
4806 path->slots[level] = slot;
4807 btrfs_item_key_to_cpu(cur, &found_key, slot);
4808 goto out;
4809 }
Yan96524802008-07-24 12:19:49 -04004810 if (sret && slot > 0)
4811 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004812 /*
4813 * check this node pointer against the cache_only and
4814 * min_trans parameters. If it isn't in cache or is too
4815 * old, skip to the next one.
4816 */
Chris Masond3977122009-01-05 21:25:51 -05004817 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004818 u64 blockptr;
4819 u64 gen;
4820 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004821 struct btrfs_disk_key disk_key;
4822
Chris Mason3f157a22008-06-25 16:01:31 -04004823 blockptr = btrfs_node_blockptr(cur, slot);
4824 gen = btrfs_node_ptr_generation(cur, slot);
4825 if (gen < min_trans) {
4826 slot++;
4827 continue;
4828 }
4829 if (!cache_only)
4830 break;
4831
Chris Masone02119d2008-09-05 16:13:11 -04004832 if (max_key) {
4833 btrfs_node_key(cur, &disk_key, slot);
4834 if (comp_keys(&disk_key, max_key) >= 0) {
4835 ret = 1;
4836 goto out;
4837 }
4838 }
4839
Chris Mason3f157a22008-06-25 16:01:31 -04004840 tmp = btrfs_find_tree_block(root, blockptr,
4841 btrfs_level_size(root, level - 1));
4842
Chris Masonb9fab912012-05-06 07:23:47 -04004843 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004844 free_extent_buffer(tmp);
4845 break;
4846 }
4847 if (tmp)
4848 free_extent_buffer(tmp);
4849 slot++;
4850 }
Chris Masone02119d2008-09-05 16:13:11 -04004851find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004852 /*
4853 * we didn't find a candidate key in this node, walk forward
4854 * and find another one
4855 */
4856 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004857 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004858 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004859 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004860 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004861 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004862 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004863 goto again;
4864 } else {
4865 goto out;
4866 }
4867 }
4868 /* save our key for returning back */
4869 btrfs_node_key_to_cpu(cur, &found_key, slot);
4870 path->slots[level] = slot;
4871 if (level == path->lowest_level) {
4872 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004873 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004874 goto out;
4875 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004876 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004877 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004878 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004879
Chris Masonbd681512011-07-16 15:23:14 -04004880 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004881
Chris Masonbd681512011-07-16 15:23:14 -04004882 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004883 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004884 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004885 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004886 }
4887out:
4888 if (ret == 0)
4889 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004890 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004891 return ret;
4892}
4893
4894/*
4895 * this is similar to btrfs_next_leaf, but does not try to preserve
4896 * and fixup the path. It looks for and returns the next key in the
4897 * tree based on the current path and the cache_only and min_trans
4898 * parameters.
4899 *
4900 * 0 is returned if another key is found, < 0 if there are any errors
4901 * and 1 is returned if there are no higher keys in the tree
4902 *
4903 * path->keep_locks should be set to 1 on the search made before
4904 * calling this function.
4905 */
Chris Masone7a84562008-06-25 16:01:31 -04004906int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004907 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004908 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004909{
Chris Masone7a84562008-06-25 16:01:31 -04004910 int slot;
4911 struct extent_buffer *c;
4912
Chris Mason934d3752008-12-08 16:43:10 -05004913 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004914 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004915 if (!path->nodes[level])
4916 return 1;
4917
4918 slot = path->slots[level] + 1;
4919 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004920next:
Chris Masone7a84562008-06-25 16:01:31 -04004921 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004922 int ret;
4923 int orig_lowest;
4924 struct btrfs_key cur_key;
4925 if (level + 1 >= BTRFS_MAX_LEVEL ||
4926 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004927 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004928
4929 if (path->locks[level + 1]) {
4930 level++;
4931 continue;
4932 }
4933
4934 slot = btrfs_header_nritems(c) - 1;
4935 if (level == 0)
4936 btrfs_item_key_to_cpu(c, &cur_key, slot);
4937 else
4938 btrfs_node_key_to_cpu(c, &cur_key, slot);
4939
4940 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004941 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004942 path->lowest_level = level;
4943 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4944 0, 0);
4945 path->lowest_level = orig_lowest;
4946 if (ret < 0)
4947 return ret;
4948
4949 c = path->nodes[level];
4950 slot = path->slots[level];
4951 if (ret == 0)
4952 slot++;
4953 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004954 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004955
Chris Masone7a84562008-06-25 16:01:31 -04004956 if (level == 0)
4957 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004958 else {
4959 u64 blockptr = btrfs_node_blockptr(c, slot);
4960 u64 gen = btrfs_node_ptr_generation(c, slot);
4961
4962 if (cache_only) {
4963 struct extent_buffer *cur;
4964 cur = btrfs_find_tree_block(root, blockptr,
4965 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004966 if (!cur ||
4967 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004968 slot++;
4969 if (cur)
4970 free_extent_buffer(cur);
4971 goto next;
4972 }
4973 free_extent_buffer(cur);
4974 }
4975 if (gen < min_trans) {
4976 slot++;
4977 goto next;
4978 }
Chris Masone7a84562008-06-25 16:01:31 -04004979 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004980 }
Chris Masone7a84562008-06-25 16:01:31 -04004981 return 0;
4982 }
4983 return 1;
4984}
4985
Chris Mason7bb86312007-12-11 09:25:06 -05004986/*
Chris Mason925baed2008-06-25 16:01:30 -04004987 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004988 * returns 0 if it found something or 1 if there are no greater leaves.
4989 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004990 */
Chris Mason234b63a2007-03-13 10:46:10 -04004991int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004992{
4993 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04004994 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04004995 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04004996 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04004997 struct btrfs_key key;
4998 u32 nritems;
4999 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005000 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005001 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005002
5003 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005004 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005005 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005006
Chris Mason8e73f272009-04-03 10:14:18 -04005007 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5008again:
5009 level = 1;
5010 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005011 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005012 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005013
Chris Masona2135012008-06-25 16:01:30 -04005014 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005015 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005016
Chris Mason925baed2008-06-25 16:01:30 -04005017 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5018 path->keep_locks = 0;
5019
5020 if (ret < 0)
5021 return ret;
5022
Chris Masona2135012008-06-25 16:01:30 -04005023 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005024 /*
5025 * by releasing the path above we dropped all our locks. A balance
5026 * could have added more items next to the key that used to be
5027 * at the very end of the block. So, check again here and
5028 * advance the path if there are now more items available.
5029 */
Chris Masona2135012008-06-25 16:01:30 -04005030 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005031 if (ret == 0)
5032 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005033 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005034 goto done;
5035 }
Chris Masond97e63b2007-02-20 16:40:44 -05005036
Chris Masond3977122009-01-05 21:25:51 -05005037 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005038 if (!path->nodes[level]) {
5039 ret = 1;
5040 goto done;
5041 }
Chris Mason5f39d392007-10-15 16:14:19 -04005042
Chris Masond97e63b2007-02-20 16:40:44 -05005043 slot = path->slots[level] + 1;
5044 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005045 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005046 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005047 if (level == BTRFS_MAX_LEVEL) {
5048 ret = 1;
5049 goto done;
5050 }
Chris Masond97e63b2007-02-20 16:40:44 -05005051 continue;
5052 }
Chris Mason5f39d392007-10-15 16:14:19 -04005053
Chris Mason925baed2008-06-25 16:01:30 -04005054 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005055 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005056 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005057 }
Chris Mason5f39d392007-10-15 16:14:19 -04005058
Chris Mason8e73f272009-04-03 10:14:18 -04005059 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005060 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005061 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005062 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005063 if (ret == -EAGAIN)
5064 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005065
Chris Mason76a05b32009-05-14 13:24:30 -04005066 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005067 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005068 goto done;
5069 }
5070
Chris Mason5cd57b22008-06-25 16:01:30 -04005071 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005072 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005073 if (!ret) {
5074 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005075 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005076 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005077 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005078 }
Chris Mason31533fb2011-07-26 16:01:59 -04005079 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005080 }
Chris Masond97e63b2007-02-20 16:40:44 -05005081 break;
5082 }
5083 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005084 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005085 level--;
5086 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005087 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005088 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005089
Chris Mason5f39d392007-10-15 16:14:19 -04005090 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005091 path->nodes[level] = next;
5092 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005093 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005094 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005095 if (!level)
5096 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005097
Chris Mason8e73f272009-04-03 10:14:18 -04005098 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005099 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005100 if (ret == -EAGAIN)
5101 goto again;
5102
Chris Mason76a05b32009-05-14 13:24:30 -04005103 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005104 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005105 goto done;
5106 }
5107
Chris Mason5cd57b22008-06-25 16:01:30 -04005108 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005109 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005110 if (!ret) {
5111 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005112 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005113 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005114 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005115 }
Chris Mason31533fb2011-07-26 16:01:59 -04005116 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005117 }
Chris Masond97e63b2007-02-20 16:40:44 -05005118 }
Chris Mason8e73f272009-04-03 10:14:18 -04005119 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005120done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005121 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005122 path->leave_spinning = old_spinning;
5123 if (!old_spinning)
5124 btrfs_set_path_blocking(path);
5125
5126 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005127}
Chris Mason0b86a832008-03-24 15:01:56 -04005128
Chris Mason3f157a22008-06-25 16:01:31 -04005129/*
5130 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5131 * searching until it gets past min_objectid or finds an item of 'type'
5132 *
5133 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5134 */
Chris Mason0b86a832008-03-24 15:01:56 -04005135int btrfs_previous_item(struct btrfs_root *root,
5136 struct btrfs_path *path, u64 min_objectid,
5137 int type)
5138{
5139 struct btrfs_key found_key;
5140 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005141 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005142 int ret;
5143
Chris Masond3977122009-01-05 21:25:51 -05005144 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005145 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005146 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005147 ret = btrfs_prev_leaf(root, path);
5148 if (ret != 0)
5149 return ret;
5150 } else {
5151 path->slots[0]--;
5152 }
5153 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005154 nritems = btrfs_header_nritems(leaf);
5155 if (nritems == 0)
5156 return 1;
5157 if (path->slots[0] == nritems)
5158 path->slots[0]--;
5159
Chris Mason0b86a832008-03-24 15:01:56 -04005160 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005161 if (found_key.objectid < min_objectid)
5162 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005163 if (found_key.type == type)
5164 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005165 if (found_key.objectid == min_objectid &&
5166 found_key.type < type)
5167 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005168 }
5169 return 1;
5170}