blob: cb76b2a1b9080f28220099d52b98e156afa07704 [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
Jan Schmidtf3956942012-05-31 15:02:32 +0200543 if (tree_mod_dont_log(fs_info, eb))
544 return 0;
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200545
546 for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
547 ret = tree_mod_log_insert_key(fs_info, eb, i + dst_slot,
548 MOD_LOG_KEY_REMOVE_WHILE_MOVING);
549 BUG_ON(ret < 0);
550 }
551
Jan Schmidtf3956942012-05-31 15:02:32 +0200552 ret = tree_mod_alloc(fs_info, flags, &tm);
553 if (ret <= 0)
554 return ret;
555
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200556 tm->index = eb->start >> PAGE_CACHE_SHIFT;
557 tm->slot = src_slot;
558 tm->move.dst_slot = dst_slot;
559 tm->move.nr_items = nr_items;
560 tm->op = MOD_LOG_MOVE_KEYS;
561
562 return __tree_mod_log_insert(fs_info, tm);
563}
564
565static noinline int
566tree_mod_log_insert_root(struct btrfs_fs_info *fs_info,
567 struct extent_buffer *old_root,
568 struct extent_buffer *new_root, gfp_t flags)
569{
570 struct tree_mod_elem *tm;
571 int ret;
572
573 ret = tree_mod_alloc(fs_info, flags, &tm);
574 if (ret <= 0)
575 return ret;
576
577 tm->index = new_root->start >> PAGE_CACHE_SHIFT;
578 tm->old_root.logical = old_root->start;
579 tm->old_root.level = btrfs_header_level(old_root);
580 tm->generation = btrfs_header_generation(old_root);
581 tm->op = MOD_LOG_ROOT_REPLACE;
582
583 return __tree_mod_log_insert(fs_info, tm);
584}
585
586static struct tree_mod_elem *
587__tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
588 int smallest)
589{
590 struct rb_root *tm_root;
591 struct rb_node *node;
592 struct tree_mod_elem *cur = NULL;
593 struct tree_mod_elem *found = NULL;
594 u64 index = start >> PAGE_CACHE_SHIFT;
595
596 read_lock(&fs_info->tree_mod_log_lock);
597 tm_root = &fs_info->tree_mod_log;
598 node = tm_root->rb_node;
599 while (node) {
600 cur = container_of(node, struct tree_mod_elem, node);
601 if (cur->index < index) {
602 node = node->rb_left;
603 } else if (cur->index > index) {
604 node = node->rb_right;
605 } else if (cur->elem.seq < min_seq) {
606 node = node->rb_left;
607 } else if (!smallest) {
608 /* we want the node with the highest seq */
609 if (found)
610 BUG_ON(found->elem.seq > cur->elem.seq);
611 found = cur;
612 node = node->rb_left;
613 } else if (cur->elem.seq > min_seq) {
614 /* we want the node with the smallest seq */
615 if (found)
616 BUG_ON(found->elem.seq < cur->elem.seq);
617 found = cur;
618 node = node->rb_right;
619 } else {
620 found = cur;
621 break;
622 }
623 }
624 read_unlock(&fs_info->tree_mod_log_lock);
625
626 return found;
627}
628
629/*
630 * this returns the element from the log with the smallest time sequence
631 * value that's in the log (the oldest log item). any element with a time
632 * sequence lower than min_seq will be ignored.
633 */
634static struct tree_mod_elem *
635tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
636 u64 min_seq)
637{
638 return __tree_mod_log_search(fs_info, start, min_seq, 1);
639}
640
641/*
642 * this returns the element from the log with the largest time sequence
643 * value that's in the log (the most recent log item). any element with
644 * a time sequence lower than min_seq will be ignored.
645 */
646static struct tree_mod_elem *
647tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
648{
649 return __tree_mod_log_search(fs_info, start, min_seq, 0);
650}
651
652static inline void
653tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
654 struct extent_buffer *src, unsigned long dst_offset,
655 unsigned long src_offset, int nr_items)
656{
657 int ret;
658 int i;
659
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200660 if (tree_mod_dont_log(fs_info, NULL))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200661 return;
662
663 if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
664 return;
665
666 /* speed this up by single seq for all operations? */
667 for (i = 0; i < nr_items; i++) {
668 ret = tree_mod_log_insert_key(fs_info, src, i + src_offset,
669 MOD_LOG_KEY_REMOVE);
670 BUG_ON(ret < 0);
671 ret = tree_mod_log_insert_key(fs_info, dst, i + dst_offset,
672 MOD_LOG_KEY_ADD);
673 BUG_ON(ret < 0);
674 }
675}
676
677static inline void
678tree_mod_log_eb_move(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
679 int dst_offset, int src_offset, int nr_items)
680{
681 int ret;
682 ret = tree_mod_log_insert_move(fs_info, dst, dst_offset, src_offset,
683 nr_items, GFP_NOFS);
684 BUG_ON(ret < 0);
685}
686
687static inline void
688tree_mod_log_set_node_key(struct btrfs_fs_info *fs_info,
689 struct extent_buffer *eb,
690 struct btrfs_disk_key *disk_key, int slot, int atomic)
691{
692 int ret;
693
694 ret = tree_mod_log_insert_key_mask(fs_info, eb, slot,
695 MOD_LOG_KEY_REPLACE,
696 atomic ? GFP_ATOMIC : GFP_NOFS);
697 BUG_ON(ret < 0);
698}
699
700static void tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
701 struct extent_buffer *eb)
702{
703 int i;
704 int ret;
705 u32 nritems;
706
Jan Schmidte9b7fd42012-05-31 14:59:09 +0200707 if (tree_mod_dont_log(fs_info, eb))
Jan Schmidtbd989ba2012-05-16 17:18:50 +0200708 return;
709
710 nritems = btrfs_header_nritems(eb);
711 for (i = nritems - 1; i >= 0; i--) {
712 ret = tree_mod_log_insert_key(fs_info, eb, i,
713 MOD_LOG_KEY_REMOVE_WHILE_FREEING);
714 BUG_ON(ret < 0);
715 }
716}
717
718static inline void
719tree_mod_log_set_root_pointer(struct btrfs_root *root,
720 struct extent_buffer *new_root_node)
721{
722 int ret;
723 tree_mod_log_free_eb(root->fs_info, root->node);
724 ret = tree_mod_log_insert_root(root->fs_info, root->node,
725 new_root_node, GFP_NOFS);
726 BUG_ON(ret < 0);
727}
728
Chris Masond352ac62008-09-29 15:18:18 -0400729/*
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400730 * check if the tree block can be shared by multiple trees
731 */
732int btrfs_block_can_be_shared(struct btrfs_root *root,
733 struct extent_buffer *buf)
734{
735 /*
736 * Tree blocks not in refernece counted trees and tree roots
737 * are never shared. If a block was allocated after the last
738 * snapshot and the block was not allocated by tree relocation,
739 * we know the block is not shared.
740 */
741 if (root->ref_cows &&
742 buf != root->node && buf != root->commit_root &&
743 (btrfs_header_generation(buf) <=
744 btrfs_root_last_snapshot(&root->root_item) ||
745 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
746 return 1;
747#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
748 if (root->ref_cows &&
749 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
750 return 1;
751#endif
752 return 0;
753}
754
755static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
756 struct btrfs_root *root,
757 struct extent_buffer *buf,
Yan, Zhengf0486c62010-05-16 10:46:25 -0400758 struct extent_buffer *cow,
759 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400760{
761 u64 refs;
762 u64 owner;
763 u64 flags;
764 u64 new_flags = 0;
765 int ret;
766
767 /*
768 * Backrefs update rules:
769 *
770 * Always use full backrefs for extent pointers in tree block
771 * allocated by tree relocation.
772 *
773 * If a shared tree block is no longer referenced by its owner
774 * tree (btrfs_header_owner(buf) == root->root_key.objectid),
775 * use full backrefs for extent pointers in tree block.
776 *
777 * If a tree block is been relocating
778 * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
779 * use full backrefs for extent pointers in tree block.
780 * The reason for this is some operations (such as drop tree)
781 * are only allowed for blocks use full backrefs.
782 */
783
784 if (btrfs_block_can_be_shared(root, buf)) {
785 ret = btrfs_lookup_extent_info(trans, root, buf->start,
786 buf->len, &refs, &flags);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700787 if (ret)
788 return ret;
Mark Fashehe5df9572011-08-29 14:17:04 -0700789 if (refs == 0) {
790 ret = -EROFS;
791 btrfs_std_error(root->fs_info, ret);
792 return ret;
793 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400794 } else {
795 refs = 1;
796 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
797 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
798 flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
799 else
800 flags = 0;
801 }
802
803 owner = btrfs_header_owner(buf);
804 BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
805 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
806
807 if (refs > 1) {
808 if ((owner == root->root_key.objectid ||
809 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
810 !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200811 ret = btrfs_inc_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100812 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400813
814 if (root->root_key.objectid ==
815 BTRFS_TREE_RELOC_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200816 ret = btrfs_dec_ref(trans, root, buf, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100817 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200818 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100819 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400820 }
821 new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
822 } else {
823
824 if (root->root_key.objectid ==
825 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200826 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400827 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200828 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100829 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400830 }
831 if (new_flags != 0) {
832 ret = btrfs_set_disk_extent_flags(trans, root,
833 buf->start,
834 buf->len,
835 new_flags, 0);
Mark Fashehbe1a5562011-08-08 13:20:18 -0700836 if (ret)
837 return ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400838 }
839 } else {
840 if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
841 if (root->root_key.objectid ==
842 BTRFS_TREE_RELOC_OBJECTID)
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200843 ret = btrfs_inc_ref(trans, root, cow, 1, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400844 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200845 ret = btrfs_inc_ref(trans, root, cow, 0, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100846 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200847 ret = btrfs_dec_ref(trans, root, buf, 1, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100848 BUG_ON(ret); /* -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400849 }
Jan Schmidtf2304752012-05-26 11:43:17 +0200850 /*
851 * don't log freeing in case we're freeing the root node, this
852 * is done by tree_mod_log_set_root_pointer later
853 */
854 if (buf != root->node && btrfs_header_level(buf) != 0)
855 tree_mod_log_free_eb(root->fs_info, buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400856 clean_tree_block(trans, root, buf);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400857 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400858 }
859 return 0;
860}
861
862/*
Chris Masond3977122009-01-05 21:25:51 -0500863 * does the dirty work in cow of a single block. The parent block (if
864 * supplied) is updated to point to the new cow copy. The new buffer is marked
865 * dirty and returned locked. If you modify the block it needs to be marked
866 * dirty again.
Chris Masond352ac62008-09-29 15:18:18 -0400867 *
868 * search_start -- an allocation hint for the new block
869 *
Chris Masond3977122009-01-05 21:25:51 -0500870 * empty_size -- a hint that you plan on doing more cow. This is the size in
871 * bytes the allocator should try to find free next to the block it returns.
872 * This is just a hint and may be ignored by the allocator.
Chris Masond352ac62008-09-29 15:18:18 -0400873 */
Chris Masond3977122009-01-05 21:25:51 -0500874static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -0400875 struct btrfs_root *root,
876 struct extent_buffer *buf,
877 struct extent_buffer *parent, int parent_slot,
878 struct extent_buffer **cow_ret,
Chris Mason9fa8cfe2009-03-13 10:24:59 -0400879 u64 search_start, u64 empty_size)
Chris Mason6702ed42007-08-07 16:15:09 -0400880{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400881 struct btrfs_disk_key disk_key;
Chris Mason5f39d392007-10-15 16:14:19 -0400882 struct extent_buffer *cow;
Mark Fashehbe1a5562011-08-08 13:20:18 -0700883 int level, ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -0400884 int last_ref = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400885 int unlock_orig = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400886 u64 parent_start;
Chris Mason6702ed42007-08-07 16:15:09 -0400887
Chris Mason925baed2008-06-25 16:01:30 -0400888 if (*cow_ret == buf)
889 unlock_orig = 1;
890
Chris Masonb9447ef2009-03-09 11:45:38 -0400891 btrfs_assert_tree_locked(buf);
Chris Mason925baed2008-06-25 16:01:30 -0400892
Chris Mason7bb86312007-12-11 09:25:06 -0500893 WARN_ON(root->ref_cows && trans->transid !=
894 root->fs_info->running_transaction->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400895 WARN_ON(root->ref_cows && trans->transid != root->last_trans);
Chris Mason5f39d392007-10-15 16:14:19 -0400896
Chris Mason7bb86312007-12-11 09:25:06 -0500897 level = btrfs_header_level(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400898
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400899 if (level == 0)
900 btrfs_item_key(buf, &disk_key, 0);
901 else
902 btrfs_node_key(buf, &disk_key, 0);
903
904 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) {
905 if (parent)
906 parent_start = parent->start;
907 else
908 parent_start = 0;
909 } else
910 parent_start = 0;
911
912 cow = btrfs_alloc_free_block(trans, root, buf->len, parent_start,
913 root->root_key.objectid, &disk_key,
Jan Schmidt5581a512012-05-16 17:04:52 +0200914 level, search_start, empty_size);
Chris Mason6702ed42007-08-07 16:15:09 -0400915 if (IS_ERR(cow))
916 return PTR_ERR(cow);
917
Chris Masonb4ce94d2009-02-04 09:25:08 -0500918 /* cow is set to blocking by btrfs_init_new_buffer */
919
Chris Mason5f39d392007-10-15 16:14:19 -0400920 copy_extent_buffer(cow, buf, 0, 0, cow->len);
Chris Masondb945352007-10-15 16:15:53 -0400921 btrfs_set_header_bytenr(cow, cow->start);
Chris Mason5f39d392007-10-15 16:14:19 -0400922 btrfs_set_header_generation(cow, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400923 btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
924 btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
925 BTRFS_HEADER_FLAG_RELOC);
926 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
927 btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
928 else
929 btrfs_set_header_owner(cow, root->root_key.objectid);
Chris Mason6702ed42007-08-07 16:15:09 -0400930
Yan Zheng2b820322008-11-17 21:11:30 -0500931 write_extent_buffer(cow, root->fs_info->fsid,
932 (unsigned long)btrfs_header_fsid(cow),
933 BTRFS_FSID_SIZE);
934
Mark Fashehbe1a5562011-08-08 13:20:18 -0700935 ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700936 if (ret) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100937 btrfs_abort_transaction(trans, root, ret);
Mark Fashehb68dc2a2011-08-29 14:30:39 -0700938 return ret;
939 }
Zheng Yan1a40e232008-09-26 10:09:34 -0400940
Yan, Zheng3fd0a552010-05-16 10:49:59 -0400941 if (root->ref_cows)
942 btrfs_reloc_cow_block(trans, root, buf, cow);
943
Chris Mason6702ed42007-08-07 16:15:09 -0400944 if (buf == root->node) {
Chris Mason925baed2008-06-25 16:01:30 -0400945 WARN_ON(parent && parent != buf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
947 btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
948 parent_start = buf->start;
949 else
950 parent_start = 0;
Chris Mason925baed2008-06-25 16:01:30 -0400951
Chris Mason5f39d392007-10-15 16:14:19 -0400952 extent_buffer_get(cow);
Jan Schmidtf2304752012-05-26 11:43:17 +0200953 tree_mod_log_set_root_pointer(root, cow);
Chris Mason240f62c2011-03-23 14:54:42 -0400954 rcu_assign_pointer(root->node, cow);
Chris Mason925baed2008-06-25 16:01:30 -0400955
Yan, Zhengf0486c62010-05-16 10:46:25 -0400956 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200957 last_ref);
Chris Mason5f39d392007-10-15 16:14:19 -0400958 free_extent_buffer(buf);
Chris Mason0b86a832008-03-24 15:01:56 -0400959 add_root_to_dirty_list(root);
Chris Mason6702ed42007-08-07 16:15:09 -0400960 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
962 parent_start = parent->start;
963 else
964 parent_start = 0;
965
966 WARN_ON(trans->transid != btrfs_header_generation(parent));
Jan Schmidtf2304752012-05-26 11:43:17 +0200967 tree_mod_log_insert_key(root->fs_info, parent, parent_slot,
968 MOD_LOG_KEY_REPLACE);
Chris Mason5f39d392007-10-15 16:14:19 -0400969 btrfs_set_node_blockptr(parent, parent_slot,
Chris Masondb945352007-10-15 16:15:53 -0400970 cow->start);
Chris Mason74493f72007-12-11 09:25:06 -0500971 btrfs_set_node_ptr_generation(parent, parent_slot,
972 trans->transid);
Chris Mason6702ed42007-08-07 16:15:09 -0400973 btrfs_mark_buffer_dirty(parent);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400974 btrfs_free_tree_block(trans, root, buf, parent_start,
Jan Schmidt5581a512012-05-16 17:04:52 +0200975 last_ref);
Chris Mason6702ed42007-08-07 16:15:09 -0400976 }
Chris Mason925baed2008-06-25 16:01:30 -0400977 if (unlock_orig)
978 btrfs_tree_unlock(buf);
Josef Bacik3083ee22012-03-09 16:01:49 -0500979 free_extent_buffer_stale(buf);
Chris Mason6702ed42007-08-07 16:15:09 -0400980 btrfs_mark_buffer_dirty(cow);
981 *cow_ret = cow;
982 return 0;
983}
984
Jan Schmidt5d9e75c42012-05-16 18:25:47 +0200985/*
986 * returns the logical address of the oldest predecessor of the given root.
987 * entries older than time_seq are ignored.
988 */
989static struct tree_mod_elem *
990__tree_mod_log_oldest_root(struct btrfs_fs_info *fs_info,
991 struct btrfs_root *root, u64 time_seq)
992{
993 struct tree_mod_elem *tm;
994 struct tree_mod_elem *found = NULL;
995 u64 root_logical = root->node->start;
996 int looped = 0;
997
998 if (!time_seq)
999 return 0;
1000
1001 /*
1002 * the very last operation that's logged for a root is the replacement
1003 * operation (if it is replaced at all). this has the index of the *new*
1004 * root, making it the very first operation that's logged for this root.
1005 */
1006 while (1) {
1007 tm = tree_mod_log_search_oldest(fs_info, root_logical,
1008 time_seq);
1009 if (!looped && !tm)
1010 return 0;
1011 /*
1012 * we must have key remove operations in the log before the
1013 * replace operation.
1014 */
1015 BUG_ON(!tm);
1016
1017 if (tm->op != MOD_LOG_ROOT_REPLACE)
1018 break;
1019
1020 found = tm;
1021 root_logical = tm->old_root.logical;
1022 BUG_ON(root_logical == root->node->start);
1023 looped = 1;
1024 }
1025
Jan Schmidta95236d2012-06-05 16:41:24 +02001026 /* if there's no old root to return, return what we found instead */
1027 if (!found)
1028 found = tm;
1029
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001030 return found;
1031}
1032
1033/*
1034 * tm is a pointer to the first operation to rewind within eb. then, all
1035 * previous operations will be rewinded (until we reach something older than
1036 * time_seq).
1037 */
1038static void
1039__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1040 struct tree_mod_elem *first_tm)
1041{
1042 u32 n;
1043 struct rb_node *next;
1044 struct tree_mod_elem *tm = first_tm;
1045 unsigned long o_dst;
1046 unsigned long o_src;
1047 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1048
1049 n = btrfs_header_nritems(eb);
1050 while (tm && tm->elem.seq >= time_seq) {
1051 /*
1052 * all the operations are recorded with the operator used for
1053 * the modification. as we're going backwards, we do the
1054 * opposite of each operation here.
1055 */
1056 switch (tm->op) {
1057 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1058 BUG_ON(tm->slot < n);
1059 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1060 case MOD_LOG_KEY_REMOVE:
1061 btrfs_set_node_key(eb, &tm->key, tm->slot);
1062 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1063 btrfs_set_node_ptr_generation(eb, tm->slot,
1064 tm->generation);
1065 n++;
1066 break;
1067 case MOD_LOG_KEY_REPLACE:
1068 BUG_ON(tm->slot >= n);
1069 btrfs_set_node_key(eb, &tm->key, tm->slot);
1070 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1071 btrfs_set_node_ptr_generation(eb, tm->slot,
1072 tm->generation);
1073 break;
1074 case MOD_LOG_KEY_ADD:
1075 if (tm->slot != n - 1) {
1076 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1077 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1078 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1079 }
1080 n--;
1081 break;
1082 case MOD_LOG_MOVE_KEYS:
Jan Schmidtc3193102012-05-31 19:24:36 +02001083 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1084 o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
1085 memmove_extent_buffer(eb, o_dst, o_src,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001086 tm->move.nr_items * p_size);
1087 break;
1088 case MOD_LOG_ROOT_REPLACE:
1089 /*
1090 * this operation is special. for roots, this must be
1091 * handled explicitly before rewinding.
1092 * for non-roots, this operation may exist if the node
1093 * was a root: root A -> child B; then A gets empty and
1094 * B is promoted to the new root. in the mod log, we'll
1095 * have a root-replace operation for B, a tree block
1096 * that is no root. we simply ignore that operation.
1097 */
1098 break;
1099 }
1100 next = rb_next(&tm->node);
1101 if (!next)
1102 break;
1103 tm = container_of(next, struct tree_mod_elem, node);
1104 if (tm->index != first_tm->index)
1105 break;
1106 }
1107 btrfs_set_header_nritems(eb, n);
1108}
1109
1110static struct extent_buffer *
1111tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1112 u64 time_seq)
1113{
1114 struct extent_buffer *eb_rewin;
1115 struct tree_mod_elem *tm;
1116
1117 if (!time_seq)
1118 return eb;
1119
1120 if (btrfs_header_level(eb) == 0)
1121 return eb;
1122
1123 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1124 if (!tm)
1125 return eb;
1126
1127 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1128 BUG_ON(tm->slot != 0);
1129 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1130 fs_info->tree_root->nodesize);
1131 BUG_ON(!eb_rewin);
1132 btrfs_set_header_bytenr(eb_rewin, eb->start);
1133 btrfs_set_header_backref_rev(eb_rewin,
1134 btrfs_header_backref_rev(eb));
1135 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
Jan Schmidtc3193102012-05-31 19:24:36 +02001136 btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001137 } else {
1138 eb_rewin = btrfs_clone_extent_buffer(eb);
1139 BUG_ON(!eb_rewin);
1140 }
1141
1142 extent_buffer_get(eb_rewin);
1143 free_extent_buffer(eb);
1144
1145 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1146
1147 return eb_rewin;
1148}
1149
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001150/*
1151 * get_old_root() rewinds the state of @root's root node to the given @time_seq
1152 * value. If there are no changes, the current root->root_node is returned. If
1153 * anything changed in between, there's a fresh buffer allocated on which the
1154 * rewind operations are done. In any case, the returned buffer is read locked.
1155 * Returns NULL on error (with no locks held).
1156 */
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001157static inline struct extent_buffer *
1158get_old_root(struct btrfs_root *root, u64 time_seq)
1159{
1160 struct tree_mod_elem *tm;
1161 struct extent_buffer *eb;
Jan Schmidta95236d2012-06-05 16:41:24 +02001162 struct tree_mod_root *old_root = NULL;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001163 u64 old_generation;
Jan Schmidta95236d2012-06-05 16:41:24 +02001164 u64 logical;
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001165
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001166 eb = btrfs_read_lock_root_node(root);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001167 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1168 if (!tm)
1169 return root->node;
1170
Jan Schmidta95236d2012-06-05 16:41:24 +02001171 if (tm->op == MOD_LOG_ROOT_REPLACE) {
1172 old_root = &tm->old_root;
1173 old_generation = tm->generation;
1174 logical = old_root->logical;
1175 } else {
1176 logical = root->node->start;
1177 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001178
Jan Schmidta95236d2012-06-05 16:41:24 +02001179 tm = tree_mod_log_search(root->fs_info, logical, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001180 /*
1181 * there was an item in the log when __tree_mod_log_oldest_root
1182 * returned. this one must not go away, because the time_seq passed to
1183 * us must be blocking its removal.
1184 */
1185 BUG_ON(!tm);
1186
Jan Schmidta95236d2012-06-05 16:41:24 +02001187 if (old_root)
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001188 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1189 root->nodesize);
Jan Schmidta95236d2012-06-05 16:41:24 +02001190 else
1191 eb = btrfs_clone_extent_buffer(root->node);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001192 btrfs_tree_read_unlock(root->node);
1193 free_extent_buffer(root->node);
1194 if (!eb)
1195 return NULL;
1196 btrfs_tree_read_lock(eb);
Jan Schmidta95236d2012-06-05 16:41:24 +02001197 if (old_root) {
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001198 btrfs_set_header_bytenr(eb, eb->start);
1199 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1200 btrfs_set_header_owner(eb, root->root_key.objectid);
Jan Schmidta95236d2012-06-05 16:41:24 +02001201 btrfs_set_header_level(eb, old_root->level);
1202 btrfs_set_header_generation(eb, old_generation);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001203 }
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001204 __tree_mod_log_rewind(eb, time_seq, tm);
Jan Schmidt8ba97a12012-06-04 16:54:57 +02001205 extent_buffer_get(eb);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02001206
1207 return eb;
1208}
1209
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001210static inline int should_cow_block(struct btrfs_trans_handle *trans,
1211 struct btrfs_root *root,
1212 struct extent_buffer *buf)
1213{
Liu Bof1ebcc72011-11-14 20:48:06 -05001214 /* ensure we can see the force_cow */
1215 smp_rmb();
1216
1217 /*
1218 * We do not need to cow a block if
1219 * 1) this block is not created or changed in this transaction;
1220 * 2) this block does not belong to TREE_RELOC tree;
1221 * 3) the root is not forced COW.
1222 *
1223 * What is forced COW:
1224 * when we create snapshot during commiting the transaction,
1225 * after we've finished coping src root, we must COW the shared
1226 * block to ensure the metadata consistency.
1227 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001228 if (btrfs_header_generation(buf) == trans->transid &&
1229 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1230 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001231 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1232 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001233 return 0;
1234 return 1;
1235}
1236
Chris Masond352ac62008-09-29 15:18:18 -04001237/*
1238 * cows a single block, see __btrfs_cow_block for the real work.
1239 * This version of it has extra checks so that a block isn't cow'd more than
1240 * once per transaction, as long as it hasn't been written yet
1241 */
Chris Masond3977122009-01-05 21:25:51 -05001242noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001243 struct btrfs_root *root, struct extent_buffer *buf,
1244 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001245 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001246{
Chris Mason6702ed42007-08-07 16:15:09 -04001247 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001248 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001249
Chris Masonccd467d2007-06-28 15:57:36 -04001250 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001251 printk(KERN_CRIT "trans %llu running %llu\n",
1252 (unsigned long long)trans->transid,
1253 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001254 root->fs_info->running_transaction->transid);
1255 WARN_ON(1);
1256 }
1257 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001258 printk(KERN_CRIT "trans %llu running %llu\n",
1259 (unsigned long long)trans->transid,
1260 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001261 WARN_ON(1);
1262 }
Chris Masondc17ff82008-01-08 15:46:30 -05001263
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001264 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001265 *cow_ret = buf;
1266 return 0;
1267 }
Chris Masonc4876852009-02-04 09:24:25 -05001268
Chris Mason0b86a832008-03-24 15:01:56 -04001269 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001270
1271 if (parent)
1272 btrfs_set_lock_blocking(parent);
1273 btrfs_set_lock_blocking(buf);
1274
Chris Masonf510cfe2007-10-15 16:14:48 -04001275 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001276 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001277
1278 trace_btrfs_cow_block(root, buf, *cow_ret);
1279
Chris Masonf510cfe2007-10-15 16:14:48 -04001280 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001281}
1282
Chris Masond352ac62008-09-29 15:18:18 -04001283/*
1284 * helper function for defrag to decide if two blocks pointed to by a
1285 * node are actually close by
1286 */
Chris Mason6b800532007-10-15 16:17:34 -04001287static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001288{
Chris Mason6b800532007-10-15 16:17:34 -04001289 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001290 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001291 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001292 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001293 return 0;
1294}
1295
Chris Mason081e9572007-11-06 10:26:24 -05001296/*
1297 * compare two keys in a memcmp fashion
1298 */
1299static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1300{
1301 struct btrfs_key k1;
1302
1303 btrfs_disk_key_to_cpu(&k1, disk);
1304
Diego Calleja20736ab2009-07-24 11:06:52 -04001305 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001306}
1307
Josef Bacikf3465ca2008-11-12 14:19:50 -05001308/*
1309 * same as comp_keys only with two btrfs_key's
1310 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001311int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001312{
1313 if (k1->objectid > k2->objectid)
1314 return 1;
1315 if (k1->objectid < k2->objectid)
1316 return -1;
1317 if (k1->type > k2->type)
1318 return 1;
1319 if (k1->type < k2->type)
1320 return -1;
1321 if (k1->offset > k2->offset)
1322 return 1;
1323 if (k1->offset < k2->offset)
1324 return -1;
1325 return 0;
1326}
Chris Mason081e9572007-11-06 10:26:24 -05001327
Chris Masond352ac62008-09-29 15:18:18 -04001328/*
1329 * this is used by the defrag code to go through all the
1330 * leaves pointed to by a node and reallocate them so that
1331 * disk order is close to key order
1332 */
Chris Mason6702ed42007-08-07 16:15:09 -04001333int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001334 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001335 int start_slot, int cache_only, u64 *last_ret,
1336 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001337{
Chris Mason6b800532007-10-15 16:17:34 -04001338 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001339 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001340 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001341 u64 search_start = *last_ret;
1342 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001343 u64 other;
1344 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001345 int end_slot;
1346 int i;
1347 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001348 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001349 int uptodate;
1350 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001351 int progress_passed = 0;
1352 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001353
Chris Mason5708b952007-10-25 15:43:18 -04001354 parent_level = btrfs_header_level(parent);
1355 if (cache_only && parent_level != 1)
1356 return 0;
1357
Chris Masond3977122009-01-05 21:25:51 -05001358 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001359 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001360 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001361 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001362
Chris Mason6b800532007-10-15 16:17:34 -04001363 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001364 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001365 end_slot = parent_nritems;
1366
1367 if (parent_nritems == 1)
1368 return 0;
1369
Chris Masonb4ce94d2009-02-04 09:25:08 -05001370 btrfs_set_lock_blocking(parent);
1371
Chris Mason6702ed42007-08-07 16:15:09 -04001372 for (i = start_slot; i < end_slot; i++) {
1373 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001374
Chris Mason081e9572007-11-06 10:26:24 -05001375 btrfs_node_key(parent, &disk_key, i);
1376 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1377 continue;
1378
1379 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001380 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001381 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001382 if (last_block == 0)
1383 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001384
Chris Mason6702ed42007-08-07 16:15:09 -04001385 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001386 other = btrfs_node_blockptr(parent, i - 1);
1387 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001388 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001389 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001390 other = btrfs_node_blockptr(parent, i + 1);
1391 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001392 }
Chris Masone9d0b132007-08-10 14:06:19 -04001393 if (close) {
1394 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001395 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001396 }
Chris Mason6702ed42007-08-07 16:15:09 -04001397
Chris Mason6b800532007-10-15 16:17:34 -04001398 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1399 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001400 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001401 else
1402 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001403 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001404 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001405 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001406 continue;
1407 }
Chris Mason6b800532007-10-15 16:17:34 -04001408 if (!cur) {
1409 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001410 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001411 if (!cur)
1412 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001413 } else if (!uptodate) {
Tsutomu Itoh018642a2012-05-29 18:10:13 +09001414 err = btrfs_read_buffer(cur, gen);
1415 if (err) {
1416 free_extent_buffer(cur);
1417 return err;
1418 }
Chris Masonf2183bd2007-08-10 14:42:37 -04001419 }
Chris Mason6702ed42007-08-07 16:15:09 -04001420 }
Chris Masone9d0b132007-08-10 14:06:19 -04001421 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001422 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001423
Chris Masone7a84562008-06-25 16:01:31 -04001424 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001425 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001426 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001427 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001428 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001429 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001430 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001431 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001432 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001433 break;
Yan252c38f2007-08-29 09:11:44 -04001434 }
Chris Masone7a84562008-06-25 16:01:31 -04001435 search_start = cur->start;
1436 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001437 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001438 btrfs_tree_unlock(cur);
1439 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001440 }
1441 return err;
1442}
1443
Chris Mason74123bd2007-02-02 11:05:29 -05001444/*
1445 * The leaf data grows from end-to-front in the node.
1446 * this returns the address of the start of the last item,
1447 * which is the stop of the leaf data stack
1448 */
Chris Mason123abc82007-03-14 14:14:43 -04001449static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001450 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001451{
Chris Mason5f39d392007-10-15 16:14:19 -04001452 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001453 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001454 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001455 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001456}
1457
Chris Masonaa5d6be2007-02-28 16:35:06 -05001458
Chris Mason74123bd2007-02-02 11:05:29 -05001459/*
Chris Mason5f39d392007-10-15 16:14:19 -04001460 * search for key in the extent_buffer. The items start at offset p,
1461 * and they are item_size apart. There are 'max' items in p.
1462 *
Chris Mason74123bd2007-02-02 11:05:29 -05001463 * the slot in the array is returned via slot, and it points to
1464 * the place where you would insert key if it is not found in
1465 * the array.
1466 *
1467 * slot may point to max if the key is bigger than all of the keys
1468 */
Chris Masone02119d2008-09-05 16:13:11 -04001469static noinline int generic_bin_search(struct extent_buffer *eb,
1470 unsigned long p,
1471 int item_size, struct btrfs_key *key,
1472 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001473{
1474 int low = 0;
1475 int high = max;
1476 int mid;
1477 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001478 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001479 struct btrfs_disk_key unaligned;
1480 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001481 char *kaddr = NULL;
1482 unsigned long map_start = 0;
1483 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001484 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001485
Chris Masond3977122009-01-05 21:25:51 -05001486 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001487 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001488 offset = p + mid * item_size;
1489
Chris Masona6591712011-07-19 12:04:14 -04001490 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001491 (offset + sizeof(struct btrfs_disk_key)) >
1492 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001493
1494 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001495 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001496 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001497
Chris Mason479965d2007-10-15 16:14:27 -04001498 if (!err) {
1499 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1500 map_start);
1501 } else {
1502 read_extent_buffer(eb, &unaligned,
1503 offset, sizeof(unaligned));
1504 tmp = &unaligned;
1505 }
1506
Chris Mason5f39d392007-10-15 16:14:19 -04001507 } else {
1508 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1509 map_start);
1510 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001511 ret = comp_keys(tmp, key);
1512
1513 if (ret < 0)
1514 low = mid + 1;
1515 else if (ret > 0)
1516 high = mid;
1517 else {
1518 *slot = mid;
1519 return 0;
1520 }
1521 }
1522 *slot = low;
1523 return 1;
1524}
1525
Chris Mason97571fd2007-02-24 13:39:08 -05001526/*
1527 * simple bin_search frontend that does the right thing for
1528 * leaves vs nodes
1529 */
Chris Mason5f39d392007-10-15 16:14:19 -04001530static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1531 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001532{
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001533 if (level == 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001534 return generic_bin_search(eb,
1535 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001536 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001537 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001538 slot);
Wang Sheng-Huif7757382012-03-30 15:14:27 +08001539 else
Chris Mason5f39d392007-10-15 16:14:19 -04001540 return generic_bin_search(eb,
1541 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001542 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001543 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001544 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001545}
1546
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001547int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1548 int level, int *slot)
1549{
1550 return bin_search(eb, key, level, slot);
1551}
1552
Yan, Zhengf0486c62010-05-16 10:46:25 -04001553static void root_add_used(struct btrfs_root *root, u32 size)
1554{
1555 spin_lock(&root->accounting_lock);
1556 btrfs_set_root_used(&root->root_item,
1557 btrfs_root_used(&root->root_item) + size);
1558 spin_unlock(&root->accounting_lock);
1559}
1560
1561static void root_sub_used(struct btrfs_root *root, u32 size)
1562{
1563 spin_lock(&root->accounting_lock);
1564 btrfs_set_root_used(&root->root_item,
1565 btrfs_root_used(&root->root_item) - size);
1566 spin_unlock(&root->accounting_lock);
1567}
1568
Chris Masond352ac62008-09-29 15:18:18 -04001569/* given a node and slot number, this reads the blocks it points to. The
1570 * extent buffer is returned with a reference taken (but unlocked).
1571 * NULL is returned on error.
1572 */
Chris Masone02119d2008-09-05 16:13:11 -04001573static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001574 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001575{
Chris Masonca7a79a2008-05-12 12:59:19 -04001576 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001577 if (slot < 0)
1578 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001579 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001580 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001581
1582 BUG_ON(level == 0);
1583
Chris Masondb945352007-10-15 16:15:53 -04001584 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001585 btrfs_level_size(root, level - 1),
1586 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001587}
1588
Chris Masond352ac62008-09-29 15:18:18 -04001589/*
1590 * node level balancing, used to make sure nodes are in proper order for
1591 * item deletion. We balance from the top down, so we have to make sure
1592 * that a deletion won't leave an node completely empty later on.
1593 */
Chris Masone02119d2008-09-05 16:13:11 -04001594static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001595 struct btrfs_root *root,
1596 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001597{
Chris Mason5f39d392007-10-15 16:14:19 -04001598 struct extent_buffer *right = NULL;
1599 struct extent_buffer *mid;
1600 struct extent_buffer *left = NULL;
1601 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001602 int ret = 0;
1603 int wret;
1604 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001605 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001606 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001607
1608 if (level == 0)
1609 return 0;
1610
Chris Mason5f39d392007-10-15 16:14:19 -04001611 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001612
Chris Masonbd681512011-07-16 15:23:14 -04001613 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1614 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001615 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1616
Chris Mason1d4f8a02007-03-13 09:28:32 -04001617 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001618
Li Zefana05a9bb2011-09-06 16:55:34 +08001619 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001620 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001621 pslot = path->slots[level + 1];
1622 }
Chris Masonbb803952007-03-01 12:04:21 -05001623
Chris Mason40689472007-03-17 14:29:23 -04001624 /*
1625 * deal with the case where there is only one pointer in the root
1626 * by promoting the node below to a root
1627 */
Chris Mason5f39d392007-10-15 16:14:19 -04001628 if (!parent) {
1629 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001630
Chris Mason5f39d392007-10-15 16:14:19 -04001631 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001632 return 0;
1633
1634 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001635 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001636 if (!child) {
1637 ret = -EROFS;
1638 btrfs_std_error(root->fs_info, ret);
1639 goto enospc;
1640 }
1641
Chris Mason925baed2008-06-25 16:01:30 -04001642 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001643 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001644 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001645 if (ret) {
1646 btrfs_tree_unlock(child);
1647 free_extent_buffer(child);
1648 goto enospc;
1649 }
Yan2f375ab2008-02-01 14:58:07 -05001650
Jan Schmidtf2304752012-05-26 11:43:17 +02001651 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001652 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001653
Chris Mason0b86a832008-03-24 15:01:56 -04001654 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001655 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001656
Chris Mason925baed2008-06-25 16:01:30 -04001657 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001658 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001659 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001660 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001661 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001662 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001663
1664 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001665 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001666 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001667 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001668 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001669 }
Chris Mason5f39d392007-10-15 16:14:19 -04001670 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001671 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001672 return 0;
1673
Chris Mason5f39d392007-10-15 16:14:19 -04001674 left = read_node_slot(root, parent, pslot - 1);
1675 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001676 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001677 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001678 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001679 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001680 if (wret) {
1681 ret = wret;
1682 goto enospc;
1683 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001684 }
Chris Mason5f39d392007-10-15 16:14:19 -04001685 right = read_node_slot(root, parent, pslot + 1);
1686 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001687 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001688 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001689 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001690 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001691 if (wret) {
1692 ret = wret;
1693 goto enospc;
1694 }
1695 }
1696
1697 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001698 if (left) {
1699 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001700 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001701 if (wret < 0)
1702 ret = wret;
Chris Masonbb803952007-03-01 12:04:21 -05001703 }
Chris Mason79f95c82007-03-01 15:16:26 -05001704
1705 /*
1706 * then try to empty the right most buffer into the middle
1707 */
Chris Mason5f39d392007-10-15 16:14:19 -04001708 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001709 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001710 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001711 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001712 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001713 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001714 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001715 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001716 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001717 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001718 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001719 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001720 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001721 struct btrfs_disk_key right_key;
1722 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001723 tree_mod_log_set_node_key(root->fs_info, parent,
1724 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001725 btrfs_set_node_key(parent, &right_key, pslot + 1);
1726 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001727 }
1728 }
Chris Mason5f39d392007-10-15 16:14:19 -04001729 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001730 /*
1731 * we're not allowed to leave a node with one item in the
1732 * tree during a delete. A deletion from lower in the tree
1733 * could try to delete the only pointer in this node.
1734 * So, pull some keys from the left.
1735 * There has to be a left pointer at this point because
1736 * otherwise we would have pulled some pointers from the
1737 * right
1738 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001739 if (!left) {
1740 ret = -EROFS;
1741 btrfs_std_error(root->fs_info, ret);
1742 goto enospc;
1743 }
Chris Mason5f39d392007-10-15 16:14:19 -04001744 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001745 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001746 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001747 goto enospc;
1748 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001749 if (wret == 1) {
1750 wret = push_node_left(trans, root, left, mid, 1);
1751 if (wret < 0)
1752 ret = wret;
1753 }
Chris Mason79f95c82007-03-01 15:16:26 -05001754 BUG_ON(wret == 1);
1755 }
Chris Mason5f39d392007-10-15 16:14:19 -04001756 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001757 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001758 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001759 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001760 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001761 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001762 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001763 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001764 } else {
1765 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001766 struct btrfs_disk_key mid_key;
1767 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001768 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1769 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001770 btrfs_set_node_key(parent, &mid_key, pslot);
1771 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001772 }
Chris Masonbb803952007-03-01 12:04:21 -05001773
Chris Mason79f95c82007-03-01 15:16:26 -05001774 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001775 if (left) {
1776 if (btrfs_header_nritems(left) > orig_slot) {
1777 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001778 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001779 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001780 path->slots[level + 1] -= 1;
1781 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001782 if (mid) {
1783 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001784 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001785 }
Chris Masonbb803952007-03-01 12:04:21 -05001786 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001787 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001788 path->slots[level] = orig_slot;
1789 }
1790 }
Chris Mason79f95c82007-03-01 15:16:26 -05001791 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001792 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001793 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001794 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001795enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001796 if (right) {
1797 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001798 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001799 }
1800 if (left) {
1801 if (path->nodes[level] != left)
1802 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001803 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001804 }
Chris Masonbb803952007-03-01 12:04:21 -05001805 return ret;
1806}
1807
Chris Masond352ac62008-09-29 15:18:18 -04001808/* Node balancing for insertion. Here we only split or push nodes around
1809 * when they are completely full. This is also done top down, so we
1810 * have to be pessimistic.
1811 */
Chris Masond3977122009-01-05 21:25:51 -05001812static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001813 struct btrfs_root *root,
1814 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001815{
Chris Mason5f39d392007-10-15 16:14:19 -04001816 struct extent_buffer *right = NULL;
1817 struct extent_buffer *mid;
1818 struct extent_buffer *left = NULL;
1819 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001820 int ret = 0;
1821 int wret;
1822 int pslot;
1823 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001824
1825 if (level == 0)
1826 return 1;
1827
Chris Mason5f39d392007-10-15 16:14:19 -04001828 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001829 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001830
Li Zefana05a9bb2011-09-06 16:55:34 +08001831 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001832 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001833 pslot = path->slots[level + 1];
1834 }
Chris Masone66f7092007-04-20 13:16:02 -04001835
Chris Mason5f39d392007-10-15 16:14:19 -04001836 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001837 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001838
Chris Mason5f39d392007-10-15 16:14:19 -04001839 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001840
1841 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001842 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001843 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001844
1845 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001846 btrfs_set_lock_blocking(left);
1847
Chris Mason5f39d392007-10-15 16:14:19 -04001848 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001849 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1850 wret = 1;
1851 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001852 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001853 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001854 if (ret)
1855 wret = 1;
1856 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001857 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001858 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001859 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001860 }
Chris Masone66f7092007-04-20 13:16:02 -04001861 if (wret < 0)
1862 ret = wret;
1863 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001864 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001865 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001866 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001867 tree_mod_log_set_node_key(root->fs_info, parent,
1868 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001869 btrfs_set_node_key(parent, &disk_key, pslot);
1870 btrfs_mark_buffer_dirty(parent);
1871 if (btrfs_header_nritems(left) > orig_slot) {
1872 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001873 path->slots[level + 1] -= 1;
1874 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001875 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001876 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001877 } else {
1878 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001879 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001880 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001881 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001882 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001883 }
Chris Masone66f7092007-04-20 13:16:02 -04001884 return 0;
1885 }
Chris Mason925baed2008-06-25 16:01:30 -04001886 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001887 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001888 }
Chris Mason925baed2008-06-25 16:01:30 -04001889 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001890
1891 /*
1892 * then try to empty the right most buffer into the middle
1893 */
Chris Mason5f39d392007-10-15 16:14:19 -04001894 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001895 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001896
Chris Mason925baed2008-06-25 16:01:30 -04001897 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001898 btrfs_set_lock_blocking(right);
1899
Chris Mason5f39d392007-10-15 16:14:19 -04001900 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001901 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1902 wret = 1;
1903 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001904 ret = btrfs_cow_block(trans, root, right,
1905 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001906 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001907 if (ret)
1908 wret = 1;
1909 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001910 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001911 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001912 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001913 }
Chris Masone66f7092007-04-20 13:16:02 -04001914 if (wret < 0)
1915 ret = wret;
1916 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001917 struct btrfs_disk_key disk_key;
1918
1919 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001920 tree_mod_log_set_node_key(root->fs_info, parent,
1921 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001922 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1923 btrfs_mark_buffer_dirty(parent);
1924
1925 if (btrfs_header_nritems(mid) <= orig_slot) {
1926 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001927 path->slots[level + 1] += 1;
1928 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001929 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001930 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001931 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001932 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001933 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001934 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001935 }
Chris Masone66f7092007-04-20 13:16:02 -04001936 return 0;
1937 }
Chris Mason925baed2008-06-25 16:01:30 -04001938 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001939 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001940 }
Chris Masone66f7092007-04-20 13:16:02 -04001941 return 1;
1942}
1943
Chris Mason74123bd2007-02-02 11:05:29 -05001944/*
Chris Masond352ac62008-09-29 15:18:18 -04001945 * readahead one full node of leaves, finding things that are close
1946 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001947 */
Chris Masonc8c42862009-04-03 10:14:18 -04001948static void reada_for_search(struct btrfs_root *root,
1949 struct btrfs_path *path,
1950 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001951{
Chris Mason5f39d392007-10-15 16:14:19 -04001952 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001953 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001954 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001955 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001956 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001957 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001958 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001959 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001960 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001961 u32 nr;
1962 u32 blocksize;
1963 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001964
Chris Masona6b6e752007-10-15 16:22:39 -04001965 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001966 return;
1967
Chris Mason6702ed42007-08-07 16:15:09 -04001968 if (!path->nodes[level])
1969 return;
1970
Chris Mason5f39d392007-10-15 16:14:19 -04001971 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001972
Chris Mason3c69fae2007-08-07 15:52:22 -04001973 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001974 blocksize = btrfs_level_size(root, level - 1);
1975 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001976 if (eb) {
1977 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001978 return;
1979 }
1980
Chris Masona7175312009-01-22 09:23:10 -05001981 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001982
Chris Mason5f39d392007-10-15 16:14:19 -04001983 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001984 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001985
Chris Masond3977122009-01-05 21:25:51 -05001986 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001987 if (direction < 0) {
1988 if (nr == 0)
1989 break;
1990 nr--;
1991 } else if (direction > 0) {
1992 nr++;
1993 if (nr >= nritems)
1994 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001995 }
Chris Mason01f46652007-12-21 16:24:26 -05001996 if (path->reada < 0 && objectid) {
1997 btrfs_node_key(node, &disk_key, nr);
1998 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1999 break;
2000 }
Chris Mason6b800532007-10-15 16:17:34 -04002001 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05002002 if ((search <= target && target - search <= 65536) ||
2003 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002004 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04002005 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04002006 nread += blocksize;
2007 }
2008 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05002009 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04002010 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04002011 }
2012}
Chris Mason925baed2008-06-25 16:01:30 -04002013
Chris Masond352ac62008-09-29 15:18:18 -04002014/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002015 * returns -EAGAIN if it had to drop the path, or zero if everything was in
2016 * cache
2017 */
2018static noinline int reada_for_balance(struct btrfs_root *root,
2019 struct btrfs_path *path, int level)
2020{
2021 int slot;
2022 int nritems;
2023 struct extent_buffer *parent;
2024 struct extent_buffer *eb;
2025 u64 gen;
2026 u64 block1 = 0;
2027 u64 block2 = 0;
2028 int ret = 0;
2029 int blocksize;
2030
Chris Mason8c594ea2009-04-20 15:50:10 -04002031 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002032 if (!parent)
2033 return 0;
2034
2035 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002036 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002037 blocksize = btrfs_level_size(root, level);
2038
2039 if (slot > 0) {
2040 block1 = btrfs_node_blockptr(parent, slot - 1);
2041 gen = btrfs_node_ptr_generation(parent, slot - 1);
2042 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002043 /*
2044 * if we get -eagain from btrfs_buffer_uptodate, we
2045 * don't want to return eagain here. That will loop
2046 * forever
2047 */
2048 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002049 block1 = 0;
2050 free_extent_buffer(eb);
2051 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002052 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002053 block2 = btrfs_node_blockptr(parent, slot + 1);
2054 gen = btrfs_node_ptr_generation(parent, slot + 1);
2055 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002056 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002057 block2 = 0;
2058 free_extent_buffer(eb);
2059 }
2060 if (block1 || block2) {
2061 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002062
2063 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002064 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002065
2066 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002067 if (block1)
2068 readahead_tree_block(root, block1, blocksize, 0);
2069 if (block2)
2070 readahead_tree_block(root, block2, blocksize, 0);
2071
2072 if (block1) {
2073 eb = read_tree_block(root, block1, blocksize, 0);
2074 free_extent_buffer(eb);
2075 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002076 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002077 eb = read_tree_block(root, block2, blocksize, 0);
2078 free_extent_buffer(eb);
2079 }
2080 }
2081 return ret;
2082}
2083
2084
2085/*
Chris Masond3977122009-01-05 21:25:51 -05002086 * when we walk down the tree, it is usually safe to unlock the higher layers
2087 * in the tree. The exceptions are when our path goes through slot 0, because
2088 * operations on the tree might require changing key pointers higher up in the
2089 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002090 *
Chris Masond3977122009-01-05 21:25:51 -05002091 * callers might also have set path->keep_locks, which tells this code to keep
2092 * the lock if the path points to the last slot in the block. This is part of
2093 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002094 *
Chris Masond3977122009-01-05 21:25:51 -05002095 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2096 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002097 */
Chris Masone02119d2008-09-05 16:13:11 -04002098static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002099 int lowest_unlock, int min_write_lock_level,
2100 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002101{
2102 int i;
2103 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002104 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002105 struct extent_buffer *t;
2106
2107 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2108 if (!path->nodes[i])
2109 break;
2110 if (!path->locks[i])
2111 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002112 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002113 skip_level = i + 1;
2114 continue;
2115 }
Chris Mason051e1b92008-06-25 16:01:30 -04002116 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002117 u32 nritems;
2118 t = path->nodes[i];
2119 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002120 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002121 skip_level = i + 1;
2122 continue;
2123 }
2124 }
Chris Mason051e1b92008-06-25 16:01:30 -04002125 if (skip_level < i && i >= lowest_unlock)
2126 no_skips = 1;
2127
Chris Mason925baed2008-06-25 16:01:30 -04002128 t = path->nodes[i];
2129 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002130 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002131 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002132 if (write_lock_level &&
2133 i > min_write_lock_level &&
2134 i <= *write_lock_level) {
2135 *write_lock_level = i - 1;
2136 }
Chris Mason925baed2008-06-25 16:01:30 -04002137 }
2138 }
2139}
2140
Chris Mason3c69fae2007-08-07 15:52:22 -04002141/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002142 * This releases any locks held in the path starting at level and
2143 * going all the way up to the root.
2144 *
2145 * btrfs_search_slot will keep the lock held on higher nodes in a few
2146 * corner cases, such as COW of the block at slot zero in the node. This
2147 * ignores those rules, and it should only be called when there are no
2148 * more updates to be done higher up in the tree.
2149 */
2150noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2151{
2152 int i;
2153
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002154 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002155 return;
2156
2157 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2158 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002159 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002160 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002161 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002162 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002163 path->locks[i] = 0;
2164 }
2165}
2166
2167/*
Chris Masonc8c42862009-04-03 10:14:18 -04002168 * helper function for btrfs_search_slot. The goal is to find a block
2169 * in cache without setting the path to blocking. If we find the block
2170 * we return zero and the path is unchanged.
2171 *
2172 * If we can't find the block, we set the path blocking and do some
2173 * reada. -EAGAIN is returned and the search must be repeated.
2174 */
2175static int
2176read_block_for_search(struct btrfs_trans_handle *trans,
2177 struct btrfs_root *root, struct btrfs_path *p,
2178 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002179 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002180{
2181 u64 blocknr;
2182 u64 gen;
2183 u32 blocksize;
2184 struct extent_buffer *b = *eb_ret;
2185 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002186 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002187
2188 blocknr = btrfs_node_blockptr(b, slot);
2189 gen = btrfs_node_ptr_generation(b, slot);
2190 blocksize = btrfs_level_size(root, level - 1);
2191
2192 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002193 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002194 /* first we do an atomic uptodate check */
2195 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2196 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002197 /*
2198 * we found an up to date block without
2199 * sleeping, return
2200 * right away
2201 */
2202 *eb_ret = tmp;
2203 return 0;
2204 }
2205 /* the pages were up to date, but we failed
2206 * the generation number check. Do a full
2207 * read for the generation number that is correct.
2208 * We must do this without dropping locks so
2209 * we can trust our generation number
2210 */
2211 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002212 btrfs_set_path_blocking(p);
2213
Chris Masonb9fab912012-05-06 07:23:47 -04002214 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002215 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002216 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002217 *eb_ret = tmp;
2218 return 0;
2219 }
2220 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002221 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002222 return -EIO;
2223 }
Chris Masonc8c42862009-04-03 10:14:18 -04002224 }
2225
2226 /*
2227 * reduce lock contention at high levels
2228 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002229 * we read. Don't release the lock on the current
2230 * level because we need to walk this node to figure
2231 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002232 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002233 btrfs_unlock_up_safe(p, level + 1);
2234 btrfs_set_path_blocking(p);
2235
Chris Masoncb449212010-10-24 11:01:27 -04002236 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002237 if (p->reada)
2238 reada_for_search(root, p, level, slot, key->objectid);
2239
David Sterbab3b4aa72011-04-21 01:20:15 +02002240 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002241
2242 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002243 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002244 if (tmp) {
2245 /*
2246 * If the read above didn't mark this buffer up to date,
2247 * it will never end up being up to date. Set ret to EIO now
2248 * and give up so that our caller doesn't loop forever
2249 * on our EAGAINs.
2250 */
Chris Masonb9fab912012-05-06 07:23:47 -04002251 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002252 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002253 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002254 }
2255 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002256}
2257
2258/*
2259 * helper function for btrfs_search_slot. This does all of the checks
2260 * for node-level blocks and does any balancing required based on
2261 * the ins_len.
2262 *
2263 * If no extra work was required, zero is returned. If we had to
2264 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2265 * start over
2266 */
2267static int
2268setup_nodes_for_search(struct btrfs_trans_handle *trans,
2269 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002270 struct extent_buffer *b, int level, int ins_len,
2271 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002272{
2273 int ret;
2274 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2275 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2276 int sret;
2277
Chris Masonbd681512011-07-16 15:23:14 -04002278 if (*write_lock_level < level + 1) {
2279 *write_lock_level = level + 1;
2280 btrfs_release_path(p);
2281 goto again;
2282 }
2283
Chris Masonc8c42862009-04-03 10:14:18 -04002284 sret = reada_for_balance(root, p, level);
2285 if (sret)
2286 goto again;
2287
2288 btrfs_set_path_blocking(p);
2289 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002290 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002291
2292 BUG_ON(sret > 0);
2293 if (sret) {
2294 ret = sret;
2295 goto done;
2296 }
2297 b = p->nodes[level];
2298 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002299 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002300 int sret;
2301
Chris Masonbd681512011-07-16 15:23:14 -04002302 if (*write_lock_level < level + 1) {
2303 *write_lock_level = level + 1;
2304 btrfs_release_path(p);
2305 goto again;
2306 }
2307
Chris Masonc8c42862009-04-03 10:14:18 -04002308 sret = reada_for_balance(root, p, level);
2309 if (sret)
2310 goto again;
2311
2312 btrfs_set_path_blocking(p);
2313 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002314 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002315
2316 if (sret) {
2317 ret = sret;
2318 goto done;
2319 }
2320 b = p->nodes[level];
2321 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002322 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002323 goto again;
2324 }
2325 BUG_ON(btrfs_header_nritems(b) == 1);
2326 }
2327 return 0;
2328
2329again:
2330 ret = -EAGAIN;
2331done:
2332 return ret;
2333}
2334
2335/*
Chris Mason74123bd2007-02-02 11:05:29 -05002336 * look for key in the tree. path is filled in with nodes along the way
2337 * if key is found, we return zero and you can find the item in the leaf
2338 * level of the path (level 0)
2339 *
2340 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002341 * be inserted, and 1 is returned. If there are other errors during the
2342 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002343 *
2344 * if ins_len > 0, nodes and leaves will be split as we walk down the
2345 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2346 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002347 */
Chris Masone089f052007-03-16 16:20:31 -04002348int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2349 *root, struct btrfs_key *key, struct btrfs_path *p, int
2350 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002351{
Chris Mason5f39d392007-10-15 16:14:19 -04002352 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002353 int slot;
2354 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002355 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002356 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002357 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002358 int root_lock;
2359 /* everything at write_lock_level or lower must be write locked */
2360 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002361 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002362 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002363
Chris Mason6702ed42007-08-07 16:15:09 -04002364 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002365 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002366 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002367
Chris Masonbd681512011-07-16 15:23:14 -04002368 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002369 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002370
Chris Masonbd681512011-07-16 15:23:14 -04002371 /* when we are removing items, we might have to go up to level
2372 * two as we update tree pointers Make sure we keep write
2373 * for those levels as well
2374 */
2375 write_lock_level = 2;
2376 } else if (ins_len > 0) {
2377 /*
2378 * for inserting items, make sure we have a write lock on
2379 * level 1 so we can update keys
2380 */
2381 write_lock_level = 1;
2382 }
2383
2384 if (!cow)
2385 write_lock_level = -1;
2386
2387 if (cow && (p->keep_locks || p->lowest_level))
2388 write_lock_level = BTRFS_MAX_LEVEL;
2389
Chris Masonf7c79f32012-03-19 15:54:38 -04002390 min_write_lock_level = write_lock_level;
2391
Chris Masonbb803952007-03-01 12:04:21 -05002392again:
Chris Masonbd681512011-07-16 15:23:14 -04002393 /*
2394 * we try very hard to do read locks on the root
2395 */
2396 root_lock = BTRFS_READ_LOCK;
2397 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002398 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002399 /*
2400 * the commit roots are read only
2401 * so we always do read locks
2402 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002403 b = root->commit_root;
2404 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002405 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002406 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002407 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002408 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002409 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002410 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002411 level = btrfs_header_level(b);
2412 } else {
2413 /* we don't know the level of the root node
2414 * until we actually have it read locked
2415 */
2416 b = btrfs_read_lock_root_node(root);
2417 level = btrfs_header_level(b);
2418 if (level <= write_lock_level) {
2419 /* whoops, must trade for write lock */
2420 btrfs_tree_read_unlock(b);
2421 free_extent_buffer(b);
2422 b = btrfs_lock_root_node(root);
2423 root_lock = BTRFS_WRITE_LOCK;
2424
2425 /* the level might have changed, check again */
2426 level = btrfs_header_level(b);
2427 }
2428 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002429 }
Chris Masonbd681512011-07-16 15:23:14 -04002430 p->nodes[level] = b;
2431 if (!p->skip_locking)
2432 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002433
Chris Masoneb60cea2007-02-02 09:18:22 -05002434 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002435 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002436
2437 /*
2438 * setup the path here so we can release it under lock
2439 * contention with the cow code
2440 */
Chris Mason02217ed2007-03-02 16:08:05 -05002441 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002442 /*
2443 * if we don't really need to cow this block
2444 * then we don't want to set the path blocking,
2445 * so we test it here
2446 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002447 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002448 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002449
Chris Masonb4ce94d2009-02-04 09:25:08 -05002450 btrfs_set_path_blocking(p);
2451
Chris Masonbd681512011-07-16 15:23:14 -04002452 /*
2453 * must have write locks on this node and the
2454 * parent
2455 */
2456 if (level + 1 > write_lock_level) {
2457 write_lock_level = level + 1;
2458 btrfs_release_path(p);
2459 goto again;
2460 }
2461
Yan Zheng33c66f42009-07-22 09:59:00 -04002462 err = btrfs_cow_block(trans, root, b,
2463 p->nodes[level + 1],
2464 p->slots[level + 1], &b);
2465 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002466 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002467 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002468 }
Chris Mason02217ed2007-03-02 16:08:05 -05002469 }
Chris Mason65b51a02008-08-01 15:11:20 -04002470cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002471 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002472
Chris Masoneb60cea2007-02-02 09:18:22 -05002473 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002474 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002475
2476 /*
2477 * we have a lock on b and as long as we aren't changing
2478 * the tree, there is no way to for the items in b to change.
2479 * It is safe to drop the lock on our parent before we
2480 * go through the expensive btree search on b.
2481 *
2482 * If cow is true, then we might be changing slot zero,
2483 * which may require changing the parent. So, we can't
2484 * drop the lock until after we know which slot we're
2485 * operating on.
2486 */
2487 if (!cow)
2488 btrfs_unlock_up_safe(p, level + 1);
2489
Chris Mason5f39d392007-10-15 16:14:19 -04002490 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002491
Chris Mason5f39d392007-10-15 16:14:19 -04002492 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002493 int dec = 0;
2494 if (ret && slot > 0) {
2495 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002496 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002497 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002498 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002499 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002500 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002501 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002502 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002503 if (err) {
2504 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002505 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002506 }
Chris Masonc8c42862009-04-03 10:14:18 -04002507 b = p->nodes[level];
2508 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002509
Chris Masonbd681512011-07-16 15:23:14 -04002510 /*
2511 * slot 0 is special, if we change the key
2512 * we have to update the parent pointer
2513 * which means we must have a write lock
2514 * on the parent
2515 */
2516 if (slot == 0 && cow &&
2517 write_lock_level < level + 1) {
2518 write_lock_level = level + 1;
2519 btrfs_release_path(p);
2520 goto again;
2521 }
2522
Chris Masonf7c79f32012-03-19 15:54:38 -04002523 unlock_up(p, level, lowest_unlock,
2524 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002525
Chris Mason925baed2008-06-25 16:01:30 -04002526 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002527 if (dec)
2528 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002529 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002530 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002531
Yan Zheng33c66f42009-07-22 09:59:00 -04002532 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002533 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002534 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002535 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002536 if (err) {
2537 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002538 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002539 }
Chris Mason76a05b32009-05-14 13:24:30 -04002540
Chris Masonb4ce94d2009-02-04 09:25:08 -05002541 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002542 level = btrfs_header_level(b);
2543 if (level <= write_lock_level) {
2544 err = btrfs_try_tree_write_lock(b);
2545 if (!err) {
2546 btrfs_set_path_blocking(p);
2547 btrfs_tree_lock(b);
2548 btrfs_clear_path_blocking(p, b,
2549 BTRFS_WRITE_LOCK);
2550 }
2551 p->locks[level] = BTRFS_WRITE_LOCK;
2552 } else {
2553 err = btrfs_try_tree_read_lock(b);
2554 if (!err) {
2555 btrfs_set_path_blocking(p);
2556 btrfs_tree_read_lock(b);
2557 btrfs_clear_path_blocking(p, b,
2558 BTRFS_READ_LOCK);
2559 }
2560 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002561 }
Chris Masonbd681512011-07-16 15:23:14 -04002562 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002563 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002564 } else {
2565 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002566 if (ins_len > 0 &&
2567 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002568 if (write_lock_level < 1) {
2569 write_lock_level = 1;
2570 btrfs_release_path(p);
2571 goto again;
2572 }
2573
Chris Masonb4ce94d2009-02-04 09:25:08 -05002574 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002575 err = split_leaf(trans, root, key,
2576 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002577 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002578
Yan Zheng33c66f42009-07-22 09:59:00 -04002579 BUG_ON(err > 0);
2580 if (err) {
2581 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002582 goto done;
2583 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002584 }
Chris Mason459931e2008-12-10 09:10:46 -05002585 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002586 unlock_up(p, level, lowest_unlock,
2587 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002588 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002589 }
2590 }
Chris Mason65b51a02008-08-01 15:11:20 -04002591 ret = 1;
2592done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002593 /*
2594 * we don't really know what they plan on doing with the path
2595 * from here on, so for now just mark it as blocking
2596 */
Chris Masonb9473432009-03-13 11:00:37 -04002597 if (!p->leave_spinning)
2598 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002599 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002600 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002601 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002602}
2603
Chris Mason74123bd2007-02-02 11:05:29 -05002604/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002605 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2606 * current state of the tree together with the operations recorded in the tree
2607 * modification log to search for the key in a previous version of this tree, as
2608 * denoted by the time_seq parameter.
2609 *
2610 * Naturally, there is no support for insert, delete or cow operations.
2611 *
2612 * The resulting path and return value will be set up as if we called
2613 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2614 */
2615int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2616 struct btrfs_path *p, u64 time_seq)
2617{
2618 struct extent_buffer *b;
2619 int slot;
2620 int ret;
2621 int err;
2622 int level;
2623 int lowest_unlock = 1;
2624 u8 lowest_level = 0;
2625
2626 lowest_level = p->lowest_level;
2627 WARN_ON(p->nodes[0] != NULL);
2628
2629 if (p->search_commit_root) {
2630 BUG_ON(time_seq);
2631 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2632 }
2633
2634again:
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002635 b = get_old_root(root, time_seq);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002636 level = btrfs_header_level(b);
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002637 p->locks[level] = BTRFS_READ_LOCK;
2638
2639 while (b) {
2640 level = btrfs_header_level(b);
2641 p->nodes[level] = b;
2642 btrfs_clear_path_blocking(p, NULL, 0);
2643
2644 /*
2645 * we have a lock on b and as long as we aren't changing
2646 * the tree, there is no way to for the items in b to change.
2647 * It is safe to drop the lock on our parent before we
2648 * go through the expensive btree search on b.
2649 */
2650 btrfs_unlock_up_safe(p, level + 1);
2651
2652 ret = bin_search(b, key, level, &slot);
2653
2654 if (level != 0) {
2655 int dec = 0;
2656 if (ret && slot > 0) {
2657 dec = 1;
2658 slot -= 1;
2659 }
2660 p->slots[level] = slot;
2661 unlock_up(p, level, lowest_unlock, 0, NULL);
2662
2663 if (level == lowest_level) {
2664 if (dec)
2665 p->slots[level]++;
2666 goto done;
2667 }
2668
2669 err = read_block_for_search(NULL, root, p, &b, level,
2670 slot, key, time_seq);
2671 if (err == -EAGAIN)
2672 goto again;
2673 if (err) {
2674 ret = err;
2675 goto done;
2676 }
2677
2678 level = btrfs_header_level(b);
2679 err = btrfs_try_tree_read_lock(b);
2680 if (!err) {
2681 btrfs_set_path_blocking(p);
2682 btrfs_tree_read_lock(b);
2683 btrfs_clear_path_blocking(p, b,
2684 BTRFS_READ_LOCK);
2685 }
2686 p->locks[level] = BTRFS_READ_LOCK;
2687 p->nodes[level] = b;
2688 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2689 if (b != p->nodes[level]) {
2690 btrfs_tree_unlock_rw(p->nodes[level],
2691 p->locks[level]);
2692 p->locks[level] = 0;
2693 p->nodes[level] = b;
2694 }
2695 } else {
2696 p->slots[level] = slot;
2697 unlock_up(p, level, lowest_unlock, 0, NULL);
2698 goto done;
2699 }
2700 }
2701 ret = 1;
2702done:
2703 if (!p->leave_spinning)
2704 btrfs_set_path_blocking(p);
2705 if (ret < 0)
2706 btrfs_release_path(p);
2707
2708 return ret;
2709}
2710
2711/*
Chris Mason74123bd2007-02-02 11:05:29 -05002712 * adjust the pointers going up the tree, starting at level
2713 * making sure the right key of each node is points to 'key'.
2714 * This is used after shifting pointers to the left, so it stops
2715 * fixing up pointers when a given leaf/node is not in slot 0 of the
2716 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002717 *
Chris Mason74123bd2007-02-02 11:05:29 -05002718 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002719static void fixup_low_keys(struct btrfs_trans_handle *trans,
2720 struct btrfs_root *root, struct btrfs_path *path,
2721 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002722{
2723 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002724 struct extent_buffer *t;
2725
Chris Mason234b63a2007-03-13 10:46:10 -04002726 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002727 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002728 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002729 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002730 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002731 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002732 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002733 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002734 if (tslot != 0)
2735 break;
2736 }
2737}
2738
Chris Mason74123bd2007-02-02 11:05:29 -05002739/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002740 * update item key.
2741 *
2742 * This function isn't completely safe. It's the caller's responsibility
2743 * that the new key won't break the order
2744 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002745void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2746 struct btrfs_root *root, struct btrfs_path *path,
2747 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002748{
2749 struct btrfs_disk_key disk_key;
2750 struct extent_buffer *eb;
2751 int slot;
2752
2753 eb = path->nodes[0];
2754 slot = path->slots[0];
2755 if (slot > 0) {
2756 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002757 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002758 }
2759 if (slot < btrfs_header_nritems(eb) - 1) {
2760 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002761 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002762 }
2763
2764 btrfs_cpu_key_to_disk(&disk_key, new_key);
2765 btrfs_set_item_key(eb, &disk_key, slot);
2766 btrfs_mark_buffer_dirty(eb);
2767 if (slot == 0)
2768 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002769}
2770
2771/*
Chris Mason74123bd2007-02-02 11:05:29 -05002772 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002773 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002774 *
2775 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2776 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002777 */
Chris Mason98ed5172008-01-03 10:01:48 -05002778static int push_node_left(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002780 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002781{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002782 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002783 int src_nritems;
2784 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002785 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002786
Chris Mason5f39d392007-10-15 16:14:19 -04002787 src_nritems = btrfs_header_nritems(src);
2788 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002789 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002790 WARN_ON(btrfs_header_generation(src) != trans->transid);
2791 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002792
Chris Masonbce4eae2008-04-24 14:42:46 -04002793 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002794 return 1;
2795
Chris Masond3977122009-01-05 21:25:51 -05002796 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002797 return 1;
2798
Chris Masonbce4eae2008-04-24 14:42:46 -04002799 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002800 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002801 if (push_items < src_nritems) {
2802 /* leave at least 8 pointers in the node if
2803 * we aren't going to empty it
2804 */
2805 if (src_nritems - push_items < 8) {
2806 if (push_items <= 8)
2807 return 1;
2808 push_items -= 8;
2809 }
2810 }
2811 } else
2812 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002813
Jan Schmidtf2304752012-05-26 11:43:17 +02002814 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2815 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002816 copy_extent_buffer(dst, src,
2817 btrfs_node_key_ptr_offset(dst_nritems),
2818 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002819 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002820
Chris Masonbb803952007-03-01 12:04:21 -05002821 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002822 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2823 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002824 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2825 btrfs_node_key_ptr_offset(push_items),
2826 (src_nritems - push_items) *
2827 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002828 }
Chris Mason5f39d392007-10-15 16:14:19 -04002829 btrfs_set_header_nritems(src, src_nritems - push_items);
2830 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2831 btrfs_mark_buffer_dirty(src);
2832 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002833
Chris Masonbb803952007-03-01 12:04:21 -05002834 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002835}
2836
Chris Mason97571fd2007-02-24 13:39:08 -05002837/*
Chris Mason79f95c82007-03-01 15:16:26 -05002838 * try to push data from one node into the next node right in the
2839 * tree.
2840 *
2841 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2842 * error, and > 0 if there was no room in the right hand block.
2843 *
2844 * this will only push up to 1/2 the contents of the left node over
2845 */
Chris Mason5f39d392007-10-15 16:14:19 -04002846static int balance_node_right(struct btrfs_trans_handle *trans,
2847 struct btrfs_root *root,
2848 struct extent_buffer *dst,
2849 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002850{
Chris Mason79f95c82007-03-01 15:16:26 -05002851 int push_items = 0;
2852 int max_push;
2853 int src_nritems;
2854 int dst_nritems;
2855 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002856
Chris Mason7bb86312007-12-11 09:25:06 -05002857 WARN_ON(btrfs_header_generation(src) != trans->transid);
2858 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2859
Chris Mason5f39d392007-10-15 16:14:19 -04002860 src_nritems = btrfs_header_nritems(src);
2861 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002862 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002863 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002864 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002865
Chris Masond3977122009-01-05 21:25:51 -05002866 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002867 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002868
2869 max_push = src_nritems / 2 + 1;
2870 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002871 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002872 return 1;
Yan252c38f2007-08-29 09:11:44 -04002873
Chris Mason79f95c82007-03-01 15:16:26 -05002874 if (max_push < push_items)
2875 push_items = max_push;
2876
Jan Schmidtf2304752012-05-26 11:43:17 +02002877 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002878 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2879 btrfs_node_key_ptr_offset(0),
2880 (dst_nritems) *
2881 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002882
Jan Schmidtf2304752012-05-26 11:43:17 +02002883 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2884 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002885 copy_extent_buffer(dst, src,
2886 btrfs_node_key_ptr_offset(0),
2887 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002888 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002889
Chris Mason5f39d392007-10-15 16:14:19 -04002890 btrfs_set_header_nritems(src, src_nritems - push_items);
2891 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002892
Chris Mason5f39d392007-10-15 16:14:19 -04002893 btrfs_mark_buffer_dirty(src);
2894 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002895
Chris Mason79f95c82007-03-01 15:16:26 -05002896 return ret;
2897}
2898
2899/*
Chris Mason97571fd2007-02-24 13:39:08 -05002900 * helper function to insert a new root level in the tree.
2901 * A new node is allocated, and a single item is inserted to
2902 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002903 *
2904 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002905 */
Chris Masond3977122009-01-05 21:25:51 -05002906static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002907 struct btrfs_root *root,
2908 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002909{
Chris Mason7bb86312007-12-11 09:25:06 -05002910 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002911 struct extent_buffer *lower;
2912 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002913 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002914 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002915
2916 BUG_ON(path->nodes[level]);
2917 BUG_ON(path->nodes[level-1] != root->node);
2918
Chris Mason7bb86312007-12-11 09:25:06 -05002919 lower = path->nodes[level-1];
2920 if (level == 1)
2921 btrfs_item_key(lower, &lower_key, 0);
2922 else
2923 btrfs_node_key(lower, &lower_key, 0);
2924
Zheng Yan31840ae2008-09-23 13:14:14 -04002925 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002926 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002927 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002928 if (IS_ERR(c))
2929 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002930
Yan, Zhengf0486c62010-05-16 10:46:25 -04002931 root_add_used(root, root->nodesize);
2932
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002933 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002934 btrfs_set_header_nritems(c, 1);
2935 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002936 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002937 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002938 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002939 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002940
Chris Mason5f39d392007-10-15 16:14:19 -04002941 write_extent_buffer(c, root->fs_info->fsid,
2942 (unsigned long)btrfs_header_fsid(c),
2943 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002944
2945 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2946 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2947 BTRFS_UUID_SIZE);
2948
Chris Mason5f39d392007-10-15 16:14:19 -04002949 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002950 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002951 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002952 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002953
2954 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002955
2956 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002957
Chris Mason925baed2008-06-25 16:01:30 -04002958 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002959 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002960 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002961
2962 /* the super has an extra ref to root->node */
2963 free_extent_buffer(old);
2964
Chris Mason0b86a832008-03-24 15:01:56 -04002965 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002966 extent_buffer_get(c);
2967 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002968 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002969 path->slots[level] = 0;
2970 return 0;
2971}
2972
Chris Mason74123bd2007-02-02 11:05:29 -05002973/*
2974 * worker function to insert a single pointer in a node.
2975 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002976 *
Chris Mason74123bd2007-02-02 11:05:29 -05002977 * slot and level indicate where you want the key to go, and
2978 * blocknr is the block the key points to.
2979 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002980static void insert_ptr(struct btrfs_trans_handle *trans,
2981 struct btrfs_root *root, struct btrfs_path *path,
2982 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002983 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002984{
Chris Mason5f39d392007-10-15 16:14:19 -04002985 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002986 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002987 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002988
2989 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002990 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002991 lower = path->nodes[level];
2992 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002993 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002994 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002995 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002996 if (tree_mod_log && level)
2997 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2998 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002999 memmove_extent_buffer(lower,
3000 btrfs_node_key_ptr_offset(slot + 1),
3001 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04003002 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05003003 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003004 if (tree_mod_log && level) {
3005 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
3006 MOD_LOG_KEY_ADD);
3007 BUG_ON(ret < 0);
3008 }
Chris Mason5f39d392007-10-15 16:14:19 -04003009 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04003010 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05003011 WARN_ON(trans->transid == 0);
3012 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003013 btrfs_set_header_nritems(lower, nritems + 1);
3014 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05003015}
3016
Chris Mason97571fd2007-02-24 13:39:08 -05003017/*
3018 * split the node at the specified level in path in two.
3019 * The path is corrected to point to the appropriate node after the split
3020 *
3021 * Before splitting this tries to make some room in the node by pushing
3022 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003023 *
3024 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003025 */
Chris Masone02119d2008-09-05 16:13:11 -04003026static noinline int split_node(struct btrfs_trans_handle *trans,
3027 struct btrfs_root *root,
3028 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003029{
Chris Mason5f39d392007-10-15 16:14:19 -04003030 struct extent_buffer *c;
3031 struct extent_buffer *split;
3032 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003033 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003034 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003035 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003036
Chris Mason5f39d392007-10-15 16:14:19 -04003037 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003038 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003039 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003040 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003041 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003042 if (ret)
3043 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003044 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003045 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003046 c = path->nodes[level];
3047 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003048 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003049 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003050 if (ret < 0)
3051 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003052 }
Chris Masone66f7092007-04-20 13:16:02 -04003053
Chris Mason5f39d392007-10-15 16:14:19 -04003054 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003055 mid = (c_nritems + 1) / 2;
3056 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003059 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003060 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003061 if (IS_ERR(split))
3062 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003063
Yan, Zhengf0486c62010-05-16 10:46:25 -04003064 root_add_used(root, root->nodesize);
3065
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003066 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003067 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003068 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003069 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003070 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003071 btrfs_set_header_owner(split, root->root_key.objectid);
3072 write_extent_buffer(split, root->fs_info->fsid,
3073 (unsigned long)btrfs_header_fsid(split),
3074 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003075 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3076 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3077 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003078
Jan Schmidtf2304752012-05-26 11:43:17 +02003079 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003080 copy_extent_buffer(split, c,
3081 btrfs_node_key_ptr_offset(0),
3082 btrfs_node_key_ptr_offset(mid),
3083 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3084 btrfs_set_header_nritems(split, c_nritems - mid);
3085 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003086 ret = 0;
3087
Chris Mason5f39d392007-10-15 16:14:19 -04003088 btrfs_mark_buffer_dirty(c);
3089 btrfs_mark_buffer_dirty(split);
3090
Jeff Mahoney143bede2012-03-01 14:56:26 +01003091 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003092 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003093
Chris Mason5de08d72007-02-24 06:24:44 -05003094 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003095 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003096 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003097 free_extent_buffer(c);
3098 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003099 path->slots[level + 1] += 1;
3100 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003101 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003102 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003103 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003104 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003105}
3106
Chris Mason74123bd2007-02-02 11:05:29 -05003107/*
3108 * how many bytes are required to store the items in a leaf. start
3109 * and nr indicate which items in the leaf to check. This totals up the
3110 * space used both by the item structs and the item data
3111 */
Chris Mason5f39d392007-10-15 16:14:19 -04003112static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003113{
3114 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003115 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003116 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003117
3118 if (!nr)
3119 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003120 data_len = btrfs_item_end_nr(l, start);
3121 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003122 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003123 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003124 return data_len;
3125}
3126
Chris Mason74123bd2007-02-02 11:05:29 -05003127/*
Chris Masond4dbff92007-04-04 14:08:15 -04003128 * The space between the end of the leaf items and
3129 * the start of the leaf data. IOW, how much room
3130 * the leaf has left for both items and data
3131 */
Chris Masond3977122009-01-05 21:25:51 -05003132noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003133 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003134{
Chris Mason5f39d392007-10-15 16:14:19 -04003135 int nritems = btrfs_header_nritems(leaf);
3136 int ret;
3137 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3138 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003139 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3140 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003141 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003142 leaf_space_used(leaf, 0, nritems), nritems);
3143 }
3144 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003145}
3146
Chris Mason99d8f832010-07-07 10:51:48 -04003147/*
3148 * min slot controls the lowest index we're willing to push to the
3149 * right. We'll push up to and including min_slot, but no lower
3150 */
Chris Mason44871b12009-03-13 10:04:31 -04003151static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3152 struct btrfs_root *root,
3153 struct btrfs_path *path,
3154 int data_size, int empty,
3155 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003156 int free_space, u32 left_nritems,
3157 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003158{
Chris Mason5f39d392007-10-15 16:14:19 -04003159 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003160 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003161 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003162 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003163 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003164 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003165 int push_space = 0;
3166 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003167 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003168 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003169 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003170 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003171 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003172
Chris Masoncfed81a2012-03-03 07:40:03 -05003173 btrfs_init_map_token(&token);
3174
Chris Mason34a38212007-11-07 13:31:03 -05003175 if (empty)
3176 nr = 0;
3177 else
Chris Mason99d8f832010-07-07 10:51:48 -04003178 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003179
Zheng Yan31840ae2008-09-23 13:14:14 -04003180 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003181 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003182
Chris Mason44871b12009-03-13 10:04:31 -04003183 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003184 i = left_nritems - 1;
3185 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003186 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003187
Zheng Yan31840ae2008-09-23 13:14:14 -04003188 if (!empty && push_items > 0) {
3189 if (path->slots[0] > i)
3190 break;
3191 if (path->slots[0] == i) {
3192 int space = btrfs_leaf_free_space(root, left);
3193 if (space + push_space * 2 > free_space)
3194 break;
3195 }
3196 }
3197
Chris Mason00ec4c52007-02-24 12:47:20 -05003198 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003199 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003200
Chris Masondb945352007-10-15 16:15:53 -04003201 this_item_size = btrfs_item_size(left, item);
3202 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003203 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003204
Chris Mason00ec4c52007-02-24 12:47:20 -05003205 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003206 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003207 if (i == 0)
3208 break;
3209 i--;
Chris Masondb945352007-10-15 16:15:53 -04003210 }
Chris Mason5f39d392007-10-15 16:14:19 -04003211
Chris Mason925baed2008-06-25 16:01:30 -04003212 if (push_items == 0)
3213 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003214
Chris Mason34a38212007-11-07 13:31:03 -05003215 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003216 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003217
Chris Mason00ec4c52007-02-24 12:47:20 -05003218 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003219 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003220
Chris Mason5f39d392007-10-15 16:14:19 -04003221 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003222 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003223
Chris Mason00ec4c52007-02-24 12:47:20 -05003224 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003225 data_end = leaf_data_end(root, right);
3226 memmove_extent_buffer(right,
3227 btrfs_leaf_data(right) + data_end - push_space,
3228 btrfs_leaf_data(right) + data_end,
3229 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3230
Chris Mason00ec4c52007-02-24 12:47:20 -05003231 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003232 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003233 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3234 btrfs_leaf_data(left) + leaf_data_end(root, left),
3235 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003236
3237 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3238 btrfs_item_nr_offset(0),
3239 right_nritems * sizeof(struct btrfs_item));
3240
Chris Mason00ec4c52007-02-24 12:47:20 -05003241 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003242 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3243 btrfs_item_nr_offset(left_nritems - push_items),
3244 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003245
3246 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003247 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003248 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003249 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003250 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003251 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003252 push_space -= btrfs_token_item_size(right, item, &token);
3253 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003254 }
3255
Chris Mason7518a232007-03-12 12:01:18 -04003256 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003257 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003258
Chris Mason34a38212007-11-07 13:31:03 -05003259 if (left_nritems)
3260 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003261 else
3262 clean_tree_block(trans, root, left);
3263
Chris Mason5f39d392007-10-15 16:14:19 -04003264 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003265
Chris Mason5f39d392007-10-15 16:14:19 -04003266 btrfs_item_key(right, &disk_key, 0);
3267 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003268 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003269
Chris Mason00ec4c52007-02-24 12:47:20 -05003270 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003271 if (path->slots[0] >= left_nritems) {
3272 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003273 if (btrfs_header_nritems(path->nodes[0]) == 0)
3274 clean_tree_block(trans, root, path->nodes[0]);
3275 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003276 free_extent_buffer(path->nodes[0]);
3277 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003278 path->slots[1] += 1;
3279 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003280 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003281 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003282 }
3283 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003284
3285out_unlock:
3286 btrfs_tree_unlock(right);
3287 free_extent_buffer(right);
3288 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003289}
Chris Mason925baed2008-06-25 16:01:30 -04003290
Chris Mason00ec4c52007-02-24 12:47:20 -05003291/*
Chris Mason44871b12009-03-13 10:04:31 -04003292 * push some data in the path leaf to the right, trying to free up at
3293 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3294 *
3295 * returns 1 if the push failed because the other node didn't have enough
3296 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003297 *
3298 * this will push starting from min_slot to the end of the leaf. It won't
3299 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003300 */
3301static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003302 *root, struct btrfs_path *path,
3303 int min_data_size, int data_size,
3304 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003305{
3306 struct extent_buffer *left = path->nodes[0];
3307 struct extent_buffer *right;
3308 struct extent_buffer *upper;
3309 int slot;
3310 int free_space;
3311 u32 left_nritems;
3312 int ret;
3313
3314 if (!path->nodes[1])
3315 return 1;
3316
3317 slot = path->slots[1];
3318 upper = path->nodes[1];
3319 if (slot >= btrfs_header_nritems(upper) - 1)
3320 return 1;
3321
3322 btrfs_assert_tree_locked(path->nodes[1]);
3323
3324 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003325 if (right == NULL)
3326 return 1;
3327
Chris Mason44871b12009-03-13 10:04:31 -04003328 btrfs_tree_lock(right);
3329 btrfs_set_lock_blocking(right);
3330
3331 free_space = btrfs_leaf_free_space(root, right);
3332 if (free_space < data_size)
3333 goto out_unlock;
3334
3335 /* cow and double check */
3336 ret = btrfs_cow_block(trans, root, right, upper,
3337 slot + 1, &right);
3338 if (ret)
3339 goto out_unlock;
3340
3341 free_space = btrfs_leaf_free_space(root, right);
3342 if (free_space < data_size)
3343 goto out_unlock;
3344
3345 left_nritems = btrfs_header_nritems(left);
3346 if (left_nritems == 0)
3347 goto out_unlock;
3348
Chris Mason99d8f832010-07-07 10:51:48 -04003349 return __push_leaf_right(trans, root, path, min_data_size, empty,
3350 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003351out_unlock:
3352 btrfs_tree_unlock(right);
3353 free_extent_buffer(right);
3354 return 1;
3355}
3356
3357/*
Chris Mason74123bd2007-02-02 11:05:29 -05003358 * push some data in the path leaf to the left, trying to free up at
3359 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003360 *
3361 * max_slot can put a limit on how far into the leaf we'll push items. The
3362 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3363 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003364 */
Chris Mason44871b12009-03-13 10:04:31 -04003365static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3366 struct btrfs_root *root,
3367 struct btrfs_path *path, int data_size,
3368 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003369 int free_space, u32 right_nritems,
3370 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003371{
Chris Mason5f39d392007-10-15 16:14:19 -04003372 struct btrfs_disk_key disk_key;
3373 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003374 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003375 int push_space = 0;
3376 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003377 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003378 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003379 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003380 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003381 u32 this_item_size;
3382 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003383 struct btrfs_map_token token;
3384
3385 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003386
Chris Mason34a38212007-11-07 13:31:03 -05003387 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003388 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003389 else
Chris Mason99d8f832010-07-07 10:51:48 -04003390 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003391
3392 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003393 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003394
Zheng Yan31840ae2008-09-23 13:14:14 -04003395 if (!empty && push_items > 0) {
3396 if (path->slots[0] < i)
3397 break;
3398 if (path->slots[0] == i) {
3399 int space = btrfs_leaf_free_space(root, right);
3400 if (space + push_space * 2 > free_space)
3401 break;
3402 }
3403 }
3404
Chris Masonbe0e5c02007-01-26 15:51:26 -05003405 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003406 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003407
3408 this_item_size = btrfs_item_size(right, item);
3409 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003410 break;
Chris Masondb945352007-10-15 16:15:53 -04003411
Chris Masonbe0e5c02007-01-26 15:51:26 -05003412 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003413 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003414 }
Chris Masondb945352007-10-15 16:15:53 -04003415
Chris Masonbe0e5c02007-01-26 15:51:26 -05003416 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003417 ret = 1;
3418 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003419 }
Chris Mason34a38212007-11-07 13:31:03 -05003420 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003421 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003422
Chris Masonbe0e5c02007-01-26 15:51:26 -05003423 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003424 copy_extent_buffer(left, right,
3425 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3426 btrfs_item_nr_offset(0),
3427 push_items * sizeof(struct btrfs_item));
3428
Chris Mason123abc82007-03-14 14:14:43 -04003429 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003430 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003431
3432 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003433 leaf_data_end(root, left) - push_space,
3434 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003435 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003436 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003437 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003438 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003439
Chris Masondb945352007-10-15 16:15:53 -04003440 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003441 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003442 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003443
Chris Mason5f39d392007-10-15 16:14:19 -04003444 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003445
Chris Masoncfed81a2012-03-03 07:40:03 -05003446 ioff = btrfs_token_item_offset(left, item, &token);
3447 btrfs_set_token_item_offset(left, item,
3448 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3449 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003450 }
Chris Mason5f39d392007-10-15 16:14:19 -04003451 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003452
3453 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003454 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003455 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3456 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003457 WARN_ON(1);
3458 }
Chris Mason5f39d392007-10-15 16:14:19 -04003459
Chris Mason34a38212007-11-07 13:31:03 -05003460 if (push_items < right_nritems) {
3461 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3462 leaf_data_end(root, right);
3463 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3464 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3465 btrfs_leaf_data(right) +
3466 leaf_data_end(root, right), push_space);
3467
3468 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003469 btrfs_item_nr_offset(push_items),
3470 (btrfs_header_nritems(right) - push_items) *
3471 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003472 }
Yaneef1c492007-11-26 10:58:13 -05003473 right_nritems -= push_items;
3474 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003475 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003476 for (i = 0; i < right_nritems; i++) {
3477 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003478
Chris Masoncfed81a2012-03-03 07:40:03 -05003479 push_space = push_space - btrfs_token_item_size(right,
3480 item, &token);
3481 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003482 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003483
Chris Mason5f39d392007-10-15 16:14:19 -04003484 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003485 if (right_nritems)
3486 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003487 else
3488 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003489
Chris Mason5f39d392007-10-15 16:14:19 -04003490 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003491 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003492
3493 /* then fixup the leaf pointer in the path */
3494 if (path->slots[0] < push_items) {
3495 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003496 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003497 free_extent_buffer(path->nodes[0]);
3498 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003499 path->slots[1] -= 1;
3500 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003501 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003502 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003503 path->slots[0] -= push_items;
3504 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003505 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003506 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003507out:
3508 btrfs_tree_unlock(left);
3509 free_extent_buffer(left);
3510 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003511}
3512
Chris Mason74123bd2007-02-02 11:05:29 -05003513/*
Chris Mason44871b12009-03-13 10:04:31 -04003514 * push some data in the path leaf to the left, trying to free up at
3515 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003516 *
3517 * max_slot can put a limit on how far into the leaf we'll push items. The
3518 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3519 * items
Chris Mason44871b12009-03-13 10:04:31 -04003520 */
3521static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003522 *root, struct btrfs_path *path, int min_data_size,
3523 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003524{
3525 struct extent_buffer *right = path->nodes[0];
3526 struct extent_buffer *left;
3527 int slot;
3528 int free_space;
3529 u32 right_nritems;
3530 int ret = 0;
3531
3532 slot = path->slots[1];
3533 if (slot == 0)
3534 return 1;
3535 if (!path->nodes[1])
3536 return 1;
3537
3538 right_nritems = btrfs_header_nritems(right);
3539 if (right_nritems == 0)
3540 return 1;
3541
3542 btrfs_assert_tree_locked(path->nodes[1]);
3543
3544 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003545 if (left == NULL)
3546 return 1;
3547
Chris Mason44871b12009-03-13 10:04:31 -04003548 btrfs_tree_lock(left);
3549 btrfs_set_lock_blocking(left);
3550
3551 free_space = btrfs_leaf_free_space(root, left);
3552 if (free_space < data_size) {
3553 ret = 1;
3554 goto out;
3555 }
3556
3557 /* cow and double check */
3558 ret = btrfs_cow_block(trans, root, left,
3559 path->nodes[1], slot - 1, &left);
3560 if (ret) {
3561 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003562 if (ret == -ENOSPC)
3563 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003564 goto out;
3565 }
3566
3567 free_space = btrfs_leaf_free_space(root, left);
3568 if (free_space < data_size) {
3569 ret = 1;
3570 goto out;
3571 }
3572
Chris Mason99d8f832010-07-07 10:51:48 -04003573 return __push_leaf_left(trans, root, path, min_data_size,
3574 empty, left, free_space, right_nritems,
3575 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003576out:
3577 btrfs_tree_unlock(left);
3578 free_extent_buffer(left);
3579 return ret;
3580}
3581
3582/*
Chris Mason74123bd2007-02-02 11:05:29 -05003583 * split the path's leaf in two, making sure there is at least data_size
3584 * available for the resulting leaf level of the path.
3585 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003586static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3587 struct btrfs_root *root,
3588 struct btrfs_path *path,
3589 struct extent_buffer *l,
3590 struct extent_buffer *right,
3591 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003592{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003593 int data_copy_size;
3594 int rt_data_off;
3595 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003596 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003597 struct btrfs_map_token token;
3598
3599 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003600
Chris Mason5f39d392007-10-15 16:14:19 -04003601 nritems = nritems - mid;
3602 btrfs_set_header_nritems(right, nritems);
3603 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3604
3605 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3606 btrfs_item_nr_offset(mid),
3607 nritems * sizeof(struct btrfs_item));
3608
3609 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003610 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3611 data_copy_size, btrfs_leaf_data(l) +
3612 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003613
Chris Mason5f39d392007-10-15 16:14:19 -04003614 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3615 btrfs_item_end_nr(l, mid);
3616
3617 for (i = 0; i < nritems; i++) {
3618 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003619 u32 ioff;
3620
Chris Masoncfed81a2012-03-03 07:40:03 -05003621 ioff = btrfs_token_item_offset(right, item, &token);
3622 btrfs_set_token_item_offset(right, item,
3623 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003624 }
Chris Mason74123bd2007-02-02 11:05:29 -05003625
Chris Mason5f39d392007-10-15 16:14:19 -04003626 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003627 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003628 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003629 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003630
3631 btrfs_mark_buffer_dirty(right);
3632 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003633 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003634
Chris Masonbe0e5c02007-01-26 15:51:26 -05003635 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003636 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003637 free_extent_buffer(path->nodes[0]);
3638 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003639 path->slots[0] -= mid;
3640 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003641 } else {
3642 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003643 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003644 }
Chris Mason5f39d392007-10-15 16:14:19 -04003645
Chris Masoneb60cea2007-02-02 09:18:22 -05003646 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003647}
3648
3649/*
Chris Mason99d8f832010-07-07 10:51:48 -04003650 * double splits happen when we need to insert a big item in the middle
3651 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3652 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3653 * A B C
3654 *
3655 * We avoid this by trying to push the items on either side of our target
3656 * into the adjacent leaves. If all goes well we can avoid the double split
3657 * completely.
3658 */
3659static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3660 struct btrfs_root *root,
3661 struct btrfs_path *path,
3662 int data_size)
3663{
3664 int ret;
3665 int progress = 0;
3666 int slot;
3667 u32 nritems;
3668
3669 slot = path->slots[0];
3670
3671 /*
3672 * try to push all the items after our slot into the
3673 * right leaf
3674 */
3675 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3676 if (ret < 0)
3677 return ret;
3678
3679 if (ret == 0)
3680 progress++;
3681
3682 nritems = btrfs_header_nritems(path->nodes[0]);
3683 /*
3684 * our goal is to get our slot at the start or end of a leaf. If
3685 * we've done so we're done
3686 */
3687 if (path->slots[0] == 0 || path->slots[0] == nritems)
3688 return 0;
3689
3690 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3691 return 0;
3692
3693 /* try to push all the items before our slot into the next leaf */
3694 slot = path->slots[0];
3695 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3696 if (ret < 0)
3697 return ret;
3698
3699 if (ret == 0)
3700 progress++;
3701
3702 if (progress)
3703 return 0;
3704 return 1;
3705}
3706
3707/*
Chris Mason44871b12009-03-13 10:04:31 -04003708 * split the path's leaf in two, making sure there is at least data_size
3709 * available for the resulting leaf level of the path.
3710 *
3711 * returns 0 if all went well and < 0 on failure.
3712 */
3713static noinline int split_leaf(struct btrfs_trans_handle *trans,
3714 struct btrfs_root *root,
3715 struct btrfs_key *ins_key,
3716 struct btrfs_path *path, int data_size,
3717 int extend)
3718{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003719 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003720 struct extent_buffer *l;
3721 u32 nritems;
3722 int mid;
3723 int slot;
3724 struct extent_buffer *right;
3725 int ret = 0;
3726 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003727 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003728 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003729 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003730
Yan, Zhenga5719522009-09-24 09:17:31 -04003731 l = path->nodes[0];
3732 slot = path->slots[0];
3733 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3734 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3735 return -EOVERFLOW;
3736
Chris Mason44871b12009-03-13 10:04:31 -04003737 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003738 if (data_size) {
3739 wret = push_leaf_right(trans, root, path, data_size,
3740 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003741 if (wret < 0)
3742 return wret;
3743 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003744 wret = push_leaf_left(trans, root, path, data_size,
3745 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003746 if (wret < 0)
3747 return wret;
3748 }
3749 l = path->nodes[0];
3750
3751 /* did the pushes work? */
3752 if (btrfs_leaf_free_space(root, l) >= data_size)
3753 return 0;
3754 }
3755
3756 if (!path->nodes[1]) {
3757 ret = insert_new_root(trans, root, path, 1);
3758 if (ret)
3759 return ret;
3760 }
3761again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003762 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003763 l = path->nodes[0];
3764 slot = path->slots[0];
3765 nritems = btrfs_header_nritems(l);
3766 mid = (nritems + 1) / 2;
3767
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003768 if (mid <= slot) {
3769 if (nritems == 1 ||
3770 leaf_space_used(l, mid, nritems - mid) + data_size >
3771 BTRFS_LEAF_DATA_SIZE(root)) {
3772 if (slot >= nritems) {
3773 split = 0;
3774 } else {
3775 mid = slot;
3776 if (mid != nritems &&
3777 leaf_space_used(l, mid, nritems - mid) +
3778 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003779 if (data_size && !tried_avoid_double)
3780 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003781 split = 2;
3782 }
3783 }
3784 }
3785 } else {
3786 if (leaf_space_used(l, 0, mid) + data_size >
3787 BTRFS_LEAF_DATA_SIZE(root)) {
3788 if (!extend && data_size && slot == 0) {
3789 split = 0;
3790 } else if ((extend || !data_size) && slot == 0) {
3791 mid = 1;
3792 } else {
3793 mid = slot;
3794 if (mid != nritems &&
3795 leaf_space_used(l, mid, nritems - mid) +
3796 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003797 if (data_size && !tried_avoid_double)
3798 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003799 split = 2 ;
3800 }
3801 }
3802 }
3803 }
3804
3805 if (split == 0)
3806 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3807 else
3808 btrfs_item_key(l, &disk_key, mid);
3809
3810 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003811 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003812 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003813 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003814 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003815
3816 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003817
3818 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3819 btrfs_set_header_bytenr(right, right->start);
3820 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003821 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003822 btrfs_set_header_owner(right, root->root_key.objectid);
3823 btrfs_set_header_level(right, 0);
3824 write_extent_buffer(right, root->fs_info->fsid,
3825 (unsigned long)btrfs_header_fsid(right),
3826 BTRFS_FSID_SIZE);
3827
3828 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3829 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3830 BTRFS_UUID_SIZE);
3831
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003832 if (split == 0) {
3833 if (mid <= slot) {
3834 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003835 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003836 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003837 btrfs_tree_unlock(path->nodes[0]);
3838 free_extent_buffer(path->nodes[0]);
3839 path->nodes[0] = right;
3840 path->slots[0] = 0;
3841 path->slots[1] += 1;
3842 } else {
3843 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003844 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003845 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003846 btrfs_tree_unlock(path->nodes[0]);
3847 free_extent_buffer(path->nodes[0]);
3848 path->nodes[0] = right;
3849 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003850 if (path->slots[1] == 0)
3851 fixup_low_keys(trans, root, path,
3852 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003853 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003854 btrfs_mark_buffer_dirty(right);
3855 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003856 }
3857
Jeff Mahoney143bede2012-03-01 14:56:26 +01003858 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003859
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003860 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003861 BUG_ON(num_doubles != 0);
3862 num_doubles++;
3863 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003864 }
Chris Mason44871b12009-03-13 10:04:31 -04003865
Jeff Mahoney143bede2012-03-01 14:56:26 +01003866 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003867
3868push_for_double:
3869 push_for_double_split(trans, root, path, data_size);
3870 tried_avoid_double = 1;
3871 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3872 return 0;
3873 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003874}
3875
Yan, Zhengad48fd752009-11-12 09:33:58 +00003876static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3877 struct btrfs_root *root,
3878 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003879{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003880 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003881 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003882 struct btrfs_file_extent_item *fi;
3883 u64 extent_len = 0;
3884 u32 item_size;
3885 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003886
3887 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003888 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3889
3890 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3891 key.type != BTRFS_EXTENT_CSUM_KEY);
3892
3893 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3894 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003895
3896 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003897 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3898 fi = btrfs_item_ptr(leaf, path->slots[0],
3899 struct btrfs_file_extent_item);
3900 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3901 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003902 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003903
Chris Mason459931e2008-12-10 09:10:46 -05003904 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003905 path->search_for_split = 1;
3906 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003907 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003908 if (ret < 0)
3909 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003910
Yan, Zhengad48fd752009-11-12 09:33:58 +00003911 ret = -EAGAIN;
3912 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003913 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003914 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3915 goto err;
3916
Chris Mason109f6ae2010-04-02 09:20:18 -04003917 /* the leaf has changed, it now has room. return now */
3918 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3919 goto err;
3920
Yan, Zhengad48fd752009-11-12 09:33:58 +00003921 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3922 fi = btrfs_item_ptr(leaf, path->slots[0],
3923 struct btrfs_file_extent_item);
3924 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3925 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003926 }
3927
Chris Masonb9473432009-03-13 11:00:37 -04003928 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003929 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003930 if (ret)
3931 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003932
Yan, Zhengad48fd752009-11-12 09:33:58 +00003933 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003934 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003935 return 0;
3936err:
3937 path->keep_locks = 0;
3938 return ret;
3939}
3940
3941static noinline int split_item(struct btrfs_trans_handle *trans,
3942 struct btrfs_root *root,
3943 struct btrfs_path *path,
3944 struct btrfs_key *new_key,
3945 unsigned long split_offset)
3946{
3947 struct extent_buffer *leaf;
3948 struct btrfs_item *item;
3949 struct btrfs_item *new_item;
3950 int slot;
3951 char *buf;
3952 u32 nritems;
3953 u32 item_size;
3954 u32 orig_offset;
3955 struct btrfs_disk_key disk_key;
3956
Chris Masonb9473432009-03-13 11:00:37 -04003957 leaf = path->nodes[0];
3958 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3959
Chris Masonb4ce94d2009-02-04 09:25:08 -05003960 btrfs_set_path_blocking(path);
3961
Chris Mason459931e2008-12-10 09:10:46 -05003962 item = btrfs_item_nr(leaf, path->slots[0]);
3963 orig_offset = btrfs_item_offset(leaf, item);
3964 item_size = btrfs_item_size(leaf, item);
3965
Chris Mason459931e2008-12-10 09:10:46 -05003966 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003967 if (!buf)
3968 return -ENOMEM;
3969
Chris Mason459931e2008-12-10 09:10:46 -05003970 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3971 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003972
Chris Mason459931e2008-12-10 09:10:46 -05003973 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003974 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003975 if (slot != nritems) {
3976 /* shift the items */
3977 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003978 btrfs_item_nr_offset(slot),
3979 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003980 }
3981
3982 btrfs_cpu_key_to_disk(&disk_key, new_key);
3983 btrfs_set_item_key(leaf, &disk_key, slot);
3984
3985 new_item = btrfs_item_nr(leaf, slot);
3986
3987 btrfs_set_item_offset(leaf, new_item, orig_offset);
3988 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3989
3990 btrfs_set_item_offset(leaf, item,
3991 orig_offset + item_size - split_offset);
3992 btrfs_set_item_size(leaf, item, split_offset);
3993
3994 btrfs_set_header_nritems(leaf, nritems + 1);
3995
3996 /* write the data for the start of the original item */
3997 write_extent_buffer(leaf, buf,
3998 btrfs_item_ptr_offset(leaf, path->slots[0]),
3999 split_offset);
4000
4001 /* write the data for the new item */
4002 write_extent_buffer(leaf, buf + split_offset,
4003 btrfs_item_ptr_offset(leaf, slot),
4004 item_size - split_offset);
4005 btrfs_mark_buffer_dirty(leaf);
4006
Yan, Zhengad48fd752009-11-12 09:33:58 +00004007 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05004008 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004009 return 0;
4010}
4011
4012/*
4013 * This function splits a single item into two items,
4014 * giving 'new_key' to the new item and splitting the
4015 * old one at split_offset (from the start of the item).
4016 *
4017 * The path may be released by this operation. After
4018 * the split, the path is pointing to the old item. The
4019 * new item is going to be in the same node as the old one.
4020 *
4021 * Note, the item being split must be smaller enough to live alone on
4022 * a tree block with room for one extra struct btrfs_item
4023 *
4024 * This allows us to split the item in place, keeping a lock on the
4025 * leaf the entire time.
4026 */
4027int btrfs_split_item(struct btrfs_trans_handle *trans,
4028 struct btrfs_root *root,
4029 struct btrfs_path *path,
4030 struct btrfs_key *new_key,
4031 unsigned long split_offset)
4032{
4033 int ret;
4034 ret = setup_leaf_for_split(trans, root, path,
4035 sizeof(struct btrfs_item));
4036 if (ret)
4037 return ret;
4038
4039 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004040 return ret;
4041}
4042
4043/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004044 * This function duplicate a item, giving 'new_key' to the new item.
4045 * It guarantees both items live in the same tree leaf and the new item
4046 * is contiguous with the original item.
4047 *
4048 * This allows us to split file extent in place, keeping a lock on the
4049 * leaf the entire time.
4050 */
4051int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4052 struct btrfs_root *root,
4053 struct btrfs_path *path,
4054 struct btrfs_key *new_key)
4055{
4056 struct extent_buffer *leaf;
4057 int ret;
4058 u32 item_size;
4059
4060 leaf = path->nodes[0];
4061 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4062 ret = setup_leaf_for_split(trans, root, path,
4063 item_size + sizeof(struct btrfs_item));
4064 if (ret)
4065 return ret;
4066
4067 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004068 setup_items_for_insert(trans, root, path, new_key, &item_size,
4069 item_size, item_size +
4070 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004071 leaf = path->nodes[0];
4072 memcpy_extent_buffer(leaf,
4073 btrfs_item_ptr_offset(leaf, path->slots[0]),
4074 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4075 item_size);
4076 return 0;
4077}
4078
4079/*
Chris Masond352ac62008-09-29 15:18:18 -04004080 * make the item pointed to by the path smaller. new_size indicates
4081 * how small to make it, and from_end tells us if we just chop bytes
4082 * off the end of the item or if we shift the item to chop bytes off
4083 * the front.
4084 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004085void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4086 struct btrfs_root *root,
4087 struct btrfs_path *path,
4088 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004089{
Chris Masonb18c6682007-04-17 13:26:50 -04004090 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004091 struct extent_buffer *leaf;
4092 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004093 u32 nritems;
4094 unsigned int data_end;
4095 unsigned int old_data_start;
4096 unsigned int old_size;
4097 unsigned int size_diff;
4098 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004099 struct btrfs_map_token token;
4100
4101 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004102
Chris Mason5f39d392007-10-15 16:14:19 -04004103 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004104 slot = path->slots[0];
4105
4106 old_size = btrfs_item_size_nr(leaf, slot);
4107 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004108 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004109
Chris Mason5f39d392007-10-15 16:14:19 -04004110 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004111 data_end = leaf_data_end(root, leaf);
4112
Chris Mason5f39d392007-10-15 16:14:19 -04004113 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004114
Chris Masonb18c6682007-04-17 13:26:50 -04004115 size_diff = old_size - new_size;
4116
4117 BUG_ON(slot < 0);
4118 BUG_ON(slot >= nritems);
4119
4120 /*
4121 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4122 */
4123 /* first correct the data pointers */
4124 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004125 u32 ioff;
4126 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004127
Chris Masoncfed81a2012-03-03 07:40:03 -05004128 ioff = btrfs_token_item_offset(leaf, item, &token);
4129 btrfs_set_token_item_offset(leaf, item,
4130 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004131 }
Chris Masondb945352007-10-15 16:15:53 -04004132
Chris Masonb18c6682007-04-17 13:26:50 -04004133 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004134 if (from_end) {
4135 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4136 data_end + size_diff, btrfs_leaf_data(leaf) +
4137 data_end, old_data_start + new_size - data_end);
4138 } else {
4139 struct btrfs_disk_key disk_key;
4140 u64 offset;
4141
4142 btrfs_item_key(leaf, &disk_key, slot);
4143
4144 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4145 unsigned long ptr;
4146 struct btrfs_file_extent_item *fi;
4147
4148 fi = btrfs_item_ptr(leaf, slot,
4149 struct btrfs_file_extent_item);
4150 fi = (struct btrfs_file_extent_item *)(
4151 (unsigned long)fi - size_diff);
4152
4153 if (btrfs_file_extent_type(leaf, fi) ==
4154 BTRFS_FILE_EXTENT_INLINE) {
4155 ptr = btrfs_item_ptr_offset(leaf, slot);
4156 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004157 (unsigned long)fi,
4158 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004159 disk_bytenr));
4160 }
4161 }
4162
4163 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4164 data_end + size_diff, btrfs_leaf_data(leaf) +
4165 data_end, old_data_start - data_end);
4166
4167 offset = btrfs_disk_key_offset(&disk_key);
4168 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4169 btrfs_set_item_key(leaf, &disk_key, slot);
4170 if (slot == 0)
4171 fixup_low_keys(trans, root, path, &disk_key, 1);
4172 }
Chris Mason5f39d392007-10-15 16:14:19 -04004173
4174 item = btrfs_item_nr(leaf, slot);
4175 btrfs_set_item_size(leaf, item, new_size);
4176 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004177
Chris Mason5f39d392007-10-15 16:14:19 -04004178 if (btrfs_leaf_free_space(root, leaf) < 0) {
4179 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004180 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004181 }
Chris Masonb18c6682007-04-17 13:26:50 -04004182}
4183
Chris Masond352ac62008-09-29 15:18:18 -04004184/*
4185 * make the item pointed to by the path bigger, data_size is the new size.
4186 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004187void btrfs_extend_item(struct btrfs_trans_handle *trans,
4188 struct btrfs_root *root, struct btrfs_path *path,
4189 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004190{
Chris Mason6567e832007-04-16 09:22:45 -04004191 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004192 struct extent_buffer *leaf;
4193 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004194 u32 nritems;
4195 unsigned int data_end;
4196 unsigned int old_data;
4197 unsigned int old_size;
4198 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004199 struct btrfs_map_token token;
4200
4201 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004202
Chris Mason5f39d392007-10-15 16:14:19 -04004203 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004204
Chris Mason5f39d392007-10-15 16:14:19 -04004205 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004206 data_end = leaf_data_end(root, leaf);
4207
Chris Mason5f39d392007-10-15 16:14:19 -04004208 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4209 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004210 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004211 }
Chris Mason6567e832007-04-16 09:22:45 -04004212 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004213 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004214
4215 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004216 if (slot >= nritems) {
4217 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004218 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4219 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004220 BUG_ON(1);
4221 }
Chris Mason6567e832007-04-16 09:22:45 -04004222
4223 /*
4224 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4225 */
4226 /* first correct the data pointers */
4227 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004228 u32 ioff;
4229 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004230
Chris Masoncfed81a2012-03-03 07:40:03 -05004231 ioff = btrfs_token_item_offset(leaf, item, &token);
4232 btrfs_set_token_item_offset(leaf, item,
4233 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004234 }
Chris Mason5f39d392007-10-15 16:14:19 -04004235
Chris Mason6567e832007-04-16 09:22:45 -04004236 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004237 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004238 data_end - data_size, btrfs_leaf_data(leaf) +
4239 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004240
Chris Mason6567e832007-04-16 09:22:45 -04004241 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004242 old_size = btrfs_item_size_nr(leaf, slot);
4243 item = btrfs_item_nr(leaf, slot);
4244 btrfs_set_item_size(leaf, item, old_size + data_size);
4245 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004246
Chris Mason5f39d392007-10-15 16:14:19 -04004247 if (btrfs_leaf_free_space(root, leaf) < 0) {
4248 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004249 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004250 }
Chris Mason6567e832007-04-16 09:22:45 -04004251}
4252
Chris Mason74123bd2007-02-02 11:05:29 -05004253/*
Chris Masond352ac62008-09-29 15:18:18 -04004254 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004255 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004256 * Returns the number of keys that were inserted.
4257 */
4258int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4259 struct btrfs_root *root,
4260 struct btrfs_path *path,
4261 struct btrfs_key *cpu_key, u32 *data_size,
4262 int nr)
4263{
4264 struct extent_buffer *leaf;
4265 struct btrfs_item *item;
4266 int ret = 0;
4267 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004268 int i;
4269 u32 nritems;
4270 u32 total_data = 0;
4271 u32 total_size = 0;
4272 unsigned int data_end;
4273 struct btrfs_disk_key disk_key;
4274 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004275 struct btrfs_map_token token;
4276
4277 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004278
Yan Zheng87b29b22008-12-17 10:21:48 -05004279 for (i = 0; i < nr; i++) {
4280 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4281 BTRFS_LEAF_DATA_SIZE(root)) {
4282 break;
4283 nr = i;
4284 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004285 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004286 total_size += data_size[i] + sizeof(struct btrfs_item);
4287 }
4288 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004289
Josef Bacikf3465ca2008-11-12 14:19:50 -05004290 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4291 if (ret == 0)
4292 return -EEXIST;
4293 if (ret < 0)
4294 goto out;
4295
Josef Bacikf3465ca2008-11-12 14:19:50 -05004296 leaf = path->nodes[0];
4297
4298 nritems = btrfs_header_nritems(leaf);
4299 data_end = leaf_data_end(root, leaf);
4300
4301 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4302 for (i = nr; i >= 0; i--) {
4303 total_data -= data_size[i];
4304 total_size -= data_size[i] + sizeof(struct btrfs_item);
4305 if (total_size < btrfs_leaf_free_space(root, leaf))
4306 break;
4307 }
4308 nr = i;
4309 }
4310
4311 slot = path->slots[0];
4312 BUG_ON(slot < 0);
4313
4314 if (slot != nritems) {
4315 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4316
4317 item = btrfs_item_nr(leaf, slot);
4318 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4319
4320 /* figure out how many keys we can insert in here */
4321 total_data = data_size[0];
4322 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004323 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004324 break;
4325 total_data += data_size[i];
4326 }
4327 nr = i;
4328
4329 if (old_data < data_end) {
4330 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004331 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004332 slot, old_data, data_end);
4333 BUG_ON(1);
4334 }
4335 /*
4336 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4337 */
4338 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004339 for (i = slot; i < nritems; i++) {
4340 u32 ioff;
4341
4342 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004343 ioff = btrfs_token_item_offset(leaf, item, &token);
4344 btrfs_set_token_item_offset(leaf, item,
4345 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004346 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004347 /* shift the items */
4348 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4349 btrfs_item_nr_offset(slot),
4350 (nritems - slot) * sizeof(struct btrfs_item));
4351
4352 /* shift the data */
4353 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4354 data_end - total_data, btrfs_leaf_data(leaf) +
4355 data_end, old_data - data_end);
4356 data_end = old_data;
4357 } else {
4358 /*
4359 * this sucks but it has to be done, if we are inserting at
4360 * the end of the leaf only insert 1 of the items, since we
4361 * have no way of knowing whats on the next leaf and we'd have
4362 * to drop our current locks to figure it out
4363 */
4364 nr = 1;
4365 }
4366
4367 /* setup the item for the new data */
4368 for (i = 0; i < nr; i++) {
4369 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4370 btrfs_set_item_key(leaf, &disk_key, slot + i);
4371 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004372 btrfs_set_token_item_offset(leaf, item,
4373 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004374 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004375 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004376 }
4377 btrfs_set_header_nritems(leaf, nritems + nr);
4378 btrfs_mark_buffer_dirty(leaf);
4379
4380 ret = 0;
4381 if (slot == 0) {
4382 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004383 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004384 }
4385
4386 if (btrfs_leaf_free_space(root, leaf) < 0) {
4387 btrfs_print_leaf(root, leaf);
4388 BUG();
4389 }
4390out:
4391 if (!ret)
4392 ret = nr;
4393 return ret;
4394}
4395
4396/*
Chris Mason44871b12009-03-13 10:04:31 -04004397 * this is a helper for btrfs_insert_empty_items, the main goal here is
4398 * to save stack depth by doing the bulk of the work in a function
4399 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004400 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004401void setup_items_for_insert(struct btrfs_trans_handle *trans,
4402 struct btrfs_root *root, struct btrfs_path *path,
4403 struct btrfs_key *cpu_key, u32 *data_size,
4404 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004405{
Chris Mason5f39d392007-10-15 16:14:19 -04004406 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004407 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004408 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004409 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004410 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004411 struct extent_buffer *leaf;
4412 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004413 struct btrfs_map_token token;
4414
4415 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004416
Chris Mason5f39d392007-10-15 16:14:19 -04004417 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004418 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004419
Chris Mason5f39d392007-10-15 16:14:19 -04004420 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004421 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004422
Chris Masonf25956c2008-09-12 15:32:53 -04004423 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004424 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004425 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004426 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004427 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004428 }
Chris Mason5f39d392007-10-15 16:14:19 -04004429
Chris Masonbe0e5c02007-01-26 15:51:26 -05004430 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004431 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004432
Chris Mason5f39d392007-10-15 16:14:19 -04004433 if (old_data < data_end) {
4434 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004435 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004436 slot, old_data, data_end);
4437 BUG_ON(1);
4438 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004439 /*
4440 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4441 */
4442 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004443 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004444 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004445
Chris Mason5f39d392007-10-15 16:14:19 -04004446 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004447 ioff = btrfs_token_item_offset(leaf, item, &token);
4448 btrfs_set_token_item_offset(leaf, item,
4449 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004450 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004451 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004452 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004453 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004454 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004455
4456 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004457 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004458 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004459 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004460 data_end = old_data;
4461 }
Chris Mason5f39d392007-10-15 16:14:19 -04004462
Chris Mason62e27492007-03-15 12:56:47 -04004463 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004464 for (i = 0; i < nr; i++) {
4465 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4466 btrfs_set_item_key(leaf, &disk_key, slot + i);
4467 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004468 btrfs_set_token_item_offset(leaf, item,
4469 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004470 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004471 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004472 }
Chris Mason44871b12009-03-13 10:04:31 -04004473
Chris Mason9c583092008-01-29 15:15:18 -05004474 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004475
Chris Mason5a01a2e2008-01-30 11:43:54 -05004476 if (slot == 0) {
4477 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004478 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004479 }
Chris Masonb9473432009-03-13 11:00:37 -04004480 btrfs_unlock_up_safe(path, 1);
4481 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004482
Chris Mason5f39d392007-10-15 16:14:19 -04004483 if (btrfs_leaf_free_space(root, leaf) < 0) {
4484 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004485 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004486 }
Chris Mason44871b12009-03-13 10:04:31 -04004487}
4488
4489/*
4490 * Given a key and some data, insert items into the tree.
4491 * This does all the path init required, making room in the tree if needed.
4492 */
4493int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4494 struct btrfs_root *root,
4495 struct btrfs_path *path,
4496 struct btrfs_key *cpu_key, u32 *data_size,
4497 int nr)
4498{
Chris Mason44871b12009-03-13 10:04:31 -04004499 int ret = 0;
4500 int slot;
4501 int i;
4502 u32 total_size = 0;
4503 u32 total_data = 0;
4504
4505 for (i = 0; i < nr; i++)
4506 total_data += data_size[i];
4507
4508 total_size = total_data + (nr * sizeof(struct btrfs_item));
4509 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4510 if (ret == 0)
4511 return -EEXIST;
4512 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004513 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004514
Chris Mason44871b12009-03-13 10:04:31 -04004515 slot = path->slots[0];
4516 BUG_ON(slot < 0);
4517
Jeff Mahoney143bede2012-03-01 14:56:26 +01004518 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004519 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004520 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004521}
4522
4523/*
4524 * Given a key and some data, insert an item into the tree.
4525 * This does all the path init required, making room in the tree if needed.
4526 */
Chris Masone089f052007-03-16 16:20:31 -04004527int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4528 *root, struct btrfs_key *cpu_key, void *data, u32
4529 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004530{
4531 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004532 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004533 struct extent_buffer *leaf;
4534 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004535
Chris Mason2c90e5d2007-04-02 10:50:19 -04004536 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004537 if (!path)
4538 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004539 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004540 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004541 leaf = path->nodes[0];
4542 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4543 write_extent_buffer(leaf, data, ptr, data_size);
4544 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004545 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004546 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004547 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004548}
4549
Chris Mason74123bd2007-02-02 11:05:29 -05004550/*
Chris Mason5de08d72007-02-24 06:24:44 -05004551 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004552 *
Chris Masond352ac62008-09-29 15:18:18 -04004553 * the tree should have been previously balanced so the deletion does not
4554 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004555 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004556static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004557 struct btrfs_path *path, int level, int slot,
4558 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004559{
Chris Mason5f39d392007-10-15 16:14:19 -04004560 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004561 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004562 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004563
Chris Mason5f39d392007-10-15 16:14:19 -04004564 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004565 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004566 if (tree_mod_log && level)
4567 tree_mod_log_eb_move(root->fs_info, parent, slot,
4568 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004569 memmove_extent_buffer(parent,
4570 btrfs_node_key_ptr_offset(slot),
4571 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004572 sizeof(struct btrfs_key_ptr) *
4573 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004574 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004575 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4576 MOD_LOG_KEY_REMOVE);
4577 BUG_ON(ret < 0);
Chris Masonbb803952007-03-01 12:04:21 -05004578 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004579
Chris Mason7518a232007-03-12 12:01:18 -04004580 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004581 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004582 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004583 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004584 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004585 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004586 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004587 struct btrfs_disk_key disk_key;
4588
4589 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004590 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004591 }
Chris Masond6025572007-03-30 14:27:56 -04004592 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004593}
4594
Chris Mason74123bd2007-02-02 11:05:29 -05004595/*
Chris Mason323ac952008-10-01 19:05:46 -04004596 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004597 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004598 *
4599 * This deletes the pointer in path->nodes[1] and frees the leaf
4600 * block extent. zero is returned if it all worked out, < 0 otherwise.
4601 *
4602 * The path must have already been setup for deleting the leaf, including
4603 * all the proper balancing. path->nodes[1] must be locked.
4604 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004605static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4606 struct btrfs_root *root,
4607 struct btrfs_path *path,
4608 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004609{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004610 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004611 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004612
Chris Mason4d081c42009-02-04 09:31:28 -05004613 /*
4614 * btrfs_free_extent is expensive, we want to make sure we
4615 * aren't holding any locks when we call it
4616 */
4617 btrfs_unlock_up_safe(path, 0);
4618
Yan, Zhengf0486c62010-05-16 10:46:25 -04004619 root_sub_used(root, leaf->len);
4620
Josef Bacik3083ee22012-03-09 16:01:49 -05004621 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004622 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004623 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004624}
4625/*
Chris Mason74123bd2007-02-02 11:05:29 -05004626 * delete the item at the leaf level in path. If that empties
4627 * the leaf, remove it from the tree
4628 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004629int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4630 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004631{
Chris Mason5f39d392007-10-15 16:14:19 -04004632 struct extent_buffer *leaf;
4633 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004634 int last_off;
4635 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004636 int ret = 0;
4637 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004638 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004639 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004640 struct btrfs_map_token token;
4641
4642 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004643
Chris Mason5f39d392007-10-15 16:14:19 -04004644 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004645 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4646
4647 for (i = 0; i < nr; i++)
4648 dsize += btrfs_item_size_nr(leaf, slot + i);
4649
Chris Mason5f39d392007-10-15 16:14:19 -04004650 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004651
Chris Mason85e21ba2008-01-29 15:11:36 -05004652 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004653 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004654
4655 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004656 data_end + dsize,
4657 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004658 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004659
Chris Mason85e21ba2008-01-29 15:11:36 -05004660 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004661 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004662
Chris Mason5f39d392007-10-15 16:14:19 -04004663 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004664 ioff = btrfs_token_item_offset(leaf, item, &token);
4665 btrfs_set_token_item_offset(leaf, item,
4666 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004667 }
Chris Masondb945352007-10-15 16:15:53 -04004668
Chris Mason5f39d392007-10-15 16:14:19 -04004669 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004670 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004671 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004672 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004673 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004674 btrfs_set_header_nritems(leaf, nritems - nr);
4675 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004676
Chris Mason74123bd2007-02-02 11:05:29 -05004677 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004678 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004679 if (leaf == root->node) {
4680 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004681 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004682 btrfs_set_path_blocking(path);
4683 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004684 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004685 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004686 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004687 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004688 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004689 struct btrfs_disk_key disk_key;
4690
4691 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004692 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004693 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004694
Chris Mason74123bd2007-02-02 11:05:29 -05004695 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004696 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004697 /* push_leaf_left fixes the path.
4698 * make sure the path still points to our leaf
4699 * for possible call to del_ptr below
4700 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004701 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004702 extent_buffer_get(leaf);
4703
Chris Masonb9473432009-03-13 11:00:37 -04004704 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004705 wret = push_leaf_left(trans, root, path, 1, 1,
4706 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004707 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004708 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004709
4710 if (path->nodes[0] == leaf &&
4711 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004712 wret = push_leaf_right(trans, root, path, 1,
4713 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004714 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004715 ret = wret;
4716 }
Chris Mason5f39d392007-10-15 16:14:19 -04004717
4718 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004719 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004720 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004721 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004722 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004723 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004724 /* if we're still in the path, make sure
4725 * we're dirty. Otherwise, one of the
4726 * push_leaf functions must have already
4727 * dirtied this buffer
4728 */
4729 if (path->nodes[0] == leaf)
4730 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004731 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004732 }
Chris Masond5719762007-03-23 10:01:08 -04004733 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004734 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004735 }
4736 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004737 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004738}
4739
Chris Mason97571fd2007-02-24 13:39:08 -05004740/*
Chris Mason925baed2008-06-25 16:01:30 -04004741 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004742 * returns 0 if it found something or 1 if there are no lesser leaves.
4743 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004744 *
4745 * This may release the path, and so you may lose any locks held at the
4746 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004747 */
4748int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4749{
Chris Mason925baed2008-06-25 16:01:30 -04004750 struct btrfs_key key;
4751 struct btrfs_disk_key found_key;
4752 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004753
Chris Mason925baed2008-06-25 16:01:30 -04004754 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004755
Chris Mason925baed2008-06-25 16:01:30 -04004756 if (key.offset > 0)
4757 key.offset--;
4758 else if (key.type > 0)
4759 key.type--;
4760 else if (key.objectid > 0)
4761 key.objectid--;
4762 else
4763 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004764
David Sterbab3b4aa72011-04-21 01:20:15 +02004765 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004766 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4767 if (ret < 0)
4768 return ret;
4769 btrfs_item_key(path->nodes[0], &found_key, 0);
4770 ret = comp_keys(&found_key, &key);
4771 if (ret < 0)
4772 return 0;
4773 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004774}
4775
Chris Mason3f157a22008-06-25 16:01:31 -04004776/*
4777 * A helper function to walk down the tree starting at min_key, and looking
4778 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004779 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004780 *
4781 * This does not cow, but it does stuff the starting key it finds back
4782 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4783 * key and get a writable path.
4784 *
4785 * This does lock as it descends, and path->keep_locks should be set
4786 * to 1 by the caller.
4787 *
4788 * This honors path->lowest_level to prevent descent past a given level
4789 * of the tree.
4790 *
Chris Masond352ac62008-09-29 15:18:18 -04004791 * min_trans indicates the oldest transaction that you are interested
4792 * in walking through. Any nodes or leaves older than min_trans are
4793 * skipped over (without reading them).
4794 *
Chris Mason3f157a22008-06-25 16:01:31 -04004795 * returns zero if something useful was found, < 0 on error and 1 if there
4796 * was nothing in the tree that matched the search criteria.
4797 */
4798int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004799 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004800 struct btrfs_path *path, int cache_only,
4801 u64 min_trans)
4802{
4803 struct extent_buffer *cur;
4804 struct btrfs_key found_key;
4805 int slot;
Yan96524802008-07-24 12:19:49 -04004806 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004807 u32 nritems;
4808 int level;
4809 int ret = 1;
4810
Chris Mason934d3752008-12-08 16:43:10 -05004811 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004812again:
Chris Masonbd681512011-07-16 15:23:14 -04004813 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004814 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004815 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004816 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004817 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004818
4819 if (btrfs_header_generation(cur) < min_trans) {
4820 ret = 1;
4821 goto out;
4822 }
Chris Masond3977122009-01-05 21:25:51 -05004823 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004824 nritems = btrfs_header_nritems(cur);
4825 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004826 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004827
Chris Mason323ac952008-10-01 19:05:46 -04004828 /* at the lowest level, we're done, setup the path and exit */
4829 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004830 if (slot >= nritems)
4831 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004832 ret = 0;
4833 path->slots[level] = slot;
4834 btrfs_item_key_to_cpu(cur, &found_key, slot);
4835 goto out;
4836 }
Yan96524802008-07-24 12:19:49 -04004837 if (sret && slot > 0)
4838 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004839 /*
4840 * check this node pointer against the cache_only and
4841 * min_trans parameters. If it isn't in cache or is too
4842 * old, skip to the next one.
4843 */
Chris Masond3977122009-01-05 21:25:51 -05004844 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004845 u64 blockptr;
4846 u64 gen;
4847 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004848 struct btrfs_disk_key disk_key;
4849
Chris Mason3f157a22008-06-25 16:01:31 -04004850 blockptr = btrfs_node_blockptr(cur, slot);
4851 gen = btrfs_node_ptr_generation(cur, slot);
4852 if (gen < min_trans) {
4853 slot++;
4854 continue;
4855 }
4856 if (!cache_only)
4857 break;
4858
Chris Masone02119d2008-09-05 16:13:11 -04004859 if (max_key) {
4860 btrfs_node_key(cur, &disk_key, slot);
4861 if (comp_keys(&disk_key, max_key) >= 0) {
4862 ret = 1;
4863 goto out;
4864 }
4865 }
4866
Chris Mason3f157a22008-06-25 16:01:31 -04004867 tmp = btrfs_find_tree_block(root, blockptr,
4868 btrfs_level_size(root, level - 1));
4869
Chris Masonb9fab912012-05-06 07:23:47 -04004870 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004871 free_extent_buffer(tmp);
4872 break;
4873 }
4874 if (tmp)
4875 free_extent_buffer(tmp);
4876 slot++;
4877 }
Chris Masone02119d2008-09-05 16:13:11 -04004878find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004879 /*
4880 * we didn't find a candidate key in this node, walk forward
4881 * and find another one
4882 */
4883 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004884 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004885 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004886 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004887 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004888 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004889 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004890 goto again;
4891 } else {
4892 goto out;
4893 }
4894 }
4895 /* save our key for returning back */
4896 btrfs_node_key_to_cpu(cur, &found_key, slot);
4897 path->slots[level] = slot;
4898 if (level == path->lowest_level) {
4899 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004900 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004901 goto out;
4902 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004903 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004904 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004905 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004906
Chris Masonbd681512011-07-16 15:23:14 -04004907 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004908
Chris Masonbd681512011-07-16 15:23:14 -04004909 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004910 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004911 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004912 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004913 }
4914out:
4915 if (ret == 0)
4916 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004917 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004918 return ret;
4919}
4920
4921/*
4922 * this is similar to btrfs_next_leaf, but does not try to preserve
4923 * and fixup the path. It looks for and returns the next key in the
4924 * tree based on the current path and the cache_only and min_trans
4925 * parameters.
4926 *
4927 * 0 is returned if another key is found, < 0 if there are any errors
4928 * and 1 is returned if there are no higher keys in the tree
4929 *
4930 * path->keep_locks should be set to 1 on the search made before
4931 * calling this function.
4932 */
Chris Masone7a84562008-06-25 16:01:31 -04004933int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004934 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004935 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004936{
Chris Masone7a84562008-06-25 16:01:31 -04004937 int slot;
4938 struct extent_buffer *c;
4939
Chris Mason934d3752008-12-08 16:43:10 -05004940 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004941 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004942 if (!path->nodes[level])
4943 return 1;
4944
4945 slot = path->slots[level] + 1;
4946 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004947next:
Chris Masone7a84562008-06-25 16:01:31 -04004948 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004949 int ret;
4950 int orig_lowest;
4951 struct btrfs_key cur_key;
4952 if (level + 1 >= BTRFS_MAX_LEVEL ||
4953 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004954 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004955
4956 if (path->locks[level + 1]) {
4957 level++;
4958 continue;
4959 }
4960
4961 slot = btrfs_header_nritems(c) - 1;
4962 if (level == 0)
4963 btrfs_item_key_to_cpu(c, &cur_key, slot);
4964 else
4965 btrfs_node_key_to_cpu(c, &cur_key, slot);
4966
4967 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004968 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004969 path->lowest_level = level;
4970 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4971 0, 0);
4972 path->lowest_level = orig_lowest;
4973 if (ret < 0)
4974 return ret;
4975
4976 c = path->nodes[level];
4977 slot = path->slots[level];
4978 if (ret == 0)
4979 slot++;
4980 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004981 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004982
Chris Masone7a84562008-06-25 16:01:31 -04004983 if (level == 0)
4984 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004985 else {
4986 u64 blockptr = btrfs_node_blockptr(c, slot);
4987 u64 gen = btrfs_node_ptr_generation(c, slot);
4988
4989 if (cache_only) {
4990 struct extent_buffer *cur;
4991 cur = btrfs_find_tree_block(root, blockptr,
4992 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004993 if (!cur ||
4994 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004995 slot++;
4996 if (cur)
4997 free_extent_buffer(cur);
4998 goto next;
4999 }
5000 free_extent_buffer(cur);
5001 }
5002 if (gen < min_trans) {
5003 slot++;
5004 goto next;
5005 }
Chris Masone7a84562008-06-25 16:01:31 -04005006 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04005007 }
Chris Masone7a84562008-06-25 16:01:31 -04005008 return 0;
5009 }
5010 return 1;
5011}
5012
Chris Mason7bb86312007-12-11 09:25:06 -05005013/*
Chris Mason925baed2008-06-25 16:01:30 -04005014 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05005015 * returns 0 if it found something or 1 if there are no greater leaves.
5016 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05005017 */
Chris Mason234b63a2007-03-13 10:46:10 -04005018int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05005019{
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005020 return btrfs_next_old_leaf(root, path, 0);
5021}
5022
5023int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
5024 u64 time_seq)
5025{
Chris Masond97e63b2007-02-20 16:40:44 -05005026 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005027 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005028 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005029 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005030 struct btrfs_key key;
5031 u32 nritems;
5032 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005033 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005034 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005035
5036 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005037 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005038 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005039
Chris Mason8e73f272009-04-03 10:14:18 -04005040 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5041again:
5042 level = 1;
5043 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005044 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005045 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005046
Chris Masona2135012008-06-25 16:01:30 -04005047 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005048 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005049
Jan Schmidt3d7806e2012-06-11 08:29:29 +02005050 if (time_seq)
5051 ret = btrfs_search_old_slot(root, &key, path, time_seq);
5052 else
5053 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason925baed2008-06-25 16:01:30 -04005054 path->keep_locks = 0;
5055
5056 if (ret < 0)
5057 return ret;
5058
Chris Masona2135012008-06-25 16:01:30 -04005059 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005060 /*
5061 * by releasing the path above we dropped all our locks. A balance
5062 * could have added more items next to the key that used to be
5063 * at the very end of the block. So, check again here and
5064 * advance the path if there are now more items available.
5065 */
Chris Masona2135012008-06-25 16:01:30 -04005066 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005067 if (ret == 0)
5068 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005069 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005070 goto done;
5071 }
Chris Masond97e63b2007-02-20 16:40:44 -05005072
Chris Masond3977122009-01-05 21:25:51 -05005073 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005074 if (!path->nodes[level]) {
5075 ret = 1;
5076 goto done;
5077 }
Chris Mason5f39d392007-10-15 16:14:19 -04005078
Chris Masond97e63b2007-02-20 16:40:44 -05005079 slot = path->slots[level] + 1;
5080 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005081 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005082 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005083 if (level == BTRFS_MAX_LEVEL) {
5084 ret = 1;
5085 goto done;
5086 }
Chris Masond97e63b2007-02-20 16:40:44 -05005087 continue;
5088 }
Chris Mason5f39d392007-10-15 16:14:19 -04005089
Chris Mason925baed2008-06-25 16:01:30 -04005090 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005091 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005092 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005093 }
Chris Mason5f39d392007-10-15 16:14:19 -04005094
Chris Mason8e73f272009-04-03 10:14:18 -04005095 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005096 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005097 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005098 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005099 if (ret == -EAGAIN)
5100 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005101
Chris Mason76a05b32009-05-14 13:24:30 -04005102 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005103 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005104 goto done;
5105 }
5106
Chris Mason5cd57b22008-06-25 16:01:30 -04005107 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005108 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005109 if (!ret) {
5110 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005111 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005112 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005113 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005114 }
Chris Mason31533fb2011-07-26 16:01:59 -04005115 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005116 }
Chris Masond97e63b2007-02-20 16:40:44 -05005117 break;
5118 }
5119 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005120 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005121 level--;
5122 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005123 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005124 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005125
Chris Mason5f39d392007-10-15 16:14:19 -04005126 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005127 path->nodes[level] = next;
5128 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005129 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005130 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005131 if (!level)
5132 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005133
Chris Mason8e73f272009-04-03 10:14:18 -04005134 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005135 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005136 if (ret == -EAGAIN)
5137 goto again;
5138
Chris Mason76a05b32009-05-14 13:24:30 -04005139 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005140 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005141 goto done;
5142 }
5143
Chris Mason5cd57b22008-06-25 16:01:30 -04005144 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005145 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005146 if (!ret) {
5147 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005148 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005149 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005150 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005151 }
Chris Mason31533fb2011-07-26 16:01:59 -04005152 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005153 }
Chris Masond97e63b2007-02-20 16:40:44 -05005154 }
Chris Mason8e73f272009-04-03 10:14:18 -04005155 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005156done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005157 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005158 path->leave_spinning = old_spinning;
5159 if (!old_spinning)
5160 btrfs_set_path_blocking(path);
5161
5162 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005163}
Chris Mason0b86a832008-03-24 15:01:56 -04005164
Chris Mason3f157a22008-06-25 16:01:31 -04005165/*
5166 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5167 * searching until it gets past min_objectid or finds an item of 'type'
5168 *
5169 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5170 */
Chris Mason0b86a832008-03-24 15:01:56 -04005171int btrfs_previous_item(struct btrfs_root *root,
5172 struct btrfs_path *path, u64 min_objectid,
5173 int type)
5174{
5175 struct btrfs_key found_key;
5176 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005177 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005178 int ret;
5179
Chris Masond3977122009-01-05 21:25:51 -05005180 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005181 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005182 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005183 ret = btrfs_prev_leaf(root, path);
5184 if (ret != 0)
5185 return ret;
5186 } else {
5187 path->slots[0]--;
5188 }
5189 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005190 nritems = btrfs_header_nritems(leaf);
5191 if (nritems == 0)
5192 return 1;
5193 if (path->slots[0] == nritems)
5194 path->slots[0]--;
5195
Chris Mason0b86a832008-03-24 15:01:56 -04005196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005197 if (found_key.objectid < min_objectid)
5198 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005199 if (found_key.type == type)
5200 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005201 if (found_key.objectid == min_objectid &&
5202 found_key.type < type)
5203 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005204 }
5205 return 1;
5206}