blob: c7c48489b963fb306ef365a3aa345aa4618159cd [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
Chris Masond352ac62008-09-29 15:18:18 -04002 * Copyright (C) 2007,2008 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Masona6b6e752007-10-15 16:22:39 -040019#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Jan Schmidtbd989ba2012-05-16 17:18:50 +020021#include <linux/rbtree.h>
Chris Masoneb60cea2007-02-02 09:18:22 -050022#include "ctree.h"
23#include "disk-io.h"
Chris Mason7f5c1512007-03-23 15:56:19 -040024#include "transaction.h"
Chris Mason5f39d392007-10-15 16:14:19 -040025#include "print-tree.h"
Chris Mason925baed2008-06-25 16:01:30 -040026#include "locking.h"
Chris Mason9a8dd152007-02-23 08:38:36 -050027
Chris Masone089f052007-03-16 16:20:31 -040028static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
29 *root, struct btrfs_path *path, int level);
30static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Masond4dbff92007-04-04 14:08:15 -040031 *root, struct btrfs_key *ins_key,
Chris Masoncc0c5532007-10-25 15:42:57 -040032 struct btrfs_path *path, int data_size, int extend);
Chris Mason5f39d392007-10-15 16:14:19 -040033static int push_node_left(struct btrfs_trans_handle *trans,
34 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -040035 struct extent_buffer *src, int empty);
Chris Mason5f39d392007-10-15 16:14:19 -040036static int balance_node_right(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 struct extent_buffer *dst_buf,
39 struct extent_buffer *src_buf);
Jeff Mahoney143bede2012-03-01 14:56:26 +010040static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +020041 struct btrfs_path *path, int level, int slot,
42 int tree_mod_log);
Jan Schmidtf2304752012-05-26 11:43:17 +020043static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
44 struct extent_buffer *eb);
45struct extent_buffer *read_old_tree_block(struct btrfs_root *root, u64 bytenr,
46 u32 blocksize, u64 parent_transid,
47 u64 time_seq);
48struct extent_buffer *btrfs_find_old_tree_block(struct btrfs_root *root,
49 u64 bytenr, u32 blocksize,
50 u64 time_seq);
Chris Masond97e63b2007-02-20 16:40:44 -050051
Chris Mason2c90e5d2007-04-02 10:50:19 -040052struct btrfs_path *btrfs_alloc_path(void)
53{
Chris Masondf24a2b2007-04-04 09:36:31 -040054 struct btrfs_path *path;
Jeff Mahoneye00f7302009-02-12 14:11:25 -050055 path = kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
Chris Masondf24a2b2007-04-04 09:36:31 -040056 return path;
Chris Mason2c90e5d2007-04-02 10:50:19 -040057}
58
Chris Masonb4ce94d2009-02-04 09:25:08 -050059/*
60 * set all locked nodes in the path to blocking locks. This should
61 * be done before scheduling
62 */
63noinline void btrfs_set_path_blocking(struct btrfs_path *p)
64{
65 int i;
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbd681512011-07-16 15:23:14 -040067 if (!p->nodes[i] || !p->locks[i])
68 continue;
69 btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
70 if (p->locks[i] == BTRFS_READ_LOCK)
71 p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
72 else if (p->locks[i] == BTRFS_WRITE_LOCK)
73 p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
Chris Masonb4ce94d2009-02-04 09:25:08 -050074 }
75}
76
77/*
78 * reset all the locked nodes in the patch to spinning locks.
Chris Mason4008c042009-02-12 14:09:45 -050079 *
80 * held is used to keep lockdep happy, when lockdep is enabled
81 * we set held to a blocking lock before we go around and
82 * retake all the spinlocks in the path. You can safely use NULL
83 * for held
Chris Masonb4ce94d2009-02-04 09:25:08 -050084 */
Chris Mason4008c042009-02-12 14:09:45 -050085noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -040086 struct extent_buffer *held, int held_rw)
Chris Masonb4ce94d2009-02-04 09:25:08 -050087{
88 int i;
Chris Mason4008c042009-02-12 14:09:45 -050089
90#ifdef CONFIG_DEBUG_LOCK_ALLOC
91 /* lockdep really cares that we take all of these spinlocks
92 * in the right order. If any of the locks in the path are not
93 * currently blocking, it is going to complain. So, make really
94 * really sure by forcing the path to blocking before we clear
95 * the path blocking.
96 */
Chris Masonbd681512011-07-16 15:23:14 -040097 if (held) {
98 btrfs_set_lock_blocking_rw(held, held_rw);
99 if (held_rw == BTRFS_WRITE_LOCK)
100 held_rw = BTRFS_WRITE_LOCK_BLOCKING;
101 else if (held_rw == BTRFS_READ_LOCK)
102 held_rw = BTRFS_READ_LOCK_BLOCKING;
103 }
Chris Mason4008c042009-02-12 14:09:45 -0500104 btrfs_set_path_blocking(p);
105#endif
106
107 for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
Chris Masonbd681512011-07-16 15:23:14 -0400108 if (p->nodes[i] && p->locks[i]) {
109 btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
110 if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
111 p->locks[i] = BTRFS_WRITE_LOCK;
112 else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
113 p->locks[i] = BTRFS_READ_LOCK;
114 }
Chris Masonb4ce94d2009-02-04 09:25:08 -0500115 }
Chris Mason4008c042009-02-12 14:09:45 -0500116
117#ifdef CONFIG_DEBUG_LOCK_ALLOC
118 if (held)
Chris Masonbd681512011-07-16 15:23:14 -0400119 btrfs_clear_lock_blocking_rw(held, held_rw);
Chris Mason4008c042009-02-12 14:09:45 -0500120#endif
Chris Masonb4ce94d2009-02-04 09:25:08 -0500121}
122
Chris Masond352ac62008-09-29 15:18:18 -0400123/* this also releases the path */
Chris Mason2c90e5d2007-04-02 10:50:19 -0400124void btrfs_free_path(struct btrfs_path *p)
125{
Jesper Juhlff175d52010-12-25 21:22:30 +0000126 if (!p)
127 return;
David Sterbab3b4aa72011-04-21 01:20:15 +0200128 btrfs_release_path(p);
Chris Mason2c90e5d2007-04-02 10:50:19 -0400129 kmem_cache_free(btrfs_path_cachep, p);
130}
131
Chris Masond352ac62008-09-29 15:18:18 -0400132/*
133 * path release drops references on the extent buffers in the path
134 * and it drops any locks held by this path
135 *
136 * It is safe to call this on paths that no locks or extent buffers held.
137 */
David Sterbab3b4aa72011-04-21 01:20:15 +0200138noinline void btrfs_release_path(struct btrfs_path *p)
Chris Masoneb60cea2007-02-02 09:18:22 -0500139{
140 int i;
Chris Masona2135012008-06-25 16:01:30 -0400141
Chris Mason234b63a2007-03-13 10:46:10 -0400142 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
Chris Mason3f157a22008-06-25 16:01:31 -0400143 p->slots[i] = 0;
Chris Masoneb60cea2007-02-02 09:18:22 -0500144 if (!p->nodes[i])
Chris Mason925baed2008-06-25 16:01:30 -0400145 continue;
146 if (p->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -0400147 btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -0400148 p->locks[i] = 0;
149 }
Chris Mason5f39d392007-10-15 16:14:19 -0400150 free_extent_buffer(p->nodes[i]);
Chris Mason3f157a22008-06-25 16:01:31 -0400151 p->nodes[i] = NULL;
Chris Masoneb60cea2007-02-02 09:18:22 -0500152 }
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
156 * safely gets a reference on the root node of a tree. A lock
157 * is not taken, so a concurrent writer may put a different node
158 * at the root of the tree. See btrfs_lock_root_node for the
159 * looping required.
160 *
161 * The extent buffer returned by this has a reference taken, so
162 * it won't disappear. It may stop being the root of the tree
163 * at any time because there are no locks held.
164 */
Chris Mason925baed2008-06-25 16:01:30 -0400165struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
166{
167 struct extent_buffer *eb;
Chris Mason240f62c2011-03-23 14:54:42 -0400168
Josef Bacik3083ee22012-03-09 16:01:49 -0500169 while (1) {
170 rcu_read_lock();
171 eb = rcu_dereference(root->node);
172
173 /*
174 * RCU really hurts here, we could free up the root node because
175 * it was cow'ed but we may not get the new root node yet so do
176 * the inc_not_zero dance and if it doesn't work then
177 * synchronize_rcu and try again.
178 */
179 if (atomic_inc_not_zero(&eb->refs)) {
180 rcu_read_unlock();
181 break;
182 }
183 rcu_read_unlock();
184 synchronize_rcu();
185 }
Chris Mason925baed2008-06-25 16:01:30 -0400186 return eb;
187}
188
Chris Masond352ac62008-09-29 15:18:18 -0400189/* loop around taking references on and locking the root node of the
190 * tree until you end up with a lock on the root. A locked buffer
191 * is returned, with a reference held.
192 */
Chris Mason925baed2008-06-25 16:01:30 -0400193struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
194{
195 struct extent_buffer *eb;
196
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason925baed2008-06-25 16:01:30 -0400198 eb = btrfs_root_node(root);
199 btrfs_tree_lock(eb);
Chris Mason240f62c2011-03-23 14:54:42 -0400200 if (eb == root->node)
Chris Mason925baed2008-06-25 16:01:30 -0400201 break;
Chris Mason925baed2008-06-25 16:01:30 -0400202 btrfs_tree_unlock(eb);
203 free_extent_buffer(eb);
204 }
205 return eb;
206}
207
Chris Masonbd681512011-07-16 15:23:14 -0400208/* loop around taking references on and locking the root node of the
209 * tree until you end up with a lock on the root. A locked buffer
210 * is returned, with a reference held.
211 */
212struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
213{
214 struct extent_buffer *eb;
215
216 while (1) {
217 eb = btrfs_root_node(root);
218 btrfs_tree_read_lock(eb);
219 if (eb == root->node)
220 break;
221 btrfs_tree_read_unlock(eb);
222 free_extent_buffer(eb);
223 }
224 return eb;
225}
226
Chris Masond352ac62008-09-29 15:18:18 -0400227/* cowonly root (everything not a reference counted cow subvolume), just get
228 * put onto a simple dirty list. transaction.c walks this to make sure they
229 * get properly updated on disk.
230 */
Chris Mason0b86a832008-03-24 15:01:56 -0400231static void add_root_to_dirty_list(struct btrfs_root *root)
232{
Chris Masone5846fc2012-05-03 12:08:48 -0400233 spin_lock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400234 if (root->track_dirty && list_empty(&root->dirty_list)) {
235 list_add(&root->dirty_list,
236 &root->fs_info->dirty_cowonly_roots);
237 }
Chris Masone5846fc2012-05-03 12:08:48 -0400238 spin_unlock(&root->fs_info->trans_lock);
Chris Mason0b86a832008-03-24 15:01:56 -0400239}
240
Chris Masond352ac62008-09-29 15:18:18 -0400241/*
242 * used by snapshot creation to make a copy of a root for a tree with
243 * a given objectid. The buffer with the new root node is returned in
244 * cow_ret, and this func returns zero on success or a negative error code.
245 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500246int btrfs_copy_root(struct btrfs_trans_handle *trans,
247 struct btrfs_root *root,
248 struct extent_buffer *buf,
249 struct extent_buffer **cow_ret, u64 new_root_objectid)
250{
251 struct extent_buffer *cow;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 int ret = 0;
253 int level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400254 struct btrfs_disk_key disk_key;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255
256 WARN_ON(root->ref_cows && trans->transid !=
257 root->fs_info->running_transaction->transid);
258 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
259
260 level = btrfs_header_level(buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400261 if (level == 0)
262 btrfs_item_key(buf, &disk_key, 0);
263 else
264 btrfs_node_key(buf, &disk_key, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400265
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400266 cow = btrfs_alloc_free_block(trans, root, buf->len, 0,
267 new_root_objectid, &disk_key, level,
Jan Schmidt5581a512012-05-16 17:04:52 +0200268 buf->start, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400269 if (IS_ERR(cow))
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 return PTR_ERR(cow);
271
272 copy_extent_buffer(cow, buf, 0, 0, cow->len);
273 btrfs_set_header_bytenr(cow, cow->start);
274 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400275 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
276 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
277 BTRFS_HEADER_FLAG_RELOC);
278 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
279 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
280 else
281 btrfs_set_header_owner(cow, new_root_objectid);
Chris Masonbe20aa92007-12-17 20:14:01 -0500282
Yan Zheng2b820322008-11-17 21:11:30 -0500283 write_extent_buffer(cow, root->fs_info->fsid,
284 (unsigned long)btrfs_header_fsid(cow),
285 BTRFS_FSID_SIZE);
286
Chris Masonbe20aa92007-12-17 20:14:01 -0500287 WARN_ON(btrfs_header_generation(buf) > trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400288 if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200289 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400290 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200291 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Chris Mason4aec2b52007-12-18 16:25:45 -0500292
Chris Masonbe20aa92007-12-17 20:14:01 -0500293 if (ret)
294 return ret;
295
296 btrfs_mark_buffer_dirty(cow);
297 *cow_ret = cow;
298 return 0;
299}
300
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200301enum mod_log_op {
302 MOD_LOG_KEY_REPLACE,
303 MOD_LOG_KEY_ADD,
304 MOD_LOG_KEY_REMOVE,
305 MOD_LOG_KEY_REMOVE_WHILE_FREEING,
306 MOD_LOG_KEY_REMOVE_WHILE_MOVING,
307 MOD_LOG_MOVE_KEYS,
308 MOD_LOG_ROOT_REPLACE,
309};
310
311struct tree_mod_move {
312 int dst_slot;
313 int nr_items;
314};
315
316struct tree_mod_root {
317 u64 logical;
318 u8 level;
319};
320
321struct tree_mod_elem {
322 struct rb_node node;
323 u64 index; /* shifted logical */
324 struct seq_list elem;
325 enum mod_log_op op;
326
327 /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
328 int slot;
329
330 /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
331 u64 generation;
332
333 /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
334 struct btrfs_disk_key key;
335 u64 blockptr;
336
337 /* this is used for op == MOD_LOG_MOVE_KEYS */
338 struct tree_mod_move move;
339
340 /* this is used for op == MOD_LOG_ROOT_REPLACE */
341 struct tree_mod_root old_root;
342};
343
344static inline void
345__get_tree_mod_seq(struct btrfs_fs_info *fs_info, struct seq_list *elem)
346{
347 elem->seq = atomic_inc_return(&fs_info->tree_mod_seq);
348 list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
349}
350
351void btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
352 struct seq_list *elem)
353{
354 elem->flags = 1;
355 spin_lock(&fs_info->tree_mod_seq_lock);
356 __get_tree_mod_seq(fs_info, elem);
357 spin_unlock(&fs_info->tree_mod_seq_lock);
358}
359
360void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
361 struct seq_list *elem)
362{
363 struct rb_root *tm_root;
364 struct rb_node *node;
365 struct rb_node *next;
366 struct seq_list *cur_elem;
367 struct tree_mod_elem *tm;
368 u64 min_seq = (u64)-1;
369 u64 seq_putting = elem->seq;
370
371 if (!seq_putting)
372 return;
373
374 BUG_ON(!(elem->flags & 1));
375 spin_lock(&fs_info->tree_mod_seq_lock);
376 list_del(&elem->list);
377
378 list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
379 if ((cur_elem->flags & 1) && cur_elem->seq < min_seq) {
380 if (seq_putting > cur_elem->seq) {
381 /*
382 * blocker with lower sequence number exists, we
383 * cannot remove anything from the log
384 */
385 goto out;
386 }
387 min_seq = cur_elem->seq;
388 }
389 }
390
391 /*
392 * anything that's lower than the lowest existing (read: blocked)
393 * sequence number can be removed from the tree.
394 */
395 write_lock(&fs_info->tree_mod_log_lock);
396 tm_root = &fs_info->tree_mod_log;
397 for (node = rb_first(tm_root); node; node = next) {
398 next = rb_next(node);
399 tm = container_of(node, struct tree_mod_elem, node);
400 if (tm->elem.seq > min_seq)
401 continue;
402 rb_erase(node, tm_root);
403 list_del(&tm->elem.list);
404 kfree(tm);
405 }
406 write_unlock(&fs_info->tree_mod_log_lock);
407out:
408 spin_unlock(&fs_info->tree_mod_seq_lock);
409}
410
411/*
412 * key order of the log:
413 * index -> sequence
414 *
415 * the index is the shifted logical of the *new* root node for root replace
416 * operations, or the shifted logical of the affected block for all other
417 * operations.
418 */
419static noinline int
420__tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
421{
422 struct rb_root *tm_root;
423 struct rb_node **new;
424 struct rb_node *parent = NULL;
425 struct tree_mod_elem *cur;
426 int ret = 0;
427
428 BUG_ON(!tm || !tm->elem.seq);
429
430 write_lock(&fs_info->tree_mod_log_lock);
431 tm_root = &fs_info->tree_mod_log;
432 new = &tm_root->rb_node;
433 while (*new) {
434 cur = container_of(*new, struct tree_mod_elem, node);
435 parent = *new;
436 if (cur->index < tm->index)
437 new = &((*new)->rb_left);
438 else if (cur->index > tm->index)
439 new = &((*new)->rb_right);
440 else if (cur->elem.seq < tm->elem.seq)
441 new = &((*new)->rb_left);
442 else if (cur->elem.seq > tm->elem.seq)
443 new = &((*new)->rb_right);
444 else {
445 kfree(tm);
446 ret = -EEXIST;
447 goto unlock;
448 }
449 }
450
451 rb_link_node(&tm->node, parent, new);
452 rb_insert_color(&tm->node, tm_root);
453unlock:
454 write_unlock(&fs_info->tree_mod_log_lock);
455 return ret;
456}
457
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200458static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
459 struct extent_buffer *eb) {
460 smp_mb();
461 if (list_empty(&(fs_info)->tree_mod_seq_list))
462 return 1;
463 if (!eb)
464 return 0;
465 if (btrfs_header_level(eb) == 0)
466 return 1;
467 return 0;
468}
469
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200470static inline int tree_mod_alloc(struct btrfs_fs_info *fs_info, gfp_t flags,
471 struct tree_mod_elem **tm_ret)
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200472{
473 struct tree_mod_elem *tm;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200474 int seq;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200475
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200476 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200477 return 0;
478
479 tm = *tm_ret = kzalloc(sizeof(*tm), flags);
480 if (!tm)
481 return -ENOMEM;
482
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200483 tm->elem.flags = 0;
Jan Schmidt926dd8a2012-05-31 14:00:15 +0200484 spin_lock(&fs_info->tree_mod_seq_lock);
485 if (list_empty(&fs_info->tree_mod_seq_list)) {
486 /*
487 * someone emptied the list while we were waiting for the lock.
488 * we must not add to the list, because no blocker exists. items
489 * are removed from the list only when the existing blocker is
490 * removed from the list.
491 */
492 kfree(tm);
493 seq = 0;
494 } else {
495 __get_tree_mod_seq(fs_info, &tm->elem);
496 seq = tm->elem.seq;
497 }
498 spin_unlock(&fs_info->tree_mod_seq_lock);
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200499
500 return seq;
501}
502
503static noinline int
504tree_mod_log_insert_key_mask(struct btrfs_fs_info *fs_info,
505 struct extent_buffer *eb, int slot,
506 enum mod_log_op op, gfp_t flags)
507{
508 struct tree_mod_elem *tm;
509 int ret;
510
511 ret = tree_mod_alloc(fs_info, flags, &tm);
512 if (ret <= 0)
513 return ret;
514
515 tm->index = eb->start >> PAGE_CACHE_SHIFT;
516 if (op != MOD_LOG_KEY_ADD) {
517 btrfs_node_key(eb, &tm->key, slot);
518 tm->blockptr = btrfs_node_blockptr(eb, slot);
519 }
520 tm->op = op;
521 tm->slot = slot;
522 tm->generation = btrfs_node_ptr_generation(eb, slot);
523
524 return __tree_mod_log_insert(fs_info, tm);
525}
526
527static noinline int
528tree_mod_log_insert_key(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
529 int slot, enum mod_log_op op)
530{
531 return tree_mod_log_insert_key_mask(fs_info, eb, slot, op, GFP_NOFS);
532}
533
534static noinline int
535tree_mod_log_insert_move(struct btrfs_fs_info *fs_info,
536 struct extent_buffer *eb, int dst_slot, int src_slot,
537 int nr_items, gfp_t flags)
538{
539 struct tree_mod_elem *tm;
540 int ret;
541 int i;
542
543 ret = tree_mod_alloc(fs_info, flags, &tm);
544 if (ret <= 0)
545 return ret;
546
547 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
548 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
549 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
550 BUG_ON(ret < 0);
551 }
552
553 tm->index = eb->start >> PAGE_CACHE_SHIFT;
554 tm->slot = src_slot;
555 tm->move.dst_slot = dst_slot;
556 tm->move.nr_items = nr_items;
557 tm->op = MOD_LOG_MOVE_KEYS;
558
559 return __tree_mod_log_insert(fs_info, tm);
560}
561
562static noinline int
563tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
564 struct extent_buffer *old_root,
565 struct extent_buffer *new_root, gfp_t flags)
566{
567 struct tree_mod_elem *tm;
568 int ret;
569
570 ret = tree_mod_alloc(fs_info, flags, &tm);
571 if (ret <= 0)
572 return ret;
573
574 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
575 tm->old_root.logical = old_root->start;
576 tm->old_root.level = btrfs_header_level(old_root);
577 tm->generation = btrfs_header_generation(old_root);
578 tm->op = MOD_LOG_ROOT_REPLACE;
579
580 return __tree_mod_log_insert(fs_info, tm);
581}
582
583static struct tree_mod_elem *
584__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
585 int smallest)
586{
587 struct rb_root *tm_root;
588 struct rb_node *node;
589 struct tree_mod_elem *cur = NULL;
590 struct tree_mod_elem *found = NULL;
591 u64 index = start >> PAGE_CACHE_SHIFT;
592
593 read_lock(&fs_info->tree_mod_log_lock);
594 tm_root = &fs_info->tree_mod_log;
595 node = tm_root->rb_node;
596 while (node) {
597 cur = container_of(node, struct tree_mod_elem, node);
598 if (cur->index < index) {
599 node = node->rb_left;
600 } else if (cur->index > index) {
601 node = node->rb_right;
602 } else if (cur->elem.seq < min_seq) {
603 node = node->rb_left;
604 } else if (!smallest) {
605 /* we want the node with the highest seq */
606 if (found)
607 BUG_ON(found->elem.seq > cur->elem.seq);
608 found = cur;
609 node = node->rb_left;
610 } else if (cur->elem.seq > min_seq) {
611 /* we want the node with the smallest seq */
612 if (found)
613 BUG_ON(found->elem.seq < cur->elem.seq);
614 found = cur;
615 node = node->rb_right;
616 } else {
617 found = cur;
618 break;
619 }
620 }
621 read_unlock(&fs_info->tree_mod_log_lock);
622
623 return found;
624}
625
626/*
627 * this returns the element from the log with the smallest time sequence
628 * value that's in the log (the oldest log item). any element with a time
629 * sequence lower than min_seq will be ignored.
630 */
631static struct tree_mod_elem *
632tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
633 u64 min_seq)
634{
635 return __tree_mod_log_search(fs_info, start, min_seq, 1);
636}
637
638/*
639 * this returns the element from the log with the largest time sequence
640 * value that's in the log (the most recent log item). any element with
641 * a time sequence lower than min_seq will be ignored.
642 */
643static struct tree_mod_elem *
644tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
645{
646 return __tree_mod_log_search(fs_info, start, min_seq, 0);
647}
648
649static inline void
650tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
651 struct extent_buffer *src, unsigned long dst_offset,
652 unsigned long src_offset, int nr_items)
653{
654 int ret;
655 int i;
656
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200657 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200658 return;
659
660 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
661 return;
662
663 /* speed this up by single seq for all operations? */
664 for (i = 0; i < nr_items; i++) {
665 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
666 MOD_LOG_KEY_REMOVE);
667 BUG_ON(ret < 0);
668 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
669 MOD_LOG_KEY_ADD);
670 BUG_ON(ret < 0);
671 }
672}
673
674static inline void
675tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
676 int dst_offset, int src_offset, int nr_items)
677{
678 int ret;
679 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
680 nr_items, GFP_NOFS);
681 BUG_ON(ret < 0);
682}
683
684static inline void
685tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
686 struct extent_buffer *eb,
687 struct btrfs_disk_key *disk_key, int slot, int atomic)
688{
689 int ret;
690
691 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
692 MOD_LOG_KEY_REPLACE,
693 atomic ? GFP_ATOMIC : GFP_NOFS);
694 BUG_ON(ret < 0);
695}
696
697static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
698 struct extent_buffer *eb)
699{
700 int i;
701 int ret;
702 u32 nritems;
703
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200704 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200705 return;
706
707 nritems = btrfs_header_nritems(eb);
708 for (i = nritems - 1; i >= 0; i--) {
709 ret = tree_mod_log_insert_key(fs_info, eb, i,
710 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
711 BUG_ON(ret < 0);
712 }
713}
714
715static inline void
716tree_mod_log_set_root_pointer(struct btrfs_root *root,
717 struct extent_buffer *new_root_node)
718{
719 int ret;
720 tree_mod_log_free_eb(root->fs_info, root->node);
721 ret = tree_mod_log_insert_root(root->fs_info, root->node,
722 new_root_node, GFP_NOFS);
723 BUG_ON(ret < 0);
724}
725
Chris Masond352ac62008-09-29 15:18:18 -0400726/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400727 * check if the tree block can be shared by multiple trees
728 */
729int btrfs_block_can_be_shared(struct btrfs_root *root,
730 struct extent_buffer *buf)
731{
732 /*
733 * Tree blocks not in refernece counted trees and tree roots
734 * are never shared. If a block was allocated after the last
735 * snapshot and the block was not allocated by tree relocation,
736 * we know the block is not shared.
737 */
738 if (root->ref_cows &&
739 buf != root->node && buf != root->commit_root &&
740 (btrfs_header_generation(buf) <=
741 btrfs_root_last_snapshot(&root->root_item) ||
742 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
743 return 1;
744#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
745 if (root->ref_cows &&
746 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
747 return 1;
748#endif
749 return 0;
750}
751
752static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
753 struct btrfs_root *root,
754 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400755 struct extent_buffer *cow,
756 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400757{
758 u64 refs;
759 u64 owner;
760 u64 flags;
761 u64 new_flags = 0;
762 int ret;
763
764 /*
765 * Backrefs update rules:
766 *
767 * Always use full backrefs for extent pointers in tree block
768 * allocated by tree relocation.
769 *
770 * If a shared tree block is no longer referenced by its owner
771 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
772 * use full backrefs for extent pointers in tree block.
773 *
774 * If a tree block is been relocating
775 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
776 * use full backrefs for extent pointers in tree block.
777 * The reason for this is some operations (such as drop tree)
778 * are only allowed for blocks use full backrefs.
779 */
780
781 if (btrfs_block_can_be_shared(root, buf)) {
782 ret = btrfs_lookup_extent_info(trans, root, buf->start,
783 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700784 if (ret)
785 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700786 if (refs == 0) {
787 ret = -EROFS;
788 btrfs_std_error(root->fs_info, ret);
789 return ret;
790 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400791 } else {
792 refs = 1;
793 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
794 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
795 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
796 else
797 flags = 0;
798 }
799
800 owner = btrfs_header_owner(buf);
801 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
802 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
803
804 if (refs > 1) {
805 if ((owner == root->root_key.objectid ||
806 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
807 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200808 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100809 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400810
811 if (root->root_key.objectid ==
812 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200813 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100814 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200815 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100816 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400817 }
818 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
819 } else {
820
821 if (root->root_key.objectid ==
822 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200823 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400824 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200825 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100826 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827 }
828 if (new_flags != 0) {
829 ret = btrfs_set_disk_extent_flags(trans, root,
830 buf->start,
831 buf->len,
832 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700833 if (ret)
834 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400835 }
836 } else {
837 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
838 if (root->root_key.objectid ==
839 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200840 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400841 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200842 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100843 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200844 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100845 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400846 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200847 /*
848 * don't log freeing in case we're freeing the root node, this
849 * is done by tree_mod_log_set_root_pointer later
850 */
851 if (buf != root->node && btrfs_header_level(buf) != 0)
852 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400853 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400854 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400855 }
856 return 0;
857}
858
859/*
Chris Masond3977122009-01-05 21:25:51 -0500860 * does the dirty work in cow of a single block. The parent block (if
861 * supplied) is updated to point to the new cow copy. The new buffer is marked
862 * dirty and returned locked. If you modify the block it needs to be marked
863 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400864 *
865 * search_start -- an allocation hint for the new block
866 *
Chris Masond3977122009-01-05 21:25:51 -0500867 * empty_size -- a hint that you plan on doing more cow. This is the size in
868 * bytes the allocator should try to find free next to the block it returns.
869 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400870 */
Chris Masond3977122009-01-05 21:25:51 -0500871static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400872 struct btrfs_root *root,
873 struct extent_buffer *buf,
874 struct extent_buffer *parent, int parent_slot,
875 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400876 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400877{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400878 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400879 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700880 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400881 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400882 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400883 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400884
Chris Mason925baed2008-06-25 16:01:30 -0400885 if (*cow_ret == buf)
886 unlock_orig = 1;
887
Chris Masonb9447ef2009-03-09 11:45:38 -0400888 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400889
Chris Mason7bb86312007-12-11 09:25:06 -0500890 WARN_ON(root->ref_cows && trans->transid !=
891 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400892 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400893
Chris Mason7bb86312007-12-11 09:25:06 -0500894 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400895
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400896 if (level == 0)
897 btrfs_item_key(buf, &disk_key, 0);
898 else
899 btrfs_node_key(buf, &disk_key, 0);
900
901 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
902 if (parent)
903 parent_start = parent->start;
904 else
905 parent_start = 0;
906 } else
907 parent_start = 0;
908
909 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
910 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200911 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400912 if (IS_ERR(cow))
913 return PTR_ERR(cow);
914
Chris Masonb4ce94d2009-02-04 09:25:08 -0500915 /* cow is set to blocking by btrfs_init_new_buffer */
916
Chris Mason5f39d392007-10-15 16:14:19 -0400917 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400918 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400919 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400920 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
921 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
922 BTRFS_HEADER_FLAG_RELOC);
923 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
924 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
925 else
926 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400927
Yan Zheng2b820322008-11-17 21:11:30 -0500928 write_extent_buffer(cow, root->fs_info->fsid,
929 (unsigned long)btrfs_header_fsid(cow),
930 BTRFS_FSID_SIZE);
931
Mark Fashehbe1a5562011-08-08 13:20:18 -0700932 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700933 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100934 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700935 return ret;
936 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400937
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400938 if (root->ref_cows)
939 btrfs_reloc_cow_block(trans, root, buf, cow);
940
Chris Mason6702ed42007-08-07 16:15:09 -0400941 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400942 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
944 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
945 parent_start = buf->start;
946 else
947 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400948
Chris Mason5f39d392007-10-15 16:14:19 -0400949 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200950 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400951 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400952
Yan, Zhengf0486c62010-05-16 10:46:25 -0400953 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200954 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400955 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400956 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400957 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400958 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
959 parent_start = parent->start;
960 else
961 parent_start = 0;
962
963 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200964 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
965 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400966 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400967 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500968 btrfs_set_node_ptr_generation(parent, parent_slot,
969 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400970 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400971 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200972 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400973 }
Chris Mason925baed2008-06-25 16:01:30 -0400974 if (unlock_orig)
975 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500976 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400977 btrfs_mark_buffer_dirty(cow);
978 *cow_ret = cow;
979 return 0;
980}
981
Jan Schmidt5d9e75c42012-05-16 18:25:47 +0200982/*
983 * returns the logical address of the oldest predecessor of the given root.
984 * entries older than time_seq are ignored.
985 */
986static struct tree_mod_elem *
987__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
988 struct btrfs_root *root, u64 time_seq)
989{
990 struct tree_mod_elem *tm;
991 struct tree_mod_elem *found = NULL;
992 u64 root_logical = root->node->start;
993 int looped = 0;
994
995 if (!time_seq)
996 return 0;
997
998 /*
999 * the very last operation that's logged for a root is the replacement
1000 * operation (if it is replaced at all). this has the index of the *new*
1001 * root, making it the very first operation that's logged for this root.
1002 */
1003 while (1) {
1004 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1005 time_seq);
1006 if (!looped && !tm)
1007 return 0;
1008 /*
1009 * we must have key remove operations in the log before the
1010 * replace operation.
1011 */
1012 BUG_ON(!tm);
1013
1014 if (tm->op != MOD_LOG_ROOT_REPLACE)
1015 break;
1016
1017 found = tm;
1018 root_logical = tm->old_root.logical;
1019 BUG_ON(root_logical == root->node->start);
1020 looped = 1;
1021 }
1022
1023 return found;
1024}
1025
1026/*
1027 * tm is a pointer to the first operation to rewind within eb. then, all
1028 * previous operations will be rewinded (until we reach something older than
1029 * time_seq).
1030 */
1031static void
1032__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1033 struct tree_mod_elem *first_tm)
1034{
1035 u32 n;
1036 struct rb_node *next;
1037 struct tree_mod_elem *tm = first_tm;
1038 unsigned long o_dst;
1039 unsigned long o_src;
1040 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1041
1042 n = btrfs_header_nritems(eb);
1043 while (tm && tm->elem.seq >= time_seq) {
1044 /*
1045 * all the operations are recorded with the operator used for
1046 * the modification. as we're going backwards, we do the
1047 * opposite of each operation here.
1048 */
1049 switch (tm->op) {
1050 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1051 BUG_ON(tm->slot < n);
1052 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1053 case MOD_LOG_KEY_REMOVE:
1054 btrfs_set_node_key(eb, &tm->key, tm->slot);
1055 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1056 btrfs_set_node_ptr_generation(eb, tm->slot,
1057 tm->generation);
1058 n++;
1059 break;
1060 case MOD_LOG_KEY_REPLACE:
1061 BUG_ON(tm->slot >= n);
1062 btrfs_set_node_key(eb, &tm->key, tm->slot);
1063 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1064 btrfs_set_node_ptr_generation(eb, tm->slot,
1065 tm->generation);
1066 break;
1067 case MOD_LOG_KEY_ADD:
1068 if (tm->slot != n - 1) {
1069 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1070 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1071 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1072 }
1073 n--;
1074 break;
1075 case MOD_LOG_MOVE_KEYS:
1076 memmove_extent_buffer(eb, tm->slot, tm->move.dst_slot,
1077 tm->move.nr_items * p_size);
1078 break;
1079 case MOD_LOG_ROOT_REPLACE:
1080 /*
1081 * this operation is special. for roots, this must be
1082 * handled explicitly before rewinding.
1083 * for non-roots, this operation may exist if the node
1084 * was a root: root A -> child B; then A gets empty and
1085 * B is promoted to the new root. in the mod log, we'll
1086 * have a root-replace operation for B, a tree block
1087 * that is no root. we simply ignore that operation.
1088 */
1089 break;
1090 }
1091 next = rb_next(&tm->node);
1092 if (!next)
1093 break;
1094 tm = container_of(next, struct tree_mod_elem, node);
1095 if (tm->index != first_tm->index)
1096 break;
1097 }
1098 btrfs_set_header_nritems(eb, n);
1099}
1100
1101static struct extent_buffer *
1102tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1103 u64 time_seq)
1104{
1105 struct extent_buffer *eb_rewin;
1106 struct tree_mod_elem *tm;
1107
1108 if (!time_seq)
1109 return eb;
1110
1111 if (btrfs_header_level(eb) == 0)
1112 return eb;
1113
1114 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1115 if (!tm)
1116 return eb;
1117
1118 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1119 BUG_ON(tm->slot != 0);
1120 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1121 fs_info->tree_root->nodesize);
1122 BUG_ON(!eb_rewin);
1123 btrfs_set_header_bytenr(eb_rewin, eb->start);
1124 btrfs_set_header_backref_rev(eb_rewin,
1125 btrfs_header_backref_rev(eb));
1126 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1127 } else {
1128 eb_rewin = btrfs_clone_extent_buffer(eb);
1129 BUG_ON(!eb_rewin);
1130 }
1131
1132 extent_buffer_get(eb_rewin);
1133 free_extent_buffer(eb);
1134
1135 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1136
1137 return eb_rewin;
1138}
1139
1140static inline struct extent_buffer *
1141get_old_root(struct btrfs_root *root, u64 time_seq)
1142{
1143 struct tree_mod_elem *tm;
1144 struct extent_buffer *eb;
1145 struct tree_mod_root *old_root;
1146 u64 old_generation;
1147
1148 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1149 if (!tm)
1150 return root->node;
1151
1152 old_root = &tm->old_root;
1153 old_generation = tm->generation;
1154
1155 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1156 /*
1157 * there was an item in the log when __tree_mod_log_oldest_root
1158 * returned. this one must not go away, because the time_seq passed to
1159 * us must be blocking its removal.
1160 */
1161 BUG_ON(!tm);
1162
1163 if (old_root->logical == root->node->start) {
1164 /* there are logged operations for the current root */
1165 eb = btrfs_clone_extent_buffer(root->node);
1166 } else {
1167 /* there's a root replace operation for the current root */
1168 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1169 root->nodesize);
1170 btrfs_set_header_bytenr(eb, eb->start);
1171 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1172 btrfs_set_header_owner(eb, root->root_key.objectid);
1173 }
1174 if (!eb)
1175 return NULL;
1176 btrfs_set_header_level(eb, old_root->level);
1177 btrfs_set_header_generation(eb, old_generation);
1178 __tree_mod_log_rewind(eb, time_seq, tm);
1179
1180 return eb;
1181}
1182
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183static inline int should_cow_block(struct btrfs_trans_handle *trans,
1184 struct btrfs_root *root,
1185 struct extent_buffer *buf)
1186{
Liu Bof1ebcc72011-11-14 20:48:06 -05001187 /* ensure we can see the force_cow */
1188 smp_rmb();
1189
1190 /*
1191 * We do not need to cow a block if
1192 * 1) this block is not created or changed in this transaction;
1193 * 2) this block does not belong to TREE_RELOC tree;
1194 * 3) the root is not forced COW.
1195 *
1196 * What is forced COW:
1197 * when we create snapshot during commiting the transaction,
1198 * after we've finished coping src root, we must COW the shared
1199 * block to ensure the metadata consistency.
1200 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001201 if (btrfs_header_generation(buf) == trans->transid &&
1202 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1203 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001204 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1205 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001206 return 0;
1207 return 1;
1208}
1209
Chris Masond352ac62008-09-29 15:18:18 -04001210/*
1211 * cows a single block, see __btrfs_cow_block for the real work.
1212 * This version of it has extra checks so that a block isn't cow'd more than
1213 * once per transaction, as long as it hasn't been written yet
1214 */
Chris Masond3977122009-01-05 21:25:51 -05001215noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001216 struct btrfs_root *root, struct extent_buffer *buf,
1217 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001218 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001219{
Chris Mason6702ed42007-08-07 16:15:09 -04001220 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001221 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001222
Chris Masonccd467d2007-06-28 15:57:36 -04001223 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001224 printk(KERN_CRIT "trans %llu running %llu\n",
1225 (unsigned long long)trans->transid,
1226 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001227 root->fs_info->running_transaction->transid);
1228 WARN_ON(1);
1229 }
1230 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001231 printk(KERN_CRIT "trans %llu running %llu\n",
1232 (unsigned long long)trans->transid,
1233 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001234 WARN_ON(1);
1235 }
Chris Masondc17ff82008-01-08 15:46:30 -05001236
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001237 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001238 *cow_ret = buf;
1239 return 0;
1240 }
Chris Masonc4876852009-02-04 09:24:25 -05001241
Chris Mason0b86a832008-03-24 15:01:56 -04001242 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001243
1244 if (parent)
1245 btrfs_set_lock_blocking(parent);
1246 btrfs_set_lock_blocking(buf);
1247
Chris Masonf510cfe2007-10-15 16:14:48 -04001248 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001249 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001250
1251 trace_btrfs_cow_block(root, buf, *cow_ret);
1252
Chris Masonf510cfe2007-10-15 16:14:48 -04001253 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001254}
1255
Chris Masond352ac62008-09-29 15:18:18 -04001256/*
1257 * helper function for defrag to decide if two blocks pointed to by a
1258 * node are actually close by
1259 */
Chris Mason6b800532007-10-15 16:17:34 -04001260static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001261{
Chris Mason6b800532007-10-15 16:17:34 -04001262 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001263 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001264 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001265 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001266 return 0;
1267}
1268
Chris Mason081e9572007-11-06 10:26:24 -05001269/*
1270 * compare two keys in a memcmp fashion
1271 */
1272static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1273{
1274 struct btrfs_key k1;
1275
1276 btrfs_disk_key_to_cpu(&k1, disk);
1277
Diego Calleja20736ab2009-07-24 11:06:52 -04001278 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001279}
1280
Josef Bacikf3465ca2008-11-12 14:19:50 -05001281/*
1282 * same as comp_keys only with two btrfs_key's
1283 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001284int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001285{
1286 if (k1->objectid > k2->objectid)
1287 return 1;
1288 if (k1->objectid < k2->objectid)
1289 return -1;
1290 if (k1->type > k2->type)
1291 return 1;
1292 if (k1->type < k2->type)
1293 return -1;
1294 if (k1->offset > k2->offset)
1295 return 1;
1296 if (k1->offset < k2->offset)
1297 return -1;
1298 return 0;
1299}
Chris Mason081e9572007-11-06 10:26:24 -05001300
Chris Masond352ac62008-09-29 15:18:18 -04001301/*
1302 * this is used by the defrag code to go through all the
1303 * leaves pointed to by a node and reallocate them so that
1304 * disk order is close to key order
1305 */
Chris Mason6702ed42007-08-07 16:15:09 -04001306int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001307 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001308 int start_slot, int cache_only, u64 *last_ret,
1309 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001310{
Chris Mason6b800532007-10-15 16:17:34 -04001311 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001312 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001313 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001314 u64 search_start = *last_ret;
1315 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001316 u64 other;
1317 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001318 int end_slot;
1319 int i;
1320 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001321 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001322 int uptodate;
1323 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001324 int progress_passed = 0;
1325 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001326
Chris Mason5708b952007-10-25 15:43:18 -04001327 parent_level = btrfs_header_level(parent);
1328 if (cache_only && parent_level != 1)
1329 return 0;
1330
Chris Masond3977122009-01-05 21:25:51 -05001331 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001332 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001333 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001334 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001335
Chris Mason6b800532007-10-15 16:17:34 -04001336 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001337 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001338 end_slot = parent_nritems;
1339
1340 if (parent_nritems == 1)
1341 return 0;
1342
Chris Masonb4ce94d2009-02-04 09:25:08 -05001343 btrfs_set_lock_blocking(parent);
1344
Chris Mason6702ed42007-08-07 16:15:09 -04001345 for (i = start_slot; i < end_slot; i++) {
1346 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001347
Chris Mason081e9572007-11-06 10:26:24 -05001348 btrfs_node_key(parent, &disk_key, i);
1349 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1350 continue;
1351
1352 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001353 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001354 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001355 if (last_block == 0)
1356 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001357
Chris Mason6702ed42007-08-07 16:15:09 -04001358 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001359 other = btrfs_node_blockptr(parent, i - 1);
1360 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001361 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001362 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001363 other = btrfs_node_blockptr(parent, i + 1);
1364 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001365 }
Chris Masone9d0b132007-08-10 14:06:19 -04001366 if (close) {
1367 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001368 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001369 }
Chris Mason6702ed42007-08-07 16:15:09 -04001370
Chris Mason6b800532007-10-15 16:17:34 -04001371 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1372 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001373 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001374 else
1375 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001376 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001377 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001378 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001379 continue;
1380 }
Chris Mason6b800532007-10-15 16:17:34 -04001381 if (!cur) {
1382 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001383 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001384 if (!cur)
1385 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001386 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001387 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001388 }
Chris Mason6702ed42007-08-07 16:15:09 -04001389 }
Chris Masone9d0b132007-08-10 14:06:19 -04001390 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001391 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001392
Chris Masone7a84562008-06-25 16:01:31 -04001393 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001394 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001395 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001396 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001397 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001398 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001399 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001400 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001401 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001402 break;
Yan252c38f2007-08-29 09:11:44 -04001403 }
Chris Masone7a84562008-06-25 16:01:31 -04001404 search_start = cur->start;
1405 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001406 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001407 btrfs_tree_unlock(cur);
1408 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001409 }
1410 return err;
1411}
1412
Chris Mason74123bd2007-02-02 11:05:29 -05001413/*
1414 * The leaf data grows from end-to-front in the node.
1415 * this returns the address of the start of the last item,
1416 * which is the stop of the leaf data stack
1417 */
Chris Mason123abc82007-03-14 14:14:43 -04001418static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001419 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001420{
Chris Mason5f39d392007-10-15 16:14:19 -04001421 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001422 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001423 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001424 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001425}
1426
Chris Masonaa5d6be2007-02-28 16:35:06 -05001427
Chris Mason74123bd2007-02-02 11:05:29 -05001428/*
Chris Mason5f39d392007-10-15 16:14:19 -04001429 * search for key in the extent_buffer. The items start at offset p,
1430 * and they are item_size apart. There are 'max' items in p.
1431 *
Chris Mason74123bd2007-02-02 11:05:29 -05001432 * the slot in the array is returned via slot, and it points to
1433 * the place where you would insert key if it is not found in
1434 * the array.
1435 *
1436 * slot may point to max if the key is bigger than all of the keys
1437 */
Chris Masone02119d2008-09-05 16:13:11 -04001438static noinline int generic_bin_search(struct extent_buffer *eb,
1439 unsigned long p,
1440 int item_size, struct btrfs_key *key,
1441 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001442{
1443 int low = 0;
1444 int high = max;
1445 int mid;
1446 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001447 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001448 struct btrfs_disk_key unaligned;
1449 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001450 char *kaddr = NULL;
1451 unsigned long map_start = 0;
1452 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001453 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001454
Chris Masond3977122009-01-05 21:25:51 -05001455 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001456 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001457 offset = p + mid * item_size;
1458
Chris Masona6591712011-07-19 12:04:14 -04001459 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001460 (offset + sizeof(struct btrfs_disk_key)) >
1461 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001462
1463 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001464 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001465 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001466
Chris Mason479965d2007-10-15 16:14:27 -04001467 if (!err) {
1468 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1469 map_start);
1470 } else {
1471 read_extent_buffer(eb, &unaligned,
1472 offset, sizeof(unaligned));
1473 tmp = &unaligned;
1474 }
1475
Chris Mason5f39d392007-10-15 16:14:19 -04001476 } else {
1477 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1478 map_start);
1479 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001480 ret = comp_keys(tmp, key);
1481
1482 if (ret < 0)
1483 low = mid + 1;
1484 else if (ret > 0)
1485 high = mid;
1486 else {
1487 *slot = mid;
1488 return 0;
1489 }
1490 }
1491 *slot = low;
1492 return 1;
1493}
1494
Chris Mason97571fd2007-02-24 13:39:08 -05001495/*
1496 * simple bin_search frontend that does the right thing for
1497 * leaves vs nodes
1498 */
Chris Mason5f39d392007-10-15 16:14:19 -04001499static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1500 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001501{
Chris Mason5f39d392007-10-15 16:14:19 -04001502 if (level == 0) {
1503 return generic_bin_search(eb,
1504 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001505 sizeof(struct btrfs_item),
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 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001509 return generic_bin_search(eb,
1510 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001511 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001512 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001513 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001514 }
1515 return -1;
1516}
1517
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001518int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1519 int level, int *slot)
1520{
1521 return bin_search(eb, key, level, slot);
1522}
1523
Yan, Zhengf0486c62010-05-16 10:46:25 -04001524static void root_add_used(struct btrfs_root *root, u32 size)
1525{
1526 spin_lock(&root->accounting_lock);
1527 btrfs_set_root_used(&root->root_item,
1528 btrfs_root_used(&root->root_item) + size);
1529 spin_unlock(&root->accounting_lock);
1530}
1531
1532static void root_sub_used(struct btrfs_root *root, u32 size)
1533{
1534 spin_lock(&root->accounting_lock);
1535 btrfs_set_root_used(&root->root_item,
1536 btrfs_root_used(&root->root_item) - size);
1537 spin_unlock(&root->accounting_lock);
1538}
1539
Chris Masond352ac62008-09-29 15:18:18 -04001540/* given a node and slot number, this reads the blocks it points to. The
1541 * extent buffer is returned with a reference taken (but unlocked).
1542 * NULL is returned on error.
1543 */
Chris Masone02119d2008-09-05 16:13:11 -04001544static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001545 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001546{
Chris Masonca7a79a2008-05-12 12:59:19 -04001547 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001548 if (slot < 0)
1549 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001550 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001551 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001552
1553 BUG_ON(level == 0);
1554
Chris Masondb945352007-10-15 16:15:53 -04001555 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001556 btrfs_level_size(root, level - 1),
1557 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001558}
1559
Chris Masond352ac62008-09-29 15:18:18 -04001560/*
1561 * node level balancing, used to make sure nodes are in proper order for
1562 * item deletion. We balance from the top down, so we have to make sure
1563 * that a deletion won't leave an node completely empty later on.
1564 */
Chris Masone02119d2008-09-05 16:13:11 -04001565static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001566 struct btrfs_root *root,
1567 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001568{
Chris Mason5f39d392007-10-15 16:14:19 -04001569 struct extent_buffer *right = NULL;
1570 struct extent_buffer *mid;
1571 struct extent_buffer *left = NULL;
1572 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001573 int ret = 0;
1574 int wret;
1575 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001576 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001577 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001578
1579 if (level == 0)
1580 return 0;
1581
Chris Mason5f39d392007-10-15 16:14:19 -04001582 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001583
Chris Masonbd681512011-07-16 15:23:14 -04001584 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1585 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001586 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1587
Chris Mason1d4f8a02007-03-13 09:28:32 -04001588 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001589
Li Zefana05a9bb2011-09-06 16:55:34 +08001590 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001591 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001592 pslot = path->slots[level + 1];
1593 }
Chris Masonbb803952007-03-01 12:04:21 -05001594
Chris Mason40689472007-03-17 14:29:23 -04001595 /*
1596 * deal with the case where there is only one pointer in the root
1597 * by promoting the node below to a root
1598 */
Chris Mason5f39d392007-10-15 16:14:19 -04001599 if (!parent) {
1600 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001601
Chris Mason5f39d392007-10-15 16:14:19 -04001602 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001603 return 0;
1604
1605 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001606 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001607 if (!child) {
1608 ret = -EROFS;
1609 btrfs_std_error(root->fs_info, ret);
1610 goto enospc;
1611 }
1612
Chris Mason925baed2008-06-25 16:01:30 -04001613 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001614 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001615 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001616 if (ret) {
1617 btrfs_tree_unlock(child);
1618 free_extent_buffer(child);
1619 goto enospc;
1620 }
Yan2f375ab2008-02-01 14:58:07 -05001621
Jan Schmidtf2304752012-05-26 11:43:17 +02001622 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001623 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001624
Chris Mason0b86a832008-03-24 15:01:56 -04001625 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001626 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001627
Chris Mason925baed2008-06-25 16:01:30 -04001628 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001629 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001630 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001631 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001632 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001633 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001634
1635 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001636 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001637 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001638 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001639 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001640 }
Chris Mason5f39d392007-10-15 16:14:19 -04001641 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001642 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001643 return 0;
1644
Andi Kleen559af822010-10-29 15:14:37 -04001645 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001646
Chris Mason5f39d392007-10-15 16:14:19 -04001647 left = read_node_slot(root, parent, pslot - 1);
1648 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001649 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001650 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001651 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001652 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001653 if (wret) {
1654 ret = wret;
1655 goto enospc;
1656 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001657 }
Chris Mason5f39d392007-10-15 16:14:19 -04001658 right = read_node_slot(root, parent, pslot + 1);
1659 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001660 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001661 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001662 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001663 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001664 if (wret) {
1665 ret = wret;
1666 goto enospc;
1667 }
1668 }
1669
1670 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001671 if (left) {
1672 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001673 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001674 if (wret < 0)
1675 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001676 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001677 }
Chris Mason79f95c82007-03-01 15:16:26 -05001678
1679 /*
1680 * then try to empty the right most buffer into the middle
1681 */
Chris Mason5f39d392007-10-15 16:14:19 -04001682 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001683 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001684 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001685 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001686 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001687 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001688 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001689 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001690 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001691 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001692 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001693 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001694 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001695 struct btrfs_disk_key right_key;
1696 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001697 tree_mod_log_set_node_key(root->fs_info, parent,
1698 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001699 btrfs_set_node_key(parent, &right_key, pslot + 1);
1700 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001701 }
1702 }
Chris Mason5f39d392007-10-15 16:14:19 -04001703 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001704 /*
1705 * we're not allowed to leave a node with one item in the
1706 * tree during a delete. A deletion from lower in the tree
1707 * could try to delete the only pointer in this node.
1708 * So, pull some keys from the left.
1709 * There has to be a left pointer at this point because
1710 * otherwise we would have pulled some pointers from the
1711 * right
1712 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001713 if (!left) {
1714 ret = -EROFS;
1715 btrfs_std_error(root->fs_info, ret);
1716 goto enospc;
1717 }
Chris Mason5f39d392007-10-15 16:14:19 -04001718 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001719 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001720 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001721 goto enospc;
1722 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001723 if (wret == 1) {
1724 wret = push_node_left(trans, root, left, mid, 1);
1725 if (wret < 0)
1726 ret = wret;
1727 }
Chris Mason79f95c82007-03-01 15:16:26 -05001728 BUG_ON(wret == 1);
1729 }
Chris Mason5f39d392007-10-15 16:14:19 -04001730 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001731 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001732 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001733 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001734 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001735 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001736 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001737 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001738 } else {
1739 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001740 struct btrfs_disk_key mid_key;
1741 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001742 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1743 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001744 btrfs_set_node_key(parent, &mid_key, pslot);
1745 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001746 }
Chris Masonbb803952007-03-01 12:04:21 -05001747
Chris Mason79f95c82007-03-01 15:16:26 -05001748 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001749 if (left) {
1750 if (btrfs_header_nritems(left) > orig_slot) {
1751 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001752 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001753 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001754 path->slots[level + 1] -= 1;
1755 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001756 if (mid) {
1757 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001758 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001759 }
Chris Masonbb803952007-03-01 12:04:21 -05001760 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001761 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001762 path->slots[level] = orig_slot;
1763 }
1764 }
Chris Mason79f95c82007-03-01 15:16:26 -05001765 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001766 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001767 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001768 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001769enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001770 if (right) {
1771 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001772 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001773 }
1774 if (left) {
1775 if (path->nodes[level] != left)
1776 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001777 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001778 }
Chris Masonbb803952007-03-01 12:04:21 -05001779 return ret;
1780}
1781
Chris Masond352ac62008-09-29 15:18:18 -04001782/* Node balancing for insertion. Here we only split or push nodes around
1783 * when they are completely full. This is also done top down, so we
1784 * have to be pessimistic.
1785 */
Chris Masond3977122009-01-05 21:25:51 -05001786static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001787 struct btrfs_root *root,
1788 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001789{
Chris Mason5f39d392007-10-15 16:14:19 -04001790 struct extent_buffer *right = NULL;
1791 struct extent_buffer *mid;
1792 struct extent_buffer *left = NULL;
1793 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001794 int ret = 0;
1795 int wret;
1796 int pslot;
1797 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001798
1799 if (level == 0)
1800 return 1;
1801
Chris Mason5f39d392007-10-15 16:14:19 -04001802 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001803 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001804
Li Zefana05a9bb2011-09-06 16:55:34 +08001805 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001806 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001807 pslot = path->slots[level + 1];
1808 }
Chris Masone66f7092007-04-20 13:16:02 -04001809
Chris Mason5f39d392007-10-15 16:14:19 -04001810 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001811 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001812
Chris Mason5f39d392007-10-15 16:14:19 -04001813 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001814
1815 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001816 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001817 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001818
1819 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001820 btrfs_set_lock_blocking(left);
1821
Chris Mason5f39d392007-10-15 16:14:19 -04001822 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001823 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1824 wret = 1;
1825 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001826 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001827 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001828 if (ret)
1829 wret = 1;
1830 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001831 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001832 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001833 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001834 }
Chris Masone66f7092007-04-20 13:16:02 -04001835 if (wret < 0)
1836 ret = wret;
1837 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001838 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001839 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001840 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001841 tree_mod_log_set_node_key(root->fs_info, parent,
1842 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001843 btrfs_set_node_key(parent, &disk_key, pslot);
1844 btrfs_mark_buffer_dirty(parent);
1845 if (btrfs_header_nritems(left) > orig_slot) {
1846 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001847 path->slots[level + 1] -= 1;
1848 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001849 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001850 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001851 } else {
1852 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001853 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001854 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001855 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001856 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001857 }
Chris Masone66f7092007-04-20 13:16:02 -04001858 return 0;
1859 }
Chris Mason925baed2008-06-25 16:01:30 -04001860 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001861 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001862 }
Chris Mason925baed2008-06-25 16:01:30 -04001863 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001864
1865 /*
1866 * then try to empty the right most buffer into the middle
1867 */
Chris Mason5f39d392007-10-15 16:14:19 -04001868 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001869 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001870
Chris Mason925baed2008-06-25 16:01:30 -04001871 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001872 btrfs_set_lock_blocking(right);
1873
Chris Mason5f39d392007-10-15 16:14:19 -04001874 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001875 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1876 wret = 1;
1877 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001878 ret = btrfs_cow_block(trans, root, right,
1879 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001880 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001881 if (ret)
1882 wret = 1;
1883 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001884 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001885 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001886 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001887 }
Chris Masone66f7092007-04-20 13:16:02 -04001888 if (wret < 0)
1889 ret = wret;
1890 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001891 struct btrfs_disk_key disk_key;
1892
1893 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001894 tree_mod_log_set_node_key(root->fs_info, parent,
1895 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001896 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1897 btrfs_mark_buffer_dirty(parent);
1898
1899 if (btrfs_header_nritems(mid) <= orig_slot) {
1900 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001901 path->slots[level + 1] += 1;
1902 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001903 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001904 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001905 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001906 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001907 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001908 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001909 }
Chris Masone66f7092007-04-20 13:16:02 -04001910 return 0;
1911 }
Chris Mason925baed2008-06-25 16:01:30 -04001912 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001913 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001914 }
Chris Masone66f7092007-04-20 13:16:02 -04001915 return 1;
1916}
1917
Chris Mason74123bd2007-02-02 11:05:29 -05001918/*
Chris Masond352ac62008-09-29 15:18:18 -04001919 * readahead one full node of leaves, finding things that are close
1920 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001921 */
Chris Masonc8c42862009-04-03 10:14:18 -04001922static void reada_for_search(struct btrfs_root *root,
1923 struct btrfs_path *path,
1924 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001925{
Chris Mason5f39d392007-10-15 16:14:19 -04001926 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001927 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001928 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001929 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001930 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001931 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001932 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001933 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001934 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001935 u32 nr;
1936 u32 blocksize;
1937 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001938
Chris Masona6b6e752007-10-15 16:22:39 -04001939 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001940 return;
1941
Chris Mason6702ed42007-08-07 16:15:09 -04001942 if (!path->nodes[level])
1943 return;
1944
Chris Mason5f39d392007-10-15 16:14:19 -04001945 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001946
Chris Mason3c69fae2007-08-07 15:52:22 -04001947 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001948 blocksize = btrfs_level_size(root, level - 1);
1949 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001950 if (eb) {
1951 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001952 return;
1953 }
1954
Chris Masona7175312009-01-22 09:23:10 -05001955 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001956
Chris Mason5f39d392007-10-15 16:14:19 -04001957 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001958 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001959
Chris Masond3977122009-01-05 21:25:51 -05001960 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001961 if (direction < 0) {
1962 if (nr == 0)
1963 break;
1964 nr--;
1965 } else if (direction > 0) {
1966 nr++;
1967 if (nr >= nritems)
1968 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001969 }
Chris Mason01f46652007-12-21 16:24:26 -05001970 if (path->reada < 0 && objectid) {
1971 btrfs_node_key(node, &disk_key, nr);
1972 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1973 break;
1974 }
Chris Mason6b800532007-10-15 16:17:34 -04001975 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001976 if ((search <= target && target - search <= 65536) ||
1977 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001978 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001979 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001980 nread += blocksize;
1981 }
1982 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001983 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001984 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001985 }
1986}
Chris Mason925baed2008-06-25 16:01:30 -04001987
Chris Masond352ac62008-09-29 15:18:18 -04001988/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001989 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1990 * cache
1991 */
1992static noinline int reada_for_balance(struct btrfs_root *root,
1993 struct btrfs_path *path, int level)
1994{
1995 int slot;
1996 int nritems;
1997 struct extent_buffer *parent;
1998 struct extent_buffer *eb;
1999 u64 gen;
2000 u64 block1 = 0;
2001 u64 block2 = 0;
2002 int ret = 0;
2003 int blocksize;
2004
Chris Mason8c594ea2009-04-20 15:50:10 -04002005 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002006 if (!parent)
2007 return 0;
2008
2009 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002010 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002011 blocksize = btrfs_level_size(root, level);
2012
2013 if (slot > 0) {
2014 block1 = btrfs_node_blockptr(parent, slot - 1);
2015 gen = btrfs_node_ptr_generation(parent, slot - 1);
2016 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002017 /*
2018 * if we get -eagain from btrfs_buffer_uptodate, we
2019 * don't want to return eagain here. That will loop
2020 * forever
2021 */
2022 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002023 block1 = 0;
2024 free_extent_buffer(eb);
2025 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002026 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002027 block2 = btrfs_node_blockptr(parent, slot + 1);
2028 gen = btrfs_node_ptr_generation(parent, slot + 1);
2029 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002030 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002031 block2 = 0;
2032 free_extent_buffer(eb);
2033 }
2034 if (block1 || block2) {
2035 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002036
2037 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002038 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002039
2040 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002041 if (block1)
2042 readahead_tree_block(root, block1, blocksize, 0);
2043 if (block2)
2044 readahead_tree_block(root, block2, blocksize, 0);
2045
2046 if (block1) {
2047 eb = read_tree_block(root, block1, blocksize, 0);
2048 free_extent_buffer(eb);
2049 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002050 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002051 eb = read_tree_block(root, block2, blocksize, 0);
2052 free_extent_buffer(eb);
2053 }
2054 }
2055 return ret;
2056}
2057
2058
2059/*
Chris Masond3977122009-01-05 21:25:51 -05002060 * when we walk down the tree, it is usually safe to unlock the higher layers
2061 * in the tree. The exceptions are when our path goes through slot 0, because
2062 * operations on the tree might require changing key pointers higher up in the
2063 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002064 *
Chris Masond3977122009-01-05 21:25:51 -05002065 * callers might also have set path->keep_locks, which tells this code to keep
2066 * the lock if the path points to the last slot in the block. This is part of
2067 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002068 *
Chris Masond3977122009-01-05 21:25:51 -05002069 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2070 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002071 */
Chris Masone02119d2008-09-05 16:13:11 -04002072static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002073 int lowest_unlock, int min_write_lock_level,
2074 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002075{
2076 int i;
2077 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002078 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002079 struct extent_buffer *t;
2080
2081 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2082 if (!path->nodes[i])
2083 break;
2084 if (!path->locks[i])
2085 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002086 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002087 skip_level = i + 1;
2088 continue;
2089 }
Chris Mason051e1b92008-06-25 16:01:30 -04002090 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002091 u32 nritems;
2092 t = path->nodes[i];
2093 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002094 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002095 skip_level = i + 1;
2096 continue;
2097 }
2098 }
Chris Mason051e1b92008-06-25 16:01:30 -04002099 if (skip_level < i && i >= lowest_unlock)
2100 no_skips = 1;
2101
Chris Mason925baed2008-06-25 16:01:30 -04002102 t = path->nodes[i];
2103 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002104 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002105 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002106 if (write_lock_level &&
2107 i > min_write_lock_level &&
2108 i <= *write_lock_level) {
2109 *write_lock_level = i - 1;
2110 }
Chris Mason925baed2008-06-25 16:01:30 -04002111 }
2112 }
2113}
2114
Chris Mason3c69fae2007-08-07 15:52:22 -04002115/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002116 * This releases any locks held in the path starting at level and
2117 * going all the way up to the root.
2118 *
2119 * btrfs_search_slot will keep the lock held on higher nodes in a few
2120 * corner cases, such as COW of the block at slot zero in the node. This
2121 * ignores those rules, and it should only be called when there are no
2122 * more updates to be done higher up in the tree.
2123 */
2124noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2125{
2126 int i;
2127
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002128 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002129 return;
2130
2131 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2132 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002133 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002134 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002135 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002136 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002137 path->locks[i] = 0;
2138 }
2139}
2140
2141/*
Chris Masonc8c42862009-04-03 10:14:18 -04002142 * helper function for btrfs_search_slot. The goal is to find a block
2143 * in cache without setting the path to blocking. If we find the block
2144 * we return zero and the path is unchanged.
2145 *
2146 * If we can't find the block, we set the path blocking and do some
2147 * reada. -EAGAIN is returned and the search must be repeated.
2148 */
2149static int
2150read_block_for_search(struct btrfs_trans_handle *trans,
2151 struct btrfs_root *root, struct btrfs_path *p,
2152 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002153 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002154{
2155 u64 blocknr;
2156 u64 gen;
2157 u32 blocksize;
2158 struct extent_buffer *b = *eb_ret;
2159 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002160 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002161
2162 blocknr = btrfs_node_blockptr(b, slot);
2163 gen = btrfs_node_ptr_generation(b, slot);
2164 blocksize = btrfs_level_size(root, level - 1);
2165
2166 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002167 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002168 /* first we do an atomic uptodate check */
2169 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2170 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002171 /*
2172 * we found an up to date block without
2173 * sleeping, return
2174 * right away
2175 */
2176 *eb_ret = tmp;
2177 return 0;
2178 }
2179 /* the pages were up to date, but we failed
2180 * the generation number check. Do a full
2181 * read for the generation number that is correct.
2182 * We must do this without dropping locks so
2183 * we can trust our generation number
2184 */
2185 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002186 btrfs_set_path_blocking(p);
2187
Chris Masonb9fab912012-05-06 07:23:47 -04002188 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002189 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002190 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002191 *eb_ret = tmp;
2192 return 0;
2193 }
2194 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002195 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002196 return -EIO;
2197 }
Chris Masonc8c42862009-04-03 10:14:18 -04002198 }
2199
2200 /*
2201 * reduce lock contention at high levels
2202 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002203 * we read. Don't release the lock on the current
2204 * level because we need to walk this node to figure
2205 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002206 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002207 btrfs_unlock_up_safe(p, level + 1);
2208 btrfs_set_path_blocking(p);
2209
Chris Masoncb449212010-10-24 11:01:27 -04002210 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002211 if (p->reada)
2212 reada_for_search(root, p, level, slot, key->objectid);
2213
David Sterbab3b4aa72011-04-21 01:20:15 +02002214 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002215
2216 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002217 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002218 if (tmp) {
2219 /*
2220 * If the read above didn't mark this buffer up to date,
2221 * it will never end up being up to date. Set ret to EIO now
2222 * and give up so that our caller doesn't loop forever
2223 * on our EAGAINs.
2224 */
Chris Masonb9fab912012-05-06 07:23:47 -04002225 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002226 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002227 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002228 }
2229 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002230}
2231
2232/*
2233 * helper function for btrfs_search_slot. This does all of the checks
2234 * for node-level blocks and does any balancing required based on
2235 * the ins_len.
2236 *
2237 * If no extra work was required, zero is returned. If we had to
2238 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2239 * start over
2240 */
2241static int
2242setup_nodes_for_search(struct btrfs_trans_handle *trans,
2243 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002244 struct extent_buffer *b, int level, int ins_len,
2245 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002246{
2247 int ret;
2248 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2249 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2250 int sret;
2251
Chris Masonbd681512011-07-16 15:23:14 -04002252 if (*write_lock_level < level + 1) {
2253 *write_lock_level = level + 1;
2254 btrfs_release_path(p);
2255 goto again;
2256 }
2257
Chris Masonc8c42862009-04-03 10:14:18 -04002258 sret = reada_for_balance(root, p, level);
2259 if (sret)
2260 goto again;
2261
2262 btrfs_set_path_blocking(p);
2263 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002264 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002265
2266 BUG_ON(sret > 0);
2267 if (sret) {
2268 ret = sret;
2269 goto done;
2270 }
2271 b = p->nodes[level];
2272 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002273 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002274 int sret;
2275
Chris Masonbd681512011-07-16 15:23:14 -04002276 if (*write_lock_level < level + 1) {
2277 *write_lock_level = level + 1;
2278 btrfs_release_path(p);
2279 goto again;
2280 }
2281
Chris Masonc8c42862009-04-03 10:14:18 -04002282 sret = reada_for_balance(root, p, level);
2283 if (sret)
2284 goto again;
2285
2286 btrfs_set_path_blocking(p);
2287 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002288 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002289
2290 if (sret) {
2291 ret = sret;
2292 goto done;
2293 }
2294 b = p->nodes[level];
2295 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002296 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002297 goto again;
2298 }
2299 BUG_ON(btrfs_header_nritems(b) == 1);
2300 }
2301 return 0;
2302
2303again:
2304 ret = -EAGAIN;
2305done:
2306 return ret;
2307}
2308
2309/*
Chris Mason74123bd2007-02-02 11:05:29 -05002310 * look for key in the tree. path is filled in with nodes along the way
2311 * if key is found, we return zero and you can find the item in the leaf
2312 * level of the path (level 0)
2313 *
2314 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002315 * be inserted, and 1 is returned. If there are other errors during the
2316 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002317 *
2318 * if ins_len > 0, nodes and leaves will be split as we walk down the
2319 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2320 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002321 */
Chris Masone089f052007-03-16 16:20:31 -04002322int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2323 *root, struct btrfs_key *key, struct btrfs_path *p, int
2324 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002325{
Chris Mason5f39d392007-10-15 16:14:19 -04002326 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002327 int slot;
2328 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002329 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002330 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002331 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002332 int root_lock;
2333 /* everything at write_lock_level or lower must be write locked */
2334 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002335 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002336 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002337
Chris Mason6702ed42007-08-07 16:15:09 -04002338 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002339 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002340 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002341
Chris Masonbd681512011-07-16 15:23:14 -04002342 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002343 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002344
Chris Masonbd681512011-07-16 15:23:14 -04002345 /* when we are removing items, we might have to go up to level
2346 * two as we update tree pointers Make sure we keep write
2347 * for those levels as well
2348 */
2349 write_lock_level = 2;
2350 } else if (ins_len > 0) {
2351 /*
2352 * for inserting items, make sure we have a write lock on
2353 * level 1 so we can update keys
2354 */
2355 write_lock_level = 1;
2356 }
2357
2358 if (!cow)
2359 write_lock_level = -1;
2360
2361 if (cow && (p->keep_locks || p->lowest_level))
2362 write_lock_level = BTRFS_MAX_LEVEL;
2363
Chris Masonf7c79f32012-03-19 15:54:38 -04002364 min_write_lock_level = write_lock_level;
2365
Chris Masonbb803952007-03-01 12:04:21 -05002366again:
Chris Masonbd681512011-07-16 15:23:14 -04002367 /*
2368 * we try very hard to do read locks on the root
2369 */
2370 root_lock = BTRFS_READ_LOCK;
2371 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002373 /*
2374 * the commit roots are read only
2375 * so we always do read locks
2376 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002377 b = root->commit_root;
2378 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002379 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002380 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002381 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002382 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002383 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002384 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002385 level = btrfs_header_level(b);
2386 } else {
2387 /* we don't know the level of the root node
2388 * until we actually have it read locked
2389 */
2390 b = btrfs_read_lock_root_node(root);
2391 level = btrfs_header_level(b);
2392 if (level <= write_lock_level) {
2393 /* whoops, must trade for write lock */
2394 btrfs_tree_read_unlock(b);
2395 free_extent_buffer(b);
2396 b = btrfs_lock_root_node(root);
2397 root_lock = BTRFS_WRITE_LOCK;
2398
2399 /* the level might have changed, check again */
2400 level = btrfs_header_level(b);
2401 }
2402 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002403 }
Chris Masonbd681512011-07-16 15:23:14 -04002404 p->nodes[level] = b;
2405 if (!p->skip_locking)
2406 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002407
Chris Masoneb60cea2007-02-02 09:18:22 -05002408 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002409 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002410
2411 /*
2412 * setup the path here so we can release it under lock
2413 * contention with the cow code
2414 */
Chris Mason02217ed2007-03-02 16:08:05 -05002415 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002416 /*
2417 * if we don't really need to cow this block
2418 * then we don't want to set the path blocking,
2419 * so we test it here
2420 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002421 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002422 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002423
Chris Masonb4ce94d2009-02-04 09:25:08 -05002424 btrfs_set_path_blocking(p);
2425
Chris Masonbd681512011-07-16 15:23:14 -04002426 /*
2427 * must have write locks on this node and the
2428 * parent
2429 */
2430 if (level + 1 > write_lock_level) {
2431 write_lock_level = level + 1;
2432 btrfs_release_path(p);
2433 goto again;
2434 }
2435
Yan Zheng33c66f42009-07-22 09:59:00 -04002436 err = btrfs_cow_block(trans, root, b,
2437 p->nodes[level + 1],
2438 p->slots[level + 1], &b);
2439 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002440 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002441 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002442 }
Chris Mason02217ed2007-03-02 16:08:05 -05002443 }
Chris Mason65b51a02008-08-01 15:11:20 -04002444cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002445 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002446
Chris Masoneb60cea2007-02-02 09:18:22 -05002447 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002448 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002449
2450 /*
2451 * we have a lock on b and as long as we aren't changing
2452 * the tree, there is no way to for the items in b to change.
2453 * It is safe to drop the lock on our parent before we
2454 * go through the expensive btree search on b.
2455 *
2456 * If cow is true, then we might be changing slot zero,
2457 * which may require changing the parent. So, we can't
2458 * drop the lock until after we know which slot we're
2459 * operating on.
2460 */
2461 if (!cow)
2462 btrfs_unlock_up_safe(p, level + 1);
2463
Chris Mason5f39d392007-10-15 16:14:19 -04002464 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002465
Chris Mason5f39d392007-10-15 16:14:19 -04002466 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002467 int dec = 0;
2468 if (ret && slot > 0) {
2469 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002470 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002471 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002472 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002473 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002474 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002475 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002476 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002477 if (err) {
2478 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002479 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002480 }
Chris Masonc8c42862009-04-03 10:14:18 -04002481 b = p->nodes[level];
2482 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002483
Chris Masonbd681512011-07-16 15:23:14 -04002484 /*
2485 * slot 0 is special, if we change the key
2486 * we have to update the parent pointer
2487 * which means we must have a write lock
2488 * on the parent
2489 */
2490 if (slot == 0 && cow &&
2491 write_lock_level < level + 1) {
2492 write_lock_level = level + 1;
2493 btrfs_release_path(p);
2494 goto again;
2495 }
2496
Chris Masonf7c79f32012-03-19 15:54:38 -04002497 unlock_up(p, level, lowest_unlock,
2498 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002499
Chris Mason925baed2008-06-25 16:01:30 -04002500 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002501 if (dec)
2502 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002503 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002504 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002505
Yan Zheng33c66f42009-07-22 09:59:00 -04002506 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002507 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002508 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002509 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002510 if (err) {
2511 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002512 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002513 }
Chris Mason76a05b32009-05-14 13:24:30 -04002514
Chris Masonb4ce94d2009-02-04 09:25:08 -05002515 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002516 level = btrfs_header_level(b);
2517 if (level <= write_lock_level) {
2518 err = btrfs_try_tree_write_lock(b);
2519 if (!err) {
2520 btrfs_set_path_blocking(p);
2521 btrfs_tree_lock(b);
2522 btrfs_clear_path_blocking(p, b,
2523 BTRFS_WRITE_LOCK);
2524 }
2525 p->locks[level] = BTRFS_WRITE_LOCK;
2526 } else {
2527 err = btrfs_try_tree_read_lock(b);
2528 if (!err) {
2529 btrfs_set_path_blocking(p);
2530 btrfs_tree_read_lock(b);
2531 btrfs_clear_path_blocking(p, b,
2532 BTRFS_READ_LOCK);
2533 }
2534 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002535 }
Chris Masonbd681512011-07-16 15:23:14 -04002536 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002537 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002538 } else {
2539 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002540 if (ins_len > 0 &&
2541 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002542 if (write_lock_level < 1) {
2543 write_lock_level = 1;
2544 btrfs_release_path(p);
2545 goto again;
2546 }
2547
Chris Masonb4ce94d2009-02-04 09:25:08 -05002548 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002549 err = split_leaf(trans, root, key,
2550 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002551 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002552
Yan Zheng33c66f42009-07-22 09:59:00 -04002553 BUG_ON(err > 0);
2554 if (err) {
2555 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002556 goto done;
2557 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002558 }
Chris Mason459931e2008-12-10 09:10:46 -05002559 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002560 unlock_up(p, level, lowest_unlock,
2561 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002562 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002563 }
2564 }
Chris Mason65b51a02008-08-01 15:11:20 -04002565 ret = 1;
2566done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002567 /*
2568 * we don't really know what they plan on doing with the path
2569 * from here on, so for now just mark it as blocking
2570 */
Chris Masonb9473432009-03-13 11:00:37 -04002571 if (!p->leave_spinning)
2572 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002573 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002574 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002575 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002576}
2577
Chris Mason74123bd2007-02-02 11:05:29 -05002578/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002579 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2580 * current state of the tree together with the operations recorded in the tree
2581 * modification log to search for the key in a previous version of this tree, as
2582 * denoted by the time_seq parameter.
2583 *
2584 * Naturally, there is no support for insert, delete or cow operations.
2585 *
2586 * The resulting path and return value will be set up as if we called
2587 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2588 */
2589int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2590 struct btrfs_path *p, u64 time_seq)
2591{
2592 struct extent_buffer *b;
2593 int slot;
2594 int ret;
2595 int err;
2596 int level;
2597 int lowest_unlock = 1;
2598 u8 lowest_level = 0;
2599
2600 lowest_level = p->lowest_level;
2601 WARN_ON(p->nodes[0] != NULL);
2602
2603 if (p->search_commit_root) {
2604 BUG_ON(time_seq);
2605 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2606 }
2607
2608again:
2609 level = 0;
2610 b = get_old_root(root, time_seq);
2611 extent_buffer_get(b);
2612 level = btrfs_header_level(b);
2613 btrfs_tree_read_lock(b);
2614 p->locks[level] = BTRFS_READ_LOCK;
2615
2616 while (b) {
2617 level = btrfs_header_level(b);
2618 p->nodes[level] = b;
2619 btrfs_clear_path_blocking(p, NULL, 0);
2620
2621 /*
2622 * we have a lock on b and as long as we aren't changing
2623 * the tree, there is no way to for the items in b to change.
2624 * It is safe to drop the lock on our parent before we
2625 * go through the expensive btree search on b.
2626 */
2627 btrfs_unlock_up_safe(p, level + 1);
2628
2629 ret = bin_search(b, key, level, &slot);
2630
2631 if (level != 0) {
2632 int dec = 0;
2633 if (ret && slot > 0) {
2634 dec = 1;
2635 slot -= 1;
2636 }
2637 p->slots[level] = slot;
2638 unlock_up(p, level, lowest_unlock, 0, NULL);
2639
2640 if (level == lowest_level) {
2641 if (dec)
2642 p->slots[level]++;
2643 goto done;
2644 }
2645
2646 err = read_block_for_search(NULL, root, p, &b, level,
2647 slot, key, time_seq);
2648 if (err == -EAGAIN)
2649 goto again;
2650 if (err) {
2651 ret = err;
2652 goto done;
2653 }
2654
2655 level = btrfs_header_level(b);
2656 err = btrfs_try_tree_read_lock(b);
2657 if (!err) {
2658 btrfs_set_path_blocking(p);
2659 btrfs_tree_read_lock(b);
2660 btrfs_clear_path_blocking(p, b,
2661 BTRFS_READ_LOCK);
2662 }
2663 p->locks[level] = BTRFS_READ_LOCK;
2664 p->nodes[level] = b;
2665 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2666 if (b != p->nodes[level]) {
2667 btrfs_tree_unlock_rw(p->nodes[level],
2668 p->locks[level]);
2669 p->locks[level] = 0;
2670 p->nodes[level] = b;
2671 }
2672 } else {
2673 p->slots[level] = slot;
2674 unlock_up(p, level, lowest_unlock, 0, NULL);
2675 goto done;
2676 }
2677 }
2678 ret = 1;
2679done:
2680 if (!p->leave_spinning)
2681 btrfs_set_path_blocking(p);
2682 if (ret < 0)
2683 btrfs_release_path(p);
2684
2685 return ret;
2686}
2687
2688/*
Chris Mason74123bd2007-02-02 11:05:29 -05002689 * adjust the pointers going up the tree, starting at level
2690 * making sure the right key of each node is points to 'key'.
2691 * This is used after shifting pointers to the left, so it stops
2692 * fixing up pointers when a given leaf/node is not in slot 0 of the
2693 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002694 *
Chris Mason74123bd2007-02-02 11:05:29 -05002695 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002696static void fixup_low_keys(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root, struct btrfs_path *path,
2698 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002699{
2700 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002701 struct extent_buffer *t;
2702
Chris Mason234b63a2007-03-13 10:46:10 -04002703 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002704 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002705 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002706 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002707 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002708 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002709 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002710 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002711 if (tslot != 0)
2712 break;
2713 }
2714}
2715
Chris Mason74123bd2007-02-02 11:05:29 -05002716/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002717 * update item key.
2718 *
2719 * This function isn't completely safe. It's the caller's responsibility
2720 * that the new key won't break the order
2721 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002722void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2723 struct btrfs_root *root, struct btrfs_path *path,
2724 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002725{
2726 struct btrfs_disk_key disk_key;
2727 struct extent_buffer *eb;
2728 int slot;
2729
2730 eb = path->nodes[0];
2731 slot = path->slots[0];
2732 if (slot > 0) {
2733 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002734 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002735 }
2736 if (slot < btrfs_header_nritems(eb) - 1) {
2737 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002738 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002739 }
2740
2741 btrfs_cpu_key_to_disk(&disk_key, new_key);
2742 btrfs_set_item_key(eb, &disk_key, slot);
2743 btrfs_mark_buffer_dirty(eb);
2744 if (slot == 0)
2745 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002746}
2747
2748/*
Chris Mason74123bd2007-02-02 11:05:29 -05002749 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002750 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002751 *
2752 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2753 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002754 */
Chris Mason98ed5172008-01-03 10:01:48 -05002755static int push_node_left(struct btrfs_trans_handle *trans,
2756 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002757 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002758{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002759 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002760 int src_nritems;
2761 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002762 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002763
Chris Mason5f39d392007-10-15 16:14:19 -04002764 src_nritems = btrfs_header_nritems(src);
2765 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002766 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002767 WARN_ON(btrfs_header_generation(src) != trans->transid);
2768 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002769
Chris Masonbce4eae2008-04-24 14:42:46 -04002770 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002771 return 1;
2772
Chris Masond3977122009-01-05 21:25:51 -05002773 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002774 return 1;
2775
Chris Masonbce4eae2008-04-24 14:42:46 -04002776 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002777 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002778 if (push_items < src_nritems) {
2779 /* leave at least 8 pointers in the node if
2780 * we aren't going to empty it
2781 */
2782 if (src_nritems - push_items < 8) {
2783 if (push_items <= 8)
2784 return 1;
2785 push_items -= 8;
2786 }
2787 }
2788 } else
2789 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002790
Jan Schmidtf2304752012-05-26 11:43:17 +02002791 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2792 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002793 copy_extent_buffer(dst, src,
2794 btrfs_node_key_ptr_offset(dst_nritems),
2795 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002796 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002797
Chris Masonbb803952007-03-01 12:04:21 -05002798 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002799 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2800 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002801 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2802 btrfs_node_key_ptr_offset(push_items),
2803 (src_nritems - push_items) *
2804 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002805 }
Chris Mason5f39d392007-10-15 16:14:19 -04002806 btrfs_set_header_nritems(src, src_nritems - push_items);
2807 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2808 btrfs_mark_buffer_dirty(src);
2809 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002810
Chris Masonbb803952007-03-01 12:04:21 -05002811 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002812}
2813
Chris Mason97571fd2007-02-24 13:39:08 -05002814/*
Chris Mason79f95c82007-03-01 15:16:26 -05002815 * try to push data from one node into the next node right in the
2816 * tree.
2817 *
2818 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2819 * error, and > 0 if there was no room in the right hand block.
2820 *
2821 * this will only push up to 1/2 the contents of the left node over
2822 */
Chris Mason5f39d392007-10-15 16:14:19 -04002823static int balance_node_right(struct btrfs_trans_handle *trans,
2824 struct btrfs_root *root,
2825 struct extent_buffer *dst,
2826 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002827{
Chris Mason79f95c82007-03-01 15:16:26 -05002828 int push_items = 0;
2829 int max_push;
2830 int src_nritems;
2831 int dst_nritems;
2832 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002833
Chris Mason7bb86312007-12-11 09:25:06 -05002834 WARN_ON(btrfs_header_generation(src) != trans->transid);
2835 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2836
Chris Mason5f39d392007-10-15 16:14:19 -04002837 src_nritems = btrfs_header_nritems(src);
2838 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002839 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002840 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002841 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002842
Chris Masond3977122009-01-05 21:25:51 -05002843 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002844 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002845
2846 max_push = src_nritems / 2 + 1;
2847 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002848 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002849 return 1;
Yan252c38f2007-08-29 09:11:44 -04002850
Chris Mason79f95c82007-03-01 15:16:26 -05002851 if (max_push < push_items)
2852 push_items = max_push;
2853
Jan Schmidtf2304752012-05-26 11:43:17 +02002854 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002855 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2856 btrfs_node_key_ptr_offset(0),
2857 (dst_nritems) *
2858 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002859
Jan Schmidtf2304752012-05-26 11:43:17 +02002860 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2861 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002862 copy_extent_buffer(dst, src,
2863 btrfs_node_key_ptr_offset(0),
2864 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002865 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002866
Chris Mason5f39d392007-10-15 16:14:19 -04002867 btrfs_set_header_nritems(src, src_nritems - push_items);
2868 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002869
Chris Mason5f39d392007-10-15 16:14:19 -04002870 btrfs_mark_buffer_dirty(src);
2871 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002872
Chris Mason79f95c82007-03-01 15:16:26 -05002873 return ret;
2874}
2875
2876/*
Chris Mason97571fd2007-02-24 13:39:08 -05002877 * helper function to insert a new root level in the tree.
2878 * A new node is allocated, and a single item is inserted to
2879 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002880 *
2881 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002882 */
Chris Masond3977122009-01-05 21:25:51 -05002883static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002884 struct btrfs_root *root,
2885 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002886{
Chris Mason7bb86312007-12-11 09:25:06 -05002887 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002888 struct extent_buffer *lower;
2889 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002890 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002891 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002892
2893 BUG_ON(path->nodes[level]);
2894 BUG_ON(path->nodes[level-1] != root->node);
2895
Chris Mason7bb86312007-12-11 09:25:06 -05002896 lower = path->nodes[level-1];
2897 if (level == 1)
2898 btrfs_item_key(lower, &lower_key, 0);
2899 else
2900 btrfs_node_key(lower, &lower_key, 0);
2901
Zheng Yan31840ae2008-09-23 13:14:14 -04002902 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002903 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002904 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002905 if (IS_ERR(c))
2906 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002907
Yan, Zhengf0486c62010-05-16 10:46:25 -04002908 root_add_used(root, root->nodesize);
2909
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002910 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002911 btrfs_set_header_nritems(c, 1);
2912 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002913 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002914 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002915 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002916 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002917
Chris Mason5f39d392007-10-15 16:14:19 -04002918 write_extent_buffer(c, root->fs_info->fsid,
2919 (unsigned long)btrfs_header_fsid(c),
2920 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002921
2922 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2923 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2924 BTRFS_UUID_SIZE);
2925
Chris Mason5f39d392007-10-15 16:14:19 -04002926 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002927 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002928 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002929 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002930
2931 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002932
2933 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002934
Chris Mason925baed2008-06-25 16:01:30 -04002935 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002936 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002937 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002938
2939 /* the super has an extra ref to root->node */
2940 free_extent_buffer(old);
2941
Chris Mason0b86a832008-03-24 15:01:56 -04002942 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002943 extent_buffer_get(c);
2944 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002945 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002946 path->slots[level] = 0;
2947 return 0;
2948}
2949
Chris Mason74123bd2007-02-02 11:05:29 -05002950/*
2951 * worker function to insert a single pointer in a node.
2952 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002953 *
Chris Mason74123bd2007-02-02 11:05:29 -05002954 * slot and level indicate where you want the key to go, and
2955 * blocknr is the block the key points to.
2956 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002957static void insert_ptr(struct btrfs_trans_handle *trans,
2958 struct btrfs_root *root, struct btrfs_path *path,
2959 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002960 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002961{
Chris Mason5f39d392007-10-15 16:14:19 -04002962 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002963 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002964 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002965
2966 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002967 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002968 lower = path->nodes[level];
2969 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002970 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002971 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002972 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002973 if (tree_mod_log && level)
2974 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2975 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002976 memmove_extent_buffer(lower,
2977 btrfs_node_key_ptr_offset(slot + 1),
2978 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002979 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002980 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002981 if (tree_mod_log && level) {
2982 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2983 MOD_LOG_KEY_ADD);
2984 BUG_ON(ret < 0);
2985 }
Chris Mason5f39d392007-10-15 16:14:19 -04002986 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002987 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002988 WARN_ON(trans->transid == 0);
2989 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002990 btrfs_set_header_nritems(lower, nritems + 1);
2991 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002992}
2993
Chris Mason97571fd2007-02-24 13:39:08 -05002994/*
2995 * split the node at the specified level in path in two.
2996 * The path is corrected to point to the appropriate node after the split
2997 *
2998 * Before splitting this tries to make some room in the node by pushing
2999 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003000 *
3001 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003002 */
Chris Masone02119d2008-09-05 16:13:11 -04003003static noinline int split_node(struct btrfs_trans_handle *trans,
3004 struct btrfs_root *root,
3005 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003006{
Chris Mason5f39d392007-10-15 16:14:19 -04003007 struct extent_buffer *c;
3008 struct extent_buffer *split;
3009 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003010 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003011 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003012 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003013
Chris Mason5f39d392007-10-15 16:14:19 -04003014 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003015 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003016 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003017 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003018 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003019 if (ret)
3020 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003021 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003022 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003023 c = path->nodes[level];
3024 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003025 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003026 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003027 if (ret < 0)
3028 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003029 }
Chris Masone66f7092007-04-20 13:16:02 -04003030
Chris Mason5f39d392007-10-15 16:14:19 -04003031 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003032 mid = (c_nritems + 1) / 2;
3033 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003034
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003036 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003037 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003038 if (IS_ERR(split))
3039 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003040
Yan, Zhengf0486c62010-05-16 10:46:25 -04003041 root_add_used(root, root->nodesize);
3042
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003043 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003044 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003045 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003046 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003047 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003048 btrfs_set_header_owner(split, root->root_key.objectid);
3049 write_extent_buffer(split, root->fs_info->fsid,
3050 (unsigned long)btrfs_header_fsid(split),
3051 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003052 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3053 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3054 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003055
Jan Schmidtf2304752012-05-26 11:43:17 +02003056 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003057 copy_extent_buffer(split, c,
3058 btrfs_node_key_ptr_offset(0),
3059 btrfs_node_key_ptr_offset(mid),
3060 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3061 btrfs_set_header_nritems(split, c_nritems - mid);
3062 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003063 ret = 0;
3064
Chris Mason5f39d392007-10-15 16:14:19 -04003065 btrfs_mark_buffer_dirty(c);
3066 btrfs_mark_buffer_dirty(split);
3067
Jeff Mahoney143bede2012-03-01 14:56:26 +01003068 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003069 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003070
Chris Mason5de08d72007-02-24 06:24:44 -05003071 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003072 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003073 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003074 free_extent_buffer(c);
3075 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003076 path->slots[level + 1] += 1;
3077 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003078 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003079 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003080 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003081 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003082}
3083
Chris Mason74123bd2007-02-02 11:05:29 -05003084/*
3085 * how many bytes are required to store the items in a leaf. start
3086 * and nr indicate which items in the leaf to check. This totals up the
3087 * space used both by the item structs and the item data
3088 */
Chris Mason5f39d392007-10-15 16:14:19 -04003089static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003090{
3091 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003092 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003093 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003094
3095 if (!nr)
3096 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003097 data_len = btrfs_item_end_nr(l, start);
3098 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003099 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003100 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003101 return data_len;
3102}
3103
Chris Mason74123bd2007-02-02 11:05:29 -05003104/*
Chris Masond4dbff92007-04-04 14:08:15 -04003105 * The space between the end of the leaf items and
3106 * the start of the leaf data. IOW, how much room
3107 * the leaf has left for both items and data
3108 */
Chris Masond3977122009-01-05 21:25:51 -05003109noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003110 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003111{
Chris Mason5f39d392007-10-15 16:14:19 -04003112 int nritems = btrfs_header_nritems(leaf);
3113 int ret;
3114 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3115 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003116 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3117 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003118 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003119 leaf_space_used(leaf, 0, nritems), nritems);
3120 }
3121 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003122}
3123
Chris Mason99d8f832010-07-07 10:51:48 -04003124/*
3125 * min slot controls the lowest index we're willing to push to the
3126 * right. We'll push up to and including min_slot, but no lower
3127 */
Chris Mason44871b12009-03-13 10:04:31 -04003128static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3129 struct btrfs_root *root,
3130 struct btrfs_path *path,
3131 int data_size, int empty,
3132 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003133 int free_space, u32 left_nritems,
3134 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003135{
Chris Mason5f39d392007-10-15 16:14:19 -04003136 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003137 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003138 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003139 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003140 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003141 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003142 int push_space = 0;
3143 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003144 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003145 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003146 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003147 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003148 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003149
Chris Masoncfed81a2012-03-03 07:40:03 -05003150 btrfs_init_map_token(&token);
3151
Chris Mason34a38212007-11-07 13:31:03 -05003152 if (empty)
3153 nr = 0;
3154 else
Chris Mason99d8f832010-07-07 10:51:48 -04003155 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003156
Zheng Yan31840ae2008-09-23 13:14:14 -04003157 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003158 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003159
Chris Mason44871b12009-03-13 10:04:31 -04003160 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003161 i = left_nritems - 1;
3162 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003163 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003164
Zheng Yan31840ae2008-09-23 13:14:14 -04003165 if (!empty && push_items > 0) {
3166 if (path->slots[0] > i)
3167 break;
3168 if (path->slots[0] == i) {
3169 int space = btrfs_leaf_free_space(root, left);
3170 if (space + push_space * 2 > free_space)
3171 break;
3172 }
3173 }
3174
Chris Mason00ec4c52007-02-24 12:47:20 -05003175 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003176 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003177
Chris Masondb945352007-10-15 16:15:53 -04003178 this_item_size = btrfs_item_size(left, item);
3179 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003180 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003181
Chris Mason00ec4c52007-02-24 12:47:20 -05003182 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003183 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003184 if (i == 0)
3185 break;
3186 i--;
Chris Masondb945352007-10-15 16:15:53 -04003187 }
Chris Mason5f39d392007-10-15 16:14:19 -04003188
Chris Mason925baed2008-06-25 16:01:30 -04003189 if (push_items == 0)
3190 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003191
Chris Mason34a38212007-11-07 13:31:03 -05003192 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003193 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003194
Chris Mason00ec4c52007-02-24 12:47:20 -05003195 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003196 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003197
Chris Mason5f39d392007-10-15 16:14:19 -04003198 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003199 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003200
Chris Mason00ec4c52007-02-24 12:47:20 -05003201 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003202 data_end = leaf_data_end(root, right);
3203 memmove_extent_buffer(right,
3204 btrfs_leaf_data(right) + data_end - push_space,
3205 btrfs_leaf_data(right) + data_end,
3206 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3207
Chris Mason00ec4c52007-02-24 12:47:20 -05003208 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003209 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003210 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3211 btrfs_leaf_data(left) + leaf_data_end(root, left),
3212 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003213
3214 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3215 btrfs_item_nr_offset(0),
3216 right_nritems * sizeof(struct btrfs_item));
3217
Chris Mason00ec4c52007-02-24 12:47:20 -05003218 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003219 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3220 btrfs_item_nr_offset(left_nritems - push_items),
3221 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003222
3223 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003224 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003226 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003227 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003228 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003229 push_space -= btrfs_token_item_size(right, item, &token);
3230 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003231 }
3232
Chris Mason7518a232007-03-12 12:01:18 -04003233 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003234 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003235
Chris Mason34a38212007-11-07 13:31:03 -05003236 if (left_nritems)
3237 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003238 else
3239 clean_tree_block(trans, root, left);
3240
Chris Mason5f39d392007-10-15 16:14:19 -04003241 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003242
Chris Mason5f39d392007-10-15 16:14:19 -04003243 btrfs_item_key(right, &disk_key, 0);
3244 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003245 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003246
Chris Mason00ec4c52007-02-24 12:47:20 -05003247 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003248 if (path->slots[0] >= left_nritems) {
3249 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003250 if (btrfs_header_nritems(path->nodes[0]) == 0)
3251 clean_tree_block(trans, root, path->nodes[0]);
3252 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003253 free_extent_buffer(path->nodes[0]);
3254 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003255 path->slots[1] += 1;
3256 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003257 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003258 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003259 }
3260 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003261
3262out_unlock:
3263 btrfs_tree_unlock(right);
3264 free_extent_buffer(right);
3265 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003266}
Chris Mason925baed2008-06-25 16:01:30 -04003267
Chris Mason00ec4c52007-02-24 12:47:20 -05003268/*
Chris Mason44871b12009-03-13 10:04:31 -04003269 * push some data in the path leaf to the right, trying to free up at
3270 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3271 *
3272 * returns 1 if the push failed because the other node didn't have enough
3273 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003274 *
3275 * this will push starting from min_slot to the end of the leaf. It won't
3276 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003277 */
3278static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003279 *root, struct btrfs_path *path,
3280 int min_data_size, int data_size,
3281 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003282{
3283 struct extent_buffer *left = path->nodes[0];
3284 struct extent_buffer *right;
3285 struct extent_buffer *upper;
3286 int slot;
3287 int free_space;
3288 u32 left_nritems;
3289 int ret;
3290
3291 if (!path->nodes[1])
3292 return 1;
3293
3294 slot = path->slots[1];
3295 upper = path->nodes[1];
3296 if (slot >= btrfs_header_nritems(upper) - 1)
3297 return 1;
3298
3299 btrfs_assert_tree_locked(path->nodes[1]);
3300
3301 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003302 if (right == NULL)
3303 return 1;
3304
Chris Mason44871b12009-03-13 10:04:31 -04003305 btrfs_tree_lock(right);
3306 btrfs_set_lock_blocking(right);
3307
3308 free_space = btrfs_leaf_free_space(root, right);
3309 if (free_space < data_size)
3310 goto out_unlock;
3311
3312 /* cow and double check */
3313 ret = btrfs_cow_block(trans, root, right, upper,
3314 slot + 1, &right);
3315 if (ret)
3316 goto out_unlock;
3317
3318 free_space = btrfs_leaf_free_space(root, right);
3319 if (free_space < data_size)
3320 goto out_unlock;
3321
3322 left_nritems = btrfs_header_nritems(left);
3323 if (left_nritems == 0)
3324 goto out_unlock;
3325
Chris Mason99d8f832010-07-07 10:51:48 -04003326 return __push_leaf_right(trans, root, path, min_data_size, empty,
3327 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003328out_unlock:
3329 btrfs_tree_unlock(right);
3330 free_extent_buffer(right);
3331 return 1;
3332}
3333
3334/*
Chris Mason74123bd2007-02-02 11:05:29 -05003335 * push some data in the path leaf to the left, trying to free up at
3336 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003337 *
3338 * max_slot can put a limit on how far into the leaf we'll push items. The
3339 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3340 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003341 */
Chris Mason44871b12009-03-13 10:04:31 -04003342static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3343 struct btrfs_root *root,
3344 struct btrfs_path *path, int data_size,
3345 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003346 int free_space, u32 right_nritems,
3347 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003348{
Chris Mason5f39d392007-10-15 16:14:19 -04003349 struct btrfs_disk_key disk_key;
3350 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003351 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003352 int push_space = 0;
3353 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003354 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003355 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003356 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003357 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003358 u32 this_item_size;
3359 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003360 struct btrfs_map_token token;
3361
3362 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003363
Chris Mason34a38212007-11-07 13:31:03 -05003364 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003365 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003366 else
Chris Mason99d8f832010-07-07 10:51:48 -04003367 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003368
3369 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003370 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003371
Zheng Yan31840ae2008-09-23 13:14:14 -04003372 if (!empty && push_items > 0) {
3373 if (path->slots[0] < i)
3374 break;
3375 if (path->slots[0] == i) {
3376 int space = btrfs_leaf_free_space(root, right);
3377 if (space + push_space * 2 > free_space)
3378 break;
3379 }
3380 }
3381
Chris Masonbe0e5c02007-01-26 15:51:26 -05003382 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003383 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003384
3385 this_item_size = btrfs_item_size(right, item);
3386 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003387 break;
Chris Masondb945352007-10-15 16:15:53 -04003388
Chris Masonbe0e5c02007-01-26 15:51:26 -05003389 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003390 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003391 }
Chris Masondb945352007-10-15 16:15:53 -04003392
Chris Masonbe0e5c02007-01-26 15:51:26 -05003393 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003394 ret = 1;
3395 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003396 }
Chris Mason34a38212007-11-07 13:31:03 -05003397 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003398 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003399
Chris Masonbe0e5c02007-01-26 15:51:26 -05003400 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003401 copy_extent_buffer(left, right,
3402 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3403 btrfs_item_nr_offset(0),
3404 push_items * sizeof(struct btrfs_item));
3405
Chris Mason123abc82007-03-14 14:14:43 -04003406 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003407 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003408
3409 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003410 leaf_data_end(root, left) - push_space,
3411 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003412 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003413 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003414 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003415 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003416
Chris Masondb945352007-10-15 16:15:53 -04003417 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003418 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003419 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003420
Chris Mason5f39d392007-10-15 16:14:19 -04003421 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003422
Chris Masoncfed81a2012-03-03 07:40:03 -05003423 ioff = btrfs_token_item_offset(left, item, &token);
3424 btrfs_set_token_item_offset(left, item,
3425 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3426 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003427 }
Chris Mason5f39d392007-10-15 16:14:19 -04003428 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003429
3430 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003431 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003432 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3433 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003434 WARN_ON(1);
3435 }
Chris Mason5f39d392007-10-15 16:14:19 -04003436
Chris Mason34a38212007-11-07 13:31:03 -05003437 if (push_items < right_nritems) {
3438 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3439 leaf_data_end(root, right);
3440 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3441 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3442 btrfs_leaf_data(right) +
3443 leaf_data_end(root, right), push_space);
3444
3445 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003446 btrfs_item_nr_offset(push_items),
3447 (btrfs_header_nritems(right) - push_items) *
3448 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003449 }
Yaneef1c492007-11-26 10:58:13 -05003450 right_nritems -= push_items;
3451 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003452 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003453 for (i = 0; i < right_nritems; i++) {
3454 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003455
Chris Masoncfed81a2012-03-03 07:40:03 -05003456 push_space = push_space - btrfs_token_item_size(right,
3457 item, &token);
3458 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003459 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003460
Chris Mason5f39d392007-10-15 16:14:19 -04003461 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003462 if (right_nritems)
3463 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003464 else
3465 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003466
Chris Mason5f39d392007-10-15 16:14:19 -04003467 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003468 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003469
3470 /* then fixup the leaf pointer in the path */
3471 if (path->slots[0] < push_items) {
3472 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003473 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003474 free_extent_buffer(path->nodes[0]);
3475 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003476 path->slots[1] -= 1;
3477 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003478 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003479 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003480 path->slots[0] -= push_items;
3481 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003482 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003483 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003484out:
3485 btrfs_tree_unlock(left);
3486 free_extent_buffer(left);
3487 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003488}
3489
Chris Mason74123bd2007-02-02 11:05:29 -05003490/*
Chris Mason44871b12009-03-13 10:04:31 -04003491 * push some data in the path leaf to the left, trying to free up at
3492 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003493 *
3494 * max_slot can put a limit on how far into the leaf we'll push items. The
3495 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3496 * items
Chris Mason44871b12009-03-13 10:04:31 -04003497 */
3498static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003499 *root, struct btrfs_path *path, int min_data_size,
3500 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003501{
3502 struct extent_buffer *right = path->nodes[0];
3503 struct extent_buffer *left;
3504 int slot;
3505 int free_space;
3506 u32 right_nritems;
3507 int ret = 0;
3508
3509 slot = path->slots[1];
3510 if (slot == 0)
3511 return 1;
3512 if (!path->nodes[1])
3513 return 1;
3514
3515 right_nritems = btrfs_header_nritems(right);
3516 if (right_nritems == 0)
3517 return 1;
3518
3519 btrfs_assert_tree_locked(path->nodes[1]);
3520
3521 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003522 if (left == NULL)
3523 return 1;
3524
Chris Mason44871b12009-03-13 10:04:31 -04003525 btrfs_tree_lock(left);
3526 btrfs_set_lock_blocking(left);
3527
3528 free_space = btrfs_leaf_free_space(root, left);
3529 if (free_space < data_size) {
3530 ret = 1;
3531 goto out;
3532 }
3533
3534 /* cow and double check */
3535 ret = btrfs_cow_block(trans, root, left,
3536 path->nodes[1], slot - 1, &left);
3537 if (ret) {
3538 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003539 if (ret == -ENOSPC)
3540 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003541 goto out;
3542 }
3543
3544 free_space = btrfs_leaf_free_space(root, left);
3545 if (free_space < data_size) {
3546 ret = 1;
3547 goto out;
3548 }
3549
Chris Mason99d8f832010-07-07 10:51:48 -04003550 return __push_leaf_left(trans, root, path, min_data_size,
3551 empty, left, free_space, right_nritems,
3552 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003553out:
3554 btrfs_tree_unlock(left);
3555 free_extent_buffer(left);
3556 return ret;
3557}
3558
3559/*
Chris Mason74123bd2007-02-02 11:05:29 -05003560 * split the path's leaf in two, making sure there is at least data_size
3561 * available for the resulting leaf level of the path.
3562 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003563static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3564 struct btrfs_root *root,
3565 struct btrfs_path *path,
3566 struct extent_buffer *l,
3567 struct extent_buffer *right,
3568 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003569{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003570 int data_copy_size;
3571 int rt_data_off;
3572 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003573 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003574 struct btrfs_map_token token;
3575
3576 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003577
Chris Mason5f39d392007-10-15 16:14:19 -04003578 nritems = nritems - mid;
3579 btrfs_set_header_nritems(right, nritems);
3580 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3581
3582 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3583 btrfs_item_nr_offset(mid),
3584 nritems * sizeof(struct btrfs_item));
3585
3586 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003587 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3588 data_copy_size, btrfs_leaf_data(l) +
3589 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003590
Chris Mason5f39d392007-10-15 16:14:19 -04003591 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3592 btrfs_item_end_nr(l, mid);
3593
3594 for (i = 0; i < nritems; i++) {
3595 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003596 u32 ioff;
3597
Chris Masoncfed81a2012-03-03 07:40:03 -05003598 ioff = btrfs_token_item_offset(right, item, &token);
3599 btrfs_set_token_item_offset(right, item,
3600 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003601 }
Chris Mason74123bd2007-02-02 11:05:29 -05003602
Chris Mason5f39d392007-10-15 16:14:19 -04003603 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003604 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003605 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003606 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003607
3608 btrfs_mark_buffer_dirty(right);
3609 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003610 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003611
Chris Masonbe0e5c02007-01-26 15:51:26 -05003612 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003613 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003614 free_extent_buffer(path->nodes[0]);
3615 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003616 path->slots[0] -= mid;
3617 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003618 } else {
3619 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003620 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003621 }
Chris Mason5f39d392007-10-15 16:14:19 -04003622
Chris Masoneb60cea2007-02-02 09:18:22 -05003623 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003624}
3625
3626/*
Chris Mason99d8f832010-07-07 10:51:48 -04003627 * double splits happen when we need to insert a big item in the middle
3628 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3629 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3630 * A B C
3631 *
3632 * We avoid this by trying to push the items on either side of our target
3633 * into the adjacent leaves. If all goes well we can avoid the double split
3634 * completely.
3635 */
3636static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3637 struct btrfs_root *root,
3638 struct btrfs_path *path,
3639 int data_size)
3640{
3641 int ret;
3642 int progress = 0;
3643 int slot;
3644 u32 nritems;
3645
3646 slot = path->slots[0];
3647
3648 /*
3649 * try to push all the items after our slot into the
3650 * right leaf
3651 */
3652 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3653 if (ret < 0)
3654 return ret;
3655
3656 if (ret == 0)
3657 progress++;
3658
3659 nritems = btrfs_header_nritems(path->nodes[0]);
3660 /*
3661 * our goal is to get our slot at the start or end of a leaf. If
3662 * we've done so we're done
3663 */
3664 if (path->slots[0] == 0 || path->slots[0] == nritems)
3665 return 0;
3666
3667 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3668 return 0;
3669
3670 /* try to push all the items before our slot into the next leaf */
3671 slot = path->slots[0];
3672 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3673 if (ret < 0)
3674 return ret;
3675
3676 if (ret == 0)
3677 progress++;
3678
3679 if (progress)
3680 return 0;
3681 return 1;
3682}
3683
3684/*
Chris Mason44871b12009-03-13 10:04:31 -04003685 * split the path's leaf in two, making sure there is at least data_size
3686 * available for the resulting leaf level of the path.
3687 *
3688 * returns 0 if all went well and < 0 on failure.
3689 */
3690static noinline int split_leaf(struct btrfs_trans_handle *trans,
3691 struct btrfs_root *root,
3692 struct btrfs_key *ins_key,
3693 struct btrfs_path *path, int data_size,
3694 int extend)
3695{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003696 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003697 struct extent_buffer *l;
3698 u32 nritems;
3699 int mid;
3700 int slot;
3701 struct extent_buffer *right;
3702 int ret = 0;
3703 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003704 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003705 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003706 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003707
Yan, Zhenga5719522009-09-24 09:17:31 -04003708 l = path->nodes[0];
3709 slot = path->slots[0];
3710 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3711 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3712 return -EOVERFLOW;
3713
Chris Mason44871b12009-03-13 10:04:31 -04003714 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003715 if (data_size) {
3716 wret = push_leaf_right(trans, root, path, data_size,
3717 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003718 if (wret < 0)
3719 return wret;
3720 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003721 wret = push_leaf_left(trans, root, path, data_size,
3722 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003723 if (wret < 0)
3724 return wret;
3725 }
3726 l = path->nodes[0];
3727
3728 /* did the pushes work? */
3729 if (btrfs_leaf_free_space(root, l) >= data_size)
3730 return 0;
3731 }
3732
3733 if (!path->nodes[1]) {
3734 ret = insert_new_root(trans, root, path, 1);
3735 if (ret)
3736 return ret;
3737 }
3738again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003739 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003740 l = path->nodes[0];
3741 slot = path->slots[0];
3742 nritems = btrfs_header_nritems(l);
3743 mid = (nritems + 1) / 2;
3744
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003745 if (mid <= slot) {
3746 if (nritems == 1 ||
3747 leaf_space_used(l, mid, nritems - mid) + data_size >
3748 BTRFS_LEAF_DATA_SIZE(root)) {
3749 if (slot >= nritems) {
3750 split = 0;
3751 } else {
3752 mid = slot;
3753 if (mid != nritems &&
3754 leaf_space_used(l, mid, nritems - mid) +
3755 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003756 if (data_size && !tried_avoid_double)
3757 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003758 split = 2;
3759 }
3760 }
3761 }
3762 } else {
3763 if (leaf_space_used(l, 0, mid) + data_size >
3764 BTRFS_LEAF_DATA_SIZE(root)) {
3765 if (!extend && data_size && slot == 0) {
3766 split = 0;
3767 } else if ((extend || !data_size) && slot == 0) {
3768 mid = 1;
3769 } else {
3770 mid = slot;
3771 if (mid != nritems &&
3772 leaf_space_used(l, mid, nritems - mid) +
3773 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003774 if (data_size && !tried_avoid_double)
3775 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003776 split = 2 ;
3777 }
3778 }
3779 }
3780 }
3781
3782 if (split == 0)
3783 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3784 else
3785 btrfs_item_key(l, &disk_key, mid);
3786
3787 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003788 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003789 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003790 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003791 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003792
3793 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003794
3795 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3796 btrfs_set_header_bytenr(right, right->start);
3797 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003798 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003799 btrfs_set_header_owner(right, root->root_key.objectid);
3800 btrfs_set_header_level(right, 0);
3801 write_extent_buffer(right, root->fs_info->fsid,
3802 (unsigned long)btrfs_header_fsid(right),
3803 BTRFS_FSID_SIZE);
3804
3805 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3806 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3807 BTRFS_UUID_SIZE);
3808
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003809 if (split == 0) {
3810 if (mid <= slot) {
3811 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003812 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003813 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003814 btrfs_tree_unlock(path->nodes[0]);
3815 free_extent_buffer(path->nodes[0]);
3816 path->nodes[0] = right;
3817 path->slots[0] = 0;
3818 path->slots[1] += 1;
3819 } else {
3820 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003821 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003822 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003823 btrfs_tree_unlock(path->nodes[0]);
3824 free_extent_buffer(path->nodes[0]);
3825 path->nodes[0] = right;
3826 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003827 if (path->slots[1] == 0)
3828 fixup_low_keys(trans, root, path,
3829 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003830 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003831 btrfs_mark_buffer_dirty(right);
3832 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003833 }
3834
Jeff Mahoney143bede2012-03-01 14:56:26 +01003835 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003836
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003837 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003838 BUG_ON(num_doubles != 0);
3839 num_doubles++;
3840 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003841 }
Chris Mason44871b12009-03-13 10:04:31 -04003842
Jeff Mahoney143bede2012-03-01 14:56:26 +01003843 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003844
3845push_for_double:
3846 push_for_double_split(trans, root, path, data_size);
3847 tried_avoid_double = 1;
3848 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3849 return 0;
3850 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003851}
3852
Yan, Zhengad48fd752009-11-12 09:33:58 +00003853static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3854 struct btrfs_root *root,
3855 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003856{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003857 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003858 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003859 struct btrfs_file_extent_item *fi;
3860 u64 extent_len = 0;
3861 u32 item_size;
3862 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003863
3864 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003865 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3866
3867 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3868 key.type != BTRFS_EXTENT_CSUM_KEY);
3869
3870 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3871 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003872
3873 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003874 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3875 fi = btrfs_item_ptr(leaf, path->slots[0],
3876 struct btrfs_file_extent_item);
3877 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3878 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003879 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003880
Chris Mason459931e2008-12-10 09:10:46 -05003881 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003882 path->search_for_split = 1;
3883 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003884 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003885 if (ret < 0)
3886 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003887
Yan, Zhengad48fd752009-11-12 09:33:58 +00003888 ret = -EAGAIN;
3889 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003890 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003891 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3892 goto err;
3893
Chris Mason109f6ae2010-04-02 09:20:18 -04003894 /* the leaf has changed, it now has room. return now */
3895 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3896 goto err;
3897
Yan, Zhengad48fd752009-11-12 09:33:58 +00003898 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3899 fi = btrfs_item_ptr(leaf, path->slots[0],
3900 struct btrfs_file_extent_item);
3901 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3902 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003903 }
3904
Chris Masonb9473432009-03-13 11:00:37 -04003905 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003906 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003907 if (ret)
3908 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003909
Yan, Zhengad48fd752009-11-12 09:33:58 +00003910 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003911 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003912 return 0;
3913err:
3914 path->keep_locks = 0;
3915 return ret;
3916}
3917
3918static noinline int split_item(struct btrfs_trans_handle *trans,
3919 struct btrfs_root *root,
3920 struct btrfs_path *path,
3921 struct btrfs_key *new_key,
3922 unsigned long split_offset)
3923{
3924 struct extent_buffer *leaf;
3925 struct btrfs_item *item;
3926 struct btrfs_item *new_item;
3927 int slot;
3928 char *buf;
3929 u32 nritems;
3930 u32 item_size;
3931 u32 orig_offset;
3932 struct btrfs_disk_key disk_key;
3933
Chris Masonb9473432009-03-13 11:00:37 -04003934 leaf = path->nodes[0];
3935 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3936
Chris Masonb4ce94d2009-02-04 09:25:08 -05003937 btrfs_set_path_blocking(path);
3938
Chris Mason459931e2008-12-10 09:10:46 -05003939 item = btrfs_item_nr(leaf, path->slots[0]);
3940 orig_offset = btrfs_item_offset(leaf, item);
3941 item_size = btrfs_item_size(leaf, item);
3942
Chris Mason459931e2008-12-10 09:10:46 -05003943 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003944 if (!buf)
3945 return -ENOMEM;
3946
Chris Mason459931e2008-12-10 09:10:46 -05003947 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3948 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003949
Chris Mason459931e2008-12-10 09:10:46 -05003950 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003951 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003952 if (slot != nritems) {
3953 /* shift the items */
3954 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003955 btrfs_item_nr_offset(slot),
3956 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003957 }
3958
3959 btrfs_cpu_key_to_disk(&disk_key, new_key);
3960 btrfs_set_item_key(leaf, &disk_key, slot);
3961
3962 new_item = btrfs_item_nr(leaf, slot);
3963
3964 btrfs_set_item_offset(leaf, new_item, orig_offset);
3965 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3966
3967 btrfs_set_item_offset(leaf, item,
3968 orig_offset + item_size - split_offset);
3969 btrfs_set_item_size(leaf, item, split_offset);
3970
3971 btrfs_set_header_nritems(leaf, nritems + 1);
3972
3973 /* write the data for the start of the original item */
3974 write_extent_buffer(leaf, buf,
3975 btrfs_item_ptr_offset(leaf, path->slots[0]),
3976 split_offset);
3977
3978 /* write the data for the new item */
3979 write_extent_buffer(leaf, buf + split_offset,
3980 btrfs_item_ptr_offset(leaf, slot),
3981 item_size - split_offset);
3982 btrfs_mark_buffer_dirty(leaf);
3983
Yan, Zhengad48fd752009-11-12 09:33:58 +00003984 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003985 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003986 return 0;
3987}
3988
3989/*
3990 * This function splits a single item into two items,
3991 * giving 'new_key' to the new item and splitting the
3992 * old one at split_offset (from the start of the item).
3993 *
3994 * The path may be released by this operation. After
3995 * the split, the path is pointing to the old item. The
3996 * new item is going to be in the same node as the old one.
3997 *
3998 * Note, the item being split must be smaller enough to live alone on
3999 * a tree block with room for one extra struct btrfs_item
4000 *
4001 * This allows us to split the item in place, keeping a lock on the
4002 * leaf the entire time.
4003 */
4004int btrfs_split_item(struct btrfs_trans_handle *trans,
4005 struct btrfs_root *root,
4006 struct btrfs_path *path,
4007 struct btrfs_key *new_key,
4008 unsigned long split_offset)
4009{
4010 int ret;
4011 ret = setup_leaf_for_split(trans, root, path,
4012 sizeof(struct btrfs_item));
4013 if (ret)
4014 return ret;
4015
4016 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004017 return ret;
4018}
4019
4020/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004021 * This function duplicate a item, giving 'new_key' to the new item.
4022 * It guarantees both items live in the same tree leaf and the new item
4023 * is contiguous with the original item.
4024 *
4025 * This allows us to split file extent in place, keeping a lock on the
4026 * leaf the entire time.
4027 */
4028int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4029 struct btrfs_root *root,
4030 struct btrfs_path *path,
4031 struct btrfs_key *new_key)
4032{
4033 struct extent_buffer *leaf;
4034 int ret;
4035 u32 item_size;
4036
4037 leaf = path->nodes[0];
4038 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4039 ret = setup_leaf_for_split(trans, root, path,
4040 item_size + sizeof(struct btrfs_item));
4041 if (ret)
4042 return ret;
4043
4044 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004045 setup_items_for_insert(trans, root, path, new_key, &item_size,
4046 item_size, item_size +
4047 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004048 leaf = path->nodes[0];
4049 memcpy_extent_buffer(leaf,
4050 btrfs_item_ptr_offset(leaf, path->slots[0]),
4051 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4052 item_size);
4053 return 0;
4054}
4055
4056/*
Chris Masond352ac62008-09-29 15:18:18 -04004057 * make the item pointed to by the path smaller. new_size indicates
4058 * how small to make it, and from_end tells us if we just chop bytes
4059 * off the end of the item or if we shift the item to chop bytes off
4060 * the front.
4061 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004062void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4063 struct btrfs_root *root,
4064 struct btrfs_path *path,
4065 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004066{
Chris Masonb18c6682007-04-17 13:26:50 -04004067 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004068 struct extent_buffer *leaf;
4069 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004070 u32 nritems;
4071 unsigned int data_end;
4072 unsigned int old_data_start;
4073 unsigned int old_size;
4074 unsigned int size_diff;
4075 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004076 struct btrfs_map_token token;
4077
4078 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004079
Chris Mason5f39d392007-10-15 16:14:19 -04004080 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004081 slot = path->slots[0];
4082
4083 old_size = btrfs_item_size_nr(leaf, slot);
4084 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004085 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004086
Chris Mason5f39d392007-10-15 16:14:19 -04004087 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004088 data_end = leaf_data_end(root, leaf);
4089
Chris Mason5f39d392007-10-15 16:14:19 -04004090 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004091
Chris Masonb18c6682007-04-17 13:26:50 -04004092 size_diff = old_size - new_size;
4093
4094 BUG_ON(slot < 0);
4095 BUG_ON(slot >= nritems);
4096
4097 /*
4098 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4099 */
4100 /* first correct the data pointers */
4101 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004102 u32 ioff;
4103 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004104
Chris Masoncfed81a2012-03-03 07:40:03 -05004105 ioff = btrfs_token_item_offset(leaf, item, &token);
4106 btrfs_set_token_item_offset(leaf, item,
4107 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004108 }
Chris Masondb945352007-10-15 16:15:53 -04004109
Chris Masonb18c6682007-04-17 13:26:50 -04004110 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004111 if (from_end) {
4112 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4113 data_end + size_diff, btrfs_leaf_data(leaf) +
4114 data_end, old_data_start + new_size - data_end);
4115 } else {
4116 struct btrfs_disk_key disk_key;
4117 u64 offset;
4118
4119 btrfs_item_key(leaf, &disk_key, slot);
4120
4121 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4122 unsigned long ptr;
4123 struct btrfs_file_extent_item *fi;
4124
4125 fi = btrfs_item_ptr(leaf, slot,
4126 struct btrfs_file_extent_item);
4127 fi = (struct btrfs_file_extent_item *)(
4128 (unsigned long)fi - size_diff);
4129
4130 if (btrfs_file_extent_type(leaf, fi) ==
4131 BTRFS_FILE_EXTENT_INLINE) {
4132 ptr = btrfs_item_ptr_offset(leaf, slot);
4133 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004134 (unsigned long)fi,
4135 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004136 disk_bytenr));
4137 }
4138 }
4139
4140 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4141 data_end + size_diff, btrfs_leaf_data(leaf) +
4142 data_end, old_data_start - data_end);
4143
4144 offset = btrfs_disk_key_offset(&disk_key);
4145 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4146 btrfs_set_item_key(leaf, &disk_key, slot);
4147 if (slot == 0)
4148 fixup_low_keys(trans, root, path, &disk_key, 1);
4149 }
Chris Mason5f39d392007-10-15 16:14:19 -04004150
4151 item = btrfs_item_nr(leaf, slot);
4152 btrfs_set_item_size(leaf, item, new_size);
4153 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004154
Chris Mason5f39d392007-10-15 16:14:19 -04004155 if (btrfs_leaf_free_space(root, leaf) < 0) {
4156 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004157 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004158 }
Chris Masonb18c6682007-04-17 13:26:50 -04004159}
4160
Chris Masond352ac62008-09-29 15:18:18 -04004161/*
4162 * make the item pointed to by the path bigger, data_size is the new size.
4163 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004164void btrfs_extend_item(struct btrfs_trans_handle *trans,
4165 struct btrfs_root *root, struct btrfs_path *path,
4166 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004167{
Chris Mason6567e832007-04-16 09:22:45 -04004168 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004169 struct extent_buffer *leaf;
4170 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004171 u32 nritems;
4172 unsigned int data_end;
4173 unsigned int old_data;
4174 unsigned int old_size;
4175 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004176 struct btrfs_map_token token;
4177
4178 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004179
Chris Mason5f39d392007-10-15 16:14:19 -04004180 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004181
Chris Mason5f39d392007-10-15 16:14:19 -04004182 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004183 data_end = leaf_data_end(root, leaf);
4184
Chris Mason5f39d392007-10-15 16:14:19 -04004185 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4186 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004187 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004188 }
Chris Mason6567e832007-04-16 09:22:45 -04004189 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004190 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004191
4192 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004193 if (slot >= nritems) {
4194 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004195 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4196 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004197 BUG_ON(1);
4198 }
Chris Mason6567e832007-04-16 09:22:45 -04004199
4200 /*
4201 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4202 */
4203 /* first correct the data pointers */
4204 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004205 u32 ioff;
4206 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004207
Chris Masoncfed81a2012-03-03 07:40:03 -05004208 ioff = btrfs_token_item_offset(leaf, item, &token);
4209 btrfs_set_token_item_offset(leaf, item,
4210 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004211 }
Chris Mason5f39d392007-10-15 16:14:19 -04004212
Chris Mason6567e832007-04-16 09:22:45 -04004213 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004214 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004215 data_end - data_size, btrfs_leaf_data(leaf) +
4216 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004217
Chris Mason6567e832007-04-16 09:22:45 -04004218 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004219 old_size = btrfs_item_size_nr(leaf, slot);
4220 item = btrfs_item_nr(leaf, slot);
4221 btrfs_set_item_size(leaf, item, old_size + data_size);
4222 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004223
Chris Mason5f39d392007-10-15 16:14:19 -04004224 if (btrfs_leaf_free_space(root, leaf) < 0) {
4225 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004226 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004227 }
Chris Mason6567e832007-04-16 09:22:45 -04004228}
4229
Chris Mason74123bd2007-02-02 11:05:29 -05004230/*
Chris Masond352ac62008-09-29 15:18:18 -04004231 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004232 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004233 * Returns the number of keys that were inserted.
4234 */
4235int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4236 struct btrfs_root *root,
4237 struct btrfs_path *path,
4238 struct btrfs_key *cpu_key, u32 *data_size,
4239 int nr)
4240{
4241 struct extent_buffer *leaf;
4242 struct btrfs_item *item;
4243 int ret = 0;
4244 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004245 int i;
4246 u32 nritems;
4247 u32 total_data = 0;
4248 u32 total_size = 0;
4249 unsigned int data_end;
4250 struct btrfs_disk_key disk_key;
4251 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004252 struct btrfs_map_token token;
4253
4254 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004255
Yan Zheng87b29b22008-12-17 10:21:48 -05004256 for (i = 0; i < nr; i++) {
4257 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4258 BTRFS_LEAF_DATA_SIZE(root)) {
4259 break;
4260 nr = i;
4261 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004262 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004263 total_size += data_size[i] + sizeof(struct btrfs_item);
4264 }
4265 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004266
Josef Bacikf3465ca2008-11-12 14:19:50 -05004267 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4268 if (ret == 0)
4269 return -EEXIST;
4270 if (ret < 0)
4271 goto out;
4272
Josef Bacikf3465ca2008-11-12 14:19:50 -05004273 leaf = path->nodes[0];
4274
4275 nritems = btrfs_header_nritems(leaf);
4276 data_end = leaf_data_end(root, leaf);
4277
4278 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4279 for (i = nr; i >= 0; i--) {
4280 total_data -= data_size[i];
4281 total_size -= data_size[i] + sizeof(struct btrfs_item);
4282 if (total_size < btrfs_leaf_free_space(root, leaf))
4283 break;
4284 }
4285 nr = i;
4286 }
4287
4288 slot = path->slots[0];
4289 BUG_ON(slot < 0);
4290
4291 if (slot != nritems) {
4292 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4293
4294 item = btrfs_item_nr(leaf, slot);
4295 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4296
4297 /* figure out how many keys we can insert in here */
4298 total_data = data_size[0];
4299 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004300 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004301 break;
4302 total_data += data_size[i];
4303 }
4304 nr = i;
4305
4306 if (old_data < data_end) {
4307 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004308 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004309 slot, old_data, data_end);
4310 BUG_ON(1);
4311 }
4312 /*
4313 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4314 */
4315 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004316 for (i = slot; i < nritems; i++) {
4317 u32 ioff;
4318
4319 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004320 ioff = btrfs_token_item_offset(leaf, item, &token);
4321 btrfs_set_token_item_offset(leaf, item,
4322 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004323 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004324 /* shift the items */
4325 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4326 btrfs_item_nr_offset(slot),
4327 (nritems - slot) * sizeof(struct btrfs_item));
4328
4329 /* shift the data */
4330 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4331 data_end - total_data, btrfs_leaf_data(leaf) +
4332 data_end, old_data - data_end);
4333 data_end = old_data;
4334 } else {
4335 /*
4336 * this sucks but it has to be done, if we are inserting at
4337 * the end of the leaf only insert 1 of the items, since we
4338 * have no way of knowing whats on the next leaf and we'd have
4339 * to drop our current locks to figure it out
4340 */
4341 nr = 1;
4342 }
4343
4344 /* setup the item for the new data */
4345 for (i = 0; i < nr; i++) {
4346 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4347 btrfs_set_item_key(leaf, &disk_key, slot + i);
4348 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004349 btrfs_set_token_item_offset(leaf, item,
4350 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004351 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004352 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004353 }
4354 btrfs_set_header_nritems(leaf, nritems + nr);
4355 btrfs_mark_buffer_dirty(leaf);
4356
4357 ret = 0;
4358 if (slot == 0) {
4359 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004360 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004361 }
4362
4363 if (btrfs_leaf_free_space(root, leaf) < 0) {
4364 btrfs_print_leaf(root, leaf);
4365 BUG();
4366 }
4367out:
4368 if (!ret)
4369 ret = nr;
4370 return ret;
4371}
4372
4373/*
Chris Mason44871b12009-03-13 10:04:31 -04004374 * this is a helper for btrfs_insert_empty_items, the main goal here is
4375 * to save stack depth by doing the bulk of the work in a function
4376 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004377 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004378void setup_items_for_insert(struct btrfs_trans_handle *trans,
4379 struct btrfs_root *root, struct btrfs_path *path,
4380 struct btrfs_key *cpu_key, u32 *data_size,
4381 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004382{
Chris Mason5f39d392007-10-15 16:14:19 -04004383 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004384 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004385 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004386 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004387 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004388 struct extent_buffer *leaf;
4389 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004390 struct btrfs_map_token token;
4391
4392 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004393
Chris Mason5f39d392007-10-15 16:14:19 -04004394 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004395 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004396
Chris Mason5f39d392007-10-15 16:14:19 -04004397 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004398 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004399
Chris Masonf25956c2008-09-12 15:32:53 -04004400 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004401 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004402 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004403 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004404 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004405 }
Chris Mason5f39d392007-10-15 16:14:19 -04004406
Chris Masonbe0e5c02007-01-26 15:51:26 -05004407 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004408 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004409
Chris Mason5f39d392007-10-15 16:14:19 -04004410 if (old_data < data_end) {
4411 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004412 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004413 slot, old_data, data_end);
4414 BUG_ON(1);
4415 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004416 /*
4417 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4418 */
4419 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004420 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004421 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004422
Chris Mason5f39d392007-10-15 16:14:19 -04004423 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004424 ioff = btrfs_token_item_offset(leaf, item, &token);
4425 btrfs_set_token_item_offset(leaf, item,
4426 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004427 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004428 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004429 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004430 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004431 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004432
4433 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004434 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004435 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004436 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004437 data_end = old_data;
4438 }
Chris Mason5f39d392007-10-15 16:14:19 -04004439
Chris Mason62e27492007-03-15 12:56:47 -04004440 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004441 for (i = 0; i < nr; i++) {
4442 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4443 btrfs_set_item_key(leaf, &disk_key, slot + i);
4444 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004445 btrfs_set_token_item_offset(leaf, item,
4446 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004447 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004448 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004449 }
Chris Mason44871b12009-03-13 10:04:31 -04004450
Chris Mason9c583092008-01-29 15:15:18 -05004451 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004452
Chris Mason5a01a2e2008-01-30 11:43:54 -05004453 if (slot == 0) {
4454 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004455 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004456 }
Chris Masonb9473432009-03-13 11:00:37 -04004457 btrfs_unlock_up_safe(path, 1);
4458 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004459
Chris Mason5f39d392007-10-15 16:14:19 -04004460 if (btrfs_leaf_free_space(root, leaf) < 0) {
4461 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004462 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004463 }
Chris Mason44871b12009-03-13 10:04:31 -04004464}
4465
4466/*
4467 * Given a key and some data, insert items into the tree.
4468 * This does all the path init required, making room in the tree if needed.
4469 */
4470int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4471 struct btrfs_root *root,
4472 struct btrfs_path *path,
4473 struct btrfs_key *cpu_key, u32 *data_size,
4474 int nr)
4475{
Chris Mason44871b12009-03-13 10:04:31 -04004476 int ret = 0;
4477 int slot;
4478 int i;
4479 u32 total_size = 0;
4480 u32 total_data = 0;
4481
4482 for (i = 0; i < nr; i++)
4483 total_data += data_size[i];
4484
4485 total_size = total_data + (nr * sizeof(struct btrfs_item));
4486 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4487 if (ret == 0)
4488 return -EEXIST;
4489 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004490 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004491
Chris Mason44871b12009-03-13 10:04:31 -04004492 slot = path->slots[0];
4493 BUG_ON(slot < 0);
4494
Jeff Mahoney143bede2012-03-01 14:56:26 +01004495 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004496 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004497 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004498}
4499
4500/*
4501 * Given a key and some data, insert an item into the tree.
4502 * This does all the path init required, making room in the tree if needed.
4503 */
Chris Masone089f052007-03-16 16:20:31 -04004504int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4505 *root, struct btrfs_key *cpu_key, void *data, u32
4506 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004507{
4508 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004509 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004510 struct extent_buffer *leaf;
4511 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004512
Chris Mason2c90e5d2007-04-02 10:50:19 -04004513 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004514 if (!path)
4515 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004516 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004517 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004518 leaf = path->nodes[0];
4519 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4520 write_extent_buffer(leaf, data, ptr, data_size);
4521 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004522 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004523 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004524 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004525}
4526
Chris Mason74123bd2007-02-02 11:05:29 -05004527/*
Chris Mason5de08d72007-02-24 06:24:44 -05004528 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004529 *
Chris Masond352ac62008-09-29 15:18:18 -04004530 * the tree should have been previously balanced so the deletion does not
4531 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004532 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004533static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004534 struct btrfs_path *path, int level, int slot,
4535 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004536{
Chris Mason5f39d392007-10-15 16:14:19 -04004537 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004538 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004539 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004540
Chris Mason5f39d392007-10-15 16:14:19 -04004541 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004542 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004543 if (tree_mod_log && level)
4544 tree_mod_log_eb_move(root->fs_info, parent, slot,
4545 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004546 memmove_extent_buffer(parent,
4547 btrfs_node_key_ptr_offset(slot),
4548 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004549 sizeof(struct btrfs_key_ptr) *
4550 (nritems - slot - 1));
Chris Masonbb803952007-03-01 12:04:21 -05004551 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004552
4553 if (tree_mod_log && level) {
4554 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4555 MOD_LOG_KEY_REMOVE);
4556 BUG_ON(ret < 0);
4557 }
4558
Chris Mason7518a232007-03-12 12:01:18 -04004559 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004560 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004561 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004562 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004563 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004564 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004565 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004566 struct btrfs_disk_key disk_key;
4567
4568 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004569 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004570 }
Chris Masond6025572007-03-30 14:27:56 -04004571 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004572}
4573
Chris Mason74123bd2007-02-02 11:05:29 -05004574/*
Chris Mason323ac952008-10-01 19:05:46 -04004575 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004576 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004577 *
4578 * This deletes the pointer in path->nodes[1] and frees the leaf
4579 * block extent. zero is returned if it all worked out, < 0 otherwise.
4580 *
4581 * The path must have already been setup for deleting the leaf, including
4582 * all the proper balancing. path->nodes[1] must be locked.
4583 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004584static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4585 struct btrfs_root *root,
4586 struct btrfs_path *path,
4587 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004588{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004589 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004590 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004591
Chris Mason4d081c42009-02-04 09:31:28 -05004592 /*
4593 * btrfs_free_extent is expensive, we want to make sure we
4594 * aren't holding any locks when we call it
4595 */
4596 btrfs_unlock_up_safe(path, 0);
4597
Yan, Zhengf0486c62010-05-16 10:46:25 -04004598 root_sub_used(root, leaf->len);
4599
Josef Bacik3083ee22012-03-09 16:01:49 -05004600 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004601 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004602 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004603}
4604/*
Chris Mason74123bd2007-02-02 11:05:29 -05004605 * delete the item at the leaf level in path. If that empties
4606 * the leaf, remove it from the tree
4607 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004608int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4609 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004610{
Chris Mason5f39d392007-10-15 16:14:19 -04004611 struct extent_buffer *leaf;
4612 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004613 int last_off;
4614 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004615 int ret = 0;
4616 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004617 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004618 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004619 struct btrfs_map_token token;
4620
4621 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004622
Chris Mason5f39d392007-10-15 16:14:19 -04004623 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004624 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4625
4626 for (i = 0; i < nr; i++)
4627 dsize += btrfs_item_size_nr(leaf, slot + i);
4628
Chris Mason5f39d392007-10-15 16:14:19 -04004629 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004630
Chris Mason85e21ba2008-01-29 15:11:36 -05004631 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004632 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004633
4634 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004635 data_end + dsize,
4636 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004637 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004638
Chris Mason85e21ba2008-01-29 15:11:36 -05004639 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004640 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004641
Chris Mason5f39d392007-10-15 16:14:19 -04004642 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004643 ioff = btrfs_token_item_offset(leaf, item, &token);
4644 btrfs_set_token_item_offset(leaf, item,
4645 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004646 }
Chris Masondb945352007-10-15 16:15:53 -04004647
Chris Mason5f39d392007-10-15 16:14:19 -04004648 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004649 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004650 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004651 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004652 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004653 btrfs_set_header_nritems(leaf, nritems - nr);
4654 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004655
Chris Mason74123bd2007-02-02 11:05:29 -05004656 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004657 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004658 if (leaf == root->node) {
4659 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004660 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004661 btrfs_set_path_blocking(path);
4662 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004663 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004664 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004665 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004666 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004667 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004668 struct btrfs_disk_key disk_key;
4669
4670 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004671 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004672 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004673
Chris Mason74123bd2007-02-02 11:05:29 -05004674 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004675 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004676 /* push_leaf_left fixes the path.
4677 * make sure the path still points to our leaf
4678 * for possible call to del_ptr below
4679 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004680 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004681 extent_buffer_get(leaf);
4682
Chris Masonb9473432009-03-13 11:00:37 -04004683 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004684 wret = push_leaf_left(trans, root, path, 1, 1,
4685 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004686 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004687 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004688
4689 if (path->nodes[0] == leaf &&
4690 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004691 wret = push_leaf_right(trans, root, path, 1,
4692 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004693 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004694 ret = wret;
4695 }
Chris Mason5f39d392007-10-15 16:14:19 -04004696
4697 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004698 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004699 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004700 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004701 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004702 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004703 /* if we're still in the path, make sure
4704 * we're dirty. Otherwise, one of the
4705 * push_leaf functions must have already
4706 * dirtied this buffer
4707 */
4708 if (path->nodes[0] == leaf)
4709 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004710 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004711 }
Chris Masond5719762007-03-23 10:01:08 -04004712 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004713 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004714 }
4715 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004716 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004717}
4718
Chris Mason97571fd2007-02-24 13:39:08 -05004719/*
Chris Mason925baed2008-06-25 16:01:30 -04004720 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004721 * returns 0 if it found something or 1 if there are no lesser leaves.
4722 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004723 *
4724 * This may release the path, and so you may lose any locks held at the
4725 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004726 */
4727int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4728{
Chris Mason925baed2008-06-25 16:01:30 -04004729 struct btrfs_key key;
4730 struct btrfs_disk_key found_key;
4731 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004732
Chris Mason925baed2008-06-25 16:01:30 -04004733 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004734
Chris Mason925baed2008-06-25 16:01:30 -04004735 if (key.offset > 0)
4736 key.offset--;
4737 else if (key.type > 0)
4738 key.type--;
4739 else if (key.objectid > 0)
4740 key.objectid--;
4741 else
4742 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004743
David Sterbab3b4aa72011-04-21 01:20:15 +02004744 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004745 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4746 if (ret < 0)
4747 return ret;
4748 btrfs_item_key(path->nodes[0], &found_key, 0);
4749 ret = comp_keys(&found_key, &key);
4750 if (ret < 0)
4751 return 0;
4752 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004753}
4754
Chris Mason3f157a22008-06-25 16:01:31 -04004755/*
4756 * A helper function to walk down the tree starting at min_key, and looking
4757 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004758 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004759 *
4760 * This does not cow, but it does stuff the starting key it finds back
4761 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4762 * key and get a writable path.
4763 *
4764 * This does lock as it descends, and path->keep_locks should be set
4765 * to 1 by the caller.
4766 *
4767 * This honors path->lowest_level to prevent descent past a given level
4768 * of the tree.
4769 *
Chris Masond352ac62008-09-29 15:18:18 -04004770 * min_trans indicates the oldest transaction that you are interested
4771 * in walking through. Any nodes or leaves older than min_trans are
4772 * skipped over (without reading them).
4773 *
Chris Mason3f157a22008-06-25 16:01:31 -04004774 * returns zero if something useful was found, < 0 on error and 1 if there
4775 * was nothing in the tree that matched the search criteria.
4776 */
4777int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004778 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004779 struct btrfs_path *path, int cache_only,
4780 u64 min_trans)
4781{
4782 struct extent_buffer *cur;
4783 struct btrfs_key found_key;
4784 int slot;
Yan96524802008-07-24 12:19:49 -04004785 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004786 u32 nritems;
4787 int level;
4788 int ret = 1;
4789
Chris Mason934d3752008-12-08 16:43:10 -05004790 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004791again:
Chris Masonbd681512011-07-16 15:23:14 -04004792 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004793 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004794 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004795 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004796 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004797
4798 if (btrfs_header_generation(cur) < min_trans) {
4799 ret = 1;
4800 goto out;
4801 }
Chris Masond3977122009-01-05 21:25:51 -05004802 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004803 nritems = btrfs_header_nritems(cur);
4804 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004805 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004806
Chris Mason323ac952008-10-01 19:05:46 -04004807 /* at the lowest level, we're done, setup the path and exit */
4808 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004809 if (slot >= nritems)
4810 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004811 ret = 0;
4812 path->slots[level] = slot;
4813 btrfs_item_key_to_cpu(cur, &found_key, slot);
4814 goto out;
4815 }
Yan96524802008-07-24 12:19:49 -04004816 if (sret && slot > 0)
4817 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004818 /*
4819 * check this node pointer against the cache_only and
4820 * min_trans parameters. If it isn't in cache or is too
4821 * old, skip to the next one.
4822 */
Chris Masond3977122009-01-05 21:25:51 -05004823 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004824 u64 blockptr;
4825 u64 gen;
4826 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004827 struct btrfs_disk_key disk_key;
4828
Chris Mason3f157a22008-06-25 16:01:31 -04004829 blockptr = btrfs_node_blockptr(cur, slot);
4830 gen = btrfs_node_ptr_generation(cur, slot);
4831 if (gen < min_trans) {
4832 slot++;
4833 continue;
4834 }
4835 if (!cache_only)
4836 break;
4837
Chris Masone02119d2008-09-05 16:13:11 -04004838 if (max_key) {
4839 btrfs_node_key(cur, &disk_key, slot);
4840 if (comp_keys(&disk_key, max_key) >= 0) {
4841 ret = 1;
4842 goto out;
4843 }
4844 }
4845
Chris Mason3f157a22008-06-25 16:01:31 -04004846 tmp = btrfs_find_tree_block(root, blockptr,
4847 btrfs_level_size(root, level - 1));
4848
Chris Masonb9fab912012-05-06 07:23:47 -04004849 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004850 free_extent_buffer(tmp);
4851 break;
4852 }
4853 if (tmp)
4854 free_extent_buffer(tmp);
4855 slot++;
4856 }
Chris Masone02119d2008-09-05 16:13:11 -04004857find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004858 /*
4859 * we didn't find a candidate key in this node, walk forward
4860 * and find another one
4861 */
4862 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004863 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004864 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004865 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004866 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004867 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004868 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004869 goto again;
4870 } else {
4871 goto out;
4872 }
4873 }
4874 /* save our key for returning back */
4875 btrfs_node_key_to_cpu(cur, &found_key, slot);
4876 path->slots[level] = slot;
4877 if (level == path->lowest_level) {
4878 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004879 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004880 goto out;
4881 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004882 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004883 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004884 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004885
Chris Masonbd681512011-07-16 15:23:14 -04004886 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004887
Chris Masonbd681512011-07-16 15:23:14 -04004888 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004889 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004890 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004891 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004892 }
4893out:
4894 if (ret == 0)
4895 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004896 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004897 return ret;
4898}
4899
4900/*
4901 * this is similar to btrfs_next_leaf, but does not try to preserve
4902 * and fixup the path. It looks for and returns the next key in the
4903 * tree based on the current path and the cache_only and min_trans
4904 * parameters.
4905 *
4906 * 0 is returned if another key is found, < 0 if there are any errors
4907 * and 1 is returned if there are no higher keys in the tree
4908 *
4909 * path->keep_locks should be set to 1 on the search made before
4910 * calling this function.
4911 */
Chris Masone7a84562008-06-25 16:01:31 -04004912int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004913 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004914 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004915{
Chris Masone7a84562008-06-25 16:01:31 -04004916 int slot;
4917 struct extent_buffer *c;
4918
Chris Mason934d3752008-12-08 16:43:10 -05004919 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004920 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004921 if (!path->nodes[level])
4922 return 1;
4923
4924 slot = path->slots[level] + 1;
4925 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004926next:
Chris Masone7a84562008-06-25 16:01:31 -04004927 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004928 int ret;
4929 int orig_lowest;
4930 struct btrfs_key cur_key;
4931 if (level + 1 >= BTRFS_MAX_LEVEL ||
4932 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004933 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004934
4935 if (path->locks[level + 1]) {
4936 level++;
4937 continue;
4938 }
4939
4940 slot = btrfs_header_nritems(c) - 1;
4941 if (level == 0)
4942 btrfs_item_key_to_cpu(c, &cur_key, slot);
4943 else
4944 btrfs_node_key_to_cpu(c, &cur_key, slot);
4945
4946 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004947 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004948 path->lowest_level = level;
4949 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4950 0, 0);
4951 path->lowest_level = orig_lowest;
4952 if (ret < 0)
4953 return ret;
4954
4955 c = path->nodes[level];
4956 slot = path->slots[level];
4957 if (ret == 0)
4958 slot++;
4959 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004960 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004961
Chris Masone7a84562008-06-25 16:01:31 -04004962 if (level == 0)
4963 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004964 else {
4965 u64 blockptr = btrfs_node_blockptr(c, slot);
4966 u64 gen = btrfs_node_ptr_generation(c, slot);
4967
4968 if (cache_only) {
4969 struct extent_buffer *cur;
4970 cur = btrfs_find_tree_block(root, blockptr,
4971 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004972 if (!cur ||
4973 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004974 slot++;
4975 if (cur)
4976 free_extent_buffer(cur);
4977 goto next;
4978 }
4979 free_extent_buffer(cur);
4980 }
4981 if (gen < min_trans) {
4982 slot++;
4983 goto next;
4984 }
Chris Masone7a84562008-06-25 16:01:31 -04004985 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004986 }
Chris Masone7a84562008-06-25 16:01:31 -04004987 return 0;
4988 }
4989 return 1;
4990}
4991
Chris Mason7bb86312007-12-11 09:25:06 -05004992/*
Chris Mason925baed2008-06-25 16:01:30 -04004993 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004994 * returns 0 if it found something or 1 if there are no greater leaves.
4995 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004996 */
Chris Mason234b63a2007-03-13 10:46:10 -04004997int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004998{
4999 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005000 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005001 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005002 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005003 struct btrfs_key key;
5004 u32 nritems;
5005 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005006 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005007 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005008
5009 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005010 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005011 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005012
Chris Mason8e73f272009-04-03 10:14:18 -04005013 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5014again:
5015 level = 1;
5016 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005017 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005018 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005019
Chris Masona2135012008-06-25 16:01:30 -04005020 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005021 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005022
Chris Mason925baed2008-06-25 16:01:30 -04005023 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5024 path->keep_locks = 0;
5025
5026 if (ret < 0)
5027 return ret;
5028
Chris Masona2135012008-06-25 16:01:30 -04005029 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005030 /*
5031 * by releasing the path above we dropped all our locks. A balance
5032 * could have added more items next to the key that used to be
5033 * at the very end of the block. So, check again here and
5034 * advance the path if there are now more items available.
5035 */
Chris Masona2135012008-06-25 16:01:30 -04005036 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005037 if (ret == 0)
5038 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005039 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005040 goto done;
5041 }
Chris Masond97e63b2007-02-20 16:40:44 -05005042
Chris Masond3977122009-01-05 21:25:51 -05005043 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005044 if (!path->nodes[level]) {
5045 ret = 1;
5046 goto done;
5047 }
Chris Mason5f39d392007-10-15 16:14:19 -04005048
Chris Masond97e63b2007-02-20 16:40:44 -05005049 slot = path->slots[level] + 1;
5050 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005051 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005052 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005053 if (level == BTRFS_MAX_LEVEL) {
5054 ret = 1;
5055 goto done;
5056 }
Chris Masond97e63b2007-02-20 16:40:44 -05005057 continue;
5058 }
Chris Mason5f39d392007-10-15 16:14:19 -04005059
Chris Mason925baed2008-06-25 16:01:30 -04005060 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005061 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005062 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005063 }
Chris Mason5f39d392007-10-15 16:14:19 -04005064
Chris Mason8e73f272009-04-03 10:14:18 -04005065 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005066 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005067 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005068 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005069 if (ret == -EAGAIN)
5070 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005071
Chris Mason76a05b32009-05-14 13:24:30 -04005072 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005073 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005074 goto done;
5075 }
5076
Chris Mason5cd57b22008-06-25 16:01:30 -04005077 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005078 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005079 if (!ret) {
5080 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005081 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005082 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005083 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005084 }
Chris Mason31533fb2011-07-26 16:01:59 -04005085 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005086 }
Chris Masond97e63b2007-02-20 16:40:44 -05005087 break;
5088 }
5089 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005090 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005091 level--;
5092 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005093 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005094 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005095
Chris Mason5f39d392007-10-15 16:14:19 -04005096 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005097 path->nodes[level] = next;
5098 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005099 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005100 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005101 if (!level)
5102 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005103
Chris Mason8e73f272009-04-03 10:14:18 -04005104 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005105 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005106 if (ret == -EAGAIN)
5107 goto again;
5108
Chris Mason76a05b32009-05-14 13:24:30 -04005109 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005110 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005111 goto done;
5112 }
5113
Chris Mason5cd57b22008-06-25 16:01:30 -04005114 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005115 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005116 if (!ret) {
5117 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005118 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005119 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005120 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005121 }
Chris Mason31533fb2011-07-26 16:01:59 -04005122 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005123 }
Chris Masond97e63b2007-02-20 16:40:44 -05005124 }
Chris Mason8e73f272009-04-03 10:14:18 -04005125 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005126done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005127 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005128 path->leave_spinning = old_spinning;
5129 if (!old_spinning)
5130 btrfs_set_path_blocking(path);
5131
5132 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005133}
Chris Mason0b86a832008-03-24 15:01:56 -04005134
Chris Mason3f157a22008-06-25 16:01:31 -04005135/*
5136 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5137 * searching until it gets past min_objectid or finds an item of 'type'
5138 *
5139 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5140 */
Chris Mason0b86a832008-03-24 15:01:56 -04005141int btrfs_previous_item(struct btrfs_root *root,
5142 struct btrfs_path *path, u64 min_objectid,
5143 int type)
5144{
5145 struct btrfs_key found_key;
5146 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005147 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005148 int ret;
5149
Chris Masond3977122009-01-05 21:25:51 -05005150 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005151 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005152 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005153 ret = btrfs_prev_leaf(root, path);
5154 if (ret != 0)
5155 return ret;
5156 } else {
5157 path->slots[0]--;
5158 }
5159 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005160 nritems = btrfs_header_nritems(leaf);
5161 if (nritems == 0)
5162 return 1;
5163 if (path->slots[0] == nritems)
5164 path->slots[0]--;
5165
Chris Mason0b86a832008-03-24 15:01:56 -04005166 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005167 if (found_key.objectid < min_objectid)
5168 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005169 if (found_key.type == type)
5170 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005171 if (found_key.objectid == min_objectid &&
5172 found_key.type < type)
5173 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005174 }
5175 return 1;
5176}