blob: 63147c1315a7ba0bc344f6bdebfee43a4bcb444b [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 Masonb9447ef82009-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
1026 return found;
1027}
1028
1029/*
1030 * tm is a pointer to the first operation to rewind within eb. then, all
1031 * previous operations will be rewinded (until we reach something older than
1032 * time_seq).
1033 */
1034static void
1035__tree_mod_log_rewind(struct extent_buffer *eb, u64 time_seq,
1036 struct tree_mod_elem *first_tm)
1037{
1038 u32 n;
1039 struct rb_node *next;
1040 struct tree_mod_elem *tm = first_tm;
1041 unsigned long o_dst;
1042 unsigned long o_src;
1043 unsigned long p_size = sizeof(struct btrfs_key_ptr);
1044
1045 n = btrfs_header_nritems(eb);
1046 while (tm && tm->elem.seq >= time_seq) {
1047 /*
1048 * all the operations are recorded with the operator used for
1049 * the modification. as we're going backwards, we do the
1050 * opposite of each operation here.
1051 */
1052 switch (tm->op) {
1053 case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
1054 BUG_ON(tm->slot < n);
1055 case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
1056 case MOD_LOG_KEY_REMOVE:
1057 btrfs_set_node_key(eb, &tm->key, tm->slot);
1058 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1059 btrfs_set_node_ptr_generation(eb, tm->slot,
1060 tm->generation);
1061 n++;
1062 break;
1063 case MOD_LOG_KEY_REPLACE:
1064 BUG_ON(tm->slot >= n);
1065 btrfs_set_node_key(eb, &tm->key, tm->slot);
1066 btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
1067 btrfs_set_node_ptr_generation(eb, tm->slot,
1068 tm->generation);
1069 break;
1070 case MOD_LOG_KEY_ADD:
1071 if (tm->slot != n - 1) {
1072 o_dst = btrfs_node_key_ptr_offset(tm->slot);
1073 o_src = btrfs_node_key_ptr_offset(tm->slot + 1);
1074 memmove_extent_buffer(eb, o_dst, o_src, p_size);
1075 }
1076 n--;
1077 break;
1078 case MOD_LOG_MOVE_KEYS:
1079 memmove_extent_buffer(eb, tm->slot, tm->move.dst_slot,
1080 tm->move.nr_items * p_size);
1081 break;
1082 case MOD_LOG_ROOT_REPLACE:
1083 /*
1084 * this operation is special. for roots, this must be
1085 * handled explicitly before rewinding.
1086 * for non-roots, this operation may exist if the node
1087 * was a root: root A -> child B; then A gets empty and
1088 * B is promoted to the new root. in the mod log, we'll
1089 * have a root-replace operation for B, a tree block
1090 * that is no root. we simply ignore that operation.
1091 */
1092 break;
1093 }
1094 next = rb_next(&tm->node);
1095 if (!next)
1096 break;
1097 tm = container_of(next, struct tree_mod_elem, node);
1098 if (tm->index != first_tm->index)
1099 break;
1100 }
1101 btrfs_set_header_nritems(eb, n);
1102}
1103
1104static struct extent_buffer *
1105tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
1106 u64 time_seq)
1107{
1108 struct extent_buffer *eb_rewin;
1109 struct tree_mod_elem *tm;
1110
1111 if (!time_seq)
1112 return eb;
1113
1114 if (btrfs_header_level(eb) == 0)
1115 return eb;
1116
1117 tm = tree_mod_log_search(fs_info, eb->start, time_seq);
1118 if (!tm)
1119 return eb;
1120
1121 if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
1122 BUG_ON(tm->slot != 0);
1123 eb_rewin = alloc_dummy_extent_buffer(eb->start,
1124 fs_info->tree_root->nodesize);
1125 BUG_ON(!eb_rewin);
1126 btrfs_set_header_bytenr(eb_rewin, eb->start);
1127 btrfs_set_header_backref_rev(eb_rewin,
1128 btrfs_header_backref_rev(eb));
1129 btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
1130 } else {
1131 eb_rewin = btrfs_clone_extent_buffer(eb);
1132 BUG_ON(!eb_rewin);
1133 }
1134
1135 extent_buffer_get(eb_rewin);
1136 free_extent_buffer(eb);
1137
1138 __tree_mod_log_rewind(eb_rewin, time_seq, tm);
1139
1140 return eb_rewin;
1141}
1142
1143static inline struct extent_buffer *
1144get_old_root(struct btrfs_root *root, u64 time_seq)
1145{
1146 struct tree_mod_elem *tm;
1147 struct extent_buffer *eb;
1148 struct tree_mod_root *old_root;
1149 u64 old_generation;
1150
1151 tm = __tree_mod_log_oldest_root(root->fs_info, root, time_seq);
1152 if (!tm)
1153 return root->node;
1154
1155 old_root = &tm->old_root;
1156 old_generation = tm->generation;
1157
1158 tm = tree_mod_log_search(root->fs_info, old_root->logical, time_seq);
1159 /*
1160 * there was an item in the log when __tree_mod_log_oldest_root
1161 * returned. this one must not go away, because the time_seq passed to
1162 * us must be blocking its removal.
1163 */
1164 BUG_ON(!tm);
1165
1166 if (old_root->logical == root->node->start) {
1167 /* there are logged operations for the current root */
1168 eb = btrfs_clone_extent_buffer(root->node);
1169 } else {
1170 /* there's a root replace operation for the current root */
1171 eb = alloc_dummy_extent_buffer(tm->index << PAGE_CACHE_SHIFT,
1172 root->nodesize);
1173 btrfs_set_header_bytenr(eb, eb->start);
1174 btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
1175 btrfs_set_header_owner(eb, root->root_key.objectid);
1176 }
1177 if (!eb)
1178 return NULL;
1179 btrfs_set_header_level(eb, old_root->level);
1180 btrfs_set_header_generation(eb, old_generation);
1181 __tree_mod_log_rewind(eb, time_seq, tm);
1182
1183 return eb;
1184}
1185
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001186static inline int should_cow_block(struct btrfs_trans_handle *trans,
1187 struct btrfs_root *root,
1188 struct extent_buffer *buf)
1189{
Liu Bof1ebcc72011-11-14 20:48:06 -05001190 /* ensure we can see the force_cow */
1191 smp_rmb();
1192
1193 /*
1194 * We do not need to cow a block if
1195 * 1) this block is not created or changed in this transaction;
1196 * 2) this block does not belong to TREE_RELOC tree;
1197 * 3) the root is not forced COW.
1198 *
1199 * What is forced COW:
1200 * when we create snapshot during commiting the transaction,
1201 * after we've finished coping src root, we must COW the shared
1202 * block to ensure the metadata consistency.
1203 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001204 if (btrfs_header_generation(buf) == trans->transid &&
1205 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
1206 !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
Liu Bof1ebcc72011-11-14 20:48:06 -05001207 btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
1208 !root->force_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001209 return 0;
1210 return 1;
1211}
1212
Chris Masond352ac62008-09-29 15:18:18 -04001213/*
1214 * cows a single block, see __btrfs_cow_block for the real work.
1215 * This version of it has extra checks so that a block isn't cow'd more than
1216 * once per transaction, as long as it hasn't been written yet
1217 */
Chris Masond3977122009-01-05 21:25:51 -05001218noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001219 struct btrfs_root *root, struct extent_buffer *buf,
1220 struct extent_buffer *parent, int parent_slot,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001221 struct extent_buffer **cow_ret)
Chris Mason02217ed2007-03-02 16:08:05 -05001222{
Chris Mason6702ed42007-08-07 16:15:09 -04001223 u64 search_start;
Chris Masonf510cfe2007-10-15 16:14:48 -04001224 int ret;
Chris Masondc17ff82008-01-08 15:46:30 -05001225
Chris Masonccd467d2007-06-28 15:57:36 -04001226 if (trans->transaction != root->fs_info->running_transaction) {
Chris Masond3977122009-01-05 21:25:51 -05001227 printk(KERN_CRIT "trans %llu running %llu\n",
1228 (unsigned long long)trans->transid,
1229 (unsigned long long)
Chris Masonccd467d2007-06-28 15:57:36 -04001230 root->fs_info->running_transaction->transid);
1231 WARN_ON(1);
1232 }
1233 if (trans->transid != root->fs_info->generation) {
Chris Masond3977122009-01-05 21:25:51 -05001234 printk(KERN_CRIT "trans %llu running %llu\n",
1235 (unsigned long long)trans->transid,
1236 (unsigned long long)root->fs_info->generation);
Chris Masonccd467d2007-06-28 15:57:36 -04001237 WARN_ON(1);
1238 }
Chris Masondc17ff82008-01-08 15:46:30 -05001239
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001240 if (!should_cow_block(trans, root, buf)) {
Chris Mason02217ed2007-03-02 16:08:05 -05001241 *cow_ret = buf;
1242 return 0;
1243 }
Chris Masonc4876852009-02-04 09:24:25 -05001244
Chris Mason0b86a832008-03-24 15:01:56 -04001245 search_start = buf->start & ~((u64)(1024 * 1024 * 1024) - 1);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001246
1247 if (parent)
1248 btrfs_set_lock_blocking(parent);
1249 btrfs_set_lock_blocking(buf);
1250
Chris Masonf510cfe2007-10-15 16:14:48 -04001251 ret = __btrfs_cow_block(trans, root, buf, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001252 parent_slot, cow_ret, search_start, 0);
liubo1abe9b82011-03-24 11:18:59 +00001253
1254 trace_btrfs_cow_block(root, buf, *cow_ret);
1255
Chris Masonf510cfe2007-10-15 16:14:48 -04001256 return ret;
Chris Mason6702ed42007-08-07 16:15:09 -04001257}
1258
Chris Masond352ac62008-09-29 15:18:18 -04001259/*
1260 * helper function for defrag to decide if two blocks pointed to by a
1261 * node are actually close by
1262 */
Chris Mason6b800532007-10-15 16:17:34 -04001263static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
Chris Mason6702ed42007-08-07 16:15:09 -04001264{
Chris Mason6b800532007-10-15 16:17:34 -04001265 if (blocknr < other && other - (blocknr + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001266 return 1;
Chris Mason6b800532007-10-15 16:17:34 -04001267 if (blocknr > other && blocknr - (other + blocksize) < 32768)
Chris Mason6702ed42007-08-07 16:15:09 -04001268 return 1;
Chris Mason02217ed2007-03-02 16:08:05 -05001269 return 0;
1270}
1271
Chris Mason081e9572007-11-06 10:26:24 -05001272/*
1273 * compare two keys in a memcmp fashion
1274 */
1275static int comp_keys(struct btrfs_disk_key *disk, struct btrfs_key *k2)
1276{
1277 struct btrfs_key k1;
1278
1279 btrfs_disk_key_to_cpu(&k1, disk);
1280
Diego Calleja20736ab2009-07-24 11:06:52 -04001281 return btrfs_comp_cpu_keys(&k1, k2);
Chris Mason081e9572007-11-06 10:26:24 -05001282}
1283
Josef Bacikf3465ca2008-11-12 14:19:50 -05001284/*
1285 * same as comp_keys only with two btrfs_key's
1286 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001287int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2)
Josef Bacikf3465ca2008-11-12 14:19:50 -05001288{
1289 if (k1->objectid > k2->objectid)
1290 return 1;
1291 if (k1->objectid < k2->objectid)
1292 return -1;
1293 if (k1->type > k2->type)
1294 return 1;
1295 if (k1->type < k2->type)
1296 return -1;
1297 if (k1->offset > k2->offset)
1298 return 1;
1299 if (k1->offset < k2->offset)
1300 return -1;
1301 return 0;
1302}
Chris Mason081e9572007-11-06 10:26:24 -05001303
Chris Masond352ac62008-09-29 15:18:18 -04001304/*
1305 * this is used by the defrag code to go through all the
1306 * leaves pointed to by a node and reallocate them so that
1307 * disk order is close to key order
1308 */
Chris Mason6702ed42007-08-07 16:15:09 -04001309int btrfs_realloc_node(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04001310 struct btrfs_root *root, struct extent_buffer *parent,
Chris Masona6b6e752007-10-15 16:22:39 -04001311 int start_slot, int cache_only, u64 *last_ret,
1312 struct btrfs_key *progress)
Chris Mason6702ed42007-08-07 16:15:09 -04001313{
Chris Mason6b800532007-10-15 16:17:34 -04001314 struct extent_buffer *cur;
Chris Mason6702ed42007-08-07 16:15:09 -04001315 u64 blocknr;
Chris Masonca7a79a2008-05-12 12:59:19 -04001316 u64 gen;
Chris Masone9d0b132007-08-10 14:06:19 -04001317 u64 search_start = *last_ret;
1318 u64 last_block = 0;
Chris Mason6702ed42007-08-07 16:15:09 -04001319 u64 other;
1320 u32 parent_nritems;
Chris Mason6702ed42007-08-07 16:15:09 -04001321 int end_slot;
1322 int i;
1323 int err = 0;
Chris Masonf2183bd2007-08-10 14:42:37 -04001324 int parent_level;
Chris Mason6b800532007-10-15 16:17:34 -04001325 int uptodate;
1326 u32 blocksize;
Chris Mason081e9572007-11-06 10:26:24 -05001327 int progress_passed = 0;
1328 struct btrfs_disk_key disk_key;
Chris Mason6702ed42007-08-07 16:15:09 -04001329
Chris Mason5708b952007-10-25 15:43:18 -04001330 parent_level = btrfs_header_level(parent);
1331 if (cache_only && parent_level != 1)
1332 return 0;
1333
Chris Masond3977122009-01-05 21:25:51 -05001334 if (trans->transaction != root->fs_info->running_transaction)
Chris Mason6702ed42007-08-07 16:15:09 -04001335 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05001336 if (trans->transid != root->fs_info->generation)
Chris Mason6702ed42007-08-07 16:15:09 -04001337 WARN_ON(1);
Chris Mason86479a02007-09-10 19:58:16 -04001338
Chris Mason6b800532007-10-15 16:17:34 -04001339 parent_nritems = btrfs_header_nritems(parent);
Chris Mason6b800532007-10-15 16:17:34 -04001340 blocksize = btrfs_level_size(root, parent_level - 1);
Chris Mason6702ed42007-08-07 16:15:09 -04001341 end_slot = parent_nritems;
1342
1343 if (parent_nritems == 1)
1344 return 0;
1345
Chris Masonb4ce94d2009-02-04 09:25:08 -05001346 btrfs_set_lock_blocking(parent);
1347
Chris Mason6702ed42007-08-07 16:15:09 -04001348 for (i = start_slot; i < end_slot; i++) {
1349 int close = 1;
Chris Masona6b6e752007-10-15 16:22:39 -04001350
Chris Mason081e9572007-11-06 10:26:24 -05001351 btrfs_node_key(parent, &disk_key, i);
1352 if (!progress_passed && comp_keys(&disk_key, progress) < 0)
1353 continue;
1354
1355 progress_passed = 1;
Chris Mason6b800532007-10-15 16:17:34 -04001356 blocknr = btrfs_node_blockptr(parent, i);
Chris Masonca7a79a2008-05-12 12:59:19 -04001357 gen = btrfs_node_ptr_generation(parent, i);
Chris Masone9d0b132007-08-10 14:06:19 -04001358 if (last_block == 0)
1359 last_block = blocknr;
Chris Mason5708b952007-10-25 15:43:18 -04001360
Chris Mason6702ed42007-08-07 16:15:09 -04001361 if (i > 0) {
Chris Mason6b800532007-10-15 16:17:34 -04001362 other = btrfs_node_blockptr(parent, i - 1);
1363 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001364 }
Chris Mason0ef3e662008-05-24 14:04:53 -04001365 if (!close && i < end_slot - 2) {
Chris Mason6b800532007-10-15 16:17:34 -04001366 other = btrfs_node_blockptr(parent, i + 1);
1367 close = close_blocks(blocknr, other, blocksize);
Chris Mason6702ed42007-08-07 16:15:09 -04001368 }
Chris Masone9d0b132007-08-10 14:06:19 -04001369 if (close) {
1370 last_block = blocknr;
Chris Mason6702ed42007-08-07 16:15:09 -04001371 continue;
Chris Masone9d0b132007-08-10 14:06:19 -04001372 }
Chris Mason6702ed42007-08-07 16:15:09 -04001373
Chris Mason6b800532007-10-15 16:17:34 -04001374 cur = btrfs_find_tree_block(root, blocknr, blocksize);
1375 if (cur)
Chris Masonb9fab912012-05-06 07:23:47 -04001376 uptodate = btrfs_buffer_uptodate(cur, gen, 0);
Chris Mason6b800532007-10-15 16:17:34 -04001377 else
1378 uptodate = 0;
Chris Mason5708b952007-10-25 15:43:18 -04001379 if (!cur || !uptodate) {
Chris Mason6702ed42007-08-07 16:15:09 -04001380 if (cache_only) {
Chris Mason6b800532007-10-15 16:17:34 -04001381 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001382 continue;
1383 }
Chris Mason6b800532007-10-15 16:17:34 -04001384 if (!cur) {
1385 cur = read_tree_block(root, blocknr,
Chris Masonca7a79a2008-05-12 12:59:19 -04001386 blocksize, gen);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00001387 if (!cur)
1388 return -EIO;
Chris Mason6b800532007-10-15 16:17:34 -04001389 } else if (!uptodate) {
Chris Masonca7a79a2008-05-12 12:59:19 -04001390 btrfs_read_buffer(cur, gen);
Chris Masonf2183bd2007-08-10 14:42:37 -04001391 }
Chris Mason6702ed42007-08-07 16:15:09 -04001392 }
Chris Masone9d0b132007-08-10 14:06:19 -04001393 if (search_start == 0)
Chris Mason6b800532007-10-15 16:17:34 -04001394 search_start = last_block;
Chris Masone9d0b132007-08-10 14:06:19 -04001395
Chris Masone7a84562008-06-25 16:01:31 -04001396 btrfs_tree_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001397 btrfs_set_lock_blocking(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001398 err = __btrfs_cow_block(trans, root, cur, parent, i,
Chris Masone7a84562008-06-25 16:01:31 -04001399 &cur, search_start,
Chris Mason6b800532007-10-15 16:17:34 -04001400 min(16 * blocksize,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001401 (end_slot - i) * blocksize));
Yan252c38f2007-08-29 09:11:44 -04001402 if (err) {
Chris Masone7a84562008-06-25 16:01:31 -04001403 btrfs_tree_unlock(cur);
Chris Mason6b800532007-10-15 16:17:34 -04001404 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001405 break;
Yan252c38f2007-08-29 09:11:44 -04001406 }
Chris Masone7a84562008-06-25 16:01:31 -04001407 search_start = cur->start;
1408 last_block = cur->start;
Chris Masonf2183bd2007-08-10 14:42:37 -04001409 *last_ret = search_start;
Chris Masone7a84562008-06-25 16:01:31 -04001410 btrfs_tree_unlock(cur);
1411 free_extent_buffer(cur);
Chris Mason6702ed42007-08-07 16:15:09 -04001412 }
1413 return err;
1414}
1415
Chris Mason74123bd2007-02-02 11:05:29 -05001416/*
1417 * The leaf data grows from end-to-front in the node.
1418 * this returns the address of the start of the last item,
1419 * which is the stop of the leaf data stack
1420 */
Chris Mason123abc82007-03-14 14:14:43 -04001421static inline unsigned int leaf_data_end(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001422 struct extent_buffer *leaf)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001423{
Chris Mason5f39d392007-10-15 16:14:19 -04001424 u32 nr = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001425 if (nr == 0)
Chris Mason123abc82007-03-14 14:14:43 -04001426 return BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04001427 return btrfs_item_offset_nr(leaf, nr - 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001428}
1429
Chris Masonaa5d6be2007-02-28 16:35:06 -05001430
Chris Mason74123bd2007-02-02 11:05:29 -05001431/*
Chris Mason5f39d392007-10-15 16:14:19 -04001432 * search for key in the extent_buffer. The items start at offset p,
1433 * and they are item_size apart. There are 'max' items in p.
1434 *
Chris Mason74123bd2007-02-02 11:05:29 -05001435 * the slot in the array is returned via slot, and it points to
1436 * the place where you would insert key if it is not found in
1437 * the array.
1438 *
1439 * slot may point to max if the key is bigger than all of the keys
1440 */
Chris Masone02119d2008-09-05 16:13:11 -04001441static noinline int generic_bin_search(struct extent_buffer *eb,
1442 unsigned long p,
1443 int item_size, struct btrfs_key *key,
1444 int max, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001445{
1446 int low = 0;
1447 int high = max;
1448 int mid;
1449 int ret;
Chris Mason479965d2007-10-15 16:14:27 -04001450 struct btrfs_disk_key *tmp = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001451 struct btrfs_disk_key unaligned;
1452 unsigned long offset;
Chris Mason5f39d392007-10-15 16:14:19 -04001453 char *kaddr = NULL;
1454 unsigned long map_start = 0;
1455 unsigned long map_len = 0;
Chris Mason479965d2007-10-15 16:14:27 -04001456 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05001457
Chris Masond3977122009-01-05 21:25:51 -05001458 while (low < high) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05001459 mid = (low + high) / 2;
Chris Mason5f39d392007-10-15 16:14:19 -04001460 offset = p + mid * item_size;
1461
Chris Masona6591712011-07-19 12:04:14 -04001462 if (!kaddr || offset < map_start ||
Chris Mason5f39d392007-10-15 16:14:19 -04001463 (offset + sizeof(struct btrfs_disk_key)) >
1464 map_start + map_len) {
Chris Mason934d3752008-12-08 16:43:10 -05001465
1466 err = map_private_extent_buffer(eb, offset,
Chris Mason479965d2007-10-15 16:14:27 -04001467 sizeof(struct btrfs_disk_key),
Chris Masona6591712011-07-19 12:04:14 -04001468 &kaddr, &map_start, &map_len);
Chris Mason5f39d392007-10-15 16:14:19 -04001469
Chris Mason479965d2007-10-15 16:14:27 -04001470 if (!err) {
1471 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1472 map_start);
1473 } else {
1474 read_extent_buffer(eb, &unaligned,
1475 offset, sizeof(unaligned));
1476 tmp = &unaligned;
1477 }
1478
Chris Mason5f39d392007-10-15 16:14:19 -04001479 } else {
1480 tmp = (struct btrfs_disk_key *)(kaddr + offset -
1481 map_start);
1482 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05001483 ret = comp_keys(tmp, key);
1484
1485 if (ret < 0)
1486 low = mid + 1;
1487 else if (ret > 0)
1488 high = mid;
1489 else {
1490 *slot = mid;
1491 return 0;
1492 }
1493 }
1494 *slot = low;
1495 return 1;
1496}
1497
Chris Mason97571fd2007-02-24 13:39:08 -05001498/*
1499 * simple bin_search frontend that does the right thing for
1500 * leaves vs nodes
1501 */
Chris Mason5f39d392007-10-15 16:14:19 -04001502static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1503 int level, int *slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05001504{
Chris Mason5f39d392007-10-15 16:14:19 -04001505 if (level == 0) {
1506 return generic_bin_search(eb,
1507 offsetof(struct btrfs_leaf, items),
Chris Mason0783fcf2007-03-12 20:12:07 -04001508 sizeof(struct btrfs_item),
Chris Mason5f39d392007-10-15 16:14:19 -04001509 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001510 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001511 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001512 return generic_bin_search(eb,
1513 offsetof(struct btrfs_node, ptrs),
Chris Mason123abc82007-03-14 14:14:43 -04001514 sizeof(struct btrfs_key_ptr),
Chris Mason5f39d392007-10-15 16:14:19 -04001515 key, btrfs_header_nritems(eb),
Chris Mason7518a232007-03-12 12:01:18 -04001516 slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05001517 }
1518 return -1;
1519}
1520
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001521int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
1522 int level, int *slot)
1523{
1524 return bin_search(eb, key, level, slot);
1525}
1526
Yan, Zhengf0486c62010-05-16 10:46:25 -04001527static void root_add_used(struct btrfs_root *root, u32 size)
1528{
1529 spin_lock(&root->accounting_lock);
1530 btrfs_set_root_used(&root->root_item,
1531 btrfs_root_used(&root->root_item) + size);
1532 spin_unlock(&root->accounting_lock);
1533}
1534
1535static void root_sub_used(struct btrfs_root *root, u32 size)
1536{
1537 spin_lock(&root->accounting_lock);
1538 btrfs_set_root_used(&root->root_item,
1539 btrfs_root_used(&root->root_item) - size);
1540 spin_unlock(&root->accounting_lock);
1541}
1542
Chris Masond352ac62008-09-29 15:18:18 -04001543/* given a node and slot number, this reads the blocks it points to. The
1544 * extent buffer is returned with a reference taken (but unlocked).
1545 * NULL is returned on error.
1546 */
Chris Masone02119d2008-09-05 16:13:11 -04001547static noinline struct extent_buffer *read_node_slot(struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -04001548 struct extent_buffer *parent, int slot)
Chris Masonbb803952007-03-01 12:04:21 -05001549{
Chris Masonca7a79a2008-05-12 12:59:19 -04001550 int level = btrfs_header_level(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001551 if (slot < 0)
1552 return NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001553 if (slot >= btrfs_header_nritems(parent))
Chris Masonbb803952007-03-01 12:04:21 -05001554 return NULL;
Chris Masonca7a79a2008-05-12 12:59:19 -04001555
1556 BUG_ON(level == 0);
1557
Chris Masondb945352007-10-15 16:15:53 -04001558 return read_tree_block(root, btrfs_node_blockptr(parent, slot),
Chris Masonca7a79a2008-05-12 12:59:19 -04001559 btrfs_level_size(root, level - 1),
1560 btrfs_node_ptr_generation(parent, slot));
Chris Masonbb803952007-03-01 12:04:21 -05001561}
1562
Chris Masond352ac62008-09-29 15:18:18 -04001563/*
1564 * node level balancing, used to make sure nodes are in proper order for
1565 * item deletion. We balance from the top down, so we have to make sure
1566 * that a deletion won't leave an node completely empty later on.
1567 */
Chris Masone02119d2008-09-05 16:13:11 -04001568static noinline int balance_level(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001569 struct btrfs_root *root,
1570 struct btrfs_path *path, int level)
Chris Masonbb803952007-03-01 12:04:21 -05001571{
Chris Mason5f39d392007-10-15 16:14:19 -04001572 struct extent_buffer *right = NULL;
1573 struct extent_buffer *mid;
1574 struct extent_buffer *left = NULL;
1575 struct extent_buffer *parent = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001576 int ret = 0;
1577 int wret;
1578 int pslot;
Chris Masonbb803952007-03-01 12:04:21 -05001579 int orig_slot = path->slots[level];
Chris Mason79f95c82007-03-01 15:16:26 -05001580 u64 orig_ptr;
Chris Masonbb803952007-03-01 12:04:21 -05001581
1582 if (level == 0)
1583 return 0;
1584
Chris Mason5f39d392007-10-15 16:14:19 -04001585 mid = path->nodes[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05001586
Chris Masonbd681512011-07-16 15:23:14 -04001587 WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
1588 path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
Chris Mason7bb86312007-12-11 09:25:06 -05001589 WARN_ON(btrfs_header_generation(mid) != trans->transid);
1590
Chris Mason1d4f8a02007-03-13 09:28:32 -04001591 orig_ptr = btrfs_node_blockptr(mid, orig_slot);
Chris Mason79f95c82007-03-01 15:16:26 -05001592
Li Zefana05a9bb2011-09-06 16:55:34 +08001593 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001594 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001595 pslot = path->slots[level + 1];
1596 }
Chris Masonbb803952007-03-01 12:04:21 -05001597
Chris Mason40689472007-03-17 14:29:23 -04001598 /*
1599 * deal with the case where there is only one pointer in the root
1600 * by promoting the node below to a root
1601 */
Chris Mason5f39d392007-10-15 16:14:19 -04001602 if (!parent) {
1603 struct extent_buffer *child;
Chris Masonbb803952007-03-01 12:04:21 -05001604
Chris Mason5f39d392007-10-15 16:14:19 -04001605 if (btrfs_header_nritems(mid) != 1)
Chris Masonbb803952007-03-01 12:04:21 -05001606 return 0;
1607
1608 /* promote the child to a root */
Chris Mason5f39d392007-10-15 16:14:19 -04001609 child = read_node_slot(root, mid, 0);
Mark Fasheh305a26a2011-09-01 11:27:57 -07001610 if (!child) {
1611 ret = -EROFS;
1612 btrfs_std_error(root->fs_info, ret);
1613 goto enospc;
1614 }
1615
Chris Mason925baed2008-06-25 16:01:30 -04001616 btrfs_tree_lock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001617 btrfs_set_lock_blocking(child);
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001618 ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001619 if (ret) {
1620 btrfs_tree_unlock(child);
1621 free_extent_buffer(child);
1622 goto enospc;
1623 }
Yan2f375ab2008-02-01 14:58:07 -05001624
Jan Schmidtf2304752012-05-26 11:43:17 +02001625 tree_mod_log_set_root_pointer(root, child);
Chris Mason240f62c2011-03-23 14:54:42 -04001626 rcu_assign_pointer(root->node, child);
Chris Mason925baed2008-06-25 16:01:30 -04001627
Chris Mason0b86a832008-03-24 15:01:56 -04001628 add_root_to_dirty_list(root);
Chris Mason925baed2008-06-25 16:01:30 -04001629 btrfs_tree_unlock(child);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001630
Chris Mason925baed2008-06-25 16:01:30 -04001631 path->locks[level] = 0;
Chris Masonbb803952007-03-01 12:04:21 -05001632 path->nodes[level] = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001633 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001634 btrfs_tree_unlock(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001635 /* once for the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001636 free_extent_buffer(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001637
1638 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001639 btrfs_free_tree_block(trans, root, mid, 0, 1);
Chris Masonbb803952007-03-01 12:04:21 -05001640 /* once for the root ptr */
Josef Bacik3083ee22012-03-09 16:01:49 -05001641 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001642 return 0;
Chris Masonbb803952007-03-01 12:04:21 -05001643 }
Chris Mason5f39d392007-10-15 16:14:19 -04001644 if (btrfs_header_nritems(mid) >
Chris Mason123abc82007-03-14 14:14:43 -04001645 BTRFS_NODEPTRS_PER_BLOCK(root) / 4)
Chris Masonbb803952007-03-01 12:04:21 -05001646 return 0;
1647
Andi Kleen559af822010-10-29 15:14:37 -04001648 btrfs_header_nritems(mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001649
Chris Mason5f39d392007-10-15 16:14:19 -04001650 left = read_node_slot(root, parent, pslot - 1);
1651 if (left) {
Chris Mason925baed2008-06-25 16:01:30 -04001652 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001653 btrfs_set_lock_blocking(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001654 wret = btrfs_cow_block(trans, root, left,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001655 parent, pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001656 if (wret) {
1657 ret = wret;
1658 goto enospc;
1659 }
Chris Mason2cc58cf2007-08-27 16:49:44 -04001660 }
Chris Mason5f39d392007-10-15 16:14:19 -04001661 right = read_node_slot(root, parent, pslot + 1);
1662 if (right) {
Chris Mason925baed2008-06-25 16:01:30 -04001663 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001664 btrfs_set_lock_blocking(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001665 wret = btrfs_cow_block(trans, root, right,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001666 parent, pslot + 1, &right);
Chris Mason2cc58cf2007-08-27 16:49:44 -04001667 if (wret) {
1668 ret = wret;
1669 goto enospc;
1670 }
1671 }
1672
1673 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001674 if (left) {
1675 orig_slot += btrfs_header_nritems(left);
Chris Masonbce4eae2008-04-24 14:42:46 -04001676 wret = push_node_left(trans, root, left, mid, 1);
Chris Mason79f95c82007-03-01 15:16:26 -05001677 if (wret < 0)
1678 ret = wret;
Andi Kleen559af822010-10-29 15:14:37 -04001679 btrfs_header_nritems(mid);
Chris Masonbb803952007-03-01 12:04:21 -05001680 }
Chris Mason79f95c82007-03-01 15:16:26 -05001681
1682 /*
1683 * then try to empty the right most buffer into the middle
1684 */
Chris Mason5f39d392007-10-15 16:14:19 -04001685 if (right) {
Chris Mason971a1f62008-04-24 10:54:32 -04001686 wret = push_node_left(trans, root, mid, right, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001687 if (wret < 0 && wret != -ENOSPC)
Chris Mason79f95c82007-03-01 15:16:26 -05001688 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04001689 if (btrfs_header_nritems(right) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001690 clean_tree_block(trans, root, right);
Chris Mason925baed2008-06-25 16:01:30 -04001691 btrfs_tree_unlock(right);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001692 del_ptr(trans, root, path, level + 1, pslot + 1, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001693 root_sub_used(root, right->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001694 btrfs_free_tree_block(trans, root, right, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001695 free_extent_buffer_stale(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001696 right = NULL;
Chris Masonbb803952007-03-01 12:04:21 -05001697 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001698 struct btrfs_disk_key right_key;
1699 btrfs_node_key(right, &right_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001700 tree_mod_log_set_node_key(root->fs_info, parent,
1701 &right_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001702 btrfs_set_node_key(parent, &right_key, pslot + 1);
1703 btrfs_mark_buffer_dirty(parent);
Chris Masonbb803952007-03-01 12:04:21 -05001704 }
1705 }
Chris Mason5f39d392007-10-15 16:14:19 -04001706 if (btrfs_header_nritems(mid) == 1) {
Chris Mason79f95c82007-03-01 15:16:26 -05001707 /*
1708 * we're not allowed to leave a node with one item in the
1709 * tree during a delete. A deletion from lower in the tree
1710 * could try to delete the only pointer in this node.
1711 * So, pull some keys from the left.
1712 * There has to be a left pointer at this point because
1713 * otherwise we would have pulled some pointers from the
1714 * right
1715 */
Mark Fasheh305a26a2011-09-01 11:27:57 -07001716 if (!left) {
1717 ret = -EROFS;
1718 btrfs_std_error(root->fs_info, ret);
1719 goto enospc;
1720 }
Chris Mason5f39d392007-10-15 16:14:19 -04001721 wret = balance_node_right(trans, root, mid, left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001722 if (wret < 0) {
Chris Mason79f95c82007-03-01 15:16:26 -05001723 ret = wret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001724 goto enospc;
1725 }
Chris Masonbce4eae2008-04-24 14:42:46 -04001726 if (wret == 1) {
1727 wret = push_node_left(trans, root, left, mid, 1);
1728 if (wret < 0)
1729 ret = wret;
1730 }
Chris Mason79f95c82007-03-01 15:16:26 -05001731 BUG_ON(wret == 1);
1732 }
Chris Mason5f39d392007-10-15 16:14:19 -04001733 if (btrfs_header_nritems(mid) == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001734 clean_tree_block(trans, root, mid);
Chris Mason925baed2008-06-25 16:01:30 -04001735 btrfs_tree_unlock(mid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02001736 del_ptr(trans, root, path, level + 1, pslot, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001737 root_sub_used(root, mid->len);
Jan Schmidt5581a512012-05-16 17:04:52 +02001738 btrfs_free_tree_block(trans, root, mid, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05001739 free_extent_buffer_stale(mid);
Yan, Zhengf0486c62010-05-16 10:46:25 -04001740 mid = NULL;
Chris Mason79f95c82007-03-01 15:16:26 -05001741 } else {
1742 /* update the parent key to reflect our changes */
Chris Mason5f39d392007-10-15 16:14:19 -04001743 struct btrfs_disk_key mid_key;
1744 btrfs_node_key(mid, &mid_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001745 tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
1746 pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001747 btrfs_set_node_key(parent, &mid_key, pslot);
1748 btrfs_mark_buffer_dirty(parent);
Chris Mason79f95c82007-03-01 15:16:26 -05001749 }
Chris Masonbb803952007-03-01 12:04:21 -05001750
Chris Mason79f95c82007-03-01 15:16:26 -05001751 /* update the path */
Chris Mason5f39d392007-10-15 16:14:19 -04001752 if (left) {
1753 if (btrfs_header_nritems(left) > orig_slot) {
1754 extent_buffer_get(left);
Chris Mason925baed2008-06-25 16:01:30 -04001755 /* left was locked after cow */
Chris Mason5f39d392007-10-15 16:14:19 -04001756 path->nodes[level] = left;
Chris Masonbb803952007-03-01 12:04:21 -05001757 path->slots[level + 1] -= 1;
1758 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001759 if (mid) {
1760 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001761 free_extent_buffer(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001762 }
Chris Masonbb803952007-03-01 12:04:21 -05001763 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001764 orig_slot -= btrfs_header_nritems(left);
Chris Masonbb803952007-03-01 12:04:21 -05001765 path->slots[level] = orig_slot;
1766 }
1767 }
Chris Mason79f95c82007-03-01 15:16:26 -05001768 /* double check we haven't messed things up */
Chris Masone20d96d2007-03-22 12:13:20 -04001769 if (orig_ptr !=
Chris Mason5f39d392007-10-15 16:14:19 -04001770 btrfs_node_blockptr(path->nodes[level], path->slots[level]))
Chris Mason79f95c82007-03-01 15:16:26 -05001771 BUG();
Chris Mason54aa1f42007-06-22 14:16:25 -04001772enospc:
Chris Mason925baed2008-06-25 16:01:30 -04001773 if (right) {
1774 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001775 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04001776 }
1777 if (left) {
1778 if (path->nodes[level] != left)
1779 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001780 free_extent_buffer(left);
Chris Mason925baed2008-06-25 16:01:30 -04001781 }
Chris Masonbb803952007-03-01 12:04:21 -05001782 return ret;
1783}
1784
Chris Masond352ac62008-09-29 15:18:18 -04001785/* Node balancing for insertion. Here we only split or push nodes around
1786 * when they are completely full. This is also done top down, so we
1787 * have to be pessimistic.
1788 */
Chris Masond3977122009-01-05 21:25:51 -05001789static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05001790 struct btrfs_root *root,
1791 struct btrfs_path *path, int level)
Chris Masone66f7092007-04-20 13:16:02 -04001792{
Chris Mason5f39d392007-10-15 16:14:19 -04001793 struct extent_buffer *right = NULL;
1794 struct extent_buffer *mid;
1795 struct extent_buffer *left = NULL;
1796 struct extent_buffer *parent = NULL;
Chris Masone66f7092007-04-20 13:16:02 -04001797 int ret = 0;
1798 int wret;
1799 int pslot;
1800 int orig_slot = path->slots[level];
Chris Masone66f7092007-04-20 13:16:02 -04001801
1802 if (level == 0)
1803 return 1;
1804
Chris Mason5f39d392007-10-15 16:14:19 -04001805 mid = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05001806 WARN_ON(btrfs_header_generation(mid) != trans->transid);
Chris Masone66f7092007-04-20 13:16:02 -04001807
Li Zefana05a9bb2011-09-06 16:55:34 +08001808 if (level < BTRFS_MAX_LEVEL - 1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001809 parent = path->nodes[level + 1];
Li Zefana05a9bb2011-09-06 16:55:34 +08001810 pslot = path->slots[level + 1];
1811 }
Chris Masone66f7092007-04-20 13:16:02 -04001812
Chris Mason5f39d392007-10-15 16:14:19 -04001813 if (!parent)
Chris Masone66f7092007-04-20 13:16:02 -04001814 return 1;
Chris Masone66f7092007-04-20 13:16:02 -04001815
Chris Mason5f39d392007-10-15 16:14:19 -04001816 left = read_node_slot(root, parent, pslot - 1);
Chris Masone66f7092007-04-20 13:16:02 -04001817
1818 /* first, try to make some room in the middle buffer */
Chris Mason5f39d392007-10-15 16:14:19 -04001819 if (left) {
Chris Masone66f7092007-04-20 13:16:02 -04001820 u32 left_nr;
Chris Mason925baed2008-06-25 16:01:30 -04001821
1822 btrfs_tree_lock(left);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001823 btrfs_set_lock_blocking(left);
1824
Chris Mason5f39d392007-10-15 16:14:19 -04001825 left_nr = btrfs_header_nritems(left);
Chris Mason33ade1f2007-04-20 13:48:57 -04001826 if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1827 wret = 1;
1828 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001829 ret = btrfs_cow_block(trans, root, left, parent,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001830 pslot - 1, &left);
Chris Mason54aa1f42007-06-22 14:16:25 -04001831 if (ret)
1832 wret = 1;
1833 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001834 wret = push_node_left(trans, root,
Chris Mason971a1f62008-04-24 10:54:32 -04001835 left, mid, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001836 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001837 }
Chris Masone66f7092007-04-20 13:16:02 -04001838 if (wret < 0)
1839 ret = wret;
1840 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001841 struct btrfs_disk_key disk_key;
Chris Masone66f7092007-04-20 13:16:02 -04001842 orig_slot += left_nr;
Chris Mason5f39d392007-10-15 16:14:19 -04001843 btrfs_node_key(mid, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001844 tree_mod_log_set_node_key(root->fs_info, parent,
1845 &disk_key, pslot, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001846 btrfs_set_node_key(parent, &disk_key, pslot);
1847 btrfs_mark_buffer_dirty(parent);
1848 if (btrfs_header_nritems(left) > orig_slot) {
1849 path->nodes[level] = left;
Chris Masone66f7092007-04-20 13:16:02 -04001850 path->slots[level + 1] -= 1;
1851 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001852 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001853 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001854 } else {
1855 orig_slot -=
Chris Mason5f39d392007-10-15 16:14:19 -04001856 btrfs_header_nritems(left);
Chris Masone66f7092007-04-20 13:16:02 -04001857 path->slots[level] = orig_slot;
Chris Mason925baed2008-06-25 16:01:30 -04001858 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001859 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001860 }
Chris Masone66f7092007-04-20 13:16:02 -04001861 return 0;
1862 }
Chris Mason925baed2008-06-25 16:01:30 -04001863 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04001864 free_extent_buffer(left);
Chris Masone66f7092007-04-20 13:16:02 -04001865 }
Chris Mason925baed2008-06-25 16:01:30 -04001866 right = read_node_slot(root, parent, pslot + 1);
Chris Masone66f7092007-04-20 13:16:02 -04001867
1868 /*
1869 * then try to empty the right most buffer into the middle
1870 */
Chris Mason5f39d392007-10-15 16:14:19 -04001871 if (right) {
Chris Mason33ade1f2007-04-20 13:48:57 -04001872 u32 right_nr;
Chris Masonb4ce94d2009-02-04 09:25:08 -05001873
Chris Mason925baed2008-06-25 16:01:30 -04001874 btrfs_tree_lock(right);
Chris Masonb4ce94d2009-02-04 09:25:08 -05001875 btrfs_set_lock_blocking(right);
1876
Chris Mason5f39d392007-10-15 16:14:19 -04001877 right_nr = btrfs_header_nritems(right);
Chris Mason33ade1f2007-04-20 13:48:57 -04001878 if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
1879 wret = 1;
1880 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04001881 ret = btrfs_cow_block(trans, root, right,
1882 parent, pslot + 1,
Chris Mason9fa8cfe2009-03-13 10:24:59 -04001883 &right);
Chris Mason54aa1f42007-06-22 14:16:25 -04001884 if (ret)
1885 wret = 1;
1886 else {
Chris Mason54aa1f42007-06-22 14:16:25 -04001887 wret = balance_node_right(trans, root,
Chris Mason5f39d392007-10-15 16:14:19 -04001888 right, mid);
Chris Mason54aa1f42007-06-22 14:16:25 -04001889 }
Chris Mason33ade1f2007-04-20 13:48:57 -04001890 }
Chris Masone66f7092007-04-20 13:16:02 -04001891 if (wret < 0)
1892 ret = wret;
1893 if (wret == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001894 struct btrfs_disk_key disk_key;
1895
1896 btrfs_node_key(right, &disk_key, 0);
Jan Schmidtf2304752012-05-26 11:43:17 +02001897 tree_mod_log_set_node_key(root->fs_info, parent,
1898 &disk_key, pslot + 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001899 btrfs_set_node_key(parent, &disk_key, pslot + 1);
1900 btrfs_mark_buffer_dirty(parent);
1901
1902 if (btrfs_header_nritems(mid) <= orig_slot) {
1903 path->nodes[level] = right;
Chris Masone66f7092007-04-20 13:16:02 -04001904 path->slots[level + 1] += 1;
1905 path->slots[level] = orig_slot -
Chris Mason5f39d392007-10-15 16:14:19 -04001906 btrfs_header_nritems(mid);
Chris Mason925baed2008-06-25 16:01:30 -04001907 btrfs_tree_unlock(mid);
Chris Mason5f39d392007-10-15 16:14:19 -04001908 free_extent_buffer(mid);
Chris Masone66f7092007-04-20 13:16:02 -04001909 } else {
Chris Mason925baed2008-06-25 16:01:30 -04001910 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001911 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001912 }
Chris Masone66f7092007-04-20 13:16:02 -04001913 return 0;
1914 }
Chris Mason925baed2008-06-25 16:01:30 -04001915 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04001916 free_extent_buffer(right);
Chris Masone66f7092007-04-20 13:16:02 -04001917 }
Chris Masone66f7092007-04-20 13:16:02 -04001918 return 1;
1919}
1920
Chris Mason74123bd2007-02-02 11:05:29 -05001921/*
Chris Masond352ac62008-09-29 15:18:18 -04001922 * readahead one full node of leaves, finding things that are close
1923 * to the block in 'slot', and triggering ra on them.
Chris Mason3c69fae2007-08-07 15:52:22 -04001924 */
Chris Masonc8c42862009-04-03 10:14:18 -04001925static void reada_for_search(struct btrfs_root *root,
1926 struct btrfs_path *path,
1927 int level, int slot, u64 objectid)
Chris Mason3c69fae2007-08-07 15:52:22 -04001928{
Chris Mason5f39d392007-10-15 16:14:19 -04001929 struct extent_buffer *node;
Chris Mason01f46652007-12-21 16:24:26 -05001930 struct btrfs_disk_key disk_key;
Chris Mason3c69fae2007-08-07 15:52:22 -04001931 u32 nritems;
Chris Mason3c69fae2007-08-07 15:52:22 -04001932 u64 search;
Chris Masona7175312009-01-22 09:23:10 -05001933 u64 target;
Chris Mason6b800532007-10-15 16:17:34 -04001934 u64 nread = 0;
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001935 u64 gen;
Chris Mason3c69fae2007-08-07 15:52:22 -04001936 int direction = path->reada;
Chris Mason5f39d392007-10-15 16:14:19 -04001937 struct extent_buffer *eb;
Chris Mason6b800532007-10-15 16:17:34 -04001938 u32 nr;
1939 u32 blocksize;
1940 u32 nscan = 0;
Chris Masondb945352007-10-15 16:15:53 -04001941
Chris Masona6b6e752007-10-15 16:22:39 -04001942 if (level != 1)
Chris Mason3c69fae2007-08-07 15:52:22 -04001943 return;
1944
Chris Mason6702ed42007-08-07 16:15:09 -04001945 if (!path->nodes[level])
1946 return;
1947
Chris Mason5f39d392007-10-15 16:14:19 -04001948 node = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04001949
Chris Mason3c69fae2007-08-07 15:52:22 -04001950 search = btrfs_node_blockptr(node, slot);
Chris Mason6b800532007-10-15 16:17:34 -04001951 blocksize = btrfs_level_size(root, level - 1);
1952 eb = btrfs_find_tree_block(root, search, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04001953 if (eb) {
1954 free_extent_buffer(eb);
Chris Mason3c69fae2007-08-07 15:52:22 -04001955 return;
1956 }
1957
Chris Masona7175312009-01-22 09:23:10 -05001958 target = search;
Chris Mason6b800532007-10-15 16:17:34 -04001959
Chris Mason5f39d392007-10-15 16:14:19 -04001960 nritems = btrfs_header_nritems(node);
Chris Mason6b800532007-10-15 16:17:34 -04001961 nr = slot;
Josef Bacik25b8b932011-06-08 14:36:54 -04001962
Chris Masond3977122009-01-05 21:25:51 -05001963 while (1) {
Chris Mason6b800532007-10-15 16:17:34 -04001964 if (direction < 0) {
1965 if (nr == 0)
1966 break;
1967 nr--;
1968 } else if (direction > 0) {
1969 nr++;
1970 if (nr >= nritems)
1971 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001972 }
Chris Mason01f46652007-12-21 16:24:26 -05001973 if (path->reada < 0 && objectid) {
1974 btrfs_node_key(node, &disk_key, nr);
1975 if (btrfs_disk_key_objectid(&disk_key) != objectid)
1976 break;
1977 }
Chris Mason6b800532007-10-15 16:17:34 -04001978 search = btrfs_node_blockptr(node, nr);
Chris Masona7175312009-01-22 09:23:10 -05001979 if ((search <= target && target - search <= 65536) ||
1980 (search > target && search - target <= 65536)) {
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001981 gen = btrfs_node_ptr_generation(node, nr);
Josef Bacikcb25c2e2011-05-11 12:17:34 -04001982 readahead_tree_block(root, search, blocksize, gen);
Chris Mason6b800532007-10-15 16:17:34 -04001983 nread += blocksize;
1984 }
1985 nscan++;
Chris Masona7175312009-01-22 09:23:10 -05001986 if ((nread > 65536 || nscan > 32))
Chris Mason6b800532007-10-15 16:17:34 -04001987 break;
Chris Mason3c69fae2007-08-07 15:52:22 -04001988 }
1989}
Chris Mason925baed2008-06-25 16:01:30 -04001990
Chris Masond352ac62008-09-29 15:18:18 -04001991/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05001992 * returns -EAGAIN if it had to drop the path, or zero if everything was in
1993 * cache
1994 */
1995static noinline int reada_for_balance(struct btrfs_root *root,
1996 struct btrfs_path *path, int level)
1997{
1998 int slot;
1999 int nritems;
2000 struct extent_buffer *parent;
2001 struct extent_buffer *eb;
2002 u64 gen;
2003 u64 block1 = 0;
2004 u64 block2 = 0;
2005 int ret = 0;
2006 int blocksize;
2007
Chris Mason8c594ea2009-04-20 15:50:10 -04002008 parent = path->nodes[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002009 if (!parent)
2010 return 0;
2011
2012 nritems = btrfs_header_nritems(parent);
Chris Mason8c594ea2009-04-20 15:50:10 -04002013 slot = path->slots[level + 1];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002014 blocksize = btrfs_level_size(root, level);
2015
2016 if (slot > 0) {
2017 block1 = btrfs_node_blockptr(parent, slot - 1);
2018 gen = btrfs_node_ptr_generation(parent, slot - 1);
2019 eb = btrfs_find_tree_block(root, block1, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002020 /*
2021 * if we get -eagain from btrfs_buffer_uptodate, we
2022 * don't want to return eagain here. That will loop
2023 * forever
2024 */
2025 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002026 block1 = 0;
2027 free_extent_buffer(eb);
2028 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002029 if (slot + 1 < nritems) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002030 block2 = btrfs_node_blockptr(parent, slot + 1);
2031 gen = btrfs_node_ptr_generation(parent, slot + 1);
2032 eb = btrfs_find_tree_block(root, block2, blocksize);
Chris Masonb9fab912012-05-06 07:23:47 -04002033 if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002034 block2 = 0;
2035 free_extent_buffer(eb);
2036 }
2037 if (block1 || block2) {
2038 ret = -EAGAIN;
Chris Mason8c594ea2009-04-20 15:50:10 -04002039
2040 /* release the whole path */
David Sterbab3b4aa72011-04-21 01:20:15 +02002041 btrfs_release_path(path);
Chris Mason8c594ea2009-04-20 15:50:10 -04002042
2043 /* read the blocks */
Chris Masonb4ce94d2009-02-04 09:25:08 -05002044 if (block1)
2045 readahead_tree_block(root, block1, blocksize, 0);
2046 if (block2)
2047 readahead_tree_block(root, block2, blocksize, 0);
2048
2049 if (block1) {
2050 eb = read_tree_block(root, block1, blocksize, 0);
2051 free_extent_buffer(eb);
2052 }
Chris Mason8c594ea2009-04-20 15:50:10 -04002053 if (block2) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05002054 eb = read_tree_block(root, block2, blocksize, 0);
2055 free_extent_buffer(eb);
2056 }
2057 }
2058 return ret;
2059}
2060
2061
2062/*
Chris Masond3977122009-01-05 21:25:51 -05002063 * when we walk down the tree, it is usually safe to unlock the higher layers
2064 * in the tree. The exceptions are when our path goes through slot 0, because
2065 * operations on the tree might require changing key pointers higher up in the
2066 * tree.
Chris Masond352ac62008-09-29 15:18:18 -04002067 *
Chris Masond3977122009-01-05 21:25:51 -05002068 * callers might also have set path->keep_locks, which tells this code to keep
2069 * the lock if the path points to the last slot in the block. This is part of
2070 * walking through the tree, and selecting the next slot in the higher block.
Chris Masond352ac62008-09-29 15:18:18 -04002071 *
Chris Masond3977122009-01-05 21:25:51 -05002072 * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
2073 * if lowest_unlock is 1, level 0 won't be unlocked
Chris Masond352ac62008-09-29 15:18:18 -04002074 */
Chris Masone02119d2008-09-05 16:13:11 -04002075static noinline void unlock_up(struct btrfs_path *path, int level,
Chris Masonf7c79f32012-03-19 15:54:38 -04002076 int lowest_unlock, int min_write_lock_level,
2077 int *write_lock_level)
Chris Mason925baed2008-06-25 16:01:30 -04002078{
2079 int i;
2080 int skip_level = level;
Chris Mason051e1b92008-06-25 16:01:30 -04002081 int no_skips = 0;
Chris Mason925baed2008-06-25 16:01:30 -04002082 struct extent_buffer *t;
2083
2084 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2085 if (!path->nodes[i])
2086 break;
2087 if (!path->locks[i])
2088 break;
Chris Mason051e1b92008-06-25 16:01:30 -04002089 if (!no_skips && path->slots[i] == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002090 skip_level = i + 1;
2091 continue;
2092 }
Chris Mason051e1b92008-06-25 16:01:30 -04002093 if (!no_skips && path->keep_locks) {
Chris Mason925baed2008-06-25 16:01:30 -04002094 u32 nritems;
2095 t = path->nodes[i];
2096 nritems = btrfs_header_nritems(t);
Chris Mason051e1b92008-06-25 16:01:30 -04002097 if (nritems < 1 || path->slots[i] >= nritems - 1) {
Chris Mason925baed2008-06-25 16:01:30 -04002098 skip_level = i + 1;
2099 continue;
2100 }
2101 }
Chris Mason051e1b92008-06-25 16:01:30 -04002102 if (skip_level < i && i >= lowest_unlock)
2103 no_skips = 1;
2104
Chris Mason925baed2008-06-25 16:01:30 -04002105 t = path->nodes[i];
2106 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
Chris Masonbd681512011-07-16 15:23:14 -04002107 btrfs_tree_unlock_rw(t, path->locks[i]);
Chris Mason925baed2008-06-25 16:01:30 -04002108 path->locks[i] = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002109 if (write_lock_level &&
2110 i > min_write_lock_level &&
2111 i <= *write_lock_level) {
2112 *write_lock_level = i - 1;
2113 }
Chris Mason925baed2008-06-25 16:01:30 -04002114 }
2115 }
2116}
2117
Chris Mason3c69fae2007-08-07 15:52:22 -04002118/*
Chris Masonb4ce94d2009-02-04 09:25:08 -05002119 * This releases any locks held in the path starting at level and
2120 * going all the way up to the root.
2121 *
2122 * btrfs_search_slot will keep the lock held on higher nodes in a few
2123 * corner cases, such as COW of the block at slot zero in the node. This
2124 * ignores those rules, and it should only be called when there are no
2125 * more updates to be done higher up in the tree.
2126 */
2127noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
2128{
2129 int i;
2130
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002131 if (path->keep_locks)
Chris Masonb4ce94d2009-02-04 09:25:08 -05002132 return;
2133
2134 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2135 if (!path->nodes[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002136 continue;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002137 if (!path->locks[i])
Chris Mason12f4dac2009-02-04 09:31:42 -05002138 continue;
Chris Masonbd681512011-07-16 15:23:14 -04002139 btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002140 path->locks[i] = 0;
2141 }
2142}
2143
2144/*
Chris Masonc8c42862009-04-03 10:14:18 -04002145 * helper function for btrfs_search_slot. The goal is to find a block
2146 * in cache without setting the path to blocking. If we find the block
2147 * we return zero and the path is unchanged.
2148 *
2149 * If we can't find the block, we set the path blocking and do some
2150 * reada. -EAGAIN is returned and the search must be repeated.
2151 */
2152static int
2153read_block_for_search(struct btrfs_trans_handle *trans,
2154 struct btrfs_root *root, struct btrfs_path *p,
2155 struct extent_buffer **eb_ret, int level, int slot,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002156 struct btrfs_key *key, u64 time_seq)
Chris Masonc8c42862009-04-03 10:14:18 -04002157{
2158 u64 blocknr;
2159 u64 gen;
2160 u32 blocksize;
2161 struct extent_buffer *b = *eb_ret;
2162 struct extent_buffer *tmp;
Chris Mason76a05b32009-05-14 13:24:30 -04002163 int ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002164
2165 blocknr = btrfs_node_blockptr(b, slot);
2166 gen = btrfs_node_ptr_generation(b, slot);
2167 blocksize = btrfs_level_size(root, level - 1);
2168
2169 tmp = btrfs_find_tree_block(root, blocknr, blocksize);
Chris Masoncb449212010-10-24 11:01:27 -04002170 if (tmp) {
Chris Masonb9fab912012-05-06 07:23:47 -04002171 /* first we do an atomic uptodate check */
2172 if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
2173 if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002174 /*
2175 * we found an up to date block without
2176 * sleeping, return
2177 * right away
2178 */
2179 *eb_ret = tmp;
2180 return 0;
2181 }
2182 /* the pages were up to date, but we failed
2183 * the generation number check. Do a full
2184 * read for the generation number that is correct.
2185 * We must do this without dropping locks so
2186 * we can trust our generation number
2187 */
2188 free_extent_buffer(tmp);
Chris Masonbd681512011-07-16 15:23:14 -04002189 btrfs_set_path_blocking(p);
2190
Chris Masonb9fab912012-05-06 07:23:47 -04002191 /* now we're allowed to do a blocking uptodate check */
Chris Masoncb449212010-10-24 11:01:27 -04002192 tmp = read_tree_block(root, blocknr, blocksize, gen);
Chris Masonb9fab912012-05-06 07:23:47 -04002193 if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
Chris Masoncb449212010-10-24 11:01:27 -04002194 *eb_ret = tmp;
2195 return 0;
2196 }
2197 free_extent_buffer(tmp);
David Sterbab3b4aa72011-04-21 01:20:15 +02002198 btrfs_release_path(p);
Chris Masoncb449212010-10-24 11:01:27 -04002199 return -EIO;
2200 }
Chris Masonc8c42862009-04-03 10:14:18 -04002201 }
2202
2203 /*
2204 * reduce lock contention at high levels
2205 * of the btree by dropping locks before
Chris Mason76a05b32009-05-14 13:24:30 -04002206 * we read. Don't release the lock on the current
2207 * level because we need to walk this node to figure
2208 * out which blocks to read.
Chris Masonc8c42862009-04-03 10:14:18 -04002209 */
Chris Mason8c594ea2009-04-20 15:50:10 -04002210 btrfs_unlock_up_safe(p, level + 1);
2211 btrfs_set_path_blocking(p);
2212
Chris Masoncb449212010-10-24 11:01:27 -04002213 free_extent_buffer(tmp);
Chris Masonc8c42862009-04-03 10:14:18 -04002214 if (p->reada)
2215 reada_for_search(root, p, level, slot, key->objectid);
2216
David Sterbab3b4aa72011-04-21 01:20:15 +02002217 btrfs_release_path(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002218
2219 ret = -EAGAIN;
Yan, Zheng5bdd3532010-05-26 11:20:30 -04002220 tmp = read_tree_block(root, blocknr, blocksize, 0);
Chris Mason76a05b32009-05-14 13:24:30 -04002221 if (tmp) {
2222 /*
2223 * If the read above didn't mark this buffer up to date,
2224 * it will never end up being up to date. Set ret to EIO now
2225 * and give up so that our caller doesn't loop forever
2226 * on our EAGAINs.
2227 */
Chris Masonb9fab912012-05-06 07:23:47 -04002228 if (!btrfs_buffer_uptodate(tmp, 0, 0))
Chris Mason76a05b32009-05-14 13:24:30 -04002229 ret = -EIO;
Chris Masonc8c42862009-04-03 10:14:18 -04002230 free_extent_buffer(tmp);
Chris Mason76a05b32009-05-14 13:24:30 -04002231 }
2232 return ret;
Chris Masonc8c42862009-04-03 10:14:18 -04002233}
2234
2235/*
2236 * helper function for btrfs_search_slot. This does all of the checks
2237 * for node-level blocks and does any balancing required based on
2238 * the ins_len.
2239 *
2240 * If no extra work was required, zero is returned. If we had to
2241 * drop the path, -EAGAIN is returned and btrfs_search_slot must
2242 * start over
2243 */
2244static int
2245setup_nodes_for_search(struct btrfs_trans_handle *trans,
2246 struct btrfs_root *root, struct btrfs_path *p,
Chris Masonbd681512011-07-16 15:23:14 -04002247 struct extent_buffer *b, int level, int ins_len,
2248 int *write_lock_level)
Chris Masonc8c42862009-04-03 10:14:18 -04002249{
2250 int ret;
2251 if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
2252 BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
2253 int sret;
2254
Chris Masonbd681512011-07-16 15:23:14 -04002255 if (*write_lock_level < level + 1) {
2256 *write_lock_level = level + 1;
2257 btrfs_release_path(p);
2258 goto again;
2259 }
2260
Chris Masonc8c42862009-04-03 10:14:18 -04002261 sret = reada_for_balance(root, p, level);
2262 if (sret)
2263 goto again;
2264
2265 btrfs_set_path_blocking(p);
2266 sret = split_node(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002267 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002268
2269 BUG_ON(sret > 0);
2270 if (sret) {
2271 ret = sret;
2272 goto done;
2273 }
2274 b = p->nodes[level];
2275 } else if (ins_len < 0 && btrfs_header_nritems(b) <
Chris Masoncfbb9302009-05-18 10:41:58 -04002276 BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
Chris Masonc8c42862009-04-03 10:14:18 -04002277 int sret;
2278
Chris Masonbd681512011-07-16 15:23:14 -04002279 if (*write_lock_level < level + 1) {
2280 *write_lock_level = level + 1;
2281 btrfs_release_path(p);
2282 goto again;
2283 }
2284
Chris Masonc8c42862009-04-03 10:14:18 -04002285 sret = reada_for_balance(root, p, level);
2286 if (sret)
2287 goto again;
2288
2289 btrfs_set_path_blocking(p);
2290 sret = balance_level(trans, root, p, level);
Chris Masonbd681512011-07-16 15:23:14 -04002291 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonc8c42862009-04-03 10:14:18 -04002292
2293 if (sret) {
2294 ret = sret;
2295 goto done;
2296 }
2297 b = p->nodes[level];
2298 if (!b) {
David Sterbab3b4aa72011-04-21 01:20:15 +02002299 btrfs_release_path(p);
Chris Masonc8c42862009-04-03 10:14:18 -04002300 goto again;
2301 }
2302 BUG_ON(btrfs_header_nritems(b) == 1);
2303 }
2304 return 0;
2305
2306again:
2307 ret = -EAGAIN;
2308done:
2309 return ret;
2310}
2311
2312/*
Chris Mason74123bd2007-02-02 11:05:29 -05002313 * look for key in the tree. path is filled in with nodes along the way
2314 * if key is found, we return zero and you can find the item in the leaf
2315 * level of the path (level 0)
2316 *
2317 * If the key isn't found, the path points to the slot where it should
Chris Masonaa5d6be2007-02-28 16:35:06 -05002318 * be inserted, and 1 is returned. If there are other errors during the
2319 * search a negative error number is returned.
Chris Mason97571fd2007-02-24 13:39:08 -05002320 *
2321 * if ins_len > 0, nodes and leaves will be split as we walk down the
2322 * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
2323 * possible)
Chris Mason74123bd2007-02-02 11:05:29 -05002324 */
Chris Masone089f052007-03-16 16:20:31 -04002325int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
2326 *root, struct btrfs_key *key, struct btrfs_path *p, int
2327 ins_len, int cow)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002328{
Chris Mason5f39d392007-10-15 16:14:19 -04002329 struct extent_buffer *b;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002330 int slot;
2331 int ret;
Yan Zheng33c66f42009-07-22 09:59:00 -04002332 int err;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002333 int level;
Chris Mason925baed2008-06-25 16:01:30 -04002334 int lowest_unlock = 1;
Chris Masonbd681512011-07-16 15:23:14 -04002335 int root_lock;
2336 /* everything at write_lock_level or lower must be write locked */
2337 int write_lock_level = 0;
Chris Mason9f3a7422007-08-07 15:52:19 -04002338 u8 lowest_level = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04002339 int min_write_lock_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002340
Chris Mason6702ed42007-08-07 16:15:09 -04002341 lowest_level = p->lowest_level;
Chris Mason323ac952008-10-01 19:05:46 -04002342 WARN_ON(lowest_level && ins_len > 0);
Chris Mason22b0ebd2007-03-30 08:47:31 -04002343 WARN_ON(p->nodes[0] != NULL);
Josef Bacik25179202008-10-29 14:49:05 -04002344
Chris Masonbd681512011-07-16 15:23:14 -04002345 if (ins_len < 0) {
Chris Mason925baed2008-06-25 16:01:30 -04002346 lowest_unlock = 2;
Chris Mason65b51a02008-08-01 15:11:20 -04002347
Chris Masonbd681512011-07-16 15:23:14 -04002348 /* when we are removing items, we might have to go up to level
2349 * two as we update tree pointers Make sure we keep write
2350 * for those levels as well
2351 */
2352 write_lock_level = 2;
2353 } else if (ins_len > 0) {
2354 /*
2355 * for inserting items, make sure we have a write lock on
2356 * level 1 so we can update keys
2357 */
2358 write_lock_level = 1;
2359 }
2360
2361 if (!cow)
2362 write_lock_level = -1;
2363
2364 if (cow && (p->keep_locks || p->lowest_level))
2365 write_lock_level = BTRFS_MAX_LEVEL;
2366
Chris Masonf7c79f32012-03-19 15:54:38 -04002367 min_write_lock_level = write_lock_level;
2368
Chris Masonbb803952007-03-01 12:04:21 -05002369again:
Chris Masonbd681512011-07-16 15:23:14 -04002370 /*
2371 * we try very hard to do read locks on the root
2372 */
2373 root_lock = BTRFS_READ_LOCK;
2374 level = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002375 if (p->search_commit_root) {
Chris Masonbd681512011-07-16 15:23:14 -04002376 /*
2377 * the commit roots are read only
2378 * so we always do read locks
2379 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002380 b = root->commit_root;
2381 extent_buffer_get(b);
Chris Masonbd681512011-07-16 15:23:14 -04002382 level = btrfs_header_level(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002383 if (!p->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04002384 btrfs_tree_read_lock(b);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002385 } else {
Chris Masonbd681512011-07-16 15:23:14 -04002386 if (p->skip_locking) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002387 b = btrfs_root_node(root);
Chris Masonbd681512011-07-16 15:23:14 -04002388 level = btrfs_header_level(b);
2389 } else {
2390 /* we don't know the level of the root node
2391 * until we actually have it read locked
2392 */
2393 b = btrfs_read_lock_root_node(root);
2394 level = btrfs_header_level(b);
2395 if (level <= write_lock_level) {
2396 /* whoops, must trade for write lock */
2397 btrfs_tree_read_unlock(b);
2398 free_extent_buffer(b);
2399 b = btrfs_lock_root_node(root);
2400 root_lock = BTRFS_WRITE_LOCK;
2401
2402 /* the level might have changed, check again */
2403 level = btrfs_header_level(b);
2404 }
2405 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002406 }
Chris Masonbd681512011-07-16 15:23:14 -04002407 p->nodes[level] = b;
2408 if (!p->skip_locking)
2409 p->locks[level] = root_lock;
Chris Mason925baed2008-06-25 16:01:30 -04002410
Chris Masoneb60cea2007-02-02 09:18:22 -05002411 while (b) {
Chris Mason5f39d392007-10-15 16:14:19 -04002412 level = btrfs_header_level(b);
Chris Mason65b51a02008-08-01 15:11:20 -04002413
2414 /*
2415 * setup the path here so we can release it under lock
2416 * contention with the cow code
2417 */
Chris Mason02217ed2007-03-02 16:08:05 -05002418 if (cow) {
Chris Masonc8c42862009-04-03 10:14:18 -04002419 /*
2420 * if we don't really need to cow this block
2421 * then we don't want to set the path blocking,
2422 * so we test it here
2423 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002424 if (!should_cow_block(trans, root, b))
Chris Mason65b51a02008-08-01 15:11:20 -04002425 goto cow_done;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002426
Chris Masonb4ce94d2009-02-04 09:25:08 -05002427 btrfs_set_path_blocking(p);
2428
Chris Masonbd681512011-07-16 15:23:14 -04002429 /*
2430 * must have write locks on this node and the
2431 * parent
2432 */
2433 if (level + 1 > write_lock_level) {
2434 write_lock_level = level + 1;
2435 btrfs_release_path(p);
2436 goto again;
2437 }
2438
Yan Zheng33c66f42009-07-22 09:59:00 -04002439 err = btrfs_cow_block(trans, root, b,
2440 p->nodes[level + 1],
2441 p->slots[level + 1], &b);
2442 if (err) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002443 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002444 goto done;
Chris Mason54aa1f42007-06-22 14:16:25 -04002445 }
Chris Mason02217ed2007-03-02 16:08:05 -05002446 }
Chris Mason65b51a02008-08-01 15:11:20 -04002447cow_done:
Chris Mason02217ed2007-03-02 16:08:05 -05002448 BUG_ON(!cow && ins_len);
Chris Mason65b51a02008-08-01 15:11:20 -04002449
Chris Masoneb60cea2007-02-02 09:18:22 -05002450 p->nodes[level] = b;
Chris Masonbd681512011-07-16 15:23:14 -04002451 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002452
2453 /*
2454 * we have a lock on b and as long as we aren't changing
2455 * the tree, there is no way to for the items in b to change.
2456 * It is safe to drop the lock on our parent before we
2457 * go through the expensive btree search on b.
2458 *
2459 * If cow is true, then we might be changing slot zero,
2460 * which may require changing the parent. So, we can't
2461 * drop the lock until after we know which slot we're
2462 * operating on.
2463 */
2464 if (!cow)
2465 btrfs_unlock_up_safe(p, level + 1);
2466
Chris Mason5f39d392007-10-15 16:14:19 -04002467 ret = bin_search(b, key, level, &slot);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002468
Chris Mason5f39d392007-10-15 16:14:19 -04002469 if (level != 0) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002470 int dec = 0;
2471 if (ret && slot > 0) {
2472 dec = 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002473 slot -= 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04002474 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002475 p->slots[level] = slot;
Yan Zheng33c66f42009-07-22 09:59:00 -04002476 err = setup_nodes_for_search(trans, root, p, b, level,
Chris Masonbd681512011-07-16 15:23:14 -04002477 ins_len, &write_lock_level);
Yan Zheng33c66f42009-07-22 09:59:00 -04002478 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002479 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002480 if (err) {
2481 ret = err;
Chris Masonc8c42862009-04-03 10:14:18 -04002482 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002483 }
Chris Masonc8c42862009-04-03 10:14:18 -04002484 b = p->nodes[level];
2485 slot = p->slots[level];
Chris Masonb4ce94d2009-02-04 09:25:08 -05002486
Chris Masonbd681512011-07-16 15:23:14 -04002487 /*
2488 * slot 0 is special, if we change the key
2489 * we have to update the parent pointer
2490 * which means we must have a write lock
2491 * on the parent
2492 */
2493 if (slot == 0 && cow &&
2494 write_lock_level < level + 1) {
2495 write_lock_level = level + 1;
2496 btrfs_release_path(p);
2497 goto again;
2498 }
2499
Chris Masonf7c79f32012-03-19 15:54:38 -04002500 unlock_up(p, level, lowest_unlock,
2501 min_write_lock_level, &write_lock_level);
Chris Masonf9efa9c2008-06-25 16:14:04 -04002502
Chris Mason925baed2008-06-25 16:01:30 -04002503 if (level == lowest_level) {
Yan Zheng33c66f42009-07-22 09:59:00 -04002504 if (dec)
2505 p->slots[level]++;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002506 goto done;
Chris Mason925baed2008-06-25 16:01:30 -04002507 }
Chris Masonca7a79a2008-05-12 12:59:19 -04002508
Yan Zheng33c66f42009-07-22 09:59:00 -04002509 err = read_block_for_search(trans, root, p,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002510 &b, level, slot, key, 0);
Yan Zheng33c66f42009-07-22 09:59:00 -04002511 if (err == -EAGAIN)
Chris Masonc8c42862009-04-03 10:14:18 -04002512 goto again;
Yan Zheng33c66f42009-07-22 09:59:00 -04002513 if (err) {
2514 ret = err;
Chris Mason76a05b32009-05-14 13:24:30 -04002515 goto done;
Yan Zheng33c66f42009-07-22 09:59:00 -04002516 }
Chris Mason76a05b32009-05-14 13:24:30 -04002517
Chris Masonb4ce94d2009-02-04 09:25:08 -05002518 if (!p->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04002519 level = btrfs_header_level(b);
2520 if (level <= write_lock_level) {
2521 err = btrfs_try_tree_write_lock(b);
2522 if (!err) {
2523 btrfs_set_path_blocking(p);
2524 btrfs_tree_lock(b);
2525 btrfs_clear_path_blocking(p, b,
2526 BTRFS_WRITE_LOCK);
2527 }
2528 p->locks[level] = BTRFS_WRITE_LOCK;
2529 } else {
2530 err = btrfs_try_tree_read_lock(b);
2531 if (!err) {
2532 btrfs_set_path_blocking(p);
2533 btrfs_tree_read_lock(b);
2534 btrfs_clear_path_blocking(p, b,
2535 BTRFS_READ_LOCK);
2536 }
2537 p->locks[level] = BTRFS_READ_LOCK;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002538 }
Chris Masonbd681512011-07-16 15:23:14 -04002539 p->nodes[level] = b;
Chris Masonb4ce94d2009-02-04 09:25:08 -05002540 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05002541 } else {
2542 p->slots[level] = slot;
Yan Zheng87b29b22008-12-17 10:21:48 -05002543 if (ins_len > 0 &&
2544 btrfs_leaf_free_space(root, b) < ins_len) {
Chris Masonbd681512011-07-16 15:23:14 -04002545 if (write_lock_level < 1) {
2546 write_lock_level = 1;
2547 btrfs_release_path(p);
2548 goto again;
2549 }
2550
Chris Masonb4ce94d2009-02-04 09:25:08 -05002551 btrfs_set_path_blocking(p);
Yan Zheng33c66f42009-07-22 09:59:00 -04002552 err = split_leaf(trans, root, key,
2553 p, ins_len, ret == 0);
Chris Masonbd681512011-07-16 15:23:14 -04002554 btrfs_clear_path_blocking(p, NULL, 0);
Chris Masonb4ce94d2009-02-04 09:25:08 -05002555
Yan Zheng33c66f42009-07-22 09:59:00 -04002556 BUG_ON(err > 0);
2557 if (err) {
2558 ret = err;
Chris Mason65b51a02008-08-01 15:11:20 -04002559 goto done;
2560 }
Chris Mason5c680ed2007-02-22 11:39:13 -05002561 }
Chris Mason459931e2008-12-10 09:10:46 -05002562 if (!p->search_for_split)
Chris Masonf7c79f32012-03-19 15:54:38 -04002563 unlock_up(p, level, lowest_unlock,
2564 min_write_lock_level, &write_lock_level);
Chris Mason65b51a02008-08-01 15:11:20 -04002565 goto done;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002566 }
2567 }
Chris Mason65b51a02008-08-01 15:11:20 -04002568 ret = 1;
2569done:
Chris Masonb4ce94d2009-02-04 09:25:08 -05002570 /*
2571 * we don't really know what they plan on doing with the path
2572 * from here on, so for now just mark it as blocking
2573 */
Chris Masonb9473432009-03-13 11:00:37 -04002574 if (!p->leave_spinning)
2575 btrfs_set_path_blocking(p);
Chris Mason76a05b32009-05-14 13:24:30 -04002576 if (ret < 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02002577 btrfs_release_path(p);
Chris Mason65b51a02008-08-01 15:11:20 -04002578 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002579}
2580
Chris Mason74123bd2007-02-02 11:05:29 -05002581/*
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02002582 * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
2583 * current state of the tree together with the operations recorded in the tree
2584 * modification log to search for the key in a previous version of this tree, as
2585 * denoted by the time_seq parameter.
2586 *
2587 * Naturally, there is no support for insert, delete or cow operations.
2588 *
2589 * The resulting path and return value will be set up as if we called
2590 * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
2591 */
2592int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
2593 struct btrfs_path *p, u64 time_seq)
2594{
2595 struct extent_buffer *b;
2596 int slot;
2597 int ret;
2598 int err;
2599 int level;
2600 int lowest_unlock = 1;
2601 u8 lowest_level = 0;
2602
2603 lowest_level = p->lowest_level;
2604 WARN_ON(p->nodes[0] != NULL);
2605
2606 if (p->search_commit_root) {
2607 BUG_ON(time_seq);
2608 return btrfs_search_slot(NULL, root, key, p, 0, 0);
2609 }
2610
2611again:
2612 level = 0;
2613 b = get_old_root(root, time_seq);
2614 extent_buffer_get(b);
2615 level = btrfs_header_level(b);
2616 btrfs_tree_read_lock(b);
2617 p->locks[level] = BTRFS_READ_LOCK;
2618
2619 while (b) {
2620 level = btrfs_header_level(b);
2621 p->nodes[level] = b;
2622 btrfs_clear_path_blocking(p, NULL, 0);
2623
2624 /*
2625 * we have a lock on b and as long as we aren't changing
2626 * the tree, there is no way to for the items in b to change.
2627 * It is safe to drop the lock on our parent before we
2628 * go through the expensive btree search on b.
2629 */
2630 btrfs_unlock_up_safe(p, level + 1);
2631
2632 ret = bin_search(b, key, level, &slot);
2633
2634 if (level != 0) {
2635 int dec = 0;
2636 if (ret && slot > 0) {
2637 dec = 1;
2638 slot -= 1;
2639 }
2640 p->slots[level] = slot;
2641 unlock_up(p, level, lowest_unlock, 0, NULL);
2642
2643 if (level == lowest_level) {
2644 if (dec)
2645 p->slots[level]++;
2646 goto done;
2647 }
2648
2649 err = read_block_for_search(NULL, root, p, &b, level,
2650 slot, key, time_seq);
2651 if (err == -EAGAIN)
2652 goto again;
2653 if (err) {
2654 ret = err;
2655 goto done;
2656 }
2657
2658 level = btrfs_header_level(b);
2659 err = btrfs_try_tree_read_lock(b);
2660 if (!err) {
2661 btrfs_set_path_blocking(p);
2662 btrfs_tree_read_lock(b);
2663 btrfs_clear_path_blocking(p, b,
2664 BTRFS_READ_LOCK);
2665 }
2666 p->locks[level] = BTRFS_READ_LOCK;
2667 p->nodes[level] = b;
2668 b = tree_mod_log_rewind(root->fs_info, b, time_seq);
2669 if (b != p->nodes[level]) {
2670 btrfs_tree_unlock_rw(p->nodes[level],
2671 p->locks[level]);
2672 p->locks[level] = 0;
2673 p->nodes[level] = b;
2674 }
2675 } else {
2676 p->slots[level] = slot;
2677 unlock_up(p, level, lowest_unlock, 0, NULL);
2678 goto done;
2679 }
2680 }
2681 ret = 1;
2682done:
2683 if (!p->leave_spinning)
2684 btrfs_set_path_blocking(p);
2685 if (ret < 0)
2686 btrfs_release_path(p);
2687
2688 return ret;
2689}
2690
2691/*
Chris Mason74123bd2007-02-02 11:05:29 -05002692 * adjust the pointers going up the tree, starting at level
2693 * making sure the right key of each node is points to 'key'.
2694 * This is used after shifting pointers to the left, so it stops
2695 * fixing up pointers when a given leaf/node is not in slot 0 of the
2696 * higher levels
Chris Masonaa5d6be2007-02-28 16:35:06 -05002697 *
Chris Mason74123bd2007-02-02 11:05:29 -05002698 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002699static void fixup_low_keys(struct btrfs_trans_handle *trans,
2700 struct btrfs_root *root, struct btrfs_path *path,
2701 struct btrfs_disk_key *key, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002702{
2703 int i;
Chris Mason5f39d392007-10-15 16:14:19 -04002704 struct extent_buffer *t;
2705
Chris Mason234b63a2007-03-13 10:46:10 -04002706 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05002707 int tslot = path->slots[i];
Chris Masoneb60cea2007-02-02 09:18:22 -05002708 if (!path->nodes[i])
Chris Masonbe0e5c02007-01-26 15:51:26 -05002709 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002710 t = path->nodes[i];
Jan Schmidtf2304752012-05-26 11:43:17 +02002711 tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002712 btrfs_set_node_key(t, key, tslot);
Chris Masond6025572007-03-30 14:27:56 -04002713 btrfs_mark_buffer_dirty(path->nodes[i]);
Chris Masonbe0e5c02007-01-26 15:51:26 -05002714 if (tslot != 0)
2715 break;
2716 }
2717}
2718
Chris Mason74123bd2007-02-02 11:05:29 -05002719/*
Zheng Yan31840ae2008-09-23 13:14:14 -04002720 * update item key.
2721 *
2722 * This function isn't completely safe. It's the caller's responsibility
2723 * that the new key won't break the order
2724 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002725void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
2726 struct btrfs_root *root, struct btrfs_path *path,
2727 struct btrfs_key *new_key)
Zheng Yan31840ae2008-09-23 13:14:14 -04002728{
2729 struct btrfs_disk_key disk_key;
2730 struct extent_buffer *eb;
2731 int slot;
2732
2733 eb = path->nodes[0];
2734 slot = path->slots[0];
2735 if (slot > 0) {
2736 btrfs_item_key(eb, &disk_key, slot - 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002737 BUG_ON(comp_keys(&disk_key, new_key) >= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002738 }
2739 if (slot < btrfs_header_nritems(eb) - 1) {
2740 btrfs_item_key(eb, &disk_key, slot + 1);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002741 BUG_ON(comp_keys(&disk_key, new_key) <= 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002742 }
2743
2744 btrfs_cpu_key_to_disk(&disk_key, new_key);
2745 btrfs_set_item_key(eb, &disk_key, slot);
2746 btrfs_mark_buffer_dirty(eb);
2747 if (slot == 0)
2748 fixup_low_keys(trans, root, path, &disk_key, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04002749}
2750
2751/*
Chris Mason74123bd2007-02-02 11:05:29 -05002752 * try to push data from one node into the next node left in the
Chris Mason79f95c82007-03-01 15:16:26 -05002753 * tree.
Chris Masonaa5d6be2007-02-28 16:35:06 -05002754 *
2755 * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
2756 * error, and > 0 if there was no room in the left hand block.
Chris Mason74123bd2007-02-02 11:05:29 -05002757 */
Chris Mason98ed5172008-01-03 10:01:48 -05002758static int push_node_left(struct btrfs_trans_handle *trans,
2759 struct btrfs_root *root, struct extent_buffer *dst,
Chris Mason971a1f62008-04-24 10:54:32 -04002760 struct extent_buffer *src, int empty)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002761{
Chris Masonbe0e5c02007-01-26 15:51:26 -05002762 int push_items = 0;
Chris Masonbb803952007-03-01 12:04:21 -05002763 int src_nritems;
2764 int dst_nritems;
Chris Masonaa5d6be2007-02-28 16:35:06 -05002765 int ret = 0;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002766
Chris Mason5f39d392007-10-15 16:14:19 -04002767 src_nritems = btrfs_header_nritems(src);
2768 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002769 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Mason7bb86312007-12-11 09:25:06 -05002770 WARN_ON(btrfs_header_generation(src) != trans->transid);
2771 WARN_ON(btrfs_header_generation(dst) != trans->transid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002772
Chris Masonbce4eae2008-04-24 14:42:46 -04002773 if (!empty && src_nritems <= 8)
Chris Mason971a1f62008-04-24 10:54:32 -04002774 return 1;
2775
Chris Masond3977122009-01-05 21:25:51 -05002776 if (push_items <= 0)
Chris Masonbe0e5c02007-01-26 15:51:26 -05002777 return 1;
2778
Chris Masonbce4eae2008-04-24 14:42:46 -04002779 if (empty) {
Chris Mason971a1f62008-04-24 10:54:32 -04002780 push_items = min(src_nritems, push_items);
Chris Masonbce4eae2008-04-24 14:42:46 -04002781 if (push_items < src_nritems) {
2782 /* leave at least 8 pointers in the node if
2783 * we aren't going to empty it
2784 */
2785 if (src_nritems - push_items < 8) {
2786 if (push_items <= 8)
2787 return 1;
2788 push_items -= 8;
2789 }
2790 }
2791 } else
2792 push_items = min(src_nritems - 8, push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002793
Jan Schmidtf2304752012-05-26 11:43:17 +02002794 tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
2795 push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002796 copy_extent_buffer(dst, src,
2797 btrfs_node_key_ptr_offset(dst_nritems),
2798 btrfs_node_key_ptr_offset(0),
Chris Masond3977122009-01-05 21:25:51 -05002799 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason5f39d392007-10-15 16:14:19 -04002800
Chris Masonbb803952007-03-01 12:04:21 -05002801 if (push_items < src_nritems) {
Jan Schmidtf2304752012-05-26 11:43:17 +02002802 tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
2803 src_nritems - push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002804 memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
2805 btrfs_node_key_ptr_offset(push_items),
2806 (src_nritems - push_items) *
2807 sizeof(struct btrfs_key_ptr));
Chris Masonbb803952007-03-01 12:04:21 -05002808 }
Chris Mason5f39d392007-10-15 16:14:19 -04002809 btrfs_set_header_nritems(src, src_nritems - push_items);
2810 btrfs_set_header_nritems(dst, dst_nritems + push_items);
2811 btrfs_mark_buffer_dirty(src);
2812 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002813
Chris Masonbb803952007-03-01 12:04:21 -05002814 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05002815}
2816
Chris Mason97571fd2007-02-24 13:39:08 -05002817/*
Chris Mason79f95c82007-03-01 15:16:26 -05002818 * try to push data from one node into the next node right in the
2819 * tree.
2820 *
2821 * returns 0 if some ptrs were pushed, < 0 if there was some horrible
2822 * error, and > 0 if there was no room in the right hand block.
2823 *
2824 * this will only push up to 1/2 the contents of the left node over
2825 */
Chris Mason5f39d392007-10-15 16:14:19 -04002826static int balance_node_right(struct btrfs_trans_handle *trans,
2827 struct btrfs_root *root,
2828 struct extent_buffer *dst,
2829 struct extent_buffer *src)
Chris Mason79f95c82007-03-01 15:16:26 -05002830{
Chris Mason79f95c82007-03-01 15:16:26 -05002831 int push_items = 0;
2832 int max_push;
2833 int src_nritems;
2834 int dst_nritems;
2835 int ret = 0;
Chris Mason79f95c82007-03-01 15:16:26 -05002836
Chris Mason7bb86312007-12-11 09:25:06 -05002837 WARN_ON(btrfs_header_generation(src) != trans->transid);
2838 WARN_ON(btrfs_header_generation(dst) != trans->transid);
2839
Chris Mason5f39d392007-10-15 16:14:19 -04002840 src_nritems = btrfs_header_nritems(src);
2841 dst_nritems = btrfs_header_nritems(dst);
Chris Mason123abc82007-03-14 14:14:43 -04002842 push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
Chris Masond3977122009-01-05 21:25:51 -05002843 if (push_items <= 0)
Chris Mason79f95c82007-03-01 15:16:26 -05002844 return 1;
Chris Masonbce4eae2008-04-24 14:42:46 -04002845
Chris Masond3977122009-01-05 21:25:51 -05002846 if (src_nritems < 4)
Chris Masonbce4eae2008-04-24 14:42:46 -04002847 return 1;
Chris Mason79f95c82007-03-01 15:16:26 -05002848
2849 max_push = src_nritems / 2 + 1;
2850 /* don't try to empty the node */
Chris Masond3977122009-01-05 21:25:51 -05002851 if (max_push >= src_nritems)
Chris Mason79f95c82007-03-01 15:16:26 -05002852 return 1;
Yan252c38f2007-08-29 09:11:44 -04002853
Chris Mason79f95c82007-03-01 15:16:26 -05002854 if (max_push < push_items)
2855 push_items = max_push;
2856
Jan Schmidtf2304752012-05-26 11:43:17 +02002857 tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
Chris Mason5f39d392007-10-15 16:14:19 -04002858 memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
2859 btrfs_node_key_ptr_offset(0),
2860 (dst_nritems) *
2861 sizeof(struct btrfs_key_ptr));
Chris Masond6025572007-03-30 14:27:56 -04002862
Jan Schmidtf2304752012-05-26 11:43:17 +02002863 tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
2864 src_nritems - push_items, push_items);
Chris Mason5f39d392007-10-15 16:14:19 -04002865 copy_extent_buffer(dst, src,
2866 btrfs_node_key_ptr_offset(0),
2867 btrfs_node_key_ptr_offset(src_nritems - push_items),
Chris Masond3977122009-01-05 21:25:51 -05002868 push_items * sizeof(struct btrfs_key_ptr));
Chris Mason79f95c82007-03-01 15:16:26 -05002869
Chris Mason5f39d392007-10-15 16:14:19 -04002870 btrfs_set_header_nritems(src, src_nritems - push_items);
2871 btrfs_set_header_nritems(dst, dst_nritems + push_items);
Chris Mason79f95c82007-03-01 15:16:26 -05002872
Chris Mason5f39d392007-10-15 16:14:19 -04002873 btrfs_mark_buffer_dirty(src);
2874 btrfs_mark_buffer_dirty(dst);
Zheng Yan31840ae2008-09-23 13:14:14 -04002875
Chris Mason79f95c82007-03-01 15:16:26 -05002876 return ret;
2877}
2878
2879/*
Chris Mason97571fd2007-02-24 13:39:08 -05002880 * helper function to insert a new root level in the tree.
2881 * A new node is allocated, and a single item is inserted to
2882 * point to the existing root
Chris Masonaa5d6be2007-02-28 16:35:06 -05002883 *
2884 * returns zero on success or < 0 on failure.
Chris Mason97571fd2007-02-24 13:39:08 -05002885 */
Chris Masond3977122009-01-05 21:25:51 -05002886static noinline int insert_new_root(struct btrfs_trans_handle *trans,
Chris Mason5f39d392007-10-15 16:14:19 -04002887 struct btrfs_root *root,
2888 struct btrfs_path *path, int level)
Chris Mason5c680ed2007-02-22 11:39:13 -05002889{
Chris Mason7bb86312007-12-11 09:25:06 -05002890 u64 lower_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002891 struct extent_buffer *lower;
2892 struct extent_buffer *c;
Chris Mason925baed2008-06-25 16:01:30 -04002893 struct extent_buffer *old;
Chris Mason5f39d392007-10-15 16:14:19 -04002894 struct btrfs_disk_key lower_key;
Chris Mason5c680ed2007-02-22 11:39:13 -05002895
2896 BUG_ON(path->nodes[level]);
2897 BUG_ON(path->nodes[level-1] != root->node);
2898
Chris Mason7bb86312007-12-11 09:25:06 -05002899 lower = path->nodes[level-1];
2900 if (level == 1)
2901 btrfs_item_key(lower, &lower_key, 0);
2902 else
2903 btrfs_node_key(lower, &lower_key, 0);
2904
Zheng Yan31840ae2008-09-23 13:14:14 -04002905 c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002906 root->root_key.objectid, &lower_key,
Jan Schmidt5581a512012-05-16 17:04:52 +02002907 level, root->node->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002908 if (IS_ERR(c))
2909 return PTR_ERR(c);
Chris Mason925baed2008-06-25 16:01:30 -04002910
Yan, Zhengf0486c62010-05-16 10:46:25 -04002911 root_add_used(root, root->nodesize);
2912
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002913 memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04002914 btrfs_set_header_nritems(c, 1);
2915 btrfs_set_header_level(c, level);
Chris Masondb945352007-10-15 16:15:53 -04002916 btrfs_set_header_bytenr(c, c->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002917 btrfs_set_header_generation(c, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918 btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04002919 btrfs_set_header_owner(c, root->root_key.objectid);
Chris Masond5719762007-03-23 10:01:08 -04002920
Chris Mason5f39d392007-10-15 16:14:19 -04002921 write_extent_buffer(c, root->fs_info->fsid,
2922 (unsigned long)btrfs_header_fsid(c),
2923 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04002924
2925 write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
2926 (unsigned long)btrfs_header_chunk_tree_uuid(c),
2927 BTRFS_UUID_SIZE);
2928
Chris Mason5f39d392007-10-15 16:14:19 -04002929 btrfs_set_node_key(c, &lower_key, 0);
Chris Masondb945352007-10-15 16:15:53 -04002930 btrfs_set_node_blockptr(c, 0, lower->start);
Chris Mason7bb86312007-12-11 09:25:06 -05002931 lower_gen = btrfs_header_generation(lower);
Zheng Yan31840ae2008-09-23 13:14:14 -04002932 WARN_ON(lower_gen != trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002933
2934 btrfs_set_node_ptr_generation(c, 0, lower_gen);
Chris Mason5f39d392007-10-15 16:14:19 -04002935
2936 btrfs_mark_buffer_dirty(c);
Chris Masond5719762007-03-23 10:01:08 -04002937
Chris Mason925baed2008-06-25 16:01:30 -04002938 old = root->node;
Jan Schmidtf2304752012-05-26 11:43:17 +02002939 tree_mod_log_set_root_pointer(root, c);
Chris Mason240f62c2011-03-23 14:54:42 -04002940 rcu_assign_pointer(root->node, c);
Chris Mason925baed2008-06-25 16:01:30 -04002941
2942 /* the super has an extra ref to root->node */
2943 free_extent_buffer(old);
2944
Chris Mason0b86a832008-03-24 15:01:56 -04002945 add_root_to_dirty_list(root);
Chris Mason5f39d392007-10-15 16:14:19 -04002946 extent_buffer_get(c);
2947 path->nodes[level] = c;
Chris Masonbd681512011-07-16 15:23:14 -04002948 path->locks[level] = BTRFS_WRITE_LOCK;
Chris Mason5c680ed2007-02-22 11:39:13 -05002949 path->slots[level] = 0;
2950 return 0;
2951}
2952
Chris Mason74123bd2007-02-02 11:05:29 -05002953/*
2954 * worker function to insert a single pointer in a node.
2955 * the node should have enough room for the pointer already
Chris Mason97571fd2007-02-24 13:39:08 -05002956 *
Chris Mason74123bd2007-02-02 11:05:29 -05002957 * slot and level indicate where you want the key to go, and
2958 * blocknr is the block the key points to.
2959 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01002960static void insert_ptr(struct btrfs_trans_handle *trans,
2961 struct btrfs_root *root, struct btrfs_path *path,
2962 struct btrfs_disk_key *key, u64 bytenr,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002963 int slot, int level, int tree_mod_log)
Chris Mason74123bd2007-02-02 11:05:29 -05002964{
Chris Mason5f39d392007-10-15 16:14:19 -04002965 struct extent_buffer *lower;
Chris Mason74123bd2007-02-02 11:05:29 -05002966 int nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002967 int ret;
Chris Mason5c680ed2007-02-22 11:39:13 -05002968
2969 BUG_ON(!path->nodes[level]);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002970 btrfs_assert_tree_locked(path->nodes[level]);
Chris Mason5f39d392007-10-15 16:14:19 -04002971 lower = path->nodes[level];
2972 nritems = btrfs_header_nritems(lower);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04002973 BUG_ON(slot > nritems);
Jeff Mahoney143bede2012-03-01 14:56:26 +01002974 BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
Chris Mason74123bd2007-02-02 11:05:29 -05002975 if (slot != nritems) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002976 if (tree_mod_log && level)
2977 tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
2978 slot, nritems - slot);
Chris Mason5f39d392007-10-15 16:14:19 -04002979 memmove_extent_buffer(lower,
2980 btrfs_node_key_ptr_offset(slot + 1),
2981 btrfs_node_key_ptr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04002982 (nritems - slot) * sizeof(struct btrfs_key_ptr));
Chris Mason74123bd2007-02-02 11:05:29 -05002983 }
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02002984 if (tree_mod_log && level) {
2985 ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
2986 MOD_LOG_KEY_ADD);
2987 BUG_ON(ret < 0);
2988 }
Chris Mason5f39d392007-10-15 16:14:19 -04002989 btrfs_set_node_key(lower, key, slot);
Chris Masondb945352007-10-15 16:15:53 -04002990 btrfs_set_node_blockptr(lower, slot, bytenr);
Chris Mason74493f72007-12-11 09:25:06 -05002991 WARN_ON(trans->transid == 0);
2992 btrfs_set_node_ptr_generation(lower, slot, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04002993 btrfs_set_header_nritems(lower, nritems + 1);
2994 btrfs_mark_buffer_dirty(lower);
Chris Mason74123bd2007-02-02 11:05:29 -05002995}
2996
Chris Mason97571fd2007-02-24 13:39:08 -05002997/*
2998 * split the node at the specified level in path in two.
2999 * The path is corrected to point to the appropriate node after the split
3000 *
3001 * Before splitting this tries to make some room in the node by pushing
3002 * left and right, if either one works, it returns right away.
Chris Masonaa5d6be2007-02-28 16:35:06 -05003003 *
3004 * returns 0 on success and < 0 on failure
Chris Mason97571fd2007-02-24 13:39:08 -05003005 */
Chris Masone02119d2008-09-05 16:13:11 -04003006static noinline int split_node(struct btrfs_trans_handle *trans,
3007 struct btrfs_root *root,
3008 struct btrfs_path *path, int level)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003009{
Chris Mason5f39d392007-10-15 16:14:19 -04003010 struct extent_buffer *c;
3011 struct extent_buffer *split;
3012 struct btrfs_disk_key disk_key;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003013 int mid;
Chris Mason5c680ed2007-02-22 11:39:13 -05003014 int ret;
Chris Mason7518a232007-03-12 12:01:18 -04003015 u32 c_nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003016
Chris Mason5f39d392007-10-15 16:14:19 -04003017 c = path->nodes[level];
Chris Mason7bb86312007-12-11 09:25:06 -05003018 WARN_ON(btrfs_header_generation(c) != trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04003019 if (c == root->node) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003020 /* trying to split the root, lets make a new one */
Chris Masone089f052007-03-16 16:20:31 -04003021 ret = insert_new_root(trans, root, path, level + 1);
Chris Mason5c680ed2007-02-22 11:39:13 -05003022 if (ret)
3023 return ret;
Chris Masonb3612422009-05-13 19:12:15 -04003024 } else {
Chris Masone66f7092007-04-20 13:16:02 -04003025 ret = push_nodes_for_insert(trans, root, path, level);
Chris Mason5f39d392007-10-15 16:14:19 -04003026 c = path->nodes[level];
3027 if (!ret && btrfs_header_nritems(c) <
Chris Masonc448acf2008-04-24 09:34:34 -04003028 BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
Chris Masone66f7092007-04-20 13:16:02 -04003029 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04003030 if (ret < 0)
3031 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003032 }
Chris Masone66f7092007-04-20 13:16:02 -04003033
Chris Mason5f39d392007-10-15 16:14:19 -04003034 c_nritems = btrfs_header_nritems(c);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035 mid = (c_nritems + 1) / 2;
3036 btrfs_node_key(c, &disk_key, mid);
Chris Mason7bb86312007-12-11 09:25:06 -05003037
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003038 split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
Zheng Yan31840ae2008-09-23 13:14:14 -04003039 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003040 &disk_key, level, c->start, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003041 if (IS_ERR(split))
3042 return PTR_ERR(split);
Chris Mason54aa1f42007-06-22 14:16:25 -04003043
Yan, Zhengf0486c62010-05-16 10:46:25 -04003044 root_add_used(root, root->nodesize);
3045
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003046 memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
Chris Mason5f39d392007-10-15 16:14:19 -04003047 btrfs_set_header_level(split, btrfs_header_level(c));
Chris Masondb945352007-10-15 16:15:53 -04003048 btrfs_set_header_bytenr(split, split->start);
Chris Mason5f39d392007-10-15 16:14:19 -04003049 btrfs_set_header_generation(split, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003050 btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
Chris Mason5f39d392007-10-15 16:14:19 -04003051 btrfs_set_header_owner(split, root->root_key.objectid);
3052 write_extent_buffer(split, root->fs_info->fsid,
3053 (unsigned long)btrfs_header_fsid(split),
3054 BTRFS_FSID_SIZE);
Chris Masone17cade2008-04-15 15:41:47 -04003055 write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
3056 (unsigned long)btrfs_header_chunk_tree_uuid(split),
3057 BTRFS_UUID_SIZE);
Chris Mason5f39d392007-10-15 16:14:19 -04003058
Jan Schmidtf2304752012-05-26 11:43:17 +02003059 tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003060 copy_extent_buffer(split, c,
3061 btrfs_node_key_ptr_offset(0),
3062 btrfs_node_key_ptr_offset(mid),
3063 (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
3064 btrfs_set_header_nritems(split, c_nritems - mid);
3065 btrfs_set_header_nritems(c, mid);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003066 ret = 0;
3067
Chris Mason5f39d392007-10-15 16:14:19 -04003068 btrfs_mark_buffer_dirty(c);
3069 btrfs_mark_buffer_dirty(split);
3070
Jeff Mahoney143bede2012-03-01 14:56:26 +01003071 insert_ptr(trans, root, path, &disk_key, split->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003072 path->slots[level + 1] + 1, level + 1, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003073
Chris Mason5de08d72007-02-24 06:24:44 -05003074 if (path->slots[level] >= mid) {
Chris Mason5c680ed2007-02-22 11:39:13 -05003075 path->slots[level] -= mid;
Chris Mason925baed2008-06-25 16:01:30 -04003076 btrfs_tree_unlock(c);
Chris Mason5f39d392007-10-15 16:14:19 -04003077 free_extent_buffer(c);
3078 path->nodes[level] = split;
Chris Mason5c680ed2007-02-22 11:39:13 -05003079 path->slots[level + 1] += 1;
3080 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003081 btrfs_tree_unlock(split);
Chris Mason5f39d392007-10-15 16:14:19 -04003082 free_extent_buffer(split);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003083 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05003084 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003085}
3086
Chris Mason74123bd2007-02-02 11:05:29 -05003087/*
3088 * how many bytes are required to store the items in a leaf. start
3089 * and nr indicate which items in the leaf to check. This totals up the
3090 * space used both by the item structs and the item data
3091 */
Chris Mason5f39d392007-10-15 16:14:19 -04003092static int leaf_space_used(struct extent_buffer *l, int start, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003093{
3094 int data_len;
Chris Mason5f39d392007-10-15 16:14:19 -04003095 int nritems = btrfs_header_nritems(l);
Chris Masond4dbff92007-04-04 14:08:15 -04003096 int end = min(nritems, start + nr) - 1;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003097
3098 if (!nr)
3099 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003100 data_len = btrfs_item_end_nr(l, start);
3101 data_len = data_len - btrfs_item_offset_nr(l, end);
Chris Mason0783fcf2007-03-12 20:12:07 -04003102 data_len += sizeof(struct btrfs_item) * nr;
Chris Masond4dbff92007-04-04 14:08:15 -04003103 WARN_ON(data_len < 0);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003104 return data_len;
3105}
3106
Chris Mason74123bd2007-02-02 11:05:29 -05003107/*
Chris Masond4dbff92007-04-04 14:08:15 -04003108 * The space between the end of the leaf items and
3109 * the start of the leaf data. IOW, how much room
3110 * the leaf has left for both items and data
3111 */
Chris Masond3977122009-01-05 21:25:51 -05003112noinline int btrfs_leaf_free_space(struct btrfs_root *root,
Chris Masone02119d2008-09-05 16:13:11 -04003113 struct extent_buffer *leaf)
Chris Masond4dbff92007-04-04 14:08:15 -04003114{
Chris Mason5f39d392007-10-15 16:14:19 -04003115 int nritems = btrfs_header_nritems(leaf);
3116 int ret;
3117 ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
3118 if (ret < 0) {
Chris Masond3977122009-01-05 21:25:51 -05003119 printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
3120 "used %d nritems %d\n",
Jens Axboeae2f5412007-10-19 09:22:59 -04003121 ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
Chris Mason5f39d392007-10-15 16:14:19 -04003122 leaf_space_used(leaf, 0, nritems), nritems);
3123 }
3124 return ret;
Chris Masond4dbff92007-04-04 14:08:15 -04003125}
3126
Chris Mason99d8f832010-07-07 10:51:48 -04003127/*
3128 * min slot controls the lowest index we're willing to push to the
3129 * right. We'll push up to and including min_slot, but no lower
3130 */
Chris Mason44871b12009-03-13 10:04:31 -04003131static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
3132 struct btrfs_root *root,
3133 struct btrfs_path *path,
3134 int data_size, int empty,
3135 struct extent_buffer *right,
Chris Mason99d8f832010-07-07 10:51:48 -04003136 int free_space, u32 left_nritems,
3137 u32 min_slot)
Chris Mason00ec4c52007-02-24 12:47:20 -05003138{
Chris Mason5f39d392007-10-15 16:14:19 -04003139 struct extent_buffer *left = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04003140 struct extent_buffer *upper = path->nodes[1];
Chris Masoncfed81a2012-03-03 07:40:03 -05003141 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003142 struct btrfs_disk_key disk_key;
Chris Mason00ec4c52007-02-24 12:47:20 -05003143 int slot;
Chris Mason34a38212007-11-07 13:31:03 -05003144 u32 i;
Chris Mason00ec4c52007-02-24 12:47:20 -05003145 int push_space = 0;
3146 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003147 struct btrfs_item *item;
Chris Mason34a38212007-11-07 13:31:03 -05003148 u32 nr;
Chris Mason7518a232007-03-12 12:01:18 -04003149 u32 right_nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04003150 u32 data_end;
Chris Masondb945352007-10-15 16:15:53 -04003151 u32 this_item_size;
Chris Mason00ec4c52007-02-24 12:47:20 -05003152
Chris Masoncfed81a2012-03-03 07:40:03 -05003153 btrfs_init_map_token(&token);
3154
Chris Mason34a38212007-11-07 13:31:03 -05003155 if (empty)
3156 nr = 0;
3157 else
Chris Mason99d8f832010-07-07 10:51:48 -04003158 nr = max_t(u32, 1, min_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003159
Zheng Yan31840ae2008-09-23 13:14:14 -04003160 if (path->slots[0] >= left_nritems)
Yan Zheng87b29b22008-12-17 10:21:48 -05003161 push_space += data_size;
Zheng Yan31840ae2008-09-23 13:14:14 -04003162
Chris Mason44871b12009-03-13 10:04:31 -04003163 slot = path->slots[1];
Chris Mason34a38212007-11-07 13:31:03 -05003164 i = left_nritems - 1;
3165 while (i >= nr) {
Chris Mason5f39d392007-10-15 16:14:19 -04003166 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003167
Zheng Yan31840ae2008-09-23 13:14:14 -04003168 if (!empty && push_items > 0) {
3169 if (path->slots[0] > i)
3170 break;
3171 if (path->slots[0] == i) {
3172 int space = btrfs_leaf_free_space(root, left);
3173 if (space + push_space * 2 > free_space)
3174 break;
3175 }
3176 }
3177
Chris Mason00ec4c52007-02-24 12:47:20 -05003178 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003179 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003180
Chris Masondb945352007-10-15 16:15:53 -04003181 this_item_size = btrfs_item_size(left, item);
3182 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Mason00ec4c52007-02-24 12:47:20 -05003183 break;
Zheng Yan31840ae2008-09-23 13:14:14 -04003184
Chris Mason00ec4c52007-02-24 12:47:20 -05003185 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003186 push_space += this_item_size + sizeof(*item);
Chris Mason34a38212007-11-07 13:31:03 -05003187 if (i == 0)
3188 break;
3189 i--;
Chris Masondb945352007-10-15 16:15:53 -04003190 }
Chris Mason5f39d392007-10-15 16:14:19 -04003191
Chris Mason925baed2008-06-25 16:01:30 -04003192 if (push_items == 0)
3193 goto out_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04003194
Chris Mason34a38212007-11-07 13:31:03 -05003195 if (!empty && push_items == left_nritems)
Chris Masona429e512007-04-18 16:15:28 -04003196 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003197
Chris Mason00ec4c52007-02-24 12:47:20 -05003198 /* push left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003199 right_nritems = btrfs_header_nritems(right);
Chris Mason34a38212007-11-07 13:31:03 -05003200
Chris Mason5f39d392007-10-15 16:14:19 -04003201 push_space = btrfs_item_end_nr(left, left_nritems - push_items);
Chris Mason123abc82007-03-14 14:14:43 -04003202 push_space -= leaf_data_end(root, left);
Chris Mason5f39d392007-10-15 16:14:19 -04003203
Chris Mason00ec4c52007-02-24 12:47:20 -05003204 /* make room in the right data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003205 data_end = leaf_data_end(root, right);
3206 memmove_extent_buffer(right,
3207 btrfs_leaf_data(right) + data_end - push_space,
3208 btrfs_leaf_data(right) + data_end,
3209 BTRFS_LEAF_DATA_SIZE(root) - data_end);
3210
Chris Mason00ec4c52007-02-24 12:47:20 -05003211 /* copy from the left data area */
Chris Mason5f39d392007-10-15 16:14:19 -04003212 copy_extent_buffer(right, left, btrfs_leaf_data(right) +
Chris Masond6025572007-03-30 14:27:56 -04003213 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3214 btrfs_leaf_data(left) + leaf_data_end(root, left),
3215 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003216
3217 memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
3218 btrfs_item_nr_offset(0),
3219 right_nritems * sizeof(struct btrfs_item));
3220
Chris Mason00ec4c52007-02-24 12:47:20 -05003221 /* copy the items from left to right */
Chris Mason5f39d392007-10-15 16:14:19 -04003222 copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
3223 btrfs_item_nr_offset(left_nritems - push_items),
3224 push_items * sizeof(struct btrfs_item));
Chris Mason00ec4c52007-02-24 12:47:20 -05003225
3226 /* update the item pointers */
Chris Mason7518a232007-03-12 12:01:18 -04003227 right_nritems += push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003228 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003229 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason7518a232007-03-12 12:01:18 -04003230 for (i = 0; i < right_nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003231 item = btrfs_item_nr(right, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05003232 push_space -= btrfs_token_item_size(right, item, &token);
3233 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003234 }
3235
Chris Mason7518a232007-03-12 12:01:18 -04003236 left_nritems -= push_items;
Chris Mason5f39d392007-10-15 16:14:19 -04003237 btrfs_set_header_nritems(left, left_nritems);
Chris Mason00ec4c52007-02-24 12:47:20 -05003238
Chris Mason34a38212007-11-07 13:31:03 -05003239 if (left_nritems)
3240 btrfs_mark_buffer_dirty(left);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003241 else
3242 clean_tree_block(trans, root, left);
3243
Chris Mason5f39d392007-10-15 16:14:19 -04003244 btrfs_mark_buffer_dirty(right);
Chris Masona429e512007-04-18 16:15:28 -04003245
Chris Mason5f39d392007-10-15 16:14:19 -04003246 btrfs_item_key(right, &disk_key, 0);
3247 btrfs_set_node_key(upper, &disk_key, slot + 1);
Chris Masond6025572007-03-30 14:27:56 -04003248 btrfs_mark_buffer_dirty(upper);
Chris Mason02217ed2007-03-02 16:08:05 -05003249
Chris Mason00ec4c52007-02-24 12:47:20 -05003250 /* then fixup the leaf pointer in the path */
Chris Mason7518a232007-03-12 12:01:18 -04003251 if (path->slots[0] >= left_nritems) {
3252 path->slots[0] -= left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003253 if (btrfs_header_nritems(path->nodes[0]) == 0)
3254 clean_tree_block(trans, root, path->nodes[0]);
3255 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003256 free_extent_buffer(path->nodes[0]);
3257 path->nodes[0] = right;
Chris Mason00ec4c52007-02-24 12:47:20 -05003258 path->slots[1] += 1;
3259 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003260 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003261 free_extent_buffer(right);
Chris Mason00ec4c52007-02-24 12:47:20 -05003262 }
3263 return 0;
Chris Mason925baed2008-06-25 16:01:30 -04003264
3265out_unlock:
3266 btrfs_tree_unlock(right);
3267 free_extent_buffer(right);
3268 return 1;
Chris Mason00ec4c52007-02-24 12:47:20 -05003269}
Chris Mason925baed2008-06-25 16:01:30 -04003270
Chris Mason00ec4c52007-02-24 12:47:20 -05003271/*
Chris Mason44871b12009-03-13 10:04:31 -04003272 * push some data in the path leaf to the right, trying to free up at
3273 * least data_size bytes. returns zero if the push worked, nonzero otherwise
3274 *
3275 * returns 1 if the push failed because the other node didn't have enough
3276 * room, 0 if everything worked out and < 0 if there were major errors.
Chris Mason99d8f832010-07-07 10:51:48 -04003277 *
3278 * this will push starting from min_slot to the end of the leaf. It won't
3279 * push any slot lower than min_slot
Chris Mason44871b12009-03-13 10:04:31 -04003280 */
3281static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003282 *root, struct btrfs_path *path,
3283 int min_data_size, int data_size,
3284 int empty, u32 min_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003285{
3286 struct extent_buffer *left = path->nodes[0];
3287 struct extent_buffer *right;
3288 struct extent_buffer *upper;
3289 int slot;
3290 int free_space;
3291 u32 left_nritems;
3292 int ret;
3293
3294 if (!path->nodes[1])
3295 return 1;
3296
3297 slot = path->slots[1];
3298 upper = path->nodes[1];
3299 if (slot >= btrfs_header_nritems(upper) - 1)
3300 return 1;
3301
3302 btrfs_assert_tree_locked(path->nodes[1]);
3303
3304 right = read_node_slot(root, upper, slot + 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003305 if (right == NULL)
3306 return 1;
3307
Chris Mason44871b12009-03-13 10:04:31 -04003308 btrfs_tree_lock(right);
3309 btrfs_set_lock_blocking(right);
3310
3311 free_space = btrfs_leaf_free_space(root, right);
3312 if (free_space < data_size)
3313 goto out_unlock;
3314
3315 /* cow and double check */
3316 ret = btrfs_cow_block(trans, root, right, upper,
3317 slot + 1, &right);
3318 if (ret)
3319 goto out_unlock;
3320
3321 free_space = btrfs_leaf_free_space(root, right);
3322 if (free_space < data_size)
3323 goto out_unlock;
3324
3325 left_nritems = btrfs_header_nritems(left);
3326 if (left_nritems == 0)
3327 goto out_unlock;
3328
Chris Mason99d8f832010-07-07 10:51:48 -04003329 return __push_leaf_right(trans, root, path, min_data_size, empty,
3330 right, free_space, left_nritems, min_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003331out_unlock:
3332 btrfs_tree_unlock(right);
3333 free_extent_buffer(right);
3334 return 1;
3335}
3336
3337/*
Chris Mason74123bd2007-02-02 11:05:29 -05003338 * push some data in the path leaf to the left, trying to free up at
3339 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003340 *
3341 * max_slot can put a limit on how far into the leaf we'll push items. The
3342 * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
3343 * items
Chris Mason74123bd2007-02-02 11:05:29 -05003344 */
Chris Mason44871b12009-03-13 10:04:31 -04003345static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
3346 struct btrfs_root *root,
3347 struct btrfs_path *path, int data_size,
3348 int empty, struct extent_buffer *left,
Chris Mason99d8f832010-07-07 10:51:48 -04003349 int free_space, u32 right_nritems,
3350 u32 max_slot)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003351{
Chris Mason5f39d392007-10-15 16:14:19 -04003352 struct btrfs_disk_key disk_key;
3353 struct extent_buffer *right = path->nodes[0];
Chris Masonbe0e5c02007-01-26 15:51:26 -05003354 int i;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003355 int push_space = 0;
3356 int push_items = 0;
Chris Mason0783fcf2007-03-12 20:12:07 -04003357 struct btrfs_item *item;
Chris Mason7518a232007-03-12 12:01:18 -04003358 u32 old_left_nritems;
Chris Mason34a38212007-11-07 13:31:03 -05003359 u32 nr;
Chris Masonaa5d6be2007-02-28 16:35:06 -05003360 int ret = 0;
Chris Masondb945352007-10-15 16:15:53 -04003361 u32 this_item_size;
3362 u32 old_left_item_size;
Chris Masoncfed81a2012-03-03 07:40:03 -05003363 struct btrfs_map_token token;
3364
3365 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003366
Chris Mason34a38212007-11-07 13:31:03 -05003367 if (empty)
Chris Mason99d8f832010-07-07 10:51:48 -04003368 nr = min(right_nritems, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003369 else
Chris Mason99d8f832010-07-07 10:51:48 -04003370 nr = min(right_nritems - 1, max_slot);
Chris Mason34a38212007-11-07 13:31:03 -05003371
3372 for (i = 0; i < nr; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003373 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003374
Zheng Yan31840ae2008-09-23 13:14:14 -04003375 if (!empty && push_items > 0) {
3376 if (path->slots[0] < i)
3377 break;
3378 if (path->slots[0] == i) {
3379 int space = btrfs_leaf_free_space(root, right);
3380 if (space + push_space * 2 > free_space)
3381 break;
3382 }
3383 }
3384
Chris Masonbe0e5c02007-01-26 15:51:26 -05003385 if (path->slots[0] == i)
Yan Zheng87b29b22008-12-17 10:21:48 -05003386 push_space += data_size;
Chris Masondb945352007-10-15 16:15:53 -04003387
3388 this_item_size = btrfs_item_size(right, item);
3389 if (this_item_size + sizeof(*item) + push_space > free_space)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003390 break;
Chris Masondb945352007-10-15 16:15:53 -04003391
Chris Masonbe0e5c02007-01-26 15:51:26 -05003392 push_items++;
Chris Masondb945352007-10-15 16:15:53 -04003393 push_space += this_item_size + sizeof(*item);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003394 }
Chris Masondb945352007-10-15 16:15:53 -04003395
Chris Masonbe0e5c02007-01-26 15:51:26 -05003396 if (push_items == 0) {
Chris Mason925baed2008-06-25 16:01:30 -04003397 ret = 1;
3398 goto out;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003399 }
Chris Mason34a38212007-11-07 13:31:03 -05003400 if (!empty && push_items == btrfs_header_nritems(right))
Chris Masona429e512007-04-18 16:15:28 -04003401 WARN_ON(1);
Chris Mason5f39d392007-10-15 16:14:19 -04003402
Chris Masonbe0e5c02007-01-26 15:51:26 -05003403 /* push data from right to left */
Chris Mason5f39d392007-10-15 16:14:19 -04003404 copy_extent_buffer(left, right,
3405 btrfs_item_nr_offset(btrfs_header_nritems(left)),
3406 btrfs_item_nr_offset(0),
3407 push_items * sizeof(struct btrfs_item));
3408
Chris Mason123abc82007-03-14 14:14:43 -04003409 push_space = BTRFS_LEAF_DATA_SIZE(root) -
Chris Masond3977122009-01-05 21:25:51 -05003410 btrfs_item_offset_nr(right, push_items - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003411
3412 copy_extent_buffer(left, right, btrfs_leaf_data(left) +
Chris Masond6025572007-03-30 14:27:56 -04003413 leaf_data_end(root, left) - push_space,
3414 btrfs_leaf_data(right) +
Chris Mason5f39d392007-10-15 16:14:19 -04003415 btrfs_item_offset_nr(right, push_items - 1),
Chris Masond6025572007-03-30 14:27:56 -04003416 push_space);
Chris Mason5f39d392007-10-15 16:14:19 -04003417 old_left_nritems = btrfs_header_nritems(left);
Yan Zheng87b29b22008-12-17 10:21:48 -05003418 BUG_ON(old_left_nritems <= 0);
Chris Masoneb60cea2007-02-02 09:18:22 -05003419
Chris Masondb945352007-10-15 16:15:53 -04003420 old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
Chris Mason0783fcf2007-03-12 20:12:07 -04003421 for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04003422 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04003423
Chris Mason5f39d392007-10-15 16:14:19 -04003424 item = btrfs_item_nr(left, i);
Chris Masondb945352007-10-15 16:15:53 -04003425
Chris Masoncfed81a2012-03-03 07:40:03 -05003426 ioff = btrfs_token_item_offset(left, item, &token);
3427 btrfs_set_token_item_offset(left, item,
3428 ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
3429 &token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003430 }
Chris Mason5f39d392007-10-15 16:14:19 -04003431 btrfs_set_header_nritems(left, old_left_nritems + push_items);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003432
3433 /* fixup right node */
Chris Mason34a38212007-11-07 13:31:03 -05003434 if (push_items > right_nritems) {
Chris Masond3977122009-01-05 21:25:51 -05003435 printk(KERN_CRIT "push items %d nr %u\n", push_items,
3436 right_nritems);
Chris Mason34a38212007-11-07 13:31:03 -05003437 WARN_ON(1);
3438 }
Chris Mason5f39d392007-10-15 16:14:19 -04003439
Chris Mason34a38212007-11-07 13:31:03 -05003440 if (push_items < right_nritems) {
3441 push_space = btrfs_item_offset_nr(right, push_items - 1) -
3442 leaf_data_end(root, right);
3443 memmove_extent_buffer(right, btrfs_leaf_data(right) +
3444 BTRFS_LEAF_DATA_SIZE(root) - push_space,
3445 btrfs_leaf_data(right) +
3446 leaf_data_end(root, right), push_space);
3447
3448 memmove_extent_buffer(right, btrfs_item_nr_offset(0),
Chris Mason5f39d392007-10-15 16:14:19 -04003449 btrfs_item_nr_offset(push_items),
3450 (btrfs_header_nritems(right) - push_items) *
3451 sizeof(struct btrfs_item));
Chris Mason34a38212007-11-07 13:31:03 -05003452 }
Yaneef1c492007-11-26 10:58:13 -05003453 right_nritems -= push_items;
3454 btrfs_set_header_nritems(right, right_nritems);
Chris Mason123abc82007-03-14 14:14:43 -04003455 push_space = BTRFS_LEAF_DATA_SIZE(root);
Chris Mason5f39d392007-10-15 16:14:19 -04003456 for (i = 0; i < right_nritems; i++) {
3457 item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003458
Chris Masoncfed81a2012-03-03 07:40:03 -05003459 push_space = push_space - btrfs_token_item_size(right,
3460 item, &token);
3461 btrfs_set_token_item_offset(right, item, push_space, &token);
Chris Masondb945352007-10-15 16:15:53 -04003462 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003463
Chris Mason5f39d392007-10-15 16:14:19 -04003464 btrfs_mark_buffer_dirty(left);
Chris Mason34a38212007-11-07 13:31:03 -05003465 if (right_nritems)
3466 btrfs_mark_buffer_dirty(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003467 else
3468 clean_tree_block(trans, root, right);
Chris Mason098f59c2007-05-11 11:33:21 -04003469
Chris Mason5f39d392007-10-15 16:14:19 -04003470 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003471 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003472
3473 /* then fixup the leaf pointer in the path */
3474 if (path->slots[0] < push_items) {
3475 path->slots[0] += old_left_nritems;
Chris Mason925baed2008-06-25 16:01:30 -04003476 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003477 free_extent_buffer(path->nodes[0]);
3478 path->nodes[0] = left;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003479 path->slots[1] -= 1;
3480 } else {
Chris Mason925baed2008-06-25 16:01:30 -04003481 btrfs_tree_unlock(left);
Chris Mason5f39d392007-10-15 16:14:19 -04003482 free_extent_buffer(left);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003483 path->slots[0] -= push_items;
3484 }
Chris Masoneb60cea2007-02-02 09:18:22 -05003485 BUG_ON(path->slots[0] < 0);
Chris Masonaa5d6be2007-02-28 16:35:06 -05003486 return ret;
Chris Mason925baed2008-06-25 16:01:30 -04003487out:
3488 btrfs_tree_unlock(left);
3489 free_extent_buffer(left);
3490 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003491}
3492
Chris Mason74123bd2007-02-02 11:05:29 -05003493/*
Chris Mason44871b12009-03-13 10:04:31 -04003494 * push some data in the path leaf to the left, trying to free up at
3495 * least data_size bytes. returns zero if the push worked, nonzero otherwise
Chris Mason99d8f832010-07-07 10:51:48 -04003496 *
3497 * max_slot can put a limit on how far into the leaf we'll push items. The
3498 * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
3499 * items
Chris Mason44871b12009-03-13 10:04:31 -04003500 */
3501static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason99d8f832010-07-07 10:51:48 -04003502 *root, struct btrfs_path *path, int min_data_size,
3503 int data_size, int empty, u32 max_slot)
Chris Mason44871b12009-03-13 10:04:31 -04003504{
3505 struct extent_buffer *right = path->nodes[0];
3506 struct extent_buffer *left;
3507 int slot;
3508 int free_space;
3509 u32 right_nritems;
3510 int ret = 0;
3511
3512 slot = path->slots[1];
3513 if (slot == 0)
3514 return 1;
3515 if (!path->nodes[1])
3516 return 1;
3517
3518 right_nritems = btrfs_header_nritems(right);
3519 if (right_nritems == 0)
3520 return 1;
3521
3522 btrfs_assert_tree_locked(path->nodes[1]);
3523
3524 left = read_node_slot(root, path->nodes[1], slot - 1);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003525 if (left == NULL)
3526 return 1;
3527
Chris Mason44871b12009-03-13 10:04:31 -04003528 btrfs_tree_lock(left);
3529 btrfs_set_lock_blocking(left);
3530
3531 free_space = btrfs_leaf_free_space(root, left);
3532 if (free_space < data_size) {
3533 ret = 1;
3534 goto out;
3535 }
3536
3537 /* cow and double check */
3538 ret = btrfs_cow_block(trans, root, left,
3539 path->nodes[1], slot - 1, &left);
3540 if (ret) {
3541 /* we hit -ENOSPC, but it isn't fatal here */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003542 if (ret == -ENOSPC)
3543 ret = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003544 goto out;
3545 }
3546
3547 free_space = btrfs_leaf_free_space(root, left);
3548 if (free_space < data_size) {
3549 ret = 1;
3550 goto out;
3551 }
3552
Chris Mason99d8f832010-07-07 10:51:48 -04003553 return __push_leaf_left(trans, root, path, min_data_size,
3554 empty, left, free_space, right_nritems,
3555 max_slot);
Chris Mason44871b12009-03-13 10:04:31 -04003556out:
3557 btrfs_tree_unlock(left);
3558 free_extent_buffer(left);
3559 return ret;
3560}
3561
3562/*
Chris Mason74123bd2007-02-02 11:05:29 -05003563 * split the path's leaf in two, making sure there is at least data_size
3564 * available for the resulting leaf level of the path.
3565 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01003566static noinline void copy_for_split(struct btrfs_trans_handle *trans,
3567 struct btrfs_root *root,
3568 struct btrfs_path *path,
3569 struct extent_buffer *l,
3570 struct extent_buffer *right,
3571 int slot, int mid, int nritems)
Chris Masonbe0e5c02007-01-26 15:51:26 -05003572{
Chris Masonbe0e5c02007-01-26 15:51:26 -05003573 int data_copy_size;
3574 int rt_data_off;
3575 int i;
Chris Masond4dbff92007-04-04 14:08:15 -04003576 struct btrfs_disk_key disk_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05003577 struct btrfs_map_token token;
3578
3579 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05003580
Chris Mason5f39d392007-10-15 16:14:19 -04003581 nritems = nritems - mid;
3582 btrfs_set_header_nritems(right, nritems);
3583 data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
3584
3585 copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
3586 btrfs_item_nr_offset(mid),
3587 nritems * sizeof(struct btrfs_item));
3588
3589 copy_extent_buffer(right, l,
Chris Masond6025572007-03-30 14:27:56 -04003590 btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
3591 data_copy_size, btrfs_leaf_data(l) +
3592 leaf_data_end(root, l), data_copy_size);
Chris Mason74123bd2007-02-02 11:05:29 -05003593
Chris Mason5f39d392007-10-15 16:14:19 -04003594 rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
3595 btrfs_item_end_nr(l, mid);
3596
3597 for (i = 0; i < nritems; i++) {
3598 struct btrfs_item *item = btrfs_item_nr(right, i);
Chris Masondb945352007-10-15 16:15:53 -04003599 u32 ioff;
3600
Chris Masoncfed81a2012-03-03 07:40:03 -05003601 ioff = btrfs_token_item_offset(right, item, &token);
3602 btrfs_set_token_item_offset(right, item,
3603 ioff + rt_data_off, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04003604 }
Chris Mason74123bd2007-02-02 11:05:29 -05003605
Chris Mason5f39d392007-10-15 16:14:19 -04003606 btrfs_set_header_nritems(l, mid);
Chris Mason5f39d392007-10-15 16:14:19 -04003607 btrfs_item_key(right, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003608 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003609 path->slots[1] + 1, 1, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003610
3611 btrfs_mark_buffer_dirty(right);
3612 btrfs_mark_buffer_dirty(l);
Chris Masoneb60cea2007-02-02 09:18:22 -05003613 BUG_ON(path->slots[0] != slot);
Chris Mason5f39d392007-10-15 16:14:19 -04003614
Chris Masonbe0e5c02007-01-26 15:51:26 -05003615 if (mid <= slot) {
Chris Mason925baed2008-06-25 16:01:30 -04003616 btrfs_tree_unlock(path->nodes[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04003617 free_extent_buffer(path->nodes[0]);
3618 path->nodes[0] = right;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003619 path->slots[0] -= mid;
3620 path->slots[1] += 1;
Chris Mason925baed2008-06-25 16:01:30 -04003621 } else {
3622 btrfs_tree_unlock(right);
Chris Mason5f39d392007-10-15 16:14:19 -04003623 free_extent_buffer(right);
Chris Mason925baed2008-06-25 16:01:30 -04003624 }
Chris Mason5f39d392007-10-15 16:14:19 -04003625
Chris Masoneb60cea2007-02-02 09:18:22 -05003626 BUG_ON(path->slots[0] < 0);
Chris Mason44871b12009-03-13 10:04:31 -04003627}
3628
3629/*
Chris Mason99d8f832010-07-07 10:51:48 -04003630 * double splits happen when we need to insert a big item in the middle
3631 * of a leaf. A double split can leave us with 3 mostly empty leaves:
3632 * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
3633 * A B C
3634 *
3635 * We avoid this by trying to push the items on either side of our target
3636 * into the adjacent leaves. If all goes well we can avoid the double split
3637 * completely.
3638 */
3639static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
3640 struct btrfs_root *root,
3641 struct btrfs_path *path,
3642 int data_size)
3643{
3644 int ret;
3645 int progress = 0;
3646 int slot;
3647 u32 nritems;
3648
3649 slot = path->slots[0];
3650
3651 /*
3652 * try to push all the items after our slot into the
3653 * right leaf
3654 */
3655 ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
3656 if (ret < 0)
3657 return ret;
3658
3659 if (ret == 0)
3660 progress++;
3661
3662 nritems = btrfs_header_nritems(path->nodes[0]);
3663 /*
3664 * our goal is to get our slot at the start or end of a leaf. If
3665 * we've done so we're done
3666 */
3667 if (path->slots[0] == 0 || path->slots[0] == nritems)
3668 return 0;
3669
3670 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3671 return 0;
3672
3673 /* try to push all the items before our slot into the next leaf */
3674 slot = path->slots[0];
3675 ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
3676 if (ret < 0)
3677 return ret;
3678
3679 if (ret == 0)
3680 progress++;
3681
3682 if (progress)
3683 return 0;
3684 return 1;
3685}
3686
3687/*
Chris Mason44871b12009-03-13 10:04:31 -04003688 * split the path's leaf in two, making sure there is at least data_size
3689 * available for the resulting leaf level of the path.
3690 *
3691 * returns 0 if all went well and < 0 on failure.
3692 */
3693static noinline int split_leaf(struct btrfs_trans_handle *trans,
3694 struct btrfs_root *root,
3695 struct btrfs_key *ins_key,
3696 struct btrfs_path *path, int data_size,
3697 int extend)
3698{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003699 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04003700 struct extent_buffer *l;
3701 u32 nritems;
3702 int mid;
3703 int slot;
3704 struct extent_buffer *right;
3705 int ret = 0;
3706 int wret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003707 int split;
Chris Mason44871b12009-03-13 10:04:31 -04003708 int num_doubles = 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003709 int tried_avoid_double = 0;
Chris Mason44871b12009-03-13 10:04:31 -04003710
Yan, Zhenga5719522009-09-24 09:17:31 -04003711 l = path->nodes[0];
3712 slot = path->slots[0];
3713 if (extend && data_size + btrfs_item_size_nr(l, slot) +
3714 sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
3715 return -EOVERFLOW;
3716
Chris Mason44871b12009-03-13 10:04:31 -04003717 /* first try to make some room by pushing left and right */
Chris Mason99d8f832010-07-07 10:51:48 -04003718 if (data_size) {
3719 wret = push_leaf_right(trans, root, path, data_size,
3720 data_size, 0, 0);
Chris Mason44871b12009-03-13 10:04:31 -04003721 if (wret < 0)
3722 return wret;
3723 if (wret) {
Chris Mason99d8f832010-07-07 10:51:48 -04003724 wret = push_leaf_left(trans, root, path, data_size,
3725 data_size, 0, (u32)-1);
Chris Mason44871b12009-03-13 10:04:31 -04003726 if (wret < 0)
3727 return wret;
3728 }
3729 l = path->nodes[0];
3730
3731 /* did the pushes work? */
3732 if (btrfs_leaf_free_space(root, l) >= data_size)
3733 return 0;
3734 }
3735
3736 if (!path->nodes[1]) {
3737 ret = insert_new_root(trans, root, path, 1);
3738 if (ret)
3739 return ret;
3740 }
3741again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003742 split = 1;
Chris Mason44871b12009-03-13 10:04:31 -04003743 l = path->nodes[0];
3744 slot = path->slots[0];
3745 nritems = btrfs_header_nritems(l);
3746 mid = (nritems + 1) / 2;
3747
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003748 if (mid <= slot) {
3749 if (nritems == 1 ||
3750 leaf_space_used(l, mid, nritems - mid) + data_size >
3751 BTRFS_LEAF_DATA_SIZE(root)) {
3752 if (slot >= nritems) {
3753 split = 0;
3754 } else {
3755 mid = slot;
3756 if (mid != nritems &&
3757 leaf_space_used(l, mid, nritems - mid) +
3758 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003759 if (data_size && !tried_avoid_double)
3760 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003761 split = 2;
3762 }
3763 }
3764 }
3765 } else {
3766 if (leaf_space_used(l, 0, mid) + data_size >
3767 BTRFS_LEAF_DATA_SIZE(root)) {
3768 if (!extend && data_size && slot == 0) {
3769 split = 0;
3770 } else if ((extend || !data_size) && slot == 0) {
3771 mid = 1;
3772 } else {
3773 mid = slot;
3774 if (mid != nritems &&
3775 leaf_space_used(l, mid, nritems - mid) +
3776 data_size > BTRFS_LEAF_DATA_SIZE(root)) {
Chris Mason99d8f832010-07-07 10:51:48 -04003777 if (data_size && !tried_avoid_double)
3778 goto push_for_double;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003779 split = 2 ;
3780 }
3781 }
3782 }
3783 }
3784
3785 if (split == 0)
3786 btrfs_cpu_key_to_disk(&disk_key, ins_key);
3787 else
3788 btrfs_item_key(l, &disk_key, mid);
3789
3790 right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
Chris Mason44871b12009-03-13 10:04:31 -04003791 root->root_key.objectid,
Jan Schmidt5581a512012-05-16 17:04:52 +02003792 &disk_key, 0, l->start, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003793 if (IS_ERR(right))
Chris Mason44871b12009-03-13 10:04:31 -04003794 return PTR_ERR(right);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003795
3796 root_add_used(root, root->leafsize);
Chris Mason44871b12009-03-13 10:04:31 -04003797
3798 memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
3799 btrfs_set_header_bytenr(right, right->start);
3800 btrfs_set_header_generation(right, trans->transid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003801 btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
Chris Mason44871b12009-03-13 10:04:31 -04003802 btrfs_set_header_owner(right, root->root_key.objectid);
3803 btrfs_set_header_level(right, 0);
3804 write_extent_buffer(right, root->fs_info->fsid,
3805 (unsigned long)btrfs_header_fsid(right),
3806 BTRFS_FSID_SIZE);
3807
3808 write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
3809 (unsigned long)btrfs_header_chunk_tree_uuid(right),
3810 BTRFS_UUID_SIZE);
3811
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003812 if (split == 0) {
3813 if (mid <= slot) {
3814 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003815 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003816 path->slots[1] + 1, 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003817 btrfs_tree_unlock(path->nodes[0]);
3818 free_extent_buffer(path->nodes[0]);
3819 path->nodes[0] = right;
3820 path->slots[0] = 0;
3821 path->slots[1] += 1;
3822 } else {
3823 btrfs_set_header_nritems(right, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01003824 insert_ptr(trans, root, path, &disk_key, right->start,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02003825 path->slots[1], 1, 0);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003826 btrfs_tree_unlock(path->nodes[0]);
3827 free_extent_buffer(path->nodes[0]);
3828 path->nodes[0] = right;
3829 path->slots[0] = 0;
Jeff Mahoney143bede2012-03-01 14:56:26 +01003830 if (path->slots[1] == 0)
3831 fixup_low_keys(trans, root, path,
3832 &disk_key, 1);
Chris Mason44871b12009-03-13 10:04:31 -04003833 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003834 btrfs_mark_buffer_dirty(right);
3835 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04003836 }
3837
Jeff Mahoney143bede2012-03-01 14:56:26 +01003838 copy_for_split(trans, root, path, l, right, slot, mid, nritems);
Chris Mason44871b12009-03-13 10:04:31 -04003839
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003840 if (split == 2) {
Chris Masoncc0c5532007-10-25 15:42:57 -04003841 BUG_ON(num_doubles != 0);
3842 num_doubles++;
3843 goto again;
Chris Mason3326d1b2007-10-15 16:18:25 -04003844 }
Chris Mason44871b12009-03-13 10:04:31 -04003845
Jeff Mahoney143bede2012-03-01 14:56:26 +01003846 return 0;
Chris Mason99d8f832010-07-07 10:51:48 -04003847
3848push_for_double:
3849 push_for_double_split(trans, root, path, data_size);
3850 tried_avoid_double = 1;
3851 if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
3852 return 0;
3853 goto again;
Chris Masonbe0e5c02007-01-26 15:51:26 -05003854}
3855
Yan, Zhengad48fd752009-11-12 09:33:58 +00003856static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
3857 struct btrfs_root *root,
3858 struct btrfs_path *path, int ins_len)
Chris Mason459931e2008-12-10 09:10:46 -05003859{
Yan, Zhengad48fd752009-11-12 09:33:58 +00003860 struct btrfs_key key;
Chris Mason459931e2008-12-10 09:10:46 -05003861 struct extent_buffer *leaf;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003862 struct btrfs_file_extent_item *fi;
3863 u64 extent_len = 0;
3864 u32 item_size;
3865 int ret;
Chris Mason459931e2008-12-10 09:10:46 -05003866
3867 leaf = path->nodes[0];
Yan, Zhengad48fd752009-11-12 09:33:58 +00003868 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3869
3870 BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
3871 key.type != BTRFS_EXTENT_CSUM_KEY);
3872
3873 if (btrfs_leaf_free_space(root, leaf) >= ins_len)
3874 return 0;
Chris Mason459931e2008-12-10 09:10:46 -05003875
3876 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003877 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3878 fi = btrfs_item_ptr(leaf, path->slots[0],
3879 struct btrfs_file_extent_item);
3880 extent_len = btrfs_file_extent_num_bytes(leaf, fi);
3881 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003882 btrfs_release_path(path);
Chris Mason459931e2008-12-10 09:10:46 -05003883
Chris Mason459931e2008-12-10 09:10:46 -05003884 path->keep_locks = 1;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003885 path->search_for_split = 1;
3886 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Chris Mason459931e2008-12-10 09:10:46 -05003887 path->search_for_split = 0;
Yan, Zhengad48fd752009-11-12 09:33:58 +00003888 if (ret < 0)
3889 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003890
Yan, Zhengad48fd752009-11-12 09:33:58 +00003891 ret = -EAGAIN;
3892 leaf = path->nodes[0];
Chris Mason459931e2008-12-10 09:10:46 -05003893 /* if our item isn't there or got smaller, return now */
Yan, Zhengad48fd752009-11-12 09:33:58 +00003894 if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
3895 goto err;
3896
Chris Mason109f6ae2010-04-02 09:20:18 -04003897 /* the leaf has changed, it now has room. return now */
3898 if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
3899 goto err;
3900
Yan, Zhengad48fd752009-11-12 09:33:58 +00003901 if (key.type == BTRFS_EXTENT_DATA_KEY) {
3902 fi = btrfs_item_ptr(leaf, path->slots[0],
3903 struct btrfs_file_extent_item);
3904 if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
3905 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003906 }
3907
Chris Masonb9473432009-03-13 11:00:37 -04003908 btrfs_set_path_blocking(path);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003909 ret = split_leaf(trans, root, &key, path, ins_len, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04003910 if (ret)
3911 goto err;
Chris Mason459931e2008-12-10 09:10:46 -05003912
Yan, Zhengad48fd752009-11-12 09:33:58 +00003913 path->keep_locks = 0;
Chris Masonb9473432009-03-13 11:00:37 -04003914 btrfs_unlock_up_safe(path, 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003915 return 0;
3916err:
3917 path->keep_locks = 0;
3918 return ret;
3919}
3920
3921static noinline int split_item(struct btrfs_trans_handle *trans,
3922 struct btrfs_root *root,
3923 struct btrfs_path *path,
3924 struct btrfs_key *new_key,
3925 unsigned long split_offset)
3926{
3927 struct extent_buffer *leaf;
3928 struct btrfs_item *item;
3929 struct btrfs_item *new_item;
3930 int slot;
3931 char *buf;
3932 u32 nritems;
3933 u32 item_size;
3934 u32 orig_offset;
3935 struct btrfs_disk_key disk_key;
3936
Chris Masonb9473432009-03-13 11:00:37 -04003937 leaf = path->nodes[0];
3938 BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
3939
Chris Masonb4ce94d2009-02-04 09:25:08 -05003940 btrfs_set_path_blocking(path);
3941
Chris Mason459931e2008-12-10 09:10:46 -05003942 item = btrfs_item_nr(leaf, path->slots[0]);
3943 orig_offset = btrfs_item_offset(leaf, item);
3944 item_size = btrfs_item_size(leaf, item);
3945
Chris Mason459931e2008-12-10 09:10:46 -05003946 buf = kmalloc(item_size, GFP_NOFS);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003947 if (!buf)
3948 return -ENOMEM;
3949
Chris Mason459931e2008-12-10 09:10:46 -05003950 read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
3951 path->slots[0]), item_size);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003952
Chris Mason459931e2008-12-10 09:10:46 -05003953 slot = path->slots[0] + 1;
Chris Mason459931e2008-12-10 09:10:46 -05003954 nritems = btrfs_header_nritems(leaf);
Chris Mason459931e2008-12-10 09:10:46 -05003955 if (slot != nritems) {
3956 /* shift the items */
3957 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
Yan, Zhengad48fd752009-11-12 09:33:58 +00003958 btrfs_item_nr_offset(slot),
3959 (nritems - slot) * sizeof(struct btrfs_item));
Chris Mason459931e2008-12-10 09:10:46 -05003960 }
3961
3962 btrfs_cpu_key_to_disk(&disk_key, new_key);
3963 btrfs_set_item_key(leaf, &disk_key, slot);
3964
3965 new_item = btrfs_item_nr(leaf, slot);
3966
3967 btrfs_set_item_offset(leaf, new_item, orig_offset);
3968 btrfs_set_item_size(leaf, new_item, item_size - split_offset);
3969
3970 btrfs_set_item_offset(leaf, item,
3971 orig_offset + item_size - split_offset);
3972 btrfs_set_item_size(leaf, item, split_offset);
3973
3974 btrfs_set_header_nritems(leaf, nritems + 1);
3975
3976 /* write the data for the start of the original item */
3977 write_extent_buffer(leaf, buf,
3978 btrfs_item_ptr_offset(leaf, path->slots[0]),
3979 split_offset);
3980
3981 /* write the data for the new item */
3982 write_extent_buffer(leaf, buf + split_offset,
3983 btrfs_item_ptr_offset(leaf, slot),
3984 item_size - split_offset);
3985 btrfs_mark_buffer_dirty(leaf);
3986
Yan, Zhengad48fd752009-11-12 09:33:58 +00003987 BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
Chris Mason459931e2008-12-10 09:10:46 -05003988 kfree(buf);
Yan, Zhengad48fd752009-11-12 09:33:58 +00003989 return 0;
3990}
3991
3992/*
3993 * This function splits a single item into two items,
3994 * giving 'new_key' to the new item and splitting the
3995 * old one at split_offset (from the start of the item).
3996 *
3997 * The path may be released by this operation. After
3998 * the split, the path is pointing to the old item. The
3999 * new item is going to be in the same node as the old one.
4000 *
4001 * Note, the item being split must be smaller enough to live alone on
4002 * a tree block with room for one extra struct btrfs_item
4003 *
4004 * This allows us to split the item in place, keeping a lock on the
4005 * leaf the entire time.
4006 */
4007int btrfs_split_item(struct btrfs_trans_handle *trans,
4008 struct btrfs_root *root,
4009 struct btrfs_path *path,
4010 struct btrfs_key *new_key,
4011 unsigned long split_offset)
4012{
4013 int ret;
4014 ret = setup_leaf_for_split(trans, root, path,
4015 sizeof(struct btrfs_item));
4016 if (ret)
4017 return ret;
4018
4019 ret = split_item(trans, root, path, new_key, split_offset);
Chris Mason459931e2008-12-10 09:10:46 -05004020 return ret;
4021}
4022
4023/*
Yan, Zhengad48fd752009-11-12 09:33:58 +00004024 * This function duplicate a item, giving 'new_key' to the new item.
4025 * It guarantees both items live in the same tree leaf and the new item
4026 * is contiguous with the original item.
4027 *
4028 * This allows us to split file extent in place, keeping a lock on the
4029 * leaf the entire time.
4030 */
4031int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
4032 struct btrfs_root *root,
4033 struct btrfs_path *path,
4034 struct btrfs_key *new_key)
4035{
4036 struct extent_buffer *leaf;
4037 int ret;
4038 u32 item_size;
4039
4040 leaf = path->nodes[0];
4041 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
4042 ret = setup_leaf_for_split(trans, root, path,
4043 item_size + sizeof(struct btrfs_item));
4044 if (ret)
4045 return ret;
4046
4047 path->slots[0]++;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004048 setup_items_for_insert(trans, root, path, new_key, &item_size,
4049 item_size, item_size +
4050 sizeof(struct btrfs_item), 1);
Yan, Zhengad48fd752009-11-12 09:33:58 +00004051 leaf = path->nodes[0];
4052 memcpy_extent_buffer(leaf,
4053 btrfs_item_ptr_offset(leaf, path->slots[0]),
4054 btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
4055 item_size);
4056 return 0;
4057}
4058
4059/*
Chris Masond352ac62008-09-29 15:18:18 -04004060 * make the item pointed to by the path smaller. new_size indicates
4061 * how small to make it, and from_end tells us if we just chop bytes
4062 * off the end of the item or if we shift the item to chop bytes off
4063 * the front.
4064 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004065void btrfs_truncate_item(struct btrfs_trans_handle *trans,
4066 struct btrfs_root *root,
4067 struct btrfs_path *path,
4068 u32 new_size, int from_end)
Chris Masonb18c6682007-04-17 13:26:50 -04004069{
Chris Masonb18c6682007-04-17 13:26:50 -04004070 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004071 struct extent_buffer *leaf;
4072 struct btrfs_item *item;
Chris Masonb18c6682007-04-17 13:26:50 -04004073 u32 nritems;
4074 unsigned int data_end;
4075 unsigned int old_data_start;
4076 unsigned int old_size;
4077 unsigned int size_diff;
4078 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004079 struct btrfs_map_token token;
4080
4081 btrfs_init_map_token(&token);
Chris Masonb18c6682007-04-17 13:26:50 -04004082
Chris Mason5f39d392007-10-15 16:14:19 -04004083 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -04004084 slot = path->slots[0];
4085
4086 old_size = btrfs_item_size_nr(leaf, slot);
4087 if (old_size == new_size)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004088 return;
Chris Masonb18c6682007-04-17 13:26:50 -04004089
Chris Mason5f39d392007-10-15 16:14:19 -04004090 nritems = btrfs_header_nritems(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004091 data_end = leaf_data_end(root, leaf);
4092
Chris Mason5f39d392007-10-15 16:14:19 -04004093 old_data_start = btrfs_item_offset_nr(leaf, slot);
Chris Mason179e29e2007-11-01 11:28:41 -04004094
Chris Masonb18c6682007-04-17 13:26:50 -04004095 size_diff = old_size - new_size;
4096
4097 BUG_ON(slot < 0);
4098 BUG_ON(slot >= nritems);
4099
4100 /*
4101 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4102 */
4103 /* first correct the data pointers */
4104 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004105 u32 ioff;
4106 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004107
Chris Masoncfed81a2012-03-03 07:40:03 -05004108 ioff = btrfs_token_item_offset(leaf, item, &token);
4109 btrfs_set_token_item_offset(leaf, item,
4110 ioff + size_diff, &token);
Chris Masonb18c6682007-04-17 13:26:50 -04004111 }
Chris Masondb945352007-10-15 16:15:53 -04004112
Chris Masonb18c6682007-04-17 13:26:50 -04004113 /* shift the data */
Chris Mason179e29e2007-11-01 11:28:41 -04004114 if (from_end) {
4115 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4116 data_end + size_diff, btrfs_leaf_data(leaf) +
4117 data_end, old_data_start + new_size - data_end);
4118 } else {
4119 struct btrfs_disk_key disk_key;
4120 u64 offset;
4121
4122 btrfs_item_key(leaf, &disk_key, slot);
4123
4124 if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
4125 unsigned long ptr;
4126 struct btrfs_file_extent_item *fi;
4127
4128 fi = btrfs_item_ptr(leaf, slot,
4129 struct btrfs_file_extent_item);
4130 fi = (struct btrfs_file_extent_item *)(
4131 (unsigned long)fi - size_diff);
4132
4133 if (btrfs_file_extent_type(leaf, fi) ==
4134 BTRFS_FILE_EXTENT_INLINE) {
4135 ptr = btrfs_item_ptr_offset(leaf, slot);
4136 memmove_extent_buffer(leaf, ptr,
Chris Masond3977122009-01-05 21:25:51 -05004137 (unsigned long)fi,
4138 offsetof(struct btrfs_file_extent_item,
Chris Mason179e29e2007-11-01 11:28:41 -04004139 disk_bytenr));
4140 }
4141 }
4142
4143 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4144 data_end + size_diff, btrfs_leaf_data(leaf) +
4145 data_end, old_data_start - data_end);
4146
4147 offset = btrfs_disk_key_offset(&disk_key);
4148 btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
4149 btrfs_set_item_key(leaf, &disk_key, slot);
4150 if (slot == 0)
4151 fixup_low_keys(trans, root, path, &disk_key, 1);
4152 }
Chris Mason5f39d392007-10-15 16:14:19 -04004153
4154 item = btrfs_item_nr(leaf, slot);
4155 btrfs_set_item_size(leaf, item, new_size);
4156 btrfs_mark_buffer_dirty(leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004157
Chris Mason5f39d392007-10-15 16:14:19 -04004158 if (btrfs_leaf_free_space(root, leaf) < 0) {
4159 btrfs_print_leaf(root, leaf);
Chris Masonb18c6682007-04-17 13:26:50 -04004160 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004161 }
Chris Masonb18c6682007-04-17 13:26:50 -04004162}
4163
Chris Masond352ac62008-09-29 15:18:18 -04004164/*
4165 * make the item pointed to by the path bigger, data_size is the new size.
4166 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004167void btrfs_extend_item(struct btrfs_trans_handle *trans,
4168 struct btrfs_root *root, struct btrfs_path *path,
4169 u32 data_size)
Chris Mason6567e832007-04-16 09:22:45 -04004170{
Chris Mason6567e832007-04-16 09:22:45 -04004171 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04004172 struct extent_buffer *leaf;
4173 struct btrfs_item *item;
Chris Mason6567e832007-04-16 09:22:45 -04004174 u32 nritems;
4175 unsigned int data_end;
4176 unsigned int old_data;
4177 unsigned int old_size;
4178 int i;
Chris Masoncfed81a2012-03-03 07:40:03 -05004179 struct btrfs_map_token token;
4180
4181 btrfs_init_map_token(&token);
Chris Mason6567e832007-04-16 09:22:45 -04004182
Chris Mason5f39d392007-10-15 16:14:19 -04004183 leaf = path->nodes[0];
Chris Mason6567e832007-04-16 09:22:45 -04004184
Chris Mason5f39d392007-10-15 16:14:19 -04004185 nritems = btrfs_header_nritems(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004186 data_end = leaf_data_end(root, leaf);
4187
Chris Mason5f39d392007-10-15 16:14:19 -04004188 if (btrfs_leaf_free_space(root, leaf) < data_size) {
4189 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004190 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004191 }
Chris Mason6567e832007-04-16 09:22:45 -04004192 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -04004193 old_data = btrfs_item_end_nr(leaf, slot);
Chris Mason6567e832007-04-16 09:22:45 -04004194
4195 BUG_ON(slot < 0);
Chris Mason3326d1b2007-10-15 16:18:25 -04004196 if (slot >= nritems) {
4197 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004198 printk(KERN_CRIT "slot %d too large, nritems %d\n",
4199 slot, nritems);
Chris Mason3326d1b2007-10-15 16:18:25 -04004200 BUG_ON(1);
4201 }
Chris Mason6567e832007-04-16 09:22:45 -04004202
4203 /*
4204 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4205 */
4206 /* first correct the data pointers */
4207 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004208 u32 ioff;
4209 item = btrfs_item_nr(leaf, i);
Chris Masondb945352007-10-15 16:15:53 -04004210
Chris Masoncfed81a2012-03-03 07:40:03 -05004211 ioff = btrfs_token_item_offset(leaf, item, &token);
4212 btrfs_set_token_item_offset(leaf, item,
4213 ioff - data_size, &token);
Chris Mason6567e832007-04-16 09:22:45 -04004214 }
Chris Mason5f39d392007-10-15 16:14:19 -04004215
Chris Mason6567e832007-04-16 09:22:45 -04004216 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004217 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason6567e832007-04-16 09:22:45 -04004218 data_end - data_size, btrfs_leaf_data(leaf) +
4219 data_end, old_data - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004220
Chris Mason6567e832007-04-16 09:22:45 -04004221 data_end = old_data;
Chris Mason5f39d392007-10-15 16:14:19 -04004222 old_size = btrfs_item_size_nr(leaf, slot);
4223 item = btrfs_item_nr(leaf, slot);
4224 btrfs_set_item_size(leaf, item, old_size + data_size);
4225 btrfs_mark_buffer_dirty(leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004226
Chris Mason5f39d392007-10-15 16:14:19 -04004227 if (btrfs_leaf_free_space(root, leaf) < 0) {
4228 btrfs_print_leaf(root, leaf);
Chris Mason6567e832007-04-16 09:22:45 -04004229 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004230 }
Chris Mason6567e832007-04-16 09:22:45 -04004231}
4232
Chris Mason74123bd2007-02-02 11:05:29 -05004233/*
Chris Masond352ac62008-09-29 15:18:18 -04004234 * Given a key and some data, insert items into the tree.
Chris Mason74123bd2007-02-02 11:05:29 -05004235 * This does all the path init required, making room in the tree if needed.
Josef Bacikf3465ca2008-11-12 14:19:50 -05004236 * Returns the number of keys that were inserted.
4237 */
4238int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
4239 struct btrfs_root *root,
4240 struct btrfs_path *path,
4241 struct btrfs_key *cpu_key, u32 *data_size,
4242 int nr)
4243{
4244 struct extent_buffer *leaf;
4245 struct btrfs_item *item;
4246 int ret = 0;
4247 int slot;
Josef Bacikf3465ca2008-11-12 14:19:50 -05004248 int i;
4249 u32 nritems;
4250 u32 total_data = 0;
4251 u32 total_size = 0;
4252 unsigned int data_end;
4253 struct btrfs_disk_key disk_key;
4254 struct btrfs_key found_key;
Chris Masoncfed81a2012-03-03 07:40:03 -05004255 struct btrfs_map_token token;
4256
4257 btrfs_init_map_token(&token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004258
Yan Zheng87b29b22008-12-17 10:21:48 -05004259 for (i = 0; i < nr; i++) {
4260 if (total_size + data_size[i] + sizeof(struct btrfs_item) >
4261 BTRFS_LEAF_DATA_SIZE(root)) {
4262 break;
4263 nr = i;
4264 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004265 total_data += data_size[i];
Yan Zheng87b29b22008-12-17 10:21:48 -05004266 total_size += data_size[i] + sizeof(struct btrfs_item);
4267 }
4268 BUG_ON(nr == 0);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004269
Josef Bacikf3465ca2008-11-12 14:19:50 -05004270 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4271 if (ret == 0)
4272 return -EEXIST;
4273 if (ret < 0)
4274 goto out;
4275
Josef Bacikf3465ca2008-11-12 14:19:50 -05004276 leaf = path->nodes[0];
4277
4278 nritems = btrfs_header_nritems(leaf);
4279 data_end = leaf_data_end(root, leaf);
4280
4281 if (btrfs_leaf_free_space(root, leaf) < total_size) {
4282 for (i = nr; i >= 0; i--) {
4283 total_data -= data_size[i];
4284 total_size -= data_size[i] + sizeof(struct btrfs_item);
4285 if (total_size < btrfs_leaf_free_space(root, leaf))
4286 break;
4287 }
4288 nr = i;
4289 }
4290
4291 slot = path->slots[0];
4292 BUG_ON(slot < 0);
4293
4294 if (slot != nritems) {
4295 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
4296
4297 item = btrfs_item_nr(leaf, slot);
4298 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4299
4300 /* figure out how many keys we can insert in here */
4301 total_data = data_size[0];
4302 for (i = 1; i < nr; i++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004303 if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
Josef Bacikf3465ca2008-11-12 14:19:50 -05004304 break;
4305 total_data += data_size[i];
4306 }
4307 nr = i;
4308
4309 if (old_data < data_end) {
4310 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004311 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Josef Bacikf3465ca2008-11-12 14:19:50 -05004312 slot, old_data, data_end);
4313 BUG_ON(1);
4314 }
4315 /*
4316 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4317 */
4318 /* first correct the data pointers */
Josef Bacikf3465ca2008-11-12 14:19:50 -05004319 for (i = slot; i < nritems; i++) {
4320 u32 ioff;
4321
4322 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004323 ioff = btrfs_token_item_offset(leaf, item, &token);
4324 btrfs_set_token_item_offset(leaf, item,
4325 ioff - total_data, &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004326 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05004327 /* shift the items */
4328 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
4329 btrfs_item_nr_offset(slot),
4330 (nritems - slot) * sizeof(struct btrfs_item));
4331
4332 /* shift the data */
4333 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
4334 data_end - total_data, btrfs_leaf_data(leaf) +
4335 data_end, old_data - data_end);
4336 data_end = old_data;
4337 } else {
4338 /*
4339 * this sucks but it has to be done, if we are inserting at
4340 * the end of the leaf only insert 1 of the items, since we
4341 * have no way of knowing whats on the next leaf and we'd have
4342 * to drop our current locks to figure it out
4343 */
4344 nr = 1;
4345 }
4346
4347 /* setup the item for the new data */
4348 for (i = 0; i < nr; i++) {
4349 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4350 btrfs_set_item_key(leaf, &disk_key, slot + i);
4351 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004352 btrfs_set_token_item_offset(leaf, item,
4353 data_end - data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004354 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004355 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004356 }
4357 btrfs_set_header_nritems(leaf, nritems + nr);
4358 btrfs_mark_buffer_dirty(leaf);
4359
4360 ret = 0;
4361 if (slot == 0) {
4362 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004363 fixup_low_keys(trans, root, path, &disk_key, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05004364 }
4365
4366 if (btrfs_leaf_free_space(root, leaf) < 0) {
4367 btrfs_print_leaf(root, leaf);
4368 BUG();
4369 }
4370out:
4371 if (!ret)
4372 ret = nr;
4373 return ret;
4374}
4375
4376/*
Chris Mason44871b12009-03-13 10:04:31 -04004377 * this is a helper for btrfs_insert_empty_items, the main goal here is
4378 * to save stack depth by doing the bulk of the work in a function
4379 * that doesn't call btrfs_search_slot
Chris Mason74123bd2007-02-02 11:05:29 -05004380 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004381void setup_items_for_insert(struct btrfs_trans_handle *trans,
4382 struct btrfs_root *root, struct btrfs_path *path,
4383 struct btrfs_key *cpu_key, u32 *data_size,
4384 u32 total_data, u32 total_size, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004385{
Chris Mason5f39d392007-10-15 16:14:19 -04004386 struct btrfs_item *item;
Chris Mason9c583092008-01-29 15:15:18 -05004387 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004388 u32 nritems;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004389 unsigned int data_end;
Chris Masone2fa7222007-03-12 16:22:34 -04004390 struct btrfs_disk_key disk_key;
Chris Mason44871b12009-03-13 10:04:31 -04004391 struct extent_buffer *leaf;
4392 int slot;
Chris Masoncfed81a2012-03-03 07:40:03 -05004393 struct btrfs_map_token token;
4394
4395 btrfs_init_map_token(&token);
Chris Masone2fa7222007-03-12 16:22:34 -04004396
Chris Mason5f39d392007-10-15 16:14:19 -04004397 leaf = path->nodes[0];
Chris Mason44871b12009-03-13 10:04:31 -04004398 slot = path->slots[0];
Chris Mason74123bd2007-02-02 11:05:29 -05004399
Chris Mason5f39d392007-10-15 16:14:19 -04004400 nritems = btrfs_header_nritems(leaf);
Chris Mason123abc82007-03-14 14:14:43 -04004401 data_end = leaf_data_end(root, leaf);
Chris Masoneb60cea2007-02-02 09:18:22 -05004402
Chris Masonf25956c2008-09-12 15:32:53 -04004403 if (btrfs_leaf_free_space(root, leaf) < total_size) {
Chris Mason3326d1b2007-10-15 16:18:25 -04004404 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004405 printk(KERN_CRIT "not enough freespace need %u have %d\n",
Chris Mason9c583092008-01-29 15:15:18 -05004406 total_size, btrfs_leaf_free_space(root, leaf));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004407 BUG();
Chris Masond4dbff92007-04-04 14:08:15 -04004408 }
Chris Mason5f39d392007-10-15 16:14:19 -04004409
Chris Masonbe0e5c02007-01-26 15:51:26 -05004410 if (slot != nritems) {
Chris Mason5f39d392007-10-15 16:14:19 -04004411 unsigned int old_data = btrfs_item_end_nr(leaf, slot);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004412
Chris Mason5f39d392007-10-15 16:14:19 -04004413 if (old_data < data_end) {
4414 btrfs_print_leaf(root, leaf);
Chris Masond3977122009-01-05 21:25:51 -05004415 printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
Chris Mason5f39d392007-10-15 16:14:19 -04004416 slot, old_data, data_end);
4417 BUG_ON(1);
4418 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004419 /*
4420 * item0..itemN ... dataN.offset..dataN.size .. data0.size
4421 */
4422 /* first correct the data pointers */
Chris Mason0783fcf2007-03-12 20:12:07 -04004423 for (i = slot; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004424 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004425
Chris Mason5f39d392007-10-15 16:14:19 -04004426 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004427 ioff = btrfs_token_item_offset(leaf, item, &token);
4428 btrfs_set_token_item_offset(leaf, item,
4429 ioff - total_data, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004430 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004431 /* shift the items */
Chris Mason9c583092008-01-29 15:15:18 -05004432 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
Chris Mason5f39d392007-10-15 16:14:19 -04004433 btrfs_item_nr_offset(slot),
Chris Masond6025572007-03-30 14:27:56 -04004434 (nritems - slot) * sizeof(struct btrfs_item));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004435
4436 /* shift the data */
Chris Mason5f39d392007-10-15 16:14:19 -04004437 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Mason9c583092008-01-29 15:15:18 -05004438 data_end - total_data, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004439 data_end, old_data - data_end);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004440 data_end = old_data;
4441 }
Chris Mason5f39d392007-10-15 16:14:19 -04004442
Chris Mason62e27492007-03-15 12:56:47 -04004443 /* setup the item for the new data */
Chris Mason9c583092008-01-29 15:15:18 -05004444 for (i = 0; i < nr; i++) {
4445 btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
4446 btrfs_set_item_key(leaf, &disk_key, slot + i);
4447 item = btrfs_item_nr(leaf, slot + i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004448 btrfs_set_token_item_offset(leaf, item,
4449 data_end - data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004450 data_end -= data_size[i];
Chris Masoncfed81a2012-03-03 07:40:03 -05004451 btrfs_set_token_item_size(leaf, item, data_size[i], &token);
Chris Mason9c583092008-01-29 15:15:18 -05004452 }
Chris Mason44871b12009-03-13 10:04:31 -04004453
Chris Mason9c583092008-01-29 15:15:18 -05004454 btrfs_set_header_nritems(leaf, nritems + nr);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004455
Chris Mason5a01a2e2008-01-30 11:43:54 -05004456 if (slot == 0) {
4457 btrfs_cpu_key_to_disk(&disk_key, cpu_key);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004458 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Mason5a01a2e2008-01-30 11:43:54 -05004459 }
Chris Masonb9473432009-03-13 11:00:37 -04004460 btrfs_unlock_up_safe(path, 1);
4461 btrfs_mark_buffer_dirty(leaf);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004462
Chris Mason5f39d392007-10-15 16:14:19 -04004463 if (btrfs_leaf_free_space(root, leaf) < 0) {
4464 btrfs_print_leaf(root, leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004465 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04004466 }
Chris Mason44871b12009-03-13 10:04:31 -04004467}
4468
4469/*
4470 * Given a key and some data, insert items into the tree.
4471 * This does all the path init required, making room in the tree if needed.
4472 */
4473int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
4474 struct btrfs_root *root,
4475 struct btrfs_path *path,
4476 struct btrfs_key *cpu_key, u32 *data_size,
4477 int nr)
4478{
Chris Mason44871b12009-03-13 10:04:31 -04004479 int ret = 0;
4480 int slot;
4481 int i;
4482 u32 total_size = 0;
4483 u32 total_data = 0;
4484
4485 for (i = 0; i < nr; i++)
4486 total_data += data_size[i];
4487
4488 total_size = total_data + (nr * sizeof(struct btrfs_item));
4489 ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
4490 if (ret == 0)
4491 return -EEXIST;
4492 if (ret < 0)
Jeff Mahoney143bede2012-03-01 14:56:26 +01004493 return ret;
Chris Mason44871b12009-03-13 10:04:31 -04004494
Chris Mason44871b12009-03-13 10:04:31 -04004495 slot = path->slots[0];
4496 BUG_ON(slot < 0);
4497
Jeff Mahoney143bede2012-03-01 14:56:26 +01004498 setup_items_for_insert(trans, root, path, cpu_key, data_size,
Chris Mason44871b12009-03-13 10:04:31 -04004499 total_data, total_size, nr);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004500 return 0;
Chris Mason62e27492007-03-15 12:56:47 -04004501}
4502
4503/*
4504 * Given a key and some data, insert an item into the tree.
4505 * This does all the path init required, making room in the tree if needed.
4506 */
Chris Masone089f052007-03-16 16:20:31 -04004507int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
4508 *root, struct btrfs_key *cpu_key, void *data, u32
4509 data_size)
Chris Mason62e27492007-03-15 12:56:47 -04004510{
4511 int ret = 0;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004512 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004513 struct extent_buffer *leaf;
4514 unsigned long ptr;
Chris Mason62e27492007-03-15 12:56:47 -04004515
Chris Mason2c90e5d2007-04-02 10:50:19 -04004516 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00004517 if (!path)
4518 return -ENOMEM;
Chris Mason2c90e5d2007-04-02 10:50:19 -04004519 ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
Chris Mason62e27492007-03-15 12:56:47 -04004520 if (!ret) {
Chris Mason5f39d392007-10-15 16:14:19 -04004521 leaf = path->nodes[0];
4522 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
4523 write_extent_buffer(leaf, data, ptr, data_size);
4524 btrfs_mark_buffer_dirty(leaf);
Chris Mason62e27492007-03-15 12:56:47 -04004525 }
Chris Mason2c90e5d2007-04-02 10:50:19 -04004526 btrfs_free_path(path);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004527 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004528}
4529
Chris Mason74123bd2007-02-02 11:05:29 -05004530/*
Chris Mason5de08d72007-02-24 06:24:44 -05004531 * delete the pointer from a given node.
Chris Mason74123bd2007-02-02 11:05:29 -05004532 *
Chris Masond352ac62008-09-29 15:18:18 -04004533 * the tree should have been previously balanced so the deletion does not
4534 * empty a node.
Chris Mason74123bd2007-02-02 11:05:29 -05004535 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004536static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004537 struct btrfs_path *path, int level, int slot,
4538 int tree_mod_log)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004539{
Chris Mason5f39d392007-10-15 16:14:19 -04004540 struct extent_buffer *parent = path->nodes[level];
Chris Mason7518a232007-03-12 12:01:18 -04004541 u32 nritems;
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004542 int ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004543
Chris Mason5f39d392007-10-15 16:14:19 -04004544 nritems = btrfs_header_nritems(parent);
Chris Masond3977122009-01-05 21:25:51 -05004545 if (slot != nritems - 1) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004546 if (tree_mod_log && level)
4547 tree_mod_log_eb_move(root->fs_info, parent, slot,
4548 slot + 1, nritems - slot - 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004549 memmove_extent_buffer(parent,
4550 btrfs_node_key_ptr_offset(slot),
4551 btrfs_node_key_ptr_offset(slot + 1),
Chris Masond6025572007-03-30 14:27:56 -04004552 sizeof(struct btrfs_key_ptr) *
4553 (nritems - slot - 1));
Jan Schmidtf3956942012-05-31 15:02:32 +02004554 } else if (tree_mod_log && level) {
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004555 ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
4556 MOD_LOG_KEY_REMOVE);
4557 BUG_ON(ret < 0);
4558 }
4559
Chris Mason7518a232007-03-12 12:01:18 -04004560 nritems--;
Chris Mason5f39d392007-10-15 16:14:19 -04004561 btrfs_set_header_nritems(parent, nritems);
Chris Mason7518a232007-03-12 12:01:18 -04004562 if (nritems == 0 && parent == root->node) {
Chris Mason5f39d392007-10-15 16:14:19 -04004563 BUG_ON(btrfs_header_level(root->node) != 1);
Chris Masonbb803952007-03-01 12:04:21 -05004564 /* just turn the root into a leaf and break */
Chris Mason5f39d392007-10-15 16:14:19 -04004565 btrfs_set_header_level(root->node, 0);
Chris Masonbb803952007-03-01 12:04:21 -05004566 } else if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004567 struct btrfs_disk_key disk_key;
4568
4569 btrfs_node_key(parent, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004570 fixup_low_keys(trans, root, path, &disk_key, level + 1);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004571 }
Chris Masond6025572007-03-30 14:27:56 -04004572 btrfs_mark_buffer_dirty(parent);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004573}
4574
Chris Mason74123bd2007-02-02 11:05:29 -05004575/*
Chris Mason323ac952008-10-01 19:05:46 -04004576 * a helper function to delete the leaf pointed to by path->slots[1] and
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004577 * path->nodes[1].
Chris Mason323ac952008-10-01 19:05:46 -04004578 *
4579 * This deletes the pointer in path->nodes[1] and frees the leaf
4580 * block extent. zero is returned if it all worked out, < 0 otherwise.
4581 *
4582 * The path must have already been setup for deleting the leaf, including
4583 * all the proper balancing. path->nodes[1] must be locked.
4584 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01004585static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
4586 struct btrfs_root *root,
4587 struct btrfs_path *path,
4588 struct extent_buffer *leaf)
Chris Mason323ac952008-10-01 19:05:46 -04004589{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004590 WARN_ON(btrfs_header_generation(leaf) != trans->transid);
Jan Schmidtf3ea38d2012-05-26 11:45:21 +02004591 del_ptr(trans, root, path, 1, path->slots[1], 1);
Chris Mason323ac952008-10-01 19:05:46 -04004592
Chris Mason4d081c42009-02-04 09:31:28 -05004593 /*
4594 * btrfs_free_extent is expensive, we want to make sure we
4595 * aren't holding any locks when we call it
4596 */
4597 btrfs_unlock_up_safe(path, 0);
4598
Yan, Zhengf0486c62010-05-16 10:46:25 -04004599 root_sub_used(root, leaf->len);
4600
Josef Bacik3083ee22012-03-09 16:01:49 -05004601 extent_buffer_get(leaf);
Jan Schmidt5581a512012-05-16 17:04:52 +02004602 btrfs_free_tree_block(trans, root, leaf, 0, 1);
Josef Bacik3083ee22012-03-09 16:01:49 -05004603 free_extent_buffer_stale(leaf);
Chris Mason323ac952008-10-01 19:05:46 -04004604}
4605/*
Chris Mason74123bd2007-02-02 11:05:29 -05004606 * delete the item at the leaf level in path. If that empties
4607 * the leaf, remove it from the tree
4608 */
Chris Mason85e21ba2008-01-29 15:11:36 -05004609int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4610 struct btrfs_path *path, int slot, int nr)
Chris Masonbe0e5c02007-01-26 15:51:26 -05004611{
Chris Mason5f39d392007-10-15 16:14:19 -04004612 struct extent_buffer *leaf;
4613 struct btrfs_item *item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004614 int last_off;
4615 int dsize = 0;
Chris Masonaa5d6be2007-02-28 16:35:06 -05004616 int ret = 0;
4617 int wret;
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 int i;
Chris Mason7518a232007-03-12 12:01:18 -04004619 u32 nritems;
Chris Masoncfed81a2012-03-03 07:40:03 -05004620 struct btrfs_map_token token;
4621
4622 btrfs_init_map_token(&token);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004623
Chris Mason5f39d392007-10-15 16:14:19 -04004624 leaf = path->nodes[0];
Chris Mason85e21ba2008-01-29 15:11:36 -05004625 last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
4626
4627 for (i = 0; i < nr; i++)
4628 dsize += btrfs_item_size_nr(leaf, slot + i);
4629
Chris Mason5f39d392007-10-15 16:14:19 -04004630 nritems = btrfs_header_nritems(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004631
Chris Mason85e21ba2008-01-29 15:11:36 -05004632 if (slot + nr != nritems) {
Chris Mason123abc82007-03-14 14:14:43 -04004633 int data_end = leaf_data_end(root, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004634
4635 memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
Chris Masond6025572007-03-30 14:27:56 -04004636 data_end + dsize,
4637 btrfs_leaf_data(leaf) + data_end,
Chris Mason85e21ba2008-01-29 15:11:36 -05004638 last_off - data_end);
Chris Mason5f39d392007-10-15 16:14:19 -04004639
Chris Mason85e21ba2008-01-29 15:11:36 -05004640 for (i = slot + nr; i < nritems; i++) {
Chris Mason5f39d392007-10-15 16:14:19 -04004641 u32 ioff;
Chris Masondb945352007-10-15 16:15:53 -04004642
Chris Mason5f39d392007-10-15 16:14:19 -04004643 item = btrfs_item_nr(leaf, i);
Chris Masoncfed81a2012-03-03 07:40:03 -05004644 ioff = btrfs_token_item_offset(leaf, item, &token);
4645 btrfs_set_token_item_offset(leaf, item,
4646 ioff + dsize, &token);
Chris Mason0783fcf2007-03-12 20:12:07 -04004647 }
Chris Masondb945352007-10-15 16:15:53 -04004648
Chris Mason5f39d392007-10-15 16:14:19 -04004649 memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
Chris Mason85e21ba2008-01-29 15:11:36 -05004650 btrfs_item_nr_offset(slot + nr),
Chris Masond6025572007-03-30 14:27:56 -04004651 sizeof(struct btrfs_item) *
Chris Mason85e21ba2008-01-29 15:11:36 -05004652 (nritems - slot - nr));
Chris Masonbe0e5c02007-01-26 15:51:26 -05004653 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004654 btrfs_set_header_nritems(leaf, nritems - nr);
4655 nritems -= nr;
Chris Mason5f39d392007-10-15 16:14:19 -04004656
Chris Mason74123bd2007-02-02 11:05:29 -05004657 /* delete the leaf if we've emptied it */
Chris Mason7518a232007-03-12 12:01:18 -04004658 if (nritems == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004659 if (leaf == root->node) {
4660 btrfs_set_header_level(leaf, 0);
Chris Mason9a8dd152007-02-23 08:38:36 -05004661 } else {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004662 btrfs_set_path_blocking(path);
4663 clean_tree_block(trans, root, leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004664 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason9a8dd152007-02-23 08:38:36 -05004665 }
Chris Masonbe0e5c02007-01-26 15:51:26 -05004666 } else {
Chris Mason7518a232007-03-12 12:01:18 -04004667 int used = leaf_space_used(leaf, 0, nritems);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004668 if (slot == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04004669 struct btrfs_disk_key disk_key;
4670
4671 btrfs_item_key(leaf, &disk_key, 0);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004672 fixup_low_keys(trans, root, path, &disk_key, 1);
Chris Masonaa5d6be2007-02-28 16:35:06 -05004673 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004674
Chris Mason74123bd2007-02-02 11:05:29 -05004675 /* delete the leaf if it is mostly empty */
Yan Zhengd717aa12009-07-24 12:42:46 -04004676 if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
Chris Masonbe0e5c02007-01-26 15:51:26 -05004677 /* push_leaf_left fixes the path.
4678 * make sure the path still points to our leaf
4679 * for possible call to del_ptr below
4680 */
Chris Mason4920c9a2007-01-26 16:38:42 -05004681 slot = path->slots[1];
Chris Mason5f39d392007-10-15 16:14:19 -04004682 extent_buffer_get(leaf);
4683
Chris Masonb9473432009-03-13 11:00:37 -04004684 btrfs_set_path_blocking(path);
Chris Mason99d8f832010-07-07 10:51:48 -04004685 wret = push_leaf_left(trans, root, path, 1, 1,
4686 1, (u32)-1);
Chris Mason54aa1f42007-06-22 14:16:25 -04004687 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004688 ret = wret;
Chris Mason5f39d392007-10-15 16:14:19 -04004689
4690 if (path->nodes[0] == leaf &&
4691 btrfs_header_nritems(leaf)) {
Chris Mason99d8f832010-07-07 10:51:48 -04004692 wret = push_leaf_right(trans, root, path, 1,
4693 1, 1, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04004694 if (wret < 0 && wret != -ENOSPC)
Chris Masonaa5d6be2007-02-28 16:35:06 -05004695 ret = wret;
4696 }
Chris Mason5f39d392007-10-15 16:14:19 -04004697
4698 if (btrfs_header_nritems(leaf) == 0) {
Chris Mason323ac952008-10-01 19:05:46 -04004699 path->slots[1] = slot;
Jeff Mahoney143bede2012-03-01 14:56:26 +01004700 btrfs_del_leaf(trans, root, path, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004701 free_extent_buffer(leaf);
Jeff Mahoney143bede2012-03-01 14:56:26 +01004702 ret = 0;
Chris Mason5de08d72007-02-24 06:24:44 -05004703 } else {
Chris Mason925baed2008-06-25 16:01:30 -04004704 /* if we're still in the path, make sure
4705 * we're dirty. Otherwise, one of the
4706 * push_leaf functions must have already
4707 * dirtied this buffer
4708 */
4709 if (path->nodes[0] == leaf)
4710 btrfs_mark_buffer_dirty(leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04004711 free_extent_buffer(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004712 }
Chris Masond5719762007-03-23 10:01:08 -04004713 } else {
Chris Mason5f39d392007-10-15 16:14:19 -04004714 btrfs_mark_buffer_dirty(leaf);
Chris Masonbe0e5c02007-01-26 15:51:26 -05004715 }
4716 }
Chris Masonaa5d6be2007-02-28 16:35:06 -05004717 return ret;
Chris Masonbe0e5c02007-01-26 15:51:26 -05004718}
4719
Chris Mason97571fd2007-02-24 13:39:08 -05004720/*
Chris Mason925baed2008-06-25 16:01:30 -04004721 * search the tree again to find a leaf with lesser keys
Chris Mason7bb86312007-12-11 09:25:06 -05004722 * returns 0 if it found something or 1 if there are no lesser leaves.
4723 * returns < 0 on io errors.
Chris Masond352ac62008-09-29 15:18:18 -04004724 *
4725 * This may release the path, and so you may lose any locks held at the
4726 * time you call it.
Chris Mason7bb86312007-12-11 09:25:06 -05004727 */
4728int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
4729{
Chris Mason925baed2008-06-25 16:01:30 -04004730 struct btrfs_key key;
4731 struct btrfs_disk_key found_key;
4732 int ret;
Chris Mason7bb86312007-12-11 09:25:06 -05004733
Chris Mason925baed2008-06-25 16:01:30 -04004734 btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
Chris Mason7bb86312007-12-11 09:25:06 -05004735
Chris Mason925baed2008-06-25 16:01:30 -04004736 if (key.offset > 0)
4737 key.offset--;
4738 else if (key.type > 0)
4739 key.type--;
4740 else if (key.objectid > 0)
4741 key.objectid--;
4742 else
4743 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004744
David Sterbab3b4aa72011-04-21 01:20:15 +02004745 btrfs_release_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04004746 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4747 if (ret < 0)
4748 return ret;
4749 btrfs_item_key(path->nodes[0], &found_key, 0);
4750 ret = comp_keys(&found_key, &key);
4751 if (ret < 0)
4752 return 0;
4753 return 1;
Chris Mason7bb86312007-12-11 09:25:06 -05004754}
4755
Chris Mason3f157a22008-06-25 16:01:31 -04004756/*
4757 * A helper function to walk down the tree starting at min_key, and looking
4758 * for nodes or leaves that are either in cache or have a minimum
Chris Masond352ac62008-09-29 15:18:18 -04004759 * transaction id. This is used by the btree defrag code, and tree logging
Chris Mason3f157a22008-06-25 16:01:31 -04004760 *
4761 * This does not cow, but it does stuff the starting key it finds back
4762 * into min_key, so you can call btrfs_search_slot with cow=1 on the
4763 * key and get a writable path.
4764 *
4765 * This does lock as it descends, and path->keep_locks should be set
4766 * to 1 by the caller.
4767 *
4768 * This honors path->lowest_level to prevent descent past a given level
4769 * of the tree.
4770 *
Chris Masond352ac62008-09-29 15:18:18 -04004771 * min_trans indicates the oldest transaction that you are interested
4772 * in walking through. Any nodes or leaves older than min_trans are
4773 * skipped over (without reading them).
4774 *
Chris Mason3f157a22008-06-25 16:01:31 -04004775 * returns zero if something useful was found, < 0 on error and 1 if there
4776 * was nothing in the tree that matched the search criteria.
4777 */
4778int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
Chris Masone02119d2008-09-05 16:13:11 -04004779 struct btrfs_key *max_key,
Chris Mason3f157a22008-06-25 16:01:31 -04004780 struct btrfs_path *path, int cache_only,
4781 u64 min_trans)
4782{
4783 struct extent_buffer *cur;
4784 struct btrfs_key found_key;
4785 int slot;
Yan96524802008-07-24 12:19:49 -04004786 int sret;
Chris Mason3f157a22008-06-25 16:01:31 -04004787 u32 nritems;
4788 int level;
4789 int ret = 1;
4790
Chris Mason934d3752008-12-08 16:43:10 -05004791 WARN_ON(!path->keep_locks);
Chris Mason3f157a22008-06-25 16:01:31 -04004792again:
Chris Masonbd681512011-07-16 15:23:14 -04004793 cur = btrfs_read_lock_root_node(root);
Chris Mason3f157a22008-06-25 16:01:31 -04004794 level = btrfs_header_level(cur);
Chris Masone02119d2008-09-05 16:13:11 -04004795 WARN_ON(path->nodes[level]);
Chris Mason3f157a22008-06-25 16:01:31 -04004796 path->nodes[level] = cur;
Chris Masonbd681512011-07-16 15:23:14 -04004797 path->locks[level] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004798
4799 if (btrfs_header_generation(cur) < min_trans) {
4800 ret = 1;
4801 goto out;
4802 }
Chris Masond3977122009-01-05 21:25:51 -05004803 while (1) {
Chris Mason3f157a22008-06-25 16:01:31 -04004804 nritems = btrfs_header_nritems(cur);
4805 level = btrfs_header_level(cur);
Yan96524802008-07-24 12:19:49 -04004806 sret = bin_search(cur, min_key, level, &slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004807
Chris Mason323ac952008-10-01 19:05:46 -04004808 /* at the lowest level, we're done, setup the path and exit */
4809 if (level == path->lowest_level) {
Chris Masone02119d2008-09-05 16:13:11 -04004810 if (slot >= nritems)
4811 goto find_next_key;
Chris Mason3f157a22008-06-25 16:01:31 -04004812 ret = 0;
4813 path->slots[level] = slot;
4814 btrfs_item_key_to_cpu(cur, &found_key, slot);
4815 goto out;
4816 }
Yan96524802008-07-24 12:19:49 -04004817 if (sret && slot > 0)
4818 slot--;
Chris Mason3f157a22008-06-25 16:01:31 -04004819 /*
4820 * check this node pointer against the cache_only and
4821 * min_trans parameters. If it isn't in cache or is too
4822 * old, skip to the next one.
4823 */
Chris Masond3977122009-01-05 21:25:51 -05004824 while (slot < nritems) {
Chris Mason3f157a22008-06-25 16:01:31 -04004825 u64 blockptr;
4826 u64 gen;
4827 struct extent_buffer *tmp;
Chris Masone02119d2008-09-05 16:13:11 -04004828 struct btrfs_disk_key disk_key;
4829
Chris Mason3f157a22008-06-25 16:01:31 -04004830 blockptr = btrfs_node_blockptr(cur, slot);
4831 gen = btrfs_node_ptr_generation(cur, slot);
4832 if (gen < min_trans) {
4833 slot++;
4834 continue;
4835 }
4836 if (!cache_only)
4837 break;
4838
Chris Masone02119d2008-09-05 16:13:11 -04004839 if (max_key) {
4840 btrfs_node_key(cur, &disk_key, slot);
4841 if (comp_keys(&disk_key, max_key) >= 0) {
4842 ret = 1;
4843 goto out;
4844 }
4845 }
4846
Chris Mason3f157a22008-06-25 16:01:31 -04004847 tmp = btrfs_find_tree_block(root, blockptr,
4848 btrfs_level_size(root, level - 1));
4849
Chris Masonb9fab912012-05-06 07:23:47 -04004850 if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004851 free_extent_buffer(tmp);
4852 break;
4853 }
4854 if (tmp)
4855 free_extent_buffer(tmp);
4856 slot++;
4857 }
Chris Masone02119d2008-09-05 16:13:11 -04004858find_next_key:
Chris Mason3f157a22008-06-25 16:01:31 -04004859 /*
4860 * we didn't find a candidate key in this node, walk forward
4861 * and find another one
4862 */
4863 if (slot >= nritems) {
Chris Masone02119d2008-09-05 16:13:11 -04004864 path->slots[level] = slot;
Chris Masonb4ce94d2009-02-04 09:25:08 -05004865 btrfs_set_path_blocking(path);
Chris Masone02119d2008-09-05 16:13:11 -04004866 sret = btrfs_find_next_key(root, path, min_key, level,
Chris Mason3f157a22008-06-25 16:01:31 -04004867 cache_only, min_trans);
Chris Masone02119d2008-09-05 16:13:11 -04004868 if (sret == 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02004869 btrfs_release_path(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004870 goto again;
4871 } else {
4872 goto out;
4873 }
4874 }
4875 /* save our key for returning back */
4876 btrfs_node_key_to_cpu(cur, &found_key, slot);
4877 path->slots[level] = slot;
4878 if (level == path->lowest_level) {
4879 ret = 0;
Chris Masonf7c79f32012-03-19 15:54:38 -04004880 unlock_up(path, level, 1, 0, NULL);
Chris Mason3f157a22008-06-25 16:01:31 -04004881 goto out;
4882 }
Chris Masonb4ce94d2009-02-04 09:25:08 -05004883 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004884 cur = read_node_slot(root, cur, slot);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004885 BUG_ON(!cur); /* -ENOMEM */
Chris Mason3f157a22008-06-25 16:01:31 -04004886
Chris Masonbd681512011-07-16 15:23:14 -04004887 btrfs_tree_read_lock(cur);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004888
Chris Masonbd681512011-07-16 15:23:14 -04004889 path->locks[level - 1] = BTRFS_READ_LOCK;
Chris Mason3f157a22008-06-25 16:01:31 -04004890 path->nodes[level - 1] = cur;
Chris Masonf7c79f32012-03-19 15:54:38 -04004891 unlock_up(path, level, 1, 0, NULL);
Chris Masonbd681512011-07-16 15:23:14 -04004892 btrfs_clear_path_blocking(path, NULL, 0);
Chris Mason3f157a22008-06-25 16:01:31 -04004893 }
4894out:
4895 if (ret == 0)
4896 memcpy(min_key, &found_key, sizeof(found_key));
Chris Masonb4ce94d2009-02-04 09:25:08 -05004897 btrfs_set_path_blocking(path);
Chris Mason3f157a22008-06-25 16:01:31 -04004898 return ret;
4899}
4900
4901/*
4902 * this is similar to btrfs_next_leaf, but does not try to preserve
4903 * and fixup the path. It looks for and returns the next key in the
4904 * tree based on the current path and the cache_only and min_trans
4905 * parameters.
4906 *
4907 * 0 is returned if another key is found, < 0 if there are any errors
4908 * and 1 is returned if there are no higher keys in the tree
4909 *
4910 * path->keep_locks should be set to 1 on the search made before
4911 * calling this function.
4912 */
Chris Masone7a84562008-06-25 16:01:31 -04004913int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
Yan Zheng33c66f42009-07-22 09:59:00 -04004914 struct btrfs_key *key, int level,
Chris Mason3f157a22008-06-25 16:01:31 -04004915 int cache_only, u64 min_trans)
Chris Masone7a84562008-06-25 16:01:31 -04004916{
Chris Masone7a84562008-06-25 16:01:31 -04004917 int slot;
4918 struct extent_buffer *c;
4919
Chris Mason934d3752008-12-08 16:43:10 -05004920 WARN_ON(!path->keep_locks);
Chris Masond3977122009-01-05 21:25:51 -05004921 while (level < BTRFS_MAX_LEVEL) {
Chris Masone7a84562008-06-25 16:01:31 -04004922 if (!path->nodes[level])
4923 return 1;
4924
4925 slot = path->slots[level] + 1;
4926 c = path->nodes[level];
Chris Mason3f157a22008-06-25 16:01:31 -04004927next:
Chris Masone7a84562008-06-25 16:01:31 -04004928 if (slot >= btrfs_header_nritems(c)) {
Yan Zheng33c66f42009-07-22 09:59:00 -04004929 int ret;
4930 int orig_lowest;
4931 struct btrfs_key cur_key;
4932 if (level + 1 >= BTRFS_MAX_LEVEL ||
4933 !path->nodes[level + 1])
Chris Masone7a84562008-06-25 16:01:31 -04004934 return 1;
Yan Zheng33c66f42009-07-22 09:59:00 -04004935
4936 if (path->locks[level + 1]) {
4937 level++;
4938 continue;
4939 }
4940
4941 slot = btrfs_header_nritems(c) - 1;
4942 if (level == 0)
4943 btrfs_item_key_to_cpu(c, &cur_key, slot);
4944 else
4945 btrfs_node_key_to_cpu(c, &cur_key, slot);
4946
4947 orig_lowest = path->lowest_level;
David Sterbab3b4aa72011-04-21 01:20:15 +02004948 btrfs_release_path(path);
Yan Zheng33c66f42009-07-22 09:59:00 -04004949 path->lowest_level = level;
4950 ret = btrfs_search_slot(NULL, root, &cur_key, path,
4951 0, 0);
4952 path->lowest_level = orig_lowest;
4953 if (ret < 0)
4954 return ret;
4955
4956 c = path->nodes[level];
4957 slot = path->slots[level];
4958 if (ret == 0)
4959 slot++;
4960 goto next;
Chris Masone7a84562008-06-25 16:01:31 -04004961 }
Yan Zheng33c66f42009-07-22 09:59:00 -04004962
Chris Masone7a84562008-06-25 16:01:31 -04004963 if (level == 0)
4964 btrfs_item_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004965 else {
4966 u64 blockptr = btrfs_node_blockptr(c, slot);
4967 u64 gen = btrfs_node_ptr_generation(c, slot);
4968
4969 if (cache_only) {
4970 struct extent_buffer *cur;
4971 cur = btrfs_find_tree_block(root, blockptr,
4972 btrfs_level_size(root, level - 1));
Chris Masonb9fab912012-05-06 07:23:47 -04004973 if (!cur ||
4974 btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
Chris Mason3f157a22008-06-25 16:01:31 -04004975 slot++;
4976 if (cur)
4977 free_extent_buffer(cur);
4978 goto next;
4979 }
4980 free_extent_buffer(cur);
4981 }
4982 if (gen < min_trans) {
4983 slot++;
4984 goto next;
4985 }
Chris Masone7a84562008-06-25 16:01:31 -04004986 btrfs_node_key_to_cpu(c, key, slot);
Chris Mason3f157a22008-06-25 16:01:31 -04004987 }
Chris Masone7a84562008-06-25 16:01:31 -04004988 return 0;
4989 }
4990 return 1;
4991}
4992
Chris Mason7bb86312007-12-11 09:25:06 -05004993/*
Chris Mason925baed2008-06-25 16:01:30 -04004994 * search the tree again to find a leaf with greater keys
Chris Mason0f70abe2007-02-28 16:46:22 -05004995 * returns 0 if it found something or 1 if there are no greater leaves.
4996 * returns < 0 on io errors.
Chris Mason97571fd2007-02-24 13:39:08 -05004997 */
Chris Mason234b63a2007-03-13 10:46:10 -04004998int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
Chris Masond97e63b2007-02-20 16:40:44 -05004999{
5000 int slot;
Chris Mason8e73f272009-04-03 10:14:18 -04005001 int level;
Chris Mason5f39d392007-10-15 16:14:19 -04005002 struct extent_buffer *c;
Chris Mason8e73f272009-04-03 10:14:18 -04005003 struct extent_buffer *next;
Chris Mason925baed2008-06-25 16:01:30 -04005004 struct btrfs_key key;
5005 u32 nritems;
5006 int ret;
Chris Mason8e73f272009-04-03 10:14:18 -04005007 int old_spinning = path->leave_spinning;
Chris Masonbd681512011-07-16 15:23:14 -04005008 int next_rw_lock = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005009
5010 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05005011 if (nritems == 0)
Chris Mason925baed2008-06-25 16:01:30 -04005012 return 1;
Chris Mason925baed2008-06-25 16:01:30 -04005013
Chris Mason8e73f272009-04-03 10:14:18 -04005014 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
5015again:
5016 level = 1;
5017 next = NULL;
Chris Masonbd681512011-07-16 15:23:14 -04005018 next_rw_lock = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02005019 btrfs_release_path(path);
Chris Mason8e73f272009-04-03 10:14:18 -04005020
Chris Masona2135012008-06-25 16:01:30 -04005021 path->keep_locks = 1;
Chris Mason31533fb2011-07-26 16:01:59 -04005022 path->leave_spinning = 1;
Chris Mason8e73f272009-04-03 10:14:18 -04005023
Chris Mason925baed2008-06-25 16:01:30 -04005024 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5025 path->keep_locks = 0;
5026
5027 if (ret < 0)
5028 return ret;
5029
Chris Masona2135012008-06-25 16:01:30 -04005030 nritems = btrfs_header_nritems(path->nodes[0]);
Chris Mason168fd7d2008-06-25 16:01:30 -04005031 /*
5032 * by releasing the path above we dropped all our locks. A balance
5033 * could have added more items next to the key that used to be
5034 * at the very end of the block. So, check again here and
5035 * advance the path if there are now more items available.
5036 */
Chris Masona2135012008-06-25 16:01:30 -04005037 if (nritems > 0 && path->slots[0] < nritems - 1) {
Yan Zhenge457afe2009-07-22 09:59:00 -04005038 if (ret == 0)
5039 path->slots[0]++;
Chris Mason8e73f272009-04-03 10:14:18 -04005040 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005041 goto done;
5042 }
Chris Masond97e63b2007-02-20 16:40:44 -05005043
Chris Masond3977122009-01-05 21:25:51 -05005044 while (level < BTRFS_MAX_LEVEL) {
Chris Mason8e73f272009-04-03 10:14:18 -04005045 if (!path->nodes[level]) {
5046 ret = 1;
5047 goto done;
5048 }
Chris Mason5f39d392007-10-15 16:14:19 -04005049
Chris Masond97e63b2007-02-20 16:40:44 -05005050 slot = path->slots[level] + 1;
5051 c = path->nodes[level];
Chris Mason5f39d392007-10-15 16:14:19 -04005052 if (slot >= btrfs_header_nritems(c)) {
Chris Masond97e63b2007-02-20 16:40:44 -05005053 level++;
Chris Mason8e73f272009-04-03 10:14:18 -04005054 if (level == BTRFS_MAX_LEVEL) {
5055 ret = 1;
5056 goto done;
5057 }
Chris Masond97e63b2007-02-20 16:40:44 -05005058 continue;
5059 }
Chris Mason5f39d392007-10-15 16:14:19 -04005060
Chris Mason925baed2008-06-25 16:01:30 -04005061 if (next) {
Chris Masonbd681512011-07-16 15:23:14 -04005062 btrfs_tree_unlock_rw(next, next_rw_lock);
Chris Mason5f39d392007-10-15 16:14:19 -04005063 free_extent_buffer(next);
Chris Mason925baed2008-06-25 16:01:30 -04005064 }
Chris Mason5f39d392007-10-15 16:14:19 -04005065
Chris Mason8e73f272009-04-03 10:14:18 -04005066 next = c;
Chris Masonbd681512011-07-16 15:23:14 -04005067 next_rw_lock = path->locks[level];
Chris Mason8e73f272009-04-03 10:14:18 -04005068 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005069 slot, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005070 if (ret == -EAGAIN)
5071 goto again;
Chris Mason5f39d392007-10-15 16:14:19 -04005072
Chris Mason76a05b32009-05-14 13:24:30 -04005073 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005074 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005075 goto done;
5076 }
5077
Chris Mason5cd57b22008-06-25 16:01:30 -04005078 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005079 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005080 if (!ret) {
5081 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005082 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005083 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005084 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005085 }
Chris Mason31533fb2011-07-26 16:01:59 -04005086 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005087 }
Chris Masond97e63b2007-02-20 16:40:44 -05005088 break;
5089 }
5090 path->slots[level] = slot;
Chris Masond3977122009-01-05 21:25:51 -05005091 while (1) {
Chris Masond97e63b2007-02-20 16:40:44 -05005092 level--;
5093 c = path->nodes[level];
Chris Mason925baed2008-06-25 16:01:30 -04005094 if (path->locks[level])
Chris Masonbd681512011-07-16 15:23:14 -04005095 btrfs_tree_unlock_rw(c, path->locks[level]);
Chris Mason8e73f272009-04-03 10:14:18 -04005096
Chris Mason5f39d392007-10-15 16:14:19 -04005097 free_extent_buffer(c);
Chris Masond97e63b2007-02-20 16:40:44 -05005098 path->nodes[level] = next;
5099 path->slots[level] = 0;
Chris Masona74a4b92008-06-25 16:01:31 -04005100 if (!path->skip_locking)
Chris Masonbd681512011-07-16 15:23:14 -04005101 path->locks[level] = next_rw_lock;
Chris Masond97e63b2007-02-20 16:40:44 -05005102 if (!level)
5103 break;
Chris Masonb4ce94d2009-02-04 09:25:08 -05005104
Chris Mason8e73f272009-04-03 10:14:18 -04005105 ret = read_block_for_search(NULL, root, path, &next, level,
Jan Schmidt5d9e75c42012-05-16 18:25:47 +02005106 0, &key, 0);
Chris Mason8e73f272009-04-03 10:14:18 -04005107 if (ret == -EAGAIN)
5108 goto again;
5109
Chris Mason76a05b32009-05-14 13:24:30 -04005110 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +02005111 btrfs_release_path(path);
Chris Mason76a05b32009-05-14 13:24:30 -04005112 goto done;
5113 }
5114
Chris Mason5cd57b22008-06-25 16:01:30 -04005115 if (!path->skip_locking) {
Chris Masonbd681512011-07-16 15:23:14 -04005116 ret = btrfs_try_tree_read_lock(next);
Chris Mason8e73f272009-04-03 10:14:18 -04005117 if (!ret) {
5118 btrfs_set_path_blocking(path);
Chris Masonbd681512011-07-16 15:23:14 -04005119 btrfs_tree_read_lock(next);
Chris Mason31533fb2011-07-26 16:01:59 -04005120 btrfs_clear_path_blocking(path, next,
Chris Masonbd681512011-07-16 15:23:14 -04005121 BTRFS_READ_LOCK);
Chris Mason8e73f272009-04-03 10:14:18 -04005122 }
Chris Mason31533fb2011-07-26 16:01:59 -04005123 next_rw_lock = BTRFS_READ_LOCK;
Chris Mason5cd57b22008-06-25 16:01:30 -04005124 }
Chris Masond97e63b2007-02-20 16:40:44 -05005125 }
Chris Mason8e73f272009-04-03 10:14:18 -04005126 ret = 0;
Chris Mason925baed2008-06-25 16:01:30 -04005127done:
Chris Masonf7c79f32012-03-19 15:54:38 -04005128 unlock_up(path, 0, 1, 0, NULL);
Chris Mason8e73f272009-04-03 10:14:18 -04005129 path->leave_spinning = old_spinning;
5130 if (!old_spinning)
5131 btrfs_set_path_blocking(path);
5132
5133 return ret;
Chris Masond97e63b2007-02-20 16:40:44 -05005134}
Chris Mason0b86a832008-03-24 15:01:56 -04005135
Chris Mason3f157a22008-06-25 16:01:31 -04005136/*
5137 * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
5138 * searching until it gets past min_objectid or finds an item of 'type'
5139 *
5140 * returns 0 if something is found, 1 if nothing was found and < 0 on error
5141 */
Chris Mason0b86a832008-03-24 15:01:56 -04005142int btrfs_previous_item(struct btrfs_root *root,
5143 struct btrfs_path *path, u64 min_objectid,
5144 int type)
5145{
5146 struct btrfs_key found_key;
5147 struct extent_buffer *leaf;
Chris Masone02119d2008-09-05 16:13:11 -04005148 u32 nritems;
Chris Mason0b86a832008-03-24 15:01:56 -04005149 int ret;
5150
Chris Masond3977122009-01-05 21:25:51 -05005151 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005152 if (path->slots[0] == 0) {
Chris Masonb4ce94d2009-02-04 09:25:08 -05005153 btrfs_set_path_blocking(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005154 ret = btrfs_prev_leaf(root, path);
5155 if (ret != 0)
5156 return ret;
5157 } else {
5158 path->slots[0]--;
5159 }
5160 leaf = path->nodes[0];
Chris Masone02119d2008-09-05 16:13:11 -04005161 nritems = btrfs_header_nritems(leaf);
5162 if (nritems == 0)
5163 return 1;
5164 if (path->slots[0] == nritems)
5165 path->slots[0]--;
5166
Chris Mason0b86a832008-03-24 15:01:56 -04005167 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masone02119d2008-09-05 16:13:11 -04005168 if (found_key.objectid < min_objectid)
5169 break;
Yan Zheng0a4eefb2009-07-24 11:06:53 -04005170 if (found_key.type == type)
5171 return 0;
Chris Masone02119d2008-09-05 16:13:11 -04005172 if (found_key.objectid == min_objectid &&
5173 found_key.type < type)
5174 break;
Chris Mason0b86a832008-03-24 15:01:56 -04005175 }
5176 return 1;
5177}